blob: 494482e4ab0a21f638b9581439f348a04d6e1be3 [file] [log] [blame]
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07001/*
2 * DMA Engine support for Tsi721 PCIExpress-to-SRIO bridge
3 *
Alexandre Bounine50835e92014-08-08 14:22:12 -07004 * Copyright (c) 2011-2014 Integrated Device Technology, Inc.
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -07005 * Alexandre Bounine <alexandre.bounine@idt.com>
6 *
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)
10 * any later version.
11 *
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
15 * more details.
16 *
Alexandre Bounine50835e92014-08-08 14:22:12 -070017 * The full GNU General Public License is included in this distribution in the
18 * file called COPYING.
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070019 */
20
21#include <linux/io.h>
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>
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -070033#include <linux/sched.h>
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070034#include <linux/delay.h>
Alexandre Bounine50835e92014-08-08 14:22:12 -070035#include "../../dma/dmaengine.h"
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070036
37#include "tsi721.h"
38
Alexandre Bounine50835e92014-08-08 14:22:12 -070039#define TSI721_DMA_TX_QUEUE_SZ 16 /* number of transaction descriptors */
40
41#ifdef CONFIG_PCI_MSI
42static irqreturn_t tsi721_bdma_msix(int irq, void *ptr);
43#endif
44static int tsi721_submit_sg(struct tsi721_tx_desc *desc);
45
46static unsigned int dma_desc_per_channel = 128;
47module_param(dma_desc_per_channel, uint, S_IWUSR | S_IRUGO);
48MODULE_PARM_DESC(dma_desc_per_channel,
49 "Number of DMA descriptors per channel (default: 128)");
50
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070051static inline struct tsi721_bdma_chan *to_tsi721_chan(struct dma_chan *chan)
52{
53 return container_of(chan, struct tsi721_bdma_chan, dchan);
54}
55
56static inline struct tsi721_device *to_tsi721(struct dma_device *ddev)
57{
58 return container_of(ddev, struct rio_mport, dma)->priv;
59}
60
61static inline
62struct tsi721_tx_desc *to_tsi721_desc(struct dma_async_tx_descriptor *txd)
63{
64 return container_of(txd, struct tsi721_tx_desc, txd);
65}
66
Alexandre Bounine50835e92014-08-08 14:22:12 -070067static int tsi721_bdma_ch_init(struct tsi721_bdma_chan *bdma_chan, int bd_num)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070068{
69 struct tsi721_dma_desc *bd_ptr;
70 struct device *dev = bdma_chan->dchan.device->dev;
71 u64 *sts_ptr;
72 dma_addr_t bd_phys;
73 dma_addr_t sts_phys;
74 int sts_size;
Alexandre Bounine50835e92014-08-08 14:22:12 -070075#ifdef CONFIG_PCI_MSI
76 struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
77#endif
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070078
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -070079 tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d", bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070080
Alexandre Bounine50835e92014-08-08 14:22:12 -070081 /*
82 * Allocate space for DMA descriptors
83 * (add an extra element for link descriptor)
84 */
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070085 bd_ptr = dma_zalloc_coherent(dev,
Alexandre Bounine50835e92014-08-08 14:22:12 -070086 (bd_num + 1) * sizeof(struct tsi721_dma_desc),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070087 &bd_phys, GFP_KERNEL);
88 if (!bd_ptr)
89 return -ENOMEM;
90
Alexandre Bounine50835e92014-08-08 14:22:12 -070091 bdma_chan->bd_num = bd_num;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070092 bdma_chan->bd_phys = bd_phys;
93 bdma_chan->bd_base = bd_ptr;
94
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -070095 tsi_debug(DMA, &bdma_chan->dchan.dev->device,
96 "DMAC%d descriptors @ %p (phys = %pad)",
97 bdma_chan->id, bd_ptr, &bd_phys);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -070098
99 /* Allocate space for descriptor status FIFO */
Alexandre Bounine50835e92014-08-08 14:22:12 -0700100 sts_size = ((bd_num + 1) >= TSI721_DMA_MINSTSSZ) ?
101 (bd_num + 1) : TSI721_DMA_MINSTSSZ;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700102 sts_size = roundup_pow_of_two(sts_size);
103 sts_ptr = dma_zalloc_coherent(dev,
104 sts_size * sizeof(struct tsi721_dma_sts),
105 &sts_phys, GFP_KERNEL);
106 if (!sts_ptr) {
107 /* Free space allocated for DMA descriptors */
108 dma_free_coherent(dev,
Alexandre Bounine50835e92014-08-08 14:22:12 -0700109 (bd_num + 1) * sizeof(struct tsi721_dma_desc),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700110 bd_ptr, bd_phys);
111 bdma_chan->bd_base = NULL;
112 return -ENOMEM;
113 }
114
115 bdma_chan->sts_phys = sts_phys;
116 bdma_chan->sts_base = sts_ptr;
117 bdma_chan->sts_size = sts_size;
118
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700119 tsi_debug(DMA, &bdma_chan->dchan.dev->device,
120 "DMAC%d desc status FIFO @ %p (phys = %pad) size=0x%x",
121 bdma_chan->id, sts_ptr, &sts_phys, sts_size);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700122
Alexandre Bounine50835e92014-08-08 14:22:12 -0700123 /* Initialize DMA descriptors ring using added link descriptor */
124 bd_ptr[bd_num].type_id = cpu_to_le32(DTYPE3 << 29);
125 bd_ptr[bd_num].next_lo = cpu_to_le32((u64)bd_phys &
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700126 TSI721_DMAC_DPTRL_MASK);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700127 bd_ptr[bd_num].next_hi = cpu_to_le32((u64)bd_phys >> 32);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700128
129 /* Setup DMA descriptor pointers */
130 iowrite32(((u64)bd_phys >> 32),
131 bdma_chan->regs + TSI721_DMAC_DPTRH);
132 iowrite32(((u64)bd_phys & TSI721_DMAC_DPTRL_MASK),
133 bdma_chan->regs + TSI721_DMAC_DPTRL);
134
135 /* Setup descriptor status FIFO */
136 iowrite32(((u64)sts_phys >> 32),
137 bdma_chan->regs + TSI721_DMAC_DSBH);
138 iowrite32(((u64)sts_phys & TSI721_DMAC_DSBL_MASK),
139 bdma_chan->regs + TSI721_DMAC_DSBL);
140 iowrite32(TSI721_DMAC_DSSZ_SIZE(sts_size),
141 bdma_chan->regs + TSI721_DMAC_DSSZ);
142
143 /* Clear interrupt bits */
144 iowrite32(TSI721_DMAC_INT_ALL,
145 bdma_chan->regs + TSI721_DMAC_INT);
146
147 ioread32(bdma_chan->regs + TSI721_DMAC_INT);
148
Alexandre Bounine50835e92014-08-08 14:22:12 -0700149#ifdef CONFIG_PCI_MSI
150 /* Request interrupt service if we are in MSI-X mode */
151 if (priv->flags & TSI721_USING_MSIX) {
152 int rc, idx;
153
154 idx = TSI721_VECT_DMA0_DONE + bdma_chan->id;
155
156 rc = request_irq(priv->msix[idx].vector, tsi721_bdma_msix, 0,
157 priv->msix[idx].irq_name, (void *)bdma_chan);
158
159 if (rc) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700160 tsi_debug(DMA, &bdma_chan->dchan.dev->device,
161 "Unable to get MSI-X for DMAC%d-DONE",
162 bdma_chan->id);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700163 goto err_out;
164 }
165
166 idx = TSI721_VECT_DMA0_INT + bdma_chan->id;
167
168 rc = request_irq(priv->msix[idx].vector, tsi721_bdma_msix, 0,
169 priv->msix[idx].irq_name, (void *)bdma_chan);
170
171 if (rc) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700172 tsi_debug(DMA, &bdma_chan->dchan.dev->device,
173 "Unable to get MSI-X for DMAC%d-INT",
174 bdma_chan->id);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700175 free_irq(
176 priv->msix[TSI721_VECT_DMA0_DONE +
177 bdma_chan->id].vector,
178 (void *)bdma_chan);
179 }
180
181err_out:
182 if (rc) {
183 /* Free space allocated for DMA descriptors */
184 dma_free_coherent(dev,
185 (bd_num + 1) * sizeof(struct tsi721_dma_desc),
186 bd_ptr, bd_phys);
187 bdma_chan->bd_base = NULL;
188
189 /* Free space allocated for status descriptors */
190 dma_free_coherent(dev,
191 sts_size * sizeof(struct tsi721_dma_sts),
192 sts_ptr, sts_phys);
193 bdma_chan->sts_base = NULL;
194
195 return -EIO;
196 }
197 }
198#endif /* CONFIG_PCI_MSI */
199
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700200 /* Toggle DMA channel initialization */
201 iowrite32(TSI721_DMAC_CTL_INIT, bdma_chan->regs + TSI721_DMAC_CTL);
202 ioread32(bdma_chan->regs + TSI721_DMAC_CTL);
203 bdma_chan->wr_count = bdma_chan->wr_count_next = 0;
204 bdma_chan->sts_rdptr = 0;
205 udelay(10);
206
207 return 0;
208}
209
210static int tsi721_bdma_ch_free(struct tsi721_bdma_chan *bdma_chan)
211{
212 u32 ch_stat;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700213#ifdef CONFIG_PCI_MSI
214 struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
215#endif
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700216
217 if (bdma_chan->bd_base == NULL)
218 return 0;
219
220 /* Check if DMA channel still running */
221 ch_stat = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
222 if (ch_stat & TSI721_DMAC_STS_RUN)
223 return -EFAULT;
224
225 /* Put DMA channel into init state */
226 iowrite32(TSI721_DMAC_CTL_INIT, bdma_chan->regs + TSI721_DMAC_CTL);
227
Alexandre Bounine50835e92014-08-08 14:22:12 -0700228#ifdef CONFIG_PCI_MSI
229 if (priv->flags & TSI721_USING_MSIX) {
230 free_irq(priv->msix[TSI721_VECT_DMA0_DONE +
231 bdma_chan->id].vector, (void *)bdma_chan);
232 free_irq(priv->msix[TSI721_VECT_DMA0_INT +
233 bdma_chan->id].vector, (void *)bdma_chan);
234 }
235#endif /* CONFIG_PCI_MSI */
236
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700237 /* Free space allocated for DMA descriptors */
238 dma_free_coherent(bdma_chan->dchan.device->dev,
Alexandre Bounine50835e92014-08-08 14:22:12 -0700239 (bdma_chan->bd_num + 1) * sizeof(struct tsi721_dma_desc),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700240 bdma_chan->bd_base, bdma_chan->bd_phys);
241 bdma_chan->bd_base = NULL;
242
243 /* Free space allocated for status FIFO */
244 dma_free_coherent(bdma_chan->dchan.device->dev,
245 bdma_chan->sts_size * sizeof(struct tsi721_dma_sts),
246 bdma_chan->sts_base, bdma_chan->sts_phys);
247 bdma_chan->sts_base = NULL;
248 return 0;
249}
250
251static void
252tsi721_bdma_interrupt_enable(struct tsi721_bdma_chan *bdma_chan, int enable)
253{
254 if (enable) {
255 /* Clear pending BDMA channel interrupts */
256 iowrite32(TSI721_DMAC_INT_ALL,
257 bdma_chan->regs + TSI721_DMAC_INT);
258 ioread32(bdma_chan->regs + TSI721_DMAC_INT);
259 /* Enable BDMA channel interrupts */
260 iowrite32(TSI721_DMAC_INT_ALL,
261 bdma_chan->regs + TSI721_DMAC_INTE);
262 } else {
263 /* Disable BDMA channel interrupts */
264 iowrite32(0, bdma_chan->regs + TSI721_DMAC_INTE);
265 /* Clear pending BDMA channel interrupts */
266 iowrite32(TSI721_DMAC_INT_ALL,
267 bdma_chan->regs + TSI721_DMAC_INT);
268 }
269
270}
271
272static bool tsi721_dma_is_idle(struct tsi721_bdma_chan *bdma_chan)
273{
274 u32 sts;
275
276 sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
277 return ((sts & TSI721_DMAC_STS_RUN) == 0);
278}
279
280void tsi721_bdma_handler(struct tsi721_bdma_chan *bdma_chan)
281{
282 /* Disable BDMA channel interrupts */
283 iowrite32(0, bdma_chan->regs + TSI721_DMAC_INTE);
Alexandre Bounine04379df2014-03-03 15:38:36 -0800284 if (bdma_chan->active)
285 tasklet_schedule(&bdma_chan->tasklet);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700286}
287
288#ifdef CONFIG_PCI_MSI
289/**
290 * tsi721_omsg_msix - MSI-X interrupt handler for BDMA channels
291 * @irq: Linux interrupt number
292 * @ptr: Pointer to interrupt-specific data (BDMA channel structure)
293 *
294 * Handles BDMA channel interrupts signaled using MSI-X.
295 */
296static irqreturn_t tsi721_bdma_msix(int irq, void *ptr)
297{
298 struct tsi721_bdma_chan *bdma_chan = ptr;
299
300 tsi721_bdma_handler(bdma_chan);
301 return IRQ_HANDLED;
302}
303#endif /* CONFIG_PCI_MSI */
304
305/* Must be called with the spinlock held */
306static void tsi721_start_dma(struct tsi721_bdma_chan *bdma_chan)
307{
308 if (!tsi721_dma_is_idle(bdma_chan)) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700309 tsi_err(&bdma_chan->dchan.dev->device,
310 "DMAC%d Attempt to start non-idle channel",
311 bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700312 return;
313 }
314
315 if (bdma_chan->wr_count == bdma_chan->wr_count_next) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700316 tsi_err(&bdma_chan->dchan.dev->device,
317 "DMAC%d Attempt to start DMA with no BDs ready %d",
318 bdma_chan->id, task_pid_nr(current));
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700319 return;
320 }
321
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700322 tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d (wrc=%d) %d",
323 bdma_chan->id, bdma_chan->wr_count_next,
324 task_pid_nr(current));
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700325
326 iowrite32(bdma_chan->wr_count_next,
327 bdma_chan->regs + TSI721_DMAC_DWRCNT);
328 ioread32(bdma_chan->regs + TSI721_DMAC_DWRCNT);
329
330 bdma_chan->wr_count = bdma_chan->wr_count_next;
331}
332
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700333static int
Alexandre Bounine50835e92014-08-08 14:22:12 -0700334tsi721_desc_fill_init(struct tsi721_tx_desc *desc,
335 struct tsi721_dma_desc *bd_ptr,
336 struct scatterlist *sg, u32 sys_size)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700337{
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700338 u64 rio_addr;
339
Alexandre Bounine50835e92014-08-08 14:22:12 -0700340 if (bd_ptr == NULL)
341 return -EINVAL;
342
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700343 /* Initialize DMA descriptor */
344 bd_ptr->type_id = cpu_to_le32((DTYPE1 << 29) |
Alexandre Bounine50835e92014-08-08 14:22:12 -0700345 (desc->rtype << 19) | desc->destid);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700346 bd_ptr->bcount = cpu_to_le32(((desc->rio_addr & 0x3) << 30) |
Alexandre Bounine40f847b2014-04-07 15:38:55 -0700347 (sys_size << 26));
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700348 rio_addr = (desc->rio_addr >> 2) |
349 ((u64)(desc->rio_addr_u & 0x3) << 62);
350 bd_ptr->raddr_lo = cpu_to_le32(rio_addr & 0xffffffff);
351 bd_ptr->raddr_hi = cpu_to_le32(rio_addr >> 32);
352 bd_ptr->t1.bufptr_lo = cpu_to_le32(
353 (u64)sg_dma_address(sg) & 0xffffffff);
354 bd_ptr->t1.bufptr_hi = cpu_to_le32((u64)sg_dma_address(sg) >> 32);
355 bd_ptr->t1.s_dist = 0;
356 bd_ptr->t1.s_size = 0;
357
358 return 0;
359}
360
Alexandre Bounine40f847b2014-04-07 15:38:55 -0700361static int
Alexandre Bounine50835e92014-08-08 14:22:12 -0700362tsi721_desc_fill_end(struct tsi721_dma_desc *bd_ptr, u32 bcount, bool interrupt)
Alexandre Bounine40f847b2014-04-07 15:38:55 -0700363{
Alexandre Bounine50835e92014-08-08 14:22:12 -0700364 if (bd_ptr == NULL)
365 return -EINVAL;
Alexandre Bounine40f847b2014-04-07 15:38:55 -0700366
367 /* Update DMA descriptor */
Alexandre Bounine50835e92014-08-08 14:22:12 -0700368 if (interrupt)
Alexandre Bounine40f847b2014-04-07 15:38:55 -0700369 bd_ptr->type_id |= cpu_to_le32(TSI721_DMAD_IOF);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700370 bd_ptr->bcount |= cpu_to_le32(bcount & TSI721_DMAD_BCOUNT1);
Alexandre Bounine40f847b2014-04-07 15:38:55 -0700371
372 return 0;
373}
374
Alexandre Bounine50835e92014-08-08 14:22:12 -0700375static void tsi721_dma_tx_err(struct tsi721_bdma_chan *bdma_chan,
376 struct tsi721_tx_desc *desc)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700377{
378 struct dma_async_tx_descriptor *txd = &desc->txd;
379 dma_async_tx_callback callback = txd->callback;
380 void *param = txd->callback_param;
381
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700382 list_move(&desc->desc_node, &bdma_chan->free_list);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700383
384 if (callback)
385 callback(param);
386}
387
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700388static void tsi721_clr_stat(struct tsi721_bdma_chan *bdma_chan)
389{
390 u32 srd_ptr;
391 u64 *sts_ptr;
392 int i, j;
393
394 /* Check and clear descriptor status FIFO entries */
395 srd_ptr = bdma_chan->sts_rdptr;
396 sts_ptr = bdma_chan->sts_base;
397 j = srd_ptr * 8;
398 while (sts_ptr[j]) {
399 for (i = 0; i < 8 && sts_ptr[j]; i++, j++)
400 sts_ptr[j] = 0;
401
402 ++srd_ptr;
403 srd_ptr %= bdma_chan->sts_size;
404 j = srd_ptr * 8;
405 }
406
407 iowrite32(srd_ptr, bdma_chan->regs + TSI721_DMAC_DSRP);
408 bdma_chan->sts_rdptr = srd_ptr;
409}
410
Alexandre Bounine50835e92014-08-08 14:22:12 -0700411/* Must be called with the channel spinlock held */
412static int tsi721_submit_sg(struct tsi721_tx_desc *desc)
413{
414 struct dma_chan *dchan = desc->txd.chan;
415 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
416 u32 sys_size;
417 u64 rio_addr;
418 dma_addr_t next_addr;
419 u32 bcount;
420 struct scatterlist *sg;
421 unsigned int i;
422 int err = 0;
423 struct tsi721_dma_desc *bd_ptr = NULL;
424 u32 idx, rd_idx;
425 u32 add_count = 0;
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700426 struct device *ch_dev = &dchan->dev->device;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700427
428 if (!tsi721_dma_is_idle(bdma_chan)) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700429 tsi_err(ch_dev, "DMAC%d ERR: Attempt to use non-idle channel",
430 bdma_chan->id);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700431 return -EIO;
432 }
433
434 /*
435 * Fill DMA channel's hardware buffer descriptors.
436 * (NOTE: RapidIO destination address is limited to 64 bits for now)
437 */
438 rio_addr = desc->rio_addr;
439 next_addr = -1;
440 bcount = 0;
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700441 sys_size = dma_to_mport(dchan->device)->sys_size;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700442
443 rd_idx = ioread32(bdma_chan->regs + TSI721_DMAC_DRDCNT);
444 rd_idx %= (bdma_chan->bd_num + 1);
445
446 idx = bdma_chan->wr_count_next % (bdma_chan->bd_num + 1);
447 if (idx == bdma_chan->bd_num) {
448 /* wrap around link descriptor */
449 idx = 0;
450 add_count++;
451 }
452
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700453 tsi_debug(DMA, ch_dev, "DMAC%d BD ring status: rdi=%d wri=%d",
454 bdma_chan->id, rd_idx, idx);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700455
456 for_each_sg(desc->sg, sg, desc->sg_len, i) {
457
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700458 tsi_debug(DMAV, ch_dev, "DMAC%d sg%d/%d addr: 0x%llx len: %d",
459 bdma_chan->id, i, desc->sg_len,
Alexandre Bounine50835e92014-08-08 14:22:12 -0700460 (unsigned long long)sg_dma_address(sg), sg_dma_len(sg));
461
462 if (sg_dma_len(sg) > TSI721_BDMA_MAX_BCOUNT) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700463 tsi_err(ch_dev, "DMAC%d SG entry %d is too large",
464 bdma_chan->id, i);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700465 err = -EINVAL;
466 break;
467 }
468
469 /*
470 * If this sg entry forms contiguous block with previous one,
471 * try to merge it into existing DMA descriptor
472 */
473 if (next_addr == sg_dma_address(sg) &&
474 bcount + sg_dma_len(sg) <= TSI721_BDMA_MAX_BCOUNT) {
475 /* Adjust byte count of the descriptor */
476 bcount += sg_dma_len(sg);
477 goto entry_done;
478 } else if (next_addr != -1) {
479 /* Finalize descriptor using total byte count value */
480 tsi721_desc_fill_end(bd_ptr, bcount, 0);
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700481 tsi_debug(DMAV, ch_dev, "DMAC%d prev desc final len: %d",
482 bdma_chan->id, bcount);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700483 }
484
485 desc->rio_addr = rio_addr;
486
487 if (i && idx == rd_idx) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700488 tsi_debug(DMAV, ch_dev,
489 "DMAC%d HW descriptor ring is full @ %d",
490 bdma_chan->id, i);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700491 desc->sg = sg;
492 desc->sg_len -= i;
493 break;
494 }
495
496 bd_ptr = &((struct tsi721_dma_desc *)bdma_chan->bd_base)[idx];
497 err = tsi721_desc_fill_init(desc, bd_ptr, sg, sys_size);
498 if (err) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700499 tsi_err(ch_dev, "Failed to build desc: err=%d", err);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700500 break;
501 }
502
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700503 tsi_debug(DMAV, ch_dev, "DMAC%d bd_ptr = %p did=%d raddr=0x%llx",
504 bdma_chan->id, bd_ptr, desc->destid, desc->rio_addr);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700505
506 next_addr = sg_dma_address(sg);
507 bcount = sg_dma_len(sg);
508
509 add_count++;
510 if (++idx == bdma_chan->bd_num) {
511 /* wrap around link descriptor */
512 idx = 0;
513 add_count++;
514 }
515
516entry_done:
517 if (sg_is_last(sg)) {
518 tsi721_desc_fill_end(bd_ptr, bcount, 0);
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700519 tsi_debug(DMAV, ch_dev,
520 "DMAC%d last desc final len: %d",
521 bdma_chan->id, bcount);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700522 desc->sg_len = 0;
523 } else {
524 rio_addr += sg_dma_len(sg);
525 next_addr += sg_dma_len(sg);
526 }
527 }
528
529 if (!err)
530 bdma_chan->wr_count_next += add_count;
531
532 return err;
533}
534
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700535static void tsi721_advance_work(struct tsi721_bdma_chan *bdma_chan,
536 struct tsi721_tx_desc *desc)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700537{
Alexandre Bounine50835e92014-08-08 14:22:12 -0700538 int err;
539
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700540 tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d", bdma_chan->id);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700541
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700542 if (!tsi721_dma_is_idle(bdma_chan))
543 return;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700544
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700545 /*
546 * If there is no data transfer in progress, fetch new descriptor from
547 * the pending queue.
548 */
549
550 if (desc == NULL && bdma_chan->active_tx == NULL &&
551 !list_empty(&bdma_chan->queue)) {
552 desc = list_first_entry(&bdma_chan->queue,
553 struct tsi721_tx_desc, desc_node);
554 list_del_init((&desc->desc_node));
555 bdma_chan->active_tx = desc;
556 }
557
558 if (desc) {
Alexandre Bounine50835e92014-08-08 14:22:12 -0700559 err = tsi721_submit_sg(desc);
560 if (!err)
561 tsi721_start_dma(bdma_chan);
562 else {
563 tsi721_dma_tx_err(bdma_chan, desc);
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700564 tsi_debug(DMA, &bdma_chan->dchan.dev->device,
565 "DMAC%d ERR: tsi721_submit_sg failed with err=%d",
566 bdma_chan->id, err);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700567 }
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700568 }
Alexandre Bounine50835e92014-08-08 14:22:12 -0700569
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700570 tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d Exit",
571 bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700572}
573
574static void tsi721_dma_tasklet(unsigned long data)
575{
576 struct tsi721_bdma_chan *bdma_chan = (struct tsi721_bdma_chan *)data;
577 u32 dmac_int, dmac_sts;
578
579 dmac_int = ioread32(bdma_chan->regs + TSI721_DMAC_INT);
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700580 tsi_debug(DMA, &bdma_chan->dchan.dev->device, "DMAC%d_INT = 0x%x",
581 bdma_chan->id, dmac_int);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700582 /* Clear channel interrupts */
583 iowrite32(dmac_int, bdma_chan->regs + TSI721_DMAC_INT);
584
585 if (dmac_int & TSI721_DMAC_INT_ERR) {
586 dmac_sts = ioread32(bdma_chan->regs + TSI721_DMAC_STS);
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700587 tsi_err(&bdma_chan->dchan.dev->device,
588 "ERR - DMAC%d_STS = 0x%x",
589 bdma_chan->id, dmac_sts);
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700590
591 spin_lock(&bdma_chan->lock);
592 bdma_chan->active_tx = NULL;
593 spin_unlock(&bdma_chan->lock);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700594 }
595
596 if (dmac_int & TSI721_DMAC_INT_STFULL) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700597 tsi_err(&bdma_chan->dchan.dev->device,
598 "DMAC%d descriptor status FIFO is full",
599 bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700600 }
601
602 if (dmac_int & (TSI721_DMAC_INT_DONE | TSI721_DMAC_INT_IOFDONE)) {
Alexandre Bounine50835e92014-08-08 14:22:12 -0700603 struct tsi721_tx_desc *desc;
604
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700605 tsi721_clr_stat(bdma_chan);
606 spin_lock(&bdma_chan->lock);
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700607 desc = bdma_chan->active_tx;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700608
609 if (desc->sg_len == 0) {
610 dma_async_tx_callback callback = NULL;
611 void *param = NULL;
612
613 desc->status = DMA_COMPLETE;
614 dma_cookie_complete(&desc->txd);
615 if (desc->txd.flags & DMA_PREP_INTERRUPT) {
616 callback = desc->txd.callback;
617 param = desc->txd.callback_param;
618 }
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700619 list_add(&desc->desc_node, &bdma_chan->free_list);
620 bdma_chan->active_tx = NULL;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700621 spin_unlock(&bdma_chan->lock);
622 if (callback)
623 callback(param);
624 spin_lock(&bdma_chan->lock);
625 }
626
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700627 tsi721_advance_work(bdma_chan, bdma_chan->active_tx);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700628 spin_unlock(&bdma_chan->lock);
629 }
630
631 /* Re-Enable BDMA channel interrupts */
632 iowrite32(TSI721_DMAC_INT_ALL, bdma_chan->regs + TSI721_DMAC_INTE);
633}
634
635static dma_cookie_t tsi721_tx_submit(struct dma_async_tx_descriptor *txd)
636{
637 struct tsi721_tx_desc *desc = to_tsi721_desc(txd);
638 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(txd->chan);
639 dma_cookie_t cookie;
640
Alexandre Bounine50835e92014-08-08 14:22:12 -0700641 /* Check if the descriptor is detached from any lists */
642 if (!list_empty(&desc->desc_node)) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700643 tsi_err(&bdma_chan->dchan.dev->device,
644 "DMAC%d wrong state of descriptor %p",
645 bdma_chan->id, txd);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700646 return -EIO;
647 }
648
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700649 spin_lock_bh(&bdma_chan->lock);
650
Alexandre Bounine50835e92014-08-08 14:22:12 -0700651 if (!bdma_chan->active) {
652 spin_unlock_bh(&bdma_chan->lock);
653 return -ENODEV;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700654 }
655
Alexandre Bounine50835e92014-08-08 14:22:12 -0700656 cookie = dma_cookie_assign(txd);
657 desc->status = DMA_IN_PROGRESS;
658 list_add_tail(&desc->desc_node, &bdma_chan->queue);
659
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700660 spin_unlock_bh(&bdma_chan->lock);
661 return cookie;
662}
663
664static int tsi721_alloc_chan_resources(struct dma_chan *dchan)
665{
666 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700667 struct tsi721_tx_desc *desc = NULL;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700668 int i;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700669
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700670 tsi_debug(DMA, &dchan->dev->device, "DMAC%d", bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700671
672 if (bdma_chan->bd_base)
Alexandre Bounine50835e92014-08-08 14:22:12 -0700673 return TSI721_DMA_TX_QUEUE_SZ;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700674
675 /* Initialize BDMA channel */
Alexandre Bounine50835e92014-08-08 14:22:12 -0700676 if (tsi721_bdma_ch_init(bdma_chan, dma_desc_per_channel)) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700677 tsi_err(&dchan->dev->device, "Unable to initialize DMAC%d",
678 bdma_chan->id);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700679 return -ENODEV;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700680 }
681
Alexandre Bounine50835e92014-08-08 14:22:12 -0700682 /* Allocate queue of transaction descriptors */
683 desc = kcalloc(TSI721_DMA_TX_QUEUE_SZ, sizeof(struct tsi721_tx_desc),
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700684 GFP_KERNEL);
685 if (!desc) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700686 tsi_err(&dchan->dev->device,
687 "DMAC%d Failed to allocate logical descriptors",
688 bdma_chan->id);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700689 tsi721_bdma_ch_free(bdma_chan);
690 return -ENOMEM;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700691 }
692
693 bdma_chan->tx_desc = desc;
694
Alexandre Bounine50835e92014-08-08 14:22:12 -0700695 for (i = 0; i < TSI721_DMA_TX_QUEUE_SZ; i++) {
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700696 dma_async_tx_descriptor_init(&desc[i].txd, dchan);
697 desc[i].txd.tx_submit = tsi721_tx_submit;
698 desc[i].txd.flags = DMA_CTRL_ACK;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700699 list_add(&desc[i].desc_node, &bdma_chan->free_list);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700700 }
701
Alexandre Bounine50835e92014-08-08 14:22:12 -0700702 dma_cookie_init(dchan);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700703
Alexandre Bounine04379df2014-03-03 15:38:36 -0800704 bdma_chan->active = true;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700705 tsi721_bdma_interrupt_enable(bdma_chan, 1);
706
Alexandre Bounine50835e92014-08-08 14:22:12 -0700707 return TSI721_DMA_TX_QUEUE_SZ;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700708}
709
Alexandre Bounine50835e92014-08-08 14:22:12 -0700710static void tsi721_sync_dma_irq(struct tsi721_bdma_chan *bdma_chan)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700711{
Alexandre Bounine50835e92014-08-08 14:22:12 -0700712 struct tsi721_device *priv = to_tsi721(bdma_chan->dchan.device);
Alexandre Bounine04379df2014-03-03 15:38:36 -0800713
714#ifdef CONFIG_PCI_MSI
715 if (priv->flags & TSI721_USING_MSIX) {
716 synchronize_irq(priv->msix[TSI721_VECT_DMA0_DONE +
717 bdma_chan->id].vector);
718 synchronize_irq(priv->msix[TSI721_VECT_DMA0_INT +
719 bdma_chan->id].vector);
720 } else
721#endif
722 synchronize_irq(priv->pdev->irq);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700723}
Alexandre Bounine04379df2014-03-03 15:38:36 -0800724
Alexandre Bounine50835e92014-08-08 14:22:12 -0700725static void tsi721_free_chan_resources(struct dma_chan *dchan)
726{
727 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
728
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700729 tsi_debug(DMA, &dchan->dev->device, "DMAC%d", bdma_chan->id);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700730
731 if (bdma_chan->bd_base == NULL)
732 return;
733
Alexandre Bounine50835e92014-08-08 14:22:12 -0700734 tsi721_bdma_interrupt_enable(bdma_chan, 0);
735 bdma_chan->active = false;
736 tsi721_sync_dma_irq(bdma_chan);
Alexandre Bounine04379df2014-03-03 15:38:36 -0800737 tasklet_kill(&bdma_chan->tasklet);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700738 INIT_LIST_HEAD(&bdma_chan->free_list);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700739 kfree(bdma_chan->tx_desc);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700740 tsi721_bdma_ch_free(bdma_chan);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700741}
742
743static
744enum dma_status tsi721_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
745 struct dma_tx_state *txstate)
746{
Alexandre Bounine50835e92014-08-08 14:22:12 -0700747 return dma_cookie_status(dchan, cookie, txstate);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700748}
749
750static void tsi721_issue_pending(struct dma_chan *dchan)
751{
752 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
753
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700754 tsi_debug(DMA, &dchan->dev->device, "DMAC%d", bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700755
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700756 spin_lock_bh(&bdma_chan->lock);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700757 if (tsi721_dma_is_idle(bdma_chan) && bdma_chan->active) {
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700758 tsi721_advance_work(bdma_chan, NULL);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700759 }
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700760 spin_unlock_bh(&bdma_chan->lock);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700761}
762
763static
764struct dma_async_tx_descriptor *tsi721_prep_rio_sg(struct dma_chan *dchan,
765 struct scatterlist *sgl, unsigned int sg_len,
766 enum dma_transfer_direction dir, unsigned long flags,
767 void *tinfo)
768{
769 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700770 struct tsi721_tx_desc *desc, *_d;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700771 struct rio_dma_ext *rext = tinfo;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700772 enum dma_rtype rtype;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700773 struct dma_async_tx_descriptor *txd = NULL;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700774
775 if (!sgl || !sg_len) {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700776 tsi_err(&dchan->dev->device, "DMAC%d No SG list",
777 bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700778 return NULL;
779 }
780
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700781 tsi_debug(DMA, &dchan->dev->device, "DMAC%d %s", bdma_chan->id,
782 (dir == DMA_DEV_TO_MEM)?"READ":"WRITE");
Alexandre Bounine50835e92014-08-08 14:22:12 -0700783
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700784 if (dir == DMA_DEV_TO_MEM)
785 rtype = NREAD;
786 else if (dir == DMA_MEM_TO_DEV) {
787 switch (rext->wr_type) {
788 case RDW_ALL_NWRITE:
789 rtype = ALL_NWRITE;
790 break;
791 case RDW_ALL_NWRITE_R:
792 rtype = ALL_NWRITE_R;
793 break;
794 case RDW_LAST_NWRITE_R:
795 default:
796 rtype = LAST_NWRITE_R;
797 break;
798 }
799 } else {
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700800 tsi_err(&dchan->dev->device,
801 "DMAC%d Unsupported DMA direction option",
802 bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700803 return NULL;
804 }
805
Alexandre Bounine50835e92014-08-08 14:22:12 -0700806 spin_lock_bh(&bdma_chan->lock);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700807
Alexandre Bounine50835e92014-08-08 14:22:12 -0700808 list_for_each_entry_safe(desc, _d, &bdma_chan->free_list, desc_node) {
809 if (async_tx_test_ack(&desc->txd)) {
810 list_del_init(&desc->desc_node);
811 desc->destid = rext->destid;
812 desc->rio_addr = rext->rio_addr;
813 desc->rio_addr_u = 0;
814 desc->rtype = rtype;
815 desc->sg_len = sg_len;
816 desc->sg = sgl;
817 txd = &desc->txd;
818 txd->flags = flags;
819 break;
Alexandre Bounine40f847b2014-04-07 15:38:55 -0700820 }
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700821 }
822
Alexandre Bounine50835e92014-08-08 14:22:12 -0700823 spin_unlock_bh(&bdma_chan->lock);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700824
Alexandre Bounine50835e92014-08-08 14:22:12 -0700825 return txd;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700826}
827
Maxime Ripard7664cfe2014-11-17 14:42:43 +0100828static int tsi721_terminate_all(struct dma_chan *dchan)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700829{
830 struct tsi721_bdma_chan *bdma_chan = to_tsi721_chan(dchan);
831 struct tsi721_tx_desc *desc, *_d;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700832 u32 dmac_int;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700833 LIST_HEAD(list);
834
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700835 tsi_debug(DMA, &dchan->dev->device, "DMAC%d", bdma_chan->id);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700836
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700837 spin_lock_bh(&bdma_chan->lock);
838
Alexandre Bounine50835e92014-08-08 14:22:12 -0700839 bdma_chan->active = false;
840
841 if (!tsi721_dma_is_idle(bdma_chan)) {
842 /* make sure to stop the transfer */
843 iowrite32(TSI721_DMAC_CTL_SUSP,
844 bdma_chan->regs + TSI721_DMAC_CTL);
845
846 /* Wait until DMA channel stops */
847 do {
848 dmac_int = ioread32(bdma_chan->regs + TSI721_DMAC_INT);
849 } while ((dmac_int & TSI721_DMAC_INT_SUSP) == 0);
850 }
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700851
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700852 if (bdma_chan->active_tx)
853 list_add(&bdma_chan->active_tx->desc_node, &list);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700854 list_splice_init(&bdma_chan->queue, &list);
855
856 list_for_each_entry_safe(desc, _d, &list, desc_node)
Alexandre Bounine50835e92014-08-08 14:22:12 -0700857 tsi721_dma_tx_err(bdma_chan, desc);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700858
859 spin_unlock_bh(&bdma_chan->lock);
860
861 return 0;
862}
863
Alexandre Bouninee3dd8cd2016-03-22 14:26:08 -0700864static void tsi721_dma_stop(struct tsi721_bdma_chan *bdma_chan)
865{
866 if (!bdma_chan->active)
867 return;
868 spin_lock_bh(&bdma_chan->lock);
869 if (!tsi721_dma_is_idle(bdma_chan)) {
870 int timeout = 100000;
871
872 /* stop the transfer in progress */
873 iowrite32(TSI721_DMAC_CTL_SUSP,
874 bdma_chan->regs + TSI721_DMAC_CTL);
875
876 /* Wait until DMA channel stops */
877 while (!tsi721_dma_is_idle(bdma_chan) && --timeout)
878 udelay(1);
879 }
880
881 spin_unlock_bh(&bdma_chan->lock);
882}
883
884void tsi721_dma_stop_all(struct tsi721_device *priv)
885{
886 int i;
887
888 for (i = 0; i < TSI721_DMA_MAXCH; i++) {
889 if (i != TSI721_DMACH_MAINT)
890 tsi721_dma_stop(&priv->bdma[i]);
891 }
892}
893
Bill Pemberton305c891e2012-11-19 13:23:25 -0500894int tsi721_register_dma(struct tsi721_device *priv)
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700895{
896 int i;
Alexandre Bounine50835e92014-08-08 14:22:12 -0700897 int nr_channels = 0;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700898 int err;
Alexandre Bounine748353c2016-03-22 14:26:23 -0700899 struct rio_mport *mport = &priv->mport;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700900
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700901 INIT_LIST_HEAD(&mport->dma.channels);
902
Alexandre Bounine50835e92014-08-08 14:22:12 -0700903 for (i = 0; i < TSI721_DMA_MAXCH; i++) {
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700904 struct tsi721_bdma_chan *bdma_chan = &priv->bdma[i];
905
906 if (i == TSI721_DMACH_MAINT)
907 continue;
908
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700909 bdma_chan->regs = priv->regs + TSI721_DMAC_BASE(i);
910
911 bdma_chan->dchan.device = &mport->dma;
912 bdma_chan->dchan.cookie = 1;
913 bdma_chan->dchan.chan_id = i;
914 bdma_chan->id = i;
Alexandre Bounine04379df2014-03-03 15:38:36 -0800915 bdma_chan->active = false;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700916
917 spin_lock_init(&bdma_chan->lock);
918
Alexandre Bounined2a321f2016-03-22 14:25:57 -0700919 bdma_chan->active_tx = NULL;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700920 INIT_LIST_HEAD(&bdma_chan->queue);
921 INIT_LIST_HEAD(&bdma_chan->free_list);
922
923 tasklet_init(&bdma_chan->tasklet, tsi721_dma_tasklet,
924 (unsigned long)bdma_chan);
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700925 list_add_tail(&bdma_chan->dchan.device_node,
926 &mport->dma.channels);
Alexandre Bounine50835e92014-08-08 14:22:12 -0700927 nr_channels++;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700928 }
929
Alexandre Bounine50835e92014-08-08 14:22:12 -0700930 mport->dma.chancnt = nr_channels;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700931 dma_cap_zero(mport->dma.cap_mask);
932 dma_cap_set(DMA_PRIVATE, mport->dma.cap_mask);
933 dma_cap_set(DMA_SLAVE, mport->dma.cap_mask);
934
Alexandre Bounine50835e92014-08-08 14:22:12 -0700935 mport->dma.dev = &priv->pdev->dev;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700936 mport->dma.device_alloc_chan_resources = tsi721_alloc_chan_resources;
937 mport->dma.device_free_chan_resources = tsi721_free_chan_resources;
938 mport->dma.device_tx_status = tsi721_tx_status;
939 mport->dma.device_issue_pending = tsi721_issue_pending;
940 mport->dma.device_prep_slave_sg = tsi721_prep_rio_sg;
Maxime Ripard7664cfe2014-11-17 14:42:43 +0100941 mport->dma.device_terminate_all = tsi721_terminate_all;
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700942
943 err = dma_async_device_register(&mport->dma);
944 if (err)
Alexandre Bounine72d8a0d2016-03-22 14:26:56 -0700945 tsi_err(&priv->pdev->dev, "Failed to register DMA device");
Alexandre Bounine9eaa3d92012-05-31 16:26:39 -0700946
947 return err;
948}
Alexandre Bounine748353c2016-03-22 14:26:23 -0700949
950void tsi721_unregister_dma(struct tsi721_device *priv)
951{
952 struct rio_mport *mport = &priv->mport;
953 struct dma_chan *chan, *_c;
954 struct tsi721_bdma_chan *bdma_chan;
955
956 tsi721_dma_stop_all(priv);
957 dma_async_device_unregister(&mport->dma);
958
959 list_for_each_entry_safe(chan, _c, &mport->dma.channels,
960 device_node) {
961 bdma_chan = to_tsi721_chan(chan);
962 if (bdma_chan->active) {
963 tsi721_bdma_interrupt_enable(bdma_chan, 0);
964 bdma_chan->active = false;
965 tsi721_sync_dma_irq(bdma_chan);
966 tasklet_kill(&bdma_chan->tasklet);
967 INIT_LIST_HEAD(&bdma_chan->free_list);
968 kfree(bdma_chan->tx_desc);
969 tsi721_bdma_ch_free(bdma_chan);
970 }
971
972 list_del(&chan->device_node);
973 }
974}