2 * timb_dma.c timberdale FPGA DMA driver
3 * Copyright (c) 2010 Intel Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Timberdale FPGA DMA engine
23 #include <linux/dmaengine.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/slab.h>
32 #include <linux/timb_dma.h>
34 #include "dmaengine.h"
36 #define DRIVER_NAME "timb-dma"
38 /* Global DMA registers */
39 #define TIMBDMA_ACR 0x34
40 #define TIMBDMA_32BIT_ADDR 0x01
42 #define TIMBDMA_ISR 0x080000
43 #define TIMBDMA_IPR 0x080004
44 #define TIMBDMA_IER 0x080008
46 /* Channel specific registers */
47 /* RX instances base addresses are 0x00, 0x40, 0x80 ...
48 * TX instances base addresses are 0x18, 0x58, 0x98 ...
50 #define TIMBDMA_INSTANCE_OFFSET 0x40
51 #define TIMBDMA_INSTANCE_TX_OFFSET 0x18
53 /* RX registers, relative the instance base */
54 #define TIMBDMA_OFFS_RX_DHAR 0x00
55 #define TIMBDMA_OFFS_RX_DLAR 0x04
56 #define TIMBDMA_OFFS_RX_LR 0x0C
57 #define TIMBDMA_OFFS_RX_BLR 0x10
58 #define TIMBDMA_OFFS_RX_ER 0x14
59 #define TIMBDMA_RX_EN 0x01
60 /* bytes per Row, video specific register
61 * which is placed after the TX registers...
63 #define TIMBDMA_OFFS_RX_BPRR 0x30
65 /* TX registers, relative the instance base */
66 #define TIMBDMA_OFFS_TX_DHAR 0x00
67 #define TIMBDMA_OFFS_TX_DLAR 0x04
68 #define TIMBDMA_OFFS_TX_BLR 0x0C
69 #define TIMBDMA_OFFS_TX_LR 0x14
72 #define TIMB_DMA_DESC_SIZE 8
74 struct timb_dma_desc {
75 struct list_head desc_node;
76 struct dma_async_tx_descriptor txd;
78 unsigned int desc_list_len;
82 struct timb_dma_chan {
84 void __iomem *membase;
85 spinlock_t lock; /* Used to protect data structures,
86 especially the lists and descriptors,
87 from races between the tasklet and calls
90 struct list_head active_list;
91 struct list_head queue;
92 struct list_head free_list;
93 unsigned int bytes_per_line;
94 enum dma_transfer_direction direction;
95 unsigned int descs; /* Descriptors to allocate */
96 unsigned int desc_elems; /* number of elems per descriptor */
100 struct dma_device dma;
101 void __iomem *membase;
102 struct tasklet_struct tasklet;
103 struct timb_dma_chan channels[0];
106 static struct device *chan2dev(struct dma_chan *chan)
108 return &chan->dev->device;
110 static struct device *chan2dmadev(struct dma_chan *chan)
112 return chan2dev(chan)->parent->parent;
115 static struct timb_dma *tdchantotd(struct timb_dma_chan *td_chan)
117 int id = td_chan->chan.chan_id;
118 return (struct timb_dma *)((u8 *)td_chan -
119 id * sizeof(struct timb_dma_chan) - sizeof(struct timb_dma));
122 /* Must be called with the spinlock held */
123 static void __td_enable_chan_irq(struct timb_dma_chan *td_chan)
125 int id = td_chan->chan.chan_id;
126 struct timb_dma *td = tdchantotd(td_chan);
129 /* enable interrupt for this channel */
130 ier = ioread32(td->membase + TIMBDMA_IER);
132 dev_dbg(chan2dev(&td_chan->chan), "Enabling irq: %d, IER: 0x%x\n", id,
134 iowrite32(ier, td->membase + TIMBDMA_IER);
137 /* Should be called with the spinlock held */
138 static bool __td_dma_done_ack(struct timb_dma_chan *td_chan)
140 int id = td_chan->chan.chan_id;
141 struct timb_dma *td = (struct timb_dma *)((u8 *)td_chan -
142 id * sizeof(struct timb_dma_chan) - sizeof(struct timb_dma));
146 dev_dbg(chan2dev(&td_chan->chan), "Checking irq: %d, td: %p\n", id, td);
148 isr = ioread32(td->membase + TIMBDMA_ISR) & (1 << id);
150 iowrite32(isr, td->membase + TIMBDMA_ISR);
157 static int td_fill_desc(struct timb_dma_chan *td_chan, u8 *dma_desc,
158 struct scatterlist *sg, bool last)
160 if (sg_dma_len(sg) > USHRT_MAX) {
161 dev_err(chan2dev(&td_chan->chan), "Too big sg element\n");
165 /* length must be word aligned */
166 if (sg_dma_len(sg) % sizeof(u32)) {
167 dev_err(chan2dev(&td_chan->chan), "Incorrect length: %d\n",
172 dev_dbg(chan2dev(&td_chan->chan), "desc: %p, addr: 0x%llx\n",
173 dma_desc, (unsigned long long)sg_dma_address(sg));
175 dma_desc[7] = (sg_dma_address(sg) >> 24) & 0xff;
176 dma_desc[6] = (sg_dma_address(sg) >> 16) & 0xff;
177 dma_desc[5] = (sg_dma_address(sg) >> 8) & 0xff;
178 dma_desc[4] = (sg_dma_address(sg) >> 0) & 0xff;
180 dma_desc[3] = (sg_dma_len(sg) >> 8) & 0xff;
181 dma_desc[2] = (sg_dma_len(sg) >> 0) & 0xff;
184 dma_desc[0] = 0x21 | (last ? 0x02 : 0); /* tran, valid */
189 /* Must be called with the spinlock held */
190 static void __td_start_dma(struct timb_dma_chan *td_chan)
192 struct timb_dma_desc *td_desc;
194 if (td_chan->ongoing) {
195 dev_err(chan2dev(&td_chan->chan),
196 "Transfer already ongoing\n");
200 td_desc = list_entry(td_chan->active_list.next, struct timb_dma_desc,
203 dev_dbg(chan2dev(&td_chan->chan),
204 "td_chan: %p, chan: %d, membase: %p\n",
205 td_chan, td_chan->chan.chan_id, td_chan->membase);
207 if (td_chan->direction == DMA_DEV_TO_MEM) {
209 /* descriptor address */
210 iowrite32(0, td_chan->membase + TIMBDMA_OFFS_RX_DHAR);
211 iowrite32(td_desc->txd.phys, td_chan->membase +
212 TIMBDMA_OFFS_RX_DLAR);
214 iowrite32(td_chan->bytes_per_line, td_chan->membase +
215 TIMBDMA_OFFS_RX_BPRR);
217 iowrite32(TIMBDMA_RX_EN, td_chan->membase + TIMBDMA_OFFS_RX_ER);
220 iowrite32(0, td_chan->membase + TIMBDMA_OFFS_TX_DHAR);
221 iowrite32(td_desc->txd.phys, td_chan->membase +
222 TIMBDMA_OFFS_TX_DLAR);
225 td_chan->ongoing = true;
227 if (td_desc->interrupt)
228 __td_enable_chan_irq(td_chan);
231 static void __td_finish(struct timb_dma_chan *td_chan)
233 dma_async_tx_callback callback;
235 struct dma_async_tx_descriptor *txd;
236 struct timb_dma_desc *td_desc;
238 /* can happen if the descriptor is canceled */
239 if (list_empty(&td_chan->active_list))
242 td_desc = list_entry(td_chan->active_list.next, struct timb_dma_desc,
246 dev_dbg(chan2dev(&td_chan->chan), "descriptor %u complete\n",
249 /* make sure to stop the transfer */
250 if (td_chan->direction == DMA_DEV_TO_MEM)
251 iowrite32(0, td_chan->membase + TIMBDMA_OFFS_RX_ER);
252 /* Currently no support for stopping DMA transfers
254 iowrite32(0, td_chan->membase + TIMBDMA_OFFS_TX_DLAR);
256 dma_cookie_complete(txd);
257 td_chan->ongoing = false;
259 callback = txd->callback;
260 param = txd->callback_param;
262 list_move(&td_desc->desc_node, &td_chan->free_list);
264 dma_descriptor_unmap(txd);
266 * The API requires that no submissions are done from a
267 * callback, so we don't need to drop the lock here
273 static u32 __td_ier_mask(struct timb_dma *td)
278 for (i = 0; i < td->dma.chancnt; i++) {
279 struct timb_dma_chan *td_chan = td->channels + i;
280 if (td_chan->ongoing) {
281 struct timb_dma_desc *td_desc =
282 list_entry(td_chan->active_list.next,
283 struct timb_dma_desc, desc_node);
284 if (td_desc->interrupt)
292 static void __td_start_next(struct timb_dma_chan *td_chan)
294 struct timb_dma_desc *td_desc;
296 BUG_ON(list_empty(&td_chan->queue));
297 BUG_ON(td_chan->ongoing);
299 td_desc = list_entry(td_chan->queue.next, struct timb_dma_desc,
302 dev_dbg(chan2dev(&td_chan->chan), "%s: started %u\n",
303 __func__, td_desc->txd.cookie);
305 list_move(&td_desc->desc_node, &td_chan->active_list);
306 __td_start_dma(td_chan);
309 static dma_cookie_t td_tx_submit(struct dma_async_tx_descriptor *txd)
311 struct timb_dma_desc *td_desc = container_of(txd, struct timb_dma_desc,
313 struct timb_dma_chan *td_chan = container_of(txd->chan,
314 struct timb_dma_chan, chan);
317 spin_lock_bh(&td_chan->lock);
318 cookie = dma_cookie_assign(txd);
320 if (list_empty(&td_chan->active_list)) {
321 dev_dbg(chan2dev(txd->chan), "%s: started %u\n", __func__,
323 list_add_tail(&td_desc->desc_node, &td_chan->active_list);
324 __td_start_dma(td_chan);
326 dev_dbg(chan2dev(txd->chan), "tx_submit: queued %u\n",
329 list_add_tail(&td_desc->desc_node, &td_chan->queue);
332 spin_unlock_bh(&td_chan->lock);
337 static struct timb_dma_desc *td_alloc_init_desc(struct timb_dma_chan *td_chan)
339 struct dma_chan *chan = &td_chan->chan;
340 struct timb_dma_desc *td_desc;
343 td_desc = kzalloc(sizeof(struct timb_dma_desc), GFP_KERNEL);
345 dev_err(chan2dev(chan), "Failed to alloc descriptor\n");
349 td_desc->desc_list_len = td_chan->desc_elems * TIMB_DMA_DESC_SIZE;
351 td_desc->desc_list = kzalloc(td_desc->desc_list_len, GFP_KERNEL);
352 if (!td_desc->desc_list) {
353 dev_err(chan2dev(chan), "Failed to alloc descriptor\n");
357 dma_async_tx_descriptor_init(&td_desc->txd, chan);
358 td_desc->txd.tx_submit = td_tx_submit;
359 td_desc->txd.flags = DMA_CTRL_ACK;
361 td_desc->txd.phys = dma_map_single(chan2dmadev(chan),
362 td_desc->desc_list, td_desc->desc_list_len, DMA_TO_DEVICE);
364 err = dma_mapping_error(chan2dmadev(chan), td_desc->txd.phys);
366 dev_err(chan2dev(chan), "DMA mapping error: %d\n", err);
372 kfree(td_desc->desc_list);
379 static void td_free_desc(struct timb_dma_desc *td_desc)
381 dev_dbg(chan2dev(td_desc->txd.chan), "Freeing desc: %p\n", td_desc);
382 dma_unmap_single(chan2dmadev(td_desc->txd.chan), td_desc->txd.phys,
383 td_desc->desc_list_len, DMA_TO_DEVICE);
385 kfree(td_desc->desc_list);
389 static void td_desc_put(struct timb_dma_chan *td_chan,
390 struct timb_dma_desc *td_desc)
392 dev_dbg(chan2dev(&td_chan->chan), "Putting desc: %p\n", td_desc);
394 spin_lock_bh(&td_chan->lock);
395 list_add(&td_desc->desc_node, &td_chan->free_list);
396 spin_unlock_bh(&td_chan->lock);
399 static struct timb_dma_desc *td_desc_get(struct timb_dma_chan *td_chan)
401 struct timb_dma_desc *td_desc, *_td_desc;
402 struct timb_dma_desc *ret = NULL;
404 spin_lock_bh(&td_chan->lock);
405 list_for_each_entry_safe(td_desc, _td_desc, &td_chan->free_list,
407 if (async_tx_test_ack(&td_desc->txd)) {
408 list_del(&td_desc->desc_node);
412 dev_dbg(chan2dev(&td_chan->chan), "desc %p not ACKed\n",
415 spin_unlock_bh(&td_chan->lock);
420 static int td_alloc_chan_resources(struct dma_chan *chan)
422 struct timb_dma_chan *td_chan =
423 container_of(chan, struct timb_dma_chan, chan);
426 dev_dbg(chan2dev(chan), "%s: entry\n", __func__);
428 BUG_ON(!list_empty(&td_chan->free_list));
429 for (i = 0; i < td_chan->descs; i++) {
430 struct timb_dma_desc *td_desc = td_alloc_init_desc(td_chan);
435 dev_err(chan2dev(chan),
436 "Couldnt allocate any descriptors\n");
441 td_desc_put(td_chan, td_desc);
444 spin_lock_bh(&td_chan->lock);
445 dma_cookie_init(chan);
446 spin_unlock_bh(&td_chan->lock);
451 static void td_free_chan_resources(struct dma_chan *chan)
453 struct timb_dma_chan *td_chan =
454 container_of(chan, struct timb_dma_chan, chan);
455 struct timb_dma_desc *td_desc, *_td_desc;
458 dev_dbg(chan2dev(chan), "%s: Entry\n", __func__);
460 /* check that all descriptors are free */
461 BUG_ON(!list_empty(&td_chan->active_list));
462 BUG_ON(!list_empty(&td_chan->queue));
464 spin_lock_bh(&td_chan->lock);
465 list_splice_init(&td_chan->free_list, &list);
466 spin_unlock_bh(&td_chan->lock);
468 list_for_each_entry_safe(td_desc, _td_desc, &list, desc_node) {
469 dev_dbg(chan2dev(chan), "%s: Freeing desc: %p\n", __func__,
471 td_free_desc(td_desc);
475 static enum dma_status td_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
476 struct dma_tx_state *txstate)
480 dev_dbg(chan2dev(chan), "%s: Entry\n", __func__);
482 ret = dma_cookie_status(chan, cookie, txstate);
484 dev_dbg(chan2dev(chan), "%s: exit, ret: %d\n", __func__, ret);
489 static void td_issue_pending(struct dma_chan *chan)
491 struct timb_dma_chan *td_chan =
492 container_of(chan, struct timb_dma_chan, chan);
494 dev_dbg(chan2dev(chan), "%s: Entry\n", __func__);
495 spin_lock_bh(&td_chan->lock);
497 if (!list_empty(&td_chan->active_list))
498 /* transfer ongoing */
499 if (__td_dma_done_ack(td_chan))
500 __td_finish(td_chan);
502 if (list_empty(&td_chan->active_list) && !list_empty(&td_chan->queue))
503 __td_start_next(td_chan);
505 spin_unlock_bh(&td_chan->lock);
508 static struct dma_async_tx_descriptor *td_prep_slave_sg(struct dma_chan *chan,
509 struct scatterlist *sgl, unsigned int sg_len,
510 enum dma_transfer_direction direction, unsigned long flags,
513 struct timb_dma_chan *td_chan =
514 container_of(chan, struct timb_dma_chan, chan);
515 struct timb_dma_desc *td_desc;
516 struct scatterlist *sg;
518 unsigned int desc_usage = 0;
520 if (!sgl || !sg_len) {
521 dev_err(chan2dev(chan), "%s: No SG list\n", __func__);
525 /* even channels are for RX, odd for TX */
526 if (td_chan->direction != direction) {
527 dev_err(chan2dev(chan),
528 "Requesting channel in wrong direction\n");
532 td_desc = td_desc_get(td_chan);
534 dev_err(chan2dev(chan), "Not enough descriptors available\n");
538 td_desc->interrupt = (flags & DMA_PREP_INTERRUPT) != 0;
540 for_each_sg(sgl, sg, sg_len, i) {
542 if (desc_usage > td_desc->desc_list_len) {
543 dev_err(chan2dev(chan), "No descriptor space\n");
547 err = td_fill_desc(td_chan, td_desc->desc_list + desc_usage, sg,
550 dev_err(chan2dev(chan), "Failed to update desc: %d\n",
552 td_desc_put(td_chan, td_desc);
555 desc_usage += TIMB_DMA_DESC_SIZE;
558 dma_sync_single_for_device(chan2dmadev(chan), td_desc->txd.phys,
559 td_desc->desc_list_len, DMA_MEM_TO_DEV);
561 return &td_desc->txd;
564 static int td_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
567 struct timb_dma_chan *td_chan =
568 container_of(chan, struct timb_dma_chan, chan);
569 struct timb_dma_desc *td_desc, *_td_desc;
571 dev_dbg(chan2dev(chan), "%s: Entry\n", __func__);
573 if (cmd != DMA_TERMINATE_ALL)
576 /* first the easy part, put the queue into the free list */
577 spin_lock_bh(&td_chan->lock);
578 list_for_each_entry_safe(td_desc, _td_desc, &td_chan->queue,
580 list_move(&td_desc->desc_node, &td_chan->free_list);
582 /* now tear down the running */
583 __td_finish(td_chan);
584 spin_unlock_bh(&td_chan->lock);
589 static void td_tasklet(unsigned long data)
591 struct timb_dma *td = (struct timb_dma *)data;
597 isr = ioread32(td->membase + TIMBDMA_ISR);
598 ipr = isr & __td_ier_mask(td);
600 /* ack the interrupts */
601 iowrite32(ipr, td->membase + TIMBDMA_ISR);
603 for (i = 0; i < td->dma.chancnt; i++)
604 if (ipr & (1 << i)) {
605 struct timb_dma_chan *td_chan = td->channels + i;
606 spin_lock(&td_chan->lock);
607 __td_finish(td_chan);
608 if (!list_empty(&td_chan->queue))
609 __td_start_next(td_chan);
610 spin_unlock(&td_chan->lock);
613 ier = __td_ier_mask(td);
614 iowrite32(ier, td->membase + TIMBDMA_IER);
618 static irqreturn_t td_irq(int irq, void *devid)
620 struct timb_dma *td = devid;
621 u32 ipr = ioread32(td->membase + TIMBDMA_IPR);
624 /* disable interrupts, will be re-enabled in tasklet */
625 iowrite32(0, td->membase + TIMBDMA_IER);
627 tasklet_schedule(&td->tasklet);
635 static int td_probe(struct platform_device *pdev)
637 struct timb_dma_platform_data *pdata = dev_get_platdata(&pdev->dev);
639 struct resource *iomem;
645 dev_err(&pdev->dev, "No platform data\n");
649 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
653 irq = platform_get_irq(pdev, 0);
657 if (!request_mem_region(iomem->start, resource_size(iomem),
661 td = kzalloc(sizeof(struct timb_dma) +
662 sizeof(struct timb_dma_chan) * pdata->nr_channels, GFP_KERNEL);
665 goto err_release_region;
668 dev_dbg(&pdev->dev, "Allocated TD: %p\n", td);
670 td->membase = ioremap(iomem->start, resource_size(iomem));
672 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
677 /* 32bit addressing */
678 iowrite32(TIMBDMA_32BIT_ADDR, td->membase + TIMBDMA_ACR);
680 /* disable and clear any interrupts */
681 iowrite32(0x0, td->membase + TIMBDMA_IER);
682 iowrite32(0xFFFFFFFF, td->membase + TIMBDMA_ISR);
684 tasklet_init(&td->tasklet, td_tasklet, (unsigned long)td);
686 err = request_irq(irq, td_irq, IRQF_SHARED, DRIVER_NAME, td);
688 dev_err(&pdev->dev, "Failed to request IRQ\n");
689 goto err_tasklet_kill;
692 td->dma.device_alloc_chan_resources = td_alloc_chan_resources;
693 td->dma.device_free_chan_resources = td_free_chan_resources;
694 td->dma.device_tx_status = td_tx_status;
695 td->dma.device_issue_pending = td_issue_pending;
697 dma_cap_set(DMA_SLAVE, td->dma.cap_mask);
698 dma_cap_set(DMA_PRIVATE, td->dma.cap_mask);
699 td->dma.device_prep_slave_sg = td_prep_slave_sg;
700 td->dma.device_control = td_control;
702 td->dma.dev = &pdev->dev;
704 INIT_LIST_HEAD(&td->dma.channels);
706 for (i = 0; i < pdata->nr_channels; i++) {
707 struct timb_dma_chan *td_chan = &td->channels[i];
708 struct timb_dma_platform_data_channel *pchan =
711 /* even channels are RX, odd are TX */
712 if ((i % 2) == pchan->rx) {
713 dev_err(&pdev->dev, "Wrong channel configuration\n");
718 td_chan->chan.device = &td->dma;
719 dma_cookie_init(&td_chan->chan);
720 spin_lock_init(&td_chan->lock);
721 INIT_LIST_HEAD(&td_chan->active_list);
722 INIT_LIST_HEAD(&td_chan->queue);
723 INIT_LIST_HEAD(&td_chan->free_list);
725 td_chan->descs = pchan->descriptors;
726 td_chan->desc_elems = pchan->descriptor_elements;
727 td_chan->bytes_per_line = pchan->bytes_per_line;
728 td_chan->direction = pchan->rx ? DMA_DEV_TO_MEM :
731 td_chan->membase = td->membase +
732 (i / 2) * TIMBDMA_INSTANCE_OFFSET +
733 (pchan->rx ? 0 : TIMBDMA_INSTANCE_TX_OFFSET);
735 dev_dbg(&pdev->dev, "Chan: %d, membase: %p\n",
736 i, td_chan->membase);
738 list_add_tail(&td_chan->chan.device_node, &td->dma.channels);
741 err = dma_async_device_register(&td->dma);
743 dev_err(&pdev->dev, "Failed to register async device\n");
747 platform_set_drvdata(pdev, td);
749 dev_dbg(&pdev->dev, "Probe result: %d\n", err);
755 tasklet_kill(&td->tasklet);
756 iounmap(td->membase);
760 release_mem_region(iomem->start, resource_size(iomem));
766 static int td_remove(struct platform_device *pdev)
768 struct timb_dma *td = platform_get_drvdata(pdev);
769 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
770 int irq = platform_get_irq(pdev, 0);
772 dma_async_device_unregister(&td->dma);
774 tasklet_kill(&td->tasklet);
775 iounmap(td->membase);
777 release_mem_region(iomem->start, resource_size(iomem));
779 dev_dbg(&pdev->dev, "Removed...\n");
783 static struct platform_driver td_driver = {
786 .owner = THIS_MODULE,
792 module_platform_driver(td_driver);
794 MODULE_LICENSE("GPL v2");
795 MODULE_DESCRIPTION("Timberdale DMA controller driver");
796 MODULE_AUTHOR("Pelagicore AB <info@pelagicore.com>");
797 MODULE_ALIAS("platform:"DRIVER_NAME);