1 // SPDX-License-Identifier: GPL-2.0-only
3 * IMG Multi-threaded DMA Controller (MDC)
5 * Copyright (C) 2009,2012,2013 Imagination Technologies Ltd.
6 * Copyright (C) 2014 Google, Inc.
10 #include <linux/dma-mapping.h>
11 #include <linux/dmaengine.h>
12 #include <linux/dmapool.h>
13 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/kernel.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
20 #include <linux/of_dma.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
27 #include "dmaengine.h"
30 #define MDC_MAX_DMA_CHANNELS 32
32 #define MDC_GENERAL_CONFIG 0x000
33 #define MDC_GENERAL_CONFIG_LIST_IEN BIT(31)
34 #define MDC_GENERAL_CONFIG_IEN BIT(29)
35 #define MDC_GENERAL_CONFIG_LEVEL_INT BIT(28)
36 #define MDC_GENERAL_CONFIG_INC_W BIT(12)
37 #define MDC_GENERAL_CONFIG_INC_R BIT(8)
38 #define MDC_GENERAL_CONFIG_PHYSICAL_W BIT(7)
39 #define MDC_GENERAL_CONFIG_WIDTH_W_SHIFT 4
40 #define MDC_GENERAL_CONFIG_WIDTH_W_MASK 0x7
41 #define MDC_GENERAL_CONFIG_PHYSICAL_R BIT(3)
42 #define MDC_GENERAL_CONFIG_WIDTH_R_SHIFT 0
43 #define MDC_GENERAL_CONFIG_WIDTH_R_MASK 0x7
45 #define MDC_READ_PORT_CONFIG 0x004
46 #define MDC_READ_PORT_CONFIG_STHREAD_SHIFT 28
47 #define MDC_READ_PORT_CONFIG_STHREAD_MASK 0xf
48 #define MDC_READ_PORT_CONFIG_RTHREAD_SHIFT 24
49 #define MDC_READ_PORT_CONFIG_RTHREAD_MASK 0xf
50 #define MDC_READ_PORT_CONFIG_WTHREAD_SHIFT 16
51 #define MDC_READ_PORT_CONFIG_WTHREAD_MASK 0xf
52 #define MDC_READ_PORT_CONFIG_BURST_SIZE_SHIFT 4
53 #define MDC_READ_PORT_CONFIG_BURST_SIZE_MASK 0xff
54 #define MDC_READ_PORT_CONFIG_DREQ_ENABLE BIT(1)
56 #define MDC_READ_ADDRESS 0x008
58 #define MDC_WRITE_ADDRESS 0x00c
60 #define MDC_TRANSFER_SIZE 0x010
61 #define MDC_TRANSFER_SIZE_MASK 0xffffff
63 #define MDC_LIST_NODE_ADDRESS 0x014
65 #define MDC_CMDS_PROCESSED 0x018
66 #define MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT 16
67 #define MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK 0x3f
68 #define MDC_CMDS_PROCESSED_INT_ACTIVE BIT(8)
69 #define MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT 0
70 #define MDC_CMDS_PROCESSED_CMDS_DONE_MASK 0x3f
72 #define MDC_CONTROL_AND_STATUS 0x01c
73 #define MDC_CONTROL_AND_STATUS_CANCEL BIT(20)
74 #define MDC_CONTROL_AND_STATUS_LIST_EN BIT(4)
75 #define MDC_CONTROL_AND_STATUS_EN BIT(0)
77 #define MDC_ACTIVE_TRANSFER_SIZE 0x030
79 #define MDC_GLOBAL_CONFIG_A 0x900
80 #define MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_SHIFT 16
81 #define MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_MASK 0xff
82 #define MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_SHIFT 8
83 #define MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_MASK 0xff
84 #define MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_SHIFT 0
85 #define MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_MASK 0xff
87 struct mdc_hw_list_desc
{
97 * Not part of the list descriptor, but instead used by the CPU to
100 struct mdc_hw_list_desc
*next_desc
;
104 struct mdc_chan
*chan
;
105 struct virt_dma_desc vd
;
106 dma_addr_t list_phys
;
107 struct mdc_hw_list_desc
*list
;
110 unsigned int list_len
;
111 unsigned int list_period_len
;
112 size_t list_xfer_size
;
113 unsigned int list_cmds_done
;
117 struct mdc_dma
*mdma
;
118 struct virt_dma_chan vc
;
119 struct dma_slave_config config
;
120 struct mdc_tx_desc
*desc
;
124 unsigned int chan_nr
;
127 struct mdc_dma_soc_data
{
128 void (*enable_chan
)(struct mdc_chan
*mchan
);
129 void (*disable_chan
)(struct mdc_chan
*mchan
);
133 struct dma_device dma_dev
;
136 struct dma_pool
*desc_pool
;
137 struct regmap
*periph_regs
;
139 unsigned int nr_threads
;
140 unsigned int nr_channels
;
141 unsigned int bus_width
;
142 unsigned int max_burst_mult
;
143 unsigned int max_xfer_size
;
144 const struct mdc_dma_soc_data
*soc
;
145 struct mdc_chan channels
[MDC_MAX_DMA_CHANNELS
];
148 static inline u32
mdc_readl(struct mdc_dma
*mdma
, u32 reg
)
150 return readl(mdma
->regs
+ reg
);
153 static inline void mdc_writel(struct mdc_dma
*mdma
, u32 val
, u32 reg
)
155 writel(val
, mdma
->regs
+ reg
);
158 static inline u32
mdc_chan_readl(struct mdc_chan
*mchan
, u32 reg
)
160 return mdc_readl(mchan
->mdma
, mchan
->chan_nr
* 0x040 + reg
);
163 static inline void mdc_chan_writel(struct mdc_chan
*mchan
, u32 val
, u32 reg
)
165 mdc_writel(mchan
->mdma
, val
, mchan
->chan_nr
* 0x040 + reg
);
168 static inline struct mdc_chan
*to_mdc_chan(struct dma_chan
*c
)
170 return container_of(to_virt_chan(c
), struct mdc_chan
, vc
);
173 static inline struct mdc_tx_desc
*to_mdc_desc(struct dma_async_tx_descriptor
*t
)
175 struct virt_dma_desc
*vdesc
= container_of(t
, struct virt_dma_desc
, tx
);
177 return container_of(vdesc
, struct mdc_tx_desc
, vd
);
180 static inline struct device
*mdma2dev(struct mdc_dma
*mdma
)
182 return mdma
->dma_dev
.dev
;
185 static inline unsigned int to_mdc_width(unsigned int bytes
)
187 return ffs(bytes
) - 1;
190 static inline void mdc_set_read_width(struct mdc_hw_list_desc
*ldesc
,
193 ldesc
->gen_conf
|= to_mdc_width(bytes
) <<
194 MDC_GENERAL_CONFIG_WIDTH_R_SHIFT
;
197 static inline void mdc_set_write_width(struct mdc_hw_list_desc
*ldesc
,
200 ldesc
->gen_conf
|= to_mdc_width(bytes
) <<
201 MDC_GENERAL_CONFIG_WIDTH_W_SHIFT
;
204 static void mdc_list_desc_config(struct mdc_chan
*mchan
,
205 struct mdc_hw_list_desc
*ldesc
,
206 enum dma_transfer_direction dir
,
207 dma_addr_t src
, dma_addr_t dst
, size_t len
)
209 struct mdc_dma
*mdma
= mchan
->mdma
;
210 unsigned int max_burst
, burst_size
;
212 ldesc
->gen_conf
= MDC_GENERAL_CONFIG_IEN
| MDC_GENERAL_CONFIG_LIST_IEN
|
213 MDC_GENERAL_CONFIG_LEVEL_INT
| MDC_GENERAL_CONFIG_PHYSICAL_W
|
214 MDC_GENERAL_CONFIG_PHYSICAL_R
;
215 ldesc
->readport_conf
=
216 (mchan
->thread
<< MDC_READ_PORT_CONFIG_STHREAD_SHIFT
) |
217 (mchan
->thread
<< MDC_READ_PORT_CONFIG_RTHREAD_SHIFT
) |
218 (mchan
->thread
<< MDC_READ_PORT_CONFIG_WTHREAD_SHIFT
);
219 ldesc
->read_addr
= src
;
220 ldesc
->write_addr
= dst
;
221 ldesc
->xfer_size
= len
- 1;
222 ldesc
->node_addr
= 0;
223 ldesc
->cmds_done
= 0;
224 ldesc
->ctrl_status
= MDC_CONTROL_AND_STATUS_LIST_EN
|
225 MDC_CONTROL_AND_STATUS_EN
;
226 ldesc
->next_desc
= NULL
;
228 if (IS_ALIGNED(dst
, mdma
->bus_width
) &&
229 IS_ALIGNED(src
, mdma
->bus_width
))
230 max_burst
= mdma
->bus_width
* mdma
->max_burst_mult
;
232 max_burst
= mdma
->bus_width
* (mdma
->max_burst_mult
- 1);
234 if (dir
== DMA_MEM_TO_DEV
) {
235 ldesc
->gen_conf
|= MDC_GENERAL_CONFIG_INC_R
;
236 ldesc
->readport_conf
|= MDC_READ_PORT_CONFIG_DREQ_ENABLE
;
237 mdc_set_read_width(ldesc
, mdma
->bus_width
);
238 mdc_set_write_width(ldesc
, mchan
->config
.dst_addr_width
);
239 burst_size
= min(max_burst
, mchan
->config
.dst_maxburst
*
240 mchan
->config
.dst_addr_width
);
241 } else if (dir
== DMA_DEV_TO_MEM
) {
242 ldesc
->gen_conf
|= MDC_GENERAL_CONFIG_INC_W
;
243 ldesc
->readport_conf
|= MDC_READ_PORT_CONFIG_DREQ_ENABLE
;
244 mdc_set_read_width(ldesc
, mchan
->config
.src_addr_width
);
245 mdc_set_write_width(ldesc
, mdma
->bus_width
);
246 burst_size
= min(max_burst
, mchan
->config
.src_maxburst
*
247 mchan
->config
.src_addr_width
);
249 ldesc
->gen_conf
|= MDC_GENERAL_CONFIG_INC_R
|
250 MDC_GENERAL_CONFIG_INC_W
;
251 mdc_set_read_width(ldesc
, mdma
->bus_width
);
252 mdc_set_write_width(ldesc
, mdma
->bus_width
);
253 burst_size
= max_burst
;
255 ldesc
->readport_conf
|= (burst_size
- 1) <<
256 MDC_READ_PORT_CONFIG_BURST_SIZE_SHIFT
;
259 static void mdc_list_desc_free(struct mdc_tx_desc
*mdesc
)
261 struct mdc_dma
*mdma
= mdesc
->chan
->mdma
;
262 struct mdc_hw_list_desc
*curr
, *next
;
263 dma_addr_t curr_phys
, next_phys
;
266 curr_phys
= mdesc
->list_phys
;
268 next
= curr
->next_desc
;
269 next_phys
= curr
->node_addr
;
270 dma_pool_free(mdma
->desc_pool
, curr
, curr_phys
);
272 curr_phys
= next_phys
;
276 static void mdc_desc_free(struct virt_dma_desc
*vd
)
278 struct mdc_tx_desc
*mdesc
= to_mdc_desc(&vd
->tx
);
280 mdc_list_desc_free(mdesc
);
284 static struct dma_async_tx_descriptor
*mdc_prep_dma_memcpy(
285 struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
, size_t len
,
288 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
289 struct mdc_dma
*mdma
= mchan
->mdma
;
290 struct mdc_tx_desc
*mdesc
;
291 struct mdc_hw_list_desc
*curr
, *prev
= NULL
;
292 dma_addr_t curr_phys
;
297 mdesc
= kzalloc(sizeof(*mdesc
), GFP_NOWAIT
);
301 mdesc
->list_xfer_size
= len
;
306 curr
= dma_pool_alloc(mdma
->desc_pool
, GFP_NOWAIT
, &curr_phys
);
311 prev
->node_addr
= curr_phys
;
312 prev
->next_desc
= curr
;
314 mdesc
->list_phys
= curr_phys
;
318 xfer_size
= min_t(size_t, mdma
->max_xfer_size
, len
);
320 mdc_list_desc_config(mchan
, curr
, DMA_MEM_TO_MEM
, src
, dest
,
331 return vchan_tx_prep(&mchan
->vc
, &mdesc
->vd
, flags
);
334 mdc_desc_free(&mdesc
->vd
);
339 static int mdc_check_slave_width(struct mdc_chan
*mchan
,
340 enum dma_transfer_direction dir
)
342 enum dma_slave_buswidth width
;
344 if (dir
== DMA_MEM_TO_DEV
)
345 width
= mchan
->config
.dst_addr_width
;
347 width
= mchan
->config
.src_addr_width
;
350 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
351 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
352 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
353 case DMA_SLAVE_BUSWIDTH_8_BYTES
:
359 if (width
> mchan
->mdma
->bus_width
)
365 static struct dma_async_tx_descriptor
*mdc_prep_dma_cyclic(
366 struct dma_chan
*chan
, dma_addr_t buf_addr
, size_t buf_len
,
367 size_t period_len
, enum dma_transfer_direction dir
,
370 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
371 struct mdc_dma
*mdma
= mchan
->mdma
;
372 struct mdc_tx_desc
*mdesc
;
373 struct mdc_hw_list_desc
*curr
, *prev
= NULL
;
374 dma_addr_t curr_phys
;
376 if (!buf_len
&& !period_len
)
379 if (!is_slave_direction(dir
))
382 if (mdc_check_slave_width(mchan
, dir
) < 0)
385 mdesc
= kzalloc(sizeof(*mdesc
), GFP_NOWAIT
);
389 mdesc
->cyclic
= true;
390 mdesc
->list_xfer_size
= buf_len
;
391 mdesc
->list_period_len
= DIV_ROUND_UP(period_len
,
392 mdma
->max_xfer_size
);
394 while (buf_len
> 0) {
395 size_t remainder
= min(period_len
, buf_len
);
397 while (remainder
> 0) {
400 curr
= dma_pool_alloc(mdma
->desc_pool
, GFP_NOWAIT
,
406 mdesc
->list_phys
= curr_phys
;
409 prev
->node_addr
= curr_phys
;
410 prev
->next_desc
= curr
;
413 xfer_size
= min_t(size_t, mdma
->max_xfer_size
,
416 if (dir
== DMA_MEM_TO_DEV
) {
417 mdc_list_desc_config(mchan
, curr
, dir
,
419 mchan
->config
.dst_addr
,
422 mdc_list_desc_config(mchan
, curr
, dir
,
423 mchan
->config
.src_addr
,
431 buf_addr
+= xfer_size
;
432 buf_len
-= xfer_size
;
433 remainder
-= xfer_size
;
436 prev
->node_addr
= mdesc
->list_phys
;
438 return vchan_tx_prep(&mchan
->vc
, &mdesc
->vd
, flags
);
441 mdc_desc_free(&mdesc
->vd
);
446 static struct dma_async_tx_descriptor
*mdc_prep_slave_sg(
447 struct dma_chan
*chan
, struct scatterlist
*sgl
,
448 unsigned int sg_len
, enum dma_transfer_direction dir
,
449 unsigned long flags
, void *context
)
451 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
452 struct mdc_dma
*mdma
= mchan
->mdma
;
453 struct mdc_tx_desc
*mdesc
;
454 struct scatterlist
*sg
;
455 struct mdc_hw_list_desc
*curr
, *prev
= NULL
;
456 dma_addr_t curr_phys
;
462 if (!is_slave_direction(dir
))
465 if (mdc_check_slave_width(mchan
, dir
) < 0)
468 mdesc
= kzalloc(sizeof(*mdesc
), GFP_NOWAIT
);
473 for_each_sg(sgl
, sg
, sg_len
, i
) {
474 dma_addr_t buf
= sg_dma_address(sg
);
475 size_t buf_len
= sg_dma_len(sg
);
477 while (buf_len
> 0) {
480 curr
= dma_pool_alloc(mdma
->desc_pool
, GFP_NOWAIT
,
486 mdesc
->list_phys
= curr_phys
;
489 prev
->node_addr
= curr_phys
;
490 prev
->next_desc
= curr
;
493 xfer_size
= min_t(size_t, mdma
->max_xfer_size
,
496 if (dir
== DMA_MEM_TO_DEV
) {
497 mdc_list_desc_config(mchan
, curr
, dir
, buf
,
498 mchan
->config
.dst_addr
,
501 mdc_list_desc_config(mchan
, curr
, dir
,
502 mchan
->config
.src_addr
,
509 mdesc
->list_xfer_size
+= xfer_size
;
511 buf_len
-= xfer_size
;
515 return vchan_tx_prep(&mchan
->vc
, &mdesc
->vd
, flags
);
518 mdc_desc_free(&mdesc
->vd
);
523 static void mdc_issue_desc(struct mdc_chan
*mchan
)
525 struct mdc_dma
*mdma
= mchan
->mdma
;
526 struct virt_dma_desc
*vd
;
527 struct mdc_tx_desc
*mdesc
;
530 vd
= vchan_next_desc(&mchan
->vc
);
536 mdesc
= to_mdc_desc(&vd
->tx
);
539 dev_dbg(mdma2dev(mdma
), "Issuing descriptor on channel %d\n",
542 mdma
->soc
->enable_chan(mchan
);
544 val
= mdc_chan_readl(mchan
, MDC_GENERAL_CONFIG
);
545 val
|= MDC_GENERAL_CONFIG_LIST_IEN
| MDC_GENERAL_CONFIG_IEN
|
546 MDC_GENERAL_CONFIG_LEVEL_INT
| MDC_GENERAL_CONFIG_PHYSICAL_W
|
547 MDC_GENERAL_CONFIG_PHYSICAL_R
;
548 mdc_chan_writel(mchan
, val
, MDC_GENERAL_CONFIG
);
549 val
= (mchan
->thread
<< MDC_READ_PORT_CONFIG_STHREAD_SHIFT
) |
550 (mchan
->thread
<< MDC_READ_PORT_CONFIG_RTHREAD_SHIFT
) |
551 (mchan
->thread
<< MDC_READ_PORT_CONFIG_WTHREAD_SHIFT
);
552 mdc_chan_writel(mchan
, val
, MDC_READ_PORT_CONFIG
);
553 mdc_chan_writel(mchan
, mdesc
->list_phys
, MDC_LIST_NODE_ADDRESS
);
554 val
= mdc_chan_readl(mchan
, MDC_CONTROL_AND_STATUS
);
555 val
|= MDC_CONTROL_AND_STATUS_LIST_EN
;
556 mdc_chan_writel(mchan
, val
, MDC_CONTROL_AND_STATUS
);
559 static void mdc_issue_pending(struct dma_chan
*chan
)
561 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
564 spin_lock_irqsave(&mchan
->vc
.lock
, flags
);
565 if (vchan_issue_pending(&mchan
->vc
) && !mchan
->desc
)
566 mdc_issue_desc(mchan
);
567 spin_unlock_irqrestore(&mchan
->vc
.lock
, flags
);
570 static enum dma_status
mdc_tx_status(struct dma_chan
*chan
,
571 dma_cookie_t cookie
, struct dma_tx_state
*txstate
)
573 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
574 struct mdc_tx_desc
*mdesc
;
575 struct virt_dma_desc
*vd
;
580 ret
= dma_cookie_status(chan
, cookie
, txstate
);
581 if (ret
== DMA_COMPLETE
)
587 spin_lock_irqsave(&mchan
->vc
.lock
, flags
);
588 vd
= vchan_find_desc(&mchan
->vc
, cookie
);
590 mdesc
= to_mdc_desc(&vd
->tx
);
591 bytes
= mdesc
->list_xfer_size
;
592 } else if (mchan
->desc
&& mchan
->desc
->vd
.tx
.cookie
== cookie
) {
593 struct mdc_hw_list_desc
*ldesc
;
594 u32 val1
, val2
, done
, processed
, residue
;
600 * Determine the number of commands that haven't been
601 * processed (handled by the IRQ handler) yet.
604 val1
= mdc_chan_readl(mchan
, MDC_CMDS_PROCESSED
) &
605 ~MDC_CMDS_PROCESSED_INT_ACTIVE
;
606 residue
= mdc_chan_readl(mchan
,
607 MDC_ACTIVE_TRANSFER_SIZE
);
608 val2
= mdc_chan_readl(mchan
, MDC_CMDS_PROCESSED
) &
609 ~MDC_CMDS_PROCESSED_INT_ACTIVE
;
610 } while (val1
!= val2
);
612 done
= (val1
>> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT
) &
613 MDC_CMDS_PROCESSED_CMDS_DONE_MASK
;
614 processed
= (val1
>> MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT
) &
615 MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK
;
616 cmds
= (done
- processed
) %
617 (MDC_CMDS_PROCESSED_CMDS_DONE_MASK
+ 1);
620 * If the command loaded event hasn't been processed yet, then
621 * the difference above includes an extra command.
623 if (!mdesc
->cmd_loaded
)
626 cmds
+= mdesc
->list_cmds_done
;
628 bytes
= mdesc
->list_xfer_size
;
630 for (i
= 0; i
< cmds
; i
++) {
631 bytes
-= ldesc
->xfer_size
+ 1;
632 ldesc
= ldesc
->next_desc
;
635 if (residue
!= MDC_TRANSFER_SIZE_MASK
)
636 bytes
-= ldesc
->xfer_size
- residue
;
638 bytes
-= ldesc
->xfer_size
+ 1;
641 spin_unlock_irqrestore(&mchan
->vc
.lock
, flags
);
643 dma_set_residue(txstate
, bytes
);
648 static unsigned int mdc_get_new_events(struct mdc_chan
*mchan
)
650 u32 val
, processed
, done1
, done2
;
653 val
= mdc_chan_readl(mchan
, MDC_CMDS_PROCESSED
);
654 processed
= (val
>> MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT
) &
655 MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK
;
657 * CMDS_DONE may have incremented between reading CMDS_PROCESSED
658 * and clearing INT_ACTIVE. Re-read CMDS_PROCESSED to ensure we
659 * didn't miss a command completion.
662 val
= mdc_chan_readl(mchan
, MDC_CMDS_PROCESSED
);
664 done1
= (val
>> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT
) &
665 MDC_CMDS_PROCESSED_CMDS_DONE_MASK
;
667 val
&= ~((MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK
<<
668 MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT
) |
669 MDC_CMDS_PROCESSED_INT_ACTIVE
);
671 val
|= done1
<< MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT
;
673 mdc_chan_writel(mchan
, val
, MDC_CMDS_PROCESSED
);
675 val
= mdc_chan_readl(mchan
, MDC_CMDS_PROCESSED
);
677 done2
= (val
>> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT
) &
678 MDC_CMDS_PROCESSED_CMDS_DONE_MASK
;
679 } while (done1
!= done2
);
681 if (done1
>= processed
)
682 ret
= done1
- processed
;
684 ret
= ((MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK
+ 1) -
690 static int mdc_terminate_all(struct dma_chan
*chan
)
692 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
696 spin_lock_irqsave(&mchan
->vc
.lock
, flags
);
698 mdc_chan_writel(mchan
, MDC_CONTROL_AND_STATUS_CANCEL
,
699 MDC_CONTROL_AND_STATUS
);
702 vchan_terminate_vdesc(&mchan
->desc
->vd
);
705 vchan_get_all_descriptors(&mchan
->vc
, &head
);
707 mdc_get_new_events(mchan
);
709 spin_unlock_irqrestore(&mchan
->vc
.lock
, flags
);
711 vchan_dma_desc_free_list(&mchan
->vc
, &head
);
716 static void mdc_synchronize(struct dma_chan
*chan
)
718 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
720 vchan_synchronize(&mchan
->vc
);
723 static int mdc_slave_config(struct dma_chan
*chan
,
724 struct dma_slave_config
*config
)
726 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
729 spin_lock_irqsave(&mchan
->vc
.lock
, flags
);
730 mchan
->config
= *config
;
731 spin_unlock_irqrestore(&mchan
->vc
.lock
, flags
);
736 static int mdc_alloc_chan_resources(struct dma_chan
*chan
)
738 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
739 struct device
*dev
= mdma2dev(mchan
->mdma
);
741 return pm_runtime_get_sync(dev
);
744 static void mdc_free_chan_resources(struct dma_chan
*chan
)
746 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
747 struct mdc_dma
*mdma
= mchan
->mdma
;
748 struct device
*dev
= mdma2dev(mdma
);
750 mdc_terminate_all(chan
);
751 mdma
->soc
->disable_chan(mchan
);
755 static irqreturn_t
mdc_chan_irq(int irq
, void *dev_id
)
757 struct mdc_chan
*mchan
= (struct mdc_chan
*)dev_id
;
758 struct mdc_tx_desc
*mdesc
;
759 unsigned int i
, new_events
;
761 spin_lock(&mchan
->vc
.lock
);
763 dev_dbg(mdma2dev(mchan
->mdma
), "IRQ on channel %d\n", mchan
->chan_nr
);
765 new_events
= mdc_get_new_events(mchan
);
772 dev_warn(mdma2dev(mchan
->mdma
),
773 "IRQ with no active descriptor on channel %d\n",
778 for (i
= 0; i
< new_events
; i
++) {
780 * The first interrupt in a transfer indicates that the
781 * command list has been loaded, not that a command has
784 if (!mdesc
->cmd_loaded
) {
785 mdesc
->cmd_loaded
= true;
789 mdesc
->list_cmds_done
++;
791 mdesc
->list_cmds_done
%= mdesc
->list_len
;
792 if (mdesc
->list_cmds_done
% mdesc
->list_period_len
== 0)
793 vchan_cyclic_callback(&mdesc
->vd
);
794 } else if (mdesc
->list_cmds_done
== mdesc
->list_len
) {
796 vchan_cookie_complete(&mdesc
->vd
);
797 mdc_issue_desc(mchan
);
802 spin_unlock(&mchan
->vc
.lock
);
807 static struct dma_chan
*mdc_of_xlate(struct of_phandle_args
*dma_spec
,
808 struct of_dma
*ofdma
)
810 struct mdc_dma
*mdma
= ofdma
->of_dma_data
;
811 struct dma_chan
*chan
;
813 if (dma_spec
->args_count
!= 3)
816 list_for_each_entry(chan
, &mdma
->dma_dev
.channels
, device_node
) {
817 struct mdc_chan
*mchan
= to_mdc_chan(chan
);
819 if (!(dma_spec
->args
[1] & BIT(mchan
->chan_nr
)))
821 if (dma_get_slave_channel(chan
)) {
822 mchan
->periph
= dma_spec
->args
[0];
823 mchan
->thread
= dma_spec
->args
[2];
831 #define PISTACHIO_CR_PERIPH_DMA_ROUTE(ch) (0x120 + 0x4 * ((ch) / 4))
832 #define PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(ch) (8 * ((ch) % 4))
833 #define PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK 0x3f
835 static void pistachio_mdc_enable_chan(struct mdc_chan
*mchan
)
837 struct mdc_dma
*mdma
= mchan
->mdma
;
839 regmap_update_bits(mdma
->periph_regs
,
840 PISTACHIO_CR_PERIPH_DMA_ROUTE(mchan
->chan_nr
),
841 PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK
<<
842 PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan
->chan_nr
),
844 PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan
->chan_nr
));
847 static void pistachio_mdc_disable_chan(struct mdc_chan
*mchan
)
849 struct mdc_dma
*mdma
= mchan
->mdma
;
851 regmap_update_bits(mdma
->periph_regs
,
852 PISTACHIO_CR_PERIPH_DMA_ROUTE(mchan
->chan_nr
),
853 PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK
<<
854 PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan
->chan_nr
),
858 static const struct mdc_dma_soc_data pistachio_mdc_data
= {
859 .enable_chan
= pistachio_mdc_enable_chan
,
860 .disable_chan
= pistachio_mdc_disable_chan
,
863 static const struct of_device_id mdc_dma_of_match
[] = {
864 { .compatible
= "img,pistachio-mdc-dma", .data
= &pistachio_mdc_data
, },
867 MODULE_DEVICE_TABLE(of
, mdc_dma_of_match
);
869 static int img_mdc_runtime_suspend(struct device
*dev
)
871 struct mdc_dma
*mdma
= dev_get_drvdata(dev
);
873 clk_disable_unprepare(mdma
->clk
);
878 static int img_mdc_runtime_resume(struct device
*dev
)
880 struct mdc_dma
*mdma
= dev_get_drvdata(dev
);
882 return clk_prepare_enable(mdma
->clk
);
885 static int mdc_dma_probe(struct platform_device
*pdev
)
887 struct mdc_dma
*mdma
;
892 mdma
= devm_kzalloc(&pdev
->dev
, sizeof(*mdma
), GFP_KERNEL
);
895 platform_set_drvdata(pdev
, mdma
);
897 mdma
->soc
= of_device_get_match_data(&pdev
->dev
);
899 mdma
->regs
= devm_platform_ioremap_resource(pdev
, 0);
900 if (IS_ERR(mdma
->regs
))
901 return PTR_ERR(mdma
->regs
);
903 mdma
->periph_regs
= syscon_regmap_lookup_by_phandle(pdev
->dev
.of_node
,
905 if (IS_ERR(mdma
->periph_regs
))
906 return PTR_ERR(mdma
->periph_regs
);
908 mdma
->clk
= devm_clk_get(&pdev
->dev
, "sys");
909 if (IS_ERR(mdma
->clk
))
910 return PTR_ERR(mdma
->clk
);
912 dma_cap_zero(mdma
->dma_dev
.cap_mask
);
913 dma_cap_set(DMA_SLAVE
, mdma
->dma_dev
.cap_mask
);
914 dma_cap_set(DMA_PRIVATE
, mdma
->dma_dev
.cap_mask
);
915 dma_cap_set(DMA_CYCLIC
, mdma
->dma_dev
.cap_mask
);
916 dma_cap_set(DMA_MEMCPY
, mdma
->dma_dev
.cap_mask
);
918 val
= mdc_readl(mdma
, MDC_GLOBAL_CONFIG_A
);
919 mdma
->nr_channels
= (val
>> MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_SHIFT
) &
920 MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_MASK
;
922 1 << ((val
>> MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_SHIFT
) &
923 MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_MASK
);
925 (1 << ((val
>> MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_SHIFT
) &
926 MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_MASK
)) / 8;
928 * Although transfer sizes of up to MDC_TRANSFER_SIZE_MASK + 1 bytes
929 * are supported, this makes it possible for the value reported in
930 * MDC_ACTIVE_TRANSFER_SIZE to be ambiguous - an active transfer size
931 * of MDC_TRANSFER_SIZE_MASK may indicate either that 0 bytes or
932 * MDC_TRANSFER_SIZE_MASK + 1 bytes are remaining. To eliminate this
933 * ambiguity, restrict transfer sizes to one bus-width less than the
936 mdma
->max_xfer_size
= MDC_TRANSFER_SIZE_MASK
+ 1 - mdma
->bus_width
;
938 of_property_read_u32(pdev
->dev
.of_node
, "dma-channels",
940 ret
= of_property_read_u32(pdev
->dev
.of_node
,
941 "img,max-burst-multiplier",
942 &mdma
->max_burst_mult
);
946 mdma
->dma_dev
.dev
= &pdev
->dev
;
947 mdma
->dma_dev
.device_prep_slave_sg
= mdc_prep_slave_sg
;
948 mdma
->dma_dev
.device_prep_dma_cyclic
= mdc_prep_dma_cyclic
;
949 mdma
->dma_dev
.device_prep_dma_memcpy
= mdc_prep_dma_memcpy
;
950 mdma
->dma_dev
.device_alloc_chan_resources
= mdc_alloc_chan_resources
;
951 mdma
->dma_dev
.device_free_chan_resources
= mdc_free_chan_resources
;
952 mdma
->dma_dev
.device_tx_status
= mdc_tx_status
;
953 mdma
->dma_dev
.device_issue_pending
= mdc_issue_pending
;
954 mdma
->dma_dev
.device_terminate_all
= mdc_terminate_all
;
955 mdma
->dma_dev
.device_synchronize
= mdc_synchronize
;
956 mdma
->dma_dev
.device_config
= mdc_slave_config
;
958 mdma
->dma_dev
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
959 mdma
->dma_dev
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
960 for (i
= 1; i
<= mdma
->bus_width
; i
<<= 1) {
961 mdma
->dma_dev
.src_addr_widths
|= BIT(i
);
962 mdma
->dma_dev
.dst_addr_widths
|= BIT(i
);
965 INIT_LIST_HEAD(&mdma
->dma_dev
.channels
);
966 for (i
= 0; i
< mdma
->nr_channels
; i
++) {
967 struct mdc_chan
*mchan
= &mdma
->channels
[i
];
971 mchan
->irq
= platform_get_irq(pdev
, i
);
975 ret
= devm_request_irq(&pdev
->dev
, mchan
->irq
, mdc_chan_irq
,
977 dev_name(&pdev
->dev
), mchan
);
981 mchan
->vc
.desc_free
= mdc_desc_free
;
982 vchan_init(&mchan
->vc
, &mdma
->dma_dev
);
985 mdma
->desc_pool
= dmam_pool_create(dev_name(&pdev
->dev
), &pdev
->dev
,
986 sizeof(struct mdc_hw_list_desc
),
988 if (!mdma
->desc_pool
)
991 pm_runtime_enable(&pdev
->dev
);
992 if (!pm_runtime_enabled(&pdev
->dev
)) {
993 ret
= img_mdc_runtime_resume(&pdev
->dev
);
998 ret
= dma_async_device_register(&mdma
->dma_dev
);
1002 ret
= of_dma_controller_register(pdev
->dev
.of_node
, mdc_of_xlate
, mdma
);
1006 dev_info(&pdev
->dev
, "MDC with %u channels and %u threads\n",
1007 mdma
->nr_channels
, mdma
->nr_threads
);
1012 dma_async_device_unregister(&mdma
->dma_dev
);
1014 if (!pm_runtime_enabled(&pdev
->dev
))
1015 img_mdc_runtime_suspend(&pdev
->dev
);
1016 pm_runtime_disable(&pdev
->dev
);
1020 static void mdc_dma_remove(struct platform_device
*pdev
)
1022 struct mdc_dma
*mdma
= platform_get_drvdata(pdev
);
1023 struct mdc_chan
*mchan
, *next
;
1025 of_dma_controller_free(pdev
->dev
.of_node
);
1026 dma_async_device_unregister(&mdma
->dma_dev
);
1028 list_for_each_entry_safe(mchan
, next
, &mdma
->dma_dev
.channels
,
1029 vc
.chan
.device_node
) {
1030 list_del(&mchan
->vc
.chan
.device_node
);
1032 devm_free_irq(&pdev
->dev
, mchan
->irq
, mchan
);
1034 tasklet_kill(&mchan
->vc
.task
);
1037 pm_runtime_disable(&pdev
->dev
);
1038 if (!pm_runtime_status_suspended(&pdev
->dev
))
1039 img_mdc_runtime_suspend(&pdev
->dev
);
1042 #ifdef CONFIG_PM_SLEEP
1043 static int img_mdc_suspend_late(struct device
*dev
)
1045 struct mdc_dma
*mdma
= dev_get_drvdata(dev
);
1048 /* Check that all channels are idle */
1049 for (i
= 0; i
< mdma
->nr_channels
; i
++) {
1050 struct mdc_chan
*mchan
= &mdma
->channels
[i
];
1052 if (unlikely(mchan
->desc
))
1056 return pm_runtime_force_suspend(dev
);
1059 static int img_mdc_resume_early(struct device
*dev
)
1061 return pm_runtime_force_resume(dev
);
1063 #endif /* CONFIG_PM_SLEEP */
1065 static const struct dev_pm_ops img_mdc_pm_ops
= {
1066 SET_RUNTIME_PM_OPS(img_mdc_runtime_suspend
,
1067 img_mdc_runtime_resume
, NULL
)
1068 SET_LATE_SYSTEM_SLEEP_PM_OPS(img_mdc_suspend_late
,
1069 img_mdc_resume_early
)
1072 static struct platform_driver mdc_dma_driver
= {
1074 .name
= "img-mdc-dma",
1075 .pm
= &img_mdc_pm_ops
,
1076 .of_match_table
= of_match_ptr(mdc_dma_of_match
),
1078 .probe
= mdc_dma_probe
,
1079 .remove_new
= mdc_dma_remove
,
1081 module_platform_driver(mdc_dma_driver
);
1083 MODULE_DESCRIPTION("IMG Multi-threaded DMA Controller (MDC) driver");
1084 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
1085 MODULE_LICENSE("GPL v2");