blob: 6df2cad61647a6d7067df5c30371fbd6eda17923 [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
David S. Millerf9c45ae2017-07-03 06:31:05 -070071#define MACB_NETIF_LSO NETIF_F_TSO
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 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000149#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100150 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000151}
152
153#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
154static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
155{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100156 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
157 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
158 return NULL;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000159}
160#endif
161
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000162/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500163static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000164{
Zach Brownb410d132016-10-19 09:56:57 -0500165 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000166}
167
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100168static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
169 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000170{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000171 index = macb_tx_ring_wrap(queue->bp, index);
172 index = macb_adj_dma_desc_idx(queue->bp, index);
173 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000174}
175
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100176static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
177 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000178{
Zach Brownb410d132016-10-19 09:56:57 -0500179 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000180}
181
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100182static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000183{
184 dma_addr_t offset;
185
Zach Brownb410d132016-10-19 09:56:57 -0500186 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000187 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000188
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100189 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000190}
191
Zach Brownb410d132016-10-19 09:56:57 -0500192static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000193{
Zach Brownb410d132016-10-19 09:56:57 -0500194 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195}
196
197static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
198{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000199 index = macb_rx_ring_wrap(bp, index);
200 index = macb_adj_dma_desc_idx(bp, index);
201 return &bp->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000202}
203
204static void *macb_rx_buffer(struct macb *bp, unsigned int index)
205{
Zach Brownb410d132016-10-19 09:56:57 -0500206 return bp->rx_buffers + bp->rx_buffer_size *
207 macb_rx_ring_wrap(bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000208}
209
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300210/* I/O accessors */
211static u32 hw_readl_native(struct macb *bp, int offset)
212{
213 return __raw_readl(bp->regs + offset);
214}
215
216static void hw_writel_native(struct macb *bp, int offset, u32 value)
217{
218 __raw_writel(value, bp->regs + offset);
219}
220
221static u32 hw_readl(struct macb *bp, int offset)
222{
223 return readl_relaxed(bp->regs + offset);
224}
225
226static void hw_writel(struct macb *bp, int offset, u32 value)
227{
228 writel_relaxed(value, bp->regs + offset);
229}
230
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700231/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700232 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300233 * descriptor access.
234 */
235static bool hw_is_native_io(void __iomem *addr)
236{
237 u32 value = MACB_BIT(LLB);
238
239 __raw_writel(value, addr + MACB_NCR);
240 value = __raw_readl(addr + MACB_NCR);
241
242 /* Write 0 back to disable everything */
243 __raw_writel(0, addr + MACB_NCR);
244
245 return value == MACB_BIT(LLB);
246}
247
248static bool hw_is_gem(void __iomem *addr, bool native_io)
249{
250 u32 id;
251
252 if (native_io)
253 id = __raw_readl(addr + MACB_MID);
254 else
255 id = readl_relaxed(addr + MACB_MID);
256
257 return MACB_BFEXT(IDNUM, id) >= 0x2;
258}
259
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100260static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100261{
262 u32 bottom;
263 u16 top;
264
265 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000266 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100267 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000268 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000269
270 /* Clear unused address register sets */
271 macb_or_gem_writel(bp, SA2B, 0);
272 macb_or_gem_writel(bp, SA2T, 0);
273 macb_or_gem_writel(bp, SA3B, 0);
274 macb_or_gem_writel(bp, SA3T, 0);
275 macb_or_gem_writel(bp, SA4B, 0);
276 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100277}
278
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100279static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100280{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000281 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282 u32 bottom;
283 u16 top;
284 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000285 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100286
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900287 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000288
Moritz Fischeraa50b552016-03-29 19:11:13 -0700289 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000290 for (i = 0; i < 4; i++) {
291 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
292 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100293
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000294 if (pdata && pdata->rev_eth_addr) {
295 addr[5] = bottom & 0xff;
296 addr[4] = (bottom >> 8) & 0xff;
297 addr[3] = (bottom >> 16) & 0xff;
298 addr[2] = (bottom >> 24) & 0xff;
299 addr[1] = top & 0xff;
300 addr[0] = (top & 0xff00) >> 8;
301 } else {
302 addr[0] = bottom & 0xff;
303 addr[1] = (bottom >> 8) & 0xff;
304 addr[2] = (bottom >> 16) & 0xff;
305 addr[3] = (bottom >> 24) & 0xff;
306 addr[4] = top & 0xff;
307 addr[5] = (top >> 8) & 0xff;
308 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100309
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000310 if (is_valid_ether_addr(addr)) {
311 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
312 return;
313 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700314 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000315
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300316 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000317 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100318}
319
frederic RODO6c36a702007-07-12 19:07:24 +0200320static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100321{
frederic RODO6c36a702007-07-12 19:07:24 +0200322 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100323 int value;
324
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100325 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
326 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200327 | MACB_BF(PHYA, mii_id)
328 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100329 | MACB_BF(CODE, MACB_MAN_CODE)));
330
frederic RODO6c36a702007-07-12 19:07:24 +0200331 /* wait for end of transfer */
332 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
333 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100334
335 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100336
337 return value;
338}
339
frederic RODO6c36a702007-07-12 19:07:24 +0200340static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
341 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100342{
frederic RODO6c36a702007-07-12 19:07:24 +0200343 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100344
345 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
346 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200347 | MACB_BF(PHYA, mii_id)
348 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100349 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200350 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351
frederic RODO6c36a702007-07-12 19:07:24 +0200352 /* wait for end of transfer */
353 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
354 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100355
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100356 return 0;
357}
358
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800359/**
360 * macb_set_tx_clk() - Set a clock to a new frequency
361 * @clk Pointer to the clock to change
362 * @rate New frequency in Hz
363 * @dev Pointer to the struct net_device
364 */
365static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
366{
367 long ferr, rate, rate_rounded;
368
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100369 if (!clk)
370 return;
371
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800372 switch (speed) {
373 case SPEED_10:
374 rate = 2500000;
375 break;
376 case SPEED_100:
377 rate = 25000000;
378 break;
379 case SPEED_1000:
380 rate = 125000000;
381 break;
382 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800383 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800384 }
385
386 rate_rounded = clk_round_rate(clk, rate);
387 if (rate_rounded < 0)
388 return;
389
390 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
391 * is not satisfied.
392 */
393 ferr = abs(rate_rounded - rate);
394 ferr = DIV_ROUND_UP(ferr, rate / 100000);
395 if (ferr > 5)
396 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700397 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800398
399 if (clk_set_rate(clk, rate_rounded))
400 netdev_err(dev, "adjusting tx_clk failed.\n");
401}
402
frederic RODO6c36a702007-07-12 19:07:24 +0200403static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100404{
frederic RODO6c36a702007-07-12 19:07:24 +0200405 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200406 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200407 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200408 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100409
frederic RODO6c36a702007-07-12 19:07:24 +0200410 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 if (phydev->link) {
413 if ((bp->speed != phydev->speed) ||
414 (bp->duplex != phydev->duplex)) {
415 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100416
frederic RODO6c36a702007-07-12 19:07:24 +0200417 reg = macb_readl(bp, NCFGR);
418 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000419 if (macb_is_gem(bp))
420 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200421
422 if (phydev->duplex)
423 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900424 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200425 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200426 if (phydev->speed == SPEED_1000 &&
427 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000428 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200429
Patrice Vilchez140b7552012-10-31 06:04:50 +0000430 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200431
432 bp->speed = phydev->speed;
433 bp->duplex = phydev->duplex;
434 status_change = 1;
435 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100436 }
437
frederic RODO6c36a702007-07-12 19:07:24 +0200438 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700439 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200440 bp->speed = 0;
441 bp->duplex = -1;
442 }
443 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100444
frederic RODO6c36a702007-07-12 19:07:24 +0200445 status_change = 1;
446 }
447
448 spin_unlock_irqrestore(&bp->lock, flags);
449
450 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000451 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500452 /* Update the TX clock rate if and only if the link is
453 * up and there has been a link change.
454 */
455 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
456
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000457 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000458 netdev_info(dev, "link up (%d/%s)\n",
459 phydev->speed,
460 phydev->duplex == DUPLEX_FULL ?
461 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000462 } else {
463 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000464 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000465 }
frederic RODO6c36a702007-07-12 19:07:24 +0200466 }
467}
468
469/* based on au1000_eth. c*/
470static int macb_mii_probe(struct net_device *dev)
471{
472 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000473 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000474 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000475 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000476 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200477
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200478 if (bp->phy_node) {
479 phydev = of_phy_connect(dev, bp->phy_node,
480 &macb_handle_link_change, 0,
481 bp->phy_interface);
482 if (!phydev)
483 return -ENODEV;
484 } else {
485 phydev = phy_find_first(bp->mii_bus);
486 if (!phydev) {
487 netdev_err(dev, "no PHY found\n");
488 return -ENXIO;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000489 }
frederic RODO6c36a702007-07-12 19:07:24 +0200490
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200491 pdata = dev_get_platdata(&bp->pdev->dev);
492 if (pdata) {
493 if (gpio_is_valid(pdata->phy_irq_pin)) {
494 ret = devm_gpio_request(&bp->pdev->dev,
495 pdata->phy_irq_pin, "phy int");
496 if (!ret) {
497 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
498 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
499 }
500 } else {
501 phydev->irq = PHY_POLL;
502 }
503 }
504
505 /* attach the mac to the phy */
506 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
507 bp->phy_interface);
508 if (ret) {
509 netdev_err(dev, "Could not attach to PHY\n");
510 return ret;
511 }
frederic RODO6c36a702007-07-12 19:07:24 +0200512 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100513
frederic RODO6c36a702007-07-12 19:07:24 +0200514 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200515 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000516 phydev->supported &= PHY_GBIT_FEATURES;
517 else
518 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100519
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500520 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
521 phydev->supported &= ~SUPPORTED_1000baseT_Half;
522
frederic RODO6c36a702007-07-12 19:07:24 +0200523 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100524
frederic RODO6c36a702007-07-12 19:07:24 +0200525 bp->link = 0;
526 bp->speed = 0;
527 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200528
529 return 0;
530}
531
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100532static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200533{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000534 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200535 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200536 int err = -ENXIO, i;
537
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200538 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200539 macb_writel(bp, NCR, MACB_BIT(MPE));
540
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700541 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700542 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200543 err = -ENOMEM;
544 goto err_out;
545 }
546
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700547 bp->mii_bus->name = "MACB_mii_bus";
548 bp->mii_bus->read = &macb_mdio_read;
549 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000550 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700551 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700552 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700553 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900554 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700555
Jamie Iles91523942011-02-28 04:05:25 +0000556 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200557
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200558 np = bp->pdev->dev.of_node;
559 if (np) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200560 if (of_phy_is_fixed_link(np)) {
561 if (of_phy_register_fixed_link(np) < 0) {
562 dev_err(&bp->pdev->dev,
563 "broken fixed-link specification\n");
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200564 goto err_out_unregister_bus;
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200565 }
566 bp->phy_node = of_node_get(np);
567
568 err = mdiobus_register(bp->mii_bus);
569 } else {
570 /* try dt phy registration */
571 err = of_mdiobus_register(bp->mii_bus, np);
572
573 /* fallback to standard phy registration if no phy were
574 * found during dt phy registration
575 */
576 if (!err && !phy_find_first(bp->mii_bus)) {
577 for (i = 0; i < PHY_MAX_ADDR; i++) {
578 struct phy_device *phydev;
579
580 phydev = mdiobus_scan(bp->mii_bus, i);
581 if (IS_ERR(phydev) &&
582 PTR_ERR(phydev) != -ENODEV) {
583 err = PTR_ERR(phydev);
584 break;
585 }
586 }
587
588 if (err)
589 goto err_out_unregister_bus;
590 }
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200591 }
592 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000593 for (i = 0; i < PHY_MAX_ADDR; i++)
594 bp->mii_bus->irq[i] = PHY_POLL;
595
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200596 if (pdata)
597 bp->mii_bus->phy_mask = pdata->phy_mask;
598
599 err = mdiobus_register(bp->mii_bus);
600 }
601
602 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100603 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200604
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200605 err = macb_mii_probe(bp->dev);
606 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200607 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200608
609 return 0;
610
611err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700612 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700613err_out_free_mdiobus:
614 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200615err_out:
616 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100617}
618
619static void macb_update_stats(struct macb *bp)
620{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000621 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
622 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300623 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100624
625 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
626
Moritz Fischer96ec6312016-03-29 19:11:11 -0700627 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700628 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100629}
630
Nicolas Ferree86cd532012-10-31 06:04:57 +0000631static int macb_halt_tx(struct macb *bp)
632{
633 unsigned long halt_time, timeout;
634 u32 status;
635
636 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
637
638 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
639 do {
640 halt_time = jiffies;
641 status = macb_readl(bp, TSR);
642 if (!(status & MACB_BIT(TGO)))
643 return 0;
644
645 usleep_range(10, 250);
646 } while (time_before(halt_time, timeout));
647
648 return -ETIMEDOUT;
649}
650
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200651static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
652{
653 if (tx_skb->mapping) {
654 if (tx_skb->mapped_as_page)
655 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
656 tx_skb->size, DMA_TO_DEVICE);
657 else
658 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
659 tx_skb->size, DMA_TO_DEVICE);
660 tx_skb->mapping = 0;
661 }
662
663 if (tx_skb->skb) {
664 dev_kfree_skb_any(tx_skb->skb);
665 tx_skb->skb = NULL;
666 }
667}
668
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000669static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530670{
Harini Katakamfff80192016-08-09 13:15:53 +0530671#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000672 struct macb_dma_desc_64 *desc_64;
673
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100674 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000675 desc_64 = macb_64b_desc(bp, desc);
676 desc_64->addrh = upper_32_bits(addr);
677 }
Harini Katakamfff80192016-08-09 13:15:53 +0530678#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000679 desc->addr = lower_32_bits(addr);
680}
681
682static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
683{
684 dma_addr_t addr = 0;
685#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
686 struct macb_dma_desc_64 *desc_64;
687
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100688 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000689 desc_64 = macb_64b_desc(bp, desc);
690 addr = ((u64)(desc_64->addrh) << 32);
691 }
692#endif
693 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
694 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530695}
696
Nicolas Ferree86cd532012-10-31 06:04:57 +0000697static void macb_tx_error_task(struct work_struct *work)
698{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100699 struct macb_queue *queue = container_of(work, struct macb_queue,
700 tx_error_task);
701 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000702 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100703 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000704 struct sk_buff *skb;
705 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100706 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000707
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100708 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
709 (unsigned int)(queue - bp->queues),
710 queue->tx_tail, queue->tx_head);
711
712 /* Prevent the queue IRQ handlers from running: each of them may call
713 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
714 * As explained below, we have to halt the transmission before updating
715 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
716 * network engine about the macb/gem being halted.
717 */
718 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000719
720 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100721 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000722
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700723 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000724 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100725 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000726 */
727 if (macb_halt_tx(bp))
728 /* Just complain for now, reinitializing TX path can be good */
729 netdev_err(bp->dev, "BUG: halt tx timed out\n");
730
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700731 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000732 * Free transmit buffers in upper layer.
733 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100734 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
735 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000736
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100737 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000738 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100739 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000740 skb = tx_skb->skb;
741
742 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200743 /* skb is set for the last buffer of the frame */
744 while (!skb) {
745 macb_tx_unmap(bp, tx_skb);
746 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100747 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200748 skb = tx_skb->skb;
749 }
750
751 /* ctrl still refers to the first buffer descriptor
752 * since it's the only one written back by the hardware
753 */
754 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
755 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500756 macb_tx_ring_wrap(bp, tail),
757 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200758 bp->dev->stats.tx_packets++;
759 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200760 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000761 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700762 /* "Buffers exhausted mid-frame" errors may only happen
763 * if the driver is buggy, so complain loudly about
764 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000765 */
766 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
767 netdev_err(bp->dev,
768 "BUG: TX buffers exhausted mid-frame\n");
769
770 desc->ctrl = ctrl | MACB_BIT(TX_USED);
771 }
772
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200773 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000774 }
775
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100776 /* Set end of TX queue */
777 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000778 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100779 desc->ctrl = MACB_BIT(TX_USED);
780
Nicolas Ferree86cd532012-10-31 06:04:57 +0000781 /* Make descriptor updates visible to hardware */
782 wmb();
783
784 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000785 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530786#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100787 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000788 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530789#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000790 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100791 queue->tx_head = 0;
792 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000793
794 /* Housework before enabling TX IRQ */
795 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100796 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
797
798 /* Now we are ready to start transmission again */
799 netif_tx_start_all_queues(bp->dev);
800 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
801
802 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000803}
804
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100805static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100806{
807 unsigned int tail;
808 unsigned int head;
809 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100810 struct macb *bp = queue->bp;
811 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100812
813 status = macb_readl(bp, TSR);
814 macb_writel(bp, TSR, status);
815
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000816 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100817 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000818
Nicolas Ferree86cd532012-10-31 06:04:57 +0000819 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700820 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100821
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100822 head = queue->tx_head;
823 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000824 struct macb_tx_skb *tx_skb;
825 struct sk_buff *skb;
826 struct macb_dma_desc *desc;
827 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100828
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100829 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100830
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000831 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100832 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000833
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000834 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100835
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200836 /* TX_USED bit is only set by hardware on the very first buffer
837 * descriptor of the transmitted frame.
838 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000839 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100840 break;
841
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200842 /* Process all buffers of the current transmitted frame */
843 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100844 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200845 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000846
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200847 /* First, update TX stats if needed */
848 if (skb) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +0100849 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
850 /* skb now belongs to timestamp buffer
851 * and will be removed later
852 */
853 tx_skb->skb = NULL;
854 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200855 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500856 macb_tx_ring_wrap(bp, tail),
857 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200858 bp->dev->stats.tx_packets++;
859 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200860 }
861
862 /* Now we can safely release resources */
863 macb_tx_unmap(bp, tx_skb);
864
865 /* skb is set only for the last buffer of the frame.
866 * WARNING: at this point skb has been freed by
867 * macb_tx_unmap().
868 */
869 if (skb)
870 break;
871 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100872 }
873
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100874 queue->tx_tail = tail;
875 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
876 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500877 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100878 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100879}
880
Nicolas Ferre4df95132013-06-04 21:57:12 +0000881static void gem_rx_refill(struct macb *bp)
882{
883 unsigned int entry;
884 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000885 dma_addr_t paddr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000886 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000887
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700888 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500889 bp->rx_ring_size) > 0) {
890 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000891
892 /* Make hw descriptor updates visible to CPU */
893 rmb();
894
Nicolas Ferre4df95132013-06-04 21:57:12 +0000895 bp->rx_prepared_head++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000896 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000897
Moritz Fischeraa50b552016-03-29 19:11:13 -0700898 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000899 /* allocate sk_buff for this free entry in ring */
900 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700901 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000902 netdev_err(bp->dev,
903 "Unable to allocate sk_buff\n");
904 break;
905 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000906
907 /* now fill corresponding descriptor entry */
908 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700909 bp->rx_buffer_size,
910 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800911 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
912 dev_kfree_skb(skb);
913 break;
914 }
915
916 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000917
Zach Brownb410d132016-10-19 09:56:57 -0500918 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000919 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000920 macb_set_addr(bp, desc, paddr);
921 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000922
923 /* properly align Ethernet header */
924 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530925 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000926 desc->addr &= ~MACB_BIT(RX_USED);
927 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000928 }
929 }
930
931 /* Make descriptor updates visible to hardware */
932 wmb();
933
934 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700935 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000936}
937
938/* Mark DMA descriptors from begin up to and not including end as unused */
939static void discard_partial_frame(struct macb *bp, unsigned int begin,
940 unsigned int end)
941{
942 unsigned int frag;
943
944 for (frag = begin; frag != end; frag++) {
945 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700946
Nicolas Ferre4df95132013-06-04 21:57:12 +0000947 desc->addr &= ~MACB_BIT(RX_USED);
948 }
949
950 /* Make descriptor updates visible to hardware */
951 wmb();
952
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700953 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000954 * whatever caused this is updated, so we don't have to record
955 * anything.
956 */
957}
958
959static int gem_rx(struct macb *bp, int budget)
960{
961 unsigned int len;
962 unsigned int entry;
963 struct sk_buff *skb;
964 struct macb_dma_desc *desc;
965 int count = 0;
966
967 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530968 u32 ctrl;
969 dma_addr_t addr;
970 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000971
Zach Brownb410d132016-10-19 09:56:57 -0500972 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000973 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000974
975 /* Make hw descriptor updates visible to CPU */
976 rmb();
977
Harini Katakamfff80192016-08-09 13:15:53 +0530978 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000979 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000980 ctrl = desc->ctrl;
981
Harini Katakamfff80192016-08-09 13:15:53 +0530982 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000983 break;
984
Nicolas Ferre4df95132013-06-04 21:57:12 +0000985 bp->rx_tail++;
986 count++;
987
988 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
989 netdev_err(bp->dev,
990 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200991 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000992 break;
993 }
994 skb = bp->rx_skbuff[entry];
995 if (unlikely(!skb)) {
996 netdev_err(bp->dev,
997 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200998 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000999 break;
1000 }
1001 /* now everything is ready for receiving packet */
1002 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301003 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001004
1005 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1006
1007 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001008 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001009 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001010
1011 skb->protocol = eth_type_trans(skb, bp->dev);
1012 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001013 if (bp->dev->features & NETIF_F_RXCSUM &&
1014 !(bp->dev->flags & IFF_PROMISC) &&
1015 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1016 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001017
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001018 bp->dev->stats.rx_packets++;
1019 bp->dev->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001020
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001021 gem_ptp_do_rxstamp(bp, skb, desc);
1022
Nicolas Ferre4df95132013-06-04 21:57:12 +00001023#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1024 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1025 skb->len, skb->csum);
1026 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001027 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001028 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1029 skb->data, 32, true);
1030#endif
1031
1032 netif_receive_skb(skb);
1033 }
1034
1035 gem_rx_refill(bp);
1036
1037 return count;
1038}
1039
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001040static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
1041 unsigned int last_frag)
1042{
1043 unsigned int len;
1044 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001045 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001046 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001047 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001048
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001049 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301050 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001051
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001052 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001053 macb_rx_ring_wrap(bp, first_frag),
1054 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001055
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001056 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001057 * first buffer. Since the header is 14 bytes, this makes the
1058 * payload word-aligned.
1059 *
1060 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1061 * the two padding bytes into the skb so that we avoid hitting
1062 * the slowpath in memcpy(), and pull them off afterwards.
1063 */
1064 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001065 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001066 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001067 for (frag = first_frag; ; frag++) {
1068 desc = macb_rx_desc(bp, frag);
1069 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001070 if (frag == last_frag)
1071 break;
1072 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001073
1074 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001075 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001076
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001077 return 1;
1078 }
1079
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001080 offset = 0;
1081 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001082 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083 skb_put(skb, len);
1084
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001085 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001086 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001087
1088 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001089 if (unlikely(frag != last_frag)) {
1090 dev_kfree_skb_any(skb);
1091 return -1;
1092 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001093 frag_len = len - offset;
1094 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001095 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001096 macb_rx_buffer(bp, frag),
1097 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001098 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001099 desc = macb_rx_desc(bp, frag);
1100 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001101
1102 if (frag == last_frag)
1103 break;
1104 }
1105
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001106 /* Make descriptor updates visible to hardware */
1107 wmb();
1108
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001109 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001110 skb->protocol = eth_type_trans(skb, bp->dev);
1111
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001112 bp->dev->stats.rx_packets++;
1113 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001114 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001115 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001116 netif_receive_skb(skb);
1117
1118 return 0;
1119}
1120
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001121static inline void macb_init_rx_ring(struct macb *bp)
1122{
1123 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001124 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001125 int i;
1126
1127 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001128 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001129 desc = macb_rx_desc(bp, i);
1130 macb_set_addr(bp, desc, addr);
1131 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001132 addr += bp->rx_buffer_size;
1133 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001134 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchena0b44ee2016-11-28 14:40:55 +01001135 bp->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001136}
1137
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001138static int macb_rx(struct macb *bp, int budget)
1139{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001140 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001141 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001142 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001143 int first_frag = -1;
1144
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001145 for (tail = bp->rx_tail; budget > 0; tail++) {
1146 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001147 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001148
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001149 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001150 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001151
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001152 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001153
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001154 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001155 break;
1156
1157 if (ctrl & MACB_BIT(RX_SOF)) {
1158 if (first_frag != -1)
1159 discard_partial_frame(bp, first_frag, tail);
1160 first_frag = tail;
1161 }
1162
1163 if (ctrl & MACB_BIT(RX_EOF)) {
1164 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001165
1166 if (unlikely(first_frag == -1)) {
1167 reset_rx_queue = true;
1168 continue;
1169 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001170
1171 dropped = macb_rx_frame(bp, first_frag, tail);
1172 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001173 if (unlikely(dropped < 0)) {
1174 reset_rx_queue = true;
1175 continue;
1176 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001177 if (!dropped) {
1178 received++;
1179 budget--;
1180 }
1181 }
1182 }
1183
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001184 if (unlikely(reset_rx_queue)) {
1185 unsigned long flags;
1186 u32 ctrl;
1187
1188 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1189
1190 spin_lock_irqsave(&bp->lock, flags);
1191
1192 ctrl = macb_readl(bp, NCR);
1193 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1194
1195 macb_init_rx_ring(bp);
1196 macb_writel(bp, RBQP, bp->rx_ring_dma);
1197
1198 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1199
1200 spin_unlock_irqrestore(&bp->lock, flags);
1201 return received;
1202 }
1203
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001204 if (first_frag != -1)
1205 bp->rx_tail = first_frag;
1206 else
1207 bp->rx_tail = tail;
1208
1209 return received;
1210}
1211
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001212static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001213{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001214 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001215 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001216 u32 status;
1217
1218 status = macb_readl(bp, RSR);
1219 macb_writel(bp, RSR, status);
1220
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001221 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001222
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001223 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001224 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001225
Nicolas Ferre4df95132013-06-04 21:57:12 +00001226 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001227 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001228 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229
Nicolas Ferre8770e912013-02-12 11:08:48 +01001230 /* Packets received while interrupts were disabled */
1231 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001232 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001233 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1234 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001235 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001236 } else {
1237 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1238 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001239 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001240
1241 /* TODO: Handle errors */
1242
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001243 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001244}
1245
1246static irqreturn_t macb_interrupt(int irq, void *dev_id)
1247{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001248 struct macb_queue *queue = dev_id;
1249 struct macb *bp = queue->bp;
1250 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001251 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001252
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001253 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001254
1255 if (unlikely(!status))
1256 return IRQ_NONE;
1257
1258 spin_lock(&bp->lock);
1259
1260 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001261 /* close possible race with dev_close */
1262 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001263 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001264 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1265 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001266 break;
1267 }
1268
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001269 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1270 (unsigned int)(queue - bp->queues),
1271 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001272
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001273 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001274 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001275 * until we have processed the buffers. The
1276 * scheduling call may fail if the poll routine
1277 * is already scheduled, so disable interrupts
1278 * now.
1279 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001280 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001281 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001282 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001283
Ben Hutchings288379f2009-01-19 16:43:59 -08001284 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001285 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001286 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001287 }
1288 }
1289
Nicolas Ferree86cd532012-10-31 06:04:57 +00001290 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001291 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1292 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001293
1294 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001295 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001296
Nicolas Ferree86cd532012-10-31 06:04:57 +00001297 break;
1298 }
1299
1300 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001301 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001302
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001303 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001304 * add that if/when we get our hands on a full-blown MII PHY.
1305 */
1306
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001307 /* There is a hardware issue under heavy load where DMA can
1308 * stop, this causes endless "used buffer descriptor read"
1309 * interrupts but it can be cleared by re-enabling RX. See
1310 * the at91 manual, section 41.3.1 or the Zynq manual
1311 * section 16.7.4 for details.
1312 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001313 if (status & MACB_BIT(RXUBR)) {
1314 ctrl = macb_readl(bp, NCR);
1315 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001316 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001317 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1318
1319 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001320 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001321 }
1322
Alexander Steinb19f7f72011-04-13 05:03:24 +00001323 if (status & MACB_BIT(ISR_ROVR)) {
1324 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001325 if (macb_is_gem(bp))
1326 bp->hw_stats.gem.rx_overruns++;
1327 else
1328 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001329
1330 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001331 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001332 }
1333
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001334 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001335 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001336 * netdev_err to a lower-priority context as well
1337 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001338 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001339 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001340
1341 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001342 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001343 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001344 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001345 }
1346
1347 spin_unlock(&bp->lock);
1348
1349 return IRQ_HANDLED;
1350}
1351
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001352#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001353/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001354 * to allow network i/o with interrupts disabled.
1355 */
1356static void macb_poll_controller(struct net_device *dev)
1357{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001358 struct macb *bp = netdev_priv(dev);
1359 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001360 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001361 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001362
1363 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001364 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1365 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001366 local_irq_restore(flags);
1367}
1368#endif
1369
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001370static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001371 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001372 struct sk_buff *skb,
1373 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001374{
1375 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001376 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001377 struct macb_tx_skb *tx_skb = NULL;
1378 struct macb_dma_desc *desc;
1379 unsigned int offset, size, count = 0;
1380 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001381 unsigned int eof = 1, mss_mfs = 0;
1382 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1383
1384 /* LSO */
1385 if (skb_shinfo(skb)->gso_size != 0) {
1386 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1387 /* UDP - UFO */
1388 lso_ctrl = MACB_LSO_UFO_ENABLE;
1389 else
1390 /* TCP - TSO */
1391 lso_ctrl = MACB_LSO_TSO_ENABLE;
1392 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001393
1394 /* First, map non-paged data */
1395 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001396
1397 /* first buffer length */
1398 size = hdrlen;
1399
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001400 offset = 0;
1401 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001402 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001403 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001404
1405 mapping = dma_map_single(&bp->pdev->dev,
1406 skb->data + offset,
1407 size, DMA_TO_DEVICE);
1408 if (dma_mapping_error(&bp->pdev->dev, mapping))
1409 goto dma_error;
1410
1411 /* Save info to properly release resources */
1412 tx_skb->skb = NULL;
1413 tx_skb->mapping = mapping;
1414 tx_skb->size = size;
1415 tx_skb->mapped_as_page = false;
1416
1417 len -= size;
1418 offset += size;
1419 count++;
1420 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001421
1422 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001423 }
1424
1425 /* Then, map paged data from fragments */
1426 for (f = 0; f < nr_frags; f++) {
1427 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1428
1429 len = skb_frag_size(frag);
1430 offset = 0;
1431 while (len) {
1432 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001433 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001434 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001435
1436 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1437 offset, size, DMA_TO_DEVICE);
1438 if (dma_mapping_error(&bp->pdev->dev, mapping))
1439 goto dma_error;
1440
1441 /* Save info to properly release resources */
1442 tx_skb->skb = NULL;
1443 tx_skb->mapping = mapping;
1444 tx_skb->size = size;
1445 tx_skb->mapped_as_page = true;
1446
1447 len -= size;
1448 offset += size;
1449 count++;
1450 tx_head++;
1451 }
1452 }
1453
1454 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001455 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001456 netdev_err(bp->dev, "BUG! empty skb!\n");
1457 return 0;
1458 }
1459
1460 /* This is the last buffer of the frame: save socket buffer */
1461 tx_skb->skb = skb;
1462
1463 /* Update TX ring: update buffer descriptors in reverse order
1464 * to avoid race condition
1465 */
1466
1467 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1468 * to set the end of TX queue
1469 */
1470 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001471 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001472 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001473 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001474 desc->ctrl = ctrl;
1475
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001476 if (lso_ctrl) {
1477 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1478 /* include header and FCS in value given to h/w */
1479 mss_mfs = skb_shinfo(skb)->gso_size +
1480 skb_transport_offset(skb) +
1481 ETH_FCS_LEN;
1482 else /* TSO */ {
1483 mss_mfs = skb_shinfo(skb)->gso_size;
1484 /* TCP Sequence Number Source Select
1485 * can be set only for TSO
1486 */
1487 seq_ctrl = 0;
1488 }
1489 }
1490
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001491 do {
1492 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001493 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001494 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001495 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001496
1497 ctrl = (u32)tx_skb->size;
1498 if (eof) {
1499 ctrl |= MACB_BIT(TX_LAST);
1500 eof = 0;
1501 }
Zach Brownb410d132016-10-19 09:56:57 -05001502 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001503 ctrl |= MACB_BIT(TX_WRAP);
1504
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001505 /* First descriptor is header descriptor */
1506 if (i == queue->tx_head) {
1507 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1508 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1509 } else
1510 /* Only set MSS/MFS on payload descriptors
1511 * (second or later descriptor)
1512 */
1513 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1514
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001515 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001516 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001517 /* desc->addr must be visible to hardware before clearing
1518 * 'TX_USED' bit in desc->ctrl.
1519 */
1520 wmb();
1521 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001522 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001523
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001524 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001525
1526 return count;
1527
1528dma_error:
1529 netdev_err(bp->dev, "TX DMA map failed\n");
1530
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001531 for (i = queue->tx_head; i != tx_head; i++) {
1532 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001533
1534 macb_tx_unmap(bp, tx_skb);
1535 }
1536
1537 return 0;
1538}
1539
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001540static netdev_features_t macb_features_check(struct sk_buff *skb,
1541 struct net_device *dev,
1542 netdev_features_t features)
1543{
1544 unsigned int nr_frags, f;
1545 unsigned int hdrlen;
1546
1547 /* Validate LSO compatibility */
1548
1549 /* there is only one buffer */
1550 if (!skb_is_nonlinear(skb))
1551 return features;
1552
1553 /* length of header */
1554 hdrlen = skb_transport_offset(skb);
1555 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1556 hdrlen += tcp_hdrlen(skb);
1557
1558 /* For LSO:
1559 * When software supplies two or more payload buffers all payload buffers
1560 * apart from the last must be a multiple of 8 bytes in size.
1561 */
1562 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1563 return features & ~MACB_NETIF_LSO;
1564
1565 nr_frags = skb_shinfo(skb)->nr_frags;
1566 /* No need to check last fragment */
1567 nr_frags--;
1568 for (f = 0; f < nr_frags; f++) {
1569 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1570
1571 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1572 return features & ~MACB_NETIF_LSO;
1573 }
1574 return features;
1575}
1576
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001577static inline int macb_clear_csum(struct sk_buff *skb)
1578{
1579 /* no change for packets without checksum offloading */
1580 if (skb->ip_summed != CHECKSUM_PARTIAL)
1581 return 0;
1582
1583 /* make sure we can modify the header */
1584 if (unlikely(skb_cow_head(skb, 0)))
1585 return -1;
1586
1587 /* initialize checksum field
1588 * This is required - at least for Zynq, which otherwise calculates
1589 * wrong UDP header checksums for UDP packets with UDP data len <=2
1590 */
1591 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1592 return 0;
1593}
1594
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001595static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1596{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001597 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001598 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001599 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001600 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001601 unsigned int desc_cnt, nr_frags, frag_size, f;
1602 unsigned int hdrlen;
1603 bool is_lso, is_udp = 0;
1604
1605 is_lso = (skb_shinfo(skb)->gso_size != 0);
1606
1607 if (is_lso) {
1608 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1609
1610 /* length of headers */
1611 if (is_udp)
1612 /* only queue eth + ip headers separately for UDP */
1613 hdrlen = skb_transport_offset(skb);
1614 else
1615 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1616 if (skb_headlen(skb) < hdrlen) {
1617 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1618 /* if this is required, would need to copy to single buffer */
1619 return NETDEV_TX_BUSY;
1620 }
1621 } else
1622 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001623
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001624#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1625 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001626 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1627 queue_index, skb->len, skb->head, skb->data,
1628 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001629 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1630 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001631#endif
1632
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001633 /* Count how many TX buffer descriptors are needed to send this
1634 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001635 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001636 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001637 if (is_lso && (skb_headlen(skb) > hdrlen))
1638 /* extra header descriptor if also payload in first buffer */
1639 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1640 else
1641 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001642 nr_frags = skb_shinfo(skb)->nr_frags;
1643 for (f = 0; f < nr_frags; f++) {
1644 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001645 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001646 }
1647
Dongdong Deng48719532009-08-23 19:49:07 -07001648 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001649
1650 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001651 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001652 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001653 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001654 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001655 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001656 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001657 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001658 }
1659
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001660 if (macb_clear_csum(skb)) {
1661 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001662 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001663 }
1664
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001665 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001666 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001667 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001668 goto unlock;
1669 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001670
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001671 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001672 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00001673 skb_tx_timestamp(skb);
1674
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001675 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1676
Zach Brownb410d132016-10-19 09:56:57 -05001677 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001678 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001679
Soren Brinkmann92030902014-03-04 08:46:39 -08001680unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001681 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001682
Patrick McHardy6ed10652009-06-23 06:03:08 +00001683 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001684}
1685
Nicolas Ferre4df95132013-06-04 21:57:12 +00001686static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001687{
1688 if (!macb_is_gem(bp)) {
1689 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1690 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001691 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001692
Nicolas Ferre1b447912013-06-04 21:57:11 +00001693 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001694 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001695 "RX buffer must be multiple of %d bytes, expanding\n",
1696 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001697 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001698 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001699 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001700 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001701
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001702 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001703 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001704}
1705
Nicolas Ferre4df95132013-06-04 21:57:12 +00001706static void gem_free_rx_buffers(struct macb *bp)
1707{
1708 struct sk_buff *skb;
1709 struct macb_dma_desc *desc;
1710 dma_addr_t addr;
1711 int i;
1712
1713 if (!bp->rx_skbuff)
1714 return;
1715
Zach Brownb410d132016-10-19 09:56:57 -05001716 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001717 skb = bp->rx_skbuff[i];
1718
Moritz Fischeraa50b552016-03-29 19:11:13 -07001719 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001720 continue;
1721
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001722 desc = macb_rx_desc(bp, i);
1723 addr = macb_get_addr(bp, desc);
1724
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001725 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001726 DMA_FROM_DEVICE);
1727 dev_kfree_skb_any(skb);
1728 skb = NULL;
1729 }
1730
1731 kfree(bp->rx_skbuff);
1732 bp->rx_skbuff = NULL;
1733}
1734
1735static void macb_free_rx_buffers(struct macb *bp)
1736{
1737 if (bp->rx_buffers) {
1738 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001739 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001740 bp->rx_buffers, bp->rx_buffers_dma);
1741 bp->rx_buffers = NULL;
1742 }
1743}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001744
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001745static void macb_free_consistent(struct macb *bp)
1746{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001747 struct macb_queue *queue;
1748 unsigned int q;
1749
Nicolas Ferre4df95132013-06-04 21:57:12 +00001750 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001751 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001752 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001753 bp->rx_ring, bp->rx_ring_dma);
1754 bp->rx_ring = NULL;
1755 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001756
1757 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1758 kfree(queue->tx_skb);
1759 queue->tx_skb = NULL;
1760 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001761 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001762 queue->tx_ring, queue->tx_ring_dma);
1763 queue->tx_ring = NULL;
1764 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001765 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001766}
1767
1768static int gem_alloc_rx_buffers(struct macb *bp)
1769{
1770 int size;
1771
Zach Brownb410d132016-10-19 09:56:57 -05001772 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001773 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1774 if (!bp->rx_skbuff)
1775 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001776 else
1777 netdev_dbg(bp->dev,
1778 "Allocated %d RX struct sk_buff entries at %p\n",
1779 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001780 return 0;
1781}
1782
1783static int macb_alloc_rx_buffers(struct macb *bp)
1784{
1785 int size;
1786
Zach Brownb410d132016-10-19 09:56:57 -05001787 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001788 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1789 &bp->rx_buffers_dma, GFP_KERNEL);
1790 if (!bp->rx_buffers)
1791 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001792
1793 netdev_dbg(bp->dev,
1794 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1795 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001796 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001797}
1798
1799static int macb_alloc_consistent(struct macb *bp)
1800{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001801 struct macb_queue *queue;
1802 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001803 int size;
1804
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001805 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001806 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001807 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1808 &queue->tx_ring_dma,
1809 GFP_KERNEL);
1810 if (!queue->tx_ring)
1811 goto out_err;
1812 netdev_dbg(bp->dev,
1813 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1814 q, size, (unsigned long)queue->tx_ring_dma,
1815 queue->tx_ring);
1816
Zach Brownb410d132016-10-19 09:56:57 -05001817 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001818 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1819 if (!queue->tx_skb)
1820 goto out_err;
1821 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001822
Zach Brownb410d132016-10-19 09:56:57 -05001823 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001824 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1825 &bp->rx_ring_dma, GFP_KERNEL);
1826 if (!bp->rx_ring)
1827 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001828 netdev_dbg(bp->dev,
1829 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1830 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001831
Nicolas Ferre4df95132013-06-04 21:57:12 +00001832 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001833 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001834
1835 return 0;
1836
1837out_err:
1838 macb_free_consistent(bp);
1839 return -ENOMEM;
1840}
1841
Nicolas Ferre4df95132013-06-04 21:57:12 +00001842static void gem_init_rings(struct macb *bp)
1843{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001844 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001845 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001846 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001847 int i;
1848
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001849 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001850 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001851 desc = macb_tx_desc(queue, i);
1852 macb_set_addr(bp, desc, 0);
1853 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001854 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001855 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001856 queue->tx_head = 0;
1857 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001858 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001859
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001860 bp->rx_tail = 0;
1861 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001862
1863 gem_rx_refill(bp);
1864}
1865
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001866static void macb_init_rings(struct macb *bp)
1867{
1868 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001869 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001870
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001871 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001872
Zach Brownb410d132016-10-19 09:56:57 -05001873 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001874 desc = macb_tx_desc(&bp->queues[0], i);
1875 macb_set_addr(bp, desc, 0);
1876 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001877 }
Ben Shelton21d35152015-04-22 17:28:54 -05001878 bp->queues[0].tx_head = 0;
1879 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001880 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001881}
1882
1883static void macb_reset_hw(struct macb *bp)
1884{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001885 struct macb_queue *queue;
1886 unsigned int q;
1887
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001888 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001889 * more gracefully?)
1890 */
1891 macb_writel(bp, NCR, 0);
1892
1893 /* Clear the stats registers (XXX: Update stats first?) */
1894 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1895
1896 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001897 macb_writel(bp, TSR, -1);
1898 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001899
1900 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001901 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1902 queue_writel(queue, IDR, -1);
1903 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001904 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1905 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001906 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001907}
1908
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001909static u32 gem_mdc_clk_div(struct macb *bp)
1910{
1911 u32 config;
1912 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1913
1914 if (pclk_hz <= 20000000)
1915 config = GEM_BF(CLK, GEM_CLK_DIV8);
1916 else if (pclk_hz <= 40000000)
1917 config = GEM_BF(CLK, GEM_CLK_DIV16);
1918 else if (pclk_hz <= 80000000)
1919 config = GEM_BF(CLK, GEM_CLK_DIV32);
1920 else if (pclk_hz <= 120000000)
1921 config = GEM_BF(CLK, GEM_CLK_DIV48);
1922 else if (pclk_hz <= 160000000)
1923 config = GEM_BF(CLK, GEM_CLK_DIV64);
1924 else
1925 config = GEM_BF(CLK, GEM_CLK_DIV96);
1926
1927 return config;
1928}
1929
1930static u32 macb_mdc_clk_div(struct macb *bp)
1931{
1932 u32 config;
1933 unsigned long pclk_hz;
1934
1935 if (macb_is_gem(bp))
1936 return gem_mdc_clk_div(bp);
1937
1938 pclk_hz = clk_get_rate(bp->pclk);
1939 if (pclk_hz <= 20000000)
1940 config = MACB_BF(CLK, MACB_CLK_DIV8);
1941 else if (pclk_hz <= 40000000)
1942 config = MACB_BF(CLK, MACB_CLK_DIV16);
1943 else if (pclk_hz <= 80000000)
1944 config = MACB_BF(CLK, MACB_CLK_DIV32);
1945 else
1946 config = MACB_BF(CLK, MACB_CLK_DIV64);
1947
1948 return config;
1949}
1950
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001951/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001952 * should program. We find the width from decoding the design configuration
1953 * register to find the maximum supported data bus width.
1954 */
1955static u32 macb_dbw(struct macb *bp)
1956{
1957 if (!macb_is_gem(bp))
1958 return 0;
1959
1960 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1961 case 4:
1962 return GEM_BF(DBW, GEM_DBW128);
1963 case 2:
1964 return GEM_BF(DBW, GEM_DBW64);
1965 case 1:
1966 default:
1967 return GEM_BF(DBW, GEM_DBW32);
1968 }
1969}
1970
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001971/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001972 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001973 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001974 * (if not supported by FIFO, it will fallback to default)
1975 * - set both rx/tx packet buffers to full memory size
1976 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001977 */
1978static void macb_configure_dma(struct macb *bp)
1979{
1980 u32 dmacfg;
1981
1982 if (macb_is_gem(bp)) {
1983 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001984 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001985 if (bp->dma_burst_length)
1986 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001987 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301988 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301989
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001990 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301991 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1992 else
1993 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1994
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001995 if (bp->dev->features & NETIF_F_HW_CSUM)
1996 dmacfg |= GEM_BIT(TXCOEN);
1997 else
1998 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301999
2000#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002001 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002002 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302003#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002004#ifdef CONFIG_MACB_USE_HWSTAMP
2005 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2006 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2007#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002008 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2009 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002010 gem_writel(bp, DMACFG, dmacfg);
2011 }
2012}
2013
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002014static void macb_init_hw(struct macb *bp)
2015{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002016 struct macb_queue *queue;
2017 unsigned int q;
2018
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002019 u32 config;
2020
2021 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002022 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002023
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002024 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302025 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2026 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002027 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002028 config |= MACB_BIT(PAE); /* PAuse Enable */
2029 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002030 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302031 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2032 else
2033 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002034 if (bp->dev->flags & IFF_PROMISC)
2035 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002036 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2037 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002038 if (!(bp->dev->flags & IFF_BROADCAST))
2039 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002040 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002041 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002042 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302043 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00002044 bp->speed = SPEED_10;
2045 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302046 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002047 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302048 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002049
Jamie Iles0116da42011-03-14 17:38:30 +00002050 macb_configure_dma(bp);
2051
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002052 /* Initialize TX and RX buffers */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002053 macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302054#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002055 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002056 macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302057#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002058 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002059 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302060#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002061 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002062 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302063#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002064
2065 /* Enable interrupts */
2066 queue_writel(queue, IER,
2067 MACB_RX_INT_FLAGS |
2068 MACB_TX_INT_FLAGS |
2069 MACB_BIT(HRESP));
2070 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002071
2072 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02002073 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002074}
2075
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002076/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002077 * locations in the memory map. The least significant bits are stored
2078 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2079 *
2080 * The unicast hash enable and the multicast hash enable bits in the
2081 * network configuration register enable the reception of hash matched
2082 * frames. The destination address is reduced to a 6 bit index into
2083 * the 64 bit hash register using the following hash function. The
2084 * hash function is an exclusive or of every sixth bit of the
2085 * destination address.
2086 *
2087 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2088 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2089 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2090 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2091 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2092 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2093 *
2094 * da[0] represents the least significant bit of the first byte
2095 * received, that is, the multicast/unicast indicator, and da[47]
2096 * represents the most significant bit of the last byte received. If
2097 * the hash index, hi[n], points to a bit that is set in the hash
2098 * register then the frame will be matched according to whether the
2099 * frame is multicast or unicast. A multicast match will be signalled
2100 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2101 * index points to a bit set in the hash register. A unicast match
2102 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2103 * and the hash index points to a bit set in the hash register. To
2104 * receive all multicast frames, the hash register should be set with
2105 * all ones and the multicast hash enable bit should be set in the
2106 * network configuration register.
2107 */
2108
2109static inline int hash_bit_value(int bitnr, __u8 *addr)
2110{
2111 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2112 return 1;
2113 return 0;
2114}
2115
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002116/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002117static int hash_get_index(__u8 *addr)
2118{
2119 int i, j, bitval;
2120 int hash_index = 0;
2121
2122 for (j = 0; j < 6; j++) {
2123 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002124 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002125
2126 hash_index |= (bitval << j);
2127 }
2128
2129 return hash_index;
2130}
2131
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002132/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002133static void macb_sethashtable(struct net_device *dev)
2134{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002135 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002136 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002137 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002138 struct macb *bp = netdev_priv(dev);
2139
Moritz Fischeraa50b552016-03-29 19:11:13 -07002140 mc_filter[0] = 0;
2141 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002142
Jiri Pirko22bedad32010-04-01 21:22:57 +00002143 netdev_for_each_mc_addr(ha, dev) {
2144 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002145 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2146 }
2147
Jamie Ilesf75ba502011-11-08 10:12:32 +00002148 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2149 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002150}
2151
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002152/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002153static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002154{
2155 unsigned long cfg;
2156 struct macb *bp = netdev_priv(dev);
2157
2158 cfg = macb_readl(bp, NCFGR);
2159
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002160 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002161 /* Enable promiscuous mode */
2162 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002163
2164 /* Disable RX checksum offload */
2165 if (macb_is_gem(bp))
2166 cfg &= ~GEM_BIT(RXCOEN);
2167 } else {
2168 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002169 cfg &= ~MACB_BIT(CAF);
2170
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002171 /* Enable RX checksum offload only if requested */
2172 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2173 cfg |= GEM_BIT(RXCOEN);
2174 }
2175
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002176 if (dev->flags & IFF_ALLMULTI) {
2177 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002178 macb_or_gem_writel(bp, HRB, -1);
2179 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002180 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002181 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002182 /* Enable specific multicasts */
2183 macb_sethashtable(dev);
2184 cfg |= MACB_BIT(NCFGR_MTI);
2185 } else if (dev->flags & (~IFF_ALLMULTI)) {
2186 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002187 macb_or_gem_writel(bp, HRB, 0);
2188 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002189 cfg &= ~MACB_BIT(NCFGR_MTI);
2190 }
2191
2192 macb_writel(bp, NCFGR, cfg);
2193}
2194
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002195static int macb_open(struct net_device *dev)
2196{
2197 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002198 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002199 int err;
2200
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002201 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002202
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002203 /* carrier starts down */
2204 netif_carrier_off(dev);
2205
frederic RODO6c36a702007-07-12 19:07:24 +02002206 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002207 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002208 return -EAGAIN;
2209
Nicolas Ferre1b447912013-06-04 21:57:11 +00002210 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002211 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002212
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002213 err = macb_alloc_consistent(bp);
2214 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002215 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2216 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002217 return err;
2218 }
2219
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002220 napi_enable(&bp->napi);
2221
Nicolas Ferre4df95132013-06-04 21:57:12 +00002222 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002223 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002224
frederic RODO6c36a702007-07-12 19:07:24 +02002225 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002226 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002227
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002228 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002229
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002230 if (bp->ptp_info)
2231 bp->ptp_info->ptp_init(dev);
2232
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002233 return 0;
2234}
2235
2236static int macb_close(struct net_device *dev)
2237{
2238 struct macb *bp = netdev_priv(dev);
2239 unsigned long flags;
2240
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002241 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002242 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002243
Philippe Reynes0a912812016-06-22 00:32:35 +02002244 if (dev->phydev)
2245 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002246
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002247 spin_lock_irqsave(&bp->lock, flags);
2248 macb_reset_hw(bp);
2249 netif_carrier_off(dev);
2250 spin_unlock_irqrestore(&bp->lock, flags);
2251
2252 macb_free_consistent(bp);
2253
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002254 if (bp->ptp_info)
2255 bp->ptp_info->ptp_remove(dev);
2256
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002257 return 0;
2258}
2259
Harini Katakama5898ea2015-05-06 22:27:18 +05302260static int macb_change_mtu(struct net_device *dev, int new_mtu)
2261{
Harini Katakama5898ea2015-05-06 22:27:18 +05302262 if (netif_running(dev))
2263 return -EBUSY;
2264
Harini Katakama5898ea2015-05-06 22:27:18 +05302265 dev->mtu = new_mtu;
2266
2267 return 0;
2268}
2269
Jamie Ilesa494ed82011-03-09 16:26:35 +00002270static void gem_update_stats(struct macb *bp)
2271{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002272 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002273 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002274
Xander Huff3ff13f12015-01-13 16:15:51 -06002275 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2276 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002277 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002278
2279 bp->ethtool_stats[i] += val;
2280 *p += val;
2281
2282 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2283 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002284 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002285 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002286 *(++p) += val;
2287 }
2288 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002289}
2290
2291static struct net_device_stats *gem_get_stats(struct macb *bp)
2292{
2293 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002294 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002295
2296 gem_update_stats(bp);
2297
2298 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2299 hwstat->rx_alignment_errors +
2300 hwstat->rx_resource_errors +
2301 hwstat->rx_overruns +
2302 hwstat->rx_oversize_frames +
2303 hwstat->rx_jabbers +
2304 hwstat->rx_undersized_frames +
2305 hwstat->rx_length_field_frame_errors);
2306 nstat->tx_errors = (hwstat->tx_late_collisions +
2307 hwstat->tx_excessive_collisions +
2308 hwstat->tx_underrun +
2309 hwstat->tx_carrier_sense_errors);
2310 nstat->multicast = hwstat->rx_multicast_frames;
2311 nstat->collisions = (hwstat->tx_single_collision_frames +
2312 hwstat->tx_multiple_collision_frames +
2313 hwstat->tx_excessive_collisions);
2314 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2315 hwstat->rx_jabbers +
2316 hwstat->rx_undersized_frames +
2317 hwstat->rx_length_field_frame_errors);
2318 nstat->rx_over_errors = hwstat->rx_resource_errors;
2319 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2320 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2321 nstat->rx_fifo_errors = hwstat->rx_overruns;
2322 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2323 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2324 nstat->tx_fifo_errors = hwstat->tx_underrun;
2325
2326 return nstat;
2327}
2328
Xander Huff3ff13f12015-01-13 16:15:51 -06002329static void gem_get_ethtool_stats(struct net_device *dev,
2330 struct ethtool_stats *stats, u64 *data)
2331{
2332 struct macb *bp;
2333
2334 bp = netdev_priv(dev);
2335 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002336 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002337}
2338
2339static int gem_get_sset_count(struct net_device *dev, int sset)
2340{
2341 switch (sset) {
2342 case ETH_SS_STATS:
2343 return GEM_STATS_LEN;
2344 default:
2345 return -EOPNOTSUPP;
2346 }
2347}
2348
2349static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2350{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002351 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002352
2353 switch (sset) {
2354 case ETH_SS_STATS:
2355 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2356 memcpy(p, gem_statistics[i].stat_string,
2357 ETH_GSTRING_LEN);
2358 break;
2359 }
2360}
2361
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002362static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002363{
2364 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002365 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002366 struct macb_stats *hwstat = &bp->hw_stats.macb;
2367
2368 if (macb_is_gem(bp))
2369 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002370
frederic RODO6c36a702007-07-12 19:07:24 +02002371 /* read stats from hardware */
2372 macb_update_stats(bp);
2373
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002374 /* Convert HW stats into netdevice stats */
2375 nstat->rx_errors = (hwstat->rx_fcs_errors +
2376 hwstat->rx_align_errors +
2377 hwstat->rx_resource_errors +
2378 hwstat->rx_overruns +
2379 hwstat->rx_oversize_pkts +
2380 hwstat->rx_jabbers +
2381 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002382 hwstat->rx_length_mismatch);
2383 nstat->tx_errors = (hwstat->tx_late_cols +
2384 hwstat->tx_excessive_cols +
2385 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002386 hwstat->tx_carrier_errors +
2387 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002388 nstat->collisions = (hwstat->tx_single_cols +
2389 hwstat->tx_multiple_cols +
2390 hwstat->tx_excessive_cols);
2391 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2392 hwstat->rx_jabbers +
2393 hwstat->rx_undersize_pkts +
2394 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002395 nstat->rx_over_errors = hwstat->rx_resource_errors +
2396 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002397 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2398 nstat->rx_frame_errors = hwstat->rx_align_errors;
2399 nstat->rx_fifo_errors = hwstat->rx_overruns;
2400 /* XXX: What does "missed" mean? */
2401 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2402 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2403 nstat->tx_fifo_errors = hwstat->tx_underruns;
2404 /* Don't know about heartbeat or window errors... */
2405
2406 return nstat;
2407}
2408
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002409static int macb_get_regs_len(struct net_device *netdev)
2410{
2411 return MACB_GREGS_NBR * sizeof(u32);
2412}
2413
2414static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2415 void *p)
2416{
2417 struct macb *bp = netdev_priv(dev);
2418 unsigned int tail, head;
2419 u32 *regs_buff = p;
2420
2421 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2422 | MACB_GREGS_VERSION;
2423
Zach Brownb410d132016-10-19 09:56:57 -05002424 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2425 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002426
2427 regs_buff[0] = macb_readl(bp, NCR);
2428 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2429 regs_buff[2] = macb_readl(bp, NSR);
2430 regs_buff[3] = macb_readl(bp, TSR);
2431 regs_buff[4] = macb_readl(bp, RBQP);
2432 regs_buff[5] = macb_readl(bp, TBQP);
2433 regs_buff[6] = macb_readl(bp, RSR);
2434 regs_buff[7] = macb_readl(bp, IMR);
2435
2436 regs_buff[8] = tail;
2437 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002438 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2439 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002440
Neil Armstrongce721a72016-01-05 14:39:16 +01002441 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2442 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002443 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002444 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002445}
2446
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002447static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2448{
2449 struct macb *bp = netdev_priv(netdev);
2450
2451 wol->supported = 0;
2452 wol->wolopts = 0;
2453
2454 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2455 wol->supported = WAKE_MAGIC;
2456
2457 if (bp->wol & MACB_WOL_ENABLED)
2458 wol->wolopts |= WAKE_MAGIC;
2459 }
2460}
2461
2462static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2463{
2464 struct macb *bp = netdev_priv(netdev);
2465
2466 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2467 (wol->wolopts & ~WAKE_MAGIC))
2468 return -EOPNOTSUPP;
2469
2470 if (wol->wolopts & WAKE_MAGIC)
2471 bp->wol |= MACB_WOL_ENABLED;
2472 else
2473 bp->wol &= ~MACB_WOL_ENABLED;
2474
2475 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2476
2477 return 0;
2478}
2479
Zach Brown8441bb32016-10-19 09:56:58 -05002480static void macb_get_ringparam(struct net_device *netdev,
2481 struct ethtool_ringparam *ring)
2482{
2483 struct macb *bp = netdev_priv(netdev);
2484
2485 ring->rx_max_pending = MAX_RX_RING_SIZE;
2486 ring->tx_max_pending = MAX_TX_RING_SIZE;
2487
2488 ring->rx_pending = bp->rx_ring_size;
2489 ring->tx_pending = bp->tx_ring_size;
2490}
2491
2492static int macb_set_ringparam(struct net_device *netdev,
2493 struct ethtool_ringparam *ring)
2494{
2495 struct macb *bp = netdev_priv(netdev);
2496 u32 new_rx_size, new_tx_size;
2497 unsigned int reset = 0;
2498
2499 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2500 return -EINVAL;
2501
2502 new_rx_size = clamp_t(u32, ring->rx_pending,
2503 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2504 new_rx_size = roundup_pow_of_two(new_rx_size);
2505
2506 new_tx_size = clamp_t(u32, ring->tx_pending,
2507 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2508 new_tx_size = roundup_pow_of_two(new_tx_size);
2509
2510 if ((new_tx_size == bp->tx_ring_size) &&
2511 (new_rx_size == bp->rx_ring_size)) {
2512 /* nothing to do */
2513 return 0;
2514 }
2515
2516 if (netif_running(bp->dev)) {
2517 reset = 1;
2518 macb_close(bp->dev);
2519 }
2520
2521 bp->rx_ring_size = new_rx_size;
2522 bp->tx_ring_size = new_tx_size;
2523
2524 if (reset)
2525 macb_open(bp->dev);
2526
2527 return 0;
2528}
2529
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002530#ifdef CONFIG_MACB_USE_HWSTAMP
2531static unsigned int gem_get_tsu_rate(struct macb *bp)
2532{
2533 struct clk *tsu_clk;
2534 unsigned int tsu_rate;
2535
2536 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2537 if (!IS_ERR(tsu_clk))
2538 tsu_rate = clk_get_rate(tsu_clk);
2539 /* try pclk instead */
2540 else if (!IS_ERR(bp->pclk)) {
2541 tsu_clk = bp->pclk;
2542 tsu_rate = clk_get_rate(tsu_clk);
2543 } else
2544 return -ENOTSUPP;
2545 return tsu_rate;
2546}
2547
2548static s32 gem_get_ptp_max_adj(void)
2549{
2550 return 64000000;
2551}
2552
2553static int gem_get_ts_info(struct net_device *dev,
2554 struct ethtool_ts_info *info)
2555{
2556 struct macb *bp = netdev_priv(dev);
2557
2558 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2559 ethtool_op_get_ts_info(dev, info);
2560 return 0;
2561 }
2562
2563 info->so_timestamping =
2564 SOF_TIMESTAMPING_TX_SOFTWARE |
2565 SOF_TIMESTAMPING_RX_SOFTWARE |
2566 SOF_TIMESTAMPING_SOFTWARE |
2567 SOF_TIMESTAMPING_TX_HARDWARE |
2568 SOF_TIMESTAMPING_RX_HARDWARE |
2569 SOF_TIMESTAMPING_RAW_HARDWARE;
2570 info->tx_types =
2571 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2572 (1 << HWTSTAMP_TX_OFF) |
2573 (1 << HWTSTAMP_TX_ON);
2574 info->rx_filters =
2575 (1 << HWTSTAMP_FILTER_NONE) |
2576 (1 << HWTSTAMP_FILTER_ALL);
2577
2578 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2579
2580 return 0;
2581}
2582
2583static struct macb_ptp_info gem_ptp_info = {
2584 .ptp_init = gem_ptp_init,
2585 .ptp_remove = gem_ptp_remove,
2586 .get_ptp_max_adj = gem_get_ptp_max_adj,
2587 .get_tsu_rate = gem_get_tsu_rate,
2588 .get_ts_info = gem_get_ts_info,
2589 .get_hwtst = gem_get_hwtst,
2590 .set_hwtst = gem_set_hwtst,
2591};
2592#endif
2593
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002594static int macb_get_ts_info(struct net_device *netdev,
2595 struct ethtool_ts_info *info)
2596{
2597 struct macb *bp = netdev_priv(netdev);
2598
2599 if (bp->ptp_info)
2600 return bp->ptp_info->get_ts_info(netdev, info);
2601
2602 return ethtool_op_get_ts_info(netdev, info);
2603}
2604
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002605static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002606 .get_regs_len = macb_get_regs_len,
2607 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002608 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002609 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002610 .get_wol = macb_get_wol,
2611 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002612 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2613 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002614 .get_ringparam = macb_get_ringparam,
2615 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002616};
Xander Huff8cd5a562015-01-15 15:55:20 -06002617
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002618static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002619 .get_regs_len = macb_get_regs_len,
2620 .get_regs = macb_get_regs,
2621 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002622 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002623 .get_ethtool_stats = gem_get_ethtool_stats,
2624 .get_strings = gem_get_ethtool_strings,
2625 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002626 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2627 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002628 .get_ringparam = macb_get_ringparam,
2629 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002630};
2631
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002632static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002633{
Philippe Reynes0a912812016-06-22 00:32:35 +02002634 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002635 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002636
2637 if (!netif_running(dev))
2638 return -EINVAL;
2639
frederic RODO6c36a702007-07-12 19:07:24 +02002640 if (!phydev)
2641 return -ENODEV;
2642
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002643 if (!bp->ptp_info)
2644 return phy_mii_ioctl(phydev, rq, cmd);
2645
2646 switch (cmd) {
2647 case SIOCSHWTSTAMP:
2648 return bp->ptp_info->set_hwtst(dev, rq, cmd);
2649 case SIOCGHWTSTAMP:
2650 return bp->ptp_info->get_hwtst(dev, rq);
2651 default:
2652 return phy_mii_ioctl(phydev, rq, cmd);
2653 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002654}
2655
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002656static int macb_set_features(struct net_device *netdev,
2657 netdev_features_t features)
2658{
2659 struct macb *bp = netdev_priv(netdev);
2660 netdev_features_t changed = features ^ netdev->features;
2661
2662 /* TX checksum offload */
2663 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2664 u32 dmacfg;
2665
2666 dmacfg = gem_readl(bp, DMACFG);
2667 if (features & NETIF_F_HW_CSUM)
2668 dmacfg |= GEM_BIT(TXCOEN);
2669 else
2670 dmacfg &= ~GEM_BIT(TXCOEN);
2671 gem_writel(bp, DMACFG, dmacfg);
2672 }
2673
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002674 /* RX checksum offload */
2675 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2676 u32 netcfg;
2677
2678 netcfg = gem_readl(bp, NCFGR);
2679 if (features & NETIF_F_RXCSUM &&
2680 !(netdev->flags & IFF_PROMISC))
2681 netcfg |= GEM_BIT(RXCOEN);
2682 else
2683 netcfg &= ~GEM_BIT(RXCOEN);
2684 gem_writel(bp, NCFGR, netcfg);
2685 }
2686
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002687 return 0;
2688}
2689
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002690static const struct net_device_ops macb_netdev_ops = {
2691 .ndo_open = macb_open,
2692 .ndo_stop = macb_close,
2693 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002694 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002695 .ndo_get_stats = macb_get_stats,
2696 .ndo_do_ioctl = macb_ioctl,
2697 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302698 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002699 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002700#ifdef CONFIG_NET_POLL_CONTROLLER
2701 .ndo_poll_controller = macb_poll_controller,
2702#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002703 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002704 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002705};
2706
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002707/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002708 * and integration options used
2709 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002710static void macb_configure_caps(struct macb *bp,
2711 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002712{
2713 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002714
Nicolas Ferref6970502015-03-31 15:02:01 +02002715 if (dt_conf)
2716 bp->caps = dt_conf->caps;
2717
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002718 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002719 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2720
Nicolas Ferree1755872014-07-24 13:50:58 +02002721 dcfg = gem_readl(bp, DCFG1);
2722 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2723 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2724 dcfg = gem_readl(bp, DCFG2);
2725 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2726 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002727#ifdef CONFIG_MACB_USE_HWSTAMP
2728 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002729 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
2730 pr_err("GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002731 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002732 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002733 bp->ptp_info = &gem_ptp_info;
2734 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002735 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002736#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002737 }
2738
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002739 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002740}
2741
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002742static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002743 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002744 unsigned int *queue_mask,
2745 unsigned int *num_queues)
2746{
2747 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002748
2749 *queue_mask = 0x1;
2750 *num_queues = 1;
2751
Nicolas Ferreda120112015-03-31 15:02:00 +02002752 /* is it macb or gem ?
2753 *
2754 * We need to read directly from the hardware here because
2755 * we are early in the probe process and don't have the
2756 * MACB_CAPS_MACB_IS_GEM flag positioned
2757 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002758 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002759 return;
2760
2761 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302762 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2763
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002764 *queue_mask |= 0x1;
2765
2766 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2767 if (*queue_mask & (1 << hw_q))
2768 (*num_queues)++;
2769}
2770
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002771static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302772 struct clk **hclk, struct clk **tx_clk,
2773 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002774{
Bartosz Folta83a77e92016-12-14 06:39:15 +00002775 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002776 int err;
2777
Bartosz Folta83a77e92016-12-14 06:39:15 +00002778 pdata = dev_get_platdata(&pdev->dev);
2779 if (pdata) {
2780 *pclk = pdata->pclk;
2781 *hclk = pdata->hclk;
2782 } else {
2783 *pclk = devm_clk_get(&pdev->dev, "pclk");
2784 *hclk = devm_clk_get(&pdev->dev, "hclk");
2785 }
2786
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002787 if (IS_ERR(*pclk)) {
2788 err = PTR_ERR(*pclk);
2789 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2790 return err;
2791 }
2792
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002793 if (IS_ERR(*hclk)) {
2794 err = PTR_ERR(*hclk);
2795 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2796 return err;
2797 }
2798
2799 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2800 if (IS_ERR(*tx_clk))
2801 *tx_clk = NULL;
2802
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302803 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2804 if (IS_ERR(*rx_clk))
2805 *rx_clk = NULL;
2806
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002807 err = clk_prepare_enable(*pclk);
2808 if (err) {
2809 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2810 return err;
2811 }
2812
2813 err = clk_prepare_enable(*hclk);
2814 if (err) {
2815 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2816 goto err_disable_pclk;
2817 }
2818
2819 err = clk_prepare_enable(*tx_clk);
2820 if (err) {
2821 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2822 goto err_disable_hclk;
2823 }
2824
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302825 err = clk_prepare_enable(*rx_clk);
2826 if (err) {
2827 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2828 goto err_disable_txclk;
2829 }
2830
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002831 return 0;
2832
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302833err_disable_txclk:
2834 clk_disable_unprepare(*tx_clk);
2835
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002836err_disable_hclk:
2837 clk_disable_unprepare(*hclk);
2838
2839err_disable_pclk:
2840 clk_disable_unprepare(*pclk);
2841
2842 return err;
2843}
2844
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002845static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002846{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002847 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002848 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002849 struct macb *bp = netdev_priv(dev);
2850 struct macb_queue *queue;
2851 int err;
2852 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002853
Zach Brownb410d132016-10-19 09:56:57 -05002854 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2855 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2856
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002857 /* set the queue register mapping once for all: queue0 has a special
2858 * register mapping but we don't want to test the queue index then
2859 * compute the corresponding register offset at run time.
2860 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002861 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002862 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002863 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002864
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002865 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002866 queue->bp = bp;
2867 if (hw_q) {
2868 queue->ISR = GEM_ISR(hw_q - 1);
2869 queue->IER = GEM_IER(hw_q - 1);
2870 queue->IDR = GEM_IDR(hw_q - 1);
2871 queue->IMR = GEM_IMR(hw_q - 1);
2872 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302873#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002874 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002875 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302876#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002877 } else {
2878 /* queue0 uses legacy registers */
2879 queue->ISR = MACB_ISR;
2880 queue->IER = MACB_IER;
2881 queue->IDR = MACB_IDR;
2882 queue->IMR = MACB_IMR;
2883 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302884#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002885 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002886 queue->TBQPH = MACB_TBQPH;
Harini Katakamfff80192016-08-09 13:15:53 +05302887#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002888 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002889
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002890 /* get irq: here we use the linux queue index, not the hardware
2891 * queue index. the queue irq definitions in the device tree
2892 * must remove the optional gaps that could exist in the
2893 * hardware queue mask.
2894 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002895 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002896 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002897 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002898 if (err) {
2899 dev_err(&pdev->dev,
2900 "Unable to request IRQ %d (error %d)\n",
2901 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002902 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002903 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002904
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002905 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002906 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002907 }
2908
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002909 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002910 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002911
Nicolas Ferre4df95132013-06-04 21:57:12 +00002912 /* setup appropriated routines according to adapter type */
2913 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002914 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002915 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2916 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2917 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2918 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002919 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002920 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002921 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002922 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2923 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2924 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2925 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002926 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002927 }
2928
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002929 /* Set features */
2930 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002931
2932 /* Check LSO capability */
2933 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
2934 dev->hw_features |= MACB_NETIF_LSO;
2935
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002936 /* Checksum offload is only available on gem with packet buffer */
2937 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002938 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002939 if (bp->caps & MACB_CAPS_SG_DISABLED)
2940 dev->hw_features &= ~NETIF_F_SG;
2941 dev->features = dev->hw_features;
2942
Neil Armstrongce721a72016-01-05 14:39:16 +01002943 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2944 val = 0;
2945 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2946 val = GEM_BIT(RGMII);
2947 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002948 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002949 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002950 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002951 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002952
Neil Armstrongce721a72016-01-05 14:39:16 +01002953 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2954 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002955
Neil Armstrongce721a72016-01-05 14:39:16 +01002956 macb_or_gem_writel(bp, USRIO, val);
2957 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002958
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002959 /* Set MII management clock divider */
2960 val = macb_mdc_clk_div(bp);
2961 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302962 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2963 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002964 macb_writel(bp, NCFGR, val);
2965
2966 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002967}
2968
2969#if defined(CONFIG_OF)
2970/* 1518 rounded up */
2971#define AT91ETHER_MAX_RBUFF_SZ 0x600
2972/* max number of receive buffers */
2973#define AT91ETHER_MAX_RX_DESCR 9
2974
2975/* Initialize and start the Receiver and Transmit subsystems */
2976static int at91ether_start(struct net_device *dev)
2977{
2978 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002979 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002980 dma_addr_t addr;
2981 u32 ctl;
2982 int i;
2983
2984 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2985 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002986 macb_dma_desc_get_size(lp)),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002987 &lp->rx_ring_dma, GFP_KERNEL);
2988 if (!lp->rx_ring)
2989 return -ENOMEM;
2990
2991 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2992 AT91ETHER_MAX_RX_DESCR *
2993 AT91ETHER_MAX_RBUFF_SZ,
2994 &lp->rx_buffers_dma, GFP_KERNEL);
2995 if (!lp->rx_buffers) {
2996 dma_free_coherent(&lp->pdev->dev,
2997 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002998 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002999 lp->rx_ring, lp->rx_ring_dma);
3000 lp->rx_ring = NULL;
3001 return -ENOMEM;
3002 }
3003
3004 addr = lp->rx_buffers_dma;
3005 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003006 desc = macb_rx_desc(lp, i);
3007 macb_set_addr(lp, desc, addr);
3008 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003009 addr += AT91ETHER_MAX_RBUFF_SZ;
3010 }
3011
3012 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003013 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003014
3015 /* Reset buffer index */
3016 lp->rx_tail = 0;
3017
3018 /* Program address of descriptor list in Rx Buffer Queue register */
3019 macb_writel(lp, RBQP, lp->rx_ring_dma);
3020
3021 /* Enable Receive and Transmit */
3022 ctl = macb_readl(lp, NCR);
3023 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3024
3025 return 0;
3026}
3027
3028/* Open the ethernet interface */
3029static int at91ether_open(struct net_device *dev)
3030{
3031 struct macb *lp = netdev_priv(dev);
3032 u32 ctl;
3033 int ret;
3034
3035 /* Clear internal statistics */
3036 ctl = macb_readl(lp, NCR);
3037 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3038
3039 macb_set_hwaddr(lp);
3040
3041 ret = at91ether_start(dev);
3042 if (ret)
3043 return ret;
3044
3045 /* Enable MAC interrupts */
3046 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3047 MACB_BIT(RXUBR) |
3048 MACB_BIT(ISR_TUND) |
3049 MACB_BIT(ISR_RLE) |
3050 MACB_BIT(TCOMP) |
3051 MACB_BIT(ISR_ROVR) |
3052 MACB_BIT(HRESP));
3053
3054 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02003055 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003056
3057 netif_start_queue(dev);
3058
3059 return 0;
3060}
3061
3062/* Close the interface */
3063static int at91ether_close(struct net_device *dev)
3064{
3065 struct macb *lp = netdev_priv(dev);
3066 u32 ctl;
3067
3068 /* Disable Receiver and Transmitter */
3069 ctl = macb_readl(lp, NCR);
3070 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3071
3072 /* Disable MAC interrupts */
3073 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3074 MACB_BIT(RXUBR) |
3075 MACB_BIT(ISR_TUND) |
3076 MACB_BIT(ISR_RLE) |
3077 MACB_BIT(TCOMP) |
3078 MACB_BIT(ISR_ROVR) |
3079 MACB_BIT(HRESP));
3080
3081 netif_stop_queue(dev);
3082
3083 dma_free_coherent(&lp->pdev->dev,
3084 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003085 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003086 lp->rx_ring, lp->rx_ring_dma);
3087 lp->rx_ring = NULL;
3088
3089 dma_free_coherent(&lp->pdev->dev,
3090 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
3091 lp->rx_buffers, lp->rx_buffers_dma);
3092 lp->rx_buffers = NULL;
3093
3094 return 0;
3095}
3096
3097/* Transmit packet */
3098static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
3099{
3100 struct macb *lp = netdev_priv(dev);
3101
3102 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3103 netif_stop_queue(dev);
3104
3105 /* Store packet information (to free when Tx completed) */
3106 lp->skb = skb;
3107 lp->skb_length = skb->len;
3108 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3109 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003110 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3111 dev_kfree_skb_any(skb);
3112 dev->stats.tx_dropped++;
3113 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3114 return NETDEV_TX_OK;
3115 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003116
3117 /* Set address of the data in the Transmit Address register */
3118 macb_writel(lp, TAR, lp->skb_physaddr);
3119 /* Set length of the packet in the Transmit Control register */
3120 macb_writel(lp, TCR, skb->len);
3121
3122 } else {
3123 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3124 return NETDEV_TX_BUSY;
3125 }
3126
3127 return NETDEV_TX_OK;
3128}
3129
3130/* Extract received frame from buffer descriptors and sent to upper layers.
3131 * (Called from interrupt context)
3132 */
3133static void at91ether_rx(struct net_device *dev)
3134{
3135 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003136 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003137 unsigned char *p_recv;
3138 struct sk_buff *skb;
3139 unsigned int pktlen;
3140
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003141 desc = macb_rx_desc(lp, lp->rx_tail);
3142 while (desc->addr & MACB_BIT(RX_USED)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003143 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003144 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003145 skb = netdev_alloc_skb(dev, pktlen + 2);
3146 if (skb) {
3147 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003148 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003149
3150 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003151 dev->stats.rx_packets++;
3152 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003153 netif_rx(skb);
3154 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003155 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003156 }
3157
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003158 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003159 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003160
3161 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003162 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003163
3164 /* wrap after last buffer */
3165 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3166 lp->rx_tail = 0;
3167 else
3168 lp->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003169
3170 desc = macb_rx_desc(lp, lp->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003171 }
3172}
3173
3174/* MAC interrupt handler */
3175static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3176{
3177 struct net_device *dev = dev_id;
3178 struct macb *lp = netdev_priv(dev);
3179 u32 intstatus, ctl;
3180
3181 /* MAC Interrupt Status register indicates what interrupts are pending.
3182 * It is automatically cleared once read.
3183 */
3184 intstatus = macb_readl(lp, ISR);
3185
3186 /* Receive complete */
3187 if (intstatus & MACB_BIT(RCOMP))
3188 at91ether_rx(dev);
3189
3190 /* Transmit complete */
3191 if (intstatus & MACB_BIT(TCOMP)) {
3192 /* The TCOM bit is set even if the transmission failed */
3193 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003194 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003195
3196 if (lp->skb) {
3197 dev_kfree_skb_irq(lp->skb);
3198 lp->skb = NULL;
3199 dma_unmap_single(NULL, lp->skb_physaddr,
3200 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003201 dev->stats.tx_packets++;
3202 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003203 }
3204 netif_wake_queue(dev);
3205 }
3206
3207 /* Work-around for EMAC Errata section 41.3.1 */
3208 if (intstatus & MACB_BIT(RXUBR)) {
3209 ctl = macb_readl(lp, NCR);
3210 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003211 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003212 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3213 }
3214
3215 if (intstatus & MACB_BIT(ISR_ROVR))
3216 netdev_err(dev, "ROVR error\n");
3217
3218 return IRQ_HANDLED;
3219}
3220
3221#ifdef CONFIG_NET_POLL_CONTROLLER
3222static void at91ether_poll_controller(struct net_device *dev)
3223{
3224 unsigned long flags;
3225
3226 local_irq_save(flags);
3227 at91ether_interrupt(dev->irq, dev);
3228 local_irq_restore(flags);
3229}
3230#endif
3231
3232static const struct net_device_ops at91ether_netdev_ops = {
3233 .ndo_open = at91ether_open,
3234 .ndo_stop = at91ether_close,
3235 .ndo_start_xmit = at91ether_start_xmit,
3236 .ndo_get_stats = macb_get_stats,
3237 .ndo_set_rx_mode = macb_set_rx_mode,
3238 .ndo_set_mac_address = eth_mac_addr,
3239 .ndo_do_ioctl = macb_ioctl,
3240 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003241#ifdef CONFIG_NET_POLL_CONTROLLER
3242 .ndo_poll_controller = at91ether_poll_controller,
3243#endif
3244};
3245
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003246static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303247 struct clk **hclk, struct clk **tx_clk,
3248 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003249{
3250 int err;
3251
3252 *hclk = NULL;
3253 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303254 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003255
3256 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3257 if (IS_ERR(*pclk))
3258 return PTR_ERR(*pclk);
3259
3260 err = clk_prepare_enable(*pclk);
3261 if (err) {
3262 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3263 return err;
3264 }
3265
3266 return 0;
3267}
3268
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003269static int at91ether_init(struct platform_device *pdev)
3270{
3271 struct net_device *dev = platform_get_drvdata(pdev);
3272 struct macb *bp = netdev_priv(dev);
3273 int err;
3274 u32 reg;
3275
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003276 dev->netdev_ops = &at91ether_netdev_ops;
3277 dev->ethtool_ops = &macb_ethtool_ops;
3278
3279 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3280 0, dev->name, dev);
3281 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003282 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003283
3284 macb_writel(bp, NCR, 0);
3285
3286 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3287 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3288 reg |= MACB_BIT(RM9200_RMII);
3289
3290 macb_writel(bp, NCFGR, reg);
3291
3292 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003293}
3294
David S. Miller3cef5c52015-03-09 23:38:02 -04003295static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003296 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003297 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003298 .init = macb_init,
3299};
3300
David S. Miller3cef5c52015-03-09 23:38:02 -04003301static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003302 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3303 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003304 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003305 .init = macb_init,
3306};
3307
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003308static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003309 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003310 .dma_burst_length = 16,
3311 .clk_init = macb_clk_init,
3312 .init = macb_init,
3313};
3314
David S. Miller3cef5c52015-03-09 23:38:02 -04003315static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003316 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02003317 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003318 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003319 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003320 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02003321 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003322};
3323
David S. Miller3cef5c52015-03-09 23:38:02 -04003324static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003325 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003326 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003327 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003328 .init = macb_init,
3329};
3330
David S. Miller3cef5c52015-03-09 23:38:02 -04003331static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003332 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003333 .init = at91ether_init,
3334};
3335
Neil Armstronge611b5b2016-01-05 14:39:17 +01003336static const struct macb_config np4_config = {
3337 .caps = MACB_CAPS_USRIO_DISABLED,
3338 .clk_init = macb_clk_init,
3339 .init = macb_init,
3340};
David S. Miller36583eb2015-05-23 01:22:35 -04003341
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303342static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003343 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3344 MACB_CAPS_JUMBO |
3345 MACB_CAPS_GEM_HAS_PTP,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303346 .dma_burst_length = 16,
3347 .clk_init = macb_clk_init,
3348 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303349 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303350};
3351
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003352static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303353 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003354 .dma_burst_length = 16,
3355 .clk_init = macb_clk_init,
3356 .init = macb_init,
3357};
3358
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003359static const struct of_device_id macb_dt_ids[] = {
3360 { .compatible = "cdns,at32ap7000-macb" },
3361 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3362 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003363 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003364 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3365 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003366 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003367 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3368 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3369 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3370 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303371 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003372 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003373 { /* sentinel */ }
3374};
3375MODULE_DEVICE_TABLE(of, macb_dt_ids);
3376#endif /* CONFIG_OF */
3377
Bartosz Folta83a77e92016-12-14 06:39:15 +00003378static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003379 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3380 MACB_CAPS_JUMBO |
3381 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00003382 .dma_burst_length = 16,
3383 .clk_init = macb_clk_init,
3384 .init = macb_init,
3385 .jumbo_max_len = 10240,
3386};
3387
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003388static int macb_probe(struct platform_device *pdev)
3389{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003390 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003391 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303392 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003393 = macb_config->clk_init;
3394 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003395 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003396 struct device_node *phy_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303397 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003398 unsigned int queue_mask, num_queues;
3399 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003400 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003401 struct phy_device *phydev;
3402 struct net_device *dev;
3403 struct resource *regs;
3404 void __iomem *mem;
3405 const char *mac;
3406 struct macb *bp;
3407 int err;
3408
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003409 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3410 mem = devm_ioremap_resource(&pdev->dev, regs);
3411 if (IS_ERR(mem))
3412 return PTR_ERR(mem);
3413
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003414 if (np) {
3415 const struct of_device_id *match;
3416
3417 match = of_match_node(macb_dt_ids, np);
3418 if (match && match->data) {
3419 macb_config = match->data;
3420 clk_init = macb_config->clk_init;
3421 init = macb_config->init;
3422 }
3423 }
3424
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303425 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003426 if (err)
3427 return err;
3428
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003429 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003430
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003431 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003432 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003433 if (!dev) {
3434 err = -ENOMEM;
3435 goto err_disable_clocks;
3436 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003437
3438 dev->base_addr = regs->start;
3439
3440 SET_NETDEV_DEV(dev, &pdev->dev);
3441
3442 bp = netdev_priv(dev);
3443 bp->pdev = pdev;
3444 bp->dev = dev;
3445 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003446 bp->native_io = native_io;
3447 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003448 bp->macb_reg_readl = hw_readl_native;
3449 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003450 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003451 bp->macb_reg_readl = hw_readl;
3452 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003453 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003454 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003455 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003456 if (macb_config)
3457 bp->dma_burst_length = macb_config->dma_burst_length;
3458 bp->pclk = pclk;
3459 bp->hclk = hclk;
3460 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303461 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003462 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303463 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303464
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003465 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003466 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003467 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3468 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3469
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003470 spin_lock_init(&bp->lock);
3471
Nicolas Ferread783472015-03-31 15:02:02 +02003472 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003473 macb_configure_caps(bp, macb_config);
3474
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003475#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3476 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
3477 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3478 bp->hw_dma_cap |= HW_DMA_CAP_64B;
3479 }
3480#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003481 platform_set_drvdata(pdev, dev);
3482
3483 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003484 if (dev->irq < 0) {
3485 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003486 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003487 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003488
Jarod Wilson44770e12016-10-17 15:54:17 -04003489 /* MTU range: 68 - 1500 or 10240 */
3490 dev->min_mtu = GEM_MTU_MIN_SIZE;
3491 if (bp->caps & MACB_CAPS_JUMBO)
3492 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3493 else
3494 dev->max_mtu = ETH_DATA_LEN;
3495
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003496 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003497 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003498 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003499 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003500 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003501
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003502 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003503 phy_node = of_get_next_available_child(np, NULL);
3504 if (phy_node) {
3505 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003506
Charles Keepax0e3e7992016-03-28 13:47:42 +01003507 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003508 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003509 gpiod_direction_output(bp->reset_gpio, 1);
3510 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003511 }
3512 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003513
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003514 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003515 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003516 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003517 if (pdata && pdata->is_rmii)
3518 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3519 else
3520 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3521 } else {
3522 bp->phy_interface = err;
3523 }
3524
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003525 /* IP specific init */
3526 err = init(pdev);
3527 if (err)
3528 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003529
Florian Fainellicf669662016-05-02 18:38:45 -07003530 err = macb_mii_init(bp);
3531 if (err)
3532 goto err_out_free_netdev;
3533
Philippe Reynes0a912812016-06-22 00:32:35 +02003534 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003535
3536 netif_carrier_off(dev);
3537
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003538 err = register_netdev(dev);
3539 if (err) {
3540 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003541 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003542 }
3543
Florian Fainellicf669662016-05-02 18:38:45 -07003544 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003545
Bo Shen58798232014-09-13 01:57:49 +02003546 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3547 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3548 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003549
3550 return 0;
3551
Florian Fainellicf669662016-05-02 18:38:45 -07003552err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003553 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003554 mdiobus_unregister(bp->mii_bus);
3555 mdiobus_free(bp->mii_bus);
3556
3557 /* Shutdown the PHY if there is a GPIO reset */
3558 if (bp->reset_gpio)
3559 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003560
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003561err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003562 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003563
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003564err_disable_clocks:
3565 clk_disable_unprepare(tx_clk);
3566 clk_disable_unprepare(hclk);
3567 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303568 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003569
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003570 return err;
3571}
3572
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003573static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003574{
3575 struct net_device *dev;
3576 struct macb *bp;
3577
3578 dev = platform_get_drvdata(pdev);
3579
3580 if (dev) {
3581 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003582 if (dev->phydev)
3583 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003584 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003585 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003586 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003587
3588 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003589 if (bp->reset_gpio)
3590 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003591
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003592 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003593 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003594 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003595 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303596 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02003597 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003598 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003599 }
3600
3601 return 0;
3602}
3603
Michal Simekd23823d2015-01-23 09:36:03 +01003604static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003605{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003606 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003607 struct net_device *netdev = platform_get_drvdata(pdev);
3608 struct macb *bp = netdev_priv(netdev);
3609
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003610 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003611 netif_device_detach(netdev);
3612
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003613 if (bp->wol & MACB_WOL_ENABLED) {
3614 macb_writel(bp, IER, MACB_BIT(WOL));
3615 macb_writel(bp, WOL, MACB_BIT(MAG));
3616 enable_irq_wake(bp->queues[0].irq);
3617 } else {
3618 clk_disable_unprepare(bp->tx_clk);
3619 clk_disable_unprepare(bp->hclk);
3620 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303621 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003622 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003623
3624 return 0;
3625}
3626
Michal Simekd23823d2015-01-23 09:36:03 +01003627static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003628{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003629 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003630 struct net_device *netdev = platform_get_drvdata(pdev);
3631 struct macb *bp = netdev_priv(netdev);
3632
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003633 if (bp->wol & MACB_WOL_ENABLED) {
3634 macb_writel(bp, IDR, MACB_BIT(WOL));
3635 macb_writel(bp, WOL, 0);
3636 disable_irq_wake(bp->queues[0].irq);
3637 } else {
3638 clk_prepare_enable(bp->pclk);
3639 clk_prepare_enable(bp->hclk);
3640 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303641 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003642 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003643
3644 netif_device_attach(netdev);
3645
3646 return 0;
3647}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003648
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003649static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3650
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003651static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003652 .probe = macb_probe,
3653 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003654 .driver = {
3655 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003656 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003657 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003658 },
3659};
3660
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003661module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003662
3663MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003664MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003665MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003666MODULE_ALIAS("platform:macb");