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_DCKE 0x10
33 #define JZ_DMA_REG_DCKES 0x14
34 #define JZ_DMA_REG_DCKEC 0x18
35 #define JZ_DMA_REG_DMACP 0x1c
36 #define JZ_DMA_REG_DSIRQP 0x20
37 #define JZ_DMA_REG_DSIRQM 0x24
38 #define JZ_DMA_REG_DCIRQP 0x28
39 #define JZ_DMA_REG_DCIRQM 0x2c
41 /* Per-channel registers. */
42 #define JZ_DMA_REG_CHAN(n) (n * 0x20)
43 #define JZ_DMA_REG_DSA 0x00
44 #define JZ_DMA_REG_DTA 0x04
45 #define JZ_DMA_REG_DTC 0x08
46 #define JZ_DMA_REG_DRT 0x0c
47 #define JZ_DMA_REG_DCS 0x10
48 #define JZ_DMA_REG_DCM 0x14
49 #define JZ_DMA_REG_DDA 0x18
50 #define JZ_DMA_REG_DSD 0x1c
52 #define JZ_DMA_DMAC_DMAE BIT(0)
53 #define JZ_DMA_DMAC_AR BIT(2)
54 #define JZ_DMA_DMAC_HLT BIT(3)
55 #define JZ_DMA_DMAC_FMSC BIT(31)
57 #define JZ_DMA_DRT_AUTO 0x8
59 #define JZ_DMA_DCS_CTE BIT(0)
60 #define JZ_DMA_DCS_HLT BIT(2)
61 #define JZ_DMA_DCS_TT BIT(3)
62 #define JZ_DMA_DCS_AR BIT(4)
63 #define JZ_DMA_DCS_DES8 BIT(30)
65 #define JZ_DMA_DCM_LINK BIT(0)
66 #define JZ_DMA_DCM_TIE BIT(1)
67 #define JZ_DMA_DCM_STDE BIT(2)
68 #define JZ_DMA_DCM_TSZ_SHIFT 8
69 #define JZ_DMA_DCM_TSZ_MASK (0x7 << JZ_DMA_DCM_TSZ_SHIFT)
70 #define JZ_DMA_DCM_DP_SHIFT 12
71 #define JZ_DMA_DCM_SP_SHIFT 14
72 #define JZ_DMA_DCM_DAI BIT(22)
73 #define JZ_DMA_DCM_SAI BIT(23)
75 #define JZ_DMA_SIZE_4_BYTE 0x0
76 #define JZ_DMA_SIZE_1_BYTE 0x1
77 #define JZ_DMA_SIZE_2_BYTE 0x2
78 #define JZ_DMA_SIZE_16_BYTE 0x3
79 #define JZ_DMA_SIZE_32_BYTE 0x4
80 #define JZ_DMA_SIZE_64_BYTE 0x5
81 #define JZ_DMA_SIZE_128_BYTE 0x6
83 #define JZ_DMA_WIDTH_32_BIT 0x0
84 #define JZ_DMA_WIDTH_8_BIT 0x1
85 #define JZ_DMA_WIDTH_16_BIT 0x2
87 #define JZ_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
88 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
89 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
91 #define JZ4780_DMA_CTRL_OFFSET 0x1000
93 /* macros for use with jz4780_dma_soc_data.flags */
94 #define JZ_SOC_DATA_ALLOW_LEGACY_DT BIT(0)
95 #define JZ_SOC_DATA_PROGRAMMABLE_DMA BIT(1)
96 #define JZ_SOC_DATA_PER_CHAN_PM BIT(2)
99 * struct jz4780_dma_hwdesc - descriptor structure read by the DMA controller.
100 * @dcm: value for the DCM (channel command) register
101 * @dsa: source address
102 * @dta: target address
103 * @dtc: transfer count (number of blocks of the transfer size specified in DCM
104 * to transfer) in the low 24 bits, offset of the next descriptor from the
105 * descriptor base address in the upper 8 bits.
107 struct jz4780_dma_hwdesc {
114 /* Size of allocations for hardware descriptor blocks. */
115 #define JZ_DMA_DESC_BLOCK_SIZE PAGE_SIZE
116 #define JZ_DMA_MAX_DESC \
117 (JZ_DMA_DESC_BLOCK_SIZE / sizeof(struct jz4780_dma_hwdesc))
119 struct jz4780_dma_desc {
120 struct virt_dma_desc vdesc;
122 struct jz4780_dma_hwdesc *desc;
123 dma_addr_t desc_phys;
125 enum dma_transaction_type type;
129 struct jz4780_dma_chan {
130 struct virt_dma_chan vchan;
132 struct dma_pool *desc_pool;
134 uint32_t transfer_type;
135 uint32_t transfer_shift;
136 struct dma_slave_config config;
138 struct jz4780_dma_desc *desc;
139 unsigned int curr_hwdesc;
142 struct jz4780_dma_soc_data {
143 unsigned int nb_channels;
144 unsigned int transfer_ord_max;
148 struct jz4780_dma_dev {
149 struct dma_device dma_device;
150 void __iomem *chn_base;
151 void __iomem *ctrl_base;
154 const struct jz4780_dma_soc_data *soc_data;
156 uint32_t chan_reserved;
157 struct jz4780_dma_chan chan[];
160 struct jz4780_dma_filter_data {
161 struct device_node *of_node;
162 uint32_t transfer_type;
166 static inline struct jz4780_dma_chan *to_jz4780_dma_chan(struct dma_chan *chan)
168 return container_of(chan, struct jz4780_dma_chan, vchan.chan);
171 static inline struct jz4780_dma_desc *to_jz4780_dma_desc(
172 struct virt_dma_desc *vdesc)
174 return container_of(vdesc, struct jz4780_dma_desc, vdesc);
177 static inline struct jz4780_dma_dev *jz4780_dma_chan_parent(
178 struct jz4780_dma_chan *jzchan)
180 return container_of(jzchan->vchan.chan.device, struct jz4780_dma_dev,
184 static inline uint32_t jz4780_dma_chn_readl(struct jz4780_dma_dev *jzdma,
185 unsigned int chn, unsigned int reg)
187 return readl(jzdma->chn_base + reg + JZ_DMA_REG_CHAN(chn));
190 static inline void jz4780_dma_chn_writel(struct jz4780_dma_dev *jzdma,
191 unsigned int chn, unsigned int reg, uint32_t val)
193 writel(val, jzdma->chn_base + reg + JZ_DMA_REG_CHAN(chn));
196 static inline uint32_t jz4780_dma_ctrl_readl(struct jz4780_dma_dev *jzdma,
199 return readl(jzdma->ctrl_base + reg);
202 static inline void jz4780_dma_ctrl_writel(struct jz4780_dma_dev *jzdma,
203 unsigned int reg, uint32_t val)
205 writel(val, jzdma->ctrl_base + reg);
208 static inline void jz4780_dma_chan_enable(struct jz4780_dma_dev *jzdma,
211 if (jzdma->soc_data->flags & JZ_SOC_DATA_PER_CHAN_PM)
212 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DCKES, BIT(chn));
215 static inline void jz4780_dma_chan_disable(struct jz4780_dma_dev *jzdma,
218 if (jzdma->soc_data->flags & JZ_SOC_DATA_PER_CHAN_PM)
219 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DCKEC, BIT(chn));
222 static struct jz4780_dma_desc *jz4780_dma_desc_alloc(
223 struct jz4780_dma_chan *jzchan, unsigned int count,
224 enum dma_transaction_type type)
226 struct jz4780_dma_desc *desc;
228 if (count > JZ_DMA_MAX_DESC)
231 desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
235 desc->desc = dma_pool_alloc(jzchan->desc_pool, GFP_NOWAIT,
247 static void jz4780_dma_desc_free(struct virt_dma_desc *vdesc)
249 struct jz4780_dma_desc *desc = to_jz4780_dma_desc(vdesc);
250 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(vdesc->tx.chan);
252 dma_pool_free(jzchan->desc_pool, desc->desc, desc->desc_phys);
256 static uint32_t jz4780_dma_transfer_size(struct jz4780_dma_chan *jzchan,
257 unsigned long val, uint32_t *shift)
259 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
260 int ord = ffs(val) - 1;
263 * 8 byte transfer sizes unsupported so fall back on 4. If it's larger
264 * than the maximum, just limit it. It is perfectly safe to fall back
265 * in this way since we won't exceed the maximum burst size supported
266 * by the device, the only effect is reduced efficiency. This is better
267 * than refusing to perform the request at all.
271 else if (ord > jzdma->soc_data->transfer_ord_max)
272 ord = jzdma->soc_data->transfer_ord_max;
278 return JZ_DMA_SIZE_1_BYTE;
280 return JZ_DMA_SIZE_2_BYTE;
282 return JZ_DMA_SIZE_4_BYTE;
284 return JZ_DMA_SIZE_16_BYTE;
286 return JZ_DMA_SIZE_32_BYTE;
288 return JZ_DMA_SIZE_64_BYTE;
290 return JZ_DMA_SIZE_128_BYTE;
294 static int jz4780_dma_setup_hwdesc(struct jz4780_dma_chan *jzchan,
295 struct jz4780_dma_hwdesc *desc, dma_addr_t addr, size_t len,
296 enum dma_transfer_direction direction)
298 struct dma_slave_config *config = &jzchan->config;
299 uint32_t width, maxburst, tsz;
301 if (direction == DMA_MEM_TO_DEV) {
302 desc->dcm = JZ_DMA_DCM_SAI;
304 desc->dta = config->dst_addr;
306 width = config->dst_addr_width;
307 maxburst = config->dst_maxburst;
309 desc->dcm = JZ_DMA_DCM_DAI;
310 desc->dsa = config->src_addr;
313 width = config->src_addr_width;
314 maxburst = config->src_maxburst;
318 * This calculates the maximum transfer size that can be used with the
319 * given address, length, width and maximum burst size. The address
320 * must be aligned to the transfer size, the total length must be
321 * divisible by the transfer size, and we must not use more than the
322 * maximum burst specified by the user.
324 tsz = jz4780_dma_transfer_size(jzchan, addr | len | (width * maxburst),
325 &jzchan->transfer_shift);
328 case DMA_SLAVE_BUSWIDTH_1_BYTE:
329 case DMA_SLAVE_BUSWIDTH_2_BYTES:
331 case DMA_SLAVE_BUSWIDTH_4_BYTES:
332 width = JZ_DMA_WIDTH_32_BIT;
338 desc->dcm |= tsz << JZ_DMA_DCM_TSZ_SHIFT;
339 desc->dcm |= width << JZ_DMA_DCM_SP_SHIFT;
340 desc->dcm |= width << JZ_DMA_DCM_DP_SHIFT;
342 desc->dtc = len >> jzchan->transfer_shift;
346 static struct dma_async_tx_descriptor *jz4780_dma_prep_slave_sg(
347 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
348 enum dma_transfer_direction direction, unsigned long flags,
351 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
352 struct jz4780_dma_desc *desc;
356 desc = jz4780_dma_desc_alloc(jzchan, sg_len, DMA_SLAVE);
360 for (i = 0; i < sg_len; i++) {
361 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i],
362 sg_dma_address(&sgl[i]),
366 jz4780_dma_desc_free(&jzchan->desc->vdesc);
370 desc->desc[i].dcm |= JZ_DMA_DCM_TIE;
372 if (i != (sg_len - 1)) {
373 /* Automatically proceeed to the next descriptor. */
374 desc->desc[i].dcm |= JZ_DMA_DCM_LINK;
377 * The upper 8 bits of the DTC field in the descriptor
378 * must be set to (offset from descriptor base of next
382 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
386 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
389 static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic(
390 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
391 size_t period_len, enum dma_transfer_direction direction,
394 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
395 struct jz4780_dma_desc *desc;
396 unsigned int periods, i;
399 if (buf_len % period_len)
402 periods = buf_len / period_len;
404 desc = jz4780_dma_desc_alloc(jzchan, periods, DMA_CYCLIC);
408 for (i = 0; i < periods; i++) {
409 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i], buf_addr,
410 period_len, direction);
412 jz4780_dma_desc_free(&jzchan->desc->vdesc);
416 buf_addr += period_len;
419 * Set the link bit to indicate that the controller should
420 * automatically proceed to the next descriptor. In
421 * jz4780_dma_begin(), this will be cleared if we need to issue
422 * an interrupt after each period.
424 desc->desc[i].dcm |= JZ_DMA_DCM_TIE | JZ_DMA_DCM_LINK;
427 * The upper 8 bits of the DTC field in the descriptor must be
428 * set to (offset from descriptor base of next descriptor >> 4).
429 * If this is the last descriptor, link it back to the first,
430 * i.e. leave offset set to 0, otherwise point to the next one.
432 if (i != (periods - 1)) {
434 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
438 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
441 static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_memcpy(
442 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
443 size_t len, unsigned long flags)
445 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
446 struct jz4780_dma_desc *desc;
449 desc = jz4780_dma_desc_alloc(jzchan, 1, DMA_MEMCPY);
453 tsz = jz4780_dma_transfer_size(jzchan, dest | src | len,
454 &jzchan->transfer_shift);
456 jzchan->transfer_type = JZ_DMA_DRT_AUTO;
458 desc->desc[0].dsa = src;
459 desc->desc[0].dta = dest;
460 desc->desc[0].dcm = JZ_DMA_DCM_TIE | JZ_DMA_DCM_SAI | JZ_DMA_DCM_DAI |
461 tsz << JZ_DMA_DCM_TSZ_SHIFT |
462 JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_SP_SHIFT |
463 JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_DP_SHIFT;
464 desc->desc[0].dtc = len >> jzchan->transfer_shift;
466 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
469 static void jz4780_dma_begin(struct jz4780_dma_chan *jzchan)
471 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
472 struct virt_dma_desc *vdesc;
474 dma_addr_t desc_phys;
477 vdesc = vchan_next_desc(&jzchan->vchan);
481 list_del(&vdesc->node);
483 jzchan->desc = to_jz4780_dma_desc(vdesc);
484 jzchan->curr_hwdesc = 0;
486 if (jzchan->desc->type == DMA_CYCLIC && vdesc->tx.callback) {
488 * The DMA controller doesn't support triggering an
489 * interrupt after processing each descriptor, only
490 * after processing an entire terminated list of
491 * descriptors. For a cyclic DMA setup the list of
492 * descriptors is not terminated so we can never get an
495 * If the user requested a callback for a cyclic DMA
496 * setup then we workaround this hardware limitation
497 * here by degrading to a set of unlinked descriptors
498 * which we will submit in sequence in response to the
499 * completion of processing the previous descriptor.
501 for (i = 0; i < jzchan->desc->count; i++)
502 jzchan->desc->desc[i].dcm &= ~JZ_DMA_DCM_LINK;
506 * There is an existing transfer, therefore this must be one
507 * for which we unlinked the descriptors above. Advance to the
508 * next one in the list.
510 jzchan->curr_hwdesc =
511 (jzchan->curr_hwdesc + 1) % jzchan->desc->count;
514 /* Enable the channel's clock. */
515 jz4780_dma_chan_enable(jzdma, jzchan->id);
517 /* Use 4-word descriptors. */
518 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
520 /* Set transfer type. */
521 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DRT,
522 jzchan->transfer_type);
524 /* Write descriptor address and initiate descriptor fetch. */
525 desc_phys = jzchan->desc->desc_phys +
526 (jzchan->curr_hwdesc * sizeof(*jzchan->desc->desc));
527 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DDA, desc_phys);
528 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DDRS, BIT(jzchan->id));
530 /* Enable the channel. */
531 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS,
535 static void jz4780_dma_issue_pending(struct dma_chan *chan)
537 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
540 spin_lock_irqsave(&jzchan->vchan.lock, flags);
542 if (vchan_issue_pending(&jzchan->vchan) && !jzchan->desc)
543 jz4780_dma_begin(jzchan);
545 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
548 static int jz4780_dma_terminate_all(struct dma_chan *chan)
550 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
551 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
555 spin_lock_irqsave(&jzchan->vchan.lock, flags);
557 /* Clear the DMA status and stop the transfer. */
558 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
560 vchan_terminate_vdesc(&jzchan->desc->vdesc);
564 jz4780_dma_chan_disable(jzdma, jzchan->id);
566 vchan_get_all_descriptors(&jzchan->vchan, &head);
568 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
570 vchan_dma_desc_free_list(&jzchan->vchan, &head);
574 static void jz4780_dma_synchronize(struct dma_chan *chan)
576 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
577 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
579 vchan_synchronize(&jzchan->vchan);
580 jz4780_dma_chan_disable(jzdma, jzchan->id);
583 static int jz4780_dma_config(struct dma_chan *chan,
584 struct dma_slave_config *config)
586 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
588 if ((config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
589 || (config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES))
592 /* Copy the reset of the slave configuration, it is used later. */
593 memcpy(&jzchan->config, config, sizeof(jzchan->config));
598 static size_t jz4780_dma_desc_residue(struct jz4780_dma_chan *jzchan,
599 struct jz4780_dma_desc *desc, unsigned int next_sg)
601 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
602 unsigned int residue, count;
607 for (i = next_sg; i < desc->count; i++)
608 residue += desc->desc[i].dtc << jzchan->transfer_shift;
611 count = jz4780_dma_chn_readl(jzdma, jzchan->id,
613 residue += count << jzchan->transfer_shift;
619 static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan,
620 dma_cookie_t cookie, struct dma_tx_state *txstate)
622 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
623 struct virt_dma_desc *vdesc;
624 enum dma_status status;
627 status = dma_cookie_status(chan, cookie, txstate);
628 if ((status == DMA_COMPLETE) || (txstate == NULL))
631 spin_lock_irqsave(&jzchan->vchan.lock, flags);
633 vdesc = vchan_find_desc(&jzchan->vchan, cookie);
635 /* On the issued list, so hasn't been processed yet */
636 txstate->residue = jz4780_dma_desc_residue(jzchan,
637 to_jz4780_dma_desc(vdesc), 0);
638 } else if (cookie == jzchan->desc->vdesc.tx.cookie) {
639 txstate->residue = jz4780_dma_desc_residue(jzchan, jzchan->desc,
640 (jzchan->curr_hwdesc + 1) % jzchan->desc->count);
642 txstate->residue = 0;
644 if (vdesc && jzchan->desc && vdesc == &jzchan->desc->vdesc
645 && jzchan->desc->status & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT))
648 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
652 static void jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma,
653 struct jz4780_dma_chan *jzchan)
657 spin_lock(&jzchan->vchan.lock);
659 dcs = jz4780_dma_chn_readl(jzdma, jzchan->id, JZ_DMA_REG_DCS);
660 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
662 if (dcs & JZ_DMA_DCS_AR) {
663 dev_warn(&jzchan->vchan.chan.dev->device,
664 "address error (DCS=0x%x)\n", dcs);
667 if (dcs & JZ_DMA_DCS_HLT) {
668 dev_warn(&jzchan->vchan.chan.dev->device,
669 "channel halt (DCS=0x%x)\n", dcs);
673 jzchan->desc->status = dcs;
675 if ((dcs & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT)) == 0) {
676 if (jzchan->desc->type == DMA_CYCLIC) {
677 vchan_cyclic_callback(&jzchan->desc->vdesc);
679 vchan_cookie_complete(&jzchan->desc->vdesc);
683 jz4780_dma_begin(jzchan);
686 dev_err(&jzchan->vchan.chan.dev->device,
687 "channel IRQ with no active transfer\n");
690 spin_unlock(&jzchan->vchan.lock);
693 static irqreturn_t jz4780_dma_irq_handler(int irq, void *data)
695 struct jz4780_dma_dev *jzdma = data;
696 uint32_t pending, dmac;
699 pending = jz4780_dma_ctrl_readl(jzdma, JZ_DMA_REG_DIRQP);
701 for (i = 0; i < jzdma->soc_data->nb_channels; i++) {
702 if (!(pending & (1<<i)))
705 jz4780_dma_chan_irq(jzdma, &jzdma->chan[i]);
708 /* Clear halt and address error status of all channels. */
709 dmac = jz4780_dma_ctrl_readl(jzdma, JZ_DMA_REG_DMAC);
710 dmac &= ~(JZ_DMA_DMAC_HLT | JZ_DMA_DMAC_AR);
711 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMAC, dmac);
713 /* Clear interrupt pending status. */
714 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DIRQP, 0);
719 static int jz4780_dma_alloc_chan_resources(struct dma_chan *chan)
721 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
723 jzchan->desc_pool = dma_pool_create(dev_name(&chan->dev->device),
725 JZ_DMA_DESC_BLOCK_SIZE,
727 if (!jzchan->desc_pool) {
728 dev_err(&chan->dev->device,
729 "failed to allocate descriptor pool\n");
736 static void jz4780_dma_free_chan_resources(struct dma_chan *chan)
738 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
740 vchan_free_chan_resources(&jzchan->vchan);
741 dma_pool_destroy(jzchan->desc_pool);
742 jzchan->desc_pool = NULL;
745 static bool jz4780_dma_filter_fn(struct dma_chan *chan, void *param)
747 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
748 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
749 struct jz4780_dma_filter_data *data = param;
751 if (jzdma->dma_device.dev->of_node != data->of_node)
754 if (data->channel > -1) {
755 if (data->channel != jzchan->id)
757 } else if (jzdma->chan_reserved & BIT(jzchan->id)) {
761 jzchan->transfer_type = data->transfer_type;
766 static struct dma_chan *jz4780_of_dma_xlate(struct of_phandle_args *dma_spec,
767 struct of_dma *ofdma)
769 struct jz4780_dma_dev *jzdma = ofdma->of_dma_data;
770 dma_cap_mask_t mask = jzdma->dma_device.cap_mask;
771 struct jz4780_dma_filter_data data;
773 if (dma_spec->args_count != 2)
776 data.of_node = ofdma->of_node;
777 data.transfer_type = dma_spec->args[0];
778 data.channel = dma_spec->args[1];
780 if (data.channel > -1) {
781 if (data.channel >= jzdma->soc_data->nb_channels) {
782 dev_err(jzdma->dma_device.dev,
783 "device requested non-existent channel %u\n",
788 /* Can only select a channel marked as reserved. */
789 if (!(jzdma->chan_reserved & BIT(data.channel))) {
790 dev_err(jzdma->dma_device.dev,
791 "device requested unreserved channel %u\n",
796 jzdma->chan[data.channel].transfer_type = data.transfer_type;
798 return dma_get_slave_channel(
799 &jzdma->chan[data.channel].vchan.chan);
801 return dma_request_channel(mask, jz4780_dma_filter_fn, &data);
805 static int jz4780_dma_probe(struct platform_device *pdev)
807 struct device *dev = &pdev->dev;
808 const struct jz4780_dma_soc_data *soc_data;
809 struct jz4780_dma_dev *jzdma;
810 struct jz4780_dma_chan *jzchan;
811 struct dma_device *dd;
812 struct resource *res;
816 dev_err(dev, "This driver must be probed from devicetree\n");
820 soc_data = device_get_match_data(dev);
824 jzdma = devm_kzalloc(dev, sizeof(*jzdma)
825 + sizeof(*jzdma->chan) * soc_data->nb_channels,
830 jzdma->soc_data = soc_data;
831 platform_set_drvdata(pdev, jzdma);
833 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
835 dev_err(dev, "failed to get I/O memory\n");
839 jzdma->chn_base = devm_ioremap_resource(dev, res);
840 if (IS_ERR(jzdma->chn_base))
841 return PTR_ERR(jzdma->chn_base);
843 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
845 jzdma->ctrl_base = devm_ioremap_resource(dev, res);
846 if (IS_ERR(jzdma->ctrl_base))
847 return PTR_ERR(jzdma->ctrl_base);
848 } else if (soc_data->flags & JZ_SOC_DATA_ALLOW_LEGACY_DT) {
850 * On JZ4780, if the second memory resource was not supplied,
851 * assume we're using an old devicetree, and calculate the
852 * offset to the control registers.
854 jzdma->ctrl_base = jzdma->chn_base + JZ4780_DMA_CTRL_OFFSET;
856 dev_err(dev, "failed to get I/O memory\n");
860 ret = platform_get_irq(pdev, 0);
862 dev_err(dev, "failed to get IRQ: %d\n", ret);
868 ret = request_irq(jzdma->irq, jz4780_dma_irq_handler, 0, dev_name(dev),
871 dev_err(dev, "failed to request IRQ %u!\n", jzdma->irq);
875 jzdma->clk = devm_clk_get(dev, NULL);
876 if (IS_ERR(jzdma->clk)) {
877 dev_err(dev, "failed to get clock\n");
878 ret = PTR_ERR(jzdma->clk);
882 clk_prepare_enable(jzdma->clk);
884 /* Property is optional, if it doesn't exist the value will remain 0. */
885 of_property_read_u32_index(dev->of_node, "ingenic,reserved-channels",
886 0, &jzdma->chan_reserved);
888 dd = &jzdma->dma_device;
890 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
891 dma_cap_set(DMA_SLAVE, dd->cap_mask);
892 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
895 dd->copy_align = DMAENGINE_ALIGN_4_BYTES;
896 dd->device_alloc_chan_resources = jz4780_dma_alloc_chan_resources;
897 dd->device_free_chan_resources = jz4780_dma_free_chan_resources;
898 dd->device_prep_slave_sg = jz4780_dma_prep_slave_sg;
899 dd->device_prep_dma_cyclic = jz4780_dma_prep_dma_cyclic;
900 dd->device_prep_dma_memcpy = jz4780_dma_prep_dma_memcpy;
901 dd->device_config = jz4780_dma_config;
902 dd->device_terminate_all = jz4780_dma_terminate_all;
903 dd->device_synchronize = jz4780_dma_synchronize;
904 dd->device_tx_status = jz4780_dma_tx_status;
905 dd->device_issue_pending = jz4780_dma_issue_pending;
906 dd->src_addr_widths = JZ_DMA_BUSWIDTHS;
907 dd->dst_addr_widths = JZ_DMA_BUSWIDTHS;
908 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
909 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
912 * Enable DMA controller, mark all channels as not programmable.
913 * Also set the FMSC bit - it increases MSC performance, so it makes
914 * little sense not to enable it.
916 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMAC,
917 JZ_DMA_DMAC_DMAE | JZ_DMA_DMAC_FMSC);
919 if (soc_data->flags & JZ_SOC_DATA_PROGRAMMABLE_DMA)
920 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMACP, 0);
922 INIT_LIST_HEAD(&dd->channels);
924 for (i = 0; i < soc_data->nb_channels; i++) {
925 jzchan = &jzdma->chan[i];
928 vchan_init(&jzchan->vchan, dd);
929 jzchan->vchan.desc_free = jz4780_dma_desc_free;
932 ret = dma_async_device_register(dd);
934 dev_err(dev, "failed to register device\n");
935 goto err_disable_clk;
938 /* Register with OF DMA helpers. */
939 ret = of_dma_controller_register(dev->of_node, jz4780_of_dma_xlate,
942 dev_err(dev, "failed to register OF DMA controller\n");
943 goto err_unregister_dev;
946 dev_info(dev, "JZ4780 DMA controller initialised\n");
950 dma_async_device_unregister(dd);
953 clk_disable_unprepare(jzdma->clk);
956 free_irq(jzdma->irq, jzdma);
960 static int jz4780_dma_remove(struct platform_device *pdev)
962 struct jz4780_dma_dev *jzdma = platform_get_drvdata(pdev);
965 of_dma_controller_free(pdev->dev.of_node);
967 free_irq(jzdma->irq, jzdma);
969 for (i = 0; i < jzdma->soc_data->nb_channels; i++)
970 tasklet_kill(&jzdma->chan[i].vchan.task);
972 dma_async_device_unregister(&jzdma->dma_device);
976 static const struct jz4780_dma_soc_data jz4740_dma_soc_data = {
978 .transfer_ord_max = 5,
981 static const struct jz4780_dma_soc_data jz4770_dma_soc_data = {
983 .transfer_ord_max = 6,
984 .flags = JZ_SOC_DATA_PER_CHAN_PM,
987 static const struct jz4780_dma_soc_data jz4780_dma_soc_data = {
989 .transfer_ord_max = 7,
990 .flags = JZ_SOC_DATA_ALLOW_LEGACY_DT | JZ_SOC_DATA_PROGRAMMABLE_DMA,
993 static const struct of_device_id jz4780_dma_dt_match[] = {
994 { .compatible = "ingenic,jz4740-dma", .data = &jz4740_dma_soc_data },
995 { .compatible = "ingenic,jz4770-dma", .data = &jz4770_dma_soc_data },
996 { .compatible = "ingenic,jz4780-dma", .data = &jz4780_dma_soc_data },
999 MODULE_DEVICE_TABLE(of, jz4780_dma_dt_match);
1001 static struct platform_driver jz4780_dma_driver = {
1002 .probe = jz4780_dma_probe,
1003 .remove = jz4780_dma_remove,
1005 .name = "jz4780-dma",
1006 .of_match_table = of_match_ptr(jz4780_dma_dt_match),
1010 static int __init jz4780_dma_init(void)
1012 return platform_driver_register(&jz4780_dma_driver);
1014 subsys_initcall(jz4780_dma_init);
1016 static void __exit jz4780_dma_exit(void)
1018 platform_driver_unregister(&jz4780_dma_driver);
1020 module_exit(jz4780_dma_exit);
1022 MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
1023 MODULE_DESCRIPTION("Ingenic JZ4780 DMA controller driver");
1024 MODULE_LICENSE("GPL");