dma-g2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/sh/drivers/dma/dma-g2.c
  4. *
  5. * G2 bus DMA support
  6. *
  7. * Copyright (C) 2003 - 2006 Paul Mundt
  8. */
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include <asm/cacheflush.h>
  14. #include <mach/sysasic.h>
  15. #include <mach/dma.h>
  16. #include <asm/dma.h>
  17. struct g2_channel {
  18. unsigned long g2_addr; /* G2 bus address */
  19. unsigned long root_addr; /* Root bus (SH-4) address */
  20. unsigned long size; /* Size (in bytes), 32-byte aligned */
  21. unsigned long direction; /* Transfer direction */
  22. unsigned long ctrl; /* Transfer control */
  23. unsigned long chan_enable; /* Channel enable */
  24. unsigned long xfer_enable; /* Transfer enable */
  25. unsigned long xfer_stat; /* Transfer status */
  26. } __attribute__ ((aligned(32)));
  27. struct g2_status {
  28. unsigned long g2_addr;
  29. unsigned long root_addr;
  30. unsigned long size;
  31. unsigned long status;
  32. } __attribute__ ((aligned(16)));
  33. struct g2_dma_info {
  34. struct g2_channel channel[G2_NR_DMA_CHANNELS];
  35. unsigned long pad1[G2_NR_DMA_CHANNELS];
  36. unsigned long wait_state;
  37. unsigned long pad2[10];
  38. unsigned long magic;
  39. struct g2_status status[G2_NR_DMA_CHANNELS];
  40. } __attribute__ ((aligned(256)));
  41. static volatile struct g2_dma_info *g2_dma = (volatile struct g2_dma_info *)0xa05f7800;
  42. #define g2_bytes_remaining(i) \
  43. ((g2_dma->channel[i].size - \
  44. g2_dma->status[i].size) & 0x0fffffff)
  45. static irqreturn_t g2_dma_interrupt(int irq, void *dev_id)
  46. {
  47. int i;
  48. for (i = 0; i < G2_NR_DMA_CHANNELS; i++) {
  49. if (g2_dma->status[i].status & 0x20000000) {
  50. unsigned int bytes = g2_bytes_remaining(i);
  51. if (likely(bytes == 0)) {
  52. struct dma_info *info = dev_id;
  53. struct dma_channel *chan = info->channels + i;
  54. wake_up(&chan->wait_queue);
  55. return IRQ_HANDLED;
  56. }
  57. }
  58. }
  59. return IRQ_NONE;
  60. }
  61. static int g2_enable_dma(struct dma_channel *chan)
  62. {
  63. unsigned int chan_nr = chan->chan;
  64. g2_dma->channel[chan_nr].chan_enable = 1;
  65. g2_dma->channel[chan_nr].xfer_enable = 1;
  66. return 0;
  67. }
  68. static int g2_disable_dma(struct dma_channel *chan)
  69. {
  70. unsigned int chan_nr = chan->chan;
  71. g2_dma->channel[chan_nr].chan_enable = 0;
  72. g2_dma->channel[chan_nr].xfer_enable = 0;
  73. return 0;
  74. }
  75. static int g2_xfer_dma(struct dma_channel *chan)
  76. {
  77. unsigned int chan_nr = chan->chan;
  78. if (chan->sar & 31) {
  79. printk("g2dma: unaligned source 0x%lx\n", chan->sar);
  80. return -EINVAL;
  81. }
  82. if (chan->dar & 31) {
  83. printk("g2dma: unaligned dest 0x%lx\n", chan->dar);
  84. return -EINVAL;
  85. }
  86. /* Align the count */
  87. if (chan->count & 31)
  88. chan->count = (chan->count + (32 - 1)) & ~(32 - 1);
  89. /* Fixup destination */
  90. chan->dar += 0xa0800000;
  91. /* Fixup direction */
  92. chan->mode = !chan->mode;
  93. flush_icache_range((unsigned long)chan->sar, chan->count);
  94. g2_disable_dma(chan);
  95. g2_dma->channel[chan_nr].g2_addr = chan->dar & 0x1fffffe0;
  96. g2_dma->channel[chan_nr].root_addr = chan->sar & 0x1fffffe0;
  97. g2_dma->channel[chan_nr].size = (chan->count & ~31) | 0x80000000;
  98. g2_dma->channel[chan_nr].direction = chan->mode;
  99. /*
  100. * bit 0 - ???
  101. * bit 1 - if set, generate a hardware event on transfer completion
  102. * bit 2 - ??? something to do with suspend?
  103. */
  104. g2_dma->channel[chan_nr].ctrl = 5; /* ?? */
  105. g2_enable_dma(chan);
  106. /* debug cruft */
  107. pr_debug("count, sar, dar, mode, ctrl, chan, xfer: %ld, 0x%08lx, "
  108. "0x%08lx, %ld, %ld, %ld, %ld\n",
  109. g2_dma->channel[chan_nr].size,
  110. g2_dma->channel[chan_nr].root_addr,
  111. g2_dma->channel[chan_nr].g2_addr,
  112. g2_dma->channel[chan_nr].direction,
  113. g2_dma->channel[chan_nr].ctrl,
  114. g2_dma->channel[chan_nr].chan_enable,
  115. g2_dma->channel[chan_nr].xfer_enable);
  116. return 0;
  117. }
  118. static int g2_get_residue(struct dma_channel *chan)
  119. {
  120. return g2_bytes_remaining(chan->chan);
  121. }
  122. static struct dma_ops g2_dma_ops = {
  123. .xfer = g2_xfer_dma,
  124. .get_residue = g2_get_residue,
  125. };
  126. static struct dma_info g2_dma_info = {
  127. .name = "g2_dmac",
  128. .nr_channels = 4,
  129. .ops = &g2_dma_ops,
  130. .flags = DMAC_CHANNELS_TEI_CAPABLE,
  131. };
  132. static int __init g2_dma_init(void)
  133. {
  134. int ret;
  135. ret = request_irq(HW_EVENT_G2_DMA, g2_dma_interrupt, 0,
  136. "g2 DMA handler", &g2_dma_info);
  137. if (unlikely(ret))
  138. return -EINVAL;
  139. /* Magic */
  140. g2_dma->wait_state = 27;
  141. g2_dma->magic = 0x4659404f;
  142. ret = register_dmac(&g2_dma_info);
  143. if (unlikely(ret != 0))
  144. free_irq(HW_EVENT_G2_DMA, &g2_dma_info);
  145. return ret;
  146. }
  147. static void __exit g2_dma_exit(void)
  148. {
  149. free_irq(HW_EVENT_G2_DMA, &g2_dma_info);
  150. unregister_dmac(&g2_dma_info);
  151. }
  152. subsys_initcall(g2_dma_init);
  153. module_exit(g2_dma_exit);
  154. MODULE_AUTHOR("Paul Mundt <[email protected]>");
  155. MODULE_DESCRIPTION("G2 bus DMA driver");
  156. MODULE_LICENSE("GPL v2");