4f5e392d781d734d8deeee2d8242f00dada0d9de
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0043-MMC-added-alternative-MMC-driver.patch
1 From e15eff977effee05b5b47b20762e11ca345315c6 Mon Sep 17 00:00:00 2001
2 From: gellert <gellert@raspberrypi.org>
3 Date: Fri, 15 Aug 2014 16:35:06 +0100
4 Subject: [PATCH] MMC: added alternative MMC driver
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 mmc: Disable CMD23 transfers on all cards
10
11 Pending wire-level investigation of these types of transfers
12 and associated errors on bcm2835-mmc, disable for now. Fallback of
13 CMD18/CMD25 transfers will be used automatically by the MMC layer.
14
15 Reported/Tested-by: Gellert Weisz <gellert@raspberrypi.org>
16
17 mmc: bcm2835-mmc: enable DT support for all architectures
18
19 Both ARCH_BCM2835 and ARCH_BCM270x are built with OF now.
20 Enable Device Tree support for all architectures.
21
22 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
23
24 mmc: bcm2835-mmc: fix probe error handling
25
26 Probe error handling is broken in several places.
27 Simplify error handling by using device managed functions.
28 Replace pr_{err,info} with dev_{err,info}.
29
30 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
31
32 bcm2835-mmc: Add locks when accessing sdhost registers
33
34 bcm2835-mmc: Add range of debug options for slowing things down
35
36 bcm2835-mmc: Add option to disable some delays
37
38 bcm2835-mmc: Add option to disable MMC_QUIRK_BLK_NO_CMD23
39
40 bcm2835-mmc: Default to disabling MMC_QUIRK_BLK_NO_CMD23
41
42 bcm2835-mmc: Adding overclocking option
43
44 Allow a different clock speed to be substitued for a requested 50MHz.
45 This option is exposed using the "overclock_50" DT parameter.
46 Note that the mmc interface is restricted to EVEN integer divisions of
47 250MHz, and the highest sensible option is 63 (250/4 = 62.5), the
48 next being 125 (250/2) which is much too high.
49
50 Use at your own risk.
51
52 bcm2835-mmc: Round up the overclock, so 62 works for 62.5Mhz
53
54 Also only warn once for each overclock setting.
55
56 mmc: bcm2835-mmc: Make available on ARCH_BCM2835
57
58 Make the bcm2835-mmc driver available for use on ARCH_BCM2835.
59
60 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
61
62 BCM270x_DT: add bcm2835-mmc entry
63
64 Add Device Tree entry for bcm2835-mmc.
65 In non-DT mode, don't add the device in the board file.
66
67 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
68
69 bcm2835-mmc: Don't overwrite MMC capabilities from DT
70
71 bcm2835-mmc: Don't override bus width capabilities from devicetree
72
73 Take out the force setting of the MMC_CAP_4_BIT_DATA host capability
74 so that the result read from devicetree via mmc_of_parse() is
75 preserved.
76
77 bcm2835-mmc: Only claim one DMA channel
78
79 With both MMC controllers enabled there are few DMA channels left. The
80 bcm2835-mmc driver only uses DMA in one direction at a time, so it
81 doesn't need to claim two channels.
82
83 See: https://github.com/raspberrypi/linux/issues/1327
84
85 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
86
87 bcm2835-mmc: New timer API
88
89 mmc: bcm2835-mmc: Support underclocking
90
91 Support underclocking of the SD bus using the max-frequency DT property
92 (which currently has no DT parameter). The sd_overclock parameter
93 already provides another way to achieve the same thing which should be
94 equivalent in end result, but it is a bug not to support max-frequency
95 as well.
96
97 See: https://github.com/raspberrypi/linux/issues/2350
98
99 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
100
101 mmc/bcm2835: Recover from MMC_SEND_EXT_CSD
102
103 If the user issues an "mmc extcsd read", the SD controller receives
104 what it thinks is a SEND_IF_COND command with an unexpected data block.
105 The resulting operations leave the FSM stuck in READWAIT, a state which
106 persists until the MMC framework resets the controller, by which point
107 the root filesystem is likely to have been unmounted.
108
109 A less heavyweight solution is to detect the condition and nudge the
110 FSM by asserting the (self-clearing) FORCE_DATA_MODE bit.
111
112 N.B. This workaround was essentially discovered by accident and without
113 a full understanding the inner workings of the controller, so it is
114 fortunate that the "fix" only modifies error paths.
115
116 See: https://github.com/raspberrypi/linux/issues/2728
117
118 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
119
120 bcm2835-mmc: Fix DMA channel leak
121
122 The BCM2835 MMC host driver requests a DMA channel on probe but neglects
123 to release the channel in the probe error path and on driver unbind.
124
125 I'm seeing this happen on every boot of the Compute Module 3: On first
126 driver probe, DMA channel 2 is allocated and then leaked with a "could
127 not get clk, deferring probe" message. On second driver probe, channel 4
128 is allocated.
129
130 Fix it.
131
132 Signed-off-by: Lukas Wunner <lukas@wunner.de>
133 Cc: Frank Pavlic <f.pavlic@kunbus.de>
134
135 bcm2835-mmc: Fix struct mmc_host leak on probe
136
137 The BCM2835 MMC host driver requests the bus address of the host's
138 register map on probe. If that fails, the driver leaks the struct
139 mmc_host allocated earlier.
140
141 Fix it.
142
143 Signed-off-by: Lukas Wunner <lukas@wunner.de>
144 Cc: Frank Pavlic <f.pavlic@kunbus.de>
145
146 bcm2835-mmc: Fix duplicate free_irq() on remove
147
148 The BCM2835 MMC host driver requests its interrupt as a device-managed
149 resource, so the interrupt is automatically freed after the driver is
150 unbound.
151
152 However on driver unbind, bcm2835_mmc_remove() frees the interrupt
153 explicitly to avoid invocation of the interrupt handler after driver
154 structures have been torn down.
155
156 The interrupt is thus freed twice, leading to a WARN splat in
157 __free_irq(). Fix by not requesting the interrupt as a device-managed
158 resource.
159
160 Signed-off-by: Lukas Wunner <lukas@wunner.de>
161 Cc: Frank Pavlic <f.pavlic@kunbus.de>
162
163 bcm2835-mmc: Handle mmc_add_host() errors
164
165 The BCM2835 MMC host driver calls mmc_add_host() but doesn't check its
166 return value. Errors occurring in that function are therefore not
167 handled. Fix it.
168
169 Signed-off-by: Lukas Wunner <lukas@wunner.de>
170 Cc: Frank Pavlic <f.pavlic@kunbus.de>
171
172 bcm2835-mmc: Deduplicate reset of driver data on remove
173
174 The BCM2835 MMC host driver sets the device's driver data pointer to
175 NULL on ->remove() even though the driver core subsequently does the
176 same in __device_release_driver(). Drop the duplicate assignment.
177
178 Signed-off-by: Lukas Wunner <lukas@wunner.de>
179 Cc: Frank Pavlic <f.pavlic@kunbus.de>
180
181 bcm2835_mmc: Remove vestigial threaded IRQ
182
183 With SDIO processing now managed by the MMC framework with a
184 workqueue, the bcm2835_mmc driver no longer needs a threaded
185 IRQ.
186
187 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
188
189 Add missing dma_unmap_sg calls to free relevant swiotlb bounce buffers.
190 This prevents DMA leaks.
191
192 Signed-off-by: Yaroslav Rosomakho <yaroslavros@gmail.com>
193
194 Limit max_req_size under arm64 (or any other platform that uses swiotlb) to prevent potential buffer overflow due to bouncing.
195
196 Signed-off-by: Yaroslav Rosomakho <yaroslavros@gmail.com>
197 ---
198 drivers/mmc/core/block.c | 28 +-
199 drivers/mmc/core/core.c | 3 +-
200 drivers/mmc/core/host.c | 17 +-
201 drivers/mmc/core/quirks.h | 8 +
202 drivers/mmc/host/Kconfig | 29 +
203 drivers/mmc/host/Makefile | 1 +
204 drivers/mmc/host/bcm2835-mmc.c | 1576 ++++++++++++++++++++++++++++++++
205 include/linux/mmc/card.h | 2 +
206 8 files changed, 1659 insertions(+), 5 deletions(-)
207 create mode 100644 drivers/mmc/host/bcm2835-mmc.c
208
209 --- a/drivers/mmc/core/block.c
210 +++ b/drivers/mmc/core/block.c
211 @@ -166,6 +166,13 @@ static DEFINE_MUTEX(open_lock);
212 module_param(perdev_minors, int, 0444);
213 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
214
215 +/*
216 + * Allow quirks to be overridden for the current card
217 + */
218 +static char *card_quirks;
219 +module_param(card_quirks, charp, 0644);
220 +MODULE_PARM_DESC(card_quirks, "Force the use of the indicated quirks (a bitfield)");
221 +
222 static inline int mmc_blk_part_switch(struct mmc_card *card,
223 unsigned int part_type);
224
225 @@ -2868,6 +2875,7 @@ static int mmc_blk_probe(struct mmc_card
226 {
227 struct mmc_blk_data *md, *part_md;
228 char cap_str[10];
229 + char quirk_str[24];
230
231 /*
232 * Check that the card supports the command class(es) we need.
233 @@ -2875,7 +2883,16 @@ static int mmc_blk_probe(struct mmc_card
234 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
235 return -ENODEV;
236
237 - mmc_fixup_device(card, mmc_blk_fixups);
238 + if (card_quirks) {
239 + unsigned long quirks;
240 + if (kstrtoul(card_quirks, 0, &quirks) == 0)
241 + card->quirks = (unsigned int)quirks;
242 + else
243 + pr_err("mmc_block: Invalid card_quirks parameter '%s'\n",
244 + card_quirks);
245 + }
246 + else
247 + mmc_fixup_device(card, mmc_blk_fixups);
248
249 card->complete_wq = alloc_workqueue("mmc_complete",
250 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
251 @@ -2890,9 +2907,14 @@ static int mmc_blk_probe(struct mmc_card
252
253 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
254 cap_str, sizeof(cap_str));
255 - pr_info("%s: %s %s %s %s\n",
256 + if (card->quirks)
257 + snprintf(quirk_str, sizeof(quirk_str),
258 + " (quirks 0x%08x)", card->quirks);
259 + else
260 + quirk_str[0] = '\0';
261 + pr_info("%s: %s %s %s%s%s\n",
262 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
263 - cap_str, md->read_only ? "(ro)" : "");
264 + cap_str, md->read_only ? " (ro)" : "", quirk_str);
265
266 if (mmc_blk_alloc_parts(card, md))
267 goto out;
268 --- a/drivers/mmc/core/core.c
269 +++ b/drivers/mmc/core/core.c
270 @@ -1922,7 +1922,8 @@ EXPORT_SYMBOL(mmc_erase);
271 int mmc_can_erase(struct mmc_card *card)
272 {
273 if ((card->host->caps & MMC_CAP_ERASE) &&
274 - (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
275 + (card->csd.cmdclass & CCC_ERASE) && card->erase_size &&
276 + !(card->quirks & MMC_QUIRK_ERASE_BROKEN))
277 return 1;
278 return 0;
279 }
280 --- a/drivers/mmc/core/host.c
281 +++ b/drivers/mmc/core/host.c
282 @@ -397,15 +397,30 @@ struct mmc_host *mmc_alloc_host(int extr
283 {
284 int err;
285 struct mmc_host *host;
286 + int id;
287
288 host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
289 if (!host)
290 return NULL;
291
292 + /* If OF aliases exist, start dynamic assignment after highest */
293 + id = of_alias_get_highest_id("mmc");
294 + id = (id < 0) ? 0 : id + 1;
295 +
296 + /* If this devices has OF node, maybe it has an alias */
297 + if (dev->of_node) {
298 + int of_id = of_alias_get_id(dev->of_node, "mmc");
299 +
300 + if (of_id < 0)
301 + dev_warn(dev, "/aliases ID not available\n");
302 + else
303 + id = of_id;
304 + }
305 +
306 /* scanning will be enabled when we're ready */
307 host->rescan_disable = 1;
308
309 - err = ida_simple_get(&mmc_host_ida, 0, 0, GFP_KERNEL);
310 + err = ida_simple_get(&mmc_host_ida, id, 0, GFP_KERNEL);
311 if (err < 0) {
312 kfree(host);
313 return NULL;
314 --- a/drivers/mmc/core/quirks.h
315 +++ b/drivers/mmc/core/quirks.h
316 @@ -99,6 +99,14 @@ static const struct mmc_fixup mmc_blk_fi
317 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
318 MMC_QUIRK_TRIM_BROKEN),
319
320 + /*
321 + * On some Kingston SD cards, multiple erases of less than 64
322 + * sectors can cause corruption.
323 + */
324 + MMC_FIXUP("SD16G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
325 + MMC_FIXUP("SD32G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
326 + MMC_FIXUP("SD64G", 0x41, 0x3432, add_quirk, MMC_QUIRK_ERASE_BROKEN),
327 +
328 END_FIXUP
329 };
330
331 --- a/drivers/mmc/host/Kconfig
332 +++ b/drivers/mmc/host/Kconfig
333 @@ -5,6 +5,35 @@
334
335 comment "MMC/SD/SDIO Host Controller Drivers"
336
337 +config MMC_BCM2835_MMC
338 + tristate "MMC support on BCM2835"
339 + depends on MACH_BCM2708 || MACH_BCM2709 || ARCH_BCM2835
340 + help
341 + This selects the MMC Interface on BCM2835.
342 +
343 + If you have a controller with this interface, say Y or M here.
344 +
345 + If unsure, say N.
346 +
347 +config MMC_BCM2835_DMA
348 + bool "DMA support on BCM2835 Arasan controller"
349 + depends on MMC_BCM2835_MMC
350 + help
351 + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
352 + based chips.
353 +
354 + If unsure, say N.
355 +
356 +config MMC_BCM2835_PIO_DMA_BARRIER
357 + int "Block count limit for PIO transfers"
358 + depends on MMC_BCM2835_MMC && MMC_BCM2835_DMA
359 + range 0 256
360 + default 2
361 + help
362 + The inclusive limit in bytes under which PIO will be used instead of DMA
363 +
364 + If unsure, say 2 here.
365 +
366 config MMC_DEBUG
367 bool "MMC host drivers debugging"
368 depends on MMC != n
369 --- a/drivers/mmc/host/Makefile
370 +++ b/drivers/mmc/host/Makefile
371 @@ -23,6 +23,7 @@ obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci
372 obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci_f_sdh30.o
373 obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
374 obj-$(CONFIG_MMC_SDHCI_AM654) += sdhci_am654.o
375 +obj-$(CONFIG_MMC_BCM2835_MMC) += bcm2835-mmc.o
376 obj-$(CONFIG_MMC_WBSD) += wbsd.o
377 obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
378 obj-$(CONFIG_MMC_ALCOR) += alcor.o
379 --- /dev/null
380 +++ b/drivers/mmc/host/bcm2835-mmc.c
381 @@ -0,0 +1,1576 @@
382 +/*
383 + * BCM2835 MMC host driver.
384 + *
385 + * Author: Gellert Weisz <gellert@raspberrypi.org>
386 + * Copyright 2014
387 + *
388 + * Based on
389 + * sdhci-bcm2708.c by Broadcom
390 + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
391 + * sdhci.c and sdhci-pci.c by Pierre Ossman
392 + *
393 + * This program is free software; you can redistribute it and/or modify it
394 + * under the terms and conditions of the GNU General Public License,
395 + * version 2, as published by the Free Software Foundation.
396 + *
397 + * This program is distributed in the hope it will be useful, but WITHOUT
398 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
399 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
400 + * more details.
401 + *
402 + * You should have received a copy of the GNU General Public License
403 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
404 + */
405 +
406 +#include <linux/delay.h>
407 +#include <linux/module.h>
408 +#include <linux/io.h>
409 +#include <linux/mmc/mmc.h>
410 +#include <linux/mmc/host.h>
411 +#include <linux/mmc/sd.h>
412 +#include <linux/scatterlist.h>
413 +#include <linux/of_address.h>
414 +#include <linux/of_irq.h>
415 +#include <linux/clk.h>
416 +#include <linux/platform_device.h>
417 +#include <linux/err.h>
418 +#include <linux/blkdev.h>
419 +#include <linux/dmaengine.h>
420 +#include <linux/dma-mapping.h>
421 +#include <linux/of_dma.h>
422 +#include <linux/swiotlb.h>
423 +
424 +#include "sdhci.h"
425 +
426 +
427 +#define DRIVER_NAME "mmc-bcm2835"
428 +
429 +#define DBG(f, x...) \
430 +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
431 +
432 +#ifndef CONFIG_MMC_BCM2835_DMA
433 + #define FORCE_PIO
434 +#endif
435 +
436 +
437 +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
438 +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
439 +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
440 +#else
441 +#define PIO_DMA_BARRIER 00
442 +#endif
443 +
444 +#define MIN_FREQ 400000
445 +#define TIMEOUT_VAL 0xE
446 +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
447 +
448 +
449 +unsigned mmc_debug;
450 +unsigned mmc_debug2;
451 +
452 +struct bcm2835_host {
453 + spinlock_t lock;
454 +
455 + void __iomem *ioaddr;
456 + u32 bus_addr;
457 +
458 + struct mmc_host *mmc;
459 +
460 + u32 timeout;
461 +
462 + int clock; /* Current clock speed */
463 + u8 pwr; /* Current voltage */
464 +
465 + unsigned int max_clk; /* Max possible freq */
466 + unsigned int timeout_clk; /* Timeout freq (KHz) */
467 + unsigned int clk_mul; /* Clock Muliplier value */
468 +
469 + struct tasklet_struct finish_tasklet; /* Tasklet structures */
470 +
471 + struct timer_list timer; /* Timer for timeouts */
472 +
473 + struct sg_mapping_iter sg_miter; /* SG state for PIO */
474 + unsigned int blocks; /* remaining PIO blocks */
475 +
476 + int irq; /* Device IRQ */
477 +
478 +
479 + u32 ier; /* cached registers */
480 +
481 + struct mmc_request *mrq; /* Current request */
482 + struct mmc_command *cmd; /* Current command */
483 + struct mmc_data *data; /* Current data request */
484 + unsigned int data_early:1; /* Data finished before cmd */
485 +
486 + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
487 +
488 + u32 shadow;
489 +
490 + /*DMA part*/
491 + struct dma_chan *dma_chan_rxtx; /* DMA channel for reads and writes */
492 + struct dma_slave_config dma_cfg_rx;
493 + struct dma_slave_config dma_cfg_tx;
494 + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
495 +
496 + bool have_dma;
497 + bool use_dma;
498 + bool wait_for_dma;
499 + /*end of DMA part*/
500 +
501 + int max_delay; /* maximum length of time spent waiting */
502 +
503 + int flags; /* Host attributes */
504 +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
505 +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
506 +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
507 +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
508 +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
509 +
510 + u32 overclock_50; /* frequency to use when 50MHz is requested (in MHz) */
511 + u32 max_overclock; /* Highest reported */
512 +};
513 +
514 +
515 +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg, int from)
516 +{
517 + unsigned delay;
518 + lockdep_assert_held_once(&host->lock);
519 + writel(val, host->ioaddr + reg);
520 + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
521 +
522 + delay = ((mmc_debug >> 16) & 0xf) << ((mmc_debug >> 20) & 0xf);
523 + if (delay && !((1<<from) & mmc_debug2))
524 + udelay(delay);
525 +}
526 +
527 +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
528 +{
529 + unsigned delay;
530 + lockdep_assert_held_once(&host->lock);
531 + writel(val, host->ioaddr + reg);
532 +
533 + delay = ((mmc_debug >> 24) & 0xf) << ((mmc_debug >> 28) & 0xf);
534 + if (delay)
535 + udelay(delay);
536 +}
537 +
538 +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
539 +{
540 + lockdep_assert_held_once(&host->lock);
541 + return readl(host->ioaddr + reg);
542 +}
543 +
544 +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
545 +{
546 + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
547 + bcm2835_mmc_readl(host, reg & ~3);
548 + u32 word_num = (reg >> 1) & 1;
549 + u32 word_shift = word_num * 16;
550 + u32 mask = 0xffff << word_shift;
551 + u32 newval = (oldval & ~mask) | (val << word_shift);
552 +
553 + if (reg == SDHCI_TRANSFER_MODE)
554 + host->shadow = newval;
555 + else
556 + bcm2835_mmc_writel(host, newval, reg & ~3, 0);
557 +
558 +}
559 +
560 +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
561 +{
562 + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
563 + u32 byte_num = reg & 3;
564 + u32 byte_shift = byte_num * 8;
565 + u32 mask = 0xff << byte_shift;
566 + u32 newval = (oldval & ~mask) | (val << byte_shift);
567 +
568 + bcm2835_mmc_writel(host, newval, reg & ~3, 1);
569 +}
570 +
571 +
572 +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
573 +{
574 + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
575 + u32 word_num = (reg >> 1) & 1;
576 + u32 word_shift = word_num * 16;
577 + u32 word = (val >> word_shift) & 0xffff;
578 +
579 + return word;
580 +}
581 +
582 +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
583 +{
584 + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
585 + u32 byte_num = reg & 3;
586 + u32 byte_shift = byte_num * 8;
587 + u32 byte = (val >> byte_shift) & 0xff;
588 +
589 + return byte;
590 +}
591 +
592 +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
593 +{
594 + u32 ier;
595 +
596 + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
597 + ier &= ~clear;
598 + /* change which requests generate IRQs - makes no difference to
599 + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
600 + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE, 2);
601 +}
602 +
603 +
604 +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
605 +{
606 + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
607 + mmc_hostname(host->mmc));
608 +
609 + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
610 + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
611 + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
612 + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
613 + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
614 + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
615 + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
616 + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
617 + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
618 + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
619 + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
620 + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
621 + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
622 + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
623 + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
624 + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
625 + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
626 + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
627 + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
628 + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
629 + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
630 + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
631 + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
632 + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
633 + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
634 + bcm2835_mmc_readw(host, SDHCI_AUTO_CMD_STATUS),
635 + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
636 + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
637 + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
638 + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
639 + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
640 + bcm2835_mmc_readw(host, SDHCI_COMMAND),
641 + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
642 + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
643 + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
644 +
645 + pr_debug(DRIVER_NAME ": ===========================================\n");
646 +}
647 +
648 +
649 +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
650 +{
651 + unsigned long timeout;
652 + unsigned long flags;
653 +
654 + spin_lock_irqsave(&host->lock, flags);
655 + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
656 +
657 + if (mask & SDHCI_RESET_ALL)
658 + host->clock = 0;
659 +
660 + /* Wait max 100 ms */
661 + timeout = 100;
662 +
663 + /* hw clears the bit when it's done */
664 + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
665 + if (timeout == 0) {
666 + pr_err("%s: Reset 0x%x never completed.\n",
667 + mmc_hostname(host->mmc), (int)mask);
668 + bcm2835_mmc_dumpregs(host);
669 + return;
670 + }
671 + timeout--;
672 + spin_unlock_irqrestore(&host->lock, flags);
673 + mdelay(1);
674 + spin_lock_irqsave(&host->lock, flags);
675 + }
676 +
677 + if (100-timeout > 10 && 100-timeout > host->max_delay) {
678 + host->max_delay = 100-timeout;
679 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
680 + }
681 + spin_unlock_irqrestore(&host->lock, flags);
682 +}
683 +
684 +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
685 +
686 +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
687 +{
688 + unsigned long flags;
689 + if (soft)
690 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
691 + else
692 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
693 +
694 + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
695 + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
696 + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
697 + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
698 + SDHCI_INT_RESPONSE;
699 +
700 + spin_lock_irqsave(&host->lock, flags);
701 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 3);
702 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 3);
703 + spin_unlock_irqrestore(&host->lock, flags);
704 +
705 + if (soft) {
706 + /* force clock reconfiguration */
707 + host->clock = 0;
708 + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
709 + }
710 +}
711 +
712 +
713 +
714 +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
715 +
716 +static void bcm2835_mmc_dma_complete(void *param)
717 +{
718 + struct bcm2835_host *host = param;
719 + struct dma_chan *dma_chan;
720 + unsigned long flags;
721 + u32 dir_data;
722 +
723 + spin_lock_irqsave(&host->lock, flags);
724 +
725 + host->use_dma = false;
726 +
727 + if (host->data) {
728 + dma_chan = host->dma_chan_rxtx;
729 + if (host->data->flags & MMC_DATA_WRITE)
730 + dir_data = DMA_TO_DEVICE;
731 + else
732 + dir_data = DMA_FROM_DEVICE;
733 + dma_unmap_sg(dma_chan->device->dev,
734 + host->data->sg, host->data->sg_len,
735 + dir_data);
736 + if (! (host->data->flags & MMC_DATA_WRITE))
737 + bcm2835_mmc_finish_data(host);
738 + } else if (host->wait_for_dma) {
739 + host->wait_for_dma = false;
740 + tasklet_schedule(&host->finish_tasklet);
741 + }
742 +
743 + spin_unlock_irqrestore(&host->lock, flags);
744 +}
745 +
746 +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
747 +{
748 + unsigned long flags;
749 + size_t blksize, len, chunk;
750 +
751 + u32 uninitialized_var(scratch);
752 + u8 *buf;
753 +
754 + blksize = host->data->blksz;
755 + chunk = 0;
756 +
757 + local_irq_save(flags);
758 +
759 + while (blksize) {
760 + if (!sg_miter_next(&host->sg_miter))
761 + BUG();
762 +
763 + len = min(host->sg_miter.length, blksize);
764 +
765 + blksize -= len;
766 + host->sg_miter.consumed = len;
767 +
768 + buf = host->sg_miter.addr;
769 +
770 + while (len) {
771 + if (chunk == 0) {
772 + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
773 + chunk = 4;
774 + }
775 +
776 + *buf = scratch & 0xFF;
777 +
778 + buf++;
779 + scratch >>= 8;
780 + chunk--;
781 + len--;
782 + }
783 + }
784 +
785 + sg_miter_stop(&host->sg_miter);
786 +
787 + local_irq_restore(flags);
788 +}
789 +
790 +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
791 +{
792 + unsigned long flags;
793 + size_t blksize, len, chunk;
794 + u32 scratch;
795 + u8 *buf;
796 +
797 + blksize = host->data->blksz;
798 + chunk = 0;
799 + chunk = 0;
800 + scratch = 0;
801 +
802 + local_irq_save(flags);
803 +
804 + while (blksize) {
805 + if (!sg_miter_next(&host->sg_miter))
806 + BUG();
807 +
808 + len = min(host->sg_miter.length, blksize);
809 +
810 + blksize -= len;
811 + host->sg_miter.consumed = len;
812 +
813 + buf = host->sg_miter.addr;
814 +
815 + while (len) {
816 + scratch |= (u32)*buf << (chunk * 8);
817 +
818 + buf++;
819 + chunk++;
820 + len--;
821 +
822 + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
823 + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
824 + chunk = 0;
825 + scratch = 0;
826 + }
827 + }
828 + }
829 +
830 + sg_miter_stop(&host->sg_miter);
831 +
832 + local_irq_restore(flags);
833 +}
834 +
835 +
836 +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
837 +{
838 + u32 mask;
839 +
840 + BUG_ON(!host->data);
841 +
842 + if (host->blocks == 0)
843 + return;
844 +
845 + if (host->data->flags & MMC_DATA_READ)
846 + mask = SDHCI_DATA_AVAILABLE;
847 + else
848 + mask = SDHCI_SPACE_AVAILABLE;
849 +
850 + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
851 +
852 + if (host->data->flags & MMC_DATA_READ)
853 + bcm2835_bcm2835_mmc_read_block_pio(host);
854 + else
855 + bcm2835_bcm2835_mmc_write_block_pio(host);
856 +
857 + host->blocks--;
858 +
859 + /* QUIRK used in sdhci.c removes the 'if' */
860 + /* but it seems this is unnecessary */
861 + if (host->blocks == 0)
862 + break;
863 +
864 +
865 + }
866 +}
867 +
868 +
869 +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
870 +{
871 + u32 len, dir_data, dir_slave;
872 + struct dma_async_tx_descriptor *desc = NULL;
873 + struct dma_chan *dma_chan;
874 +
875 +
876 + WARN_ON(!host->data);
877 +
878 + if (!host->data)
879 + return;
880 +
881 + if (host->blocks == 0)
882 + return;
883 +
884 + dma_chan = host->dma_chan_rxtx;
885 + if (host->data->flags & MMC_DATA_READ) {
886 + dir_data = DMA_FROM_DEVICE;
887 + dir_slave = DMA_DEV_TO_MEM;
888 + } else {
889 + dir_data = DMA_TO_DEVICE;
890 + dir_slave = DMA_MEM_TO_DEV;
891 + }
892 +
893 + /* The parameters have already been validated, so this will not fail */
894 + (void)dmaengine_slave_config(dma_chan,
895 + (dir_data == DMA_FROM_DEVICE) ?
896 + &host->dma_cfg_rx :
897 + &host->dma_cfg_tx);
898 +
899 + BUG_ON(!dma_chan->device);
900 + BUG_ON(!dma_chan->device->dev);
901 + BUG_ON(!host->data->sg);
902 +
903 + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
904 + host->data->sg_len, dir_data);
905 + if (len > 0) {
906 + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
907 + len, dir_slave,
908 + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
909 + } else {
910 + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
911 + }
912 + if (desc) {
913 + unsigned long flags;
914 + spin_lock_irqsave(&host->lock, flags);
915 + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
916 + SDHCI_INT_SPACE_AVAIL);
917 + host->tx_desc = desc;
918 + desc->callback = bcm2835_mmc_dma_complete;
919 + desc->callback_param = host;
920 + spin_unlock_irqrestore(&host->lock, flags);
921 + dmaengine_submit(desc);
922 + dma_async_issue_pending(dma_chan);
923 + } else {
924 + dma_unmap_sg(dma_chan->device->dev, host->data->sg, len, dir_data);
925 + }
926 +
927 +}
928 +
929 +
930 +
931 +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
932 +{
933 + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
934 + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
935 +
936 + if (host->use_dma)
937 + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
938 + else
939 + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
940 +
941 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 4);
942 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 4);
943 +}
944 +
945 +
946 +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
947 +{
948 + u8 count;
949 + struct mmc_data *data = cmd->data;
950 +
951 + WARN_ON(host->data);
952 +
953 + if (data || (cmd->flags & MMC_RSP_BUSY)) {
954 + count = TIMEOUT_VAL;
955 + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
956 + }
957 +
958 + if (!data)
959 + return;
960 +
961 + /* Sanity checks */
962 + BUG_ON(data->blksz * data->blocks > 524288);
963 + BUG_ON(data->blksz > host->mmc->max_blk_size);
964 + BUG_ON(data->blocks > 65535);
965 +
966 + host->data = data;
967 + host->data_early = 0;
968 + host->data->bytes_xfered = 0;
969 +
970 +
971 + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
972 + int flags;
973 +
974 + flags = SG_MITER_ATOMIC;
975 + if (host->data->flags & MMC_DATA_READ)
976 + flags |= SG_MITER_TO_SG;
977 + else
978 + flags |= SG_MITER_FROM_SG;
979 + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
980 + host->blocks = data->blocks;
981 + }
982 +
983 + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
984 +
985 + bcm2835_mmc_set_transfer_irqs(host);
986 +
987 + /* Set the DMA boundary value and block size */
988 + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
989 + data->blksz), SDHCI_BLOCK_SIZE);
990 + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
991 +
992 + BUG_ON(!host->data);
993 +}
994 +
995 +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
996 + struct mmc_command *cmd)
997 +{
998 + u16 mode;
999 + struct mmc_data *data = cmd->data;
1000 +
1001 + if (data == NULL) {
1002 + /* clear Auto CMD settings for no data CMDs */
1003 + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
1004 + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
1005 + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
1006 + return;
1007 + }
1008 +
1009 + WARN_ON(!host->data);
1010 +
1011 + mode = SDHCI_TRNS_BLK_CNT_EN;
1012 +
1013 + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
1014 + mode |= SDHCI_TRNS_MULTI;
1015 +
1016 + /*
1017 + * If we are sending CMD23, CMD12 never gets sent
1018 + * on successful completion (so no Auto-CMD12).
1019 + */
1020 + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
1021 + mode |= SDHCI_TRNS_AUTO_CMD12;
1022 + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
1023 + mode |= SDHCI_TRNS_AUTO_CMD23;
1024 + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2, 5);
1025 + }
1026 + }
1027 +
1028 + if (data->flags & MMC_DATA_READ)
1029 + mode |= SDHCI_TRNS_READ;
1030 + if (host->flags & SDHCI_REQ_USE_DMA)
1031 + mode |= SDHCI_TRNS_DMA;
1032 +
1033 + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
1034 +}
1035 +
1036 +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
1037 +{
1038 + int flags;
1039 + u32 mask;
1040 + unsigned long timeout;
1041 +
1042 + WARN_ON(host->cmd);
1043 +
1044 + /* Wait max 10 ms */
1045 + timeout = 1000;
1046 +
1047 + mask = SDHCI_CMD_INHIBIT;
1048 + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1049 + mask |= SDHCI_DATA_INHIBIT;
1050 +
1051 + /* We shouldn't wait for data inihibit for stop commands, even
1052 + though they might use busy signaling */
1053 + if (host->mrq->data && (cmd == host->mrq->data->stop))
1054 + mask &= ~SDHCI_DATA_INHIBIT;
1055 +
1056 + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
1057 + if (timeout == 0) {
1058 + pr_err("%s: Controller never released inhibit bit(s).\n",
1059 + mmc_hostname(host->mmc));
1060 + bcm2835_mmc_dumpregs(host);
1061 + cmd->error = -EIO;
1062 + tasklet_schedule(&host->finish_tasklet);
1063 + return;
1064 + }
1065 + timeout--;
1066 + udelay(10);
1067 + }
1068 +
1069 + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
1070 + host->max_delay = (1000-timeout)/100;
1071 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
1072 + }
1073 +
1074 + timeout = jiffies;
1075 + if (!cmd->data && cmd->busy_timeout > 9000)
1076 + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1077 + else
1078 + timeout += 10 * HZ;
1079 + mod_timer(&host->timer, timeout);
1080 +
1081 + host->cmd = cmd;
1082 + host->use_dma = false;
1083 +
1084 + bcm2835_mmc_prepare_data(host, cmd);
1085 +
1086 + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT, 6);
1087 +
1088 + bcm2835_mmc_set_transfer_mode(host, cmd);
1089 +
1090 + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1091 + pr_err("%s: Unsupported response type!\n",
1092 + mmc_hostname(host->mmc));
1093 + cmd->error = -EINVAL;
1094 + tasklet_schedule(&host->finish_tasklet);
1095 + return;
1096 + }
1097 +
1098 + if (!(cmd->flags & MMC_RSP_PRESENT))
1099 + flags = SDHCI_CMD_RESP_NONE;
1100 + else if (cmd->flags & MMC_RSP_136)
1101 + flags = SDHCI_CMD_RESP_LONG;
1102 + else if (cmd->flags & MMC_RSP_BUSY)
1103 + flags = SDHCI_CMD_RESP_SHORT_BUSY;
1104 + else
1105 + flags = SDHCI_CMD_RESP_SHORT;
1106 +
1107 + if (cmd->flags & MMC_RSP_CRC)
1108 + flags |= SDHCI_CMD_CRC;
1109 + if (cmd->flags & MMC_RSP_OPCODE)
1110 + flags |= SDHCI_CMD_INDEX;
1111 +
1112 + if (cmd->data)
1113 + flags |= SDHCI_CMD_DATA;
1114 +
1115 + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1116 +}
1117 +
1118 +
1119 +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
1120 +{
1121 + struct mmc_data *data;
1122 +
1123 + BUG_ON(!host->data);
1124 +
1125 + data = host->data;
1126 + host->data = NULL;
1127 +
1128 + if (data->error)
1129 + data->bytes_xfered = 0;
1130 + else
1131 + data->bytes_xfered = data->blksz * data->blocks;
1132 +
1133 + /*
1134 + * Need to send CMD12 if -
1135 + * a) open-ended multiblock transfer (no CMD23)
1136 + * b) error in multiblock transfer
1137 + */
1138 + if (data->stop &&
1139 + (data->error ||
1140 + !host->mrq->sbc)) {
1141 +
1142 + /*
1143 + * The controller needs a reset of internal state machines
1144 + * upon error conditions.
1145 + */
1146 + if (data->error) {
1147 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
1148 + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
1149 + }
1150 +
1151 + bcm2835_mmc_send_command(host, data->stop);
1152 + } else if (host->use_dma) {
1153 + host->wait_for_dma = true;
1154 + } else {
1155 + tasklet_schedule(&host->finish_tasklet);
1156 + }
1157 +}
1158 +
1159 +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
1160 +{
1161 + int i;
1162 +
1163 + BUG_ON(host->cmd == NULL);
1164 +
1165 + if (host->cmd->flags & MMC_RSP_PRESENT) {
1166 + if (host->cmd->flags & MMC_RSP_136) {
1167 + /* CRC is stripped so we need to do some shifting. */
1168 + for (i = 0; i < 4; i++) {
1169 + host->cmd->resp[i] = bcm2835_mmc_readl(host,
1170 + SDHCI_RESPONSE + (3-i)*4) << 8;
1171 + if (i != 3)
1172 + host->cmd->resp[i] |=
1173 + bcm2835_mmc_readb(host,
1174 + SDHCI_RESPONSE + (3-i)*4-1);
1175 + }
1176 + } else {
1177 + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
1178 + }
1179 + }
1180 +
1181 + host->cmd->error = 0;
1182 +
1183 + /* Finished CMD23, now send actual command. */
1184 + if (host->cmd == host->mrq->sbc) {
1185 + host->cmd = NULL;
1186 + bcm2835_mmc_send_command(host, host->mrq->cmd);
1187 +
1188 + if (host->mrq->cmd->data && host->use_dma) {
1189 + /* DMA transfer starts now, PIO starts after interrupt */
1190 + bcm2835_mmc_transfer_dma(host);
1191 + }
1192 + } else {
1193 +
1194 + /* Processed actual command. */
1195 + if (host->data && host->data_early)
1196 + bcm2835_mmc_finish_data(host);
1197 +
1198 + if (!host->cmd->data)
1199 + tasklet_schedule(&host->finish_tasklet);
1200 +
1201 + host->cmd = NULL;
1202 + }
1203 +}
1204 +
1205 +
1206 +static void bcm2835_mmc_timeout_timer(struct timer_list *t)
1207 +{
1208 + struct bcm2835_host *host = from_timer(host, t, timer);
1209 + unsigned long flags;
1210 +
1211 + spin_lock_irqsave(&host->lock, flags);
1212 +
1213 + if (host->mrq) {
1214 + pr_err("%s: Timeout waiting for hardware interrupt.\n",
1215 + mmc_hostname(host->mmc));
1216 + bcm2835_mmc_dumpregs(host);
1217 +
1218 + if (host->data) {
1219 + host->data->error = -ETIMEDOUT;
1220 + bcm2835_mmc_finish_data(host);
1221 + } else {
1222 + if (host->cmd)
1223 + host->cmd->error = -ETIMEDOUT;
1224 + else
1225 + host->mrq->cmd->error = -ETIMEDOUT;
1226 +
1227 + tasklet_schedule(&host->finish_tasklet);
1228 + }
1229 + }
1230 +
1231 + spin_unlock_irqrestore(&host->lock, flags);
1232 +}
1233 +
1234 +
1235 +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
1236 +{
1237 + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1238 + if (enable)
1239 + host->ier |= SDHCI_INT_CARD_INT;
1240 + else
1241 + host->ier &= ~SDHCI_INT_CARD_INT;
1242 +
1243 + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE, 7);
1244 + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE, 7);
1245 + }
1246 +}
1247 +
1248 +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1249 +{
1250 + struct bcm2835_host *host = mmc_priv(mmc);
1251 + unsigned long flags;
1252 +
1253 + spin_lock_irqsave(&host->lock, flags);
1254 + if (enable)
1255 + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1256 + else
1257 + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1258 +
1259 + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
1260 + spin_unlock_irqrestore(&host->lock, flags);
1261 +}
1262 +
1263 +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
1264 +{
1265 +
1266 + BUG_ON(intmask == 0);
1267 +
1268 + if (!host->cmd) {
1269 + pr_err("%s: Got command interrupt 0x%08x even "
1270 + "though no command operation was in progress.\n",
1271 + mmc_hostname(host->mmc), (unsigned)intmask);
1272 + bcm2835_mmc_dumpregs(host);
1273 + return;
1274 + }
1275 +
1276 + if (intmask & SDHCI_INT_TIMEOUT)
1277 + host->cmd->error = -ETIMEDOUT;
1278 + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1279 + SDHCI_INT_INDEX)) {
1280 + host->cmd->error = -EILSEQ;
1281 + }
1282 +
1283 + if (host->cmd->error) {
1284 + tasklet_schedule(&host->finish_tasklet);
1285 + return;
1286 + }
1287 +
1288 + if (intmask & SDHCI_INT_RESPONSE)
1289 + bcm2835_mmc_finish_command(host);
1290 +
1291 +}
1292 +
1293 +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
1294 +{
1295 + struct dma_chan *dma_chan;
1296 + u32 dir_data;
1297 +
1298 + BUG_ON(intmask == 0);
1299 +
1300 + if (!host->data) {
1301 + /*
1302 + * The "data complete" interrupt is also used to
1303 + * indicate that a busy state has ended. See comment
1304 + * above in sdhci_cmd_irq().
1305 + */
1306 + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1307 + if (intmask & SDHCI_INT_DATA_END) {
1308 + bcm2835_mmc_finish_command(host);
1309 + return;
1310 + }
1311 + }
1312 +
1313 + pr_debug("%s: Got data interrupt 0x%08x even "
1314 + "though no data operation was in progress.\n",
1315 + mmc_hostname(host->mmc), (unsigned)intmask);
1316 + bcm2835_mmc_dumpregs(host);
1317 +
1318 + return;
1319 + }
1320 +
1321 + if (intmask & SDHCI_INT_DATA_TIMEOUT)
1322 + host->data->error = -ETIMEDOUT;
1323 + else if (intmask & SDHCI_INT_DATA_END_BIT)
1324 + host->data->error = -EILSEQ;
1325 + else if ((intmask & SDHCI_INT_DATA_CRC) &&
1326 + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
1327 + != MMC_BUS_TEST_R)
1328 + host->data->error = -EILSEQ;
1329 +
1330 + if (host->use_dma) {
1331 + if (host->data->flags & MMC_DATA_WRITE) {
1332 + /* IRQ handled here */
1333 +
1334 + dma_chan = host->dma_chan_rxtx;
1335 + dir_data = DMA_TO_DEVICE;
1336 + dma_unmap_sg(dma_chan->device->dev,
1337 + host->data->sg, host->data->sg_len,
1338 + dir_data);
1339 +
1340 + bcm2835_mmc_finish_data(host);
1341 + }
1342 +
1343 + } else {
1344 + if (host->data->error)
1345 + bcm2835_mmc_finish_data(host);
1346 + else {
1347 + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1348 + bcm2835_mmc_transfer_pio(host);
1349 +
1350 + if (intmask & SDHCI_INT_DATA_END) {
1351 + if (host->cmd) {
1352 + /*
1353 + * Data managed to finish before the
1354 + * command completed. Make sure we do
1355 + * things in the proper order.
1356 + */
1357 + host->data_early = 1;
1358 + } else {
1359 + bcm2835_mmc_finish_data(host);
1360 + }
1361 + }
1362 + }
1363 + }
1364 +}
1365 +
1366 +
1367 +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
1368 +{
1369 + irqreturn_t result = IRQ_NONE;
1370 + struct bcm2835_host *host = dev_id;
1371 + u32 intmask, mask, unexpected = 0;
1372 + int max_loops = 16;
1373 +
1374 + spin_lock(&host->lock);
1375 +
1376 + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1377 +
1378 + if (!intmask || intmask == 0xffffffff) {
1379 + result = IRQ_NONE;
1380 + goto out;
1381 + }
1382 +
1383 + do {
1384 + /* Clear selected interrupts. */
1385 + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
1386 + SDHCI_INT_BUS_POWER);
1387 + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS, 8);
1388 +
1389 +
1390 + if (intmask & SDHCI_INT_CMD_MASK)
1391 + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1392 +
1393 + if (intmask & SDHCI_INT_DATA_MASK)
1394 + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1395 +
1396 + if (intmask & SDHCI_INT_BUS_POWER)
1397 + pr_err("%s: Card is consuming too much power!\n",
1398 + mmc_hostname(host->mmc));
1399 +
1400 + if (intmask & SDHCI_INT_CARD_INT) {
1401 + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
1402 + sdio_signal_irq(host->mmc);
1403 + }
1404 +
1405 + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
1406 + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
1407 + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
1408 + SDHCI_INT_CARD_INT);
1409 +
1410 + if (intmask) {
1411 + unexpected |= intmask;
1412 + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS, 9);
1413 + }
1414 +
1415 + if (result == IRQ_NONE)
1416 + result = IRQ_HANDLED;
1417 +
1418 + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1419 + } while (intmask && --max_loops);
1420 +out:
1421 + spin_unlock(&host->lock);
1422 +
1423 + if (unexpected) {
1424 + pr_err("%s: Unexpected interrupt 0x%08x.\n",
1425 + mmc_hostname(host->mmc), unexpected);
1426 + bcm2835_mmc_dumpregs(host);
1427 + }
1428 +
1429 + return result;
1430 +}
1431 +
1432 +
1433 +static void bcm2835_mmc_ack_sdio_irq(struct mmc_host *mmc)
1434 +{
1435 + struct bcm2835_host *host = mmc_priv(mmc);
1436 + unsigned long flags;
1437 +
1438 + spin_lock_irqsave(&host->lock, flags);
1439 + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
1440 + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
1441 + spin_unlock_irqrestore(&host->lock, flags);
1442 +}
1443 +
1444 +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
1445 +{
1446 + int div = 0; /* Initialized for compiler warning */
1447 + int real_div = div, clk_mul = 1;
1448 + u16 clk = 0;
1449 + unsigned long timeout;
1450 + unsigned int input_clock = clock;
1451 +
1452 + if (host->overclock_50 && (clock == 50000000))
1453 + clock = host->overclock_50 * 1000000 + 999999;
1454 +
1455 + host->mmc->actual_clock = 0;
1456 +
1457 + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
1458 +
1459 + if (clock == 0)
1460 + return;
1461 +
1462 + /* Version 3.00 divisors must be a multiple of 2. */
1463 + if (host->max_clk <= clock)
1464 + div = 1;
1465 + else {
1466 + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1467 + div += 2) {
1468 + if ((host->max_clk / div) <= clock)
1469 + break;
1470 + }
1471 + }
1472 +
1473 + real_div = div;
1474 + div >>= 1;
1475 +
1476 + if (real_div)
1477 + clock = (host->max_clk * clk_mul) / real_div;
1478 + host->mmc->actual_clock = clock;
1479 +
1480 + if ((clock > input_clock) && (clock > host->max_overclock)) {
1481 + pr_warn("%s: Overclocking to %dHz\n",
1482 + mmc_hostname(host->mmc), clock);
1483 + host->max_overclock = clock;
1484 + }
1485 +
1486 + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1487 + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1488 + << SDHCI_DIVIDER_HI_SHIFT;
1489 + clk |= SDHCI_CLOCK_INT_EN;
1490 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1491 +
1492 + /* Wait max 20 ms */
1493 + timeout = 20;
1494 + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
1495 + & SDHCI_CLOCK_INT_STABLE)) {
1496 + if (timeout == 0) {
1497 + pr_err("%s: Internal clock never "
1498 + "stabilised.\n", mmc_hostname(host->mmc));
1499 + bcm2835_mmc_dumpregs(host);
1500 + return;
1501 + }
1502 + timeout--;
1503 + mdelay(1);
1504 + }
1505 +
1506 + if (20-timeout > 10 && 20-timeout > host->max_delay) {
1507 + host->max_delay = 20-timeout;
1508 + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
1509 + }
1510 +
1511 + clk |= SDHCI_CLOCK_CARD_EN;
1512 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1513 +}
1514 +
1515 +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
1516 +{
1517 + struct bcm2835_host *host;
1518 + unsigned long flags;
1519 +
1520 + host = mmc_priv(mmc);
1521 +
1522 + spin_lock_irqsave(&host->lock, flags);
1523 +
1524 + WARN_ON(host->mrq != NULL);
1525 +
1526 + host->mrq = mrq;
1527 +
1528 + if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1529 + bcm2835_mmc_send_command(host, mrq->sbc);
1530 + else
1531 + bcm2835_mmc_send_command(host, mrq->cmd);
1532 +
1533 + spin_unlock_irqrestore(&host->lock, flags);
1534 +
1535 + if (!(mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) && mrq->cmd->data && host->use_dma) {
1536 + /* DMA transfer starts now, PIO starts after interrupt */
1537 + bcm2835_mmc_transfer_dma(host);
1538 + }
1539 +}
1540 +
1541 +
1542 +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1543 +{
1544 +
1545 + struct bcm2835_host *host = mmc_priv(mmc);
1546 + unsigned long flags;
1547 + u8 ctrl;
1548 + u16 clk, ctrl_2;
1549 +
1550 + pr_debug("bcm2835_mmc_set_ios: clock %d, pwr %d, bus_width %d, timing %d, vdd %d, drv_type %d\n",
1551 + ios->clock, ios->power_mode, ios->bus_width,
1552 + ios->timing, ios->signal_voltage, ios->drv_type);
1553 +
1554 + spin_lock_irqsave(&host->lock, flags);
1555 +
1556 + if (!ios->clock || ios->clock != host->clock) {
1557 + bcm2835_mmc_set_clock(host, ios->clock);
1558 + host->clock = ios->clock;
1559 + }
1560 +
1561 + if (host->pwr != SDHCI_POWER_330) {
1562 + host->pwr = SDHCI_POWER_330;
1563 + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1564 + }
1565 +
1566 + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
1567 +
1568 + /* set bus width */
1569 + ctrl &= ~SDHCI_CTRL_8BITBUS;
1570 + if (ios->bus_width == MMC_BUS_WIDTH_4)
1571 + ctrl |= SDHCI_CTRL_4BITBUS;
1572 + else
1573 + ctrl &= ~SDHCI_CTRL_4BITBUS;
1574 +
1575 + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
1576 +
1577 +
1578 + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1579 + /*
1580 + * We only need to set Driver Strength if the
1581 + * preset value enable is not set.
1582 + */
1583 + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
1584 + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1585 + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1586 + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1587 + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1588 + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1589 +
1590 + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1591 +
1592 + /* Reset SD Clock Enable */
1593 + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
1594 + clk &= ~SDHCI_CLOCK_CARD_EN;
1595 + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
1596 +
1597 + /* Re-enable SD Clock */
1598 + bcm2835_mmc_set_clock(host, host->clock);
1599 + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1600 +
1601 + spin_unlock_irqrestore(&host->lock, flags);
1602 +}
1603 +
1604 +
1605 +static struct mmc_host_ops bcm2835_ops = {
1606 + .request = bcm2835_mmc_request,
1607 + .set_ios = bcm2835_mmc_set_ios,
1608 + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
1609 + .ack_sdio_irq = bcm2835_mmc_ack_sdio_irq,
1610 +};
1611 +
1612 +
1613 +static void bcm2835_mmc_tasklet_finish(unsigned long param)
1614 +{
1615 + struct bcm2835_host *host;
1616 + unsigned long flags;
1617 + struct mmc_request *mrq;
1618 +
1619 + host = (struct bcm2835_host *)param;
1620 +
1621 + spin_lock_irqsave(&host->lock, flags);
1622 +
1623 + /*
1624 + * If this tasklet gets rescheduled while running, it will
1625 + * be run again afterwards but without any active request.
1626 + */
1627 + if (!host->mrq) {
1628 + spin_unlock_irqrestore(&host->lock, flags);
1629 + return;
1630 + }
1631 +
1632 + del_timer(&host->timer);
1633 +
1634 + mrq = host->mrq;
1635 +
1636 + /*
1637 + * The controller needs a reset of internal state machines
1638 + * upon error conditions.
1639 + */
1640 + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1641 + ((mrq->cmd && mrq->cmd->error) ||
1642 + (mrq->data && (mrq->data->error ||
1643 + (mrq->data->stop && mrq->data->stop->error))))) {
1644 +
1645 + spin_unlock_irqrestore(&host->lock, flags);
1646 + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
1647 + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
1648 + spin_lock_irqsave(&host->lock, flags);
1649 + }
1650 +
1651 + host->mrq = NULL;
1652 + host->cmd = NULL;
1653 + host->data = NULL;
1654 +
1655 + spin_unlock_irqrestore(&host->lock, flags);
1656 + mmc_request_done(host->mmc, mrq);
1657 +}
1658 +
1659 +
1660 +
1661 +static int bcm2835_mmc_add_host(struct bcm2835_host *host)
1662 +{
1663 + struct mmc_host *mmc = host->mmc;
1664 + struct device *dev = mmc->parent;
1665 +#ifndef FORCE_PIO
1666 + struct dma_slave_config cfg;
1667 +#endif
1668 + int ret;
1669 +
1670 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
1671 +
1672 + host->clk_mul = 0;
1673 +
1674 + if (!mmc->f_max || mmc->f_max > host->max_clk)
1675 + mmc->f_max = host->max_clk;
1676 + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
1677 +
1678 + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
1679 + host->timeout_clk = mmc->f_max / 1000;
1680 + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
1681 +
1682 + /* host controller capabilities */
1683 + mmc->caps |= MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL |
1684 + MMC_CAP_SDIO_IRQ | MMC_CAP_SD_HIGHSPEED |
1685 + MMC_CAP_MMC_HIGHSPEED;
1686 +
1687 + mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
1688 +
1689 + host->flags = SDHCI_AUTO_CMD23;
1690 +
1691 + dev_info(dev, "mmc_debug:%x mmc_debug2:%x\n", mmc_debug, mmc_debug2);
1692 +#ifdef FORCE_PIO
1693 + dev_info(dev, "Forcing PIO mode\n");
1694 + host->have_dma = false;
1695 +#else
1696 + if (IS_ERR_OR_NULL(host->dma_chan_rxtx)) {
1697 + dev_err(dev, "%s: Unable to initialise DMA channel. Falling back to PIO\n",
1698 + DRIVER_NAME);
1699 + host->have_dma = false;
1700 + } else {
1701 + dev_info(dev, "DMA channel allocated");
1702 +
1703 + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1704 + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1705 + cfg.slave_id = 11; /* DREQ channel */
1706 +
1707 + /* Validate the slave configurations */
1708 +
1709 + cfg.direction = DMA_MEM_TO_DEV;
1710 + cfg.src_addr = 0;
1711 + cfg.dst_addr = host->bus_addr + SDHCI_BUFFER;
1712 +
1713 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
1714 +
1715 + if (ret == 0) {
1716 + host->dma_cfg_tx = cfg;
1717 +
1718 + cfg.direction = DMA_DEV_TO_MEM;
1719 + cfg.src_addr = host->bus_addr + SDHCI_BUFFER;
1720 + cfg.dst_addr = 0;
1721 +
1722 + ret = dmaengine_slave_config(host->dma_chan_rxtx, &cfg);
1723 + }
1724 +
1725 + if (ret == 0) {
1726 + host->dma_cfg_rx = cfg;
1727 +
1728 + host->have_dma = true;
1729 + } else {
1730 + pr_err("%s: unable to configure DMA channel. "
1731 + "Falling back to PIO\n",
1732 + mmc_hostname(mmc));
1733 + dma_release_channel(host->dma_chan_rxtx);
1734 + host->dma_chan_rxtx = NULL;
1735 + host->have_dma = false;
1736 + }
1737 + }
1738 +#endif
1739 + mmc->max_segs = 128;
1740 + if (swiotlb_max_segment())
1741 + mmc->max_req_size = (1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
1742 + else
1743 + mmc->max_req_size = 524288;
1744 + mmc->max_seg_size = mmc->max_req_size;
1745 + mmc->max_blk_size = 512;
1746 + mmc->max_blk_count = 65535;
1747 +
1748 + /* report supported voltage ranges */
1749 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1750 +
1751 + tasklet_init(&host->finish_tasklet,
1752 + bcm2835_mmc_tasklet_finish, (unsigned long)host);
1753 +
1754 + timer_setup(&host->timer, bcm2835_mmc_timeout_timer, 0);
1755 + init_waitqueue_head(&host->buf_ready_int);
1756 +
1757 + bcm2835_mmc_init(host, 0);
1758 + ret = request_irq(host->irq, bcm2835_mmc_irq, IRQF_SHARED,
1759 + mmc_hostname(mmc), host);
1760 + if (ret) {
1761 + dev_err(dev, "Failed to request IRQ %d: %d\n", host->irq, ret);
1762 + goto untasklet;
1763 + }
1764 +
1765 + ret = mmc_add_host(mmc);
1766 + if (ret) {
1767 + dev_err(dev, "could not add MMC host\n");
1768 + goto free_irq;
1769 + }
1770 +
1771 + return 0;
1772 +
1773 +free_irq:
1774 + free_irq(host->irq, host);
1775 +untasklet:
1776 + tasklet_kill(&host->finish_tasklet);
1777 +
1778 + return ret;
1779 +}
1780 +
1781 +static int bcm2835_mmc_probe(struct platform_device *pdev)
1782 +{
1783 + struct device *dev = &pdev->dev;
1784 + struct device_node *node = dev->of_node;
1785 + struct clk *clk;
1786 + struct resource *iomem;
1787 + struct bcm2835_host *host;
1788 + struct mmc_host *mmc;
1789 + const __be32 *addr;
1790 + int ret;
1791 +
1792 + mmc = mmc_alloc_host(sizeof(*host), dev);
1793 + if (!mmc)
1794 + return -ENOMEM;
1795 +
1796 + mmc->ops = &bcm2835_ops;
1797 + host = mmc_priv(mmc);
1798 + host->mmc = mmc;
1799 + host->timeout = msecs_to_jiffies(1000);
1800 + spin_lock_init(&host->lock);
1801 +
1802 + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1803 + host->ioaddr = devm_ioremap_resource(dev, iomem);
1804 + if (IS_ERR(host->ioaddr)) {
1805 + ret = PTR_ERR(host->ioaddr);
1806 + goto err;
1807 + }
1808 +
1809 + addr = of_get_address(node, 0, NULL, NULL);
1810 + if (!addr) {
1811 + dev_err(dev, "could not get DMA-register address\n");
1812 + ret = -ENODEV;
1813 + goto err;
1814 + }
1815 + host->bus_addr = be32_to_cpup(addr);
1816 + pr_debug(" - ioaddr %lx, iomem->start %lx, bus_addr %lx\n",
1817 + (unsigned long)host->ioaddr,
1818 + (unsigned long)iomem->start,
1819 + (unsigned long)host->bus_addr);
1820 +
1821 +#ifndef FORCE_PIO
1822 + if (node) {
1823 + host->dma_chan_rxtx = dma_request_slave_channel(dev, "rx-tx");
1824 + if (!host->dma_chan_rxtx)
1825 + host->dma_chan_rxtx =
1826 + dma_request_slave_channel(dev, "tx");
1827 + if (!host->dma_chan_rxtx)
1828 + host->dma_chan_rxtx =
1829 + dma_request_slave_channel(dev, "rx");
1830 + } else {
1831 + dma_cap_mask_t mask;
1832 +
1833 + dma_cap_zero(mask);
1834 + /* we don't care about the channel, any would work */
1835 + dma_cap_set(DMA_SLAVE, mask);
1836 + host->dma_chan_rxtx = dma_request_channel(mask, NULL, NULL);
1837 + }
1838 +#endif
1839 + clk = devm_clk_get(dev, NULL);
1840 + if (IS_ERR(clk)) {
1841 + ret = PTR_ERR(clk);
1842 + if (ret == -EPROBE_DEFER)
1843 + dev_info(dev, "could not get clk, deferring probe\n");
1844 + else
1845 + dev_err(dev, "could not get clk\n");
1846 + goto err;
1847 + }
1848 +
1849 + host->max_clk = clk_get_rate(clk);
1850 +
1851 + host->irq = platform_get_irq(pdev, 0);
1852 + if (host->irq <= 0) {
1853 + dev_err(dev, "get IRQ failed\n");
1854 + ret = -EINVAL;
1855 + goto err;
1856 + }
1857 +
1858 + if (node) {
1859 + mmc_of_parse(mmc);
1860 +
1861 + /* Read any custom properties */
1862 + of_property_read_u32(node,
1863 + "brcm,overclock-50",
1864 + &host->overclock_50);
1865 + } else {
1866 + mmc->caps |= MMC_CAP_4_BIT_DATA;
1867 + }
1868 +
1869 + ret = bcm2835_mmc_add_host(host);
1870 + if (ret)
1871 + goto err;
1872 +
1873 + platform_set_drvdata(pdev, host);
1874 +
1875 + return 0;
1876 +err:
1877 + if (host->dma_chan_rxtx)
1878 + dma_release_channel(host->dma_chan_rxtx);
1879 + mmc_free_host(mmc);
1880 +
1881 + return ret;
1882 +}
1883 +
1884 +static int bcm2835_mmc_remove(struct platform_device *pdev)
1885 +{
1886 + struct bcm2835_host *host = platform_get_drvdata(pdev);
1887 + unsigned long flags;
1888 + int dead;
1889 + u32 scratch;
1890 +
1891 + dead = 0;
1892 + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
1893 + if (scratch == (u32)-1)
1894 + dead = 1;
1895 +
1896 +
1897 + if (dead) {
1898 + spin_lock_irqsave(&host->lock, flags);
1899 +
1900 + host->flags |= SDHCI_DEVICE_DEAD;
1901 +
1902 + if (host->mrq) {
1903 + pr_err("%s: Controller removed during "
1904 + " transfer!\n", mmc_hostname(host->mmc));
1905 +
1906 + host->mrq->cmd->error = -ENOMEDIUM;
1907 + tasklet_schedule(&host->finish_tasklet);
1908 + }
1909 +
1910 + spin_unlock_irqrestore(&host->lock, flags);
1911 + }
1912 +
1913 + mmc_remove_host(host->mmc);
1914 +
1915 + if (!dead)
1916 + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
1917 +
1918 + free_irq(host->irq, host);
1919 +
1920 + del_timer_sync(&host->timer);
1921 +
1922 + tasklet_kill(&host->finish_tasklet);
1923 +
1924 + if (host->dma_chan_rxtx)
1925 + dma_release_channel(host->dma_chan_rxtx);
1926 +
1927 + mmc_free_host(host->mmc);
1928 +
1929 + return 0;
1930 +}
1931 +
1932 +
1933 +static const struct of_device_id bcm2835_mmc_match[] = {
1934 + { .compatible = "brcm,bcm2835-mmc" },
1935 + { }
1936 +};
1937 +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
1938 +
1939 +
1940 +
1941 +static struct platform_driver bcm2835_mmc_driver = {
1942 + .probe = bcm2835_mmc_probe,
1943 + .remove = bcm2835_mmc_remove,
1944 + .driver = {
1945 + .name = DRIVER_NAME,
1946 + .owner = THIS_MODULE,
1947 + .of_match_table = bcm2835_mmc_match,
1948 + },
1949 +};
1950 +module_platform_driver(bcm2835_mmc_driver);
1951 +
1952 +module_param(mmc_debug, uint, 0644);
1953 +module_param(mmc_debug2, uint, 0644);
1954 +MODULE_ALIAS("platform:mmc-bcm2835");
1955 +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
1956 +MODULE_LICENSE("GPL v2");
1957 +MODULE_AUTHOR("Gellert Weisz");
1958 --- a/include/linux/mmc/card.h
1959 +++ b/include/linux/mmc/card.h
1960 @@ -270,6 +270,8 @@ struct mmc_card {
1961 #define MMC_QUIRK_TRIM_BROKEN (1<<12) /* Skip trim */
1962 #define MMC_QUIRK_BROKEN_HPI (1<<13) /* Disable broken HPI support */
1963
1964 +#define MMC_QUIRK_ERASE_BROKEN (1<<31) /* Skip erase */
1965 +
1966 bool reenable_cmdq; /* Re-enable Command Queue */
1967
1968 unsigned int erase_size; /* erase size in sectors */