dma.c 3.3 KB

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