2 * Driver For Marvell Two-channel DMA Engine
4 * Copyright: Marvell International Ltd.
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:
12 #include <linux/err.h>
13 #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 <linux/platform_data/dma-mmp_tdma.h>
23 #include <linux/of_device.h>
24 #include <linux/of_dma.h>
26 #include "dmaengine.h"
29 * Two-Channel DMA registers
31 #define TDBCR 0x00 /* Byte Count */
32 #define TDSAR 0x10 /* Src Addr */
33 #define TDDAR 0x20 /* Dst Addr */
34 #define TDNDPR 0x30 /* Next Desc */
35 #define TDCR 0x40 /* Control */
36 #define TDCP 0x60 /* Priority*/
37 #define TDCDPR 0x70 /* Current Desc */
38 #define TDIMR 0x80 /* Int Mask */
39 #define TDISR 0xa0 /* Int Status */
41 /* Two-Channel DMA Control Register */
42 #define TDCR_SSZ_8_BITS (0x0 << 22) /* Sample Size */
43 #define TDCR_SSZ_12_BITS (0x1 << 22)
44 #define TDCR_SSZ_16_BITS (0x2 << 22)
45 #define TDCR_SSZ_20_BITS (0x3 << 22)
46 #define TDCR_SSZ_24_BITS (0x4 << 22)
47 #define TDCR_SSZ_32_BITS (0x5 << 22)
48 #define TDCR_SSZ_SHIFT (0x1 << 22)
49 #define TDCR_SSZ_MASK (0x7 << 22)
50 #define TDCR_SSPMOD (0x1 << 21) /* SSP MOD */
51 #define TDCR_ABR (0x1 << 20) /* Channel Abort */
52 #define TDCR_CDE (0x1 << 17) /* Close Desc Enable */
53 #define TDCR_PACKMOD (0x1 << 16) /* Pack Mode (ADMA Only) */
54 #define TDCR_CHANACT (0x1 << 14) /* Channel Active */
55 #define TDCR_FETCHND (0x1 << 13) /* Fetch Next Desc */
56 #define TDCR_CHANEN (0x1 << 12) /* Channel Enable */
57 #define TDCR_INTMODE (0x1 << 10) /* Interrupt Mode */
58 #define TDCR_CHAINMOD (0x1 << 9) /* Chain Mode */
59 #define TDCR_BURSTSZ_MSK (0x7 << 6) /* Burst Size */
60 #define TDCR_BURSTSZ_4B (0x0 << 6)
61 #define TDCR_BURSTSZ_8B (0x1 << 6)
62 #define TDCR_BURSTSZ_16B (0x3 << 6)
63 #define TDCR_BURSTSZ_32B (0x6 << 6)
64 #define TDCR_BURSTSZ_64B (0x7 << 6)
65 #define TDCR_BURSTSZ_SQU_1B (0x5 << 6)
66 #define TDCR_BURSTSZ_SQU_2B (0x6 << 6)
67 #define TDCR_BURSTSZ_SQU_4B (0x0 << 6)
68 #define TDCR_BURSTSZ_SQU_8B (0x1 << 6)
69 #define TDCR_BURSTSZ_SQU_16B (0x3 << 6)
70 #define TDCR_BURSTSZ_SQU_32B (0x7 << 6)
71 #define TDCR_BURSTSZ_128B (0x5 << 6)
72 #define TDCR_DSTDIR_MSK (0x3 << 4) /* Dst Direction */
73 #define TDCR_DSTDIR_ADDR_HOLD (0x2 << 4) /* Dst Addr Hold */
74 #define TDCR_DSTDIR_ADDR_INC (0x0 << 4) /* Dst Addr Increment */
75 #define TDCR_SRCDIR_MSK (0x3 << 2) /* Src Direction */
76 #define TDCR_SRCDIR_ADDR_HOLD (0x2 << 2) /* Src Addr Hold */
77 #define TDCR_SRCDIR_ADDR_INC (0x0 << 2) /* Src Addr Increment */
78 #define TDCR_DSTDESCCONT (0x1 << 1)
79 #define TDCR_SRCDESTCONT (0x1 << 0)
81 /* Two-Channel DMA Int Mask Register */
82 #define TDIMR_COMP (0x1 << 0)
84 /* Two-Channel DMA Int Status Register */
85 #define TDISR_COMP (0x1 << 0)
88 * Two-Channel DMA Descriptor Struct
89 * NOTE: desc's buf must be aligned to 16 bytes.
91 struct mmp_tdma_desc {
103 #define TDMA_MAX_XFER_BYTES SZ_64K
105 struct mmp_tdma_chan {
107 struct dma_chan chan;
108 struct dma_async_tx_descriptor desc;
109 struct tasklet_struct tasklet;
111 struct mmp_tdma_desc *desc_arr;
112 dma_addr_t desc_arr_phys;
114 enum dma_transfer_direction dir;
117 enum dma_slave_buswidth buswidth;
118 enum dma_status status;
121 enum mmp_tdma_type type;
123 void __iomem *reg_base;
129 struct gen_pool *pool;
132 #define TDMA_CHANNEL_NUM 2
133 struct mmp_tdma_device {
136 struct dma_device device;
137 struct mmp_tdma_chan *tdmac[TDMA_CHANNEL_NUM];
140 #define to_mmp_tdma_chan(dchan) container_of(dchan, struct mmp_tdma_chan, chan)
142 static void mmp_tdma_chan_set_desc(struct mmp_tdma_chan *tdmac, dma_addr_t phys)
144 writel(phys, tdmac->reg_base + TDNDPR);
145 writel(readl(tdmac->reg_base + TDCR) | TDCR_FETCHND,
146 tdmac->reg_base + TDCR);
149 static void mmp_tdma_enable_irq(struct mmp_tdma_chan *tdmac, bool enable)
152 writel(TDIMR_COMP, tdmac->reg_base + TDIMR);
154 writel(0, tdmac->reg_base + TDIMR);
157 static void mmp_tdma_enable_chan(struct mmp_tdma_chan *tdmac)
159 /* enable dma chan */
160 writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN,
161 tdmac->reg_base + TDCR);
162 tdmac->status = DMA_IN_PROGRESS;
165 static int mmp_tdma_disable_chan(struct dma_chan *chan)
167 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
170 tdcr = readl(tdmac->reg_base + TDCR);
172 tdcr &= ~TDCR_CHANEN;
173 writel(tdcr, tdmac->reg_base + TDCR);
175 tdmac->status = DMA_COMPLETE;
180 static int mmp_tdma_resume_chan(struct dma_chan *chan)
182 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
184 writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN,
185 tdmac->reg_base + TDCR);
186 tdmac->status = DMA_IN_PROGRESS;
191 static int mmp_tdma_pause_chan(struct dma_chan *chan)
193 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
195 writel(readl(tdmac->reg_base + TDCR) & ~TDCR_CHANEN,
196 tdmac->reg_base + TDCR);
197 tdmac->status = DMA_PAUSED;
202 static int mmp_tdma_config_chan(struct dma_chan *chan)
204 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
205 unsigned int tdcr = 0;
207 mmp_tdma_disable_chan(chan);
209 if (tdmac->dir == DMA_MEM_TO_DEV)
210 tdcr = TDCR_DSTDIR_ADDR_HOLD | TDCR_SRCDIR_ADDR_INC;
211 else if (tdmac->dir == DMA_DEV_TO_MEM)
212 tdcr = TDCR_SRCDIR_ADDR_HOLD | TDCR_DSTDIR_ADDR_INC;
214 if (tdmac->type == MMP_AUD_TDMA) {
215 tdcr |= TDCR_PACKMOD;
217 switch (tdmac->burst_sz) {
219 tdcr |= TDCR_BURSTSZ_4B;
222 tdcr |= TDCR_BURSTSZ_8B;
225 tdcr |= TDCR_BURSTSZ_16B;
228 tdcr |= TDCR_BURSTSZ_32B;
231 tdcr |= TDCR_BURSTSZ_64B;
234 tdcr |= TDCR_BURSTSZ_128B;
237 dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n");
241 switch (tdmac->buswidth) {
242 case DMA_SLAVE_BUSWIDTH_1_BYTE:
243 tdcr |= TDCR_SSZ_8_BITS;
245 case DMA_SLAVE_BUSWIDTH_2_BYTES:
246 tdcr |= TDCR_SSZ_16_BITS;
248 case DMA_SLAVE_BUSWIDTH_4_BYTES:
249 tdcr |= TDCR_SSZ_32_BITS;
252 dev_err(tdmac->dev, "mmp_tdma: unknown bus size.\n");
255 } else if (tdmac->type == PXA910_SQU) {
258 switch (tdmac->burst_sz) {
260 tdcr |= TDCR_BURSTSZ_SQU_1B;
263 tdcr |= TDCR_BURSTSZ_SQU_2B;
266 tdcr |= TDCR_BURSTSZ_SQU_4B;
269 tdcr |= TDCR_BURSTSZ_SQU_8B;
272 tdcr |= TDCR_BURSTSZ_SQU_16B;
275 tdcr |= TDCR_BURSTSZ_SQU_32B;
278 dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n");
283 writel(tdcr, tdmac->reg_base + TDCR);
287 static int mmp_tdma_clear_chan_irq(struct mmp_tdma_chan *tdmac)
289 u32 reg = readl(tdmac->reg_base + TDISR);
291 if (reg & TDISR_COMP) {
294 writel(reg, tdmac->reg_base + TDISR);
301 static size_t mmp_tdma_get_pos(struct mmp_tdma_chan *tdmac)
305 if (tdmac->idx == 0) {
306 reg = __raw_readl(tdmac->reg_base + TDSAR);
307 reg -= tdmac->desc_arr[0].src_addr;
308 } else if (tdmac->idx == 1) {
309 reg = __raw_readl(tdmac->reg_base + TDDAR);
310 reg -= tdmac->desc_arr[0].dst_addr;
317 static irqreturn_t mmp_tdma_chan_handler(int irq, void *dev_id)
319 struct mmp_tdma_chan *tdmac = dev_id;
321 if (mmp_tdma_clear_chan_irq(tdmac) == 0) {
322 tasklet_schedule(&tdmac->tasklet);
328 static irqreturn_t mmp_tdma_int_handler(int irq, void *dev_id)
330 struct mmp_tdma_device *tdev = dev_id;
334 for (i = 0; i < TDMA_CHANNEL_NUM; i++) {
335 struct mmp_tdma_chan *tdmac = tdev->tdmac[i];
337 ret = mmp_tdma_chan_handler(irq, tdmac);
338 if (ret == IRQ_HANDLED)
348 static void dma_do_tasklet(unsigned long data)
350 struct mmp_tdma_chan *tdmac = (struct mmp_tdma_chan *)data;
352 if (tdmac->desc.callback)
353 tdmac->desc.callback(tdmac->desc.callback_param);
357 static void mmp_tdma_free_descriptor(struct mmp_tdma_chan *tdmac)
359 struct gen_pool *gpool;
360 int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc);
363 if (gpool && tdmac->desc_arr)
364 gen_pool_free(gpool, (unsigned long)tdmac->desc_arr,
366 tdmac->desc_arr = NULL;
371 static dma_cookie_t mmp_tdma_tx_submit(struct dma_async_tx_descriptor *tx)
373 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(tx->chan);
375 mmp_tdma_chan_set_desc(tdmac, tdmac->desc_arr_phys);
380 static int mmp_tdma_alloc_chan_resources(struct dma_chan *chan)
382 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
385 dma_async_tx_descriptor_init(&tdmac->desc, chan);
386 tdmac->desc.tx_submit = mmp_tdma_tx_submit;
389 ret = devm_request_irq(tdmac->dev, tdmac->irq,
390 mmp_tdma_chan_handler, 0, "tdma", tdmac);
397 static void mmp_tdma_free_chan_resources(struct dma_chan *chan)
399 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
402 devm_free_irq(tdmac->dev, tdmac->irq, tdmac);
403 mmp_tdma_free_descriptor(tdmac);
407 struct mmp_tdma_desc *mmp_tdma_alloc_descriptor(struct mmp_tdma_chan *tdmac)
409 struct gen_pool *gpool;
410 int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc);
416 tdmac->desc_arr = gen_pool_dma_alloc(gpool, size, &tdmac->desc_arr_phys);
418 return tdmac->desc_arr;
421 static struct dma_async_tx_descriptor *mmp_tdma_prep_dma_cyclic(
422 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
423 size_t period_len, enum dma_transfer_direction direction,
426 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
427 struct mmp_tdma_desc *desc;
428 int num_periods = buf_len / period_len;
431 if (tdmac->status != DMA_COMPLETE)
434 if (period_len > TDMA_MAX_XFER_BYTES) {
436 "maximum period size exceeded: %d > %d\n",
437 period_len, TDMA_MAX_XFER_BYTES);
441 tdmac->status = DMA_IN_PROGRESS;
442 tdmac->desc_num = num_periods;
443 desc = mmp_tdma_alloc_descriptor(tdmac);
447 while (buf < buf_len) {
448 desc = &tdmac->desc_arr[i];
450 if (i + 1 == num_periods)
451 desc->nxt_desc = tdmac->desc_arr_phys;
453 desc->nxt_desc = tdmac->desc_arr_phys +
454 sizeof(*desc) * (i + 1);
456 if (direction == DMA_MEM_TO_DEV) {
457 desc->src_addr = dma_addr;
458 desc->dst_addr = tdmac->dev_addr;
460 desc->src_addr = tdmac->dev_addr;
461 desc->dst_addr = dma_addr;
463 desc->byte_cnt = period_len;
464 dma_addr += period_len;
469 /* enable interrupt */
470 if (flags & DMA_PREP_INTERRUPT)
471 mmp_tdma_enable_irq(tdmac, true);
473 tdmac->buf_len = buf_len;
474 tdmac->period_len = period_len;
480 tdmac->status = DMA_ERROR;
484 static int mmp_tdma_terminate_all(struct dma_chan *chan)
486 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
488 mmp_tdma_disable_chan(chan);
489 /* disable interrupt */
490 mmp_tdma_enable_irq(tdmac, false);
495 static int mmp_tdma_config(struct dma_chan *chan,
496 struct dma_slave_config *dmaengine_cfg)
498 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
500 if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
501 tdmac->dev_addr = dmaengine_cfg->src_addr;
502 tdmac->burst_sz = dmaengine_cfg->src_maxburst;
503 tdmac->buswidth = dmaengine_cfg->src_addr_width;
505 tdmac->dev_addr = dmaengine_cfg->dst_addr;
506 tdmac->burst_sz = dmaengine_cfg->dst_maxburst;
507 tdmac->buswidth = dmaengine_cfg->dst_addr_width;
509 tdmac->dir = dmaengine_cfg->direction;
511 return mmp_tdma_config_chan(chan);
514 static enum dma_status mmp_tdma_tx_status(struct dma_chan *chan,
515 dma_cookie_t cookie, struct dma_tx_state *txstate)
517 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
519 tdmac->pos = mmp_tdma_get_pos(tdmac);
520 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
521 tdmac->buf_len - tdmac->pos);
523 return tdmac->status;
526 static void mmp_tdma_issue_pending(struct dma_chan *chan)
528 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
530 mmp_tdma_enable_chan(tdmac);
533 static int mmp_tdma_remove(struct platform_device *pdev)
535 struct mmp_tdma_device *tdev = platform_get_drvdata(pdev);
537 dma_async_device_unregister(&tdev->device);
541 static int mmp_tdma_chan_init(struct mmp_tdma_device *tdev,
543 int type, struct gen_pool *pool)
545 struct mmp_tdma_chan *tdmac;
547 if (idx >= TDMA_CHANNEL_NUM) {
548 dev_err(tdev->dev, "too many channels for device!\n");
553 tdmac = devm_kzalloc(tdev->dev, sizeof(*tdmac), GFP_KERNEL);
555 dev_err(tdev->dev, "no free memory for DMA channels!\n");
560 tdmac->dev = tdev->dev;
561 tdmac->chan.device = &tdev->device;
564 tdmac->reg_base = tdev->base + idx * 4;
566 tdmac->status = DMA_COMPLETE;
567 tdev->tdmac[tdmac->idx] = tdmac;
568 tasklet_init(&tdmac->tasklet, dma_do_tasklet, (unsigned long)tdmac);
570 /* add the channel to tdma_chan list */
571 list_add_tail(&tdmac->chan.device_node,
572 &tdev->device.channels);
576 struct mmp_tdma_filter_param {
577 struct device_node *of_node;
578 unsigned int chan_id;
581 static bool mmp_tdma_filter_fn(struct dma_chan *chan, void *fn_param)
583 struct mmp_tdma_filter_param *param = fn_param;
584 struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
585 struct dma_device *pdma_device = tdmac->chan.device;
587 if (pdma_device->dev->of_node != param->of_node)
590 if (chan->chan_id != param->chan_id)
596 struct dma_chan *mmp_tdma_xlate(struct of_phandle_args *dma_spec,
597 struct of_dma *ofdma)
599 struct mmp_tdma_device *tdev = ofdma->of_dma_data;
600 dma_cap_mask_t mask = tdev->device.cap_mask;
601 struct mmp_tdma_filter_param param;
603 if (dma_spec->args_count != 1)
606 param.of_node = ofdma->of_node;
607 param.chan_id = dma_spec->args[0];
609 if (param.chan_id >= TDMA_CHANNEL_NUM)
612 return dma_request_channel(mask, mmp_tdma_filter_fn, ¶m);
615 static const struct of_device_id mmp_tdma_dt_ids[] = {
616 { .compatible = "marvell,adma-1.0", .data = (void *)MMP_AUD_TDMA},
617 { .compatible = "marvell,pxa910-squ", .data = (void *)PXA910_SQU},
620 MODULE_DEVICE_TABLE(of, mmp_tdma_dt_ids);
622 static int mmp_tdma_probe(struct platform_device *pdev)
624 enum mmp_tdma_type type;
625 const struct of_device_id *of_id;
626 struct mmp_tdma_device *tdev;
627 struct resource *iores;
629 int irq = 0, irq_num = 0;
630 int chan_num = TDMA_CHANNEL_NUM;
631 struct gen_pool *pool = NULL;
633 of_id = of_match_device(mmp_tdma_dt_ids, &pdev->dev);
635 type = (enum mmp_tdma_type) of_id->data;
637 type = platform_get_device_id(pdev)->driver_data;
639 /* always have couple channels */
640 tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
644 tdev->dev = &pdev->dev;
646 for (i = 0; i < chan_num; i++) {
647 if (platform_get_irq(pdev, i) > 0)
651 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
652 tdev->base = devm_ioremap_resource(&pdev->dev, iores);
653 if (IS_ERR(tdev->base))
654 return PTR_ERR(tdev->base);
656 INIT_LIST_HEAD(&tdev->device.channels);
658 if (pdev->dev.of_node)
659 pool = of_gen_pool_get(pdev->dev.of_node, "asram", 0);
661 pool = sram_get_gpool("asram");
663 dev_err(&pdev->dev, "asram pool not available\n");
667 if (irq_num != chan_num) {
668 irq = platform_get_irq(pdev, 0);
669 ret = devm_request_irq(&pdev->dev, irq,
670 mmp_tdma_int_handler, 0, "tdma", tdev);
675 /* initialize channel parameters */
676 for (i = 0; i < chan_num; i++) {
677 irq = (irq_num != chan_num) ? 0 : platform_get_irq(pdev, i);
678 ret = mmp_tdma_chan_init(tdev, i, irq, type, pool);
683 dma_cap_set(DMA_SLAVE, tdev->device.cap_mask);
684 dma_cap_set(DMA_CYCLIC, tdev->device.cap_mask);
685 tdev->device.dev = &pdev->dev;
686 tdev->device.device_alloc_chan_resources =
687 mmp_tdma_alloc_chan_resources;
688 tdev->device.device_free_chan_resources =
689 mmp_tdma_free_chan_resources;
690 tdev->device.device_prep_dma_cyclic = mmp_tdma_prep_dma_cyclic;
691 tdev->device.device_tx_status = mmp_tdma_tx_status;
692 tdev->device.device_issue_pending = mmp_tdma_issue_pending;
693 tdev->device.device_config = mmp_tdma_config;
694 tdev->device.device_pause = mmp_tdma_pause_chan;
695 tdev->device.device_resume = mmp_tdma_resume_chan;
696 tdev->device.device_terminate_all = mmp_tdma_terminate_all;
697 tdev->device.copy_align = DMAENGINE_ALIGN_8_BYTES;
699 dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
700 platform_set_drvdata(pdev, tdev);
702 ret = dma_async_device_register(&tdev->device);
704 dev_err(tdev->device.dev, "unable to register\n");
708 if (pdev->dev.of_node) {
709 ret = of_dma_controller_register(pdev->dev.of_node,
710 mmp_tdma_xlate, tdev);
712 dev_err(tdev->device.dev,
713 "failed to register controller\n");
714 dma_async_device_unregister(&tdev->device);
718 dev_info(tdev->device.dev, "initialized\n");
722 static const struct platform_device_id mmp_tdma_id_table[] = {
723 { "mmp-adma", MMP_AUD_TDMA },
724 { "pxa910-squ", PXA910_SQU },
728 static struct platform_driver mmp_tdma_driver = {
731 .of_match_table = mmp_tdma_dt_ids,
733 .id_table = mmp_tdma_id_table,
734 .probe = mmp_tdma_probe,
735 .remove = mmp_tdma_remove,
738 module_platform_driver(mmp_tdma_driver);
740 MODULE_LICENSE("GPL");
741 MODULE_DESCRIPTION("MMP Two-Channel DMA Driver");
742 MODULE_ALIAS("platform:mmp-tdma");
743 MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
744 MODULE_AUTHOR("Zhangfei Gao <zhangfei.gao@marvell.com>");