1 From 2ea9159435f6fdc6190126e7c413cb8c37259036 Mon Sep 17 00:00:00 2001
2 From: Florian Meier <florian.meier@koalo.de>
3 Date: Fri, 22 Nov 2013 14:22:53 +0100
4 Subject: [PATCH 031/381] dmaengine: Add support for BCM2708
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Add support for DMA controller of BCM2708 as used in the Raspberry Pi.
10 Currently it only supports cyclic DMA.
12 Signed-off-by: Florian Meier <florian.meier@koalo.de>
14 dmaengine: expand functionality by supporting scatter/gather transfers sdhci-bcm2708 and dma.c: fix for LITE channels
16 DMA: fix cyclic LITE length overflow bug
18 dmaengine: bcm2708: Remove chancnt affectations
20 Mirror bcm2835-dma.c commit 9eba5536a7434c69d8c185d4bd1c70734d92287d:
21 chancnt is already filled by dma_async_device_register, which uses the channel
22 list to know how much channels there is.
24 Since it's already filled, we can safely remove it from the drivers' probe
27 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
29 dmaengine: bcm2708: overwrite dreq only if it is not set
31 dreq is set when the DMA channel is fetched from Device Tree.
32 slave_id is set using dmaengine_slave_config().
33 Only overwrite dreq with slave_id if it is not set.
35 dreq/slave_id in the cyclic DMA case is not touched, because I don't
36 have hardware to test with.
38 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
40 dmaengine: bcm2708: do device registration in the board file
42 Don't register the device in the driver. Do it in the board file.
44 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
46 dmaengine: bcm2708: don't restrict DT support to ARCH_BCM2835
48 Both ARCH_BCM2835 and ARCH_BCM270x are built with OF now.
49 Add Device Tree support to the non ARCH_BCM2835 case.
50 Use the same driver name regardless of architecture.
52 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
54 BCM270x_DT: add bcm2835-dma entry
56 Add Device Tree entry for bcm2835-dma.
57 The entry doesn't contain any resources since they are handled
58 by the arch/arm/mach-bcm270x/dma.c driver.
59 In non-DT mode, don't add the device in the board file.
61 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
63 bcm2708-dmaengine: Add debug options
65 BCM270x: Add memory and irq resources to dmaengine device and DT
67 Prepare for merging of the legacy DMA API arch driver dma.c
68 with bcm2708-dmaengine by adding memory and irq resources both
69 to platform file device and Device Tree node.
70 Don't use BCM_DMAMAN_DRIVER_NAME so we don't have to include mach/dma.h
72 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
74 dmaengine: bcm2708: Merge with arch dma.c driver and disable dma.c
76 Merge the legacy DMA API driver with bcm2708-dmaengine.
77 This is done so we can use bcm2708_fb on ARCH_BCM2835 (mailbox
78 driver is also needed).
80 Changes to the dma.c code:
82 - Cutdown some comments to one line.
83 - Add mutex to vc_dmaman and use this, since the dev lock is locked
84 during probing of the engine part.
85 - Add global g_dmaman variable since drvdata is used by the engine part.
86 - Restructure for readability:
87 vc_dmaman_chan_alloc()
90 - Restructure bcm_dma_chan_alloc() to simplify error handling.
91 - Use device irq resources instead of hardcoded bcm_dma_irqs table.
92 - Remove dev_dmaman_register() and code it directly.
93 - Remove dev_dmaman_deregister() and code it directly.
94 - Simplify bcm_dmaman_probe() using devm_* functions.
95 - Get dmachans from DT if available.
96 - Keep 'dma.dmachans' module argument name for backwards compatibility.
98 Make it available on ARCH_BCM2835 as well.
100 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
102 dmaengine: bcm2708: set residue_granularity field
104 bcm2708-dmaengine supports residue reporting at burst level
105 but didn't report this via the residue_granularity field.
107 Without this field set properly we get playback issues with I2S cards.
109 dmaengine: bcm2708-dmaengine: Fix memory leak when stopping a running transfer
111 bcm2708-dmaengine: Use more DMA channels (but not 12)
113 1) Only the bcm2708_fb drivers uses the legacy DMA API, and
114 it requires a BULK-capable channel, so all other types
115 (FAST, NORMAL and LITE) can be made available to the regular
118 2) DMA channels 11-14 share an interrupt. The driver can't
119 handle this, so don't use channels 12-14 (12 was used, probably
120 because it appears to have an interrupt, but in reality that
121 interrupt is for activity on ANY channel). This may explain
122 a lockup encountered when running out of DMA channels.
124 The combined effect of this patch is to leave 7 DMA channels
125 available + channel 0 for bcm2708_fb via the legacy API.
127 See: https://github.com/raspberrypi/linux/issues/1110
128 https://github.com/raspberrypi/linux/issues/1108
130 dmaengine: bcm2708: Make legacy API available for bcm2835-dma
132 bcm2708_fb uses the legacy DMA API, so in order to start using
133 bcm2835-dma, bcm2835-dma has to support the legacy API. Make this
134 possible by exporting bcm_dmaman_probe() and bcm_dmaman_remove().
136 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
138 dmaengine: bcm2708: Change DT compatible string
140 Both bcm2835-dma and bcm2708-dmaengine have the same compatible string.
141 So change compatible to "brcm,bcm2708-dma".
143 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
145 dmaengine: bcm2708: Remove driver but keep legacy API
147 Dropping non-DT support means we don't need this driver,
148 but we still need the legacy DMA API.
150 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
152 drivers/dma/Kconfig | 4 +
153 drivers/dma/Makefile | 1 +
154 drivers/dma/bcm2708-dmaengine.c | 281 ++++++++++++++++++++++++++++++
155 include/linux/platform_data/dma-bcm2708.h | 143 +++++++++++++++
156 4 files changed, 429 insertions(+)
157 create mode 100644 drivers/dma/bcm2708-dmaengine.c
158 create mode 100644 include/linux/platform_data/dma-bcm2708.h
160 --- a/drivers/dma/Kconfig
161 +++ b/drivers/dma/Kconfig
162 @@ -470,6 +470,10 @@ config TIMB_DMA
164 Enable support for the Timberdale FPGA DMA engine.
167 + tristate "BCM2708 DMA legacy API support"
168 + depends on DMA_BCM2835
171 tristate "AM33xx CPPI41 DMA support"
173 --- a/drivers/dma/Makefile
174 +++ b/drivers/dma/Makefile
175 @@ -18,6 +18,7 @@ obj-$(CONFIG_AT_HDMAC) += at_hdmac.o
176 obj-$(CONFIG_AT_XDMAC) += at_xdmac.o
177 obj-$(CONFIG_AXI_DMAC) += dma-axi-dmac.o
178 obj-$(CONFIG_COH901318) += coh901318.o coh901318_lli.o
179 +obj-$(CONFIG_DMA_BCM2708) += bcm2708-dmaengine.o
180 obj-$(CONFIG_DMA_BCM2835) += bcm2835-dma.o
181 obj-$(CONFIG_DMA_JZ4740) += dma-jz4740.o
182 obj-$(CONFIG_DMA_JZ4780) += dma-jz4780.o
184 +++ b/drivers/dma/bcm2708-dmaengine.c
187 + * BCM2708 legacy DMA API
189 + * This program is free software; you can redistribute it and/or modify
190 + * it under the terms of the GNU General Public License as published by
191 + * the Free Software Foundation; either version 2 of the License, or
192 + * (at your option) any later version.
194 + * This program is distributed in the hope that it will be useful,
195 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
196 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
197 + * GNU General Public License for more details.
200 +#include <linux/init.h>
201 +#include <linux/interrupt.h>
202 +#include <linux/list.h>
203 +#include <linux/module.h>
204 +#include <linux/platform_data/dma-bcm2708.h>
205 +#include <linux/platform_device.h>
206 +#include <linux/slab.h>
207 +#include <linux/io.h>
208 +#include <linux/spinlock.h>
210 +#include "virt-dma.h"
212 +#define CACHE_LINE_MASK 31
213 +#define DEFAULT_DMACHAN_BITMAP 0x10 /* channel 4 only */
215 +/* valid only for channels 0 - 14, 15 has its own base address */
216 +#define BCM2708_DMA_CHAN(n) ((n) << 8) /* base address */
217 +#define BCM2708_DMA_CHANIO(dma_base, n) \
218 + ((void __iomem *)((char *)(dma_base) + BCM2708_DMA_CHAN(n)))
221 + void __iomem *dma_base;
222 + u32 chan_available; /* bitmap of available channels */
223 + u32 has_feature[BCM_DMA_FEATURE_COUNT]; /* bitmap of feature presence */
227 +static struct device *dmaman_dev; /* we assume there's only one! */
228 +static struct vc_dmaman *g_dmaman; /* DMA manager */
230 +/* DMA Auxiliary Functions */
232 +/* A DMA buffer on an arbitrary boundary may separate a cache line into a
233 + section inside the DMA buffer and another section outside it.
234 + Even if we flush DMA buffers from the cache there is always the chance that
235 + during a DMA someone will access the part of a cache line that is outside
236 + the DMA buffer - which will then bring in unwelcome data.
237 + Without being able to dictate our own buffer pools we must insist that
238 + DMA buffers consist of a whole number of cache lines.
240 +extern int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len)
244 + for (i = 0; i < sg_len; i++) {
245 + if (sg_ptr[i].offset & CACHE_LINE_MASK ||
246 + sg_ptr[i].length & CACHE_LINE_MASK)
252 +EXPORT_SYMBOL_GPL(bcm_sg_suitable_for_dma);
254 +extern void bcm_dma_start(void __iomem *dma_chan_base,
255 + dma_addr_t control_block)
257 + dsb(); /* ARM data synchronization (push) operation */
259 + writel(control_block, dma_chan_base + BCM2708_DMA_ADDR);
260 + writel(BCM2708_DMA_ACTIVE, dma_chan_base + BCM2708_DMA_CS);
262 +EXPORT_SYMBOL_GPL(bcm_dma_start);
264 +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base)
268 + /* ugly busy wait only option for now */
269 + while (readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE)
272 +EXPORT_SYMBOL_GPL(bcm_dma_wait_idle);
274 +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base)
278 + return readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE;
280 +EXPORT_SYMBOL_GPL(bcm_dma_is_busy);
282 +/* Complete an ongoing DMA (assuming its results are to be ignored)
283 + Does nothing if there is no DMA in progress.
284 + This routine waits for the current AXI transfer to complete before
285 + terminating the current DMA. If the current transfer is hung on a DREQ used
286 + by an uncooperative peripheral the AXI transfer may never complete. In this
287 + case the routine times out and return a non-zero error code.
288 + Use of this routine doesn't guarantee that the ongoing or aborted DMA
289 + does not produce an interrupt.
291 +extern int bcm_dma_abort(void __iomem *dma_chan_base)
293 + unsigned long int cs;
296 + cs = readl(dma_chan_base + BCM2708_DMA_CS);
298 + if (BCM2708_DMA_ACTIVE & cs) {
299 + long int timeout = 10000;
301 + /* write 0 to the active bit - pause the DMA */
302 + writel(0, dma_chan_base + BCM2708_DMA_CS);
304 + /* wait for any current AXI transfer to complete */
305 + while (0 != (cs & BCM2708_DMA_ISPAUSED) && --timeout >= 0)
306 + cs = readl(dma_chan_base + BCM2708_DMA_CS);
308 + if (0 != (cs & BCM2708_DMA_ISPAUSED)) {
309 + /* we'll un-pause when we set of our next DMA */
312 + } else if (BCM2708_DMA_ACTIVE & cs) {
313 + /* terminate the control block chain */
314 + writel(0, dma_chan_base + BCM2708_DMA_NEXTCB);
316 + /* abort the whole DMA */
317 + writel(BCM2708_DMA_ABORT | BCM2708_DMA_ACTIVE,
318 + dma_chan_base + BCM2708_DMA_CS);
324 +EXPORT_SYMBOL_GPL(bcm_dma_abort);
326 + /* DMA Manager Device Methods */
328 +static void vc_dmaman_init(struct vc_dmaman *dmaman, void __iomem *dma_base,
329 + u32 chans_available)
331 + dmaman->dma_base = dma_base;
332 + dmaman->chan_available = chans_available;
333 + dmaman->has_feature[BCM_DMA_FEATURE_FAST_ORD] = 0x0c; /* 2 & 3 */
334 + dmaman->has_feature[BCM_DMA_FEATURE_BULK_ORD] = 0x01; /* 0 */
335 + dmaman->has_feature[BCM_DMA_FEATURE_NORMAL_ORD] = 0xfe; /* 1 to 7 */
336 + dmaman->has_feature[BCM_DMA_FEATURE_LITE_ORD] = 0x7f00; /* 8 to 14 */
339 +static int vc_dmaman_chan_alloc(struct vc_dmaman *dmaman,
340 + unsigned required_feature_set)
346 + chans = dmaman->chan_available;
347 + for (feature = 0; feature < BCM_DMA_FEATURE_COUNT; feature++)
348 + /* select the subset of available channels with the desired
350 + if (required_feature_set & (1 << feature))
351 + chans &= dmaman->has_feature[feature];
356 + /* return the ordinal of the first channel in the bitmap */
357 + while (chans != 0 && (chans & 1) == 0) {
361 + /* claim the channel */
362 + dmaman->chan_available &= ~(1 << chan);
367 +static int vc_dmaman_chan_free(struct vc_dmaman *dmaman, int chan)
372 + if ((1 << chan) & dmaman->chan_available)
375 + dmaman->chan_available |= (1 << chan);
380 +/* DMA Manager Monitor */
382 +extern int bcm_dma_chan_alloc(unsigned required_feature_set,
383 + void __iomem **out_dma_base, int *out_dma_irq)
385 + struct vc_dmaman *dmaman = g_dmaman;
386 + struct platform_device *pdev = to_platform_device(dmaman_dev);
387 + struct resource *r;
393 + mutex_lock(&dmaman->lock);
394 + chan = vc_dmaman_chan_alloc(dmaman, required_feature_set);
398 + r = platform_get_resource(pdev, IORESOURCE_IRQ, (unsigned int)chan);
400 + dev_err(dmaman_dev, "failed to get irq for DMA channel %d\n",
402 + vc_dmaman_chan_free(dmaman, chan);
407 + *out_dma_base = BCM2708_DMA_CHANIO(dmaman->dma_base, chan);
408 + *out_dma_irq = r->start;
409 + dev_dbg(dmaman_dev,
410 + "Legacy API allocated channel=%d, base=%p, irq=%i\n",
411 + chan, *out_dma_base, *out_dma_irq);
414 + mutex_unlock(&dmaman->lock);
418 +EXPORT_SYMBOL_GPL(bcm_dma_chan_alloc);
420 +extern int bcm_dma_chan_free(int channel)
422 + struct vc_dmaman *dmaman = g_dmaman;
428 + mutex_lock(&dmaman->lock);
429 + rc = vc_dmaman_chan_free(dmaman, channel);
430 + mutex_unlock(&dmaman->lock);
434 +EXPORT_SYMBOL_GPL(bcm_dma_chan_free);
436 +int bcm_dmaman_probe(struct platform_device *pdev, void __iomem *base,
437 + u32 chans_available)
439 + struct device *dev = &pdev->dev;
440 + struct vc_dmaman *dmaman;
442 + dmaman = devm_kzalloc(dev, sizeof(*dmaman), GFP_KERNEL);
446 + mutex_init(&dmaman->lock);
447 + vc_dmaman_init(dmaman, base, chans_available);
451 + dev_info(dev, "DMA legacy API manager at %p, dmachans=0x%x\n",
452 + base, chans_available);
456 +EXPORT_SYMBOL(bcm_dmaman_probe);
458 +int bcm_dmaman_remove(struct platform_device *pdev)
464 +EXPORT_SYMBOL(bcm_dmaman_remove);
466 +MODULE_LICENSE("GPL");
468 +++ b/include/linux/platform_data/dma-bcm2708.h
471 + * Copyright (C) 2010 Broadcom
473 + * This program is free software; you can redistribute it and/or modify
474 + * it under the terms of the GNU General Public License version 2 as
475 + * published by the Free Software Foundation.
478 +#ifndef _PLAT_BCM2708_DMA_H
479 +#define _PLAT_BCM2708_DMA_H
481 +/* DMA CS Control and Status bits */
482 +#define BCM2708_DMA_ACTIVE BIT(0)
483 +#define BCM2708_DMA_INT BIT(2)
484 +#define BCM2708_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
485 +#define BCM2708_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
486 +#define BCM2708_DMA_ERR BIT(8)
487 +#define BCM2708_DMA_ABORT BIT(30) /* stop current CB, go to next, WO */
488 +#define BCM2708_DMA_RESET BIT(31) /* WO, self clearing */
490 +/* DMA control block "info" field bits */
491 +#define BCM2708_DMA_INT_EN BIT(0)
492 +#define BCM2708_DMA_TDMODE BIT(1)
493 +#define BCM2708_DMA_WAIT_RESP BIT(3)
494 +#define BCM2708_DMA_D_INC BIT(4)
495 +#define BCM2708_DMA_D_WIDTH BIT(5)
496 +#define BCM2708_DMA_D_DREQ BIT(6)
497 +#define BCM2708_DMA_S_INC BIT(8)
498 +#define BCM2708_DMA_S_WIDTH BIT(9)
499 +#define BCM2708_DMA_S_DREQ BIT(10)
501 +#define BCM2708_DMA_BURST(x) (((x) & 0xf) << 12)
502 +#define BCM2708_DMA_PER_MAP(x) ((x) << 16)
503 +#define BCM2708_DMA_WAITS(x) (((x) & 0x1f) << 21)
505 +#define BCM2708_DMA_DREQ_EMMC 11
506 +#define BCM2708_DMA_DREQ_SDHOST 13
508 +#define BCM2708_DMA_CS 0x00 /* Control and Status */
509 +#define BCM2708_DMA_ADDR 0x04
510 +/* the current control block appears in the following registers - read only */
511 +#define BCM2708_DMA_INFO 0x08
512 +#define BCM2708_DMA_SOURCE_AD 0x0c
513 +#define BCM2708_DMA_DEST_AD 0x10
514 +#define BCM2708_DMA_NEXTCB 0x1C
515 +#define BCM2708_DMA_DEBUG 0x20
517 +#define BCM2708_DMA4_CS (BCM2708_DMA_CHAN(4) + BCM2708_DMA_CS)
518 +#define BCM2708_DMA4_ADDR (BCM2708_DMA_CHAN(4) + BCM2708_DMA_ADDR)
520 +#define BCM2708_DMA_TDMODE_LEN(w, h) ((h) << 16 | (w))
522 +/* When listing features we can ask for when allocating DMA channels give
523 + those with higher priority smaller ordinal numbers */
524 +#define BCM_DMA_FEATURE_FAST_ORD 0
525 +#define BCM_DMA_FEATURE_BULK_ORD 1
526 +#define BCM_DMA_FEATURE_NORMAL_ORD 2
527 +#define BCM_DMA_FEATURE_LITE_ORD 3
528 +#define BCM_DMA_FEATURE_FAST BIT(BCM_DMA_FEATURE_FAST_ORD)
529 +#define BCM_DMA_FEATURE_BULK BIT(BCM_DMA_FEATURE_BULK_ORD)
530 +#define BCM_DMA_FEATURE_NORMAL BIT(BCM_DMA_FEATURE_NORMAL_ORD)
531 +#define BCM_DMA_FEATURE_LITE BIT(BCM_DMA_FEATURE_LITE_ORD)
532 +#define BCM_DMA_FEATURE_COUNT 4
534 +struct bcm2708_dma_cb {
535 + unsigned long info;
538 + unsigned long length;
539 + unsigned long stride;
540 + unsigned long next;
541 + unsigned long pad[2];
545 +struct platform_device;
547 +#ifdef CONFIG_DMA_BCM2708
549 +int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len);
550 +void bcm_dma_start(void __iomem *dma_chan_base, dma_addr_t control_block);
551 +void bcm_dma_wait_idle(void __iomem *dma_chan_base);
552 +bool bcm_dma_is_busy(void __iomem *dma_chan_base);
553 +int bcm_dma_abort(void __iomem *dma_chan_base);
555 +/* return channel no or -ve error */
556 +int bcm_dma_chan_alloc(unsigned preferred_feature_set,
557 + void __iomem **out_dma_base, int *out_dma_irq);
558 +int bcm_dma_chan_free(int channel);
560 +int bcm_dmaman_probe(struct platform_device *pdev, void __iomem *base,
561 + u32 chans_available);
562 +int bcm_dmaman_remove(struct platform_device *pdev);
564 +#else /* CONFIG_DMA_BCM2708 */
566 +static inline int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr,
572 +static inline void bcm_dma_start(void __iomem *dma_chan_base,
573 + dma_addr_t control_block) { }
575 +static inline void bcm_dma_wait_idle(void __iomem *dma_chan_base) { }
577 +static inline bool bcm_dma_is_busy(void __iomem *dma_chan_base)
582 +static inline int bcm_dma_abort(void __iomem *dma_chan_base)
587 +static inline int bcm_dma_chan_alloc(unsigned preferred_feature_set,
588 + void __iomem **out_dma_base,
594 +static inline int bcm_dma_chan_free(int channel)
599 +static inline int bcm_dmaman_probe(struct platform_device *pdev,
600 + void __iomem *base, u32 chans_available)
605 +static inline int bcm_dmaman_remove(struct platform_device *pdev)
610 +#endif /* CONFIG_DMA_BCM2708 */
612 +#endif /* _PLAT_BCM2708_DMA_H */