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_dma.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
23 #include "dmaengine.h"
26 #define JZ_DMA_NR_CHANNELS 32
28 /* Global registers. */
29 #define JZ_DMA_REG_DMAC 0x1000
30 #define JZ_DMA_REG_DIRQP 0x1004
31 #define JZ_DMA_REG_DDR 0x1008
32 #define JZ_DMA_REG_DDRS 0x100c
33 #define JZ_DMA_REG_DMACP 0x101c
34 #define JZ_DMA_REG_DSIRQP 0x1020
35 #define JZ_DMA_REG_DSIRQM 0x1024
36 #define JZ_DMA_REG_DCIRQP 0x1028
37 #define JZ_DMA_REG_DCIRQM 0x102c
39 /* Per-channel registers. */
40 #define JZ_DMA_REG_CHAN(n) (n * 0x20)
41 #define JZ_DMA_REG_DSA(n) (0x00 + JZ_DMA_REG_CHAN(n))
42 #define JZ_DMA_REG_DTA(n) (0x04 + JZ_DMA_REG_CHAN(n))
43 #define JZ_DMA_REG_DTC(n) (0x08 + JZ_DMA_REG_CHAN(n))
44 #define JZ_DMA_REG_DRT(n) (0x0c + JZ_DMA_REG_CHAN(n))
45 #define JZ_DMA_REG_DCS(n) (0x10 + JZ_DMA_REG_CHAN(n))
46 #define JZ_DMA_REG_DCM(n) (0x14 + JZ_DMA_REG_CHAN(n))
47 #define JZ_DMA_REG_DDA(n) (0x18 + JZ_DMA_REG_CHAN(n))
48 #define JZ_DMA_REG_DSD(n) (0x1c + JZ_DMA_REG_CHAN(n))
50 #define JZ_DMA_DMAC_DMAE BIT(0)
51 #define JZ_DMA_DMAC_AR BIT(2)
52 #define JZ_DMA_DMAC_HLT BIT(3)
53 #define JZ_DMA_DMAC_FMSC BIT(31)
55 #define JZ_DMA_DRT_AUTO 0x8
57 #define JZ_DMA_DCS_CTE BIT(0)
58 #define JZ_DMA_DCS_HLT BIT(2)
59 #define JZ_DMA_DCS_TT BIT(3)
60 #define JZ_DMA_DCS_AR BIT(4)
61 #define JZ_DMA_DCS_DES8 BIT(30)
63 #define JZ_DMA_DCM_LINK BIT(0)
64 #define JZ_DMA_DCM_TIE BIT(1)
65 #define JZ_DMA_DCM_STDE BIT(2)
66 #define JZ_DMA_DCM_TSZ_SHIFT 8
67 #define JZ_DMA_DCM_TSZ_MASK (0x7 << JZ_DMA_DCM_TSZ_SHIFT)
68 #define JZ_DMA_DCM_DP_SHIFT 12
69 #define JZ_DMA_DCM_SP_SHIFT 14
70 #define JZ_DMA_DCM_DAI BIT(22)
71 #define JZ_DMA_DCM_SAI BIT(23)
73 #define JZ_DMA_SIZE_4_BYTE 0x0
74 #define JZ_DMA_SIZE_1_BYTE 0x1
75 #define JZ_DMA_SIZE_2_BYTE 0x2
76 #define JZ_DMA_SIZE_16_BYTE 0x3
77 #define JZ_DMA_SIZE_32_BYTE 0x4
78 #define JZ_DMA_SIZE_64_BYTE 0x5
79 #define JZ_DMA_SIZE_128_BYTE 0x6
81 #define JZ_DMA_WIDTH_32_BIT 0x0
82 #define JZ_DMA_WIDTH_8_BIT 0x1
83 #define JZ_DMA_WIDTH_16_BIT 0x2
85 #define JZ_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
86 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
87 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
90 * struct jz4780_dma_hwdesc - descriptor structure read by the DMA controller.
91 * @dcm: value for the DCM (channel command) register
92 * @dsa: source address
93 * @dta: target address
94 * @dtc: transfer count (number of blocks of the transfer size specified in DCM
95 * to transfer) in the low 24 bits, offset of the next descriptor from the
96 * descriptor base address in the upper 8 bits.
97 * @sd: target/source stride difference (in stride transfer mode).
100 struct jz4780_dma_hwdesc {
107 uint32_t reserved[2];
110 /* Size of allocations for hardware descriptor blocks. */
111 #define JZ_DMA_DESC_BLOCK_SIZE PAGE_SIZE
112 #define JZ_DMA_MAX_DESC \
113 (JZ_DMA_DESC_BLOCK_SIZE / sizeof(struct jz4780_dma_hwdesc))
115 struct jz4780_dma_desc {
116 struct virt_dma_desc vdesc;
118 struct jz4780_dma_hwdesc *desc;
119 dma_addr_t desc_phys;
121 enum dma_transaction_type type;
125 struct jz4780_dma_chan {
126 struct virt_dma_chan vchan;
128 struct dma_pool *desc_pool;
130 uint32_t transfer_type;
131 uint32_t transfer_shift;
132 struct dma_slave_config config;
134 struct jz4780_dma_desc *desc;
135 unsigned int curr_hwdesc;
138 struct jz4780_dma_dev {
139 struct dma_device dma_device;
144 uint32_t chan_reserved;
145 struct jz4780_dma_chan chan[JZ_DMA_NR_CHANNELS];
148 struct jz4780_dma_filter_data {
149 struct device_node *of_node;
150 uint32_t transfer_type;
154 static inline struct jz4780_dma_chan *to_jz4780_dma_chan(struct dma_chan *chan)
156 return container_of(chan, struct jz4780_dma_chan, vchan.chan);
159 static inline struct jz4780_dma_desc *to_jz4780_dma_desc(
160 struct virt_dma_desc *vdesc)
162 return container_of(vdesc, struct jz4780_dma_desc, vdesc);
165 static inline struct jz4780_dma_dev *jz4780_dma_chan_parent(
166 struct jz4780_dma_chan *jzchan)
168 return container_of(jzchan->vchan.chan.device, struct jz4780_dma_dev,
172 static inline uint32_t jz4780_dma_readl(struct jz4780_dma_dev *jzdma,
175 return readl(jzdma->base + reg);
178 static inline void jz4780_dma_writel(struct jz4780_dma_dev *jzdma,
179 unsigned int reg, uint32_t val)
181 writel(val, jzdma->base + reg);
184 static struct jz4780_dma_desc *jz4780_dma_desc_alloc(
185 struct jz4780_dma_chan *jzchan, unsigned int count,
186 enum dma_transaction_type type)
188 struct jz4780_dma_desc *desc;
190 if (count > JZ_DMA_MAX_DESC)
193 desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
197 desc->desc = dma_pool_alloc(jzchan->desc_pool, GFP_NOWAIT,
209 static void jz4780_dma_desc_free(struct virt_dma_desc *vdesc)
211 struct jz4780_dma_desc *desc = to_jz4780_dma_desc(vdesc);
212 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(vdesc->tx.chan);
214 dma_pool_free(jzchan->desc_pool, desc->desc, desc->desc_phys);
218 static uint32_t jz4780_dma_transfer_size(unsigned long val, uint32_t *shift)
220 int ord = ffs(val) - 1;
223 * 8 byte transfer sizes unsupported so fall back on 4. If it's larger
224 * than the maximum, just limit it. It is perfectly safe to fall back
225 * in this way since we won't exceed the maximum burst size supported
226 * by the device, the only effect is reduced efficiency. This is better
227 * than refusing to perform the request at all.
238 return JZ_DMA_SIZE_1_BYTE;
240 return JZ_DMA_SIZE_2_BYTE;
242 return JZ_DMA_SIZE_4_BYTE;
244 return JZ_DMA_SIZE_16_BYTE;
246 return JZ_DMA_SIZE_32_BYTE;
248 return JZ_DMA_SIZE_64_BYTE;
250 return JZ_DMA_SIZE_128_BYTE;
254 static int jz4780_dma_setup_hwdesc(struct jz4780_dma_chan *jzchan,
255 struct jz4780_dma_hwdesc *desc, dma_addr_t addr, size_t len,
256 enum dma_transfer_direction direction)
258 struct dma_slave_config *config = &jzchan->config;
259 uint32_t width, maxburst, tsz;
261 if (direction == DMA_MEM_TO_DEV) {
262 desc->dcm = JZ_DMA_DCM_SAI;
264 desc->dta = config->dst_addr;
265 desc->drt = jzchan->transfer_type;
267 width = config->dst_addr_width;
268 maxburst = config->dst_maxburst;
270 desc->dcm = JZ_DMA_DCM_DAI;
271 desc->dsa = config->src_addr;
273 desc->drt = jzchan->transfer_type;
275 width = config->src_addr_width;
276 maxburst = config->src_maxburst;
280 * This calculates the maximum transfer size that can be used with the
281 * given address, length, width and maximum burst size. The address
282 * must be aligned to the transfer size, the total length must be
283 * divisible by the transfer size, and we must not use more than the
284 * maximum burst specified by the user.
286 tsz = jz4780_dma_transfer_size(addr | len | (width * maxburst),
287 &jzchan->transfer_shift);
290 case DMA_SLAVE_BUSWIDTH_1_BYTE:
291 case DMA_SLAVE_BUSWIDTH_2_BYTES:
293 case DMA_SLAVE_BUSWIDTH_4_BYTES:
294 width = JZ_DMA_WIDTH_32_BIT;
300 desc->dcm |= tsz << JZ_DMA_DCM_TSZ_SHIFT;
301 desc->dcm |= width << JZ_DMA_DCM_SP_SHIFT;
302 desc->dcm |= width << JZ_DMA_DCM_DP_SHIFT;
304 desc->dtc = len >> jzchan->transfer_shift;
308 static struct dma_async_tx_descriptor *jz4780_dma_prep_slave_sg(
309 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
310 enum dma_transfer_direction direction, unsigned long flags,
313 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
314 struct jz4780_dma_desc *desc;
318 desc = jz4780_dma_desc_alloc(jzchan, sg_len, DMA_SLAVE);
322 for (i = 0; i < sg_len; i++) {
323 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i],
324 sg_dma_address(&sgl[i]),
330 desc->desc[i].dcm |= JZ_DMA_DCM_TIE;
332 if (i != (sg_len - 1)) {
333 /* Automatically proceeed to the next descriptor. */
334 desc->desc[i].dcm |= JZ_DMA_DCM_LINK;
337 * The upper 8 bits of the DTC field in the descriptor
338 * must be set to (offset from descriptor base of next
342 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
346 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
349 static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic(
350 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
351 size_t period_len, enum dma_transfer_direction direction,
354 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
355 struct jz4780_dma_desc *desc;
356 unsigned int periods, i;
359 if (buf_len % period_len)
362 periods = buf_len / period_len;
364 desc = jz4780_dma_desc_alloc(jzchan, periods, DMA_CYCLIC);
368 for (i = 0; i < periods; i++) {
369 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i], buf_addr,
370 period_len, direction);
374 buf_addr += period_len;
377 * Set the link bit to indicate that the controller should
378 * automatically proceed to the next descriptor. In
379 * jz4780_dma_begin(), this will be cleared if we need to issue
380 * an interrupt after each period.
382 desc->desc[i].dcm |= JZ_DMA_DCM_TIE | JZ_DMA_DCM_LINK;
385 * The upper 8 bits of the DTC field in the descriptor must be
386 * set to (offset from descriptor base of next descriptor >> 4).
387 * If this is the last descriptor, link it back to the first,
388 * i.e. leave offset set to 0, otherwise point to the next one.
390 if (i != (periods - 1)) {
392 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
396 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
399 struct dma_async_tx_descriptor *jz4780_dma_prep_dma_memcpy(
400 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
401 size_t len, unsigned long flags)
403 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
404 struct jz4780_dma_desc *desc;
407 desc = jz4780_dma_desc_alloc(jzchan, 1, DMA_MEMCPY);
411 tsz = jz4780_dma_transfer_size(dest | src | len,
412 &jzchan->transfer_shift);
414 desc->desc[0].dsa = src;
415 desc->desc[0].dta = dest;
416 desc->desc[0].drt = JZ_DMA_DRT_AUTO;
417 desc->desc[0].dcm = JZ_DMA_DCM_TIE | JZ_DMA_DCM_SAI | JZ_DMA_DCM_DAI |
418 tsz << JZ_DMA_DCM_TSZ_SHIFT |
419 JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_SP_SHIFT |
420 JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_DP_SHIFT;
421 desc->desc[0].dtc = len >> jzchan->transfer_shift;
423 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
426 static void jz4780_dma_begin(struct jz4780_dma_chan *jzchan)
428 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
429 struct virt_dma_desc *vdesc;
431 dma_addr_t desc_phys;
434 vdesc = vchan_next_desc(&jzchan->vchan);
438 list_del(&vdesc->node);
440 jzchan->desc = to_jz4780_dma_desc(vdesc);
441 jzchan->curr_hwdesc = 0;
443 if (jzchan->desc->type == DMA_CYCLIC && vdesc->tx.callback) {
445 * The DMA controller doesn't support triggering an
446 * interrupt after processing each descriptor, only
447 * after processing an entire terminated list of
448 * descriptors. For a cyclic DMA setup the list of
449 * descriptors is not terminated so we can never get an
452 * If the user requested a callback for a cyclic DMA
453 * setup then we workaround this hardware limitation
454 * here by degrading to a set of unlinked descriptors
455 * which we will submit in sequence in response to the
456 * completion of processing the previous descriptor.
458 for (i = 0; i < jzchan->desc->count; i++)
459 jzchan->desc->desc[i].dcm &= ~JZ_DMA_DCM_LINK;
463 * There is an existing transfer, therefore this must be one
464 * for which we unlinked the descriptors above. Advance to the
465 * next one in the list.
467 jzchan->curr_hwdesc =
468 (jzchan->curr_hwdesc + 1) % jzchan->desc->count;
471 /* Use 8-word descriptors. */
472 jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), JZ_DMA_DCS_DES8);
474 /* Write descriptor address and initiate descriptor fetch. */
475 desc_phys = jzchan->desc->desc_phys +
476 (jzchan->curr_hwdesc * sizeof(*jzchan->desc->desc));
477 jz4780_dma_writel(jzdma, JZ_DMA_REG_DDA(jzchan->id), desc_phys);
478 jz4780_dma_writel(jzdma, JZ_DMA_REG_DDRS, BIT(jzchan->id));
480 /* Enable the channel. */
481 jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id),
482 JZ_DMA_DCS_DES8 | JZ_DMA_DCS_CTE);
485 static void jz4780_dma_issue_pending(struct dma_chan *chan)
487 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
490 spin_lock_irqsave(&jzchan->vchan.lock, flags);
492 if (vchan_issue_pending(&jzchan->vchan) && !jzchan->desc)
493 jz4780_dma_begin(jzchan);
495 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
498 static int jz4780_dma_terminate_all(struct dma_chan *chan)
500 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
501 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
505 spin_lock_irqsave(&jzchan->vchan.lock, flags);
507 /* Clear the DMA status and stop the transfer. */
508 jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), 0);
510 jz4780_dma_desc_free(&jzchan->desc->vdesc);
514 vchan_get_all_descriptors(&jzchan->vchan, &head);
516 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
518 vchan_dma_desc_free_list(&jzchan->vchan, &head);
522 static int jz4780_dma_config(struct dma_chan *chan,
523 struct dma_slave_config *config)
525 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
527 if ((config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
528 || (config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES))
531 /* Copy the reset of the slave configuration, it is used later. */
532 memcpy(&jzchan->config, config, sizeof(jzchan->config));
537 static size_t jz4780_dma_desc_residue(struct jz4780_dma_chan *jzchan,
538 struct jz4780_dma_desc *desc, unsigned int next_sg)
540 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
541 unsigned int residue, count;
546 for (i = next_sg; i < desc->count; i++)
547 residue += desc->desc[i].dtc << jzchan->transfer_shift;
550 count = jz4780_dma_readl(jzdma,
551 JZ_DMA_REG_DTC(jzchan->id));
552 residue += count << jzchan->transfer_shift;
558 static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan,
559 dma_cookie_t cookie, struct dma_tx_state *txstate)
561 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
562 struct virt_dma_desc *vdesc;
563 enum dma_status status;
566 status = dma_cookie_status(chan, cookie, txstate);
567 if ((status == DMA_COMPLETE) || (txstate == NULL))
570 spin_lock_irqsave(&jzchan->vchan.lock, flags);
572 vdesc = vchan_find_desc(&jzchan->vchan, cookie);
574 /* On the issued list, so hasn't been processed yet */
575 txstate->residue = jz4780_dma_desc_residue(jzchan,
576 to_jz4780_dma_desc(vdesc), 0);
577 } else if (cookie == jzchan->desc->vdesc.tx.cookie) {
578 txstate->residue = jz4780_dma_desc_residue(jzchan, jzchan->desc,
579 (jzchan->curr_hwdesc + 1) % jzchan->desc->count);
581 txstate->residue = 0;
583 if (vdesc && jzchan->desc && vdesc == &jzchan->desc->vdesc
584 && jzchan->desc->status & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT))
587 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
591 static void jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma,
592 struct jz4780_dma_chan *jzchan)
596 spin_lock(&jzchan->vchan.lock);
598 dcs = jz4780_dma_readl(jzdma, JZ_DMA_REG_DCS(jzchan->id));
599 jz4780_dma_writel(jzdma, JZ_DMA_REG_DCS(jzchan->id), 0);
601 if (dcs & JZ_DMA_DCS_AR) {
602 dev_warn(&jzchan->vchan.chan.dev->device,
603 "address error (DCS=0x%x)\n", dcs);
606 if (dcs & JZ_DMA_DCS_HLT) {
607 dev_warn(&jzchan->vchan.chan.dev->device,
608 "channel halt (DCS=0x%x)\n", dcs);
612 jzchan->desc->status = dcs;
614 if ((dcs & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT)) == 0) {
615 if (jzchan->desc->type == DMA_CYCLIC) {
616 vchan_cyclic_callback(&jzchan->desc->vdesc);
618 vchan_cookie_complete(&jzchan->desc->vdesc);
622 jz4780_dma_begin(jzchan);
625 dev_err(&jzchan->vchan.chan.dev->device,
626 "channel IRQ with no active transfer\n");
629 spin_unlock(&jzchan->vchan.lock);
632 static irqreturn_t jz4780_dma_irq_handler(int irq, void *data)
634 struct jz4780_dma_dev *jzdma = data;
635 uint32_t pending, dmac;
638 pending = jz4780_dma_readl(jzdma, JZ_DMA_REG_DIRQP);
640 for (i = 0; i < JZ_DMA_NR_CHANNELS; i++) {
641 if (!(pending & (1<<i)))
644 jz4780_dma_chan_irq(jzdma, &jzdma->chan[i]);
647 /* Clear halt and address error status of all channels. */
648 dmac = jz4780_dma_readl(jzdma, JZ_DMA_REG_DMAC);
649 dmac &= ~(JZ_DMA_DMAC_HLT | JZ_DMA_DMAC_AR);
650 jz4780_dma_writel(jzdma, JZ_DMA_REG_DMAC, dmac);
652 /* Clear interrupt pending status. */
653 jz4780_dma_writel(jzdma, JZ_DMA_REG_DIRQP, 0);
658 static int jz4780_dma_alloc_chan_resources(struct dma_chan *chan)
660 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
662 jzchan->desc_pool = dma_pool_create(dev_name(&chan->dev->device),
664 JZ_DMA_DESC_BLOCK_SIZE,
666 if (!jzchan->desc_pool) {
667 dev_err(&chan->dev->device,
668 "failed to allocate descriptor pool\n");
675 static void jz4780_dma_free_chan_resources(struct dma_chan *chan)
677 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
679 vchan_free_chan_resources(&jzchan->vchan);
680 dma_pool_destroy(jzchan->desc_pool);
681 jzchan->desc_pool = NULL;
684 static bool jz4780_dma_filter_fn(struct dma_chan *chan, void *param)
686 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
687 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
688 struct jz4780_dma_filter_data *data = param;
690 if (jzdma->dma_device.dev->of_node != data->of_node)
693 if (data->channel > -1) {
694 if (data->channel != jzchan->id)
696 } else if (jzdma->chan_reserved & BIT(jzchan->id)) {
700 jzchan->transfer_type = data->transfer_type;
705 static struct dma_chan *jz4780_of_dma_xlate(struct of_phandle_args *dma_spec,
706 struct of_dma *ofdma)
708 struct jz4780_dma_dev *jzdma = ofdma->of_dma_data;
709 dma_cap_mask_t mask = jzdma->dma_device.cap_mask;
710 struct jz4780_dma_filter_data data;
712 if (dma_spec->args_count != 2)
715 data.of_node = ofdma->of_node;
716 data.transfer_type = dma_spec->args[0];
717 data.channel = dma_spec->args[1];
719 if (data.channel > -1) {
720 if (data.channel >= JZ_DMA_NR_CHANNELS) {
721 dev_err(jzdma->dma_device.dev,
722 "device requested non-existent channel %u\n",
727 /* Can only select a channel marked as reserved. */
728 if (!(jzdma->chan_reserved & BIT(data.channel))) {
729 dev_err(jzdma->dma_device.dev,
730 "device requested unreserved channel %u\n",
735 jzdma->chan[data.channel].transfer_type = data.transfer_type;
737 return dma_get_slave_channel(
738 &jzdma->chan[data.channel].vchan.chan);
740 return dma_request_channel(mask, jz4780_dma_filter_fn, &data);
744 static int jz4780_dma_probe(struct platform_device *pdev)
746 struct device *dev = &pdev->dev;
747 struct jz4780_dma_dev *jzdma;
748 struct jz4780_dma_chan *jzchan;
749 struct dma_device *dd;
750 struct resource *res;
753 jzdma = devm_kzalloc(dev, sizeof(*jzdma), GFP_KERNEL);
757 platform_set_drvdata(pdev, jzdma);
759 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
761 dev_err(dev, "failed to get I/O memory\n");
765 jzdma->base = devm_ioremap_resource(dev, res);
766 if (IS_ERR(jzdma->base))
767 return PTR_ERR(jzdma->base);
769 ret = platform_get_irq(pdev, 0);
771 dev_err(dev, "failed to get IRQ: %d\n", ret);
777 ret = request_irq(jzdma->irq, jz4780_dma_irq_handler, 0, dev_name(dev),
780 dev_err(dev, "failed to request IRQ %u!\n", jzdma->irq);
784 jzdma->clk = devm_clk_get(dev, NULL);
785 if (IS_ERR(jzdma->clk)) {
786 dev_err(dev, "failed to get clock\n");
787 ret = PTR_ERR(jzdma->clk);
791 clk_prepare_enable(jzdma->clk);
793 /* Property is optional, if it doesn't exist the value will remain 0. */
794 of_property_read_u32_index(dev->of_node, "ingenic,reserved-channels",
795 0, &jzdma->chan_reserved);
797 dd = &jzdma->dma_device;
799 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
800 dma_cap_set(DMA_SLAVE, dd->cap_mask);
801 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
804 dd->copy_align = DMAENGINE_ALIGN_4_BYTES;
805 dd->device_alloc_chan_resources = jz4780_dma_alloc_chan_resources;
806 dd->device_free_chan_resources = jz4780_dma_free_chan_resources;
807 dd->device_prep_slave_sg = jz4780_dma_prep_slave_sg;
808 dd->device_prep_dma_cyclic = jz4780_dma_prep_dma_cyclic;
809 dd->device_prep_dma_memcpy = jz4780_dma_prep_dma_memcpy;
810 dd->device_config = jz4780_dma_config;
811 dd->device_terminate_all = jz4780_dma_terminate_all;
812 dd->device_tx_status = jz4780_dma_tx_status;
813 dd->device_issue_pending = jz4780_dma_issue_pending;
814 dd->src_addr_widths = JZ_DMA_BUSWIDTHS;
815 dd->dst_addr_widths = JZ_DMA_BUSWIDTHS;
816 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
817 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
820 * Enable DMA controller, mark all channels as not programmable.
821 * Also set the FMSC bit - it increases MSC performance, so it makes
822 * little sense not to enable it.
824 jz4780_dma_writel(jzdma, JZ_DMA_REG_DMAC,
825 JZ_DMA_DMAC_DMAE | JZ_DMA_DMAC_FMSC);
826 jz4780_dma_writel(jzdma, JZ_DMA_REG_DMACP, 0);
828 INIT_LIST_HEAD(&dd->channels);
830 for (i = 0; i < JZ_DMA_NR_CHANNELS; i++) {
831 jzchan = &jzdma->chan[i];
834 vchan_init(&jzchan->vchan, dd);
835 jzchan->vchan.desc_free = jz4780_dma_desc_free;
838 ret = dma_async_device_register(dd);
840 dev_err(dev, "failed to register device\n");
841 goto err_disable_clk;
844 /* Register with OF DMA helpers. */
845 ret = of_dma_controller_register(dev->of_node, jz4780_of_dma_xlate,
848 dev_err(dev, "failed to register OF DMA controller\n");
849 goto err_unregister_dev;
852 dev_info(dev, "JZ4780 DMA controller initialised\n");
856 dma_async_device_unregister(dd);
859 clk_disable_unprepare(jzdma->clk);
862 free_irq(jzdma->irq, jzdma);
866 static int jz4780_dma_remove(struct platform_device *pdev)
868 struct jz4780_dma_dev *jzdma = platform_get_drvdata(pdev);
871 of_dma_controller_free(pdev->dev.of_node);
873 free_irq(jzdma->irq, jzdma);
875 for (i = 0; i < JZ_DMA_NR_CHANNELS; i++)
876 tasklet_kill(&jzdma->chan[i].vchan.task);
878 dma_async_device_unregister(&jzdma->dma_device);
882 static const struct of_device_id jz4780_dma_dt_match[] = {
883 { .compatible = "ingenic,jz4780-dma", .data = NULL },
886 MODULE_DEVICE_TABLE(of, jz4780_dma_dt_match);
888 static struct platform_driver jz4780_dma_driver = {
889 .probe = jz4780_dma_probe,
890 .remove = jz4780_dma_remove,
892 .name = "jz4780-dma",
893 .of_match_table = of_match_ptr(jz4780_dma_dt_match),
897 static int __init jz4780_dma_init(void)
899 return platform_driver_register(&jz4780_dma_driver);
901 subsys_initcall(jz4780_dma_init);
903 static void __exit jz4780_dma_exit(void)
905 platform_driver_unregister(&jz4780_dma_driver);
907 module_exit(jz4780_dma_exit);
909 MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
910 MODULE_DESCRIPTION("Ingenic JZ4780 DMA controller driver");
911 MODULE_LICENSE("GPL");