c4597ec3c1aa0ec9566d16be84069dc8e9a2ebe2
[openwrt/openwrt.git] /
1 From 5da331581bb2a8d90f6a7a7bdf8c1b30efe3cf64 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] Adding bcm2835-sdhost driver, and an overlay to enable it
5
6 BCM2835 has two SD card interfaces. This driver uses the other one.
7
8 bcm2835-sdhost: Error handling fix, and code clarification
9
10 bcm2835-sdhost: Adding overclocking option
11
12 Allow a different clock speed to be substitued for a requested 50MHz.
13 This option is exposed using the "overclock_50" DT parameter.
14 Note that the sdhost interface is restricted to integer divisions of
15 core_freq, and the highest sensible option for a core_freq of 250MHz
16 is 84 (250/3 = 83.3MHz), the next being 125 (250/2) which is much too
17 high.
18
19 Use at your own risk.
20
21 bcm2835-sdhost: Round up the overclock, so 62 works for 62.5Mhz
22
23 Also only warn once for each overclock setting.
24
25 bcm2835-sdhost: Improve error handling and recovery
26
27 1) Expose the hw_reset method to the MMC framework, removing many
28 internal calls by the driver.
29
30 2) Reduce overclock setting on error.
31
32 3) Increase timeout to cope with high capacity cards.
33
34 4) Add properties and parameters to control pio_limit and debug.
35
36 5) Reduce messages at probe time.
37
38 bcm2835-sdhost: Further improve overclock back-off
39
40 bcm2835-sdhost: Clear HBLC for PIO mode
41
42 Also update pio_limit default in overlay README.
43
44 bcm2835-sdhost: Add the ERASE capability
45
46 See: https://github.com/raspberrypi/linux/issues/1076
47
48 bcm2835-sdhost: Ignore CRC7 for MMC CMD1
49
50 It seems that the sdhost interface returns CRC7 errors for CMD1,
51 which is the MMC-specific SEND_OP_COND. Returning these errors to
52 the MMC layer causes a downward spiral, but ignoring them seems
53 to be harmless.
54
55 bcm2835-mmc/sdhost: Remove ARCH_BCM2835 differences
56
57 The bcm2835-mmc driver (and -sdhost driver that copied from it)
58 contains code to handle SDIO interrupts in a threaded interrupt
59 handler rather than waking the MMC framework thread. The change
60 follows a patch from Russell King that adds the facility as the
61 preferred way of working.
62
63 However, the new code path is only present in ARCH_BCM2835
64 builds, which I have taken to be a way of testing the waters
65 rather than making the change across the board; I can't see
66 any technical reason why it wouldn't be enabled for MACH_BCM270X
67 builds. So this patch standardises on the ARCH_BCM2835 code,
68 removing the old code paths.
69
70 bcm2835-sdhost: Don't log timeout errors unless debug=1
71
72 The MMC card-discovery process generates timeouts. This is
73 expected behaviour, so reporting it to the user serves no purpose.
74 Suppress the reporting of timeout errors unless the debug flag
75 is on.
76
77 bcm2835-sdhost: Add workaround for odd behaviour on some cards
78
79 For reasons not understood, the sdhost driver fails when reading
80 sectors very near the end of some SD cards. The problem could
81 be related to the similar issue that reading the final sector
82 of any card as part of a multiple read never completes, and the
83 workaround is an extension of the mechanism introduced to solve
84 that problem which ensures those sectors are always read singly.
85
86 bcm2835-sdhost: Major revision
87
88 This is a significant revision of the bcm2835-sdhost driver. It
89 improves on the original in a number of ways:
90
91 1) Through the use of CMD23 for reads it appears to avoid problems
92 reading some sectors on certain high speed cards.
93 2) Better atomicity to prevent crashes.
94 3) Higher performance.
95 4) Activity logging included, for easier diagnosis in the event
96 of a problem.
97
98 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
99
100 bcm2835-sdhost: Restore ATOMIC flag to PIO sg mapping
101
102 Allocation problems have been seen in a wireless driver, and
103 this is the only change which might have been responsible.
104
105 SQUASH: bcm2835-sdhost: Only claim one DMA channel
106
107 With both MMC controllers enabled there are few DMA channels left. The
108 bcm2835-sdhost driver only uses DMA in one direction at a time, so it
109 doesn't need to claim two channels.
110
111 See: https://github.com/raspberrypi/linux/issues/1327
112
113 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
114
115 bcm2835-sdhost: Workaround for "slow" sectors
116
117 Some cards have been seen to cause timeouts after certain sectors are
118 read. This workaround enforces a minimum delay between the stop after
119 reading one of those sectors and a subsequent data command.
120
121 Using CMD23 (SET_BLOCK_COUNT) avoids this problem, so good cards will
122 not be penalised by this workaround.
123
124 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
125
126 bcm2835-sdhost: Firmware manages the clock divisor
127
128 The bcm2835-sdhost driver hands control of the CDIV clock divisor
129 register to matching firmware, allowing it to adjust to a changing
130 core clock. This removes the need to use the performance governor or
131 to enable io_is_busy on the on-demand governor in order to get the
132 best SD performance.
133
134 N.B. As SD clocks must be an integer divisor of the core clock, it is
135 possible that the SD clock for "turbo" mode can be different (even
136 lower) than "normal" mode.
137
138 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
139
140 bcm2835-sdhost: Reset the clock in task context
141
142 Since reprogramming the clock can now involve a round-trip to the
143 firmware it must not be done at atomic context, and a tasklet
144 is not a task.
145
146 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
147
148 bcm2835-sdhost: Don't exit cmd wait loop on error
149
150 The FAIL flag can be set in the CMD register before command processing
151 is complete, leading to spurious "failed to complete" errors. This has
152 the effect of promoting harmless CRC7 errors during CMD1 processing
153 into errors that can delay and even prevent booting.
154
155 Also:
156 1) Convert the last KERN_ERROR message in the register dumping to
157 KERN_INFO.
158 2) Remove an unnecessary reset call from bcm2835_sdhost_add_host.
159
160 See: https://github.com/raspberrypi/linux/pull/1492
161
162 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
163 ---
164 drivers/mmc/host/Kconfig | 12 +-
165 drivers/mmc/host/Makefile | 1 +
166 drivers/mmc/host/bcm2835-sdhost.c | 2189 +++++++++++++++++++++++++++++++++++++
167 3 files changed, 2201 insertions(+), 1 deletion(-)
168 create mode 100644 drivers/mmc/host/bcm2835-sdhost.c
169
170 --- a/drivers/mmc/host/Kconfig
171 +++ b/drivers/mmc/host/Kconfig
172 @@ -6,7 +6,7 @@ comment "MMC/SD/SDIO Host Controller Dri
173
174 config MMC_BCM2835
175 tristate "MMC support on BCM2835"
176 - depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
177 + depends on ARCH_BCM2835
178 help
179 This selects the MMC Interface on BCM2835.
180
181 @@ -33,6 +33,16 @@ config MMC_BCM2835_PIO_DMA_BARRIER
182
183 If unsure, say 2 here.
184
185 +config MMC_BCM2835_SDHOST
186 + tristate "Support for the SDHost controller on BCM2708/9"
187 + depends on ARCH_BCM2835
188 + help
189 + This selects the SDHost controller on BCM2835/6.
190 +
191 + If you have a controller with this interface, say Y or M here.
192 +
193 + If unsure, say N.
194 +
195 config MMC_ARMMMCI
196 tristate "ARM AMBA Multimedia Card Interface support"
197 depends on ARM_AMBA
198 --- a/drivers/mmc/host/Makefile
199 +++ b/drivers/mmc/host/Makefile
200 @@ -18,6 +18,7 @@ obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c
201 obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
202 obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci_f_sdh30.o
203 obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
204 +obj-$(CONFIG_MMC_BCM2835_SDHOST) += bcm2835-sdhost.o
205 obj-$(CONFIG_MMC_BCM2835) += bcm2835-mmc.o
206 obj-$(CONFIG_MMC_WBSD) += wbsd.o
207 obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
208 --- /dev/null
209 +++ b/drivers/mmc/host/bcm2835-sdhost.c
210 @@ -0,0 +1,2189 @@
211 +/*
212 + * BCM2835 SD host driver.
213 + *
214 + * Author: Phil Elwell <phil@raspberrypi.org>
215 + * Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd.
216 + *
217 + * Based on
218 + * mmc-bcm2835.c by Gellert Weisz
219 + * which is, in turn, based on
220 + * sdhci-bcm2708.c by Broadcom
221 + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
222 + * sdhci.c and sdhci-pci.c by Pierre Ossman
223 + *
224 + * This program is free software; you can redistribute it and/or modify it
225 + * under the terms and conditions of the GNU General Public License,
226 + * version 2, as published by the Free Software Foundation.
227 + *
228 + * This program is distributed in the hope it will be useful, but WITHOUT
229 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
230 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
231 + * more details.
232 + *
233 + * You should have received a copy of the GNU General Public License
234 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
235 + */
236 +
237 +#define FIFO_READ_THRESHOLD 4
238 +#define FIFO_WRITE_THRESHOLD 4
239 +#define ALLOW_CMD23_READ 1
240 +#define ALLOW_CMD23_WRITE 0
241 +#define ENABLE_LOG 1
242 +#define SDDATA_FIFO_PIO_BURST 8
243 +#define CMD_DALLY_US 1
244 +
245 +#include <linux/delay.h>
246 +#include <linux/module.h>
247 +#include <linux/io.h>
248 +#include <linux/mmc/mmc.h>
249 +#include <linux/mmc/host.h>
250 +#include <linux/mmc/sd.h>
251 +#include <linux/mmc/sdio.h>
252 +#include <linux/scatterlist.h>
253 +#include <linux/of_address.h>
254 +#include <linux/of_irq.h>
255 +#include <linux/clk.h>
256 +#include <linux/platform_device.h>
257 +#include <linux/err.h>
258 +#include <linux/blkdev.h>
259 +#include <linux/dmaengine.h>
260 +#include <linux/dma-mapping.h>
261 +#include <linux/of_dma.h>
262 +#include <linux/time.h>
263 +#include <linux/workqueue.h>
264 +#include <soc/bcm2835/raspberrypi-firmware.h>
265 +
266 +#define DRIVER_NAME "sdhost-bcm2835"
267 +
268 +#define SDCMD 0x00 /* Command to SD card - 16 R/W */
269 +#define SDARG 0x04 /* Argument to SD card - 32 R/W */
270 +#define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */
271 +#define SDCDIV 0x0c /* Start value for clock divider - 11 R/W */
272 +#define SDRSP0 0x10 /* SD card response (31:0) - 32 R */
273 +#define SDRSP1 0x14 /* SD card response (63:32) - 32 R */
274 +#define SDRSP2 0x18 /* SD card response (95:64) - 32 R */
275 +#define SDRSP3 0x1c /* SD card response (127:96) - 32 R */
276 +#define SDHSTS 0x20 /* SD host status - 11 R */
277 +#define SDVDD 0x30 /* SD card power control - 1 R/W */
278 +#define SDEDM 0x34 /* Emergency Debug Mode - 13 R/W */
279 +#define SDHCFG 0x38 /* Host configuration - 2 R/W */
280 +#define SDHBCT 0x3c /* Host byte count (debug) - 32 R/W */
281 +#define SDDATA 0x40 /* Data to/from SD card - 32 R/W */
282 +#define SDHBLC 0x50 /* Host block count (SDIO/SDHC) - 9 R/W */
283 +
284 +#define SDCMD_NEW_FLAG 0x8000
285 +#define SDCMD_FAIL_FLAG 0x4000
286 +#define SDCMD_BUSYWAIT 0x800
287 +#define SDCMD_NO_RESPONSE 0x400
288 +#define SDCMD_LONG_RESPONSE 0x200
289 +#define SDCMD_WRITE_CMD 0x80
290 +#define SDCMD_READ_CMD 0x40
291 +#define SDCMD_CMD_MASK 0x3f
292 +
293 +#define SDCDIV_MAX_CDIV 0x7ff
294 +
295 +#define SDHSTS_BUSY_IRPT 0x400
296 +#define SDHSTS_BLOCK_IRPT 0x200
297 +#define SDHSTS_SDIO_IRPT 0x100
298 +#define SDHSTS_REW_TIME_OUT 0x80
299 +#define SDHSTS_CMD_TIME_OUT 0x40
300 +#define SDHSTS_CRC16_ERROR 0x20
301 +#define SDHSTS_CRC7_ERROR 0x10
302 +#define SDHSTS_FIFO_ERROR 0x08
303 +/* Reserved */
304 +/* Reserved */
305 +#define SDHSTS_DATA_FLAG 0x01
306 +
307 +#define SDHSTS_TRANSFER_ERROR_MASK (SDHSTS_CRC7_ERROR|SDHSTS_CRC16_ERROR|SDHSTS_REW_TIME_OUT|SDHSTS_FIFO_ERROR)
308 +#define SDHSTS_ERROR_MASK (SDHSTS_CMD_TIME_OUT|SDHSTS_TRANSFER_ERROR_MASK)
309 +
310 +#define SDHCFG_BUSY_IRPT_EN (1<<10)
311 +#define SDHCFG_BLOCK_IRPT_EN (1<<8)
312 +#define SDHCFG_SDIO_IRPT_EN (1<<5)
313 +#define SDHCFG_DATA_IRPT_EN (1<<4)
314 +#define SDHCFG_SLOW_CARD (1<<3)
315 +#define SDHCFG_WIDE_EXT_BUS (1<<2)
316 +#define SDHCFG_WIDE_INT_BUS (1<<1)
317 +#define SDHCFG_REL_CMD_LINE (1<<0)
318 +
319 +#define SDEDM_FORCE_DATA_MODE (1<<19)
320 +#define SDEDM_CLOCK_PULSE (1<<20)
321 +#define SDEDM_BYPASS (1<<21)
322 +
323 +#define SDEDM_WRITE_THRESHOLD_SHIFT 9
324 +#define SDEDM_READ_THRESHOLD_SHIFT 14
325 +#define SDEDM_THRESHOLD_MASK 0x1f
326 +
327 +#define SDEDM_FSM_MASK 0xf
328 +#define SDEDM_FSM_IDENTMODE 0x0
329 +#define SDEDM_FSM_DATAMODE 0x1
330 +#define SDEDM_FSM_READDATA 0x2
331 +#define SDEDM_FSM_WRITEDATA 0x3
332 +#define SDEDM_FSM_READWAIT 0x4
333 +#define SDEDM_FSM_READCRC 0x5
334 +#define SDEDM_FSM_WRITECRC 0x6
335 +#define SDEDM_FSM_WRITEWAIT1 0x7
336 +#define SDEDM_FSM_POWERDOWN 0x8
337 +#define SDEDM_FSM_POWERUP 0x9
338 +#define SDEDM_FSM_WRITESTART1 0xa
339 +#define SDEDM_FSM_WRITESTART2 0xb
340 +#define SDEDM_FSM_GENPULSES 0xc
341 +#define SDEDM_FSM_WRITEWAIT2 0xd
342 +#define SDEDM_FSM_STARTPOWDOWN 0xf
343 +
344 +#define SDDATA_FIFO_WORDS 16
345 +
346 +#define USE_CMD23_FLAGS ((ALLOW_CMD23_READ * MMC_DATA_READ) | \
347 + (ALLOW_CMD23_WRITE * MMC_DATA_WRITE))
348 +
349 +#define MHZ 1000000
350 +
351 +
352 +struct bcm2835_host {
353 + spinlock_t lock;
354 +
355 + void __iomem *ioaddr;
356 + u32 bus_addr;
357 +
358 + struct mmc_host *mmc;
359 +
360 + u32 pio_timeout; /* In jiffies */
361 +
362 + int clock; /* Current clock speed */
363 +
364 + bool slow_card; /* Force 11-bit divisor */
365 +
366 + unsigned int max_clk; /* Max possible freq */
367 +
368 + struct tasklet_struct finish_tasklet; /* Tasklet structures */
369 +
370 + struct work_struct cmd_wait_wq; /* Workqueue function */
371 +
372 + struct timer_list timer; /* Timer for timeouts */
373 +
374 + struct sg_mapping_iter sg_miter; /* SG state for PIO */
375 + unsigned int blocks; /* remaining PIO blocks */
376 +
377 + int irq; /* Device IRQ */
378 +
379 + u32 cmd_quick_poll_retries;
380 + u32 ns_per_fifo_word;
381 +
382 + /* cached registers */
383 + u32 hcfg;
384 + u32 cdiv;
385 +
386 + struct mmc_request *mrq; /* Current request */
387 + struct mmc_command *cmd; /* Current command */
388 + struct mmc_data *data; /* Current data request */
389 + unsigned int data_complete:1; /* Data finished before cmd */
390 +
391 + unsigned int flush_fifo:1; /* Drain the fifo when finishing */
392 +
393 + unsigned int use_busy:1; /* Wait for busy interrupt */
394 +
395 + unsigned int use_sbc:1; /* Send CMD23 */
396 +
397 + unsigned int debug:1; /* Enable debug output */
398 + unsigned int firmware_sets_cdiv:1; /* Let the firmware manage the clock */
399 + unsigned int reset_clock:1; /* Reset the clock fore the next request */
400 +
401 + /*DMA part*/
402 + struct dma_chan *dma_chan_rxtx; /* DMA channel for reads and writes */
403 + struct dma_chan *dma_chan; /* Channel in use */
404 + struct dma_slave_config dma_cfg_rx;
405 + struct dma_slave_config dma_cfg_tx;
406 + struct dma_async_tx_descriptor *dma_desc;
407 + u32 dma_dir;
408 + u32 drain_words;
409 + struct page *drain_page;
410 + u32 drain_offset;
411 +
412 + bool allow_dma;
413 + bool use_dma;
414 + /*end of DMA part*/
415 +
416 + int max_delay; /* maximum length of time spent waiting */
417 + struct timeval stop_time; /* when the last stop was issued */
418 + u32 delay_after_stop; /* minimum time between stop and subsequent data transfer */
419 + u32 delay_after_this_stop; /* minimum time between this stop and subsequent data transfer */
420 + u32 user_overclock_50; /* User's preferred frequency to use when 50MHz is requested (in MHz) */
421 + u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
422 + u32 overclock; /* Current frequency if overclocked, else zero */
423 + u32 pio_limit; /* Maximum block count for PIO (0 = always DMA) */
424 +
425 + u32 sectors; /* Cached card size in sectors */
426 +};
427 +
428 +#if ENABLE_LOG
429 +
430 +struct log_entry_struct {
431 + char event[4];
432 + u32 timestamp;
433 + u32 param1;
434 + u32 param2;
435 +};
436 +
437 +typedef struct log_entry_struct LOG_ENTRY_T;
438 +
439 +LOG_ENTRY_T *sdhost_log_buf;
440 +dma_addr_t sdhost_log_addr;
441 +static u32 sdhost_log_idx;
442 +static spinlock_t log_lock;
443 +static void __iomem *timer_base;
444 +
445 +#define LOG_ENTRIES (256*1)
446 +#define LOG_SIZE (sizeof(LOG_ENTRY_T)*LOG_ENTRIES)
447 +
448 +static void log_init(struct device *dev, u32 bus_to_phys)
449 +{
450 + spin_lock_init(&log_lock);
451 + sdhost_log_buf = dma_zalloc_coherent(dev, LOG_SIZE, &sdhost_log_addr,
452 + GFP_KERNEL);
453 + if (sdhost_log_buf) {
454 + pr_info("sdhost: log_buf @ %p (%x)\n",
455 + sdhost_log_buf, sdhost_log_addr);
456 + timer_base = ioremap_nocache(bus_to_phys + 0x7e003000, SZ_4K);
457 + if (!timer_base)
458 + pr_err("sdhost: failed to remap timer\n");
459 + }
460 + else
461 + pr_err("sdhost: failed to allocate log buf\n");
462 +}
463 +
464 +static void log_event_impl(const char *event, u32 param1, u32 param2)
465 +{
466 + if (sdhost_log_buf) {
467 + LOG_ENTRY_T *entry;
468 + unsigned long flags;
469 +
470 + spin_lock_irqsave(&log_lock, flags);
471 +
472 + entry = sdhost_log_buf + sdhost_log_idx;
473 + memcpy(entry->event, event, 4);
474 + entry->timestamp = (readl(timer_base + 4) & 0x3fffffff) +
475 + (smp_processor_id()<<30);
476 + entry->param1 = param1;
477 + entry->param2 = param2;
478 + sdhost_log_idx = (sdhost_log_idx + 1) % LOG_ENTRIES;
479 +
480 + spin_unlock_irqrestore(&log_lock, flags);
481 + }
482 +}
483 +
484 +static void log_dump(void)
485 +{
486 + if (sdhost_log_buf) {
487 + LOG_ENTRY_T *entry;
488 + unsigned long flags;
489 + int idx;
490 +
491 + spin_lock_irqsave(&log_lock, flags);
492 +
493 + idx = sdhost_log_idx;
494 + do {
495 + entry = sdhost_log_buf + idx;
496 + if (entry->event[0] != '\0')
497 + pr_info("[%08x] %.4s %x %x\n",
498 + entry->timestamp,
499 + entry->event,
500 + entry->param1,
501 + entry->param2);
502 + idx = (idx + 1) % LOG_ENTRIES;
503 + } while (idx != sdhost_log_idx);
504 +
505 + spin_unlock_irqrestore(&log_lock, flags);
506 + }
507 +}
508 +
509 +#define log_event(event, param1, param2) log_event_impl(event, param1, param2)
510 +
511 +#else
512 +
513 +#define log_init(x) (void)0
514 +#define log_event(event, param1, param2) (void)0
515 +#define log_dump() (void)0
516 +
517 +#endif
518 +
519 +static inline void bcm2835_sdhost_write(struct bcm2835_host *host, u32 val, int reg)
520 +{
521 + writel(val, host->ioaddr + reg);
522 +}
523 +
524 +static inline u32 bcm2835_sdhost_read(struct bcm2835_host *host, int reg)
525 +{
526 + return readl(host->ioaddr + reg);
527 +}
528 +
529 +static inline u32 bcm2835_sdhost_read_relaxed(struct bcm2835_host *host, int reg)
530 +{
531 + return readl_relaxed(host->ioaddr + reg);
532 +}
533 +
534 +static void bcm2835_sdhost_dumpcmd(struct bcm2835_host *host,
535 + struct mmc_command *cmd,
536 + const char *label)
537 +{
538 + if (cmd)
539 + pr_info("%s:%c%s op %d arg 0x%x flags 0x%x - resp %08x %08x %08x %08x, err %d\n",
540 + mmc_hostname(host->mmc),
541 + (cmd == host->cmd) ? '>' : ' ',
542 + label, cmd->opcode, cmd->arg, cmd->flags,
543 + cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3],
544 + cmd->error);
545 +}
546 +
547 +static void bcm2835_sdhost_dumpregs(struct bcm2835_host *host)
548 +{
549 + if (host->mrq)
550 + {
551 + bcm2835_sdhost_dumpcmd(host, host->mrq->sbc, "sbc");
552 + bcm2835_sdhost_dumpcmd(host, host->mrq->cmd, "cmd");
553 + if (host->mrq->data)
554 + pr_info("%s: data blocks %x blksz %x - err %d\n",
555 + mmc_hostname(host->mmc),
556 + host->mrq->data->blocks,
557 + host->mrq->data->blksz,
558 + host->mrq->data->error);
559 + bcm2835_sdhost_dumpcmd(host, host->mrq->stop, "stop");
560 + }
561 +
562 + pr_info("%s: =========== REGISTER DUMP ===========\n",
563 + mmc_hostname(host->mmc));
564 +
565 + pr_info("%s: SDCMD 0x%08x\n",
566 + mmc_hostname(host->mmc),
567 + bcm2835_sdhost_read(host, SDCMD));
568 + pr_info("%s: SDARG 0x%08x\n",
569 + mmc_hostname(host->mmc),
570 + bcm2835_sdhost_read(host, SDARG));
571 + pr_info("%s: SDTOUT 0x%08x\n",
572 + mmc_hostname(host->mmc),
573 + bcm2835_sdhost_read(host, SDTOUT));
574 + pr_info("%s: SDCDIV 0x%08x\n",
575 + mmc_hostname(host->mmc),
576 + bcm2835_sdhost_read(host, SDCDIV));
577 + pr_info("%s: SDRSP0 0x%08x\n",
578 + mmc_hostname(host->mmc),
579 + bcm2835_sdhost_read(host, SDRSP0));
580 + pr_info("%s: SDRSP1 0x%08x\n",
581 + mmc_hostname(host->mmc),
582 + bcm2835_sdhost_read(host, SDRSP1));
583 + pr_info("%s: SDRSP2 0x%08x\n",
584 + mmc_hostname(host->mmc),
585 + bcm2835_sdhost_read(host, SDRSP2));
586 + pr_info("%s: SDRSP3 0x%08x\n",
587 + mmc_hostname(host->mmc),
588 + bcm2835_sdhost_read(host, SDRSP3));
589 + pr_info("%s: SDHSTS 0x%08x\n",
590 + mmc_hostname(host->mmc),
591 + bcm2835_sdhost_read(host, SDHSTS));
592 + pr_info("%s: SDVDD 0x%08x\n",
593 + mmc_hostname(host->mmc),
594 + bcm2835_sdhost_read(host, SDVDD));
595 + pr_info("%s: SDEDM 0x%08x\n",
596 + mmc_hostname(host->mmc),
597 + bcm2835_sdhost_read(host, SDEDM));
598 + pr_info("%s: SDHCFG 0x%08x\n",
599 + mmc_hostname(host->mmc),
600 + bcm2835_sdhost_read(host, SDHCFG));
601 + pr_info("%s: SDHBCT 0x%08x\n",
602 + mmc_hostname(host->mmc),
603 + bcm2835_sdhost_read(host, SDHBCT));
604 + pr_info("%s: SDHBLC 0x%08x\n",
605 + mmc_hostname(host->mmc),
606 + bcm2835_sdhost_read(host, SDHBLC));
607 +
608 + pr_info("%s: ===========================================\n",
609 + mmc_hostname(host->mmc));
610 +}
611 +
612 +static void bcm2835_sdhost_set_power(struct bcm2835_host *host, bool on)
613 +{
614 + bcm2835_sdhost_write(host, on ? 1 : 0, SDVDD);
615 +}
616 +
617 +static void bcm2835_sdhost_reset_internal(struct bcm2835_host *host)
618 +{
619 + u32 temp;
620 +
621 + if (host->debug)
622 + pr_info("%s: reset\n", mmc_hostname(host->mmc));
623 +
624 + bcm2835_sdhost_set_power(host, false);
625 +
626 + bcm2835_sdhost_write(host, 0, SDCMD);
627 + bcm2835_sdhost_write(host, 0, SDARG);
628 + bcm2835_sdhost_write(host, 0xf00000, SDTOUT);
629 + bcm2835_sdhost_write(host, 0, SDCDIV);
630 + bcm2835_sdhost_write(host, 0x7f8, SDHSTS); /* Write 1s to clear */
631 + bcm2835_sdhost_write(host, 0, SDHCFG);
632 + bcm2835_sdhost_write(host, 0, SDHBCT);
633 + bcm2835_sdhost_write(host, 0, SDHBLC);
634 +
635 + /* Limit fifo usage due to silicon bug */
636 + temp = bcm2835_sdhost_read(host, SDEDM);
637 + temp &= ~((SDEDM_THRESHOLD_MASK<<SDEDM_READ_THRESHOLD_SHIFT) |
638 + (SDEDM_THRESHOLD_MASK<<SDEDM_WRITE_THRESHOLD_SHIFT));
639 + temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |
640 + (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);
641 + bcm2835_sdhost_write(host, temp, SDEDM);
642 + mdelay(10);
643 + bcm2835_sdhost_set_power(host, true);
644 + mdelay(10);
645 + host->clock = 0;
646 + host->sectors = 0;
647 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
648 + bcm2835_sdhost_write(host, SDCDIV_MAX_CDIV, SDCDIV);
649 + mmiowb();
650 +}
651 +
652 +static void bcm2835_sdhost_reset(struct mmc_host *mmc)
653 +{
654 + struct bcm2835_host *host = mmc_priv(mmc);
655 + unsigned long flags;
656 + spin_lock_irqsave(&host->lock, flags);
657 + log_event("RST<", 0, 0);
658 +
659 + bcm2835_sdhost_reset_internal(host);
660 +
661 + spin_unlock_irqrestore(&host->lock, flags);
662 +}
663 +
664 +static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
665 +
666 +static void bcm2835_sdhost_init(struct bcm2835_host *host, int soft)
667 +{
668 + pr_debug("bcm2835_sdhost_init(%d)\n", soft);
669 +
670 + /* Set interrupt enables */
671 + host->hcfg = SDHCFG_BUSY_IRPT_EN;
672 +
673 + bcm2835_sdhost_reset_internal(host);
674 +
675 + if (soft) {
676 + /* force clock reconfiguration */
677 + host->clock = 0;
678 + bcm2835_sdhost_set_ios(host->mmc, &host->mmc->ios);
679 + }
680 +}
681 +
682 +static void bcm2835_sdhost_wait_transfer_complete(struct bcm2835_host *host)
683 +{
684 + int timediff;
685 + u32 alternate_idle;
686 + u32 edm;
687 +
688 + alternate_idle = (host->mrq->data->flags & MMC_DATA_READ) ?
689 + SDEDM_FSM_READWAIT : SDEDM_FSM_WRITESTART1;
690 +
691 + edm = bcm2835_sdhost_read(host, SDEDM);
692 +
693 + log_event("WTC<", edm, 0);
694 +
695 + timediff = 0;
696 +
697 + while (1) {
698 + u32 fsm = edm & SDEDM_FSM_MASK;
699 + if ((fsm == SDEDM_FSM_IDENTMODE) ||
700 + (fsm == SDEDM_FSM_DATAMODE))
701 + break;
702 + if (fsm == alternate_idle) {
703 + bcm2835_sdhost_write(host,
704 + edm | SDEDM_FORCE_DATA_MODE,
705 + SDEDM);
706 + break;
707 + }
708 +
709 + timediff++;
710 + if (timediff == 100000) {
711 + pr_err("%s: wait_transfer_complete - still waiting after %d retries\n",
712 + mmc_hostname(host->mmc),
713 + timediff);
714 + log_dump();
715 + bcm2835_sdhost_dumpregs(host);
716 + host->mrq->data->error = -ETIMEDOUT;
717 + log_event("WTC!", edm, 0);
718 + return;
719 + }
720 + cpu_relax();
721 + edm = bcm2835_sdhost_read(host, SDEDM);
722 + }
723 + log_event("WTC>", edm, 0);
724 +}
725 +
726 +static void bcm2835_sdhost_finish_data(struct bcm2835_host *host);
727 +
728 +static void bcm2835_sdhost_dma_complete(void *param)
729 +{
730 + struct bcm2835_host *host = param;
731 + struct mmc_data *data = host->data;
732 + unsigned long flags;
733 +
734 + spin_lock_irqsave(&host->lock, flags);
735 + log_event("DMA<", (u32)host->data, bcm2835_sdhost_read(host, SDHSTS));
736 + log_event("DMA ", bcm2835_sdhost_read(host, SDCMD),
737 + bcm2835_sdhost_read(host, SDEDM));
738 +
739 + if (host->dma_chan) {
740 + dma_unmap_sg(host->dma_chan->device->dev,
741 + data->sg, data->sg_len,
742 + host->dma_dir);
743 +
744 + host->dma_chan = NULL;
745 + }
746 +
747 + if (host->drain_words) {
748 + void *page;
749 + u32 *buf;
750 +
751 + page = kmap_atomic(host->drain_page);
752 + buf = page + host->drain_offset;
753 +
754 + while (host->drain_words) {
755 + u32 edm = bcm2835_sdhost_read(host, SDEDM);
756 + if ((edm >> 4) & 0x1f)
757 + *(buf++) = bcm2835_sdhost_read(host,
758 + SDDATA);
759 + host->drain_words--;
760 + }
761 +
762 + kunmap_atomic(page);
763 + }
764 +
765 + bcm2835_sdhost_finish_data(host);
766 +
767 + log_event("DMA>", (u32)host->data, 0);
768 + spin_unlock_irqrestore(&host->lock, flags);
769 +}
770 +
771 +static void bcm2835_sdhost_read_block_pio(struct bcm2835_host *host)
772 +{
773 + unsigned long flags;
774 + size_t blksize, len;
775 + u32 *buf;
776 + unsigned long wait_max;
777 +
778 + blksize = host->data->blksz;
779 +
780 + wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
781 +
782 + local_irq_save(flags);
783 +
784 + while (blksize) {
785 + int copy_words;
786 + u32 hsts = 0;
787 +
788 + if (!sg_miter_next(&host->sg_miter)) {
789 + host->data->error = -EINVAL;
790 + break;
791 + }
792 +
793 + len = min(host->sg_miter.length, blksize);
794 + if (len % 4) {
795 + host->data->error = -EINVAL;
796 + break;
797 + }
798 +
799 + blksize -= len;
800 + host->sg_miter.consumed = len;
801 +
802 + buf = (u32 *)host->sg_miter.addr;
803 +
804 + copy_words = len/4;
805 +
806 + while (copy_words) {
807 + int burst_words, words;
808 + u32 edm;
809 +
810 + burst_words = SDDATA_FIFO_PIO_BURST;
811 + if (burst_words > copy_words)
812 + burst_words = copy_words;
813 + edm = bcm2835_sdhost_read(host, SDEDM);
814 + words = ((edm >> 4) & 0x1f);
815 +
816 + if (words < burst_words) {
817 + int fsm_state = (edm & SDEDM_FSM_MASK);
818 + if ((fsm_state != SDEDM_FSM_READDATA) &&
819 + (fsm_state != SDEDM_FSM_READWAIT) &&
820 + (fsm_state != SDEDM_FSM_READCRC)) {
821 + hsts = bcm2835_sdhost_read(host,
822 + SDHSTS);
823 + pr_info("%s: fsm %x, hsts %x\n",
824 + mmc_hostname(host->mmc),
825 + fsm_state, hsts);
826 + if (hsts & SDHSTS_ERROR_MASK)
827 + break;
828 + }
829 +
830 + if (time_after(jiffies, wait_max)) {
831 + pr_err("%s: PIO read timeout - EDM %x\n",
832 + mmc_hostname(host->mmc),
833 + edm);
834 + hsts = SDHSTS_REW_TIME_OUT;
835 + break;
836 + }
837 + ndelay((burst_words - words) *
838 + host->ns_per_fifo_word);
839 + continue;
840 + } else if (words > copy_words) {
841 + words = copy_words;
842 + }
843 +
844 + copy_words -= words;
845 +
846 + while (words) {
847 + *(buf++) = bcm2835_sdhost_read(host, SDDATA);
848 + words--;
849 + }
850 + }
851 +
852 + if (hsts & SDHSTS_ERROR_MASK)
853 + break;
854 + }
855 +
856 + sg_miter_stop(&host->sg_miter);
857 +
858 + local_irq_restore(flags);
859 +}
860 +
861 +static void bcm2835_sdhost_write_block_pio(struct bcm2835_host *host)
862 +{
863 + unsigned long flags;
864 + size_t blksize, len;
865 + u32 *buf;
866 + unsigned long wait_max;
867 +
868 + blksize = host->data->blksz;
869 +
870 + wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
871 +
872 + local_irq_save(flags);
873 +
874 + while (blksize) {
875 + int copy_words;
876 + u32 hsts = 0;
877 +
878 + if (!sg_miter_next(&host->sg_miter)) {
879 + host->data->error = -EINVAL;
880 + break;
881 + }
882 +
883 + len = min(host->sg_miter.length, blksize);
884 + if (len % 4) {
885 + host->data->error = -EINVAL;
886 + break;
887 + }
888 +
889 + blksize -= len;
890 + host->sg_miter.consumed = len;
891 +
892 + buf = (u32 *)host->sg_miter.addr;
893 +
894 + copy_words = len/4;
895 +
896 + while (copy_words) {
897 + int burst_words, words;
898 + u32 edm;
899 +
900 + burst_words = SDDATA_FIFO_PIO_BURST;
901 + if (burst_words > copy_words)
902 + burst_words = copy_words;
903 + edm = bcm2835_sdhost_read(host, SDEDM);
904 + words = SDDATA_FIFO_WORDS - ((edm >> 4) & 0x1f);
905 +
906 + if (words < burst_words) {
907 + int fsm_state = (edm & SDEDM_FSM_MASK);
908 + if ((fsm_state != SDEDM_FSM_WRITEDATA) &&
909 + (fsm_state != SDEDM_FSM_WRITESTART1) &&
910 + (fsm_state != SDEDM_FSM_WRITESTART2)) {
911 + hsts = bcm2835_sdhost_read(host,
912 + SDHSTS);
913 + pr_info("%s: fsm %x, hsts %x\n",
914 + mmc_hostname(host->mmc),
915 + fsm_state, hsts);
916 + if (hsts & SDHSTS_ERROR_MASK)
917 + break;
918 + }
919 +
920 + if (time_after(jiffies, wait_max)) {
921 + pr_err("%s: PIO write timeout - EDM %x\n",
922 + mmc_hostname(host->mmc),
923 + edm);
924 + hsts = SDHSTS_REW_TIME_OUT;
925 + break;
926 + }
927 + ndelay((burst_words - words) *
928 + host->ns_per_fifo_word);
929 + continue;
930 + } else if (words > copy_words) {
931 + words = copy_words;
932 + }
933 +
934 + copy_words -= words;
935 +
936 + while (words) {
937 + bcm2835_sdhost_write(host, *(buf++), SDDATA);
938 + words--;
939 + }
940 + }
941 +
942 + if (hsts & SDHSTS_ERROR_MASK)
943 + break;
944 + }
945 +
946 + sg_miter_stop(&host->sg_miter);
947 +
948 + local_irq_restore(flags);
949 +}
950 +
951 +static void bcm2835_sdhost_transfer_pio(struct bcm2835_host *host)
952 +{
953 + u32 sdhsts;
954 + bool is_read;
955 + BUG_ON(!host->data);
956 + log_event("XFP<", (u32)host->data, host->blocks);
957 +
958 + is_read = (host->data->flags & MMC_DATA_READ) != 0;
959 + if (is_read)
960 + bcm2835_sdhost_read_block_pio(host);
961 + else
962 + bcm2835_sdhost_write_block_pio(host);
963 +
964 + sdhsts = bcm2835_sdhost_read(host, SDHSTS);
965 + if (sdhsts & (SDHSTS_CRC16_ERROR |
966 + SDHSTS_CRC7_ERROR |
967 + SDHSTS_FIFO_ERROR)) {
968 + pr_err("%s: %s transfer error - HSTS %x\n",
969 + mmc_hostname(host->mmc),
970 + is_read ? "read" : "write",
971 + sdhsts);
972 + host->data->error = -EILSEQ;
973 + } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
974 + SDHSTS_REW_TIME_OUT))) {
975 + pr_err("%s: %s timeout error - HSTS %x\n",
976 + mmc_hostname(host->mmc),
977 + is_read ? "read" : "write",
978 + sdhsts);
979 + host->data->error = -ETIMEDOUT;
980 + }
981 + log_event("XFP>", (u32)host->data, host->blocks);
982 +}
983 +
984 +static void bcm2835_sdhost_prepare_dma(struct bcm2835_host *host,
985 + struct mmc_data *data)
986 +{
987 + int len, dir_data, dir_slave;
988 + struct dma_async_tx_descriptor *desc = NULL;
989 + struct dma_chan *dma_chan;
990 +
991 + log_event("PRD<", (u32)data, 0);
992 + pr_debug("bcm2835_sdhost_prepare_dma()\n");
993 +
994 + dma_chan = host->dma_chan_rxtx;
995 + if (data->flags & MMC_DATA_READ) {
996 + dir_data = DMA_FROM_DEVICE;
997 + dir_slave = DMA_DEV_TO_MEM;
998 + } else {
999 + dir_data = DMA_TO_DEVICE;
1000 + dir_slave = DMA_MEM_TO_DEV;
1001 + }
1002 + log_event("PRD1", (u32)dma_chan, 0);
1003 +
1004 + BUG_ON(!dma_chan->device);
1005 + BUG_ON(!dma_chan->device->dev);
1006 + BUG_ON(!data->sg);
1007 +
1008 + /* The block doesn't manage the FIFO DREQs properly for multi-block
1009 + transfers, so don't attempt to DMA the final few words.
1010 + Unfortunately this requires the final sg entry to be trimmed.
1011 + N.B. This code demands that the overspill is contained in
1012 + a single sg entry.
1013 + */
1014 +
1015 + host->drain_words = 0;
1016 + if ((data->blocks > 1) && (dir_data == DMA_FROM_DEVICE)) {
1017 + struct scatterlist *sg;
1018 + u32 len;
1019 + int i;
1020 +
1021 + len = min((u32)(FIFO_READ_THRESHOLD - 1) * 4,
1022 + (u32)data->blocks * data->blksz);
1023 +
1024 + for_each_sg(data->sg, sg, data->sg_len, i) {
1025 + if (sg_is_last(sg)) {
1026 + BUG_ON(sg->length < len);
1027 + sg->length -= len;
1028 + host->drain_page = sg_page(sg);
1029 + host->drain_offset = sg->offset + sg->length;
1030 + }
1031 + }
1032 + host->drain_words = len/4;
1033 + }
1034 +
1035 + /* The parameters have already been validated, so this will not fail */
1036 + (void)dmaengine_slave_config(dma_chan,
1037 + (dir_data == DMA_FROM_DEVICE) ?
1038 + &host->dma_cfg_rx :
1039 + &host->dma_cfg_tx);
1040 +
1041 + len = dma_map_sg(dma_chan->device->dev, data->sg, data->sg_len,
1042 + dir_data);
1043 +
1044 + log_event("PRD2", len, 0);
1045 + if (len > 0)
1046 + desc = dmaengine_prep_slave_sg(dma_chan, data->sg,
1047 + len, dir_slave,
1048 + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1049 + log_event("PRD3", (u32)desc, 0);
1050 +
1051 + if (desc) {
1052 + desc->callback = bcm2835_sdhost_dma_complete;
1053 + desc->callback_param = host;
1054 + host->dma_desc = desc;
1055 + host->dma_chan = dma_chan;
1056 + host->dma_dir = dir_data;
1057 + }
1058 + log_event("PDM>", (u32)data, 0);
1059 +}
1060 +
1061 +static void bcm2835_sdhost_start_dma(struct bcm2835_host *host)
1062 +{
1063 + log_event("SDMA", (u32)host->data, (u32)host->dma_chan);
1064 + dmaengine_submit(host->dma_desc);
1065 + dma_async_issue_pending(host->dma_chan);
1066 +}
1067 +
1068 +static void bcm2835_sdhost_set_transfer_irqs(struct bcm2835_host *host)
1069 +{
1070 + u32 all_irqs = SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN |
1071 + SDHCFG_BUSY_IRPT_EN;
1072 + if (host->dma_desc)
1073 + host->hcfg = (host->hcfg & ~all_irqs) |
1074 + SDHCFG_BUSY_IRPT_EN;
1075 + else
1076 + host->hcfg = (host->hcfg & ~all_irqs) |
1077 + SDHCFG_DATA_IRPT_EN |
1078 + SDHCFG_BUSY_IRPT_EN;
1079 +
1080 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1081 +}
1082 +
1083 +static void bcm2835_sdhost_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
1084 +{
1085 + struct mmc_data *data = cmd->data;
1086 +
1087 + WARN_ON(host->data);
1088 +
1089 + host->data = data;
1090 + if (!data)
1091 + return;
1092 +
1093 + /* Sanity checks */
1094 + BUG_ON(data->blksz * data->blocks > 524288);
1095 + BUG_ON(data->blksz > host->mmc->max_blk_size);
1096 + BUG_ON(data->blocks > 65535);
1097 +
1098 + host->data_complete = 0;
1099 + host->flush_fifo = 0;
1100 + host->data->bytes_xfered = 0;
1101 +
1102 + if (!host->sectors && host->mmc->card) {
1103 + struct mmc_card *card = host->mmc->card;
1104 + if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1105 + /*
1106 + * The EXT_CSD sector count is in number of 512 byte
1107 + * sectors.
1108 + */
1109 + host->sectors = card->ext_csd.sectors;
1110 + } else {
1111 + /*
1112 + * The CSD capacity field is in units of read_blkbits.
1113 + * set_capacity takes units of 512 bytes.
1114 + */
1115 + host->sectors = card->csd.capacity <<
1116 + (card->csd.read_blkbits - 9);
1117 + }
1118 + }
1119 +
1120 + if (!host->dma_desc) {
1121 + /* Use PIO */
1122 + int flags = SG_MITER_ATOMIC;
1123 +
1124 + if (data->flags & MMC_DATA_READ)
1125 + flags |= SG_MITER_TO_SG;
1126 + else
1127 + flags |= SG_MITER_FROM_SG;
1128 + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
1129 + host->blocks = data->blocks;
1130 + }
1131 +
1132 + bcm2835_sdhost_set_transfer_irqs(host);
1133 +
1134 + bcm2835_sdhost_write(host, data->blksz, SDHBCT);
1135 + bcm2835_sdhost_write(host, data->blocks, SDHBLC);
1136 +
1137 + BUG_ON(!host->data);
1138 +}
1139 +
1140 +bool bcm2835_sdhost_send_command(struct bcm2835_host *host,
1141 + struct mmc_command *cmd)
1142 +{
1143 + u32 sdcmd, sdhsts;
1144 + unsigned long timeout;
1145 + int delay;
1146 +
1147 + WARN_ON(host->cmd);
1148 + log_event("CMD<", cmd->opcode, cmd->arg);
1149 +
1150 + if (cmd->data)
1151 + pr_debug("%s: send_command %d 0x%x "
1152 + "(flags 0x%x) - %s %d*%d\n",
1153 + mmc_hostname(host->mmc),
1154 + cmd->opcode, cmd->arg, cmd->flags,
1155 + (cmd->data->flags & MMC_DATA_READ) ?
1156 + "read" : "write", cmd->data->blocks,
1157 + cmd->data->blksz);
1158 + else
1159 + pr_debug("%s: send_command %d 0x%x (flags 0x%x)\n",
1160 + mmc_hostname(host->mmc),
1161 + cmd->opcode, cmd->arg, cmd->flags);
1162 +
1163 + /* Wait max 100 ms */
1164 + timeout = 10000;
1165 +
1166 + while (bcm2835_sdhost_read(host, SDCMD) & SDCMD_NEW_FLAG) {
1167 + if (timeout == 0) {
1168 + pr_warn("%s: previous command never completed.\n",
1169 + mmc_hostname(host->mmc));
1170 + if (host->debug)
1171 + bcm2835_sdhost_dumpregs(host);
1172 + cmd->error = -EILSEQ;
1173 + tasklet_schedule(&host->finish_tasklet);
1174 + return false;
1175 + }
1176 + timeout--;
1177 + udelay(10);
1178 + }
1179 +
1180 + delay = (10000 - timeout)/100;
1181 + if (delay > host->max_delay) {
1182 + host->max_delay = delay;
1183 + pr_warning("%s: controller hung for %d ms\n",
1184 + mmc_hostname(host->mmc),
1185 + host->max_delay);
1186 + }
1187 +
1188 + timeout = jiffies;
1189 + if (!cmd->data && cmd->busy_timeout > 9000)
1190 + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1191 + else
1192 + timeout += 10 * HZ;
1193 + mod_timer(&host->timer, timeout);
1194 +
1195 + host->cmd = cmd;
1196 +
1197 + /* Clear any error flags */
1198 + sdhsts = bcm2835_sdhost_read(host, SDHSTS);
1199 + if (sdhsts & SDHSTS_ERROR_MASK)
1200 + bcm2835_sdhost_write(host, sdhsts, SDHSTS);
1201 +
1202 + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1203 + pr_err("%s: unsupported response type!\n",
1204 + mmc_hostname(host->mmc));
1205 + cmd->error = -EINVAL;
1206 + tasklet_schedule(&host->finish_tasklet);
1207 + return false;
1208 + }
1209 +
1210 + bcm2835_sdhost_prepare_data(host, cmd);
1211 +
1212 + bcm2835_sdhost_write(host, cmd->arg, SDARG);
1213 +
1214 + sdcmd = cmd->opcode & SDCMD_CMD_MASK;
1215 +
1216 + host->use_busy = 0;
1217 + if (!(cmd->flags & MMC_RSP_PRESENT)) {
1218 + sdcmd |= SDCMD_NO_RESPONSE;
1219 + } else {
1220 + if (cmd->flags & MMC_RSP_136)
1221 + sdcmd |= SDCMD_LONG_RESPONSE;
1222 + if (cmd->flags & MMC_RSP_BUSY) {
1223 + sdcmd |= SDCMD_BUSYWAIT;
1224 + host->use_busy = 1;
1225 + }
1226 + }
1227 +
1228 + if (cmd->data) {
1229 + log_event("CMDD", cmd->data->blocks, cmd->data->blksz);
1230 + if (host->delay_after_this_stop) {
1231 + struct timeval now;
1232 + int time_since_stop;
1233 + do_gettimeofday(&now);
1234 + time_since_stop = (now.tv_sec - host->stop_time.tv_sec);
1235 + if (time_since_stop < 2) {
1236 + /* Possibly less than one second */
1237 + time_since_stop = time_since_stop * 1000000 +
1238 + (now.tv_usec - host->stop_time.tv_usec);
1239 + if (time_since_stop <
1240 + host->delay_after_this_stop)
1241 + udelay(host->delay_after_this_stop -
1242 + time_since_stop);
1243 + }
1244 + }
1245 +
1246 + host->delay_after_this_stop = host->delay_after_stop;
1247 + if ((cmd->data->flags & MMC_DATA_READ) && !host->use_sbc) {
1248 + /* See if read crosses one of the hazardous sectors */
1249 + u32 first_blk, last_blk;
1250 +
1251 + /* Intentionally include the following sector because
1252 + without CMD23/SBC the read may run on. */
1253 + first_blk = host->mrq->cmd->arg;
1254 + last_blk = first_blk + cmd->data->blocks;
1255 +
1256 + if (((last_blk >= (host->sectors - 64)) &&
1257 + (first_blk <= (host->sectors - 64))) ||
1258 + ((last_blk >= (host->sectors - 32)) &&
1259 + (first_blk <= (host->sectors - 32)))) {
1260 + host->delay_after_this_stop =
1261 + max(250u, host->delay_after_stop);
1262 + }
1263 + }
1264 +
1265 + if (cmd->data->flags & MMC_DATA_WRITE)
1266 + sdcmd |= SDCMD_WRITE_CMD;
1267 + if (cmd->data->flags & MMC_DATA_READ)
1268 + sdcmd |= SDCMD_READ_CMD;
1269 + }
1270 +
1271 + bcm2835_sdhost_write(host, sdcmd | SDCMD_NEW_FLAG, SDCMD);
1272 +
1273 + return true;
1274 +}
1275 +
1276 +static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
1277 + unsigned long *irq_flags);
1278 +static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host);
1279 +
1280 +static void bcm2835_sdhost_finish_data(struct bcm2835_host *host)
1281 +{
1282 + struct mmc_data *data;
1283 +
1284 + data = host->data;
1285 + BUG_ON(!data);
1286 +
1287 + log_event("FDA<", (u32)host->mrq, (u32)host->cmd);
1288 + pr_debug("finish_data(error %d, stop %d, sbc %d)\n",
1289 + data->error, data->stop ? 1 : 0,
1290 + host->mrq->sbc ? 1 : 0);
1291 +
1292 + host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
1293 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1294 +
1295 + data->bytes_xfered = data->error ? 0 : (data->blksz * data->blocks);
1296 +
1297 + host->data_complete = 1;
1298 +
1299 + if (host->cmd) {
1300 + /*
1301 + * Data managed to finish before the
1302 + * command completed. Make sure we do
1303 + * things in the proper order.
1304 + */
1305 + pr_debug("Finished early - HSTS %x\n",
1306 + bcm2835_sdhost_read(host, SDHSTS));
1307 + }
1308 + else
1309 + bcm2835_sdhost_transfer_complete(host);
1310 + log_event("FDA>", (u32)host->mrq, (u32)host->cmd);
1311 +}
1312 +
1313 +static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host)
1314 +{
1315 + struct mmc_data *data;
1316 +
1317 + BUG_ON(host->cmd);
1318 + BUG_ON(!host->data);
1319 + BUG_ON(!host->data_complete);
1320 +
1321 + data = host->data;
1322 + host->data = NULL;
1323 +
1324 + log_event("TCM<", (u32)data, data->error);
1325 + pr_debug("transfer_complete(error %d, stop %d)\n",
1326 + data->error, data->stop ? 1 : 0);
1327 +
1328 + /*
1329 + * Need to send CMD12 if -
1330 + * a) open-ended multiblock transfer (no CMD23)
1331 + * b) error in multiblock transfer
1332 + */
1333 + if (host->mrq->stop && (data->error || !host->use_sbc)) {
1334 + if (bcm2835_sdhost_send_command(host, host->mrq->stop)) {
1335 + /* No busy, so poll for completion */
1336 + if (!host->use_busy)
1337 + bcm2835_sdhost_finish_command(host, NULL);
1338 +
1339 + if (host->delay_after_this_stop)
1340 + do_gettimeofday(&host->stop_time);
1341 + }
1342 + } else {
1343 + bcm2835_sdhost_wait_transfer_complete(host);
1344 + tasklet_schedule(&host->finish_tasklet);
1345 + }
1346 + log_event("TCM>", (u32)data, 0);
1347 +}
1348 +
1349 +/* If irq_flags is valid, the caller is in a thread context and is allowed
1350 + to sleep */
1351 +static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
1352 + unsigned long *irq_flags)
1353 +{
1354 + u32 sdcmd;
1355 + u32 retries;
1356 +#ifdef DEBUG
1357 + struct timeval before, after;
1358 + int timediff = 0;
1359 +#endif
1360 +
1361 + log_event("FCM<", (u32)host->mrq, (u32)host->cmd);
1362 + pr_debug("finish_command(%x)\n", bcm2835_sdhost_read(host, SDCMD));
1363 +
1364 + BUG_ON(!host->cmd || !host->mrq);
1365 +
1366 + /* Poll quickly at first */
1367 +
1368 + retries = host->cmd_quick_poll_retries;
1369 + if (!retries) {
1370 + /* Work out how many polls take 1us by timing 10us */
1371 + struct timeval start, now;
1372 + int us_diff;
1373 +
1374 + retries = 1;
1375 + do {
1376 + int i;
1377 +
1378 + retries *= 2;
1379 +
1380 + do_gettimeofday(&start);
1381 +
1382 + for (i = 0; i < retries; i++) {
1383 + cpu_relax();
1384 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1385 + }
1386 +
1387 + do_gettimeofday(&now);
1388 + us_diff = (now.tv_sec - start.tv_sec) * 1000000 +
1389 + (now.tv_usec - start.tv_usec);
1390 + } while (us_diff < 10);
1391 +
1392 + host->cmd_quick_poll_retries = ((retries * us_diff + 9)*CMD_DALLY_US)/10 + 1;
1393 + retries = 1; // We've already waited long enough this time
1394 + }
1395 +
1396 + for (sdcmd = bcm2835_sdhost_read(host, SDCMD);
1397 + (sdcmd & SDCMD_NEW_FLAG) && retries;
1398 + retries--) {
1399 + cpu_relax();
1400 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1401 + }
1402 +
1403 + if (!retries) {
1404 + unsigned long wait_max;
1405 +
1406 + if (!irq_flags) {
1407 + /* Schedule the work */
1408 + log_event("CWWQ", 0, 0);
1409 + schedule_work(&host->cmd_wait_wq);
1410 + return;
1411 + }
1412 +
1413 + /* Wait max 100 ms */
1414 + wait_max = jiffies + msecs_to_jiffies(100);
1415 + while (time_before(jiffies, wait_max)) {
1416 + spin_unlock_irqrestore(&host->lock, *irq_flags);
1417 + usleep_range(1, 10);
1418 + spin_lock_irqsave(&host->lock, *irq_flags);
1419 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1420 + if (!(sdcmd & SDCMD_NEW_FLAG))
1421 + break;
1422 + }
1423 + }
1424 +
1425 + /* Check for errors */
1426 + if (sdcmd & SDCMD_NEW_FLAG) {
1427 + if (host->debug) {
1428 + pr_err("%s: command %d never completed.\n",
1429 + mmc_hostname(host->mmc), host->cmd->opcode);
1430 + bcm2835_sdhost_dumpregs(host);
1431 + }
1432 + host->cmd->error = -EILSEQ;
1433 + tasklet_schedule(&host->finish_tasklet);
1434 + return;
1435 + } else if (sdcmd & SDCMD_FAIL_FLAG) {
1436 + u32 sdhsts = bcm2835_sdhost_read(host, SDHSTS);
1437 +
1438 + /* Clear the errors */
1439 + bcm2835_sdhost_write(host, SDHSTS_ERROR_MASK, SDHSTS);
1440 +
1441 + if (host->debug)
1442 + pr_info("%s: error detected - CMD %x, HSTS %03x, EDM %x\n",
1443 + mmc_hostname(host->mmc), sdcmd, sdhsts,
1444 + bcm2835_sdhost_read(host, SDEDM));
1445 +
1446 + if ((sdhsts & SDHSTS_CRC7_ERROR) &&
1447 + (host->cmd->opcode == 1)) {
1448 + if (host->debug)
1449 + pr_info("%s: ignoring CRC7 error for CMD1\n",
1450 + mmc_hostname(host->mmc));
1451 + } else {
1452 + if (sdhsts & SDHSTS_CMD_TIME_OUT) {
1453 + if (host->debug)
1454 + pr_warn("%s: command %d timeout\n",
1455 + mmc_hostname(host->mmc),
1456 + host->cmd->opcode);
1457 + host->cmd->error = -ETIMEDOUT;
1458 + } else {
1459 + pr_warn("%s: unexpected command %d error\n",
1460 + mmc_hostname(host->mmc),
1461 + host->cmd->opcode);
1462 + host->cmd->error = -EILSEQ;
1463 + }
1464 + tasklet_schedule(&host->finish_tasklet);
1465 + return;
1466 + }
1467 + }
1468 +
1469 + if (host->cmd->flags & MMC_RSP_PRESENT) {
1470 + if (host->cmd->flags & MMC_RSP_136) {
1471 + int i;
1472 + for (i = 0; i < 4; i++)
1473 + host->cmd->resp[3 - i] = bcm2835_sdhost_read(host, SDRSP0 + i*4);
1474 + pr_debug("%s: finish_command %08x %08x %08x %08x\n",
1475 + mmc_hostname(host->mmc),
1476 + host->cmd->resp[0], host->cmd->resp[1], host->cmd->resp[2], host->cmd->resp[3]);
1477 + log_event("RSP ", host->cmd->resp[0], host->cmd->resp[1]);
1478 + } else {
1479 + host->cmd->resp[0] = bcm2835_sdhost_read(host, SDRSP0);
1480 + pr_debug("%s: finish_command %08x\n",
1481 + mmc_hostname(host->mmc),
1482 + host->cmd->resp[0]);
1483 + log_event("RSP ", host->cmd->resp[0], 0);
1484 + }
1485 + }
1486 +
1487 + if (host->cmd == host->mrq->sbc) {
1488 + /* Finished CMD23, now send actual command. */
1489 + host->cmd = NULL;
1490 + if (bcm2835_sdhost_send_command(host, host->mrq->cmd)) {
1491 + if (host->data && host->dma_desc)
1492 + /* DMA transfer starts now, PIO starts after irq */
1493 + bcm2835_sdhost_start_dma(host);
1494 +
1495 + if (!host->use_busy)
1496 + bcm2835_sdhost_finish_command(host, NULL);
1497 + }
1498 + } else if (host->cmd == host->mrq->stop) {
1499 + /* Finished CMD12 */
1500 + tasklet_schedule(&host->finish_tasklet);
1501 + } else {
1502 + /* Processed actual command. */
1503 + host->cmd = NULL;
1504 + if (!host->data)
1505 + tasklet_schedule(&host->finish_tasklet);
1506 + else if (host->data_complete)
1507 + bcm2835_sdhost_transfer_complete(host);
1508 + }
1509 + log_event("FCM>", (u32)host->mrq, (u32)host->cmd);
1510 +}
1511 +
1512 +static void bcm2835_sdhost_timeout(unsigned long data)
1513 +{
1514 + struct bcm2835_host *host;
1515 + unsigned long flags;
1516 +
1517 + host = (struct bcm2835_host *)data;
1518 +
1519 + spin_lock_irqsave(&host->lock, flags);
1520 + log_event("TIM<", 0, 0);
1521 +
1522 + if (host->mrq) {
1523 + pr_err("%s: timeout waiting for hardware interrupt.\n",
1524 + mmc_hostname(host->mmc));
1525 + log_dump();
1526 + bcm2835_sdhost_dumpregs(host);
1527 +
1528 + if (host->data) {
1529 + host->data->error = -ETIMEDOUT;
1530 + bcm2835_sdhost_finish_data(host);
1531 + } else {
1532 + if (host->cmd)
1533 + host->cmd->error = -ETIMEDOUT;
1534 + else
1535 + host->mrq->cmd->error = -ETIMEDOUT;
1536 +
1537 + pr_debug("timeout_timer tasklet_schedule\n");
1538 + tasklet_schedule(&host->finish_tasklet);
1539 + }
1540 + }
1541 +
1542 + mmiowb();
1543 + spin_unlock_irqrestore(&host->lock, flags);
1544 +}
1545 +
1546 +static void bcm2835_sdhost_busy_irq(struct bcm2835_host *host, u32 intmask)
1547 +{
1548 + log_event("IRQB", (u32)host->cmd, intmask);
1549 + if (!host->cmd) {
1550 + pr_err("%s: got command busy interrupt 0x%08x even "
1551 + "though no command operation was in progress.\n",
1552 + mmc_hostname(host->mmc), (unsigned)intmask);
1553 + bcm2835_sdhost_dumpregs(host);
1554 + return;
1555 + }
1556 +
1557 + if (!host->use_busy) {
1558 + pr_err("%s: got command busy interrupt 0x%08x even "
1559 + "though not expecting one.\n",
1560 + mmc_hostname(host->mmc), (unsigned)intmask);
1561 + bcm2835_sdhost_dumpregs(host);
1562 + return;
1563 + }
1564 + host->use_busy = 0;
1565 +
1566 + if (intmask & SDHSTS_ERROR_MASK)
1567 + {
1568 + pr_err("sdhost_busy_irq: intmask %x, data %p\n", intmask, host->mrq->data);
1569 + if (intmask & SDHSTS_CRC7_ERROR)
1570 + host->cmd->error = -EILSEQ;
1571 + else if (intmask & (SDHSTS_CRC16_ERROR |
1572 + SDHSTS_FIFO_ERROR)) {
1573 + if (host->mrq->data)
1574 + host->mrq->data->error = -EILSEQ;
1575 + else
1576 + host->cmd->error = -EILSEQ;
1577 + } else if (intmask & SDHSTS_REW_TIME_OUT) {
1578 + if (host->mrq->data)
1579 + host->mrq->data->error = -ETIMEDOUT;
1580 + else
1581 + host->cmd->error = -ETIMEDOUT;
1582 + } else if (intmask & SDHSTS_CMD_TIME_OUT)
1583 + host->cmd->error = -ETIMEDOUT;
1584 +
1585 + if (host->debug) {
1586 + log_dump();
1587 + bcm2835_sdhost_dumpregs(host);
1588 + }
1589 + }
1590 + else
1591 + bcm2835_sdhost_finish_command(host, NULL);
1592 +}
1593 +
1594 +static void bcm2835_sdhost_data_irq(struct bcm2835_host *host, u32 intmask)
1595 +{
1596 + /* There are no dedicated data/space available interrupt
1597 + status bits, so it is necessary to use the single shared
1598 + data/space available FIFO status bits. It is therefore not
1599 + an error to get here when there is no data transfer in
1600 + progress. */
1601 + log_event("IRQD", (u32)host->data, intmask);
1602 + if (!host->data)
1603 + return;
1604 +
1605 + if (intmask & (SDHSTS_CRC16_ERROR |
1606 + SDHSTS_FIFO_ERROR |
1607 + SDHSTS_REW_TIME_OUT)) {
1608 + if (intmask & (SDHSTS_CRC16_ERROR |
1609 + SDHSTS_FIFO_ERROR))
1610 + host->data->error = -EILSEQ;
1611 + else
1612 + host->data->error = -ETIMEDOUT;
1613 +
1614 + if (host->debug) {
1615 + log_dump();
1616 + bcm2835_sdhost_dumpregs(host);
1617 + }
1618 + }
1619 +
1620 + if (host->data->error) {
1621 + bcm2835_sdhost_finish_data(host);
1622 + } else if (host->data->flags & MMC_DATA_WRITE) {
1623 + /* Use the block interrupt for writes after the first block */
1624 + host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
1625 + host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
1626 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1627 + bcm2835_sdhost_transfer_pio(host);
1628 + } else {
1629 + bcm2835_sdhost_transfer_pio(host);
1630 + host->blocks--;
1631 + if ((host->blocks == 0) || host->data->error)
1632 + bcm2835_sdhost_finish_data(host);
1633 + }
1634 +}
1635 +
1636 +static void bcm2835_sdhost_block_irq(struct bcm2835_host *host, u32 intmask)
1637 +{
1638 + log_event("IRQK", (u32)host->data, intmask);
1639 + if (!host->data) {
1640 + pr_err("%s: got block interrupt 0x%08x even "
1641 + "though no data operation was in progress.\n",
1642 + mmc_hostname(host->mmc), (unsigned)intmask);
1643 + bcm2835_sdhost_dumpregs(host);
1644 + return;
1645 + }
1646 +
1647 + if (intmask & (SDHSTS_CRC16_ERROR |
1648 + SDHSTS_FIFO_ERROR |
1649 + SDHSTS_REW_TIME_OUT)) {
1650 + if (intmask & (SDHSTS_CRC16_ERROR |
1651 + SDHSTS_FIFO_ERROR))
1652 + host->data->error = -EILSEQ;
1653 + else
1654 + host->data->error = -ETIMEDOUT;
1655 +
1656 + if (host->debug) {
1657 + log_dump();
1658 + bcm2835_sdhost_dumpregs(host);
1659 + }
1660 + }
1661 +
1662 + if (!host->dma_desc) {
1663 + BUG_ON(!host->blocks);
1664 + if (host->data->error || (--host->blocks == 0)) {
1665 + bcm2835_sdhost_finish_data(host);
1666 + } else {
1667 + bcm2835_sdhost_transfer_pio(host);
1668 + }
1669 + } else if (host->data->flags & MMC_DATA_WRITE) {
1670 + bcm2835_sdhost_finish_data(host);
1671 + }
1672 +}
1673 +
1674 +static irqreturn_t bcm2835_sdhost_irq(int irq, void *dev_id)
1675 +{
1676 + irqreturn_t result = IRQ_NONE;
1677 + struct bcm2835_host *host = dev_id;
1678 + u32 intmask;
1679 +
1680 + spin_lock(&host->lock);
1681 +
1682 + intmask = bcm2835_sdhost_read(host, SDHSTS);
1683 + log_event("IRQ<", intmask, 0);
1684 +
1685 + bcm2835_sdhost_write(host,
1686 + SDHSTS_BUSY_IRPT |
1687 + SDHSTS_BLOCK_IRPT |
1688 + SDHSTS_SDIO_IRPT |
1689 + SDHSTS_DATA_FLAG,
1690 + SDHSTS);
1691 +
1692 + if (intmask & SDHSTS_BLOCK_IRPT) {
1693 + bcm2835_sdhost_block_irq(host, intmask);
1694 + result = IRQ_HANDLED;
1695 + }
1696 +
1697 + if (intmask & SDHSTS_BUSY_IRPT) {
1698 + bcm2835_sdhost_busy_irq(host, intmask);
1699 + result = IRQ_HANDLED;
1700 + }
1701 +
1702 + /* There is no true data interrupt status bit, so it is
1703 + necessary to qualify the data flag with the interrupt
1704 + enable bit */
1705 + if ((intmask & SDHSTS_DATA_FLAG) &&
1706 + (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
1707 + bcm2835_sdhost_data_irq(host, intmask);
1708 + result = IRQ_HANDLED;
1709 + }
1710 +
1711 + mmiowb();
1712 +
1713 + log_event("IRQ>", bcm2835_sdhost_read(host, SDHSTS), 0);
1714 + spin_unlock(&host->lock);
1715 +
1716 + return result;
1717 +}
1718 +
1719 +void bcm2835_sdhost_set_clock(struct bcm2835_host *host, unsigned int clock)
1720 +{
1721 + int div = 0; /* Initialized for compiler warning */
1722 + unsigned int input_clock = clock;
1723 + unsigned long flags;
1724 +
1725 + if (host->debug)
1726 + pr_info("%s: set_clock(%d)\n", mmc_hostname(host->mmc), clock);
1727 +
1728 + if ((host->overclock_50 > 50) &&
1729 + (clock == 50*MHZ))
1730 + clock = host->overclock_50 * MHZ + (MHZ - 1);
1731 +
1732 + /* The SDCDIV register has 11 bits, and holds (div - 2).
1733 + But in data mode the max is 50MHz wihout a minimum, and only the
1734 + bottom 3 bits are used. Since the switch over is automatic (unless
1735 + we have marked the card as slow...), chosen values have to make
1736 + sense in both modes.
1737 + Ident mode must be 100-400KHz, so can range check the requested
1738 + clock. CMD15 must be used to return to data mode, so this can be
1739 + monitored.
1740 +
1741 + clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
1742 + 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
1743 +
1744 + 623->400KHz/27.8MHz
1745 + reset value (507)->491159/50MHz
1746 +
1747 + BUT, the 3-bit clock divisor in data mode is too small if the
1748 + core clock is higher than 250MHz, so instead use the SLOW_CARD
1749 + configuration bit to force the use of the ident clock divisor
1750 + at all times.
1751 + */
1752 +
1753 + host->mmc->actual_clock = 0;
1754 +
1755 + if (host->firmware_sets_cdiv) {
1756 + u32 msg[3] = { clock, 0, 0 };
1757 +
1758 + rpi_firmware_property(rpi_firmware_get(NULL),
1759 + RPI_FIRMWARE_SET_SDHOST_CLOCK,
1760 + &msg, sizeof(msg));
1761 +
1762 + clock = max(msg[1], msg[2]);
1763 + spin_lock_irqsave(&host->lock, flags);
1764 + } else {
1765 + spin_lock_irqsave(&host->lock, flags);
1766 + if (clock < 100000) {
1767 + /* Can't stop the clock, but make it as slow as
1768 + * possible to show willing
1769 + */
1770 + host->cdiv = SDCDIV_MAX_CDIV;
1771 + bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
1772 + mmiowb();
1773 + spin_unlock_irqrestore(&host->lock, flags);
1774 + return;
1775 + }
1776 +
1777 + div = host->max_clk / clock;
1778 + if (div < 2)
1779 + div = 2;
1780 + if ((host->max_clk / div) > clock)
1781 + div++;
1782 + div -= 2;
1783 +
1784 + if (div > SDCDIV_MAX_CDIV)
1785 + div = SDCDIV_MAX_CDIV;
1786 +
1787 + clock = host->max_clk / (div + 2);
1788 +
1789 + host->cdiv = div;
1790 + bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
1791 +
1792 + if (host->debug)
1793 + pr_info("%s: clock=%d -> max_clk=%d, cdiv=%x "
1794 + "(actual clock %d)\n",
1795 + mmc_hostname(host->mmc), input_clock,
1796 + host->max_clk, host->cdiv,
1797 + clock);
1798 + }
1799 +
1800 + /* Calibrate some delays */
1801 +
1802 + host->ns_per_fifo_word = (1000000000/clock) *
1803 + ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
1804 +
1805 + if (input_clock == 50 * MHZ) {
1806 + if (clock > input_clock) {
1807 + /* Save the closest value, to make it easier
1808 + to reduce in the event of error */
1809 + host->overclock_50 = (clock/MHZ);
1810 +
1811 + if (clock != host->overclock) {
1812 + pr_info("%s: overclocking to %dHz\n",
1813 + mmc_hostname(host->mmc), clock);
1814 + host->overclock = clock;
1815 + }
1816 + } else if (host->overclock) {
1817 + host->overclock = 0;
1818 + if (clock == 50 * MHZ)
1819 + pr_warn("%s: cancelling overclock\n",
1820 + mmc_hostname(host->mmc));
1821 + }
1822 + } else if (input_clock == 0) {
1823 + /* Reset the preferred overclock when the clock is stopped.
1824 + * This always happens during initialisation. */
1825 + host->overclock_50 = host->user_overclock_50;
1826 + host->overclock = 0;
1827 + }
1828 +
1829 + /* Set the timeout to 500ms */
1830 + bcm2835_sdhost_write(host, clock/2, SDTOUT);
1831 +
1832 + host->mmc->actual_clock = clock;
1833 + host->clock = input_clock;
1834 + host->reset_clock = 0;
1835 +
1836 + mmiowb();
1837 + spin_unlock_irqrestore(&host->lock, flags);
1838 +}
1839 +
1840 +static void bcm2835_sdhost_request(struct mmc_host *mmc, struct mmc_request *mrq)
1841 +{
1842 + struct bcm2835_host *host;
1843 + unsigned long flags;
1844 + u32 edm, fsm;
1845 +
1846 + host = mmc_priv(mmc);
1847 +
1848 + if (host->debug) {
1849 + struct mmc_command *cmd = mrq->cmd;
1850 + BUG_ON(!cmd);
1851 + if (cmd->data)
1852 + pr_info("%s: cmd %d 0x%x (flags 0x%x) - %s %d*%d\n",
1853 + mmc_hostname(mmc),
1854 + cmd->opcode, cmd->arg, cmd->flags,
1855 + (cmd->data->flags & MMC_DATA_READ) ?
1856 + "read" : "write", cmd->data->blocks,
1857 + cmd->data->blksz);
1858 + else
1859 + pr_info("%s: cmd %d 0x%x (flags 0x%x)\n",
1860 + mmc_hostname(mmc),
1861 + cmd->opcode, cmd->arg, cmd->flags);
1862 + }
1863 +
1864 + /* Reset the error statuses in case this is a retry */
1865 + if (mrq->sbc)
1866 + mrq->sbc->error = 0;
1867 + if (mrq->cmd)
1868 + mrq->cmd->error = 0;
1869 + if (mrq->data)
1870 + mrq->data->error = 0;
1871 + if (mrq->stop)
1872 + mrq->stop->error = 0;
1873 +
1874 + if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
1875 + pr_err("%s: unsupported block size (%d bytes)\n",
1876 + mmc_hostname(mmc), mrq->data->blksz);
1877 + mrq->cmd->error = -EINVAL;
1878 + mmc_request_done(mmc, mrq);
1879 + return;
1880 + }
1881 +
1882 + if (host->use_dma && mrq->data &&
1883 + (mrq->data->blocks > host->pio_limit))
1884 + bcm2835_sdhost_prepare_dma(host, mrq->data);
1885 +
1886 + if (host->reset_clock)
1887 + bcm2835_sdhost_set_clock(host, host->clock);
1888 +
1889 + spin_lock_irqsave(&host->lock, flags);
1890 +
1891 + WARN_ON(host->mrq != NULL);
1892 + host->mrq = mrq;
1893 +
1894 + edm = bcm2835_sdhost_read(host, SDEDM);
1895 + fsm = edm & SDEDM_FSM_MASK;
1896 +
1897 + log_event("REQ<", (u32)mrq, edm);
1898 + if ((fsm != SDEDM_FSM_IDENTMODE) &&
1899 + (fsm != SDEDM_FSM_DATAMODE)) {
1900 + log_event("REQ!", (u32)mrq, edm);
1901 + if (host->debug) {
1902 + pr_warn("%s: previous command (%d) not complete (EDM %x)\n",
1903 + mmc_hostname(host->mmc),
1904 + bcm2835_sdhost_read(host, SDCMD) & SDCMD_CMD_MASK,
1905 + edm);
1906 + log_dump();
1907 + bcm2835_sdhost_dumpregs(host);
1908 + }
1909 + mrq->cmd->error = -EILSEQ;
1910 + tasklet_schedule(&host->finish_tasklet);
1911 + mmiowb();
1912 + spin_unlock_irqrestore(&host->lock, flags);
1913 + return;
1914 + }
1915 +
1916 + host->use_sbc = !!mrq->sbc &&
1917 + (host->mrq->data->flags & USE_CMD23_FLAGS);
1918 + if (host->use_sbc) {
1919 + if (bcm2835_sdhost_send_command(host, mrq->sbc)) {
1920 + if (!host->use_busy)
1921 + bcm2835_sdhost_finish_command(host, &flags);
1922 + }
1923 + } else if (bcm2835_sdhost_send_command(host, mrq->cmd)) {
1924 + if (host->data && host->dma_desc)
1925 + /* DMA transfer starts now, PIO starts after irq */
1926 + bcm2835_sdhost_start_dma(host);
1927 +
1928 + if (!host->use_busy)
1929 + bcm2835_sdhost_finish_command(host, &flags);
1930 + }
1931 +
1932 + log_event("CMD ", (u32)mrq->cmd->opcode,
1933 + mrq->data ? (u32)mrq->data->blksz : 0);
1934 + mmiowb();
1935 +
1936 + log_event("REQ>", (u32)mrq, 0);
1937 + spin_unlock_irqrestore(&host->lock, flags);
1938 +}
1939 +
1940 +static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1941 +{
1942 +
1943 + struct bcm2835_host *host = mmc_priv(mmc);
1944 + unsigned long flags;
1945 +
1946 + if (host->debug)
1947 + pr_info("%s: ios clock %d, pwr %d, bus_width %d, "
1948 + "timing %d, vdd %d, drv_type %d\n",
1949 + mmc_hostname(mmc),
1950 + ios->clock, ios->power_mode, ios->bus_width,
1951 + ios->timing, ios->signal_voltage, ios->drv_type);
1952 +
1953 + spin_lock_irqsave(&host->lock, flags);
1954 +
1955 + log_event("IOS<", ios->clock, 0);
1956 +
1957 + /* set bus width */
1958 + host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
1959 + if (ios->bus_width == MMC_BUS_WIDTH_4)
1960 + host->hcfg |= SDHCFG_WIDE_EXT_BUS;
1961 +
1962 + host->hcfg |= SDHCFG_WIDE_INT_BUS;
1963 +
1964 + /* Disable clever clock switching, to cope with fast core clocks */
1965 + host->hcfg |= SDHCFG_SLOW_CARD;
1966 +
1967 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1968 +
1969 + mmiowb();
1970 +
1971 + spin_unlock_irqrestore(&host->lock, flags);
1972 +
1973 + if (!ios->clock || ios->clock != host->clock)
1974 + bcm2835_sdhost_set_clock(host, ios->clock);
1975 +}
1976 +
1977 +static struct mmc_host_ops bcm2835_sdhost_ops = {
1978 + .request = bcm2835_sdhost_request,
1979 + .set_ios = bcm2835_sdhost_set_ios,
1980 + .hw_reset = bcm2835_sdhost_reset,
1981 +};
1982 +
1983 +static void bcm2835_sdhost_cmd_wait_work(struct work_struct *work)
1984 +{
1985 + struct bcm2835_host *host;
1986 + unsigned long flags;
1987 +
1988 + host = container_of(work, struct bcm2835_host, cmd_wait_wq);
1989 +
1990 + spin_lock_irqsave(&host->lock, flags);
1991 +
1992 + log_event("CWK<", (u32)host->cmd, (u32)host->mrq);
1993 +
1994 + /*
1995 + * If this tasklet gets rescheduled while running, it will
1996 + * be run again afterwards but without any active request.
1997 + */
1998 + if (!host->mrq) {
1999 + spin_unlock_irqrestore(&host->lock, flags);
2000 + return;
2001 + }
2002 +
2003 + bcm2835_sdhost_finish_command(host, &flags);
2004 +
2005 + mmiowb();
2006 +
2007 + log_event("CWK>", (u32)host->cmd, 0);
2008 +
2009 + spin_unlock_irqrestore(&host->lock, flags);
2010 +}
2011 +
2012 +static void bcm2835_sdhost_tasklet_finish(unsigned long param)
2013 +{
2014 + struct bcm2835_host *host;
2015 + unsigned long flags;
2016 + struct mmc_request *mrq;
2017 + struct dma_chan *terminate_chan = NULL;
2018 +
2019 + host = (struct bcm2835_host *)param;
2020 +
2021 + spin_lock_irqsave(&host->lock, flags);
2022 +
2023 + log_event("TSK<", (u32)host->mrq, 0);
2024 + /*
2025 + * If this tasklet gets rescheduled while running, it will
2026 + * be run again afterwards but without any active request.
2027 + */
2028 + if (!host->mrq) {
2029 + spin_unlock_irqrestore(&host->lock, flags);
2030 + return;
2031 + }
2032 +
2033 + del_timer(&host->timer);
2034 +
2035 + mrq = host->mrq;
2036 +
2037 + /* Drop the overclock after any data corruption, or after any
2038 + * error while overclocked. Ignore errors for status commands,
2039 + * as they are likely when a card is ejected. */
2040 + if (host->overclock) {
2041 + if ((mrq->cmd && mrq->cmd->error &&
2042 + (mrq->cmd->opcode != MMC_SEND_STATUS)) ||
2043 + (mrq->data && mrq->data->error) ||
2044 + (mrq->stop && mrq->stop->error) ||
2045 + (mrq->sbc && mrq->sbc->error)) {
2046 + host->overclock_50--;
2047 + pr_warn("%s: reducing overclock due to errors\n",
2048 + mmc_hostname(host->mmc));
2049 + host->reset_clock = 1;
2050 + mrq->cmd->error = -ETIMEDOUT;
2051 + mrq->cmd->retries = 1;
2052 + }
2053 + }
2054 +
2055 + host->mrq = NULL;
2056 + host->cmd = NULL;
2057 + host->data = NULL;
2058 +
2059 + mmiowb();
2060 +
2061 + host->dma_desc = NULL;
2062 + terminate_chan = host->dma_chan;
2063 + host->dma_chan = NULL;
2064 +
2065 + spin_unlock_irqrestore(&host->lock, flags);
2066 +
2067 + if (terminate_chan)
2068 + {
2069 + int err = dmaengine_terminate_all(terminate_chan);
2070 + if (err)
2071 + pr_err("%s: failed to terminate DMA (%d)\n",
2072 + mmc_hostname(host->mmc), err);
2073 + }
2074 +
2075 + /* The SDHOST block doesn't report any errors for a disconnected
2076 + interface. All cards and SDIO devices should report some supported
2077 + voltage range, so a zero response to SEND_OP_COND, IO_SEND_OP_COND
2078 + or APP_SEND_OP_COND can be treated as an error. */
2079 + if (((mrq->cmd->opcode == MMC_SEND_OP_COND) ||
2080 + (mrq->cmd->opcode == SD_IO_SEND_OP_COND) ||
2081 + (mrq->cmd->opcode == SD_APP_OP_COND)) &&
2082 + (mrq->cmd->error == 0) &&
2083 + (mrq->cmd->resp[0] == 0)) {
2084 + mrq->cmd->error = -ETIMEDOUT;
2085 + if (host->debug)
2086 + pr_info("%s: faking timeout due to zero OCR\n",
2087 + mmc_hostname(host->mmc));
2088 + }
2089 +
2090 + mmc_request_done(host->mmc, mrq);
2091 + log_event("TSK>", (u32)mrq, 0);
2092 +}
2093 +
2094 +int bcm2835_sdhost_add_host(struct bcm2835_host *host)
2095 +{
2096 + struct mmc_host *mmc;
2097 + struct dma_slave_config cfg;
2098 + char pio_limit_string[20];
2099 + int ret;
2100 +
2101 + mmc = host->mmc;
2102 +
2103 + mmc->f_max = host->max_clk;
2104 + mmc->f_min = host->max_clk / SDCDIV_MAX_CDIV;
2105 +
2106 + mmc->max_busy_timeout = (~(unsigned int)0)/(mmc->f_max/1000);
2107 +
2108 + pr_debug("f_max %d, f_min %d, max_busy_timeout %d\n",
2109 + mmc->f_max, mmc->f_min, mmc->max_busy_timeout);
2110 +
2111 + /* host controller capabilities */
2112 + mmc->caps |=
2113 + MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
2114 + MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET | MMC_CAP_ERASE |
2115 + ((ALLOW_CMD23_READ|ALLOW_CMD23_WRITE) * MMC_CAP_CMD23);
2116 +
2117 + spin_lock_init(&host->lock);
2118 +
2119 + if (host->allow_dma) {
2120 + if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
2121 + pr_err("%s: unable to initialise DMA channel. "
2122 + "Falling back to PIO\n",
2123 + mmc_hostname(mmc));
2124 + host->use_dma = false;
2125 + } else {
2126 + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2127 + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2128 + cfg.slave_id = 13; /* DREQ channel */
2129 +
2130 + /* Validate the slave configurations */
2131 +
2132 + cfg.direction = DMA_MEM_TO_DEV;
2133 + cfg.src_addr = 0;
2134 + cfg.dst_addr = host->bus_addr + SDDATA;
2135 +
2136 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
2137 +
2138 + if (ret == 0) {
2139 + host->dma_cfg_tx = cfg;
2140 +
2141 + cfg.direction = DMA_DEV_TO_MEM;
2142 + cfg.src_addr = host->bus_addr + SDDATA;
2143 + cfg.dst_addr = 0;
2144 +
2145 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
2146 + }
2147 +
2148 + if (ret == 0) {
2149 + host->dma_cfg_rx = cfg;
2150 +
2151 + host->use_dma = true;
2152 + } else {
2153 + pr_err("%s: unable to configure DMA channel. "
2154 + "Falling back to PIO\n",
2155 + mmc_hostname(mmc));
2156 + dma_release_channel(host->dma_chan_rxtx);
2157 + host->dma_chan_rxtx = NULL;
2158 + host->use_dma = false;
2159 + }
2160 + }
2161 + } else {
2162 + host->use_dma = false;
2163 + }
2164 +
2165 + mmc->max_segs = 128;
2166 + mmc->max_req_size = 524288;
2167 + mmc->max_seg_size = mmc->max_req_size;
2168 + mmc->max_blk_size = 512;
2169 + mmc->max_blk_count = 65535;
2170 +
2171 + /* report supported voltage ranges */
2172 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2173 +
2174 + tasklet_init(&host->finish_tasklet,
2175 + bcm2835_sdhost_tasklet_finish, (unsigned long)host);
2176 +
2177 + INIT_WORK(&host->cmd_wait_wq, bcm2835_sdhost_cmd_wait_work);
2178 +
2179 + setup_timer(&host->timer, bcm2835_sdhost_timeout,
2180 + (unsigned long)host);
2181 +
2182 + bcm2835_sdhost_init(host, 0);
2183 +
2184 + ret = request_irq(host->irq, bcm2835_sdhost_irq, 0 /*IRQF_SHARED*/,
2185 + mmc_hostname(mmc), host);
2186 + if (ret) {
2187 + pr_err("%s: failed to request IRQ %d: %d\n",
2188 + mmc_hostname(mmc), host->irq, ret);
2189 + goto untasklet;
2190 + }
2191 +
2192 + mmiowb();
2193 + mmc_add_host(mmc);
2194 +
2195 + pio_limit_string[0] = '\0';
2196 + if (host->use_dma && (host->pio_limit > 0))
2197 + sprintf(pio_limit_string, " (>%d)", host->pio_limit);
2198 + pr_info("%s: %s loaded - DMA %s%s\n",
2199 + mmc_hostname(mmc), DRIVER_NAME,
2200 + host->use_dma ? "enabled" : "disabled",
2201 + pio_limit_string);
2202 +
2203 + return 0;
2204 +
2205 +untasklet:
2206 + tasklet_kill(&host->finish_tasklet);
2207 +
2208 + return ret;
2209 +}
2210 +
2211 +static int bcm2835_sdhost_probe(struct platform_device *pdev)
2212 +{
2213 + struct device *dev = &pdev->dev;
2214 + struct device_node *node = dev->of_node;
2215 + struct clk *clk;
2216 + struct resource *iomem;
2217 + struct bcm2835_host *host;
2218 + struct mmc_host *mmc;
2219 + const __be32 *addr;
2220 + u32 msg[3];
2221 + int ret;
2222 +
2223 + pr_debug("bcm2835_sdhost_probe\n");
2224 + mmc = mmc_alloc_host(sizeof(*host), dev);
2225 + if (!mmc)
2226 + return -ENOMEM;
2227 +
2228 + mmc->ops = &bcm2835_sdhost_ops;
2229 + host = mmc_priv(mmc);
2230 + host->mmc = mmc;
2231 + host->pio_timeout = msecs_to_jiffies(500);
2232 + host->pio_limit = 1;
2233 + host->max_delay = 1; /* Warn if over 1ms */
2234 + host->allow_dma = 1;
2235 + spin_lock_init(&host->lock);
2236 +
2237 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2238 + host->ioaddr = devm_ioremap_resource(dev, iomem);
2239 + if (IS_ERR(host->ioaddr)) {
2240 + ret = PTR_ERR(host->ioaddr);
2241 + goto err;
2242 + }
2243 +
2244 + addr = of_get_address(node, 0, NULL, NULL);
2245 + if (!addr) {
2246 + dev_err(dev, "could not get DMA-register address\n");
2247 + return -ENODEV;
2248 + }
2249 + host->bus_addr = be32_to_cpup(addr);
2250 + pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
2251 + (unsigned long)host->ioaddr,
2252 + (unsigned long)iomem->start,
2253 + (unsigned long)host->bus_addr);
2254 +
2255 + if (node) {
2256 + /* Read any custom properties */
2257 + of_property_read_u32(node,
2258 + "brcm,delay-after-stop",
2259 + &host->delay_after_stop);
2260 + of_property_read_u32(node,
2261 + "brcm,overclock-50",
2262 + &host->user_overclock_50);
2263 + of_property_read_u32(node,
2264 + "brcm,pio-limit",
2265 + &host->pio_limit);
2266 + host->allow_dma =
2267 + !of_property_read_bool(node, "brcm,force-pio");
2268 + host->debug = of_property_read_bool(node, "brcm,debug");
2269 + }
2270 +
2271 + host->dma_chan = NULL;
2272 + host->dma_desc = NULL;
2273 +
2274 + /* Formally recognise the other way of disabling DMA */
2275 + if (host->pio_limit == 0x7fffffff)
2276 + host->allow_dma = false;
2277 +
2278 + if (host->allow_dma) {
2279 + if (node) {
2280 + host->dma_chan_rxtx =
2281 + dma_request_slave_channel(dev, "rx-tx");
2282 + if (!host->dma_chan_rxtx)
2283 + host->dma_chan_rxtx =
2284 + dma_request_slave_channel(dev, "tx");
2285 + if (!host->dma_chan_rxtx)
2286 + host->dma_chan_rxtx =
2287 + dma_request_slave_channel(dev, "rx");
2288 + } else {
2289 + dma_cap_mask_t mask;
2290 +
2291 + dma_cap_zero(mask);
2292 + /* we don't care about the channel, any would work */
2293 + dma_cap_set(DMA_SLAVE, mask);
2294 + host->dma_chan_rxtx =
2295 + dma_request_channel(mask, NULL, NULL);
2296 + }
2297 + }
2298 +
2299 + clk = devm_clk_get(dev, NULL);
2300 + if (IS_ERR(clk)) {
2301 + ret = PTR_ERR(clk);
2302 + if (ret == -EPROBE_DEFER)
2303 + dev_info(dev, "could not get clk, deferring probe\n");
2304 + else
2305 + dev_err(dev, "could not get clk\n");
2306 + goto err;
2307 + }
2308 +
2309 + host->max_clk = clk_get_rate(clk);
2310 +
2311 + host->irq = platform_get_irq(pdev, 0);
2312 + if (host->irq <= 0) {
2313 + dev_err(dev, "get IRQ failed\n");
2314 + ret = -EINVAL;
2315 + goto err;
2316 + }
2317 +
2318 + pr_debug(" - max_clk %lx, irq %d\n",
2319 + (unsigned long)host->max_clk,
2320 + (int)host->irq);
2321 +
2322 + log_init(dev, iomem->start - host->bus_addr);
2323 +
2324 + if (node)
2325 + mmc_of_parse(mmc);
2326 + else
2327 + mmc->caps |= MMC_CAP_4_BIT_DATA;
2328 +
2329 + msg[0] = 0;
2330 + msg[1] = ~0;
2331 + msg[2] = ~0;
2332 +
2333 + rpi_firmware_property(rpi_firmware_get(NULL),
2334 + RPI_FIRMWARE_SET_SDHOST_CLOCK,
2335 + &msg, sizeof(msg));
2336 +
2337 + host->firmware_sets_cdiv = (msg[1] != ~0);
2338 +
2339 + ret = bcm2835_sdhost_add_host(host);
2340 + if (ret)
2341 + goto err;
2342 +
2343 + platform_set_drvdata(pdev, host);
2344 +
2345 + pr_debug("bcm2835_sdhost_probe -> OK\n");
2346 +
2347 + return 0;
2348 +
2349 +err:
2350 + pr_debug("bcm2835_sdhost_probe -> err %d\n", ret);
2351 + mmc_free_host(mmc);
2352 +
2353 + return ret;
2354 +}
2355 +
2356 +static int bcm2835_sdhost_remove(struct platform_device *pdev)
2357 +{
2358 + struct bcm2835_host *host = platform_get_drvdata(pdev);
2359 +
2360 + pr_debug("bcm2835_sdhost_remove\n");
2361 +
2362 + mmc_remove_host(host->mmc);
2363 +
2364 + bcm2835_sdhost_set_power(host, false);
2365 +
2366 + free_irq(host->irq, host);
2367 +
2368 + del_timer_sync(&host->timer);
2369 +
2370 + tasklet_kill(&host->finish_tasklet);
2371 +
2372 + mmc_free_host(host->mmc);
2373 + platform_set_drvdata(pdev, NULL);
2374 +
2375 + pr_debug("bcm2835_sdhost_remove - OK\n");
2376 + return 0;
2377 +}
2378 +
2379 +static const struct of_device_id bcm2835_sdhost_match[] = {
2380 + { .compatible = "brcm,bcm2835-sdhost" },
2381 + { }
2382 +};
2383 +MODULE_DEVICE_TABLE(of, bcm2835_sdhost_match);
2384 +
2385 +static struct platform_driver bcm2835_sdhost_driver = {
2386 + .probe = bcm2835_sdhost_probe,
2387 + .remove = bcm2835_sdhost_remove,
2388 + .driver = {
2389 + .name = DRIVER_NAME,
2390 + .owner = THIS_MODULE,
2391 + .of_match_table = bcm2835_sdhost_match,
2392 + },
2393 +};
2394 +module_platform_driver(bcm2835_sdhost_driver);
2395 +
2396 +MODULE_ALIAS("platform:sdhost-bcm2835");
2397 +MODULE_DESCRIPTION("BCM2835 SDHost driver");
2398 +MODULE_LICENSE("GPL v2");
2399 +MODULE_AUTHOR("Phil Elwell");