blob: 84e3e84cec7d33dddf13e0fc83e235d1e181cbda [file] [log] [blame]
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001/*******************************************************************************
2 Copyright (C) 2007-2009 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 This program is distributed in the hope it will be useful, but WITHOUT
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16
17 The full GNU General Public License is included in this distribution in
18 the file called "COPYING".
19
20 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
21*******************************************************************************/
22
23#include <linux/io.h>
24#include "common.h"
25#include "dwmac_dma.h"
26
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +000027#define GMAC_HI_REG_AE 0x80000000
28
Giuseppe Cavallaro495db272016-02-29 14:27:27 +010029int dwmac_dma_reset(void __iomem *ioaddr)
30{
31 u32 value = readl(ioaddr + DMA_BUS_MODE);
32 int limit;
33
34 /* DMA SW reset */
35 value |= DMA_BUS_MODE_SFT_RESET;
36 writel(value, ioaddr + DMA_BUS_MODE);
37 limit = 10;
38 while (limit--) {
39 if (!(readl(ioaddr + DMA_BUS_MODE) & DMA_BUS_MODE_SFT_RESET))
40 break;
41 mdelay(10);
42 }
43
44 if (limit < 0)
45 return -EBUSY;
46
47 return 0;
48}
49
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000050/* CSR1 enables the transmit DMA to check for new descriptor */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000051void dwmac_enable_dma_transmission(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000052{
53 writel(1, ioaddr + DMA_XMT_POLL_DEMAND);
54}
55
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000056void dwmac_enable_dma_irq(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000057{
58 writel(DMA_INTR_DEFAULT_MASK, ioaddr + DMA_INTR_ENA);
59}
60
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000061void dwmac_disable_dma_irq(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000062{
63 writel(0, ioaddr + DMA_INTR_ENA);
64}
65
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000066void dwmac_dma_start_tx(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000067{
68 u32 value = readl(ioaddr + DMA_CONTROL);
69 value |= DMA_CONTROL_ST;
70 writel(value, ioaddr + DMA_CONTROL);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000071}
72
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000073void dwmac_dma_stop_tx(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000074{
75 u32 value = readl(ioaddr + DMA_CONTROL);
76 value &= ~DMA_CONTROL_ST;
77 writel(value, ioaddr + DMA_CONTROL);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000078}
79
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000080void dwmac_dma_start_rx(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000081{
82 u32 value = readl(ioaddr + DMA_CONTROL);
83 value |= DMA_CONTROL_SR;
84 writel(value, ioaddr + DMA_CONTROL);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000085}
86
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000087void dwmac_dma_stop_rx(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000088{
89 u32 value = readl(ioaddr + DMA_CONTROL);
90 value &= ~DMA_CONTROL_SR;
91 writel(value, ioaddr + DMA_CONTROL);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000092}
93
94#ifdef DWMAC_DMA_DEBUG
95static void show_tx_process_state(unsigned int status)
96{
97 unsigned int state;
98 state = (status & DMA_STATUS_TS_MASK) >> DMA_STATUS_TS_SHIFT;
99
100 switch (state) {
101 case 0:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200102 pr_debug("- TX (Stopped): Reset or Stop command\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000103 break;
104 case 1:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200105 pr_debug("- TX (Running):Fetching the Tx desc\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000106 break;
107 case 2:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200108 pr_debug("- TX (Running): Waiting for end of tx\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000109 break;
110 case 3:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200111 pr_debug("- TX (Running): Reading the data "
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000112 "and queuing the data into the Tx buf\n");
113 break;
114 case 6:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200115 pr_debug("- TX (Suspended): Tx Buff Underflow "
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000116 "or an unavailable Transmit descriptor\n");
117 break;
118 case 7:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200119 pr_debug("- TX (Running): Closing Tx descriptor\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000120 break;
121 default:
122 break;
123 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000124}
125
126static void show_rx_process_state(unsigned int status)
127{
128 unsigned int state;
129 state = (status & DMA_STATUS_RS_MASK) >> DMA_STATUS_RS_SHIFT;
130
131 switch (state) {
132 case 0:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200133 pr_debug("- RX (Stopped): Reset or Stop command\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000134 break;
135 case 1:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200136 pr_debug("- RX (Running): Fetching the Rx desc\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000137 break;
138 case 2:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200139 pr_debug("- RX (Running):Checking for end of pkt\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000140 break;
141 case 3:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200142 pr_debug("- RX (Running): Waiting for Rx pkt\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000143 break;
144 case 4:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200145 pr_debug("- RX (Suspended): Unavailable Rx buf\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000146 break;
147 case 5:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200148 pr_debug("- RX (Running): Closing Rx descriptor\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000149 break;
150 case 6:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200151 pr_debug("- RX(Running): Flushing the current frame"
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000152 " from the Rx buf\n");
153 break;
154 case 7:
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200155 pr_debug("- RX (Running): Queuing the Rx frame"
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000156 " from the Rx buf into memory\n");
157 break;
158 default:
159 break;
160 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000161}
162#endif
163
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000164int dwmac_dma_interrupt(void __iomem *ioaddr,
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000165 struct stmmac_extra_stats *x)
166{
167 int ret = 0;
168 /* read the status register (CSR5) */
169 u32 intr_status = readl(ioaddr + DMA_STATUS);
170
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000171#ifdef DWMAC_DMA_DEBUG
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200172 /* Enable it to monitor DMA rx/tx status in case of critical problems */
173 pr_debug("%s: [CSR5: 0x%08x]\n", __func__, intr_status);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000174 show_tx_process_state(intr_status);
175 show_rx_process_state(intr_status);
176#endif
177 /* ABNORMAL interrupts */
178 if (unlikely(intr_status & DMA_STATUS_AIS)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000179 if (unlikely(intr_status & DMA_STATUS_UNF)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000180 ret = tx_hard_error_bump_tc;
181 x->tx_undeflow_irq++;
182 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200183 if (unlikely(intr_status & DMA_STATUS_TJT))
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000184 x->tx_jabber_irq++;
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200185
186 if (unlikely(intr_status & DMA_STATUS_OVF))
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000187 x->rx_overflow_irq++;
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200188
189 if (unlikely(intr_status & DMA_STATUS_RU))
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000190 x->rx_buf_unav_irq++;
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200191 if (unlikely(intr_status & DMA_STATUS_RPS))
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000192 x->rx_process_stopped_irq++;
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200193 if (unlikely(intr_status & DMA_STATUS_RWT))
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000194 x->rx_watchdog_irq++;
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200195 if (unlikely(intr_status & DMA_STATUS_ETI))
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000196 x->tx_early_irq++;
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000197 if (unlikely(intr_status & DMA_STATUS_TPS)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000198 x->tx_process_stopped_irq++;
199 ret = tx_hard_error;
200 }
201 if (unlikely(intr_status & DMA_STATUS_FBI)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000202 x->fatal_bus_error_irq++;
203 ret = tx_hard_error;
204 }
205 }
206 /* TX/RX NORMAL interrupts */
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +0000207 if (likely(intr_status & DMA_STATUS_NIS)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000208 x->normal_irq_n++;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +0000209 if (likely(intr_status & DMA_STATUS_RI)) {
210 u32 value = readl(ioaddr + DMA_INTR_ENA);
211 /* to schedule NAPI on real RIE event. */
212 if (likely(value & DMA_INTR_ENA_RIE)) {
213 x->rx_normal_irq_n++;
214 ret |= handle_rx;
215 }
216 }
217 if (likely(intr_status & DMA_STATUS_TI)) {
218 x->tx_normal_irq_n++;
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000219 ret |= handle_tx;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +0000220 }
221 if (unlikely(intr_status & DMA_STATUS_ERI))
222 x->rx_early_irq++;
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000223 }
224 /* Optional hardware blocks, interrupts should be disabled */
225 if (unlikely(intr_status &
226 (DMA_STATUS_GPI | DMA_STATUS_GMI | DMA_STATUS_GLI)))
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200227 pr_warn("%s: unexpected status %08x\n", __func__, intr_status);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +0000228
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000229 /* Clear the interrupt by writing a logic 1 to the CSR5[15-0] */
230 writel((intr_status & 0x1ffff), ioaddr + DMA_STATUS);
231
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000232 return ret;
233}
234
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000235void dwmac_dma_flush_tx_fifo(void __iomem *ioaddr)
Giuseppe CAVALLARO688911c2010-04-13 20:21:13 +0000236{
237 u32 csr6 = readl(ioaddr + DMA_CONTROL);
238 writel((csr6 | DMA_CONTROL_FTF), ioaddr + DMA_CONTROL);
239
240 do {} while ((readl(ioaddr + DMA_CONTROL) & DMA_CONTROL_FTF));
241}
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000242
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000243void stmmac_set_mac_addr(void __iomem *ioaddr, u8 addr[6],
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000244 unsigned int high, unsigned int low)
245{
246 unsigned long data;
247
248 data = (addr[5] << 8) | addr[4];
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +0000249 /* For MAC Addr registers se have to set the Address Enable (AE)
250 * bit that has no effect on the High Reg 0 where the bit 31 (MO)
251 * is RO.
252 */
253 writel(data | GMAC_HI_REG_AE, ioaddr + high);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000254 data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
255 writel(data, ioaddr + low);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000256}
257
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000258/* Enable disable MAC RX/TX */
259void stmmac_set_mac(void __iomem *ioaddr, bool enable)
260{
261 u32 value = readl(ioaddr + MAC_CTRL_REG);
262
263 if (enable)
264 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
265 else
266 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
267
268 writel(value, ioaddr + MAC_CTRL_REG);
269}
270
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000271void stmmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000272 unsigned int high, unsigned int low)
273{
274 unsigned int hi_addr, lo_addr;
275
276 /* Read the MAC address from the hardware */
277 hi_addr = readl(ioaddr + high);
278 lo_addr = readl(ioaddr + low);
279
280 /* Extract the MAC address from the high and low words */
281 addr[0] = lo_addr & 0xff;
282 addr[1] = (lo_addr >> 8) & 0xff;
283 addr[2] = (lo_addr >> 16) & 0xff;
284 addr[3] = (lo_addr >> 24) & 0xff;
285 addr[4] = hi_addr & 0xff;
286 addr[5] = (hi_addr >> 8) & 0xff;
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000287}
288