1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3 Copyright (C) 2007-2009 STMicroelectronics Ltd
6 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
7 *******************************************************************************/
10 #include <linux/iopoll.h>
12 #include "dwmac_dma.h"
15 #define GMAC_HI_REG_AE 0x80000000
17 int dwmac_dma_reset(void __iomem *ioaddr)
19 u32 value = readl(ioaddr + DMA_BUS_MODE);
22 value |= DMA_BUS_MODE_SFT_RESET;
23 writel(value, ioaddr + DMA_BUS_MODE);
25 return readl_poll_timeout(ioaddr + DMA_BUS_MODE, value,
26 !(value & DMA_BUS_MODE_SFT_RESET),
30 /* CSR1 enables the transmit DMA to check for new descriptor */
31 void dwmac_enable_dma_transmission(void __iomem *ioaddr)
33 writel(1, ioaddr + DMA_XMT_POLL_DEMAND);
36 void dwmac_enable_dma_irq(struct stmmac_priv *priv, void __iomem *ioaddr,
37 u32 chan, bool rx, bool tx)
39 u32 value = readl(ioaddr + DMA_INTR_ENA);
42 value |= DMA_INTR_DEFAULT_RX;
44 value |= DMA_INTR_DEFAULT_TX;
46 writel(value, ioaddr + DMA_INTR_ENA);
49 void dwmac_disable_dma_irq(struct stmmac_priv *priv, void __iomem *ioaddr,
50 u32 chan, bool rx, bool tx)
52 u32 value = readl(ioaddr + DMA_INTR_ENA);
55 value &= ~DMA_INTR_DEFAULT_RX;
57 value &= ~DMA_INTR_DEFAULT_TX;
59 writel(value, ioaddr + DMA_INTR_ENA);
62 void dwmac_dma_start_tx(struct stmmac_priv *priv, void __iomem *ioaddr,
65 u32 value = readl(ioaddr + DMA_CONTROL);
66 value |= DMA_CONTROL_ST;
67 writel(value, ioaddr + DMA_CONTROL);
70 void dwmac_dma_stop_tx(struct stmmac_priv *priv, void __iomem *ioaddr, u32 chan)
72 u32 value = readl(ioaddr + DMA_CONTROL);
73 value &= ~DMA_CONTROL_ST;
74 writel(value, ioaddr + DMA_CONTROL);
77 void dwmac_dma_start_rx(struct stmmac_priv *priv, void __iomem *ioaddr,
80 u32 value = readl(ioaddr + DMA_CONTROL);
81 value |= DMA_CONTROL_SR;
82 writel(value, ioaddr + DMA_CONTROL);
85 void dwmac_dma_stop_rx(struct stmmac_priv *priv, void __iomem *ioaddr, u32 chan)
87 u32 value = readl(ioaddr + DMA_CONTROL);
88 value &= ~DMA_CONTROL_SR;
89 writel(value, ioaddr + DMA_CONTROL);
92 #ifdef DWMAC_DMA_DEBUG
93 static void show_tx_process_state(unsigned int status)
96 state = (status & DMA_STATUS_TS_MASK) >> DMA_STATUS_TS_SHIFT;
100 pr_debug("- TX (Stopped): Reset or Stop command\n");
103 pr_debug("- TX (Running): Fetching the Tx desc\n");
106 pr_debug("- TX (Running): Waiting for end of tx\n");
109 pr_debug("- TX (Running): Reading the data "
110 "and queuing the data into the Tx buf\n");
113 pr_debug("- TX (Suspended): Tx Buff Underflow "
114 "or an unavailable Transmit descriptor\n");
117 pr_debug("- TX (Running): Closing Tx descriptor\n");
124 static void show_rx_process_state(unsigned int status)
127 state = (status & DMA_STATUS_RS_MASK) >> DMA_STATUS_RS_SHIFT;
131 pr_debug("- RX (Stopped): Reset or Stop command\n");
134 pr_debug("- RX (Running): Fetching the Rx desc\n");
137 pr_debug("- RX (Running): Checking for end of pkt\n");
140 pr_debug("- RX (Running): Waiting for Rx pkt\n");
143 pr_debug("- RX (Suspended): Unavailable Rx buf\n");
146 pr_debug("- RX (Running): Closing Rx descriptor\n");
149 pr_debug("- RX(Running): Flushing the current frame"
150 " from the Rx buf\n");
153 pr_debug("- RX (Running): Queuing the Rx frame"
154 " from the Rx buf into memory\n");
162 int dwmac_dma_interrupt(struct stmmac_priv *priv, void __iomem *ioaddr,
163 struct stmmac_extra_stats *x, u32 chan, u32 dir)
165 struct stmmac_rx_queue *rx_q = &priv->dma_conf.rx_queue[chan];
166 struct stmmac_tx_queue *tx_q = &priv->dma_conf.tx_queue[chan];
168 /* read the status register (CSR5) */
169 u32 intr_status = readl(ioaddr + DMA_STATUS);
171 #ifdef DWMAC_DMA_DEBUG
172 /* Enable it to monitor DMA rx/tx status in case of critical problems */
173 pr_debug("%s: [CSR5: 0x%08x]\n", __func__, intr_status);
174 show_tx_process_state(intr_status);
175 show_rx_process_state(intr_status);
178 if (dir == DMA_DIR_RX)
179 intr_status &= DMA_STATUS_MSK_RX;
180 else if (dir == DMA_DIR_TX)
181 intr_status &= DMA_STATUS_MSK_TX;
183 /* ABNORMAL interrupts */
184 if (unlikely(intr_status & DMA_STATUS_AIS)) {
185 if (unlikely(intr_status & DMA_STATUS_UNF)) {
186 ret = tx_hard_error_bump_tc;
187 x->tx_undeflow_irq++;
189 if (unlikely(intr_status & DMA_STATUS_TJT))
192 if (unlikely(intr_status & DMA_STATUS_OVF))
193 x->rx_overflow_irq++;
195 if (unlikely(intr_status & DMA_STATUS_RU))
196 x->rx_buf_unav_irq++;
197 if (unlikely(intr_status & DMA_STATUS_RPS))
198 x->rx_process_stopped_irq++;
199 if (unlikely(intr_status & DMA_STATUS_RWT))
200 x->rx_watchdog_irq++;
201 if (unlikely(intr_status & DMA_STATUS_ETI))
203 if (unlikely(intr_status & DMA_STATUS_TPS)) {
204 x->tx_process_stopped_irq++;
207 if (unlikely(intr_status & DMA_STATUS_FBI)) {
208 x->fatal_bus_error_irq++;
212 /* TX/RX NORMAL interrupts */
213 if (likely(intr_status & DMA_STATUS_NIS)) {
214 if (likely(intr_status & DMA_STATUS_RI)) {
215 u32 value = readl(ioaddr + DMA_INTR_ENA);
216 /* to schedule NAPI on real RIE event. */
217 if (likely(value & DMA_INTR_ENA_RIE)) {
218 u64_stats_update_begin(&rx_q->rxq_stats.syncp);
219 rx_q->rxq_stats.rx_normal_irq_n++;
220 u64_stats_update_end(&rx_q->rxq_stats.syncp);
224 if (likely(intr_status & DMA_STATUS_TI)) {
225 u64_stats_update_begin(&tx_q->txq_stats.syncp);
226 tx_q->txq_stats.tx_normal_irq_n++;
227 u64_stats_update_end(&tx_q->txq_stats.syncp);
230 if (unlikely(intr_status & DMA_STATUS_ERI))
233 /* Optional hardware blocks, interrupts should be disabled */
234 if (unlikely(intr_status &
235 (DMA_STATUS_GPI | DMA_STATUS_GMI | DMA_STATUS_GLI)))
236 pr_warn("%s: unexpected status %08x\n", __func__, intr_status);
238 /* Clear the interrupt by writing a logic 1 to the CSR5[15-0] */
239 writel((intr_status & 0x1ffff), ioaddr + DMA_STATUS);
244 void dwmac_dma_flush_tx_fifo(void __iomem *ioaddr)
246 u32 csr6 = readl(ioaddr + DMA_CONTROL);
247 writel((csr6 | DMA_CONTROL_FTF), ioaddr + DMA_CONTROL);
249 do {} while ((readl(ioaddr + DMA_CONTROL) & DMA_CONTROL_FTF));
252 void stmmac_set_mac_addr(void __iomem *ioaddr, const u8 addr[6],
253 unsigned int high, unsigned int low)
257 data = (addr[5] << 8) | addr[4];
258 /* For MAC Addr registers we have to set the Address Enable (AE)
259 * bit that has no effect on the High Reg 0 where the bit 31 (MO)
262 writel(data | GMAC_HI_REG_AE, ioaddr + high);
263 data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
264 writel(data, ioaddr + low);
266 EXPORT_SYMBOL_GPL(stmmac_set_mac_addr);
268 /* Enable disable MAC RX/TX */
269 void stmmac_set_mac(void __iomem *ioaddr, bool enable)
273 old_val = readl(ioaddr + MAC_CTRL_REG);
277 value |= MAC_ENABLE_RX | MAC_ENABLE_TX;
279 value &= ~(MAC_ENABLE_TX | MAC_ENABLE_RX);
281 if (value != old_val)
282 writel(value, ioaddr + MAC_CTRL_REG);
285 void stmmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
286 unsigned int high, unsigned int low)
288 unsigned int hi_addr, lo_addr;
290 /* Read the MAC address from the hardware */
291 hi_addr = readl(ioaddr + high);
292 lo_addr = readl(ioaddr + low);
294 /* Extract the MAC address from the high and low words */
295 addr[0] = lo_addr & 0xff;
296 addr[1] = (lo_addr >> 8) & 0xff;
297 addr[2] = (lo_addr >> 16) & 0xff;
298 addr[3] = (lo_addr >> 24) & 0xff;
299 addr[4] = hi_addr & 0xff;
300 addr[5] = (hi_addr >> 8) & 0xff;
302 EXPORT_SYMBOL_GPL(stmmac_get_mac_addr);