blob: 491d7e9306037a119ae1cb5e4454935213cc01e8 [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
27#undef DWMAC_DMA_DEBUG
28#ifdef DWMAC_DMA_DEBUG
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +000029#define DWMAC_LIB_DBG(fmt, args...) printk(fmt, ## args)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000030#else
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +000031#define DWMAC_LIB_DBG(fmt, args...) do { } while (0)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000032#endif
33
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +000034#define GMAC_HI_REG_AE 0x80000000
35
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000036/* CSR1 enables the transmit DMA to check for new descriptor */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000037void dwmac_enable_dma_transmission(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000038{
39 writel(1, ioaddr + DMA_XMT_POLL_DEMAND);
40}
41
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000042void dwmac_enable_dma_irq(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000043{
44 writel(DMA_INTR_DEFAULT_MASK, ioaddr + DMA_INTR_ENA);
45}
46
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000047void dwmac_disable_dma_irq(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000048{
49 writel(0, ioaddr + DMA_INTR_ENA);
50}
51
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000052void dwmac_dma_start_tx(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000053{
54 u32 value = readl(ioaddr + DMA_CONTROL);
55 value |= DMA_CONTROL_ST;
56 writel(value, ioaddr + DMA_CONTROL);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000057}
58
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000059void dwmac_dma_stop_tx(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000060{
61 u32 value = readl(ioaddr + DMA_CONTROL);
62 value &= ~DMA_CONTROL_ST;
63 writel(value, ioaddr + DMA_CONTROL);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000064}
65
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +000066void dwmac_dma_start_rx(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000067{
68 u32 value = readl(ioaddr + DMA_CONTROL);
69 value |= DMA_CONTROL_SR;
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_rx(void __iomem *ioaddr)
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000074{
75 u32 value = readl(ioaddr + DMA_CONTROL);
76 value &= ~DMA_CONTROL_SR;
77 writel(value, ioaddr + DMA_CONTROL);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000078}
79
80#ifdef DWMAC_DMA_DEBUG
81static void show_tx_process_state(unsigned int status)
82{
83 unsigned int state;
84 state = (status & DMA_STATUS_TS_MASK) >> DMA_STATUS_TS_SHIFT;
85
86 switch (state) {
87 case 0:
88 pr_info("- TX (Stopped): Reset or Stop command\n");
89 break;
90 case 1:
91 pr_info("- TX (Running):Fetching the Tx desc\n");
92 break;
93 case 2:
94 pr_info("- TX (Running): Waiting for end of tx\n");
95 break;
96 case 3:
97 pr_info("- TX (Running): Reading the data "
98 "and queuing the data into the Tx buf\n");
99 break;
100 case 6:
101 pr_info("- TX (Suspended): Tx Buff Underflow "
102 "or an unavailable Transmit descriptor\n");
103 break;
104 case 7:
105 pr_info("- TX (Running): Closing Tx descriptor\n");
106 break;
107 default:
108 break;
109 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000110}
111
112static void show_rx_process_state(unsigned int status)
113{
114 unsigned int state;
115 state = (status & DMA_STATUS_RS_MASK) >> DMA_STATUS_RS_SHIFT;
116
117 switch (state) {
118 case 0:
119 pr_info("- RX (Stopped): Reset or Stop command\n");
120 break;
121 case 1:
122 pr_info("- RX (Running): Fetching the Rx desc\n");
123 break;
124 case 2:
125 pr_info("- RX (Running):Checking for end of pkt\n");
126 break;
127 case 3:
128 pr_info("- RX (Running): Waiting for Rx pkt\n");
129 break;
130 case 4:
131 pr_info("- RX (Suspended): Unavailable Rx buf\n");
132 break;
133 case 5:
134 pr_info("- RX (Running): Closing Rx descriptor\n");
135 break;
136 case 6:
137 pr_info("- RX(Running): Flushing the current frame"
138 " from the Rx buf\n");
139 break;
140 case 7:
141 pr_info("- RX (Running): Queuing the Rx frame"
142 " from the Rx buf into memory\n");
143 break;
144 default:
145 break;
146 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000147}
148#endif
149
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000150int dwmac_dma_interrupt(void __iomem *ioaddr,
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000151 struct stmmac_extra_stats *x)
152{
153 int ret = 0;
154 /* read the status register (CSR5) */
155 u32 intr_status = readl(ioaddr + DMA_STATUS);
156
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000157 DWMAC_LIB_DBG(KERN_INFO "%s: [CSR5: 0x%08x]\n", __func__, intr_status);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000158#ifdef DWMAC_DMA_DEBUG
159 /* It displays the DMA process states (CSR5 register) */
160 show_tx_process_state(intr_status);
161 show_rx_process_state(intr_status);
162#endif
163 /* ABNORMAL interrupts */
164 if (unlikely(intr_status & DMA_STATUS_AIS)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000165 DWMAC_LIB_DBG(KERN_INFO "CSR5[15] DMA ABNORMAL IRQ: ");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000166 if (unlikely(intr_status & DMA_STATUS_UNF)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000167 DWMAC_LIB_DBG(KERN_INFO "transmit underflow\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000168 ret = tx_hard_error_bump_tc;
169 x->tx_undeflow_irq++;
170 }
171 if (unlikely(intr_status & DMA_STATUS_TJT)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000172 DWMAC_LIB_DBG(KERN_INFO "transmit jabber\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000173 x->tx_jabber_irq++;
174 }
175 if (unlikely(intr_status & DMA_STATUS_OVF)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000176 DWMAC_LIB_DBG(KERN_INFO "recv overflow\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000177 x->rx_overflow_irq++;
178 }
179 if (unlikely(intr_status & DMA_STATUS_RU)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000180 DWMAC_LIB_DBG(KERN_INFO "receive buffer unavailable\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000181 x->rx_buf_unav_irq++;
182 }
183 if (unlikely(intr_status & DMA_STATUS_RPS)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000184 DWMAC_LIB_DBG(KERN_INFO "receive process stopped\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000185 x->rx_process_stopped_irq++;
186 }
187 if (unlikely(intr_status & DMA_STATUS_RWT)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000188 DWMAC_LIB_DBG(KERN_INFO "receive watchdog\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000189 x->rx_watchdog_irq++;
190 }
191 if (unlikely(intr_status & DMA_STATUS_ETI)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000192 DWMAC_LIB_DBG(KERN_INFO "transmit early interrupt\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000193 x->tx_early_irq++;
194 }
195 if (unlikely(intr_status & DMA_STATUS_TPS)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000196 DWMAC_LIB_DBG(KERN_INFO "transmit process stopped\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000197 x->tx_process_stopped_irq++;
198 ret = tx_hard_error;
199 }
200 if (unlikely(intr_status & DMA_STATUS_FBI)) {
Giuseppe CAVALLARObded18c2011-04-10 23:16:44 +0000201 DWMAC_LIB_DBG(KERN_INFO "fatal bus error\n");
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)))
227 pr_info("%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 CAVALLARObded18c2011-04-10 23:16:44 +0000232 DWMAC_LIB_DBG(KERN_INFO "\n\n");
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000233 return ret;
234}
235
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000236void dwmac_dma_flush_tx_fifo(void __iomem *ioaddr)
Giuseppe CAVALLARO688911c2010-04-13 20:21:13 +0000237{
238 u32 csr6 = readl(ioaddr + DMA_CONTROL);
239 writel((csr6 | DMA_CONTROL_FTF), ioaddr + DMA_CONTROL);
240
241 do {} while ((readl(ioaddr + DMA_CONTROL) & DMA_CONTROL_FTF));
242}
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000243
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000244void stmmac_set_mac_addr(void __iomem *ioaddr, u8 addr[6],
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000245 unsigned int high, unsigned int low)
246{
247 unsigned long data;
248
249 data = (addr[5] << 8) | addr[4];
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +0000250 /* For MAC Addr registers se have to set the Address Enable (AE)
251 * bit that has no effect on the High Reg 0 where the bit 31 (MO)
252 * is RO.
253 */
254 writel(data | GMAC_HI_REG_AE, ioaddr + high);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000255 data = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
256 writel(data, ioaddr + low);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000257}
258
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000259/* Enable disable MAC RX/TX */
260void stmmac_set_mac(void __iomem *ioaddr, bool enable)
261{
262 u32 value = readl(ioaddr + MAC_CTRL_REG);
263
264 if (enable)
265 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
266 else
267 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
268
269 writel(value, ioaddr + MAC_CTRL_REG);
270}
271
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000272void stmmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr,
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000273 unsigned int high, unsigned int low)
274{
275 unsigned int hi_addr, lo_addr;
276
277 /* Read the MAC address from the hardware */
278 hi_addr = readl(ioaddr + high);
279 lo_addr = readl(ioaddr + low);
280
281 /* Extract the MAC address from the high and low words */
282 addr[0] = lo_addr & 0xff;
283 addr[1] = (lo_addr >> 8) & 0xff;
284 addr[2] = (lo_addr >> 16) & 0xff;
285 addr[3] = (lo_addr >> 24) & 0xff;
286 addr[4] = hi_addr & 0xff;
287 addr[5] = (hi_addr >> 8) & 0xff;
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000288}
289