2 * DMA Engine support for Tsi721 PCIExpress-to-SRIO bridge
4 * Copyright (c) 2011-2014 Integrated Device Technology, Inc.
5 * Alexandre Bounine <alexandre.bounine@idt.com>
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 Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * The full GNU General Public License is included in this distribution in the
18 * file called COPYING.
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/ioport.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/rio.h>
29 #include <linux/rio_drv.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/interrupt.h>
32 #include <linux/kfifo.h>
33 #include <linux/sched.h>
34 #include <linux/delay.h>
35 #include "../../dma/dmaengine.h"
40 static irqreturn_t
tsi721_bdma_msix(int irq
, void *ptr
);
42 static int tsi721_submit_sg(struct tsi721_tx_desc
*desc
);
44 static unsigned int dma_desc_per_channel
= 128;
45 module_param(dma_desc_per_channel
, uint
, S_IRUGO
);
46 MODULE_PARM_DESC(dma_desc_per_channel
,
47 "Number of DMA descriptors per channel (default: 128)");
49 static unsigned int dma_txqueue_sz
= 16;
50 module_param(dma_txqueue_sz
, uint
, S_IRUGO
);
51 MODULE_PARM_DESC(dma_txqueue_sz
,
52 "DMA Transactions Queue Size (default: 16)");
54 static u8 dma_sel
= 0x7f;
55 module_param(dma_sel
, byte
, S_IRUGO
);
56 MODULE_PARM_DESC(dma_sel
,
57 "DMA Channel Selection Mask (default: 0x7f = all)");
59 static inline struct tsi721_bdma_chan
*to_tsi721_chan(struct dma_chan
*chan
)
61 return container_of(chan
, struct tsi721_bdma_chan
, dchan
);
64 static inline struct tsi721_device
*to_tsi721(struct dma_device
*ddev
)
66 return container_of(ddev
, struct rio_mport
, dma
)->priv
;
70 struct tsi721_tx_desc
*to_tsi721_desc(struct dma_async_tx_descriptor
*txd
)
72 return container_of(txd
, struct tsi721_tx_desc
, txd
);
75 static int tsi721_bdma_ch_init(struct tsi721_bdma_chan
*bdma_chan
, int bd_num
)
77 struct tsi721_dma_desc
*bd_ptr
;
78 struct device
*dev
= bdma_chan
->dchan
.device
->dev
;
84 struct tsi721_device
*priv
= to_tsi721(bdma_chan
->dchan
.device
);
87 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
, "DMAC%d", bdma_chan
->id
);
90 * Allocate space for DMA descriptors
91 * (add an extra element for link descriptor)
93 bd_ptr
= dma_alloc_coherent(dev
,
94 (bd_num
+ 1) * sizeof(struct tsi721_dma_desc
),
95 &bd_phys
, GFP_ATOMIC
);
99 bdma_chan
->bd_num
= bd_num
;
100 bdma_chan
->bd_phys
= bd_phys
;
101 bdma_chan
->bd_base
= bd_ptr
;
103 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
,
104 "DMAC%d descriptors @ %p (phys = %pad)",
105 bdma_chan
->id
, bd_ptr
, &bd_phys
);
107 /* Allocate space for descriptor status FIFO */
108 sts_size
= ((bd_num
+ 1) >= TSI721_DMA_MINSTSSZ
) ?
109 (bd_num
+ 1) : TSI721_DMA_MINSTSSZ
;
110 sts_size
= roundup_pow_of_two(sts_size
);
111 sts_ptr
= dma_alloc_coherent(dev
,
112 sts_size
* sizeof(struct tsi721_dma_sts
),
113 &sts_phys
, GFP_ATOMIC
);
115 /* Free space allocated for DMA descriptors */
116 dma_free_coherent(dev
,
117 (bd_num
+ 1) * sizeof(struct tsi721_dma_desc
),
119 bdma_chan
->bd_base
= NULL
;
123 bdma_chan
->sts_phys
= sts_phys
;
124 bdma_chan
->sts_base
= sts_ptr
;
125 bdma_chan
->sts_size
= sts_size
;
127 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
,
128 "DMAC%d desc status FIFO @ %p (phys = %pad) size=0x%x",
129 bdma_chan
->id
, sts_ptr
, &sts_phys
, sts_size
);
131 /* Initialize DMA descriptors ring using added link descriptor */
132 bd_ptr
[bd_num
].type_id
= cpu_to_le32(DTYPE3
<< 29);
133 bd_ptr
[bd_num
].next_lo
= cpu_to_le32((u64
)bd_phys
&
134 TSI721_DMAC_DPTRL_MASK
);
135 bd_ptr
[bd_num
].next_hi
= cpu_to_le32((u64
)bd_phys
>> 32);
137 /* Setup DMA descriptor pointers */
138 iowrite32(((u64
)bd_phys
>> 32),
139 bdma_chan
->regs
+ TSI721_DMAC_DPTRH
);
140 iowrite32(((u64
)bd_phys
& TSI721_DMAC_DPTRL_MASK
),
141 bdma_chan
->regs
+ TSI721_DMAC_DPTRL
);
143 /* Setup descriptor status FIFO */
144 iowrite32(((u64
)sts_phys
>> 32),
145 bdma_chan
->regs
+ TSI721_DMAC_DSBH
);
146 iowrite32(((u64
)sts_phys
& TSI721_DMAC_DSBL_MASK
),
147 bdma_chan
->regs
+ TSI721_DMAC_DSBL
);
148 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size
),
149 bdma_chan
->regs
+ TSI721_DMAC_DSSZ
);
151 /* Clear interrupt bits */
152 iowrite32(TSI721_DMAC_INT_ALL
,
153 bdma_chan
->regs
+ TSI721_DMAC_INT
);
155 ioread32(bdma_chan
->regs
+ TSI721_DMAC_INT
);
157 #ifdef CONFIG_PCI_MSI
158 /* Request interrupt service if we are in MSI-X mode */
159 if (priv
->flags
& TSI721_USING_MSIX
) {
162 idx
= TSI721_VECT_DMA0_DONE
+ bdma_chan
->id
;
164 rc
= request_irq(priv
->msix
[idx
].vector
, tsi721_bdma_msix
, 0,
165 priv
->msix
[idx
].irq_name
, (void *)bdma_chan
);
168 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
,
169 "Unable to get MSI-X for DMAC%d-DONE",
174 idx
= TSI721_VECT_DMA0_INT
+ bdma_chan
->id
;
176 rc
= request_irq(priv
->msix
[idx
].vector
, tsi721_bdma_msix
, 0,
177 priv
->msix
[idx
].irq_name
, (void *)bdma_chan
);
180 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
,
181 "Unable to get MSI-X for DMAC%d-INT",
184 priv
->msix
[TSI721_VECT_DMA0_DONE
+
185 bdma_chan
->id
].vector
,
191 /* Free space allocated for DMA descriptors */
192 dma_free_coherent(dev
,
193 (bd_num
+ 1) * sizeof(struct tsi721_dma_desc
),
195 bdma_chan
->bd_base
= NULL
;
197 /* Free space allocated for status descriptors */
198 dma_free_coherent(dev
,
199 sts_size
* sizeof(struct tsi721_dma_sts
),
201 bdma_chan
->sts_base
= NULL
;
206 #endif /* CONFIG_PCI_MSI */
208 /* Toggle DMA channel initialization */
209 iowrite32(TSI721_DMAC_CTL_INIT
, bdma_chan
->regs
+ TSI721_DMAC_CTL
);
210 ioread32(bdma_chan
->regs
+ TSI721_DMAC_CTL
);
211 bdma_chan
->wr_count
= bdma_chan
->wr_count_next
= 0;
212 bdma_chan
->sts_rdptr
= 0;
218 static int tsi721_bdma_ch_free(struct tsi721_bdma_chan
*bdma_chan
)
221 #ifdef CONFIG_PCI_MSI
222 struct tsi721_device
*priv
= to_tsi721(bdma_chan
->dchan
.device
);
225 if (!bdma_chan
->bd_base
)
228 /* Check if DMA channel still running */
229 ch_stat
= ioread32(bdma_chan
->regs
+ TSI721_DMAC_STS
);
230 if (ch_stat
& TSI721_DMAC_STS_RUN
)
233 /* Put DMA channel into init state */
234 iowrite32(TSI721_DMAC_CTL_INIT
, bdma_chan
->regs
+ TSI721_DMAC_CTL
);
236 #ifdef CONFIG_PCI_MSI
237 if (priv
->flags
& TSI721_USING_MSIX
) {
238 free_irq(priv
->msix
[TSI721_VECT_DMA0_DONE
+
239 bdma_chan
->id
].vector
, (void *)bdma_chan
);
240 free_irq(priv
->msix
[TSI721_VECT_DMA0_INT
+
241 bdma_chan
->id
].vector
, (void *)bdma_chan
);
243 #endif /* CONFIG_PCI_MSI */
245 /* Free space allocated for DMA descriptors */
246 dma_free_coherent(bdma_chan
->dchan
.device
->dev
,
247 (bdma_chan
->bd_num
+ 1) * sizeof(struct tsi721_dma_desc
),
248 bdma_chan
->bd_base
, bdma_chan
->bd_phys
);
249 bdma_chan
->bd_base
= NULL
;
251 /* Free space allocated for status FIFO */
252 dma_free_coherent(bdma_chan
->dchan
.device
->dev
,
253 bdma_chan
->sts_size
* sizeof(struct tsi721_dma_sts
),
254 bdma_chan
->sts_base
, bdma_chan
->sts_phys
);
255 bdma_chan
->sts_base
= NULL
;
260 tsi721_bdma_interrupt_enable(struct tsi721_bdma_chan
*bdma_chan
, int enable
)
263 /* Clear pending BDMA channel interrupts */
264 iowrite32(TSI721_DMAC_INT_ALL
,
265 bdma_chan
->regs
+ TSI721_DMAC_INT
);
266 ioread32(bdma_chan
->regs
+ TSI721_DMAC_INT
);
267 /* Enable BDMA channel interrupts */
268 iowrite32(TSI721_DMAC_INT_ALL
,
269 bdma_chan
->regs
+ TSI721_DMAC_INTE
);
271 /* Disable BDMA channel interrupts */
272 iowrite32(0, bdma_chan
->regs
+ TSI721_DMAC_INTE
);
273 /* Clear pending BDMA channel interrupts */
274 iowrite32(TSI721_DMAC_INT_ALL
,
275 bdma_chan
->regs
+ TSI721_DMAC_INT
);
280 static bool tsi721_dma_is_idle(struct tsi721_bdma_chan
*bdma_chan
)
284 sts
= ioread32(bdma_chan
->regs
+ TSI721_DMAC_STS
);
285 return ((sts
& TSI721_DMAC_STS_RUN
) == 0);
288 void tsi721_bdma_handler(struct tsi721_bdma_chan
*bdma_chan
)
290 /* Disable BDMA channel interrupts */
291 iowrite32(0, bdma_chan
->regs
+ TSI721_DMAC_INTE
);
292 if (bdma_chan
->active
)
293 tasklet_hi_schedule(&bdma_chan
->tasklet
);
296 #ifdef CONFIG_PCI_MSI
298 * tsi721_omsg_msix - MSI-X interrupt handler for BDMA channels
299 * @irq: Linux interrupt number
300 * @ptr: Pointer to interrupt-specific data (BDMA channel structure)
302 * Handles BDMA channel interrupts signaled using MSI-X.
304 static irqreturn_t
tsi721_bdma_msix(int irq
, void *ptr
)
306 struct tsi721_bdma_chan
*bdma_chan
= ptr
;
308 if (bdma_chan
->active
)
309 tasklet_hi_schedule(&bdma_chan
->tasklet
);
312 #endif /* CONFIG_PCI_MSI */
314 /* Must be called with the spinlock held */
315 static void tsi721_start_dma(struct tsi721_bdma_chan
*bdma_chan
)
317 if (!tsi721_dma_is_idle(bdma_chan
)) {
318 tsi_err(&bdma_chan
->dchan
.dev
->device
,
319 "DMAC%d Attempt to start non-idle channel",
324 if (bdma_chan
->wr_count
== bdma_chan
->wr_count_next
) {
325 tsi_err(&bdma_chan
->dchan
.dev
->device
,
326 "DMAC%d Attempt to start DMA with no BDs ready %d",
327 bdma_chan
->id
, task_pid_nr(current
));
331 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
, "DMAC%d (wrc=%d) %d",
332 bdma_chan
->id
, bdma_chan
->wr_count_next
,
333 task_pid_nr(current
));
335 iowrite32(bdma_chan
->wr_count_next
,
336 bdma_chan
->regs
+ TSI721_DMAC_DWRCNT
);
337 ioread32(bdma_chan
->regs
+ TSI721_DMAC_DWRCNT
);
339 bdma_chan
->wr_count
= bdma_chan
->wr_count_next
;
343 tsi721_desc_fill_init(struct tsi721_tx_desc
*desc
,
344 struct tsi721_dma_desc
*bd_ptr
,
345 struct scatterlist
*sg
, u32 sys_size
)
352 /* Initialize DMA descriptor */
353 bd_ptr
->type_id
= cpu_to_le32((DTYPE1
<< 29) |
354 (desc
->rtype
<< 19) | desc
->destid
);
355 bd_ptr
->bcount
= cpu_to_le32(((desc
->rio_addr
& 0x3) << 30) |
357 rio_addr
= (desc
->rio_addr
>> 2) |
358 ((u64
)(desc
->rio_addr_u
& 0x3) << 62);
359 bd_ptr
->raddr_lo
= cpu_to_le32(rio_addr
& 0xffffffff);
360 bd_ptr
->raddr_hi
= cpu_to_le32(rio_addr
>> 32);
361 bd_ptr
->t1
.bufptr_lo
= cpu_to_le32(
362 (u64
)sg_dma_address(sg
) & 0xffffffff);
363 bd_ptr
->t1
.bufptr_hi
= cpu_to_le32((u64
)sg_dma_address(sg
) >> 32);
364 bd_ptr
->t1
.s_dist
= 0;
365 bd_ptr
->t1
.s_size
= 0;
371 tsi721_desc_fill_end(struct tsi721_dma_desc
*bd_ptr
, u32 bcount
, bool interrupt
)
376 /* Update DMA descriptor */
378 bd_ptr
->type_id
|= cpu_to_le32(TSI721_DMAD_IOF
);
379 bd_ptr
->bcount
|= cpu_to_le32(bcount
& TSI721_DMAD_BCOUNT1
);
384 static void tsi721_dma_tx_err(struct tsi721_bdma_chan
*bdma_chan
,
385 struct tsi721_tx_desc
*desc
)
387 struct dma_async_tx_descriptor
*txd
= &desc
->txd
;
388 dma_async_tx_callback callback
= txd
->callback
;
389 void *param
= txd
->callback_param
;
391 list_move(&desc
->desc_node
, &bdma_chan
->free_list
);
397 static void tsi721_clr_stat(struct tsi721_bdma_chan
*bdma_chan
)
403 /* Check and clear descriptor status FIFO entries */
404 srd_ptr
= bdma_chan
->sts_rdptr
;
405 sts_ptr
= bdma_chan
->sts_base
;
408 for (i
= 0; i
< 8 && sts_ptr
[j
]; i
++, j
++)
412 srd_ptr
%= bdma_chan
->sts_size
;
416 iowrite32(srd_ptr
, bdma_chan
->regs
+ TSI721_DMAC_DSRP
);
417 bdma_chan
->sts_rdptr
= srd_ptr
;
420 /* Must be called with the channel spinlock held */
421 static int tsi721_submit_sg(struct tsi721_tx_desc
*desc
)
423 struct dma_chan
*dchan
= desc
->txd
.chan
;
424 struct tsi721_bdma_chan
*bdma_chan
= to_tsi721_chan(dchan
);
427 dma_addr_t next_addr
;
429 struct scatterlist
*sg
;
432 struct tsi721_dma_desc
*bd_ptr
= NULL
;
435 struct device
*ch_dev
= &dchan
->dev
->device
;
437 if (!tsi721_dma_is_idle(bdma_chan
)) {
438 tsi_err(ch_dev
, "DMAC%d ERR: Attempt to use non-idle channel",
444 * Fill DMA channel's hardware buffer descriptors.
445 * (NOTE: RapidIO destination address is limited to 64 bits for now)
447 rio_addr
= desc
->rio_addr
;
450 sys_size
= dma_to_mport(dchan
->device
)->sys_size
;
452 rd_idx
= ioread32(bdma_chan
->regs
+ TSI721_DMAC_DRDCNT
);
453 rd_idx
%= (bdma_chan
->bd_num
+ 1);
455 idx
= bdma_chan
->wr_count_next
% (bdma_chan
->bd_num
+ 1);
456 if (idx
== bdma_chan
->bd_num
) {
457 /* wrap around link descriptor */
462 tsi_debug(DMA
, ch_dev
, "DMAC%d BD ring status: rdi=%d wri=%d",
463 bdma_chan
->id
, rd_idx
, idx
);
465 for_each_sg(desc
->sg
, sg
, desc
->sg_len
, i
) {
467 tsi_debug(DMAV
, ch_dev
, "DMAC%d sg%d/%d addr: 0x%llx len: %d",
468 bdma_chan
->id
, i
, desc
->sg_len
,
469 (unsigned long long)sg_dma_address(sg
), sg_dma_len(sg
));
471 if (sg_dma_len(sg
) > TSI721_BDMA_MAX_BCOUNT
) {
472 tsi_err(ch_dev
, "DMAC%d SG entry %d is too large",
479 * If this sg entry forms contiguous block with previous one,
480 * try to merge it into existing DMA descriptor
482 if (next_addr
== sg_dma_address(sg
) &&
483 bcount
+ sg_dma_len(sg
) <= TSI721_BDMA_MAX_BCOUNT
) {
484 /* Adjust byte count of the descriptor */
485 bcount
+= sg_dma_len(sg
);
487 } else if (next_addr
!= -1) {
488 /* Finalize descriptor using total byte count value */
489 tsi721_desc_fill_end(bd_ptr
, bcount
, 0);
490 tsi_debug(DMAV
, ch_dev
, "DMAC%d prev desc final len: %d",
491 bdma_chan
->id
, bcount
);
494 desc
->rio_addr
= rio_addr
;
496 if (i
&& idx
== rd_idx
) {
497 tsi_debug(DMAV
, ch_dev
,
498 "DMAC%d HW descriptor ring is full @ %d",
505 bd_ptr
= &((struct tsi721_dma_desc
*)bdma_chan
->bd_base
)[idx
];
506 err
= tsi721_desc_fill_init(desc
, bd_ptr
, sg
, sys_size
);
508 tsi_err(ch_dev
, "Failed to build desc: err=%d", err
);
512 tsi_debug(DMAV
, ch_dev
, "DMAC%d bd_ptr = %p did=%d raddr=0x%llx",
513 bdma_chan
->id
, bd_ptr
, desc
->destid
, desc
->rio_addr
);
515 next_addr
= sg_dma_address(sg
);
516 bcount
= sg_dma_len(sg
);
519 if (++idx
== bdma_chan
->bd_num
) {
520 /* wrap around link descriptor */
526 if (sg_is_last(sg
)) {
527 tsi721_desc_fill_end(bd_ptr
, bcount
, 0);
528 tsi_debug(DMAV
, ch_dev
,
529 "DMAC%d last desc final len: %d",
530 bdma_chan
->id
, bcount
);
533 rio_addr
+= sg_dma_len(sg
);
534 next_addr
+= sg_dma_len(sg
);
539 bdma_chan
->wr_count_next
+= add_count
;
544 static void tsi721_advance_work(struct tsi721_bdma_chan
*bdma_chan
,
545 struct tsi721_tx_desc
*desc
)
549 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
, "DMAC%d", bdma_chan
->id
);
551 if (!tsi721_dma_is_idle(bdma_chan
))
555 * If there is no data transfer in progress, fetch new descriptor from
558 if (!desc
&& !bdma_chan
->active_tx
&& !list_empty(&bdma_chan
->queue
)) {
559 desc
= list_first_entry(&bdma_chan
->queue
,
560 struct tsi721_tx_desc
, desc_node
);
561 list_del_init((&desc
->desc_node
));
562 bdma_chan
->active_tx
= desc
;
566 err
= tsi721_submit_sg(desc
);
568 tsi721_start_dma(bdma_chan
);
570 tsi721_dma_tx_err(bdma_chan
, desc
);
571 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
,
572 "DMAC%d ERR: tsi721_submit_sg failed with err=%d",
577 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
, "DMAC%d Exit",
581 static void tsi721_dma_tasklet(unsigned long data
)
583 struct tsi721_bdma_chan
*bdma_chan
= (struct tsi721_bdma_chan
*)data
;
584 u32 dmac_int
, dmac_sts
;
586 dmac_int
= ioread32(bdma_chan
->regs
+ TSI721_DMAC_INT
);
587 tsi_debug(DMA
, &bdma_chan
->dchan
.dev
->device
, "DMAC%d_INT = 0x%x",
588 bdma_chan
->id
, dmac_int
);
589 /* Clear channel interrupts */
590 iowrite32(dmac_int
, bdma_chan
->regs
+ TSI721_DMAC_INT
);
592 if (dmac_int
& TSI721_DMAC_INT_ERR
) {
594 struct tsi721_tx_desc
*desc
;
596 desc
= bdma_chan
->active_tx
;
597 dmac_sts
= ioread32(bdma_chan
->regs
+ TSI721_DMAC_STS
);
598 tsi_err(&bdma_chan
->dchan
.dev
->device
,
599 "DMAC%d_STS = 0x%x did=%d raddr=0x%llx",
600 bdma_chan
->id
, dmac_sts
, desc
->destid
, desc
->rio_addr
);
602 /* Re-initialize DMA channel if possible */
604 if ((dmac_sts
& TSI721_DMAC_STS_ABORT
) == 0)
607 tsi721_clr_stat(bdma_chan
);
609 spin_lock(&bdma_chan
->lock
);
611 /* Put DMA channel into init state */
612 iowrite32(TSI721_DMAC_CTL_INIT
,
613 bdma_chan
->regs
+ TSI721_DMAC_CTL
);
616 dmac_sts
= ioread32(bdma_chan
->regs
+ TSI721_DMAC_STS
);
618 } while ((dmac_sts
& TSI721_DMAC_STS_ABORT
) && i
);
620 if (dmac_sts
& TSI721_DMAC_STS_ABORT
) {
621 tsi_err(&bdma_chan
->dchan
.dev
->device
,
622 "Failed to re-initiate DMAC%d", bdma_chan
->id
);
623 spin_unlock(&bdma_chan
->lock
);
627 /* Setup DMA descriptor pointers */
628 iowrite32(((u64
)bdma_chan
->bd_phys
>> 32),
629 bdma_chan
->regs
+ TSI721_DMAC_DPTRH
);
630 iowrite32(((u64
)bdma_chan
->bd_phys
& TSI721_DMAC_DPTRL_MASK
),
631 bdma_chan
->regs
+ TSI721_DMAC_DPTRL
);
633 /* Setup descriptor status FIFO */
634 iowrite32(((u64
)bdma_chan
->sts_phys
>> 32),
635 bdma_chan
->regs
+ TSI721_DMAC_DSBH
);
636 iowrite32(((u64
)bdma_chan
->sts_phys
& TSI721_DMAC_DSBL_MASK
),
637 bdma_chan
->regs
+ TSI721_DMAC_DSBL
);
638 iowrite32(TSI721_DMAC_DSSZ_SIZE(bdma_chan
->sts_size
),
639 bdma_chan
->regs
+ TSI721_DMAC_DSSZ
);
641 /* Clear interrupt bits */
642 iowrite32(TSI721_DMAC_INT_ALL
,
643 bdma_chan
->regs
+ TSI721_DMAC_INT
);
645 ioread32(bdma_chan
->regs
+ TSI721_DMAC_INT
);
647 bdma_chan
->wr_count
= bdma_chan
->wr_count_next
= 0;
648 bdma_chan
->sts_rdptr
= 0;
651 desc
= bdma_chan
->active_tx
;
652 desc
->status
= DMA_ERROR
;
653 dma_cookie_complete(&desc
->txd
);
654 list_add(&desc
->desc_node
, &bdma_chan
->free_list
);
655 bdma_chan
->active_tx
= NULL
;
656 if (bdma_chan
->active
)
657 tsi721_advance_work(bdma_chan
, NULL
);
658 spin_unlock(&bdma_chan
->lock
);
661 if (dmac_int
& TSI721_DMAC_INT_STFULL
) {
662 tsi_err(&bdma_chan
->dchan
.dev
->device
,
663 "DMAC%d descriptor status FIFO is full",
667 if (dmac_int
& (TSI721_DMAC_INT_DONE
| TSI721_DMAC_INT_IOFDONE
)) {
668 struct tsi721_tx_desc
*desc
;
670 tsi721_clr_stat(bdma_chan
);
671 spin_lock(&bdma_chan
->lock
);
672 desc
= bdma_chan
->active_tx
;
674 if (desc
->sg_len
== 0) {
675 dma_async_tx_callback callback
= NULL
;
678 desc
->status
= DMA_COMPLETE
;
679 dma_cookie_complete(&desc
->txd
);
680 if (desc
->txd
.flags
& DMA_PREP_INTERRUPT
) {
681 callback
= desc
->txd
.callback
;
682 param
= desc
->txd
.callback_param
;
684 list_add(&desc
->desc_node
, &bdma_chan
->free_list
);
685 bdma_chan
->active_tx
= NULL
;
686 if (bdma_chan
->active
)
687 tsi721_advance_work(bdma_chan
, NULL
);
688 spin_unlock(&bdma_chan
->lock
);
692 if (bdma_chan
->active
)
693 tsi721_advance_work(bdma_chan
,
694 bdma_chan
->active_tx
);
695 spin_unlock(&bdma_chan
->lock
);
699 /* Re-Enable BDMA channel interrupts */
700 iowrite32(TSI721_DMAC_INT_ALL
, bdma_chan
->regs
+ TSI721_DMAC_INTE
);
703 static dma_cookie_t
tsi721_tx_submit(struct dma_async_tx_descriptor
*txd
)
705 struct tsi721_tx_desc
*desc
= to_tsi721_desc(txd
);
706 struct tsi721_bdma_chan
*bdma_chan
= to_tsi721_chan(txd
->chan
);
709 /* Check if the descriptor is detached from any lists */
710 if (!list_empty(&desc
->desc_node
)) {
711 tsi_err(&bdma_chan
->dchan
.dev
->device
,
712 "DMAC%d wrong state of descriptor %p",
717 spin_lock_bh(&bdma_chan
->lock
);
719 if (!bdma_chan
->active
) {
720 spin_unlock_bh(&bdma_chan
->lock
);
724 cookie
= dma_cookie_assign(txd
);
725 desc
->status
= DMA_IN_PROGRESS
;
726 list_add_tail(&desc
->desc_node
, &bdma_chan
->queue
);
727 tsi721_advance_work(bdma_chan
, NULL
);
729 spin_unlock_bh(&bdma_chan
->lock
);
733 static int tsi721_alloc_chan_resources(struct dma_chan
*dchan
)
735 struct tsi721_bdma_chan
*bdma_chan
= to_tsi721_chan(dchan
);
736 struct tsi721_tx_desc
*desc
;
739 tsi_debug(DMA
, &dchan
->dev
->device
, "DMAC%d", bdma_chan
->id
);
741 if (bdma_chan
->bd_base
)
742 return dma_txqueue_sz
;
744 /* Initialize BDMA channel */
745 if (tsi721_bdma_ch_init(bdma_chan
, dma_desc_per_channel
)) {
746 tsi_err(&dchan
->dev
->device
, "Unable to initialize DMAC%d",
751 /* Allocate queue of transaction descriptors */
752 desc
= kcalloc(dma_txqueue_sz
, sizeof(struct tsi721_tx_desc
),
755 tsi721_bdma_ch_free(bdma_chan
);
759 bdma_chan
->tx_desc
= desc
;
761 for (i
= 0; i
< dma_txqueue_sz
; i
++) {
762 dma_async_tx_descriptor_init(&desc
[i
].txd
, dchan
);
763 desc
[i
].txd
.tx_submit
= tsi721_tx_submit
;
764 desc
[i
].txd
.flags
= DMA_CTRL_ACK
;
765 list_add(&desc
[i
].desc_node
, &bdma_chan
->free_list
);
768 dma_cookie_init(dchan
);
770 bdma_chan
->active
= true;
771 tsi721_bdma_interrupt_enable(bdma_chan
, 1);
773 return dma_txqueue_sz
;
776 static void tsi721_sync_dma_irq(struct tsi721_bdma_chan
*bdma_chan
)
778 struct tsi721_device
*priv
= to_tsi721(bdma_chan
->dchan
.device
);
780 #ifdef CONFIG_PCI_MSI
781 if (priv
->flags
& TSI721_USING_MSIX
) {
782 synchronize_irq(priv
->msix
[TSI721_VECT_DMA0_DONE
+
783 bdma_chan
->id
].vector
);
784 synchronize_irq(priv
->msix
[TSI721_VECT_DMA0_INT
+
785 bdma_chan
->id
].vector
);
788 synchronize_irq(priv
->pdev
->irq
);
791 static void tsi721_free_chan_resources(struct dma_chan
*dchan
)
793 struct tsi721_bdma_chan
*bdma_chan
= to_tsi721_chan(dchan
);
795 tsi_debug(DMA
, &dchan
->dev
->device
, "DMAC%d", bdma_chan
->id
);
797 if (!bdma_chan
->bd_base
)
800 tsi721_bdma_interrupt_enable(bdma_chan
, 0);
801 bdma_chan
->active
= false;
802 tsi721_sync_dma_irq(bdma_chan
);
803 tasklet_kill(&bdma_chan
->tasklet
);
804 INIT_LIST_HEAD(&bdma_chan
->free_list
);
805 kfree(bdma_chan
->tx_desc
);
806 tsi721_bdma_ch_free(bdma_chan
);
810 enum dma_status
tsi721_tx_status(struct dma_chan
*dchan
, dma_cookie_t cookie
,
811 struct dma_tx_state
*txstate
)
813 struct tsi721_bdma_chan
*bdma_chan
= to_tsi721_chan(dchan
);
814 enum dma_status status
;
816 spin_lock_bh(&bdma_chan
->lock
);
817 status
= dma_cookie_status(dchan
, cookie
, txstate
);
818 spin_unlock_bh(&bdma_chan
->lock
);
822 static void tsi721_issue_pending(struct dma_chan
*dchan
)
824 struct tsi721_bdma_chan
*bdma_chan
= to_tsi721_chan(dchan
);
826 tsi_debug(DMA
, &dchan
->dev
->device
, "DMAC%d", bdma_chan
->id
);
828 spin_lock_bh(&bdma_chan
->lock
);
829 if (tsi721_dma_is_idle(bdma_chan
) && bdma_chan
->active
) {
830 tsi721_advance_work(bdma_chan
, NULL
);
832 spin_unlock_bh(&bdma_chan
->lock
);
836 struct dma_async_tx_descriptor
*tsi721_prep_rio_sg(struct dma_chan
*dchan
,
837 struct scatterlist
*sgl
, unsigned int sg_len
,
838 enum dma_transfer_direction dir
, unsigned long flags
,
841 struct tsi721_bdma_chan
*bdma_chan
= to_tsi721_chan(dchan
);
842 struct tsi721_tx_desc
*desc
;
843 struct rio_dma_ext
*rext
= tinfo
;
844 enum dma_rtype rtype
;
845 struct dma_async_tx_descriptor
*txd
= NULL
;
847 if (!sgl
|| !sg_len
) {
848 tsi_err(&dchan
->dev
->device
, "DMAC%d No SG list",
850 return ERR_PTR(-EINVAL
);
853 tsi_debug(DMA
, &dchan
->dev
->device
, "DMAC%d %s", bdma_chan
->id
,
854 (dir
== DMA_DEV_TO_MEM
)?"READ":"WRITE");
856 if (dir
== DMA_DEV_TO_MEM
)
858 else if (dir
== DMA_MEM_TO_DEV
) {
859 switch (rext
->wr_type
) {
863 case RDW_ALL_NWRITE_R
:
864 rtype
= ALL_NWRITE_R
;
866 case RDW_LAST_NWRITE_R
:
868 rtype
= LAST_NWRITE_R
;
872 tsi_err(&dchan
->dev
->device
,
873 "DMAC%d Unsupported DMA direction option",
875 return ERR_PTR(-EINVAL
);
878 spin_lock_bh(&bdma_chan
->lock
);
880 if (!list_empty(&bdma_chan
->free_list
)) {
881 desc
= list_first_entry(&bdma_chan
->free_list
,
882 struct tsi721_tx_desc
, desc_node
);
883 list_del_init(&desc
->desc_node
);
884 desc
->destid
= rext
->destid
;
885 desc
->rio_addr
= rext
->rio_addr
;
886 desc
->rio_addr_u
= 0;
888 desc
->sg_len
= sg_len
;
894 spin_unlock_bh(&bdma_chan
->lock
);
897 tsi_debug(DMA
, &dchan
->dev
->device
,
898 "DMAC%d free TXD is not available", bdma_chan
->id
);
899 return ERR_PTR(-EBUSY
);
905 static int tsi721_terminate_all(struct dma_chan
*dchan
)
907 struct tsi721_bdma_chan
*bdma_chan
= to_tsi721_chan(dchan
);
908 struct tsi721_tx_desc
*desc
, *_d
;
911 tsi_debug(DMA
, &dchan
->dev
->device
, "DMAC%d", bdma_chan
->id
);
913 spin_lock_bh(&bdma_chan
->lock
);
915 bdma_chan
->active
= false;
917 while (!tsi721_dma_is_idle(bdma_chan
)) {
921 /* make sure to stop the transfer */
922 iowrite32(TSI721_DMAC_CTL_SUSP
,
923 bdma_chan
->regs
+ TSI721_DMAC_CTL
);
925 /* Wait until DMA channel stops */
927 dmac_int
= ioread32(bdma_chan
->regs
+ TSI721_DMAC_INT
);
928 } while ((dmac_int
& TSI721_DMAC_INT_SUSP
) == 0);
932 if (bdma_chan
->active_tx
)
933 list_add(&bdma_chan
->active_tx
->desc_node
, &list
);
934 list_splice_init(&bdma_chan
->queue
, &list
);
936 list_for_each_entry_safe(desc
, _d
, &list
, desc_node
)
937 tsi721_dma_tx_err(bdma_chan
, desc
);
939 spin_unlock_bh(&bdma_chan
->lock
);
944 static void tsi721_dma_stop(struct tsi721_bdma_chan
*bdma_chan
)
946 if (!bdma_chan
->active
)
948 spin_lock_bh(&bdma_chan
->lock
);
949 if (!tsi721_dma_is_idle(bdma_chan
)) {
950 int timeout
= 100000;
952 /* stop the transfer in progress */
953 iowrite32(TSI721_DMAC_CTL_SUSP
,
954 bdma_chan
->regs
+ TSI721_DMAC_CTL
);
956 /* Wait until DMA channel stops */
957 while (!tsi721_dma_is_idle(bdma_chan
) && --timeout
)
961 spin_unlock_bh(&bdma_chan
->lock
);
964 void tsi721_dma_stop_all(struct tsi721_device
*priv
)
968 for (i
= 0; i
< TSI721_DMA_MAXCH
; i
++) {
969 if ((i
!= TSI721_DMACH_MAINT
) && (dma_sel
& (1 << i
)))
970 tsi721_dma_stop(&priv
->bdma
[i
]);
974 int tsi721_register_dma(struct tsi721_device
*priv
)
979 struct rio_mport
*mport
= &priv
->mport
;
981 INIT_LIST_HEAD(&mport
->dma
.channels
);
983 for (i
= 0; i
< TSI721_DMA_MAXCH
; i
++) {
984 struct tsi721_bdma_chan
*bdma_chan
= &priv
->bdma
[i
];
986 if ((i
== TSI721_DMACH_MAINT
) || (dma_sel
& (1 << i
)) == 0)
989 bdma_chan
->regs
= priv
->regs
+ TSI721_DMAC_BASE(i
);
991 bdma_chan
->dchan
.device
= &mport
->dma
;
992 bdma_chan
->dchan
.cookie
= 1;
993 bdma_chan
->dchan
.chan_id
= i
;
995 bdma_chan
->active
= false;
997 spin_lock_init(&bdma_chan
->lock
);
999 bdma_chan
->active_tx
= NULL
;
1000 INIT_LIST_HEAD(&bdma_chan
->queue
);
1001 INIT_LIST_HEAD(&bdma_chan
->free_list
);
1003 tasklet_init(&bdma_chan
->tasklet
, tsi721_dma_tasklet
,
1004 (unsigned long)bdma_chan
);
1005 list_add_tail(&bdma_chan
->dchan
.device_node
,
1006 &mport
->dma
.channels
);
1010 mport
->dma
.chancnt
= nr_channels
;
1011 dma_cap_zero(mport
->dma
.cap_mask
);
1012 dma_cap_set(DMA_PRIVATE
, mport
->dma
.cap_mask
);
1013 dma_cap_set(DMA_SLAVE
, mport
->dma
.cap_mask
);
1015 mport
->dma
.dev
= &priv
->pdev
->dev
;
1016 mport
->dma
.device_alloc_chan_resources
= tsi721_alloc_chan_resources
;
1017 mport
->dma
.device_free_chan_resources
= tsi721_free_chan_resources
;
1018 mport
->dma
.device_tx_status
= tsi721_tx_status
;
1019 mport
->dma
.device_issue_pending
= tsi721_issue_pending
;
1020 mport
->dma
.device_prep_slave_sg
= tsi721_prep_rio_sg
;
1021 mport
->dma
.device_terminate_all
= tsi721_terminate_all
;
1023 err
= dma_async_device_register(&mport
->dma
);
1025 tsi_err(&priv
->pdev
->dev
, "Failed to register DMA device");
1030 void tsi721_unregister_dma(struct tsi721_device
*priv
)
1032 struct rio_mport
*mport
= &priv
->mport
;
1033 struct dma_chan
*chan
, *_c
;
1034 struct tsi721_bdma_chan
*bdma_chan
;
1036 tsi721_dma_stop_all(priv
);
1037 dma_async_device_unregister(&mport
->dma
);
1039 list_for_each_entry_safe(chan
, _c
, &mport
->dma
.channels
,
1041 bdma_chan
= to_tsi721_chan(chan
);
1042 if (bdma_chan
->active
) {
1043 tsi721_bdma_interrupt_enable(bdma_chan
, 0);
1044 bdma_chan
->active
= false;
1045 tsi721_sync_dma_irq(bdma_chan
);
1046 tasklet_kill(&bdma_chan
->tasklet
);
1047 INIT_LIST_HEAD(&bdma_chan
->free_list
);
1048 kfree(bdma_chan
->tx_desc
);
1049 tsi721_bdma_ch_free(bdma_chan
);
1052 list_del(&chan
->device_node
);