xref: /openbmc/linux/drivers/net/ethernet/stmicro/stmmac/dwmac4_lib.c (revision 87fcfa7b7fe6bf819033fe827a27f710e38639b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2007-2015  STMicroelectronics Ltd
4  *
5  * Author: Alexandre Torgue <alexandre.torgue@st.com>
6  */
7 
8 #include <linux/io.h>
9 #include <linux/delay.h>
10 #include "common.h"
11 #include "dwmac4_dma.h"
12 #include "dwmac4.h"
13 
14 int dwmac4_dma_reset(void __iomem *ioaddr)
15 {
16 	u32 value = readl(ioaddr + DMA_BUS_MODE);
17 	int limit;
18 
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 
29 	if (limit < 0)
30 		return -EBUSY;
31 
32 	return 0;
33 }
34 
35 void dwmac4_set_rx_tail_ptr(void __iomem *ioaddr, u32 tail_ptr, u32 chan)
36 {
37 	writel(tail_ptr, ioaddr + DMA_CHAN_RX_END_ADDR(chan));
38 }
39 
40 void dwmac4_set_tx_tail_ptr(void __iomem *ioaddr, u32 tail_ptr, u32 chan)
41 {
42 	writel(tail_ptr, ioaddr + DMA_CHAN_TX_END_ADDR(chan));
43 }
44 
45 void dwmac4_dma_start_tx(void __iomem *ioaddr, u32 chan)
46 {
47 	u32 value = readl(ioaddr + DMA_CHAN_TX_CONTROL(chan));
48 
49 	value |= DMA_CONTROL_ST;
50 	writel(value, ioaddr + DMA_CHAN_TX_CONTROL(chan));
51 
52 	value = readl(ioaddr + GMAC_CONFIG);
53 	value |= GMAC_CONFIG_TE;
54 	writel(value, ioaddr + GMAC_CONFIG);
55 }
56 
57 void dwmac4_dma_stop_tx(void __iomem *ioaddr, u32 chan)
58 {
59 	u32 value = readl(ioaddr + DMA_CHAN_TX_CONTROL(chan));
60 
61 	value &= ~DMA_CONTROL_ST;
62 	writel(value, ioaddr + DMA_CHAN_TX_CONTROL(chan));
63 
64 	value = readl(ioaddr + GMAC_CONFIG);
65 	value &= ~GMAC_CONFIG_TE;
66 	writel(value, ioaddr + GMAC_CONFIG);
67 }
68 
69 void dwmac4_dma_start_rx(void __iomem *ioaddr, u32 chan)
70 {
71 	u32 value = readl(ioaddr + DMA_CHAN_RX_CONTROL(chan));
72 
73 	value |= DMA_CONTROL_SR;
74 
75 	writel(value, ioaddr + DMA_CHAN_RX_CONTROL(chan));
76 
77 	value = readl(ioaddr + GMAC_CONFIG);
78 	value |= GMAC_CONFIG_RE;
79 	writel(value, ioaddr + GMAC_CONFIG);
80 }
81 
82 void dwmac4_dma_stop_rx(void __iomem *ioaddr, u32 chan)
83 {
84 	u32 value = readl(ioaddr + DMA_CHAN_RX_CONTROL(chan));
85 
86 	value &= ~DMA_CONTROL_SR;
87 	writel(value, ioaddr + DMA_CHAN_RX_CONTROL(chan));
88 }
89 
90 void dwmac4_set_tx_ring_len(void __iomem *ioaddr, u32 len, u32 chan)
91 {
92 	writel(len, ioaddr + DMA_CHAN_TX_RING_LEN(chan));
93 }
94 
95 void dwmac4_set_rx_ring_len(void __iomem *ioaddr, u32 len, u32 chan)
96 {
97 	writel(len, ioaddr + DMA_CHAN_RX_RING_LEN(chan));
98 }
99 
100 void dwmac4_enable_dma_irq(void __iomem *ioaddr, u32 chan, bool rx, bool tx)
101 {
102 	u32 value = readl(ioaddr + DMA_CHAN_INTR_ENA(chan));
103 
104 	if (rx)
105 		value |= DMA_CHAN_INTR_DEFAULT_RX;
106 	if (tx)
107 		value |= DMA_CHAN_INTR_DEFAULT_TX;
108 
109 	writel(value, ioaddr + DMA_CHAN_INTR_ENA(chan));
110 }
111 
112 void dwmac410_enable_dma_irq(void __iomem *ioaddr, u32 chan, bool rx, bool tx)
113 {
114 	u32 value = readl(ioaddr + DMA_CHAN_INTR_ENA(chan));
115 
116 	if (rx)
117 		value |= DMA_CHAN_INTR_DEFAULT_RX_4_10;
118 	if (tx)
119 		value |= DMA_CHAN_INTR_DEFAULT_TX_4_10;
120 
121 	writel(value, ioaddr + DMA_CHAN_INTR_ENA(chan));
122 }
123 
124 void dwmac4_disable_dma_irq(void __iomem *ioaddr, u32 chan, bool rx, bool tx)
125 {
126 	u32 value = readl(ioaddr + DMA_CHAN_INTR_ENA(chan));
127 
128 	if (rx)
129 		value &= ~DMA_CHAN_INTR_DEFAULT_RX;
130 	if (tx)
131 		value &= ~DMA_CHAN_INTR_DEFAULT_TX;
132 
133 	writel(value, ioaddr + DMA_CHAN_INTR_ENA(chan));
134 }
135 
136 void dwmac410_disable_dma_irq(void __iomem *ioaddr, u32 chan, bool rx, bool tx)
137 {
138 	u32 value = readl(ioaddr + DMA_CHAN_INTR_ENA(chan));
139 
140 	if (rx)
141 		value &= ~DMA_CHAN_INTR_DEFAULT_RX_4_10;
142 	if (tx)
143 		value &= ~DMA_CHAN_INTR_DEFAULT_TX_4_10;
144 
145 	writel(value, ioaddr + DMA_CHAN_INTR_ENA(chan));
146 }
147 
148 int dwmac4_dma_interrupt(void __iomem *ioaddr,
149 			 struct stmmac_extra_stats *x, u32 chan)
150 {
151 	u32 intr_status = readl(ioaddr + DMA_CHAN_STATUS(chan));
152 	u32 intr_en = readl(ioaddr + DMA_CHAN_INTR_ENA(chan));
153 	int ret = 0;
154 
155 	/* ABNORMAL interrupts */
156 	if (unlikely(intr_status & DMA_CHAN_STATUS_AIS)) {
157 		if (unlikely(intr_status & DMA_CHAN_STATUS_RBU))
158 			x->rx_buf_unav_irq++;
159 		if (unlikely(intr_status & DMA_CHAN_STATUS_RPS))
160 			x->rx_process_stopped_irq++;
161 		if (unlikely(intr_status & DMA_CHAN_STATUS_RWT))
162 			x->rx_watchdog_irq++;
163 		if (unlikely(intr_status & DMA_CHAN_STATUS_ETI))
164 			x->tx_early_irq++;
165 		if (unlikely(intr_status & DMA_CHAN_STATUS_TPS)) {
166 			x->tx_process_stopped_irq++;
167 			ret = tx_hard_error;
168 		}
169 		if (unlikely(intr_status & DMA_CHAN_STATUS_FBE)) {
170 			x->fatal_bus_error_irq++;
171 			ret = tx_hard_error;
172 		}
173 	}
174 	/* TX/RX NORMAL interrupts */
175 	if (likely(intr_status & DMA_CHAN_STATUS_NIS)) {
176 		x->normal_irq_n++;
177 		if (likely(intr_status & DMA_CHAN_STATUS_RI)) {
178 			x->rx_normal_irq_n++;
179 			ret |= handle_rx;
180 		}
181 		if (likely(intr_status & (DMA_CHAN_STATUS_TI |
182 					  DMA_CHAN_STATUS_TBU))) {
183 			x->tx_normal_irq_n++;
184 			ret |= handle_tx;
185 		}
186 		if (unlikely(intr_status & DMA_CHAN_STATUS_ERI))
187 			x->rx_early_irq++;
188 	}
189 
190 	writel(intr_status & intr_en, ioaddr + DMA_CHAN_STATUS(chan));
191 	return ret;
192 }
193 
194 void stmmac_dwmac4_set_mac_addr(void __iomem *ioaddr, u8 addr[6],
195 				unsigned int high, unsigned int low)
196 {
197 	unsigned long data;
198 
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 	data |= (STMMAC_CHAN0 << GMAC_HI_DCS_SHIFT);
205 	writel(data | GMAC_HI_REG_AE, ioaddr + high);
206 	data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
207 	writel(data, ioaddr + low);
208 }
209 
210 /* Enable disable MAC RX/TX */
211 void stmmac_dwmac4_set_mac(void __iomem *ioaddr, bool enable)
212 {
213 	u32 value = readl(ioaddr + GMAC_CONFIG);
214 
215 	if (enable)
216 		value |= GMAC_CONFIG_RE | GMAC_CONFIG_TE;
217 	else
218 		value &= ~(GMAC_CONFIG_TE | GMAC_CONFIG_RE);
219 
220 	writel(value, ioaddr + GMAC_CONFIG);
221 }
222 
223 void stmmac_dwmac4_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
224 				unsigned int high, unsigned int low)
225 {
226 	unsigned int hi_addr, lo_addr;
227 
228 	/* Read the MAC address from the hardware */
229 	hi_addr = readl(ioaddr + high);
230 	lo_addr = readl(ioaddr + low);
231 
232 	/* Extract the MAC address from the high and low words */
233 	addr[0] = lo_addr & 0xff;
234 	addr[1] = (lo_addr >> 8) & 0xff;
235 	addr[2] = (lo_addr >> 16) & 0xff;
236 	addr[3] = (lo_addr >> 24) & 0xff;
237 	addr[4] = hi_addr & 0xff;
238 	addr[5] = (hi_addr >> 8) & 0xff;
239 }
240