dma.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <linux/io.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;
  84. dint = readl(DMAC_ITCSR);
  85. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  86. if (dint & DMAC_CHANNEL(i)) {
  87. struct dma_channel *channel = &dma_channels[i];
  88. /* Clear TC interrupt of channel i */
  89. writel(DMAC_CHANNEL(i), DMAC_ITCCR);
  90. writel(0, DMAC_ITCCR);
  91. if (channel->name && channel->irq_handler) {
  92. channel->irq_handler(i, channel->data);
  93. } else {
  94. /*
  95. * IRQ for an unregistered DMA channel:
  96. * let's clear the interrupts and disable it.
  97. */
  98. printk(KERN_WARNING "spurious IRQ for"
  99. " DMA channel %d\n", i);
  100. }
  101. }
  102. }
  103. return IRQ_HANDLED;
  104. }
  105. static irqreturn_t dma_err_handler(int irq, void *dev_id)
  106. {
  107. int i, dint;
  108. dint = readl(DMAC_IESR);
  109. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  110. if (dint & DMAC_CHANNEL(i)) {
  111. struct dma_channel *channel = &dma_channels[i];
  112. /* Clear Err interrupt of channel i */
  113. writel(DMAC_CHANNEL(i), DMAC_IECR);
  114. writel(0, DMAC_IECR);
  115. if (channel->name && channel->err_handler) {
  116. channel->err_handler(i, channel->data);
  117. } else {
  118. /*
  119. * IRQ for an unregistered DMA channel:
  120. * let's clear the interrupts and disable it.
  121. */
  122. printk(KERN_WARNING "spurious IRQ for"
  123. " DMA channel %d\n", i);
  124. }
  125. }
  126. }
  127. return IRQ_HANDLED;
  128. }
  129. int __init puv3_init_dma(void)
  130. {
  131. int i, ret;
  132. /* dma channel priorities on v8 processors:
  133. * ch 0 - 1 <--> (0) DMA_PRIO_HIGH
  134. * ch 2 - 3 <--> (1) DMA_PRIO_MEDIUM
  135. * ch 4 - 5 <--> (2) DMA_PRIO_LOW
  136. */
  137. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  138. puv3_stop_dma(i);
  139. dma_channels[i].name = NULL;
  140. dma_channels[i].prio = min((i & 0x7) >> 1, DMA_PRIO_LOW);
  141. }
  142. ret = request_irq(IRQ_DMA, dma_irq_handler, 0, "DMA", NULL);
  143. if (ret) {
  144. printk(KERN_CRIT "Can't register IRQ for DMA\n");
  145. return ret;
  146. }
  147. ret = request_irq(IRQ_DMAERR, dma_err_handler, 0, "DMAERR", NULL);
  148. if (ret) {
  149. printk(KERN_CRIT "Can't register IRQ for DMAERR\n");
  150. free_irq(IRQ_DMA, "DMA");
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. postcore_initcall(puv3_init_dma);