dma.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  14. *
  15. * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
  16. */
  17. #include <linux/init.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/export.h>
  22. #include <lantiq_soc.h>
  23. #include <xway_dma.h>
  24. #define LTQ_DMA_CTRL 0x10
  25. #define LTQ_DMA_CPOLL 0x14
  26. #define LTQ_DMA_CS 0x18
  27. #define LTQ_DMA_CCTRL 0x1C
  28. #define LTQ_DMA_CDBA 0x20
  29. #define LTQ_DMA_CDLEN 0x24
  30. #define LTQ_DMA_CIS 0x28
  31. #define LTQ_DMA_CIE 0x2C
  32. #define LTQ_DMA_PS 0x40
  33. #define LTQ_DMA_PCTRL 0x44
  34. #define LTQ_DMA_IRNEN 0xf4
  35. #define DMA_DESCPT BIT(3) /* descriptor complete irq */
  36. #define DMA_TX BIT(8) /* TX channel direction */
  37. #define DMA_CHAN_ON BIT(0) /* channel on / off bit */
  38. #define DMA_PDEN BIT(6) /* enable packet drop */
  39. #define DMA_CHAN_RST BIT(1) /* channel on / off bit */
  40. #define DMA_RESET BIT(0) /* channel on / off bit */
  41. #define DMA_IRQ_ACK 0x7e /* IRQ status register */
  42. #define DMA_POLL BIT(31) /* turn on channel polling */
  43. #define DMA_CLK_DIV4 BIT(6) /* polling clock divider */
  44. #define DMA_2W_BURST BIT(1) /* 2 word burst length */
  45. #define DMA_MAX_CHANNEL 20 /* the soc has 20 channels */
  46. #define DMA_ETOP_ENDIANESS (0xf << 8) /* endianess swap etop channels */
  47. #define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */
  48. #define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x))
  49. #define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y))
  50. #define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \
  51. ltq_dma_membase + (z))
  52. static struct resource ltq_dma_resource = {
  53. .name = "dma",
  54. .start = LTQ_DMA_BASE_ADDR,
  55. .end = LTQ_DMA_BASE_ADDR + LTQ_DMA_SIZE - 1,
  56. .flags = IORESOURCE_MEM,
  57. };
  58. static void __iomem *ltq_dma_membase;
  59. void
  60. ltq_dma_enable_irq(struct ltq_dma_channel *ch)
  61. {
  62. unsigned long flags;
  63. local_irq_save(flags);
  64. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  65. ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
  66. local_irq_restore(flags);
  67. }
  68. EXPORT_SYMBOL_GPL(ltq_dma_enable_irq);
  69. void
  70. ltq_dma_disable_irq(struct ltq_dma_channel *ch)
  71. {
  72. unsigned long flags;
  73. local_irq_save(flags);
  74. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  75. ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
  76. local_irq_restore(flags);
  77. }
  78. EXPORT_SYMBOL_GPL(ltq_dma_disable_irq);
  79. void
  80. ltq_dma_ack_irq(struct ltq_dma_channel *ch)
  81. {
  82. unsigned long flags;
  83. local_irq_save(flags);
  84. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  85. ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS);
  86. local_irq_restore(flags);
  87. }
  88. EXPORT_SYMBOL_GPL(ltq_dma_ack_irq);
  89. void
  90. ltq_dma_open(struct ltq_dma_channel *ch)
  91. {
  92. unsigned long flag;
  93. local_irq_save(flag);
  94. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  95. ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL);
  96. ltq_dma_enable_irq(ch);
  97. local_irq_restore(flag);
  98. }
  99. EXPORT_SYMBOL_GPL(ltq_dma_open);
  100. void
  101. ltq_dma_close(struct ltq_dma_channel *ch)
  102. {
  103. unsigned long flag;
  104. local_irq_save(flag);
  105. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  106. ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
  107. ltq_dma_disable_irq(ch);
  108. local_irq_restore(flag);
  109. }
  110. EXPORT_SYMBOL_GPL(ltq_dma_close);
  111. static void
  112. ltq_dma_alloc(struct ltq_dma_channel *ch)
  113. {
  114. unsigned long flags;
  115. ch->desc = 0;
  116. ch->desc_base = dma_alloc_coherent(NULL,
  117. LTQ_DESC_NUM * LTQ_DESC_SIZE,
  118. &ch->phys, GFP_ATOMIC);
  119. memset(ch->desc_base, 0, LTQ_DESC_NUM * LTQ_DESC_SIZE);
  120. local_irq_save(flags);
  121. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  122. ltq_dma_w32(ch->phys, LTQ_DMA_CDBA);
  123. ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN);
  124. ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
  125. wmb();
  126. ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL);
  127. while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST)
  128. ;
  129. local_irq_restore(flags);
  130. }
  131. void
  132. ltq_dma_alloc_tx(struct ltq_dma_channel *ch)
  133. {
  134. unsigned long flags;
  135. ltq_dma_alloc(ch);
  136. local_irq_save(flags);
  137. ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
  138. ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
  139. ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL);
  140. local_irq_restore(flags);
  141. }
  142. EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx);
  143. void
  144. ltq_dma_alloc_rx(struct ltq_dma_channel *ch)
  145. {
  146. unsigned long flags;
  147. ltq_dma_alloc(ch);
  148. local_irq_save(flags);
  149. ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
  150. ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
  151. ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL);
  152. local_irq_restore(flags);
  153. }
  154. EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx);
  155. void
  156. ltq_dma_free(struct ltq_dma_channel *ch)
  157. {
  158. if (!ch->desc_base)
  159. return;
  160. ltq_dma_close(ch);
  161. dma_free_coherent(NULL, LTQ_DESC_NUM * LTQ_DESC_SIZE,
  162. ch->desc_base, ch->phys);
  163. }
  164. EXPORT_SYMBOL_GPL(ltq_dma_free);
  165. void
  166. ltq_dma_init_port(int p)
  167. {
  168. ltq_dma_w32(p, LTQ_DMA_PS);
  169. switch (p) {
  170. case DMA_PORT_ETOP:
  171. /*
  172. * Tell the DMA engine to swap the endianess of data frames and
  173. * drop packets if the channel arbitration fails.
  174. */
  175. ltq_dma_w32_mask(0, DMA_ETOP_ENDIANESS | DMA_PDEN,
  176. LTQ_DMA_PCTRL);
  177. break;
  178. case DMA_PORT_DEU:
  179. ltq_dma_w32((DMA_2W_BURST << 4) | (DMA_2W_BURST << 2),
  180. LTQ_DMA_PCTRL);
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. EXPORT_SYMBOL_GPL(ltq_dma_init_port);
  187. int __init
  188. ltq_dma_init(void)
  189. {
  190. int i;
  191. /* insert and request the memory region */
  192. if (insert_resource(&iomem_resource, &ltq_dma_resource) < 0)
  193. panic("Failed to insert dma memory");
  194. if (request_mem_region(ltq_dma_resource.start,
  195. resource_size(&ltq_dma_resource), "dma") < 0)
  196. panic("Failed to request dma memory");
  197. /* remap dma register range */
  198. ltq_dma_membase = ioremap_nocache(ltq_dma_resource.start,
  199. resource_size(&ltq_dma_resource));
  200. if (!ltq_dma_membase)
  201. panic("Failed to remap dma memory");
  202. /* power up and reset the dma engine */
  203. ltq_pmu_enable(PMU_DMA);
  204. ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL);
  205. /* disable all interrupts */
  206. ltq_dma_w32(0, LTQ_DMA_IRNEN);
  207. /* reset/configure each channel */
  208. for (i = 0; i < DMA_MAX_CHANNEL; i++) {
  209. ltq_dma_w32(i, LTQ_DMA_CS);
  210. ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL);
  211. ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL);
  212. ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
  213. }
  214. return 0;
  215. }
  216. postcore_initcall(ltq_dma_init);