xilinx_ll_temac_sdma.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. /* Xilinx Processor Local Bus (PLB) in/out accessors */
  51. inline unsigned ll_temac_xlplb_in32(phys_addr_t addr)
  52. {
  53. return in_be32((void *)addr);
  54. }
  55. inline void ll_temac_xlplb_out32(phys_addr_t addr, unsigned value)
  56. {
  57. out_be32((void *)addr, value);
  58. }
  59. /* collect all register addresses for Xilinx PLB in/out accessors */
  60. void ll_temac_collect_xlplb_sdma_reg_addr(struct eth_device *dev)
  61. {
  62. struct ll_temac *ll_temac = dev->priv;
  63. struct sdma_ctrl *sdma_ctrl = (void *)ll_temac->ctrladdr;
  64. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  65. ra[TX_NXTDESC_PTR] = (phys_addr_t)&sdma_ctrl->tx_nxtdesc_ptr;
  66. ra[TX_CURBUF_ADDR] = (phys_addr_t)&sdma_ctrl->tx_curbuf_addr;
  67. ra[TX_CURBUF_LENGTH] = (phys_addr_t)&sdma_ctrl->tx_curbuf_length;
  68. ra[TX_CURDESC_PTR] = (phys_addr_t)&sdma_ctrl->tx_curdesc_ptr;
  69. ra[TX_TAILDESC_PTR] = (phys_addr_t)&sdma_ctrl->tx_taildesc_ptr;
  70. ra[TX_CHNL_CTRL] = (phys_addr_t)&sdma_ctrl->tx_chnl_ctrl;
  71. ra[TX_IRQ_REG] = (phys_addr_t)&sdma_ctrl->tx_irq_reg;
  72. ra[TX_CHNL_STS] = (phys_addr_t)&sdma_ctrl->tx_chnl_sts;
  73. ra[RX_NXTDESC_PTR] = (phys_addr_t)&sdma_ctrl->rx_nxtdesc_ptr;
  74. ra[RX_CURBUF_ADDR] = (phys_addr_t)&sdma_ctrl->rx_curbuf_addr;
  75. ra[RX_CURBUF_LENGTH] = (phys_addr_t)&sdma_ctrl->rx_curbuf_length;
  76. ra[RX_CURDESC_PTR] = (phys_addr_t)&sdma_ctrl->rx_curdesc_ptr;
  77. ra[RX_TAILDESC_PTR] = (phys_addr_t)&sdma_ctrl->rx_taildesc_ptr;
  78. ra[RX_CHNL_CTRL] = (phys_addr_t)&sdma_ctrl->rx_chnl_ctrl;
  79. ra[RX_IRQ_REG] = (phys_addr_t)&sdma_ctrl->rx_irq_reg;
  80. ra[RX_CHNL_STS] = (phys_addr_t)&sdma_ctrl->rx_chnl_sts;
  81. ra[DMA_CONTROL_REG] = (phys_addr_t)&sdma_ctrl->dma_control_reg;
  82. }
  83. /* Check for TX and RX channel errors. */
  84. static inline int ll_temac_sdma_error(struct eth_device *dev)
  85. {
  86. int err;
  87. struct ll_temac *ll_temac = dev->priv;
  88. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  89. err = ll_temac->in32(ra[TX_CHNL_STS]) & CHNL_STS_ERROR;
  90. err |= ll_temac->in32(ra[RX_CHNL_STS]) & CHNL_STS_ERROR;
  91. return err;
  92. }
  93. int ll_temac_init_sdma(struct eth_device *dev)
  94. {
  95. struct ll_temac *ll_temac = dev->priv;
  96. struct cdmac_bd *rx_dp;
  97. struct cdmac_bd *tx_dp;
  98. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  99. int i;
  100. printf("%s: SDMA: %d Rx buffers, %d Tx buffers\n",
  101. dev->name, PKTBUFSRX, TX_BUF_CNT);
  102. /* Initialize the Rx Buffer descriptors */
  103. for (i = 0; i < PKTBUFSRX; i++) {
  104. rx_dp = &cdmac_bd.rx[i];
  105. memset(rx_dp, 0, sizeof(*rx_dp));
  106. rx_dp->next_p = rx_dp;
  107. rx_dp->buf_len = PKTSIZE_ALIGN;
  108. rx_dp->phys_buf_p = (u8 *)net_rx_packets[i];
  109. flush_cache((u32)rx_dp->phys_buf_p, PKTSIZE_ALIGN);
  110. }
  111. flush_cache((u32)cdmac_bd.rx, sizeof(cdmac_bd.rx));
  112. /* Initialize the TX Buffer Descriptors */
  113. for (i = 0; i < TX_BUF_CNT; i++) {
  114. tx_dp = &cdmac_bd.tx[i];
  115. memset(tx_dp, 0, sizeof(*tx_dp));
  116. tx_dp->next_p = tx_dp;
  117. }
  118. flush_cache((u32)cdmac_bd.tx, sizeof(cdmac_bd.tx));
  119. /* Reset index counter to the Rx and Tx Buffer descriptors */
  120. rx_idx = tx_idx = 0;
  121. /* initial Rx DMA start by writing to respective TAILDESC_PTR */
  122. ll_temac->out32(ra[RX_CURDESC_PTR], (int)&cdmac_bd.rx[rx_idx]);
  123. ll_temac->out32(ra[RX_TAILDESC_PTR], (int)&cdmac_bd.rx[rx_idx]);
  124. return 0;
  125. }
  126. int ll_temac_halt_sdma(struct eth_device *dev)
  127. {
  128. unsigned timeout = 50; /* 1usec * 50 = 50usec */
  129. struct ll_temac *ll_temac = dev->priv;
  130. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  131. /*
  132. * Soft reset the DMA
  133. *
  134. * Quote from MPMC documentation: Writing a 1 to this field
  135. * forces the DMA engine to shutdown and reset itself. After
  136. * setting this bit, software must poll it until the bit is
  137. * cleared by the DMA. This indicates that the reset process
  138. * is done and the pipeline has been flushed.
  139. */
  140. ll_temac->out32(ra[DMA_CONTROL_REG], DMA_CONTROL_RESET);
  141. while (timeout && (ll_temac->in32(ra[DMA_CONTROL_REG])
  142. & DMA_CONTROL_RESET)) {
  143. timeout--;
  144. udelay(1);
  145. }
  146. if (!timeout) {
  147. printf("%s: Timeout\n", __func__);
  148. return -1;
  149. }
  150. return 0;
  151. }
  152. int ll_temac_reset_sdma(struct eth_device *dev)
  153. {
  154. u32 r;
  155. struct ll_temac *ll_temac = dev->priv;
  156. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  157. /* Soft reset the DMA. */
  158. if (ll_temac_halt_sdma(dev))
  159. return -1;
  160. /* Now clear the interrupts. */
  161. r = ll_temac->in32(ra[TX_CHNL_CTRL]);
  162. r &= ~CHNL_CTRL_IRQ_MASK;
  163. ll_temac->out32(ra[TX_CHNL_CTRL], r);
  164. r = ll_temac->in32(ra[RX_CHNL_CTRL]);
  165. r &= ~CHNL_CTRL_IRQ_MASK;
  166. ll_temac->out32(ra[RX_CHNL_CTRL], r);
  167. /* Now ACK pending IRQs. */
  168. ll_temac->out32(ra[TX_IRQ_REG], IRQ_REG_IRQ_MASK);
  169. ll_temac->out32(ra[RX_IRQ_REG], IRQ_REG_IRQ_MASK);
  170. /* Set tail-ptr mode, disable errors for both channels. */
  171. ll_temac->out32(ra[DMA_CONTROL_REG],
  172. /* Enable use of tail pointer register */
  173. DMA_CONTROL_TPE |
  174. /* Disable error when 2 or 4 bit coalesce cnt overfl */
  175. DMA_CONTROL_RXOCEID |
  176. /* Disable error when 2 or 4 bit coalesce cnt overfl */
  177. DMA_CONTROL_TXOCEID);
  178. return 0;
  179. }
  180. int ll_temac_recv_sdma(struct eth_device *dev)
  181. {
  182. int length, pb_idx;
  183. struct cdmac_bd *rx_dp = &cdmac_bd.rx[rx_idx];
  184. struct ll_temac *ll_temac = dev->priv;
  185. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  186. if (ll_temac_sdma_error(dev)) {
  187. if (ll_temac_reset_sdma(dev))
  188. return -1;
  189. ll_temac_init_sdma(dev);
  190. }
  191. flush_cache((u32)rx_dp, sizeof(*rx_dp));
  192. if (!(rx_dp->sca.stctrl & CDMAC_BD_STCTRL_COMPLETED))
  193. return 0;
  194. if (rx_dp->sca.stctrl & (CDMAC_BD_STCTRL_SOP | CDMAC_BD_STCTRL_EOP)) {
  195. pb_idx = rx_idx;
  196. length = rx_dp->sca.app[4] & CDMAC_BD_APP4_RXBYTECNT_MASK;
  197. } else {
  198. pb_idx = -1;
  199. length = 0;
  200. printf("%s: Got part of package, unsupported (%x)\n",
  201. __func__, rx_dp->sca.stctrl);
  202. }
  203. /* flip the buffer */
  204. flush_cache((u32)rx_dp->phys_buf_p, length);
  205. /* reset the current descriptor */
  206. rx_dp->sca.stctrl = 0;
  207. rx_dp->sca.app[4] = 0;
  208. flush_cache((u32)rx_dp, sizeof(*rx_dp));
  209. /* Find next empty buffer descriptor, preparation for next iteration */
  210. rx_idx = (rx_idx + 1) % PKTBUFSRX;
  211. rx_dp = &cdmac_bd.rx[rx_idx];
  212. flush_cache((u32)rx_dp, sizeof(*rx_dp));
  213. /* DMA start by writing to respective TAILDESC_PTR */
  214. ll_temac->out32(ra[RX_CURDESC_PTR], (int)&cdmac_bd.rx[rx_idx]);
  215. ll_temac->out32(ra[RX_TAILDESC_PTR], (int)&cdmac_bd.rx[rx_idx]);
  216. if (length > 0 && pb_idx != -1)
  217. net_process_received_packet(net_rx_packets[pb_idx], length);
  218. return 0;
  219. }
  220. int ll_temac_send_sdma(struct eth_device *dev, void *packet, int length)
  221. {
  222. unsigned timeout = 50; /* 1usec * 50 = 50usec */
  223. struct cdmac_bd *tx_dp = &cdmac_bd.tx[tx_idx];
  224. struct ll_temac *ll_temac = dev->priv;
  225. phys_addr_t *ra = ll_temac->sdma_reg_addr;
  226. if (ll_temac_sdma_error(dev)) {
  227. if (ll_temac_reset_sdma(dev))
  228. return -1;
  229. ll_temac_init_sdma(dev);
  230. }
  231. tx_dp->phys_buf_p = (u8 *)packet;
  232. tx_dp->buf_len = length;
  233. tx_dp->sca.stctrl = CDMAC_BD_STCTRL_SOP | CDMAC_BD_STCTRL_EOP |
  234. CDMAC_BD_STCTRL_STOP_ON_END;
  235. flush_cache((u32)packet, length);
  236. flush_cache((u32)tx_dp, sizeof(*tx_dp));
  237. /* DMA start by writing to respective TAILDESC_PTR */
  238. ll_temac->out32(ra[TX_CURDESC_PTR], (int)tx_dp);
  239. ll_temac->out32(ra[TX_TAILDESC_PTR], (int)tx_dp);
  240. /* Find next empty buffer descriptor, preparation for next iteration */
  241. tx_idx = (tx_idx + 1) % TX_BUF_CNT;
  242. tx_dp = &cdmac_bd.tx[tx_idx];
  243. do {
  244. flush_cache((u32)tx_dp, sizeof(*tx_dp));
  245. udelay(1);
  246. } while (timeout-- && !(tx_dp->sca.stctrl & CDMAC_BD_STCTRL_COMPLETED));
  247. if (!timeout) {
  248. printf("%s: Timeout\n", __func__);
  249. return -1;
  250. }
  251. return 0;
  252. }