ebus.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* ebus.c: EBUS DMA library code.
  2. *
  3. * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
  4. * Copyright (C) 1999 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/export.h>
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/delay.h>
  11. #include <asm/ebus_dma.h>
  12. #include <asm/io.h>
  13. #define EBDMA_CSR 0x00UL /* Control/Status */
  14. #define EBDMA_ADDR 0x04UL /* DMA Address */
  15. #define EBDMA_COUNT 0x08UL /* DMA Count */
  16. #define EBDMA_CSR_INT_PEND 0x00000001
  17. #define EBDMA_CSR_ERR_PEND 0x00000002
  18. #define EBDMA_CSR_DRAIN 0x00000004
  19. #define EBDMA_CSR_INT_EN 0x00000010
  20. #define EBDMA_CSR_RESET 0x00000080
  21. #define EBDMA_CSR_WRITE 0x00000100
  22. #define EBDMA_CSR_EN_DMA 0x00000200
  23. #define EBDMA_CSR_CYC_PEND 0x00000400
  24. #define EBDMA_CSR_DIAG_RD_DONE 0x00000800
  25. #define EBDMA_CSR_DIAG_WR_DONE 0x00001000
  26. #define EBDMA_CSR_EN_CNT 0x00002000
  27. #define EBDMA_CSR_TC 0x00004000
  28. #define EBDMA_CSR_DIS_CSR_DRN 0x00010000
  29. #define EBDMA_CSR_BURST_SZ_MASK 0x000c0000
  30. #define EBDMA_CSR_BURST_SZ_1 0x00080000
  31. #define EBDMA_CSR_BURST_SZ_4 0x00000000
  32. #define EBDMA_CSR_BURST_SZ_8 0x00040000
  33. #define EBDMA_CSR_BURST_SZ_16 0x000c0000
  34. #define EBDMA_CSR_DIAG_EN 0x00100000
  35. #define EBDMA_CSR_DIS_ERR_PEND 0x00400000
  36. #define EBDMA_CSR_TCI_DIS 0x00800000
  37. #define EBDMA_CSR_EN_NEXT 0x01000000
  38. #define EBDMA_CSR_DMA_ON 0x02000000
  39. #define EBDMA_CSR_A_LOADED 0x04000000
  40. #define EBDMA_CSR_NA_LOADED 0x08000000
  41. #define EBDMA_CSR_DEV_ID_MASK 0xf0000000
  42. #define EBUS_DMA_RESET_TIMEOUT 10000
  43. static void __ebus_dma_reset(struct ebus_dma_info *p, int no_drain)
  44. {
  45. int i;
  46. u32 val = 0;
  47. writel(EBDMA_CSR_RESET, p->regs + EBDMA_CSR);
  48. udelay(1);
  49. if (no_drain)
  50. return;
  51. for (i = EBUS_DMA_RESET_TIMEOUT; i > 0; i--) {
  52. val = readl(p->regs + EBDMA_CSR);
  53. if (!(val & (EBDMA_CSR_DRAIN | EBDMA_CSR_CYC_PEND)))
  54. break;
  55. udelay(10);
  56. }
  57. }
  58. static irqreturn_t ebus_dma_irq(int irq, void *dev_id)
  59. {
  60. struct ebus_dma_info *p = dev_id;
  61. unsigned long flags;
  62. u32 csr = 0;
  63. spin_lock_irqsave(&p->lock, flags);
  64. csr = readl(p->regs + EBDMA_CSR);
  65. writel(csr, p->regs + EBDMA_CSR);
  66. spin_unlock_irqrestore(&p->lock, flags);
  67. if (csr & EBDMA_CSR_ERR_PEND) {
  68. printk(KERN_CRIT "ebus_dma(%s): DMA error!\n", p->name);
  69. p->callback(p, EBUS_DMA_EVENT_ERROR, p->client_cookie);
  70. return IRQ_HANDLED;
  71. } else if (csr & EBDMA_CSR_INT_PEND) {
  72. p->callback(p,
  73. (csr & EBDMA_CSR_TC) ?
  74. EBUS_DMA_EVENT_DMA : EBUS_DMA_EVENT_DEVICE,
  75. p->client_cookie);
  76. return IRQ_HANDLED;
  77. }
  78. return IRQ_NONE;
  79. }
  80. int ebus_dma_register(struct ebus_dma_info *p)
  81. {
  82. u32 csr;
  83. if (!p->regs)
  84. return -EINVAL;
  85. if (p->flags & ~(EBUS_DMA_FLAG_USE_EBDMA_HANDLER |
  86. EBUS_DMA_FLAG_TCI_DISABLE))
  87. return -EINVAL;
  88. if ((p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) && !p->callback)
  89. return -EINVAL;
  90. if (!strlen(p->name))
  91. return -EINVAL;
  92. __ebus_dma_reset(p, 1);
  93. csr = EBDMA_CSR_BURST_SZ_16 | EBDMA_CSR_EN_CNT;
  94. if (p->flags & EBUS_DMA_FLAG_TCI_DISABLE)
  95. csr |= EBDMA_CSR_TCI_DIS;
  96. writel(csr, p->regs + EBDMA_CSR);
  97. return 0;
  98. }
  99. EXPORT_SYMBOL(ebus_dma_register);
  100. int ebus_dma_irq_enable(struct ebus_dma_info *p, int on)
  101. {
  102. unsigned long flags;
  103. u32 csr;
  104. if (on) {
  105. if (p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) {
  106. if (request_irq(p->irq, ebus_dma_irq, IRQF_SHARED, p->name, p))
  107. return -EBUSY;
  108. }
  109. spin_lock_irqsave(&p->lock, flags);
  110. csr = readl(p->regs + EBDMA_CSR);
  111. csr |= EBDMA_CSR_INT_EN;
  112. writel(csr, p->regs + EBDMA_CSR);
  113. spin_unlock_irqrestore(&p->lock, flags);
  114. } else {
  115. spin_lock_irqsave(&p->lock, flags);
  116. csr = readl(p->regs + EBDMA_CSR);
  117. csr &= ~EBDMA_CSR_INT_EN;
  118. writel(csr, p->regs + EBDMA_CSR);
  119. spin_unlock_irqrestore(&p->lock, flags);
  120. if (p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) {
  121. free_irq(p->irq, p);
  122. }
  123. }
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(ebus_dma_irq_enable);
  127. void ebus_dma_unregister(struct ebus_dma_info *p)
  128. {
  129. unsigned long flags;
  130. u32 csr;
  131. int irq_on = 0;
  132. spin_lock_irqsave(&p->lock, flags);
  133. csr = readl(p->regs + EBDMA_CSR);
  134. if (csr & EBDMA_CSR_INT_EN) {
  135. csr &= ~EBDMA_CSR_INT_EN;
  136. writel(csr, p->regs + EBDMA_CSR);
  137. irq_on = 1;
  138. }
  139. spin_unlock_irqrestore(&p->lock, flags);
  140. if (irq_on)
  141. free_irq(p->irq, p);
  142. }
  143. EXPORT_SYMBOL(ebus_dma_unregister);
  144. int ebus_dma_request(struct ebus_dma_info *p, dma_addr_t bus_addr, size_t len)
  145. {
  146. unsigned long flags;
  147. u32 csr;
  148. int err;
  149. if (len >= (1 << 24))
  150. return -EINVAL;
  151. spin_lock_irqsave(&p->lock, flags);
  152. csr = readl(p->regs + EBDMA_CSR);
  153. err = -EINVAL;
  154. if (!(csr & EBDMA_CSR_EN_DMA))
  155. goto out;
  156. err = -EBUSY;
  157. if (csr & EBDMA_CSR_NA_LOADED)
  158. goto out;
  159. writel(len, p->regs + EBDMA_COUNT);
  160. writel(bus_addr, p->regs + EBDMA_ADDR);
  161. err = 0;
  162. out:
  163. spin_unlock_irqrestore(&p->lock, flags);
  164. return err;
  165. }
  166. EXPORT_SYMBOL(ebus_dma_request);
  167. void ebus_dma_prepare(struct ebus_dma_info *p, int write)
  168. {
  169. unsigned long flags;
  170. u32 csr;
  171. spin_lock_irqsave(&p->lock, flags);
  172. __ebus_dma_reset(p, 0);
  173. csr = (EBDMA_CSR_INT_EN |
  174. EBDMA_CSR_EN_CNT |
  175. EBDMA_CSR_BURST_SZ_16 |
  176. EBDMA_CSR_EN_NEXT);
  177. if (write)
  178. csr |= EBDMA_CSR_WRITE;
  179. if (p->flags & EBUS_DMA_FLAG_TCI_DISABLE)
  180. csr |= EBDMA_CSR_TCI_DIS;
  181. writel(csr, p->regs + EBDMA_CSR);
  182. spin_unlock_irqrestore(&p->lock, flags);
  183. }
  184. EXPORT_SYMBOL(ebus_dma_prepare);
  185. unsigned int ebus_dma_residue(struct ebus_dma_info *p)
  186. {
  187. return readl(p->regs + EBDMA_COUNT);
  188. }
  189. EXPORT_SYMBOL(ebus_dma_residue);
  190. unsigned int ebus_dma_addr(struct ebus_dma_info *p)
  191. {
  192. return readl(p->regs + EBDMA_ADDR);
  193. }
  194. EXPORT_SYMBOL(ebus_dma_addr);
  195. void ebus_dma_enable(struct ebus_dma_info *p, int on)
  196. {
  197. unsigned long flags;
  198. u32 orig_csr, csr;
  199. spin_lock_irqsave(&p->lock, flags);
  200. orig_csr = csr = readl(p->regs + EBDMA_CSR);
  201. if (on)
  202. csr |= EBDMA_CSR_EN_DMA;
  203. else
  204. csr &= ~EBDMA_CSR_EN_DMA;
  205. if ((orig_csr & EBDMA_CSR_EN_DMA) !=
  206. (csr & EBDMA_CSR_EN_DMA))
  207. writel(csr, p->regs + EBDMA_CSR);
  208. spin_unlock_irqrestore(&p->lock, flags);
  209. }
  210. EXPORT_SYMBOL(ebus_dma_enable);