dwmac_lib.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. int dwmac_dma_reset(void __iomem *ioaddr)
  22. {
  23. u32 value = readl(ioaddr + DMA_BUS_MODE);
  24. int limit;
  25. /* DMA SW reset */
  26. value |= DMA_BUS_MODE_SFT_RESET;
  27. writel(value, ioaddr + DMA_BUS_MODE);
  28. limit = 10;
  29. while (limit--) {
  30. if (!(readl(ioaddr + DMA_BUS_MODE) & DMA_BUS_MODE_SFT_RESET))
  31. break;
  32. mdelay(10);
  33. }
  34. if (limit < 0)
  35. return -EBUSY;
  36. return 0;
  37. }
  38. /* CSR1 enables the transmit DMA to check for new descriptor */
  39. void dwmac_enable_dma_transmission(void __iomem *ioaddr)
  40. {
  41. writel(1, ioaddr + DMA_XMT_POLL_DEMAND);
  42. }
  43. void dwmac_enable_dma_irq(void __iomem *ioaddr)
  44. {
  45. writel(DMA_INTR_DEFAULT_MASK, ioaddr + DMA_INTR_ENA);
  46. }
  47. void dwmac_disable_dma_irq(void __iomem *ioaddr)
  48. {
  49. writel(0, ioaddr + DMA_INTR_ENA);
  50. }
  51. void dwmac_dma_start_tx(void __iomem *ioaddr)
  52. {
  53. u32 value = readl(ioaddr + DMA_CONTROL);
  54. value |= DMA_CONTROL_ST;
  55. writel(value, ioaddr + DMA_CONTROL);
  56. }
  57. void dwmac_dma_stop_tx(void __iomem *ioaddr)
  58. {
  59. u32 value = readl(ioaddr + DMA_CONTROL);
  60. value &= ~DMA_CONTROL_ST;
  61. writel(value, ioaddr + DMA_CONTROL);
  62. }
  63. void dwmac_dma_start_rx(void __iomem *ioaddr)
  64. {
  65. u32 value = readl(ioaddr + DMA_CONTROL);
  66. value |= DMA_CONTROL_SR;
  67. writel(value, ioaddr + DMA_CONTROL);
  68. }
  69. void dwmac_dma_stop_rx(void __iomem *ioaddr)
  70. {
  71. u32 value = readl(ioaddr + DMA_CONTROL);
  72. value &= ~DMA_CONTROL_SR;
  73. writel(value, ioaddr + DMA_CONTROL);
  74. }
  75. #ifdef DWMAC_DMA_DEBUG
  76. static void show_tx_process_state(unsigned int status)
  77. {
  78. unsigned int state;
  79. state = (status & DMA_STATUS_TS_MASK) >> DMA_STATUS_TS_SHIFT;
  80. switch (state) {
  81. case 0:
  82. pr_debug("- TX (Stopped): Reset or Stop command\n");
  83. break;
  84. case 1:
  85. pr_debug("- TX (Running):Fetching the Tx desc\n");
  86. break;
  87. case 2:
  88. pr_debug("- TX (Running): Waiting for end of tx\n");
  89. break;
  90. case 3:
  91. pr_debug("- TX (Running): Reading the data "
  92. "and queuing the data into the Tx buf\n");
  93. break;
  94. case 6:
  95. pr_debug("- TX (Suspended): Tx Buff Underflow "
  96. "or an unavailable Transmit descriptor\n");
  97. break;
  98. case 7:
  99. pr_debug("- TX (Running): Closing Tx descriptor\n");
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. static void show_rx_process_state(unsigned int status)
  106. {
  107. unsigned int state;
  108. state = (status & DMA_STATUS_RS_MASK) >> DMA_STATUS_RS_SHIFT;
  109. switch (state) {
  110. case 0:
  111. pr_debug("- RX (Stopped): Reset or Stop command\n");
  112. break;
  113. case 1:
  114. pr_debug("- RX (Running): Fetching the Rx desc\n");
  115. break;
  116. case 2:
  117. pr_debug("- RX (Running):Checking for end of pkt\n");
  118. break;
  119. case 3:
  120. pr_debug("- RX (Running): Waiting for Rx pkt\n");
  121. break;
  122. case 4:
  123. pr_debug("- RX (Suspended): Unavailable Rx buf\n");
  124. break;
  125. case 5:
  126. pr_debug("- RX (Running): Closing Rx descriptor\n");
  127. break;
  128. case 6:
  129. pr_debug("- RX(Running): Flushing the current frame"
  130. " from the Rx buf\n");
  131. break;
  132. case 7:
  133. pr_debug("- RX (Running): Queuing the Rx frame"
  134. " from the Rx buf into memory\n");
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. #endif
  141. int dwmac_dma_interrupt(void __iomem *ioaddr,
  142. struct stmmac_extra_stats *x)
  143. {
  144. int ret = 0;
  145. /* read the status register (CSR5) */
  146. u32 intr_status = readl(ioaddr + DMA_STATUS);
  147. #ifdef DWMAC_DMA_DEBUG
  148. /* Enable it to monitor DMA rx/tx status in case of critical problems */
  149. pr_debug("%s: [CSR5: 0x%08x]\n", __func__, intr_status);
  150. show_tx_process_state(intr_status);
  151. show_rx_process_state(intr_status);
  152. #endif
  153. /* ABNORMAL interrupts */
  154. if (unlikely(intr_status & DMA_STATUS_AIS)) {
  155. if (unlikely(intr_status & DMA_STATUS_UNF)) {
  156. ret = tx_hard_error_bump_tc;
  157. x->tx_undeflow_irq++;
  158. }
  159. if (unlikely(intr_status & DMA_STATUS_TJT))
  160. x->tx_jabber_irq++;
  161. if (unlikely(intr_status & DMA_STATUS_OVF))
  162. x->rx_overflow_irq++;
  163. if (unlikely(intr_status & DMA_STATUS_RU))
  164. x->rx_buf_unav_irq++;
  165. if (unlikely(intr_status & DMA_STATUS_RPS))
  166. x->rx_process_stopped_irq++;
  167. if (unlikely(intr_status & DMA_STATUS_RWT))
  168. x->rx_watchdog_irq++;
  169. if (unlikely(intr_status & DMA_STATUS_ETI))
  170. x->tx_early_irq++;
  171. if (unlikely(intr_status & DMA_STATUS_TPS)) {
  172. x->tx_process_stopped_irq++;
  173. ret = tx_hard_error;
  174. }
  175. if (unlikely(intr_status & DMA_STATUS_FBI)) {
  176. x->fatal_bus_error_irq++;
  177. ret = tx_hard_error;
  178. }
  179. }
  180. /* TX/RX NORMAL interrupts */
  181. if (likely(intr_status & DMA_STATUS_NIS)) {
  182. x->normal_irq_n++;
  183. if (likely(intr_status & DMA_STATUS_RI)) {
  184. u32 value = readl(ioaddr + DMA_INTR_ENA);
  185. /* to schedule NAPI on real RIE event. */
  186. if (likely(value & DMA_INTR_ENA_RIE)) {
  187. x->rx_normal_irq_n++;
  188. ret |= handle_rx;
  189. }
  190. }
  191. if (likely(intr_status & DMA_STATUS_TI)) {
  192. x->tx_normal_irq_n++;
  193. ret |= handle_tx;
  194. }
  195. if (unlikely(intr_status & DMA_STATUS_ERI))
  196. x->rx_early_irq++;
  197. }
  198. /* Optional hardware blocks, interrupts should be disabled */
  199. if (unlikely(intr_status &
  200. (DMA_STATUS_GPI | DMA_STATUS_GMI | DMA_STATUS_GLI)))
  201. pr_warn("%s: unexpected status %08x\n", __func__, intr_status);
  202. /* Clear the interrupt by writing a logic 1 to the CSR5[15-0] */
  203. writel((intr_status & 0x1ffff), ioaddr + DMA_STATUS);
  204. return ret;
  205. }
  206. void dwmac_dma_flush_tx_fifo(void __iomem *ioaddr)
  207. {
  208. u32 csr6 = readl(ioaddr + DMA_CONTROL);
  209. writel((csr6 | DMA_CONTROL_FTF), ioaddr + DMA_CONTROL);
  210. do {} while ((readl(ioaddr + DMA_CONTROL) & DMA_CONTROL_FTF));
  211. }
  212. void stmmac_set_mac_addr(void __iomem *ioaddr, u8 addr[6],
  213. unsigned int high, unsigned int low)
  214. {
  215. unsigned long data;
  216. data = (addr[5] << 8) | addr[4];
  217. /* For MAC Addr registers se have to set the Address Enable (AE)
  218. * bit that has no effect on the High Reg 0 where the bit 31 (MO)
  219. * is RO.
  220. */
  221. writel(data | GMAC_HI_REG_AE, ioaddr + high);
  222. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  223. writel(data, ioaddr + low);
  224. }
  225. /* Enable disable MAC RX/TX */
  226. void stmmac_set_mac(void __iomem *ioaddr, bool enable)
  227. {
  228. u32 value = readl(ioaddr + MAC_CTRL_REG);
  229. if (enable)
  230. value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
  231. else
  232. value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
  233. writel(value, ioaddr + MAC_CTRL_REG);
  234. }
  235. void stmmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  236. unsigned int high, unsigned int low)
  237. {
  238. unsigned int hi_addr, lo_addr;
  239. /* Read the MAC address from the hardware */
  240. hi_addr = readl(ioaddr + high);
  241. lo_addr = readl(ioaddr + low);
  242. /* Extract the MAC address from the high and low words */
  243. addr[0] = lo_addr & 0xff;
  244. addr[1] = (lo_addr >> 8) & 0xff;
  245. addr[2] = (lo_addr >> 16) & 0xff;
  246. addr[3] = (lo_addr >> 24) & 0xff;
  247. addr[4] = hi_addr & 0xff;
  248. addr[5] = (hi_addr >> 8) & 0xff;
  249. }