xilinx_ll_temac_sdma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Xilinx xps_ll_temac ethernet driver for u-boot
  3. *
  4. * SDMA sub-controller
  5. *
  6. * Copyright (C) 2011 - 2012 Stephan Linz <linz@li-pro.net>
  7. * Copyright (C) 2008 - 2011 Michal Simek <monstr@monstr.eu>
  8. * Copyright (C) 2008 - 2011 PetaLogix
  9. *
  10. * Based on Yoshio Kashiwagi kashiwagi@co-nss.co.jp driver
  11. * Copyright (C) 2008 Nissin Systems Co.,Ltd.
  12. * March 2008 created
  13. *
  14. * CREDITS: tsec driver
  15. *
  16. * SPDX-License-Identifier: GPL-2.0+
  17. *
  18. * [0]: http://www.xilinx.com/support/documentation
  19. *
  20. * [M]: [0]/ip_documentation/mpmc.pdf
  21. * [S]: [0]/ip_documentation/xps_ll_temac.pdf
  22. * [A]: [0]/application_notes/xapp1041.pdf
  23. */
  24. #include <config.h>
  25. #include <common.h>
  26. #include <net.h>
  27. #include <asm/types.h>
  28. #include <asm/io.h>
  29. #include "xilinx_ll_temac.h"
  30. #include "xilinx_ll_temac_sdma.h"
  31. #define TX_BUF_CNT 2
  32. static unsigned int rx_idx; /* index of the current RX buffer */
  33. static unsigned int tx_idx; /* index of the current TX buffer */
  34. struct rtx_cdmac_bd {
  35. struct cdmac_bd rx[PKTBUFSRX];
  36. struct cdmac_bd tx[TX_BUF_CNT];
  37. };
  38. /*
  39. * DMA Buffer Descriptor alignment
  40. *
  41. * If the address contained in the Next Descriptor Pointer register is not
  42. * 8-word aligned or reaches beyond the range of available memory, the SDMA
  43. * halts processing and sets the CDMAC_BD_STCTRL_ERROR bit in the respective
  44. * status register (tx_chnl_sts or rx_chnl_sts).
  45. *
  46. * [1]: [0]/ip_documentation/mpmc.pdf
  47. * page 161, Next Descriptor Pointer
  48. */
  49. static struct rtx_cdmac_bd cdmac_bd __aligned(32);
  50. #if defined(CONFIG_XILINX_440) || defined(CONFIG_XILINX_405)
  51. /*
  52. * Indirect DCR access operations mi{ft}dcr_xilinx() espacialy
  53. * for Xilinx PowerPC implementations on FPGA.
  54. *
  55. * FIXME: This part should go up to arch/powerpc -- but where?
  56. */
  57. #include <asm/processor.h>
  58. #define XILINX_INDIRECT_DCR_ADDRESS_REG 0
  59. #define XILINX_INDIRECT_DCR_ACCESS_REG 1
  60. inline unsigned mifdcr_xilinx(const unsigned dcrn)
  61. {
  62. mtdcr(XILINX_INDIRECT_DCR_ADDRESS_REG, dcrn);
  63. return mfdcr(XILINX_INDIRECT_DCR_ACCESS_REG);
  64. }
  65. inline void mitdcr_xilinx(const unsigned dcrn, int val)
  66. {
  67. mtdcr(XILINX_INDIRECT_DCR_ADDRESS_REG, dcrn);
  68. mtdcr(XILINX_INDIRECT_DCR_ACCESS_REG, val);
  69. }
  70. /* Xilinx Device Control Register (DCR) in/out accessors */
  71. inline unsigned ll_temac_xldcr_in32(phys_addr_t addr)
  72. {
  73. return mifdcr_xilinx((const unsigned)addr);
  74. }
  75. inline void ll_temac_xldcr_out32(phys_addr_t addr, unsigned value)
  76. {
  77. mitdcr_xilinx((const unsigned)addr, value);
  78. }
  79. void ll_temac_collect_xldcr_sdma_reg_addr(struct eth_device *dev)
  80. {
  81. struct ll_temac *ll_temac = dev->priv;
  82. phys_addr_t dmac_ctrl = ll_temac->ctrladdr;
  83. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  84. ra[TX_NXTDESC_PTR] = dmac_ctrl + TX_NXTDESC_PTR;
  85. ra[TX_CURBUF_ADDR] = dmac_ctrl + TX_CURBUF_ADDR;
  86. ra[TX_CURBUF_LENGTH] = dmac_ctrl + TX_CURBUF_LENGTH;
  87. ra[TX_CURDESC_PTR] = dmac_ctrl + TX_CURDESC_PTR;
  88. ra[TX_TAILDESC_PTR] = dmac_ctrl + TX_TAILDESC_PTR;
  89. ra[TX_CHNL_CTRL] = dmac_ctrl + TX_CHNL_CTRL;
  90. ra[TX_IRQ_REG] = dmac_ctrl + TX_IRQ_REG;
  91. ra[TX_CHNL_STS] = dmac_ctrl + TX_CHNL_STS;
  92. ra[RX_NXTDESC_PTR] = dmac_ctrl + RX_NXTDESC_PTR;
  93. ra[RX_CURBUF_ADDR] = dmac_ctrl + RX_CURBUF_ADDR;
  94. ra[RX_CURBUF_LENGTH] = dmac_ctrl + RX_CURBUF_LENGTH;
  95. ra[RX_CURDESC_PTR] = dmac_ctrl + RX_CURDESC_PTR;
  96. ra[RX_TAILDESC_PTR] = dmac_ctrl + RX_TAILDESC_PTR;
  97. ra[RX_CHNL_CTRL] = dmac_ctrl + RX_CHNL_CTRL;
  98. ra[RX_IRQ_REG] = dmac_ctrl + RX_IRQ_REG;
  99. ra[RX_CHNL_STS] = dmac_ctrl + RX_CHNL_STS;
  100. ra[DMA_CONTROL_REG] = dmac_ctrl + DMA_CONTROL_REG;
  101. }
  102. #endif /* CONFIG_XILINX_440 || ONFIG_XILINX_405 */
  103. /* Xilinx Processor Local Bus (PLB) in/out accessors */
  104. inline unsigned ll_temac_xlplb_in32(phys_addr_t addr)
  105. {
  106. return in_be32((void *)addr);
  107. }
  108. inline void ll_temac_xlplb_out32(phys_addr_t addr, unsigned value)
  109. {
  110. out_be32((void *)addr, value);
  111. }
  112. /* collect all register addresses for Xilinx PLB in/out accessors */
  113. void ll_temac_collect_xlplb_sdma_reg_addr(struct eth_device *dev)
  114. {
  115. struct ll_temac *ll_temac = dev->priv;
  116. struct sdma_ctrl *sdma_ctrl = (void *)ll_temac->ctrladdr;
  117. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  118. ra[TX_NXTDESC_PTR] = (phys_addr_t)&sdma_ctrl->tx_nxtdesc_ptr;
  119. ra[TX_CURBUF_ADDR] = (phys_addr_t)&sdma_ctrl->tx_curbuf_addr;
  120. ra[TX_CURBUF_LENGTH] = (phys_addr_t)&sdma_ctrl->tx_curbuf_length;
  121. ra[TX_CURDESC_PTR] = (phys_addr_t)&sdma_ctrl->tx_curdesc_ptr;
  122. ra[TX_TAILDESC_PTR] = (phys_addr_t)&sdma_ctrl->tx_taildesc_ptr;
  123. ra[TX_CHNL_CTRL] = (phys_addr_t)&sdma_ctrl->tx_chnl_ctrl;
  124. ra[TX_IRQ_REG] = (phys_addr_t)&sdma_ctrl->tx_irq_reg;
  125. ra[TX_CHNL_STS] = (phys_addr_t)&sdma_ctrl->tx_chnl_sts;
  126. ra[RX_NXTDESC_PTR] = (phys_addr_t)&sdma_ctrl->rx_nxtdesc_ptr;
  127. ra[RX_CURBUF_ADDR] = (phys_addr_t)&sdma_ctrl->rx_curbuf_addr;
  128. ra[RX_CURBUF_LENGTH] = (phys_addr_t)&sdma_ctrl->rx_curbuf_length;
  129. ra[RX_CURDESC_PTR] = (phys_addr_t)&sdma_ctrl->rx_curdesc_ptr;
  130. ra[RX_TAILDESC_PTR] = (phys_addr_t)&sdma_ctrl->rx_taildesc_ptr;
  131. ra[RX_CHNL_CTRL] = (phys_addr_t)&sdma_ctrl->rx_chnl_ctrl;
  132. ra[RX_IRQ_REG] = (phys_addr_t)&sdma_ctrl->rx_irq_reg;
  133. ra[RX_CHNL_STS] = (phys_addr_t)&sdma_ctrl->rx_chnl_sts;
  134. ra[DMA_CONTROL_REG] = (phys_addr_t)&sdma_ctrl->dma_control_reg;
  135. }
  136. /* Check for TX and RX channel errors. */
  137. static inline int ll_temac_sdma_error(struct eth_device *dev)
  138. {
  139. int err;
  140. struct ll_temac *ll_temac = dev->priv;
  141. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  142. err = ll_temac->in32(ra[TX_CHNL_STS]) & CHNL_STS_ERROR;
  143. err |= ll_temac->in32(ra[RX_CHNL_STS]) & CHNL_STS_ERROR;
  144. return err;
  145. }
  146. int ll_temac_init_sdma(struct eth_device *dev)
  147. {
  148. struct ll_temac *ll_temac = dev->priv;
  149. struct cdmac_bd *rx_dp;
  150. struct cdmac_bd *tx_dp;
  151. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  152. int i;
  153. printf("%s: SDMA: %d Rx buffers, %d Tx buffers\n",
  154. dev->name, PKTBUFSRX, TX_BUF_CNT);
  155. /* Initialize the Rx Buffer descriptors */
  156. for (i = 0; i < PKTBUFSRX; i++) {
  157. rx_dp = &cdmac_bd.rx[i];
  158. memset(rx_dp, 0, sizeof(*rx_dp));
  159. rx_dp->next_p = rx_dp;
  160. rx_dp->buf_len = PKTSIZE_ALIGN;
  161. rx_dp->phys_buf_p = (u8 *)NetRxPackets[i];
  162. flush_cache((u32)rx_dp->phys_buf_p, PKTSIZE_ALIGN);
  163. }
  164. flush_cache((u32)cdmac_bd.rx, sizeof(cdmac_bd.rx));
  165. /* Initialize the TX Buffer Descriptors */
  166. for (i = 0; i < TX_BUF_CNT; i++) {
  167. tx_dp = &cdmac_bd.tx[i];
  168. memset(tx_dp, 0, sizeof(*tx_dp));
  169. tx_dp->next_p = tx_dp;
  170. }
  171. flush_cache((u32)cdmac_bd.tx, sizeof(cdmac_bd.tx));
  172. /* Reset index counter to the Rx and Tx Buffer descriptors */
  173. rx_idx = tx_idx = 0;
  174. /* initial Rx DMA start by writing to respective TAILDESC_PTR */
  175. ll_temac->out32(ra[RX_CURDESC_PTR], (int)&cdmac_bd.rx[rx_idx]);
  176. ll_temac->out32(ra[RX_TAILDESC_PTR], (int)&cdmac_bd.rx[rx_idx]);
  177. return 0;
  178. }
  179. int ll_temac_halt_sdma(struct eth_device *dev)
  180. {
  181. unsigned timeout = 50; /* 1usec * 50 = 50usec */
  182. struct ll_temac *ll_temac = dev->priv;
  183. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  184. /*
  185. * Soft reset the DMA
  186. *
  187. * Quote from MPMC documentation: Writing a 1 to this field
  188. * forces the DMA engine to shutdown and reset itself. After
  189. * setting this bit, software must poll it until the bit is
  190. * cleared by the DMA. This indicates that the reset process
  191. * is done and the pipeline has been flushed.
  192. */
  193. ll_temac->out32(ra[DMA_CONTROL_REG], DMA_CONTROL_RESET);
  194. while (timeout && (ll_temac->in32(ra[DMA_CONTROL_REG])
  195. & DMA_CONTROL_RESET)) {
  196. timeout--;
  197. udelay(1);
  198. }
  199. if (!timeout) {
  200. printf("%s: Timeout\n", __func__);
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. int ll_temac_reset_sdma(struct eth_device *dev)
  206. {
  207. u32 r;
  208. struct ll_temac *ll_temac = dev->priv;
  209. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  210. /* Soft reset the DMA. */
  211. if (ll_temac_halt_sdma(dev))
  212. return -1;
  213. /* Now clear the interrupts. */
  214. r = ll_temac->in32(ra[TX_CHNL_CTRL]);
  215. r &= ~CHNL_CTRL_IRQ_MASK;
  216. ll_temac->out32(ra[TX_CHNL_CTRL], r);
  217. r = ll_temac->in32(ra[RX_CHNL_CTRL]);
  218. r &= ~CHNL_CTRL_IRQ_MASK;
  219. ll_temac->out32(ra[RX_CHNL_CTRL], r);
  220. /* Now ACK pending IRQs. */
  221. ll_temac->out32(ra[TX_IRQ_REG], IRQ_REG_IRQ_MASK);
  222. ll_temac->out32(ra[RX_IRQ_REG], IRQ_REG_IRQ_MASK);
  223. /* Set tail-ptr mode, disable errors for both channels. */
  224. ll_temac->out32(ra[DMA_CONTROL_REG],
  225. /* Enable use of tail pointer register */
  226. DMA_CONTROL_TPE |
  227. /* Disable error when 2 or 4 bit coalesce cnt overfl */
  228. DMA_CONTROL_RXOCEID |
  229. /* Disable error when 2 or 4 bit coalesce cnt overfl */
  230. DMA_CONTROL_TXOCEID);
  231. return 0;
  232. }
  233. int ll_temac_recv_sdma(struct eth_device *dev)
  234. {
  235. int length, pb_idx;
  236. struct cdmac_bd *rx_dp = &cdmac_bd.rx[rx_idx];
  237. struct ll_temac *ll_temac = dev->priv;
  238. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  239. if (ll_temac_sdma_error(dev)) {
  240. if (ll_temac_reset_sdma(dev))
  241. return -1;
  242. ll_temac_init_sdma(dev);
  243. }
  244. flush_cache((u32)rx_dp, sizeof(*rx_dp));
  245. if (!(rx_dp->sca.stctrl & CDMAC_BD_STCTRL_COMPLETED))
  246. return 0;
  247. if (rx_dp->sca.stctrl & (CDMAC_BD_STCTRL_SOP | CDMAC_BD_STCTRL_EOP)) {
  248. pb_idx = rx_idx;
  249. length = rx_dp->sca.app[4] & CDMAC_BD_APP4_RXBYTECNT_MASK;
  250. } else {
  251. pb_idx = -1;
  252. length = 0;
  253. printf("%s: Got part of package, unsupported (%x)\n",
  254. __func__, rx_dp->sca.stctrl);
  255. }
  256. /* flip the buffer */
  257. flush_cache((u32)rx_dp->phys_buf_p, length);
  258. /* reset the current descriptor */
  259. rx_dp->sca.stctrl = 0;
  260. rx_dp->sca.app[4] = 0;
  261. flush_cache((u32)rx_dp, sizeof(*rx_dp));
  262. /* Find next empty buffer descriptor, preparation for next iteration */
  263. rx_idx = (rx_idx + 1) % PKTBUFSRX;
  264. rx_dp = &cdmac_bd.rx[rx_idx];
  265. flush_cache((u32)rx_dp, sizeof(*rx_dp));
  266. /* DMA start by writing to respective TAILDESC_PTR */
  267. ll_temac->out32(ra[RX_CURDESC_PTR], (int)&cdmac_bd.rx[rx_idx]);
  268. ll_temac->out32(ra[RX_TAILDESC_PTR], (int)&cdmac_bd.rx[rx_idx]);
  269. if (length > 0 && pb_idx != -1)
  270. NetReceive(NetRxPackets[pb_idx], length);
  271. return 0;
  272. }
  273. int ll_temac_send_sdma(struct eth_device *dev, void *packet, int length)
  274. {
  275. unsigned timeout = 50; /* 1usec * 50 = 50usec */
  276. struct cdmac_bd *tx_dp = &cdmac_bd.tx[tx_idx];
  277. struct ll_temac *ll_temac = dev->priv;
  278. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  279. if (ll_temac_sdma_error(dev)) {
  280. if (ll_temac_reset_sdma(dev))
  281. return -1;
  282. ll_temac_init_sdma(dev);
  283. }
  284. tx_dp->phys_buf_p = (u8 *)packet;
  285. tx_dp->buf_len = length;
  286. tx_dp->sca.stctrl = CDMAC_BD_STCTRL_SOP | CDMAC_BD_STCTRL_EOP |
  287. CDMAC_BD_STCTRL_STOP_ON_END;
  288. flush_cache((u32)packet, length);
  289. flush_cache((u32)tx_dp, sizeof(*tx_dp));
  290. /* DMA start by writing to respective TAILDESC_PTR */
  291. ll_temac->out32(ra[TX_CURDESC_PTR], (int)tx_dp);
  292. ll_temac->out32(ra[TX_TAILDESC_PTR], (int)tx_dp);
  293. /* Find next empty buffer descriptor, preparation for next iteration */
  294. tx_idx = (tx_idx + 1) % TX_BUF_CNT;
  295. tx_dp = &cdmac_bd.tx[tx_idx];
  296. do {
  297. flush_cache((u32)tx_dp, sizeof(*tx_dp));
  298. udelay(1);
  299. } while (timeout-- && !(tx_dp->sca.stctrl & CDMAC_BD_STCTRL_COMPLETED));
  300. if (!timeout) {
  301. printf("%s: Timeout\n", __func__);
  302. return -1;
  303. }
  304. return 0;
  305. }