dma.c 6.7 KB

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