blob: a8a79b1763d5605820db48c733cbbe358f686d89 [file] [log] [blame]
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +08001/*
2 * Driver For Marvell Two-channel DMA Engine
3 *
4 * Copyright: Marvell International Ltd.
5 *
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
9 *
10 */
11
Thierry Reding73312052013-01-21 11:09:00 +010012#include <linux/err.h>
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +080013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/types.h>
16#include <linux/interrupt.h>
17#include <linux/dma-mapping.h>
18#include <linux/slab.h>
19#include <linux/dmaengine.h>
20#include <linux/platform_device.h>
21#include <linux/device.h>
22#include <mach/regs-icu.h>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020023#include <linux/platform_data/dma-mmp_tdma.h>
Zhangfei Gaof1a77572012-09-03 11:03:46 +080024#include <linux/of_device.h>
Nenghua Cao7dedc002014-01-20 20:39:01 +080025#include <linux/of_dma.h>
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +080026
27#include "dmaengine.h"
28
29/*
30 * Two-Channel DMA registers
31 */
32#define TDBCR 0x00 /* Byte Count */
33#define TDSAR 0x10 /* Src Addr */
34#define TDDAR 0x20 /* Dst Addr */
35#define TDNDPR 0x30 /* Next Desc */
36#define TDCR 0x40 /* Control */
37#define TDCP 0x60 /* Priority*/
38#define TDCDPR 0x70 /* Current Desc */
39#define TDIMR 0x80 /* Int Mask */
40#define TDISR 0xa0 /* Int Status */
41
42/* Two-Channel DMA Control Register */
43#define TDCR_SSZ_8_BITS (0x0 << 22) /* Sample Size */
44#define TDCR_SSZ_12_BITS (0x1 << 22)
45#define TDCR_SSZ_16_BITS (0x2 << 22)
46#define TDCR_SSZ_20_BITS (0x3 << 22)
47#define TDCR_SSZ_24_BITS (0x4 << 22)
48#define TDCR_SSZ_32_BITS (0x5 << 22)
49#define TDCR_SSZ_SHIFT (0x1 << 22)
50#define TDCR_SSZ_MASK (0x7 << 22)
51#define TDCR_SSPMOD (0x1 << 21) /* SSP MOD */
52#define TDCR_ABR (0x1 << 20) /* Channel Abort */
53#define TDCR_CDE (0x1 << 17) /* Close Desc Enable */
54#define TDCR_PACKMOD (0x1 << 16) /* Pack Mode (ADMA Only) */
55#define TDCR_CHANACT (0x1 << 14) /* Channel Active */
56#define TDCR_FETCHND (0x1 << 13) /* Fetch Next Desc */
57#define TDCR_CHANEN (0x1 << 12) /* Channel Enable */
58#define TDCR_INTMODE (0x1 << 10) /* Interrupt Mode */
59#define TDCR_CHAINMOD (0x1 << 9) /* Chain Mode */
60#define TDCR_BURSTSZ_MSK (0x7 << 6) /* Burst Size */
61#define TDCR_BURSTSZ_4B (0x0 << 6)
62#define TDCR_BURSTSZ_8B (0x1 << 6)
63#define TDCR_BURSTSZ_16B (0x3 << 6)
64#define TDCR_BURSTSZ_32B (0x6 << 6)
65#define TDCR_BURSTSZ_64B (0x7 << 6)
Qiao Zhou20a90b02013-10-11 09:07:01 +080066#define TDCR_BURSTSZ_SQU_1B (0x5 << 6)
67#define TDCR_BURSTSZ_SQU_2B (0x6 << 6)
68#define TDCR_BURSTSZ_SQU_4B (0x0 << 6)
69#define TDCR_BURSTSZ_SQU_8B (0x1 << 6)
70#define TDCR_BURSTSZ_SQU_16B (0x3 << 6)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +080071#define TDCR_BURSTSZ_SQU_32B (0x7 << 6)
72#define TDCR_BURSTSZ_128B (0x5 << 6)
73#define TDCR_DSTDIR_MSK (0x3 << 4) /* Dst Direction */
74#define TDCR_DSTDIR_ADDR_HOLD (0x2 << 4) /* Dst Addr Hold */
75#define TDCR_DSTDIR_ADDR_INC (0x0 << 4) /* Dst Addr Increment */
76#define TDCR_SRCDIR_MSK (0x3 << 2) /* Src Direction */
77#define TDCR_SRCDIR_ADDR_HOLD (0x2 << 2) /* Src Addr Hold */
78#define TDCR_SRCDIR_ADDR_INC (0x0 << 2) /* Src Addr Increment */
79#define TDCR_DSTDESCCONT (0x1 << 1)
80#define TDCR_SRCDESTCONT (0x1 << 0)
81
82/* Two-Channel DMA Int Mask Register */
83#define TDIMR_COMP (0x1 << 0)
84
85/* Two-Channel DMA Int Status Register */
86#define TDISR_COMP (0x1 << 0)
87
88/*
89 * Two-Channel DMA Descriptor Struct
90 * NOTE: desc's buf must be aligned to 16 bytes.
91 */
92struct mmp_tdma_desc {
93 u32 byte_cnt;
94 u32 src_addr;
95 u32 dst_addr;
96 u32 nxt_desc;
97};
98
99enum mmp_tdma_type {
100 MMP_AUD_TDMA = 0,
101 PXA910_SQU,
102};
103
104#define TDMA_ALIGNMENT 3
105#define TDMA_MAX_XFER_BYTES SZ_64K
106
107struct mmp_tdma_chan {
108 struct device *dev;
109 struct dma_chan chan;
110 struct dma_async_tx_descriptor desc;
111 struct tasklet_struct tasklet;
112
113 struct mmp_tdma_desc *desc_arr;
114 phys_addr_t desc_arr_phys;
115 int desc_num;
116 enum dma_transfer_direction dir;
117 dma_addr_t dev_addr;
118 u32 burst_sz;
119 enum dma_slave_buswidth buswidth;
120 enum dma_status status;
121
122 int idx;
123 enum mmp_tdma_type type;
124 int irq;
Vinod Koul9d0f1fa62013-11-28 14:59:39 +0530125 void __iomem *reg_base;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800126
127 size_t buf_len;
128 size_t period_len;
129 size_t pos;
Nenghua Cao3b0f4a52013-12-13 16:14:31 +0800130
131 struct gen_pool *pool;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800132};
133
134#define TDMA_CHANNEL_NUM 2
135struct mmp_tdma_device {
136 struct device *dev;
137 void __iomem *base;
138 struct dma_device device;
139 struct mmp_tdma_chan *tdmac[TDMA_CHANNEL_NUM];
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800140};
141
142#define to_mmp_tdma_chan(dchan) container_of(dchan, struct mmp_tdma_chan, chan)
143
144static void mmp_tdma_chan_set_desc(struct mmp_tdma_chan *tdmac, dma_addr_t phys)
145{
146 writel(phys, tdmac->reg_base + TDNDPR);
147 writel(readl(tdmac->reg_base + TDCR) | TDCR_FETCHND,
148 tdmac->reg_base + TDCR);
149}
150
Qiao Zhoue6222262014-09-10 16:40:48 +0800151static void mmp_tdma_enable_irq(struct mmp_tdma_chan *tdmac, bool enable)
152{
153 if (enable)
154 writel(TDIMR_COMP, tdmac->reg_base + TDIMR);
155 else
156 writel(0, tdmac->reg_base + TDIMR);
157}
158
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800159static void mmp_tdma_enable_chan(struct mmp_tdma_chan *tdmac)
160{
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800161 /* enable dma chan */
162 writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN,
163 tdmac->reg_base + TDCR);
164 tdmac->status = DMA_IN_PROGRESS;
165}
166
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100167static int mmp_tdma_disable_chan(struct dma_chan *chan)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800168{
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100169 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
170
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800171 writel(readl(tdmac->reg_base + TDCR) & ~TDCR_CHANEN,
172 tdmac->reg_base + TDCR);
Qiao Zhou8e3c5182013-06-15 12:51:48 +0800173
Vinod Koulf64eabd2013-10-16 20:50:36 +0530174 tdmac->status = DMA_COMPLETE;
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100175
176 return 0;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800177}
178
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100179static int mmp_tdma_resume_chan(struct dma_chan *chan)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800180{
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100181 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
182
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800183 writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN,
184 tdmac->reg_base + TDCR);
185 tdmac->status = DMA_IN_PROGRESS;
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100186
187 return 0;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800188}
189
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100190static int mmp_tdma_pause_chan(struct dma_chan *chan)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800191{
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100192 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
193
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800194 writel(readl(tdmac->reg_base + TDCR) & ~TDCR_CHANEN,
195 tdmac->reg_base + TDCR);
196 tdmac->status = DMA_PAUSED;
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100197
198 return 0;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800199}
200
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100201static int mmp_tdma_config_chan(struct dma_chan *chan)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800202{
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100203 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
Vinod Koula9ebbcd2013-11-29 10:52:52 +0530204 unsigned int tdcr = 0;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800205
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100206 mmp_tdma_disable_chan(chan);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800207
208 if (tdmac->dir == DMA_MEM_TO_DEV)
209 tdcr = TDCR_DSTDIR_ADDR_HOLD | TDCR_SRCDIR_ADDR_INC;
210 else if (tdmac->dir == DMA_DEV_TO_MEM)
211 tdcr = TDCR_SRCDIR_ADDR_HOLD | TDCR_DSTDIR_ADDR_INC;
212
213 if (tdmac->type == MMP_AUD_TDMA) {
214 tdcr |= TDCR_PACKMOD;
215
216 switch (tdmac->burst_sz) {
217 case 4:
218 tdcr |= TDCR_BURSTSZ_4B;
219 break;
220 case 8:
221 tdcr |= TDCR_BURSTSZ_8B;
222 break;
223 case 16:
224 tdcr |= TDCR_BURSTSZ_16B;
225 break;
226 case 32:
227 tdcr |= TDCR_BURSTSZ_32B;
228 break;
229 case 64:
230 tdcr |= TDCR_BURSTSZ_64B;
231 break;
232 case 128:
233 tdcr |= TDCR_BURSTSZ_128B;
234 break;
235 default:
236 dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n");
237 return -EINVAL;
238 }
239
240 switch (tdmac->buswidth) {
241 case DMA_SLAVE_BUSWIDTH_1_BYTE:
242 tdcr |= TDCR_SSZ_8_BITS;
243 break;
244 case DMA_SLAVE_BUSWIDTH_2_BYTES:
245 tdcr |= TDCR_SSZ_16_BITS;
246 break;
247 case DMA_SLAVE_BUSWIDTH_4_BYTES:
248 tdcr |= TDCR_SSZ_32_BITS;
249 break;
250 default:
251 dev_err(tdmac->dev, "mmp_tdma: unknown bus size.\n");
252 return -EINVAL;
253 }
254 } else if (tdmac->type == PXA910_SQU) {
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800255 tdcr |= TDCR_SSPMOD;
Qiao Zhou20a90b02013-10-11 09:07:01 +0800256
257 switch (tdmac->burst_sz) {
258 case 1:
259 tdcr |= TDCR_BURSTSZ_SQU_1B;
260 break;
261 case 2:
262 tdcr |= TDCR_BURSTSZ_SQU_2B;
263 break;
264 case 4:
265 tdcr |= TDCR_BURSTSZ_SQU_4B;
266 break;
267 case 8:
268 tdcr |= TDCR_BURSTSZ_SQU_8B;
269 break;
270 case 16:
271 tdcr |= TDCR_BURSTSZ_SQU_16B;
272 break;
273 case 32:
274 tdcr |= TDCR_BURSTSZ_SQU_32B;
275 break;
276 default:
277 dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n");
278 return -EINVAL;
279 }
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800280 }
281
282 writel(tdcr, tdmac->reg_base + TDCR);
283 return 0;
284}
285
286static int mmp_tdma_clear_chan_irq(struct mmp_tdma_chan *tdmac)
287{
288 u32 reg = readl(tdmac->reg_base + TDISR);
289
290 if (reg & TDISR_COMP) {
291 /* clear irq */
292 reg &= ~TDISR_COMP;
293 writel(reg, tdmac->reg_base + TDISR);
294
295 return 0;
296 }
297 return -EAGAIN;
298}
299
300static irqreturn_t mmp_tdma_chan_handler(int irq, void *dev_id)
301{
302 struct mmp_tdma_chan *tdmac = dev_id;
303
304 if (mmp_tdma_clear_chan_irq(tdmac) == 0) {
305 tdmac->pos = (tdmac->pos + tdmac->period_len) % tdmac->buf_len;
306 tasklet_schedule(&tdmac->tasklet);
307 return IRQ_HANDLED;
308 } else
309 return IRQ_NONE;
310}
311
312static irqreturn_t mmp_tdma_int_handler(int irq, void *dev_id)
313{
314 struct mmp_tdma_device *tdev = dev_id;
315 int i, ret;
316 int irq_num = 0;
317
318 for (i = 0; i < TDMA_CHANNEL_NUM; i++) {
319 struct mmp_tdma_chan *tdmac = tdev->tdmac[i];
320
321 ret = mmp_tdma_chan_handler(irq, tdmac);
322 if (ret == IRQ_HANDLED)
323 irq_num++;
324 }
325
326 if (irq_num)
327 return IRQ_HANDLED;
328 else
329 return IRQ_NONE;
330}
331
332static void dma_do_tasklet(unsigned long data)
333{
334 struct mmp_tdma_chan *tdmac = (struct mmp_tdma_chan *)data;
335
336 if (tdmac->desc.callback)
337 tdmac->desc.callback(tdmac->desc.callback_param);
338
339}
340
341static void mmp_tdma_free_descriptor(struct mmp_tdma_chan *tdmac)
342{
343 struct gen_pool *gpool;
344 int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc);
345
Nenghua Cao3b0f4a52013-12-13 16:14:31 +0800346 gpool = tdmac->pool;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800347 if (tdmac->desc_arr)
348 gen_pool_free(gpool, (unsigned long)tdmac->desc_arr,
349 size);
350 tdmac->desc_arr = NULL;
351
352 return;
353}
354
355static dma_cookie_t mmp_tdma_tx_submit(struct dma_async_tx_descriptor *tx)
356{
357 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(tx->chan);
358
359 mmp_tdma_chan_set_desc(tdmac, tdmac->desc_arr_phys);
360
361 return 0;
362}
363
364static int mmp_tdma_alloc_chan_resources(struct dma_chan *chan)
365{
366 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
367 int ret;
368
369 dma_async_tx_descriptor_init(&tdmac->desc, chan);
370 tdmac->desc.tx_submit = mmp_tdma_tx_submit;
371
372 if (tdmac->irq) {
373 ret = devm_request_irq(tdmac->dev, tdmac->irq,
Michael Opdenacker174b5372013-10-13 07:10:51 +0200374 mmp_tdma_chan_handler, 0, "tdma", tdmac);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800375 if (ret)
376 return ret;
377 }
378 return 1;
379}
380
381static void mmp_tdma_free_chan_resources(struct dma_chan *chan)
382{
383 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
384
385 if (tdmac->irq)
386 devm_free_irq(tdmac->dev, tdmac->irq, tdmac);
387 mmp_tdma_free_descriptor(tdmac);
388 return;
389}
390
391struct mmp_tdma_desc *mmp_tdma_alloc_descriptor(struct mmp_tdma_chan *tdmac)
392{
393 struct gen_pool *gpool;
394 int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc);
395
Nenghua Cao3b0f4a52013-12-13 16:14:31 +0800396 gpool = tdmac->pool;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800397 if (!gpool)
398 return NULL;
399
Nicolin Chena6dd30e2013-11-12 15:09:55 -0800400 tdmac->desc_arr = gen_pool_dma_alloc(gpool, size, &tdmac->desc_arr_phys);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800401
402 return tdmac->desc_arr;
403}
404
405static struct dma_async_tx_descriptor *mmp_tdma_prep_dma_cyclic(
406 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
407 size_t period_len, enum dma_transfer_direction direction,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200408 unsigned long flags)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800409{
410 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
411 struct mmp_tdma_desc *desc;
412 int num_periods = buf_len / period_len;
413 int i = 0, buf = 0;
414
Vinod Koulf64eabd2013-10-16 20:50:36 +0530415 if (tdmac->status != DMA_COMPLETE)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800416 return NULL;
417
418 if (period_len > TDMA_MAX_XFER_BYTES) {
419 dev_err(tdmac->dev,
420 "maximum period size exceeded: %d > %d\n",
421 period_len, TDMA_MAX_XFER_BYTES);
422 goto err_out;
423 }
424
425 tdmac->status = DMA_IN_PROGRESS;
426 tdmac->desc_num = num_periods;
427 desc = mmp_tdma_alloc_descriptor(tdmac);
428 if (!desc)
429 goto err_out;
430
431 while (buf < buf_len) {
432 desc = &tdmac->desc_arr[i];
433
434 if (i + 1 == num_periods)
435 desc->nxt_desc = tdmac->desc_arr_phys;
436 else
437 desc->nxt_desc = tdmac->desc_arr_phys +
438 sizeof(*desc) * (i + 1);
439
440 if (direction == DMA_MEM_TO_DEV) {
441 desc->src_addr = dma_addr;
442 desc->dst_addr = tdmac->dev_addr;
443 } else {
444 desc->src_addr = tdmac->dev_addr;
445 desc->dst_addr = dma_addr;
446 }
447 desc->byte_cnt = period_len;
448 dma_addr += period_len;
449 buf += period_len;
450 i++;
451 }
452
Qiao Zhoue6222262014-09-10 16:40:48 +0800453 /* enable interrupt */
454 if (flags & DMA_PREP_INTERRUPT)
455 mmp_tdma_enable_irq(tdmac, true);
456
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800457 tdmac->buf_len = buf_len;
458 tdmac->period_len = period_len;
459 tdmac->pos = 0;
460
461 return &tdmac->desc;
462
463err_out:
464 tdmac->status = DMA_ERROR;
465 return NULL;
466}
467
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100468static int mmp_tdma_terminate_all(struct dma_chan *chan)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800469{
470 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800471
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100472 mmp_tdma_disable_chan(chan);
473 /* disable interrupt */
474 mmp_tdma_enable_irq(tdmac, false);
475}
476
477static int mmp_tdma_config(struct dma_chan *chan,
478 struct dma_slave_config *dmaengine_cfg)
479{
480 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
481
482 if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
483 tdmac->dev_addr = dmaengine_cfg->src_addr;
484 tdmac->burst_sz = dmaengine_cfg->src_maxburst;
485 tdmac->buswidth = dmaengine_cfg->src_addr_width;
486 } else {
487 tdmac->dev_addr = dmaengine_cfg->dst_addr;
488 tdmac->burst_sz = dmaengine_cfg->dst_maxburst;
489 tdmac->buswidth = dmaengine_cfg->dst_addr_width;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800490 }
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100491 tdmac->dir = dmaengine_cfg->direction;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800492
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100493 return mmp_tdma_config_chan(chan);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800494}
495
496static enum dma_status mmp_tdma_tx_status(struct dma_chan *chan,
497 dma_cookie_t cookie, struct dma_tx_state *txstate)
498{
499 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
500
Andy Shevchenkoc14d2bc2013-05-27 15:14:41 +0300501 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
502 tdmac->buf_len - tdmac->pos);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800503
504 return tdmac->status;
505}
506
507static void mmp_tdma_issue_pending(struct dma_chan *chan)
508{
509 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
510
511 mmp_tdma_enable_chan(tdmac);
512}
513
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800514static int mmp_tdma_remove(struct platform_device *pdev)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800515{
516 struct mmp_tdma_device *tdev = platform_get_drvdata(pdev);
517
518 dma_async_device_unregister(&tdev->device);
519 return 0;
520}
521
Bill Pemberton463a1f82012-11-19 13:22:55 -0500522static int mmp_tdma_chan_init(struct mmp_tdma_device *tdev,
Nenghua Cao3b0f4a52013-12-13 16:14:31 +0800523 int idx, int irq,
524 int type, struct gen_pool *pool)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800525{
526 struct mmp_tdma_chan *tdmac;
527
528 if (idx >= TDMA_CHANNEL_NUM) {
529 dev_err(tdev->dev, "too many channels for device!\n");
530 return -EINVAL;
531 }
532
533 /* alloc channel */
534 tdmac = devm_kzalloc(tdev->dev, sizeof(*tdmac), GFP_KERNEL);
535 if (!tdmac) {
536 dev_err(tdev->dev, "no free memory for DMA channels!\n");
537 return -ENOMEM;
538 }
539 if (irq)
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800540 tdmac->irq = irq;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800541 tdmac->dev = tdev->dev;
542 tdmac->chan.device = &tdev->device;
543 tdmac->idx = idx;
544 tdmac->type = type;
Vinod Koul9d0f1fa62013-11-28 14:59:39 +0530545 tdmac->reg_base = tdev->base + idx * 4;
Nenghua Cao3b0f4a52013-12-13 16:14:31 +0800546 tdmac->pool = pool;
Vinod Koulf64eabd2013-10-16 20:50:36 +0530547 tdmac->status = DMA_COMPLETE;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800548 tdev->tdmac[tdmac->idx] = tdmac;
549 tasklet_init(&tdmac->tasklet, dma_do_tasklet, (unsigned long)tdmac);
550
551 /* add the channel to tdma_chan list */
552 list_add_tail(&tdmac->chan.device_node,
553 &tdev->device.channels);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800554 return 0;
555}
556
Nenghua Cao7dedc002014-01-20 20:39:01 +0800557struct mmp_tdma_filter_param {
558 struct device_node *of_node;
559 unsigned int chan_id;
560};
561
562static bool mmp_tdma_filter_fn(struct dma_chan *chan, void *fn_param)
563{
564 struct mmp_tdma_filter_param *param = fn_param;
565 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
566 struct dma_device *pdma_device = tdmac->chan.device;
567
568 if (pdma_device->dev->of_node != param->of_node)
569 return false;
570
571 if (chan->chan_id != param->chan_id)
572 return false;
573
574 return true;
575}
576
577struct dma_chan *mmp_tdma_xlate(struct of_phandle_args *dma_spec,
578 struct of_dma *ofdma)
579{
580 struct mmp_tdma_device *tdev = ofdma->of_dma_data;
581 dma_cap_mask_t mask = tdev->device.cap_mask;
582 struct mmp_tdma_filter_param param;
583
584 if (dma_spec->args_count != 1)
585 return NULL;
586
587 param.of_node = ofdma->of_node;
588 param.chan_id = dma_spec->args[0];
589
590 if (param.chan_id >= TDMA_CHANNEL_NUM)
591 return NULL;
592
593 return dma_request_channel(mask, mmp_tdma_filter_fn, &param);
594}
595
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800596static struct of_device_id mmp_tdma_dt_ids[] = {
597 { .compatible = "marvell,adma-1.0", .data = (void *)MMP_AUD_TDMA},
598 { .compatible = "marvell,pxa910-squ", .data = (void *)PXA910_SQU},
599 {}
600};
601MODULE_DEVICE_TABLE(of, mmp_tdma_dt_ids);
602
Bill Pemberton463a1f82012-11-19 13:22:55 -0500603static int mmp_tdma_probe(struct platform_device *pdev)
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800604{
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800605 enum mmp_tdma_type type;
606 const struct of_device_id *of_id;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800607 struct mmp_tdma_device *tdev;
608 struct resource *iores;
609 int i, ret;
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800610 int irq = 0, irq_num = 0;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800611 int chan_num = TDMA_CHANNEL_NUM;
Nenghua Cao3b0f4a52013-12-13 16:14:31 +0800612 struct gen_pool *pool;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800613
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800614 of_id = of_match_device(mmp_tdma_dt_ids, &pdev->dev);
615 if (of_id)
616 type = (enum mmp_tdma_type) of_id->data;
617 else
618 type = platform_get_device_id(pdev)->driver_data;
619
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800620 /* always have couple channels */
621 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
622 if (!tdev)
623 return -ENOMEM;
624
625 tdev->dev = &pdev->dev;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800626
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800627 for (i = 0; i < chan_num; i++) {
628 if (platform_get_irq(pdev, i) > 0)
629 irq_num++;
630 }
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800631
632 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding73312052013-01-21 11:09:00 +0100633 tdev->base = devm_ioremap_resource(&pdev->dev, iores);
634 if (IS_ERR(tdev->base))
635 return PTR_ERR(tdev->base);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800636
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800637 INIT_LIST_HEAD(&tdev->device.channels);
638
Nenghua Cao3b0f4a52013-12-13 16:14:31 +0800639 if (pdev->dev.of_node)
640 pool = of_get_named_gen_pool(pdev->dev.of_node, "asram", 0);
641 else
642 pool = sram_get_gpool("asram");
643 if (!pool) {
644 dev_err(&pdev->dev, "asram pool not available\n");
645 return -ENOMEM;
646 }
647
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800648 if (irq_num != chan_num) {
649 irq = platform_get_irq(pdev, 0);
650 ret = devm_request_irq(&pdev->dev, irq,
Michael Opdenacker174b5372013-10-13 07:10:51 +0200651 mmp_tdma_int_handler, 0, "tdma", tdev);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800652 if (ret)
653 return ret;
654 }
655
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800656 /* initialize channel parameters */
657 for (i = 0; i < chan_num; i++) {
658 irq = (irq_num != chan_num) ? 0 : platform_get_irq(pdev, i);
Nenghua Cao3b0f4a52013-12-13 16:14:31 +0800659 ret = mmp_tdma_chan_init(tdev, i, irq, type, pool);
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800660 if (ret)
661 return ret;
662 }
663
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800664 dma_cap_set(DMA_SLAVE, tdev->device.cap_mask);
665 dma_cap_set(DMA_CYCLIC, tdev->device.cap_mask);
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800666 tdev->device.dev = &pdev->dev;
667 tdev->device.device_alloc_chan_resources =
668 mmp_tdma_alloc_chan_resources;
669 tdev->device.device_free_chan_resources =
670 mmp_tdma_free_chan_resources;
671 tdev->device.device_prep_dma_cyclic = mmp_tdma_prep_dma_cyclic;
672 tdev->device.device_tx_status = mmp_tdma_tx_status;
673 tdev->device.device_issue_pending = mmp_tdma_issue_pending;
Maxime Ripardf43a6fd2014-11-17 14:42:22 +0100674 tdev->device.device_config = mmp_tdma_config;
675 tdev->device.device_pause = mmp_tdma_pause_chan;
676 tdev->device.device_resume = mmp_tdma_resume_chan;
677 tdev->device.device_terminate_all = mmp_tdma_terminate_all;
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800678 tdev->device.copy_align = TDMA_ALIGNMENT;
679
680 dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
681 platform_set_drvdata(pdev, tdev);
682
683 ret = dma_async_device_register(&tdev->device);
684 if (ret) {
685 dev_err(tdev->device.dev, "unable to register\n");
686 return ret;
687 }
688
Nenghua Cao7dedc002014-01-20 20:39:01 +0800689 if (pdev->dev.of_node) {
690 ret = of_dma_controller_register(pdev->dev.of_node,
691 mmp_tdma_xlate, tdev);
692 if (ret) {
693 dev_err(tdev->device.dev,
694 "failed to register controller\n");
695 dma_async_device_unregister(&tdev->device);
696 }
697 }
698
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800699 dev_info(tdev->device.dev, "initialized\n");
700 return 0;
701}
702
703static const struct platform_device_id mmp_tdma_id_table[] = {
704 { "mmp-adma", MMP_AUD_TDMA },
705 { "pxa910-squ", PXA910_SQU },
706 { },
707};
708
709static struct platform_driver mmp_tdma_driver = {
710 .driver = {
711 .name = "mmp-tdma",
Zhangfei Gaof1a77572012-09-03 11:03:46 +0800712 .of_match_table = mmp_tdma_dt_ids,
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800713 },
714 .id_table = mmp_tdma_id_table,
715 .probe = mmp_tdma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500716 .remove = mmp_tdma_remove,
Zhangfei Gaoc6da0ba2012-06-15 11:04:08 +0800717};
718
719module_platform_driver(mmp_tdma_driver);
720
721MODULE_LICENSE("GPL");
722MODULE_DESCRIPTION("MMP Two-Channel DMA Driver");
723MODULE_ALIAS("platform:mmp-tdma");
724MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
725MODULE_AUTHOR("Zhangfei Gao <zhangfei.gao@marvell.com>");