dma.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * linux/arch/arm/plat-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 <mach/hardware.h>
  22. #include <mach/dma.h>
  23. struct dma_channel {
  24. char *name;
  25. pxa_dma_prio prio;
  26. void (*irq_handler)(int, void *);
  27. void *data;
  28. };
  29. static struct dma_channel *dma_channels;
  30. static int num_dma_channels;
  31. int pxa_request_dma (char *name, pxa_dma_prio prio,
  32. void (*irq_handler)(int, void *),
  33. 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. do {
  42. /* try grabbing a DMA channel with the requested priority */
  43. for (i = 0; i < num_dma_channels; i++) {
  44. if ((dma_channels[i].prio == prio) &&
  45. !dma_channels[i].name) {
  46. found = 1;
  47. break;
  48. }
  49. }
  50. /* if requested prio group is full, try a hier priority */
  51. } while (!found && prio--);
  52. if (found) {
  53. DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  54. dma_channels[i].name = name;
  55. dma_channels[i].irq_handler = irq_handler;
  56. dma_channels[i].data = data;
  57. } else {
  58. printk (KERN_WARNING "No more available DMA channels for %s\n", name);
  59. i = -ENODEV;
  60. }
  61. local_irq_restore(flags);
  62. return i;
  63. }
  64. void pxa_free_dma (int dma_ch)
  65. {
  66. unsigned long flags;
  67. if (!dma_channels[dma_ch].name) {
  68. printk (KERN_CRIT
  69. "%s: trying to free channel %d which is already freed\n",
  70. __func__, dma_ch);
  71. return;
  72. }
  73. local_irq_save(flags);
  74. DCSR(dma_ch) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  75. dma_channels[dma_ch].name = NULL;
  76. local_irq_restore(flags);
  77. }
  78. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  79. {
  80. int i, dint = DINT;
  81. for (i = 0; i < num_dma_channels; i++) {
  82. if (dint & (1 << i)) {
  83. struct dma_channel *channel = &dma_channels[i];
  84. if (channel->name && channel->irq_handler) {
  85. channel->irq_handler(i, channel->data);
  86. } else {
  87. /*
  88. * IRQ for an unregistered DMA channel:
  89. * let's clear the interrupts and disable it.
  90. */
  91. printk (KERN_WARNING "spurious IRQ for DMA channel %d\n", i);
  92. DCSR(i) = DCSR_STARTINTR|DCSR_ENDINTR|DCSR_BUSERR;
  93. }
  94. }
  95. }
  96. return IRQ_HANDLED;
  97. }
  98. int __init pxa_init_dma(int irq, int num_ch)
  99. {
  100. int i, ret;
  101. dma_channels = kzalloc(sizeof(struct dma_channel) * num_ch, GFP_KERNEL);
  102. if (dma_channels == NULL)
  103. return -ENOMEM;
  104. /* dma channel priorities on pxa2xx processors:
  105. * ch 0 - 3, 16 - 19 <--> (0) DMA_PRIO_HIGH
  106. * ch 4 - 7, 20 - 23 <--> (1) DMA_PRIO_MEDIUM
  107. * ch 8 - 15, 24 - 31 <--> (2) DMA_PRIO_LOW
  108. */
  109. for (i = 0; i < num_ch; i++) {
  110. DCSR(i) = 0;
  111. dma_channels[i].prio = min((i & 0xf) >> 2, DMA_PRIO_LOW);
  112. }
  113. ret = request_irq(irq, dma_irq_handler, IRQF_DISABLED, "DMA", NULL);
  114. if (ret) {
  115. printk (KERN_CRIT "Wow! Can't register IRQ for DMA\n");
  116. kfree(dma_channels);
  117. return ret;
  118. }
  119. num_dma_channels = num_ch;
  120. return 0;
  121. }
  122. EXPORT_SYMBOL(pxa_request_dma);
  123. EXPORT_SYMBOL(pxa_free_dma);