dma.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * linux/arch/unicore32/kernel/dma.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Maintained by GUAN Xue-tao <gxt@mprc.pku.edu.cn>
  7. * Copyright (C) 2001-2010 Guan Xuetao
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/errno.h>
  18. #include <asm/system.h>
  19. #include <asm/irq.h>
  20. #include <mach/hardware.h>
  21. #include <mach/dma.h>
  22. struct dma_channel {
  23. char *name;
  24. puv3_dma_prio prio;
  25. void (*irq_handler)(int, void *);
  26. void (*err_handler)(int, void *);
  27. void *data;
  28. };
  29. static struct dma_channel dma_channels[MAX_DMA_CHANNELS];
  30. int puv3_request_dma(char *name, puv3_dma_prio prio,
  31. void (*irq_handler)(int, void *),
  32. void (*err_handler)(int, void *),
  33. void *data)
  34. {
  35. unsigned long flags;
  36. int i, found = 0;
  37. /* basic sanity checks */
  38. if (!name)
  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 < MAX_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. dma_channels[i].name = name;
  54. dma_channels[i].irq_handler = irq_handler;
  55. dma_channels[i].err_handler = err_handler;
  56. dma_channels[i].data = data;
  57. } else {
  58. printk(KERN_WARNING "No more available DMA channels for %s\n",
  59. name);
  60. i = -ENODEV;
  61. }
  62. local_irq_restore(flags);
  63. return i;
  64. }
  65. EXPORT_SYMBOL(puv3_request_dma);
  66. void puv3_free_dma(int dma_ch)
  67. {
  68. unsigned long flags;
  69. if (!dma_channels[dma_ch].name) {
  70. printk(KERN_CRIT
  71. "%s: trying to free channel %d which is already freed\n",
  72. __func__, dma_ch);
  73. return;
  74. }
  75. local_irq_save(flags);
  76. dma_channels[dma_ch].name = NULL;
  77. dma_channels[dma_ch].err_handler = NULL;
  78. local_irq_restore(flags);
  79. }
  80. EXPORT_SYMBOL(puv3_free_dma);
  81. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  82. {
  83. int i, dint = DMAC_ITCSR;
  84. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  85. if (dint & DMAC_CHANNEL(i)) {
  86. struct dma_channel *channel = &dma_channels[i];
  87. /* Clear TC interrupt of channel i */
  88. DMAC_ITCCR = DMAC_CHANNEL(i);
  89. DMAC_ITCCR = 0;
  90. if (channel->name && channel->irq_handler) {
  91. channel->irq_handler(i, channel->data);
  92. } else {
  93. /*
  94. * IRQ for an unregistered DMA channel:
  95. * let's clear the interrupts and disable it.
  96. */
  97. printk(KERN_WARNING "spurious IRQ for"
  98. " DMA channel %d\n", i);
  99. }
  100. }
  101. }
  102. return IRQ_HANDLED;
  103. }
  104. static irqreturn_t dma_err_handler(int irq, void *dev_id)
  105. {
  106. int i, dint = DMAC_IESR;
  107. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  108. if (dint & DMAC_CHANNEL(i)) {
  109. struct dma_channel *channel = &dma_channels[i];
  110. /* Clear Err interrupt of channel i */
  111. DMAC_IECR = DMAC_CHANNEL(i);
  112. DMAC_IECR = 0;
  113. if (channel->name && channel->err_handler) {
  114. channel->err_handler(i, channel->data);
  115. } else {
  116. /*
  117. * IRQ for an unregistered DMA channel:
  118. * let's clear the interrupts and disable it.
  119. */
  120. printk(KERN_WARNING "spurious IRQ for"
  121. " DMA channel %d\n", i);
  122. }
  123. }
  124. }
  125. return IRQ_HANDLED;
  126. }
  127. int __init puv3_init_dma(void)
  128. {
  129. int i, ret;
  130. /* dma channel priorities on v8 processors:
  131. * ch 0 - 1 <--> (0) DMA_PRIO_HIGH
  132. * ch 2 - 3 <--> (1) DMA_PRIO_MEDIUM
  133. * ch 4 - 5 <--> (2) DMA_PRIO_LOW
  134. */
  135. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  136. puv3_stop_dma(i);
  137. dma_channels[i].name = NULL;
  138. dma_channels[i].prio = min((i & 0x7) >> 1, DMA_PRIO_LOW);
  139. }
  140. ret = request_irq(IRQ_DMA, dma_irq_handler, 0, "DMA", NULL);
  141. if (ret) {
  142. printk(KERN_CRIT "Can't register IRQ for DMA\n");
  143. return ret;
  144. }
  145. ret = request_irq(IRQ_DMAERR, dma_err_handler, 0, "DMAERR", NULL);
  146. if (ret) {
  147. printk(KERN_CRIT "Can't register IRQ for DMAERR\n");
  148. free_irq(IRQ_DMA, "DMA");
  149. return ret;
  150. }
  151. return 0;
  152. }
  153. postcore_initcall(puv3_init_dma);