altera_msgdma.c 6.0 KB

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