2 * Ingenic JZ4780 DMA controller
4 * Copyright (c) 2015 Imagination Technologies
5 * Author: Alex Smith <alex@alex-smith.me.uk>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/clk.h>
14 #include <linux/dmapool.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/of_dma.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #include "dmaengine.h"
27 /* Global registers. */
28 #define JZ_DMA_REG_DMAC 0x00
29 #define JZ_DMA_REG_DIRQP 0x04
30 #define JZ_DMA_REG_DDR 0x08
31 #define JZ_DMA_REG_DDRS 0x0c
32 #define JZ_DMA_REG_DMACP 0x1c
33 #define JZ_DMA_REG_DSIRQP 0x20
34 #define JZ_DMA_REG_DSIRQM 0x24
35 #define JZ_DMA_REG_DCIRQP 0x28
36 #define JZ_DMA_REG_DCIRQM 0x2c
38 /* Per-channel registers. */
39 #define JZ_DMA_REG_CHAN(n) (n * 0x20)
40 #define JZ_DMA_REG_DSA 0x00
41 #define JZ_DMA_REG_DTA 0x04
42 #define JZ_DMA_REG_DTC 0x08
43 #define JZ_DMA_REG_DRT 0x0c
44 #define JZ_DMA_REG_DCS 0x10
45 #define JZ_DMA_REG_DCM 0x14
46 #define JZ_DMA_REG_DDA 0x18
47 #define JZ_DMA_REG_DSD 0x1c
49 #define JZ_DMA_DMAC_DMAE BIT(0)
50 #define JZ_DMA_DMAC_AR BIT(2)
51 #define JZ_DMA_DMAC_HLT BIT(3)
52 #define JZ_DMA_DMAC_FMSC BIT(31)
54 #define JZ_DMA_DRT_AUTO 0x8
56 #define JZ_DMA_DCS_CTE BIT(0)
57 #define JZ_DMA_DCS_HLT BIT(2)
58 #define JZ_DMA_DCS_TT BIT(3)
59 #define JZ_DMA_DCS_AR BIT(4)
60 #define JZ_DMA_DCS_DES8 BIT(30)
62 #define JZ_DMA_DCM_LINK BIT(0)
63 #define JZ_DMA_DCM_TIE BIT(1)
64 #define JZ_DMA_DCM_STDE BIT(2)
65 #define JZ_DMA_DCM_TSZ_SHIFT 8
66 #define JZ_DMA_DCM_TSZ_MASK (0x7 << JZ_DMA_DCM_TSZ_SHIFT)
67 #define JZ_DMA_DCM_DP_SHIFT 12
68 #define JZ_DMA_DCM_SP_SHIFT 14
69 #define JZ_DMA_DCM_DAI BIT(22)
70 #define JZ_DMA_DCM_SAI BIT(23)
72 #define JZ_DMA_SIZE_4_BYTE 0x0
73 #define JZ_DMA_SIZE_1_BYTE 0x1
74 #define JZ_DMA_SIZE_2_BYTE 0x2
75 #define JZ_DMA_SIZE_16_BYTE 0x3
76 #define JZ_DMA_SIZE_32_BYTE 0x4
77 #define JZ_DMA_SIZE_64_BYTE 0x5
78 #define JZ_DMA_SIZE_128_BYTE 0x6
80 #define JZ_DMA_WIDTH_32_BIT 0x0
81 #define JZ_DMA_WIDTH_8_BIT 0x1
82 #define JZ_DMA_WIDTH_16_BIT 0x2
84 #define JZ_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
85 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
86 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
88 #define JZ4780_DMA_CTRL_OFFSET 0x1000
91 * struct jz4780_dma_hwdesc - descriptor structure read by the DMA controller.
92 * @dcm: value for the DCM (channel command) register
93 * @dsa: source address
94 * @dta: target address
95 * @dtc: transfer count (number of blocks of the transfer size specified in DCM
96 * to transfer) in the low 24 bits, offset of the next descriptor from the
97 * descriptor base address in the upper 8 bits.
98 * @sd: target/source stride difference (in stride transfer mode).
101 struct jz4780_dma_hwdesc {
108 uint32_t reserved[2];
111 /* Size of allocations for hardware descriptor blocks. */
112 #define JZ_DMA_DESC_BLOCK_SIZE PAGE_SIZE
113 #define JZ_DMA_MAX_DESC \
114 (JZ_DMA_DESC_BLOCK_SIZE / sizeof(struct jz4780_dma_hwdesc))
116 struct jz4780_dma_desc {
117 struct virt_dma_desc vdesc;
119 struct jz4780_dma_hwdesc *desc;
120 dma_addr_t desc_phys;
122 enum dma_transaction_type type;
126 struct jz4780_dma_chan {
127 struct virt_dma_chan vchan;
129 struct dma_pool *desc_pool;
131 uint32_t transfer_type;
132 uint32_t transfer_shift;
133 struct dma_slave_config config;
135 struct jz4780_dma_desc *desc;
136 unsigned int curr_hwdesc;
139 struct jz4780_dma_soc_data {
140 unsigned int nb_channels;
143 struct jz4780_dma_dev {
144 struct dma_device dma_device;
145 void __iomem *chn_base;
146 void __iomem *ctrl_base;
149 const struct jz4780_dma_soc_data *soc_data;
151 uint32_t chan_reserved;
152 struct jz4780_dma_chan chan[];
155 struct jz4780_dma_filter_data {
156 struct device_node *of_node;
157 uint32_t transfer_type;
161 static inline struct jz4780_dma_chan *to_jz4780_dma_chan(struct dma_chan *chan)
163 return container_of(chan, struct jz4780_dma_chan, vchan.chan);
166 static inline struct jz4780_dma_desc *to_jz4780_dma_desc(
167 struct virt_dma_desc *vdesc)
169 return container_of(vdesc, struct jz4780_dma_desc, vdesc);
172 static inline struct jz4780_dma_dev *jz4780_dma_chan_parent(
173 struct jz4780_dma_chan *jzchan)
175 return container_of(jzchan->vchan.chan.device, struct jz4780_dma_dev,
179 static inline uint32_t jz4780_dma_chn_readl(struct jz4780_dma_dev *jzdma,
180 unsigned int chn, unsigned int reg)
182 return readl(jzdma->chn_base + reg + JZ_DMA_REG_CHAN(chn));
185 static inline void jz4780_dma_chn_writel(struct jz4780_dma_dev *jzdma,
186 unsigned int chn, unsigned int reg, uint32_t val)
188 writel(val, jzdma->chn_base + reg + JZ_DMA_REG_CHAN(chn));
191 static inline uint32_t jz4780_dma_ctrl_readl(struct jz4780_dma_dev *jzdma,
194 return readl(jzdma->ctrl_base + reg);
197 static inline void jz4780_dma_ctrl_writel(struct jz4780_dma_dev *jzdma,
198 unsigned int reg, uint32_t val)
200 writel(val, jzdma->ctrl_base + reg);
203 static struct jz4780_dma_desc *jz4780_dma_desc_alloc(
204 struct jz4780_dma_chan *jzchan, unsigned int count,
205 enum dma_transaction_type type)
207 struct jz4780_dma_desc *desc;
209 if (count > JZ_DMA_MAX_DESC)
212 desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
216 desc->desc = dma_pool_alloc(jzchan->desc_pool, GFP_NOWAIT,
228 static void jz4780_dma_desc_free(struct virt_dma_desc *vdesc)
230 struct jz4780_dma_desc *desc = to_jz4780_dma_desc(vdesc);
231 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(vdesc->tx.chan);
233 dma_pool_free(jzchan->desc_pool, desc->desc, desc->desc_phys);
237 static uint32_t jz4780_dma_transfer_size(unsigned long val, uint32_t *shift)
239 int ord = ffs(val) - 1;
242 * 8 byte transfer sizes unsupported so fall back on 4. If it's larger
243 * than the maximum, just limit it. It is perfectly safe to fall back
244 * in this way since we won't exceed the maximum burst size supported
245 * by the device, the only effect is reduced efficiency. This is better
246 * than refusing to perform the request at all.
257 return JZ_DMA_SIZE_1_BYTE;
259 return JZ_DMA_SIZE_2_BYTE;
261 return JZ_DMA_SIZE_4_BYTE;
263 return JZ_DMA_SIZE_16_BYTE;
265 return JZ_DMA_SIZE_32_BYTE;
267 return JZ_DMA_SIZE_64_BYTE;
269 return JZ_DMA_SIZE_128_BYTE;
273 static int jz4780_dma_setup_hwdesc(struct jz4780_dma_chan *jzchan,
274 struct jz4780_dma_hwdesc *desc, dma_addr_t addr, size_t len,
275 enum dma_transfer_direction direction)
277 struct dma_slave_config *config = &jzchan->config;
278 uint32_t width, maxburst, tsz;
280 if (direction == DMA_MEM_TO_DEV) {
281 desc->dcm = JZ_DMA_DCM_SAI;
283 desc->dta = config->dst_addr;
284 desc->drt = jzchan->transfer_type;
286 width = config->dst_addr_width;
287 maxburst = config->dst_maxburst;
289 desc->dcm = JZ_DMA_DCM_DAI;
290 desc->dsa = config->src_addr;
292 desc->drt = jzchan->transfer_type;
294 width = config->src_addr_width;
295 maxburst = config->src_maxburst;
299 * This calculates the maximum transfer size that can be used with the
300 * given address, length, width and maximum burst size. The address
301 * must be aligned to the transfer size, the total length must be
302 * divisible by the transfer size, and we must not use more than the
303 * maximum burst specified by the user.
305 tsz = jz4780_dma_transfer_size(addr | len | (width * maxburst),
306 &jzchan->transfer_shift);
309 case DMA_SLAVE_BUSWIDTH_1_BYTE:
310 case DMA_SLAVE_BUSWIDTH_2_BYTES:
312 case DMA_SLAVE_BUSWIDTH_4_BYTES:
313 width = JZ_DMA_WIDTH_32_BIT;
319 desc->dcm |= tsz << JZ_DMA_DCM_TSZ_SHIFT;
320 desc->dcm |= width << JZ_DMA_DCM_SP_SHIFT;
321 desc->dcm |= width << JZ_DMA_DCM_DP_SHIFT;
323 desc->dtc = len >> jzchan->transfer_shift;
327 static struct dma_async_tx_descriptor *jz4780_dma_prep_slave_sg(
328 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
329 enum dma_transfer_direction direction, unsigned long flags,
332 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
333 struct jz4780_dma_desc *desc;
337 desc = jz4780_dma_desc_alloc(jzchan, sg_len, DMA_SLAVE);
341 for (i = 0; i < sg_len; i++) {
342 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i],
343 sg_dma_address(&sgl[i]),
347 jz4780_dma_desc_free(&jzchan->desc->vdesc);
351 desc->desc[i].dcm |= JZ_DMA_DCM_TIE;
353 if (i != (sg_len - 1)) {
354 /* Automatically proceeed to the next descriptor. */
355 desc->desc[i].dcm |= JZ_DMA_DCM_LINK;
358 * The upper 8 bits of the DTC field in the descriptor
359 * must be set to (offset from descriptor base of next
363 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
367 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
370 static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic(
371 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
372 size_t period_len, enum dma_transfer_direction direction,
375 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
376 struct jz4780_dma_desc *desc;
377 unsigned int periods, i;
380 if (buf_len % period_len)
383 periods = buf_len / period_len;
385 desc = jz4780_dma_desc_alloc(jzchan, periods, DMA_CYCLIC);
389 for (i = 0; i < periods; i++) {
390 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i], buf_addr,
391 period_len, direction);
393 jz4780_dma_desc_free(&jzchan->desc->vdesc);
397 buf_addr += period_len;
400 * Set the link bit to indicate that the controller should
401 * automatically proceed to the next descriptor. In
402 * jz4780_dma_begin(), this will be cleared if we need to issue
403 * an interrupt after each period.
405 desc->desc[i].dcm |= JZ_DMA_DCM_TIE | JZ_DMA_DCM_LINK;
408 * The upper 8 bits of the DTC field in the descriptor must be
409 * set to (offset from descriptor base of next descriptor >> 4).
410 * If this is the last descriptor, link it back to the first,
411 * i.e. leave offset set to 0, otherwise point to the next one.
413 if (i != (periods - 1)) {
415 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
419 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
422 static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_memcpy(
423 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
424 size_t len, unsigned long flags)
426 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
427 struct jz4780_dma_desc *desc;
430 desc = jz4780_dma_desc_alloc(jzchan, 1, DMA_MEMCPY);
434 tsz = jz4780_dma_transfer_size(dest | src | len,
435 &jzchan->transfer_shift);
437 desc->desc[0].dsa = src;
438 desc->desc[0].dta = dest;
439 desc->desc[0].drt = JZ_DMA_DRT_AUTO;
440 desc->desc[0].dcm = JZ_DMA_DCM_TIE | JZ_DMA_DCM_SAI | JZ_DMA_DCM_DAI |
441 tsz << JZ_DMA_DCM_TSZ_SHIFT |
442 JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_SP_SHIFT |
443 JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_DP_SHIFT;
444 desc->desc[0].dtc = len >> jzchan->transfer_shift;
446 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
449 static void jz4780_dma_begin(struct jz4780_dma_chan *jzchan)
451 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
452 struct virt_dma_desc *vdesc;
454 dma_addr_t desc_phys;
457 vdesc = vchan_next_desc(&jzchan->vchan);
461 list_del(&vdesc->node);
463 jzchan->desc = to_jz4780_dma_desc(vdesc);
464 jzchan->curr_hwdesc = 0;
466 if (jzchan->desc->type == DMA_CYCLIC && vdesc->tx.callback) {
468 * The DMA controller doesn't support triggering an
469 * interrupt after processing each descriptor, only
470 * after processing an entire terminated list of
471 * descriptors. For a cyclic DMA setup the list of
472 * descriptors is not terminated so we can never get an
475 * If the user requested a callback for a cyclic DMA
476 * setup then we workaround this hardware limitation
477 * here by degrading to a set of unlinked descriptors
478 * which we will submit in sequence in response to the
479 * completion of processing the previous descriptor.
481 for (i = 0; i < jzchan->desc->count; i++)
482 jzchan->desc->desc[i].dcm &= ~JZ_DMA_DCM_LINK;
486 * There is an existing transfer, therefore this must be one
487 * for which we unlinked the descriptors above. Advance to the
488 * next one in the list.
490 jzchan->curr_hwdesc =
491 (jzchan->curr_hwdesc + 1) % jzchan->desc->count;
494 /* Use 8-word descriptors. */
495 jz4780_dma_chn_writel(jzdma, jzchan->id,
496 JZ_DMA_REG_DCS, JZ_DMA_DCS_DES8);
498 /* Write descriptor address and initiate descriptor fetch. */
499 desc_phys = jzchan->desc->desc_phys +
500 (jzchan->curr_hwdesc * sizeof(*jzchan->desc->desc));
501 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DDA, desc_phys);
502 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DDRS, BIT(jzchan->id));
504 /* Enable the channel. */
505 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS,
506 JZ_DMA_DCS_DES8 | JZ_DMA_DCS_CTE);
509 static void jz4780_dma_issue_pending(struct dma_chan *chan)
511 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
514 spin_lock_irqsave(&jzchan->vchan.lock, flags);
516 if (vchan_issue_pending(&jzchan->vchan) && !jzchan->desc)
517 jz4780_dma_begin(jzchan);
519 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
522 static int jz4780_dma_terminate_all(struct dma_chan *chan)
524 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
525 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
529 spin_lock_irqsave(&jzchan->vchan.lock, flags);
531 /* Clear the DMA status and stop the transfer. */
532 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
534 vchan_terminate_vdesc(&jzchan->desc->vdesc);
538 vchan_get_all_descriptors(&jzchan->vchan, &head);
540 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
542 vchan_dma_desc_free_list(&jzchan->vchan, &head);
546 static void jz4780_dma_synchronize(struct dma_chan *chan)
548 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
550 vchan_synchronize(&jzchan->vchan);
553 static int jz4780_dma_config(struct dma_chan *chan,
554 struct dma_slave_config *config)
556 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
558 if ((config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
559 || (config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES))
562 /* Copy the reset of the slave configuration, it is used later. */
563 memcpy(&jzchan->config, config, sizeof(jzchan->config));
568 static size_t jz4780_dma_desc_residue(struct jz4780_dma_chan *jzchan,
569 struct jz4780_dma_desc *desc, unsigned int next_sg)
571 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
572 unsigned int residue, count;
577 for (i = next_sg; i < desc->count; i++)
578 residue += desc->desc[i].dtc << jzchan->transfer_shift;
581 count = jz4780_dma_chn_readl(jzdma, jzchan->id,
583 residue += count << jzchan->transfer_shift;
589 static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan,
590 dma_cookie_t cookie, struct dma_tx_state *txstate)
592 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
593 struct virt_dma_desc *vdesc;
594 enum dma_status status;
597 status = dma_cookie_status(chan, cookie, txstate);
598 if ((status == DMA_COMPLETE) || (txstate == NULL))
601 spin_lock_irqsave(&jzchan->vchan.lock, flags);
603 vdesc = vchan_find_desc(&jzchan->vchan, cookie);
605 /* On the issued list, so hasn't been processed yet */
606 txstate->residue = jz4780_dma_desc_residue(jzchan,
607 to_jz4780_dma_desc(vdesc), 0);
608 } else if (cookie == jzchan->desc->vdesc.tx.cookie) {
609 txstate->residue = jz4780_dma_desc_residue(jzchan, jzchan->desc,
610 (jzchan->curr_hwdesc + 1) % jzchan->desc->count);
612 txstate->residue = 0;
614 if (vdesc && jzchan->desc && vdesc == &jzchan->desc->vdesc
615 && jzchan->desc->status & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT))
618 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
622 static void jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma,
623 struct jz4780_dma_chan *jzchan)
627 spin_lock(&jzchan->vchan.lock);
629 dcs = jz4780_dma_chn_readl(jzdma, jzchan->id, JZ_DMA_REG_DCS);
630 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
632 if (dcs & JZ_DMA_DCS_AR) {
633 dev_warn(&jzchan->vchan.chan.dev->device,
634 "address error (DCS=0x%x)\n", dcs);
637 if (dcs & JZ_DMA_DCS_HLT) {
638 dev_warn(&jzchan->vchan.chan.dev->device,
639 "channel halt (DCS=0x%x)\n", dcs);
643 jzchan->desc->status = dcs;
645 if ((dcs & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT)) == 0) {
646 if (jzchan->desc->type == DMA_CYCLIC) {
647 vchan_cyclic_callback(&jzchan->desc->vdesc);
649 vchan_cookie_complete(&jzchan->desc->vdesc);
653 jz4780_dma_begin(jzchan);
656 dev_err(&jzchan->vchan.chan.dev->device,
657 "channel IRQ with no active transfer\n");
660 spin_unlock(&jzchan->vchan.lock);
663 static irqreturn_t jz4780_dma_irq_handler(int irq, void *data)
665 struct jz4780_dma_dev *jzdma = data;
666 uint32_t pending, dmac;
669 pending = jz4780_dma_ctrl_readl(jzdma, JZ_DMA_REG_DIRQP);
671 for (i = 0; i < jzdma->soc_data->nb_channels; i++) {
672 if (!(pending & (1<<i)))
675 jz4780_dma_chan_irq(jzdma, &jzdma->chan[i]);
678 /* Clear halt and address error status of all channels. */
679 dmac = jz4780_dma_ctrl_readl(jzdma, JZ_DMA_REG_DMAC);
680 dmac &= ~(JZ_DMA_DMAC_HLT | JZ_DMA_DMAC_AR);
681 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMAC, dmac);
683 /* Clear interrupt pending status. */
684 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DIRQP, 0);
689 static int jz4780_dma_alloc_chan_resources(struct dma_chan *chan)
691 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
693 jzchan->desc_pool = dma_pool_create(dev_name(&chan->dev->device),
695 JZ_DMA_DESC_BLOCK_SIZE,
697 if (!jzchan->desc_pool) {
698 dev_err(&chan->dev->device,
699 "failed to allocate descriptor pool\n");
706 static void jz4780_dma_free_chan_resources(struct dma_chan *chan)
708 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
710 vchan_free_chan_resources(&jzchan->vchan);
711 dma_pool_destroy(jzchan->desc_pool);
712 jzchan->desc_pool = NULL;
715 static bool jz4780_dma_filter_fn(struct dma_chan *chan, void *param)
717 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
718 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
719 struct jz4780_dma_filter_data *data = param;
721 if (jzdma->dma_device.dev->of_node != data->of_node)
724 if (data->channel > -1) {
725 if (data->channel != jzchan->id)
727 } else if (jzdma->chan_reserved & BIT(jzchan->id)) {
731 jzchan->transfer_type = data->transfer_type;
736 static struct dma_chan *jz4780_of_dma_xlate(struct of_phandle_args *dma_spec,
737 struct of_dma *ofdma)
739 struct jz4780_dma_dev *jzdma = ofdma->of_dma_data;
740 dma_cap_mask_t mask = jzdma->dma_device.cap_mask;
741 struct jz4780_dma_filter_data data;
743 if (dma_spec->args_count != 2)
746 data.of_node = ofdma->of_node;
747 data.transfer_type = dma_spec->args[0];
748 data.channel = dma_spec->args[1];
750 if (data.channel > -1) {
751 if (data.channel >= jzdma->soc_data->nb_channels) {
752 dev_err(jzdma->dma_device.dev,
753 "device requested non-existent channel %u\n",
758 /* Can only select a channel marked as reserved. */
759 if (!(jzdma->chan_reserved & BIT(data.channel))) {
760 dev_err(jzdma->dma_device.dev,
761 "device requested unreserved channel %u\n",
766 jzdma->chan[data.channel].transfer_type = data.transfer_type;
768 return dma_get_slave_channel(
769 &jzdma->chan[data.channel].vchan.chan);
771 return dma_request_channel(mask, jz4780_dma_filter_fn, &data);
775 static int jz4780_dma_probe(struct platform_device *pdev)
777 struct device *dev = &pdev->dev;
778 const struct jz4780_dma_soc_data *soc_data;
779 struct jz4780_dma_dev *jzdma;
780 struct jz4780_dma_chan *jzchan;
781 struct dma_device *dd;
782 struct resource *res;
786 dev_err(dev, "This driver must be probed from devicetree\n");
790 soc_data = device_get_match_data(dev);
794 jzdma = devm_kzalloc(dev, sizeof(*jzdma)
795 + sizeof(*jzdma->chan) * soc_data->nb_channels,
800 jzdma->soc_data = soc_data;
801 platform_set_drvdata(pdev, jzdma);
803 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
805 dev_err(dev, "failed to get I/O memory\n");
809 jzdma->chn_base = devm_ioremap_resource(dev, res);
810 if (IS_ERR(jzdma->chn_base))
811 return PTR_ERR(jzdma->chn_base);
813 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
815 jzdma->ctrl_base = devm_ioremap_resource(dev, res);
816 if (IS_ERR(jzdma->ctrl_base))
817 return PTR_ERR(jzdma->ctrl_base);
820 * On JZ4780, if the second memory resource was not supplied,
821 * assume we're using an old devicetree, and calculate the
822 * offset to the control registers.
824 jzdma->ctrl_base = jzdma->chn_base + JZ4780_DMA_CTRL_OFFSET;
827 ret = platform_get_irq(pdev, 0);
829 dev_err(dev, "failed to get IRQ: %d\n", ret);
835 ret = request_irq(jzdma->irq, jz4780_dma_irq_handler, 0, dev_name(dev),
838 dev_err(dev, "failed to request IRQ %u!\n", jzdma->irq);
842 jzdma->clk = devm_clk_get(dev, NULL);
843 if (IS_ERR(jzdma->clk)) {
844 dev_err(dev, "failed to get clock\n");
845 ret = PTR_ERR(jzdma->clk);
849 clk_prepare_enable(jzdma->clk);
851 /* Property is optional, if it doesn't exist the value will remain 0. */
852 of_property_read_u32_index(dev->of_node, "ingenic,reserved-channels",
853 0, &jzdma->chan_reserved);
855 dd = &jzdma->dma_device;
857 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
858 dma_cap_set(DMA_SLAVE, dd->cap_mask);
859 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
862 dd->copy_align = DMAENGINE_ALIGN_4_BYTES;
863 dd->device_alloc_chan_resources = jz4780_dma_alloc_chan_resources;
864 dd->device_free_chan_resources = jz4780_dma_free_chan_resources;
865 dd->device_prep_slave_sg = jz4780_dma_prep_slave_sg;
866 dd->device_prep_dma_cyclic = jz4780_dma_prep_dma_cyclic;
867 dd->device_prep_dma_memcpy = jz4780_dma_prep_dma_memcpy;
868 dd->device_config = jz4780_dma_config;
869 dd->device_terminate_all = jz4780_dma_terminate_all;
870 dd->device_synchronize = jz4780_dma_synchronize;
871 dd->device_tx_status = jz4780_dma_tx_status;
872 dd->device_issue_pending = jz4780_dma_issue_pending;
873 dd->src_addr_widths = JZ_DMA_BUSWIDTHS;
874 dd->dst_addr_widths = JZ_DMA_BUSWIDTHS;
875 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
876 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
879 * Enable DMA controller, mark all channels as not programmable.
880 * Also set the FMSC bit - it increases MSC performance, so it makes
881 * little sense not to enable it.
883 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMAC,
884 JZ_DMA_DMAC_DMAE | JZ_DMA_DMAC_FMSC);
885 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMACP, 0);
887 INIT_LIST_HEAD(&dd->channels);
889 for (i = 0; i < soc_data->nb_channels; i++) {
890 jzchan = &jzdma->chan[i];
893 vchan_init(&jzchan->vchan, dd);
894 jzchan->vchan.desc_free = jz4780_dma_desc_free;
897 ret = dma_async_device_register(dd);
899 dev_err(dev, "failed to register device\n");
900 goto err_disable_clk;
903 /* Register with OF DMA helpers. */
904 ret = of_dma_controller_register(dev->of_node, jz4780_of_dma_xlate,
907 dev_err(dev, "failed to register OF DMA controller\n");
908 goto err_unregister_dev;
911 dev_info(dev, "JZ4780 DMA controller initialised\n");
915 dma_async_device_unregister(dd);
918 clk_disable_unprepare(jzdma->clk);
921 free_irq(jzdma->irq, jzdma);
925 static int jz4780_dma_remove(struct platform_device *pdev)
927 struct jz4780_dma_dev *jzdma = platform_get_drvdata(pdev);
930 of_dma_controller_free(pdev->dev.of_node);
932 free_irq(jzdma->irq, jzdma);
934 for (i = 0; i < jzdma->soc_data->nb_channels; i++)
935 tasklet_kill(&jzdma->chan[i].vchan.task);
937 dma_async_device_unregister(&jzdma->dma_device);
941 static const struct jz4780_dma_soc_data jz4780_dma_soc_data = {
945 static const struct of_device_id jz4780_dma_dt_match[] = {
946 { .compatible = "ingenic,jz4780-dma", .data = &jz4780_dma_soc_data },
949 MODULE_DEVICE_TABLE(of, jz4780_dma_dt_match);
951 static struct platform_driver jz4780_dma_driver = {
952 .probe = jz4780_dma_probe,
953 .remove = jz4780_dma_remove,
955 .name = "jz4780-dma",
956 .of_match_table = of_match_ptr(jz4780_dma_dt_match),
960 static int __init jz4780_dma_init(void)
962 return platform_driver_register(&jz4780_dma_driver);
964 subsys_initcall(jz4780_dma_init);
966 static void __exit jz4780_dma_exit(void)
968 platform_driver_unregister(&jz4780_dma_driver);
970 module_exit(jz4780_dma_exit);
972 MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
973 MODULE_DESCRIPTION("Ingenic JZ4780 DMA controller driver");
974 MODULE_LICENSE("GPL");