altera_msgdma.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Altera TSE SGDMA and MSGDMA Linux driver
  3. * Copyright (C) 2014 Altera Corporation. All rights reserved
  4. */
  5. #include <linux/netdevice.h>
  6. #include "altera_utils.h"
  7. #include "altera_tse.h"
  8. #include "altera_msgdmahw.h"
  9. #include "altera_msgdma.h"
  10. /* No initialization work to do for MSGDMA */
  11. int msgdma_initialize(struct altera_tse_private *priv)
  12. {
  13. return 0;
  14. }
  15. void msgdma_uninitialize(struct altera_tse_private *priv)
  16. {
  17. }
  18. void msgdma_start_rxdma(struct altera_tse_private *priv)
  19. {
  20. }
  21. void msgdma_reset(struct altera_tse_private *priv)
  22. {
  23. int counter;
  24. /* Reset Rx mSGDMA */
  25. csrwr32(MSGDMA_CSR_STAT_MASK, priv->rx_dma_csr,
  26. msgdma_csroffs(status));
  27. csrwr32(MSGDMA_CSR_CTL_RESET, priv->rx_dma_csr,
  28. msgdma_csroffs(control));
  29. counter = 0;
  30. while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
  31. if (tse_bit_is_clear(priv->rx_dma_csr, msgdma_csroffs(status),
  32. MSGDMA_CSR_STAT_RESETTING))
  33. break;
  34. udelay(1);
  35. }
  36. if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR)
  37. netif_warn(priv, drv, priv->dev,
  38. "TSE Rx mSGDMA resetting bit never cleared!\n");
  39. /* clear all status bits */
  40. csrwr32(MSGDMA_CSR_STAT_MASK, priv->rx_dma_csr, msgdma_csroffs(status));
  41. /* Reset Tx mSGDMA */
  42. csrwr32(MSGDMA_CSR_STAT_MASK, priv->tx_dma_csr,
  43. msgdma_csroffs(status));
  44. csrwr32(MSGDMA_CSR_CTL_RESET, priv->tx_dma_csr,
  45. msgdma_csroffs(control));
  46. counter = 0;
  47. while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
  48. if (tse_bit_is_clear(priv->tx_dma_csr, msgdma_csroffs(status),
  49. MSGDMA_CSR_STAT_RESETTING))
  50. break;
  51. udelay(1);
  52. }
  53. if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR)
  54. netif_warn(priv, drv, priv->dev,
  55. "TSE Tx mSGDMA resetting bit never cleared!\n");
  56. /* clear all status bits */
  57. csrwr32(MSGDMA_CSR_STAT_MASK, priv->tx_dma_csr, msgdma_csroffs(status));
  58. }
  59. void msgdma_disable_rxirq(struct altera_tse_private *priv)
  60. {
  61. tse_clear_bit(priv->rx_dma_csr, msgdma_csroffs(control),
  62. MSGDMA_CSR_CTL_GLOBAL_INTR);
  63. }
  64. void msgdma_enable_rxirq(struct altera_tse_private *priv)
  65. {
  66. tse_set_bit(priv->rx_dma_csr, msgdma_csroffs(control),
  67. MSGDMA_CSR_CTL_GLOBAL_INTR);
  68. }
  69. void msgdma_disable_txirq(struct altera_tse_private *priv)
  70. {
  71. tse_clear_bit(priv->tx_dma_csr, msgdma_csroffs(control),
  72. MSGDMA_CSR_CTL_GLOBAL_INTR);
  73. }
  74. void msgdma_enable_txirq(struct altera_tse_private *priv)
  75. {
  76. tse_set_bit(priv->tx_dma_csr, msgdma_csroffs(control),
  77. MSGDMA_CSR_CTL_GLOBAL_INTR);
  78. }
  79. void msgdma_clear_rxirq(struct altera_tse_private *priv)
  80. {
  81. csrwr32(MSGDMA_CSR_STAT_IRQ, priv->rx_dma_csr, msgdma_csroffs(status));
  82. }
  83. void msgdma_clear_txirq(struct altera_tse_private *priv)
  84. {
  85. csrwr32(MSGDMA_CSR_STAT_IRQ, priv->tx_dma_csr, msgdma_csroffs(status));
  86. }
  87. /* return 0 to indicate transmit is pending */
  88. int msgdma_tx_buffer(struct altera_tse_private *priv, struct tse_buffer *buffer)
  89. {
  90. csrwr32(lower_32_bits(buffer->dma_addr), priv->tx_dma_desc,
  91. msgdma_descroffs(read_addr_lo));
  92. csrwr32(upper_32_bits(buffer->dma_addr), priv->tx_dma_desc,
  93. msgdma_descroffs(read_addr_hi));
  94. csrwr32(0, priv->tx_dma_desc, msgdma_descroffs(write_addr_lo));
  95. csrwr32(0, priv->tx_dma_desc, msgdma_descroffs(write_addr_hi));
  96. csrwr32(buffer->len, priv->tx_dma_desc, msgdma_descroffs(len));
  97. csrwr32(0, priv->tx_dma_desc, msgdma_descroffs(burst_seq_num));
  98. csrwr32(MSGDMA_DESC_TX_STRIDE, priv->tx_dma_desc,
  99. msgdma_descroffs(stride));
  100. csrwr32(MSGDMA_DESC_CTL_TX_SINGLE, priv->tx_dma_desc,
  101. msgdma_descroffs(control));
  102. return 0;
  103. }
  104. u32 msgdma_tx_completions(struct altera_tse_private *priv)
  105. {
  106. u32 ready = 0;
  107. u32 inuse;
  108. u32 status;
  109. /* Get number of sent descriptors */
  110. inuse = csrrd32(priv->tx_dma_csr, msgdma_csroffs(rw_fill_level))
  111. & 0xffff;
  112. if (inuse) { /* Tx FIFO is not empty */
  113. ready = max_t(int,
  114. priv->tx_prod - priv->tx_cons - inuse - 1, 0);
  115. } else {
  116. /* Check for buffered last packet */
  117. status = csrrd32(priv->tx_dma_csr, msgdma_csroffs(status));
  118. if (status & MSGDMA_CSR_STAT_BUSY)
  119. ready = priv->tx_prod - priv->tx_cons - 1;
  120. else
  121. ready = priv->tx_prod - priv->tx_cons;
  122. }
  123. return ready;
  124. }
  125. /* Put buffer to the mSGDMA RX FIFO
  126. */
  127. void msgdma_add_rx_desc(struct altera_tse_private *priv,
  128. struct tse_buffer *rxbuffer)
  129. {
  130. u32 len = priv->rx_dma_buf_sz;
  131. dma_addr_t dma_addr = rxbuffer->dma_addr;
  132. u32 control = (MSGDMA_DESC_CTL_END_ON_EOP
  133. | MSGDMA_DESC_CTL_END_ON_LEN
  134. | MSGDMA_DESC_CTL_TR_COMP_IRQ
  135. | MSGDMA_DESC_CTL_EARLY_IRQ
  136. | MSGDMA_DESC_CTL_TR_ERR_IRQ
  137. | MSGDMA_DESC_CTL_GO);
  138. csrwr32(0, priv->rx_dma_desc, msgdma_descroffs(read_addr_lo));
  139. csrwr32(0, priv->rx_dma_desc, msgdma_descroffs(read_addr_hi));
  140. csrwr32(lower_32_bits(dma_addr), priv->rx_dma_desc,
  141. msgdma_descroffs(write_addr_lo));
  142. csrwr32(upper_32_bits(dma_addr), priv->rx_dma_desc,
  143. msgdma_descroffs(write_addr_hi));
  144. csrwr32(len, priv->rx_dma_desc, msgdma_descroffs(len));
  145. csrwr32(0, priv->rx_dma_desc, msgdma_descroffs(burst_seq_num));
  146. csrwr32(0x00010001, priv->rx_dma_desc, msgdma_descroffs(stride));
  147. csrwr32(control, priv->rx_dma_desc, msgdma_descroffs(control));
  148. }
  149. /* status is returned on upper 16 bits,
  150. * length is returned in lower 16 bits
  151. */
  152. u32 msgdma_rx_status(struct altera_tse_private *priv)
  153. {
  154. u32 rxstatus = 0;
  155. u32 pktlength;
  156. u32 pktstatus;
  157. if (csrrd32(priv->rx_dma_csr, msgdma_csroffs(resp_fill_level))
  158. & 0xffff) {
  159. pktlength = csrrd32(priv->rx_dma_resp,
  160. msgdma_respoffs(bytes_transferred));
  161. pktstatus = csrrd32(priv->rx_dma_resp,
  162. msgdma_respoffs(status));
  163. rxstatus = pktstatus;
  164. rxstatus = rxstatus << 16;
  165. rxstatus |= (pktlength & 0xffff);
  166. }
  167. return rxstatus;
  168. }