2 * OMAP DMAengine support
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #include <linux/delay.h>
9 #include <linux/dmaengine.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/omap-dma.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/of_dma.h>
21 #include <linux/of_device.h>
25 #define OMAP_SDMA_REQUESTS 127
26 #define OMAP_SDMA_CHANNELS 32
29 struct dma_device ddev
;
32 const struct omap_dma_reg
*reg_map
;
33 struct omap_system_dma_plat_info
*plat
;
35 unsigned dma_requests
;
37 uint32_t irq_enable_mask
;
38 struct omap_chan
*lch_map
[OMAP_SDMA_CHANNELS
];
42 struct virt_dma_chan vc
;
43 void __iomem
*channel_base
;
44 const struct omap_dma_reg
*reg_map
;
47 struct dma_slave_config cfg
;
54 struct omap_desc
*desc
;
60 uint32_t en
; /* number of elements (24-bit) */
61 uint32_t fn
; /* number of frames (16-bit) */
65 struct virt_dma_desc vd
;
66 enum dma_transfer_direction dir
;
69 int16_t fi
; /* for OMAP_DMA_SYNC_PACKET */
70 uint8_t es
; /* CSDP_DATA_TYPE_xxx */
71 uint32_t ccr
; /* CCR value */
72 uint16_t clnk_ctrl
; /* CLNK_CTRL value */
73 uint16_t cicr
; /* CICR value */
74 uint32_t csdp
; /* CSDP value */
82 CCR_READ_PRIORITY
= BIT(6),
84 CCR_AUTO_INIT
= BIT(8), /* OMAP1 only */
85 CCR_REPEAT
= BIT(9), /* OMAP1 only */
86 CCR_OMAP31_DISABLE
= BIT(10), /* OMAP1 only */
87 CCR_SUSPEND_SENSITIVE
= BIT(8), /* OMAP2+ only */
88 CCR_RD_ACTIVE
= BIT(9), /* OMAP2+ only */
89 CCR_WR_ACTIVE
= BIT(10), /* OMAP2+ only */
90 CCR_SRC_AMODE_CONSTANT
= 0 << 12,
91 CCR_SRC_AMODE_POSTINC
= 1 << 12,
92 CCR_SRC_AMODE_SGLIDX
= 2 << 12,
93 CCR_SRC_AMODE_DBLIDX
= 3 << 12,
94 CCR_DST_AMODE_CONSTANT
= 0 << 14,
95 CCR_DST_AMODE_POSTINC
= 1 << 14,
96 CCR_DST_AMODE_SGLIDX
= 2 << 14,
97 CCR_DST_AMODE_DBLIDX
= 3 << 14,
98 CCR_CONSTANT_FILL
= BIT(16),
99 CCR_TRANSPARENT_COPY
= BIT(17),
101 CCR_SUPERVISOR
= BIT(22),
102 CCR_PREFETCH
= BIT(23),
103 CCR_TRIGGER_SRC
= BIT(24),
104 CCR_BUFFERING_DISABLE
= BIT(25),
105 CCR_WRITE_PRIORITY
= BIT(26),
106 CCR_SYNC_ELEMENT
= 0,
107 CCR_SYNC_FRAME
= CCR_FS
,
108 CCR_SYNC_BLOCK
= CCR_BS
,
109 CCR_SYNC_PACKET
= CCR_BS
| CCR_FS
,
111 CSDP_DATA_TYPE_8
= 0,
112 CSDP_DATA_TYPE_16
= 1,
113 CSDP_DATA_TYPE_32
= 2,
114 CSDP_SRC_PORT_EMIFF
= 0 << 2, /* OMAP1 only */
115 CSDP_SRC_PORT_EMIFS
= 1 << 2, /* OMAP1 only */
116 CSDP_SRC_PORT_OCP_T1
= 2 << 2, /* OMAP1 only */
117 CSDP_SRC_PORT_TIPB
= 3 << 2, /* OMAP1 only */
118 CSDP_SRC_PORT_OCP_T2
= 4 << 2, /* OMAP1 only */
119 CSDP_SRC_PORT_MPUI
= 5 << 2, /* OMAP1 only */
120 CSDP_SRC_PACKED
= BIT(6),
121 CSDP_SRC_BURST_1
= 0 << 7,
122 CSDP_SRC_BURST_16
= 1 << 7,
123 CSDP_SRC_BURST_32
= 2 << 7,
124 CSDP_SRC_BURST_64
= 3 << 7,
125 CSDP_DST_PORT_EMIFF
= 0 << 9, /* OMAP1 only */
126 CSDP_DST_PORT_EMIFS
= 1 << 9, /* OMAP1 only */
127 CSDP_DST_PORT_OCP_T1
= 2 << 9, /* OMAP1 only */
128 CSDP_DST_PORT_TIPB
= 3 << 9, /* OMAP1 only */
129 CSDP_DST_PORT_OCP_T2
= 4 << 9, /* OMAP1 only */
130 CSDP_DST_PORT_MPUI
= 5 << 9, /* OMAP1 only */
131 CSDP_DST_PACKED
= BIT(13),
132 CSDP_DST_BURST_1
= 0 << 14,
133 CSDP_DST_BURST_16
= 1 << 14,
134 CSDP_DST_BURST_32
= 2 << 14,
135 CSDP_DST_BURST_64
= 3 << 14,
137 CICR_TOUT_IE
= BIT(0), /* OMAP1 only */
138 CICR_DROP_IE
= BIT(1),
139 CICR_HALF_IE
= BIT(2),
140 CICR_FRAME_IE
= BIT(3),
141 CICR_LAST_IE
= BIT(4),
142 CICR_BLOCK_IE
= BIT(5),
143 CICR_PKT_IE
= BIT(7), /* OMAP2+ only */
144 CICR_TRANS_ERR_IE
= BIT(8), /* OMAP2+ only */
145 CICR_SUPERVISOR_ERR_IE
= BIT(10), /* OMAP2+ only */
146 CICR_MISALIGNED_ERR_IE
= BIT(11), /* OMAP2+ only */
147 CICR_DRAIN_IE
= BIT(12), /* OMAP2+ only */
148 CICR_SUPER_BLOCK_IE
= BIT(14), /* OMAP2+ only */
150 CLNK_CTRL_ENABLE_LNK
= BIT(15),
153 static const unsigned es_bytes
[] = {
154 [CSDP_DATA_TYPE_8
] = 1,
155 [CSDP_DATA_TYPE_16
] = 2,
156 [CSDP_DATA_TYPE_32
] = 4,
159 static struct of_dma_filter_info omap_dma_info
= {
160 .filter_fn
= omap_dma_filter_fn
,
163 static inline struct omap_dmadev
*to_omap_dma_dev(struct dma_device
*d
)
165 return container_of(d
, struct omap_dmadev
, ddev
);
168 static inline struct omap_chan
*to_omap_dma_chan(struct dma_chan
*c
)
170 return container_of(c
, struct omap_chan
, vc
.chan
);
173 static inline struct omap_desc
*to_omap_dma_desc(struct dma_async_tx_descriptor
*t
)
175 return container_of(t
, struct omap_desc
, vd
.tx
);
178 static void omap_dma_desc_free(struct virt_dma_desc
*vd
)
180 kfree(container_of(vd
, struct omap_desc
, vd
));
183 static void omap_dma_write(uint32_t val
, unsigned type
, void __iomem
*addr
)
186 case OMAP_DMA_REG_16BIT
:
187 writew_relaxed(val
, addr
);
189 case OMAP_DMA_REG_2X16BIT
:
190 writew_relaxed(val
, addr
);
191 writew_relaxed(val
>> 16, addr
+ 2);
193 case OMAP_DMA_REG_32BIT
:
194 writel_relaxed(val
, addr
);
201 static unsigned omap_dma_read(unsigned type
, void __iomem
*addr
)
206 case OMAP_DMA_REG_16BIT
:
207 val
= readw_relaxed(addr
);
209 case OMAP_DMA_REG_2X16BIT
:
210 val
= readw_relaxed(addr
);
211 val
|= readw_relaxed(addr
+ 2) << 16;
213 case OMAP_DMA_REG_32BIT
:
214 val
= readl_relaxed(addr
);
224 static void omap_dma_glbl_write(struct omap_dmadev
*od
, unsigned reg
, unsigned val
)
226 const struct omap_dma_reg
*r
= od
->reg_map
+ reg
;
230 omap_dma_write(val
, r
->type
, od
->base
+ r
->offset
);
233 static unsigned omap_dma_glbl_read(struct omap_dmadev
*od
, unsigned reg
)
235 const struct omap_dma_reg
*r
= od
->reg_map
+ reg
;
239 return omap_dma_read(r
->type
, od
->base
+ r
->offset
);
242 static void omap_dma_chan_write(struct omap_chan
*c
, unsigned reg
, unsigned val
)
244 const struct omap_dma_reg
*r
= c
->reg_map
+ reg
;
246 omap_dma_write(val
, r
->type
, c
->channel_base
+ r
->offset
);
249 static unsigned omap_dma_chan_read(struct omap_chan
*c
, unsigned reg
)
251 const struct omap_dma_reg
*r
= c
->reg_map
+ reg
;
253 return omap_dma_read(r
->type
, c
->channel_base
+ r
->offset
);
256 static void omap_dma_clear_csr(struct omap_chan
*c
)
259 omap_dma_chan_read(c
, CSR
);
261 omap_dma_chan_write(c
, CSR
, ~0);
264 static unsigned omap_dma_get_csr(struct omap_chan
*c
)
266 unsigned val
= omap_dma_chan_read(c
, CSR
);
269 omap_dma_chan_write(c
, CSR
, val
);
274 static void omap_dma_assign(struct omap_dmadev
*od
, struct omap_chan
*c
,
277 c
->channel_base
= od
->base
+ od
->plat
->channel_stride
* lch
;
279 od
->lch_map
[lch
] = c
;
282 static void omap_dma_start(struct omap_chan
*c
, struct omap_desc
*d
)
284 struct omap_dmadev
*od
= to_omap_dma_dev(c
->vc
.chan
.device
);
286 if (__dma_omap15xx(od
->plat
->dma_attr
))
287 omap_dma_chan_write(c
, CPC
, 0);
289 omap_dma_chan_write(c
, CDAC
, 0);
291 omap_dma_clear_csr(c
);
293 /* Enable interrupts */
294 omap_dma_chan_write(c
, CICR
, d
->cicr
);
297 omap_dma_chan_write(c
, CCR
, d
->ccr
| CCR_ENABLE
);
302 static void omap_dma_stop(struct omap_chan
*c
)
304 struct omap_dmadev
*od
= to_omap_dma_dev(c
->vc
.chan
.device
);
308 omap_dma_chan_write(c
, CICR
, 0);
310 omap_dma_clear_csr(c
);
312 val
= omap_dma_chan_read(c
, CCR
);
313 if (od
->plat
->errata
& DMA_ERRATA_i541
&& val
& CCR_TRIGGER_SRC
) {
317 sysconfig
= omap_dma_glbl_read(od
, OCP_SYSCONFIG
);
318 val
= sysconfig
& ~DMA_SYSCONFIG_MIDLEMODE_MASK
;
319 val
|= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE
);
320 omap_dma_glbl_write(od
, OCP_SYSCONFIG
, val
);
322 val
= omap_dma_chan_read(c
, CCR
);
324 omap_dma_chan_write(c
, CCR
, val
);
326 /* Wait for sDMA FIFO to drain */
328 val
= omap_dma_chan_read(c
, CCR
);
329 if (!(val
& (CCR_RD_ACTIVE
| CCR_WR_ACTIVE
)))
338 if (val
& (CCR_RD_ACTIVE
| CCR_WR_ACTIVE
))
339 dev_err(c
->vc
.chan
.device
->dev
,
340 "DMA drain did not complete on lch %d\n",
343 omap_dma_glbl_write(od
, OCP_SYSCONFIG
, sysconfig
);
346 omap_dma_chan_write(c
, CCR
, val
);
351 if (!__dma_omap15xx(od
->plat
->dma_attr
) && c
->cyclic
) {
352 val
= omap_dma_chan_read(c
, CLNK_CTRL
);
355 val
|= 1 << 14; /* set the STOP_LNK bit */
357 val
&= ~CLNK_CTRL_ENABLE_LNK
;
359 omap_dma_chan_write(c
, CLNK_CTRL
, val
);
365 static void omap_dma_start_sg(struct omap_chan
*c
, struct omap_desc
*d
,
368 struct omap_sg
*sg
= d
->sg
+ idx
;
369 unsigned cxsa
, cxei
, cxfi
;
371 if (d
->dir
== DMA_DEV_TO_MEM
|| d
->dir
== DMA_MEM_TO_MEM
) {
381 omap_dma_chan_write(c
, cxsa
, sg
->addr
);
382 omap_dma_chan_write(c
, cxei
, 0);
383 omap_dma_chan_write(c
, cxfi
, 0);
384 omap_dma_chan_write(c
, CEN
, sg
->en
);
385 omap_dma_chan_write(c
, CFN
, sg
->fn
);
387 omap_dma_start(c
, d
);
390 static void omap_dma_start_desc(struct omap_chan
*c
)
392 struct virt_dma_desc
*vd
= vchan_next_desc(&c
->vc
);
394 unsigned cxsa
, cxei
, cxfi
;
403 c
->desc
= d
= to_omap_dma_desc(&vd
->tx
);
407 * This provides the necessary barrier to ensure data held in
408 * DMA coherent memory is visible to the DMA engine prior to
409 * the transfer starting.
413 omap_dma_chan_write(c
, CCR
, d
->ccr
);
415 omap_dma_chan_write(c
, CCR2
, d
->ccr
>> 16);
417 if (d
->dir
== DMA_DEV_TO_MEM
|| d
->dir
== DMA_MEM_TO_MEM
) {
427 omap_dma_chan_write(c
, cxsa
, d
->dev_addr
);
428 omap_dma_chan_write(c
, cxei
, 0);
429 omap_dma_chan_write(c
, cxfi
, d
->fi
);
430 omap_dma_chan_write(c
, CSDP
, d
->csdp
);
431 omap_dma_chan_write(c
, CLNK_CTRL
, d
->clnk_ctrl
);
433 omap_dma_start_sg(c
, d
, 0);
436 static void omap_dma_callback(int ch
, u16 status
, void *data
)
438 struct omap_chan
*c
= data
;
442 spin_lock_irqsave(&c
->vc
.lock
, flags
);
446 if (++c
->sgidx
< d
->sglen
) {
447 omap_dma_start_sg(c
, d
, c
->sgidx
);
449 omap_dma_start_desc(c
);
450 vchan_cookie_complete(&d
->vd
);
453 vchan_cyclic_callback(&d
->vd
);
456 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
459 static irqreturn_t
omap_dma_irq(int irq
, void *devid
)
461 struct omap_dmadev
*od
= devid
;
462 unsigned status
, channel
;
464 spin_lock(&od
->irq_lock
);
466 status
= omap_dma_glbl_read(od
, IRQSTATUS_L1
);
467 status
&= od
->irq_enable_mask
;
469 spin_unlock(&od
->irq_lock
);
473 while ((channel
= ffs(status
)) != 0) {
481 c
= od
->lch_map
[channel
];
483 /* This should never happen */
484 dev_err(od
->ddev
.dev
, "invalid channel %u\n", channel
);
488 csr
= omap_dma_get_csr(c
);
489 omap_dma_glbl_write(od
, IRQSTATUS_L1
, mask
);
491 omap_dma_callback(channel
, csr
, c
);
494 spin_unlock(&od
->irq_lock
);
499 static int omap_dma_alloc_chan_resources(struct dma_chan
*chan
)
501 struct omap_dmadev
*od
= to_omap_dma_dev(chan
->device
);
502 struct omap_chan
*c
= to_omap_dma_chan(chan
);
506 ret
= omap_request_dma(c
->dma_sig
, "DMA engine",
507 omap_dma_callback
, c
, &c
->dma_ch
);
509 ret
= omap_request_dma(c
->dma_sig
, "DMA engine", NULL
, NULL
,
513 dev_dbg(od
->ddev
.dev
, "allocating channel %u for %u\n",
514 c
->dma_ch
, c
->dma_sig
);
517 omap_dma_assign(od
, c
, c
->dma_ch
);
522 spin_lock_irq(&od
->irq_lock
);
523 val
= BIT(c
->dma_ch
);
524 omap_dma_glbl_write(od
, IRQSTATUS_L1
, val
);
525 od
->irq_enable_mask
|= val
;
526 omap_dma_glbl_write(od
, IRQENABLE_L1
, od
->irq_enable_mask
);
528 val
= omap_dma_glbl_read(od
, IRQENABLE_L0
);
529 val
&= ~BIT(c
->dma_ch
);
530 omap_dma_glbl_write(od
, IRQENABLE_L0
, val
);
531 spin_unlock_irq(&od
->irq_lock
);
536 if (__dma_omap16xx(od
->plat
->dma_attr
)) {
537 c
->ccr
= CCR_OMAP31_DISABLE
;
538 /* Duplicate what plat-omap/dma.c does */
539 c
->ccr
|= c
->dma_ch
+ 1;
541 c
->ccr
= c
->dma_sig
& 0x1f;
544 c
->ccr
= c
->dma_sig
& 0x1f;
545 c
->ccr
|= (c
->dma_sig
& ~0x1f) << 14;
547 if (od
->plat
->errata
& DMA_ERRATA_IFRAME_BUFFERING
)
548 c
->ccr
|= CCR_BUFFERING_DISABLE
;
553 static void omap_dma_free_chan_resources(struct dma_chan
*chan
)
555 struct omap_dmadev
*od
= to_omap_dma_dev(chan
->device
);
556 struct omap_chan
*c
= to_omap_dma_chan(chan
);
559 spin_lock_irq(&od
->irq_lock
);
560 od
->irq_enable_mask
&= ~BIT(c
->dma_ch
);
561 omap_dma_glbl_write(od
, IRQENABLE_L1
, od
->irq_enable_mask
);
562 spin_unlock_irq(&od
->irq_lock
);
565 c
->channel_base
= NULL
;
566 od
->lch_map
[c
->dma_ch
] = NULL
;
567 vchan_free_chan_resources(&c
->vc
);
568 omap_free_dma(c
->dma_ch
);
570 dev_dbg(od
->ddev
.dev
, "freeing channel for %u\n", c
->dma_sig
);
574 static size_t omap_dma_sg_size(struct omap_sg
*sg
)
576 return sg
->en
* sg
->fn
;
579 static size_t omap_dma_desc_size(struct omap_desc
*d
)
584 for (size
= i
= 0; i
< d
->sglen
; i
++)
585 size
+= omap_dma_sg_size(&d
->sg
[i
]);
587 return size
* es_bytes
[d
->es
];
590 static size_t omap_dma_desc_size_pos(struct omap_desc
*d
, dma_addr_t addr
)
593 size_t size
, es_size
= es_bytes
[d
->es
];
595 for (size
= i
= 0; i
< d
->sglen
; i
++) {
596 size_t this_size
= omap_dma_sg_size(&d
->sg
[i
]) * es_size
;
600 else if (addr
>= d
->sg
[i
].addr
&&
601 addr
< d
->sg
[i
].addr
+ this_size
)
602 size
+= d
->sg
[i
].addr
+ this_size
- addr
;
608 * OMAP 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
609 * read before the DMA controller finished disabling the channel.
611 static uint32_t omap_dma_chan_read_3_3(struct omap_chan
*c
, unsigned reg
)
613 struct omap_dmadev
*od
= to_omap_dma_dev(c
->vc
.chan
.device
);
616 val
= omap_dma_chan_read(c
, reg
);
617 if (val
== 0 && od
->plat
->errata
& DMA_ERRATA_3_3
)
618 val
= omap_dma_chan_read(c
, reg
);
623 static dma_addr_t
omap_dma_get_src_pos(struct omap_chan
*c
)
625 struct omap_dmadev
*od
= to_omap_dma_dev(c
->vc
.chan
.device
);
626 dma_addr_t addr
, cdac
;
628 if (__dma_omap15xx(od
->plat
->dma_attr
)) {
629 addr
= omap_dma_chan_read(c
, CPC
);
631 addr
= omap_dma_chan_read_3_3(c
, CSAC
);
632 cdac
= omap_dma_chan_read_3_3(c
, CDAC
);
635 * CDAC == 0 indicates that the DMA transfer on the channel has
636 * not been started (no data has been transferred so far).
637 * Return the programmed source start address in this case.
640 addr
= omap_dma_chan_read(c
, CSSA
);
644 addr
|= omap_dma_chan_read(c
, CSSA
) & 0xffff0000;
649 static dma_addr_t
omap_dma_get_dst_pos(struct omap_chan
*c
)
651 struct omap_dmadev
*od
= to_omap_dma_dev(c
->vc
.chan
.device
);
654 if (__dma_omap15xx(od
->plat
->dma_attr
)) {
655 addr
= omap_dma_chan_read(c
, CPC
);
657 addr
= omap_dma_chan_read_3_3(c
, CDAC
);
660 * CDAC == 0 indicates that the DMA transfer on the channel
661 * has not been started (no data has been transferred so
662 * far). Return the programmed destination start address in
666 addr
= omap_dma_chan_read(c
, CDSA
);
670 addr
|= omap_dma_chan_read(c
, CDSA
) & 0xffff0000;
675 static enum dma_status
omap_dma_tx_status(struct dma_chan
*chan
,
676 dma_cookie_t cookie
, struct dma_tx_state
*txstate
)
678 struct omap_chan
*c
= to_omap_dma_chan(chan
);
679 struct virt_dma_desc
*vd
;
683 ret
= dma_cookie_status(chan
, cookie
, txstate
);
685 if (!c
->paused
&& c
->running
) {
686 uint32_t ccr
= omap_dma_chan_read(c
, CCR
);
688 * The channel is no longer active, set the return value
691 if (!(ccr
& CCR_ENABLE
))
695 if (ret
== DMA_COMPLETE
|| !txstate
)
698 spin_lock_irqsave(&c
->vc
.lock
, flags
);
699 vd
= vchan_find_desc(&c
->vc
, cookie
);
701 txstate
->residue
= omap_dma_desc_size(to_omap_dma_desc(&vd
->tx
));
702 } else if (c
->desc
&& c
->desc
->vd
.tx
.cookie
== cookie
) {
703 struct omap_desc
*d
= c
->desc
;
706 if (d
->dir
== DMA_MEM_TO_DEV
)
707 pos
= omap_dma_get_src_pos(c
);
708 else if (d
->dir
== DMA_DEV_TO_MEM
|| d
->dir
== DMA_MEM_TO_MEM
)
709 pos
= omap_dma_get_dst_pos(c
);
713 txstate
->residue
= omap_dma_desc_size_pos(d
, pos
);
715 txstate
->residue
= 0;
717 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
722 static void omap_dma_issue_pending(struct dma_chan
*chan
)
724 struct omap_chan
*c
= to_omap_dma_chan(chan
);
727 spin_lock_irqsave(&c
->vc
.lock
, flags
);
728 if (vchan_issue_pending(&c
->vc
) && !c
->desc
)
729 omap_dma_start_desc(c
);
730 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
733 static struct dma_async_tx_descriptor
*omap_dma_prep_slave_sg(
734 struct dma_chan
*chan
, struct scatterlist
*sgl
, unsigned sglen
,
735 enum dma_transfer_direction dir
, unsigned long tx_flags
, void *context
)
737 struct omap_dmadev
*od
= to_omap_dma_dev(chan
->device
);
738 struct omap_chan
*c
= to_omap_dma_chan(chan
);
739 enum dma_slave_buswidth dev_width
;
740 struct scatterlist
*sgent
;
743 unsigned i
, es
, en
, frame_bytes
;
746 if (dir
== DMA_DEV_TO_MEM
) {
747 dev_addr
= c
->cfg
.src_addr
;
748 dev_width
= c
->cfg
.src_addr_width
;
749 burst
= c
->cfg
.src_maxburst
;
750 } else if (dir
== DMA_MEM_TO_DEV
) {
751 dev_addr
= c
->cfg
.dst_addr
;
752 dev_width
= c
->cfg
.dst_addr_width
;
753 burst
= c
->cfg
.dst_maxburst
;
755 dev_err(chan
->device
->dev
, "%s: bad direction?\n", __func__
);
759 /* Bus width translates to the element size (ES) */
761 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
762 es
= CSDP_DATA_TYPE_8
;
764 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
765 es
= CSDP_DATA_TYPE_16
;
767 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
768 es
= CSDP_DATA_TYPE_32
;
770 default: /* not reached */
774 /* Now allocate and setup the descriptor. */
775 d
= kzalloc(sizeof(*d
) + sglen
* sizeof(d
->sg
[0]), GFP_ATOMIC
);
780 d
->dev_addr
= dev_addr
;
783 d
->ccr
= c
->ccr
| CCR_SYNC_FRAME
;
784 if (dir
== DMA_DEV_TO_MEM
)
785 d
->ccr
|= CCR_DST_AMODE_POSTINC
| CCR_SRC_AMODE_CONSTANT
;
787 d
->ccr
|= CCR_DST_AMODE_CONSTANT
| CCR_SRC_AMODE_POSTINC
;
789 d
->cicr
= CICR_DROP_IE
| CICR_BLOCK_IE
;
793 d
->cicr
|= CICR_TOUT_IE
;
795 if (dir
== DMA_DEV_TO_MEM
)
796 d
->csdp
|= CSDP_DST_PORT_EMIFF
| CSDP_SRC_PORT_TIPB
;
798 d
->csdp
|= CSDP_DST_PORT_TIPB
| CSDP_SRC_PORT_EMIFF
;
800 if (dir
== DMA_DEV_TO_MEM
)
801 d
->ccr
|= CCR_TRIGGER_SRC
;
803 d
->cicr
|= CICR_MISALIGNED_ERR_IE
| CICR_TRANS_ERR_IE
;
805 if (od
->plat
->errata
& DMA_ERRATA_PARALLEL_CHANNELS
)
806 d
->clnk_ctrl
= c
->dma_ch
;
809 * Build our scatterlist entries: each contains the address,
810 * the number of elements (EN) in each frame, and the number of
811 * frames (FN). Number of bytes for this entry = ES * EN * FN.
813 * Burst size translates to number of elements with frame sync.
814 * Note: DMA engine defines burst to be the number of dev-width
818 frame_bytes
= es_bytes
[es
] * en
;
819 for_each_sg(sgl
, sgent
, sglen
, i
) {
820 d
->sg
[i
].addr
= sg_dma_address(sgent
);
822 d
->sg
[i
].fn
= sg_dma_len(sgent
) / frame_bytes
;
827 return vchan_tx_prep(&c
->vc
, &d
->vd
, tx_flags
);
830 static struct dma_async_tx_descriptor
*omap_dma_prep_dma_cyclic(
831 struct dma_chan
*chan
, dma_addr_t buf_addr
, size_t buf_len
,
832 size_t period_len
, enum dma_transfer_direction dir
, unsigned long flags
)
834 struct omap_dmadev
*od
= to_omap_dma_dev(chan
->device
);
835 struct omap_chan
*c
= to_omap_dma_chan(chan
);
836 enum dma_slave_buswidth dev_width
;
842 if (dir
== DMA_DEV_TO_MEM
) {
843 dev_addr
= c
->cfg
.src_addr
;
844 dev_width
= c
->cfg
.src_addr_width
;
845 burst
= c
->cfg
.src_maxburst
;
846 } else if (dir
== DMA_MEM_TO_DEV
) {
847 dev_addr
= c
->cfg
.dst_addr
;
848 dev_width
= c
->cfg
.dst_addr_width
;
849 burst
= c
->cfg
.dst_maxburst
;
851 dev_err(chan
->device
->dev
, "%s: bad direction?\n", __func__
);
855 /* Bus width translates to the element size (ES) */
857 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
858 es
= CSDP_DATA_TYPE_8
;
860 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
861 es
= CSDP_DATA_TYPE_16
;
863 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
864 es
= CSDP_DATA_TYPE_32
;
866 default: /* not reached */
870 /* Now allocate and setup the descriptor. */
871 d
= kzalloc(sizeof(*d
) + sizeof(d
->sg
[0]), GFP_ATOMIC
);
876 d
->dev_addr
= dev_addr
;
879 d
->sg
[0].addr
= buf_addr
;
880 d
->sg
[0].en
= period_len
/ es_bytes
[es
];
881 d
->sg
[0].fn
= buf_len
/ period_len
;
885 if (dir
== DMA_DEV_TO_MEM
)
886 d
->ccr
|= CCR_DST_AMODE_POSTINC
| CCR_SRC_AMODE_CONSTANT
;
888 d
->ccr
|= CCR_DST_AMODE_CONSTANT
| CCR_SRC_AMODE_POSTINC
;
890 d
->cicr
= CICR_DROP_IE
;
891 if (flags
& DMA_PREP_INTERRUPT
)
892 d
->cicr
|= CICR_FRAME_IE
;
897 d
->cicr
|= CICR_TOUT_IE
;
899 if (dir
== DMA_DEV_TO_MEM
)
900 d
->csdp
|= CSDP_DST_PORT_EMIFF
| CSDP_SRC_PORT_MPUI
;
902 d
->csdp
|= CSDP_DST_PORT_MPUI
| CSDP_SRC_PORT_EMIFF
;
905 d
->ccr
|= CCR_SYNC_PACKET
;
907 d
->ccr
|= CCR_SYNC_ELEMENT
;
909 if (dir
== DMA_DEV_TO_MEM
) {
910 d
->ccr
|= CCR_TRIGGER_SRC
;
911 d
->csdp
|= CSDP_DST_PACKED
;
913 d
->csdp
|= CSDP_SRC_PACKED
;
916 d
->cicr
|= CICR_MISALIGNED_ERR_IE
| CICR_TRANS_ERR_IE
;
918 d
->csdp
|= CSDP_DST_BURST_64
| CSDP_SRC_BURST_64
;
921 if (__dma_omap15xx(od
->plat
->dma_attr
))
922 d
->ccr
|= CCR_AUTO_INIT
| CCR_REPEAT
;
924 d
->clnk_ctrl
= c
->dma_ch
| CLNK_CTRL_ENABLE_LNK
;
928 return vchan_tx_prep(&c
->vc
, &d
->vd
, flags
);
931 static struct dma_async_tx_descriptor
*omap_dma_prep_dma_memcpy(
932 struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
933 size_t len
, unsigned long tx_flags
)
935 struct omap_chan
*c
= to_omap_dma_chan(chan
);
939 d
= kzalloc(sizeof(*d
) + sizeof(d
->sg
[0]), GFP_ATOMIC
);
943 data_type
= __ffs((src
| dest
| len
));
944 if (data_type
> CSDP_DATA_TYPE_32
)
945 data_type
= CSDP_DATA_TYPE_32
;
947 d
->dir
= DMA_MEM_TO_MEM
;
951 d
->sg
[0].en
= len
/ BIT(data_type
);
953 d
->sg
[0].addr
= dest
;
956 d
->ccr
|= CCR_DST_AMODE_POSTINC
| CCR_SRC_AMODE_POSTINC
;
958 d
->cicr
= CICR_DROP_IE
| CICR_FRAME_IE
;
963 d
->cicr
|= CICR_TOUT_IE
;
964 d
->csdp
|= CSDP_DST_PORT_EMIFF
| CSDP_SRC_PORT_EMIFF
;
966 d
->csdp
|= CSDP_DST_PACKED
| CSDP_SRC_PACKED
;
967 d
->cicr
|= CICR_MISALIGNED_ERR_IE
| CICR_TRANS_ERR_IE
;
968 d
->csdp
|= CSDP_DST_BURST_64
| CSDP_SRC_BURST_64
;
971 return vchan_tx_prep(&c
->vc
, &d
->vd
, tx_flags
);
974 static int omap_dma_slave_config(struct dma_chan
*chan
, struct dma_slave_config
*cfg
)
976 struct omap_chan
*c
= to_omap_dma_chan(chan
);
978 if (cfg
->src_addr_width
== DMA_SLAVE_BUSWIDTH_8_BYTES
||
979 cfg
->dst_addr_width
== DMA_SLAVE_BUSWIDTH_8_BYTES
)
982 memcpy(&c
->cfg
, cfg
, sizeof(c
->cfg
));
987 static int omap_dma_terminate_all(struct dma_chan
*chan
)
989 struct omap_chan
*c
= to_omap_dma_chan(chan
);
993 spin_lock_irqsave(&c
->vc
.lock
, flags
);
996 * Stop DMA activity: we assume the callback will not be called
997 * after omap_dma_stop() returns (even if it does, it will see
998 * c->desc is NULL and exit.)
1001 omap_dma_desc_free(&c
->desc
->vd
);
1003 /* Avoid stopping the dma twice */
1013 vchan_get_all_descriptors(&c
->vc
, &head
);
1014 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
1015 vchan_dma_desc_free_list(&c
->vc
, &head
);
1020 static void omap_dma_synchronize(struct dma_chan
*chan
)
1022 struct omap_chan
*c
= to_omap_dma_chan(chan
);
1024 vchan_synchronize(&c
->vc
);
1027 static int omap_dma_pause(struct dma_chan
*chan
)
1029 struct omap_chan
*c
= to_omap_dma_chan(chan
);
1031 /* Pause/Resume only allowed with cyclic mode */
1043 static int omap_dma_resume(struct dma_chan
*chan
)
1045 struct omap_chan
*c
= to_omap_dma_chan(chan
);
1047 /* Pause/Resume only allowed with cyclic mode */
1054 /* Restore channel link register */
1055 omap_dma_chan_write(c
, CLNK_CTRL
, c
->desc
->clnk_ctrl
);
1057 omap_dma_start(c
, c
->desc
);
1064 static int omap_dma_chan_init(struct omap_dmadev
*od
)
1066 struct omap_chan
*c
;
1068 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
1072 c
->reg_map
= od
->reg_map
;
1073 c
->vc
.desc_free
= omap_dma_desc_free
;
1074 vchan_init(&c
->vc
, &od
->ddev
);
1079 static void omap_dma_free(struct omap_dmadev
*od
)
1081 while (!list_empty(&od
->ddev
.channels
)) {
1082 struct omap_chan
*c
= list_first_entry(&od
->ddev
.channels
,
1083 struct omap_chan
, vc
.chan
.device_node
);
1085 list_del(&c
->vc
.chan
.device_node
);
1086 tasklet_kill(&c
->vc
.task
);
1091 #define OMAP_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1092 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
1093 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1095 static int omap_dma_probe(struct platform_device
*pdev
)
1097 struct omap_dmadev
*od
;
1098 struct resource
*res
;
1101 od
= devm_kzalloc(&pdev
->dev
, sizeof(*od
), GFP_KERNEL
);
1105 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1106 od
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1107 if (IS_ERR(od
->base
))
1108 return PTR_ERR(od
->base
);
1110 od
->plat
= omap_get_plat_info();
1112 return -EPROBE_DEFER
;
1114 od
->reg_map
= od
->plat
->reg_map
;
1116 dma_cap_set(DMA_SLAVE
, od
->ddev
.cap_mask
);
1117 dma_cap_set(DMA_CYCLIC
, od
->ddev
.cap_mask
);
1118 dma_cap_set(DMA_MEMCPY
, od
->ddev
.cap_mask
);
1119 od
->ddev
.device_alloc_chan_resources
= omap_dma_alloc_chan_resources
;
1120 od
->ddev
.device_free_chan_resources
= omap_dma_free_chan_resources
;
1121 od
->ddev
.device_tx_status
= omap_dma_tx_status
;
1122 od
->ddev
.device_issue_pending
= omap_dma_issue_pending
;
1123 od
->ddev
.device_prep_slave_sg
= omap_dma_prep_slave_sg
;
1124 od
->ddev
.device_prep_dma_cyclic
= omap_dma_prep_dma_cyclic
;
1125 od
->ddev
.device_prep_dma_memcpy
= omap_dma_prep_dma_memcpy
;
1126 od
->ddev
.device_config
= omap_dma_slave_config
;
1127 od
->ddev
.device_pause
= omap_dma_pause
;
1128 od
->ddev
.device_resume
= omap_dma_resume
;
1129 od
->ddev
.device_terminate_all
= omap_dma_terminate_all
;
1130 od
->ddev
.device_synchronize
= omap_dma_synchronize
;
1131 od
->ddev
.src_addr_widths
= OMAP_DMA_BUSWIDTHS
;
1132 od
->ddev
.dst_addr_widths
= OMAP_DMA_BUSWIDTHS
;
1133 od
->ddev
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
1134 od
->ddev
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
1135 od
->ddev
.dev
= &pdev
->dev
;
1136 INIT_LIST_HEAD(&od
->ddev
.channels
);
1137 spin_lock_init(&od
->lock
);
1138 spin_lock_init(&od
->irq_lock
);
1140 od
->dma_requests
= OMAP_SDMA_REQUESTS
;
1141 if (pdev
->dev
.of_node
&& of_property_read_u32(pdev
->dev
.of_node
,
1143 &od
->dma_requests
)) {
1144 dev_info(&pdev
->dev
,
1145 "Missing dma-requests property, using %u.\n",
1146 OMAP_SDMA_REQUESTS
);
1149 for (i
= 0; i
< OMAP_SDMA_CHANNELS
; i
++) {
1150 rc
= omap_dma_chan_init(od
);
1157 irq
= platform_get_irq(pdev
, 1);
1159 dev_info(&pdev
->dev
, "failed to get L1 IRQ: %d\n", irq
);
1162 /* Disable all interrupts */
1163 od
->irq_enable_mask
= 0;
1164 omap_dma_glbl_write(od
, IRQENABLE_L1
, 0);
1166 rc
= devm_request_irq(&pdev
->dev
, irq
, omap_dma_irq
,
1167 IRQF_SHARED
, "omap-dma-engine", od
);
1172 od
->ddev
.filter
.map
= od
->plat
->slave_map
;
1173 od
->ddev
.filter
.mapcnt
= od
->plat
->slavecnt
;
1174 od
->ddev
.filter
.fn
= omap_dma_filter_fn
;
1176 rc
= dma_async_device_register(&od
->ddev
);
1178 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
1184 platform_set_drvdata(pdev
, od
);
1186 if (pdev
->dev
.of_node
) {
1187 omap_dma_info
.dma_cap
= od
->ddev
.cap_mask
;
1189 /* Device-tree DMA controller registration */
1190 rc
= of_dma_controller_register(pdev
->dev
.of_node
,
1191 of_dma_simple_xlate
, &omap_dma_info
);
1193 pr_warn("OMAP-DMA: failed to register DMA controller\n");
1194 dma_async_device_unregister(&od
->ddev
);
1199 dev_info(&pdev
->dev
, "OMAP DMA engine driver\n");
1204 static int omap_dma_remove(struct platform_device
*pdev
)
1206 struct omap_dmadev
*od
= platform_get_drvdata(pdev
);
1208 if (pdev
->dev
.of_node
)
1209 of_dma_controller_free(pdev
->dev
.of_node
);
1211 dma_async_device_unregister(&od
->ddev
);
1214 /* Disable all interrupts */
1215 omap_dma_glbl_write(od
, IRQENABLE_L0
, 0);
1223 static const struct of_device_id omap_dma_match
[] = {
1224 { .compatible
= "ti,omap2420-sdma", },
1225 { .compatible
= "ti,omap2430-sdma", },
1226 { .compatible
= "ti,omap3430-sdma", },
1227 { .compatible
= "ti,omap3630-sdma", },
1228 { .compatible
= "ti,omap4430-sdma", },
1231 MODULE_DEVICE_TABLE(of
, omap_dma_match
);
1233 static struct platform_driver omap_dma_driver
= {
1234 .probe
= omap_dma_probe
,
1235 .remove
= omap_dma_remove
,
1237 .name
= "omap-dma-engine",
1238 .of_match_table
= of_match_ptr(omap_dma_match
),
1242 bool omap_dma_filter_fn(struct dma_chan
*chan
, void *param
)
1244 if (chan
->device
->dev
->driver
== &omap_dma_driver
.driver
) {
1245 struct omap_dmadev
*od
= to_omap_dma_dev(chan
->device
);
1246 struct omap_chan
*c
= to_omap_dma_chan(chan
);
1247 unsigned req
= *(unsigned *)param
;
1249 if (req
<= od
->dma_requests
) {
1256 EXPORT_SYMBOL_GPL(omap_dma_filter_fn
);
1258 static int omap_dma_init(void)
1260 return platform_driver_register(&omap_dma_driver
);
1262 subsys_initcall(omap_dma_init
);
1264 static void __exit
omap_dma_exit(void)
1266 platform_driver_unregister(&omap_dma_driver
);
1268 module_exit(omap_dma_exit
);
1270 MODULE_AUTHOR("Russell King");
1271 MODULE_LICENSE("GPL");