dmabrg.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * SH7760 DMABRG IRQ handling
  3. *
  4. * (c) 2007 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
  5. * licensed under the GPLv2.
  6. *
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <asm/dma.h>
  12. #include <asm/dmabrg.h>
  13. #include <asm/io.h>
  14. /*
  15. * The DMABRG is a special DMA unit within the SH7760. It does transfers
  16. * from USB-SRAM/Audio units to main memory (and also the LCDC; but that
  17. * part is sensibly placed in the LCDC registers and requires no irqs)
  18. * It has 3 IRQ lines which trigger 10 events, and works independently
  19. * from the traditional SH DMAC (although it blocks usage of DMAC 0)
  20. *
  21. * BRGIRQID | component | dir | meaning | source
  22. * -----------------------------------------------------
  23. * 0 | USB-DMA | ... | xfer done | DMABRGI1
  24. * 1 | USB-UAE | ... | USB addr err.| DMABRGI0
  25. * 2 | HAC0/SSI0 | play| all done | DMABRGI1
  26. * 3 | HAC0/SSI0 | play| half done | DMABRGI2
  27. * 4 | HAC0/SSI0 | rec | all done | DMABRGI1
  28. * 5 | HAC0/SSI0 | rec | half done | DMABRGI2
  29. * 6 | HAC1/SSI1 | play| all done | DMABRGI1
  30. * 7 | HAC1/SSI1 | play| half done | DMABRGI2
  31. * 8 | HAC1/SSI1 | rec | all done | DMABRGI1
  32. * 9 | HAC1/SSI1 | rec | half done | DMABRGI2
  33. *
  34. * all can be enabled/disabled in the DMABRGCR register,
  35. * as well as checked if they occurred.
  36. *
  37. * DMABRGI0 services USB DMA Address errors, but it still must be
  38. * enabled/acked in the DMABRGCR register. USB-DMA complete indicator
  39. * is grouped together with the audio buffer end indicators, too bad...
  40. *
  41. * DMABRGCR: Bits 31-24: audio-dma ENABLE flags,
  42. * Bits 23-16: audio-dma STATUS flags,
  43. * Bits 9-8: USB error/xfer ENABLE,
  44. * Bits 1-0: USB error/xfer STATUS.
  45. * Ack an IRQ by writing 0 to the STATUS flag.
  46. * Mask IRQ by writing 0 to ENABLE flag.
  47. *
  48. * Usage is almost like with any other IRQ:
  49. * dmabrg_request_irq(BRGIRQID, handler, data)
  50. * dmabrg_free_irq(BRGIRQID)
  51. *
  52. * handler prototype: void brgirqhandler(void *data)
  53. */
  54. #define DMARSRA 0xfe090000
  55. #define DMAOR 0xffa00040
  56. #define DMACHCR0 0xffa0000c
  57. #define DMABRGCR 0xfe3c0000
  58. #define DMAOR_BRG 0x0000c000
  59. #define DMAOR_DMEN 0x00000001
  60. #define DMABRGI0 68
  61. #define DMABRGI1 69
  62. #define DMABRGI2 70
  63. struct dmabrg_handler {
  64. void (*handler)(void *);
  65. void *data;
  66. } *dmabrg_handlers;
  67. static inline void dmabrg_call_handler(int i)
  68. {
  69. dmabrg_handlers[i].handler(dmabrg_handlers[i].data);
  70. }
  71. /*
  72. * main DMABRG irq handler. It acks irqs and then
  73. * handles every set and unmasked bit sequentially.
  74. * No locking and no validity checks; it should be
  75. * as fast as possible (audio!)
  76. */
  77. static irqreturn_t dmabrg_irq(int irq, void *data)
  78. {
  79. unsigned long dcr;
  80. unsigned int i;
  81. dcr = __raw_readl(DMABRGCR);
  82. __raw_writel(dcr & ~0x00ff0003, DMABRGCR); /* ack all */
  83. dcr &= dcr >> 8; /* ignore masked */
  84. /* USB stuff, get it out of the way first */
  85. if (dcr & 1)
  86. dmabrg_call_handler(DMABRGIRQ_USBDMA);
  87. if (dcr & 2)
  88. dmabrg_call_handler(DMABRGIRQ_USBDMAERR);
  89. /* Audio */
  90. dcr >>= 16;
  91. while (dcr) {
  92. i = __ffs(dcr);
  93. dcr &= dcr - 1;
  94. dmabrg_call_handler(i + DMABRGIRQ_A0TXF);
  95. }
  96. return IRQ_HANDLED;
  97. }
  98. static void dmabrg_disable_irq(unsigned int dmairq)
  99. {
  100. unsigned long dcr;
  101. dcr = __raw_readl(DMABRGCR);
  102. dcr &= ~(1 << ((dmairq > 1) ? dmairq + 22 : dmairq + 8));
  103. __raw_writel(dcr, DMABRGCR);
  104. }
  105. static void dmabrg_enable_irq(unsigned int dmairq)
  106. {
  107. unsigned long dcr;
  108. dcr = __raw_readl(DMABRGCR);
  109. dcr |= (1 << ((dmairq > 1) ? dmairq + 22 : dmairq + 8));
  110. __raw_writel(dcr, DMABRGCR);
  111. }
  112. int dmabrg_request_irq(unsigned int dmairq, void(*handler)(void*),
  113. void *data)
  114. {
  115. if ((dmairq > 9) || !handler)
  116. return -ENOENT;
  117. if (dmabrg_handlers[dmairq].handler)
  118. return -EBUSY;
  119. dmabrg_handlers[dmairq].handler = handler;
  120. dmabrg_handlers[dmairq].data = data;
  121. dmabrg_enable_irq(dmairq);
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(dmabrg_request_irq);
  125. void dmabrg_free_irq(unsigned int dmairq)
  126. {
  127. if (likely(dmairq < 10)) {
  128. dmabrg_disable_irq(dmairq);
  129. dmabrg_handlers[dmairq].handler = NULL;
  130. dmabrg_handlers[dmairq].data = NULL;
  131. }
  132. }
  133. EXPORT_SYMBOL_GPL(dmabrg_free_irq);
  134. static int __init dmabrg_init(void)
  135. {
  136. unsigned long or;
  137. int ret;
  138. dmabrg_handlers = kzalloc(10 * sizeof(struct dmabrg_handler),
  139. GFP_KERNEL);
  140. if (!dmabrg_handlers)
  141. return -ENOMEM;
  142. #ifdef CONFIG_SH_DMA
  143. /* request DMAC channel 0 before anyone else can get it */
  144. ret = request_dma(0, "DMAC 0 (DMABRG)");
  145. if (ret < 0)
  146. printk(KERN_INFO "DMABRG: DMAC ch0 not reserved!\n");
  147. #endif
  148. __raw_writel(0, DMABRGCR);
  149. __raw_writel(0, DMACHCR0);
  150. __raw_writel(0x94000000, DMARSRA); /* enable DMABRG in DMAC 0 */
  151. /* enable DMABRG mode, enable the DMAC */
  152. or = __raw_readl(DMAOR);
  153. __raw_writel(or | DMAOR_BRG | DMAOR_DMEN, DMAOR);
  154. ret = request_irq(DMABRGI0, dmabrg_irq, 0,
  155. "DMABRG USB address error", NULL);
  156. if (ret)
  157. goto out0;
  158. ret = request_irq(DMABRGI1, dmabrg_irq, 0,
  159. "DMABRG Transfer End", NULL);
  160. if (ret)
  161. goto out1;
  162. ret = request_irq(DMABRGI2, dmabrg_irq, 0,
  163. "DMABRG Transfer Half", NULL);
  164. if (ret == 0)
  165. return ret;
  166. free_irq(DMABRGI1, NULL);
  167. out1: free_irq(DMABRGI0, NULL);
  168. out0: kfree(dmabrg_handlers);
  169. return ret;
  170. }
  171. subsys_initcall(dmabrg_init);