dmabrg.c 5.2 KB

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