dma.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/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. puv3_dma_prio prio;
  26. void (*irq_handler)(int, void *);
  27. void (*err_handler)(int, void *);
  28. void *data;
  29. };
  30. static struct dma_channel dma_channels[MAX_DMA_CHANNELS];
  31. int puv3_request_dma(char *name, puv3_dma_prio prio,
  32. void (*irq_handler)(int, void *),
  33. void (*err_handler)(int, void *),
  34. void *data)
  35. {
  36. unsigned long flags;
  37. int i, found = 0;
  38. /* basic sanity checks */
  39. if (!name)
  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 < MAX_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. dma_channels[i].name = name;
  55. dma_channels[i].irq_handler = irq_handler;
  56. dma_channels[i].err_handler = err_handler;
  57. dma_channels[i].data = data;
  58. } else {
  59. printk(KERN_WARNING "No more available DMA channels for %s\n",
  60. name);
  61. i = -ENODEV;
  62. }
  63. local_irq_restore(flags);
  64. return i;
  65. }
  66. EXPORT_SYMBOL(puv3_request_dma);
  67. void puv3_free_dma(int dma_ch)
  68. {
  69. unsigned long flags;
  70. if (!dma_channels[dma_ch].name) {
  71. printk(KERN_CRIT
  72. "%s: trying to free channel %d which is already freed\n",
  73. __func__, dma_ch);
  74. return;
  75. }
  76. local_irq_save(flags);
  77. dma_channels[dma_ch].name = NULL;
  78. dma_channels[dma_ch].err_handler = NULL;
  79. local_irq_restore(flags);
  80. }
  81. EXPORT_SYMBOL(puv3_free_dma);
  82. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  83. {
  84. int i, dint;
  85. dint = readl(DMAC_ITCSR);
  86. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  87. if (dint & DMAC_CHANNEL(i)) {
  88. struct dma_channel *channel = &dma_channels[i];
  89. /* Clear TC interrupt of channel i */
  90. writel(DMAC_CHANNEL(i), DMAC_ITCCR);
  91. writel(0, DMAC_ITCCR);
  92. if (channel->name && channel->irq_handler) {
  93. channel->irq_handler(i, channel->data);
  94. } else {
  95. /*
  96. * IRQ for an unregistered DMA channel:
  97. * let's clear the interrupts and disable it.
  98. */
  99. printk(KERN_WARNING "spurious IRQ for"
  100. " DMA channel %d\n", i);
  101. }
  102. }
  103. }
  104. return IRQ_HANDLED;
  105. }
  106. static irqreturn_t dma_err_handler(int irq, void *dev_id)
  107. {
  108. int i, dint;
  109. dint = readl(DMAC_IESR);
  110. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  111. if (dint & DMAC_CHANNEL(i)) {
  112. struct dma_channel *channel = &dma_channels[i];
  113. /* Clear Err interrupt of channel i */
  114. writel(DMAC_CHANNEL(i), DMAC_IECR);
  115. writel(0, DMAC_IECR);
  116. if (channel->name && channel->err_handler) {
  117. channel->err_handler(i, channel->data);
  118. } else {
  119. /*
  120. * IRQ for an unregistered DMA channel:
  121. * let's clear the interrupts and disable it.
  122. */
  123. printk(KERN_WARNING "spurious IRQ for"
  124. " DMA channel %d\n", i);
  125. }
  126. }
  127. }
  128. return IRQ_HANDLED;
  129. }
  130. int __init puv3_init_dma(void)
  131. {
  132. int i, ret;
  133. /* dma channel priorities on v8 processors:
  134. * ch 0 - 1 <--> (0) DMA_PRIO_HIGH
  135. * ch 2 - 3 <--> (1) DMA_PRIO_MEDIUM
  136. * ch 4 - 5 <--> (2) DMA_PRIO_LOW
  137. */
  138. for (i = 0; i < MAX_DMA_CHANNELS; i++) {
  139. puv3_stop_dma(i);
  140. dma_channels[i].name = NULL;
  141. dma_channels[i].prio = min((i & 0x7) >> 1, DMA_PRIO_LOW);
  142. }
  143. ret = request_irq(IRQ_DMA, dma_irq_handler, 0, "DMA", NULL);
  144. if (ret) {
  145. printk(KERN_CRIT "Can't register IRQ for DMA\n");
  146. return ret;
  147. }
  148. ret = request_irq(IRQ_DMAERR, dma_err_handler, 0, "DMAERR", NULL);
  149. if (ret) {
  150. printk(KERN_CRIT "Can't register IRQ for DMAERR\n");
  151. free_irq(IRQ_DMA, "DMA");
  152. return ret;
  153. }
  154. return 0;
  155. }
  156. postcore_initcall(puv3_init_dma);