blob: 3ae9d8071ded533c317eb5a39d1900bb99510db3 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000035#include <linux/ip.h>
36#include <linux/udp.h>
37#include <linux/tcp.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038#include "macb.h"
39
Nicolas Ferre1b447912013-06-04 21:57:11 +000040#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000041#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050042
Zach Brownb410d132016-10-19 09:56:57 -050043#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050044#define MIN_RX_RING_SIZE 64
45#define MAX_RX_RING_SIZE 8192
Rafal Ozieblodc97a892017-01-27 15:08:20 +000046#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050047 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010048
Zach Brownb410d132016-10-19 09:56:57 -050049#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050050#define MIN_TX_RING_SIZE 64
51#define MAX_TX_RING_SIZE 4096
Rafal Ozieblodc97a892017-01-27 15:08:20 +000052#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
Zach Brownb410d132016-10-19 09:56:57 -050053 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010054
Nicolas Ferre909a8582012-11-19 06:00:21 +000055/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050056#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010057
58#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
59 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000060#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
61 | MACB_BIT(ISR_RLE) \
62 | MACB_BIT(TXERR))
63#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
64
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000065/* Max length of transmit frame must be a multiple of 8 bytes */
66#define MACB_TX_LEN_ALIGN 8
67#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
68#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020069
Jarod Wilson44770e12016-10-17 15:54:17 -040070#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
Rafal Ozieblo1629dd42016-11-16 10:02:34 +000071#define MACB_NETIF_LSO (NETIF_F_TSO | NETIF_F_UFO)
Harini Katakama5898ea2015-05-06 22:27:18 +053072
Sergio Prado3e2a5e12016-02-09 12:07:16 -020073#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
74#define MACB_WOL_ENABLED (0x1 << 1)
75
Moritz Fischer64ec42f2016-03-29 19:11:12 -070076/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000077 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
78 */
79#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010080
Rafal Ozieblodc97a892017-01-27 15:08:20 +000081/* DMA buffer descriptor might be different size
82 * depends on hardware configuration.
83 */
84static unsigned int macb_dma_desc_get_size(struct macb *bp)
85{
86#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
87 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
88 return sizeof(struct macb_dma_desc) + sizeof(struct macb_dma_desc_64);
89#endif
90 return sizeof(struct macb_dma_desc);
91}
92
93static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int idx)
94{
95#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
96 /* Dma buffer descriptor is 4 words length (instead of 2 words)
97 * for 64b GEM.
98 */
99 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
100 idx <<= 1;
101#endif
102 return idx;
103}
104
105#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
106static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
107{
108 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
109}
110#endif
111
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000112/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -0500113static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000114{
Zach Brownb410d132016-10-19 09:56:57 -0500115 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000116}
117
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100118static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
119 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000120{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000121 index = macb_tx_ring_wrap(queue->bp, index);
122 index = macb_adj_dma_desc_idx(queue->bp, index);
123 return &queue->tx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000124}
125
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100126static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
127 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000128{
Zach Brownb410d132016-10-19 09:56:57 -0500129 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000130}
131
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100132static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000133{
134 dma_addr_t offset;
135
Zach Brownb410d132016-10-19 09:56:57 -0500136 offset = macb_tx_ring_wrap(queue->bp, index) *
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000137 macb_dma_desc_get_size(queue->bp);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000138
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100139 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000140}
141
Zach Brownb410d132016-10-19 09:56:57 -0500142static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000143{
Zach Brownb410d132016-10-19 09:56:57 -0500144 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000145}
146
147static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
148{
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000149 index = macb_rx_ring_wrap(bp, index);
150 index = macb_adj_dma_desc_idx(bp, index);
151 return &bp->rx_ring[index];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000152}
153
154static void *macb_rx_buffer(struct macb *bp, unsigned int index)
155{
Zach Brownb410d132016-10-19 09:56:57 -0500156 return bp->rx_buffers + bp->rx_buffer_size *
157 macb_rx_ring_wrap(bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000158}
159
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300160/* I/O accessors */
161static u32 hw_readl_native(struct macb *bp, int offset)
162{
163 return __raw_readl(bp->regs + offset);
164}
165
166static void hw_writel_native(struct macb *bp, int offset, u32 value)
167{
168 __raw_writel(value, bp->regs + offset);
169}
170
171static u32 hw_readl(struct macb *bp, int offset)
172{
173 return readl_relaxed(bp->regs + offset);
174}
175
176static void hw_writel(struct macb *bp, int offset, u32 value)
177{
178 writel_relaxed(value, bp->regs + offset);
179}
180
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700181/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700182 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300183 * descriptor access.
184 */
185static bool hw_is_native_io(void __iomem *addr)
186{
187 u32 value = MACB_BIT(LLB);
188
189 __raw_writel(value, addr + MACB_NCR);
190 value = __raw_readl(addr + MACB_NCR);
191
192 /* Write 0 back to disable everything */
193 __raw_writel(0, addr + MACB_NCR);
194
195 return value == MACB_BIT(LLB);
196}
197
198static bool hw_is_gem(void __iomem *addr, bool native_io)
199{
200 u32 id;
201
202 if (native_io)
203 id = __raw_readl(addr + MACB_MID);
204 else
205 id = readl_relaxed(addr + MACB_MID);
206
207 return MACB_BFEXT(IDNUM, id) >= 0x2;
208}
209
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100210static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100211{
212 u32 bottom;
213 u16 top;
214
215 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000216 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100217 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000218 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000219
220 /* Clear unused address register sets */
221 macb_or_gem_writel(bp, SA2B, 0);
222 macb_or_gem_writel(bp, SA2T, 0);
223 macb_or_gem_writel(bp, SA3B, 0);
224 macb_or_gem_writel(bp, SA3T, 0);
225 macb_or_gem_writel(bp, SA4B, 0);
226 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100227}
228
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100229static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100230{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000231 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100232 u32 bottom;
233 u16 top;
234 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000235 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100236
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900237 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000238
Moritz Fischeraa50b552016-03-29 19:11:13 -0700239 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000240 for (i = 0; i < 4; i++) {
241 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
242 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100243
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000244 if (pdata && pdata->rev_eth_addr) {
245 addr[5] = bottom & 0xff;
246 addr[4] = (bottom >> 8) & 0xff;
247 addr[3] = (bottom >> 16) & 0xff;
248 addr[2] = (bottom >> 24) & 0xff;
249 addr[1] = top & 0xff;
250 addr[0] = (top & 0xff00) >> 8;
251 } else {
252 addr[0] = bottom & 0xff;
253 addr[1] = (bottom >> 8) & 0xff;
254 addr[2] = (bottom >> 16) & 0xff;
255 addr[3] = (bottom >> 24) & 0xff;
256 addr[4] = top & 0xff;
257 addr[5] = (top >> 8) & 0xff;
258 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100259
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000260 if (is_valid_ether_addr(addr)) {
261 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
262 return;
263 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700264 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000265
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300266 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000267 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100268}
269
frederic RODO6c36a702007-07-12 19:07:24 +0200270static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100271{
frederic RODO6c36a702007-07-12 19:07:24 +0200272 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100273 int value;
274
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100275 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
276 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200277 | MACB_BF(PHYA, mii_id)
278 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100279 | MACB_BF(CODE, MACB_MAN_CODE)));
280
frederic RODO6c36a702007-07-12 19:07:24 +0200281 /* wait for end of transfer */
282 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
283 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284
285 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100286
287 return value;
288}
289
frederic RODO6c36a702007-07-12 19:07:24 +0200290static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
291 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100292{
frederic RODO6c36a702007-07-12 19:07:24 +0200293 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100294
295 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
296 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200297 | MACB_BF(PHYA, mii_id)
298 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100299 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200300 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100301
frederic RODO6c36a702007-07-12 19:07:24 +0200302 /* wait for end of transfer */
303 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
304 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100305
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100306 return 0;
307}
308
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800309/**
310 * macb_set_tx_clk() - Set a clock to a new frequency
311 * @clk Pointer to the clock to change
312 * @rate New frequency in Hz
313 * @dev Pointer to the struct net_device
314 */
315static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
316{
317 long ferr, rate, rate_rounded;
318
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100319 if (!clk)
320 return;
321
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800322 switch (speed) {
323 case SPEED_10:
324 rate = 2500000;
325 break;
326 case SPEED_100:
327 rate = 25000000;
328 break;
329 case SPEED_1000:
330 rate = 125000000;
331 break;
332 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800333 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800334 }
335
336 rate_rounded = clk_round_rate(clk, rate);
337 if (rate_rounded < 0)
338 return;
339
340 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
341 * is not satisfied.
342 */
343 ferr = abs(rate_rounded - rate);
344 ferr = DIV_ROUND_UP(ferr, rate / 100000);
345 if (ferr > 5)
346 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700347 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800348
349 if (clk_set_rate(clk, rate_rounded))
350 netdev_err(dev, "adjusting tx_clk failed.\n");
351}
352
frederic RODO6c36a702007-07-12 19:07:24 +0200353static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100354{
frederic RODO6c36a702007-07-12 19:07:24 +0200355 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200356 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200357 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200358 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100359
frederic RODO6c36a702007-07-12 19:07:24 +0200360 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100361
frederic RODO6c36a702007-07-12 19:07:24 +0200362 if (phydev->link) {
363 if ((bp->speed != phydev->speed) ||
364 (bp->duplex != phydev->duplex)) {
365 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100366
frederic RODO6c36a702007-07-12 19:07:24 +0200367 reg = macb_readl(bp, NCFGR);
368 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000369 if (macb_is_gem(bp))
370 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200371
372 if (phydev->duplex)
373 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900374 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200375 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200376 if (phydev->speed == SPEED_1000 &&
377 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000378 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200379
Patrice Vilchez140b7552012-10-31 06:04:50 +0000380 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200381
382 bp->speed = phydev->speed;
383 bp->duplex = phydev->duplex;
384 status_change = 1;
385 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100386 }
387
frederic RODO6c36a702007-07-12 19:07:24 +0200388 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700389 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200390 bp->speed = 0;
391 bp->duplex = -1;
392 }
393 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100394
frederic RODO6c36a702007-07-12 19:07:24 +0200395 status_change = 1;
396 }
397
398 spin_unlock_irqrestore(&bp->lock, flags);
399
400 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000401 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500402 /* Update the TX clock rate if and only if the link is
403 * up and there has been a link change.
404 */
405 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
406
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000407 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000408 netdev_info(dev, "link up (%d/%s)\n",
409 phydev->speed,
410 phydev->duplex == DUPLEX_FULL ?
411 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000412 } else {
413 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000414 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000415 }
frederic RODO6c36a702007-07-12 19:07:24 +0200416 }
417}
418
419/* based on au1000_eth. c*/
420static int macb_mii_probe(struct net_device *dev)
421{
422 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000423 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000424 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000425 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000426 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200427
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200428 if (bp->phy_node) {
429 phydev = of_phy_connect(dev, bp->phy_node,
430 &macb_handle_link_change, 0,
431 bp->phy_interface);
432 if (!phydev)
433 return -ENODEV;
434 } else {
435 phydev = phy_find_first(bp->mii_bus);
436 if (!phydev) {
437 netdev_err(dev, "no PHY found\n");
438 return -ENXIO;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000439 }
frederic RODO6c36a702007-07-12 19:07:24 +0200440
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200441 pdata = dev_get_platdata(&bp->pdev->dev);
442 if (pdata) {
443 if (gpio_is_valid(pdata->phy_irq_pin)) {
444 ret = devm_gpio_request(&bp->pdev->dev,
445 pdata->phy_irq_pin, "phy int");
446 if (!ret) {
447 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
448 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
449 }
450 } else {
451 phydev->irq = PHY_POLL;
452 }
453 }
454
455 /* attach the mac to the phy */
456 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
457 bp->phy_interface);
458 if (ret) {
459 netdev_err(dev, "Could not attach to PHY\n");
460 return ret;
461 }
frederic RODO6c36a702007-07-12 19:07:24 +0200462 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100463
frederic RODO6c36a702007-07-12 19:07:24 +0200464 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200465 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000466 phydev->supported &= PHY_GBIT_FEATURES;
467 else
468 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100469
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500470 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
471 phydev->supported &= ~SUPPORTED_1000baseT_Half;
472
frederic RODO6c36a702007-07-12 19:07:24 +0200473 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100474
frederic RODO6c36a702007-07-12 19:07:24 +0200475 bp->link = 0;
476 bp->speed = 0;
477 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200478
479 return 0;
480}
481
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100482static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200483{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000484 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200485 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200486 int err = -ENXIO, i;
487
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200488 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200489 macb_writel(bp, NCR, MACB_BIT(MPE));
490
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700491 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700492 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200493 err = -ENOMEM;
494 goto err_out;
495 }
496
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700497 bp->mii_bus->name = "MACB_mii_bus";
498 bp->mii_bus->read = &macb_mdio_read;
499 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000500 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700501 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700502 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700503 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900504 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700505
Jamie Iles91523942011-02-28 04:05:25 +0000506 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200507
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200508 np = bp->pdev->dev.of_node;
509 if (np) {
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200510 if (of_phy_is_fixed_link(np)) {
511 if (of_phy_register_fixed_link(np) < 0) {
512 dev_err(&bp->pdev->dev,
513 "broken fixed-link specification\n");
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200514 goto err_out_unregister_bus;
Michael Grzeschikdacdbb42017-06-23 16:54:10 +0200515 }
516 bp->phy_node = of_node_get(np);
517
518 err = mdiobus_register(bp->mii_bus);
519 } else {
520 /* try dt phy registration */
521 err = of_mdiobus_register(bp->mii_bus, np);
522
523 /* fallback to standard phy registration if no phy were
524 * found during dt phy registration
525 */
526 if (!err && !phy_find_first(bp->mii_bus)) {
527 for (i = 0; i < PHY_MAX_ADDR; i++) {
528 struct phy_device *phydev;
529
530 phydev = mdiobus_scan(bp->mii_bus, i);
531 if (IS_ERR(phydev) &&
532 PTR_ERR(phydev) != -ENODEV) {
533 err = PTR_ERR(phydev);
534 break;
535 }
536 }
537
538 if (err)
539 goto err_out_unregister_bus;
540 }
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200541 }
542 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000543 for (i = 0; i < PHY_MAX_ADDR; i++)
544 bp->mii_bus->irq[i] = PHY_POLL;
545
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200546 if (pdata)
547 bp->mii_bus->phy_mask = pdata->phy_mask;
548
549 err = mdiobus_register(bp->mii_bus);
550 }
551
552 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100553 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200554
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200555 err = macb_mii_probe(bp->dev);
556 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200557 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200558
559 return 0;
560
561err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700562 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700563err_out_free_mdiobus:
564 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200565err_out:
566 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100567}
568
569static void macb_update_stats(struct macb *bp)
570{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000571 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
572 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300573 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100574
575 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
576
Moritz Fischer96ec6312016-03-29 19:11:11 -0700577 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700578 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100579}
580
Nicolas Ferree86cd532012-10-31 06:04:57 +0000581static int macb_halt_tx(struct macb *bp)
582{
583 unsigned long halt_time, timeout;
584 u32 status;
585
586 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
587
588 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
589 do {
590 halt_time = jiffies;
591 status = macb_readl(bp, TSR);
592 if (!(status & MACB_BIT(TGO)))
593 return 0;
594
595 usleep_range(10, 250);
596 } while (time_before(halt_time, timeout));
597
598 return -ETIMEDOUT;
599}
600
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200601static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
602{
603 if (tx_skb->mapping) {
604 if (tx_skb->mapped_as_page)
605 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
606 tx_skb->size, DMA_TO_DEVICE);
607 else
608 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
609 tx_skb->size, DMA_TO_DEVICE);
610 tx_skb->mapping = 0;
611 }
612
613 if (tx_skb->skb) {
614 dev_kfree_skb_any(tx_skb->skb);
615 tx_skb->skb = NULL;
616 }
617}
618
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000619static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530620{
Harini Katakamfff80192016-08-09 13:15:53 +0530621#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000622 struct macb_dma_desc_64 *desc_64;
623
624 if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
625 desc_64 = macb_64b_desc(bp, desc);
626 desc_64->addrh = upper_32_bits(addr);
627 }
Harini Katakamfff80192016-08-09 13:15:53 +0530628#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000629 desc->addr = lower_32_bits(addr);
630}
631
632static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
633{
634 dma_addr_t addr = 0;
635#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
636 struct macb_dma_desc_64 *desc_64;
637
638 if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
639 desc_64 = macb_64b_desc(bp, desc);
640 addr = ((u64)(desc_64->addrh) << 32);
641 }
642#endif
643 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
644 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530645}
646
Nicolas Ferree86cd532012-10-31 06:04:57 +0000647static void macb_tx_error_task(struct work_struct *work)
648{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100649 struct macb_queue *queue = container_of(work, struct macb_queue,
650 tx_error_task);
651 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000652 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100653 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000654 struct sk_buff *skb;
655 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100656 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000657
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100658 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
659 (unsigned int)(queue - bp->queues),
660 queue->tx_tail, queue->tx_head);
661
662 /* Prevent the queue IRQ handlers from running: each of them may call
663 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
664 * As explained below, we have to halt the transmission before updating
665 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
666 * network engine about the macb/gem being halted.
667 */
668 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000669
670 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100671 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000672
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700673 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000674 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100675 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000676 */
677 if (macb_halt_tx(bp))
678 /* Just complain for now, reinitializing TX path can be good */
679 netdev_err(bp->dev, "BUG: halt tx timed out\n");
680
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700681 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000682 * Free transmit buffers in upper layer.
683 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100684 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
685 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000686
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100687 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000688 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100689 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000690 skb = tx_skb->skb;
691
692 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200693 /* skb is set for the last buffer of the frame */
694 while (!skb) {
695 macb_tx_unmap(bp, tx_skb);
696 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100697 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200698 skb = tx_skb->skb;
699 }
700
701 /* ctrl still refers to the first buffer descriptor
702 * since it's the only one written back by the hardware
703 */
704 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
705 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500706 macb_tx_ring_wrap(bp, tail),
707 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200708 bp->dev->stats.tx_packets++;
709 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200710 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000711 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700712 /* "Buffers exhausted mid-frame" errors may only happen
713 * if the driver is buggy, so complain loudly about
714 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000715 */
716 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
717 netdev_err(bp->dev,
718 "BUG: TX buffers exhausted mid-frame\n");
719
720 desc->ctrl = ctrl | MACB_BIT(TX_USED);
721 }
722
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200723 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000724 }
725
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100726 /* Set end of TX queue */
727 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000728 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100729 desc->ctrl = MACB_BIT(TX_USED);
730
Nicolas Ferree86cd532012-10-31 06:04:57 +0000731 /* Make descriptor updates visible to hardware */
732 wmb();
733
734 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000735 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530736#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000737 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
738 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530739#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000740 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100741 queue->tx_head = 0;
742 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000743
744 /* Housework before enabling TX IRQ */
745 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100746 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
747
748 /* Now we are ready to start transmission again */
749 netif_tx_start_all_queues(bp->dev);
750 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
751
752 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000753}
754
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100755static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100756{
757 unsigned int tail;
758 unsigned int head;
759 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100760 struct macb *bp = queue->bp;
761 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100762
763 status = macb_readl(bp, TSR);
764 macb_writel(bp, TSR, status);
765
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000766 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100767 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000768
Nicolas Ferree86cd532012-10-31 06:04:57 +0000769 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700770 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100771
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100772 head = queue->tx_head;
773 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000774 struct macb_tx_skb *tx_skb;
775 struct sk_buff *skb;
776 struct macb_dma_desc *desc;
777 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100778
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100779 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100780
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000781 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100782 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000783
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000784 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100785
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200786 /* TX_USED bit is only set by hardware on the very first buffer
787 * descriptor of the transmitted frame.
788 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000789 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100790 break;
791
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200792 /* Process all buffers of the current transmitted frame */
793 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100794 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200795 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000796
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200797 /* First, update TX stats if needed */
798 if (skb) {
799 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500800 macb_tx_ring_wrap(bp, tail),
801 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200802 bp->dev->stats.tx_packets++;
803 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200804 }
805
806 /* Now we can safely release resources */
807 macb_tx_unmap(bp, tx_skb);
808
809 /* skb is set only for the last buffer of the frame.
810 * WARNING: at this point skb has been freed by
811 * macb_tx_unmap().
812 */
813 if (skb)
814 break;
815 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100816 }
817
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100818 queue->tx_tail = tail;
819 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
820 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500821 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100822 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100823}
824
Nicolas Ferre4df95132013-06-04 21:57:12 +0000825static void gem_rx_refill(struct macb *bp)
826{
827 unsigned int entry;
828 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000829 dma_addr_t paddr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000830 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000831
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700832 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500833 bp->rx_ring_size) > 0) {
834 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000835
836 /* Make hw descriptor updates visible to CPU */
837 rmb();
838
Nicolas Ferre4df95132013-06-04 21:57:12 +0000839 bp->rx_prepared_head++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000840 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000841
Moritz Fischeraa50b552016-03-29 19:11:13 -0700842 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000843 /* allocate sk_buff for this free entry in ring */
844 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700845 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000846 netdev_err(bp->dev,
847 "Unable to allocate sk_buff\n");
848 break;
849 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000850
851 /* now fill corresponding descriptor entry */
852 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700853 bp->rx_buffer_size,
854 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800855 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
856 dev_kfree_skb(skb);
857 break;
858 }
859
860 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000861
Zach Brownb410d132016-10-19 09:56:57 -0500862 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000863 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000864 macb_set_addr(bp, desc, paddr);
865 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000866
867 /* properly align Ethernet header */
868 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530869 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000870 desc->addr &= ~MACB_BIT(RX_USED);
871 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000872 }
873 }
874
875 /* Make descriptor updates visible to hardware */
876 wmb();
877
878 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700879 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000880}
881
882/* Mark DMA descriptors from begin up to and not including end as unused */
883static void discard_partial_frame(struct macb *bp, unsigned int begin,
884 unsigned int end)
885{
886 unsigned int frag;
887
888 for (frag = begin; frag != end; frag++) {
889 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700890
Nicolas Ferre4df95132013-06-04 21:57:12 +0000891 desc->addr &= ~MACB_BIT(RX_USED);
892 }
893
894 /* Make descriptor updates visible to hardware */
895 wmb();
896
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700897 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000898 * whatever caused this is updated, so we don't have to record
899 * anything.
900 */
901}
902
903static int gem_rx(struct macb *bp, int budget)
904{
905 unsigned int len;
906 unsigned int entry;
907 struct sk_buff *skb;
908 struct macb_dma_desc *desc;
909 int count = 0;
910
911 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530912 u32 ctrl;
913 dma_addr_t addr;
914 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000915
Zach Brownb410d132016-10-19 09:56:57 -0500916 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000917 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000918
919 /* Make hw descriptor updates visible to CPU */
920 rmb();
921
Harini Katakamfff80192016-08-09 13:15:53 +0530922 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000923 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000924 ctrl = desc->ctrl;
925
Harini Katakamfff80192016-08-09 13:15:53 +0530926 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000927 break;
928
Nicolas Ferre4df95132013-06-04 21:57:12 +0000929 bp->rx_tail++;
930 count++;
931
932 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
933 netdev_err(bp->dev,
934 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200935 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000936 break;
937 }
938 skb = bp->rx_skbuff[entry];
939 if (unlikely(!skb)) {
940 netdev_err(bp->dev,
941 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200942 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000943 break;
944 }
945 /* now everything is ready for receiving packet */
946 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530947 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000948
949 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
950
951 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000952 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800953 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000954
955 skb->protocol = eth_type_trans(skb, bp->dev);
956 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200957 if (bp->dev->features & NETIF_F_RXCSUM &&
958 !(bp->dev->flags & IFF_PROMISC) &&
959 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
960 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000961
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200962 bp->dev->stats.rx_packets++;
963 bp->dev->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000964
965#if defined(DEBUG) && defined(VERBOSE_DEBUG)
966 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
967 skb->len, skb->csum);
968 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100969 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000970 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
971 skb->data, 32, true);
972#endif
973
974 netif_receive_skb(skb);
975 }
976
977 gem_rx_refill(bp);
978
979 return count;
980}
981
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100982static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
983 unsigned int last_frag)
984{
985 unsigned int len;
986 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000987 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100988 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000989 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100990
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000991 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530992 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100993
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000994 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -0500995 macb_rx_ring_wrap(bp, first_frag),
996 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100997
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700998 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000999 * first buffer. Since the header is 14 bytes, this makes the
1000 * payload word-aligned.
1001 *
1002 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1003 * the two padding bytes into the skb so that we avoid hitting
1004 * the slowpath in memcpy(), and pull them off afterwards.
1005 */
1006 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001007 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001008 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001009 for (frag = first_frag; ; frag++) {
1010 desc = macb_rx_desc(bp, frag);
1011 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001012 if (frag == last_frag)
1013 break;
1014 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001015
1016 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001017 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001018
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001019 return 1;
1020 }
1021
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001022 offset = 0;
1023 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001024 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001025 skb_put(skb, len);
1026
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001027 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001028 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001029
1030 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001031 if (unlikely(frag != last_frag)) {
1032 dev_kfree_skb_any(skb);
1033 return -1;
1034 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001035 frag_len = len - offset;
1036 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001037 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001038 macb_rx_buffer(bp, frag),
1039 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001040 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001041 desc = macb_rx_desc(bp, frag);
1042 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001043
1044 if (frag == last_frag)
1045 break;
1046 }
1047
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001048 /* Make descriptor updates visible to hardware */
1049 wmb();
1050
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001051 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001052 skb->protocol = eth_type_trans(skb, bp->dev);
1053
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001054 bp->dev->stats.rx_packets++;
1055 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001056 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001057 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001058 netif_receive_skb(skb);
1059
1060 return 0;
1061}
1062
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001063static inline void macb_init_rx_ring(struct macb *bp)
1064{
1065 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001066 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001067 int i;
1068
1069 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001070 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001071 desc = macb_rx_desc(bp, i);
1072 macb_set_addr(bp, desc, addr);
1073 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001074 addr += bp->rx_buffer_size;
1075 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001076 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchena0b44ee2016-11-28 14:40:55 +01001077 bp->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001078}
1079
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001080static int macb_rx(struct macb *bp, int budget)
1081{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001082 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001084 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001085 int first_frag = -1;
1086
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001087 for (tail = bp->rx_tail; budget > 0; tail++) {
1088 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001089 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001090
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001091 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001092 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001093
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001094 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001095
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001096 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001097 break;
1098
1099 if (ctrl & MACB_BIT(RX_SOF)) {
1100 if (first_frag != -1)
1101 discard_partial_frame(bp, first_frag, tail);
1102 first_frag = tail;
1103 }
1104
1105 if (ctrl & MACB_BIT(RX_EOF)) {
1106 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001107
1108 if (unlikely(first_frag == -1)) {
1109 reset_rx_queue = true;
1110 continue;
1111 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001112
1113 dropped = macb_rx_frame(bp, first_frag, tail);
1114 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001115 if (unlikely(dropped < 0)) {
1116 reset_rx_queue = true;
1117 continue;
1118 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001119 if (!dropped) {
1120 received++;
1121 budget--;
1122 }
1123 }
1124 }
1125
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001126 if (unlikely(reset_rx_queue)) {
1127 unsigned long flags;
1128 u32 ctrl;
1129
1130 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1131
1132 spin_lock_irqsave(&bp->lock, flags);
1133
1134 ctrl = macb_readl(bp, NCR);
1135 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1136
1137 macb_init_rx_ring(bp);
1138 macb_writel(bp, RBQP, bp->rx_ring_dma);
1139
1140 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1141
1142 spin_unlock_irqrestore(&bp->lock, flags);
1143 return received;
1144 }
1145
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001146 if (first_frag != -1)
1147 bp->rx_tail = first_frag;
1148 else
1149 bp->rx_tail = tail;
1150
1151 return received;
1152}
1153
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001154static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001155{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001156 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001157 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001158 u32 status;
1159
1160 status = macb_readl(bp, RSR);
1161 macb_writel(bp, RSR, status);
1162
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001163 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001164
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001165 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001166 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001167
Nicolas Ferre4df95132013-06-04 21:57:12 +00001168 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001169 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001170 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001171
Nicolas Ferre8770e912013-02-12 11:08:48 +01001172 /* Packets received while interrupts were disabled */
1173 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001174 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001175 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1176 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001177 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001178 } else {
1179 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1180 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001181 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001182
1183 /* TODO: Handle errors */
1184
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001185 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001186}
1187
1188static irqreturn_t macb_interrupt(int irq, void *dev_id)
1189{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001190 struct macb_queue *queue = dev_id;
1191 struct macb *bp = queue->bp;
1192 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001193 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001194
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001195 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196
1197 if (unlikely(!status))
1198 return IRQ_NONE;
1199
1200 spin_lock(&bp->lock);
1201
1202 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001203 /* close possible race with dev_close */
1204 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001205 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001206 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1207 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001208 break;
1209 }
1210
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001211 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1212 (unsigned int)(queue - bp->queues),
1213 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001214
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001215 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001216 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001217 * until we have processed the buffers. The
1218 * scheduling call may fail if the poll routine
1219 * is already scheduled, so disable interrupts
1220 * now.
1221 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001222 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001223 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001224 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001225
Ben Hutchings288379f2009-01-19 16:43:59 -08001226 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001227 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001228 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229 }
1230 }
1231
Nicolas Ferree86cd532012-10-31 06:04:57 +00001232 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001233 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1234 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001235
1236 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001237 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001238
Nicolas Ferree86cd532012-10-31 06:04:57 +00001239 break;
1240 }
1241
1242 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001243 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001244
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001245 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001246 * add that if/when we get our hands on a full-blown MII PHY.
1247 */
1248
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001249 /* There is a hardware issue under heavy load where DMA can
1250 * stop, this causes endless "used buffer descriptor read"
1251 * interrupts but it can be cleared by re-enabling RX. See
1252 * the at91 manual, section 41.3.1 or the Zynq manual
1253 * section 16.7.4 for details.
1254 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001255 if (status & MACB_BIT(RXUBR)) {
1256 ctrl = macb_readl(bp, NCR);
1257 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001258 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001259 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1260
1261 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001262 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001263 }
1264
Alexander Steinb19f7f72011-04-13 05:03:24 +00001265 if (status & MACB_BIT(ISR_ROVR)) {
1266 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001267 if (macb_is_gem(bp))
1268 bp->hw_stats.gem.rx_overruns++;
1269 else
1270 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001271
1272 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001273 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001274 }
1275
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001276 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001277 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001278 * netdev_err to a lower-priority context as well
1279 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001280 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001281 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001282
1283 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001284 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001285 }
1286
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001287 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001288 }
1289
1290 spin_unlock(&bp->lock);
1291
1292 return IRQ_HANDLED;
1293}
1294
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001295#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001296/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001297 * to allow network i/o with interrupts disabled.
1298 */
1299static void macb_poll_controller(struct net_device *dev)
1300{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001301 struct macb *bp = netdev_priv(dev);
1302 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001303 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001304 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001305
1306 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001307 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1308 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001309 local_irq_restore(flags);
1310}
1311#endif
1312
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001313static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001314 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001315 struct sk_buff *skb,
1316 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001317{
1318 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001319 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001320 struct macb_tx_skb *tx_skb = NULL;
1321 struct macb_dma_desc *desc;
1322 unsigned int offset, size, count = 0;
1323 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001324 unsigned int eof = 1, mss_mfs = 0;
1325 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1326
1327 /* LSO */
1328 if (skb_shinfo(skb)->gso_size != 0) {
1329 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1330 /* UDP - UFO */
1331 lso_ctrl = MACB_LSO_UFO_ENABLE;
1332 else
1333 /* TCP - TSO */
1334 lso_ctrl = MACB_LSO_TSO_ENABLE;
1335 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001336
1337 /* First, map non-paged data */
1338 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001339
1340 /* first buffer length */
1341 size = hdrlen;
1342
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001343 offset = 0;
1344 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001345 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001346 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001347
1348 mapping = dma_map_single(&bp->pdev->dev,
1349 skb->data + offset,
1350 size, DMA_TO_DEVICE);
1351 if (dma_mapping_error(&bp->pdev->dev, mapping))
1352 goto dma_error;
1353
1354 /* Save info to properly release resources */
1355 tx_skb->skb = NULL;
1356 tx_skb->mapping = mapping;
1357 tx_skb->size = size;
1358 tx_skb->mapped_as_page = false;
1359
1360 len -= size;
1361 offset += size;
1362 count++;
1363 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001364
1365 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001366 }
1367
1368 /* Then, map paged data from fragments */
1369 for (f = 0; f < nr_frags; f++) {
1370 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1371
1372 len = skb_frag_size(frag);
1373 offset = 0;
1374 while (len) {
1375 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001376 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001377 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001378
1379 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1380 offset, size, DMA_TO_DEVICE);
1381 if (dma_mapping_error(&bp->pdev->dev, mapping))
1382 goto dma_error;
1383
1384 /* Save info to properly release resources */
1385 tx_skb->skb = NULL;
1386 tx_skb->mapping = mapping;
1387 tx_skb->size = size;
1388 tx_skb->mapped_as_page = true;
1389
1390 len -= size;
1391 offset += size;
1392 count++;
1393 tx_head++;
1394 }
1395 }
1396
1397 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001398 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001399 netdev_err(bp->dev, "BUG! empty skb!\n");
1400 return 0;
1401 }
1402
1403 /* This is the last buffer of the frame: save socket buffer */
1404 tx_skb->skb = skb;
1405
1406 /* Update TX ring: update buffer descriptors in reverse order
1407 * to avoid race condition
1408 */
1409
1410 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1411 * to set the end of TX queue
1412 */
1413 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001414 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001415 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001416 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001417 desc->ctrl = ctrl;
1418
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001419 if (lso_ctrl) {
1420 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1421 /* include header and FCS in value given to h/w */
1422 mss_mfs = skb_shinfo(skb)->gso_size +
1423 skb_transport_offset(skb) +
1424 ETH_FCS_LEN;
1425 else /* TSO */ {
1426 mss_mfs = skb_shinfo(skb)->gso_size;
1427 /* TCP Sequence Number Source Select
1428 * can be set only for TSO
1429 */
1430 seq_ctrl = 0;
1431 }
1432 }
1433
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001434 do {
1435 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001436 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001437 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001438 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001439
1440 ctrl = (u32)tx_skb->size;
1441 if (eof) {
1442 ctrl |= MACB_BIT(TX_LAST);
1443 eof = 0;
1444 }
Zach Brownb410d132016-10-19 09:56:57 -05001445 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001446 ctrl |= MACB_BIT(TX_WRAP);
1447
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001448 /* First descriptor is header descriptor */
1449 if (i == queue->tx_head) {
1450 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1451 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1452 } else
1453 /* Only set MSS/MFS on payload descriptors
1454 * (second or later descriptor)
1455 */
1456 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1457
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001458 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001459 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001460 /* desc->addr must be visible to hardware before clearing
1461 * 'TX_USED' bit in desc->ctrl.
1462 */
1463 wmb();
1464 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001465 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001466
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001467 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001468
1469 return count;
1470
1471dma_error:
1472 netdev_err(bp->dev, "TX DMA map failed\n");
1473
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001474 for (i = queue->tx_head; i != tx_head; i++) {
1475 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001476
1477 macb_tx_unmap(bp, tx_skb);
1478 }
1479
1480 return 0;
1481}
1482
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001483static netdev_features_t macb_features_check(struct sk_buff *skb,
1484 struct net_device *dev,
1485 netdev_features_t features)
1486{
1487 unsigned int nr_frags, f;
1488 unsigned int hdrlen;
1489
1490 /* Validate LSO compatibility */
1491
1492 /* there is only one buffer */
1493 if (!skb_is_nonlinear(skb))
1494 return features;
1495
1496 /* length of header */
1497 hdrlen = skb_transport_offset(skb);
1498 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1499 hdrlen += tcp_hdrlen(skb);
1500
1501 /* For LSO:
1502 * When software supplies two or more payload buffers all payload buffers
1503 * apart from the last must be a multiple of 8 bytes in size.
1504 */
1505 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1506 return features & ~MACB_NETIF_LSO;
1507
1508 nr_frags = skb_shinfo(skb)->nr_frags;
1509 /* No need to check last fragment */
1510 nr_frags--;
1511 for (f = 0; f < nr_frags; f++) {
1512 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1513
1514 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1515 return features & ~MACB_NETIF_LSO;
1516 }
1517 return features;
1518}
1519
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001520static inline int macb_clear_csum(struct sk_buff *skb)
1521{
1522 /* no change for packets without checksum offloading */
1523 if (skb->ip_summed != CHECKSUM_PARTIAL)
1524 return 0;
1525
1526 /* make sure we can modify the header */
1527 if (unlikely(skb_cow_head(skb, 0)))
1528 return -1;
1529
1530 /* initialize checksum field
1531 * This is required - at least for Zynq, which otherwise calculates
1532 * wrong UDP header checksums for UDP packets with UDP data len <=2
1533 */
1534 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1535 return 0;
1536}
1537
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001538static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1539{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001540 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001541 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001542 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001543 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001544 unsigned int desc_cnt, nr_frags, frag_size, f;
1545 unsigned int hdrlen;
1546 bool is_lso, is_udp = 0;
1547
1548 is_lso = (skb_shinfo(skb)->gso_size != 0);
1549
1550 if (is_lso) {
1551 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1552
1553 /* length of headers */
1554 if (is_udp)
1555 /* only queue eth + ip headers separately for UDP */
1556 hdrlen = skb_transport_offset(skb);
1557 else
1558 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1559 if (skb_headlen(skb) < hdrlen) {
1560 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1561 /* if this is required, would need to copy to single buffer */
1562 return NETDEV_TX_BUSY;
1563 }
1564 } else
1565 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001566
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001567#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1568 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001569 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1570 queue_index, skb->len, skb->head, skb->data,
1571 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001572 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1573 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001574#endif
1575
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001576 /* Count how many TX buffer descriptors are needed to send this
1577 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001578 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001579 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001580 if (is_lso && (skb_headlen(skb) > hdrlen))
1581 /* extra header descriptor if also payload in first buffer */
1582 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1583 else
1584 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001585 nr_frags = skb_shinfo(skb)->nr_frags;
1586 for (f = 0; f < nr_frags; f++) {
1587 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001588 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001589 }
1590
Dongdong Deng48719532009-08-23 19:49:07 -07001591 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001592
1593 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001594 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001595 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001596 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001597 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001598 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001599 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001600 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001601 }
1602
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001603 if (macb_clear_csum(skb)) {
1604 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001605 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001606 }
1607
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001608 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001609 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001610 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001611 goto unlock;
1612 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001613
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001614 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001615 wmb();
1616
Richard Cochrane0720922011-06-19 21:51:28 +00001617 skb_tx_timestamp(skb);
1618
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001619 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1620
Zach Brownb410d132016-10-19 09:56:57 -05001621 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001622 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001623
Soren Brinkmann92030902014-03-04 08:46:39 -08001624unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001625 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001626
Patrick McHardy6ed10652009-06-23 06:03:08 +00001627 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001628}
1629
Nicolas Ferre4df95132013-06-04 21:57:12 +00001630static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001631{
1632 if (!macb_is_gem(bp)) {
1633 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1634 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001635 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001636
Nicolas Ferre1b447912013-06-04 21:57:11 +00001637 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001638 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001639 "RX buffer must be multiple of %d bytes, expanding\n",
1640 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001641 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001642 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001643 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001644 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001645
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001646 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001647 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001648}
1649
Nicolas Ferre4df95132013-06-04 21:57:12 +00001650static void gem_free_rx_buffers(struct macb *bp)
1651{
1652 struct sk_buff *skb;
1653 struct macb_dma_desc *desc;
1654 dma_addr_t addr;
1655 int i;
1656
1657 if (!bp->rx_skbuff)
1658 return;
1659
Zach Brownb410d132016-10-19 09:56:57 -05001660 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001661 skb = bp->rx_skbuff[i];
1662
Moritz Fischeraa50b552016-03-29 19:11:13 -07001663 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001664 continue;
1665
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001666 desc = macb_rx_desc(bp, i);
1667 addr = macb_get_addr(bp, desc);
1668
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001669 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001670 DMA_FROM_DEVICE);
1671 dev_kfree_skb_any(skb);
1672 skb = NULL;
1673 }
1674
1675 kfree(bp->rx_skbuff);
1676 bp->rx_skbuff = NULL;
1677}
1678
1679static void macb_free_rx_buffers(struct macb *bp)
1680{
1681 if (bp->rx_buffers) {
1682 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001683 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001684 bp->rx_buffers, bp->rx_buffers_dma);
1685 bp->rx_buffers = NULL;
1686 }
1687}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001688
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001689static void macb_free_consistent(struct macb *bp)
1690{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001691 struct macb_queue *queue;
1692 unsigned int q;
1693
Nicolas Ferre4df95132013-06-04 21:57:12 +00001694 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001695 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001696 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001697 bp->rx_ring, bp->rx_ring_dma);
1698 bp->rx_ring = NULL;
1699 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001700
1701 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1702 kfree(queue->tx_skb);
1703 queue->tx_skb = NULL;
1704 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001705 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001706 queue->tx_ring, queue->tx_ring_dma);
1707 queue->tx_ring = NULL;
1708 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001709 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001710}
1711
1712static int gem_alloc_rx_buffers(struct macb *bp)
1713{
1714 int size;
1715
Zach Brownb410d132016-10-19 09:56:57 -05001716 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001717 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1718 if (!bp->rx_skbuff)
1719 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001720 else
1721 netdev_dbg(bp->dev,
1722 "Allocated %d RX struct sk_buff entries at %p\n",
1723 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001724 return 0;
1725}
1726
1727static int macb_alloc_rx_buffers(struct macb *bp)
1728{
1729 int size;
1730
Zach Brownb410d132016-10-19 09:56:57 -05001731 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001732 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1733 &bp->rx_buffers_dma, GFP_KERNEL);
1734 if (!bp->rx_buffers)
1735 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001736
1737 netdev_dbg(bp->dev,
1738 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1739 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001740 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001741}
1742
1743static int macb_alloc_consistent(struct macb *bp)
1744{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001745 struct macb_queue *queue;
1746 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001747 int size;
1748
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001749 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001750 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001751 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1752 &queue->tx_ring_dma,
1753 GFP_KERNEL);
1754 if (!queue->tx_ring)
1755 goto out_err;
1756 netdev_dbg(bp->dev,
1757 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1758 q, size, (unsigned long)queue->tx_ring_dma,
1759 queue->tx_ring);
1760
Zach Brownb410d132016-10-19 09:56:57 -05001761 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001762 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1763 if (!queue->tx_skb)
1764 goto out_err;
1765 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001766
Zach Brownb410d132016-10-19 09:56:57 -05001767 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001768 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1769 &bp->rx_ring_dma, GFP_KERNEL);
1770 if (!bp->rx_ring)
1771 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001772 netdev_dbg(bp->dev,
1773 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1774 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001775
Nicolas Ferre4df95132013-06-04 21:57:12 +00001776 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001777 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001778
1779 return 0;
1780
1781out_err:
1782 macb_free_consistent(bp);
1783 return -ENOMEM;
1784}
1785
Nicolas Ferre4df95132013-06-04 21:57:12 +00001786static void gem_init_rings(struct macb *bp)
1787{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001788 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001789 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001790 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001791 int i;
1792
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001793 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001794 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001795 desc = macb_tx_desc(queue, i);
1796 macb_set_addr(bp, desc, 0);
1797 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001798 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001799 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001800 queue->tx_head = 0;
1801 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001802 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001803
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001804 bp->rx_tail = 0;
1805 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001806
1807 gem_rx_refill(bp);
1808}
1809
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001810static void macb_init_rings(struct macb *bp)
1811{
1812 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001813 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001815 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001816
Zach Brownb410d132016-10-19 09:56:57 -05001817 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001818 desc = macb_tx_desc(&bp->queues[0], i);
1819 macb_set_addr(bp, desc, 0);
1820 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001821 }
Ben Shelton21d35152015-04-22 17:28:54 -05001822 bp->queues[0].tx_head = 0;
1823 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001824 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001825}
1826
1827static void macb_reset_hw(struct macb *bp)
1828{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001829 struct macb_queue *queue;
1830 unsigned int q;
1831
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001832 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001833 * more gracefully?)
1834 */
1835 macb_writel(bp, NCR, 0);
1836
1837 /* Clear the stats registers (XXX: Update stats first?) */
1838 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1839
1840 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001841 macb_writel(bp, TSR, -1);
1842 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001843
1844 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001845 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1846 queue_writel(queue, IDR, -1);
1847 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001848 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1849 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001850 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001851}
1852
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001853static u32 gem_mdc_clk_div(struct macb *bp)
1854{
1855 u32 config;
1856 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1857
1858 if (pclk_hz <= 20000000)
1859 config = GEM_BF(CLK, GEM_CLK_DIV8);
1860 else if (pclk_hz <= 40000000)
1861 config = GEM_BF(CLK, GEM_CLK_DIV16);
1862 else if (pclk_hz <= 80000000)
1863 config = GEM_BF(CLK, GEM_CLK_DIV32);
1864 else if (pclk_hz <= 120000000)
1865 config = GEM_BF(CLK, GEM_CLK_DIV48);
1866 else if (pclk_hz <= 160000000)
1867 config = GEM_BF(CLK, GEM_CLK_DIV64);
1868 else
1869 config = GEM_BF(CLK, GEM_CLK_DIV96);
1870
1871 return config;
1872}
1873
1874static u32 macb_mdc_clk_div(struct macb *bp)
1875{
1876 u32 config;
1877 unsigned long pclk_hz;
1878
1879 if (macb_is_gem(bp))
1880 return gem_mdc_clk_div(bp);
1881
1882 pclk_hz = clk_get_rate(bp->pclk);
1883 if (pclk_hz <= 20000000)
1884 config = MACB_BF(CLK, MACB_CLK_DIV8);
1885 else if (pclk_hz <= 40000000)
1886 config = MACB_BF(CLK, MACB_CLK_DIV16);
1887 else if (pclk_hz <= 80000000)
1888 config = MACB_BF(CLK, MACB_CLK_DIV32);
1889 else
1890 config = MACB_BF(CLK, MACB_CLK_DIV64);
1891
1892 return config;
1893}
1894
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001895/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001896 * should program. We find the width from decoding the design configuration
1897 * register to find the maximum supported data bus width.
1898 */
1899static u32 macb_dbw(struct macb *bp)
1900{
1901 if (!macb_is_gem(bp))
1902 return 0;
1903
1904 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1905 case 4:
1906 return GEM_BF(DBW, GEM_DBW128);
1907 case 2:
1908 return GEM_BF(DBW, GEM_DBW64);
1909 case 1:
1910 default:
1911 return GEM_BF(DBW, GEM_DBW32);
1912 }
1913}
1914
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001915/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001916 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001917 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001918 * (if not supported by FIFO, it will fallback to default)
1919 * - set both rx/tx packet buffers to full memory size
1920 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001921 */
1922static void macb_configure_dma(struct macb *bp)
1923{
1924 u32 dmacfg;
1925
1926 if (macb_is_gem(bp)) {
1927 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001928 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001929 if (bp->dma_burst_length)
1930 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001931 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301932 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301933
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001934 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301935 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1936 else
1937 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1938
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001939 if (bp->dev->features & NETIF_F_HW_CSUM)
1940 dmacfg |= GEM_BIT(TXCOEN);
1941 else
1942 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301943
1944#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001945 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1946 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05301947#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001948 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1949 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001950 gem_writel(bp, DMACFG, dmacfg);
1951 }
1952}
1953
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001954static void macb_init_hw(struct macb *bp)
1955{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001956 struct macb_queue *queue;
1957 unsigned int q;
1958
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001959 u32 config;
1960
1961 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001962 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001963
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001964 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301965 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1966 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001967 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001968 config |= MACB_BIT(PAE); /* PAuse Enable */
1969 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001970 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301971 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1972 else
1973 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001974 if (bp->dev->flags & IFF_PROMISC)
1975 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001976 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1977 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001978 if (!(bp->dev->flags & IFF_BROADCAST))
1979 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001980 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001981 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001982 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301983 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001984 bp->speed = SPEED_10;
1985 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301986 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001987 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301988 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001989
Jamie Iles0116da42011-03-14 17:38:30 +00001990 macb_configure_dma(bp);
1991
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992 /* Initialize TX and RX buffers */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001993 macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301994#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001995 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1996 macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301997#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001998 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001999 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302000#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002001 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2002 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05302003#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002004
2005 /* Enable interrupts */
2006 queue_writel(queue, IER,
2007 MACB_RX_INT_FLAGS |
2008 MACB_TX_INT_FLAGS |
2009 MACB_BIT(HRESP));
2010 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002011
2012 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02002013 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002014}
2015
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002016/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002017 * locations in the memory map. The least significant bits are stored
2018 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2019 *
2020 * The unicast hash enable and the multicast hash enable bits in the
2021 * network configuration register enable the reception of hash matched
2022 * frames. The destination address is reduced to a 6 bit index into
2023 * the 64 bit hash register using the following hash function. The
2024 * hash function is an exclusive or of every sixth bit of the
2025 * destination address.
2026 *
2027 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2028 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2029 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2030 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2031 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2032 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2033 *
2034 * da[0] represents the least significant bit of the first byte
2035 * received, that is, the multicast/unicast indicator, and da[47]
2036 * represents the most significant bit of the last byte received. If
2037 * the hash index, hi[n], points to a bit that is set in the hash
2038 * register then the frame will be matched according to whether the
2039 * frame is multicast or unicast. A multicast match will be signalled
2040 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2041 * index points to a bit set in the hash register. A unicast match
2042 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2043 * and the hash index points to a bit set in the hash register. To
2044 * receive all multicast frames, the hash register should be set with
2045 * all ones and the multicast hash enable bit should be set in the
2046 * network configuration register.
2047 */
2048
2049static inline int hash_bit_value(int bitnr, __u8 *addr)
2050{
2051 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2052 return 1;
2053 return 0;
2054}
2055
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002056/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002057static int hash_get_index(__u8 *addr)
2058{
2059 int i, j, bitval;
2060 int hash_index = 0;
2061
2062 for (j = 0; j < 6; j++) {
2063 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002064 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002065
2066 hash_index |= (bitval << j);
2067 }
2068
2069 return hash_index;
2070}
2071
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002072/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002073static void macb_sethashtable(struct net_device *dev)
2074{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002075 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002076 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002077 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002078 struct macb *bp = netdev_priv(dev);
2079
Moritz Fischeraa50b552016-03-29 19:11:13 -07002080 mc_filter[0] = 0;
2081 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002082
Jiri Pirko22bedad32010-04-01 21:22:57 +00002083 netdev_for_each_mc_addr(ha, dev) {
2084 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002085 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2086 }
2087
Jamie Ilesf75ba502011-11-08 10:12:32 +00002088 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2089 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002090}
2091
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002092/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002093static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002094{
2095 unsigned long cfg;
2096 struct macb *bp = netdev_priv(dev);
2097
2098 cfg = macb_readl(bp, NCFGR);
2099
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002100 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002101 /* Enable promiscuous mode */
2102 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002103
2104 /* Disable RX checksum offload */
2105 if (macb_is_gem(bp))
2106 cfg &= ~GEM_BIT(RXCOEN);
2107 } else {
2108 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002109 cfg &= ~MACB_BIT(CAF);
2110
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002111 /* Enable RX checksum offload only if requested */
2112 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2113 cfg |= GEM_BIT(RXCOEN);
2114 }
2115
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002116 if (dev->flags & IFF_ALLMULTI) {
2117 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002118 macb_or_gem_writel(bp, HRB, -1);
2119 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002120 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002121 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002122 /* Enable specific multicasts */
2123 macb_sethashtable(dev);
2124 cfg |= MACB_BIT(NCFGR_MTI);
2125 } else if (dev->flags & (~IFF_ALLMULTI)) {
2126 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002127 macb_or_gem_writel(bp, HRB, 0);
2128 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002129 cfg &= ~MACB_BIT(NCFGR_MTI);
2130 }
2131
2132 macb_writel(bp, NCFGR, cfg);
2133}
2134
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002135static int macb_open(struct net_device *dev)
2136{
2137 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002138 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002139 int err;
2140
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002141 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002142
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002143 /* carrier starts down */
2144 netif_carrier_off(dev);
2145
frederic RODO6c36a702007-07-12 19:07:24 +02002146 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002147 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002148 return -EAGAIN;
2149
Nicolas Ferre1b447912013-06-04 21:57:11 +00002150 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002151 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002152
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002153 err = macb_alloc_consistent(bp);
2154 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002155 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2156 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002157 return err;
2158 }
2159
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002160 napi_enable(&bp->napi);
2161
Nicolas Ferre4df95132013-06-04 21:57:12 +00002162 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002163 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002164
frederic RODO6c36a702007-07-12 19:07:24 +02002165 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002166 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002167
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002168 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002169
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002170 if (bp->ptp_info)
2171 bp->ptp_info->ptp_init(dev);
2172
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002173 return 0;
2174}
2175
2176static int macb_close(struct net_device *dev)
2177{
2178 struct macb *bp = netdev_priv(dev);
2179 unsigned long flags;
2180
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002181 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002182 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002183
Philippe Reynes0a912812016-06-22 00:32:35 +02002184 if (dev->phydev)
2185 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002186
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002187 spin_lock_irqsave(&bp->lock, flags);
2188 macb_reset_hw(bp);
2189 netif_carrier_off(dev);
2190 spin_unlock_irqrestore(&bp->lock, flags);
2191
2192 macb_free_consistent(bp);
2193
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002194 if (bp->ptp_info)
2195 bp->ptp_info->ptp_remove(dev);
2196
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002197 return 0;
2198}
2199
Harini Katakama5898ea2015-05-06 22:27:18 +05302200static int macb_change_mtu(struct net_device *dev, int new_mtu)
2201{
Harini Katakama5898ea2015-05-06 22:27:18 +05302202 if (netif_running(dev))
2203 return -EBUSY;
2204
Harini Katakama5898ea2015-05-06 22:27:18 +05302205 dev->mtu = new_mtu;
2206
2207 return 0;
2208}
2209
Jamie Ilesa494ed82011-03-09 16:26:35 +00002210static void gem_update_stats(struct macb *bp)
2211{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002212 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002213 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002214
Xander Huff3ff13f12015-01-13 16:15:51 -06002215 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2216 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002217 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002218
2219 bp->ethtool_stats[i] += val;
2220 *p += val;
2221
2222 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2223 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002224 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002225 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002226 *(++p) += val;
2227 }
2228 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002229}
2230
2231static struct net_device_stats *gem_get_stats(struct macb *bp)
2232{
2233 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002234 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002235
2236 gem_update_stats(bp);
2237
2238 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2239 hwstat->rx_alignment_errors +
2240 hwstat->rx_resource_errors +
2241 hwstat->rx_overruns +
2242 hwstat->rx_oversize_frames +
2243 hwstat->rx_jabbers +
2244 hwstat->rx_undersized_frames +
2245 hwstat->rx_length_field_frame_errors);
2246 nstat->tx_errors = (hwstat->tx_late_collisions +
2247 hwstat->tx_excessive_collisions +
2248 hwstat->tx_underrun +
2249 hwstat->tx_carrier_sense_errors);
2250 nstat->multicast = hwstat->rx_multicast_frames;
2251 nstat->collisions = (hwstat->tx_single_collision_frames +
2252 hwstat->tx_multiple_collision_frames +
2253 hwstat->tx_excessive_collisions);
2254 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2255 hwstat->rx_jabbers +
2256 hwstat->rx_undersized_frames +
2257 hwstat->rx_length_field_frame_errors);
2258 nstat->rx_over_errors = hwstat->rx_resource_errors;
2259 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2260 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2261 nstat->rx_fifo_errors = hwstat->rx_overruns;
2262 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2263 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2264 nstat->tx_fifo_errors = hwstat->tx_underrun;
2265
2266 return nstat;
2267}
2268
Xander Huff3ff13f12015-01-13 16:15:51 -06002269static void gem_get_ethtool_stats(struct net_device *dev,
2270 struct ethtool_stats *stats, u64 *data)
2271{
2272 struct macb *bp;
2273
2274 bp = netdev_priv(dev);
2275 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002276 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002277}
2278
2279static int gem_get_sset_count(struct net_device *dev, int sset)
2280{
2281 switch (sset) {
2282 case ETH_SS_STATS:
2283 return GEM_STATS_LEN;
2284 default:
2285 return -EOPNOTSUPP;
2286 }
2287}
2288
2289static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2290{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002291 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002292
2293 switch (sset) {
2294 case ETH_SS_STATS:
2295 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2296 memcpy(p, gem_statistics[i].stat_string,
2297 ETH_GSTRING_LEN);
2298 break;
2299 }
2300}
2301
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002302static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002303{
2304 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002305 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002306 struct macb_stats *hwstat = &bp->hw_stats.macb;
2307
2308 if (macb_is_gem(bp))
2309 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002310
frederic RODO6c36a702007-07-12 19:07:24 +02002311 /* read stats from hardware */
2312 macb_update_stats(bp);
2313
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002314 /* Convert HW stats into netdevice stats */
2315 nstat->rx_errors = (hwstat->rx_fcs_errors +
2316 hwstat->rx_align_errors +
2317 hwstat->rx_resource_errors +
2318 hwstat->rx_overruns +
2319 hwstat->rx_oversize_pkts +
2320 hwstat->rx_jabbers +
2321 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002322 hwstat->rx_length_mismatch);
2323 nstat->tx_errors = (hwstat->tx_late_cols +
2324 hwstat->tx_excessive_cols +
2325 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002326 hwstat->tx_carrier_errors +
2327 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002328 nstat->collisions = (hwstat->tx_single_cols +
2329 hwstat->tx_multiple_cols +
2330 hwstat->tx_excessive_cols);
2331 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2332 hwstat->rx_jabbers +
2333 hwstat->rx_undersize_pkts +
2334 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002335 nstat->rx_over_errors = hwstat->rx_resource_errors +
2336 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002337 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2338 nstat->rx_frame_errors = hwstat->rx_align_errors;
2339 nstat->rx_fifo_errors = hwstat->rx_overruns;
2340 /* XXX: What does "missed" mean? */
2341 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2342 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2343 nstat->tx_fifo_errors = hwstat->tx_underruns;
2344 /* Don't know about heartbeat or window errors... */
2345
2346 return nstat;
2347}
2348
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002349static int macb_get_regs_len(struct net_device *netdev)
2350{
2351 return MACB_GREGS_NBR * sizeof(u32);
2352}
2353
2354static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2355 void *p)
2356{
2357 struct macb *bp = netdev_priv(dev);
2358 unsigned int tail, head;
2359 u32 *regs_buff = p;
2360
2361 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2362 | MACB_GREGS_VERSION;
2363
Zach Brownb410d132016-10-19 09:56:57 -05002364 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2365 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002366
2367 regs_buff[0] = macb_readl(bp, NCR);
2368 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2369 regs_buff[2] = macb_readl(bp, NSR);
2370 regs_buff[3] = macb_readl(bp, TSR);
2371 regs_buff[4] = macb_readl(bp, RBQP);
2372 regs_buff[5] = macb_readl(bp, TBQP);
2373 regs_buff[6] = macb_readl(bp, RSR);
2374 regs_buff[7] = macb_readl(bp, IMR);
2375
2376 regs_buff[8] = tail;
2377 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002378 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2379 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002380
Neil Armstrongce721a72016-01-05 14:39:16 +01002381 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2382 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002383 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002384 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002385}
2386
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002387static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2388{
2389 struct macb *bp = netdev_priv(netdev);
2390
2391 wol->supported = 0;
2392 wol->wolopts = 0;
2393
2394 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2395 wol->supported = WAKE_MAGIC;
2396
2397 if (bp->wol & MACB_WOL_ENABLED)
2398 wol->wolopts |= WAKE_MAGIC;
2399 }
2400}
2401
2402static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2403{
2404 struct macb *bp = netdev_priv(netdev);
2405
2406 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2407 (wol->wolopts & ~WAKE_MAGIC))
2408 return -EOPNOTSUPP;
2409
2410 if (wol->wolopts & WAKE_MAGIC)
2411 bp->wol |= MACB_WOL_ENABLED;
2412 else
2413 bp->wol &= ~MACB_WOL_ENABLED;
2414
2415 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2416
2417 return 0;
2418}
2419
Zach Brown8441bb32016-10-19 09:56:58 -05002420static void macb_get_ringparam(struct net_device *netdev,
2421 struct ethtool_ringparam *ring)
2422{
2423 struct macb *bp = netdev_priv(netdev);
2424
2425 ring->rx_max_pending = MAX_RX_RING_SIZE;
2426 ring->tx_max_pending = MAX_TX_RING_SIZE;
2427
2428 ring->rx_pending = bp->rx_ring_size;
2429 ring->tx_pending = bp->tx_ring_size;
2430}
2431
2432static int macb_set_ringparam(struct net_device *netdev,
2433 struct ethtool_ringparam *ring)
2434{
2435 struct macb *bp = netdev_priv(netdev);
2436 u32 new_rx_size, new_tx_size;
2437 unsigned int reset = 0;
2438
2439 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2440 return -EINVAL;
2441
2442 new_rx_size = clamp_t(u32, ring->rx_pending,
2443 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2444 new_rx_size = roundup_pow_of_two(new_rx_size);
2445
2446 new_tx_size = clamp_t(u32, ring->tx_pending,
2447 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2448 new_tx_size = roundup_pow_of_two(new_tx_size);
2449
2450 if ((new_tx_size == bp->tx_ring_size) &&
2451 (new_rx_size == bp->rx_ring_size)) {
2452 /* nothing to do */
2453 return 0;
2454 }
2455
2456 if (netif_running(bp->dev)) {
2457 reset = 1;
2458 macb_close(bp->dev);
2459 }
2460
2461 bp->rx_ring_size = new_rx_size;
2462 bp->tx_ring_size = new_tx_size;
2463
2464 if (reset)
2465 macb_open(bp->dev);
2466
2467 return 0;
2468}
2469
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002470static int macb_get_ts_info(struct net_device *netdev,
2471 struct ethtool_ts_info *info)
2472{
2473 struct macb *bp = netdev_priv(netdev);
2474
2475 if (bp->ptp_info)
2476 return bp->ptp_info->get_ts_info(netdev, info);
2477
2478 return ethtool_op_get_ts_info(netdev, info);
2479}
2480
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002481static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002482 .get_regs_len = macb_get_regs_len,
2483 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002484 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002485 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002486 .get_wol = macb_get_wol,
2487 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002488 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2489 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002490 .get_ringparam = macb_get_ringparam,
2491 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002492};
Xander Huff8cd5a562015-01-15 15:55:20 -06002493
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002494static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002495 .get_regs_len = macb_get_regs_len,
2496 .get_regs = macb_get_regs,
2497 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002498 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002499 .get_ethtool_stats = gem_get_ethtool_stats,
2500 .get_strings = gem_get_ethtool_strings,
2501 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002502 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2503 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002504 .get_ringparam = macb_get_ringparam,
2505 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002506};
2507
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002508static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002509{
Philippe Reynes0a912812016-06-22 00:32:35 +02002510 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002511 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002512
2513 if (!netif_running(dev))
2514 return -EINVAL;
2515
frederic RODO6c36a702007-07-12 19:07:24 +02002516 if (!phydev)
2517 return -ENODEV;
2518
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002519 if (!bp->ptp_info)
2520 return phy_mii_ioctl(phydev, rq, cmd);
2521
2522 switch (cmd) {
2523 case SIOCSHWTSTAMP:
2524 return bp->ptp_info->set_hwtst(dev, rq, cmd);
2525 case SIOCGHWTSTAMP:
2526 return bp->ptp_info->get_hwtst(dev, rq);
2527 default:
2528 return phy_mii_ioctl(phydev, rq, cmd);
2529 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002530}
2531
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002532static int macb_set_features(struct net_device *netdev,
2533 netdev_features_t features)
2534{
2535 struct macb *bp = netdev_priv(netdev);
2536 netdev_features_t changed = features ^ netdev->features;
2537
2538 /* TX checksum offload */
2539 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2540 u32 dmacfg;
2541
2542 dmacfg = gem_readl(bp, DMACFG);
2543 if (features & NETIF_F_HW_CSUM)
2544 dmacfg |= GEM_BIT(TXCOEN);
2545 else
2546 dmacfg &= ~GEM_BIT(TXCOEN);
2547 gem_writel(bp, DMACFG, dmacfg);
2548 }
2549
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002550 /* RX checksum offload */
2551 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2552 u32 netcfg;
2553
2554 netcfg = gem_readl(bp, NCFGR);
2555 if (features & NETIF_F_RXCSUM &&
2556 !(netdev->flags & IFF_PROMISC))
2557 netcfg |= GEM_BIT(RXCOEN);
2558 else
2559 netcfg &= ~GEM_BIT(RXCOEN);
2560 gem_writel(bp, NCFGR, netcfg);
2561 }
2562
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002563 return 0;
2564}
2565
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002566static const struct net_device_ops macb_netdev_ops = {
2567 .ndo_open = macb_open,
2568 .ndo_stop = macb_close,
2569 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002570 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002571 .ndo_get_stats = macb_get_stats,
2572 .ndo_do_ioctl = macb_ioctl,
2573 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302574 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002575 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002576#ifdef CONFIG_NET_POLL_CONTROLLER
2577 .ndo_poll_controller = macb_poll_controller,
2578#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002579 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002580 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002581};
2582
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002583/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002584 * and integration options used
2585 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002586static void macb_configure_caps(struct macb *bp,
2587 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002588{
2589 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002590
Nicolas Ferref6970502015-03-31 15:02:01 +02002591 if (dt_conf)
2592 bp->caps = dt_conf->caps;
2593
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002594 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002595 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2596
Nicolas Ferree1755872014-07-24 13:50:58 +02002597 dcfg = gem_readl(bp, DCFG1);
2598 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2599 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2600 dcfg = gem_readl(bp, DCFG2);
2601 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2602 bp->caps |= MACB_CAPS_FIFO_MODE;
2603 }
2604
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002605 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002606}
2607
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002608static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002609 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002610 unsigned int *queue_mask,
2611 unsigned int *num_queues)
2612{
2613 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002614
2615 *queue_mask = 0x1;
2616 *num_queues = 1;
2617
Nicolas Ferreda120112015-03-31 15:02:00 +02002618 /* is it macb or gem ?
2619 *
2620 * We need to read directly from the hardware here because
2621 * we are early in the probe process and don't have the
2622 * MACB_CAPS_MACB_IS_GEM flag positioned
2623 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002624 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002625 return;
2626
2627 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302628 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2629
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002630 *queue_mask |= 0x1;
2631
2632 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2633 if (*queue_mask & (1 << hw_q))
2634 (*num_queues)++;
2635}
2636
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002637static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302638 struct clk **hclk, struct clk **tx_clk,
2639 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002640{
Bartosz Folta83a77e92016-12-14 06:39:15 +00002641 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002642 int err;
2643
Bartosz Folta83a77e92016-12-14 06:39:15 +00002644 pdata = dev_get_platdata(&pdev->dev);
2645 if (pdata) {
2646 *pclk = pdata->pclk;
2647 *hclk = pdata->hclk;
2648 } else {
2649 *pclk = devm_clk_get(&pdev->dev, "pclk");
2650 *hclk = devm_clk_get(&pdev->dev, "hclk");
2651 }
2652
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002653 if (IS_ERR(*pclk)) {
2654 err = PTR_ERR(*pclk);
2655 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2656 return err;
2657 }
2658
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002659 if (IS_ERR(*hclk)) {
2660 err = PTR_ERR(*hclk);
2661 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2662 return err;
2663 }
2664
2665 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2666 if (IS_ERR(*tx_clk))
2667 *tx_clk = NULL;
2668
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302669 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2670 if (IS_ERR(*rx_clk))
2671 *rx_clk = NULL;
2672
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002673 err = clk_prepare_enable(*pclk);
2674 if (err) {
2675 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2676 return err;
2677 }
2678
2679 err = clk_prepare_enable(*hclk);
2680 if (err) {
2681 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2682 goto err_disable_pclk;
2683 }
2684
2685 err = clk_prepare_enable(*tx_clk);
2686 if (err) {
2687 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2688 goto err_disable_hclk;
2689 }
2690
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302691 err = clk_prepare_enable(*rx_clk);
2692 if (err) {
2693 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2694 goto err_disable_txclk;
2695 }
2696
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002697 return 0;
2698
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302699err_disable_txclk:
2700 clk_disable_unprepare(*tx_clk);
2701
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002702err_disable_hclk:
2703 clk_disable_unprepare(*hclk);
2704
2705err_disable_pclk:
2706 clk_disable_unprepare(*pclk);
2707
2708 return err;
2709}
2710
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002711static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002712{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002713 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002714 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002715 struct macb *bp = netdev_priv(dev);
2716 struct macb_queue *queue;
2717 int err;
2718 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002719
Zach Brownb410d132016-10-19 09:56:57 -05002720 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2721 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2722
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002723 /* set the queue register mapping once for all: queue0 has a special
2724 * register mapping but we don't want to test the queue index then
2725 * compute the corresponding register offset at run time.
2726 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002727 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002728 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002729 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002730
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002731 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002732 queue->bp = bp;
2733 if (hw_q) {
2734 queue->ISR = GEM_ISR(hw_q - 1);
2735 queue->IER = GEM_IER(hw_q - 1);
2736 queue->IDR = GEM_IDR(hw_q - 1);
2737 queue->IMR = GEM_IMR(hw_q - 1);
2738 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302739#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002740 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2741 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302742#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002743 } else {
2744 /* queue0 uses legacy registers */
2745 queue->ISR = MACB_ISR;
2746 queue->IER = MACB_IER;
2747 queue->IDR = MACB_IDR;
2748 queue->IMR = MACB_IMR;
2749 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302750#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002751 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2752 queue->TBQPH = MACB_TBQPH;
Harini Katakamfff80192016-08-09 13:15:53 +05302753#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002754 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002755
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002756 /* get irq: here we use the linux queue index, not the hardware
2757 * queue index. the queue irq definitions in the device tree
2758 * must remove the optional gaps that could exist in the
2759 * hardware queue mask.
2760 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002761 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002762 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002763 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002764 if (err) {
2765 dev_err(&pdev->dev,
2766 "Unable to request IRQ %d (error %d)\n",
2767 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002768 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002769 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002770
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002771 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002772 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002773 }
2774
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002775 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002776 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002777
Nicolas Ferre4df95132013-06-04 21:57:12 +00002778 /* setup appropriated routines according to adapter type */
2779 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002780 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002781 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2782 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2783 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2784 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002785 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002786 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002787 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002788 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2789 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2790 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2791 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002792 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002793 }
2794
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002795 /* Set features */
2796 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002797
2798 /* Check LSO capability */
2799 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
2800 dev->hw_features |= MACB_NETIF_LSO;
2801
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002802 /* Checksum offload is only available on gem with packet buffer */
2803 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002804 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002805 if (bp->caps & MACB_CAPS_SG_DISABLED)
2806 dev->hw_features &= ~NETIF_F_SG;
2807 dev->features = dev->hw_features;
2808
Neil Armstrongce721a72016-01-05 14:39:16 +01002809 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2810 val = 0;
2811 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2812 val = GEM_BIT(RGMII);
2813 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002814 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002815 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002816 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002817 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002818
Neil Armstrongce721a72016-01-05 14:39:16 +01002819 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2820 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002821
Neil Armstrongce721a72016-01-05 14:39:16 +01002822 macb_or_gem_writel(bp, USRIO, val);
2823 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002824
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002825 /* Set MII management clock divider */
2826 val = macb_mdc_clk_div(bp);
2827 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302828 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2829 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002830 macb_writel(bp, NCFGR, val);
2831
2832 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002833}
2834
2835#if defined(CONFIG_OF)
2836/* 1518 rounded up */
2837#define AT91ETHER_MAX_RBUFF_SZ 0x600
2838/* max number of receive buffers */
2839#define AT91ETHER_MAX_RX_DESCR 9
2840
2841/* Initialize and start the Receiver and Transmit subsystems */
2842static int at91ether_start(struct net_device *dev)
2843{
2844 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002845 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002846 dma_addr_t addr;
2847 u32 ctl;
2848 int i;
2849
2850 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2851 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002852 macb_dma_desc_get_size(lp)),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002853 &lp->rx_ring_dma, GFP_KERNEL);
2854 if (!lp->rx_ring)
2855 return -ENOMEM;
2856
2857 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2858 AT91ETHER_MAX_RX_DESCR *
2859 AT91ETHER_MAX_RBUFF_SZ,
2860 &lp->rx_buffers_dma, GFP_KERNEL);
2861 if (!lp->rx_buffers) {
2862 dma_free_coherent(&lp->pdev->dev,
2863 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002864 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002865 lp->rx_ring, lp->rx_ring_dma);
2866 lp->rx_ring = NULL;
2867 return -ENOMEM;
2868 }
2869
2870 addr = lp->rx_buffers_dma;
2871 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002872 desc = macb_rx_desc(lp, i);
2873 macb_set_addr(lp, desc, addr);
2874 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002875 addr += AT91ETHER_MAX_RBUFF_SZ;
2876 }
2877
2878 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002879 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002880
2881 /* Reset buffer index */
2882 lp->rx_tail = 0;
2883
2884 /* Program address of descriptor list in Rx Buffer Queue register */
2885 macb_writel(lp, RBQP, lp->rx_ring_dma);
2886
2887 /* Enable Receive and Transmit */
2888 ctl = macb_readl(lp, NCR);
2889 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2890
2891 return 0;
2892}
2893
2894/* Open the ethernet interface */
2895static int at91ether_open(struct net_device *dev)
2896{
2897 struct macb *lp = netdev_priv(dev);
2898 u32 ctl;
2899 int ret;
2900
2901 /* Clear internal statistics */
2902 ctl = macb_readl(lp, NCR);
2903 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2904
2905 macb_set_hwaddr(lp);
2906
2907 ret = at91ether_start(dev);
2908 if (ret)
2909 return ret;
2910
2911 /* Enable MAC interrupts */
2912 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2913 MACB_BIT(RXUBR) |
2914 MACB_BIT(ISR_TUND) |
2915 MACB_BIT(ISR_RLE) |
2916 MACB_BIT(TCOMP) |
2917 MACB_BIT(ISR_ROVR) |
2918 MACB_BIT(HRESP));
2919
2920 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002921 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002922
2923 netif_start_queue(dev);
2924
2925 return 0;
2926}
2927
2928/* Close the interface */
2929static int at91ether_close(struct net_device *dev)
2930{
2931 struct macb *lp = netdev_priv(dev);
2932 u32 ctl;
2933
2934 /* Disable Receiver and Transmitter */
2935 ctl = macb_readl(lp, NCR);
2936 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2937
2938 /* Disable MAC interrupts */
2939 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2940 MACB_BIT(RXUBR) |
2941 MACB_BIT(ISR_TUND) |
2942 MACB_BIT(ISR_RLE) |
2943 MACB_BIT(TCOMP) |
2944 MACB_BIT(ISR_ROVR) |
2945 MACB_BIT(HRESP));
2946
2947 netif_stop_queue(dev);
2948
2949 dma_free_coherent(&lp->pdev->dev,
2950 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002951 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002952 lp->rx_ring, lp->rx_ring_dma);
2953 lp->rx_ring = NULL;
2954
2955 dma_free_coherent(&lp->pdev->dev,
2956 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2957 lp->rx_buffers, lp->rx_buffers_dma);
2958 lp->rx_buffers = NULL;
2959
2960 return 0;
2961}
2962
2963/* Transmit packet */
2964static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2965{
2966 struct macb *lp = netdev_priv(dev);
2967
2968 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2969 netif_stop_queue(dev);
2970
2971 /* Store packet information (to free when Tx completed) */
2972 lp->skb = skb;
2973 lp->skb_length = skb->len;
2974 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2975 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03002976 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
2977 dev_kfree_skb_any(skb);
2978 dev->stats.tx_dropped++;
2979 netdev_err(dev, "%s: DMA mapping error\n", __func__);
2980 return NETDEV_TX_OK;
2981 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002982
2983 /* Set address of the data in the Transmit Address register */
2984 macb_writel(lp, TAR, lp->skb_physaddr);
2985 /* Set length of the packet in the Transmit Control register */
2986 macb_writel(lp, TCR, skb->len);
2987
2988 } else {
2989 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2990 return NETDEV_TX_BUSY;
2991 }
2992
2993 return NETDEV_TX_OK;
2994}
2995
2996/* Extract received frame from buffer descriptors and sent to upper layers.
2997 * (Called from interrupt context)
2998 */
2999static void at91ether_rx(struct net_device *dev)
3000{
3001 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003002 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003003 unsigned char *p_recv;
3004 struct sk_buff *skb;
3005 unsigned int pktlen;
3006
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003007 desc = macb_rx_desc(lp, lp->rx_tail);
3008 while (desc->addr & MACB_BIT(RX_USED)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003009 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003010 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003011 skb = netdev_alloc_skb(dev, pktlen + 2);
3012 if (skb) {
3013 skb_reserve(skb, 2);
Johannes Berg59ae1d12017-06-16 14:29:20 +02003014 skb_put_data(skb, p_recv, pktlen);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003015
3016 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003017 dev->stats.rx_packets++;
3018 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003019 netif_rx(skb);
3020 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003021 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003022 }
3023
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003024 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003025 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003026
3027 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003028 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003029
3030 /* wrap after last buffer */
3031 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3032 lp->rx_tail = 0;
3033 else
3034 lp->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003035
3036 desc = macb_rx_desc(lp, lp->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003037 }
3038}
3039
3040/* MAC interrupt handler */
3041static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3042{
3043 struct net_device *dev = dev_id;
3044 struct macb *lp = netdev_priv(dev);
3045 u32 intstatus, ctl;
3046
3047 /* MAC Interrupt Status register indicates what interrupts are pending.
3048 * It is automatically cleared once read.
3049 */
3050 intstatus = macb_readl(lp, ISR);
3051
3052 /* Receive complete */
3053 if (intstatus & MACB_BIT(RCOMP))
3054 at91ether_rx(dev);
3055
3056 /* Transmit complete */
3057 if (intstatus & MACB_BIT(TCOMP)) {
3058 /* The TCOM bit is set even if the transmission failed */
3059 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003060 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003061
3062 if (lp->skb) {
3063 dev_kfree_skb_irq(lp->skb);
3064 lp->skb = NULL;
3065 dma_unmap_single(NULL, lp->skb_physaddr,
3066 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003067 dev->stats.tx_packets++;
3068 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003069 }
3070 netif_wake_queue(dev);
3071 }
3072
3073 /* Work-around for EMAC Errata section 41.3.1 */
3074 if (intstatus & MACB_BIT(RXUBR)) {
3075 ctl = macb_readl(lp, NCR);
3076 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003077 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003078 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3079 }
3080
3081 if (intstatus & MACB_BIT(ISR_ROVR))
3082 netdev_err(dev, "ROVR error\n");
3083
3084 return IRQ_HANDLED;
3085}
3086
3087#ifdef CONFIG_NET_POLL_CONTROLLER
3088static void at91ether_poll_controller(struct net_device *dev)
3089{
3090 unsigned long flags;
3091
3092 local_irq_save(flags);
3093 at91ether_interrupt(dev->irq, dev);
3094 local_irq_restore(flags);
3095}
3096#endif
3097
3098static const struct net_device_ops at91ether_netdev_ops = {
3099 .ndo_open = at91ether_open,
3100 .ndo_stop = at91ether_close,
3101 .ndo_start_xmit = at91ether_start_xmit,
3102 .ndo_get_stats = macb_get_stats,
3103 .ndo_set_rx_mode = macb_set_rx_mode,
3104 .ndo_set_mac_address = eth_mac_addr,
3105 .ndo_do_ioctl = macb_ioctl,
3106 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003107#ifdef CONFIG_NET_POLL_CONTROLLER
3108 .ndo_poll_controller = at91ether_poll_controller,
3109#endif
3110};
3111
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003112static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303113 struct clk **hclk, struct clk **tx_clk,
3114 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003115{
3116 int err;
3117
3118 *hclk = NULL;
3119 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303120 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003121
3122 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3123 if (IS_ERR(*pclk))
3124 return PTR_ERR(*pclk);
3125
3126 err = clk_prepare_enable(*pclk);
3127 if (err) {
3128 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3129 return err;
3130 }
3131
3132 return 0;
3133}
3134
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003135static int at91ether_init(struct platform_device *pdev)
3136{
3137 struct net_device *dev = platform_get_drvdata(pdev);
3138 struct macb *bp = netdev_priv(dev);
3139 int err;
3140 u32 reg;
3141
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003142 dev->netdev_ops = &at91ether_netdev_ops;
3143 dev->ethtool_ops = &macb_ethtool_ops;
3144
3145 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3146 0, dev->name, dev);
3147 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003148 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003149
3150 macb_writel(bp, NCR, 0);
3151
3152 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3153 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3154 reg |= MACB_BIT(RM9200_RMII);
3155
3156 macb_writel(bp, NCFGR, reg);
3157
3158 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003159}
3160
David S. Miller3cef5c52015-03-09 23:38:02 -04003161static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003162 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003163 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003164 .init = macb_init,
3165};
3166
David S. Miller3cef5c52015-03-09 23:38:02 -04003167static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003168 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3169 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003170 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003171 .init = macb_init,
3172};
3173
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003174static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003175 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003176 .dma_burst_length = 16,
3177 .clk_init = macb_clk_init,
3178 .init = macb_init,
3179};
3180
David S. Miller3cef5c52015-03-09 23:38:02 -04003181static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003182 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3183 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003184 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003185 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003186 .init = macb_init,
3187};
3188
David S. Miller3cef5c52015-03-09 23:38:02 -04003189static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003190 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003191 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003192 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003193 .init = macb_init,
3194};
3195
David S. Miller3cef5c52015-03-09 23:38:02 -04003196static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003197 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003198 .init = at91ether_init,
3199};
3200
Neil Armstronge611b5b2016-01-05 14:39:17 +01003201static const struct macb_config np4_config = {
3202 .caps = MACB_CAPS_USRIO_DISABLED,
3203 .clk_init = macb_clk_init,
3204 .init = macb_init,
3205};
David S. Miller36583eb2015-05-23 01:22:35 -04003206
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303207static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303208 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303209 .dma_burst_length = 16,
3210 .clk_init = macb_clk_init,
3211 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303212 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303213};
3214
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003215static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303216 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003217 .dma_burst_length = 16,
3218 .clk_init = macb_clk_init,
3219 .init = macb_init,
3220};
3221
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003222static const struct of_device_id macb_dt_ids[] = {
3223 { .compatible = "cdns,at32ap7000-macb" },
3224 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3225 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003226 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003227 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3228 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003229 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003230 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3231 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3232 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3233 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303234 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003235 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003236 { /* sentinel */ }
3237};
3238MODULE_DEVICE_TABLE(of, macb_dt_ids);
3239#endif /* CONFIG_OF */
3240
Bartosz Folta83a77e92016-12-14 06:39:15 +00003241static const struct macb_config default_gem_config = {
3242 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
3243 .dma_burst_length = 16,
3244 .clk_init = macb_clk_init,
3245 .init = macb_init,
3246 .jumbo_max_len = 10240,
3247};
3248
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003249static int macb_probe(struct platform_device *pdev)
3250{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003251 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003252 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303253 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003254 = macb_config->clk_init;
3255 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003256 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003257 struct device_node *phy_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303258 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003259 unsigned int queue_mask, num_queues;
3260 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003261 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003262 struct phy_device *phydev;
3263 struct net_device *dev;
3264 struct resource *regs;
3265 void __iomem *mem;
3266 const char *mac;
3267 struct macb *bp;
3268 int err;
3269
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003270 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3271 mem = devm_ioremap_resource(&pdev->dev, regs);
3272 if (IS_ERR(mem))
3273 return PTR_ERR(mem);
3274
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003275 if (np) {
3276 const struct of_device_id *match;
3277
3278 match = of_match_node(macb_dt_ids, np);
3279 if (match && match->data) {
3280 macb_config = match->data;
3281 clk_init = macb_config->clk_init;
3282 init = macb_config->init;
3283 }
3284 }
3285
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303286 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003287 if (err)
3288 return err;
3289
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003290 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003291
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003292 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003293 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003294 if (!dev) {
3295 err = -ENOMEM;
3296 goto err_disable_clocks;
3297 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003298
3299 dev->base_addr = regs->start;
3300
3301 SET_NETDEV_DEV(dev, &pdev->dev);
3302
3303 bp = netdev_priv(dev);
3304 bp->pdev = pdev;
3305 bp->dev = dev;
3306 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003307 bp->native_io = native_io;
3308 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003309 bp->macb_reg_readl = hw_readl_native;
3310 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003311 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003312 bp->macb_reg_readl = hw_readl;
3313 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003314 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003315 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003316 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003317 if (macb_config)
3318 bp->dma_burst_length = macb_config->dma_burst_length;
3319 bp->pclk = pclk;
3320 bp->hclk = hclk;
3321 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303322 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003323 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303324 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303325
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003326 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003327 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003328 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3329 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3330
Harini Katakamfff80192016-08-09 13:15:53 +05303331#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003332 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
Harini Katakamfff80192016-08-09 13:15:53 +05303333 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003334 bp->hw_dma_cap = HW_DMA_CAP_64B;
3335 } else
3336 bp->hw_dma_cap = HW_DMA_CAP_32B;
Harini Katakamfff80192016-08-09 13:15:53 +05303337#endif
3338
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003339 spin_lock_init(&bp->lock);
3340
Nicolas Ferread783472015-03-31 15:02:02 +02003341 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003342 macb_configure_caps(bp, macb_config);
3343
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003344 platform_set_drvdata(pdev, dev);
3345
3346 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003347 if (dev->irq < 0) {
3348 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003349 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003350 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003351
Jarod Wilson44770e12016-10-17 15:54:17 -04003352 /* MTU range: 68 - 1500 or 10240 */
3353 dev->min_mtu = GEM_MTU_MIN_SIZE;
3354 if (bp->caps & MACB_CAPS_JUMBO)
3355 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3356 else
3357 dev->max_mtu = ETH_DATA_LEN;
3358
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003359 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003360 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003361 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003362 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003363 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003364
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003365 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003366 phy_node = of_get_next_available_child(np, NULL);
3367 if (phy_node) {
3368 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003369
Charles Keepax0e3e7992016-03-28 13:47:42 +01003370 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003371 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003372 gpiod_direction_output(bp->reset_gpio, 1);
3373 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003374 }
3375 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003376
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003377 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003378 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003379 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003380 if (pdata && pdata->is_rmii)
3381 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3382 else
3383 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3384 } else {
3385 bp->phy_interface = err;
3386 }
3387
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003388 /* IP specific init */
3389 err = init(pdev);
3390 if (err)
3391 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003392
Florian Fainellicf669662016-05-02 18:38:45 -07003393 err = macb_mii_init(bp);
3394 if (err)
3395 goto err_out_free_netdev;
3396
Philippe Reynes0a912812016-06-22 00:32:35 +02003397 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003398
3399 netif_carrier_off(dev);
3400
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003401 err = register_netdev(dev);
3402 if (err) {
3403 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003404 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003405 }
3406
Florian Fainellicf669662016-05-02 18:38:45 -07003407 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003408
Bo Shen58798232014-09-13 01:57:49 +02003409 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3410 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3411 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003412
3413 return 0;
3414
Florian Fainellicf669662016-05-02 18:38:45 -07003415err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003416 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003417 mdiobus_unregister(bp->mii_bus);
3418 mdiobus_free(bp->mii_bus);
3419
3420 /* Shutdown the PHY if there is a GPIO reset */
3421 if (bp->reset_gpio)
3422 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003423
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003424err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003425 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003426
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003427err_disable_clocks:
3428 clk_disable_unprepare(tx_clk);
3429 clk_disable_unprepare(hclk);
3430 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303431 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003432
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003433 return err;
3434}
3435
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003436static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003437{
3438 struct net_device *dev;
3439 struct macb *bp;
3440
3441 dev = platform_get_drvdata(pdev);
3442
3443 if (dev) {
3444 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003445 if (dev->phydev)
3446 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003447 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003448 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003449 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003450
3451 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003452 if (bp->reset_gpio)
3453 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003454
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003455 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003456 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003457 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003458 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303459 clk_disable_unprepare(bp->rx_clk);
Michael Grzeschikdacdbb42017-06-23 16:54:10 +02003460 of_node_put(bp->phy_node);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003461 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003462 }
3463
3464 return 0;
3465}
3466
Michal Simekd23823d2015-01-23 09:36:03 +01003467static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003468{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003469 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003470 struct net_device *netdev = platform_get_drvdata(pdev);
3471 struct macb *bp = netdev_priv(netdev);
3472
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003473 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003474 netif_device_detach(netdev);
3475
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003476 if (bp->wol & MACB_WOL_ENABLED) {
3477 macb_writel(bp, IER, MACB_BIT(WOL));
3478 macb_writel(bp, WOL, MACB_BIT(MAG));
3479 enable_irq_wake(bp->queues[0].irq);
3480 } else {
3481 clk_disable_unprepare(bp->tx_clk);
3482 clk_disable_unprepare(bp->hclk);
3483 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303484 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003485 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003486
3487 return 0;
3488}
3489
Michal Simekd23823d2015-01-23 09:36:03 +01003490static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003491{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003492 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003493 struct net_device *netdev = platform_get_drvdata(pdev);
3494 struct macb *bp = netdev_priv(netdev);
3495
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003496 if (bp->wol & MACB_WOL_ENABLED) {
3497 macb_writel(bp, IDR, MACB_BIT(WOL));
3498 macb_writel(bp, WOL, 0);
3499 disable_irq_wake(bp->queues[0].irq);
3500 } else {
3501 clk_prepare_enable(bp->pclk);
3502 clk_prepare_enable(bp->hclk);
3503 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303504 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003505 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003506
3507 netif_device_attach(netdev);
3508
3509 return 0;
3510}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003511
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003512static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3513
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003514static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003515 .probe = macb_probe,
3516 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003517 .driver = {
3518 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003519 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003520 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003521 },
3522};
3523
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003524module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003525
3526MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003527MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003528MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003529MODULE_ALIAS("platform:macb");