blob: 2d43a7619f58abed1a20e219c7cfba1e23d89d88 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000035#include <linux/ip.h>
36#include <linux/udp.h>
37#include <linux/tcp.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038#include "macb.h"
39
Nicolas Ferre1b447912013-06-04 21:57:11 +000040#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000041#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050042
Zach Brownb410d132016-10-19 09:56:57 -050043#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050044#define MIN_RX_RING_SIZE 64
45#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000046#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050047 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010048
Zach Brownb410d132016-10-19 09:56:57 -050049#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050050#define MIN_TX_RING_SIZE 64
51#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000052#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050053 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010054
Nicolas Ferre909a8582012-11-19 06:00:21 +000055/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050056#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010057
58#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
59 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000060#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
61 | MACB_BIT(ISR_RLE) \
62 | MACB_BIT(TXERR))
63#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
64
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000065/* Max length of transmit frame must be a multiple of 8 bytes */
66#define MACB_TX_LEN_ALIGN 8
67#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
68#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020069
Jarod Wilson44770e12016-10-17 15:54:17 -040070#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000071#define MACB_NETIF_LSO (NETIF_F_TSO | NETIF_F_UFO)
Harini Katakama5898ea2015-05-06 22:27:18 +053072
Sergio Prado3e2a5e12016-02-09 12:07:16 -020073#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
74#define MACB_WOL_ENABLED (0x1 << 1)
75
Moritz Fischer64ec42f2016-03-29 19:11:12 -070076/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000077 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
78 */
79#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010080
Rafal Ozieblodc97a892017-01-27 15:08:20 +000081/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010082 * depends on hardware configuration:
83 *
84 * 1. dma address width 32 bits:
85 * word 1: 32 bit address of Data Buffer
86 * word 2: control
87 *
88 * 2. dma address width 64 bits:
89 * word 1: 32 bit address of Data Buffer
90 * word 2: control
91 * word 3: upper 32 bit address of Data Buffer
92 * word 4: unused
93 *
94 * 3. dma address width 32 bits with hardware timestamping:
95 * word 1: 32 bit address of Data Buffer
96 * word 2: control
97 * word 3: timestamp word 1
98 * word 4: timestamp word 2
99 *
100 * 4. dma address width 64 bits with hardware timestamping:
101 * word 1: 32 bit address of Data Buffer
102 * word 2: control
103 * word 3: upper 32 bit address of Data Buffer
104 * word 4: unused
105 * word 5: timestamp word 1
106 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000107 */
108static unsigned int macb_dma_desc_get_size(struct macb *bp)
109{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100110#ifdef MACB_EXT_DESC
111 unsigned int desc_size;
112
113 switch (bp->hw_dma_cap) {
114 case HW_DMA_CAP_64B:
115 desc_size = sizeof(struct macb_dma_desc)
116 + sizeof(struct macb_dma_desc_64);
117 break;
118 case HW_DMA_CAP_PTP:
119 desc_size = sizeof(struct macb_dma_desc)
120 + sizeof(struct macb_dma_desc_ptp);
121 break;
122 case HW_DMA_CAP_64B_PTP:
123 desc_size = sizeof(struct macb_dma_desc)
124 + sizeof(struct macb_dma_desc_64)
125 + sizeof(struct macb_dma_desc_ptp);
126 break;
127 default:
128 desc_size = sizeof(struct macb_dma_desc);
129 }
130 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000131#endif
132 return sizeof(struct macb_dma_desc);
133}
134
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100135static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000136{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100137#ifdef MACB_EXT_DESC
138 switch (bp->hw_dma_cap) {
139 case HW_DMA_CAP_64B:
140 case HW_DMA_CAP_PTP:
141 desc_idx <<= 1;
142 break;
143 case HW_DMA_CAP_64B_PTP:
144 desc_idx *= 3;
145 break;
146 default:
147 break;
148 }
149 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000150#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100151 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000152}
153
154#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
155static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
156{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100157 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
158 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
159 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000160}
161#endif
162
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000163/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500164static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000165{
Zach Brownb410d132016-10-19 09:56:57 -0500166 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000167}
168
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100169static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
170 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000171{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000172 index = macb_tx_ring_wrap(queue->bp, index);
173 index = macb_adj_dma_desc_idx(queue->bp, index);
174 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000175}
176
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100177static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
178 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000179{
Zach Brownb410d132016-10-19 09:56:57 -0500180 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181}
182
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100183static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000184{
185 dma_addr_t offset;
186
Zach Brownb410d132016-10-19 09:56:57 -0500187 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000188 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000189
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100190 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000191}
192
Zach Brownb410d132016-10-19 09:56:57 -0500193static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000194{
Zach Brownb410d132016-10-19 09:56:57 -0500195 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000196}
197
198static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
199{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000200 index = macb_rx_ring_wrap(bp, index);
201 index = macb_adj_dma_desc_idx(bp, index);
202 return &bp->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000203}
204
205static void *macb_rx_buffer(struct macb *bp, unsigned int index)
206{
Zach Brownb410d132016-10-19 09:56:57 -0500207 return bp->rx_buffers + bp->rx_buffer_size *
208 macb_rx_ring_wrap(bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000209}
210
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300211/* I/O accessors */
212static u32 hw_readl_native(struct macb *bp, int offset)
213{
214 return __raw_readl(bp->regs + offset);
215}
216
217static void hw_writel_native(struct macb *bp, int offset, u32 value)
218{
219 __raw_writel(value, bp->regs + offset);
220}
221
222static u32 hw_readl(struct macb *bp, int offset)
223{
224 return readl_relaxed(bp->regs + offset);
225}
226
227static void hw_writel(struct macb *bp, int offset, u32 value)
228{
229 writel_relaxed(value, bp->regs + offset);
230}
231
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700232/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700233 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300234 * descriptor access.
235 */
236static bool hw_is_native_io(void __iomem *addr)
237{
238 u32 value = MACB_BIT(LLB);
239
240 __raw_writel(value, addr + MACB_NCR);
241 value = __raw_readl(addr + MACB_NCR);
242
243 /* Write 0 back to disable everything */
244 __raw_writel(0, addr + MACB_NCR);
245
246 return value == MACB_BIT(LLB);
247}
248
249static bool hw_is_gem(void __iomem *addr, bool native_io)
250{
251 u32 id;
252
253 if (native_io)
254 id = __raw_readl(addr + MACB_MID);
255 else
256 id = readl_relaxed(addr + MACB_MID);
257
258 return MACB_BFEXT(IDNUM, id) >= 0x2;
259}
260
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100261static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100262{
263 u32 bottom;
264 u16 top;
265
266 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000267 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100268 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000269 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000270
271 /* Clear unused address register sets */
272 macb_or_gem_writel(bp, SA2B, 0);
273 macb_or_gem_writel(bp, SA2T, 0);
274 macb_or_gem_writel(bp, SA3B, 0);
275 macb_or_gem_writel(bp, SA3T, 0);
276 macb_or_gem_writel(bp, SA4B, 0);
277 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100278}
279
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100280static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100281{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000282 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100283 u32 bottom;
284 u16 top;
285 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000286 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100287
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900288 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000289
Moritz Fischeraa50b552016-03-29 19:11:13 -0700290 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000291 for (i = 0; i < 4; i++) {
292 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
293 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100294
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000295 if (pdata && pdata->rev_eth_addr) {
296 addr[5] = bottom & 0xff;
297 addr[4] = (bottom >> 8) & 0xff;
298 addr[3] = (bottom >> 16) & 0xff;
299 addr[2] = (bottom >> 24) & 0xff;
300 addr[1] = top & 0xff;
301 addr[0] = (top & 0xff00) >> 8;
302 } else {
303 addr[0] = bottom & 0xff;
304 addr[1] = (bottom >> 8) & 0xff;
305 addr[2] = (bottom >> 16) & 0xff;
306 addr[3] = (bottom >> 24) & 0xff;
307 addr[4] = top & 0xff;
308 addr[5] = (top >> 8) & 0xff;
309 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100310
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000311 if (is_valid_ether_addr(addr)) {
312 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
313 return;
314 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700315 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000316
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300317 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000318 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100319}
320
frederic RODO6c36a702007-07-12 19:07:24 +0200321static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100322{
frederic RODO6c36a702007-07-12 19:07:24 +0200323 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100324 int value;
325
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100326 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
327 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200328 | MACB_BF(PHYA, mii_id)
329 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100330 | MACB_BF(CODE, MACB_MAN_CODE)));
331
frederic RODO6c36a702007-07-12 19:07:24 +0200332 /* wait for end of transfer */
333 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
334 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100335
336 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100337
338 return value;
339}
340
frederic RODO6c36a702007-07-12 19:07:24 +0200341static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
342 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100343{
frederic RODO6c36a702007-07-12 19:07:24 +0200344 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100345
346 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
347 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200348 | MACB_BF(PHYA, mii_id)
349 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100350 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200351 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100352
frederic RODO6c36a702007-07-12 19:07:24 +0200353 /* wait for end of transfer */
354 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
355 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100356
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100357 return 0;
358}
359
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800360/**
361 * macb_set_tx_clk() - Set a clock to a new frequency
362 * @clk Pointer to the clock to change
363 * @rate New frequency in Hz
364 * @dev Pointer to the struct net_device
365 */
366static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
367{
368 long ferr, rate, rate_rounded;
369
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100370 if (!clk)
371 return;
372
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800373 switch (speed) {
374 case SPEED_10:
375 rate = 2500000;
376 break;
377 case SPEED_100:
378 rate = 25000000;
379 break;
380 case SPEED_1000:
381 rate = 125000000;
382 break;
383 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800384 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800385 }
386
387 rate_rounded = clk_round_rate(clk, rate);
388 if (rate_rounded < 0)
389 return;
390
391 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
392 * is not satisfied.
393 */
394 ferr = abs(rate_rounded - rate);
395 ferr = DIV_ROUND_UP(ferr, rate / 100000);
396 if (ferr > 5)
397 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700398 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800399
400 if (clk_set_rate(clk, rate_rounded))
401 netdev_err(dev, "adjusting tx_clk failed.\n");
402}
403
frederic RODO6c36a702007-07-12 19:07:24 +0200404static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100405{
frederic RODO6c36a702007-07-12 19:07:24 +0200406 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200407 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200408 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200409 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100410
frederic RODO6c36a702007-07-12 19:07:24 +0200411 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100412
frederic RODO6c36a702007-07-12 19:07:24 +0200413 if (phydev->link) {
414 if ((bp->speed != phydev->speed) ||
415 (bp->duplex != phydev->duplex)) {
416 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100417
frederic RODO6c36a702007-07-12 19:07:24 +0200418 reg = macb_readl(bp, NCFGR);
419 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000420 if (macb_is_gem(bp))
421 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200422
423 if (phydev->duplex)
424 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900425 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200426 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200427 if (phydev->speed == SPEED_1000 &&
428 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000429 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200430
Patrice Vilchez140b7552012-10-31 06:04:50 +0000431 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200432
433 bp->speed = phydev->speed;
434 bp->duplex = phydev->duplex;
435 status_change = 1;
436 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100437 }
438
frederic RODO6c36a702007-07-12 19:07:24 +0200439 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700440 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200441 bp->speed = 0;
442 bp->duplex = -1;
443 }
444 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100445
frederic RODO6c36a702007-07-12 19:07:24 +0200446 status_change = 1;
447 }
448
449 spin_unlock_irqrestore(&bp->lock, flags);
450
451 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000452 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500453 /* Update the TX clock rate if and only if the link is
454 * up and there has been a link change.
455 */
456 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
457
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000458 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000459 netdev_info(dev, "link up (%d/%s)\n",
460 phydev->speed,
461 phydev->duplex == DUPLEX_FULL ?
462 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000463 } else {
464 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000465 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000466 }
frederic RODO6c36a702007-07-12 19:07:24 +0200467 }
468}
469
470/* based on au1000_eth. c*/
471static int macb_mii_probe(struct net_device *dev)
472{
473 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000474 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000475 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000476 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000477 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200478
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200479 if (bp->phy_node) {
480 phydev = of_phy_connect(dev, bp->phy_node,
481 &macb_handle_link_change, 0,
482 bp->phy_interface);
483 if (!phydev)
484 return -ENODEV;
485 } else {
486 phydev = phy_find_first(bp->mii_bus);
487 if (!phydev) {
488 netdev_err(dev, "no PHY found\n");
489 return -ENXIO;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000490 }
frederic RODO6c36a702007-07-12 19:07:24 +0200491
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200492 pdata = dev_get_platdata(&bp->pdev->dev);
493 if (pdata) {
494 if (gpio_is_valid(pdata->phy_irq_pin)) {
495 ret = devm_gpio_request(&bp->pdev->dev,
496 pdata->phy_irq_pin, "phy int");
497 if (!ret) {
498 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
499 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
500 }
501 } else {
502 phydev->irq = PHY_POLL;
503 }
504 }
505
506 /* attach the mac to the phy */
507 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
508 bp->phy_interface);
509 if (ret) {
510 netdev_err(dev, "Could not attach to PHY\n");
511 return ret;
512 }
frederic RODO6c36a702007-07-12 19:07:24 +0200513 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100514
frederic RODO6c36a702007-07-12 19:07:24 +0200515 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200516 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000517 phydev->supported &= PHY_GBIT_FEATURES;
518 else
519 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100520
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500521 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
522 phydev->supported &= ~SUPPORTED_1000baseT_Half;
523
frederic RODO6c36a702007-07-12 19:07:24 +0200524 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100525
frederic RODO6c36a702007-07-12 19:07:24 +0200526 bp->link = 0;
527 bp->speed = 0;
528 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200529
530 return 0;
531}
532
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100533static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200534{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000535 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200536 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200537 int err = -ENXIO, i;
538
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200539 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200540 macb_writel(bp, NCR, MACB_BIT(MPE));
541
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700542 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700543 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200544 err = -ENOMEM;
545 goto err_out;
546 }
547
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700548 bp->mii_bus->name = "MACB_mii_bus";
549 bp->mii_bus->read = &macb_mdio_read;
550 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000551 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700552 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700553 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700554 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900555 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700556
Jamie Iles91523942011-02-28 04:05:25 +0000557 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200558
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200559 np = bp->pdev->dev.of_node;
560 if (np) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200561 if (of_phy_is_fixed_link(np)) {
562 if (of_phy_register_fixed_link(np) < 0) {
563 dev_err(&bp->pdev->dev,
564 "broken fixed-link specification\n");
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200565 goto err_out_unregister_bus;
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200566 }
567 bp->phy_node = of_node_get(np);
568
569 err = mdiobus_register(bp->mii_bus);
570 } else {
571 /* try dt phy registration */
572 err = of_mdiobus_register(bp->mii_bus, np);
573
574 /* fallback to standard phy registration if no phy were
575 * found during dt phy registration
576 */
577 if (!err && !phy_find_first(bp->mii_bus)) {
578 for (i = 0; i < PHY_MAX_ADDR; i++) {
579 struct phy_device *phydev;
580
581 phydev = mdiobus_scan(bp->mii_bus, i);
582 if (IS_ERR(phydev) &&
583 PTR_ERR(phydev) != -ENODEV) {
584 err = PTR_ERR(phydev);
585 break;
586 }
587 }
588
589 if (err)
590 goto err_out_unregister_bus;
591 }
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200592 }
593 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000594 for (i = 0; i < PHY_MAX_ADDR; i++)
595 bp->mii_bus->irq[i] = PHY_POLL;
596
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200597 if (pdata)
598 bp->mii_bus->phy_mask = pdata->phy_mask;
599
600 err = mdiobus_register(bp->mii_bus);
601 }
602
603 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100604 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200605
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200606 err = macb_mii_probe(bp->dev);
607 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200608 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200609
610 return 0;
611
612err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700613 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700614err_out_free_mdiobus:
615 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200616err_out:
617 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100618}
619
620static void macb_update_stats(struct macb *bp)
621{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000622 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
623 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300624 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100625
626 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
627
Moritz Fischer96ec6312016-03-29 19:11:11 -0700628 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700629 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100630}
631
Nicolas Ferree86cd532012-10-31 06:04:57 +0000632static int macb_halt_tx(struct macb *bp)
633{
634 unsigned long halt_time, timeout;
635 u32 status;
636
637 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
638
639 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
640 do {
641 halt_time = jiffies;
642 status = macb_readl(bp, TSR);
643 if (!(status & MACB_BIT(TGO)))
644 return 0;
645
646 usleep_range(10, 250);
647 } while (time_before(halt_time, timeout));
648
649 return -ETIMEDOUT;
650}
651
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200652static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
653{
654 if (tx_skb->mapping) {
655 if (tx_skb->mapped_as_page)
656 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
657 tx_skb->size, DMA_TO_DEVICE);
658 else
659 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
660 tx_skb->size, DMA_TO_DEVICE);
661 tx_skb->mapping = 0;
662 }
663
664 if (tx_skb->skb) {
665 dev_kfree_skb_any(tx_skb->skb);
666 tx_skb->skb = NULL;
667 }
668}
669
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000670static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530671{
Harini Katakamfff80192016-08-09 13:15:53 +0530672#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000673 struct macb_dma_desc_64 *desc_64;
674
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100675 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000676 desc_64 = macb_64b_desc(bp, desc);
677 desc_64->addrh = upper_32_bits(addr);
678 }
Harini Katakamfff80192016-08-09 13:15:53 +0530679#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000680 desc->addr = lower_32_bits(addr);
681}
682
683static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
684{
685 dma_addr_t addr = 0;
686#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
687 struct macb_dma_desc_64 *desc_64;
688
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100689 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000690 desc_64 = macb_64b_desc(bp, desc);
691 addr = ((u64)(desc_64->addrh) << 32);
692 }
693#endif
694 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
695 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530696}
697
Nicolas Ferree86cd532012-10-31 06:04:57 +0000698static void macb_tx_error_task(struct work_struct *work)
699{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100700 struct macb_queue *queue = container_of(work, struct macb_queue,
701 tx_error_task);
702 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000703 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100704 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000705 struct sk_buff *skb;
706 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100707 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000708
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100709 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
710 (unsigned int)(queue - bp->queues),
711 queue->tx_tail, queue->tx_head);
712
713 /* Prevent the queue IRQ handlers from running: each of them may call
714 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
715 * As explained below, we have to halt the transmission before updating
716 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
717 * network engine about the macb/gem being halted.
718 */
719 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000720
721 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100722 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000723
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700724 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000725 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100726 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000727 */
728 if (macb_halt_tx(bp))
729 /* Just complain for now, reinitializing TX path can be good */
730 netdev_err(bp->dev, "BUG: halt tx timed out\n");
731
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700732 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000733 * Free transmit buffers in upper layer.
734 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100735 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
736 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000737
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100738 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000739 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100740 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000741 skb = tx_skb->skb;
742
743 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200744 /* skb is set for the last buffer of the frame */
745 while (!skb) {
746 macb_tx_unmap(bp, tx_skb);
747 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100748 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200749 skb = tx_skb->skb;
750 }
751
752 /* ctrl still refers to the first buffer descriptor
753 * since it's the only one written back by the hardware
754 */
755 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
756 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500757 macb_tx_ring_wrap(bp, tail),
758 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200759 bp->dev->stats.tx_packets++;
760 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200761 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000762 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700763 /* "Buffers exhausted mid-frame" errors may only happen
764 * if the driver is buggy, so complain loudly about
765 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000766 */
767 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
768 netdev_err(bp->dev,
769 "BUG: TX buffers exhausted mid-frame\n");
770
771 desc->ctrl = ctrl | MACB_BIT(TX_USED);
772 }
773
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200774 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000775 }
776
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100777 /* Set end of TX queue */
778 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000779 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100780 desc->ctrl = MACB_BIT(TX_USED);
781
Nicolas Ferree86cd532012-10-31 06:04:57 +0000782 /* Make descriptor updates visible to hardware */
783 wmb();
784
785 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000786 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530787#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100788 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000789 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530790#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000791 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100792 queue->tx_head = 0;
793 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000794
795 /* Housework before enabling TX IRQ */
796 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100797 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
798
799 /* Now we are ready to start transmission again */
800 netif_tx_start_all_queues(bp->dev);
801 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
802
803 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000804}
805
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100806static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100807{
808 unsigned int tail;
809 unsigned int head;
810 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100811 struct macb *bp = queue->bp;
812 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100813
814 status = macb_readl(bp, TSR);
815 macb_writel(bp, TSR, status);
816
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000817 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100818 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000819
Nicolas Ferree86cd532012-10-31 06:04:57 +0000820 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700821 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100822
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100823 head = queue->tx_head;
824 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000825 struct macb_tx_skb *tx_skb;
826 struct sk_buff *skb;
827 struct macb_dma_desc *desc;
828 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100829
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100830 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100831
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000832 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100833 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000834
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000835 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100836
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200837 /* TX_USED bit is only set by hardware on the very first buffer
838 * descriptor of the transmitted frame.
839 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000840 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100841 break;
842
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200843 /* Process all buffers of the current transmitted frame */
844 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100845 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200846 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000847
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200848 /* First, update TX stats if needed */
849 if (skb) {
850 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500851 macb_tx_ring_wrap(bp, tail),
852 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200853 bp->dev->stats.tx_packets++;
854 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200855 }
856
857 /* Now we can safely release resources */
858 macb_tx_unmap(bp, tx_skb);
859
860 /* skb is set only for the last buffer of the frame.
861 * WARNING: at this point skb has been freed by
862 * macb_tx_unmap().
863 */
864 if (skb)
865 break;
866 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100867 }
868
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100869 queue->tx_tail = tail;
870 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
871 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500872 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100873 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100874}
875
Nicolas Ferre4df95132013-06-04 21:57:12 +0000876static void gem_rx_refill(struct macb *bp)
877{
878 unsigned int entry;
879 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000880 dma_addr_t paddr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000881 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000882
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700883 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500884 bp->rx_ring_size) > 0) {
885 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000886
887 /* Make hw descriptor updates visible to CPU */
888 rmb();
889
Nicolas Ferre4df95132013-06-04 21:57:12 +0000890 bp->rx_prepared_head++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000891 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000892
Moritz Fischeraa50b552016-03-29 19:11:13 -0700893 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000894 /* allocate sk_buff for this free entry in ring */
895 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700896 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000897 netdev_err(bp->dev,
898 "Unable to allocate sk_buff\n");
899 break;
900 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000901
902 /* now fill corresponding descriptor entry */
903 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700904 bp->rx_buffer_size,
905 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800906 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
907 dev_kfree_skb(skb);
908 break;
909 }
910
911 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000912
Zach Brownb410d132016-10-19 09:56:57 -0500913 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000914 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000915 macb_set_addr(bp, desc, paddr);
916 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000917
918 /* properly align Ethernet header */
919 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530920 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000921 desc->addr &= ~MACB_BIT(RX_USED);
922 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000923 }
924 }
925
926 /* Make descriptor updates visible to hardware */
927 wmb();
928
929 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700930 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000931}
932
933/* Mark DMA descriptors from begin up to and not including end as unused */
934static void discard_partial_frame(struct macb *bp, unsigned int begin,
935 unsigned int end)
936{
937 unsigned int frag;
938
939 for (frag = begin; frag != end; frag++) {
940 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700941
Nicolas Ferre4df95132013-06-04 21:57:12 +0000942 desc->addr &= ~MACB_BIT(RX_USED);
943 }
944
945 /* Make descriptor updates visible to hardware */
946 wmb();
947
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700948 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000949 * whatever caused this is updated, so we don't have to record
950 * anything.
951 */
952}
953
954static int gem_rx(struct macb *bp, int budget)
955{
956 unsigned int len;
957 unsigned int entry;
958 struct sk_buff *skb;
959 struct macb_dma_desc *desc;
960 int count = 0;
961
962 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530963 u32 ctrl;
964 dma_addr_t addr;
965 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000966
Zach Brownb410d132016-10-19 09:56:57 -0500967 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000968 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000969
970 /* Make hw descriptor updates visible to CPU */
971 rmb();
972
Harini Katakamfff80192016-08-09 13:15:53 +0530973 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000974 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000975 ctrl = desc->ctrl;
976
Harini Katakamfff80192016-08-09 13:15:53 +0530977 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000978 break;
979
Nicolas Ferre4df95132013-06-04 21:57:12 +0000980 bp->rx_tail++;
981 count++;
982
983 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
984 netdev_err(bp->dev,
985 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200986 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000987 break;
988 }
989 skb = bp->rx_skbuff[entry];
990 if (unlikely(!skb)) {
991 netdev_err(bp->dev,
992 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200993 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000994 break;
995 }
996 /* now everything is ready for receiving packet */
997 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530998 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000999
1000 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1001
1002 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001003 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001004 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001005
1006 skb->protocol = eth_type_trans(skb, bp->dev);
1007 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001008 if (bp->dev->features & NETIF_F_RXCSUM &&
1009 !(bp->dev->flags & IFF_PROMISC) &&
1010 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1011 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001012
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001013 bp->dev->stats.rx_packets++;
1014 bp->dev->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001015
1016#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1017 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1018 skb->len, skb->csum);
1019 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001020 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001021 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1022 skb->data, 32, true);
1023#endif
1024
1025 netif_receive_skb(skb);
1026 }
1027
1028 gem_rx_refill(bp);
1029
1030 return count;
1031}
1032
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001033static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
1034 unsigned int last_frag)
1035{
1036 unsigned int len;
1037 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001038 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001039 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001040 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001041
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001042 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301043 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001044
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001045 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001046 macb_rx_ring_wrap(bp, first_frag),
1047 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001048
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001049 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001050 * first buffer. Since the header is 14 bytes, this makes the
1051 * payload word-aligned.
1052 *
1053 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1054 * the two padding bytes into the skb so that we avoid hitting
1055 * the slowpath in memcpy(), and pull them off afterwards.
1056 */
1057 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001058 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001059 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001060 for (frag = first_frag; ; frag++) {
1061 desc = macb_rx_desc(bp, frag);
1062 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001063 if (frag == last_frag)
1064 break;
1065 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001066
1067 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001068 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001069
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001070 return 1;
1071 }
1072
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001073 offset = 0;
1074 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001075 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076 skb_put(skb, len);
1077
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001078 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001079 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001080
1081 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001082 if (unlikely(frag != last_frag)) {
1083 dev_kfree_skb_any(skb);
1084 return -1;
1085 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001086 frag_len = len - offset;
1087 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001088 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001089 macb_rx_buffer(bp, frag),
1090 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001091 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001092 desc = macb_rx_desc(bp, frag);
1093 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001094
1095 if (frag == last_frag)
1096 break;
1097 }
1098
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001099 /* Make descriptor updates visible to hardware */
1100 wmb();
1101
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001102 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001103 skb->protocol = eth_type_trans(skb, bp->dev);
1104
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001105 bp->dev->stats.rx_packets++;
1106 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001107 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001108 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001109 netif_receive_skb(skb);
1110
1111 return 0;
1112}
1113
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001114static inline void macb_init_rx_ring(struct macb *bp)
1115{
1116 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001117 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001118 int i;
1119
1120 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001121 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001122 desc = macb_rx_desc(bp, i);
1123 macb_set_addr(bp, desc, addr);
1124 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001125 addr += bp->rx_buffer_size;
1126 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001127 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchena0b44ee2016-11-28 14:40:55 +01001128 bp->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001129}
1130
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001131static int macb_rx(struct macb *bp, int budget)
1132{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001133 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001134 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001135 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001136 int first_frag = -1;
1137
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001138 for (tail = bp->rx_tail; budget > 0; tail++) {
1139 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001140 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001141
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001142 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001143 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001144
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001145 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001146
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001147 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001148 break;
1149
1150 if (ctrl & MACB_BIT(RX_SOF)) {
1151 if (first_frag != -1)
1152 discard_partial_frame(bp, first_frag, tail);
1153 first_frag = tail;
1154 }
1155
1156 if (ctrl & MACB_BIT(RX_EOF)) {
1157 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001158
1159 if (unlikely(first_frag == -1)) {
1160 reset_rx_queue = true;
1161 continue;
1162 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001163
1164 dropped = macb_rx_frame(bp, first_frag, tail);
1165 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001166 if (unlikely(dropped < 0)) {
1167 reset_rx_queue = true;
1168 continue;
1169 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001170 if (!dropped) {
1171 received++;
1172 budget--;
1173 }
1174 }
1175 }
1176
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001177 if (unlikely(reset_rx_queue)) {
1178 unsigned long flags;
1179 u32 ctrl;
1180
1181 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1182
1183 spin_lock_irqsave(&bp->lock, flags);
1184
1185 ctrl = macb_readl(bp, NCR);
1186 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1187
1188 macb_init_rx_ring(bp);
1189 macb_writel(bp, RBQP, bp->rx_ring_dma);
1190
1191 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1192
1193 spin_unlock_irqrestore(&bp->lock, flags);
1194 return received;
1195 }
1196
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001197 if (first_frag != -1)
1198 bp->rx_tail = first_frag;
1199 else
1200 bp->rx_tail = tail;
1201
1202 return received;
1203}
1204
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001205static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001206{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001207 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001208 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001209 u32 status;
1210
1211 status = macb_readl(bp, RSR);
1212 macb_writel(bp, RSR, status);
1213
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001214 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001215
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001216 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001217 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001218
Nicolas Ferre4df95132013-06-04 21:57:12 +00001219 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001220 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001221 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001222
Nicolas Ferre8770e912013-02-12 11:08:48 +01001223 /* Packets received while interrupts were disabled */
1224 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001225 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001226 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1227 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001228 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001229 } else {
1230 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1231 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001232 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001233
1234 /* TODO: Handle errors */
1235
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001236 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001237}
1238
1239static irqreturn_t macb_interrupt(int irq, void *dev_id)
1240{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001241 struct macb_queue *queue = dev_id;
1242 struct macb *bp = queue->bp;
1243 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001244 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001245
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001246 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001247
1248 if (unlikely(!status))
1249 return IRQ_NONE;
1250
1251 spin_lock(&bp->lock);
1252
1253 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001254 /* close possible race with dev_close */
1255 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001256 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001257 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1258 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001259 break;
1260 }
1261
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001262 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1263 (unsigned int)(queue - bp->queues),
1264 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001265
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001266 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001267 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001268 * until we have processed the buffers. The
1269 * scheduling call may fail if the poll routine
1270 * is already scheduled, so disable interrupts
1271 * now.
1272 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001273 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001274 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001275 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001276
Ben Hutchings288379f2009-01-19 16:43:59 -08001277 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001278 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001279 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001280 }
1281 }
1282
Nicolas Ferree86cd532012-10-31 06:04:57 +00001283 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001284 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1285 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001286
1287 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001288 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001289
Nicolas Ferree86cd532012-10-31 06:04:57 +00001290 break;
1291 }
1292
1293 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001294 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001295
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001296 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001297 * add that if/when we get our hands on a full-blown MII PHY.
1298 */
1299
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001300 /* There is a hardware issue under heavy load where DMA can
1301 * stop, this causes endless "used buffer descriptor read"
1302 * interrupts but it can be cleared by re-enabling RX. See
1303 * the at91 manual, section 41.3.1 or the Zynq manual
1304 * section 16.7.4 for details.
1305 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001306 if (status & MACB_BIT(RXUBR)) {
1307 ctrl = macb_readl(bp, NCR);
1308 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001309 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001310 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1311
1312 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001313 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001314 }
1315
Alexander Steinb19f7f72011-04-13 05:03:24 +00001316 if (status & MACB_BIT(ISR_ROVR)) {
1317 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001318 if (macb_is_gem(bp))
1319 bp->hw_stats.gem.rx_overruns++;
1320 else
1321 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001322
1323 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001324 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001325 }
1326
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001327 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001328 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001329 * netdev_err to a lower-priority context as well
1330 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001331 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001332 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001333
1334 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001335 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001336 }
1337
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001338 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001339 }
1340
1341 spin_unlock(&bp->lock);
1342
1343 return IRQ_HANDLED;
1344}
1345
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001346#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001347/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001348 * to allow network i/o with interrupts disabled.
1349 */
1350static void macb_poll_controller(struct net_device *dev)
1351{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001352 struct macb *bp = netdev_priv(dev);
1353 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001354 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001355 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001356
1357 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001358 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1359 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001360 local_irq_restore(flags);
1361}
1362#endif
1363
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001364static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001365 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001366 struct sk_buff *skb,
1367 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001368{
1369 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001370 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001371 struct macb_tx_skb *tx_skb = NULL;
1372 struct macb_dma_desc *desc;
1373 unsigned int offset, size, count = 0;
1374 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001375 unsigned int eof = 1, mss_mfs = 0;
1376 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1377
1378 /* LSO */
1379 if (skb_shinfo(skb)->gso_size != 0) {
1380 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1381 /* UDP - UFO */
1382 lso_ctrl = MACB_LSO_UFO_ENABLE;
1383 else
1384 /* TCP - TSO */
1385 lso_ctrl = MACB_LSO_TSO_ENABLE;
1386 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001387
1388 /* First, map non-paged data */
1389 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001390
1391 /* first buffer length */
1392 size = hdrlen;
1393
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001394 offset = 0;
1395 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001396 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001397 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001398
1399 mapping = dma_map_single(&bp->pdev->dev,
1400 skb->data + offset,
1401 size, DMA_TO_DEVICE);
1402 if (dma_mapping_error(&bp->pdev->dev, mapping))
1403 goto dma_error;
1404
1405 /* Save info to properly release resources */
1406 tx_skb->skb = NULL;
1407 tx_skb->mapping = mapping;
1408 tx_skb->size = size;
1409 tx_skb->mapped_as_page = false;
1410
1411 len -= size;
1412 offset += size;
1413 count++;
1414 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001415
1416 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001417 }
1418
1419 /* Then, map paged data from fragments */
1420 for (f = 0; f < nr_frags; f++) {
1421 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1422
1423 len = skb_frag_size(frag);
1424 offset = 0;
1425 while (len) {
1426 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001427 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001428 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001429
1430 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1431 offset, size, DMA_TO_DEVICE);
1432 if (dma_mapping_error(&bp->pdev->dev, mapping))
1433 goto dma_error;
1434
1435 /* Save info to properly release resources */
1436 tx_skb->skb = NULL;
1437 tx_skb->mapping = mapping;
1438 tx_skb->size = size;
1439 tx_skb->mapped_as_page = true;
1440
1441 len -= size;
1442 offset += size;
1443 count++;
1444 tx_head++;
1445 }
1446 }
1447
1448 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001449 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001450 netdev_err(bp->dev, "BUG! empty skb!\n");
1451 return 0;
1452 }
1453
1454 /* This is the last buffer of the frame: save socket buffer */
1455 tx_skb->skb = skb;
1456
1457 /* Update TX ring: update buffer descriptors in reverse order
1458 * to avoid race condition
1459 */
1460
1461 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1462 * to set the end of TX queue
1463 */
1464 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001465 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001466 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001467 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001468 desc->ctrl = ctrl;
1469
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001470 if (lso_ctrl) {
1471 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1472 /* include header and FCS in value given to h/w */
1473 mss_mfs = skb_shinfo(skb)->gso_size +
1474 skb_transport_offset(skb) +
1475 ETH_FCS_LEN;
1476 else /* TSO */ {
1477 mss_mfs = skb_shinfo(skb)->gso_size;
1478 /* TCP Sequence Number Source Select
1479 * can be set only for TSO
1480 */
1481 seq_ctrl = 0;
1482 }
1483 }
1484
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001485 do {
1486 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001487 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001488 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001489 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001490
1491 ctrl = (u32)tx_skb->size;
1492 if (eof) {
1493 ctrl |= MACB_BIT(TX_LAST);
1494 eof = 0;
1495 }
Zach Brownb410d132016-10-19 09:56:57 -05001496 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001497 ctrl |= MACB_BIT(TX_WRAP);
1498
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001499 /* First descriptor is header descriptor */
1500 if (i == queue->tx_head) {
1501 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1502 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1503 } else
1504 /* Only set MSS/MFS on payload descriptors
1505 * (second or later descriptor)
1506 */
1507 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1508
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001509 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001510 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001511 /* desc->addr must be visible to hardware before clearing
1512 * 'TX_USED' bit in desc->ctrl.
1513 */
1514 wmb();
1515 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001516 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001517
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001518 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001519
1520 return count;
1521
1522dma_error:
1523 netdev_err(bp->dev, "TX DMA map failed\n");
1524
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001525 for (i = queue->tx_head; i != tx_head; i++) {
1526 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001527
1528 macb_tx_unmap(bp, tx_skb);
1529 }
1530
1531 return 0;
1532}
1533
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001534static netdev_features_t macb_features_check(struct sk_buff *skb,
1535 struct net_device *dev,
1536 netdev_features_t features)
1537{
1538 unsigned int nr_frags, f;
1539 unsigned int hdrlen;
1540
1541 /* Validate LSO compatibility */
1542
1543 /* there is only one buffer */
1544 if (!skb_is_nonlinear(skb))
1545 return features;
1546
1547 /* length of header */
1548 hdrlen = skb_transport_offset(skb);
1549 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1550 hdrlen += tcp_hdrlen(skb);
1551
1552 /* For LSO:
1553 * When software supplies two or more payload buffers all payload buffers
1554 * apart from the last must be a multiple of 8 bytes in size.
1555 */
1556 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1557 return features & ~MACB_NETIF_LSO;
1558
1559 nr_frags = skb_shinfo(skb)->nr_frags;
1560 /* No need to check last fragment */
1561 nr_frags--;
1562 for (f = 0; f < nr_frags; f++) {
1563 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1564
1565 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1566 return features & ~MACB_NETIF_LSO;
1567 }
1568 return features;
1569}
1570
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001571static inline int macb_clear_csum(struct sk_buff *skb)
1572{
1573 /* no change for packets without checksum offloading */
1574 if (skb->ip_summed != CHECKSUM_PARTIAL)
1575 return 0;
1576
1577 /* make sure we can modify the header */
1578 if (unlikely(skb_cow_head(skb, 0)))
1579 return -1;
1580
1581 /* initialize checksum field
1582 * This is required - at least for Zynq, which otherwise calculates
1583 * wrong UDP header checksums for UDP packets with UDP data len <=2
1584 */
1585 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1586 return 0;
1587}
1588
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001589static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1590{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001591 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001592 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001593 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001594 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001595 unsigned int desc_cnt, nr_frags, frag_size, f;
1596 unsigned int hdrlen;
1597 bool is_lso, is_udp = 0;
1598
1599 is_lso = (skb_shinfo(skb)->gso_size != 0);
1600
1601 if (is_lso) {
1602 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1603
1604 /* length of headers */
1605 if (is_udp)
1606 /* only queue eth + ip headers separately for UDP */
1607 hdrlen = skb_transport_offset(skb);
1608 else
1609 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1610 if (skb_headlen(skb) < hdrlen) {
1611 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1612 /* if this is required, would need to copy to single buffer */
1613 return NETDEV_TX_BUSY;
1614 }
1615 } else
1616 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001617
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001618#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1619 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001620 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1621 queue_index, skb->len, skb->head, skb->data,
1622 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001623 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1624 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001625#endif
1626
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001627 /* Count how many TX buffer descriptors are needed to send this
1628 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001629 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001630 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001631 if (is_lso && (skb_headlen(skb) > hdrlen))
1632 /* extra header descriptor if also payload in first buffer */
1633 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1634 else
1635 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001636 nr_frags = skb_shinfo(skb)->nr_frags;
1637 for (f = 0; f < nr_frags; f++) {
1638 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001639 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001640 }
1641
Dongdong Deng48719532009-08-23 19:49:07 -07001642 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001643
1644 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001645 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001646 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001647 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001648 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001649 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001650 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001651 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001652 }
1653
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001654 if (macb_clear_csum(skb)) {
1655 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001656 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001657 }
1658
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001659 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001660 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001661 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001662 goto unlock;
1663 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001664
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001665 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001666 wmb();
1667
Richard Cochrane0720922011-06-19 21:51:28 +00001668 skb_tx_timestamp(skb);
1669
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001670 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1671
Zach Brownb410d132016-10-19 09:56:57 -05001672 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001673 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001674
Soren Brinkmann92030902014-03-04 08:46:39 -08001675unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001676 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001677
Patrick McHardy6ed10652009-06-23 06:03:08 +00001678 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001679}
1680
Nicolas Ferre4df95132013-06-04 21:57:12 +00001681static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001682{
1683 if (!macb_is_gem(bp)) {
1684 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1685 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001686 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001687
Nicolas Ferre1b447912013-06-04 21:57:11 +00001688 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001689 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001690 "RX buffer must be multiple of %d bytes, expanding\n",
1691 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001692 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001693 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001694 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001695 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001696
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001697 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001698 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001699}
1700
Nicolas Ferre4df95132013-06-04 21:57:12 +00001701static void gem_free_rx_buffers(struct macb *bp)
1702{
1703 struct sk_buff *skb;
1704 struct macb_dma_desc *desc;
1705 dma_addr_t addr;
1706 int i;
1707
1708 if (!bp->rx_skbuff)
1709 return;
1710
Zach Brownb410d132016-10-19 09:56:57 -05001711 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001712 skb = bp->rx_skbuff[i];
1713
Moritz Fischeraa50b552016-03-29 19:11:13 -07001714 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001715 continue;
1716
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001717 desc = macb_rx_desc(bp, i);
1718 addr = macb_get_addr(bp, desc);
1719
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001720 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001721 DMA_FROM_DEVICE);
1722 dev_kfree_skb_any(skb);
1723 skb = NULL;
1724 }
1725
1726 kfree(bp->rx_skbuff);
1727 bp->rx_skbuff = NULL;
1728}
1729
1730static void macb_free_rx_buffers(struct macb *bp)
1731{
1732 if (bp->rx_buffers) {
1733 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001734 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001735 bp->rx_buffers, bp->rx_buffers_dma);
1736 bp->rx_buffers = NULL;
1737 }
1738}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001739
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001740static void macb_free_consistent(struct macb *bp)
1741{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001742 struct macb_queue *queue;
1743 unsigned int q;
1744
Nicolas Ferre4df95132013-06-04 21:57:12 +00001745 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001746 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001747 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001748 bp->rx_ring, bp->rx_ring_dma);
1749 bp->rx_ring = NULL;
1750 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001751
1752 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1753 kfree(queue->tx_skb);
1754 queue->tx_skb = NULL;
1755 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001756 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001757 queue->tx_ring, queue->tx_ring_dma);
1758 queue->tx_ring = NULL;
1759 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001760 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001761}
1762
1763static int gem_alloc_rx_buffers(struct macb *bp)
1764{
1765 int size;
1766
Zach Brownb410d132016-10-19 09:56:57 -05001767 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001768 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1769 if (!bp->rx_skbuff)
1770 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001771 else
1772 netdev_dbg(bp->dev,
1773 "Allocated %d RX struct sk_buff entries at %p\n",
1774 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001775 return 0;
1776}
1777
1778static int macb_alloc_rx_buffers(struct macb *bp)
1779{
1780 int size;
1781
Zach Brownb410d132016-10-19 09:56:57 -05001782 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001783 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1784 &bp->rx_buffers_dma, GFP_KERNEL);
1785 if (!bp->rx_buffers)
1786 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001787
1788 netdev_dbg(bp->dev,
1789 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1790 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001791 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001792}
1793
1794static int macb_alloc_consistent(struct macb *bp)
1795{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001796 struct macb_queue *queue;
1797 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001798 int size;
1799
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001800 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001801 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001802 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1803 &queue->tx_ring_dma,
1804 GFP_KERNEL);
1805 if (!queue->tx_ring)
1806 goto out_err;
1807 netdev_dbg(bp->dev,
1808 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1809 q, size, (unsigned long)queue->tx_ring_dma,
1810 queue->tx_ring);
1811
Zach Brownb410d132016-10-19 09:56:57 -05001812 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001813 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1814 if (!queue->tx_skb)
1815 goto out_err;
1816 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001817
Zach Brownb410d132016-10-19 09:56:57 -05001818 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001819 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1820 &bp->rx_ring_dma, GFP_KERNEL);
1821 if (!bp->rx_ring)
1822 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001823 netdev_dbg(bp->dev,
1824 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1825 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001826
Nicolas Ferre4df95132013-06-04 21:57:12 +00001827 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001828 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001829
1830 return 0;
1831
1832out_err:
1833 macb_free_consistent(bp);
1834 return -ENOMEM;
1835}
1836
Nicolas Ferre4df95132013-06-04 21:57:12 +00001837static void gem_init_rings(struct macb *bp)
1838{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001839 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001840 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001841 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001842 int i;
1843
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001844 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001845 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001846 desc = macb_tx_desc(queue, i);
1847 macb_set_addr(bp, desc, 0);
1848 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001849 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001850 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001851 queue->tx_head = 0;
1852 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001853 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001854
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001855 bp->rx_tail = 0;
1856 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001857
1858 gem_rx_refill(bp);
1859}
1860
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001861static void macb_init_rings(struct macb *bp)
1862{
1863 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001864 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001865
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001866 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001867
Zach Brownb410d132016-10-19 09:56:57 -05001868 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001869 desc = macb_tx_desc(&bp->queues[0], i);
1870 macb_set_addr(bp, desc, 0);
1871 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001872 }
Ben Shelton21d35152015-04-22 17:28:54 -05001873 bp->queues[0].tx_head = 0;
1874 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001875 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001876}
1877
1878static void macb_reset_hw(struct macb *bp)
1879{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001880 struct macb_queue *queue;
1881 unsigned int q;
1882
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001883 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001884 * more gracefully?)
1885 */
1886 macb_writel(bp, NCR, 0);
1887
1888 /* Clear the stats registers (XXX: Update stats first?) */
1889 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1890
1891 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001892 macb_writel(bp, TSR, -1);
1893 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001894
1895 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001896 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1897 queue_writel(queue, IDR, -1);
1898 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001899 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1900 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001901 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001902}
1903
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001904static u32 gem_mdc_clk_div(struct macb *bp)
1905{
1906 u32 config;
1907 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1908
1909 if (pclk_hz <= 20000000)
1910 config = GEM_BF(CLK, GEM_CLK_DIV8);
1911 else if (pclk_hz <= 40000000)
1912 config = GEM_BF(CLK, GEM_CLK_DIV16);
1913 else if (pclk_hz <= 80000000)
1914 config = GEM_BF(CLK, GEM_CLK_DIV32);
1915 else if (pclk_hz <= 120000000)
1916 config = GEM_BF(CLK, GEM_CLK_DIV48);
1917 else if (pclk_hz <= 160000000)
1918 config = GEM_BF(CLK, GEM_CLK_DIV64);
1919 else
1920 config = GEM_BF(CLK, GEM_CLK_DIV96);
1921
1922 return config;
1923}
1924
1925static u32 macb_mdc_clk_div(struct macb *bp)
1926{
1927 u32 config;
1928 unsigned long pclk_hz;
1929
1930 if (macb_is_gem(bp))
1931 return gem_mdc_clk_div(bp);
1932
1933 pclk_hz = clk_get_rate(bp->pclk);
1934 if (pclk_hz <= 20000000)
1935 config = MACB_BF(CLK, MACB_CLK_DIV8);
1936 else if (pclk_hz <= 40000000)
1937 config = MACB_BF(CLK, MACB_CLK_DIV16);
1938 else if (pclk_hz <= 80000000)
1939 config = MACB_BF(CLK, MACB_CLK_DIV32);
1940 else
1941 config = MACB_BF(CLK, MACB_CLK_DIV64);
1942
1943 return config;
1944}
1945
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001946/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001947 * should program. We find the width from decoding the design configuration
1948 * register to find the maximum supported data bus width.
1949 */
1950static u32 macb_dbw(struct macb *bp)
1951{
1952 if (!macb_is_gem(bp))
1953 return 0;
1954
1955 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1956 case 4:
1957 return GEM_BF(DBW, GEM_DBW128);
1958 case 2:
1959 return GEM_BF(DBW, GEM_DBW64);
1960 case 1:
1961 default:
1962 return GEM_BF(DBW, GEM_DBW32);
1963 }
1964}
1965
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001966/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001967 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001968 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001969 * (if not supported by FIFO, it will fallback to default)
1970 * - set both rx/tx packet buffers to full memory size
1971 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001972 */
1973static void macb_configure_dma(struct macb *bp)
1974{
1975 u32 dmacfg;
1976
1977 if (macb_is_gem(bp)) {
1978 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001979 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001980 if (bp->dma_burst_length)
1981 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001982 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301983 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301984
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001985 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301986 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1987 else
1988 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1989
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001990 if (bp->dev->features & NETIF_F_HW_CSUM)
1991 dmacfg |= GEM_BIT(TXCOEN);
1992 else
1993 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301994
1995#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001996 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001997 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05301998#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01001999#ifdef CONFIG_MACB_USE_HWSTAMP
2000 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2001 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2002#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002003 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2004 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002005 gem_writel(bp, DMACFG, dmacfg);
2006 }
2007}
2008
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002009static void macb_init_hw(struct macb *bp)
2010{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002011 struct macb_queue *queue;
2012 unsigned int q;
2013
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002014 u32 config;
2015
2016 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002017 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002018
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002019 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302020 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2021 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002022 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002023 config |= MACB_BIT(PAE); /* PAuse Enable */
2024 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002025 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302026 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2027 else
2028 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002029 if (bp->dev->flags & IFF_PROMISC)
2030 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002031 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2032 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002033 if (!(bp->dev->flags & IFF_BROADCAST))
2034 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002035 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002036 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002037 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302038 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002039 bp->speed = SPEED_10;
2040 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302041 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002042 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302043 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002044
Jamie Iles0116da42011-03-14 17:38:30 +00002045 macb_configure_dma(bp);
2046
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002047 /* Initialize TX and RX buffers */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002048 macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302049#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002050 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002051 macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302052#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002053 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002054 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302055#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002056 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002057 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302058#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002059
2060 /* Enable interrupts */
2061 queue_writel(queue, IER,
2062 MACB_RX_INT_FLAGS |
2063 MACB_TX_INT_FLAGS |
2064 MACB_BIT(HRESP));
2065 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002066
2067 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02002068 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002069}
2070
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002071/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002072 * locations in the memory map. The least significant bits are stored
2073 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2074 *
2075 * The unicast hash enable and the multicast hash enable bits in the
2076 * network configuration register enable the reception of hash matched
2077 * frames. The destination address is reduced to a 6 bit index into
2078 * the 64 bit hash register using the following hash function. The
2079 * hash function is an exclusive or of every sixth bit of the
2080 * destination address.
2081 *
2082 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2083 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2084 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2085 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2086 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2087 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2088 *
2089 * da[0] represents the least significant bit of the first byte
2090 * received, that is, the multicast/unicast indicator, and da[47]
2091 * represents the most significant bit of the last byte received. If
2092 * the hash index, hi[n], points to a bit that is set in the hash
2093 * register then the frame will be matched according to whether the
2094 * frame is multicast or unicast. A multicast match will be signalled
2095 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2096 * index points to a bit set in the hash register. A unicast match
2097 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2098 * and the hash index points to a bit set in the hash register. To
2099 * receive all multicast frames, the hash register should be set with
2100 * all ones and the multicast hash enable bit should be set in the
2101 * network configuration register.
2102 */
2103
2104static inline int hash_bit_value(int bitnr, __u8 *addr)
2105{
2106 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2107 return 1;
2108 return 0;
2109}
2110
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002111/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002112static int hash_get_index(__u8 *addr)
2113{
2114 int i, j, bitval;
2115 int hash_index = 0;
2116
2117 for (j = 0; j < 6; j++) {
2118 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002119 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002120
2121 hash_index |= (bitval << j);
2122 }
2123
2124 return hash_index;
2125}
2126
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002127/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002128static void macb_sethashtable(struct net_device *dev)
2129{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002130 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002131 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002132 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002133 struct macb *bp = netdev_priv(dev);
2134
Moritz Fischeraa50b552016-03-29 19:11:13 -07002135 mc_filter[0] = 0;
2136 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002137
Jiri Pirko22bedad32010-04-01 21:22:57 +00002138 netdev_for_each_mc_addr(ha, dev) {
2139 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002140 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2141 }
2142
Jamie Ilesf75ba502011-11-08 10:12:32 +00002143 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2144 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002145}
2146
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002147/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002148static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002149{
2150 unsigned long cfg;
2151 struct macb *bp = netdev_priv(dev);
2152
2153 cfg = macb_readl(bp, NCFGR);
2154
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002155 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002156 /* Enable promiscuous mode */
2157 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002158
2159 /* Disable RX checksum offload */
2160 if (macb_is_gem(bp))
2161 cfg &= ~GEM_BIT(RXCOEN);
2162 } else {
2163 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002164 cfg &= ~MACB_BIT(CAF);
2165
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002166 /* Enable RX checksum offload only if requested */
2167 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2168 cfg |= GEM_BIT(RXCOEN);
2169 }
2170
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002171 if (dev->flags & IFF_ALLMULTI) {
2172 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002173 macb_or_gem_writel(bp, HRB, -1);
2174 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002175 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002176 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002177 /* Enable specific multicasts */
2178 macb_sethashtable(dev);
2179 cfg |= MACB_BIT(NCFGR_MTI);
2180 } else if (dev->flags & (~IFF_ALLMULTI)) {
2181 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002182 macb_or_gem_writel(bp, HRB, 0);
2183 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002184 cfg &= ~MACB_BIT(NCFGR_MTI);
2185 }
2186
2187 macb_writel(bp, NCFGR, cfg);
2188}
2189
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002190static int macb_open(struct net_device *dev)
2191{
2192 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002193 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002194 int err;
2195
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002196 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002197
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002198 /* carrier starts down */
2199 netif_carrier_off(dev);
2200
frederic RODO6c36a702007-07-12 19:07:24 +02002201 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002202 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002203 return -EAGAIN;
2204
Nicolas Ferre1b447912013-06-04 21:57:11 +00002205 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002206 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002207
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002208 err = macb_alloc_consistent(bp);
2209 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002210 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2211 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002212 return err;
2213 }
2214
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002215 napi_enable(&bp->napi);
2216
Nicolas Ferre4df95132013-06-04 21:57:12 +00002217 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002218 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002219
frederic RODO6c36a702007-07-12 19:07:24 +02002220 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002221 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002222
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002223 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002224
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002225 if (bp->ptp_info)
2226 bp->ptp_info->ptp_init(dev);
2227
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002228 return 0;
2229}
2230
2231static int macb_close(struct net_device *dev)
2232{
2233 struct macb *bp = netdev_priv(dev);
2234 unsigned long flags;
2235
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002236 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002237 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002238
Philippe Reynes0a912812016-06-22 00:32:35 +02002239 if (dev->phydev)
2240 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002241
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002242 spin_lock_irqsave(&bp->lock, flags);
2243 macb_reset_hw(bp);
2244 netif_carrier_off(dev);
2245 spin_unlock_irqrestore(&bp->lock, flags);
2246
2247 macb_free_consistent(bp);
2248
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002249 if (bp->ptp_info)
2250 bp->ptp_info->ptp_remove(dev);
2251
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002252 return 0;
2253}
2254
Harini Katakama5898ea2015-05-06 22:27:18 +05302255static int macb_change_mtu(struct net_device *dev, int new_mtu)
2256{
Harini Katakama5898ea2015-05-06 22:27:18 +05302257 if (netif_running(dev))
2258 return -EBUSY;
2259
Harini Katakama5898ea2015-05-06 22:27:18 +05302260 dev->mtu = new_mtu;
2261
2262 return 0;
2263}
2264
Jamie Ilesa494ed82011-03-09 16:26:35 +00002265static void gem_update_stats(struct macb *bp)
2266{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002267 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002268 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002269
Xander Huff3ff13f12015-01-13 16:15:51 -06002270 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2271 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002272 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002273
2274 bp->ethtool_stats[i] += val;
2275 *p += val;
2276
2277 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2278 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002279 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002280 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002281 *(++p) += val;
2282 }
2283 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002284}
2285
2286static struct net_device_stats *gem_get_stats(struct macb *bp)
2287{
2288 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002289 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002290
2291 gem_update_stats(bp);
2292
2293 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2294 hwstat->rx_alignment_errors +
2295 hwstat->rx_resource_errors +
2296 hwstat->rx_overruns +
2297 hwstat->rx_oversize_frames +
2298 hwstat->rx_jabbers +
2299 hwstat->rx_undersized_frames +
2300 hwstat->rx_length_field_frame_errors);
2301 nstat->tx_errors = (hwstat->tx_late_collisions +
2302 hwstat->tx_excessive_collisions +
2303 hwstat->tx_underrun +
2304 hwstat->tx_carrier_sense_errors);
2305 nstat->multicast = hwstat->rx_multicast_frames;
2306 nstat->collisions = (hwstat->tx_single_collision_frames +
2307 hwstat->tx_multiple_collision_frames +
2308 hwstat->tx_excessive_collisions);
2309 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2310 hwstat->rx_jabbers +
2311 hwstat->rx_undersized_frames +
2312 hwstat->rx_length_field_frame_errors);
2313 nstat->rx_over_errors = hwstat->rx_resource_errors;
2314 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2315 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2316 nstat->rx_fifo_errors = hwstat->rx_overruns;
2317 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2318 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2319 nstat->tx_fifo_errors = hwstat->tx_underrun;
2320
2321 return nstat;
2322}
2323
Xander Huff3ff13f12015-01-13 16:15:51 -06002324static void gem_get_ethtool_stats(struct net_device *dev,
2325 struct ethtool_stats *stats, u64 *data)
2326{
2327 struct macb *bp;
2328
2329 bp = netdev_priv(dev);
2330 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002331 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002332}
2333
2334static int gem_get_sset_count(struct net_device *dev, int sset)
2335{
2336 switch (sset) {
2337 case ETH_SS_STATS:
2338 return GEM_STATS_LEN;
2339 default:
2340 return -EOPNOTSUPP;
2341 }
2342}
2343
2344static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2345{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002346 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002347
2348 switch (sset) {
2349 case ETH_SS_STATS:
2350 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2351 memcpy(p, gem_statistics[i].stat_string,
2352 ETH_GSTRING_LEN);
2353 break;
2354 }
2355}
2356
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002357static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002358{
2359 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002360 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002361 struct macb_stats *hwstat = &bp->hw_stats.macb;
2362
2363 if (macb_is_gem(bp))
2364 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002365
frederic RODO6c36a702007-07-12 19:07:24 +02002366 /* read stats from hardware */
2367 macb_update_stats(bp);
2368
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002369 /* Convert HW stats into netdevice stats */
2370 nstat->rx_errors = (hwstat->rx_fcs_errors +
2371 hwstat->rx_align_errors +
2372 hwstat->rx_resource_errors +
2373 hwstat->rx_overruns +
2374 hwstat->rx_oversize_pkts +
2375 hwstat->rx_jabbers +
2376 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002377 hwstat->rx_length_mismatch);
2378 nstat->tx_errors = (hwstat->tx_late_cols +
2379 hwstat->tx_excessive_cols +
2380 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002381 hwstat->tx_carrier_errors +
2382 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002383 nstat->collisions = (hwstat->tx_single_cols +
2384 hwstat->tx_multiple_cols +
2385 hwstat->tx_excessive_cols);
2386 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2387 hwstat->rx_jabbers +
2388 hwstat->rx_undersize_pkts +
2389 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002390 nstat->rx_over_errors = hwstat->rx_resource_errors +
2391 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002392 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2393 nstat->rx_frame_errors = hwstat->rx_align_errors;
2394 nstat->rx_fifo_errors = hwstat->rx_overruns;
2395 /* XXX: What does "missed" mean? */
2396 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2397 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2398 nstat->tx_fifo_errors = hwstat->tx_underruns;
2399 /* Don't know about heartbeat or window errors... */
2400
2401 return nstat;
2402}
2403
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002404static int macb_get_regs_len(struct net_device *netdev)
2405{
2406 return MACB_GREGS_NBR * sizeof(u32);
2407}
2408
2409static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2410 void *p)
2411{
2412 struct macb *bp = netdev_priv(dev);
2413 unsigned int tail, head;
2414 u32 *regs_buff = p;
2415
2416 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2417 | MACB_GREGS_VERSION;
2418
Zach Brownb410d132016-10-19 09:56:57 -05002419 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2420 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002421
2422 regs_buff[0] = macb_readl(bp, NCR);
2423 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2424 regs_buff[2] = macb_readl(bp, NSR);
2425 regs_buff[3] = macb_readl(bp, TSR);
2426 regs_buff[4] = macb_readl(bp, RBQP);
2427 regs_buff[5] = macb_readl(bp, TBQP);
2428 regs_buff[6] = macb_readl(bp, RSR);
2429 regs_buff[7] = macb_readl(bp, IMR);
2430
2431 regs_buff[8] = tail;
2432 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002433 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2434 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002435
Neil Armstrongce721a72016-01-05 14:39:16 +01002436 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2437 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002438 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002439 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002440}
2441
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002442static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2443{
2444 struct macb *bp = netdev_priv(netdev);
2445
2446 wol->supported = 0;
2447 wol->wolopts = 0;
2448
2449 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2450 wol->supported = WAKE_MAGIC;
2451
2452 if (bp->wol & MACB_WOL_ENABLED)
2453 wol->wolopts |= WAKE_MAGIC;
2454 }
2455}
2456
2457static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2458{
2459 struct macb *bp = netdev_priv(netdev);
2460
2461 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2462 (wol->wolopts & ~WAKE_MAGIC))
2463 return -EOPNOTSUPP;
2464
2465 if (wol->wolopts & WAKE_MAGIC)
2466 bp->wol |= MACB_WOL_ENABLED;
2467 else
2468 bp->wol &= ~MACB_WOL_ENABLED;
2469
2470 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2471
2472 return 0;
2473}
2474
Zach Brown8441bb32016-10-19 09:56:58 -05002475static void macb_get_ringparam(struct net_device *netdev,
2476 struct ethtool_ringparam *ring)
2477{
2478 struct macb *bp = netdev_priv(netdev);
2479
2480 ring->rx_max_pending = MAX_RX_RING_SIZE;
2481 ring->tx_max_pending = MAX_TX_RING_SIZE;
2482
2483 ring->rx_pending = bp->rx_ring_size;
2484 ring->tx_pending = bp->tx_ring_size;
2485}
2486
2487static int macb_set_ringparam(struct net_device *netdev,
2488 struct ethtool_ringparam *ring)
2489{
2490 struct macb *bp = netdev_priv(netdev);
2491 u32 new_rx_size, new_tx_size;
2492 unsigned int reset = 0;
2493
2494 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2495 return -EINVAL;
2496
2497 new_rx_size = clamp_t(u32, ring->rx_pending,
2498 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2499 new_rx_size = roundup_pow_of_two(new_rx_size);
2500
2501 new_tx_size = clamp_t(u32, ring->tx_pending,
2502 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2503 new_tx_size = roundup_pow_of_two(new_tx_size);
2504
2505 if ((new_tx_size == bp->tx_ring_size) &&
2506 (new_rx_size == bp->rx_ring_size)) {
2507 /* nothing to do */
2508 return 0;
2509 }
2510
2511 if (netif_running(bp->dev)) {
2512 reset = 1;
2513 macb_close(bp->dev);
2514 }
2515
2516 bp->rx_ring_size = new_rx_size;
2517 bp->tx_ring_size = new_tx_size;
2518
2519 if (reset)
2520 macb_open(bp->dev);
2521
2522 return 0;
2523}
2524
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002525static int macb_get_ts_info(struct net_device *netdev,
2526 struct ethtool_ts_info *info)
2527{
2528 struct macb *bp = netdev_priv(netdev);
2529
2530 if (bp->ptp_info)
2531 return bp->ptp_info->get_ts_info(netdev, info);
2532
2533 return ethtool_op_get_ts_info(netdev, info);
2534}
2535
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002536static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002537 .get_regs_len = macb_get_regs_len,
2538 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002539 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002540 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002541 .get_wol = macb_get_wol,
2542 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002543 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2544 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002545 .get_ringparam = macb_get_ringparam,
2546 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002547};
Xander Huff8cd5a562015-01-15 15:55:20 -06002548
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002549static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002550 .get_regs_len = macb_get_regs_len,
2551 .get_regs = macb_get_regs,
2552 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002553 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002554 .get_ethtool_stats = gem_get_ethtool_stats,
2555 .get_strings = gem_get_ethtool_strings,
2556 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002557 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2558 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002559 .get_ringparam = macb_get_ringparam,
2560 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002561};
2562
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002563static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002564{
Philippe Reynes0a912812016-06-22 00:32:35 +02002565 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002566 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002567
2568 if (!netif_running(dev))
2569 return -EINVAL;
2570
frederic RODO6c36a702007-07-12 19:07:24 +02002571 if (!phydev)
2572 return -ENODEV;
2573
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002574 if (!bp->ptp_info)
2575 return phy_mii_ioctl(phydev, rq, cmd);
2576
2577 switch (cmd) {
2578 case SIOCSHWTSTAMP:
2579 return bp->ptp_info->set_hwtst(dev, rq, cmd);
2580 case SIOCGHWTSTAMP:
2581 return bp->ptp_info->get_hwtst(dev, rq);
2582 default:
2583 return phy_mii_ioctl(phydev, rq, cmd);
2584 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002585}
2586
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002587static int macb_set_features(struct net_device *netdev,
2588 netdev_features_t features)
2589{
2590 struct macb *bp = netdev_priv(netdev);
2591 netdev_features_t changed = features ^ netdev->features;
2592
2593 /* TX checksum offload */
2594 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2595 u32 dmacfg;
2596
2597 dmacfg = gem_readl(bp, DMACFG);
2598 if (features & NETIF_F_HW_CSUM)
2599 dmacfg |= GEM_BIT(TXCOEN);
2600 else
2601 dmacfg &= ~GEM_BIT(TXCOEN);
2602 gem_writel(bp, DMACFG, dmacfg);
2603 }
2604
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002605 /* RX checksum offload */
2606 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2607 u32 netcfg;
2608
2609 netcfg = gem_readl(bp, NCFGR);
2610 if (features & NETIF_F_RXCSUM &&
2611 !(netdev->flags & IFF_PROMISC))
2612 netcfg |= GEM_BIT(RXCOEN);
2613 else
2614 netcfg &= ~GEM_BIT(RXCOEN);
2615 gem_writel(bp, NCFGR, netcfg);
2616 }
2617
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002618 return 0;
2619}
2620
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002621static const struct net_device_ops macb_netdev_ops = {
2622 .ndo_open = macb_open,
2623 .ndo_stop = macb_close,
2624 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002625 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002626 .ndo_get_stats = macb_get_stats,
2627 .ndo_do_ioctl = macb_ioctl,
2628 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302629 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002630 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002631#ifdef CONFIG_NET_POLL_CONTROLLER
2632 .ndo_poll_controller = macb_poll_controller,
2633#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002634 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002635 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002636};
2637
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002638/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002639 * and integration options used
2640 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002641static void macb_configure_caps(struct macb *bp,
2642 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002643{
2644 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002645
Nicolas Ferref6970502015-03-31 15:02:01 +02002646 if (dt_conf)
2647 bp->caps = dt_conf->caps;
2648
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002649 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002650 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2651
Nicolas Ferree1755872014-07-24 13:50:58 +02002652 dcfg = gem_readl(bp, DCFG1);
2653 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2654 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2655 dcfg = gem_readl(bp, DCFG2);
2656 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2657 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002658 if (IS_ENABLED(CONFIG_MACB_USE_HWSTAMP) && gem_has_ptp(bp)) {
2659 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
2660 pr_err("GEM doesn't support hardware ptp.\n");
2661 else
2662 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
2663 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002664 }
2665
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002666 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002667}
2668
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002669static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002670 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002671 unsigned int *queue_mask,
2672 unsigned int *num_queues)
2673{
2674 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002675
2676 *queue_mask = 0x1;
2677 *num_queues = 1;
2678
Nicolas Ferreda120112015-03-31 15:02:00 +02002679 /* is it macb or gem ?
2680 *
2681 * We need to read directly from the hardware here because
2682 * we are early in the probe process and don't have the
2683 * MACB_CAPS_MACB_IS_GEM flag positioned
2684 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002685 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002686 return;
2687
2688 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302689 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2690
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002691 *queue_mask |= 0x1;
2692
2693 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2694 if (*queue_mask & (1 << hw_q))
2695 (*num_queues)++;
2696}
2697
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002698static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302699 struct clk **hclk, struct clk **tx_clk,
2700 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002701{
Bartosz Folta83a77e92016-12-14 06:39:15 +00002702 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002703 int err;
2704
Bartosz Folta83a77e92016-12-14 06:39:15 +00002705 pdata = dev_get_platdata(&pdev->dev);
2706 if (pdata) {
2707 *pclk = pdata->pclk;
2708 *hclk = pdata->hclk;
2709 } else {
2710 *pclk = devm_clk_get(&pdev->dev, "pclk");
2711 *hclk = devm_clk_get(&pdev->dev, "hclk");
2712 }
2713
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002714 if (IS_ERR(*pclk)) {
2715 err = PTR_ERR(*pclk);
2716 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2717 return err;
2718 }
2719
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002720 if (IS_ERR(*hclk)) {
2721 err = PTR_ERR(*hclk);
2722 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2723 return err;
2724 }
2725
2726 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2727 if (IS_ERR(*tx_clk))
2728 *tx_clk = NULL;
2729
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302730 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2731 if (IS_ERR(*rx_clk))
2732 *rx_clk = NULL;
2733
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002734 err = clk_prepare_enable(*pclk);
2735 if (err) {
2736 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2737 return err;
2738 }
2739
2740 err = clk_prepare_enable(*hclk);
2741 if (err) {
2742 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2743 goto err_disable_pclk;
2744 }
2745
2746 err = clk_prepare_enable(*tx_clk);
2747 if (err) {
2748 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2749 goto err_disable_hclk;
2750 }
2751
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302752 err = clk_prepare_enable(*rx_clk);
2753 if (err) {
2754 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2755 goto err_disable_txclk;
2756 }
2757
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002758 return 0;
2759
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302760err_disable_txclk:
2761 clk_disable_unprepare(*tx_clk);
2762
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002763err_disable_hclk:
2764 clk_disable_unprepare(*hclk);
2765
2766err_disable_pclk:
2767 clk_disable_unprepare(*pclk);
2768
2769 return err;
2770}
2771
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002772static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002773{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002774 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002775 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002776 struct macb *bp = netdev_priv(dev);
2777 struct macb_queue *queue;
2778 int err;
2779 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002780
Zach Brownb410d132016-10-19 09:56:57 -05002781 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2782 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2783
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002784 /* set the queue register mapping once for all: queue0 has a special
2785 * register mapping but we don't want to test the queue index then
2786 * compute the corresponding register offset at run time.
2787 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002788 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002789 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002790 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002791
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002792 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002793 queue->bp = bp;
2794 if (hw_q) {
2795 queue->ISR = GEM_ISR(hw_q - 1);
2796 queue->IER = GEM_IER(hw_q - 1);
2797 queue->IDR = GEM_IDR(hw_q - 1);
2798 queue->IMR = GEM_IMR(hw_q - 1);
2799 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302800#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002801 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002802 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302803#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002804 } else {
2805 /* queue0 uses legacy registers */
2806 queue->ISR = MACB_ISR;
2807 queue->IER = MACB_IER;
2808 queue->IDR = MACB_IDR;
2809 queue->IMR = MACB_IMR;
2810 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302811#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002812 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002813 queue->TBQPH = MACB_TBQPH;
Harini Katakamfff80192016-08-09 13:15:53 +05302814#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002815 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002816
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002817 /* get irq: here we use the linux queue index, not the hardware
2818 * queue index. the queue irq definitions in the device tree
2819 * must remove the optional gaps that could exist in the
2820 * hardware queue mask.
2821 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002822 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002823 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002824 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002825 if (err) {
2826 dev_err(&pdev->dev,
2827 "Unable to request IRQ %d (error %d)\n",
2828 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002829 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002830 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002831
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002832 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002833 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002834 }
2835
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002836 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002837 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002838
Nicolas Ferre4df95132013-06-04 21:57:12 +00002839 /* setup appropriated routines according to adapter type */
2840 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002841 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002842 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2843 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2844 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2845 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002846 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002847 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002848 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002849 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2850 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2851 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2852 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002853 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002854 }
2855
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002856 /* Set features */
2857 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002858
2859 /* Check LSO capability */
2860 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
2861 dev->hw_features |= MACB_NETIF_LSO;
2862
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002863 /* Checksum offload is only available on gem with packet buffer */
2864 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002865 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002866 if (bp->caps & MACB_CAPS_SG_DISABLED)
2867 dev->hw_features &= ~NETIF_F_SG;
2868 dev->features = dev->hw_features;
2869
Neil Armstrongce721a72016-01-05 14:39:16 +01002870 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2871 val = 0;
2872 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2873 val = GEM_BIT(RGMII);
2874 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002875 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002876 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002877 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002878 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002879
Neil Armstrongce721a72016-01-05 14:39:16 +01002880 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2881 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002882
Neil Armstrongce721a72016-01-05 14:39:16 +01002883 macb_or_gem_writel(bp, USRIO, val);
2884 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002885
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002886 /* Set MII management clock divider */
2887 val = macb_mdc_clk_div(bp);
2888 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302889 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2890 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002891 macb_writel(bp, NCFGR, val);
2892
2893 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002894}
2895
2896#if defined(CONFIG_OF)
2897/* 1518 rounded up */
2898#define AT91ETHER_MAX_RBUFF_SZ 0x600
2899/* max number of receive buffers */
2900#define AT91ETHER_MAX_RX_DESCR 9
2901
2902/* Initialize and start the Receiver and Transmit subsystems */
2903static int at91ether_start(struct net_device *dev)
2904{
2905 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002906 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002907 dma_addr_t addr;
2908 u32 ctl;
2909 int i;
2910
2911 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2912 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002913 macb_dma_desc_get_size(lp)),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002914 &lp->rx_ring_dma, GFP_KERNEL);
2915 if (!lp->rx_ring)
2916 return -ENOMEM;
2917
2918 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2919 AT91ETHER_MAX_RX_DESCR *
2920 AT91ETHER_MAX_RBUFF_SZ,
2921 &lp->rx_buffers_dma, GFP_KERNEL);
2922 if (!lp->rx_buffers) {
2923 dma_free_coherent(&lp->pdev->dev,
2924 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002925 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002926 lp->rx_ring, lp->rx_ring_dma);
2927 lp->rx_ring = NULL;
2928 return -ENOMEM;
2929 }
2930
2931 addr = lp->rx_buffers_dma;
2932 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002933 desc = macb_rx_desc(lp, i);
2934 macb_set_addr(lp, desc, addr);
2935 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002936 addr += AT91ETHER_MAX_RBUFF_SZ;
2937 }
2938
2939 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002940 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002941
2942 /* Reset buffer index */
2943 lp->rx_tail = 0;
2944
2945 /* Program address of descriptor list in Rx Buffer Queue register */
2946 macb_writel(lp, RBQP, lp->rx_ring_dma);
2947
2948 /* Enable Receive and Transmit */
2949 ctl = macb_readl(lp, NCR);
2950 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2951
2952 return 0;
2953}
2954
2955/* Open the ethernet interface */
2956static int at91ether_open(struct net_device *dev)
2957{
2958 struct macb *lp = netdev_priv(dev);
2959 u32 ctl;
2960 int ret;
2961
2962 /* Clear internal statistics */
2963 ctl = macb_readl(lp, NCR);
2964 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2965
2966 macb_set_hwaddr(lp);
2967
2968 ret = at91ether_start(dev);
2969 if (ret)
2970 return ret;
2971
2972 /* Enable MAC interrupts */
2973 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2974 MACB_BIT(RXUBR) |
2975 MACB_BIT(ISR_TUND) |
2976 MACB_BIT(ISR_RLE) |
2977 MACB_BIT(TCOMP) |
2978 MACB_BIT(ISR_ROVR) |
2979 MACB_BIT(HRESP));
2980
2981 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002982 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002983
2984 netif_start_queue(dev);
2985
2986 return 0;
2987}
2988
2989/* Close the interface */
2990static int at91ether_close(struct net_device *dev)
2991{
2992 struct macb *lp = netdev_priv(dev);
2993 u32 ctl;
2994
2995 /* Disable Receiver and Transmitter */
2996 ctl = macb_readl(lp, NCR);
2997 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2998
2999 /* Disable MAC interrupts */
3000 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3001 MACB_BIT(RXUBR) |
3002 MACB_BIT(ISR_TUND) |
3003 MACB_BIT(ISR_RLE) |
3004 MACB_BIT(TCOMP) |
3005 MACB_BIT(ISR_ROVR) |
3006 MACB_BIT(HRESP));
3007
3008 netif_stop_queue(dev);
3009
3010 dma_free_coherent(&lp->pdev->dev,
3011 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003012 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003013 lp->rx_ring, lp->rx_ring_dma);
3014 lp->rx_ring = NULL;
3015
3016 dma_free_coherent(&lp->pdev->dev,
3017 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
3018 lp->rx_buffers, lp->rx_buffers_dma);
3019 lp->rx_buffers = NULL;
3020
3021 return 0;
3022}
3023
3024/* Transmit packet */
3025static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
3026{
3027 struct macb *lp = netdev_priv(dev);
3028
3029 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3030 netif_stop_queue(dev);
3031
3032 /* Store packet information (to free when Tx completed) */
3033 lp->skb = skb;
3034 lp->skb_length = skb->len;
3035 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3036 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003037 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3038 dev_kfree_skb_any(skb);
3039 dev->stats.tx_dropped++;
3040 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3041 return NETDEV_TX_OK;
3042 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003043
3044 /* Set address of the data in the Transmit Address register */
3045 macb_writel(lp, TAR, lp->skb_physaddr);
3046 /* Set length of the packet in the Transmit Control register */
3047 macb_writel(lp, TCR, skb->len);
3048
3049 } else {
3050 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3051 return NETDEV_TX_BUSY;
3052 }
3053
3054 return NETDEV_TX_OK;
3055}
3056
3057/* Extract received frame from buffer descriptors and sent to upper layers.
3058 * (Called from interrupt context)
3059 */
3060static void at91ether_rx(struct net_device *dev)
3061{
3062 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003063 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003064 unsigned char *p_recv;
3065 struct sk_buff *skb;
3066 unsigned int pktlen;
3067
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003068 desc = macb_rx_desc(lp, lp->rx_tail);
3069 while (desc->addr & MACB_BIT(RX_USED)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003070 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003071 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003072 skb = netdev_alloc_skb(dev, pktlen + 2);
3073 if (skb) {
3074 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003075 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003076
3077 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003078 dev->stats.rx_packets++;
3079 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003080 netif_rx(skb);
3081 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003082 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003083 }
3084
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003085 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003086 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003087
3088 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003089 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003090
3091 /* wrap after last buffer */
3092 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3093 lp->rx_tail = 0;
3094 else
3095 lp->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003096
3097 desc = macb_rx_desc(lp, lp->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003098 }
3099}
3100
3101/* MAC interrupt handler */
3102static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3103{
3104 struct net_device *dev = dev_id;
3105 struct macb *lp = netdev_priv(dev);
3106 u32 intstatus, ctl;
3107
3108 /* MAC Interrupt Status register indicates what interrupts are pending.
3109 * It is automatically cleared once read.
3110 */
3111 intstatus = macb_readl(lp, ISR);
3112
3113 /* Receive complete */
3114 if (intstatus & MACB_BIT(RCOMP))
3115 at91ether_rx(dev);
3116
3117 /* Transmit complete */
3118 if (intstatus & MACB_BIT(TCOMP)) {
3119 /* The TCOM bit is set even if the transmission failed */
3120 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003121 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003122
3123 if (lp->skb) {
3124 dev_kfree_skb_irq(lp->skb);
3125 lp->skb = NULL;
3126 dma_unmap_single(NULL, lp->skb_physaddr,
3127 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003128 dev->stats.tx_packets++;
3129 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003130 }
3131 netif_wake_queue(dev);
3132 }
3133
3134 /* Work-around for EMAC Errata section 41.3.1 */
3135 if (intstatus & MACB_BIT(RXUBR)) {
3136 ctl = macb_readl(lp, NCR);
3137 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003138 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003139 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3140 }
3141
3142 if (intstatus & MACB_BIT(ISR_ROVR))
3143 netdev_err(dev, "ROVR error\n");
3144
3145 return IRQ_HANDLED;
3146}
3147
3148#ifdef CONFIG_NET_POLL_CONTROLLER
3149static void at91ether_poll_controller(struct net_device *dev)
3150{
3151 unsigned long flags;
3152
3153 local_irq_save(flags);
3154 at91ether_interrupt(dev->irq, dev);
3155 local_irq_restore(flags);
3156}
3157#endif
3158
3159static const struct net_device_ops at91ether_netdev_ops = {
3160 .ndo_open = at91ether_open,
3161 .ndo_stop = at91ether_close,
3162 .ndo_start_xmit = at91ether_start_xmit,
3163 .ndo_get_stats = macb_get_stats,
3164 .ndo_set_rx_mode = macb_set_rx_mode,
3165 .ndo_set_mac_address = eth_mac_addr,
3166 .ndo_do_ioctl = macb_ioctl,
3167 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003168#ifdef CONFIG_NET_POLL_CONTROLLER
3169 .ndo_poll_controller = at91ether_poll_controller,
3170#endif
3171};
3172
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003173static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303174 struct clk **hclk, struct clk **tx_clk,
3175 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003176{
3177 int err;
3178
3179 *hclk = NULL;
3180 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303181 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003182
3183 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3184 if (IS_ERR(*pclk))
3185 return PTR_ERR(*pclk);
3186
3187 err = clk_prepare_enable(*pclk);
3188 if (err) {
3189 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3190 return err;
3191 }
3192
3193 return 0;
3194}
3195
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003196static int at91ether_init(struct platform_device *pdev)
3197{
3198 struct net_device *dev = platform_get_drvdata(pdev);
3199 struct macb *bp = netdev_priv(dev);
3200 int err;
3201 u32 reg;
3202
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003203 dev->netdev_ops = &at91ether_netdev_ops;
3204 dev->ethtool_ops = &macb_ethtool_ops;
3205
3206 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3207 0, dev->name, dev);
3208 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003209 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003210
3211 macb_writel(bp, NCR, 0);
3212
3213 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3214 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3215 reg |= MACB_BIT(RM9200_RMII);
3216
3217 macb_writel(bp, NCFGR, reg);
3218
3219 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003220}
3221
David S. Miller3cef5c52015-03-09 23:38:02 -04003222static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003223 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003224 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003225 .init = macb_init,
3226};
3227
David S. Miller3cef5c52015-03-09 23:38:02 -04003228static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003229 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3230 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003231 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003232 .init = macb_init,
3233};
3234
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003235static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003236 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003237 .dma_burst_length = 16,
3238 .clk_init = macb_clk_init,
3239 .init = macb_init,
3240};
3241
David S. Miller3cef5c52015-03-09 23:38:02 -04003242static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003243 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3244 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003245 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003246 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003247 .init = macb_init,
3248};
3249
David S. Miller3cef5c52015-03-09 23:38:02 -04003250static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003251 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003252 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003253 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003254 .init = macb_init,
3255};
3256
David S. Miller3cef5c52015-03-09 23:38:02 -04003257static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003258 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003259 .init = at91ether_init,
3260};
3261
Neil Armstronge611b5b2016-01-05 14:39:17 +01003262static const struct macb_config np4_config = {
3263 .caps = MACB_CAPS_USRIO_DISABLED,
3264 .clk_init = macb_clk_init,
3265 .init = macb_init,
3266};
David S. Miller36583eb2015-05-23 01:22:35 -04003267
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303268static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303269 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303270 .dma_burst_length = 16,
3271 .clk_init = macb_clk_init,
3272 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303273 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303274};
3275
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003276static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303277 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003278 .dma_burst_length = 16,
3279 .clk_init = macb_clk_init,
3280 .init = macb_init,
3281};
3282
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003283static const struct of_device_id macb_dt_ids[] = {
3284 { .compatible = "cdns,at32ap7000-macb" },
3285 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3286 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003287 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003288 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3289 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003290 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003291 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3292 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3293 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3294 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303295 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003296 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003297 { /* sentinel */ }
3298};
3299MODULE_DEVICE_TABLE(of, macb_dt_ids);
3300#endif /* CONFIG_OF */
3301
Bartosz Folta83a77e92016-12-14 06:39:15 +00003302static const struct macb_config default_gem_config = {
3303 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
3304 .dma_burst_length = 16,
3305 .clk_init = macb_clk_init,
3306 .init = macb_init,
3307 .jumbo_max_len = 10240,
3308};
3309
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003310static int macb_probe(struct platform_device *pdev)
3311{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003312 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003313 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303314 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003315 = macb_config->clk_init;
3316 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003317 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003318 struct device_node *phy_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303319 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003320 unsigned int queue_mask, num_queues;
3321 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003322 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003323 struct phy_device *phydev;
3324 struct net_device *dev;
3325 struct resource *regs;
3326 void __iomem *mem;
3327 const char *mac;
3328 struct macb *bp;
3329 int err;
3330
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003331 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3332 mem = devm_ioremap_resource(&pdev->dev, regs);
3333 if (IS_ERR(mem))
3334 return PTR_ERR(mem);
3335
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003336 if (np) {
3337 const struct of_device_id *match;
3338
3339 match = of_match_node(macb_dt_ids, np);
3340 if (match && match->data) {
3341 macb_config = match->data;
3342 clk_init = macb_config->clk_init;
3343 init = macb_config->init;
3344 }
3345 }
3346
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303347 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003348 if (err)
3349 return err;
3350
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003351 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003352
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003353 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003354 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003355 if (!dev) {
3356 err = -ENOMEM;
3357 goto err_disable_clocks;
3358 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003359
3360 dev->base_addr = regs->start;
3361
3362 SET_NETDEV_DEV(dev, &pdev->dev);
3363
3364 bp = netdev_priv(dev);
3365 bp->pdev = pdev;
3366 bp->dev = dev;
3367 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003368 bp->native_io = native_io;
3369 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003370 bp->macb_reg_readl = hw_readl_native;
3371 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003372 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003373 bp->macb_reg_readl = hw_readl;
3374 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003375 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003376 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003377 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003378 if (macb_config)
3379 bp->dma_burst_length = macb_config->dma_burst_length;
3380 bp->pclk = pclk;
3381 bp->hclk = hclk;
3382 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303383 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003384 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303385 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303386
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003387 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003388 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003389 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3390 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3391
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003392 spin_lock_init(&bp->lock);
3393
Nicolas Ferread783472015-03-31 15:02:02 +02003394 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003395 macb_configure_caps(bp, macb_config);
3396
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003397#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3398 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
3399 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3400 bp->hw_dma_cap |= HW_DMA_CAP_64B;
3401 }
3402#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003403 platform_set_drvdata(pdev, dev);
3404
3405 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003406 if (dev->irq < 0) {
3407 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003408 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003409 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003410
Jarod Wilson44770e12016-10-17 15:54:17 -04003411 /* MTU range: 68 - 1500 or 10240 */
3412 dev->min_mtu = GEM_MTU_MIN_SIZE;
3413 if (bp->caps & MACB_CAPS_JUMBO)
3414 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3415 else
3416 dev->max_mtu = ETH_DATA_LEN;
3417
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003418 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003419 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003420 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003421 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003422 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003423
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003424 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003425 phy_node = of_get_next_available_child(np, NULL);
3426 if (phy_node) {
3427 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003428
Charles Keepax0e3e7992016-03-28 13:47:42 +01003429 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003430 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003431 gpiod_direction_output(bp->reset_gpio, 1);
3432 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003433 }
3434 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003435
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003436 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003437 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003438 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003439 if (pdata && pdata->is_rmii)
3440 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3441 else
3442 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3443 } else {
3444 bp->phy_interface = err;
3445 }
3446
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003447 /* IP specific init */
3448 err = init(pdev);
3449 if (err)
3450 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003451
Florian Fainellicf669662016-05-02 18:38:45 -07003452 err = macb_mii_init(bp);
3453 if (err)
3454 goto err_out_free_netdev;
3455
Philippe Reynes0a912812016-06-22 00:32:35 +02003456 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003457
3458 netif_carrier_off(dev);
3459
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003460 err = register_netdev(dev);
3461 if (err) {
3462 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003463 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003464 }
3465
Florian Fainellicf669662016-05-02 18:38:45 -07003466 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003467
Bo Shen58798232014-09-13 01:57:49 +02003468 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3469 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3470 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003471
3472 return 0;
3473
Florian Fainellicf669662016-05-02 18:38:45 -07003474err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003475 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003476 mdiobus_unregister(bp->mii_bus);
3477 mdiobus_free(bp->mii_bus);
3478
3479 /* Shutdown the PHY if there is a GPIO reset */
3480 if (bp->reset_gpio)
3481 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003482
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003483err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003484 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003485
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003486err_disable_clocks:
3487 clk_disable_unprepare(tx_clk);
3488 clk_disable_unprepare(hclk);
3489 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303490 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003491
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003492 return err;
3493}
3494
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003495static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003496{
3497 struct net_device *dev;
3498 struct macb *bp;
3499
3500 dev = platform_get_drvdata(pdev);
3501
3502 if (dev) {
3503 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003504 if (dev->phydev)
3505 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003506 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003507 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003508 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003509
3510 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003511 if (bp->reset_gpio)
3512 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003513
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003514 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003515 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003516 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003517 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303518 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02003519 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003520 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003521 }
3522
3523 return 0;
3524}
3525
Michal Simekd23823d2015-01-23 09:36:03 +01003526static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003527{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003528 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003529 struct net_device *netdev = platform_get_drvdata(pdev);
3530 struct macb *bp = netdev_priv(netdev);
3531
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003532 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003533 netif_device_detach(netdev);
3534
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003535 if (bp->wol & MACB_WOL_ENABLED) {
3536 macb_writel(bp, IER, MACB_BIT(WOL));
3537 macb_writel(bp, WOL, MACB_BIT(MAG));
3538 enable_irq_wake(bp->queues[0].irq);
3539 } else {
3540 clk_disable_unprepare(bp->tx_clk);
3541 clk_disable_unprepare(bp->hclk);
3542 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303543 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003544 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003545
3546 return 0;
3547}
3548
Michal Simekd23823d2015-01-23 09:36:03 +01003549static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003550{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003551 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003552 struct net_device *netdev = platform_get_drvdata(pdev);
3553 struct macb *bp = netdev_priv(netdev);
3554
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003555 if (bp->wol & MACB_WOL_ENABLED) {
3556 macb_writel(bp, IDR, MACB_BIT(WOL));
3557 macb_writel(bp, WOL, 0);
3558 disable_irq_wake(bp->queues[0].irq);
3559 } else {
3560 clk_prepare_enable(bp->pclk);
3561 clk_prepare_enable(bp->hclk);
3562 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303563 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003564 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003565
3566 netif_device_attach(netdev);
3567
3568 return 0;
3569}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003570
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003571static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3572
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003573static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003574 .probe = macb_probe,
3575 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003576 .driver = {
3577 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003578 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003579 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003580 },
3581};
3582
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003583module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003584
3585MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003586MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003587MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003588MODULE_ALIAS("platform:macb");