dma.c 6.8 KB

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