ea1e100a069ad9cc6887179591249630122e6629
[openwrt/svn-archive/archive.git] / target / linux / sunxi / patches-4.1 / 160-dmaengine-add-sun4i-driver.patch
1 From 1a28c76f3965775854ed6f6229de457c3d0674ab Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Emilio=20L=C3=B3pez?= <emilio@elopez.com.ar>
3 Date: Sat, 4 Apr 2015 11:37:24 +0200
4 Subject: [PATCH] dma: sun4i: Add support for the DMA engine on sun[457]i SoCs
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This patch adds support for the DMA engine present on Allwinner A10,
10 A13, A10S and A20 SoCs. This engine has two kinds of channels: normal
11 and dedicated. The main difference is in the mode of operation;
12 while a single normal channel may be operating at any given time,
13 dedicated channels may operate simultaneously provided there is no
14 overlap of source or destination.
15
16 Hardware documentation can be found on A10 User Manual (section 12), A13
17 User Manual (section 14) and A20 User Manual (section 1.12)
18
19 Signed-off-by: Emilio López <emilio@elopez.com.ar>
20 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
21 ---
22
23 Changes from v4:
24 * Fix for interrupt triggering after freeing a dma-channel, this fixed
25 the problems with jack
26 * Adjust to recent kernel dma API changes
27
28 Changes from v3:
29 * Drop threaded IRQ to get lower latency
30 * Drop chancnt
31 * Fix crash on first use when using a DMA-aware bootloader (eg., one
32 that supports NAND)
33
34 Changes from v2:
35 * Faster memcpy
36 * Quicker cyclic transfers
37 * Address some stylistic and locking comments from Maxime
38 * probably some more stuff I'm forgetting
39
40 Changes from v1:
41 * address comments from Chen-Yu and Maxime
42 * fix issue converting bus width
43 * switch to using a threaded IRQ instead of a tasklet on
44 recommendation from Maxime
45 * fix issue setting magic timing parameter for SPI transfers
46 * fix an issue with list handling reported by the kbuild 0-DAY robot (thanks!)
47 * drop a lot of unused #define
48 * probably some more stuff I'm forgetting
49 ---
50 .../devicetree/bindings/dma/sun4i-dma.txt | 46 +
51 drivers/dma/Kconfig | 11 +
52 drivers/dma/Makefile | 1 +
53 drivers/dma/sun4i-dma.c | 1235 ++++++++++++++++++++
54 4 files changed, 1293 insertions(+)
55 create mode 100644 Documentation/devicetree/bindings/dma/sun4i-dma.txt
56 create mode 100644 drivers/dma/sun4i-dma.c
57
58 diff --git a/Documentation/devicetree/bindings/dma/sun4i-dma.txt b/Documentation/devicetree/bindings/dma/sun4i-dma.txt
59 new file mode 100644
60 index 0000000..f1634a2
61 --- /dev/null
62 +++ b/Documentation/devicetree/bindings/dma/sun4i-dma.txt
63 @@ -0,0 +1,46 @@
64 +Allwinner A10 DMA Controller
65 +
66 +This driver follows the generic DMA bindings defined in dma.txt.
67 +
68 +Required properties:
69 +
70 +- compatible: Must be "allwinner,sun4i-a10-dma"
71 +- reg: Should contain the registers base address and length
72 +- interrupts: Should contain a reference to the interrupt used by this device
73 +- clocks: Should contain a reference to the parent AHB clock
74 +- #dma-cells : Should be 2, first cell denoting normal or dedicated dma,
75 + second cell holding the request line number.
76 +
77 +Example:
78 + dma: dma-controller@01c02000 {
79 + compatible = "allwinner,sun4i-a10-dma";
80 + reg = <0x01c02000 0x1000>;
81 + interrupts = <27>;
82 + clocks = <&ahb_gates 6>;
83 + #dma-cells = <2>;
84 + };
85 +
86 +Clients:
87 +
88 +DMA clients connected to the Allwinner A10 DMA controller must use the
89 +format described in the dma.txt file, using a three-cell specifier for
90 +each channel: a phandle plus two integer cells.
91 +The three cells in order are:
92 +
93 +1. A phandle pointing to the DMA controller.
94 +2. Whether it is using normal (0) or dedicated (1) channels
95 +3. The port ID as specified in the datasheet
96 +
97 +Example:
98 + spi2: spi@01c17000 {
99 + compatible = "allwinner,sun4i-a10-spi";
100 + reg = <0x01c17000 0x1000>;
101 + interrupts = <0 12 4>;
102 + clocks = <&ahb_gates 22>, <&spi2_clk>;
103 + clock-names = "ahb", "mod";
104 + dmas = <&dma 1 29>, <&dma 1 28>;
105 + dma-names = "rx", "tx";
106 + status = "disabled";
107 + #address-cells = <1>;
108 + #size-cells = <0>;
109 + };
110 diff --git a/drivers/dma/Kconfig b/drivers/dma/Kconfig
111 index fd7ac13..12372c2 100644
112 --- a/drivers/dma/Kconfig
113 +++ b/drivers/dma/Kconfig
114 @@ -443,6 +443,17 @@ config XGENE_DMA
115 help
116 Enable support for the APM X-Gene SoC DMA engine.
117
118 +config SUN4I_DMA
119 + tristate "Allwinner A10 DMA support"
120 + depends on (MACH_SUN4I || MACH_SUN5I || MACH_SUN7I || (COMPILE_TEST && OF && ARM))
121 + default (MACH_SUN4I || MACH_SUN5I || MACH_SUN7I)
122 + select DMA_ENGINE
123 + select DMA_OF
124 + select DMA_VIRTUAL_CHANNELS
125 + help
126 + Enable support for the DMA controller present in the sun4i,
127 + sun5i and sun7i Allwinner ARM SoCs.
128 +
129 config DMA_ENGINE
130 bool
131
132 diff --git a/drivers/dma/Makefile b/drivers/dma/Makefile
133 index 69f77d5..3eba5e9 100644
134 --- a/drivers/dma/Makefile
135 +++ b/drivers/dma/Makefile
136 @@ -54,3 +54,4 @@ obj-$(CONFIG_NBPFAXI_DMA) += nbpfaxi.o
137 obj-$(CONFIG_DMA_SUN6I) += sun6i-dma.o
138 obj-$(CONFIG_IMG_MDC_DMA) += img-mdc-dma.o
139 obj-$(CONFIG_XGENE_DMA) += xgene-dma.o
140 +obj-$(CONFIG_SUN4I_DMA) += sun4i-dma.o
141 diff --git a/drivers/dma/sun4i-dma.c b/drivers/dma/sun4i-dma.c
142 new file mode 100644
143 index 0000000..a8d55518
144 --- /dev/null
145 +++ b/drivers/dma/sun4i-dma.c
146 @@ -0,0 +1,1235 @@
147 +/*
148 + * Copyright (C) 2014 Emilio López
149 + * Emilio López <emilio@elopez.com.ar>
150 + *
151 + * This program is free software; you can redistribute it and/or modify
152 + * it under the terms of the GNU General Public License as published by
153 + * the Free Software Foundation; either version 2 of the License, or
154 + * (at your option) any later version.
155 + */
156 +
157 +#include <linux/bitmap.h>
158 +#include <linux/bitops.h>
159 +#include <linux/clk.h>
160 +#include <linux/dmaengine.h>
161 +#include <linux/dmapool.h>
162 +#include <linux/interrupt.h>
163 +#include <linux/module.h>
164 +#include <linux/of_dma.h>
165 +#include <linux/platform_device.h>
166 +#include <linux/slab.h>
167 +#include <linux/spinlock.h>
168 +
169 +#include "virt-dma.h"
170 +
171 +/** Normal DMA register values **/
172 +
173 +/* Normal DMA source/destination data request type values */
174 +#define NDMA_DRQ_TYPE_SDRAM 0x16
175 +#define NDMA_DRQ_TYPE_LIMIT (0x1F + 1)
176 +
177 +/** Normal DMA register layout **/
178 +
179 +/* Normal DMA configuration register layout */
180 +#define NDMA_CFG_LOADING BIT(31)
181 +#define NDMA_CFG_CONT_MODE BIT(30)
182 +#define NDMA_CFG_WAIT_STATE(n) ((n) << 27)
183 +#define NDMA_CFG_DEST_DATA_WIDTH(width) ((width) << 25)
184 +#define NDMA_CFG_DEST_BURST_LENGTH(len) ((len) << 23)
185 +#define NDMA_CFG_DEST_NON_SECURE BIT(22)
186 +#define NDMA_CFG_DEST_FIXED_ADDR BIT(21)
187 +#define NDMA_CFG_DEST_DRQ_TYPE(type) ((type) << 16)
188 +#define NDMA_CFG_BYTE_COUNT_MODE_REMAIN BIT(15)
189 +#define NDMA_CFG_SRC_DATA_WIDTH(width) ((width) << 9)
190 +#define NDMA_CFG_SRC_BURST_LENGTH(len) ((len) << 7)
191 +#define NDMA_CFG_SRC_NON_SECURE BIT(6)
192 +#define NDMA_CFG_SRC_FIXED_ADDR BIT(5)
193 +#define NDMA_CFG_SRC_DRQ_TYPE(type) ((type) << 0)
194 +
195 +/** Dedicated DMA register values **/
196 +
197 +/* Dedicated DMA source/destination address mode values */
198 +#define DDMA_ADDR_MODE_LINEAR 0
199 +#define DDMA_ADDR_MODE_IO 1
200 +#define DDMA_ADDR_MODE_HORIZONTAL_PAGE 2
201 +#define DDMA_ADDR_MODE_VERTICAL_PAGE 3
202 +
203 +/* Dedicated DMA source/destination data request type values */
204 +#define DDMA_DRQ_TYPE_SDRAM 0x1
205 +#define DDMA_DRQ_TYPE_LIMIT (0x1F + 1)
206 +
207 +/** Dedicated DMA register layout **/
208 +
209 +/* Dedicated DMA configuration register layout */
210 +#define DDMA_CFG_LOADING BIT(31)
211 +#define DDMA_CFG_BUSY BIT(30)
212 +#define DDMA_CFG_CONT_MODE BIT(29)
213 +#define DDMA_CFG_DEST_NON_SECURE BIT(28)
214 +#define DDMA_CFG_DEST_DATA_WIDTH(width) ((width) << 25)
215 +#define DDMA_CFG_DEST_BURST_LENGTH(len) ((len) << 23)
216 +#define DDMA_CFG_DEST_ADDR_MODE(mode) ((mode) << 21)
217 +#define DDMA_CFG_DEST_DRQ_TYPE(type) ((type) << 16)
218 +#define DDMA_CFG_BYTE_COUNT_MODE_REMAIN BIT(15)
219 +#define DDMA_CFG_SRC_NON_SECURE BIT(12)
220 +#define DDMA_CFG_SRC_DATA_WIDTH(width) ((width) << 9)
221 +#define DDMA_CFG_SRC_BURST_LENGTH(len) ((len) << 7)
222 +#define DDMA_CFG_SRC_ADDR_MODE(mode) ((mode) << 5)
223 +#define DDMA_CFG_SRC_DRQ_TYPE(type) ((type) << 0)
224 +
225 +/* Dedicated DMA parameter register layout */
226 +#define DDMA_PARA_DEST_DATA_BLK_SIZE(n) (((n) - 1) << 24)
227 +#define DDMA_PARA_DEST_WAIT_CYCLES(n) (((n) - 1) << 16)
228 +#define DDMA_PARA_SRC_DATA_BLK_SIZE(n) (((n) - 1) << 8)
229 +#define DDMA_PARA_SRC_WAIT_CYCLES(n) (((n) - 1) << 0)
230 +
231 +/** DMA register offsets **/
232 +
233 +/* General register offsets */
234 +#define DMA_IRQ_ENABLE_REG 0x0
235 +#define DMA_IRQ_PENDING_STATUS_REG 0x4
236 +
237 +/* Normal DMA register offsets */
238 +#define NDMA_CHANNEL_REG_BASE(n) (0x100 + (n) * 0x20)
239 +#define NDMA_CFG_REG 0x0
240 +#define NDMA_SRC_ADDR_REG 0x4
241 +#define NDMA_DEST_ADDR_REG 0x8
242 +#define NDMA_BYTE_COUNT_REG 0xC
243 +
244 +/* Dedicated DMA register offsets */
245 +#define DDMA_CHANNEL_REG_BASE(n) (0x300 + (n) * 0x20)
246 +#define DDMA_CFG_REG 0x0
247 +#define DDMA_SRC_ADDR_REG 0x4
248 +#define DDMA_DEST_ADDR_REG 0x8
249 +#define DDMA_BYTE_COUNT_REG 0xC
250 +#define DDMA_PARA_REG 0x18
251 +
252 +/** DMA Driver **/
253 +
254 +/*
255 + * Normal DMA has 8 channels, and Dedicated DMA has another 8, so that's
256 + * 16 channels. As for endpoints, there's 29 and 21 respectively. Given
257 + * that the Normal DMA endpoints (other than SDRAM) can be used as tx/rx,
258 + * we need 78 vchans in total
259 + */
260 +#define NDMA_NR_MAX_CHANNELS 8
261 +#define DDMA_NR_MAX_CHANNELS 8
262 +#define DMA_NR_MAX_CHANNELS (NDMA_NR_MAX_CHANNELS + DDMA_NR_MAX_CHANNELS)
263 +#define NDMA_NR_MAX_VCHANS (29 * 2 - 1)
264 +#define DDMA_NR_MAX_VCHANS 21
265 +#define DMA_NR_MAX_VCHANS (NDMA_NR_MAX_VCHANS + DDMA_NR_MAX_VCHANS)
266 +
267 +/* This set of DDMA timing parameters were found experimentally while
268 + * working with the SPI driver and seem to make it behave correctly */
269 +#define DDMA_MAGIC_SPI_PARAMETERS (DDMA_PARA_DEST_DATA_BLK_SIZE(1) | \
270 + DDMA_PARA_SRC_DATA_BLK_SIZE(1) | \
271 + DDMA_PARA_DEST_WAIT_CYCLES(2) | \
272 + DDMA_PARA_SRC_WAIT_CYCLES(2))
273 +
274 +struct sun4i_dma_pchan {
275 + /* Register base of channel */
276 + void __iomem *base;
277 + /* vchan currently being serviced */
278 + struct sun4i_dma_vchan *vchan;
279 + /* Is this a dedicated pchan? */
280 + int is_dedicated;
281 +};
282 +
283 +struct sun4i_dma_vchan {
284 + struct virt_dma_chan vc;
285 + struct dma_slave_config cfg;
286 + struct sun4i_dma_pchan *pchan;
287 + struct sun4i_dma_promise *processing;
288 + struct sun4i_dma_contract *contract;
289 + u8 endpoint;
290 + int is_dedicated;
291 +};
292 +
293 +struct sun4i_dma_promise {
294 + u32 cfg;
295 + u32 para;
296 + dma_addr_t src;
297 + dma_addr_t dst;
298 + size_t len;
299 + struct list_head list;
300 +};
301 +
302 +/* A contract is a set of promises */
303 +struct sun4i_dma_contract {
304 + struct virt_dma_desc vd;
305 + struct list_head demands;
306 + struct list_head completed_demands;
307 + int is_cyclic;
308 +};
309 +
310 +struct sun4i_dma_dev {
311 + DECLARE_BITMAP(pchans_used, DMA_NR_MAX_CHANNELS);
312 + struct dma_device slave;
313 + struct sun4i_dma_pchan *pchans;
314 + struct sun4i_dma_vchan *vchans;
315 + void __iomem *base;
316 + struct clk *clk;
317 + int irq;
318 + spinlock_t lock;
319 +};
320 +
321 +static struct sun4i_dma_dev *to_sun4i_dma_dev(struct dma_device *dev)
322 +{
323 + return container_of(dev, struct sun4i_dma_dev, slave);
324 +}
325 +
326 +static struct sun4i_dma_vchan *to_sun4i_dma_vchan(struct dma_chan *chan)
327 +{
328 + return container_of(chan, struct sun4i_dma_vchan, vc.chan);
329 +}
330 +
331 +static struct sun4i_dma_contract *to_sun4i_dma_contract(struct virt_dma_desc *vd)
332 +{
333 + return container_of(vd, struct sun4i_dma_contract, vd);
334 +}
335 +
336 +static struct device *chan2dev(struct dma_chan *chan)
337 +{
338 + return &chan->dev->device;
339 +}
340 +
341 +static int convert_burst(u32 maxburst)
342 +{
343 + if (maxburst > 8)
344 + return -EINVAL;
345 +
346 + /* 1 -> 0, 4 -> 1, 8 -> 2 */
347 + return (maxburst >> 2);
348 +}
349 +
350 +static int convert_buswidth(enum dma_slave_buswidth addr_width)
351 +{
352 + if (addr_width > DMA_SLAVE_BUSWIDTH_4_BYTES)
353 + return -EINVAL;
354 +
355 + /* 8 (1 byte) -> 0, 16 (2 bytes) -> 1, 32 (4 bytes) -> 2 */
356 + return (addr_width >> 1);
357 +}
358 +
359 +static int choose_optimal_buswidth(dma_addr_t addr)
360 +{
361 + /* On 32 bit aligned addresses, we can use a 32 bit bus width */
362 + if (addr % 4 == 0)
363 + return DMA_SLAVE_BUSWIDTH_4_BYTES;
364 + /* On 16 bit aligned addresses, we can use a 16 bit bus width */
365 + else if (addr % 2 == 0)
366 + return DMA_SLAVE_BUSWIDTH_2_BYTES;
367 +
368 + /* Worst-case scenario, we need to do byte aligned reads */
369 + return DMA_SLAVE_BUSWIDTH_1_BYTE;
370 +}
371 +
372 +static void sun4i_dma_free_chan_resources(struct dma_chan *chan)
373 +{
374 + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
375 +
376 + vchan_free_chan_resources(&vchan->vc);
377 +}
378 +
379 +static struct sun4i_dma_pchan *find_and_use_pchan(struct sun4i_dma_dev *priv,
380 + struct sun4i_dma_vchan *vchan)
381 +{
382 + struct sun4i_dma_pchan *pchan = NULL, *pchans = priv->pchans;
383 + unsigned long flags;
384 + int i, max;
385 +
386 + /*
387 + * pchans 0-NDMA_NR_MAX_CHANNELS are normal, and
388 + * NDMA_NR_MAX_CHANNELS+ are dedicated ones
389 + */
390 + if (vchan->is_dedicated) {
391 + i = NDMA_NR_MAX_CHANNELS;
392 + max = DMA_NR_MAX_CHANNELS;
393 + } else {
394 + i = 0;
395 + max = NDMA_NR_MAX_CHANNELS;
396 + }
397 +
398 + spin_lock_irqsave(&priv->lock, flags);
399 + for_each_clear_bit_from(i, &priv->pchans_used, max) {
400 + pchan = &pchans[i];
401 + pchan->vchan = vchan;
402 + set_bit(i, priv->pchans_used);
403 + break;
404 + }
405 + spin_unlock_irqrestore(&priv->lock, flags);
406 +
407 + return pchan;
408 +}
409 +
410 +static void release_pchan(struct sun4i_dma_dev *priv,
411 + struct sun4i_dma_pchan *pchan)
412 +{
413 + unsigned long flags;
414 + int nr = pchan - priv->pchans;
415 +
416 + spin_lock_irqsave(&priv->lock, flags);
417 +
418 + pchan->vchan = NULL;
419 + clear_bit(nr, priv->pchans_used);
420 +
421 + spin_unlock_irqrestore(&priv->lock, flags);
422 +}
423 +
424 +static void configure_pchan(struct sun4i_dma_pchan *pchan,
425 + struct sun4i_dma_promise *d)
426 +{
427 + /*
428 + * Configure addresses and misc parameters depending on type
429 + * DDMA has an extra field with timing parameters
430 + */
431 + if (pchan->is_dedicated) {
432 + writel_relaxed(d->src, pchan->base + DDMA_SRC_ADDR_REG);
433 + writel_relaxed(d->dst, pchan->base + DDMA_DEST_ADDR_REG);
434 + writel_relaxed(d->len, pchan->base + DDMA_BYTE_COUNT_REG);
435 + writel_relaxed(d->para, pchan->base + DDMA_PARA_REG);
436 + writel_relaxed(d->cfg, pchan->base + DDMA_CFG_REG);
437 + } else {
438 + writel_relaxed(d->src, pchan->base + NDMA_SRC_ADDR_REG);
439 + writel_relaxed(d->dst, pchan->base + NDMA_DEST_ADDR_REG);
440 + writel_relaxed(d->len, pchan->base + NDMA_BYTE_COUNT_REG);
441 + writel_relaxed(d->cfg, pchan->base + NDMA_CFG_REG);
442 + }
443 +}
444 +
445 +static void set_pchan_interrupt(struct sun4i_dma_dev *priv,
446 + struct sun4i_dma_pchan *pchan,
447 + int half, int end)
448 +{
449 + u32 reg;
450 + int pchan_number = pchan - priv->pchans;
451 + unsigned long flags;
452 +
453 + spin_lock_irqsave(&priv->lock, flags);
454 +
455 + reg = readl_relaxed(priv->base + DMA_IRQ_ENABLE_REG);
456 +
457 + if (half)
458 + reg |= BIT(pchan_number * 2);
459 + else
460 + reg &= ~BIT(pchan_number * 2);
461 +
462 + if (end)
463 + reg |= BIT(pchan_number * 2 + 1);
464 + else
465 + reg &= ~BIT(pchan_number * 2 + 1);
466 +
467 + writel_relaxed(reg, priv->base + DMA_IRQ_ENABLE_REG);
468 +
469 + spin_unlock_irqrestore(&priv->lock, flags);
470 +}
471 +
472 +/**
473 + * Execute pending operations on a vchan
474 + *
475 + * When given a vchan, this function will try to acquire a suitable
476 + * pchan and, if successful, will configure it to fulfill a promise
477 + * from the next pending contract.
478 + *
479 + * This function must be called with &vchan->vc.lock held.
480 + */
481 +static int __execute_vchan_pending(struct sun4i_dma_dev *priv,
482 + struct sun4i_dma_vchan *vchan)
483 +{
484 + struct sun4i_dma_promise *promise = NULL;
485 + struct sun4i_dma_contract *contract = NULL;
486 + struct sun4i_dma_pchan *pchan;
487 + struct virt_dma_desc *vd;
488 + int ret;
489 +
490 + lockdep_assert_held(&vchan->vc.lock);
491 +
492 + /* We need a pchan to do anything, so secure one if available */
493 + pchan = find_and_use_pchan(priv, vchan);
494 + if (!pchan)
495 + return -EBUSY;
496 +
497 + /*
498 + * Channel endpoints must not be repeated, so if this vchan
499 + * has already submitted some work, we can't do anything else
500 + */
501 + if (vchan->processing) {
502 + dev_dbg(chan2dev(&vchan->vc.chan),
503 + "processing something to this endpoint already\n");
504 + ret = -EBUSY;
505 + goto release_pchan;
506 + }
507 +
508 + do {
509 + /* Figure out which contract we're working with today */
510 + vd = vchan_next_desc(&vchan->vc);
511 + if (!vd) {
512 + dev_dbg(chan2dev(&vchan->vc.chan),
513 + "No pending contract found");
514 + ret = 0;
515 + goto release_pchan;
516 + }
517 +
518 + contract = to_sun4i_dma_contract(vd);
519 + if (list_empty(&contract->demands)) {
520 + /* The contract has been completed so mark it as such */
521 + list_del(&contract->vd.node);
522 + vchan_cookie_complete(&contract->vd);
523 + dev_dbg(chan2dev(&vchan->vc.chan),
524 + "Empty contract found and marked complete");
525 + }
526 + } while (list_empty(&contract->demands));
527 +
528 + /* Now find out what we need to do */
529 + promise = list_first_entry(&contract->demands,
530 + struct sun4i_dma_promise, list);
531 + vchan->processing = promise;
532 +
533 + /* ... and make it reality */
534 + if (promise) {
535 + vchan->contract = contract;
536 + vchan->pchan = pchan;
537 + set_pchan_interrupt(priv, pchan, contract->is_cyclic, 1);
538 + configure_pchan(pchan, promise);
539 + }
540 +
541 + return 0;
542 +
543 +release_pchan:
544 + release_pchan(priv, pchan);
545 + return ret;
546 +}
547 +
548 +/**
549 + * Generate a promise, to be used in a normal DMA contract.
550 + *
551 + * A NDMA promise contains all the information required to program the
552 + * normal part of the DMA Engine and get data copied. A non-executed
553 + * promise will live in the demands list on a contract. Once it has been
554 + * completed, it will be moved to the completed demands list for later freeing.
555 + * All linked promises will be freed when the corresponding contract is freed
556 + */
557 +static struct sun4i_dma_promise *
558 +generate_ndma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
559 + size_t len, struct dma_slave_config *sconfig)
560 +{
561 + struct sun4i_dma_promise *promise;
562 + int ret;
563 +
564 + promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
565 + if (!promise)
566 + return NULL;
567 +
568 + promise->src = src;
569 + promise->dst = dest;
570 + promise->len = len;
571 + promise->cfg = NDMA_CFG_LOADING | NDMA_CFG_BYTE_COUNT_MODE_REMAIN;
572 +
573 + /* Use sensible default values if client is using undefined ones */
574 + if (sconfig->src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
575 + sconfig->src_addr_width = sconfig->dst_addr_width;
576 + if (sconfig->dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
577 + sconfig->dst_addr_width = sconfig->src_addr_width;
578 + if (sconfig->src_maxburst == 0)
579 + sconfig->src_maxburst = sconfig->dst_maxburst;
580 + if (sconfig->dst_maxburst == 0)
581 + sconfig->dst_maxburst = sconfig->src_maxburst;
582 +
583 + dev_dbg(chan2dev(chan),
584 + "src burst %d, dst burst %d, src buswidth %d, dst buswidth %d",
585 + sconfig->src_maxburst, sconfig->dst_maxburst,
586 + sconfig->src_addr_width, sconfig->dst_addr_width);
587 +
588 + /* Source burst */
589 + ret = convert_burst(sconfig->src_maxburst);
590 + if (IS_ERR_VALUE(ret))
591 + goto fail;
592 + promise->cfg |= NDMA_CFG_SRC_BURST_LENGTH(ret);
593 +
594 + /* Destination burst */
595 + ret = convert_burst(sconfig->dst_maxburst);
596 + if (IS_ERR_VALUE(ret))
597 + goto fail;
598 + promise->cfg |= NDMA_CFG_DEST_BURST_LENGTH(ret);
599 +
600 + /* Source bus width */
601 + ret = convert_buswidth(sconfig->src_addr_width);
602 + if (IS_ERR_VALUE(ret))
603 + goto fail;
604 + promise->cfg |= NDMA_CFG_SRC_DATA_WIDTH(ret);
605 +
606 + /* Destination bus width */
607 + ret = convert_buswidth(sconfig->dst_addr_width);
608 + if (IS_ERR_VALUE(ret))
609 + goto fail;
610 + promise->cfg |= NDMA_CFG_DEST_DATA_WIDTH(ret);
611 +
612 + return promise;
613 +
614 +fail:
615 + kfree(promise);
616 + return NULL;
617 +}
618 +
619 +/**
620 + * Generate a promise, to be used in a dedicated DMA contract.
621 + *
622 + * A DDMA promise contains all the information required to program the
623 + * Dedicated part of the DMA Engine and get data copied. A non-executed
624 + * promise will live in the demands list on a contract. Once it has been
625 + * completed, it will be moved to the completed demands list for later freeing.
626 + * All linked promises will be freed when the corresponding contract is freed
627 + */
628 +static struct sun4i_dma_promise *
629 +generate_ddma_promise(struct dma_chan *chan, dma_addr_t src, dma_addr_t dest,
630 + size_t len, struct dma_slave_config *sconfig)
631 +{
632 + struct sun4i_dma_promise *promise;
633 + int ret;
634 +
635 + promise = kzalloc(sizeof(*promise), GFP_NOWAIT);
636 + if (!promise)
637 + return NULL;
638 +
639 + promise->src = src;
640 + promise->dst = dest;
641 + promise->len = len;
642 + promise->cfg = DDMA_CFG_LOADING | DDMA_CFG_BYTE_COUNT_MODE_REMAIN;
643 +
644 + /* Source burst */
645 + ret = convert_burst(sconfig->src_maxburst);
646 + if (IS_ERR_VALUE(ret))
647 + goto fail;
648 + promise->cfg |= DDMA_CFG_SRC_BURST_LENGTH(ret);
649 +
650 + /* Destination burst */
651 + ret = convert_burst(sconfig->dst_maxburst);
652 + if (IS_ERR_VALUE(ret))
653 + goto fail;
654 + promise->cfg |= DDMA_CFG_DEST_BURST_LENGTH(ret);
655 +
656 + /* Source bus width */
657 + ret = convert_buswidth(sconfig->src_addr_width);
658 + if (IS_ERR_VALUE(ret))
659 + goto fail;
660 + promise->cfg |= DDMA_CFG_SRC_DATA_WIDTH(ret);
661 +
662 + /* Destination bus width */
663 + ret = convert_buswidth(sconfig->dst_addr_width);
664 + if (IS_ERR_VALUE(ret))
665 + goto fail;
666 + promise->cfg |= DDMA_CFG_DEST_DATA_WIDTH(ret);
667 +
668 + return promise;
669 +
670 +fail:
671 + kfree(promise);
672 + return NULL;
673 +}
674 +
675 +/**
676 + * Generate a contract
677 + *
678 + * Contracts function as DMA descriptors. As our hardware does not support
679 + * linked lists, we need to implement SG via software. We use a contract
680 + * to hold all the pieces of the request and process them serially one
681 + * after another. Each piece is represented as a promise.
682 + */
683 +static struct sun4i_dma_contract *generate_dma_contract(void)
684 +{
685 + struct sun4i_dma_contract *contract;
686 +
687 + contract = kzalloc(sizeof(*contract), GFP_NOWAIT);
688 + if (!contract)
689 + return NULL;
690 +
691 + INIT_LIST_HEAD(&contract->demands);
692 + INIT_LIST_HEAD(&contract->completed_demands);
693 +
694 + return contract;
695 +}
696 +
697 +/**
698 + * Get next promise on a cyclic transfer
699 + *
700 + * Cyclic contracts contain a series of promises which are executed on a
701 + * loop. This function returns the next promise from a cyclic contract,
702 + * so it can be programmed into the hardware.
703 + */
704 +static struct sun4i_dma_promise *
705 +get_next_cyclic_promise(struct sun4i_dma_contract *contract)
706 +{
707 + struct sun4i_dma_promise *promise;
708 +
709 + promise = list_first_entry_or_null(&contract->demands,
710 + struct sun4i_dma_promise, list);
711 + if (!promise) {
712 + list_splice_init(&contract->completed_demands,
713 + &contract->demands);
714 + promise = list_first_entry(&contract->demands,
715 + struct sun4i_dma_promise, list);
716 + }
717 +
718 + return promise;
719 +}
720 +
721 +/**
722 + * Free a contract and all its associated promises
723 + */
724 +static void sun4i_dma_free_contract(struct virt_dma_desc *vd)
725 +{
726 + struct sun4i_dma_contract *contract = to_sun4i_dma_contract(vd);
727 + struct sun4i_dma_promise *promise;
728 +
729 + /* Free all the demands and completed demands */
730 + list_for_each_entry(promise, &contract->demands, list)
731 + kfree(promise);
732 +
733 + list_for_each_entry(promise, &contract->completed_demands, list)
734 + kfree(promise);
735 +
736 + kfree(contract);
737 +}
738 +
739 +static struct dma_async_tx_descriptor *
740 +sun4i_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
741 + dma_addr_t src, size_t len, unsigned long flags)
742 +{
743 + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
744 + struct dma_slave_config *sconfig = &vchan->cfg;
745 + struct sun4i_dma_promise *promise;
746 + struct sun4i_dma_contract *contract;
747 +
748 + contract = generate_dma_contract();
749 + if (!contract)
750 + return NULL;
751 +
752 + /*
753 + * We can only do the copy to bus aligned addresses, so
754 + * choose the best one so we get decent performance. We also
755 + * maximize the burst size for this same reason.
756 + */
757 + sconfig->src_addr_width = choose_optimal_buswidth(src);
758 + sconfig->dst_addr_width = choose_optimal_buswidth(dest);
759 + sconfig->src_maxburst = 8;
760 + sconfig->dst_maxburst = 8;
761 +
762 + if (vchan->is_dedicated)
763 + promise = generate_ddma_promise(chan, src, dest, len, sconfig);
764 + else
765 + promise = generate_ndma_promise(chan, src, dest, len, sconfig);
766 +
767 + if (!promise) {
768 + kfree(contract);
769 + return NULL;
770 + }
771 +
772 + /* Configure memcpy mode */
773 + if (vchan->is_dedicated) {
774 + promise->cfg |= DDMA_CFG_SRC_DRQ_TYPE(DDMA_DRQ_TYPE_SDRAM) |
775 + DDMA_CFG_DEST_DRQ_TYPE(DDMA_DRQ_TYPE_SDRAM);
776 + } else {
777 + promise->cfg |= NDMA_CFG_SRC_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM) |
778 + NDMA_CFG_DEST_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM);
779 + }
780 +
781 + /* Fill the contract with our only promise */
782 + list_add_tail(&promise->list, &contract->demands);
783 +
784 + /* And add it to the vchan */
785 + return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
786 +}
787 +
788 +static struct dma_async_tx_descriptor *
789 +sun4i_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf, size_t len,
790 + size_t period_len, enum dma_transfer_direction dir,
791 + unsigned long flags)
792 +{
793 + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
794 + struct dma_slave_config *sconfig = &vchan->cfg;
795 + struct sun4i_dma_promise *promise;
796 + struct sun4i_dma_contract *contract;
797 + dma_addr_t src, dest;
798 + u32 endpoints;
799 + int nr_periods, offset, plength, i;
800 +
801 + if (!is_slave_direction(dir)) {
802 + dev_err(chan2dev(chan), "Invalid DMA direction\n");
803 + return NULL;
804 + }
805 +
806 + if (vchan->is_dedicated) {
807 + /*
808 + * As we are using this just for audio data, we need to use
809 + * normal DMA. There is nothing stopping us from supporting
810 + * dedicated DMA here as well, so if a client comes up and
811 + * requires it, it will be simple to implement it.
812 + */
813 + dev_err(chan2dev(chan),
814 + "Cyclic transfers are only supported on Normal DMA\n");
815 + return NULL;
816 + }
817 +
818 + contract = generate_dma_contract();
819 + if (!contract)
820 + return NULL;
821 +
822 + contract->is_cyclic = 1;
823 +
824 + /* Figure out the endpoints and the address we need */
825 + if (dir == DMA_MEM_TO_DEV) {
826 + src = buf;
827 + dest = sconfig->dst_addr;
828 + endpoints = NDMA_CFG_SRC_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM) |
829 + NDMA_CFG_DEST_DRQ_TYPE(vchan->endpoint) |
830 + NDMA_CFG_DEST_FIXED_ADDR;
831 + } else {
832 + src = sconfig->src_addr;
833 + dest = buf;
834 + endpoints = NDMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
835 + NDMA_CFG_SRC_FIXED_ADDR |
836 + NDMA_CFG_DEST_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM);
837 + }
838 +
839 + /*
840 + * We will be using half done interrupts to make two periods
841 + * out of a promise, so we need to program the DMA engine less
842 + * often
843 + */
844 + nr_periods = DIV_ROUND_UP(len / period_len, 2);
845 + for (i = 0; i < nr_periods; i++) {
846 + /* Calculate the offset in the buffer and the length needed */
847 + offset = i * period_len * 2;
848 + plength = min((len - offset), (period_len * 2));
849 + if (dir == DMA_MEM_TO_DEV)
850 + src = buf + offset;
851 + else
852 + dest = buf + offset;
853 +
854 + /* Make the promise */
855 + promise = generate_ndma_promise(chan, src, dest,
856 + plength, sconfig);
857 + if (!promise) {
858 + /* TODO: should we free everything? */
859 + return NULL;
860 + }
861 + promise->cfg |= endpoints;
862 +
863 + /* Then add it to the contract */
864 + list_add_tail(&promise->list, &contract->demands);
865 + }
866 +
867 + /* And add it to the vchan */
868 + return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
869 +}
870 +
871 +static struct dma_async_tx_descriptor *
872 +sun4i_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
873 + unsigned int sg_len, enum dma_transfer_direction dir,
874 + unsigned long flags, void *context)
875 +{
876 + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
877 + struct dma_slave_config *sconfig = &vchan->cfg;
878 + struct sun4i_dma_promise *promise;
879 + struct sun4i_dma_contract *contract;
880 + struct scatterlist *sg;
881 + dma_addr_t srcaddr, dstaddr;
882 + u32 endpoints, para;
883 + int i;
884 +
885 + if (!sgl)
886 + return NULL;
887 +
888 + if (!is_slave_direction(dir)) {
889 + dev_err(chan2dev(chan), "Invalid DMA direction\n");
890 + return NULL;
891 + }
892 +
893 + contract = generate_dma_contract();
894 + if (!contract)
895 + return NULL;
896 +
897 + /* Figure out endpoints */
898 + if (vchan->is_dedicated && dir == DMA_MEM_TO_DEV) {
899 + endpoints = DDMA_CFG_SRC_DRQ_TYPE(DDMA_DRQ_TYPE_SDRAM) |
900 + DDMA_CFG_SRC_ADDR_MODE(DDMA_ADDR_MODE_LINEAR) |
901 + DDMA_CFG_DEST_DRQ_TYPE(vchan->endpoint) |
902 + DDMA_CFG_DEST_ADDR_MODE(DDMA_ADDR_MODE_IO);
903 + } else if (!vchan->is_dedicated && dir == DMA_MEM_TO_DEV) {
904 + endpoints = NDMA_CFG_SRC_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM) |
905 + NDMA_CFG_DEST_DRQ_TYPE(vchan->endpoint) |
906 + NDMA_CFG_DEST_FIXED_ADDR;
907 + } else if (vchan->is_dedicated) {
908 + endpoints = DDMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
909 + DDMA_CFG_SRC_ADDR_MODE(DDMA_ADDR_MODE_IO) |
910 + DDMA_CFG_DEST_DRQ_TYPE(DDMA_DRQ_TYPE_SDRAM) |
911 + DDMA_CFG_DEST_ADDR_MODE(DDMA_ADDR_MODE_LINEAR);
912 + } else {
913 + endpoints = NDMA_CFG_SRC_DRQ_TYPE(vchan->endpoint) |
914 + NDMA_CFG_SRC_FIXED_ADDR |
915 + NDMA_CFG_DEST_DRQ_TYPE(NDMA_DRQ_TYPE_SDRAM);
916 + }
917 +
918 + for_each_sg(sgl, sg, sg_len, i) {
919 + /* Figure out addresses */
920 + if (dir == DMA_MEM_TO_DEV) {
921 + srcaddr = sg_dma_address(sg);
922 + dstaddr = sconfig->dst_addr;
923 + } else {
924 + srcaddr = sconfig->src_addr;
925 + dstaddr = sg_dma_address(sg);
926 + }
927 +
928 + /*
929 + * These are the magic DMA engine timings that keep SPI going.
930 + * I haven't seen any interface on DMAEngine to configure
931 + * timings, and so far they seem to work for everything we
932 + * support, so I've kept them here. I don't know if other
933 + * devices need different timings because, as usual, we only
934 + * have the "para" bitfield meanings, but no comment on what
935 + * the values should be when doing a certain operation :|
936 + */
937 + para = DDMA_MAGIC_SPI_PARAMETERS;
938 +
939 + /* And make a suitable promise */
940 + if (vchan->is_dedicated)
941 + promise = generate_ddma_promise(chan, srcaddr, dstaddr,
942 + sg_dma_len(sg), sconfig);
943 + else
944 + promise = generate_ndma_promise(chan, srcaddr, dstaddr,
945 + sg_dma_len(sg), sconfig);
946 +
947 + if (!promise)
948 + return NULL; /* TODO: should we free everything? */
949 +
950 + promise->cfg |= endpoints;
951 + promise->para = para;
952 +
953 + /* Then add it to the contract */
954 + list_add_tail(&promise->list, &contract->demands);
955 + }
956 +
957 + /*
958 + * Once we've got all the promises ready, add the contract
959 + * to the pending list on the vchan
960 + */
961 + return vchan_tx_prep(&vchan->vc, &contract->vd, flags);
962 +}
963 +
964 +static int sun4i_dma_terminate_all(struct dma_chan *chan)
965 +{
966 + struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
967 + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
968 + struct sun4i_dma_pchan *pchan = vchan->pchan;
969 + LIST_HEAD(head);
970 + unsigned long flags;
971 +
972 + spin_lock_irqsave(&vchan->vc.lock, flags);
973 + vchan_get_all_descriptors(&vchan->vc, &head);
974 + spin_unlock_irqrestore(&vchan->vc.lock, flags);
975 +
976 + /*
977 + * Clearing the configuration register will halt the pchan. Interrupts
978 + * may still trigger, so don't forget to disable them.
979 + */
980 + if (pchan) {
981 + if (pchan->is_dedicated)
982 + writel(0, pchan->base + DDMA_CFG_REG);
983 + else
984 + writel(0, pchan->base + NDMA_CFG_REG);
985 + set_pchan_interrupt(priv, pchan, 0, 0);
986 + release_pchan(priv, pchan);
987 + }
988 +
989 + spin_lock_irqsave(&vchan->vc.lock, flags);
990 + vchan_dma_desc_free_list(&vchan->vc, &head);
991 + /* Clear these so the vchan is usable again */
992 + vchan->processing = NULL;
993 + vchan->pchan = NULL;
994 + spin_unlock_irqrestore(&vchan->vc.lock, flags);
995 +
996 + return 0;
997 +}
998 +
999 +static int sun4i_dma_config(struct dma_chan *chan,
1000 + struct dma_slave_config *config)
1001 +{
1002 + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
1003 +
1004 + memcpy(&vchan->cfg, config, sizeof(*config));
1005 +
1006 + return 0;
1007 +}
1008 +
1009 +static struct dma_chan *sun4i_dma_of_xlate(struct of_phandle_args *dma_spec,
1010 + struct of_dma *ofdma)
1011 +{
1012 + struct sun4i_dma_dev *priv = ofdma->of_dma_data;
1013 + struct sun4i_dma_vchan *vchan;
1014 + struct dma_chan *chan;
1015 + u8 is_dedicated = dma_spec->args[0];
1016 + u8 endpoint = dma_spec->args[1];
1017 +
1018 + /* Check if type is Normal or Dedicated */
1019 + if (is_dedicated != 0 && is_dedicated != 1)
1020 + return NULL;
1021 +
1022 + /* Make sure the endpoint looks sane */
1023 + if ((is_dedicated && endpoint >= DDMA_DRQ_TYPE_LIMIT) ||
1024 + (!is_dedicated && endpoint >= NDMA_DRQ_TYPE_LIMIT))
1025 + return NULL;
1026 +
1027 + chan = dma_get_any_slave_channel(&priv->slave);
1028 + if (!chan)
1029 + return NULL;
1030 +
1031 + /* Assign the endpoint to the vchan */
1032 + vchan = to_sun4i_dma_vchan(chan);
1033 + vchan->is_dedicated = is_dedicated;
1034 + vchan->endpoint = endpoint;
1035 +
1036 + return chan;
1037 +}
1038 +
1039 +static enum dma_status sun4i_dma_tx_status(struct dma_chan *chan,
1040 + dma_cookie_t cookie,
1041 + struct dma_tx_state *state)
1042 +{
1043 + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
1044 + struct sun4i_dma_pchan *pchan = vchan->pchan;
1045 + struct sun4i_dma_contract *contract;
1046 + struct sun4i_dma_promise *promise;
1047 + struct virt_dma_desc *vd;
1048 + unsigned long flags;
1049 + enum dma_status ret;
1050 + size_t bytes = 0;
1051 +
1052 + ret = dma_cookie_status(chan, cookie, state);
1053 + if (ret == DMA_COMPLETE)
1054 + return ret;
1055 +
1056 + spin_lock_irqsave(&vchan->vc.lock, flags);
1057 + vd = vchan_find_desc(&vchan->vc, cookie);
1058 + if (!vd)
1059 + goto exit;
1060 + contract = to_sun4i_dma_contract(vd);
1061 +
1062 + list_for_each_entry(promise, &contract->demands, list)
1063 + bytes += promise->len;
1064 +
1065 + /*
1066 + * The hardware is configured to return the remaining byte
1067 + * quantity. If possible, replace the first listed element's
1068 + * full size with the actual remaining amount
1069 + */
1070 + promise = list_first_entry_or_null(&contract->demands,
1071 + struct sun4i_dma_promise, list);
1072 + if (promise && pchan) {
1073 + bytes -= promise->len;
1074 + if (pchan->is_dedicated)
1075 + bytes += readl(pchan->base + DDMA_BYTE_COUNT_REG);
1076 + else
1077 + bytes += readl(pchan->base + NDMA_BYTE_COUNT_REG);
1078 + }
1079 +
1080 +exit:
1081 +
1082 + dma_set_residue(state, bytes);
1083 + spin_unlock_irqrestore(&vchan->vc.lock, flags);
1084 +
1085 + return ret;
1086 +}
1087 +
1088 +static void sun4i_dma_issue_pending(struct dma_chan *chan)
1089 +{
1090 + struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
1091 + struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
1092 + unsigned long flags;
1093 +
1094 + spin_lock_irqsave(&vchan->vc.lock, flags);
1095 +
1096 + /*
1097 + * If there are pending transactions for this vchan, push one of
1098 + * them into the engine to get the ball rolling.
1099 + */
1100 + if (vchan_issue_pending(&vchan->vc))
1101 + __execute_vchan_pending(priv, vchan);
1102 +
1103 + spin_unlock_irqrestore(&vchan->vc.lock, flags);
1104 +}
1105 +
1106 +static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
1107 +{
1108 + struct sun4i_dma_dev *priv = dev_id;
1109 + struct sun4i_dma_pchan *pchans = priv->pchans, *pchan;
1110 + struct sun4i_dma_vchan *vchan;
1111 + struct sun4i_dma_contract *contract;
1112 + struct sun4i_dma_promise *promise;
1113 + unsigned long pendirq, irqs, disableirqs;
1114 + int bit, i, free_room, allow_mitigation = 1;
1115 +
1116 + pendirq = readl_relaxed(priv->base + DMA_IRQ_PENDING_STATUS_REG);
1117 +
1118 +handle_pending:
1119 +
1120 + disableirqs = 0;
1121 + free_room = 0;
1122 +
1123 + for_each_set_bit(bit, &pendirq, 32) {
1124 + pchan = &pchans[bit >> 1];
1125 + vchan = pchan->vchan;
1126 + if (!vchan) /* a terminated channel may still interrupt */
1127 + continue;
1128 + contract = vchan->contract;
1129 +
1130 + /*
1131 + * Disable the IRQ and free the pchan if it's an end
1132 + * interrupt (odd bit)
1133 + */
1134 + if (bit & 1) {
1135 + spin_lock(&vchan->vc.lock);
1136 +
1137 + /*
1138 + * Move the promise into the completed list now that
1139 + * we're done with it
1140 + */
1141 + list_del(&vchan->processing->list);
1142 + list_add_tail(&vchan->processing->list,
1143 + &contract->completed_demands);
1144 +
1145 + /*
1146 + * Cyclic DMA transfers are special:
1147 + * - There's always something we can dispatch
1148 + * - We need to run the callback
1149 + * - Latency is very important, as this is used by audio
1150 + * We therefore just cycle through the list and dispatch
1151 + * whatever we have here, reusing the pchan. There's
1152 + * no need to run the thread after this.
1153 + *
1154 + * For non-cyclic transfers we need to look around,
1155 + * so we can program some more work, or notify the
1156 + * client that their transfers have been completed.
1157 + */
1158 + if (contract->is_cyclic) {
1159 + promise = get_next_cyclic_promise(contract);
1160 + vchan->processing = promise;
1161 + configure_pchan(pchan, promise);
1162 + vchan_cyclic_callback(&contract->vd);
1163 + } else {
1164 + vchan->processing = NULL;
1165 + vchan->pchan = NULL;
1166 +
1167 + free_room = 1;
1168 + disableirqs |= BIT(bit);
1169 + release_pchan(priv, pchan);
1170 + }
1171 +
1172 + spin_unlock(&vchan->vc.lock);
1173 + } else {
1174 + /* Half done interrupt */
1175 + if (contract->is_cyclic)
1176 + vchan_cyclic_callback(&contract->vd);
1177 + else
1178 + disableirqs |= BIT(bit);
1179 + }
1180 + }
1181 +
1182 + /* Disable the IRQs for events we handled */
1183 + spin_lock(&priv->lock);
1184 + irqs = readl_relaxed(priv->base + DMA_IRQ_ENABLE_REG);
1185 + writel_relaxed(irqs & ~disableirqs, priv->base + DMA_IRQ_ENABLE_REG);
1186 + spin_unlock(&priv->lock);
1187 +
1188 + /* Writing 1 to the pending field will clear the pending interrupt */
1189 + writel_relaxed(pendirq, priv->base + DMA_IRQ_PENDING_STATUS_REG);
1190 +
1191 + /*
1192 + * If a pchan was freed, we may be able to schedule something else,
1193 + * so have a look around
1194 + */
1195 + if (free_room) {
1196 + for (i = 0; i < DMA_NR_MAX_VCHANS; i++) {
1197 + vchan = &priv->vchans[i];
1198 + spin_lock(&vchan->vc.lock);
1199 + __execute_vchan_pending(priv, vchan);
1200 + spin_unlock(&vchan->vc.lock);
1201 + }
1202 + }
1203 +
1204 + /*
1205 + * Handle newer interrupts if some showed up, but only do it once
1206 + * to avoid a too long a loop
1207 + */
1208 + if (allow_mitigation) {
1209 + pendirq = readl_relaxed(priv->base + DMA_IRQ_PENDING_STATUS_REG);
1210 + if (pendirq) {
1211 + allow_mitigation = 0;
1212 + goto handle_pending;
1213 + }
1214 + }
1215 +
1216 + return IRQ_HANDLED;
1217 +}
1218 +
1219 +static int sun4i_dma_probe(struct platform_device *pdev)
1220 +{
1221 + struct sun4i_dma_dev *priv;
1222 + struct resource *res;
1223 + int i, j, ret;
1224 +
1225 + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1226 + if (!priv)
1227 + return -ENOMEM;
1228 +
1229 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1230 + priv->base = devm_ioremap_resource(&pdev->dev, res);
1231 + if (IS_ERR(priv->base))
1232 + return PTR_ERR(priv->base);
1233 +
1234 + priv->irq = platform_get_irq(pdev, 0);
1235 + if (priv->irq < 0) {
1236 + dev_err(&pdev->dev, "Cannot claim IRQ\n");
1237 + return priv->irq;
1238 + }
1239 +
1240 + priv->clk = devm_clk_get(&pdev->dev, NULL);
1241 + if (IS_ERR(priv->clk)) {
1242 + dev_err(&pdev->dev, "No clock specified\n");
1243 + return PTR_ERR(priv->clk);
1244 + }
1245 +
1246 + platform_set_drvdata(pdev, priv);
1247 + spin_lock_init(&priv->lock);
1248 +
1249 + dma_cap_zero(priv->slave.cap_mask);
1250 + dma_cap_set(DMA_PRIVATE, priv->slave.cap_mask);
1251 + dma_cap_set(DMA_MEMCPY, priv->slave.cap_mask);
1252 + dma_cap_set(DMA_CYCLIC, priv->slave.cap_mask);
1253 + dma_cap_set(DMA_SLAVE, priv->slave.cap_mask);
1254 +
1255 + INIT_LIST_HEAD(&priv->slave.channels);
1256 + priv->slave.device_free_chan_resources = sun4i_dma_free_chan_resources;
1257 + priv->slave.device_tx_status = sun4i_dma_tx_status;
1258 + priv->slave.device_issue_pending = sun4i_dma_issue_pending;
1259 + priv->slave.device_prep_slave_sg = sun4i_dma_prep_slave_sg;
1260 + priv->slave.device_prep_dma_memcpy = sun4i_dma_prep_dma_memcpy;
1261 + priv->slave.device_prep_dma_cyclic = sun4i_dma_prep_dma_cyclic;
1262 + priv->slave.device_config = sun4i_dma_config;
1263 + priv->slave.device_terminate_all = sun4i_dma_terminate_all;
1264 + priv->slave.copy_align = DMA_SLAVE_BUSWIDTH_4_BYTES;
1265 + priv->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1266 + BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1267 + BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1268 + priv->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1269 + BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1270 + BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1271 + priv->slave.directions = BIT(DMA_DEV_TO_MEM) |
1272 + BIT(DMA_MEM_TO_DEV);
1273 + priv->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1274 +
1275 + priv->slave.dev = &pdev->dev;
1276 +
1277 + priv->pchans = devm_kcalloc(&pdev->dev, DMA_NR_MAX_CHANNELS,
1278 + sizeof(struct sun4i_dma_pchan), GFP_KERNEL);
1279 + priv->vchans = devm_kcalloc(&pdev->dev, DMA_NR_MAX_VCHANS,
1280 + sizeof(struct sun4i_dma_vchan), GFP_KERNEL);
1281 + if (!priv->vchans || !priv->pchans)
1282 + return -ENOMEM;
1283 +
1284 + /*
1285 + * [0..NDMA_NR_MAX_CHANNELS) are normal pchans, and
1286 + * [NDMA_NR_MAX_CHANNELS..DMA_NR_MAX_CHANNELS) are dedicated ones
1287 + */
1288 + for (i = 0; i < NDMA_NR_MAX_CHANNELS; i++)
1289 + priv->pchans[i].base = priv->base + NDMA_CHANNEL_REG_BASE(i);
1290 +
1291 + for (j = 0; i < DMA_NR_MAX_CHANNELS; i++, j++) {
1292 + priv->pchans[i].base = priv->base + DDMA_CHANNEL_REG_BASE(j);
1293 + priv->pchans[i].is_dedicated = 1;
1294 + }
1295 +
1296 + for (i = 0; i < DMA_NR_MAX_VCHANS; i++) {
1297 + struct sun4i_dma_vchan *vchan = &priv->vchans[i];
1298 +
1299 + spin_lock_init(&vchan->vc.lock);
1300 + vchan->vc.desc_free = sun4i_dma_free_contract;
1301 + vchan_init(&vchan->vc, &priv->slave);
1302 + }
1303 +
1304 + ret = clk_prepare_enable(priv->clk);
1305 + if (ret) {
1306 + dev_err(&pdev->dev, "Couldn't enable the clock\n");
1307 + return ret;
1308 + }
1309 +
1310 + /*
1311 + * Make sure the IRQs are all disabled and accounted for. The bootloader
1312 + * likes to leave these dirty
1313 + */
1314 + writel(0, priv->base + DMA_IRQ_ENABLE_REG);
1315 + writel(0xFFFFFFFF, priv->base + DMA_IRQ_PENDING_STATUS_REG);
1316 +
1317 + ret = devm_request_irq(&pdev->dev, priv->irq, sun4i_dma_interrupt,
1318 + 0, dev_name(&pdev->dev), priv);
1319 + if (ret) {
1320 + dev_err(&pdev->dev, "Cannot request IRQ\n");
1321 + goto err_clk_disable;
1322 + }
1323 +
1324 + ret = dma_async_device_register(&priv->slave);
1325 + if (ret) {
1326 + dev_warn(&pdev->dev, "Failed to register DMA engine device\n");
1327 + goto err_clk_disable;
1328 + }
1329 +
1330 + ret = of_dma_controller_register(pdev->dev.of_node, sun4i_dma_of_xlate,
1331 + priv);
1332 + if (ret) {
1333 + dev_err(&pdev->dev, "of_dma_controller_register failed\n");
1334 + goto err_dma_unregister;
1335 + }
1336 +
1337 + dev_dbg(&pdev->dev, "Successfully probed SUN4I_DMA\n");
1338 +
1339 + return 0;
1340 +
1341 +err_dma_unregister:
1342 + dma_async_device_unregister(&priv->slave);
1343 +err_clk_disable:
1344 + clk_disable_unprepare(priv->clk);
1345 + return ret;
1346 +}
1347 +
1348 +static int sun4i_dma_remove(struct platform_device *pdev)
1349 +{
1350 + struct sun4i_dma_dev *priv = platform_get_drvdata(pdev);
1351 +
1352 + /* Disable IRQ so no more work is scheduled */
1353 + disable_irq(priv->irq);
1354 +
1355 + of_dma_controller_free(pdev->dev.of_node);
1356 + dma_async_device_unregister(&priv->slave);
1357 +
1358 + clk_disable_unprepare(priv->clk);
1359 +
1360 + return 0;
1361 +}
1362 +
1363 +static struct of_device_id sun4i_dma_match[] = {
1364 + { .compatible = "allwinner,sun4i-a10-dma" },
1365 + { /* sentinel */ },
1366 +};
1367 +
1368 +static struct platform_driver sun4i_dma_driver = {
1369 + .probe = sun4i_dma_probe,
1370 + .remove = sun4i_dma_remove,
1371 + .driver = {
1372 + .name = "sun4i-dma",
1373 + .of_match_table = sun4i_dma_match,
1374 + },
1375 +};
1376 +
1377 +module_platform_driver(sun4i_dma_driver);
1378 +
1379 +MODULE_DESCRIPTION("Allwinner A10 Dedicated DMA Controller Driver");
1380 +MODULE_AUTHOR("Emilio López <emilio@elopez.com.ar>");
1381 +MODULE_LICENSE("GPL");