dma.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * linux/arch/arm/mach-imx/dma.c
  3. *
  4. * imx DMA registration and IRQ dispatching
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * 03/03/2004 Sascha Hauer <sascha@saschahauer.de>
  11. * initial version heavily inspired by
  12. * linux/arch/arm/mach-pxa/dma.c
  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. static struct dma_channel {
  24. char *name;
  25. void (*irq_handler) (int, void *, struct pt_regs *);
  26. void (*err_handler) (int, void *, struct pt_regs *);
  27. void *data;
  28. } dma_channels[11];
  29. /* set err_handler to NULL to have the standard info-only error handler */
  30. int
  31. imx_request_dma(char *name, imx_dma_prio prio,
  32. void (*irq_handler) (int, void *, struct pt_regs *),
  33. void (*err_handler) (int, void *, struct pt_regs *), void *data)
  34. {
  35. unsigned long flags;
  36. int i, found = 0;
  37. /* basic sanity checks */
  38. if (!name || !irq_handler)
  39. return -EINVAL;
  40. local_irq_save(flags);
  41. /* try grabbing a DMA channel with the requested priority */
  42. for (i = prio; i < prio + (prio == DMA_PRIO_LOW) ? 8 : 4; i++) {
  43. if (!dma_channels[i].name) {
  44. found = 1;
  45. break;
  46. }
  47. }
  48. if (!found) {
  49. /* requested prio group is full, try hier priorities */
  50. for (i = prio - 1; i >= 0; i--) {
  51. if (!dma_channels[i].name) {
  52. found = 1;
  53. break;
  54. }
  55. }
  56. }
  57. if (found) {
  58. DIMR &= ~(1 << i);
  59. dma_channels[i].name = name;
  60. dma_channels[i].irq_handler = irq_handler;
  61. dma_channels[i].err_handler = err_handler;
  62. dma_channels[i].data = data;
  63. } else {
  64. printk(KERN_WARNING "No more available DMA channels for %s\n",
  65. name);
  66. i = -ENODEV;
  67. }
  68. local_irq_restore(flags);
  69. return i;
  70. }
  71. void
  72. imx_free_dma(int dma_ch)
  73. {
  74. unsigned long flags;
  75. if (!dma_channels[dma_ch].name) {
  76. printk(KERN_CRIT
  77. "%s: trying to free channel %d which is already freed\n",
  78. __FUNCTION__, dma_ch);
  79. return;
  80. }
  81. local_irq_save(flags);
  82. DIMR &= ~(1 << dma_ch);
  83. dma_channels[dma_ch].name = NULL;
  84. local_irq_restore(flags);
  85. }
  86. static irqreturn_t
  87. dma_err_handler(int irq, void *dev_id, struct pt_regs *regs)
  88. {
  89. int i, disr = DISR;
  90. struct dma_channel *channel;
  91. unsigned int err_mask = DBTOSR | DRTOSR | DSESR | DBOSR;
  92. DISR = disr;
  93. for (i = 0; i < 11; i++) {
  94. channel = &dma_channels[i];
  95. if ( (err_mask & 1<<i) && channel->name && channel->err_handler) {
  96. channel->err_handler(i, channel->data, regs);
  97. continue;
  98. }
  99. if (DBTOSR & (1 << i)) {
  100. printk(KERN_WARNING
  101. "Burst timeout on channel %d (%s)\n",
  102. i, channel->name);
  103. DBTOSR |= (1 << i);
  104. }
  105. if (DRTOSR & (1 << i)) {
  106. printk(KERN_WARNING
  107. "Request timeout on channel %d (%s)\n",
  108. i, channel->name);
  109. DRTOSR |= (1 << i);
  110. }
  111. if (DSESR & (1 << i)) {
  112. printk(KERN_WARNING
  113. "Transfer timeout on channel %d (%s)\n",
  114. i, channel->name);
  115. DSESR |= (1 << i);
  116. }
  117. if (DBOSR & (1 << i)) {
  118. printk(KERN_WARNING
  119. "Buffer overflow timeout on channel %d (%s)\n",
  120. i, channel->name);
  121. DBOSR |= (1 << i);
  122. }
  123. }
  124. return IRQ_HANDLED;
  125. }
  126. static irqreturn_t
  127. dma_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
  128. {
  129. int i, disr = DISR;
  130. DISR = disr;
  131. for (i = 0; i < 11; i++) {
  132. if (disr & (1 << i)) {
  133. struct dma_channel *channel = &dma_channels[i];
  134. if (channel->name && channel->irq_handler) {
  135. channel->irq_handler(i, channel->data, regs);
  136. } else {
  137. /*
  138. * IRQ for an unregistered DMA channel:
  139. * let's clear the interrupts and disable it.
  140. */
  141. printk(KERN_WARNING
  142. "spurious IRQ for DMA channel %d\n", i);
  143. }
  144. }
  145. }
  146. return IRQ_HANDLED;
  147. }
  148. static int __init
  149. imx_dma_init(void)
  150. {
  151. int ret;
  152. /* reset DMA module */
  153. DCR = DCR_DRST;
  154. ret = request_irq(DMA_INT, dma_irq_handler, 0, "DMA", NULL);
  155. if (ret) {
  156. printk(KERN_CRIT "Wow! Can't register IRQ for DMA\n");
  157. return ret;
  158. }
  159. ret = request_irq(DMA_ERR, dma_err_handler, 0, "DMA", NULL);
  160. if (ret) {
  161. printk(KERN_CRIT "Wow! Can't register ERRIRQ for DMA\n");
  162. free_irq(DMA_INT, NULL);
  163. }
  164. /* enable DMA module */
  165. DCR = DCR_DEN;
  166. /* clear all interrupts */
  167. DISR = 0x3ff;
  168. /* enable interrupts */
  169. DIMR = 0;
  170. return ret;
  171. }
  172. arch_initcall(imx_dma_init);
  173. EXPORT_SYMBOL(imx_request_dma);
  174. EXPORT_SYMBOL(imx_free_dma);