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