blob: 67933079aeea5c7ffdfafb77883d1c94d959a11a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00003 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004 *
5 * Copyright (C) 2004-2006 Atmel Corporation
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01006 */
7
Jamie Ilesc220f8c2011-03-08 20:27:08 +00008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01009#include <linux/clk.h>
Yash Shahc218ad52019-06-18 13:26:08 +053010#include <linux/clk-provider.h>
Claudiu Beznea653e92a2018-08-07 12:25:14 +030011#include <linux/crc32.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000016#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010017#include <linux/slab.h>
18#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080019#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000020#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010021#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000026#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/platform_device.h>
Antoine Tenart7897b072019-11-13 10:00:06 +010028#include <linux/phylink.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080029#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010030#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010031#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020032#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010033#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000034#include <linux/ip.h>
35#include <linux/udp.h>
36#include <linux/tcp.h>
Harini Katakam8beb79b2019-03-01 16:20:32 +053037#include <linux/iopoll.h>
Harini Katakamd54f89a2019-03-01 16:20:34 +053038#include <linux/pm_runtime.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010039#include "macb.h"
40
Yash Shahc218ad52019-06-18 13:26:08 +053041/* This structure is only used for MACB on SiFive FU540 devices */
42struct sifive_fu540_macb_mgmt {
43 void __iomem *reg;
44 unsigned long rate;
45 struct clk_hw hw;
46};
47
Nicolas Ferre1b447912013-06-04 21:57:11 +000048#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000049#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050050
Zach Brownb410d132016-10-19 09:56:57 -050051#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050052#define MIN_RX_RING_SIZE 64
53#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000054#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050055 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010056
Zach Brownb410d132016-10-19 09:56:57 -050057#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050058#define MIN_TX_RING_SIZE 64
59#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000060#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050061 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010062
Nicolas Ferre909a8582012-11-19 06:00:21 +000063/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050064#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010065
Harini Katakame5010702019-01-29 15:20:03 +053066#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000067#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
68 | MACB_BIT(ISR_RLE) \
69 | MACB_BIT(TXERR))
Claudiu Beznea42983882018-12-17 10:02:42 +000070#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
71 | MACB_BIT(TXUBR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000072
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000073/* Max length of transmit frame must be a multiple of 8 bytes */
74#define MACB_TX_LEN_ALIGN 8
75#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
Harini Katakamf822e9c2020-02-05 18:08:12 +053076/* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
77 * false amba_error in TX path from the DMA assuming there is not enough
78 * space in the SRAM (16KB) even when there is.
79 */
80#define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020081
Jarod Wilson44770e12016-10-17 15:54:17 -040082#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
David S. Millerf9c45ae2017-07-03 06:31:05 -070083#define MACB_NETIF_LSO NETIF_F_TSO
Harini Katakama5898ea2015-05-06 22:27:18 +053084
Sergio Prado3e2a5e12016-02-09 12:07:16 -020085#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
86#define MACB_WOL_ENABLED (0x1 << 1)
87
Moritz Fischer64ec42f2016-03-29 19:11:12 -070088/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000089 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
90 */
91#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010092
Harini Katakamd54f89a2019-03-01 16:20:34 +053093#define MACB_PM_TIMEOUT 100 /* ms */
94
Harini Katakam8beb79b2019-03-01 16:20:32 +053095#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
96
Rafal Ozieblodc97a892017-01-27 15:08:20 +000097/* DMA buffer descriptor might be different size
Rafal Ozieblo7b429612017-06-29 07:12:51 +010098 * depends on hardware configuration:
99 *
100 * 1. dma address width 32 bits:
101 * word 1: 32 bit address of Data Buffer
102 * word 2: control
103 *
104 * 2. dma address width 64 bits:
105 * word 1: 32 bit address of Data Buffer
106 * word 2: control
107 * word 3: upper 32 bit address of Data Buffer
108 * word 4: unused
109 *
110 * 3. dma address width 32 bits with hardware timestamping:
111 * word 1: 32 bit address of Data Buffer
112 * word 2: control
113 * word 3: timestamp word 1
114 * word 4: timestamp word 2
115 *
116 * 4. dma address width 64 bits with hardware timestamping:
117 * word 1: 32 bit address of Data Buffer
118 * word 2: control
119 * word 3: upper 32 bit address of Data Buffer
120 * word 4: unused
121 * word 5: timestamp word 1
122 * word 6: timestamp word 2
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000123 */
124static unsigned int macb_dma_desc_get_size(struct macb *bp)
125{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100126#ifdef MACB_EXT_DESC
127 unsigned int desc_size;
128
129 switch (bp->hw_dma_cap) {
130 case HW_DMA_CAP_64B:
131 desc_size = sizeof(struct macb_dma_desc)
132 + sizeof(struct macb_dma_desc_64);
133 break;
134 case HW_DMA_CAP_PTP:
135 desc_size = sizeof(struct macb_dma_desc)
136 + sizeof(struct macb_dma_desc_ptp);
137 break;
138 case HW_DMA_CAP_64B_PTP:
139 desc_size = sizeof(struct macb_dma_desc)
140 + sizeof(struct macb_dma_desc_64)
141 + sizeof(struct macb_dma_desc_ptp);
142 break;
143 default:
144 desc_size = sizeof(struct macb_dma_desc);
145 }
146 return desc_size;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000147#endif
148 return sizeof(struct macb_dma_desc);
149}
150
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100151static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000152{
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100153#ifdef MACB_EXT_DESC
154 switch (bp->hw_dma_cap) {
155 case HW_DMA_CAP_64B:
156 case HW_DMA_CAP_PTP:
157 desc_idx <<= 1;
158 break;
159 case HW_DMA_CAP_64B_PTP:
160 desc_idx *= 3;
161 break;
162 default:
163 break;
164 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000165#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100166 return desc_idx;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000167}
168
169#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
170static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
171{
Shubhrajyoti Datta99dcb842019-09-23 14:03:51 +0530172 return (struct macb_dma_desc_64 *)((void *)desc
173 + sizeof(struct macb_dma_desc));
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000174}
175#endif
176
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000177/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500178static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000179{
Zach Brownb410d132016-10-19 09:56:57 -0500180 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000181}
182
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100183static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
184 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000185{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000186 index = macb_tx_ring_wrap(queue->bp, index);
187 index = macb_adj_dma_desc_idx(queue->bp, index);
188 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000189}
190
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100191static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
192 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000193{
Zach Brownb410d132016-10-19 09:56:57 -0500194 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000195}
196
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100197static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000198{
199 dma_addr_t offset;
200
Zach Brownb410d132016-10-19 09:56:57 -0500201 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000202 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000203
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100204 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000205}
206
Zach Brownb410d132016-10-19 09:56:57 -0500207static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000208{
Zach Brownb410d132016-10-19 09:56:57 -0500209 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000210}
211
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000212static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000213{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000214 index = macb_rx_ring_wrap(queue->bp, index);
215 index = macb_adj_dma_desc_idx(queue->bp, index);
216 return &queue->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000217}
218
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000219static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000220{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +0000221 return queue->rx_buffers + queue->bp->rx_buffer_size *
222 macb_rx_ring_wrap(queue->bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000223}
224
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300225/* I/O accessors */
226static u32 hw_readl_native(struct macb *bp, int offset)
227{
228 return __raw_readl(bp->regs + offset);
229}
230
231static void hw_writel_native(struct macb *bp, int offset, u32 value)
232{
233 __raw_writel(value, bp->regs + offset);
234}
235
236static u32 hw_readl(struct macb *bp, int offset)
237{
238 return readl_relaxed(bp->regs + offset);
239}
240
241static void hw_writel(struct macb *bp, int offset, u32 value)
242{
243 writel_relaxed(value, bp->regs + offset);
244}
245
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700246/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700247 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300248 * descriptor access.
249 */
250static bool hw_is_native_io(void __iomem *addr)
251{
252 u32 value = MACB_BIT(LLB);
253
254 __raw_writel(value, addr + MACB_NCR);
255 value = __raw_readl(addr + MACB_NCR);
256
257 /* Write 0 back to disable everything */
258 __raw_writel(0, addr + MACB_NCR);
259
260 return value == MACB_BIT(LLB);
261}
262
263static bool hw_is_gem(void __iomem *addr, bool native_io)
264{
265 u32 id;
266
267 if (native_io)
268 id = __raw_readl(addr + MACB_MID);
269 else
270 id = readl_relaxed(addr + MACB_MID);
271
272 return MACB_BFEXT(IDNUM, id) >= 0x2;
273}
274
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100275static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100276{
277 u32 bottom;
278 u16 top;
279
280 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000281 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000283 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000284
285 /* Clear unused address register sets */
286 macb_or_gem_writel(bp, SA2B, 0);
287 macb_or_gem_writel(bp, SA2T, 0);
288 macb_or_gem_writel(bp, SA3B, 0);
289 macb_or_gem_writel(bp, SA3T, 0);
290 macb_or_gem_writel(bp, SA4B, 0);
291 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100292}
293
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100294static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100295{
296 u32 bottom;
297 u16 top;
298 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000299 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100300
Moritz Fischeraa50b552016-03-29 19:11:13 -0700301 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000302 for (i = 0; i < 4; i++) {
303 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
304 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100305
Nicolas Ferre8b952742019-05-03 12:36:58 +0200306 addr[0] = bottom & 0xff;
307 addr[1] = (bottom >> 8) & 0xff;
308 addr[2] = (bottom >> 16) & 0xff;
309 addr[3] = (bottom >> 24) & 0xff;
310 addr[4] = top & 0xff;
311 addr[5] = (top >> 8) & 0xff;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100312
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000313 if (is_valid_ether_addr(addr)) {
314 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
315 return;
316 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700317 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000318
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300319 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000320 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100321}
322
Harini Katakam8beb79b2019-03-01 16:20:32 +0530323static int macb_mdio_wait_for_idle(struct macb *bp)
324{
325 u32 val;
326
327 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
328 1, MACB_MDIO_TIMEOUT);
329}
330
frederic RODO6c36a702007-07-12 19:07:24 +0200331static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100332{
frederic RODO6c36a702007-07-12 19:07:24 +0200333 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530334 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530335
Harini Katakamd54f89a2019-03-01 16:20:34 +0530336 status = pm_runtime_get_sync(&bp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300337 if (status < 0) {
338 pm_runtime_put_noidle(&bp->pdev->dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +0530339 goto mdio_pm_exit;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300340 }
Harini Katakamd54f89a2019-03-01 16:20:34 +0530341
342 status = macb_mdio_wait_for_idle(bp);
343 if (status < 0)
344 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100345
Milind Parab43ad3522020-01-09 08:36:46 +0000346 if (regnum & MII_ADDR_C45) {
347 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
348 | MACB_BF(RW, MACB_MAN_C45_ADDR)
349 | MACB_BF(PHYA, mii_id)
350 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
351 | MACB_BF(DATA, regnum & 0xFFFF)
352 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
353
354 status = macb_mdio_wait_for_idle(bp);
355 if (status < 0)
356 goto mdio_read_exit;
357
358 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
359 | MACB_BF(RW, MACB_MAN_C45_READ)
360 | MACB_BF(PHYA, mii_id)
361 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
362 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
363 } else {
364 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
365 | MACB_BF(RW, MACB_MAN_C22_READ)
366 | MACB_BF(PHYA, mii_id)
367 | MACB_BF(REGA, regnum)
368 | MACB_BF(CODE, MACB_MAN_C22_CODE)));
369 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100370
Harini Katakamd54f89a2019-03-01 16:20:34 +0530371 status = macb_mdio_wait_for_idle(bp);
372 if (status < 0)
373 goto mdio_read_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100374
Harini Katakamd54f89a2019-03-01 16:20:34 +0530375 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100376
Harini Katakamd54f89a2019-03-01 16:20:34 +0530377mdio_read_exit:
378 pm_runtime_mark_last_busy(&bp->pdev->dev);
379 pm_runtime_put_autosuspend(&bp->pdev->dev);
380mdio_pm_exit:
381 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100382}
383
frederic RODO6c36a702007-07-12 19:07:24 +0200384static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
385 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100386{
frederic RODO6c36a702007-07-12 19:07:24 +0200387 struct macb *bp = bus->priv;
Harini Katakamd54f89a2019-03-01 16:20:34 +0530388 int status;
Harini Katakam8beb79b2019-03-01 16:20:32 +0530389
Harini Katakamd54f89a2019-03-01 16:20:34 +0530390 status = pm_runtime_get_sync(&bp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300391 if (status < 0) {
392 pm_runtime_put_noidle(&bp->pdev->dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +0530393 goto mdio_pm_exit;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +0300394 }
Harini Katakamd54f89a2019-03-01 16:20:34 +0530395
396 status = macb_mdio_wait_for_idle(bp);
397 if (status < 0)
398 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100399
Milind Parab43ad3522020-01-09 08:36:46 +0000400 if (regnum & MII_ADDR_C45) {
401 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
402 | MACB_BF(RW, MACB_MAN_C45_ADDR)
403 | MACB_BF(PHYA, mii_id)
404 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
405 | MACB_BF(DATA, regnum & 0xFFFF)
406 | MACB_BF(CODE, MACB_MAN_C45_CODE)));
407
408 status = macb_mdio_wait_for_idle(bp);
409 if (status < 0)
410 goto mdio_write_exit;
411
412 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
413 | MACB_BF(RW, MACB_MAN_C45_WRITE)
414 | MACB_BF(PHYA, mii_id)
415 | MACB_BF(REGA, (regnum >> 16) & 0x1F)
416 | MACB_BF(CODE, MACB_MAN_C45_CODE)
417 | MACB_BF(DATA, value)));
418 } else {
419 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
420 | MACB_BF(RW, MACB_MAN_C22_WRITE)
421 | MACB_BF(PHYA, mii_id)
422 | MACB_BF(REGA, regnum)
423 | MACB_BF(CODE, MACB_MAN_C22_CODE)
424 | MACB_BF(DATA, value)));
425 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100426
Harini Katakamd54f89a2019-03-01 16:20:34 +0530427 status = macb_mdio_wait_for_idle(bp);
428 if (status < 0)
429 goto mdio_write_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100430
Harini Katakamd54f89a2019-03-01 16:20:34 +0530431mdio_write_exit:
432 pm_runtime_mark_last_busy(&bp->pdev->dev);
433 pm_runtime_put_autosuspend(&bp->pdev->dev);
434mdio_pm_exit:
435 return status;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100436}
437
Antoine Tenart6e952d92019-11-13 10:00:05 +0100438static void macb_init_buffers(struct macb *bp)
439{
440 struct macb_queue *queue;
441 unsigned int q;
442
443 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
444 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
445#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
446 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
447 queue_writel(queue, RBQPH,
448 upper_32_bits(queue->rx_ring_dma));
449#endif
450 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
451#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
452 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
453 queue_writel(queue, TBQPH,
454 upper_32_bits(queue->tx_ring_dma));
455#endif
456 }
457}
458
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800459/**
460 * macb_set_tx_clk() - Set a clock to a new frequency
461 * @clk Pointer to the clock to change
462 * @rate New frequency in Hz
463 * @dev Pointer to the struct net_device
464 */
465static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
466{
467 long ferr, rate, rate_rounded;
468
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100469 if (!clk)
470 return;
471
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800472 switch (speed) {
473 case SPEED_10:
474 rate = 2500000;
475 break;
476 case SPEED_100:
477 rate = 25000000;
478 break;
479 case SPEED_1000:
480 rate = 125000000;
481 break;
482 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800483 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800484 }
485
486 rate_rounded = clk_round_rate(clk, rate);
487 if (rate_rounded < 0)
488 return;
489
490 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
491 * is not satisfied.
492 */
493 ferr = abs(rate_rounded - rate);
494 ferr = DIV_ROUND_UP(ferr, rate / 100000);
495 if (ferr > 5)
496 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700497 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800498
499 if (clk_set_rate(clk, rate_rounded))
500 netdev_err(dev, "adjusting tx_clk failed.\n");
501}
502
Antoine Tenart7897b072019-11-13 10:00:06 +0100503static void macb_validate(struct phylink_config *config,
504 unsigned long *supported,
505 struct phylink_link_state *state)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100506{
Antoine Tenart7897b072019-11-13 10:00:06 +0100507 struct net_device *ndev = to_net_dev(config->dev);
508 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
509 struct macb *bp = netdev_priv(ndev);
510
511 /* We only support MII, RMII, GMII, RGMII & SGMII. */
512 if (state->interface != PHY_INTERFACE_MODE_NA &&
513 state->interface != PHY_INTERFACE_MODE_MII &&
514 state->interface != PHY_INTERFACE_MODE_RMII &&
515 state->interface != PHY_INTERFACE_MODE_GMII &&
516 state->interface != PHY_INTERFACE_MODE_SGMII &&
517 !phy_interface_mode_is_rgmii(state->interface)) {
518 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
519 return;
520 }
521
522 if (!macb_is_gem(bp) &&
523 (state->interface == PHY_INTERFACE_MODE_GMII ||
524 phy_interface_mode_is_rgmii(state->interface))) {
525 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
526 return;
527 }
528
529 phylink_set_port_modes(mask);
530 phylink_set(mask, Autoneg);
531 phylink_set(mask, Asym_Pause);
532
533 phylink_set(mask, 10baseT_Half);
534 phylink_set(mask, 10baseT_Full);
535 phylink_set(mask, 100baseT_Half);
536 phylink_set(mask, 100baseT_Full);
537
538 if (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE &&
539 (state->interface == PHY_INTERFACE_MODE_NA ||
540 state->interface == PHY_INTERFACE_MODE_GMII ||
541 state->interface == PHY_INTERFACE_MODE_SGMII ||
542 phy_interface_mode_is_rgmii(state->interface))) {
543 phylink_set(mask, 1000baseT_Full);
544 phylink_set(mask, 1000baseX_Full);
545
546 if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
547 phylink_set(mask, 1000baseT_Half);
548 }
549
550 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
551 bitmap_and(state->advertising, state->advertising, mask,
552 __ETHTOOL_LINK_MODE_MASK_NBITS);
553}
554
Russell Kingd46b7e42019-11-21 00:36:22 +0000555static void macb_mac_pcs_get_state(struct phylink_config *config,
556 struct phylink_link_state *state)
Antoine Tenart7897b072019-11-13 10:00:06 +0100557{
Russell Kingd46b7e42019-11-21 00:36:22 +0000558 state->link = 0;
Antoine Tenart7897b072019-11-13 10:00:06 +0100559}
560
561static void macb_mac_an_restart(struct phylink_config *config)
562{
563 /* Not supported */
564}
565
566static void macb_mac_config(struct phylink_config *config, unsigned int mode,
567 const struct phylink_link_state *state)
568{
569 struct net_device *ndev = to_net_dev(config->dev);
570 struct macb *bp = netdev_priv(ndev);
frederic RODO6c36a702007-07-12 19:07:24 +0200571 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100572 u32 old_ctrl, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100573
frederic RODO6c36a702007-07-12 19:07:24 +0200574 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100575
Antoine Tenart7897b072019-11-13 10:00:06 +0100576 old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100577
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100578 if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
579 if (state->interface == PHY_INTERFACE_MODE_RMII)
580 ctrl |= MACB_BIT(RM9200_RMII);
581 } else {
Russell King633e98a2020-02-26 10:24:06 +0000582 ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100583
584 if (state->interface == PHY_INTERFACE_MODE_SGMII)
585 ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
586 }
frederic RODO6c36a702007-07-12 19:07:24 +0200587
Antoine Tenart7897b072019-11-13 10:00:06 +0100588 /* Apply the new configuration, if any */
589 if (old_ctrl ^ ctrl)
590 macb_or_gem_writel(bp, NCFGR, ctrl);
591
frederic RODO6c36a702007-07-12 19:07:24 +0200592 spin_unlock_irqrestore(&bp->lock, flags);
frederic RODO6c36a702007-07-12 19:07:24 +0200593}
594
Antoine Tenart7897b072019-11-13 10:00:06 +0100595static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
596 phy_interface_t interface)
frederic RODO6c36a702007-07-12 19:07:24 +0200597{
Antoine Tenart7897b072019-11-13 10:00:06 +0100598 struct net_device *ndev = to_net_dev(config->dev);
599 struct macb *bp = netdev_priv(ndev);
600 struct macb_queue *queue;
601 unsigned int q;
602 u32 ctrl;
603
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100604 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
605 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
606 queue_writel(queue, IDR,
607 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
Antoine Tenart7897b072019-11-13 10:00:06 +0100608
609 /* Disable Rx and Tx */
610 ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
611 macb_writel(bp, NCR, ctrl);
612
613 netif_tx_stop_all_queues(ndev);
614}
615
Russell King91a208f2020-02-26 10:23:41 +0000616static void macb_mac_link_up(struct phylink_config *config,
617 struct phy_device *phy,
618 unsigned int mode, phy_interface_t interface,
619 int speed, int duplex,
620 bool tx_pause, bool rx_pause)
Antoine Tenart7897b072019-11-13 10:00:06 +0100621{
622 struct net_device *ndev = to_net_dev(config->dev);
623 struct macb *bp = netdev_priv(ndev);
624 struct macb_queue *queue;
Russell King633e98a2020-02-26 10:24:06 +0000625 unsigned long flags;
Antoine Tenart7897b072019-11-13 10:00:06 +0100626 unsigned int q;
Russell King633e98a2020-02-26 10:24:06 +0000627 u32 ctrl;
628
629 spin_lock_irqsave(&bp->lock, flags);
630
631 ctrl = macb_or_gem_readl(bp, NCFGR);
632
633 ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
634
635 if (speed == SPEED_100)
636 ctrl |= MACB_BIT(SPD);
637
638 if (duplex)
639 ctrl |= MACB_BIT(FD);
Antoine Tenart7897b072019-11-13 10:00:06 +0100640
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100641 if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
Russell King633e98a2020-02-26 10:24:06 +0000642 ctrl &= ~(GEM_BIT(GBE) | MACB_BIT(PAE));
643
644 if (speed == SPEED_1000)
645 ctrl |= GEM_BIT(GBE);
646
647 /* We do not support MLO_PAUSE_RX yet */
648 if (tx_pause)
649 ctrl |= MACB_BIT(PAE);
650
651 macb_set_tx_clk(bp->tx_clk, speed, ndev);
Antoine Tenart7897b072019-11-13 10:00:06 +0100652
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100653 /* Initialize rings & buffers as clearing MACB_BIT(TE) in link down
654 * cleared the pipeline and control registers.
655 */
656 bp->macbgem_ops.mog_init_rings(bp);
657 macb_init_buffers(bp);
Antoine Tenart7897b072019-11-13 10:00:06 +0100658
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +0100659 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
660 queue_writel(queue, IER,
661 bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
662 }
Antoine Tenart7897b072019-11-13 10:00:06 +0100663
Russell King633e98a2020-02-26 10:24:06 +0000664 macb_or_gem_writel(bp, NCFGR, ctrl);
665
666 spin_unlock_irqrestore(&bp->lock, flags);
667
Antoine Tenart7897b072019-11-13 10:00:06 +0100668 /* Enable Rx and Tx */
669 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
670
671 netif_tx_wake_all_queues(ndev);
672}
673
674static const struct phylink_mac_ops macb_phylink_ops = {
675 .validate = macb_validate,
Russell Kingd46b7e42019-11-21 00:36:22 +0000676 .mac_pcs_get_state = macb_mac_pcs_get_state,
Antoine Tenart7897b072019-11-13 10:00:06 +0100677 .mac_an_restart = macb_mac_an_restart,
678 .mac_config = macb_mac_config,
679 .mac_link_down = macb_mac_link_down,
680 .mac_link_up = macb_mac_link_up,
681};
682
Milind Parabfd2a8912020-01-13 03:30:43 +0000683static bool macb_phy_handle_exists(struct device_node *dn)
684{
685 dn = of_parse_phandle(dn, "phy-handle", 0);
686 of_node_put(dn);
687 return dn != NULL;
688}
689
Antoine Tenart7897b072019-11-13 10:00:06 +0100690static int macb_phylink_connect(struct macb *bp)
691{
Milind Parabfd2a8912020-01-13 03:30:43 +0000692 struct device_node *dn = bp->pdev->dev.of_node;
Antoine Tenart7897b072019-11-13 10:00:06 +0100693 struct net_device *dev = bp->dev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000694 struct phy_device *phydev;
Antoine Tenart7897b072019-11-13 10:00:06 +0100695 int ret;
Brad Mouring739de9a2018-03-13 16:32:13 -0500696
Milind Parabfd2a8912020-01-13 03:30:43 +0000697 if (dn)
698 ret = phylink_of_phy_connect(bp->phylink, dn, 0);
699
700 if (!dn || (ret && !macb_phy_handle_exists(dn))) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200701 phydev = phy_find_first(bp->mii_bus);
702 if (!phydev) {
703 netdev_err(dev, "no PHY found\n");
704 return -ENXIO;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000705 }
frederic RODO6c36a702007-07-12 19:07:24 +0200706
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200707 /* attach the mac to the phy */
Antoine Tenart7897b072019-11-13 10:00:06 +0100708 ret = phylink_connect_phy(bp->phylink, phydev);
Milind Parabfd2a8912020-01-13 03:30:43 +0000709 }
710
711 if (ret) {
712 netdev_err(dev, "Could not attach PHY (%d)\n", ret);
713 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200714 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100715
Antoine Tenart7897b072019-11-13 10:00:06 +0100716 phylink_start(bp->phylink);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100717
Antoine Tenart7897b072019-11-13 10:00:06 +0100718 return 0;
719}
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100720
Antoine Tenart7897b072019-11-13 10:00:06 +0100721/* based on au1000_eth. c*/
722static int macb_mii_probe(struct net_device *dev)
723{
724 struct macb *bp = netdev_priv(dev);
725
726 bp->phylink_config.dev = &dev->dev;
727 bp->phylink_config.type = PHYLINK_NETDEV;
728
729 bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
730 bp->phy_interface, &macb_phylink_ops);
731 if (IS_ERR(bp->phylink)) {
732 netdev_err(dev, "Could not create a phylink instance (%ld)\n",
733 PTR_ERR(bp->phylink));
734 return PTR_ERR(bp->phylink);
735 }
frederic RODO6c36a702007-07-12 19:07:24 +0200736
737 return 0;
738}
739
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100740static int macb_mdiobus_register(struct macb *bp)
741{
742 struct device_node *child, *np = bp->pdev->dev.of_node;
743
Codrin Ciubotariu79540d12020-03-31 12:39:35 +0300744 if (of_phy_is_fixed_link(np))
745 return mdiobus_register(bp->mii_bus);
746
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100747 /* Only create the PHY from the device tree if at least one PHY is
748 * described. Otherwise scan the entire MDIO bus. We do this to support
749 * old device tree that did not follow the best practices and did not
750 * describe their network PHYs.
751 */
752 for_each_available_child_of_node(np, child)
753 if (of_mdiobus_child_is_phy(child)) {
754 /* The loop increments the child refcount,
755 * decrement it before returning.
756 */
757 of_node_put(child);
758
759 return of_mdiobus_register(bp->mii_bus, np);
760 }
761
762 return mdiobus_register(bp->mii_bus);
763}
764
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100765static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200766{
Ahmad Fatoumab5f1102018-08-21 17:35:48 +0200767 int err = -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200768
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200769 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200770 macb_writel(bp, NCR, MACB_BIT(MPE));
771
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700772 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700773 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200774 err = -ENOMEM;
775 goto err_out;
776 }
777
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700778 bp->mii_bus->name = "MACB_mii_bus";
779 bp->mii_bus->read = &macb_mdio_read;
780 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000781 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700782 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700783 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700784 bp->mii_bus->parent = &bp->pdev->dev;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700785
Jamie Iles91523942011-02-28 04:05:25 +0000786 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200787
Antoine Tenartef8a2e22019-12-17 18:07:42 +0100788 err = macb_mdiobus_register(bp);
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200789 if (err)
Antoine Tenart7897b072019-11-13 10:00:06 +0100790 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200791
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200792 err = macb_mii_probe(bp->dev);
793 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200794 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200795
796 return 0;
797
798err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700799 mdiobus_unregister(bp->mii_bus);
Brad Mouring739de9a2018-03-13 16:32:13 -0500800err_out_free_mdiobus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700801 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200802err_out:
803 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100804}
805
806static void macb_update_stats(struct macb *bp)
807{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000808 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
809 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300810 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100811
812 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
813
Moritz Fischer96ec6312016-03-29 19:11:11 -0700814 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700815 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100816}
817
Nicolas Ferree86cd532012-10-31 06:04:57 +0000818static int macb_halt_tx(struct macb *bp)
819{
820 unsigned long halt_time, timeout;
821 u32 status;
822
823 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
824
825 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
826 do {
827 halt_time = jiffies;
828 status = macb_readl(bp, TSR);
829 if (!(status & MACB_BIT(TGO)))
830 return 0;
831
Jia-Ju Bai16fe10c2018-09-01 20:11:05 +0800832 udelay(250);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000833 } while (time_before(halt_time, timeout));
834
835 return -ETIMEDOUT;
836}
837
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200838static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
839{
840 if (tx_skb->mapping) {
841 if (tx_skb->mapped_as_page)
842 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
843 tx_skb->size, DMA_TO_DEVICE);
844 else
845 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
846 tx_skb->size, DMA_TO_DEVICE);
847 tx_skb->mapping = 0;
848 }
849
850 if (tx_skb->skb) {
851 dev_kfree_skb_any(tx_skb->skb);
852 tx_skb->skb = NULL;
853 }
854}
855
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000856static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530857{
Harini Katakamfff80192016-08-09 13:15:53 +0530858#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000859 struct macb_dma_desc_64 *desc_64;
860
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100861 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000862 desc_64 = macb_64b_desc(bp, desc);
863 desc_64->addrh = upper_32_bits(addr);
Anssi Hannulae100a892018-12-17 15:05:39 +0200864 /* The low bits of RX address contain the RX_USED bit, clearing
865 * of which allows packet RX. Make sure the high bits are also
866 * visible to HW at that point.
867 */
868 dma_wmb();
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000869 }
Harini Katakamfff80192016-08-09 13:15:53 +0530870#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000871 desc->addr = lower_32_bits(addr);
872}
873
874static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
875{
876 dma_addr_t addr = 0;
877#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
878 struct macb_dma_desc_64 *desc_64;
879
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100880 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000881 desc_64 = macb_64b_desc(bp, desc);
882 addr = ((u64)(desc_64->addrh) << 32);
883 }
884#endif
885 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
886 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530887}
888
Nicolas Ferree86cd532012-10-31 06:04:57 +0000889static void macb_tx_error_task(struct work_struct *work)
890{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100891 struct macb_queue *queue = container_of(work, struct macb_queue,
892 tx_error_task);
893 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000894 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100895 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000896 struct sk_buff *skb;
897 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100898 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000899
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100900 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
901 (unsigned int)(queue - bp->queues),
902 queue->tx_tail, queue->tx_head);
903
904 /* Prevent the queue IRQ handlers from running: each of them may call
905 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
906 * As explained below, we have to halt the transmission before updating
907 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
908 * network engine about the macb/gem being halted.
909 */
910 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000911
912 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100913 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000914
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700915 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000916 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100917 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000918 */
919 if (macb_halt_tx(bp))
920 /* Just complain for now, reinitializing TX path can be good */
921 netdev_err(bp->dev, "BUG: halt tx timed out\n");
922
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700923 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000924 * Free transmit buffers in upper layer.
925 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100926 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
927 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000928
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100929 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000930 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100931 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000932 skb = tx_skb->skb;
933
934 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200935 /* skb is set for the last buffer of the frame */
936 while (!skb) {
937 macb_tx_unmap(bp, tx_skb);
938 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100939 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200940 skb = tx_skb->skb;
941 }
942
943 /* ctrl still refers to the first buffer descriptor
944 * since it's the only one written back by the hardware
945 */
946 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
947 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500948 macb_tx_ring_wrap(bp, tail),
949 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200950 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000951 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200952 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +0000953 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200954 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000955 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700956 /* "Buffers exhausted mid-frame" errors may only happen
957 * if the driver is buggy, so complain loudly about
958 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000959 */
960 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
961 netdev_err(bp->dev,
962 "BUG: TX buffers exhausted mid-frame\n");
963
964 desc->ctrl = ctrl | MACB_BIT(TX_USED);
965 }
966
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200967 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000968 }
969
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100970 /* Set end of TX queue */
971 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000972 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100973 desc->ctrl = MACB_BIT(TX_USED);
974
Nicolas Ferree86cd532012-10-31 06:04:57 +0000975 /* Make descriptor updates visible to hardware */
976 wmb();
977
978 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000979 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530980#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +0100981 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000982 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530983#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000984 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100985 queue->tx_head = 0;
986 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000987
988 /* Housework before enabling TX IRQ */
989 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100990 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
991
992 /* Now we are ready to start transmission again */
993 netif_tx_start_all_queues(bp->dev);
994 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
995
996 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000997}
998
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100999static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001000{
1001 unsigned int tail;
1002 unsigned int head;
1003 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001004 struct macb *bp = queue->bp;
1005 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001006
1007 status = macb_readl(bp, TSR);
1008 macb_writel(bp, TSR, status);
1009
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001010 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001011 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +00001012
Nicolas Ferree86cd532012-10-31 06:04:57 +00001013 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001014 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001015
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001016 head = queue->tx_head;
1017 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001018 struct macb_tx_skb *tx_skb;
1019 struct sk_buff *skb;
1020 struct macb_dma_desc *desc;
1021 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001022
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001023 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001024
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001025 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001026 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001027
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001028 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001029
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001030 /* TX_USED bit is only set by hardware on the very first buffer
1031 * descriptor of the transmitted frame.
1032 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001033 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001034 break;
1035
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001036 /* Process all buffers of the current transmitted frame */
1037 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001038 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001039 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001040
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001041 /* First, update TX stats if needed */
1042 if (skb) {
Paul Thomasa6252042019-04-08 15:37:54 -04001043 if (unlikely(skb_shinfo(skb)->tx_flags &
1044 SKBTX_HW_TSTAMP) &&
1045 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001046 /* skb now belongs to timestamp buffer
1047 * and will be removed later
1048 */
1049 tx_skb->skb = NULL;
1050 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001051 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -05001052 macb_tx_ring_wrap(bp, tail),
1053 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001054 bp->dev->stats.tx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001055 queue->stats.tx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001056 bp->dev->stats.tx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001057 queue->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001058 }
1059
1060 /* Now we can safely release resources */
1061 macb_tx_unmap(bp, tx_skb);
1062
1063 /* skb is set only for the last buffer of the frame.
1064 * WARNING: at this point skb has been freed by
1065 * macb_tx_unmap().
1066 */
1067 if (skb)
1068 break;
1069 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001070 }
1071
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001072 queue->tx_tail = tail;
1073 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
1074 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -05001075 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001076 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001077}
1078
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001079static void gem_rx_refill(struct macb_queue *queue)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001080{
1081 unsigned int entry;
1082 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001083 dma_addr_t paddr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001084 struct macb *bp = queue->bp;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001085 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001086
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001087 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
1088 bp->rx_ring_size) > 0) {
1089 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001090
1091 /* Make hw descriptor updates visible to CPU */
1092 rmb();
1093
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001094 queue->rx_prepared_head++;
1095 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001096
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001097 if (!queue->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001098 /* allocate sk_buff for this free entry in ring */
1099 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -07001100 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001101 netdev_err(bp->dev,
1102 "Unable to allocate sk_buff\n");
1103 break;
1104 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001105
1106 /* now fill corresponding descriptor entry */
1107 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001108 bp->rx_buffer_size,
1109 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001110 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
1111 dev_kfree_skb(skb);
1112 break;
1113 }
1114
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001115 queue->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001116
Zach Brownb410d132016-10-19 09:56:57 -05001117 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001118 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001119 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001120 /* Setting addr clears RX_USED and allows reception,
1121 * make sure ctrl is cleared first to avoid a race.
1122 */
1123 dma_wmb();
1124 macb_set_addr(bp, desc, paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001125
1126 /* properly align Ethernet header */
1127 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +05301128 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001129 desc->ctrl = 0;
Anssi Hannula8159eca2018-12-17 15:05:40 +02001130 dma_wmb();
1131 desc->addr &= ~MACB_BIT(RX_USED);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001132 }
1133 }
1134
1135 /* Make descriptor updates visible to hardware */
1136 wmb();
1137
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001138 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
1139 queue, queue->rx_prepared_head, queue->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001140}
1141
1142/* Mark DMA descriptors from begin up to and not including end as unused */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001143static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001144 unsigned int end)
1145{
1146 unsigned int frag;
1147
1148 for (frag = begin; frag != end; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001149 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001150
Nicolas Ferre4df95132013-06-04 21:57:12 +00001151 desc->addr &= ~MACB_BIT(RX_USED);
1152 }
1153
1154 /* Make descriptor updates visible to hardware */
1155 wmb();
1156
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001157 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +00001158 * whatever caused this is updated, so we don't have to record
1159 * anything.
1160 */
1161}
1162
Antoine Tenart97236cd2019-06-21 17:30:02 +02001163static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1164 int budget)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001165{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001166 struct macb *bp = queue->bp;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001167 unsigned int len;
1168 unsigned int entry;
1169 struct sk_buff *skb;
1170 struct macb_dma_desc *desc;
1171 int count = 0;
1172
1173 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +05301174 u32 ctrl;
1175 dma_addr_t addr;
1176 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001177
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001178 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1179 desc = macb_rx_desc(queue, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001180
1181 /* Make hw descriptor updates visible to CPU */
1182 rmb();
1183
Harini Katakamfff80192016-08-09 13:15:53 +05301184 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001185 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001186
Harini Katakamfff80192016-08-09 13:15:53 +05301187 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001188 break;
1189
Anssi Hannula6e0af292018-12-17 15:05:41 +02001190 /* Ensure ctrl is at least as up-to-date as rxused */
1191 dma_rmb();
1192
1193 ctrl = desc->ctrl;
1194
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001195 queue->rx_tail++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001196 count++;
1197
1198 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1199 netdev_err(bp->dev,
1200 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001201 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001202 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001203 break;
1204 }
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001205 skb = queue->rx_skbuff[entry];
Nicolas Ferre4df95132013-06-04 21:57:12 +00001206 if (unlikely(!skb)) {
1207 netdev_err(bp->dev,
1208 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001209 bp->dev->stats.rx_dropped++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001210 queue->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001211 break;
1212 }
1213 /* now everything is ready for receiving packet */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001214 queue->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301215 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001216
1217 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1218
1219 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001220 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -08001221 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001222
1223 skb->protocol = eth_type_trans(skb, bp->dev);
1224 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001225 if (bp->dev->features & NETIF_F_RXCSUM &&
1226 !(bp->dev->flags & IFF_PROMISC) &&
1227 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1228 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001229
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001230 bp->dev->stats.rx_packets++;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001231 queue->stats.rx_packets++;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001232 bp->dev->stats.rx_bytes += skb->len;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00001233 queue->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001234
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01001235 gem_ptp_do_rxstamp(bp, skb, desc);
1236
Nicolas Ferre4df95132013-06-04 21:57:12 +00001237#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1238 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1239 skb->len, skb->csum);
1240 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +01001241 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001242 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1243 skb->data, 32, true);
1244#endif
1245
Antoine Tenart97236cd2019-06-21 17:30:02 +02001246 napi_gro_receive(napi, skb);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001247 }
1248
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001249 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001250
1251 return count;
1252}
1253
Antoine Tenart97236cd2019-06-21 17:30:02 +02001254static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1255 unsigned int first_frag, unsigned int last_frag)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001256{
1257 unsigned int len;
1258 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001259 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001260 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001261 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001262 struct macb *bp = queue->bp;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001263
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001264 desc = macb_rx_desc(queue, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301265 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001266
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001267 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -05001268 macb_rx_ring_wrap(bp, first_frag),
1269 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001270
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001271 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001272 * first buffer. Since the header is 14 bytes, this makes the
1273 * payload word-aligned.
1274 *
1275 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1276 * the two padding bytes into the skb so that we avoid hitting
1277 * the slowpath in memcpy(), and pull them off afterwards.
1278 */
1279 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001280 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001281 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001282 for (frag = first_frag; ; frag++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001283 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001284 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001285 if (frag == last_frag)
1286 break;
1287 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001288
1289 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001290 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001291
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001292 return 1;
1293 }
1294
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001295 offset = 0;
1296 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001297 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001298 skb_put(skb, len);
1299
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001300 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001301 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001302
1303 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001304 if (unlikely(frag != last_frag)) {
1305 dev_kfree_skb_any(skb);
1306 return -1;
1307 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001308 frag_len = len - offset;
1309 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001310 skb_copy_to_linear_data_offset(skb, offset,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001311 macb_rx_buffer(queue, frag),
Moritz Fischeraa50b552016-03-29 19:11:13 -07001312 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001313 offset += bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001314 desc = macb_rx_desc(queue, frag);
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001315 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001316
1317 if (frag == last_frag)
1318 break;
1319 }
1320
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001321 /* Make descriptor updates visible to hardware */
1322 wmb();
1323
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001324 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001325 skb->protocol = eth_type_trans(skb, bp->dev);
1326
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001327 bp->dev->stats.rx_packets++;
1328 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001329 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001330 skb->len, skb->csum);
Antoine Tenart97236cd2019-06-21 17:30:02 +02001331 napi_gro_receive(napi, skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001332
1333 return 0;
1334}
1335
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001336static inline void macb_init_rx_ring(struct macb_queue *queue)
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001337{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001338 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001339 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001340 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001341 int i;
1342
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001343 addr = queue->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001344 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001345 desc = macb_rx_desc(queue, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001346 macb_set_addr(bp, desc, addr);
1347 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001348 addr += bp->rx_buffer_size;
1349 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001350 desc->addr |= MACB_BIT(RX_WRAP);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001351 queue->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001352}
1353
Antoine Tenart97236cd2019-06-21 17:30:02 +02001354static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1355 int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001356{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001357 struct macb *bp = queue->bp;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001358 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001359 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001360 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001361 int first_frag = -1;
1362
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001363 for (tail = queue->rx_tail; budget > 0; tail++) {
1364 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001365 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001366
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001367 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001368 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001369
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001370 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001371 break;
1372
Anssi Hannula6e0af292018-12-17 15:05:41 +02001373 /* Ensure ctrl is at least as up-to-date as addr */
1374 dma_rmb();
1375
1376 ctrl = desc->ctrl;
1377
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001378 if (ctrl & MACB_BIT(RX_SOF)) {
1379 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001380 discard_partial_frame(queue, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001381 first_frag = tail;
1382 }
1383
1384 if (ctrl & MACB_BIT(RX_EOF)) {
1385 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001386
1387 if (unlikely(first_frag == -1)) {
1388 reset_rx_queue = true;
1389 continue;
1390 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001391
Antoine Tenart97236cd2019-06-21 17:30:02 +02001392 dropped = macb_rx_frame(queue, napi, first_frag, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001393 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001394 if (unlikely(dropped < 0)) {
1395 reset_rx_queue = true;
1396 continue;
1397 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001398 if (!dropped) {
1399 received++;
1400 budget--;
1401 }
1402 }
1403 }
1404
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001405 if (unlikely(reset_rx_queue)) {
1406 unsigned long flags;
1407 u32 ctrl;
1408
1409 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1410
1411 spin_lock_irqsave(&bp->lock, flags);
1412
1413 ctrl = macb_readl(bp, NCR);
1414 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1415
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001416 macb_init_rx_ring(queue);
1417 queue_writel(queue, RBQP, queue->rx_ring_dma);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001418
1419 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1420
1421 spin_unlock_irqrestore(&bp->lock, flags);
1422 return received;
1423 }
1424
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001425 if (first_frag != -1)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001426 queue->rx_tail = first_frag;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001428 queue->rx_tail = tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001429
1430 return received;
1431}
1432
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001433static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001434{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001435 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1436 struct macb *bp = queue->bp;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001437 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001438 u32 status;
1439
1440 status = macb_readl(bp, RSR);
1441 macb_writel(bp, RSR, status);
1442
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001443 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001444 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001445
Antoine Tenart97236cd2019-06-21 17:30:02 +02001446 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001447 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001448 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001449
Nicolas Ferre8770e912013-02-12 11:08:48 +01001450 /* Packets received while interrupts were disabled */
1451 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001452 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001453 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001454 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001455 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001456 } else {
Harini Katakame5010702019-01-29 15:20:03 +05301457 queue_writel(queue, IER, bp->rx_intr_mask);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001458 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001459 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001460
1461 /* TODO: Handle errors */
1462
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001463 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001464}
1465
Harini Katakam032dc412018-01-27 12:09:01 +05301466static void macb_hresp_error_task(unsigned long data)
1467{
1468 struct macb *bp = (struct macb *)data;
1469 struct net_device *dev = bp->dev;
1470 struct macb_queue *queue = bp->queues;
1471 unsigned int q;
1472 u32 ctrl;
1473
1474 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakame5010702019-01-29 15:20:03 +05301475 queue_writel(queue, IDR, bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301476 MACB_TX_INT_FLAGS |
1477 MACB_BIT(HRESP));
1478 }
1479 ctrl = macb_readl(bp, NCR);
1480 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1481 macb_writel(bp, NCR, ctrl);
1482
1483 netif_tx_stop_all_queues(dev);
1484 netif_carrier_off(dev);
1485
1486 bp->macbgem_ops.mog_init_rings(bp);
1487
1488 /* Initialize TX and RX buffers */
Antoine Tenart6e952d92019-11-13 10:00:05 +01001489 macb_init_buffers(bp);
Harini Katakam032dc412018-01-27 12:09:01 +05301490
Antoine Tenart6e952d92019-11-13 10:00:05 +01001491 /* Enable interrupts */
1492 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
Harini Katakam032dc412018-01-27 12:09:01 +05301493 queue_writel(queue, IER,
Harini Katakame5010702019-01-29 15:20:03 +05301494 bp->rx_intr_mask |
Harini Katakam032dc412018-01-27 12:09:01 +05301495 MACB_TX_INT_FLAGS |
1496 MACB_BIT(HRESP));
Harini Katakam032dc412018-01-27 12:09:01 +05301497
1498 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1499 macb_writel(bp, NCR, ctrl);
1500
1501 netif_carrier_on(dev);
1502 netif_tx_start_all_queues(dev);
1503}
1504
Claudiu Beznea42983882018-12-17 10:02:42 +00001505static void macb_tx_restart(struct macb_queue *queue)
1506{
1507 unsigned int head = queue->tx_head;
1508 unsigned int tail = queue->tx_tail;
1509 struct macb *bp = queue->bp;
1510
1511 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1512 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1513
1514 if (head == tail)
1515 return;
1516
1517 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1518}
1519
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001520static irqreturn_t macb_interrupt(int irq, void *dev_id)
1521{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001522 struct macb_queue *queue = dev_id;
1523 struct macb *bp = queue->bp;
1524 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001525 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001526
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001527 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001528
1529 if (unlikely(!status))
1530 return IRQ_NONE;
1531
1532 spin_lock(&bp->lock);
1533
1534 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001535 /* close possible race with dev_close */
1536 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001537 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001538 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1539 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001540 break;
1541 }
1542
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001543 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1544 (unsigned int)(queue - bp->queues),
1545 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001546
Harini Katakame5010702019-01-29 15:20:03 +05301547 if (status & bp->rx_intr_mask) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001548 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001549 * until we have processed the buffers. The
1550 * scheduling call may fail if the poll routine
1551 * is already scheduled, so disable interrupts
1552 * now.
1553 */
Harini Katakame5010702019-01-29 15:20:03 +05301554 queue_writel(queue, IDR, bp->rx_intr_mask);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001555 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001556 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001557
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001558 if (napi_schedule_prep(&queue->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001559 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00001560 __napi_schedule(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001561 }
1562 }
1563
Nicolas Ferree86cd532012-10-31 06:04:57 +00001564 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001565 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1566 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001567
1568 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001569 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001570
Nicolas Ferree86cd532012-10-31 06:04:57 +00001571 break;
1572 }
1573
1574 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001575 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001576
Claudiu Beznea42983882018-12-17 10:02:42 +00001577 if (status & MACB_BIT(TXUBR))
1578 macb_tx_restart(queue);
1579
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001580 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001581 * add that if/when we get our hands on a full-blown MII PHY.
1582 */
1583
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001584 /* There is a hardware issue under heavy load where DMA can
1585 * stop, this causes endless "used buffer descriptor read"
1586 * interrupts but it can be cleared by re-enabling RX. See
Harini Katakame5010702019-01-29 15:20:03 +05301587 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1588 * section 16.7.4 for details. RXUBR is only enabled for
1589 * these two versions.
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001590 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001591 if (status & MACB_BIT(RXUBR)) {
1592 ctrl = macb_readl(bp, NCR);
1593 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001594 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001595 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1596
1597 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001598 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001599 }
1600
Alexander Steinb19f7f72011-04-13 05:03:24 +00001601 if (status & MACB_BIT(ISR_ROVR)) {
1602 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001603 if (macb_is_gem(bp))
1604 bp->hw_stats.gem.rx_overruns++;
1605 else
1606 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001607
1608 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001609 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001610 }
1611
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001612 if (status & MACB_BIT(HRESP)) {
Harini Katakam032dc412018-01-27 12:09:01 +05301613 tasklet_schedule(&bp->hresp_err_tasklet);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001614 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001615
1616 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001617 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001618 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001619 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001620 }
1621
1622 spin_unlock(&bp->lock);
1623
1624 return IRQ_HANDLED;
1625}
1626
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001627#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001628/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001629 * to allow network i/o with interrupts disabled.
1630 */
1631static void macb_poll_controller(struct net_device *dev)
1632{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001633 struct macb *bp = netdev_priv(dev);
1634 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001635 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001636 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001637
1638 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001639 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1640 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001641 local_irq_restore(flags);
1642}
1643#endif
1644
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001645static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001646 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001647 struct sk_buff *skb,
1648 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001649{
1650 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001651 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001652 struct macb_tx_skb *tx_skb = NULL;
1653 struct macb_dma_desc *desc;
1654 unsigned int offset, size, count = 0;
1655 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001656 unsigned int eof = 1, mss_mfs = 0;
1657 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1658
1659 /* LSO */
1660 if (skb_shinfo(skb)->gso_size != 0) {
1661 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1662 /* UDP - UFO */
1663 lso_ctrl = MACB_LSO_UFO_ENABLE;
1664 else
1665 /* TCP - TSO */
1666 lso_ctrl = MACB_LSO_TSO_ENABLE;
1667 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001668
1669 /* First, map non-paged data */
1670 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001671
1672 /* first buffer length */
1673 size = hdrlen;
1674
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001675 offset = 0;
1676 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001677 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001678 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001679
1680 mapping = dma_map_single(&bp->pdev->dev,
1681 skb->data + offset,
1682 size, DMA_TO_DEVICE);
1683 if (dma_mapping_error(&bp->pdev->dev, mapping))
1684 goto dma_error;
1685
1686 /* Save info to properly release resources */
1687 tx_skb->skb = NULL;
1688 tx_skb->mapping = mapping;
1689 tx_skb->size = size;
1690 tx_skb->mapped_as_page = false;
1691
1692 len -= size;
1693 offset += size;
1694 count++;
1695 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001696
1697 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001698 }
1699
1700 /* Then, map paged data from fragments */
1701 for (f = 0; f < nr_frags; f++) {
1702 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1703
1704 len = skb_frag_size(frag);
1705 offset = 0;
1706 while (len) {
1707 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001708 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001709 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001710
1711 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1712 offset, size, DMA_TO_DEVICE);
1713 if (dma_mapping_error(&bp->pdev->dev, mapping))
1714 goto dma_error;
1715
1716 /* Save info to properly release resources */
1717 tx_skb->skb = NULL;
1718 tx_skb->mapping = mapping;
1719 tx_skb->size = size;
1720 tx_skb->mapped_as_page = true;
1721
1722 len -= size;
1723 offset += size;
1724 count++;
1725 tx_head++;
1726 }
1727 }
1728
1729 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001730 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001731 netdev_err(bp->dev, "BUG! empty skb!\n");
1732 return 0;
1733 }
1734
1735 /* This is the last buffer of the frame: save socket buffer */
1736 tx_skb->skb = skb;
1737
1738 /* Update TX ring: update buffer descriptors in reverse order
1739 * to avoid race condition
1740 */
1741
1742 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1743 * to set the end of TX queue
1744 */
1745 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001746 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001747 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001748 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001749 desc->ctrl = ctrl;
1750
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001751 if (lso_ctrl) {
1752 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1753 /* include header and FCS in value given to h/w */
1754 mss_mfs = skb_shinfo(skb)->gso_size +
1755 skb_transport_offset(skb) +
1756 ETH_FCS_LEN;
1757 else /* TSO */ {
1758 mss_mfs = skb_shinfo(skb)->gso_size;
1759 /* TCP Sequence Number Source Select
1760 * can be set only for TSO
1761 */
1762 seq_ctrl = 0;
1763 }
1764 }
1765
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001766 do {
1767 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001768 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001769 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001770 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001771
1772 ctrl = (u32)tx_skb->size;
1773 if (eof) {
1774 ctrl |= MACB_BIT(TX_LAST);
1775 eof = 0;
1776 }
Zach Brownb410d132016-10-19 09:56:57 -05001777 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001778 ctrl |= MACB_BIT(TX_WRAP);
1779
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001780 /* First descriptor is header descriptor */
1781 if (i == queue->tx_head) {
1782 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1783 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001784 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1785 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1786 ctrl |= MACB_BIT(TX_NOCRC);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001787 } else
1788 /* Only set MSS/MFS on payload descriptors
1789 * (second or later descriptor)
1790 */
1791 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1792
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001793 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001794 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001795 /* desc->addr must be visible to hardware before clearing
1796 * 'TX_USED' bit in desc->ctrl.
1797 */
1798 wmb();
1799 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001800 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001801
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001802 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001803
1804 return count;
1805
1806dma_error:
1807 netdev_err(bp->dev, "TX DMA map failed\n");
1808
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001809 for (i = queue->tx_head; i != tx_head; i++) {
1810 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001811
1812 macb_tx_unmap(bp, tx_skb);
1813 }
1814
1815 return 0;
1816}
1817
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001818static netdev_features_t macb_features_check(struct sk_buff *skb,
1819 struct net_device *dev,
1820 netdev_features_t features)
1821{
1822 unsigned int nr_frags, f;
1823 unsigned int hdrlen;
1824
1825 /* Validate LSO compatibility */
1826
Harini Katakam41c1ef92020-02-05 18:08:11 +05301827 /* there is only one buffer or protocol is not UDP */
1828 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001829 return features;
1830
1831 /* length of header */
1832 hdrlen = skb_transport_offset(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001833
Harini Katakam41c1ef92020-02-05 18:08:11 +05301834 /* For UFO only:
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001835 * When software supplies two or more payload buffers all payload buffers
1836 * apart from the last must be a multiple of 8 bytes in size.
1837 */
1838 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1839 return features & ~MACB_NETIF_LSO;
1840
1841 nr_frags = skb_shinfo(skb)->nr_frags;
1842 /* No need to check last fragment */
1843 nr_frags--;
1844 for (f = 0; f < nr_frags; f++) {
1845 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1846
1847 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1848 return features & ~MACB_NETIF_LSO;
1849 }
1850 return features;
1851}
1852
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001853static inline int macb_clear_csum(struct sk_buff *skb)
1854{
1855 /* no change for packets without checksum offloading */
1856 if (skb->ip_summed != CHECKSUM_PARTIAL)
1857 return 0;
1858
1859 /* make sure we can modify the header */
1860 if (unlikely(skb_cow_head(skb, 0)))
1861 return -1;
1862
1863 /* initialize checksum field
1864 * This is required - at least for Zynq, which otherwise calculates
1865 * wrong UDP header checksums for UDP packets with UDP data len <=2
1866 */
1867 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1868 return 0;
1869}
1870
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001871static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1872{
1873 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1874 int padlen = ETH_ZLEN - (*skb)->len;
1875 int headroom = skb_headroom(*skb);
1876 int tailroom = skb_tailroom(*skb);
1877 struct sk_buff *nskb;
1878 u32 fcs;
1879
1880 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1881 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1882 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1883 return 0;
1884
1885 if (padlen <= 0) {
1886 /* FCS could be appeded to tailroom. */
1887 if (tailroom >= ETH_FCS_LEN)
1888 goto add_fcs;
1889 /* FCS could be appeded by moving data to headroom. */
1890 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1891 padlen = 0;
1892 /* No room for FCS, need to reallocate skb. */
1893 else
Tristram Ha899ecae2018-10-24 14:51:23 -07001894 padlen = ETH_FCS_LEN;
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001895 } else {
1896 /* Add room for FCS. */
1897 padlen += ETH_FCS_LEN;
1898 }
1899
1900 if (!cloned && headroom + tailroom >= padlen) {
1901 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1902 skb_set_tail_pointer(*skb, (*skb)->len);
1903 } else {
1904 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1905 if (!nskb)
1906 return -ENOMEM;
1907
Huang Zijiangf3e5c072019-02-14 14:41:18 +08001908 dev_consume_skb_any(*skb);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001909 *skb = nskb;
1910 }
1911
Claudiu Bezneaba3e1842019-01-03 14:59:35 +00001912 if (padlen > ETH_FCS_LEN)
1913 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001914
1915add_fcs:
1916 /* set FCS to packet */
1917 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1918 fcs = ~fcs;
1919
1920 skb_put_u8(*skb, fcs & 0xff);
1921 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1922 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1923 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1924
1925 return 0;
1926}
1927
Claudiu Beznead1c38952018-08-07 12:25:12 +03001928static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001929{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001930 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001931 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001932 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001933 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001934 unsigned int desc_cnt, nr_frags, frag_size, f;
1935 unsigned int hdrlen;
1936 bool is_lso, is_udp = 0;
Claudiu Beznead1c38952018-08-07 12:25:12 +03001937 netdev_tx_t ret = NETDEV_TX_OK;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001938
Claudiu Beznea33729f22018-08-07 12:25:13 +03001939 if (macb_clear_csum(skb)) {
1940 dev_kfree_skb_any(skb);
1941 return ret;
1942 }
1943
Claudiu Beznea653e92a2018-08-07 12:25:14 +03001944 if (macb_pad_and_fcs(&skb, dev)) {
1945 dev_kfree_skb_any(skb);
1946 return ret;
1947 }
1948
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001949 is_lso = (skb_shinfo(skb)->gso_size != 0);
1950
1951 if (is_lso) {
1952 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1953
1954 /* length of headers */
1955 if (is_udp)
1956 /* only queue eth + ip headers separately for UDP */
1957 hdrlen = skb_transport_offset(skb);
1958 else
1959 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1960 if (skb_headlen(skb) < hdrlen) {
1961 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1962 /* if this is required, would need to copy to single buffer */
1963 return NETDEV_TX_BUSY;
1964 }
1965 } else
1966 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001967
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001968#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1969 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001970 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1971 queue_index, skb->len, skb->head, skb->data,
1972 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001973 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1974 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001975#endif
1976
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001977 /* Count how many TX buffer descriptors are needed to send this
1978 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001979 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001980 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001981 if (is_lso && (skb_headlen(skb) > hdrlen))
1982 /* extra header descriptor if also payload in first buffer */
1983 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1984 else
1985 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001986 nr_frags = skb_shinfo(skb)->nr_frags;
1987 for (f = 0; f < nr_frags; f++) {
1988 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001989 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001990 }
1991
Dongdong Deng48719532009-08-23 19:49:07 -07001992 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001993
1994 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001995 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001996 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001997 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001998 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001999 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002000 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00002001 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002002 }
2003
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002004 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002005 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07002006 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08002007 goto unlock;
2008 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00002009
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00002010 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002011 wmb();
Richard Cochrane0720922011-06-19 21:51:28 +00002012 skb_tx_timestamp(skb);
2013
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002014 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
2015
Zach Brownb410d132016-10-19 09:56:57 -05002016 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002017 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002018
Soren Brinkmann92030902014-03-04 08:46:39 -08002019unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07002020 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002021
Claudiu Beznead1c38952018-08-07 12:25:12 +03002022 return ret;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002023}
2024
Nicolas Ferre4df95132013-06-04 21:57:12 +00002025static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00002026{
2027 if (!macb_is_gem(bp)) {
2028 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
2029 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002030 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00002031
Nicolas Ferre1b447912013-06-04 21:57:11 +00002032 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002033 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07002034 "RX buffer must be multiple of %d bytes, expanding\n",
2035 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002036 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00002037 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002038 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00002039 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002040
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002041 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00002042 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002043}
2044
Nicolas Ferre4df95132013-06-04 21:57:12 +00002045static void gem_free_rx_buffers(struct macb *bp)
2046{
2047 struct sk_buff *skb;
2048 struct macb_dma_desc *desc;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002049 struct macb_queue *queue;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002050 dma_addr_t addr;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002051 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002052 int i;
2053
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002054 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2055 if (!queue->rx_skbuff)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002056 continue;
2057
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002058 for (i = 0; i < bp->rx_ring_size; i++) {
2059 skb = queue->rx_skbuff[i];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002060
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002061 if (!skb)
2062 continue;
2063
2064 desc = macb_rx_desc(queue, i);
2065 addr = macb_get_addr(bp, desc);
2066
2067 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
2068 DMA_FROM_DEVICE);
2069 dev_kfree_skb_any(skb);
2070 skb = NULL;
2071 }
2072
2073 kfree(queue->rx_skbuff);
2074 queue->rx_skbuff = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002075 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002076}
2077
2078static void macb_free_rx_buffers(struct macb *bp)
2079{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002080 struct macb_queue *queue = &bp->queues[0];
2081
2082 if (queue->rx_buffers) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00002083 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05002084 bp->rx_ring_size * bp->rx_buffer_size,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002085 queue->rx_buffers, queue->rx_buffers_dma);
2086 queue->rx_buffers = NULL;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002087 }
2088}
Nicolas Ferre1b447912013-06-04 21:57:11 +00002089
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002090static void macb_free_consistent(struct macb *bp)
2091{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002092 struct macb_queue *queue;
2093 unsigned int q;
Harini Katakam404cd082018-07-06 12:18:58 +05302094 int size;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002095
Nicolas Ferre4df95132013-06-04 21:57:12 +00002096 bp->macbgem_ops.mog_free_rx_buffers(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002097
2098 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2099 kfree(queue->tx_skb);
2100 queue->tx_skb = NULL;
2101 if (queue->tx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302102 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2103 dma_free_coherent(&bp->pdev->dev, size,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002104 queue->tx_ring, queue->tx_ring_dma);
2105 queue->tx_ring = NULL;
2106 }
Harini Katakame50b7702018-07-06 12:18:57 +05302107 if (queue->rx_ring) {
Harini Katakam404cd082018-07-06 12:18:58 +05302108 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2109 dma_free_coherent(&bp->pdev->dev, size,
Harini Katakame50b7702018-07-06 12:18:57 +05302110 queue->rx_ring, queue->rx_ring_dma);
2111 queue->rx_ring = NULL;
2112 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002113 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002114}
2115
2116static int gem_alloc_rx_buffers(struct macb *bp)
2117{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002118 struct macb_queue *queue;
2119 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002120 int size;
2121
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002122 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2123 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2124 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2125 if (!queue->rx_skbuff)
2126 return -ENOMEM;
2127 else
2128 netdev_dbg(bp->dev,
2129 "Allocated %d RX struct sk_buff entries at %p\n",
2130 bp->rx_ring_size, queue->rx_skbuff);
2131 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002132 return 0;
2133}
2134
2135static int macb_alloc_rx_buffers(struct macb *bp)
2136{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002137 struct macb_queue *queue = &bp->queues[0];
Nicolas Ferre4df95132013-06-04 21:57:12 +00002138 int size;
2139
Zach Brownb410d132016-10-19 09:56:57 -05002140 size = bp->rx_ring_size * bp->rx_buffer_size;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002141 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2142 &queue->rx_buffers_dma, GFP_KERNEL);
2143 if (!queue->rx_buffers)
Nicolas Ferre4df95132013-06-04 21:57:12 +00002144 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002145
2146 netdev_dbg(bp->dev,
2147 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002148 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002149 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002150}
2151
2152static int macb_alloc_consistent(struct macb *bp)
2153{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002154 struct macb_queue *queue;
2155 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002156 int size;
2157
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002158 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakam404cd082018-07-06 12:18:58 +05302159 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002160 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2161 &queue->tx_ring_dma,
2162 GFP_KERNEL);
2163 if (!queue->tx_ring)
2164 goto out_err;
2165 netdev_dbg(bp->dev,
2166 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2167 q, size, (unsigned long)queue->tx_ring_dma,
2168 queue->tx_ring);
2169
Zach Brownb410d132016-10-19 09:56:57 -05002170 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002171 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2172 if (!queue->tx_skb)
2173 goto out_err;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002174
Harini Katakam404cd082018-07-06 12:18:58 +05302175 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002176 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2177 &queue->rx_ring_dma, GFP_KERNEL);
2178 if (!queue->rx_ring)
2179 goto out_err;
2180 netdev_dbg(bp->dev,
2181 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2182 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002183 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002184 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002185 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002186
2187 return 0;
2188
2189out_err:
2190 macb_free_consistent(bp);
2191 return -ENOMEM;
2192}
2193
Nicolas Ferre4df95132013-06-04 21:57:12 +00002194static void gem_init_rings(struct macb *bp)
2195{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002196 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002197 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002198 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002199 int i;
2200
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002201 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05002202 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002203 desc = macb_tx_desc(queue, i);
2204 macb_set_addr(bp, desc, 0);
2205 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002206 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002207 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002208 queue->tx_head = 0;
2209 queue->tx_tail = 0;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002210
2211 queue->rx_tail = 0;
2212 queue->rx_prepared_head = 0;
2213
2214 gem_rx_refill(queue);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002215 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00002216
Nicolas Ferre4df95132013-06-04 21:57:12 +00002217}
2218
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002219static void macb_init_rings(struct macb *bp)
2220{
2221 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002222 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002223
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002224 macb_init_rx_ring(&bp->queues[0]);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002225
Zach Brownb410d132016-10-19 09:56:57 -05002226 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002227 desc = macb_tx_desc(&bp->queues[0], i);
2228 macb_set_addr(bp, desc, 0);
2229 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002230 }
Ben Shelton21d35152015-04-22 17:28:54 -05002231 bp->queues[0].tx_head = 0;
2232 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002233 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002234}
2235
2236static void macb_reset_hw(struct macb *bp)
2237{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002238 struct macb_queue *queue;
2239 unsigned int q;
Anssi Hannula0da70f82018-08-23 10:45:22 +03002240 u32 ctrl = macb_readl(bp, NCR);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002241
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002242 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002243 * more gracefully?)
2244 */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002245 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002246
2247 /* Clear the stats registers (XXX: Update stats first?) */
Anssi Hannula0da70f82018-08-23 10:45:22 +03002248 ctrl |= MACB_BIT(CLRSTAT);
2249
2250 macb_writel(bp, NCR, ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002251
2252 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00002253 macb_writel(bp, TSR, -1);
2254 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002255
2256 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002257 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2258 queue_writel(queue, IDR, -1);
2259 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06002260 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2261 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002262 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002263}
2264
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002265static u32 gem_mdc_clk_div(struct macb *bp)
2266{
2267 u32 config;
2268 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2269
2270 if (pclk_hz <= 20000000)
2271 config = GEM_BF(CLK, GEM_CLK_DIV8);
2272 else if (pclk_hz <= 40000000)
2273 config = GEM_BF(CLK, GEM_CLK_DIV16);
2274 else if (pclk_hz <= 80000000)
2275 config = GEM_BF(CLK, GEM_CLK_DIV32);
2276 else if (pclk_hz <= 120000000)
2277 config = GEM_BF(CLK, GEM_CLK_DIV48);
2278 else if (pclk_hz <= 160000000)
2279 config = GEM_BF(CLK, GEM_CLK_DIV64);
2280 else
2281 config = GEM_BF(CLK, GEM_CLK_DIV96);
2282
2283 return config;
2284}
2285
2286static u32 macb_mdc_clk_div(struct macb *bp)
2287{
2288 u32 config;
2289 unsigned long pclk_hz;
2290
2291 if (macb_is_gem(bp))
2292 return gem_mdc_clk_div(bp);
2293
2294 pclk_hz = clk_get_rate(bp->pclk);
2295 if (pclk_hz <= 20000000)
2296 config = MACB_BF(CLK, MACB_CLK_DIV8);
2297 else if (pclk_hz <= 40000000)
2298 config = MACB_BF(CLK, MACB_CLK_DIV16);
2299 else if (pclk_hz <= 80000000)
2300 config = MACB_BF(CLK, MACB_CLK_DIV32);
2301 else
2302 config = MACB_BF(CLK, MACB_CLK_DIV64);
2303
2304 return config;
2305}
2306
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002307/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00002308 * should program. We find the width from decoding the design configuration
2309 * register to find the maximum supported data bus width.
2310 */
2311static u32 macb_dbw(struct macb *bp)
2312{
2313 if (!macb_is_gem(bp))
2314 return 0;
2315
2316 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2317 case 4:
2318 return GEM_BF(DBW, GEM_DBW128);
2319 case 2:
2320 return GEM_BF(DBW, GEM_DBW64);
2321 case 1:
2322 default:
2323 return GEM_BF(DBW, GEM_DBW32);
2324 }
2325}
2326
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002327/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002328 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02002329 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002330 * (if not supported by FIFO, it will fallback to default)
2331 * - set both rx/tx packet buffers to full memory size
2332 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00002333 */
2334static void macb_configure_dma(struct macb *bp)
2335{
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002336 struct macb_queue *queue;
2337 u32 buffer_size;
2338 unsigned int q;
Jamie Iles0116da42011-03-14 17:38:30 +00002339 u32 dmacfg;
2340
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002341 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
Jamie Iles0116da42011-03-14 17:38:30 +00002342 if (macb_is_gem(bp)) {
2343 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002344 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2345 if (q)
2346 queue_writel(queue, RBQS, buffer_size);
2347 else
2348 dmacfg |= GEM_BF(RXBS, buffer_size);
2349 }
Nicolas Ferree1755872014-07-24 13:50:58 +02002350 if (bp->dma_burst_length)
2351 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00002352 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05302353 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05302354
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002355 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05302356 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2357 else
2358 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2359
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002360 if (bp->dev->features & NETIF_F_HW_CSUM)
2361 dmacfg |= GEM_BIT(TXCOEN);
2362 else
2363 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05302364
Michal Simekbd620722018-09-25 08:32:50 +02002365 dmacfg &= ~GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302366#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002367 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002368 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05302369#endif
Rafal Ozieblo7b429612017-06-29 07:12:51 +01002370#ifdef CONFIG_MACB_USE_HWSTAMP
2371 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2372 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2373#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02002374 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2375 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00002376 gem_writel(bp, DMACFG, dmacfg);
2377 }
2378}
2379
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002380static void macb_init_hw(struct macb *bp)
2381{
2382 u32 config;
2383
2384 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00002385 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002386
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002387 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00002388 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002389 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03002390 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302391 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2392 else
2393 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002394 if (bp->dev->flags & IFF_PROMISC)
2395 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002396 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2397 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002398 if (!(bp->dev->flags & IFF_BROADCAST))
2399 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00002400 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002401 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03002402 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302403 gem_writel(bp, JML, bp->jumbo_max_len);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302404 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03002405 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302406 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002407
Jamie Iles0116da42011-03-14 17:38:30 +00002408 macb_configure_dma(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002409}
2410
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002411/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002412 * locations in the memory map. The least significant bits are stored
2413 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2414 *
2415 * The unicast hash enable and the multicast hash enable bits in the
2416 * network configuration register enable the reception of hash matched
2417 * frames. The destination address is reduced to a 6 bit index into
2418 * the 64 bit hash register using the following hash function. The
2419 * hash function is an exclusive or of every sixth bit of the
2420 * destination address.
2421 *
2422 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2423 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2424 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2425 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2426 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2427 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2428 *
2429 * da[0] represents the least significant bit of the first byte
2430 * received, that is, the multicast/unicast indicator, and da[47]
2431 * represents the most significant bit of the last byte received. If
2432 * the hash index, hi[n], points to a bit that is set in the hash
2433 * register then the frame will be matched according to whether the
2434 * frame is multicast or unicast. A multicast match will be signalled
2435 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2436 * index points to a bit set in the hash register. A unicast match
2437 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2438 * and the hash index points to a bit set in the hash register. To
2439 * receive all multicast frames, the hash register should be set with
2440 * all ones and the multicast hash enable bit should be set in the
2441 * network configuration register.
2442 */
2443
2444static inline int hash_bit_value(int bitnr, __u8 *addr)
2445{
2446 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2447 return 1;
2448 return 0;
2449}
2450
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002451/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002452static int hash_get_index(__u8 *addr)
2453{
2454 int i, j, bitval;
2455 int hash_index = 0;
2456
2457 for (j = 0; j < 6; j++) {
2458 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002459 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002460
2461 hash_index |= (bitval << j);
2462 }
2463
2464 return hash_index;
2465}
2466
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002467/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002468static void macb_sethashtable(struct net_device *dev)
2469{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002470 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002471 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002472 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002473 struct macb *bp = netdev_priv(dev);
2474
Moritz Fischeraa50b552016-03-29 19:11:13 -07002475 mc_filter[0] = 0;
2476 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002477
Jiri Pirko22bedad32010-04-01 21:22:57 +00002478 netdev_for_each_mc_addr(ha, dev) {
2479 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002480 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2481 }
2482
Jamie Ilesf75ba502011-11-08 10:12:32 +00002483 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2484 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002485}
2486
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002487/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002488static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002489{
2490 unsigned long cfg;
2491 struct macb *bp = netdev_priv(dev);
2492
2493 cfg = macb_readl(bp, NCFGR);
2494
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002495 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002496 /* Enable promiscuous mode */
2497 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002498
2499 /* Disable RX checksum offload */
2500 if (macb_is_gem(bp))
2501 cfg &= ~GEM_BIT(RXCOEN);
2502 } else {
2503 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002504 cfg &= ~MACB_BIT(CAF);
2505
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002506 /* Enable RX checksum offload only if requested */
2507 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2508 cfg |= GEM_BIT(RXCOEN);
2509 }
2510
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002511 if (dev->flags & IFF_ALLMULTI) {
2512 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002513 macb_or_gem_writel(bp, HRB, -1);
2514 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002515 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002516 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002517 /* Enable specific multicasts */
2518 macb_sethashtable(dev);
2519 cfg |= MACB_BIT(NCFGR_MTI);
2520 } else if (dev->flags & (~IFF_ALLMULTI)) {
2521 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002522 macb_or_gem_writel(bp, HRB, 0);
2523 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002524 cfg &= ~MACB_BIT(NCFGR_MTI);
2525 }
2526
2527 macb_writel(bp, NCFGR, cfg);
2528}
2529
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002530static int macb_open(struct net_device *dev)
2531{
Nicolas Ferre4df95132013-06-04 21:57:12 +00002532 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Antoine Tenart7897b072019-11-13 10:00:06 +01002533 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002534 struct macb_queue *queue;
2535 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002536 int err;
2537
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002538 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002539
Harini Katakamd54f89a2019-03-01 16:20:34 +05302540 err = pm_runtime_get_sync(&bp->pdev->dev);
2541 if (err < 0)
2542 goto pm_exit;
2543
Nicolas Ferre1b447912013-06-04 21:57:11 +00002544 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002545 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002546
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002547 err = macb_alloc_consistent(bp);
2548 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002549 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2550 err);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302551 goto pm_exit;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002552 }
2553
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002554 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2555 napi_enable(&queue->napi);
2556
Harini Katakam05044532019-05-07 19:59:10 +05302557 macb_init_hw(bp);
2558
Antoine Tenart7897b072019-11-13 10:00:06 +01002559 err = macb_phylink_connect(bp);
2560 if (err)
Corentin Labbe014406b2020-06-10 09:53:44 +00002561 goto napi_exit;
frederic RODO6c36a702007-07-12 19:07:24 +02002562
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002563 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002564
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002565 if (bp->ptp_info)
2566 bp->ptp_info->ptp_init(dev);
2567
Charles Keepax939a5bf72020-06-15 14:18:54 +01002568 return 0;
2569
Corentin Labbe014406b2020-06-10 09:53:44 +00002570napi_exit:
2571 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2572 napi_disable(&queue->napi);
Harini Katakamd54f89a2019-03-01 16:20:34 +05302573pm_exit:
Charles Keepax939a5bf72020-06-15 14:18:54 +01002574 pm_runtime_put_sync(&bp->pdev->dev);
2575 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002576}
2577
2578static int macb_close(struct net_device *dev)
2579{
2580 struct macb *bp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002581 struct macb_queue *queue;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002582 unsigned long flags;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002583 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002584
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002585 netif_tx_stop_all_queues(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00002586
2587 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2588 napi_disable(&queue->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002589
Antoine Tenart7897b072019-11-13 10:00:06 +01002590 phylink_stop(bp->phylink);
2591 phylink_disconnect_phy(bp->phylink);
frederic RODO6c36a702007-07-12 19:07:24 +02002592
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002593 spin_lock_irqsave(&bp->lock, flags);
2594 macb_reset_hw(bp);
2595 netif_carrier_off(dev);
2596 spin_unlock_irqrestore(&bp->lock, flags);
2597
2598 macb_free_consistent(bp);
2599
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002600 if (bp->ptp_info)
2601 bp->ptp_info->ptp_remove(dev);
2602
Harini Katakamd54f89a2019-03-01 16:20:34 +05302603 pm_runtime_put(&bp->pdev->dev);
2604
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002605 return 0;
2606}
2607
Harini Katakama5898ea2015-05-06 22:27:18 +05302608static int macb_change_mtu(struct net_device *dev, int new_mtu)
2609{
Harini Katakama5898ea2015-05-06 22:27:18 +05302610 if (netif_running(dev))
2611 return -EBUSY;
2612
Harini Katakama5898ea2015-05-06 22:27:18 +05302613 dev->mtu = new_mtu;
2614
2615 return 0;
2616}
2617
Jamie Ilesa494ed82011-03-09 16:26:35 +00002618static void gem_update_stats(struct macb *bp)
2619{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002620 struct macb_queue *queue;
2621 unsigned int i, q, idx;
2622 unsigned long *stat;
2623
Jamie Ilesa494ed82011-03-09 16:26:35 +00002624 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002625
Xander Huff3ff13f12015-01-13 16:15:51 -06002626 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2627 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002628 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002629
2630 bp->ethtool_stats[i] += val;
2631 *p += val;
2632
2633 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2634 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002635 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002636 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002637 *(++p) += val;
2638 }
2639 }
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002640
2641 idx = GEM_STATS_LEN;
2642 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2643 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2644 bp->ethtool_stats[idx++] = *stat;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002645}
2646
2647static struct net_device_stats *gem_get_stats(struct macb *bp)
2648{
2649 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002650 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002651
2652 gem_update_stats(bp);
2653
2654 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2655 hwstat->rx_alignment_errors +
2656 hwstat->rx_resource_errors +
2657 hwstat->rx_overruns +
2658 hwstat->rx_oversize_frames +
2659 hwstat->rx_jabbers +
2660 hwstat->rx_undersized_frames +
2661 hwstat->rx_length_field_frame_errors);
2662 nstat->tx_errors = (hwstat->tx_late_collisions +
2663 hwstat->tx_excessive_collisions +
2664 hwstat->tx_underrun +
2665 hwstat->tx_carrier_sense_errors);
2666 nstat->multicast = hwstat->rx_multicast_frames;
2667 nstat->collisions = (hwstat->tx_single_collision_frames +
2668 hwstat->tx_multiple_collision_frames +
2669 hwstat->tx_excessive_collisions);
2670 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2671 hwstat->rx_jabbers +
2672 hwstat->rx_undersized_frames +
2673 hwstat->rx_length_field_frame_errors);
2674 nstat->rx_over_errors = hwstat->rx_resource_errors;
2675 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2676 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2677 nstat->rx_fifo_errors = hwstat->rx_overruns;
2678 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2679 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2680 nstat->tx_fifo_errors = hwstat->tx_underrun;
2681
2682 return nstat;
2683}
2684
Xander Huff3ff13f12015-01-13 16:15:51 -06002685static void gem_get_ethtool_stats(struct net_device *dev,
2686 struct ethtool_stats *stats, u64 *data)
2687{
2688 struct macb *bp;
2689
2690 bp = netdev_priv(dev);
2691 gem_update_stats(bp);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002692 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2693 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
Xander Huff3ff13f12015-01-13 16:15:51 -06002694}
2695
2696static int gem_get_sset_count(struct net_device *dev, int sset)
2697{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002698 struct macb *bp = netdev_priv(dev);
2699
Xander Huff3ff13f12015-01-13 16:15:51 -06002700 switch (sset) {
2701 case ETH_SS_STATS:
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002702 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
Xander Huff3ff13f12015-01-13 16:15:51 -06002703 default:
2704 return -EOPNOTSUPP;
2705 }
2706}
2707
2708static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2709{
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002710 char stat_string[ETH_GSTRING_LEN];
2711 struct macb *bp = netdev_priv(dev);
2712 struct macb_queue *queue;
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002713 unsigned int i;
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002714 unsigned int q;
Xander Huff3ff13f12015-01-13 16:15:51 -06002715
2716 switch (sset) {
2717 case ETH_SS_STATS:
2718 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2719 memcpy(p, gem_statistics[i].stat_string,
2720 ETH_GSTRING_LEN);
Rafal Ozieblo512286b2017-11-30 18:19:56 +00002721
2722 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2723 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2724 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2725 q, queue_statistics[i].stat_string);
2726 memcpy(p, stat_string, ETH_GSTRING_LEN);
2727 }
2728 }
Xander Huff3ff13f12015-01-13 16:15:51 -06002729 break;
2730 }
2731}
2732
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002733static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002734{
2735 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002736 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002737 struct macb_stats *hwstat = &bp->hw_stats.macb;
2738
2739 if (macb_is_gem(bp))
2740 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002741
frederic RODO6c36a702007-07-12 19:07:24 +02002742 /* read stats from hardware */
2743 macb_update_stats(bp);
2744
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002745 /* Convert HW stats into netdevice stats */
2746 nstat->rx_errors = (hwstat->rx_fcs_errors +
2747 hwstat->rx_align_errors +
2748 hwstat->rx_resource_errors +
2749 hwstat->rx_overruns +
2750 hwstat->rx_oversize_pkts +
2751 hwstat->rx_jabbers +
2752 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002753 hwstat->rx_length_mismatch);
2754 nstat->tx_errors = (hwstat->tx_late_cols +
2755 hwstat->tx_excessive_cols +
2756 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002757 hwstat->tx_carrier_errors +
2758 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002759 nstat->collisions = (hwstat->tx_single_cols +
2760 hwstat->tx_multiple_cols +
2761 hwstat->tx_excessive_cols);
2762 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2763 hwstat->rx_jabbers +
2764 hwstat->rx_undersize_pkts +
2765 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002766 nstat->rx_over_errors = hwstat->rx_resource_errors +
2767 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002768 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2769 nstat->rx_frame_errors = hwstat->rx_align_errors;
2770 nstat->rx_fifo_errors = hwstat->rx_overruns;
2771 /* XXX: What does "missed" mean? */
2772 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2773 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2774 nstat->tx_fifo_errors = hwstat->tx_underruns;
2775 /* Don't know about heartbeat or window errors... */
2776
2777 return nstat;
2778}
2779
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002780static int macb_get_regs_len(struct net_device *netdev)
2781{
2782 return MACB_GREGS_NBR * sizeof(u32);
2783}
2784
2785static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2786 void *p)
2787{
2788 struct macb *bp = netdev_priv(dev);
2789 unsigned int tail, head;
2790 u32 *regs_buff = p;
2791
2792 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2793 | MACB_GREGS_VERSION;
2794
Zach Brownb410d132016-10-19 09:56:57 -05002795 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2796 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002797
2798 regs_buff[0] = macb_readl(bp, NCR);
2799 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2800 regs_buff[2] = macb_readl(bp, NSR);
2801 regs_buff[3] = macb_readl(bp, TSR);
2802 regs_buff[4] = macb_readl(bp, RBQP);
2803 regs_buff[5] = macb_readl(bp, TBQP);
2804 regs_buff[6] = macb_readl(bp, RSR);
2805 regs_buff[7] = macb_readl(bp, IMR);
2806
2807 regs_buff[8] = tail;
2808 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002809 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2810 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002811
Neil Armstrongce721a72016-01-05 14:39:16 +01002812 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2813 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002814 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002815 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002816}
2817
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002818static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2819{
2820 struct macb *bp = netdev_priv(netdev);
2821
2822 wol->supported = 0;
2823 wol->wolopts = 0;
2824
Antoine Tenart7897b072019-11-13 10:00:06 +01002825 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET)
2826 phylink_ethtool_get_wol(bp->phylink, wol);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002827}
2828
2829static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2830{
2831 struct macb *bp = netdev_priv(netdev);
Antoine Tenart7897b072019-11-13 10:00:06 +01002832 int ret;
2833
2834 ret = phylink_ethtool_set_wol(bp->phylink, wol);
2835 if (!ret)
2836 return 0;
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002837
2838 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2839 (wol->wolopts & ~WAKE_MAGIC))
2840 return -EOPNOTSUPP;
2841
2842 if (wol->wolopts & WAKE_MAGIC)
2843 bp->wol |= MACB_WOL_ENABLED;
2844 else
2845 bp->wol &= ~MACB_WOL_ENABLED;
2846
2847 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2848
2849 return 0;
2850}
2851
Antoine Tenart7897b072019-11-13 10:00:06 +01002852static int macb_get_link_ksettings(struct net_device *netdev,
2853 struct ethtool_link_ksettings *kset)
2854{
2855 struct macb *bp = netdev_priv(netdev);
2856
2857 return phylink_ethtool_ksettings_get(bp->phylink, kset);
2858}
2859
2860static int macb_set_link_ksettings(struct net_device *netdev,
2861 const struct ethtool_link_ksettings *kset)
2862{
2863 struct macb *bp = netdev_priv(netdev);
2864
2865 return phylink_ethtool_ksettings_set(bp->phylink, kset);
2866}
2867
Zach Brown8441bb32016-10-19 09:56:58 -05002868static void macb_get_ringparam(struct net_device *netdev,
2869 struct ethtool_ringparam *ring)
2870{
2871 struct macb *bp = netdev_priv(netdev);
2872
2873 ring->rx_max_pending = MAX_RX_RING_SIZE;
2874 ring->tx_max_pending = MAX_TX_RING_SIZE;
2875
2876 ring->rx_pending = bp->rx_ring_size;
2877 ring->tx_pending = bp->tx_ring_size;
2878}
2879
2880static int macb_set_ringparam(struct net_device *netdev,
2881 struct ethtool_ringparam *ring)
2882{
2883 struct macb *bp = netdev_priv(netdev);
2884 u32 new_rx_size, new_tx_size;
2885 unsigned int reset = 0;
2886
2887 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2888 return -EINVAL;
2889
2890 new_rx_size = clamp_t(u32, ring->rx_pending,
2891 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2892 new_rx_size = roundup_pow_of_two(new_rx_size);
2893
2894 new_tx_size = clamp_t(u32, ring->tx_pending,
2895 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2896 new_tx_size = roundup_pow_of_two(new_tx_size);
2897
2898 if ((new_tx_size == bp->tx_ring_size) &&
2899 (new_rx_size == bp->rx_ring_size)) {
2900 /* nothing to do */
2901 return 0;
2902 }
2903
2904 if (netif_running(bp->dev)) {
2905 reset = 1;
2906 macb_close(bp->dev);
2907 }
2908
2909 bp->rx_ring_size = new_rx_size;
2910 bp->tx_ring_size = new_tx_size;
2911
2912 if (reset)
2913 macb_open(bp->dev);
2914
2915 return 0;
2916}
2917
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01002918#ifdef CONFIG_MACB_USE_HWSTAMP
2919static unsigned int gem_get_tsu_rate(struct macb *bp)
2920{
2921 struct clk *tsu_clk;
2922 unsigned int tsu_rate;
2923
2924 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2925 if (!IS_ERR(tsu_clk))
2926 tsu_rate = clk_get_rate(tsu_clk);
2927 /* try pclk instead */
2928 else if (!IS_ERR(bp->pclk)) {
2929 tsu_clk = bp->pclk;
2930 tsu_rate = clk_get_rate(tsu_clk);
2931 } else
2932 return -ENOTSUPP;
2933 return tsu_rate;
2934}
2935
2936static s32 gem_get_ptp_max_adj(void)
2937{
2938 return 64000000;
2939}
2940
2941static int gem_get_ts_info(struct net_device *dev,
2942 struct ethtool_ts_info *info)
2943{
2944 struct macb *bp = netdev_priv(dev);
2945
2946 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2947 ethtool_op_get_ts_info(dev, info);
2948 return 0;
2949 }
2950
2951 info->so_timestamping =
2952 SOF_TIMESTAMPING_TX_SOFTWARE |
2953 SOF_TIMESTAMPING_RX_SOFTWARE |
2954 SOF_TIMESTAMPING_SOFTWARE |
2955 SOF_TIMESTAMPING_TX_HARDWARE |
2956 SOF_TIMESTAMPING_RX_HARDWARE |
2957 SOF_TIMESTAMPING_RAW_HARDWARE;
2958 info->tx_types =
2959 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2960 (1 << HWTSTAMP_TX_OFF) |
2961 (1 << HWTSTAMP_TX_ON);
2962 info->rx_filters =
2963 (1 << HWTSTAMP_FILTER_NONE) |
2964 (1 << HWTSTAMP_FILTER_ALL);
2965
2966 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2967
2968 return 0;
2969}
2970
2971static struct macb_ptp_info gem_ptp_info = {
2972 .ptp_init = gem_ptp_init,
2973 .ptp_remove = gem_ptp_remove,
2974 .get_ptp_max_adj = gem_get_ptp_max_adj,
2975 .get_tsu_rate = gem_get_tsu_rate,
2976 .get_ts_info = gem_get_ts_info,
2977 .get_hwtst = gem_get_hwtst,
2978 .set_hwtst = gem_set_hwtst,
2979};
2980#endif
2981
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002982static int macb_get_ts_info(struct net_device *netdev,
2983 struct ethtool_ts_info *info)
2984{
2985 struct macb *bp = netdev_priv(netdev);
2986
2987 if (bp->ptp_info)
2988 return bp->ptp_info->get_ts_info(netdev, info);
2989
2990 return ethtool_op_get_ts_info(netdev, info);
2991}
2992
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002993static void gem_enable_flow_filters(struct macb *bp, bool enable)
2994{
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00002995 struct net_device *netdev = bp->dev;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00002996 struct ethtool_rx_fs_item *item;
2997 u32 t2_scr;
2998 int num_t2_scr;
2999
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003000 if (!(netdev->features & NETIF_F_NTUPLE))
3001 return;
3002
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003003 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
3004
3005 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3006 struct ethtool_rx_flow_spec *fs = &item->fs;
3007 struct ethtool_tcpip4_spec *tp4sp_m;
3008
3009 if (fs->location >= num_t2_scr)
3010 continue;
3011
3012 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
3013
3014 /* enable/disable screener regs for the flow entry */
3015 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
3016
3017 /* only enable fields with no masking */
3018 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3019
3020 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
3021 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
3022 else
3023 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
3024
3025 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
3026 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
3027 else
3028 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
3029
3030 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
3031 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
3032 else
3033 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
3034
3035 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
3036 }
3037}
3038
3039static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
3040{
3041 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
3042 uint16_t index = fs->location;
3043 u32 w0, w1, t2_scr;
3044 bool cmp_a = false;
3045 bool cmp_b = false;
3046 bool cmp_c = false;
3047
3048 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
3049 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
3050
3051 /* ignore field if any masking set */
3052 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
3053 /* 1st compare reg - IP source address */
3054 w0 = 0;
3055 w1 = 0;
3056 w0 = tp4sp_v->ip4src;
3057 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3058 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3059 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
3060 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
3061 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
3062 cmp_a = true;
3063 }
3064
3065 /* ignore field if any masking set */
3066 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
3067 /* 2nd compare reg - IP destination address */
3068 w0 = 0;
3069 w1 = 0;
3070 w0 = tp4sp_v->ip4dst;
3071 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3072 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
3073 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
3074 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
3075 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
3076 cmp_b = true;
3077 }
3078
3079 /* ignore both port fields if masking set in both */
3080 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
3081 /* 3rd compare reg - source port, destination port */
3082 w0 = 0;
3083 w1 = 0;
3084 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
3085 if (tp4sp_m->psrc == tp4sp_m->pdst) {
3086 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
3087 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3088 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
3089 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3090 } else {
3091 /* only one port definition */
3092 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
3093 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
3094 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
3095 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3096 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3097 } else { /* dst port */
3098 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3099 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3100 }
3101 }
3102 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3103 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3104 cmp_c = true;
3105 }
3106
3107 t2_scr = 0;
3108 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3109 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3110 if (cmp_a)
3111 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3112 if (cmp_b)
3113 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3114 if (cmp_c)
3115 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3116 gem_writel_n(bp, SCRT2, index, t2_scr);
3117}
3118
3119static int gem_add_flow_filter(struct net_device *netdev,
3120 struct ethtool_rxnfc *cmd)
3121{
3122 struct macb *bp = netdev_priv(netdev);
3123 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3124 struct ethtool_rx_fs_item *item, *newfs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003125 unsigned long flags;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003126 int ret = -EINVAL;
3127 bool added = false;
3128
Julia Cartwrightcc1674e2017-12-05 18:02:50 -06003129 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003130 if (newfs == NULL)
3131 return -ENOMEM;
3132 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3133
3134 netdev_dbg(netdev,
3135 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3136 fs->flow_type, (int)fs->ring_cookie, fs->location,
3137 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3138 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3139 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3140
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003141 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3142
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003143 /* find correct place to add in list */
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003144 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3145 if (item->fs.location > newfs->fs.location) {
3146 list_add_tail(&newfs->list, &item->list);
3147 added = true;
3148 break;
3149 } else if (item->fs.location == fs->location) {
3150 netdev_err(netdev, "Rule not added: location %d not free!\n",
3151 fs->location);
3152 ret = -EBUSY;
3153 goto err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003154 }
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003155 }
Julia Cartwrighta3da8ad2017-12-05 18:02:48 -06003156 if (!added)
3157 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003158
3159 gem_prog_cmp_regs(bp, fs);
3160 bp->rx_fs_list.count++;
3161 /* enable filtering if NTUPLE on */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003162 gem_enable_flow_filters(bp, 1);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003163
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003164 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003165 return 0;
3166
3167err:
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003168 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003169 kfree(newfs);
3170 return ret;
3171}
3172
3173static int gem_del_flow_filter(struct net_device *netdev,
3174 struct ethtool_rxnfc *cmd)
3175{
3176 struct macb *bp = netdev_priv(netdev);
3177 struct ethtool_rx_fs_item *item;
3178 struct ethtool_rx_flow_spec *fs;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003179 unsigned long flags;
3180
3181 spin_lock_irqsave(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003182
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003183 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3184 if (item->fs.location == cmd->fs.location) {
3185 /* disable screener regs for the flow entry */
3186 fs = &(item->fs);
3187 netdev_dbg(netdev,
3188 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3189 fs->flow_type, (int)fs->ring_cookie, fs->location,
3190 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3191 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3192 htons(fs->h_u.tcp_ip4_spec.psrc),
3193 htons(fs->h_u.tcp_ip4_spec.pdst));
3194
3195 gem_writel_n(bp, SCRT2, fs->location, 0);
3196
3197 list_del(&item->list);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003198 bp->rx_fs_list.count--;
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003199 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3200 kfree(item);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003201 return 0;
3202 }
3203 }
3204
Julia Cartwright7038cdb2017-12-05 18:02:49 -06003205 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003206 return -EINVAL;
3207}
3208
3209static int gem_get_flow_entry(struct net_device *netdev,
3210 struct ethtool_rxnfc *cmd)
3211{
3212 struct macb *bp = netdev_priv(netdev);
3213 struct ethtool_rx_fs_item *item;
3214
3215 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3216 if (item->fs.location == cmd->fs.location) {
3217 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3218 return 0;
3219 }
3220 }
3221 return -EINVAL;
3222}
3223
3224static int gem_get_all_flow_entries(struct net_device *netdev,
3225 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3226{
3227 struct macb *bp = netdev_priv(netdev);
3228 struct ethtool_rx_fs_item *item;
3229 uint32_t cnt = 0;
3230
3231 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3232 if (cnt == cmd->rule_cnt)
3233 return -EMSGSIZE;
3234 rule_locs[cnt] = item->fs.location;
3235 cnt++;
3236 }
3237 cmd->data = bp->max_tuples;
3238 cmd->rule_cnt = cnt;
3239
3240 return 0;
3241}
3242
3243static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3244 u32 *rule_locs)
3245{
3246 struct macb *bp = netdev_priv(netdev);
3247 int ret = 0;
3248
3249 switch (cmd->cmd) {
3250 case ETHTOOL_GRXRINGS:
3251 cmd->data = bp->num_queues;
3252 break;
3253 case ETHTOOL_GRXCLSRLCNT:
3254 cmd->rule_cnt = bp->rx_fs_list.count;
3255 break;
3256 case ETHTOOL_GRXCLSRULE:
3257 ret = gem_get_flow_entry(netdev, cmd);
3258 break;
3259 case ETHTOOL_GRXCLSRLALL:
3260 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3261 break;
3262 default:
3263 netdev_err(netdev,
3264 "Command parameter %d is not supported\n", cmd->cmd);
3265 ret = -EOPNOTSUPP;
3266 }
3267
3268 return ret;
3269}
3270
3271static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3272{
3273 struct macb *bp = netdev_priv(netdev);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003274 int ret;
3275
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003276 switch (cmd->cmd) {
3277 case ETHTOOL_SRXCLSRLINS:
3278 if ((cmd->fs.location >= bp->max_tuples)
3279 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3280 ret = -EINVAL;
3281 break;
3282 }
3283 ret = gem_add_flow_filter(netdev, cmd);
3284 break;
3285 case ETHTOOL_SRXCLSRLDEL:
3286 ret = gem_del_flow_filter(netdev, cmd);
3287 break;
3288 default:
3289 netdev_err(netdev,
3290 "Command parameter %d is not supported\n", cmd->cmd);
3291 ret = -EOPNOTSUPP;
3292 }
3293
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003294 return ret;
3295}
3296
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003297static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00003298 .get_regs_len = macb_get_regs_len,
3299 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003300 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00003301 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003302 .get_wol = macb_get_wol,
3303 .set_wol = macb_set_wol,
Antoine Tenart7897b072019-11-13 10:00:06 +01003304 .get_link_ksettings = macb_get_link_ksettings,
3305 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003306 .get_ringparam = macb_get_ringparam,
3307 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06003308};
Xander Huff8cd5a562015-01-15 15:55:20 -06003309
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00003310static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06003311 .get_regs_len = macb_get_regs_len,
3312 .get_regs = macb_get_regs,
3313 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003314 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06003315 .get_ethtool_stats = gem_get_ethtool_stats,
3316 .get_strings = gem_get_ethtool_strings,
3317 .get_sset_count = gem_get_sset_count,
Antoine Tenart7897b072019-11-13 10:00:06 +01003318 .get_link_ksettings = macb_get_link_ksettings,
3319 .set_link_ksettings = macb_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05003320 .get_ringparam = macb_get_ringparam,
3321 .set_ringparam = macb_set_ringparam,
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003322 .get_rxnfc = gem_get_rxnfc,
3323 .set_rxnfc = gem_set_rxnfc,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003324};
3325
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003326static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003327{
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003328 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003329
3330 if (!netif_running(dev))
3331 return -EINVAL;
3332
Antoine Tenart7897b072019-11-13 10:00:06 +01003333 if (bp->ptp_info) {
3334 switch (cmd) {
3335 case SIOCSHWTSTAMP:
3336 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3337 case SIOCGHWTSTAMP:
3338 return bp->ptp_info->get_hwtst(dev, rq);
3339 }
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02003340 }
Antoine Tenart7897b072019-11-13 10:00:06 +01003341
3342 return phylink_mii_ioctl(bp->phylink, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003343}
3344
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003345static inline void macb_set_txcsum_feature(struct macb *bp,
3346 netdev_features_t features)
3347{
3348 u32 val;
3349
3350 if (!macb_is_gem(bp))
3351 return;
3352
3353 val = gem_readl(bp, DMACFG);
3354 if (features & NETIF_F_HW_CSUM)
3355 val |= GEM_BIT(TXCOEN);
3356 else
3357 val &= ~GEM_BIT(TXCOEN);
3358
3359 gem_writel(bp, DMACFG, val);
3360}
3361
3362static inline void macb_set_rxcsum_feature(struct macb *bp,
3363 netdev_features_t features)
3364{
3365 struct net_device *netdev = bp->dev;
3366 u32 val;
3367
3368 if (!macb_is_gem(bp))
3369 return;
3370
3371 val = gem_readl(bp, NCFGR);
3372 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3373 val |= GEM_BIT(RXCOEN);
3374 else
3375 val &= ~GEM_BIT(RXCOEN);
3376
3377 gem_writel(bp, NCFGR, val);
3378}
3379
3380static inline void macb_set_rxflow_feature(struct macb *bp,
3381 netdev_features_t features)
3382{
3383 if (!macb_is_gem(bp))
3384 return;
3385
3386 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3387}
3388
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003389static int macb_set_features(struct net_device *netdev,
3390 netdev_features_t features)
3391{
3392 struct macb *bp = netdev_priv(netdev);
3393 netdev_features_t changed = features ^ netdev->features;
3394
3395 /* TX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003396 if (changed & NETIF_F_HW_CSUM)
3397 macb_set_txcsum_feature(bp, features);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003398
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003399 /* RX checksum offload */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003400 if (changed & NETIF_F_RXCSUM)
3401 macb_set_rxcsum_feature(bp, features);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003402
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003403 /* RX Flow Filters */
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003404 if (changed & NETIF_F_NTUPLE)
3405 macb_set_rxflow_feature(bp, features);
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003406
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003407 return 0;
3408}
3409
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00003410static void macb_restore_features(struct macb *bp)
3411{
3412 struct net_device *netdev = bp->dev;
3413 netdev_features_t features = netdev->features;
3414
3415 /* TX checksum offload */
3416 macb_set_txcsum_feature(bp, features);
3417
3418 /* RX checksum offload */
3419 macb_set_rxcsum_feature(bp, features);
3420
3421 /* RX Flow Filters */
3422 macb_set_rxflow_feature(bp, features);
3423}
3424
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003425static const struct net_device_ops macb_netdev_ops = {
3426 .ndo_open = macb_open,
3427 .ndo_stop = macb_close,
3428 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003429 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003430 .ndo_get_stats = macb_get_stats,
3431 .ndo_do_ioctl = macb_ioctl,
3432 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05303433 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003434 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07003435#ifdef CONFIG_NET_POLL_CONTROLLER
3436 .ndo_poll_controller = macb_poll_controller,
3437#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003438 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003439 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003440};
3441
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003442/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02003443 * and integration options used
3444 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003445static void macb_configure_caps(struct macb *bp,
3446 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02003447{
3448 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02003449
Nicolas Ferref6970502015-03-31 15:02:01 +02003450 if (dt_conf)
3451 bp->caps = dt_conf->caps;
3452
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003453 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02003454 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3455
Nicolas Ferree1755872014-07-24 13:50:58 +02003456 dcfg = gem_readl(bp, DCFG1);
3457 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3458 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3459 dcfg = gem_readl(bp, DCFG2);
3460 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3461 bp->caps |= MACB_CAPS_FIFO_MODE;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003462#ifdef CONFIG_MACB_USE_HWSTAMP
3463 if (gem_has_ptp(bp)) {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003464 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
Antoine Tenart7897b072019-11-13 10:00:06 +01003465 dev_err(&bp->pdev->dev,
3466 "GEM doesn't support hardware ptp.\n");
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003467 else {
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003468 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003469 bp->ptp_info = &gem_ptp_info;
3470 }
Rafal Ozieblo7b429612017-06-29 07:12:51 +01003471 }
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01003472#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02003473 }
3474
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03003475 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02003476}
3477
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003478static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003479 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003480 unsigned int *queue_mask,
3481 unsigned int *num_queues)
3482{
3483 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003484
3485 *queue_mask = 0x1;
3486 *num_queues = 1;
3487
Nicolas Ferreda120112015-03-31 15:02:00 +02003488 /* is it macb or gem ?
3489 *
3490 * We need to read directly from the hardware here because
3491 * we are early in the probe process and don't have the
3492 * MACB_CAPS_MACB_IS_GEM flag positioned
3493 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003494 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003495 return;
3496
3497 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05303498 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3499
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003500 *queue_mask |= 0x1;
3501
3502 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3503 if (*queue_mask & (1 << hw_q))
3504 (*num_queues)++;
3505}
3506
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003507static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303508 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05303509 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003510{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003511 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003512 int err;
3513
Bartosz Folta83a77e92016-12-14 06:39:15 +00003514 pdata = dev_get_platdata(&pdev->dev);
3515 if (pdata) {
3516 *pclk = pdata->pclk;
3517 *hclk = pdata->hclk;
3518 } else {
3519 *pclk = devm_clk_get(&pdev->dev, "pclk");
3520 *hclk = devm_clk_get(&pdev->dev, "hclk");
3521 }
3522
Harini Katakamcd5afa92019-03-20 19:12:22 +05303523 if (IS_ERR_OR_NULL(*pclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003524 err = PTR_ERR(*pclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303525 if (!err)
3526 err = -ENODEV;
3527
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003528 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003529 return err;
3530 }
3531
Harini Katakamcd5afa92019-03-20 19:12:22 +05303532 if (IS_ERR_OR_NULL(*hclk)) {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003533 err = PTR_ERR(*hclk);
Harini Katakamcd5afa92019-03-20 19:12:22 +05303534 if (!err)
3535 err = -ENODEV;
3536
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003537 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003538 return err;
3539 }
3540
Michael Tretterbd310aca2019-10-18 16:11:43 +02003541 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003542 if (IS_ERR(*tx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003543 return PTR_ERR(*tx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003544
Michael Tretterbd310aca2019-10-18 16:11:43 +02003545 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303546 if (IS_ERR(*rx_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003547 return PTR_ERR(*rx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303548
Michael Tretterbd310aca2019-10-18 16:11:43 +02003549 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
Harini Katakamf5473d12019-03-01 16:20:33 +05303550 if (IS_ERR(*tsu_clk))
Michael Tretterbd310aca2019-10-18 16:11:43 +02003551 return PTR_ERR(*tsu_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05303552
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003553 err = clk_prepare_enable(*pclk);
3554 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003555 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003556 return err;
3557 }
3558
3559 err = clk_prepare_enable(*hclk);
3560 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003561 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003562 goto err_disable_pclk;
3563 }
3564
3565 err = clk_prepare_enable(*tx_clk);
3566 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003567 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003568 goto err_disable_hclk;
3569 }
3570
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303571 err = clk_prepare_enable(*rx_clk);
3572 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003573 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303574 goto err_disable_txclk;
3575 }
3576
Harini Katakamf5473d12019-03-01 16:20:33 +05303577 err = clk_prepare_enable(*tsu_clk);
3578 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02003579 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
Harini Katakamf5473d12019-03-01 16:20:33 +05303580 goto err_disable_rxclk;
3581 }
3582
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003583 return 0;
3584
Harini Katakamf5473d12019-03-01 16:20:33 +05303585err_disable_rxclk:
3586 clk_disable_unprepare(*rx_clk);
3587
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303588err_disable_txclk:
3589 clk_disable_unprepare(*tx_clk);
3590
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003591err_disable_hclk:
3592 clk_disable_unprepare(*hclk);
3593
3594err_disable_pclk:
3595 clk_disable_unprepare(*pclk);
3596
3597 return err;
3598}
3599
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003600static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003601{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003602 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003603 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003604 struct macb *bp = netdev_priv(dev);
3605 struct macb_queue *queue;
3606 int err;
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003607 u32 val, reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003608
Zach Brownb410d132016-10-19 09:56:57 -05003609 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3610 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3611
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003612 /* set the queue register mapping once for all: queue0 has a special
3613 * register mapping but we don't want to test the queue index then
3614 * compute the corresponding register offset at run time.
3615 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003616 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003617 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003618 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00003619
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003620 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003621 queue->bp = bp;
Antoine Tenart760a3c12019-06-21 17:28:55 +02003622 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003623 if (hw_q) {
3624 queue->ISR = GEM_ISR(hw_q - 1);
3625 queue->IER = GEM_IER(hw_q - 1);
3626 queue->IDR = GEM_IDR(hw_q - 1);
3627 queue->IMR = GEM_IMR(hw_q - 1);
3628 queue->TBQP = GEM_TBQP(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003629 queue->RBQP = GEM_RBQP(hw_q - 1);
3630 queue->RBQS = GEM_RBQS(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05303631#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003632 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003633 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003634 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3635 }
Harini Katakamfff80192016-08-09 13:15:53 +05303636#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003637 } else {
3638 /* queue0 uses legacy registers */
3639 queue->ISR = MACB_ISR;
3640 queue->IER = MACB_IER;
3641 queue->IDR = MACB_IDR;
3642 queue->IMR = MACB_IMR;
3643 queue->TBQP = MACB_TBQP;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003644 queue->RBQP = MACB_RBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05303645#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003646 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003647 queue->TBQPH = MACB_TBQPH;
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003648 queue->RBQPH = MACB_RBQPH;
3649 }
Harini Katakamfff80192016-08-09 13:15:53 +05303650#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003651 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08003652
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003653 /* get irq: here we use the linux queue index, not the hardware
3654 * queue index. the queue irq definitions in the device tree
3655 * must remove the optional gaps that could exist in the
3656 * hardware queue mask.
3657 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003658 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003659 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01003660 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003661 if (err) {
3662 dev_err(&pdev->dev,
3663 "Unable to request IRQ %d (error %d)\n",
3664 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003665 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003666 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003667
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01003668 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003669 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003670 }
3671
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00003672 dev->netdev_ops = &macb_netdev_ops;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003673
Nicolas Ferre4df95132013-06-04 21:57:12 +00003674 /* setup appropriated routines according to adapter type */
3675 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003676 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003677 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3678 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3679 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3680 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003681 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003682 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003683 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003684 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3685 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3686 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3687 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06003688 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00003689 }
3690
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003691 /* Set features */
3692 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00003693
3694 /* Check LSO capability */
3695 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3696 dev->hw_features |= MACB_NETIF_LSO;
3697
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02003698 /* Checksum offload is only available on gem with packet buffer */
3699 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02003700 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02003701 if (bp->caps & MACB_CAPS_SG_DISABLED)
3702 dev->hw_features &= ~NETIF_F_SG;
3703 dev->features = dev->hw_features;
3704
Rafal Oziebloae8223de2017-11-30 18:20:44 +00003705 /* Check RX Flow Filters support.
3706 * Max Rx flows set by availability of screeners & compare regs:
3707 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3708 */
3709 reg = gem_readl(bp, DCFG8);
3710 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3711 GEM_BFEXT(T2SCR, reg));
3712 if (bp->max_tuples > 0) {
3713 /* also needs one ethtype match to check IPv4 */
3714 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3715 /* program this reg now */
3716 reg = 0;
3717 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3718 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3719 /* Filtering is supported in hw but don't enable it in kernel now */
3720 dev->hw_features |= NETIF_F_NTUPLE;
3721 /* init Rx flow definitions */
3722 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3723 bp->rx_fs_list.count = 0;
3724 spin_lock_init(&bp->rx_fs_lock);
3725 } else
3726 bp->max_tuples = 0;
3727 }
3728
Neil Armstrongce721a72016-01-05 14:39:16 +01003729 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3730 val = 0;
3731 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3732 val = GEM_BIT(RGMII);
3733 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003734 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003735 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003736 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01003737 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003738
Neil Armstrongce721a72016-01-05 14:39:16 +01003739 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3740 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003741
Neil Armstrongce721a72016-01-05 14:39:16 +01003742 macb_or_gem_writel(bp, USRIO, val);
3743 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003744
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003745 /* Set MII management clock divider */
3746 val = macb_mdc_clk_div(bp);
3747 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05303748 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3749 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003750 macb_writel(bp, NCFGR, val);
3751
3752 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003753}
3754
3755#if defined(CONFIG_OF)
3756/* 1518 rounded up */
3757#define AT91ETHER_MAX_RBUFF_SZ 0x600
3758/* max number of receive buffers */
3759#define AT91ETHER_MAX_RX_DESCR 9
3760
Arnd Bergmann49db9222019-07-08 14:48:23 +02003761static struct sifive_fu540_macb_mgmt *mgmt;
3762
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003763/* Initialize and start the Receiver and Transmit subsystems */
3764static int at91ether_start(struct net_device *dev)
3765{
3766 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003767 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003768 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003769 dma_addr_t addr;
3770 u32 ctl;
3771 int i;
3772
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003773 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003774 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003775 macb_dma_desc_get_size(lp)),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003776 &q->rx_ring_dma, GFP_KERNEL);
3777 if (!q->rx_ring)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003778 return -ENOMEM;
3779
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003780 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003781 AT91ETHER_MAX_RX_DESCR *
3782 AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003783 &q->rx_buffers_dma, GFP_KERNEL);
3784 if (!q->rx_buffers) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003785 dma_free_coherent(&lp->pdev->dev,
3786 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003787 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003788 q->rx_ring, q->rx_ring_dma);
3789 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003790 return -ENOMEM;
3791 }
3792
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003793 addr = q->rx_buffers_dma;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003794 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003795 desc = macb_rx_desc(q, i);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003796 macb_set_addr(lp, desc, addr);
3797 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003798 addr += AT91ETHER_MAX_RBUFF_SZ;
3799 }
3800
3801 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003802 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003803
3804 /* Reset buffer index */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003805 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003806
3807 /* Program address of descriptor list in Rx Buffer Queue register */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003808 macb_writel(lp, RBQP, q->rx_ring_dma);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003809
3810 /* Enable Receive and Transmit */
3811 ctl = macb_readl(lp, NCR);
3812 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3813
3814 return 0;
3815}
3816
3817/* Open the ethernet interface */
3818static int at91ether_open(struct net_device *dev)
3819{
3820 struct macb *lp = netdev_priv(dev);
3821 u32 ctl;
3822 int ret;
3823
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01003824 ret = pm_runtime_get_sync(&lp->pdev->dev);
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03003825 if (ret < 0) {
3826 pm_runtime_put_noidle(&lp->pdev->dev);
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01003827 return ret;
Andy Shevchenko0ce205d2020-04-27 13:51:20 +03003828 }
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01003829
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003830 /* Clear internal statistics */
3831 ctl = macb_readl(lp, NCR);
3832 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3833
3834 macb_set_hwaddr(lp);
3835
3836 ret = at91ether_start(dev);
3837 if (ret)
3838 return ret;
3839
3840 /* Enable MAC interrupts */
3841 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3842 MACB_BIT(RXUBR) |
3843 MACB_BIT(ISR_TUND) |
3844 MACB_BIT(ISR_RLE) |
3845 MACB_BIT(TCOMP) |
3846 MACB_BIT(ISR_ROVR) |
3847 MACB_BIT(HRESP));
3848
Antoine Tenart7897b072019-11-13 10:00:06 +01003849 ret = macb_phylink_connect(lp);
3850 if (ret)
3851 return ret;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003852
3853 netif_start_queue(dev);
3854
3855 return 0;
3856}
3857
3858/* Close the interface */
3859static int at91ether_close(struct net_device *dev)
3860{
3861 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003862 struct macb_queue *q = &lp->queues[0];
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003863 u32 ctl;
3864
3865 /* Disable Receiver and Transmitter */
3866 ctl = macb_readl(lp, NCR);
3867 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3868
3869 /* Disable MAC interrupts */
3870 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3871 MACB_BIT(RXUBR) |
3872 MACB_BIT(ISR_TUND) |
3873 MACB_BIT(ISR_RLE) |
3874 MACB_BIT(TCOMP) |
3875 MACB_BIT(ISR_ROVR) |
3876 MACB_BIT(HRESP));
3877
3878 netif_stop_queue(dev);
3879
Antoine Tenart7897b072019-11-13 10:00:06 +01003880 phylink_stop(lp->phylink);
3881 phylink_disconnect_phy(lp->phylink);
3882
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003883 dma_free_coherent(&lp->pdev->dev,
3884 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003885 macb_dma_desc_get_size(lp),
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003886 q->rx_ring, q->rx_ring_dma);
3887 q->rx_ring = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003888
3889 dma_free_coherent(&lp->pdev->dev,
3890 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003891 q->rx_buffers, q->rx_buffers_dma);
3892 q->rx_buffers = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003893
Alexandre Bellonie6a41c22020-02-12 17:45:38 +01003894 return pm_runtime_put(&lp->pdev->dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003895}
3896
3897/* Transmit packet */
Claudiu Beznead1c38952018-08-07 12:25:12 +03003898static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3899 struct net_device *dev)
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003900{
3901 struct macb *lp = netdev_priv(dev);
3902
3903 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3904 netif_stop_queue(dev);
3905
3906 /* Store packet information (to free when Tx completed) */
3907 lp->skb = skb;
3908 lp->skb_length = skb->len;
Christoph Hellwig564923e2019-02-11 14:19:59 +01003909 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3910 skb->len, DMA_TO_DEVICE);
3911 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03003912 dev_kfree_skb_any(skb);
3913 dev->stats.tx_dropped++;
3914 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3915 return NETDEV_TX_OK;
3916 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003917
3918 /* Set address of the data in the Transmit Address register */
3919 macb_writel(lp, TAR, lp->skb_physaddr);
3920 /* Set length of the packet in the Transmit Control register */
3921 macb_writel(lp, TCR, skb->len);
3922
3923 } else {
3924 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3925 return NETDEV_TX_BUSY;
3926 }
3927
3928 return NETDEV_TX_OK;
3929}
3930
3931/* Extract received frame from buffer descriptors and sent to upper layers.
3932 * (Called from interrupt context)
3933 */
3934static void at91ether_rx(struct net_device *dev)
3935{
3936 struct macb *lp = netdev_priv(dev);
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003937 struct macb_queue *q = &lp->queues[0];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003938 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003939 unsigned char *p_recv;
3940 struct sk_buff *skb;
3941 unsigned int pktlen;
3942
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003943 desc = macb_rx_desc(q, q->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003944 while (desc->addr & MACB_BIT(RX_USED)) {
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003945 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003946 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003947 skb = netdev_alloc_skb(dev, pktlen + 2);
3948 if (skb) {
3949 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003950 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003951
3952 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003953 dev->stats.rx_packets++;
3954 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003955 netif_rx(skb);
3956 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003957 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003958 }
3959
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003960 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003961 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003962
3963 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003964 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003965
3966 /* wrap after last buffer */
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003967 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3968 q->rx_tail = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003969 else
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003970 q->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003971
Rafal Oziebloae1f2a52017-11-30 18:19:15 +00003972 desc = macb_rx_desc(q, q->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003973 }
3974}
3975
3976/* MAC interrupt handler */
3977static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3978{
3979 struct net_device *dev = dev_id;
3980 struct macb *lp = netdev_priv(dev);
3981 u32 intstatus, ctl;
3982
3983 /* MAC Interrupt Status register indicates what interrupts are pending.
3984 * It is automatically cleared once read.
3985 */
3986 intstatus = macb_readl(lp, ISR);
3987
3988 /* Receive complete */
3989 if (intstatus & MACB_BIT(RCOMP))
3990 at91ether_rx(dev);
3991
3992 /* Transmit complete */
3993 if (intstatus & MACB_BIT(TCOMP)) {
3994 /* The TCOM bit is set even if the transmission failed */
3995 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003996 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003997
3998 if (lp->skb) {
Yang Weib9560a22019-02-13 00:00:02 +08003999 dev_consume_skb_irq(lp->skb);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004000 lp->skb = NULL;
Christoph Hellwig564923e2019-02-11 14:19:59 +01004001 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004002 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02004003 dev->stats.tx_packets++;
4004 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004005 }
4006 netif_wake_queue(dev);
4007 }
4008
4009 /* Work-around for EMAC Errata section 41.3.1 */
4010 if (intstatus & MACB_BIT(RXUBR)) {
4011 ctl = macb_readl(lp, NCR);
4012 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08004013 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004014 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
4015 }
4016
4017 if (intstatus & MACB_BIT(ISR_ROVR))
4018 netdev_err(dev, "ROVR error\n");
4019
4020 return IRQ_HANDLED;
4021}
4022
4023#ifdef CONFIG_NET_POLL_CONTROLLER
4024static void at91ether_poll_controller(struct net_device *dev)
4025{
4026 unsigned long flags;
4027
4028 local_irq_save(flags);
4029 at91ether_interrupt(dev->irq, dev);
4030 local_irq_restore(flags);
4031}
4032#endif
4033
4034static const struct net_device_ops at91ether_netdev_ops = {
4035 .ndo_open = at91ether_open,
4036 .ndo_stop = at91ether_close,
4037 .ndo_start_xmit = at91ether_start_xmit,
4038 .ndo_get_stats = macb_get_stats,
4039 .ndo_set_rx_mode = macb_set_rx_mode,
4040 .ndo_set_mac_address = eth_mac_addr,
4041 .ndo_do_ioctl = macb_ioctl,
4042 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004043#ifdef CONFIG_NET_POLL_CONTROLLER
4044 .ndo_poll_controller = at91ether_poll_controller,
4045#endif
4046};
4047
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004048static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304049 struct clk **hclk, struct clk **tx_clk,
Harini Katakamf5473d12019-03-01 16:20:33 +05304050 struct clk **rx_clk, struct clk **tsu_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004051{
4052 int err;
4053
4054 *hclk = NULL;
4055 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304056 *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304057 *tsu_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004058
4059 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
4060 if (IS_ERR(*pclk))
4061 return PTR_ERR(*pclk);
4062
4063 err = clk_prepare_enable(*pclk);
4064 if (err) {
Luca Ceresolif413cbb2019-05-14 15:23:07 +02004065 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004066 return err;
4067 }
4068
4069 return 0;
4070}
4071
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004072static int at91ether_init(struct platform_device *pdev)
4073{
4074 struct net_device *dev = platform_get_drvdata(pdev);
4075 struct macb *bp = netdev_priv(dev);
4076 int err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004077
Alexandre Bellonifec9d3b2018-06-26 10:44:01 +02004078 bp->queues[0].bp = bp;
4079
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004080 dev->netdev_ops = &at91ether_netdev_ops;
4081 dev->ethtool_ops = &macb_ethtool_ops;
4082
4083 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
4084 0, dev->name, dev);
4085 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004086 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004087
4088 macb_writel(bp, NCR, 0);
4089
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004090 macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004091
4092 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004093}
4094
Yash Shahc218ad52019-06-18 13:26:08 +05304095static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4096 unsigned long parent_rate)
4097{
4098 return mgmt->rate;
4099}
4100
4101static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4102 unsigned long *parent_rate)
4103{
4104 if (WARN_ON(rate < 2500000))
4105 return 2500000;
4106 else if (rate == 2500000)
4107 return 2500000;
4108 else if (WARN_ON(rate < 13750000))
4109 return 2500000;
4110 else if (WARN_ON(rate < 25000000))
4111 return 25000000;
4112 else if (rate == 25000000)
4113 return 25000000;
4114 else if (WARN_ON(rate < 75000000))
4115 return 25000000;
4116 else if (WARN_ON(rate < 125000000))
4117 return 125000000;
4118 else if (rate == 125000000)
4119 return 125000000;
4120
4121 WARN_ON(rate > 125000000);
4122
4123 return 125000000;
4124}
4125
4126static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4127 unsigned long parent_rate)
4128{
4129 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4130 if (rate != 125000000)
4131 iowrite32(1, mgmt->reg);
4132 else
4133 iowrite32(0, mgmt->reg);
4134 mgmt->rate = rate;
4135
4136 return 0;
4137}
4138
4139static const struct clk_ops fu540_c000_ops = {
4140 .recalc_rate = fu540_macb_tx_recalc_rate,
4141 .round_rate = fu540_macb_tx_round_rate,
4142 .set_rate = fu540_macb_tx_set_rate,
4143};
4144
4145static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4146 struct clk **hclk, struct clk **tx_clk,
4147 struct clk **rx_clk, struct clk **tsu_clk)
4148{
4149 struct clk_init_data init;
4150 int err = 0;
4151
4152 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4153 if (err)
4154 return err;
4155
4156 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
4157 if (!mgmt)
4158 return -ENOMEM;
4159
4160 init.name = "sifive-gemgxl-mgmt";
4161 init.ops = &fu540_c000_ops;
4162 init.flags = 0;
4163 init.num_parents = 0;
4164
4165 mgmt->rate = 0;
4166 mgmt->hw.init = &init;
4167
Stephen Boydd89091a2020-01-03 16:19:21 -08004168 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
Yash Shahc218ad52019-06-18 13:26:08 +05304169 if (IS_ERR(*tx_clk))
4170 return PTR_ERR(*tx_clk);
4171
4172 err = clk_prepare_enable(*tx_clk);
4173 if (err)
4174 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
4175 else
4176 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
4177
4178 return 0;
4179}
4180
4181static int fu540_c000_init(struct platform_device *pdev)
4182{
Dejin Zhengb959c772020-05-03 20:32:26 +08004183 mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
4184 if (IS_ERR(mgmt->reg))
4185 return PTR_ERR(mgmt->reg);
Yash Shahc218ad52019-06-18 13:26:08 +05304186
4187 return macb_init(pdev);
4188}
4189
4190static const struct macb_config fu540_c000_config = {
4191 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4192 MACB_CAPS_GEM_HAS_PTP,
4193 .dma_burst_length = 16,
4194 .clk_init = fu540_c000_clk_init,
4195 .init = fu540_c000_init,
4196 .jumbo_max_len = 10240,
4197};
4198
David S. Miller3cef5c52015-03-09 23:38:02 -04004199static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004200 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004201 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004202 .init = macb_init,
4203};
4204
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004205static const struct macb_config sama5d3macb_config = {
4206 .caps = MACB_CAPS_SG_DISABLED
4207 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4208 .clk_init = macb_clk_init,
4209 .init = macb_init,
4210};
4211
David S. Miller3cef5c52015-03-09 23:38:02 -04004212static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004213 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4214 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004215 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004216 .init = macb_init,
4217};
4218
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004219static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004220 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004221 .dma_burst_length = 16,
4222 .clk_init = macb_clk_init,
4223 .init = macb_init,
4224};
4225
David S. Miller3cef5c52015-03-09 23:38:02 -04004226static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004227 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
vishnuvardhan233a1582017-07-05 17:36:16 +02004228 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004229 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004230 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004231 .init = macb_init,
vishnuvardhan233a1582017-07-05 17:36:16 +02004232 .jumbo_max_len = 10240,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004233};
4234
David S. Miller3cef5c52015-03-09 23:38:02 -04004235static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01004236 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004237 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004238 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004239 .init = macb_init,
4240};
4241
David S. Miller3cef5c52015-03-09 23:38:02 -04004242static const struct macb_config emac_config = {
Alexandre Belloniac2fcfa2020-02-19 15:15:51 +01004243 .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004244 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004245 .init = at91ether_init,
4246};
4247
Neil Armstronge611b5b2016-01-05 14:39:17 +01004248static const struct macb_config np4_config = {
4249 .caps = MACB_CAPS_USRIO_DISABLED,
4250 .clk_init = macb_clk_init,
4251 .init = macb_init,
4252};
David S. Miller36583eb2015-05-23 01:22:35 -04004253
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304254static const struct macb_config zynqmp_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004255 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4256 MACB_CAPS_JUMBO |
Harini Katakam404cd082018-07-06 12:18:58 +05304257 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304258 .dma_burst_length = 16,
4259 .clk_init = macb_clk_init,
4260 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304261 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304262};
4263
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004264static const struct macb_config zynq_config = {
Harini Katakame5010702019-01-29 15:20:03 +05304265 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4266 MACB_CAPS_NEEDS_RSTONUBR,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004267 .dma_burst_length = 16,
4268 .clk_init = macb_clk_init,
4269 .init = macb_init,
4270};
4271
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004272static const struct of_device_id macb_dt_ids[] = {
4273 { .compatible = "cdns,at32ap7000-macb" },
4274 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4275 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01004276 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004277 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4278 { .compatible = "cdns,gem", .data = &pc302gem_config },
Nicolas Ferre3e3e0cd2019-02-06 18:56:10 +01004279 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02004280 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004281 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Nicolas Ferreeb4ed8e2018-09-14 17:48:10 +02004282 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004283 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4284 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4285 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05304286 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05004287 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Yash Shah6342ea82019-08-27 10:36:04 +05304288 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004289 { /* sentinel */ }
4290};
4291MODULE_DEVICE_TABLE(of, macb_dt_ids);
4292#endif /* CONFIG_OF */
4293
Bartosz Folta83a77e92016-12-14 06:39:15 +00004294static const struct macb_config default_gem_config = {
Rafal Oziebloab91f0a2017-06-29 07:14:16 +01004295 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4296 MACB_CAPS_JUMBO |
4297 MACB_CAPS_GEM_HAS_PTP,
Bartosz Folta83a77e92016-12-14 06:39:15 +00004298 .dma_burst_length = 16,
4299 .clk_init = macb_clk_init,
4300 .init = macb_init,
4301 .jumbo_max_len = 10240,
4302};
4303
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004304static int macb_probe(struct platform_device *pdev)
4305{
Bartosz Folta83a77e92016-12-14 06:39:15 +00004306 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004307 int (*clk_init)(struct platform_device *, struct clk **,
Harini Katakamf5473d12019-03-01 16:20:33 +05304308 struct clk **, struct clk **, struct clk **,
4309 struct clk **) = macb_config->clk_init;
Bartosz Folta83a77e92016-12-14 06:39:15 +00004310 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004311 struct device_node *np = pdev->dev.of_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304312 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Harini Katakamf5473d12019-03-01 16:20:33 +05304313 struct clk *tsu_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004314 unsigned int queue_mask, num_queues;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004315 bool native_io;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004316 phy_interface_t interface;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004317 struct net_device *dev;
4318 struct resource *regs;
4319 void __iomem *mem;
4320 const char *mac;
4321 struct macb *bp;
Harini Katakam404cd082018-07-06 12:18:58 +05304322 int err, val;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004323
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004324 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4325 mem = devm_ioremap_resource(&pdev->dev, regs);
4326 if (IS_ERR(mem))
4327 return PTR_ERR(mem);
4328
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004329 if (np) {
4330 const struct of_device_id *match;
4331
4332 match = of_match_node(macb_dt_ids, np);
4333 if (match && match->data) {
4334 macb_config = match->data;
4335 clk_init = macb_config->clk_init;
4336 init = macb_config->init;
4337 }
4338 }
4339
Harini Katakamf5473d12019-03-01 16:20:33 +05304340 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004341 if (err)
4342 return err;
4343
Harini Katakamd54f89a2019-03-01 16:20:34 +05304344 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4345 pm_runtime_use_autosuspend(&pdev->dev);
4346 pm_runtime_get_noresume(&pdev->dev);
4347 pm_runtime_set_active(&pdev->dev);
4348 pm_runtime_enable(&pdev->dev);
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004349 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004350
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004351 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004352 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004353 if (!dev) {
4354 err = -ENOMEM;
4355 goto err_disable_clocks;
4356 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004357
4358 dev->base_addr = regs->start;
4359
4360 SET_NETDEV_DEV(dev, &pdev->dev);
4361
4362 bp = netdev_priv(dev);
4363 bp->pdev = pdev;
4364 bp->dev = dev;
4365 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004366 bp->native_io = native_io;
4367 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07004368 bp->macb_reg_readl = hw_readl_native;
4369 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004370 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07004371 bp->macb_reg_readl = hw_readl;
4372 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03004373 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004374 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02004375 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004376 if (macb_config)
4377 bp->dma_burst_length = macb_config->dma_burst_length;
4378 bp->pclk = pclk;
4379 bp->hclk = hclk;
4380 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304381 bp->rx_clk = rx_clk;
Harini Katakamf5473d12019-03-01 16:20:33 +05304382 bp->tsu_clk = tsu_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03004383 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304384 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05304385
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004386 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02004387 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004388 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4389 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4390
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004391 spin_lock_init(&bp->lock);
4392
Nicolas Ferread783472015-03-31 15:02:02 +02004393 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02004394 macb_configure_caps(bp, macb_config);
4395
Rafal Ozieblo7b429612017-06-29 07:12:51 +01004396#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4397 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4398 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4399 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4400 }
4401#endif
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004402 platform_set_drvdata(pdev, dev);
4403
4404 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004405 if (dev->irq < 0) {
4406 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00004407 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004408 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004409
Jarod Wilson44770e12016-10-17 15:54:17 -04004410 /* MTU range: 68 - 1500 or 10240 */
4411 dev->min_mtu = GEM_MTU_MIN_SIZE;
4412 if (bp->caps & MACB_CAPS_JUMBO)
4413 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4414 else
4415 dev->max_mtu = ETH_DATA_LEN;
4416
Harini Katakam404cd082018-07-06 12:18:58 +05304417 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4418 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4419 if (val)
4420 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4421 macb_dma_desc_get_size(bp);
4422
4423 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4424 if (val)
4425 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4426 macb_dma_desc_get_size(bp);
4427 }
4428
Harini Katakame5010702019-01-29 15:20:03 +05304429 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4430 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4431 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4432
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004433 mac = of_get_mac_address(np);
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004434 if (PTR_ERR(mac) == -EPROBE_DEFER) {
4435 err = -EPROBE_DEFER;
4436 goto err_out_free_netdev;
Antoine Tenart2bf4ecb2019-06-21 17:26:35 +02004437 } else if (!IS_ERR_OR_NULL(mac)) {
Moritz Fischereefb52d2016-03-29 19:11:14 -07004438 ether_addr_copy(bp->dev->dev_addr, mac);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004439 } else {
Petr Å tetiar541ddc62019-05-03 16:27:08 +02004440 macb_get_hwaddr(bp);
Mike Looijmansaa076e32018-03-29 07:29:49 +02004441 }
frederic RODO6c36a702007-07-12 19:07:24 +02004442
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004443 err = of_get_phy_mode(np, &interface);
4444 if (err)
Nicolas Ferre8b952742019-05-03 12:36:58 +02004445 /* not found in DT, MII by default */
4446 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4447 else
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004448 bp->phy_interface = interface;
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004449
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004450 /* IP specific init */
4451 err = init(pdev);
4452 if (err)
4453 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004454
Florian Fainellicf669662016-05-02 18:38:45 -07004455 err = macb_mii_init(bp);
4456 if (err)
4457 goto err_out_free_netdev;
4458
Florian Fainellicf669662016-05-02 18:38:45 -07004459 netif_carrier_off(dev);
4460
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004461 err = register_netdev(dev);
4462 if (err) {
4463 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07004464 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004465 }
4466
Harini Katakam032dc412018-01-27 12:09:01 +05304467 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4468 (unsigned long)bp);
4469
Bo Shen58798232014-09-13 01:57:49 +02004470 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4471 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4472 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004473
Harini Katakamd54f89a2019-03-01 16:20:34 +05304474 pm_runtime_mark_last_busy(&bp->pdev->dev);
4475 pm_runtime_put_autosuspend(&bp->pdev->dev);
4476
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004477 return 0;
4478
Florian Fainellicf669662016-05-02 18:38:45 -07004479err_out_unregister_mdio:
Florian Fainellicf669662016-05-02 18:38:45 -07004480 mdiobus_unregister(bp->mii_bus);
4481 mdiobus_free(bp->mii_bus);
4482
Cyrille Pitchencf250de2014-12-15 15:13:32 +01004483err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004484 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01004485
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004486err_disable_clocks:
4487 clk_disable_unprepare(tx_clk);
4488 clk_disable_unprepare(hclk);
4489 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304490 clk_disable_unprepare(rx_clk);
Harini Katakamf5473d12019-03-01 16:20:33 +05304491 clk_disable_unprepare(tsu_clk);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304492 pm_runtime_disable(&pdev->dev);
4493 pm_runtime_set_suspended(&pdev->dev);
4494 pm_runtime_dont_use_autosuspend(&pdev->dev);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02004495
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004496 return err;
4497}
4498
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004499static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004500{
4501 struct net_device *dev;
4502 struct macb *bp;
4503
4504 dev = platform_get_drvdata(pdev);
4505
4506 if (dev) {
4507 bp = netdev_priv(dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004508 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07004509 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01004510
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004511 unregister_netdev(dev);
Chuhong Yuan61183b02019-11-28 10:00:21 +08004512 tasklet_kill(&bp->hresp_err_tasklet);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304513 pm_runtime_disable(&pdev->dev);
4514 pm_runtime_dont_use_autosuspend(&pdev->dev);
4515 if (!pm_runtime_suspended(&pdev->dev)) {
4516 clk_disable_unprepare(bp->tx_clk);
4517 clk_disable_unprepare(bp->hclk);
4518 clk_disable_unprepare(bp->pclk);
4519 clk_disable_unprepare(bp->rx_clk);
4520 clk_disable_unprepare(bp->tsu_clk);
4521 pm_runtime_set_suspended(&pdev->dev);
4522 }
Antoine Tenart7897b072019-11-13 10:00:06 +01004523 phylink_destroy(bp->phylink);
Cyrille Pitchene965be72014-12-15 15:13:31 +01004524 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004525 }
4526
4527 return 0;
4528}
4529
Michal Simekd23823d2015-01-23 09:36:03 +01004530static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004531{
Wolfram Sangce886a42018-10-21 22:00:14 +02004532 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004533 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304534 struct macb_queue *queue = bp->queues;
4535 unsigned long flags;
4536 unsigned int q;
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004537
Harini Katakamde991c52019-03-01 16:20:35 +05304538 if (!netif_running(netdev))
4539 return 0;
4540
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004541 if (bp->wol & MACB_WOL_ENABLED) {
4542 macb_writel(bp, IER, MACB_BIT(WOL));
4543 macb_writel(bp, WOL, MACB_BIT(MAG));
4544 enable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304545 netif_device_detach(netdev);
4546 } else {
4547 netif_device_detach(netdev);
4548 for (q = 0, queue = bp->queues; q < bp->num_queues;
4549 ++q, ++queue)
4550 napi_disable(&queue->napi);
Antoine Tenart7897b072019-11-13 10:00:06 +01004551 rtnl_lock();
4552 phylink_stop(bp->phylink);
4553 rtnl_unlock();
Harini Katakamde991c52019-03-01 16:20:35 +05304554 spin_lock_irqsave(&bp->lock, flags);
4555 macb_reset_hw(bp);
4556 spin_unlock_irqrestore(&bp->lock, flags);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004557
4558 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4559 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4560
4561 if (netdev->hw_features & NETIF_F_NTUPLE)
4562 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304563 }
4564
Harini Katakamde991c52019-03-01 16:20:35 +05304565 netif_carrier_off(netdev);
4566 if (bp->ptp_info)
4567 bp->ptp_info->ptp_remove(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304568 pm_runtime_force_suspend(dev);
4569
4570 return 0;
4571}
4572
4573static int __maybe_unused macb_resume(struct device *dev)
4574{
4575 struct net_device *netdev = dev_get_drvdata(dev);
4576 struct macb *bp = netdev_priv(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304577 struct macb_queue *queue = bp->queues;
4578 unsigned int q;
4579
4580 if (!netif_running(netdev))
4581 return 0;
Harini Katakamd54f89a2019-03-01 16:20:34 +05304582
4583 pm_runtime_force_resume(dev);
4584
4585 if (bp->wol & MACB_WOL_ENABLED) {
4586 macb_writel(bp, IDR, MACB_BIT(WOL));
4587 macb_writel(bp, WOL, 0);
4588 disable_irq_wake(bp->queues[0].irq);
Harini Katakamde991c52019-03-01 16:20:35 +05304589 } else {
4590 macb_writel(bp, NCR, MACB_BIT(MPE));
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004591
4592 if (netdev->hw_features & NETIF_F_NTUPLE)
4593 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4594
4595 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4596 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4597
Harini Katakamde991c52019-03-01 16:20:35 +05304598 for (q = 0, queue = bp->queues; q < bp->num_queues;
4599 ++q, ++queue)
4600 napi_enable(&queue->napi);
Antoine Tenart7897b072019-11-13 10:00:06 +01004601 rtnl_lock();
4602 phylink_start(bp->phylink);
4603 rtnl_unlock();
Harini Katakamd54f89a2019-03-01 16:20:34 +05304604 }
4605
Harini Katakamde991c52019-03-01 16:20:35 +05304606 macb_init_hw(bp);
4607 macb_set_rx_mode(netdev);
Claudiu Bezneac1e85c6c2019-05-22 08:24:43 +00004608 macb_restore_features(bp);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304609 netif_device_attach(netdev);
Harini Katakamde991c52019-03-01 16:20:35 +05304610 if (bp->ptp_info)
4611 bp->ptp_info->ptp_init(netdev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304612
4613 return 0;
4614}
4615
4616static int __maybe_unused macb_runtime_suspend(struct device *dev)
4617{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004618 struct net_device *netdev = dev_get_drvdata(dev);
Harini Katakamd54f89a2019-03-01 16:20:34 +05304619 struct macb *bp = netdev_priv(netdev);
4620
4621 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004622 clk_disable_unprepare(bp->tx_clk);
4623 clk_disable_unprepare(bp->hclk);
4624 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304625 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004626 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304627 clk_disable_unprepare(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004628
4629 return 0;
4630}
4631
Harini Katakamd54f89a2019-03-01 16:20:34 +05304632static int __maybe_unused macb_runtime_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004633{
Wolfram Sangf9cb7592019-03-19 17:36:34 +01004634 struct net_device *netdev = dev_get_drvdata(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004635 struct macb *bp = netdev_priv(netdev);
4636
Harini Katakamd54f89a2019-03-01 16:20:34 +05304637 if (!(device_may_wakeup(&bp->dev->dev))) {
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004638 clk_prepare_enable(bp->pclk);
4639 clk_prepare_enable(bp->hclk);
4640 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05304641 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02004642 }
Harini Katakamf5473d12019-03-01 16:20:33 +05304643 clk_prepare_enable(bp->tsu_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004644
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004645 return 0;
4646}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01004647
Harini Katakamd54f89a2019-03-01 16:20:34 +05304648static const struct dev_pm_ops macb_pm_ops = {
4649 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4650 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4651};
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004652
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004653static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004654 .probe = macb_probe,
4655 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004656 .driver = {
4657 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01004658 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08004659 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004660 },
4661};
4662
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00004663module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01004664
4665MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00004666MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02004667MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07004668MODULE_ALIAS("platform:macb");