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