dma.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/module.h>
  22. #include <linux/clk.h>
  23. #include <lantiq_soc.h>
  24. #include <xway_dma.h>
  25. #define LTQ_DMA_CTRL 0x10
  26. #define LTQ_DMA_CPOLL 0x14
  27. #define LTQ_DMA_CS 0x18
  28. #define LTQ_DMA_CCTRL 0x1C
  29. #define LTQ_DMA_CDBA 0x20
  30. #define LTQ_DMA_CDLEN 0x24
  31. #define LTQ_DMA_CIS 0x28
  32. #define LTQ_DMA_CIE 0x2C
  33. #define LTQ_DMA_PS 0x40
  34. #define LTQ_DMA_PCTRL 0x44
  35. #define LTQ_DMA_IRNEN 0xf4
  36. #define DMA_DESCPT BIT(3) /* descriptor complete irq */
  37. #define DMA_TX BIT(8) /* TX channel direction */
  38. #define DMA_CHAN_ON BIT(0) /* channel on / off bit */
  39. #define DMA_PDEN BIT(6) /* enable packet drop */
  40. #define DMA_CHAN_RST BIT(1) /* channel on / off bit */
  41. #define DMA_RESET BIT(0) /* channel on / off bit */
  42. #define DMA_IRQ_ACK 0x7e /* IRQ status register */
  43. #define DMA_POLL BIT(31) /* turn on channel polling */
  44. #define DMA_CLK_DIV4 BIT(6) /* polling clock divider */
  45. #define DMA_2W_BURST BIT(1) /* 2 word burst length */
  46. #define DMA_MAX_CHANNEL 20 /* the soc has 20 channels */
  47. #define DMA_ETOP_ENDIANESS (0xf << 8) /* endianess swap etop channels */
  48. #define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */
  49. #define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x))
  50. #define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y))
  51. #define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \
  52. ltq_dma_membase + (z))
  53. static void __iomem *ltq_dma_membase;
  54. void
  55. ltq_dma_enable_irq(struct ltq_dma_channel *ch)
  56. {
  57. unsigned long flags;
  58. local_irq_save(flags);
  59. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  60. ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
  61. local_irq_restore(flags);
  62. }
  63. EXPORT_SYMBOL_GPL(ltq_dma_enable_irq);
  64. void
  65. ltq_dma_disable_irq(struct ltq_dma_channel *ch)
  66. {
  67. unsigned long flags;
  68. local_irq_save(flags);
  69. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  70. ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
  71. local_irq_restore(flags);
  72. }
  73. EXPORT_SYMBOL_GPL(ltq_dma_disable_irq);
  74. void
  75. ltq_dma_ack_irq(struct ltq_dma_channel *ch)
  76. {
  77. unsigned long flags;
  78. local_irq_save(flags);
  79. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  80. ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS);
  81. local_irq_restore(flags);
  82. }
  83. EXPORT_SYMBOL_GPL(ltq_dma_ack_irq);
  84. void
  85. ltq_dma_open(struct ltq_dma_channel *ch)
  86. {
  87. unsigned long flag;
  88. local_irq_save(flag);
  89. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  90. ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL);
  91. ltq_dma_enable_irq(ch);
  92. local_irq_restore(flag);
  93. }
  94. EXPORT_SYMBOL_GPL(ltq_dma_open);
  95. void
  96. ltq_dma_close(struct ltq_dma_channel *ch)
  97. {
  98. unsigned long flag;
  99. local_irq_save(flag);
  100. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  101. ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
  102. ltq_dma_disable_irq(ch);
  103. local_irq_restore(flag);
  104. }
  105. EXPORT_SYMBOL_GPL(ltq_dma_close);
  106. static void
  107. ltq_dma_alloc(struct ltq_dma_channel *ch)
  108. {
  109. unsigned long flags;
  110. ch->desc = 0;
  111. ch->desc_base = dma_alloc_coherent(NULL,
  112. LTQ_DESC_NUM * LTQ_DESC_SIZE,
  113. &ch->phys, GFP_ATOMIC);
  114. memset(ch->desc_base, 0, LTQ_DESC_NUM * LTQ_DESC_SIZE);
  115. local_irq_save(flags);
  116. ltq_dma_w32(ch->nr, LTQ_DMA_CS);
  117. ltq_dma_w32(ch->phys, LTQ_DMA_CDBA);
  118. ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN);
  119. ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
  120. wmb();
  121. ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL);
  122. while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST)
  123. ;
  124. local_irq_restore(flags);
  125. }
  126. void
  127. ltq_dma_alloc_tx(struct ltq_dma_channel *ch)
  128. {
  129. unsigned long flags;
  130. ltq_dma_alloc(ch);
  131. local_irq_save(flags);
  132. ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
  133. ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
  134. ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL);
  135. local_irq_restore(flags);
  136. }
  137. EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx);
  138. void
  139. ltq_dma_alloc_rx(struct ltq_dma_channel *ch)
  140. {
  141. unsigned long flags;
  142. ltq_dma_alloc(ch);
  143. local_irq_save(flags);
  144. ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
  145. ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
  146. ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL);
  147. local_irq_restore(flags);
  148. }
  149. EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx);
  150. void
  151. ltq_dma_free(struct ltq_dma_channel *ch)
  152. {
  153. if (!ch->desc_base)
  154. return;
  155. ltq_dma_close(ch);
  156. dma_free_coherent(NULL, LTQ_DESC_NUM * LTQ_DESC_SIZE,
  157. ch->desc_base, ch->phys);
  158. }
  159. EXPORT_SYMBOL_GPL(ltq_dma_free);
  160. void
  161. ltq_dma_init_port(int p)
  162. {
  163. ltq_dma_w32(p, LTQ_DMA_PS);
  164. switch (p) {
  165. case DMA_PORT_ETOP:
  166. /*
  167. * Tell the DMA engine to swap the endianess of data frames and
  168. * drop packets if the channel arbitration fails.
  169. */
  170. ltq_dma_w32_mask(0, DMA_ETOP_ENDIANESS | DMA_PDEN,
  171. LTQ_DMA_PCTRL);
  172. break;
  173. case DMA_PORT_DEU:
  174. ltq_dma_w32((DMA_2W_BURST << 4) | (DMA_2W_BURST << 2),
  175. LTQ_DMA_PCTRL);
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. EXPORT_SYMBOL_GPL(ltq_dma_init_port);
  182. static int __devinit
  183. ltq_dma_init(struct platform_device *pdev)
  184. {
  185. struct clk *clk;
  186. struct resource *res;
  187. int i;
  188. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  189. if (!res)
  190. panic("Failed to get dma resource");
  191. /* remap dma register range */
  192. ltq_dma_membase = devm_request_and_ioremap(&pdev->dev, res);
  193. if (!ltq_dma_membase)
  194. panic("Failed to remap dma resource");
  195. /* power up and reset the dma engine */
  196. clk = clk_get(&pdev->dev, NULL);
  197. if (IS_ERR(clk))
  198. panic("Failed to get dma clock");
  199. clk_enable(clk);
  200. ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL);
  201. /* disable all interrupts */
  202. ltq_dma_w32(0, LTQ_DMA_IRNEN);
  203. /* reset/configure each channel */
  204. for (i = 0; i < DMA_MAX_CHANNEL; i++) {
  205. ltq_dma_w32(i, LTQ_DMA_CS);
  206. ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL);
  207. ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL);
  208. ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
  209. }
  210. dev_info(&pdev->dev, "init done\n");
  211. return 0;
  212. }
  213. static const struct of_device_id dma_match[] = {
  214. { .compatible = "lantiq,dma-xway" },
  215. {},
  216. };
  217. MODULE_DEVICE_TABLE(of, dma_match);
  218. static struct platform_driver dma_driver = {
  219. .probe = ltq_dma_init,
  220. .driver = {
  221. .name = "dma-xway",
  222. .owner = THIS_MODULE,
  223. .of_match_table = dma_match,
  224. },
  225. };
  226. int __init
  227. dma_init(void)
  228. {
  229. return platform_driver_register(&dma_driver);
  230. }
  231. postcore_initcall(dma_init);