dma.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * linux/arch/arm/mach-pxa/dma.c
  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. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/errno.h>
  19. #include <asm/system.h>
  20. #include <asm/irq.h>
  21. #include <asm/hardware.h>
  22. #include <asm/dma.h>
  23. #include <asm/arch/pxa-regs.h>
  24. static struct dma_channel {
  25. char *name;
  26. void (*irq_handler)(int, void *, struct pt_regs *);
  27. void *data;
  28. } dma_channels[PXA_DMA_CHANNELS];
  29. int pxa_request_dma (char *name, pxa_dma_prio prio,
  30. void (*irq_handler)(int, void *, struct pt_regs *),
  31. void *data)
  32. {
  33. unsigned long flags;
  34. int i, found = 0;
  35. /* basic sanity checks */
  36. if (!name || !irq_handler)
  37. return -EINVAL;
  38. local_irq_save(flags);
  39. /* try grabbing a DMA channel with the requested priority */
  40. for (i = prio; i < prio + PXA_DMA_NBCH(prio); i++) {
  41. if (!dma_channels[i].name) {
  42. found = 1;
  43. break;
  44. }
  45. }
  46. if (!found) {
  47. /* requested prio group is full, try hier priorities */
  48. for (i = prio-1; i >= 0; i--) {
  49. if (!dma_channels[i].name) {
  50. found = 1;
  51. break;
  52. }
  53. }
  54. }
  55. if (found) {
  56. DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  57. dma_channels[i].name = name;
  58. dma_channels[i].irq_handler = irq_handler;
  59. dma_channels[i].data = data;
  60. } else {
  61. printk (KERN_WARNING "No more available DMA channels for %s\n", name);
  62. i = -ENODEV;
  63. }
  64. local_irq_restore(flags);
  65. return i;
  66. }
  67. void pxa_free_dma (int dma_ch)
  68. {
  69. unsigned long flags;
  70. if (!dma_channels[dma_ch].name) {
  71. printk (KERN_CRIT
  72. "%s: trying to free channel %d which is already freed\n",
  73. __FUNCTION__, dma_ch);
  74. return;
  75. }
  76. local_irq_save(flags);
  77. DCSR(dma_ch) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  78. dma_channels[dma_ch].name = NULL;
  79. local_irq_restore(flags);
  80. }
  81. static irqreturn_t dma_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
  82. {
  83. int i, dint = DINT;
  84. for (i = 0; i < PXA_DMA_CHANNELS; i++) {
  85. if (dint & (1 << i)) {
  86. struct dma_channel *channel = &dma_channels[i];
  87. if (channel->name && channel->irq_handler) {
  88. channel->irq_handler(i, channel->data, regs);
  89. } else {
  90. /*
  91. * IRQ for an unregistered DMA channel:
  92. * let's clear the interrupts and disable it.
  93. */
  94. printk (KERN_WARNING "spurious IRQ for DMA channel %d\n", i);
  95. DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  96. }
  97. }
  98. }
  99. return IRQ_HANDLED;
  100. }
  101. static int __init pxa_dma_init (void)
  102. {
  103. int ret;
  104. ret = request_irq (IRQ_DMA, dma_irq_handler, 0, "DMA", NULL);
  105. if (ret)
  106. printk (KERN_CRIT "Wow! Can't register IRQ for DMA\n");
  107. return ret;
  108. }
  109. arch_initcall(pxa_dma_init);
  110. EXPORT_SYMBOL(pxa_request_dma);
  111. EXPORT_SYMBOL(pxa_free_dma);