dwmac4_lib.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2007-2015 STMicroelectronics Ltd
  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. * Author: Alexandre Torgue <alexandre.torgue@st.com>
  9. */
  10. #include <linux/io.h>
  11. #include <linux/delay.h>
  12. #include "common.h"
  13. #include "dwmac4_dma.h"
  14. #include "dwmac4.h"
  15. int dwmac4_dma_reset(void __iomem *ioaddr)
  16. {
  17. u32 value = readl(ioaddr + DMA_BUS_MODE);
  18. int limit;
  19. /* DMA SW reset */
  20. value |= DMA_BUS_MODE_SFT_RESET;
  21. writel(value, ioaddr + DMA_BUS_MODE);
  22. limit = 10;
  23. while (limit--) {
  24. if (!(readl(ioaddr + DMA_BUS_MODE) & DMA_BUS_MODE_SFT_RESET))
  25. break;
  26. mdelay(10);
  27. }
  28. if (limit < 0)
  29. return -EBUSY;
  30. return 0;
  31. }
  32. void dwmac4_set_rx_tail_ptr(void __iomem *ioaddr, u32 tail_ptr, u32 chan)
  33. {
  34. writel(tail_ptr, ioaddr + DMA_CHAN_RX_END_ADDR(0));
  35. }
  36. void dwmac4_set_tx_tail_ptr(void __iomem *ioaddr, u32 tail_ptr, u32 chan)
  37. {
  38. writel(tail_ptr, ioaddr + DMA_CHAN_TX_END_ADDR(0));
  39. }
  40. void dwmac4_dma_start_tx(void __iomem *ioaddr)
  41. {
  42. u32 value = readl(ioaddr + DMA_CHAN_TX_CONTROL(STMMAC_CHAN0));
  43. value |= DMA_CONTROL_ST;
  44. writel(value, ioaddr + DMA_CHAN_TX_CONTROL(STMMAC_CHAN0));
  45. value = readl(ioaddr + GMAC_CONFIG);
  46. value |= GMAC_CONFIG_TE;
  47. writel(value, ioaddr + GMAC_CONFIG);
  48. }
  49. void dwmac4_dma_stop_tx(void __iomem *ioaddr)
  50. {
  51. u32 value = readl(ioaddr + DMA_CHAN_TX_CONTROL(STMMAC_CHAN0));
  52. value &= ~DMA_CONTROL_ST;
  53. writel(value, ioaddr + DMA_CHAN_TX_CONTROL(STMMAC_CHAN0));
  54. value = readl(ioaddr + GMAC_CONFIG);
  55. value &= ~GMAC_CONFIG_TE;
  56. writel(value, ioaddr + GMAC_CONFIG);
  57. }
  58. void dwmac4_dma_start_rx(void __iomem *ioaddr)
  59. {
  60. u32 value = readl(ioaddr + DMA_CHAN_RX_CONTROL(STMMAC_CHAN0));
  61. value |= DMA_CONTROL_SR;
  62. writel(value, ioaddr + DMA_CHAN_RX_CONTROL(STMMAC_CHAN0));
  63. value = readl(ioaddr + GMAC_CONFIG);
  64. value |= GMAC_CONFIG_RE;
  65. writel(value, ioaddr + GMAC_CONFIG);
  66. }
  67. void dwmac4_dma_stop_rx(void __iomem *ioaddr)
  68. {
  69. u32 value = readl(ioaddr + DMA_CHAN_RX_CONTROL(STMMAC_CHAN0));
  70. value &= ~DMA_CONTROL_SR;
  71. writel(value, ioaddr + DMA_CHAN_RX_CONTROL(STMMAC_CHAN0));
  72. value = readl(ioaddr + GMAC_CONFIG);
  73. value &= ~GMAC_CONFIG_RE;
  74. writel(value, ioaddr + GMAC_CONFIG);
  75. }
  76. void dwmac4_set_tx_ring_len(void __iomem *ioaddr, u32 len)
  77. {
  78. writel(len, ioaddr + DMA_CHAN_TX_RING_LEN(STMMAC_CHAN0));
  79. }
  80. void dwmac4_set_rx_ring_len(void __iomem *ioaddr, u32 len)
  81. {
  82. writel(len, ioaddr + DMA_CHAN_RX_RING_LEN(STMMAC_CHAN0));
  83. }
  84. void dwmac4_enable_dma_irq(void __iomem *ioaddr)
  85. {
  86. writel(DMA_CHAN_INTR_DEFAULT_MASK, ioaddr +
  87. DMA_CHAN_INTR_ENA(STMMAC_CHAN0));
  88. }
  89. void dwmac410_enable_dma_irq(void __iomem *ioaddr)
  90. {
  91. writel(DMA_CHAN_INTR_DEFAULT_MASK_4_10,
  92. ioaddr + DMA_CHAN_INTR_ENA(STMMAC_CHAN0));
  93. }
  94. void dwmac4_disable_dma_irq(void __iomem *ioaddr)
  95. {
  96. writel(0, ioaddr + DMA_CHAN_INTR_ENA(STMMAC_CHAN0));
  97. }
  98. int dwmac4_dma_interrupt(void __iomem *ioaddr,
  99. struct stmmac_extra_stats *x)
  100. {
  101. int ret = 0;
  102. u32 intr_status = readl(ioaddr + DMA_CHAN_STATUS(0));
  103. /* ABNORMAL interrupts */
  104. if (unlikely(intr_status & DMA_CHAN_STATUS_AIS)) {
  105. if (unlikely(intr_status & DMA_CHAN_STATUS_RBU))
  106. x->rx_buf_unav_irq++;
  107. if (unlikely(intr_status & DMA_CHAN_STATUS_RPS))
  108. x->rx_process_stopped_irq++;
  109. if (unlikely(intr_status & DMA_CHAN_STATUS_RWT))
  110. x->rx_watchdog_irq++;
  111. if (unlikely(intr_status & DMA_CHAN_STATUS_ETI))
  112. x->tx_early_irq++;
  113. if (unlikely(intr_status & DMA_CHAN_STATUS_TPS)) {
  114. x->tx_process_stopped_irq++;
  115. ret = tx_hard_error;
  116. }
  117. if (unlikely(intr_status & DMA_CHAN_STATUS_FBE)) {
  118. x->fatal_bus_error_irq++;
  119. ret = tx_hard_error;
  120. }
  121. }
  122. /* TX/RX NORMAL interrupts */
  123. if (likely(intr_status & DMA_CHAN_STATUS_NIS)) {
  124. x->normal_irq_n++;
  125. if (likely(intr_status & DMA_CHAN_STATUS_RI)) {
  126. u32 value;
  127. value = readl(ioaddr + DMA_CHAN_INTR_ENA(STMMAC_CHAN0));
  128. /* to schedule NAPI on real RIE event. */
  129. if (likely(value & DMA_CHAN_INTR_ENA_RIE)) {
  130. x->rx_normal_irq_n++;
  131. ret |= handle_rx;
  132. }
  133. }
  134. if (likely(intr_status & DMA_CHAN_STATUS_TI)) {
  135. x->tx_normal_irq_n++;
  136. ret |= handle_tx;
  137. }
  138. if (unlikely(intr_status & DMA_CHAN_STATUS_ERI))
  139. x->rx_early_irq++;
  140. }
  141. /* Clear the interrupt by writing a logic 1 to the chanX interrupt
  142. * status [21-0] expect reserved bits [5-3]
  143. */
  144. writel((intr_status & 0x3fffc7),
  145. ioaddr + DMA_CHAN_STATUS(STMMAC_CHAN0));
  146. return ret;
  147. }
  148. void stmmac_dwmac4_set_mac_addr(void __iomem *ioaddr, u8 addr[6],
  149. unsigned int high, unsigned int low)
  150. {
  151. unsigned long data;
  152. data = (addr[5] << 8) | addr[4];
  153. /* For MAC Addr registers se have to set the Address Enable (AE)
  154. * bit that has no effect on the High Reg 0 where the bit 31 (MO)
  155. * is RO.
  156. */
  157. data |= (STMMAC_CHAN0 << GMAC_HI_DCS_SHIFT);
  158. writel(data | GMAC_HI_REG_AE, ioaddr + high);
  159. data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  160. writel(data, ioaddr + low);
  161. }
  162. /* Enable disable MAC RX/TX */
  163. void stmmac_dwmac4_set_mac(void __iomem *ioaddr, bool enable)
  164. {
  165. u32 value = readl(ioaddr + GMAC_CONFIG);
  166. if (enable)
  167. value |= GMAC_CONFIG_RE | GMAC_CONFIG_TE;
  168. else
  169. value &= ~(GMAC_CONFIG_TE | GMAC_CONFIG_RE);
  170. writel(value, ioaddr + GMAC_CONFIG);
  171. }
  172. void stmmac_dwmac4_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
  173. unsigned int high, unsigned int low)
  174. {
  175. unsigned int hi_addr, lo_addr;
  176. /* Read the MAC address from the hardware */
  177. hi_addr = readl(ioaddr + high);
  178. lo_addr = readl(ioaddr + low);
  179. /* Extract the MAC address from the high and low words */
  180. addr[0] = lo_addr & 0xff;
  181. addr[1] = (lo_addr >> 8) & 0xff;
  182. addr[2] = (lo_addr >> 16) & 0xff;
  183. addr[3] = (lo_addr >> 24) & 0xff;
  184. addr[4] = hi_addr & 0xff;
  185. addr[5] = (hi_addr >> 8) & 0xff;
  186. }