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