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