1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2015 STMicroelectronics Ltd
5 * Author: Alexandre Torgue <alexandre.torgue@st.com>
9 #include <linux/iopoll.h>
10 #include <linux/delay.h>
12 #include "dwmac4_dma.h"
16 int dwmac4_dma_reset(void __iomem *ioaddr)
18 u32 value = readl(ioaddr + DMA_BUS_MODE);
21 value |= DMA_BUS_MODE_SFT_RESET;
22 writel(value, ioaddr + DMA_BUS_MODE);
24 return readl_poll_timeout(ioaddr + DMA_BUS_MODE, value,
25 !(value & DMA_BUS_MODE_SFT_RESET),
29 void dwmac4_set_rx_tail_ptr(struct stmmac_priv *priv, void __iomem *ioaddr,
30 u32 tail_ptr, u32 chan)
32 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
34 writel(tail_ptr, ioaddr + DMA_CHAN_RX_END_ADDR(dwmac4_addrs, chan));
37 void dwmac4_set_tx_tail_ptr(struct stmmac_priv *priv, void __iomem *ioaddr,
38 u32 tail_ptr, u32 chan)
40 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
42 writel(tail_ptr, ioaddr + DMA_CHAN_TX_END_ADDR(dwmac4_addrs, chan));
45 void dwmac4_dma_start_tx(struct stmmac_priv *priv, void __iomem *ioaddr,
48 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
49 u32 value = readl(ioaddr + DMA_CHAN_TX_CONTROL(dwmac4_addrs, chan));
51 value |= DMA_CONTROL_ST;
52 writel(value, ioaddr + DMA_CHAN_TX_CONTROL(dwmac4_addrs, chan));
54 value = readl(ioaddr + GMAC_CONFIG);
55 value |= GMAC_CONFIG_TE;
56 writel(value, ioaddr + GMAC_CONFIG);
59 void dwmac4_dma_stop_tx(struct stmmac_priv *priv, void __iomem *ioaddr,
62 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
64 u32 value = readl(ioaddr + DMA_CHAN_TX_CONTROL(dwmac4_addrs, chan));
66 value &= ~DMA_CONTROL_ST;
67 writel(value, ioaddr + DMA_CHAN_TX_CONTROL(dwmac4_addrs, chan));
70 void dwmac4_dma_start_rx(struct stmmac_priv *priv, void __iomem *ioaddr,
73 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
75 u32 value = readl(ioaddr + DMA_CHAN_RX_CONTROL(dwmac4_addrs, chan));
77 value |= DMA_CONTROL_SR;
79 writel(value, ioaddr + DMA_CHAN_RX_CONTROL(dwmac4_addrs, chan));
81 value = readl(ioaddr + GMAC_CONFIG);
82 value |= GMAC_CONFIG_RE;
83 writel(value, ioaddr + GMAC_CONFIG);
86 void dwmac4_dma_stop_rx(struct stmmac_priv *priv, void __iomem *ioaddr,
89 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
90 u32 value = readl(ioaddr + DMA_CHAN_RX_CONTROL(dwmac4_addrs, chan));
92 value &= ~DMA_CONTROL_SR;
93 writel(value, ioaddr + DMA_CHAN_RX_CONTROL(dwmac4_addrs, chan));
96 void dwmac4_set_tx_ring_len(struct stmmac_priv *priv, void __iomem *ioaddr,
99 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
101 writel(len, ioaddr + DMA_CHAN_TX_RING_LEN(dwmac4_addrs, chan));
104 void dwmac4_set_rx_ring_len(struct stmmac_priv *priv, void __iomem *ioaddr,
107 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
109 writel(len, ioaddr + DMA_CHAN_RX_RING_LEN(dwmac4_addrs, chan));
112 void dwmac4_enable_dma_irq(struct stmmac_priv *priv, void __iomem *ioaddr,
113 u32 chan, bool rx, bool tx)
115 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
116 u32 value = readl(ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
119 value |= DMA_CHAN_INTR_DEFAULT_RX;
121 value |= DMA_CHAN_INTR_DEFAULT_TX;
123 writel(value, ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
126 void dwmac410_enable_dma_irq(struct stmmac_priv *priv, void __iomem *ioaddr,
127 u32 chan, bool rx, bool tx)
129 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
130 u32 value = readl(ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
133 value |= DMA_CHAN_INTR_DEFAULT_RX_4_10;
135 value |= DMA_CHAN_INTR_DEFAULT_TX_4_10;
137 writel(value, ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
140 void dwmac4_disable_dma_irq(struct stmmac_priv *priv, void __iomem *ioaddr,
141 u32 chan, bool rx, bool tx)
143 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
144 u32 value = readl(ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
147 value &= ~DMA_CHAN_INTR_DEFAULT_RX;
149 value &= ~DMA_CHAN_INTR_DEFAULT_TX;
151 writel(value, ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
154 void dwmac410_disable_dma_irq(struct stmmac_priv *priv, void __iomem *ioaddr,
155 u32 chan, bool rx, bool tx)
157 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
158 u32 value = readl(ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
161 value &= ~DMA_CHAN_INTR_DEFAULT_RX_4_10;
163 value &= ~DMA_CHAN_INTR_DEFAULT_TX_4_10;
165 writel(value, ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
168 int dwmac4_dma_interrupt(struct stmmac_priv *priv, void __iomem *ioaddr,
169 struct stmmac_extra_stats *x, u32 chan, u32 dir)
171 const struct dwmac4_addrs *dwmac4_addrs = priv->plat->dwmac4_addrs;
172 u32 intr_status = readl(ioaddr + DMA_CHAN_STATUS(dwmac4_addrs, chan));
173 u32 intr_en = readl(ioaddr + DMA_CHAN_INTR_ENA(dwmac4_addrs, chan));
174 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[chan];
175 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[chan];
178 if (dir == DMA_DIR_RX)
179 intr_status &= DMA_CHAN_STATUS_MSK_RX;
180 else if (dir == DMA_DIR_TX)
181 intr_status &= DMA_CHAN_STATUS_MSK_TX;
183 /* ABNORMAL interrupts */
184 if (unlikely(intr_status & DMA_CHAN_STATUS_AIS)) {
185 if (unlikely(intr_status & DMA_CHAN_STATUS_RBU))
186 x->rx_buf_unav_irq++;
187 if (unlikely(intr_status & DMA_CHAN_STATUS_RPS))
188 x->rx_process_stopped_irq++;
189 if (unlikely(intr_status & DMA_CHAN_STATUS_RWT))
190 x->rx_watchdog_irq++;
191 if (unlikely(intr_status & DMA_CHAN_STATUS_ETI))
193 if (unlikely(intr_status & DMA_CHAN_STATUS_TPS)) {
194 x->tx_process_stopped_irq++;
197 if (unlikely(intr_status & DMA_CHAN_STATUS_FBE)) {
198 x->fatal_bus_error_irq++;
202 /* TX/RX NORMAL interrupts */
203 if (likely(intr_status & DMA_CHAN_STATUS_RI)) {
204 u64_stats_update_begin(&rx_q->rxq_stats.syncp);
205 rx_q->rxq_stats.rx_normal_irq_n++;
206 u64_stats_update_end(&rx_q->rxq_stats.syncp);
209 if (likely(intr_status & DMA_CHAN_STATUS_TI)) {
210 u64_stats_update_begin(&tx_q->txq_stats.syncp);
211 tx_q->txq_stats.tx_normal_irq_n++;
212 u64_stats_update_end(&tx_q->txq_stats.syncp);
216 if (unlikely(intr_status & DMA_CHAN_STATUS_TBU))
218 if (unlikely(intr_status & DMA_CHAN_STATUS_ERI))
221 writel(intr_status & intr_en,
222 ioaddr + DMA_CHAN_STATUS(dwmac4_addrs, chan));
226 void stmmac_dwmac4_set_mac_addr(void __iomem *ioaddr, const u8 addr[6],
227 unsigned int high, unsigned int low)
231 data = (addr[5] << 8) | addr[4];
232 /* For MAC Addr registers se have to set the Address Enable (AE)
233 * bit that has no effect on the High Reg 0 where the bit 31 (MO)
236 data |= (STMMAC_CHAN0 << GMAC_HI_DCS_SHIFT);
237 writel(data | GMAC_HI_REG_AE, ioaddr + high);
238 data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
239 writel(data, ioaddr + low);
242 /* Enable disable MAC RX/TX */
243 void stmmac_dwmac4_set_mac(void __iomem *ioaddr, bool enable)
245 u32 value = readl(ioaddr + GMAC_CONFIG);
249 value |= GMAC_CONFIG_RE | GMAC_CONFIG_TE;
251 value &= ~(GMAC_CONFIG_TE | GMAC_CONFIG_RE);
253 if (value != old_val)
254 writel(value, ioaddr + GMAC_CONFIG);
257 void stmmac_dwmac4_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
258 unsigned int high, unsigned int low)
260 unsigned int hi_addr, lo_addr;
262 /* Read the MAC address from the hardware */
263 hi_addr = readl(ioaddr + high);
264 lo_addr = readl(ioaddr + low);
266 /* Extract the MAC address from the high and low words */
267 addr[0] = lo_addr & 0xff;
268 addr[1] = (lo_addr >> 8) & 0xff;
269 addr[2] = (lo_addr >> 16) & 0xff;
270 addr[3] = (lo_addr >> 24) & 0xff;
271 addr[4] = hi_addr & 0xff;
272 addr[5] = (hi_addr >> 8) & 0xff;