6a9c39662f527d1c3168e3acb6969d72bf608cf5
[openwrt/staging/blocktrron.git] /
1 From 0c3db37a3b78df1459c771d559a1fb577d36f5af 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 | 2209 +++++++++++++++++++++++++++++
259 3 files changed, 2220 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 @@ -23,6 +23,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,2209 @@
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 +#if 0 // todo fix
742 +static void bcm2835_sdhost_reset(struct mmc_host *mmc)
743 +{
744 + struct bcm2835_host *host = mmc_priv(mmc);
745 + unsigned long flags;
746 + spin_lock_irqsave(&host->lock, flags);
747 + log_event("RST<", 0, 0);
748 +
749 + bcm2835_sdhost_reset_internal(host);
750 +
751 + spin_unlock_irqrestore(&host->lock, flags);
752 +}
753 +#endif
754 +
755 +static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
756 +
757 +static void bcm2835_sdhost_init(struct bcm2835_host *host, int soft)
758 +{
759 + pr_debug("bcm2835_sdhost_init(%d)\n", soft);
760 +
761 + /* Set interrupt enables */
762 + host->hcfg = SDHCFG_BUSY_IRPT_EN;
763 +
764 + bcm2835_sdhost_reset_internal(host);
765 +
766 + if (soft) {
767 + /* force clock reconfiguration */
768 + host->clock = 0;
769 + bcm2835_sdhost_set_ios(host->mmc, &host->mmc->ios);
770 + }
771 +}
772 +
773 +static void bcm2835_sdhost_wait_transfer_complete(struct bcm2835_host *host)
774 +{
775 + int timediff;
776 + u32 alternate_idle;
777 + u32 edm;
778 +
779 + alternate_idle = (host->mrq->data->flags & MMC_DATA_READ) ?
780 + SDEDM_FSM_READWAIT : SDEDM_FSM_WRITESTART1;
781 +
782 + edm = bcm2835_sdhost_read(host, SDEDM);
783 +
784 + log_event("WTC<", edm, 0);
785 +
786 + timediff = 0;
787 +
788 + while (1) {
789 + u32 fsm = edm & SDEDM_FSM_MASK;
790 + if ((fsm == SDEDM_FSM_IDENTMODE) ||
791 + (fsm == SDEDM_FSM_DATAMODE))
792 + break;
793 + if (fsm == alternate_idle) {
794 + bcm2835_sdhost_write(host,
795 + edm | SDEDM_FORCE_DATA_MODE,
796 + SDEDM);
797 + break;
798 + }
799 +
800 + timediff++;
801 + if (timediff == 100000) {
802 + pr_err("%s: wait_transfer_complete - still waiting after %d retries\n",
803 + mmc_hostname(host->mmc),
804 + timediff);
805 + log_dump();
806 + bcm2835_sdhost_dumpregs(host);
807 + host->mrq->data->error = -ETIMEDOUT;
808 + log_event("WTC!", edm, 0);
809 + return;
810 + }
811 + cpu_relax();
812 + edm = bcm2835_sdhost_read(host, SDEDM);
813 + }
814 + log_event("WTC>", edm, 0);
815 +}
816 +
817 +static void bcm2835_sdhost_finish_data(struct bcm2835_host *host);
818 +
819 +static void bcm2835_sdhost_dma_complete(void *param)
820 +{
821 + struct bcm2835_host *host = param;
822 + struct mmc_data *data = host->data;
823 + unsigned long flags;
824 +
825 + spin_lock_irqsave(&host->lock, flags);
826 + log_event("DMA<", host->data, bcm2835_sdhost_read(host, SDHSTS));
827 + log_event("DMA ", bcm2835_sdhost_read(host, SDCMD),
828 + bcm2835_sdhost_read(host, SDEDM));
829 +
830 + if (host->dma_chan) {
831 + dma_unmap_sg(host->dma_chan->device->dev,
832 + data->sg, data->sg_len,
833 + host->dma_dir);
834 +
835 + host->dma_chan = NULL;
836 + }
837 +
838 + if (host->drain_words) {
839 + void *page;
840 + u32 *buf;
841 +
842 + if (host->drain_offset & PAGE_MASK) {
843 + host->drain_page += host->drain_offset >> PAGE_SHIFT;
844 + host->drain_offset &= ~PAGE_MASK;
845 + }
846 +
847 + page = kmap_atomic(host->drain_page);
848 + buf = page + host->drain_offset;
849 +
850 + while (host->drain_words) {
851 + u32 edm = bcm2835_sdhost_read(host, SDEDM);
852 + if ((edm >> 4) & 0x1f)
853 + *(buf++) = bcm2835_sdhost_read(host,
854 + SDDATA);
855 + host->drain_words--;
856 + }
857 +
858 + kunmap_atomic(page);
859 + }
860 +
861 + bcm2835_sdhost_finish_data(host);
862 +
863 + log_event("DMA>", host->data, 0);
864 + spin_unlock_irqrestore(&host->lock, flags);
865 +}
866 +
867 +static void bcm2835_sdhost_read_block_pio(struct bcm2835_host *host)
868 +{
869 + unsigned long flags;
870 + size_t blksize, len;
871 + u32 *buf;
872 + unsigned long wait_max;
873 +
874 + blksize = host->data->blksz;
875 +
876 + wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
877 +
878 + local_irq_save(flags);
879 +
880 + while (blksize) {
881 + int copy_words;
882 + u32 hsts = 0;
883 +
884 + if (!sg_miter_next(&host->sg_miter)) {
885 + host->data->error = -EINVAL;
886 + break;
887 + }
888 +
889 + len = min(host->sg_miter.length, blksize);
890 + if (len % 4) {
891 + host->data->error = -EINVAL;
892 + break;
893 + }
894 +
895 + blksize -= len;
896 + host->sg_miter.consumed = len;
897 +
898 + buf = (u32 *)host->sg_miter.addr;
899 +
900 + copy_words = len/4;
901 +
902 + while (copy_words) {
903 + int burst_words, words;
904 + u32 edm;
905 +
906 + burst_words = SDDATA_FIFO_PIO_BURST;
907 + if (burst_words > copy_words)
908 + burst_words = copy_words;
909 + edm = bcm2835_sdhost_read(host, SDEDM);
910 + words = ((edm >> 4) & 0x1f);
911 +
912 + if (words < burst_words) {
913 + int fsm_state = (edm & SDEDM_FSM_MASK);
914 + if ((fsm_state != SDEDM_FSM_READDATA) &&
915 + (fsm_state != SDEDM_FSM_READWAIT) &&
916 + (fsm_state != SDEDM_FSM_READCRC)) {
917 + hsts = bcm2835_sdhost_read(host,
918 + SDHSTS);
919 + pr_info("%s: fsm %x, hsts %x\n",
920 + mmc_hostname(host->mmc),
921 + fsm_state, hsts);
922 + if (hsts & SDHSTS_ERROR_MASK)
923 + break;
924 + }
925 +
926 + if (time_after(jiffies, wait_max)) {
927 + pr_err("%s: PIO read timeout - EDM %x\n",
928 + mmc_hostname(host->mmc),
929 + edm);
930 + hsts = SDHSTS_REW_TIME_OUT;
931 + break;
932 + }
933 + ndelay((burst_words - words) *
934 + host->ns_per_fifo_word);
935 + continue;
936 + } else if (words > copy_words) {
937 + words = copy_words;
938 + }
939 +
940 + copy_words -= words;
941 +
942 + while (words) {
943 + *(buf++) = bcm2835_sdhost_read(host, SDDATA);
944 + words--;
945 + }
946 + }
947 +
948 + if (hsts & SDHSTS_ERROR_MASK)
949 + break;
950 + }
951 +
952 + sg_miter_stop(&host->sg_miter);
953 +
954 + local_irq_restore(flags);
955 +}
956 +
957 +static void bcm2835_sdhost_write_block_pio(struct bcm2835_host *host)
958 +{
959 + unsigned long flags;
960 + size_t blksize, len;
961 + u32 *buf;
962 + unsigned long wait_max;
963 +
964 + blksize = host->data->blksz;
965 +
966 + wait_max = jiffies + msecs_to_jiffies(host->pio_timeout);
967 +
968 + local_irq_save(flags);
969 +
970 + while (blksize) {
971 + int copy_words;
972 + u32 hsts = 0;
973 +
974 + if (!sg_miter_next(&host->sg_miter)) {
975 + host->data->error = -EINVAL;
976 + break;
977 + }
978 +
979 + len = min(host->sg_miter.length, blksize);
980 + if (len % 4) {
981 + host->data->error = -EINVAL;
982 + break;
983 + }
984 +
985 + blksize -= len;
986 + host->sg_miter.consumed = len;
987 +
988 + buf = (u32 *)host->sg_miter.addr;
989 +
990 + copy_words = len/4;
991 +
992 + while (copy_words) {
993 + int burst_words, words;
994 + u32 edm;
995 +
996 + burst_words = SDDATA_FIFO_PIO_BURST;
997 + if (burst_words > copy_words)
998 + burst_words = copy_words;
999 + edm = bcm2835_sdhost_read(host, SDEDM);
1000 + words = SDDATA_FIFO_WORDS - ((edm >> 4) & 0x1f);
1001 +
1002 + if (words < burst_words) {
1003 + int fsm_state = (edm & SDEDM_FSM_MASK);
1004 + if ((fsm_state != SDEDM_FSM_WRITEDATA) &&
1005 + (fsm_state != SDEDM_FSM_WRITESTART1) &&
1006 + (fsm_state != SDEDM_FSM_WRITESTART2)) {
1007 + hsts = bcm2835_sdhost_read(host,
1008 + SDHSTS);
1009 + pr_info("%s: fsm %x, hsts %x\n",
1010 + mmc_hostname(host->mmc),
1011 + fsm_state, hsts);
1012 + if (hsts & SDHSTS_ERROR_MASK)
1013 + break;
1014 + }
1015 +
1016 + if (time_after(jiffies, wait_max)) {
1017 + pr_err("%s: PIO write timeout - EDM %x\n",
1018 + mmc_hostname(host->mmc),
1019 + edm);
1020 + hsts = SDHSTS_REW_TIME_OUT;
1021 + break;
1022 + }
1023 + ndelay((burst_words - words) *
1024 + host->ns_per_fifo_word);
1025 + continue;
1026 + } else if (words > copy_words) {
1027 + words = copy_words;
1028 + }
1029 +
1030 + copy_words -= words;
1031 +
1032 + while (words) {
1033 + bcm2835_sdhost_write(host, *(buf++), SDDATA);
1034 + words--;
1035 + }
1036 + }
1037 +
1038 + if (hsts & SDHSTS_ERROR_MASK)
1039 + break;
1040 + }
1041 +
1042 + sg_miter_stop(&host->sg_miter);
1043 +
1044 + local_irq_restore(flags);
1045 +}
1046 +
1047 +static void bcm2835_sdhost_transfer_pio(struct bcm2835_host *host)
1048 +{
1049 + u32 sdhsts;
1050 + bool is_read;
1051 + BUG_ON(!host->data);
1052 + log_event("XFP<", host->data, host->blocks);
1053 +
1054 + is_read = (host->data->flags & MMC_DATA_READ) != 0;
1055 + if (is_read)
1056 + bcm2835_sdhost_read_block_pio(host);
1057 + else
1058 + bcm2835_sdhost_write_block_pio(host);
1059 +
1060 + sdhsts = bcm2835_sdhost_read(host, SDHSTS);
1061 + if (sdhsts & (SDHSTS_CRC16_ERROR |
1062 + SDHSTS_CRC7_ERROR |
1063 + SDHSTS_FIFO_ERROR)) {
1064 + pr_err("%s: %s transfer error - HSTS %x\n",
1065 + mmc_hostname(host->mmc),
1066 + is_read ? "read" : "write",
1067 + sdhsts);
1068 + host->data->error = -EILSEQ;
1069 + } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT |
1070 + SDHSTS_REW_TIME_OUT))) {
1071 + pr_err("%s: %s timeout error - HSTS %x\n",
1072 + mmc_hostname(host->mmc),
1073 + is_read ? "read" : "write",
1074 + sdhsts);
1075 + host->data->error = -ETIMEDOUT;
1076 + }
1077 + log_event("XFP>", host->data, host->blocks);
1078 +}
1079 +
1080 +static void bcm2835_sdhost_prepare_dma(struct bcm2835_host *host,
1081 + struct mmc_data *data)
1082 +{
1083 + int len, dir_data, dir_slave;
1084 + struct dma_async_tx_descriptor *desc = NULL;
1085 + struct dma_chan *dma_chan;
1086 +
1087 + log_event("PRD<", data, 0);
1088 + pr_debug("bcm2835_sdhost_prepare_dma()\n");
1089 +
1090 + dma_chan = host->dma_chan_rxtx;
1091 + if (data->flags & MMC_DATA_READ) {
1092 + dir_data = DMA_FROM_DEVICE;
1093 + dir_slave = DMA_DEV_TO_MEM;
1094 + } else {
1095 + dir_data = DMA_TO_DEVICE;
1096 + dir_slave = DMA_MEM_TO_DEV;
1097 + }
1098 + log_event("PRD1", dma_chan, 0);
1099 +
1100 + BUG_ON(!dma_chan->device);
1101 + BUG_ON(!dma_chan->device->dev);
1102 + BUG_ON(!data->sg);
1103 +
1104 + /* The block doesn't manage the FIFO DREQs properly for multi-block
1105 + transfers, so don't attempt to DMA the final few words.
1106 + Unfortunately this requires the final sg entry to be trimmed.
1107 + N.B. This code demands that the overspill is contained in
1108 + a single sg entry.
1109 + */
1110 +
1111 + host->drain_words = 0;
1112 + if ((data->blocks > 1) && (dir_data == DMA_FROM_DEVICE)) {
1113 + struct scatterlist *sg;
1114 + u32 len;
1115 + int i;
1116 +
1117 + len = min((u32)(FIFO_READ_THRESHOLD - 1) * 4,
1118 + (u32)data->blocks * data->blksz);
1119 +
1120 + for_each_sg(data->sg, sg, data->sg_len, i) {
1121 + if (sg_is_last(sg)) {
1122 + BUG_ON(sg->length < len);
1123 + sg->length -= len;
1124 + host->drain_page = sg_page(sg);
1125 + host->drain_offset = sg->offset + sg->length;
1126 + }
1127 + }
1128 + host->drain_words = len/4;
1129 + }
1130 +
1131 + /* The parameters have already been validated, so this will not fail */
1132 + (void)dmaengine_slave_config(dma_chan,
1133 + (dir_data == DMA_FROM_DEVICE) ?
1134 + &host->dma_cfg_rx :
1135 + &host->dma_cfg_tx);
1136 +
1137 + len = dma_map_sg(dma_chan->device->dev, data->sg, data->sg_len,
1138 + dir_data);
1139 +
1140 + log_event("PRD2", len, 0);
1141 + if (len > 0)
1142 + desc = dmaengine_prep_slave_sg(dma_chan, data->sg,
1143 + len, dir_slave,
1144 + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1145 + log_event("PRD3", desc, 0);
1146 +
1147 + if (desc) {
1148 + desc->callback = bcm2835_sdhost_dma_complete;
1149 + desc->callback_param = host;
1150 + host->dma_desc = desc;
1151 + host->dma_chan = dma_chan;
1152 + host->dma_dir = dir_data;
1153 + }
1154 + log_event("PDM>", data, 0);
1155 +}
1156 +
1157 +static void bcm2835_sdhost_start_dma(struct bcm2835_host *host)
1158 +{
1159 + log_event("SDMA", host->data, host->dma_chan);
1160 + dmaengine_submit(host->dma_desc);
1161 + dma_async_issue_pending(host->dma_chan);
1162 +}
1163 +
1164 +static void bcm2835_sdhost_set_transfer_irqs(struct bcm2835_host *host)
1165 +{
1166 + u32 all_irqs = SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN |
1167 + SDHCFG_BUSY_IRPT_EN;
1168 + if (host->dma_desc)
1169 + host->hcfg = (host->hcfg & ~all_irqs) |
1170 + SDHCFG_BUSY_IRPT_EN;
1171 + else
1172 + host->hcfg = (host->hcfg & ~all_irqs) |
1173 + SDHCFG_DATA_IRPT_EN |
1174 + SDHCFG_BUSY_IRPT_EN;
1175 +
1176 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1177 +}
1178 +
1179 +static void bcm2835_sdhost_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
1180 +{
1181 + struct mmc_data *data = cmd->data;
1182 +
1183 + WARN_ON(host->data);
1184 +
1185 + host->data = data;
1186 + if (!data)
1187 + return;
1188 +
1189 + /* Sanity checks */
1190 + BUG_ON(data->blksz * data->blocks > 524288);
1191 + BUG_ON(data->blksz > host->mmc->max_blk_size);
1192 + BUG_ON(data->blocks > 65535);
1193 +
1194 + host->data_complete = 0;
1195 + host->flush_fifo = 0;
1196 + host->data->bytes_xfered = 0;
1197 +
1198 + if (!host->sectors && host->mmc->card) {
1199 + struct mmc_card *card = host->mmc->card;
1200 + if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1201 + /*
1202 + * The EXT_CSD sector count is in number of 512 byte
1203 + * sectors.
1204 + */
1205 + host->sectors = card->ext_csd.sectors;
1206 + } else {
1207 + /*
1208 + * The CSD capacity field is in units of read_blkbits.
1209 + * set_capacity takes units of 512 bytes.
1210 + */
1211 + host->sectors = card->csd.capacity <<
1212 + (card->csd.read_blkbits - 9);
1213 + }
1214 + }
1215 +
1216 + if (!host->dma_desc) {
1217 + /* Use PIO */
1218 + int flags = SG_MITER_ATOMIC;
1219 +
1220 + if (data->flags & MMC_DATA_READ)
1221 + flags |= SG_MITER_TO_SG;
1222 + else
1223 + flags |= SG_MITER_FROM_SG;
1224 + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
1225 + host->blocks = data->blocks;
1226 + }
1227 +
1228 + bcm2835_sdhost_set_transfer_irqs(host);
1229 +
1230 + bcm2835_sdhost_write(host, data->blksz, SDHBCT);
1231 + bcm2835_sdhost_write(host, data->blocks, SDHBLC);
1232 +
1233 + BUG_ON(!host->data);
1234 +}
1235 +
1236 +bool bcm2835_sdhost_send_command(struct bcm2835_host *host,
1237 + struct mmc_command *cmd)
1238 +{
1239 + u32 sdcmd, sdhsts;
1240 + unsigned long timeout;
1241 + int delay;
1242 +
1243 + WARN_ON(host->cmd);
1244 + log_event("CMD<", cmd->opcode, cmd->arg);
1245 +
1246 + if (cmd->data)
1247 + pr_debug("%s: send_command %d 0x%x "
1248 + "(flags 0x%x) - %s %d*%d\n",
1249 + mmc_hostname(host->mmc),
1250 + cmd->opcode, cmd->arg, cmd->flags,
1251 + (cmd->data->flags & MMC_DATA_READ) ?
1252 + "read" : "write", cmd->data->blocks,
1253 + cmd->data->blksz);
1254 + else
1255 + pr_debug("%s: send_command %d 0x%x (flags 0x%x)\n",
1256 + mmc_hostname(host->mmc),
1257 + cmd->opcode, cmd->arg, cmd->flags);
1258 +
1259 + /* Wait max 100 ms */
1260 + timeout = 10000;
1261 +
1262 + while (bcm2835_sdhost_read(host, SDCMD) & SDCMD_NEW_FLAG) {
1263 + if (timeout == 0) {
1264 + pr_warn("%s: previous command never completed.\n",
1265 + mmc_hostname(host->mmc));
1266 + if (host->debug)
1267 + bcm2835_sdhost_dumpregs(host);
1268 + cmd->error = -EILSEQ;
1269 + tasklet_schedule(&host->finish_tasklet);
1270 + return false;
1271 + }
1272 + timeout--;
1273 + udelay(10);
1274 + }
1275 +
1276 + delay = (10000 - timeout)/100;
1277 + if (delay > host->max_delay) {
1278 + host->max_delay = delay;
1279 + pr_warn("%s: controller hung for %d ms\n",
1280 + mmc_hostname(host->mmc),
1281 + host->max_delay);
1282 + }
1283 +
1284 + timeout = jiffies;
1285 + if (!cmd->data && cmd->busy_timeout > 9000)
1286 + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1287 + else
1288 + timeout += 10 * HZ;
1289 + mod_timer(&host->timer, timeout);
1290 +
1291 + host->cmd = cmd;
1292 +
1293 + /* Clear any error flags */
1294 + sdhsts = bcm2835_sdhost_read(host, SDHSTS);
1295 + if (sdhsts & SDHSTS_ERROR_MASK)
1296 + bcm2835_sdhost_write(host, sdhsts, SDHSTS);
1297 +
1298 + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1299 + pr_err("%s: unsupported response type!\n",
1300 + mmc_hostname(host->mmc));
1301 + cmd->error = -EINVAL;
1302 + tasklet_schedule(&host->finish_tasklet);
1303 + return false;
1304 + }
1305 +
1306 + bcm2835_sdhost_prepare_data(host, cmd);
1307 +
1308 + bcm2835_sdhost_write(host, cmd->arg, SDARG);
1309 +
1310 + sdcmd = cmd->opcode & SDCMD_CMD_MASK;
1311 +
1312 + host->use_busy = 0;
1313 + if (!(cmd->flags & MMC_RSP_PRESENT)) {
1314 + sdcmd |= SDCMD_NO_RESPONSE;
1315 + } else {
1316 + if (cmd->flags & MMC_RSP_136)
1317 + sdcmd |= SDCMD_LONG_RESPONSE;
1318 + if (cmd->flags & MMC_RSP_BUSY) {
1319 + sdcmd |= SDCMD_BUSYWAIT;
1320 + host->use_busy = 1;
1321 + }
1322 + }
1323 +
1324 + if (cmd->data) {
1325 + log_event("CMDD", cmd->data->blocks, cmd->data->blksz);
1326 + if (host->delay_after_this_stop) {
1327 + struct timespec64 now;
1328 + int time_since_stop;
1329 +
1330 + ktime_get_real_ts64(&now);
1331 + time_since_stop = now.tv_sec - host->stop_time.tv_sec;
1332 + if (time_since_stop < 2) {
1333 + /* Possibly less than one second */
1334 + time_since_stop = time_since_stop * 1000000 +
1335 + (now.tv_nsec - host->stop_time.tv_nsec)/1000;
1336 + if (time_since_stop <
1337 + host->delay_after_this_stop)
1338 + udelay(host->delay_after_this_stop -
1339 + time_since_stop);
1340 + }
1341 + }
1342 +
1343 + host->delay_after_this_stop = host->delay_after_stop;
1344 + if ((cmd->data->flags & MMC_DATA_READ) && !host->use_sbc) {
1345 + /* See if read crosses one of the hazardous sectors */
1346 + u32 first_blk, last_blk;
1347 +
1348 + /* Intentionally include the following sector because
1349 + without CMD23/SBC the read may run on. */
1350 + first_blk = host->mrq->cmd->arg;
1351 + last_blk = first_blk + cmd->data->blocks;
1352 +
1353 + if (((last_blk >= (host->sectors - 64)) &&
1354 + (first_blk <= (host->sectors - 64))) ||
1355 + ((last_blk >= (host->sectors - 32)) &&
1356 + (first_blk <= (host->sectors - 32)))) {
1357 + host->delay_after_this_stop =
1358 + max(250u, host->delay_after_stop);
1359 + }
1360 + }
1361 +
1362 + if (cmd->data->flags & MMC_DATA_WRITE)
1363 + sdcmd |= SDCMD_WRITE_CMD;
1364 + if (cmd->data->flags & MMC_DATA_READ)
1365 + sdcmd |= SDCMD_READ_CMD;
1366 + }
1367 +
1368 + bcm2835_sdhost_write(host, sdcmd | SDCMD_NEW_FLAG, SDCMD);
1369 +
1370 + return true;
1371 +}
1372 +
1373 +static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
1374 + unsigned long *irq_flags);
1375 +static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host);
1376 +
1377 +static void bcm2835_sdhost_finish_data(struct bcm2835_host *host)
1378 +{
1379 + struct mmc_data *data;
1380 +
1381 + data = host->data;
1382 + BUG_ON(!data);
1383 +
1384 + log_event("FDA<", host->mrq, host->cmd);
1385 + pr_debug("finish_data(error %d, stop %d, sbc %d)\n",
1386 + data->error, data->stop ? 1 : 0,
1387 + host->mrq->sbc ? 1 : 0);
1388 +
1389 + host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
1390 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1391 +
1392 + data->bytes_xfered = data->error ? 0 : (data->blksz * data->blocks);
1393 +
1394 + host->data_complete = 1;
1395 +
1396 + if (host->cmd) {
1397 + /*
1398 + * Data managed to finish before the
1399 + * command completed. Make sure we do
1400 + * things in the proper order.
1401 + */
1402 + pr_debug("Finished early - HSTS %x\n",
1403 + bcm2835_sdhost_read(host, SDHSTS));
1404 + }
1405 + else
1406 + bcm2835_sdhost_transfer_complete(host);
1407 + log_event("FDA>", host->mrq, host->cmd);
1408 +}
1409 +
1410 +static void bcm2835_sdhost_transfer_complete(struct bcm2835_host *host)
1411 +{
1412 + struct mmc_data *data;
1413 +
1414 + BUG_ON(host->cmd);
1415 + BUG_ON(!host->data);
1416 + BUG_ON(!host->data_complete);
1417 +
1418 + data = host->data;
1419 + host->data = NULL;
1420 +
1421 + log_event("TCM<", data, data->error);
1422 + pr_debug("transfer_complete(error %d, stop %d)\n",
1423 + data->error, data->stop ? 1 : 0);
1424 +
1425 + /*
1426 + * Need to send CMD12 if -
1427 + * a) open-ended multiblock transfer (no CMD23)
1428 + * b) error in multiblock transfer
1429 + */
1430 + if (host->mrq->stop && (data->error || !host->use_sbc)) {
1431 + if (bcm2835_sdhost_send_command(host, host->mrq->stop)) {
1432 + /* No busy, so poll for completion */
1433 + if (!host->use_busy)
1434 + bcm2835_sdhost_finish_command(host, NULL);
1435 +
1436 + if (host->delay_after_this_stop)
1437 + ktime_get_real_ts64(&host->stop_time);
1438 + }
1439 + } else {
1440 + bcm2835_sdhost_wait_transfer_complete(host);
1441 + tasklet_schedule(&host->finish_tasklet);
1442 + }
1443 + log_event("TCM>", data, 0);
1444 +}
1445 +
1446 +/* If irq_flags is valid, the caller is in a thread context and is allowed
1447 + to sleep */
1448 +static void bcm2835_sdhost_finish_command(struct bcm2835_host *host,
1449 + unsigned long *irq_flags)
1450 +{
1451 + u32 sdcmd;
1452 + u32 retries;
1453 +#ifdef DEBUG
1454 + struct timespec64 before, after;
1455 + int timediff = 0;
1456 +#endif
1457 +
1458 + log_event("FCM<", host->mrq, host->cmd);
1459 + pr_debug("finish_command(%x)\n", bcm2835_sdhost_read(host, SDCMD));
1460 +
1461 + BUG_ON(!host->cmd || !host->mrq);
1462 +
1463 + /* Poll quickly at first */
1464 +
1465 + retries = host->cmd_quick_poll_retries;
1466 + if (!retries) {
1467 + /* Work out how many polls take 1us by timing 10us */
1468 + struct timespec64 start, now;
1469 + int us_diff;
1470 +
1471 + retries = 1;
1472 + do {
1473 + int i;
1474 +
1475 + retries *= 2;
1476 +
1477 + ktime_get_real_ts64(&start);
1478 +
1479 + for (i = 0; i < retries; i++) {
1480 + cpu_relax();
1481 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1482 + }
1483 +
1484 + ktime_get_real_ts64(&now);
1485 + us_diff = (now.tv_sec - start.tv_sec) * 1000000 +
1486 + (now.tv_nsec - start.tv_nsec)/1000;
1487 + } while (us_diff < 10);
1488 +
1489 + host->cmd_quick_poll_retries = ((retries * us_diff + 9)*CMD_DALLY_US)/10 + 1;
1490 + retries = 1; // We've already waited long enough this time
1491 + }
1492 +
1493 + for (sdcmd = bcm2835_sdhost_read(host, SDCMD);
1494 + (sdcmd & SDCMD_NEW_FLAG) && retries;
1495 + retries--) {
1496 + cpu_relax();
1497 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1498 + }
1499 +
1500 + if (!retries) {
1501 + unsigned long wait_max;
1502 +
1503 + if (!irq_flags) {
1504 + /* Schedule the work */
1505 + log_event("CWWQ", 0, 0);
1506 + schedule_work(&host->cmd_wait_wq);
1507 + return;
1508 + }
1509 +
1510 + /* Wait max 100 ms */
1511 + wait_max = jiffies + msecs_to_jiffies(100);
1512 + while (time_before(jiffies, wait_max)) {
1513 + spin_unlock_irqrestore(&host->lock, *irq_flags);
1514 + usleep_range(1, 10);
1515 + spin_lock_irqsave(&host->lock, *irq_flags);
1516 + sdcmd = bcm2835_sdhost_read(host, SDCMD);
1517 + if (!(sdcmd & SDCMD_NEW_FLAG))
1518 + break;
1519 + }
1520 + }
1521 +
1522 + /* Check for errors */
1523 + if (sdcmd & SDCMD_NEW_FLAG) {
1524 + if (host->debug) {
1525 + pr_err("%s: command %d never completed.\n",
1526 + mmc_hostname(host->mmc), host->cmd->opcode);
1527 + bcm2835_sdhost_dumpregs(host);
1528 + }
1529 + host->cmd->error = -EILSEQ;
1530 + tasklet_schedule(&host->finish_tasklet);
1531 + return;
1532 + } else if (sdcmd & SDCMD_FAIL_FLAG) {
1533 + u32 sdhsts = bcm2835_sdhost_read(host, SDHSTS);
1534 +
1535 + /* Clear the errors */
1536 + bcm2835_sdhost_write(host, SDHSTS_ERROR_MASK, SDHSTS);
1537 +
1538 + if (host->debug)
1539 + pr_info("%s: error detected - CMD %x, HSTS %03x, EDM %x\n",
1540 + mmc_hostname(host->mmc), sdcmd, sdhsts,
1541 + bcm2835_sdhost_read(host, SDEDM));
1542 +
1543 + if ((sdhsts & SDHSTS_CRC7_ERROR) &&
1544 + (host->cmd->opcode == 1)) {
1545 + if (host->debug)
1546 + pr_info("%s: ignoring CRC7 error for CMD1\n",
1547 + mmc_hostname(host->mmc));
1548 + } else {
1549 + u32 edm, fsm;
1550 +
1551 + if (sdhsts & SDHSTS_CMD_TIME_OUT) {
1552 + if (host->debug)
1553 + pr_warn("%s: command %d timeout\n",
1554 + mmc_hostname(host->mmc),
1555 + host->cmd->opcode);
1556 + host->cmd->error = -ETIMEDOUT;
1557 + } else {
1558 + pr_warn("%s: unexpected command %d error\n",
1559 + mmc_hostname(host->mmc),
1560 + host->cmd->opcode);
1561 + host->cmd->error = -EILSEQ;
1562 + }
1563 +
1564 + edm = readl(host->ioaddr + SDEDM);
1565 + fsm = edm & SDEDM_FSM_MASK;
1566 + if (fsm == SDEDM_FSM_READWAIT ||
1567 + fsm == SDEDM_FSM_WRITESTART1)
1568 + writel(edm | SDEDM_FORCE_DATA_MODE,
1569 + host->ioaddr + SDEDM);
1570 + tasklet_schedule(&host->finish_tasklet);
1571 + return;
1572 + }
1573 + }
1574 +
1575 + if (host->cmd->flags & MMC_RSP_PRESENT) {
1576 + if (host->cmd->flags & MMC_RSP_136) {
1577 + int i;
1578 + for (i = 0; i < 4; i++)
1579 + host->cmd->resp[3 - i] = bcm2835_sdhost_read(host, SDRSP0 + i*4);
1580 + pr_debug("%s: finish_command %08x %08x %08x %08x\n",
1581 + mmc_hostname(host->mmc),
1582 + host->cmd->resp[0], host->cmd->resp[1], host->cmd->resp[2], host->cmd->resp[3]);
1583 + log_event("RSP ", host->cmd->resp[0], host->cmd->resp[1]);
1584 + } else {
1585 + host->cmd->resp[0] = bcm2835_sdhost_read(host, SDRSP0);
1586 + pr_debug("%s: finish_command %08x\n",
1587 + mmc_hostname(host->mmc),
1588 + host->cmd->resp[0]);
1589 + log_event("RSP ", host->cmd->resp[0], 0);
1590 + }
1591 + }
1592 +
1593 + if (host->cmd == host->mrq->sbc) {
1594 + /* Finished CMD23, now send actual command. */
1595 + host->cmd = NULL;
1596 + if (bcm2835_sdhost_send_command(host, host->mrq->cmd)) {
1597 + if (host->data && host->dma_desc)
1598 + /* DMA transfer starts now, PIO starts after irq */
1599 + bcm2835_sdhost_start_dma(host);
1600 +
1601 + if (!host->use_busy)
1602 + bcm2835_sdhost_finish_command(host, NULL);
1603 + }
1604 + } else if (host->cmd == host->mrq->stop) {
1605 + /* Finished CMD12 */
1606 + tasklet_schedule(&host->finish_tasklet);
1607 + } else {
1608 + /* Processed actual command. */
1609 + host->cmd = NULL;
1610 + if (!host->data)
1611 + tasklet_schedule(&host->finish_tasklet);
1612 + else if (host->data_complete)
1613 + bcm2835_sdhost_transfer_complete(host);
1614 + }
1615 + log_event("FCM>", host->mrq, host->cmd);
1616 +}
1617 +
1618 +static void bcm2835_sdhost_timeout(struct timer_list *t)
1619 +{
1620 + struct bcm2835_host *host = from_timer(host, t, timer);
1621 + unsigned long flags;
1622 +
1623 + spin_lock_irqsave(&host->lock, flags);
1624 + log_event("TIM<", 0, 0);
1625 +
1626 + if (host->mrq) {
1627 + pr_err("%s: timeout waiting for hardware interrupt.\n",
1628 + mmc_hostname(host->mmc));
1629 + log_dump();
1630 + bcm2835_sdhost_dumpregs(host);
1631 +
1632 + if (host->data) {
1633 + host->data->error = -ETIMEDOUT;
1634 + bcm2835_sdhost_finish_data(host);
1635 + } else {
1636 + if (host->cmd)
1637 + host->cmd->error = -ETIMEDOUT;
1638 + else
1639 + host->mrq->cmd->error = -ETIMEDOUT;
1640 +
1641 + pr_debug("timeout_timer tasklet_schedule\n");
1642 + tasklet_schedule(&host->finish_tasklet);
1643 + }
1644 + }
1645 +
1646 + spin_unlock_irqrestore(&host->lock, flags);
1647 +}
1648 +
1649 +static void bcm2835_sdhost_busy_irq(struct bcm2835_host *host, u32 intmask)
1650 +{
1651 + log_event("IRQB", host->cmd, intmask);
1652 + if (!host->cmd) {
1653 + pr_err("%s: got command busy interrupt 0x%08x even "
1654 + "though no command operation was in progress.\n",
1655 + mmc_hostname(host->mmc), (unsigned)intmask);
1656 + bcm2835_sdhost_dumpregs(host);
1657 + return;
1658 + }
1659 +
1660 + if (!host->use_busy) {
1661 + pr_err("%s: got command busy interrupt 0x%08x even "
1662 + "though not expecting one.\n",
1663 + mmc_hostname(host->mmc), (unsigned)intmask);
1664 + bcm2835_sdhost_dumpregs(host);
1665 + return;
1666 + }
1667 + host->use_busy = 0;
1668 +
1669 + if (intmask & SDHSTS_ERROR_MASK)
1670 + {
1671 + pr_err("sdhost_busy_irq: intmask %x, data %p\n", intmask, host->mrq->data);
1672 + if (intmask & SDHSTS_CRC7_ERROR)
1673 + host->cmd->error = -EILSEQ;
1674 + else if (intmask & (SDHSTS_CRC16_ERROR |
1675 + SDHSTS_FIFO_ERROR)) {
1676 + if (host->mrq->data)
1677 + host->mrq->data->error = -EILSEQ;
1678 + else
1679 + host->cmd->error = -EILSEQ;
1680 + } else if (intmask & SDHSTS_REW_TIME_OUT) {
1681 + if (host->mrq->data)
1682 + host->mrq->data->error = -ETIMEDOUT;
1683 + else
1684 + host->cmd->error = -ETIMEDOUT;
1685 + } else if (intmask & SDHSTS_CMD_TIME_OUT)
1686 + host->cmd->error = -ETIMEDOUT;
1687 +
1688 + if (host->debug) {
1689 + log_dump();
1690 + bcm2835_sdhost_dumpregs(host);
1691 + }
1692 + }
1693 + else
1694 + bcm2835_sdhost_finish_command(host, NULL);
1695 +}
1696 +
1697 +static void bcm2835_sdhost_data_irq(struct bcm2835_host *host, u32 intmask)
1698 +{
1699 + /* There are no dedicated data/space available interrupt
1700 + status bits, so it is necessary to use the single shared
1701 + data/space available FIFO status bits. It is therefore not
1702 + an error to get here when there is no data transfer in
1703 + progress. */
1704 + log_event("IRQD", host->data, intmask);
1705 + if (!host->data)
1706 + return;
1707 +
1708 + if (intmask & (SDHSTS_CRC16_ERROR |
1709 + SDHSTS_FIFO_ERROR |
1710 + SDHSTS_REW_TIME_OUT)) {
1711 + if (intmask & (SDHSTS_CRC16_ERROR |
1712 + SDHSTS_FIFO_ERROR))
1713 + host->data->error = -EILSEQ;
1714 + else
1715 + host->data->error = -ETIMEDOUT;
1716 +
1717 + if (host->debug) {
1718 + log_dump();
1719 + bcm2835_sdhost_dumpregs(host);
1720 + }
1721 + }
1722 +
1723 + if (host->data->error) {
1724 + bcm2835_sdhost_finish_data(host);
1725 + } else if (host->data->flags & MMC_DATA_WRITE) {
1726 + /* Use the block interrupt for writes after the first block */
1727 + host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
1728 + host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
1729 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
1730 + bcm2835_sdhost_transfer_pio(host);
1731 + } else {
1732 + bcm2835_sdhost_transfer_pio(host);
1733 + host->blocks--;
1734 + if ((host->blocks == 0) || host->data->error)
1735 + bcm2835_sdhost_finish_data(host);
1736 + }
1737 +}
1738 +
1739 +static void bcm2835_sdhost_block_irq(struct bcm2835_host *host, u32 intmask)
1740 +{
1741 + log_event("IRQK", host->data, intmask);
1742 + if (!host->data) {
1743 + pr_err("%s: got block interrupt 0x%08x even "
1744 + "though no data operation was in progress.\n",
1745 + mmc_hostname(host->mmc), (unsigned)intmask);
1746 + bcm2835_sdhost_dumpregs(host);
1747 + return;
1748 + }
1749 +
1750 + if (intmask & (SDHSTS_CRC16_ERROR |
1751 + SDHSTS_FIFO_ERROR |
1752 + SDHSTS_REW_TIME_OUT)) {
1753 + if (intmask & (SDHSTS_CRC16_ERROR |
1754 + SDHSTS_FIFO_ERROR))
1755 + host->data->error = -EILSEQ;
1756 + else
1757 + host->data->error = -ETIMEDOUT;
1758 +
1759 + if (host->debug) {
1760 + log_dump();
1761 + bcm2835_sdhost_dumpregs(host);
1762 + }
1763 + }
1764 +
1765 + if (!host->dma_desc) {
1766 + BUG_ON(!host->blocks);
1767 + if (host->data->error || (--host->blocks == 0)) {
1768 + bcm2835_sdhost_finish_data(host);
1769 + } else {
1770 + bcm2835_sdhost_transfer_pio(host);
1771 + }
1772 + } else if (host->data->flags & MMC_DATA_WRITE) {
1773 + bcm2835_sdhost_finish_data(host);
1774 + }
1775 +}
1776 +
1777 +static irqreturn_t bcm2835_sdhost_irq(int irq, void *dev_id)
1778 +{
1779 + irqreturn_t result = IRQ_NONE;
1780 + struct bcm2835_host *host = dev_id;
1781 + u32 intmask;
1782 +
1783 + spin_lock(&host->lock);
1784 +
1785 + intmask = bcm2835_sdhost_read(host, SDHSTS);
1786 + log_event("IRQ<", intmask, 0);
1787 +
1788 + bcm2835_sdhost_write(host,
1789 + SDHSTS_BUSY_IRPT |
1790 + SDHSTS_BLOCK_IRPT |
1791 + SDHSTS_SDIO_IRPT |
1792 + SDHSTS_DATA_FLAG,
1793 + SDHSTS);
1794 +
1795 + if (intmask & SDHSTS_BLOCK_IRPT) {
1796 + bcm2835_sdhost_block_irq(host, intmask);
1797 + result = IRQ_HANDLED;
1798 + }
1799 +
1800 + if (intmask & SDHSTS_BUSY_IRPT) {
1801 + bcm2835_sdhost_busy_irq(host, intmask);
1802 + result = IRQ_HANDLED;
1803 + }
1804 +
1805 + /* There is no true data interrupt status bit, so it is
1806 + necessary to qualify the data flag with the interrupt
1807 + enable bit */
1808 + if ((intmask & SDHSTS_DATA_FLAG) &&
1809 + (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
1810 + bcm2835_sdhost_data_irq(host, intmask);
1811 + result = IRQ_HANDLED;
1812 + }
1813 +
1814 + log_event("IRQ>", bcm2835_sdhost_read(host, SDHSTS), 0);
1815 + spin_unlock(&host->lock);
1816 +
1817 + return result;
1818 +}
1819 +
1820 +void bcm2835_sdhost_set_clock(struct bcm2835_host *host, unsigned int clock)
1821 +{
1822 + int div = 0; /* Initialized for compiler warning */
1823 + unsigned int input_clock = clock;
1824 + unsigned long flags;
1825 +
1826 + if (host->debug)
1827 + pr_info("%s: set_clock(%d)\n", mmc_hostname(host->mmc), clock);
1828 +
1829 + if (host->overclock_50 && (clock == 50*MHZ))
1830 + clock = host->overclock_50 * MHZ + (MHZ - 1);
1831 +
1832 + /* The SDCDIV register has 11 bits, and holds (div - 2).
1833 + But in data mode the max is 50MHz wihout a minimum, and only the
1834 + bottom 3 bits are used. Since the switch over is automatic (unless
1835 + we have marked the card as slow...), chosen values have to make
1836 + sense in both modes.
1837 + Ident mode must be 100-400KHz, so can range check the requested
1838 + clock. CMD15 must be used to return to data mode, so this can be
1839 + monitored.
1840 +
1841 + clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz
1842 + 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz
1843 +
1844 + 623->400KHz/27.8MHz
1845 + reset value (507)->491159/50MHz
1846 +
1847 + BUT, the 3-bit clock divisor in data mode is too small if the
1848 + core clock is higher than 250MHz, so instead use the SLOW_CARD
1849 + configuration bit to force the use of the ident clock divisor
1850 + at all times.
1851 + */
1852 +
1853 + host->mmc->actual_clock = 0;
1854 +
1855 + if (host->firmware_sets_cdiv) {
1856 + u32 msg[3] = { clock, 0, 0 };
1857 +
1858 + rpi_firmware_property(host->fw,
1859 + RPI_FIRMWARE_SET_SDHOST_CLOCK,
1860 + &msg, sizeof(msg));
1861 +
1862 + clock = max(msg[1], msg[2]);
1863 + spin_lock_irqsave(&host->lock, flags);
1864 + } else {
1865 + spin_lock_irqsave(&host->lock, flags);
1866 + if (clock < 100000) {
1867 + /* Can't stop the clock, but make it as slow as
1868 + * possible to show willing
1869 + */
1870 + host->cdiv = SDCDIV_MAX_CDIV;
1871 + bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
1872 + spin_unlock_irqrestore(&host->lock, flags);
1873 + return;
1874 + }
1875 +
1876 + div = host->max_clk / clock;
1877 + if (div < 2)
1878 + div = 2;
1879 + if ((host->max_clk / div) > clock)
1880 + div++;
1881 + div -= 2;
1882 +
1883 + if (div > SDCDIV_MAX_CDIV)
1884 + div = SDCDIV_MAX_CDIV;
1885 +
1886 + clock = host->max_clk / (div + 2);
1887 +
1888 + host->cdiv = div;
1889 + bcm2835_sdhost_write(host, host->cdiv, SDCDIV);
1890 +
1891 + if (host->debug)
1892 + pr_info("%s: clock=%d -> max_clk=%d, cdiv=%x "
1893 + "(actual clock %d)\n",
1894 + mmc_hostname(host->mmc), input_clock,
1895 + host->max_clk, host->cdiv,
1896 + clock);
1897 + }
1898 +
1899 + /* Calibrate some delays */
1900 +
1901 + host->ns_per_fifo_word = (1000000000/clock) *
1902 + ((host->mmc->caps & MMC_CAP_4_BIT_DATA) ? 8 : 32);
1903 +
1904 + if (input_clock == 50 * MHZ) {
1905 + if (clock > input_clock) {
1906 + /* Save the closest value, to make it easier
1907 + to reduce in the event of error */
1908 + host->overclock_50 = (clock/MHZ);
1909 +
1910 + if (clock != host->overclock) {
1911 + pr_info("%s: overclocking to %dHz\n",
1912 + mmc_hostname(host->mmc), clock);
1913 + host->overclock = clock;
1914 + }
1915 + } else if (host->overclock) {
1916 + host->overclock = 0;
1917 + if (clock == 50 * MHZ)
1918 + pr_warn("%s: cancelling overclock\n",
1919 + mmc_hostname(host->mmc));
1920 + }
1921 + } else if (input_clock == 0) {
1922 + /* Reset the preferred overclock when the clock is stopped.
1923 + * This always happens during initialisation. */
1924 + host->overclock_50 = host->user_overclock_50;
1925 + host->overclock = 0;
1926 + }
1927 +
1928 + /* Set the timeout to 500ms */
1929 + bcm2835_sdhost_write(host, clock/2, SDTOUT);
1930 +
1931 + host->mmc->actual_clock = clock;
1932 + host->clock = input_clock;
1933 + host->reset_clock = 0;
1934 +
1935 + spin_unlock_irqrestore(&host->lock, flags);
1936 +}
1937 +
1938 +static void bcm2835_sdhost_request(struct mmc_host *mmc, struct mmc_request *mrq)
1939 +{
1940 + struct bcm2835_host *host;
1941 + unsigned long flags;
1942 + u32 edm, fsm;
1943 +
1944 + host = mmc_priv(mmc);
1945 +
1946 + if (host->debug) {
1947 + struct mmc_command *cmd = mrq->cmd;
1948 + BUG_ON(!cmd);
1949 + if (cmd->data)
1950 + pr_info("%s: cmd %d 0x%x (flags 0x%x) - %s %d*%d\n",
1951 + mmc_hostname(mmc),
1952 + cmd->opcode, cmd->arg, cmd->flags,
1953 + (cmd->data->flags & MMC_DATA_READ) ?
1954 + "read" : "write", cmd->data->blocks,
1955 + cmd->data->blksz);
1956 + else
1957 + pr_info("%s: cmd %d 0x%x (flags 0x%x)\n",
1958 + mmc_hostname(mmc),
1959 + cmd->opcode, cmd->arg, cmd->flags);
1960 + }
1961 +
1962 + /* Reset the error statuses in case this is a retry */
1963 + if (mrq->sbc)
1964 + mrq->sbc->error = 0;
1965 + if (mrq->cmd)
1966 + mrq->cmd->error = 0;
1967 + if (mrq->data)
1968 + mrq->data->error = 0;
1969 + if (mrq->stop)
1970 + mrq->stop->error = 0;
1971 +
1972 + if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
1973 + pr_err("%s: unsupported block size (%d bytes)\n",
1974 + mmc_hostname(mmc), mrq->data->blksz);
1975 + mrq->cmd->error = -EINVAL;
1976 + mmc_request_done(mmc, mrq);
1977 + return;
1978 + }
1979 +
1980 + if (host->use_dma && mrq->data &&
1981 + (mrq->data->blocks > host->pio_limit))
1982 + bcm2835_sdhost_prepare_dma(host, mrq->data);
1983 +
1984 + if (host->reset_clock)
1985 + bcm2835_sdhost_set_clock(host, host->clock);
1986 +
1987 + spin_lock_irqsave(&host->lock, flags);
1988 +
1989 + WARN_ON(host->mrq != NULL);
1990 + host->mrq = mrq;
1991 +
1992 + edm = bcm2835_sdhost_read(host, SDEDM);
1993 + fsm = edm & SDEDM_FSM_MASK;
1994 +
1995 + log_event("REQ<", mrq, edm);
1996 + if ((fsm != SDEDM_FSM_IDENTMODE) &&
1997 + (fsm != SDEDM_FSM_DATAMODE)) {
1998 + log_event("REQ!", mrq, edm);
1999 + if (host->debug) {
2000 + pr_warn("%s: previous command (%d) not complete (EDM %x)\n",
2001 + mmc_hostname(host->mmc),
2002 + bcm2835_sdhost_read(host, SDCMD) & SDCMD_CMD_MASK,
2003 + edm);
2004 + log_dump();
2005 + bcm2835_sdhost_dumpregs(host);
2006 + }
2007 + mrq->cmd->error = -EILSEQ;
2008 + tasklet_schedule(&host->finish_tasklet);
2009 + spin_unlock_irqrestore(&host->lock, flags);
2010 + return;
2011 + }
2012 +
2013 + host->use_sbc = !!mrq->sbc &&
2014 + (host->mrq->data->flags & USE_CMD23_FLAGS);
2015 + if (host->use_sbc) {
2016 + if (bcm2835_sdhost_send_command(host, mrq->sbc)) {
2017 + if (!host->use_busy)
2018 + bcm2835_sdhost_finish_command(host, &flags);
2019 + }
2020 + } else if (bcm2835_sdhost_send_command(host, mrq->cmd)) {
2021 + if (host->data && host->dma_desc)
2022 + /* DMA transfer starts now, PIO starts after irq */
2023 + bcm2835_sdhost_start_dma(host);
2024 +
2025 + if (!host->use_busy)
2026 + bcm2835_sdhost_finish_command(host, &flags);
2027 + }
2028 +
2029 + log_event("CMD ", mrq->cmd->opcode,
2030 + mrq->data ? (u32)mrq->data->blksz : 0);
2031 +
2032 + log_event("REQ>", mrq, 0);
2033 + spin_unlock_irqrestore(&host->lock, flags);
2034 +}
2035 +
2036 +static void bcm2835_sdhost_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
2037 +{
2038 +
2039 + struct bcm2835_host *host = mmc_priv(mmc);
2040 + unsigned long flags;
2041 +
2042 + if (host->debug)
2043 + pr_info("%s: ios clock %d, pwr %d, bus_width %d, "
2044 + "timing %d, vdd %d, drv_type %d\n",
2045 + mmc_hostname(mmc),
2046 + ios->clock, ios->power_mode, ios->bus_width,
2047 + ios->timing, ios->signal_voltage, ios->drv_type);
2048 +
2049 + spin_lock_irqsave(&host->lock, flags);
2050 +
2051 + log_event("IOS<", ios->clock, 0);
2052 +
2053 + /* set bus width */
2054 + host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
2055 + if (ios->bus_width == MMC_BUS_WIDTH_4)
2056 + host->hcfg |= SDHCFG_WIDE_EXT_BUS;
2057 +
2058 + host->hcfg |= SDHCFG_WIDE_INT_BUS;
2059 +
2060 + /* Disable clever clock switching, to cope with fast core clocks */
2061 + host->hcfg |= SDHCFG_SLOW_CARD;
2062 +
2063 + bcm2835_sdhost_write(host, host->hcfg, SDHCFG);
2064 +
2065 + spin_unlock_irqrestore(&host->lock, flags);
2066 +
2067 + if (!ios->clock || ios->clock != host->clock)
2068 + bcm2835_sdhost_set_clock(host, ios->clock);
2069 +}
2070 +
2071 +static struct mmc_host_ops bcm2835_sdhost_ops = {
2072 + .request = bcm2835_sdhost_request,
2073 + .set_ios = bcm2835_sdhost_set_ios,
2074 +// todo:fix .hw_reset = bcm2835_sdhost_reset,
2075 +};
2076 +
2077 +static void bcm2835_sdhost_cmd_wait_work(struct work_struct *work)
2078 +{
2079 + struct bcm2835_host *host;
2080 + unsigned long flags;
2081 +
2082 + host = container_of(work, struct bcm2835_host, cmd_wait_wq);
2083 +
2084 + spin_lock_irqsave(&host->lock, flags);
2085 +
2086 + log_event("CWK<", host->cmd, host->mrq);
2087 +
2088 + /*
2089 + * If this tasklet gets rescheduled while running, it will
2090 + * be run again afterwards but without any active request.
2091 + */
2092 + if (!host->mrq) {
2093 + spin_unlock_irqrestore(&host->lock, flags);
2094 + return;
2095 + }
2096 +
2097 + bcm2835_sdhost_finish_command(host, &flags);
2098 +
2099 + log_event("CWK>", host->cmd, 0);
2100 +
2101 + spin_unlock_irqrestore(&host->lock, flags);
2102 +}
2103 +
2104 +static void bcm2835_sdhost_tasklet_finish(unsigned long param)
2105 +{
2106 + struct bcm2835_host *host;
2107 + unsigned long flags;
2108 + struct mmc_request *mrq;
2109 + struct dma_chan *terminate_chan = NULL;
2110 +
2111 + host = (struct bcm2835_host *)param;
2112 +
2113 + spin_lock_irqsave(&host->lock, flags);
2114 +
2115 + log_event("TSK<", host->mrq, 0);
2116 + /*
2117 + * If this tasklet gets rescheduled while running, it will
2118 + * be run again afterwards but without any active request.
2119 + */
2120 + if (!host->mrq) {
2121 + spin_unlock_irqrestore(&host->lock, flags);
2122 + return;
2123 + }
2124 +
2125 + del_timer(&host->timer);
2126 +
2127 + mrq = host->mrq;
2128 +
2129 + /* Drop the overclock after any data corruption, or after any
2130 + * error while overclocked. Ignore errors for status commands,
2131 + * as they are likely when a card is ejected. */
2132 + if (host->overclock) {
2133 + if ((mrq->cmd && mrq->cmd->error &&
2134 + (mrq->cmd->opcode != MMC_SEND_STATUS)) ||
2135 + (mrq->data && mrq->data->error) ||
2136 + (mrq->stop && mrq->stop->error) ||
2137 + (mrq->sbc && mrq->sbc->error)) {
2138 + host->overclock_50--;
2139 + pr_warn("%s: reducing overclock due to errors\n",
2140 + mmc_hostname(host->mmc));
2141 + host->reset_clock = 1;
2142 + mrq->cmd->error = -ETIMEDOUT;
2143 + mrq->cmd->retries = 1;
2144 + }
2145 + }
2146 +
2147 + host->mrq = NULL;
2148 + host->cmd = NULL;
2149 + host->data = NULL;
2150 +
2151 + host->dma_desc = NULL;
2152 + terminate_chan = host->dma_chan;
2153 + host->dma_chan = NULL;
2154 +
2155 + spin_unlock_irqrestore(&host->lock, flags);
2156 +
2157 + if (terminate_chan)
2158 + {
2159 + int err = dmaengine_terminate_all(terminate_chan);
2160 + if (err)
2161 + pr_err("%s: failed to terminate DMA (%d)\n",
2162 + mmc_hostname(host->mmc), err);
2163 + }
2164 +
2165 + /* The SDHOST block doesn't report any errors for a disconnected
2166 + interface. All cards and SDIO devices should report some supported
2167 + voltage range, so a zero response to SEND_OP_COND, IO_SEND_OP_COND
2168 + or APP_SEND_OP_COND can be treated as an error. */
2169 + if (((mrq->cmd->opcode == MMC_SEND_OP_COND) ||
2170 + (mrq->cmd->opcode == SD_IO_SEND_OP_COND) ||
2171 + (mrq->cmd->opcode == SD_APP_OP_COND)) &&
2172 + (mrq->cmd->error == 0) &&
2173 + (mrq->cmd->resp[0] == 0)) {
2174 + mrq->cmd->error = -ETIMEDOUT;
2175 + if (host->debug)
2176 + pr_info("%s: faking timeout due to zero OCR\n",
2177 + mmc_hostname(host->mmc));
2178 + }
2179 +
2180 + mmc_request_done(host->mmc, mrq);
2181 + log_event("TSK>", mrq, 0);
2182 +}
2183 +
2184 +int bcm2835_sdhost_add_host(struct bcm2835_host *host)
2185 +{
2186 + struct mmc_host *mmc;
2187 + struct dma_slave_config cfg;
2188 + char pio_limit_string[20];
2189 + int ret;
2190 +
2191 + mmc = host->mmc;
2192 +
2193 + if (!mmc->f_max || mmc->f_max > host->max_clk)
2194 + mmc->f_max = host->max_clk;
2195 + mmc->f_min = host->max_clk / SDCDIV_MAX_CDIV;
2196 +
2197 + mmc->max_busy_timeout = (~(unsigned int)0)/(mmc->f_max/1000);
2198 +
2199 + pr_debug("f_max %d, f_min %d, max_busy_timeout %d\n",
2200 + mmc->f_max, mmc->f_min, mmc->max_busy_timeout);
2201 +
2202 + /* host controller capabilities */
2203 + mmc->caps |=
2204 + MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
2205 + MMC_CAP_NEEDS_POLL | MMC_CAP_HW_RESET |
2206 + ((ALLOW_CMD23_READ|ALLOW_CMD23_WRITE) * MMC_CAP_CMD23);
2207 +
2208 + spin_lock_init(&host->lock);
2209 +
2210 + if (host->allow_dma) {
2211 + if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
2212 + pr_err("%s: unable to initialise DMA channel. "
2213 + "Falling back to PIO\n",
2214 + mmc_hostname(mmc));
2215 + host->use_dma = false;
2216 + } else {
2217 + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2218 + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2219 +
2220 + /* Validate the slave configurations */
2221 +
2222 + cfg.direction = DMA_MEM_TO_DEV;
2223 + cfg.src_addr = 0;
2224 + cfg.dst_addr = host->bus_addr + SDDATA;
2225 +
2226 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
2227 +
2228 + if (ret == 0) {
2229 + host->dma_cfg_tx = cfg;
2230 +
2231 + cfg.direction = DMA_DEV_TO_MEM;
2232 + cfg.src_addr = host->bus_addr + SDDATA;
2233 + cfg.dst_addr = 0;
2234 +
2235 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
2236 + }
2237 +
2238 + if (ret == 0) {
2239 + host->dma_cfg_rx = cfg;
2240 +
2241 + host->use_dma = true;
2242 + } else {
2243 + pr_err("%s: unable to configure DMA channel. "
2244 + "Falling back to PIO\n",
2245 + mmc_hostname(mmc));
2246 + dma_release_channel(host->dma_chan_rxtx);
2247 + host->dma_chan_rxtx = NULL;
2248 + host->use_dma = false;
2249 + }
2250 + }
2251 + } else {
2252 + host->use_dma = false;
2253 + }
2254 +
2255 + mmc->max_segs = 128;
2256 + mmc->max_req_size = 524288;
2257 + mmc->max_seg_size = mmc->max_req_size;
2258 + mmc->max_blk_size = 512;
2259 + mmc->max_blk_count = 65535;
2260 +
2261 + /* report supported voltage ranges */
2262 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
2263 +
2264 + tasklet_init(&host->finish_tasklet,
2265 + bcm2835_sdhost_tasklet_finish, (unsigned long)host);
2266 +
2267 + INIT_WORK(&host->cmd_wait_wq, bcm2835_sdhost_cmd_wait_work);
2268 +
2269 + timer_setup(&host->timer, bcm2835_sdhost_timeout, 0);
2270 +
2271 + bcm2835_sdhost_init(host, 0);
2272 +
2273 + ret = request_irq(host->irq, bcm2835_sdhost_irq, 0 /*IRQF_SHARED*/,
2274 + mmc_hostname(mmc), host);
2275 + if (ret) {
2276 + pr_err("%s: failed to request IRQ %d: %d\n",
2277 + mmc_hostname(mmc), host->irq, ret);
2278 + goto untasklet;
2279 + }
2280 +
2281 + mmc_add_host(mmc);
2282 +
2283 + pio_limit_string[0] = '\0';
2284 + if (host->use_dma && (host->pio_limit > 0))
2285 + sprintf(pio_limit_string, " (>%d)", host->pio_limit);
2286 + pr_info("%s: %s loaded - DMA %s%s\n",
2287 + mmc_hostname(mmc), DRIVER_NAME,
2288 + host->use_dma ? "enabled" : "disabled",
2289 + pio_limit_string);
2290 +
2291 + return 0;
2292 +
2293 +untasklet:
2294 + tasklet_kill(&host->finish_tasklet);
2295 +
2296 + return ret;
2297 +}
2298 +
2299 +static int bcm2835_sdhost_probe(struct platform_device *pdev)
2300 +{
2301 + struct device *dev = &pdev->dev;
2302 + struct device_node *node = dev->of_node;
2303 + struct clk *clk;
2304 + struct resource *iomem;
2305 + struct bcm2835_host *host;
2306 + struct mmc_host *mmc;
2307 + const __be32 *addr;
2308 + u32 msg[3];
2309 + int na;
2310 + int ret;
2311 +
2312 + pr_debug("bcm2835_sdhost_probe\n");
2313 + mmc = mmc_alloc_host(sizeof(*host), dev);
2314 + if (!mmc)
2315 + return -ENOMEM;
2316 +
2317 + mmc->ops = &bcm2835_sdhost_ops;
2318 + host = mmc_priv(mmc);
2319 + host->mmc = mmc;
2320 + host->pio_timeout = msecs_to_jiffies(500);
2321 + host->pio_limit = 1;
2322 + host->max_delay = 1; /* Warn if over 1ms */
2323 + host->allow_dma = 1;
2324 + spin_lock_init(&host->lock);
2325 +
2326 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2327 + host->ioaddr = devm_ioremap_resource(dev, iomem);
2328 + if (IS_ERR(host->ioaddr)) {
2329 + ret = PTR_ERR(host->ioaddr);
2330 + goto err;
2331 + }
2332 +
2333 + na = of_n_addr_cells(node);
2334 + addr = of_get_address(node, 0, NULL, NULL);
2335 + if (!addr) {
2336 + dev_err(dev, "could not get DMA-register address\n");
2337 + return -ENODEV;
2338 + }
2339 + host->bus_addr = (phys_addr_t)of_read_number(addr, na);
2340 + pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
2341 + (unsigned long)host->ioaddr,
2342 + (unsigned long)iomem->start,
2343 + (unsigned long)host->bus_addr);
2344 +
2345 + if (node) {
2346 + /* Read any custom properties */
2347 + of_property_read_u32(node,
2348 + "brcm,delay-after-stop",
2349 + &host->delay_after_stop);
2350 + of_property_read_u32(node,
2351 + "brcm,overclock-50",
2352 + &host->user_overclock_50);
2353 + of_property_read_u32(node,
2354 + "brcm,pio-limit",
2355 + &host->pio_limit);
2356 + host->allow_dma =
2357 + !of_property_read_bool(node, "brcm,force-pio");
2358 + host->debug = of_property_read_bool(node, "brcm,debug");
2359 + }
2360 +
2361 + host->dma_chan = NULL;
2362 + host->dma_desc = NULL;
2363 +
2364 + /* Formally recognise the other way of disabling DMA */
2365 + if (host->pio_limit == 0x7fffffff)
2366 + host->allow_dma = false;
2367 +
2368 + if (host->allow_dma) {
2369 + if (node) {
2370 + host->dma_chan_rxtx =
2371 + dma_request_slave_channel(dev, "rx-tx");
2372 + if (!host->dma_chan_rxtx)
2373 + host->dma_chan_rxtx =
2374 + dma_request_slave_channel(dev, "tx");
2375 + if (!host->dma_chan_rxtx)
2376 + host->dma_chan_rxtx =
2377 + dma_request_slave_channel(dev, "rx");
2378 + } else {
2379 + dma_cap_mask_t mask;
2380 +
2381 + dma_cap_zero(mask);
2382 + /* we don't care about the channel, any would work */
2383 + dma_cap_set(DMA_SLAVE, mask);
2384 + host->dma_chan_rxtx =
2385 + dma_request_channel(mask, NULL, NULL);
2386 + }
2387 + }
2388 +
2389 + clk = devm_clk_get(dev, NULL);
2390 + if (IS_ERR(clk)) {
2391 + ret = PTR_ERR(clk);
2392 + if (ret == -EPROBE_DEFER)
2393 + dev_info(dev, "could not get clk, deferring probe\n");
2394 + else
2395 + dev_err(dev, "could not get clk\n");
2396 + goto err;
2397 + }
2398 +
2399 + host->fw = rpi_firmware_get(
2400 + of_parse_phandle(dev->of_node, "firmware", 0));
2401 + if (!host->fw) {
2402 + ret = -EPROBE_DEFER;
2403 + goto err;
2404 + }
2405 +
2406 + host->max_clk = clk_get_rate(clk);
2407 +
2408 + host->irq = platform_get_irq(pdev, 0);
2409 + if (host->irq <= 0) {
2410 + dev_err(dev, "get IRQ failed\n");
2411 + ret = -EINVAL;
2412 + goto err;
2413 + }
2414 +
2415 + pr_debug(" - max_clk %lx, irq %d\n",
2416 + (unsigned long)host->max_clk,
2417 + (int)host->irq);
2418 +
2419 + log_init(dev, iomem->start - host->bus_addr);
2420 +
2421 + if (node)
2422 + mmc_of_parse(mmc);
2423 + else
2424 + mmc->caps |= MMC_CAP_4_BIT_DATA;
2425 +
2426 + msg[0] = 0;
2427 + msg[1] = ~0;
2428 + msg[2] = ~0;
2429 +
2430 + rpi_firmware_property(host->fw,
2431 + RPI_FIRMWARE_SET_SDHOST_CLOCK,
2432 + &msg, sizeof(msg));
2433 +
2434 + host->firmware_sets_cdiv = (msg[1] != ~0);
2435 +
2436 + ret = bcm2835_sdhost_add_host(host);
2437 + if (ret)
2438 + goto err;
2439 +
2440 + platform_set_drvdata(pdev, host);
2441 +
2442 + pr_debug("bcm2835_sdhost_probe -> OK\n");
2443 +
2444 + return 0;
2445 +
2446 +err:
2447 + pr_debug("bcm2835_sdhost_probe -> err %d\n", ret);
2448 + if (host->fw)
2449 + rpi_firmware_put(host->fw);
2450 + if (host->dma_chan_rxtx)
2451 + dma_release_channel(host->dma_chan_rxtx);
2452 + mmc_free_host(mmc);
2453 +
2454 + return ret;
2455 +}
2456 +
2457 +static int bcm2835_sdhost_remove(struct platform_device *pdev)
2458 +{
2459 + struct bcm2835_host *host = platform_get_drvdata(pdev);
2460 +
2461 + pr_debug("bcm2835_sdhost_remove\n");
2462 +
2463 + mmc_remove_host(host->mmc);
2464 +
2465 + bcm2835_sdhost_set_power(host, false);
2466 +
2467 + free_irq(host->irq, host);
2468 +
2469 + del_timer_sync(&host->timer);
2470 +
2471 + tasklet_kill(&host->finish_tasklet);
2472 + rpi_firmware_put(host->fw);
2473 + if (host->dma_chan_rxtx)
2474 + dma_release_channel(host->dma_chan_rxtx);
2475 + mmc_free_host(host->mmc);
2476 + platform_set_drvdata(pdev, NULL);
2477 +
2478 + pr_debug("bcm2835_sdhost_remove - OK\n");
2479 + return 0;
2480 +}
2481 +
2482 +static const struct of_device_id bcm2835_sdhost_match[] = {
2483 + { .compatible = "brcm,bcm2835-sdhost" },
2484 + { }
2485 +};
2486 +MODULE_DEVICE_TABLE(of, bcm2835_sdhost_match);
2487 +
2488 +static struct platform_driver bcm2835_sdhost_driver = {
2489 + .probe = bcm2835_sdhost_probe,
2490 + .remove = bcm2835_sdhost_remove,
2491 + .driver = {
2492 + .name = DRIVER_NAME,
2493 + .owner = THIS_MODULE,
2494 + .of_match_table = bcm2835_sdhost_match,
2495 + },
2496 +};
2497 +module_platform_driver(bcm2835_sdhost_driver);
2498 +
2499 +MODULE_ALIAS("platform:sdhost-bcm2835");
2500 +MODULE_DESCRIPTION("BCM2835 SDHost driver");
2501 +MODULE_LICENSE("GPL v2");
2502 +MODULE_AUTHOR("Phil Elwell");