of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / dma / bcm2835-dma.c
blob996c4b00d323ed5c2dd6455f80eb36ed8e9a8b23
1 /*
2 * BCM2835 DMA engine support
4 * This driver only supports cyclic DMA transfers
5 * as needed for the I2S module.
7 * Author: Florian Meier <florian.meier@koalo.de>
8 * Copyright 2013
10 * Based on
11 * OMAP DMAengine support by Russell King
13 * BCM2708 DMA Driver
14 * Copyright (C) 2010 Broadcom
16 * Raspberry Pi PCM I2S ALSA Driver
17 * Copyright (c) by Phil Poole 2013
19 * MARVELL MMP Peripheral DMA Driver
20 * Copyright 2012 Marvell International Ltd.
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 #include <linux/dmaengine.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/dmapool.h>
35 #include <linux/err.h>
36 #include <linux/init.h>
37 #include <linux/interrupt.h>
38 #include <linux/list.h>
39 #include <linux/module.h>
40 #include <linux/platform_device.h>
41 #include <linux/slab.h>
42 #include <linux/io.h>
43 #include <linux/spinlock.h>
44 #include <linux/of.h>
45 #include <linux/of_dma.h>
47 #include "virt-dma.h"
49 struct bcm2835_dmadev {
50 struct dma_device ddev;
51 spinlock_t lock;
52 void __iomem *base;
53 struct device_dma_parameters dma_parms;
56 struct bcm2835_dma_cb {
57 uint32_t info;
58 uint32_t src;
59 uint32_t dst;
60 uint32_t length;
61 uint32_t stride;
62 uint32_t next;
63 uint32_t pad[2];
66 struct bcm2835_cb_entry {
67 struct bcm2835_dma_cb *cb;
68 dma_addr_t paddr;
71 struct bcm2835_chan {
72 struct virt_dma_chan vc;
73 struct list_head node;
75 struct dma_slave_config cfg;
76 bool cyclic;
77 unsigned int dreq;
79 int ch;
80 struct bcm2835_desc *desc;
81 struct dma_pool *cb_pool;
83 void __iomem *chan_base;
84 int irq_number;
87 struct bcm2835_desc {
88 struct bcm2835_chan *c;
89 struct virt_dma_desc vd;
90 enum dma_transfer_direction dir;
92 struct bcm2835_cb_entry *cb_list;
94 unsigned int frames;
95 size_t size;
98 #define BCM2835_DMA_CS 0x00
99 #define BCM2835_DMA_ADDR 0x04
100 #define BCM2835_DMA_SOURCE_AD 0x0c
101 #define BCM2835_DMA_DEST_AD 0x10
102 #define BCM2835_DMA_NEXTCB 0x1C
104 /* DMA CS Control and Status bits */
105 #define BCM2835_DMA_ACTIVE BIT(0)
106 #define BCM2835_DMA_INT BIT(2)
107 #define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
108 #define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
109 #define BCM2835_DMA_ERR BIT(8)
110 #define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
111 #define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
113 #define BCM2835_DMA_INT_EN BIT(0)
114 #define BCM2835_DMA_D_INC BIT(4)
115 #define BCM2835_DMA_D_DREQ BIT(6)
116 #define BCM2835_DMA_S_INC BIT(8)
117 #define BCM2835_DMA_S_DREQ BIT(10)
119 #define BCM2835_DMA_PER_MAP(x) ((x) << 16)
121 #define BCM2835_DMA_DATA_TYPE_S8 1
122 #define BCM2835_DMA_DATA_TYPE_S16 2
123 #define BCM2835_DMA_DATA_TYPE_S32 4
124 #define BCM2835_DMA_DATA_TYPE_S128 16
126 #define BCM2835_DMA_BULK_MASK BIT(0)
127 #define BCM2835_DMA_FIQ_MASK (BIT(2) | BIT(3))
129 /* Valid only for channels 0 - 14, 15 has its own base address */
130 #define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
131 #define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
133 static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
135 return container_of(d, struct bcm2835_dmadev, ddev);
138 static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
140 return container_of(c, struct bcm2835_chan, vc.chan);
143 static inline struct bcm2835_desc *to_bcm2835_dma_desc(
144 struct dma_async_tx_descriptor *t)
146 return container_of(t, struct bcm2835_desc, vd.tx);
149 static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
151 struct bcm2835_desc *desc = container_of(vd, struct bcm2835_desc, vd);
152 int i;
154 for (i = 0; i < desc->frames; i++)
155 dma_pool_free(desc->c->cb_pool, desc->cb_list[i].cb,
156 desc->cb_list[i].paddr);
158 kfree(desc->cb_list);
159 kfree(desc);
162 static int bcm2835_dma_abort(void __iomem *chan_base)
164 unsigned long cs;
165 long int timeout = 10000;
167 cs = readl(chan_base + BCM2835_DMA_CS);
168 if (!(cs & BCM2835_DMA_ACTIVE))
169 return 0;
171 /* Write 0 to the active bit - Pause the DMA */
172 writel(0, chan_base + BCM2835_DMA_CS);
174 /* Wait for any current AXI transfer to complete */
175 while ((cs & BCM2835_DMA_ISPAUSED) && --timeout) {
176 cpu_relax();
177 cs = readl(chan_base + BCM2835_DMA_CS);
180 /* We'll un-pause when we set of our next DMA */
181 if (!timeout)
182 return -ETIMEDOUT;
184 if (!(cs & BCM2835_DMA_ACTIVE))
185 return 0;
187 /* Terminate the control block chain */
188 writel(0, chan_base + BCM2835_DMA_NEXTCB);
190 /* Abort the whole DMA */
191 writel(BCM2835_DMA_ABORT | BCM2835_DMA_ACTIVE,
192 chan_base + BCM2835_DMA_CS);
194 return 0;
197 static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
199 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
200 struct bcm2835_desc *d;
202 if (!vd) {
203 c->desc = NULL;
204 return;
207 list_del(&vd->node);
209 c->desc = d = to_bcm2835_dma_desc(&vd->tx);
211 writel(d->cb_list[0].paddr, c->chan_base + BCM2835_DMA_ADDR);
212 writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
215 static irqreturn_t bcm2835_dma_callback(int irq, void *data)
217 struct bcm2835_chan *c = data;
218 struct bcm2835_desc *d;
219 unsigned long flags;
221 spin_lock_irqsave(&c->vc.lock, flags);
223 /* Acknowledge interrupt */
224 writel(BCM2835_DMA_INT, c->chan_base + BCM2835_DMA_CS);
226 d = c->desc;
228 if (d) {
229 /* TODO Only works for cyclic DMA */
230 vchan_cyclic_callback(&d->vd);
233 /* Keep the DMA engine running */
234 writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
236 spin_unlock_irqrestore(&c->vc.lock, flags);
238 return IRQ_HANDLED;
241 static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
243 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
244 struct device *dev = c->vc.chan.device->dev;
246 dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
248 c->cb_pool = dma_pool_create(dev_name(dev), dev,
249 sizeof(struct bcm2835_dma_cb), 0, 0);
250 if (!c->cb_pool) {
251 dev_err(dev, "unable to allocate descriptor pool\n");
252 return -ENOMEM;
255 return request_irq(c->irq_number,
256 bcm2835_dma_callback, 0, "DMA IRQ", c);
259 static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
261 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
263 vchan_free_chan_resources(&c->vc);
264 free_irq(c->irq_number, c);
265 dma_pool_destroy(c->cb_pool);
267 dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
270 static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
272 return d->size;
275 static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
277 unsigned int i;
278 size_t size;
280 for (size = i = 0; i < d->frames; i++) {
281 struct bcm2835_dma_cb *control_block = d->cb_list[i].cb;
282 size_t this_size = control_block->length;
283 dma_addr_t dma;
285 if (d->dir == DMA_DEV_TO_MEM)
286 dma = control_block->dst;
287 else
288 dma = control_block->src;
290 if (size)
291 size += this_size;
292 else if (addr >= dma && addr < dma + this_size)
293 size += dma + this_size - addr;
296 return size;
299 static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
300 dma_cookie_t cookie, struct dma_tx_state *txstate)
302 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
303 struct virt_dma_desc *vd;
304 enum dma_status ret;
305 unsigned long flags;
307 ret = dma_cookie_status(chan, cookie, txstate);
308 if (ret == DMA_COMPLETE || !txstate)
309 return ret;
311 spin_lock_irqsave(&c->vc.lock, flags);
312 vd = vchan_find_desc(&c->vc, cookie);
313 if (vd) {
314 txstate->residue =
315 bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
316 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
317 struct bcm2835_desc *d = c->desc;
318 dma_addr_t pos;
320 if (d->dir == DMA_MEM_TO_DEV)
321 pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
322 else if (d->dir == DMA_DEV_TO_MEM)
323 pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
324 else
325 pos = 0;
327 txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
328 } else {
329 txstate->residue = 0;
332 spin_unlock_irqrestore(&c->vc.lock, flags);
334 return ret;
337 static void bcm2835_dma_issue_pending(struct dma_chan *chan)
339 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
340 unsigned long flags;
342 c->cyclic = true; /* Nothing else is implemented */
344 spin_lock_irqsave(&c->vc.lock, flags);
345 if (vchan_issue_pending(&c->vc) && !c->desc)
346 bcm2835_dma_start_desc(c);
348 spin_unlock_irqrestore(&c->vc.lock, flags);
351 static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
352 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
353 size_t period_len, enum dma_transfer_direction direction,
354 unsigned long flags)
356 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
357 enum dma_slave_buswidth dev_width;
358 struct bcm2835_desc *d;
359 dma_addr_t dev_addr;
360 unsigned int es, sync_type;
361 unsigned int frame;
362 int i;
364 /* Grab configuration */
365 if (!is_slave_direction(direction)) {
366 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
367 return NULL;
370 if (direction == DMA_DEV_TO_MEM) {
371 dev_addr = c->cfg.src_addr;
372 dev_width = c->cfg.src_addr_width;
373 sync_type = BCM2835_DMA_S_DREQ;
374 } else {
375 dev_addr = c->cfg.dst_addr;
376 dev_width = c->cfg.dst_addr_width;
377 sync_type = BCM2835_DMA_D_DREQ;
380 /* Bus width translates to the element size (ES) */
381 switch (dev_width) {
382 case DMA_SLAVE_BUSWIDTH_4_BYTES:
383 es = BCM2835_DMA_DATA_TYPE_S32;
384 break;
385 default:
386 return NULL;
389 /* Now allocate and setup the descriptor. */
390 d = kzalloc(sizeof(*d), GFP_NOWAIT);
391 if (!d)
392 return NULL;
394 d->c = c;
395 d->dir = direction;
396 d->frames = buf_len / period_len;
398 d->cb_list = kcalloc(d->frames, sizeof(*d->cb_list), GFP_KERNEL);
399 if (!d->cb_list) {
400 kfree(d);
401 return NULL;
403 /* Allocate memory for control blocks */
404 for (i = 0; i < d->frames; i++) {
405 struct bcm2835_cb_entry *cb_entry = &d->cb_list[i];
407 cb_entry->cb = dma_pool_zalloc(c->cb_pool, GFP_ATOMIC,
408 &cb_entry->paddr);
409 if (!cb_entry->cb)
410 goto error_cb;
414 * Iterate over all frames, create a control block
415 * for each frame and link them together.
417 for (frame = 0; frame < d->frames; frame++) {
418 struct bcm2835_dma_cb *control_block = d->cb_list[frame].cb;
420 /* Setup adresses */
421 if (d->dir == DMA_DEV_TO_MEM) {
422 control_block->info = BCM2835_DMA_D_INC;
423 control_block->src = dev_addr;
424 control_block->dst = buf_addr + frame * period_len;
425 } else {
426 control_block->info = BCM2835_DMA_S_INC;
427 control_block->src = buf_addr + frame * period_len;
428 control_block->dst = dev_addr;
431 /* Enable interrupt */
432 control_block->info |= BCM2835_DMA_INT_EN;
434 /* Setup synchronization */
435 if (sync_type != 0)
436 control_block->info |= sync_type;
438 /* Setup DREQ channel */
439 if (c->dreq != 0)
440 control_block->info |=
441 BCM2835_DMA_PER_MAP(c->dreq);
443 /* Length of a frame */
444 control_block->length = period_len;
445 d->size += control_block->length;
448 * Next block is the next frame.
449 * This DMA engine driver currently only supports cyclic DMA.
450 * Therefore, wrap around at number of frames.
452 control_block->next = d->cb_list[((frame + 1) % d->frames)].paddr;
455 return vchan_tx_prep(&c->vc, &d->vd, flags);
456 error_cb:
457 i--;
458 for (; i >= 0; i--) {
459 struct bcm2835_cb_entry *cb_entry = &d->cb_list[i];
461 dma_pool_free(c->cb_pool, cb_entry->cb, cb_entry->paddr);
464 kfree(d->cb_list);
465 kfree(d);
466 return NULL;
469 static int bcm2835_dma_slave_config(struct dma_chan *chan,
470 struct dma_slave_config *cfg)
472 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
474 if ((cfg->direction == DMA_DEV_TO_MEM &&
475 cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
476 (cfg->direction == DMA_MEM_TO_DEV &&
477 cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
478 !is_slave_direction(cfg->direction)) {
479 return -EINVAL;
482 c->cfg = *cfg;
484 return 0;
487 static int bcm2835_dma_terminate_all(struct dma_chan *chan)
489 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
490 struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
491 unsigned long flags;
492 int timeout = 10000;
493 LIST_HEAD(head);
495 spin_lock_irqsave(&c->vc.lock, flags);
497 /* Prevent this channel being scheduled */
498 spin_lock(&d->lock);
499 list_del_init(&c->node);
500 spin_unlock(&d->lock);
503 * Stop DMA activity: we assume the callback will not be called
504 * after bcm_dma_abort() returns (even if it does, it will see
505 * c->desc is NULL and exit.)
507 if (c->desc) {
508 bcm2835_dma_desc_free(&c->desc->vd);
509 c->desc = NULL;
510 bcm2835_dma_abort(c->chan_base);
512 /* Wait for stopping */
513 while (--timeout) {
514 if (!(readl(c->chan_base + BCM2835_DMA_CS) &
515 BCM2835_DMA_ACTIVE))
516 break;
518 cpu_relax();
521 if (!timeout)
522 dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
525 vchan_get_all_descriptors(&c->vc, &head);
526 spin_unlock_irqrestore(&c->vc.lock, flags);
527 vchan_dma_desc_free_list(&c->vc, &head);
529 return 0;
532 static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id, int irq)
534 struct bcm2835_chan *c;
536 c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
537 if (!c)
538 return -ENOMEM;
540 c->vc.desc_free = bcm2835_dma_desc_free;
541 vchan_init(&c->vc, &d->ddev);
542 INIT_LIST_HEAD(&c->node);
544 c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
545 c->ch = chan_id;
546 c->irq_number = irq;
548 return 0;
551 static void bcm2835_dma_free(struct bcm2835_dmadev *od)
553 struct bcm2835_chan *c, *next;
555 list_for_each_entry_safe(c, next, &od->ddev.channels,
556 vc.chan.device_node) {
557 list_del(&c->vc.chan.device_node);
558 tasklet_kill(&c->vc.task);
562 static const struct of_device_id bcm2835_dma_of_match[] = {
563 { .compatible = "brcm,bcm2835-dma", },
566 MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
568 static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
569 struct of_dma *ofdma)
571 struct bcm2835_dmadev *d = ofdma->of_dma_data;
572 struct dma_chan *chan;
574 chan = dma_get_any_slave_channel(&d->ddev);
575 if (!chan)
576 return NULL;
578 /* Set DREQ from param */
579 to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
581 return chan;
584 static int bcm2835_dma_probe(struct platform_device *pdev)
586 struct bcm2835_dmadev *od;
587 struct resource *res;
588 void __iomem *base;
589 int rc;
590 int i;
591 int irq;
592 uint32_t chans_available;
594 if (!pdev->dev.dma_mask)
595 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
597 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
598 if (rc)
599 return rc;
601 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
602 if (!od)
603 return -ENOMEM;
605 pdev->dev.dma_parms = &od->dma_parms;
606 dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
608 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
609 base = devm_ioremap_resource(&pdev->dev, res);
610 if (IS_ERR(base))
611 return PTR_ERR(base);
613 od->base = base;
615 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
616 dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
617 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
618 od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
619 od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
620 od->ddev.device_tx_status = bcm2835_dma_tx_status;
621 od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
622 od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
623 od->ddev.device_config = bcm2835_dma_slave_config;
624 od->ddev.device_terminate_all = bcm2835_dma_terminate_all;
625 od->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
626 od->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
627 od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
628 od->ddev.dev = &pdev->dev;
629 INIT_LIST_HEAD(&od->ddev.channels);
630 spin_lock_init(&od->lock);
632 platform_set_drvdata(pdev, od);
634 /* Request DMA channel mask from device tree */
635 if (of_property_read_u32(pdev->dev.of_node,
636 "brcm,dma-channel-mask",
637 &chans_available)) {
638 dev_err(&pdev->dev, "Failed to get channel mask\n");
639 rc = -EINVAL;
640 goto err_no_dma;
644 * Do not use the FIQ and BULK channels,
645 * because they are used by the GPU.
647 chans_available &= ~(BCM2835_DMA_FIQ_MASK | BCM2835_DMA_BULK_MASK);
649 for (i = 0; i < pdev->num_resources; i++) {
650 irq = platform_get_irq(pdev, i);
651 if (irq < 0)
652 break;
654 if (chans_available & (1 << i)) {
655 rc = bcm2835_dma_chan_init(od, i, irq);
656 if (rc)
657 goto err_no_dma;
661 dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
663 /* Device-tree DMA controller registration */
664 rc = of_dma_controller_register(pdev->dev.of_node,
665 bcm2835_dma_xlate, od);
666 if (rc) {
667 dev_err(&pdev->dev, "Failed to register DMA controller\n");
668 goto err_no_dma;
671 rc = dma_async_device_register(&od->ddev);
672 if (rc) {
673 dev_err(&pdev->dev,
674 "Failed to register slave DMA engine device: %d\n", rc);
675 goto err_no_dma;
678 dev_dbg(&pdev->dev, "Load BCM2835 DMA engine driver\n");
680 return 0;
682 err_no_dma:
683 bcm2835_dma_free(od);
684 return rc;
687 static int bcm2835_dma_remove(struct platform_device *pdev)
689 struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
691 dma_async_device_unregister(&od->ddev);
692 bcm2835_dma_free(od);
694 return 0;
697 static struct platform_driver bcm2835_dma_driver = {
698 .probe = bcm2835_dma_probe,
699 .remove = bcm2835_dma_remove,
700 .driver = {
701 .name = "bcm2835-dma",
702 .of_match_table = of_match_ptr(bcm2835_dma_of_match),
706 module_platform_driver(bcm2835_dma_driver);
708 MODULE_ALIAS("platform:bcm2835-dma");
709 MODULE_DESCRIPTION("BCM2835 DMA engine driver");
710 MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
711 MODULE_LICENSE("GPL v2");