dwmac_lib.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*******************************************************************************
  2. Copyright (C) 2007-2009 STMicroelectronics Ltd
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms and conditions of the GNU General Public License,
  5. version 2, as published by the Free Software Foundation.
  6. This program is distributed in the hope it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  9. more details.
  10. You should have received a copy of the GNU General Public License along with
  11. this program; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  13. The full GNU General Public License is included in this distribution in
  14. the file called "COPYING".
  15. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  16. *******************************************************************************/
  17. #include <linux/io.h>
  18. #include "common.h"
  19. #include "dwmac_dma.h"
  20. #define GMAC_HI_REG_AE 0x80000000
  21. /* CSR1 enables the transmit DMA to check for new descriptor */
  22. void dwmac_enable_dma_transmission(void __iomem *ioaddr)
  23. {
  24. writel(1, ioaddr + DMA_XMT_POLL_DEMAND);
  25. }
  26. void dwmac_enable_dma_irq(void __iomem *ioaddr)
  27. {
  28. writel(DMA_INTR_DEFAULT_MASK, ioaddr + DMA_INTR_ENA);
  29. }
  30. void dwmac_disable_dma_irq(void __iomem *ioaddr)
  31. {
  32. writel(0, ioaddr + DMA_INTR_ENA);
  33. }
  34. void dwmac_dma_start_tx(void __iomem *ioaddr)
  35. {
  36. u32 value = readl(ioaddr + DMA_CONTROL);
  37. value |= DMA_CONTROL_ST;
  38. writel(value, ioaddr + DMA_CONTROL);
  39. }
  40. void dwmac_dma_stop_tx(void __iomem *ioaddr)
  41. {
  42. u32 value = readl(ioaddr + DMA_CONTROL);
  43. value &= ~DMA_CONTROL_ST;
  44. writel(value, ioaddr + DMA_CONTROL);
  45. }
  46. void dwmac_dma_start_rx(void __iomem *ioaddr)
  47. {
  48. u32 value = readl(ioaddr + DMA_CONTROL);
  49. value |= DMA_CONTROL_SR;
  50. writel(value, ioaddr + DMA_CONTROL);
  51. }
  52. void dwmac_dma_stop_rx(void __iomem *ioaddr)
  53. {
  54. u32 value = readl(ioaddr + DMA_CONTROL);
  55. value &= ~DMA_CONTROL_SR;
  56. writel(value, ioaddr + DMA_CONTROL);
  57. }
  58. #ifdef DWMAC_DMA_DEBUG
  59. static void show_tx_process_state(unsigned int status)
  60. {
  61. unsigned int state;
  62. state = (status & DMA_STATUS_TS_MASK) >> DMA_STATUS_TS_SHIFT;
  63. switch (state) {
  64. case 0:
  65. pr_debug("- TX (Stopped): Reset or Stop command\n");
  66. break;
  67. case 1:
  68. pr_debug("- TX (Running):Fetching the Tx desc\n");
  69. break;
  70. case 2:
  71. pr_debug("- TX (Running): Waiting for end of tx\n");
  72. break;
  73. case 3:
  74. pr_debug("- TX (Running): Reading the data "
  75. "and queuing the data into the Tx buf\n");
  76. break;
  77. case 6:
  78. pr_debug("- TX (Suspended): Tx Buff Underflow "
  79. "or an unavailable Transmit descriptor\n");
  80. break;
  81. case 7:
  82. pr_debug("- TX (Running): Closing Tx descriptor\n");
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. static void show_rx_process_state(unsigned int status)
  89. {
  90. unsigned int state;
  91. state = (status & DMA_STATUS_RS_MASK) >> DMA_STATUS_RS_SHIFT;
  92. switch (state) {
  93. case 0:
  94. pr_debug("- RX (Stopped): Reset or Stop command\n");
  95. break;
  96. case 1:
  97. pr_debug("- RX (Running): Fetching the Rx desc\n");
  98. break;
  99. case 2:
  100. pr_debug("- RX (Running):Checking for end of pkt\n");
  101. break;
  102. case 3:
  103. pr_debug("- RX (Running): Waiting for Rx pkt\n");
  104. break;
  105. case 4:
  106. pr_debug("- RX (Suspended): Unavailable Rx buf\n");
  107. break;
  108. case 5:
  109. pr_debug("- RX (Running): Closing Rx descriptor\n");
  110. break;
  111. case 6:
  112. pr_debug("- RX(Running): Flushing the current frame"
  113. " from the Rx buf\n");
  114. break;
  115. case 7:
  116. pr_debug("- RX (Running): Queuing the Rx frame"
  117. " from the Rx buf into memory\n");
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. #endif
  124. int dwmac_dma_interrupt(void __iomem *ioaddr,
  125. struct stmmac_extra_stats *x)
  126. {
  127. int ret = 0;
  128. /* read the status register (CSR5) */
  129. u32 intr_status = readl(ioaddr + DMA_STATUS);
  130. #ifdef DWMAC_DMA_DEBUG
  131. /* Enable it to monitor DMA rx/tx status in case of critical problems */
  132. pr_debug("%s: [CSR5: 0x%08x]\n", __func__, intr_status);
  133. show_tx_process_state(intr_status);
  134. show_rx_process_state(intr_status);
  135. #endif
  136. /* ABNORMAL interrupts */
  137. if (unlikely(intr_status & DMA_STATUS_AIS)) {
  138. if (unlikely(intr_status & DMA_STATUS_UNF)) {
  139. ret = tx_hard_error_bump_tc;
  140. x->tx_undeflow_irq++;
  141. }
  142. if (unlikely(intr_status & DMA_STATUS_TJT))
  143. x->tx_jabber_irq++;
  144. if (unlikely(intr_status & DMA_STATUS_OVF))
  145. x->rx_overflow_irq++;
  146. if (unlikely(intr_status & DMA_STATUS_RU))
  147. x->rx_buf_unav_irq++;
  148. if (unlikely(intr_status & DMA_STATUS_RPS))
  149. x->rx_process_stopped_irq++;
  150. if (unlikely(intr_status & DMA_STATUS_RWT))
  151. x->rx_watchdog_irq++;
  152. if (unlikely(intr_status & DMA_STATUS_ETI))
  153. x->tx_early_irq++;
  154. if (unlikely(intr_status & DMA_STATUS_TPS)) {
  155. x->tx_process_stopped_irq++;
  156. ret = tx_hard_error;
  157. }
  158. if (unlikely(intr_status & DMA_STATUS_FBI)) {
  159. x->fatal_bus_error_irq++;
  160. ret = tx_hard_error;
  161. }
  162. }
  163. /* TX/RX NORMAL interrupts */
  164. if (likely(intr_status & DMA_STATUS_NIS)) {
  165. x->normal_irq_n++;
  166. if (likely(intr_status & DMA_STATUS_RI)) {
  167. u32 value = readl(ioaddr + DMA_INTR_ENA);
  168. /* to schedule NAPI on real RIE event. */
  169. if (likely(value & DMA_INTR_ENA_RIE)) {
  170. x->rx_normal_irq_n++;
  171. ret |= handle_rx;
  172. }
  173. }
  174. if (likely(intr_status & DMA_STATUS_TI)) {
  175. x->tx_normal_irq_n++;
  176. ret |= handle_tx;
  177. }
  178. if (unlikely(intr_status & DMA_STATUS_ERI))
  179. x->rx_early_irq++;
  180. }
  181. /* Optional hardware blocks, interrupts should be disabled */
  182. if (unlikely(intr_status &
  183. (DMA_STATUS_GPI | DMA_STATUS_GMI | DMA_STATUS_GLI)))
  184. pr_warn("%s: unexpected status %08x\n", __func__, intr_status);
  185. /* Clear the interrupt by writing a logic 1 to the CSR5[15-0] */
  186. writel((intr_status & 0x1ffff), ioaddr + DMA_STATUS);
  187. return ret;
  188. }
  189. void dwmac_dma_flush_tx_fifo(void __iomem *ioaddr)
  190. {
  191. u32 csr6 = readl(ioaddr + DMA_CONTROL);
  192. writel((csr6 | DMA_CONTROL_FTF), ioaddr + DMA_CONTROL);
  193. do {} while ((readl(ioaddr + DMA_CONTROL) & DMA_CONTROL_FTF));
  194. }
  195. void stmmac_set_mac_addr(void __iomem *ioaddr, u8 addr[6],
  196. unsigned int high, unsigned int low)
  197. {
  198. unsigned long data;
  199. data = (addr[5] << 8) | addr[4];
  200. /* For MAC Addr registers se have to set the Address Enable (AE)
  201. * bit that has no effect on the High Reg 0 where the bit 31 (MO)
  202. * is RO.
  203. */
  204. writel(data | GMAC_HI_REG_AE, ioaddr + high);
  205. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  206. writel(data, ioaddr + low);
  207. }
  208. /* Enable disable MAC RX/TX */
  209. void stmmac_set_mac(void __iomem *ioaddr, bool enable)
  210. {
  211. u32 value = readl(ioaddr + MAC_CTRL_REG);
  212. if (enable)
  213. value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
  214. else
  215. value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
  216. writel(value, ioaddr + MAC_CTRL_REG);
  217. }
  218. void stmmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  219. unsigned int high, unsigned int low)
  220. {
  221. unsigned int hi_addr, lo_addr;
  222. /* Read the MAC address from the hardware */
  223. hi_addr = readl(ioaddr + high);
  224. lo_addr = readl(ioaddr + low);
  225. /* Extract the MAC address from the high and low words */
  226. addr[0] = lo_addr & 0xff;
  227. addr[1] = (lo_addr >> 8) & 0xff;
  228. addr[2] = (lo_addr >> 16) & 0xff;
  229. addr[3] = (lo_addr >> 24) & 0xff;
  230. addr[4] = hi_addr & 0xff;
  231. addr[5] = (hi_addr >> 8) & 0xff;
  232. }