Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Eric Miao | bd5ce43 | 2009-01-20 12:06:01 +0800 | [diff] [blame] | 2 | * linux/arch/arm/plat-pxa/dma.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * PXA DMA registration and IRQ dispatching |
| 5 | * |
| 6 | * Author: Nicolas Pitre |
| 7 | * Created: Nov 15, 2001 |
| 8 | * Copyright: MontaVista Software Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/errno.h> |
| 20 | |
| 21 | #include <asm/system.h> |
| 22 | #include <asm/irq.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 23 | #include <mach/hardware.h> |
Russell King | dcea83a | 2008-11-29 11:40:28 +0000 | [diff] [blame] | 24 | #include <mach/dma.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 26 | struct dma_channel { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | char *name; |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 28 | pxa_dma_prio prio; |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 29 | void (*irq_handler)(int, void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | void *data; |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 31 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 33 | static struct dma_channel *dma_channels; |
| 34 | static int num_dma_channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | int pxa_request_dma (char *name, pxa_dma_prio prio, |
Eric Miao | bd5ce43 | 2009-01-20 12:06:01 +0800 | [diff] [blame] | 37 | void (*irq_handler)(int, void *), |
| 38 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
| 40 | unsigned long flags; |
| 41 | int i, found = 0; |
| 42 | |
| 43 | /* basic sanity checks */ |
| 44 | if (!name || !irq_handler) |
| 45 | return -EINVAL; |
| 46 | |
| 47 | local_irq_save(flags); |
| 48 | |
Nicolas Pitre | 9953255 | 2006-05-05 22:32:24 +0100 | [diff] [blame] | 49 | do { |
| 50 | /* try grabbing a DMA channel with the requested priority */ |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 51 | for (i = 0; i < num_dma_channels; i++) { |
| 52 | if ((dma_channels[i].prio == prio) && |
| 53 | !dma_channels[i].name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | found = 1; |
| 55 | break; |
| 56 | } |
| 57 | } |
Nicolas Pitre | 9953255 | 2006-05-05 22:32:24 +0100 | [diff] [blame] | 58 | /* if requested prio group is full, try a hier priority */ |
| 59 | } while (!found && prio--); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | if (found) { |
| 62 | DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR; |
| 63 | dma_channels[i].name = name; |
| 64 | dma_channels[i].irq_handler = irq_handler; |
| 65 | dma_channels[i].data = data; |
| 66 | } else { |
| 67 | printk (KERN_WARNING "No more available DMA channels for %s\n", name); |
| 68 | i = -ENODEV; |
| 69 | } |
| 70 | |
| 71 | local_irq_restore(flags); |
| 72 | return i; |
| 73 | } |
Robert Jarzmik | 43c6342 | 2009-08-08 23:07:20 +0200 | [diff] [blame] | 74 | EXPORT_SYMBOL(pxa_request_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | void pxa_free_dma (int dma_ch) |
| 77 | { |
| 78 | unsigned long flags; |
| 79 | |
| 80 | if (!dma_channels[dma_ch].name) { |
| 81 | printk (KERN_CRIT |
| 82 | "%s: trying to free channel %d which is already freed\n", |
Harvey Harrison | 8e86f42 | 2008-03-04 15:08:02 -0800 | [diff] [blame] | 83 | __func__, dma_ch); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
| 87 | local_irq_save(flags); |
| 88 | DCSR(dma_ch) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR; |
| 89 | dma_channels[dma_ch].name = NULL; |
| 90 | local_irq_restore(flags); |
| 91 | } |
Robert Jarzmik | 43c6342 | 2009-08-08 23:07:20 +0200 | [diff] [blame] | 92 | EXPORT_SYMBOL(pxa_free_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 94 | static irqreturn_t dma_irq_handler(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | int i, dint = DINT; |
Robert Jarzmik | d46f5e4 | 2009-08-08 23:07:21 +0200 | [diff] [blame^] | 97 | struct dma_channel *channel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Robert Jarzmik | d46f5e4 | 2009-08-08 23:07:21 +0200 | [diff] [blame^] | 99 | while (dint) { |
| 100 | i = __ffs(dint); |
| 101 | dint &= (dint - 1); |
| 102 | channel = &dma_channels[i]; |
| 103 | if (channel->name && channel->irq_handler) { |
| 104 | channel->irq_handler(i, channel->data); |
| 105 | } else { |
| 106 | /* |
| 107 | * IRQ for an unregistered DMA channel: |
| 108 | * let's clear the interrupts and disable it. |
| 109 | */ |
| 110 | printk (KERN_WARNING "spurious IRQ for DMA channel %d\n", i); |
| 111 | DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | return IRQ_HANDLED; |
| 115 | } |
| 116 | |
Eric Miao | fef1f99 | 2009-01-02 16:26:33 +0800 | [diff] [blame] | 117 | int __init pxa_init_dma(int irq, int num_ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 119 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 121 | dma_channels = kzalloc(sizeof(struct dma_channel) * num_ch, GFP_KERNEL); |
| 122 | if (dma_channels == NULL) |
| 123 | return -ENOMEM; |
| 124 | |
Eric Miao | 26a5522 | 2009-01-21 11:29:19 +0800 | [diff] [blame] | 125 | /* dma channel priorities on pxa2xx processors: |
| 126 | * ch 0 - 3, 16 - 19 <--> (0) DMA_PRIO_HIGH |
| 127 | * ch 4 - 7, 20 - 23 <--> (1) DMA_PRIO_MEDIUM |
| 128 | * ch 8 - 15, 24 - 31 <--> (2) DMA_PRIO_LOW |
| 129 | */ |
| 130 | for (i = 0; i < num_ch; i++) { |
| 131 | DCSR(i) = 0; |
| 132 | dma_channels[i].prio = min((i & 0xf) >> 2, DMA_PRIO_LOW); |
| 133 | } |
| 134 | |
Eric Miao | fef1f99 | 2009-01-02 16:26:33 +0800 | [diff] [blame] | 135 | ret = request_irq(irq, dma_irq_handler, IRQF_DISABLED, "DMA", NULL); |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 136 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | printk (KERN_CRIT "Wow! Can't register IRQ for DMA\n"); |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 138 | kfree(dma_channels); |
| 139 | return ret; |
| 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Eric Miao | f53f066 | 2007-06-22 05:40:17 +0100 | [diff] [blame] | 142 | num_dma_channels = num_ch; |
| 143 | return 0; |
| 144 | } |