dma.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * arch/sh64/kernel/dma.c
  3. *
  4. * DMA routines for the SH-5 DMAC.
  5. *
  6. * Copyright (C) 2003 Paul Mundt
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/types.h>
  16. #include <linux/irq.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/mm.h>
  19. #include <asm/hardware.h>
  20. #include <asm/dma.h>
  21. #include <asm/signal.h>
  22. #include <asm/errno.h>
  23. #include <asm/io.h>
  24. typedef struct {
  25. unsigned long dev_addr;
  26. unsigned long mem_addr;
  27. unsigned int mode;
  28. unsigned int count;
  29. } dma_info_t;
  30. static dma_info_t dma_info[MAX_DMA_CHANNELS];
  31. static DEFINE_SPINLOCK(dma_spin_lock);
  32. /* arch/sh64/kernel/irq_intc.c */
  33. extern void make_intc_irq(unsigned int irq);
  34. /* DMAC Interrupts */
  35. #define DMA_IRQ_DMTE0 18
  36. #define DMA_IRQ_DERR 22
  37. #define DMAC_COMMON_BASE (dmac_base + 0x08)
  38. #define DMAC_SAR_BASE (dmac_base + 0x10)
  39. #define DMAC_DAR_BASE (dmac_base + 0x18)
  40. #define DMAC_COUNT_BASE (dmac_base + 0x20)
  41. #define DMAC_CTRL_BASE (dmac_base + 0x28)
  42. #define DMAC_STATUS_BASE (dmac_base + 0x30)
  43. #define DMAC_SAR(n) (DMAC_SAR_BASE + ((n) * 0x28))
  44. #define DMAC_DAR(n) (DMAC_DAR_BASE + ((n) * 0x28))
  45. #define DMAC_COUNT(n) (DMAC_COUNT_BASE + ((n) * 0x28))
  46. #define DMAC_CTRL(n) (DMAC_CTRL_BASE + ((n) * 0x28))
  47. #define DMAC_STATUS(n) (DMAC_STATUS_BASE + ((n) * 0x28))
  48. /* DMAC.COMMON Bit Definitions */
  49. #define DMAC_COMMON_PR 0x00000001 /* Priority */
  50. /* Bits 1-2 Reserved */
  51. #define DMAC_COMMON_ME 0x00000008 /* Master Enable */
  52. #define DMAC_COMMON_NMI 0x00000010 /* NMI Flag */
  53. /* Bits 5-6 Reserved */
  54. #define DMAC_COMMON_ER 0x00000780 /* Error Response */
  55. #define DMAC_COMMON_AAE 0x00007800 /* Address Alignment Error */
  56. /* Bits 15-63 Reserved */
  57. /* DMAC.SAR Bit Definitions */
  58. #define DMAC_SAR_ADDR 0xffffffff /* Source Address */
  59. /* DMAC.DAR Bit Definitions */
  60. #define DMAC_DAR_ADDR 0xffffffff /* Destination Address */
  61. /* DMAC.COUNT Bit Definitions */
  62. #define DMAC_COUNT_CNT 0xffffffff /* Transfer Count */
  63. /* DMAC.CTRL Bit Definitions */
  64. #define DMAC_CTRL_TS 0x00000007 /* Transfer Size */
  65. #define DMAC_CTRL_SI 0x00000018 /* Source Increment */
  66. #define DMAC_CTRL_DI 0x00000060 /* Destination Increment */
  67. #define DMAC_CTRL_RS 0x00000780 /* Resource Select */
  68. #define DMAC_CTRL_IE 0x00000800 /* Interrupt Enable */
  69. #define DMAC_CTRL_TE 0x00001000 /* Transfer Enable */
  70. /* Bits 15-63 Reserved */
  71. /* DMAC.STATUS Bit Definitions */
  72. #define DMAC_STATUS_TE 0x00000001 /* Transfer End */
  73. #define DMAC_STATUS_AAE 0x00000002 /* Address Alignment Error */
  74. /* Bits 2-63 Reserved */
  75. static unsigned long dmac_base;
  76. void set_dma_count(unsigned int chan, unsigned int count);
  77. void set_dma_addr(unsigned int chan, unsigned int addr);
  78. static irqreturn_t dma_mte(int irq, void *dev_id, struct pt_regs *regs)
  79. {
  80. unsigned int chan = irq - DMA_IRQ_DMTE0;
  81. dma_info_t *info = dma_info + chan;
  82. u64 status;
  83. if (info->mode & DMA_MODE_WRITE) {
  84. sh64_out64(info->mem_addr & DMAC_SAR_ADDR, DMAC_SAR(chan));
  85. } else {
  86. sh64_out64(info->mem_addr & DMAC_DAR_ADDR, DMAC_DAR(chan));
  87. }
  88. set_dma_count(chan, info->count);
  89. /* Clear the TE bit */
  90. status = sh64_in64(DMAC_STATUS(chan));
  91. status &= ~DMAC_STATUS_TE;
  92. sh64_out64(status, DMAC_STATUS(chan));
  93. return IRQ_HANDLED;
  94. }
  95. static struct irqaction irq_dmte = {
  96. .handler = dma_mte,
  97. .flags = SA_INTERRUPT,
  98. .name = "DMA MTE",
  99. };
  100. static irqreturn_t dma_err(int irq, void *dev_id, struct pt_regs *regs)
  101. {
  102. u64 tmp;
  103. u8 chan;
  104. printk(KERN_NOTICE "DMAC: Got a DMA Error!\n");
  105. tmp = sh64_in64(DMAC_COMMON_BASE);
  106. /* Check for the type of error */
  107. if ((chan = tmp & DMAC_COMMON_AAE)) {
  108. /* It's an address alignment error.. */
  109. printk(KERN_NOTICE "DMAC: Alignment error on channel %d, ", chan);
  110. printk(KERN_NOTICE "SAR: 0x%08llx, DAR: 0x%08llx, COUNT: %lld\n",
  111. (sh64_in64(DMAC_SAR(chan)) & DMAC_SAR_ADDR),
  112. (sh64_in64(DMAC_DAR(chan)) & DMAC_DAR_ADDR),
  113. (sh64_in64(DMAC_COUNT(chan)) & DMAC_COUNT_CNT));
  114. } else if ((chan = tmp & DMAC_COMMON_ER)) {
  115. /* Something else went wrong.. */
  116. printk(KERN_NOTICE "DMAC: Error on channel %d\n", chan);
  117. }
  118. /* Reset the ME bit to clear the interrupt */
  119. tmp |= DMAC_COMMON_ME;
  120. sh64_out64(tmp, DMAC_COMMON_BASE);
  121. return IRQ_HANDLED;
  122. }
  123. static struct irqaction irq_derr = {
  124. .handler = dma_err,
  125. .flags = SA_INTERRUPT,
  126. .name = "DMA Error",
  127. };
  128. static inline unsigned long calc_xmit_shift(unsigned int chan)
  129. {
  130. return sh64_in64(DMAC_CTRL(chan)) & 0x03;
  131. }
  132. void setup_dma(unsigned int chan, dma_info_t *info)
  133. {
  134. unsigned int irq = DMA_IRQ_DMTE0 + chan;
  135. dma_info_t *dma = dma_info + chan;
  136. make_intc_irq(irq);
  137. setup_irq(irq, &irq_dmte);
  138. dma = info;
  139. }
  140. void enable_dma(unsigned int chan)
  141. {
  142. u64 ctrl;
  143. ctrl = sh64_in64(DMAC_CTRL(chan));
  144. ctrl |= DMAC_CTRL_TE;
  145. sh64_out64(ctrl, DMAC_CTRL(chan));
  146. }
  147. void disable_dma(unsigned int chan)
  148. {
  149. u64 ctrl;
  150. ctrl = sh64_in64(DMAC_CTRL(chan));
  151. ctrl &= ~DMAC_CTRL_TE;
  152. sh64_out64(ctrl, DMAC_CTRL(chan));
  153. }
  154. void set_dma_mode(unsigned int chan, char mode)
  155. {
  156. dma_info_t *info = dma_info + chan;
  157. info->mode = mode;
  158. set_dma_addr(chan, info->mem_addr);
  159. set_dma_count(chan, info->count);
  160. }
  161. void set_dma_addr(unsigned int chan, unsigned int addr)
  162. {
  163. dma_info_t *info = dma_info + chan;
  164. unsigned long sar, dar;
  165. info->mem_addr = addr;
  166. sar = (info->mode & DMA_MODE_WRITE) ? info->mem_addr : info->dev_addr;
  167. dar = (info->mode & DMA_MODE_WRITE) ? info->dev_addr : info->mem_addr;
  168. sh64_out64(sar & DMAC_SAR_ADDR, DMAC_SAR(chan));
  169. sh64_out64(dar & DMAC_SAR_ADDR, DMAC_DAR(chan));
  170. }
  171. void set_dma_count(unsigned int chan, unsigned int count)
  172. {
  173. dma_info_t *info = dma_info + chan;
  174. u64 tmp;
  175. info->count = count;
  176. tmp = (info->count >> calc_xmit_shift(chan)) & DMAC_COUNT_CNT;
  177. sh64_out64(tmp, DMAC_COUNT(chan));
  178. }
  179. unsigned long claim_dma_lock(void)
  180. {
  181. unsigned long flags;
  182. spin_lock_irqsave(&dma_spin_lock, flags);
  183. return flags;
  184. }
  185. void release_dma_lock(unsigned long flags)
  186. {
  187. spin_unlock_irqrestore(&dma_spin_lock, flags);
  188. }
  189. int get_dma_residue(unsigned int chan)
  190. {
  191. return sh64_in64(DMAC_COUNT(chan) << calc_xmit_shift(chan));
  192. }
  193. int __init init_dma(void)
  194. {
  195. struct vcr_info vcr;
  196. u64 tmp;
  197. /* Remap the DMAC */
  198. dmac_base = onchip_remap(PHYS_DMAC_BLOCK, 1024, "DMAC");
  199. if (!dmac_base) {
  200. printk(KERN_ERR "Unable to remap DMAC\n");
  201. return -ENOMEM;
  202. }
  203. /* Report DMAC.VCR Info */
  204. vcr = sh64_get_vcr_info(dmac_base);
  205. printk("DMAC: Module ID: 0x%04x, Module version: 0x%04x\n",
  206. vcr.mod_id, vcr.mod_vers);
  207. /* Set the ME bit */
  208. tmp = sh64_in64(DMAC_COMMON_BASE);
  209. tmp |= DMAC_COMMON_ME;
  210. sh64_out64(tmp, DMAC_COMMON_BASE);
  211. /* Enable the DMAC Error Interrupt */
  212. make_intc_irq(DMA_IRQ_DERR);
  213. setup_irq(DMA_IRQ_DERR, &irq_derr);
  214. return 0;
  215. }
  216. static void __exit exit_dma(void)
  217. {
  218. onchip_unmap(dmac_base);
  219. free_irq(DMA_IRQ_DERR, 0);
  220. }
  221. module_init(init_dma);
  222. module_exit(exit_dma);
  223. MODULE_AUTHOR("Paul Mundt");
  224. MODULE_DESCRIPTION("DMA API for SH-5 DMAC");
  225. MODULE_LICENSE("GPL");
  226. EXPORT_SYMBOL(setup_dma);
  227. EXPORT_SYMBOL(claim_dma_lock);
  228. EXPORT_SYMBOL(release_dma_lock);
  229. EXPORT_SYMBOL(enable_dma);
  230. EXPORT_SYMBOL(disable_dma);
  231. EXPORT_SYMBOL(set_dma_mode);
  232. EXPORT_SYMBOL(set_dma_addr);
  233. EXPORT_SYMBOL(set_dma_count);
  234. EXPORT_SYMBOL(get_dma_residue);