6dcba78ce5dc1f804da782e0ee16680132af5308
[openwrt.git] / target / linux / ipq806x / patches / 0045-dmaengine-add-Qualcomm-BAM-dma-driver.patch
1 From 23dd43da9c885789b3d5aceed3e401345f8e8106 Mon Sep 17 00:00:00 2001
2 From: Andy Gross <agross@codeaurora.org>
3 Date: Sat, 29 Mar 2014 18:53:16 +0530
4 Subject: [PATCH 045/182] dmaengine: add Qualcomm BAM dma driver
5
6 Add the DMA engine driver for the QCOM Bus Access Manager (BAM) DMA controller
7 found in the MSM 8x74 platforms.
8
9 Each BAM DMA device is associated with a specific on-chip peripheral.  Each
10 channel provides a uni-directional data transfer engine that is capable of
11 transferring data between the peripheral and system memory (System mode), or
12 between two peripherals (BAM2BAM).
13
14 The initial release of this driver only supports slave transfers between
15 peripherals and system memory.
16
17 Signed-off-by: Andy Gross <agross@codeaurora.org>
18 Tested-by: Stanimir Varbanov <svarbanov@mm-sol.com>
19 Signed-off-by: Vinod Koul <vinod.koul@intel.com>
20 ---
21  drivers/dma/Kconfig        |    9 +
22  drivers/dma/Makefile       |    2 +
23  drivers/dma/qcom_bam_dma.c | 1111 ++++++++++++++++++++++++++++++++++++++++++++
24  3 files changed, 1122 insertions(+)
25  create mode 100644 drivers/dma/qcom_bam_dma.c
26
27 diff --git a/drivers/dma/Kconfig b/drivers/dma/Kconfig
28 index 605b016..f87cef9 100644
29 --- a/drivers/dma/Kconfig
30 +++ b/drivers/dma/Kconfig
31 @@ -401,4 +401,13 @@ config DMATEST
32  config DMA_ENGINE_RAID
33         bool
34  
35 +config QCOM_BAM_DMA
36 +       tristate "QCOM BAM DMA support"
37 +       depends on ARCH_QCOM || (COMPILE_TEST && OF && ARM)
38 +       select DMA_ENGINE
39 +       select DMA_VIRTUAL_CHANNELS
40 +       ---help---
41 +         Enable support for the QCOM BAM DMA controller.  This controller
42 +         provides DMA capabilities for a variety of on-chip devices.
43 +
44  endif
45 diff --git a/drivers/dma/Makefile b/drivers/dma/Makefile
46 index a029d0f4..5150c82 100644
47 --- a/drivers/dma/Makefile
48 +++ b/drivers/dma/Makefile
49 @@ -44,3 +44,5 @@ obj-$(CONFIG_DMA_JZ4740) += dma-jz4740.o
50  obj-$(CONFIG_TI_CPPI41) += cppi41.o
51  obj-$(CONFIG_K3_DMA) += k3dma.o
52  obj-$(CONFIG_MOXART_DMA) += moxart-dma.o
53 +obj-$(CONFIG_FSL_EDMA) += fsl-edma.o
54 +obj-$(CONFIG_QCOM_BAM_DMA) += qcom_bam_dma.o
55 diff --git a/drivers/dma/qcom_bam_dma.c b/drivers/dma/qcom_bam_dma.c
56 new file mode 100644
57 index 0000000..82c9231
58 --- /dev/null
59 +++ b/drivers/dma/qcom_bam_dma.c
60 @@ -0,0 +1,1111 @@
61 +/*
62 + * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
63 + *
64 + * This program is free software; you can redistribute it and/or modify
65 + * it under the terms of the GNU General Public License version 2 and
66 + * only version 2 as published by the Free Software Foundation.
67 + *
68 + * This program is distributed in the hope that it will be useful,
69 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
70 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
71 + * GNU General Public License for more details.
72 + *
73 + */
74 +/*
75 + * QCOM BAM DMA engine driver
76 + *
77 + * QCOM BAM DMA blocks are distributed amongst a number of the on-chip
78 + * peripherals on the MSM 8x74.  The configuration of the channels are dependent
79 + * on the way they are hard wired to that specific peripheral.  The peripheral
80 + * device tree entries specify the configuration of each channel.
81 + *
82 + * The DMA controller requires the use of external memory for storage of the
83 + * hardware descriptors for each channel.  The descriptor FIFO is accessed as a
84 + * circular buffer and operations are managed according to the offset within the
85 + * FIFO.  After pipe/channel reset, all of the pipe registers and internal state
86 + * are back to defaults.
87 + *
88 + * During DMA operations, we write descriptors to the FIFO, being careful to
89 + * handle wrapping and then write the last FIFO offset to that channel's
90 + * P_EVNT_REG register to kick off the transaction.  The P_SW_OFSTS register
91 + * indicates the current FIFO offset that is being processed, so there is some
92 + * indication of where the hardware is currently working.
93 + */
94 +
95 +#include <linux/kernel.h>
96 +#include <linux/io.h>
97 +#include <linux/init.h>
98 +#include <linux/slab.h>
99 +#include <linux/module.h>
100 +#include <linux/interrupt.h>
101 +#include <linux/dma-mapping.h>
102 +#include <linux/scatterlist.h>
103 +#include <linux/device.h>
104 +#include <linux/platform_device.h>
105 +#include <linux/of.h>
106 +#include <linux/of_address.h>
107 +#include <linux/of_irq.h>
108 +#include <linux/of_dma.h>
109 +#include <linux/clk.h>
110 +#include <linux/dmaengine.h>
111 +
112 +#include "dmaengine.h"
113 +#include "virt-dma.h"
114 +
115 +struct bam_desc_hw {
116 +       u32 addr;               /* Buffer physical address */
117 +       u16 size;               /* Buffer size in bytes */
118 +       u16 flags;
119 +};
120 +
121 +#define DESC_FLAG_INT BIT(15)
122 +#define DESC_FLAG_EOT BIT(14)
123 +#define DESC_FLAG_EOB BIT(13)
124 +
125 +struct bam_async_desc {
126 +       struct virt_dma_desc vd;
127 +
128 +       u32 num_desc;
129 +       u32 xfer_len;
130 +       struct bam_desc_hw *curr_desc;
131 +
132 +       enum dma_transfer_direction dir;
133 +       size_t length;
134 +       struct bam_desc_hw desc[0];
135 +};
136 +
137 +#define BAM_CTRL                       0x0000
138 +#define BAM_REVISION                   0x0004
139 +#define BAM_SW_REVISION                        0x0080
140 +#define BAM_NUM_PIPES                  0x003C
141 +#define BAM_TIMER                      0x0040
142 +#define BAM_TIMER_CTRL                 0x0044
143 +#define BAM_DESC_CNT_TRSHLD            0x0008
144 +#define BAM_IRQ_SRCS                   0x000C
145 +#define BAM_IRQ_SRCS_MSK               0x0010
146 +#define BAM_IRQ_SRCS_UNMASKED          0x0030
147 +#define BAM_IRQ_STTS                   0x0014
148 +#define BAM_IRQ_CLR                    0x0018
149 +#define BAM_IRQ_EN                     0x001C
150 +#define BAM_CNFG_BITS                  0x007C
151 +#define BAM_IRQ_SRCS_EE(ee)            (0x0800 + ((ee) * 0x80))
152 +#define BAM_IRQ_SRCS_MSK_EE(ee)                (0x0804 + ((ee) * 0x80))
153 +#define BAM_P_CTRL(pipe)               (0x1000 + ((pipe) * 0x1000))
154 +#define BAM_P_RST(pipe)                        (0x1004 + ((pipe) * 0x1000))
155 +#define BAM_P_HALT(pipe)               (0x1008 + ((pipe) * 0x1000))
156 +#define BAM_P_IRQ_STTS(pipe)           (0x1010 + ((pipe) * 0x1000))
157 +#define BAM_P_IRQ_CLR(pipe)            (0x1014 + ((pipe) * 0x1000))
158 +#define BAM_P_IRQ_EN(pipe)             (0x1018 + ((pipe) * 0x1000))
159 +#define BAM_P_EVNT_DEST_ADDR(pipe)     (0x182C + ((pipe) * 0x1000))
160 +#define BAM_P_EVNT_REG(pipe)           (0x1818 + ((pipe) * 0x1000))
161 +#define BAM_P_SW_OFSTS(pipe)           (0x1800 + ((pipe) * 0x1000))
162 +#define BAM_P_DATA_FIFO_ADDR(pipe)     (0x1824 + ((pipe) * 0x1000))
163 +#define BAM_P_DESC_FIFO_ADDR(pipe)     (0x181C + ((pipe) * 0x1000))
164 +#define BAM_P_EVNT_TRSHLD(pipe)                (0x1828 + ((pipe) * 0x1000))
165 +#define BAM_P_FIFO_SIZES(pipe)         (0x1820 + ((pipe) * 0x1000))
166 +
167 +/* BAM CTRL */
168 +#define BAM_SW_RST                     BIT(0)
169 +#define BAM_EN                         BIT(1)
170 +#define BAM_EN_ACCUM                   BIT(4)
171 +#define BAM_TESTBUS_SEL_SHIFT          5
172 +#define BAM_TESTBUS_SEL_MASK           0x3F
173 +#define BAM_DESC_CACHE_SEL_SHIFT       13
174 +#define BAM_DESC_CACHE_SEL_MASK                0x3
175 +#define BAM_CACHED_DESC_STORE          BIT(15)
176 +#define IBC_DISABLE                    BIT(16)
177 +
178 +/* BAM REVISION */
179 +#define REVISION_SHIFT         0
180 +#define REVISION_MASK          0xFF
181 +#define NUM_EES_SHIFT          8
182 +#define NUM_EES_MASK           0xF
183 +#define CE_BUFFER_SIZE         BIT(13)
184 +#define AXI_ACTIVE             BIT(14)
185 +#define USE_VMIDMT             BIT(15)
186 +#define SECURED                        BIT(16)
187 +#define BAM_HAS_NO_BYPASS      BIT(17)
188 +#define HIGH_FREQUENCY_BAM     BIT(18)
189 +#define INACTIV_TMRS_EXST      BIT(19)
190 +#define NUM_INACTIV_TMRS       BIT(20)
191 +#define DESC_CACHE_DEPTH_SHIFT 21
192 +#define DESC_CACHE_DEPTH_1     (0 << DESC_CACHE_DEPTH_SHIFT)
193 +#define DESC_CACHE_DEPTH_2     (1 << DESC_CACHE_DEPTH_SHIFT)
194 +#define DESC_CACHE_DEPTH_3     (2 << DESC_CACHE_DEPTH_SHIFT)
195 +#define DESC_CACHE_DEPTH_4     (3 << DESC_CACHE_DEPTH_SHIFT)
196 +#define CMD_DESC_EN            BIT(23)
197 +#define INACTIV_TMR_BASE_SHIFT 24
198 +#define INACTIV_TMR_BASE_MASK  0xFF
199 +
200 +/* BAM NUM PIPES */
201 +#define BAM_NUM_PIPES_SHIFT            0
202 +#define BAM_NUM_PIPES_MASK             0xFF
203 +#define PERIPH_NON_PIPE_GRP_SHIFT      16
204 +#define PERIPH_NON_PIP_GRP_MASK                0xFF
205 +#define BAM_NON_PIPE_GRP_SHIFT         24
206 +#define BAM_NON_PIPE_GRP_MASK          0xFF
207 +
208 +/* BAM CNFG BITS */
209 +#define BAM_PIPE_CNFG          BIT(2)
210 +#define BAM_FULL_PIPE          BIT(11)
211 +#define BAM_NO_EXT_P_RST       BIT(12)
212 +#define BAM_IBC_DISABLE                BIT(13)
213 +#define BAM_SB_CLK_REQ         BIT(14)
214 +#define BAM_PSM_CSW_REQ                BIT(15)
215 +#define BAM_PSM_P_RES          BIT(16)
216 +#define BAM_AU_P_RES           BIT(17)
217 +#define BAM_SI_P_RES           BIT(18)
218 +#define BAM_WB_P_RES           BIT(19)
219 +#define BAM_WB_BLK_CSW         BIT(20)
220 +#define BAM_WB_CSW_ACK_IDL     BIT(21)
221 +#define BAM_WB_RETR_SVPNT      BIT(22)
222 +#define BAM_WB_DSC_AVL_P_RST   BIT(23)
223 +#define BAM_REG_P_EN           BIT(24)
224 +#define BAM_PSM_P_HD_DATA      BIT(25)
225 +#define BAM_AU_ACCUMED         BIT(26)
226 +#define BAM_CMD_ENABLE         BIT(27)
227 +
228 +#define BAM_CNFG_BITS_DEFAULT  (BAM_PIPE_CNFG |        \
229 +                                BAM_NO_EXT_P_RST |     \
230 +                                BAM_IBC_DISABLE |      \
231 +                                BAM_SB_CLK_REQ |       \
232 +                                BAM_PSM_CSW_REQ |      \
233 +                                BAM_PSM_P_RES |        \
234 +                                BAM_AU_P_RES |         \
235 +                                BAM_SI_P_RES |         \
236 +                                BAM_WB_P_RES |         \
237 +                                BAM_WB_BLK_CSW |       \
238 +                                BAM_WB_CSW_ACK_IDL |   \
239 +                                BAM_WB_RETR_SVPNT |    \
240 +                                BAM_WB_DSC_AVL_P_RST | \
241 +                                BAM_REG_P_EN |         \
242 +                                BAM_PSM_P_HD_DATA |    \
243 +                                BAM_AU_ACCUMED |       \
244 +                                BAM_CMD_ENABLE)
245 +
246 +/* PIPE CTRL */
247 +#define P_EN                   BIT(1)
248 +#define P_DIRECTION            BIT(3)
249 +#define P_SYS_STRM             BIT(4)
250 +#define P_SYS_MODE             BIT(5)
251 +#define P_AUTO_EOB             BIT(6)
252 +#define P_AUTO_EOB_SEL_SHIFT   7
253 +#define P_AUTO_EOB_SEL_512     (0 << P_AUTO_EOB_SEL_SHIFT)
254 +#define P_AUTO_EOB_SEL_256     (1 << P_AUTO_EOB_SEL_SHIFT)
255 +#define P_AUTO_EOB_SEL_128     (2 << P_AUTO_EOB_SEL_SHIFT)
256 +#define P_AUTO_EOB_SEL_64      (3 << P_AUTO_EOB_SEL_SHIFT)
257 +#define P_PREFETCH_LIMIT_SHIFT 9
258 +#define P_PREFETCH_LIMIT_32    (0 << P_PREFETCH_LIMIT_SHIFT)
259 +#define P_PREFETCH_LIMIT_16    (1 << P_PREFETCH_LIMIT_SHIFT)
260 +#define P_PREFETCH_LIMIT_4     (2 << P_PREFETCH_LIMIT_SHIFT)
261 +#define P_WRITE_NWD            BIT(11)
262 +#define P_LOCK_GROUP_SHIFT     16
263 +#define P_LOCK_GROUP_MASK      0x1F
264 +
265 +/* BAM_DESC_CNT_TRSHLD */
266 +#define CNT_TRSHLD             0xffff
267 +#define DEFAULT_CNT_THRSHLD    0x4
268 +
269 +/* BAM_IRQ_SRCS */
270 +#define BAM_IRQ                        BIT(31)
271 +#define P_IRQ                  0x7fffffff
272 +
273 +/* BAM_IRQ_SRCS_MSK */
274 +#define BAM_IRQ_MSK            BAM_IRQ
275 +#define P_IRQ_MSK              P_IRQ
276 +
277 +/* BAM_IRQ_STTS */
278 +#define BAM_TIMER_IRQ          BIT(4)
279 +#define BAM_EMPTY_IRQ          BIT(3)
280 +#define BAM_ERROR_IRQ          BIT(2)
281 +#define BAM_HRESP_ERR_IRQ      BIT(1)
282 +
283 +/* BAM_IRQ_CLR */
284 +#define BAM_TIMER_CLR          BIT(4)
285 +#define BAM_EMPTY_CLR          BIT(3)
286 +#define BAM_ERROR_CLR          BIT(2)
287 +#define BAM_HRESP_ERR_CLR      BIT(1)
288 +
289 +/* BAM_IRQ_EN */
290 +#define BAM_TIMER_EN           BIT(4)
291 +#define BAM_EMPTY_EN           BIT(3)
292 +#define BAM_ERROR_EN           BIT(2)
293 +#define BAM_HRESP_ERR_EN       BIT(1)
294 +
295 +/* BAM_P_IRQ_EN */
296 +#define P_PRCSD_DESC_EN                BIT(0)
297 +#define P_TIMER_EN             BIT(1)
298 +#define P_WAKE_EN              BIT(2)
299 +#define P_OUT_OF_DESC_EN       BIT(3)
300 +#define P_ERR_EN               BIT(4)
301 +#define P_TRNSFR_END_EN                BIT(5)
302 +#define P_DEFAULT_IRQS_EN      (P_PRCSD_DESC_EN | P_ERR_EN | P_TRNSFR_END_EN)
303 +
304 +/* BAM_P_SW_OFSTS */
305 +#define P_SW_OFSTS_MASK                0xffff
306 +
307 +#define BAM_DESC_FIFO_SIZE     SZ_32K
308 +#define MAX_DESCRIPTORS (BAM_DESC_FIFO_SIZE / sizeof(struct bam_desc_hw) - 1)
309 +#define BAM_MAX_DATA_SIZE      (SZ_32K - 8)
310 +
311 +struct bam_chan {
312 +       struct virt_dma_chan vc;
313 +
314 +       struct bam_device *bdev;
315 +
316 +       /* configuration from device tree */
317 +       u32 id;
318 +
319 +       struct bam_async_desc *curr_txd;        /* current running dma */
320 +
321 +       /* runtime configuration */
322 +       struct dma_slave_config slave;
323 +
324 +       /* fifo storage */
325 +       struct bam_desc_hw *fifo_virt;
326 +       dma_addr_t fifo_phys;
327 +
328 +       /* fifo markers */
329 +       unsigned short head;            /* start of active descriptor entries */
330 +       unsigned short tail;            /* end of active descriptor entries */
331 +
332 +       unsigned int initialized;       /* is the channel hw initialized? */
333 +       unsigned int paused;            /* is the channel paused? */
334 +       unsigned int reconfigure;       /* new slave config? */
335 +
336 +       struct list_head node;
337 +};
338 +
339 +static inline struct bam_chan *to_bam_chan(struct dma_chan *common)
340 +{
341 +       return container_of(common, struct bam_chan, vc.chan);
342 +}
343 +
344 +struct bam_device {
345 +       void __iomem *regs;
346 +       struct device *dev;
347 +       struct dma_device common;
348 +       struct device_dma_parameters dma_parms;
349 +       struct bam_chan *channels;
350 +       u32 num_channels;
351 +
352 +       /* execution environment ID, from DT */
353 +       u32 ee;
354 +
355 +       struct clk *bamclk;
356 +       int irq;
357 +
358 +       /* dma start transaction tasklet */
359 +       struct tasklet_struct task;
360 +};
361 +
362 +/**
363 + * bam_reset_channel - Reset individual BAM DMA channel
364 + * @bchan: bam channel
365 + *
366 + * This function resets a specific BAM channel
367 + */
368 +static void bam_reset_channel(struct bam_chan *bchan)
369 +{
370 +       struct bam_device *bdev = bchan->bdev;
371 +
372 +       lockdep_assert_held(&bchan->vc.lock);
373 +
374 +       /* reset channel */
375 +       writel_relaxed(1, bdev->regs + BAM_P_RST(bchan->id));
376 +       writel_relaxed(0, bdev->regs + BAM_P_RST(bchan->id));
377 +
378 +       /* don't allow cpu to reorder BAM register accesses done after this */
379 +       wmb();
380 +
381 +       /* make sure hw is initialized when channel is used the first time  */
382 +       bchan->initialized = 0;
383 +}
384 +
385 +/**
386 + * bam_chan_init_hw - Initialize channel hardware
387 + * @bchan: bam channel
388 + *
389 + * This function resets and initializes the BAM channel
390 + */
391 +static void bam_chan_init_hw(struct bam_chan *bchan,
392 +       enum dma_transfer_direction dir)
393 +{
394 +       struct bam_device *bdev = bchan->bdev;
395 +       u32 val;
396 +
397 +       /* Reset the channel to clear internal state of the FIFO */
398 +       bam_reset_channel(bchan);
399 +
400 +       /*
401 +        * write out 8 byte aligned address.  We have enough space for this
402 +        * because we allocated 1 more descriptor (8 bytes) than we can use
403 +        */
404 +       writel_relaxed(ALIGN(bchan->fifo_phys, sizeof(struct bam_desc_hw)),
405 +                       bdev->regs + BAM_P_DESC_FIFO_ADDR(bchan->id));
406 +       writel_relaxed(BAM_DESC_FIFO_SIZE, bdev->regs +
407 +                       BAM_P_FIFO_SIZES(bchan->id));
408 +
409 +       /* enable the per pipe interrupts, enable EOT, ERR, and INT irqs */
410 +       writel_relaxed(P_DEFAULT_IRQS_EN, bdev->regs + BAM_P_IRQ_EN(bchan->id));
411 +
412 +       /* unmask the specific pipe and EE combo */
413 +       val = readl_relaxed(bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
414 +       val |= BIT(bchan->id);
415 +       writel_relaxed(val, bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
416 +
417 +       /* don't allow cpu to reorder the channel enable done below */
418 +       wmb();
419 +
420 +       /* set fixed direction and mode, then enable channel */
421 +       val = P_EN | P_SYS_MODE;
422 +       if (dir == DMA_DEV_TO_MEM)
423 +               val |= P_DIRECTION;
424 +
425 +       writel_relaxed(val, bdev->regs + BAM_P_CTRL(bchan->id));
426 +
427 +       bchan->initialized = 1;
428 +
429 +       /* init FIFO pointers */
430 +       bchan->head = 0;
431 +       bchan->tail = 0;
432 +}
433 +
434 +/**
435 + * bam_alloc_chan - Allocate channel resources for DMA channel.
436 + * @chan: specified channel
437 + *
438 + * This function allocates the FIFO descriptor memory
439 + */
440 +static int bam_alloc_chan(struct dma_chan *chan)
441 +{
442 +       struct bam_chan *bchan = to_bam_chan(chan);
443 +       struct bam_device *bdev = bchan->bdev;
444 +
445 +       if (bchan->fifo_virt)
446 +               return 0;
447 +
448 +       /* allocate FIFO descriptor space, but only if necessary */
449 +       bchan->fifo_virt = dma_alloc_writecombine(bdev->dev, BAM_DESC_FIFO_SIZE,
450 +                               &bchan->fifo_phys, GFP_KERNEL);
451 +
452 +       if (!bchan->fifo_virt) {
453 +               dev_err(bdev->dev, "Failed to allocate desc fifo\n");
454 +               return -ENOMEM;
455 +       }
456 +
457 +       return 0;
458 +}
459 +
460 +/**
461 + * bam_free_chan - Frees dma resources associated with specific channel
462 + * @chan: specified channel
463 + *
464 + * Free the allocated fifo descriptor memory and channel resources
465 + *
466 + */
467 +static void bam_free_chan(struct dma_chan *chan)
468 +{
469 +       struct bam_chan *bchan = to_bam_chan(chan);
470 +       struct bam_device *bdev = bchan->bdev;
471 +       u32 val;
472 +       unsigned long flags;
473 +
474 +       vchan_free_chan_resources(to_virt_chan(chan));
475 +
476 +       if (bchan->curr_txd) {
477 +               dev_err(bchan->bdev->dev, "Cannot free busy channel\n");
478 +               return;
479 +       }
480 +
481 +       spin_lock_irqsave(&bchan->vc.lock, flags);
482 +       bam_reset_channel(bchan);
483 +       spin_unlock_irqrestore(&bchan->vc.lock, flags);
484 +
485 +       dma_free_writecombine(bdev->dev, BAM_DESC_FIFO_SIZE, bchan->fifo_virt,
486 +                               bchan->fifo_phys);
487 +       bchan->fifo_virt = NULL;
488 +
489 +       /* mask irq for pipe/channel */
490 +       val = readl_relaxed(bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
491 +       val &= ~BIT(bchan->id);
492 +       writel_relaxed(val, bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
493 +
494 +       /* disable irq */
495 +       writel_relaxed(0, bdev->regs + BAM_P_IRQ_EN(bchan->id));
496 +}
497 +
498 +/**
499 + * bam_slave_config - set slave configuration for channel
500 + * @chan: dma channel
501 + * @cfg: slave configuration
502 + *
503 + * Sets slave configuration for channel
504 + *
505 + */
506 +static void bam_slave_config(struct bam_chan *bchan,
507 +               struct dma_slave_config *cfg)
508 +{
509 +       memcpy(&bchan->slave, cfg, sizeof(*cfg));
510 +       bchan->reconfigure = 1;
511 +}
512 +
513 +/**
514 + * bam_prep_slave_sg - Prep slave sg transaction
515 + *
516 + * @chan: dma channel
517 + * @sgl: scatter gather list
518 + * @sg_len: length of sg
519 + * @direction: DMA transfer direction
520 + * @flags: DMA flags
521 + * @context: transfer context (unused)
522 + */
523 +static struct dma_async_tx_descriptor *bam_prep_slave_sg(struct dma_chan *chan,
524 +       struct scatterlist *sgl, unsigned int sg_len,
525 +       enum dma_transfer_direction direction, unsigned long flags,
526 +       void *context)
527 +{
528 +       struct bam_chan *bchan = to_bam_chan(chan);
529 +       struct bam_device *bdev = bchan->bdev;
530 +       struct bam_async_desc *async_desc;
531 +       struct scatterlist *sg;
532 +       u32 i;
533 +       struct bam_desc_hw *desc;
534 +       unsigned int num_alloc = 0;
535 +
536 +
537 +       if (!is_slave_direction(direction)) {
538 +               dev_err(bdev->dev, "invalid dma direction\n");
539 +               return NULL;
540 +       }
541 +
542 +       /* calculate number of required entries */
543 +       for_each_sg(sgl, sg, sg_len, i)
544 +               num_alloc += DIV_ROUND_UP(sg_dma_len(sg), BAM_MAX_DATA_SIZE);
545 +
546 +       /* allocate enough room to accomodate the number of entries */
547 +       async_desc = kzalloc(sizeof(*async_desc) +
548 +                       (num_alloc * sizeof(struct bam_desc_hw)), GFP_NOWAIT);
549 +
550 +       if (!async_desc)
551 +               goto err_out;
552 +
553 +       async_desc->num_desc = num_alloc;
554 +       async_desc->curr_desc = async_desc->desc;
555 +       async_desc->dir = direction;
556 +
557 +       /* fill in temporary descriptors */
558 +       desc = async_desc->desc;
559 +       for_each_sg(sgl, sg, sg_len, i) {
560 +               unsigned int remainder = sg_dma_len(sg);
561 +               unsigned int curr_offset = 0;
562 +
563 +               do {
564 +                       desc->addr = sg_dma_address(sg) + curr_offset;
565 +
566 +                       if (remainder > BAM_MAX_DATA_SIZE) {
567 +                               desc->size = BAM_MAX_DATA_SIZE;
568 +                               remainder -= BAM_MAX_DATA_SIZE;
569 +                               curr_offset += BAM_MAX_DATA_SIZE;
570 +                       } else {
571 +                               desc->size = remainder;
572 +                               remainder = 0;
573 +                       }
574 +
575 +                       async_desc->length += desc->size;
576 +                       desc++;
577 +               } while (remainder > 0);
578 +       }
579 +
580 +       return vchan_tx_prep(&bchan->vc, &async_desc->vd, flags);
581 +
582 +err_out:
583 +       kfree(async_desc);
584 +       return NULL;
585 +}
586 +
587 +/**
588 + * bam_dma_terminate_all - terminate all transactions on a channel
589 + * @bchan: bam dma channel
590 + *
591 + * Dequeues and frees all transactions
592 + * No callbacks are done
593 + *
594 + */
595 +static void bam_dma_terminate_all(struct bam_chan *bchan)
596 +{
597 +       unsigned long flag;
598 +       LIST_HEAD(head);
599 +
600 +       /* remove all transactions, including active transaction */
601 +       spin_lock_irqsave(&bchan->vc.lock, flag);
602 +       if (bchan->curr_txd) {
603 +               list_add(&bchan->curr_txd->vd.node, &bchan->vc.desc_issued);
604 +               bchan->curr_txd = NULL;
605 +       }
606 +
607 +       vchan_get_all_descriptors(&bchan->vc, &head);
608 +       spin_unlock_irqrestore(&bchan->vc.lock, flag);
609 +
610 +       vchan_dma_desc_free_list(&bchan->vc, &head);
611 +}
612 +
613 +/**
614 + * bam_control - DMA device control
615 + * @chan: dma channel
616 + * @cmd: control cmd
617 + * @arg: cmd argument
618 + *
619 + * Perform DMA control command
620 + *
621 + */
622 +static int bam_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
623 +       unsigned long arg)
624 +{
625 +       struct bam_chan *bchan = to_bam_chan(chan);
626 +       struct bam_device *bdev = bchan->bdev;
627 +       int ret = 0;
628 +       unsigned long flag;
629 +
630 +       switch (cmd) {
631 +       case DMA_PAUSE:
632 +               spin_lock_irqsave(&bchan->vc.lock, flag);
633 +               writel_relaxed(1, bdev->regs + BAM_P_HALT(bchan->id));
634 +               bchan->paused = 1;
635 +               spin_unlock_irqrestore(&bchan->vc.lock, flag);
636 +               break;
637 +
638 +       case DMA_RESUME:
639 +               spin_lock_irqsave(&bchan->vc.lock, flag);
640 +               writel_relaxed(0, bdev->regs + BAM_P_HALT(bchan->id));
641 +               bchan->paused = 0;
642 +               spin_unlock_irqrestore(&bchan->vc.lock, flag);
643 +               break;
644 +
645 +       case DMA_TERMINATE_ALL:
646 +               bam_dma_terminate_all(bchan);
647 +               break;
648 +
649 +       case DMA_SLAVE_CONFIG:
650 +               spin_lock_irqsave(&bchan->vc.lock, flag);
651 +               bam_slave_config(bchan, (struct dma_slave_config *)arg);
652 +               spin_unlock_irqrestore(&bchan->vc.lock, flag);
653 +               break;
654 +
655 +       default:
656 +               ret = -ENXIO;
657 +               break;
658 +       }
659 +
660 +       return ret;
661 +}
662 +
663 +/**
664 + * process_channel_irqs - processes the channel interrupts
665 + * @bdev: bam controller
666 + *
667 + * This function processes the channel interrupts
668 + *
669 + */
670 +static u32 process_channel_irqs(struct bam_device *bdev)
671 +{
672 +       u32 i, srcs, pipe_stts;
673 +       unsigned long flags;
674 +       struct bam_async_desc *async_desc;
675 +
676 +       srcs = readl_relaxed(bdev->regs + BAM_IRQ_SRCS_EE(bdev->ee));
677 +
678 +       /* return early if no pipe/channel interrupts are present */
679 +       if (!(srcs & P_IRQ))
680 +               return srcs;
681 +
682 +       for (i = 0; i < bdev->num_channels; i++) {
683 +               struct bam_chan *bchan = &bdev->channels[i];
684 +
685 +               if (!(srcs & BIT(i)))
686 +                       continue;
687 +
688 +               /* clear pipe irq */
689 +               pipe_stts = readl_relaxed(bdev->regs +
690 +                       BAM_P_IRQ_STTS(i));
691 +
692 +               writel_relaxed(pipe_stts, bdev->regs +
693 +                               BAM_P_IRQ_CLR(i));
694 +
695 +               spin_lock_irqsave(&bchan->vc.lock, flags);
696 +               async_desc = bchan->curr_txd;
697 +
698 +               if (async_desc) {
699 +                       async_desc->num_desc -= async_desc->xfer_len;
700 +                       async_desc->curr_desc += async_desc->xfer_len;
701 +                       bchan->curr_txd = NULL;
702 +
703 +                       /* manage FIFO */
704 +                       bchan->head += async_desc->xfer_len;
705 +                       bchan->head %= MAX_DESCRIPTORS;
706 +
707 +                       /*
708 +                        * if complete, process cookie.  Otherwise
709 +                        * push back to front of desc_issued so that
710 +                        * it gets restarted by the tasklet
711 +                        */
712 +                       if (!async_desc->num_desc)
713 +                               vchan_cookie_complete(&async_desc->vd);
714 +                       else
715 +                               list_add(&async_desc->vd.node,
716 +                                       &bchan->vc.desc_issued);
717 +               }
718 +
719 +               spin_unlock_irqrestore(&bchan->vc.lock, flags);
720 +       }
721 +
722 +       return srcs;
723 +}
724 +
725 +/**
726 + * bam_dma_irq - irq handler for bam controller
727 + * @irq: IRQ of interrupt
728 + * @data: callback data
729 + *
730 + * IRQ handler for the bam controller
731 + */
732 +static irqreturn_t bam_dma_irq(int irq, void *data)
733 +{
734 +       struct bam_device *bdev = data;
735 +       u32 clr_mask = 0, srcs = 0;
736 +
737 +       srcs |= process_channel_irqs(bdev);
738 +
739 +       /* kick off tasklet to start next dma transfer */
740 +       if (srcs & P_IRQ)
741 +               tasklet_schedule(&bdev->task);
742 +
743 +       if (srcs & BAM_IRQ)
744 +               clr_mask = readl_relaxed(bdev->regs + BAM_IRQ_STTS);
745 +
746 +       /* don't allow reorder of the various accesses to the BAM registers */
747 +       mb();
748 +
749 +       writel_relaxed(clr_mask, bdev->regs + BAM_IRQ_CLR);
750 +
751 +       return IRQ_HANDLED;
752 +}
753 +
754 +/**
755 + * bam_tx_status - returns status of transaction
756 + * @chan: dma channel
757 + * @cookie: transaction cookie
758 + * @txstate: DMA transaction state
759 + *
760 + * Return status of dma transaction
761 + */
762 +static enum dma_status bam_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
763 +               struct dma_tx_state *txstate)
764 +{
765 +       struct bam_chan *bchan = to_bam_chan(chan);
766 +       struct virt_dma_desc *vd;
767 +       int ret;
768 +       size_t residue = 0;
769 +       unsigned int i;
770 +       unsigned long flags;
771 +
772 +       ret = dma_cookie_status(chan, cookie, txstate);
773 +       if (ret == DMA_COMPLETE)
774 +               return ret;
775 +
776 +       if (!txstate)
777 +               return bchan->paused ? DMA_PAUSED : ret;
778 +
779 +       spin_lock_irqsave(&bchan->vc.lock, flags);
780 +       vd = vchan_find_desc(&bchan->vc, cookie);
781 +       if (vd)
782 +               residue = container_of(vd, struct bam_async_desc, vd)->length;
783 +       else if (bchan->curr_txd && bchan->curr_txd->vd.tx.cookie == cookie)
784 +               for (i = 0; i < bchan->curr_txd->num_desc; i++)
785 +                       residue += bchan->curr_txd->curr_desc[i].size;
786 +
787 +       spin_unlock_irqrestore(&bchan->vc.lock, flags);
788 +
789 +       dma_set_residue(txstate, residue);
790 +
791 +       if (ret == DMA_IN_PROGRESS && bchan->paused)
792 +               ret = DMA_PAUSED;
793 +
794 +       return ret;
795 +}
796 +
797 +/**
798 + * bam_apply_new_config
799 + * @bchan: bam dma channel
800 + * @dir: DMA direction
801 + */
802 +static void bam_apply_new_config(struct bam_chan *bchan,
803 +       enum dma_transfer_direction dir)
804 +{
805 +       struct bam_device *bdev = bchan->bdev;
806 +       u32 maxburst;
807 +
808 +       if (dir == DMA_DEV_TO_MEM)
809 +               maxburst = bchan->slave.src_maxburst;
810 +       else
811 +               maxburst = bchan->slave.dst_maxburst;
812 +
813 +       writel_relaxed(maxburst, bdev->regs + BAM_DESC_CNT_TRSHLD);
814 +
815 +       bchan->reconfigure = 0;
816 +}
817 +
818 +/**
819 + * bam_start_dma - start next transaction
820 + * @bchan - bam dma channel
821 + */
822 +static void bam_start_dma(struct bam_chan *bchan)
823 +{
824 +       struct virt_dma_desc *vd = vchan_next_desc(&bchan->vc);
825 +       struct bam_device *bdev = bchan->bdev;
826 +       struct bam_async_desc *async_desc;
827 +       struct bam_desc_hw *desc;
828 +       struct bam_desc_hw *fifo = PTR_ALIGN(bchan->fifo_virt,
829 +                                       sizeof(struct bam_desc_hw));
830 +
831 +       lockdep_assert_held(&bchan->vc.lock);
832 +
833 +       if (!vd)
834 +               return;
835 +
836 +       list_del(&vd->node);
837 +
838 +       async_desc = container_of(vd, struct bam_async_desc, vd);
839 +       bchan->curr_txd = async_desc;
840 +
841 +       /* on first use, initialize the channel hardware */
842 +       if (!bchan->initialized)
843 +               bam_chan_init_hw(bchan, async_desc->dir);
844 +
845 +       /* apply new slave config changes, if necessary */
846 +       if (bchan->reconfigure)
847 +               bam_apply_new_config(bchan, async_desc->dir);
848 +
849 +       desc = bchan->curr_txd->curr_desc;
850 +
851 +       if (async_desc->num_desc > MAX_DESCRIPTORS)
852 +               async_desc->xfer_len = MAX_DESCRIPTORS;
853 +       else
854 +               async_desc->xfer_len = async_desc->num_desc;
855 +
856 +       /* set INT on last descriptor */
857 +       desc[async_desc->xfer_len - 1].flags |= DESC_FLAG_INT;
858 +
859 +       if (bchan->tail + async_desc->xfer_len > MAX_DESCRIPTORS) {
860 +               u32 partial = MAX_DESCRIPTORS - bchan->tail;
861 +
862 +               memcpy(&fifo[bchan->tail], desc,
863 +                               partial * sizeof(struct bam_desc_hw));
864 +               memcpy(fifo, &desc[partial], (async_desc->xfer_len - partial) *
865 +                               sizeof(struct bam_desc_hw));
866 +       } else {
867 +               memcpy(&fifo[bchan->tail], desc,
868 +                       async_desc->xfer_len * sizeof(struct bam_desc_hw));
869 +       }
870 +
871 +       bchan->tail += async_desc->xfer_len;
872 +       bchan->tail %= MAX_DESCRIPTORS;
873 +
874 +       /* ensure descriptor writes and dma start not reordered */
875 +       wmb();
876 +       writel_relaxed(bchan->tail * sizeof(struct bam_desc_hw),
877 +                       bdev->regs + BAM_P_EVNT_REG(bchan->id));
878 +}
879 +
880 +/**
881 + * dma_tasklet - DMA IRQ tasklet
882 + * @data: tasklet argument (bam controller structure)
883 + *
884 + * Sets up next DMA operation and then processes all completed transactions
885 + */
886 +static void dma_tasklet(unsigned long data)
887 +{
888 +       struct bam_device *bdev = (struct bam_device *)data;
889 +       struct bam_chan *bchan;
890 +       unsigned long flags;
891 +       unsigned int i;
892 +
893 +       /* go through the channels and kick off transactions */
894 +       for (i = 0; i < bdev->num_channels; i++) {
895 +               bchan = &bdev->channels[i];
896 +               spin_lock_irqsave(&bchan->vc.lock, flags);
897 +
898 +               if (!list_empty(&bchan->vc.desc_issued) && !bchan->curr_txd)
899 +                       bam_start_dma(bchan);
900 +               spin_unlock_irqrestore(&bchan->vc.lock, flags);
901 +       }
902 +}
903 +
904 +/**
905 + * bam_issue_pending - starts pending transactions
906 + * @chan: dma channel
907 + *
908 + * Calls tasklet directly which in turn starts any pending transactions
909 + */
910 +static void bam_issue_pending(struct dma_chan *chan)
911 +{
912 +       struct bam_chan *bchan = to_bam_chan(chan);
913 +       unsigned long flags;
914 +
915 +       spin_lock_irqsave(&bchan->vc.lock, flags);
916 +
917 +       /* if work pending and idle, start a transaction */
918 +       if (vchan_issue_pending(&bchan->vc) && !bchan->curr_txd)
919 +               bam_start_dma(bchan);
920 +
921 +       spin_unlock_irqrestore(&bchan->vc.lock, flags);
922 +}
923 +
924 +/**
925 + * bam_dma_free_desc - free descriptor memory
926 + * @vd: virtual descriptor
927 + *
928 + */
929 +static void bam_dma_free_desc(struct virt_dma_desc *vd)
930 +{
931 +       struct bam_async_desc *async_desc = container_of(vd,
932 +                       struct bam_async_desc, vd);
933 +
934 +       kfree(async_desc);
935 +}
936 +
937 +static struct dma_chan *bam_dma_xlate(struct of_phandle_args *dma_spec,
938 +               struct of_dma *of)
939 +{
940 +       struct bam_device *bdev = container_of(of->of_dma_data,
941 +                                       struct bam_device, common);
942 +       unsigned int request;
943 +
944 +       if (dma_spec->args_count != 1)
945 +               return NULL;
946 +
947 +       request = dma_spec->args[0];
948 +       if (request >= bdev->num_channels)
949 +               return NULL;
950 +
951 +       return dma_get_slave_channel(&(bdev->channels[request].vc.chan));
952 +}
953 +
954 +/**
955 + * bam_init
956 + * @bdev: bam device
957 + *
958 + * Initialization helper for global bam registers
959 + */
960 +static int bam_init(struct bam_device *bdev)
961 +{
962 +       u32 val;
963 +
964 +       /* read revision and configuration information */
965 +       val = readl_relaxed(bdev->regs + BAM_REVISION) >> NUM_EES_SHIFT;
966 +       val &= NUM_EES_MASK;
967 +
968 +       /* check that configured EE is within range */
969 +       if (bdev->ee >= val)
970 +               return -EINVAL;
971 +
972 +       val = readl_relaxed(bdev->regs + BAM_NUM_PIPES);
973 +       bdev->num_channels = val & BAM_NUM_PIPES_MASK;
974 +
975 +       /* s/w reset bam */
976 +       /* after reset all pipes are disabled and idle */
977 +       val = readl_relaxed(bdev->regs + BAM_CTRL);
978 +       val |= BAM_SW_RST;
979 +       writel_relaxed(val, bdev->regs + BAM_CTRL);
980 +       val &= ~BAM_SW_RST;
981 +       writel_relaxed(val, bdev->regs + BAM_CTRL);
982 +
983 +       /* make sure previous stores are visible before enabling BAM */
984 +       wmb();
985 +
986 +       /* enable bam */
987 +       val |= BAM_EN;
988 +       writel_relaxed(val, bdev->regs + BAM_CTRL);
989 +
990 +       /* set descriptor threshhold, start with 4 bytes */
991 +       writel_relaxed(DEFAULT_CNT_THRSHLD, bdev->regs + BAM_DESC_CNT_TRSHLD);
992 +
993 +       /* Enable default set of h/w workarounds, ie all except BAM_FULL_PIPE */
994 +       writel_relaxed(BAM_CNFG_BITS_DEFAULT, bdev->regs + BAM_CNFG_BITS);
995 +
996 +       /* enable irqs for errors */
997 +       writel_relaxed(BAM_ERROR_EN | BAM_HRESP_ERR_EN,
998 +                               bdev->regs + BAM_IRQ_EN);
999 +
1000 +       /* unmask global bam interrupt */
1001 +       writel_relaxed(BAM_IRQ_MSK, bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
1002 +
1003 +       return 0;
1004 +}
1005 +
1006 +static void bam_channel_init(struct bam_device *bdev, struct bam_chan *bchan,
1007 +       u32 index)
1008 +{
1009 +       bchan->id = index;
1010 +       bchan->bdev = bdev;
1011 +
1012 +       vchan_init(&bchan->vc, &bdev->common);
1013 +       bchan->vc.desc_free = bam_dma_free_desc;
1014 +}
1015 +
1016 +static int bam_dma_probe(struct platform_device *pdev)
1017 +{
1018 +       struct bam_device *bdev;
1019 +       struct resource *iores;
1020 +       int ret, i;
1021 +
1022 +       bdev = devm_kzalloc(&pdev->dev, sizeof(*bdev), GFP_KERNEL);
1023 +       if (!bdev)
1024 +               return -ENOMEM;
1025 +
1026 +       bdev->dev = &pdev->dev;
1027 +
1028 +       iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1029 +       bdev->regs = devm_ioremap_resource(&pdev->dev, iores);
1030 +       if (IS_ERR(bdev->regs))
1031 +               return PTR_ERR(bdev->regs);
1032 +
1033 +       bdev->irq = platform_get_irq(pdev, 0);
1034 +       if (bdev->irq < 0)
1035 +               return bdev->irq;
1036 +
1037 +       ret = of_property_read_u32(pdev->dev.of_node, "qcom,ee", &bdev->ee);
1038 +       if (ret) {
1039 +               dev_err(bdev->dev, "Execution environment unspecified\n");
1040 +               return ret;
1041 +       }
1042 +
1043 +       bdev->bamclk = devm_clk_get(bdev->dev, "bam_clk");
1044 +       if (IS_ERR(bdev->bamclk))
1045 +               return PTR_ERR(bdev->bamclk);
1046 +
1047 +       ret = clk_prepare_enable(bdev->bamclk);
1048 +       if (ret) {
1049 +               dev_err(bdev->dev, "failed to prepare/enable clock\n");
1050 +               return ret;
1051 +       }
1052 +
1053 +       ret = bam_init(bdev);
1054 +       if (ret)
1055 +               goto err_disable_clk;
1056 +
1057 +       tasklet_init(&bdev->task, dma_tasklet, (unsigned long)bdev);
1058 +
1059 +       bdev->channels = devm_kcalloc(bdev->dev, bdev->num_channels,
1060 +                               sizeof(*bdev->channels), GFP_KERNEL);
1061 +
1062 +       if (!bdev->channels) {
1063 +               ret = -ENOMEM;
1064 +               goto err_disable_clk;
1065 +       }
1066 +
1067 +       /* allocate and initialize channels */
1068 +       INIT_LIST_HEAD(&bdev->common.channels);
1069 +
1070 +       for (i = 0; i < bdev->num_channels; i++)
1071 +               bam_channel_init(bdev, &bdev->channels[i], i);
1072 +
1073 +       ret = devm_request_irq(bdev->dev, bdev->irq, bam_dma_irq,
1074 +                       IRQF_TRIGGER_HIGH, "bam_dma", bdev);
1075 +       if (ret)
1076 +               goto err_disable_clk;
1077 +
1078 +       /* set max dma segment size */
1079 +       bdev->common.dev = bdev->dev;
1080 +       bdev->common.dev->dma_parms = &bdev->dma_parms;
1081 +       ret = dma_set_max_seg_size(bdev->common.dev, BAM_MAX_DATA_SIZE);
1082 +       if (ret) {
1083 +               dev_err(bdev->dev, "cannot set maximum segment size\n");
1084 +               goto err_disable_clk;
1085 +       }
1086 +
1087 +       platform_set_drvdata(pdev, bdev);
1088 +
1089 +       /* set capabilities */
1090 +       dma_cap_zero(bdev->common.cap_mask);
1091 +       dma_cap_set(DMA_SLAVE, bdev->common.cap_mask);
1092 +
1093 +       /* initialize dmaengine apis */
1094 +       bdev->common.device_alloc_chan_resources = bam_alloc_chan;
1095 +       bdev->common.device_free_chan_resources = bam_free_chan;
1096 +       bdev->common.device_prep_slave_sg = bam_prep_slave_sg;
1097 +       bdev->common.device_control = bam_control;
1098 +       bdev->common.device_issue_pending = bam_issue_pending;
1099 +       bdev->common.device_tx_status = bam_tx_status;
1100 +       bdev->common.dev = bdev->dev;
1101 +
1102 +       ret = dma_async_device_register(&bdev->common);
1103 +       if (ret) {
1104 +               dev_err(bdev->dev, "failed to register dma async device\n");
1105 +               goto err_disable_clk;
1106 +       }
1107 +
1108 +       ret = of_dma_controller_register(pdev->dev.of_node, bam_dma_xlate,
1109 +                                       &bdev->common);
1110 +       if (ret)
1111 +               goto err_unregister_dma;
1112 +
1113 +       return 0;
1114 +
1115 +err_unregister_dma:
1116 +       dma_async_device_unregister(&bdev->common);
1117 +err_disable_clk:
1118 +       clk_disable_unprepare(bdev->bamclk);
1119 +       return ret;
1120 +}
1121 +
1122 +static int bam_dma_remove(struct platform_device *pdev)
1123 +{
1124 +       struct bam_device *bdev = platform_get_drvdata(pdev);
1125 +       u32 i;
1126 +
1127 +       of_dma_controller_free(pdev->dev.of_node);
1128 +       dma_async_device_unregister(&bdev->common);
1129 +
1130 +       /* mask all interrupts for this execution environment */
1131 +       writel_relaxed(0, bdev->regs + BAM_IRQ_SRCS_MSK_EE(bdev->ee));
1132 +
1133 +       devm_free_irq(bdev->dev, bdev->irq, bdev);
1134 +
1135 +       for (i = 0; i < bdev->num_channels; i++) {
1136 +               bam_dma_terminate_all(&bdev->channels[i]);
1137 +               tasklet_kill(&bdev->channels[i].vc.task);
1138 +
1139 +               dma_free_writecombine(bdev->dev, BAM_DESC_FIFO_SIZE,
1140 +                       bdev->channels[i].fifo_virt,
1141 +                       bdev->channels[i].fifo_phys);
1142 +       }
1143 +
1144 +       tasklet_kill(&bdev->task);
1145 +
1146 +       clk_disable_unprepare(bdev->bamclk);
1147 +
1148 +       return 0;
1149 +}
1150 +
1151 +static const struct of_device_id bam_of_match[] = {
1152 +       { .compatible = "qcom,bam-v1.4.0", },
1153 +       {}
1154 +};
1155 +MODULE_DEVICE_TABLE(of, bam_of_match);
1156 +
1157 +static struct platform_driver bam_dma_driver = {
1158 +       .probe = bam_dma_probe,
1159 +       .remove = bam_dma_remove,
1160 +       .driver = {
1161 +               .name = "bam-dma-engine",
1162 +               .owner = THIS_MODULE,
1163 +               .of_match_table = bam_of_match,
1164 +       },
1165 +};
1166 +
1167 +module_platform_driver(bam_dma_driver);
1168 +
1169 +MODULE_AUTHOR("Andy Gross <agross@codeaurora.org>");
1170 +MODULE_DESCRIPTION("QCOM BAM DMA engine driver");
1171 +MODULE_LICENSE("GPL v2");
1172 -- 
1173 1.7.10.4
1174