blob: 5cbd1e7a926aa2f1eaf3a2f73aa75fe231b219f2 [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
Jiri Pirko7455a762010-02-08 05:12:08 +0000428 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200429 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000430 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200431 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200432 }
433
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000434 pdata = dev_get_platdata(&bp->pdev->dev);
435 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700436 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin,
437 "phy int");
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000438 if (!ret) {
439 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
440 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
441 }
Bartosz Folta83a77e92016-12-14 06:39:15 +0000442 } else {
443 phydev->irq = PHY_POLL;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000444 }
frederic RODO6c36a702007-07-12 19:07:24 +0200445
446 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000447 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100448 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000449 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000450 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000451 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200452 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100453
frederic RODO6c36a702007-07-12 19:07:24 +0200454 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200455 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000456 phydev->supported &= PHY_GBIT_FEATURES;
457 else
458 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100459
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500460 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
461 phydev->supported &= ~SUPPORTED_1000baseT_Half;
462
frederic RODO6c36a702007-07-12 19:07:24 +0200463 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100464
frederic RODO6c36a702007-07-12 19:07:24 +0200465 bp->link = 0;
466 bp->speed = 0;
467 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200468
469 return 0;
470}
471
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100472static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200473{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000474 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200475 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200476 int err = -ENXIO, i;
477
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200478 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200479 macb_writel(bp, NCR, MACB_BIT(MPE));
480
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700481 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700482 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200483 err = -ENOMEM;
484 goto err_out;
485 }
486
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700487 bp->mii_bus->name = "MACB_mii_bus";
488 bp->mii_bus->read = &macb_mdio_read;
489 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000490 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700491 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700492 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700493 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900494 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700495
Jamie Iles91523942011-02-28 04:05:25 +0000496 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200497
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200498 np = bp->pdev->dev.of_node;
499 if (np) {
500 /* try dt phy registration */
501 err = of_mdiobus_register(bp->mii_bus, np);
502
503 /* fallback to standard phy registration if no phy were
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700504 * found during dt phy registration
505 */
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200506 if (!err && !phy_find_first(bp->mii_bus)) {
507 for (i = 0; i < PHY_MAX_ADDR; i++) {
508 struct phy_device *phydev;
509
510 phydev = mdiobus_scan(bp->mii_bus, i);
Sergei Shtylyovce24c2b2016-05-01 01:47:36 +0300511 if (IS_ERR(phydev) &&
512 PTR_ERR(phydev) != -ENODEV) {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200513 err = PTR_ERR(phydev);
514 break;
515 }
516 }
517
518 if (err)
519 goto err_out_unregister_bus;
520 }
521 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000522 for (i = 0; i < PHY_MAX_ADDR; i++)
523 bp->mii_bus->irq[i] = PHY_POLL;
524
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200525 if (pdata)
526 bp->mii_bus->phy_mask = pdata->phy_mask;
527
528 err = mdiobus_register(bp->mii_bus);
529 }
530
531 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100532 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200533
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200534 err = macb_mii_probe(bp->dev);
535 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200536 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200537
538 return 0;
539
540err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700541 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700542err_out_free_mdiobus:
543 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200544err_out:
545 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100546}
547
548static void macb_update_stats(struct macb *bp)
549{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000550 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
551 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300552 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100553
554 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
555
Moritz Fischer96ec6312016-03-29 19:11:11 -0700556 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700557 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100558}
559
Nicolas Ferree86cd532012-10-31 06:04:57 +0000560static int macb_halt_tx(struct macb *bp)
561{
562 unsigned long halt_time, timeout;
563 u32 status;
564
565 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
566
567 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
568 do {
569 halt_time = jiffies;
570 status = macb_readl(bp, TSR);
571 if (!(status & MACB_BIT(TGO)))
572 return 0;
573
574 usleep_range(10, 250);
575 } while (time_before(halt_time, timeout));
576
577 return -ETIMEDOUT;
578}
579
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200580static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
581{
582 if (tx_skb->mapping) {
583 if (tx_skb->mapped_as_page)
584 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
585 tx_skb->size, DMA_TO_DEVICE);
586 else
587 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
588 tx_skb->size, DMA_TO_DEVICE);
589 tx_skb->mapping = 0;
590 }
591
592 if (tx_skb->skb) {
593 dev_kfree_skb_any(tx_skb->skb);
594 tx_skb->skb = NULL;
595 }
596}
597
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000598static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530599{
Harini Katakamfff80192016-08-09 13:15:53 +0530600#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000601 struct macb_dma_desc_64 *desc_64;
602
603 if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
604 desc_64 = macb_64b_desc(bp, desc);
605 desc_64->addrh = upper_32_bits(addr);
606 }
Harini Katakamfff80192016-08-09 13:15:53 +0530607#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000608 desc->addr = lower_32_bits(addr);
609}
610
611static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
612{
613 dma_addr_t addr = 0;
614#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
615 struct macb_dma_desc_64 *desc_64;
616
617 if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
618 desc_64 = macb_64b_desc(bp, desc);
619 addr = ((u64)(desc_64->addrh) << 32);
620 }
621#endif
622 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
623 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530624}
625
Nicolas Ferree86cd532012-10-31 06:04:57 +0000626static void macb_tx_error_task(struct work_struct *work)
627{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100628 struct macb_queue *queue = container_of(work, struct macb_queue,
629 tx_error_task);
630 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000631 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100632 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000633 struct sk_buff *skb;
634 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100635 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000636
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100637 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
638 (unsigned int)(queue - bp->queues),
639 queue->tx_tail, queue->tx_head);
640
641 /* Prevent the queue IRQ handlers from running: each of them may call
642 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
643 * As explained below, we have to halt the transmission before updating
644 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
645 * network engine about the macb/gem being halted.
646 */
647 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000648
649 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100650 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000651
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700652 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000653 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100654 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000655 */
656 if (macb_halt_tx(bp))
657 /* Just complain for now, reinitializing TX path can be good */
658 netdev_err(bp->dev, "BUG: halt tx timed out\n");
659
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700660 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000661 * Free transmit buffers in upper layer.
662 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100663 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
664 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000665
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100666 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000667 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100668 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000669 skb = tx_skb->skb;
670
671 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200672 /* skb is set for the last buffer of the frame */
673 while (!skb) {
674 macb_tx_unmap(bp, tx_skb);
675 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100676 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200677 skb = tx_skb->skb;
678 }
679
680 /* ctrl still refers to the first buffer descriptor
681 * since it's the only one written back by the hardware
682 */
683 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
684 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500685 macb_tx_ring_wrap(bp, tail),
686 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200687 bp->dev->stats.tx_packets++;
688 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200689 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000690 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700691 /* "Buffers exhausted mid-frame" errors may only happen
692 * if the driver is buggy, so complain loudly about
693 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000694 */
695 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
696 netdev_err(bp->dev,
697 "BUG: TX buffers exhausted mid-frame\n");
698
699 desc->ctrl = ctrl | MACB_BIT(TX_USED);
700 }
701
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200702 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000703 }
704
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100705 /* Set end of TX queue */
706 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000707 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100708 desc->ctrl = MACB_BIT(TX_USED);
709
Nicolas Ferree86cd532012-10-31 06:04:57 +0000710 /* Make descriptor updates visible to hardware */
711 wmb();
712
713 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000714 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530715#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000716 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
717 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530718#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000719 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100720 queue->tx_head = 0;
721 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000722
723 /* Housework before enabling TX IRQ */
724 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100725 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
726
727 /* Now we are ready to start transmission again */
728 netif_tx_start_all_queues(bp->dev);
729 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
730
731 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000732}
733
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100734static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100735{
736 unsigned int tail;
737 unsigned int head;
738 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100739 struct macb *bp = queue->bp;
740 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100741
742 status = macb_readl(bp, TSR);
743 macb_writel(bp, TSR, status);
744
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000745 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100746 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000747
Nicolas Ferree86cd532012-10-31 06:04:57 +0000748 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700749 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100750
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100751 head = queue->tx_head;
752 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000753 struct macb_tx_skb *tx_skb;
754 struct sk_buff *skb;
755 struct macb_dma_desc *desc;
756 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100757
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100758 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100759
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000760 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100761 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000762
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000763 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100764
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200765 /* TX_USED bit is only set by hardware on the very first buffer
766 * descriptor of the transmitted frame.
767 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000768 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100769 break;
770
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200771 /* Process all buffers of the current transmitted frame */
772 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100773 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200774 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000775
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200776 /* First, update TX stats if needed */
777 if (skb) {
778 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500779 macb_tx_ring_wrap(bp, tail),
780 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200781 bp->dev->stats.tx_packets++;
782 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200783 }
784
785 /* Now we can safely release resources */
786 macb_tx_unmap(bp, tx_skb);
787
788 /* skb is set only for the last buffer of the frame.
789 * WARNING: at this point skb has been freed by
790 * macb_tx_unmap().
791 */
792 if (skb)
793 break;
794 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100795 }
796
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100797 queue->tx_tail = tail;
798 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
799 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500800 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100801 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100802}
803
Nicolas Ferre4df95132013-06-04 21:57:12 +0000804static void gem_rx_refill(struct macb *bp)
805{
806 unsigned int entry;
807 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000808 dma_addr_t paddr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000809 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000810
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700811 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500812 bp->rx_ring_size) > 0) {
813 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000814
815 /* Make hw descriptor updates visible to CPU */
816 rmb();
817
Nicolas Ferre4df95132013-06-04 21:57:12 +0000818 bp->rx_prepared_head++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000819 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000820
Moritz Fischeraa50b552016-03-29 19:11:13 -0700821 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000822 /* allocate sk_buff for this free entry in ring */
823 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700824 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000825 netdev_err(bp->dev,
826 "Unable to allocate sk_buff\n");
827 break;
828 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000829
830 /* now fill corresponding descriptor entry */
831 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700832 bp->rx_buffer_size,
833 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800834 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
835 dev_kfree_skb(skb);
836 break;
837 }
838
839 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000840
Zach Brownb410d132016-10-19 09:56:57 -0500841 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000842 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000843 macb_set_addr(bp, desc, paddr);
844 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000845
846 /* properly align Ethernet header */
847 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530848 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000849 desc->addr &= ~MACB_BIT(RX_USED);
850 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000851 }
852 }
853
854 /* Make descriptor updates visible to hardware */
855 wmb();
856
857 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700858 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000859}
860
861/* Mark DMA descriptors from begin up to and not including end as unused */
862static void discard_partial_frame(struct macb *bp, unsigned int begin,
863 unsigned int end)
864{
865 unsigned int frag;
866
867 for (frag = begin; frag != end; frag++) {
868 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700869
Nicolas Ferre4df95132013-06-04 21:57:12 +0000870 desc->addr &= ~MACB_BIT(RX_USED);
871 }
872
873 /* Make descriptor updates visible to hardware */
874 wmb();
875
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700876 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000877 * whatever caused this is updated, so we don't have to record
878 * anything.
879 */
880}
881
882static int gem_rx(struct macb *bp, int budget)
883{
884 unsigned int len;
885 unsigned int entry;
886 struct sk_buff *skb;
887 struct macb_dma_desc *desc;
888 int count = 0;
889
890 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530891 u32 ctrl;
892 dma_addr_t addr;
893 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000894
Zach Brownb410d132016-10-19 09:56:57 -0500895 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000896 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000897
898 /* Make hw descriptor updates visible to CPU */
899 rmb();
900
Harini Katakamfff80192016-08-09 13:15:53 +0530901 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000902 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000903 ctrl = desc->ctrl;
904
Harini Katakamfff80192016-08-09 13:15:53 +0530905 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000906 break;
907
Nicolas Ferre4df95132013-06-04 21:57:12 +0000908 bp->rx_tail++;
909 count++;
910
911 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
912 netdev_err(bp->dev,
913 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200914 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000915 break;
916 }
917 skb = bp->rx_skbuff[entry];
918 if (unlikely(!skb)) {
919 netdev_err(bp->dev,
920 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200921 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000922 break;
923 }
924 /* now everything is ready for receiving packet */
925 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530926 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000927
928 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
929
930 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000931 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800932 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000933
934 skb->protocol = eth_type_trans(skb, bp->dev);
935 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200936 if (bp->dev->features & NETIF_F_RXCSUM &&
937 !(bp->dev->flags & IFF_PROMISC) &&
938 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
939 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000940
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200941 bp->dev->stats.rx_packets++;
942 bp->dev->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000943
944#if defined(DEBUG) && defined(VERBOSE_DEBUG)
945 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
946 skb->len, skb->csum);
947 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100948 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000949 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
950 skb->data, 32, true);
951#endif
952
953 netif_receive_skb(skb);
954 }
955
956 gem_rx_refill(bp);
957
958 return count;
959}
960
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100961static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
962 unsigned int last_frag)
963{
964 unsigned int len;
965 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000966 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100967 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000968 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100969
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000970 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530971 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100972
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000973 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -0500974 macb_rx_ring_wrap(bp, first_frag),
975 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100976
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700977 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000978 * first buffer. Since the header is 14 bytes, this makes the
979 * payload word-aligned.
980 *
981 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
982 * the two padding bytes into the skb so that we avoid hitting
983 * the slowpath in memcpy(), and pull them off afterwards.
984 */
985 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100986 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200987 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000988 for (frag = first_frag; ; frag++) {
989 desc = macb_rx_desc(bp, frag);
990 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100991 if (frag == last_frag)
992 break;
993 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000994
995 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100996 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000997
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100998 return 1;
999 }
1000
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001001 offset = 0;
1002 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001003 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001004 skb_put(skb, len);
1005
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001006 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001007 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001008
1009 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001010 if (unlikely(frag != last_frag)) {
1011 dev_kfree_skb_any(skb);
1012 return -1;
1013 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001014 frag_len = len - offset;
1015 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001016 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001017 macb_rx_buffer(bp, frag),
1018 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001019 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001020 desc = macb_rx_desc(bp, frag);
1021 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001022
1023 if (frag == last_frag)
1024 break;
1025 }
1026
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001027 /* Make descriptor updates visible to hardware */
1028 wmb();
1029
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001030 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001031 skb->protocol = eth_type_trans(skb, bp->dev);
1032
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001033 bp->dev->stats.rx_packets++;
1034 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001035 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001036 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001037 netif_receive_skb(skb);
1038
1039 return 0;
1040}
1041
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001042static inline void macb_init_rx_ring(struct macb *bp)
1043{
1044 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001045 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001046 int i;
1047
1048 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001049 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001050 desc = macb_rx_desc(bp, i);
1051 macb_set_addr(bp, desc, addr);
1052 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001053 addr += bp->rx_buffer_size;
1054 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001055 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchena0b44ee2016-11-28 14:40:55 +01001056 bp->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001057}
1058
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001059static int macb_rx(struct macb *bp, int budget)
1060{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001061 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001062 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001063 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001064 int first_frag = -1;
1065
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001066 for (tail = bp->rx_tail; budget > 0; tail++) {
1067 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001068 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001069
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001070 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001071 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001072
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001073 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001074
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001075 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076 break;
1077
1078 if (ctrl & MACB_BIT(RX_SOF)) {
1079 if (first_frag != -1)
1080 discard_partial_frame(bp, first_frag, tail);
1081 first_frag = tail;
1082 }
1083
1084 if (ctrl & MACB_BIT(RX_EOF)) {
1085 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001086
1087 if (unlikely(first_frag == -1)) {
1088 reset_rx_queue = true;
1089 continue;
1090 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001091
1092 dropped = macb_rx_frame(bp, first_frag, tail);
1093 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001094 if (unlikely(dropped < 0)) {
1095 reset_rx_queue = true;
1096 continue;
1097 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001098 if (!dropped) {
1099 received++;
1100 budget--;
1101 }
1102 }
1103 }
1104
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001105 if (unlikely(reset_rx_queue)) {
1106 unsigned long flags;
1107 u32 ctrl;
1108
1109 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1110
1111 spin_lock_irqsave(&bp->lock, flags);
1112
1113 ctrl = macb_readl(bp, NCR);
1114 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1115
1116 macb_init_rx_ring(bp);
1117 macb_writel(bp, RBQP, bp->rx_ring_dma);
1118
1119 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1120
1121 spin_unlock_irqrestore(&bp->lock, flags);
1122 return received;
1123 }
1124
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001125 if (first_frag != -1)
1126 bp->rx_tail = first_frag;
1127 else
1128 bp->rx_tail = tail;
1129
1130 return received;
1131}
1132
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001133static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001134{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001135 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001136 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001137 u32 status;
1138
1139 status = macb_readl(bp, RSR);
1140 macb_writel(bp, RSR, status);
1141
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001142 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001143
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001144 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001145 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001146
Nicolas Ferre4df95132013-06-04 21:57:12 +00001147 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001148 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001149 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001150
Nicolas Ferre8770e912013-02-12 11:08:48 +01001151 /* Packets received while interrupts were disabled */
1152 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001153 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001154 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1155 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001156 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001157 } else {
1158 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1159 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001160 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001161
1162 /* TODO: Handle errors */
1163
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001164 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001165}
1166
1167static irqreturn_t macb_interrupt(int irq, void *dev_id)
1168{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001169 struct macb_queue *queue = dev_id;
1170 struct macb *bp = queue->bp;
1171 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001172 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001173
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001174 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001175
1176 if (unlikely(!status))
1177 return IRQ_NONE;
1178
1179 spin_lock(&bp->lock);
1180
1181 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001182 /* close possible race with dev_close */
1183 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001184 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001185 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1186 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001187 break;
1188 }
1189
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001190 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1191 (unsigned int)(queue - bp->queues),
1192 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001193
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001194 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001195 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001196 * until we have processed the buffers. The
1197 * scheduling call may fail if the poll routine
1198 * is already scheduled, so disable interrupts
1199 * now.
1200 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001201 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001202 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001203 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001204
Ben Hutchings288379f2009-01-19 16:43:59 -08001205 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001206 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001207 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001208 }
1209 }
1210
Nicolas Ferree86cd532012-10-31 06:04:57 +00001211 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001212 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1213 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001214
1215 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001216 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001217
Nicolas Ferree86cd532012-10-31 06:04:57 +00001218 break;
1219 }
1220
1221 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001222 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001223
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001224 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001225 * add that if/when we get our hands on a full-blown MII PHY.
1226 */
1227
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001228 /* There is a hardware issue under heavy load where DMA can
1229 * stop, this causes endless "used buffer descriptor read"
1230 * interrupts but it can be cleared by re-enabling RX. See
1231 * the at91 manual, section 41.3.1 or the Zynq manual
1232 * section 16.7.4 for details.
1233 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001234 if (status & MACB_BIT(RXUBR)) {
1235 ctrl = macb_readl(bp, NCR);
1236 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001237 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001238 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1239
1240 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001241 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001242 }
1243
Alexander Steinb19f7f72011-04-13 05:03:24 +00001244 if (status & MACB_BIT(ISR_ROVR)) {
1245 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001246 if (macb_is_gem(bp))
1247 bp->hw_stats.gem.rx_overruns++;
1248 else
1249 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001250
1251 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001252 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001253 }
1254
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001255 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001256 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001257 * netdev_err to a lower-priority context as well
1258 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001259 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001260 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001261
1262 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001263 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001264 }
1265
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001266 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001267 }
1268
1269 spin_unlock(&bp->lock);
1270
1271 return IRQ_HANDLED;
1272}
1273
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001274#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001275/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001276 * to allow network i/o with interrupts disabled.
1277 */
1278static void macb_poll_controller(struct net_device *dev)
1279{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001280 struct macb *bp = netdev_priv(dev);
1281 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001282 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001283 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001284
1285 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001286 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1287 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001288 local_irq_restore(flags);
1289}
1290#endif
1291
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001292static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001293 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001294 struct sk_buff *skb,
1295 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001296{
1297 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001298 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001299 struct macb_tx_skb *tx_skb = NULL;
1300 struct macb_dma_desc *desc;
1301 unsigned int offset, size, count = 0;
1302 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001303 unsigned int eof = 1, mss_mfs = 0;
1304 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1305
1306 /* LSO */
1307 if (skb_shinfo(skb)->gso_size != 0) {
1308 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1309 /* UDP - UFO */
1310 lso_ctrl = MACB_LSO_UFO_ENABLE;
1311 else
1312 /* TCP - TSO */
1313 lso_ctrl = MACB_LSO_TSO_ENABLE;
1314 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001315
1316 /* First, map non-paged data */
1317 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001318
1319 /* first buffer length */
1320 size = hdrlen;
1321
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001322 offset = 0;
1323 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001324 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001325 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001326
1327 mapping = dma_map_single(&bp->pdev->dev,
1328 skb->data + offset,
1329 size, DMA_TO_DEVICE);
1330 if (dma_mapping_error(&bp->pdev->dev, mapping))
1331 goto dma_error;
1332
1333 /* Save info to properly release resources */
1334 tx_skb->skb = NULL;
1335 tx_skb->mapping = mapping;
1336 tx_skb->size = size;
1337 tx_skb->mapped_as_page = false;
1338
1339 len -= size;
1340 offset += size;
1341 count++;
1342 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001343
1344 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001345 }
1346
1347 /* Then, map paged data from fragments */
1348 for (f = 0; f < nr_frags; f++) {
1349 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1350
1351 len = skb_frag_size(frag);
1352 offset = 0;
1353 while (len) {
1354 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001355 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001356 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001357
1358 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1359 offset, size, DMA_TO_DEVICE);
1360 if (dma_mapping_error(&bp->pdev->dev, mapping))
1361 goto dma_error;
1362
1363 /* Save info to properly release resources */
1364 tx_skb->skb = NULL;
1365 tx_skb->mapping = mapping;
1366 tx_skb->size = size;
1367 tx_skb->mapped_as_page = true;
1368
1369 len -= size;
1370 offset += size;
1371 count++;
1372 tx_head++;
1373 }
1374 }
1375
1376 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001377 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001378 netdev_err(bp->dev, "BUG! empty skb!\n");
1379 return 0;
1380 }
1381
1382 /* This is the last buffer of the frame: save socket buffer */
1383 tx_skb->skb = skb;
1384
1385 /* Update TX ring: update buffer descriptors in reverse order
1386 * to avoid race condition
1387 */
1388
1389 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1390 * to set the end of TX queue
1391 */
1392 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001393 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001394 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001395 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001396 desc->ctrl = ctrl;
1397
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001398 if (lso_ctrl) {
1399 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1400 /* include header and FCS in value given to h/w */
1401 mss_mfs = skb_shinfo(skb)->gso_size +
1402 skb_transport_offset(skb) +
1403 ETH_FCS_LEN;
1404 else /* TSO */ {
1405 mss_mfs = skb_shinfo(skb)->gso_size;
1406 /* TCP Sequence Number Source Select
1407 * can be set only for TSO
1408 */
1409 seq_ctrl = 0;
1410 }
1411 }
1412
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001413 do {
1414 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001415 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001416 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001417 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001418
1419 ctrl = (u32)tx_skb->size;
1420 if (eof) {
1421 ctrl |= MACB_BIT(TX_LAST);
1422 eof = 0;
1423 }
Zach Brownb410d132016-10-19 09:56:57 -05001424 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001425 ctrl |= MACB_BIT(TX_WRAP);
1426
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001427 /* First descriptor is header descriptor */
1428 if (i == queue->tx_head) {
1429 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1430 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1431 } else
1432 /* Only set MSS/MFS on payload descriptors
1433 * (second or later descriptor)
1434 */
1435 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1436
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001437 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001438 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001439 /* desc->addr must be visible to hardware before clearing
1440 * 'TX_USED' bit in desc->ctrl.
1441 */
1442 wmb();
1443 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001444 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001445
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001446 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001447
1448 return count;
1449
1450dma_error:
1451 netdev_err(bp->dev, "TX DMA map failed\n");
1452
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001453 for (i = queue->tx_head; i != tx_head; i++) {
1454 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001455
1456 macb_tx_unmap(bp, tx_skb);
1457 }
1458
1459 return 0;
1460}
1461
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001462static netdev_features_t macb_features_check(struct sk_buff *skb,
1463 struct net_device *dev,
1464 netdev_features_t features)
1465{
1466 unsigned int nr_frags, f;
1467 unsigned int hdrlen;
1468
1469 /* Validate LSO compatibility */
1470
1471 /* there is only one buffer */
1472 if (!skb_is_nonlinear(skb))
1473 return features;
1474
1475 /* length of header */
1476 hdrlen = skb_transport_offset(skb);
1477 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1478 hdrlen += tcp_hdrlen(skb);
1479
1480 /* For LSO:
1481 * When software supplies two or more payload buffers all payload buffers
1482 * apart from the last must be a multiple of 8 bytes in size.
1483 */
1484 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1485 return features & ~MACB_NETIF_LSO;
1486
1487 nr_frags = skb_shinfo(skb)->nr_frags;
1488 /* No need to check last fragment */
1489 nr_frags--;
1490 for (f = 0; f < nr_frags; f++) {
1491 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1492
1493 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1494 return features & ~MACB_NETIF_LSO;
1495 }
1496 return features;
1497}
1498
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001499static inline int macb_clear_csum(struct sk_buff *skb)
1500{
1501 /* no change for packets without checksum offloading */
1502 if (skb->ip_summed != CHECKSUM_PARTIAL)
1503 return 0;
1504
1505 /* make sure we can modify the header */
1506 if (unlikely(skb_cow_head(skb, 0)))
1507 return -1;
1508
1509 /* initialize checksum field
1510 * This is required - at least for Zynq, which otherwise calculates
1511 * wrong UDP header checksums for UDP packets with UDP data len <=2
1512 */
1513 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1514 return 0;
1515}
1516
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001517static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1518{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001519 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001520 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001521 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001522 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001523 unsigned int desc_cnt, nr_frags, frag_size, f;
1524 unsigned int hdrlen;
1525 bool is_lso, is_udp = 0;
1526
1527 is_lso = (skb_shinfo(skb)->gso_size != 0);
1528
1529 if (is_lso) {
1530 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1531
1532 /* length of headers */
1533 if (is_udp)
1534 /* only queue eth + ip headers separately for UDP */
1535 hdrlen = skb_transport_offset(skb);
1536 else
1537 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1538 if (skb_headlen(skb) < hdrlen) {
1539 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1540 /* if this is required, would need to copy to single buffer */
1541 return NETDEV_TX_BUSY;
1542 }
1543 } else
1544 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001545
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001546#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1547 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001548 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1549 queue_index, skb->len, skb->head, skb->data,
1550 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001551 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1552 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001553#endif
1554
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001555 /* Count how many TX buffer descriptors are needed to send this
1556 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001557 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001558 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001559 if (is_lso && (skb_headlen(skb) > hdrlen))
1560 /* extra header descriptor if also payload in first buffer */
1561 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1562 else
1563 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001564 nr_frags = skb_shinfo(skb)->nr_frags;
1565 for (f = 0; f < nr_frags; f++) {
1566 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001567 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001568 }
1569
Dongdong Deng48719532009-08-23 19:49:07 -07001570 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001571
1572 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001573 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001574 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001575 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001576 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001577 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001578 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001579 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001580 }
1581
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001582 if (macb_clear_csum(skb)) {
1583 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001584 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001585 }
1586
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001587 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001588 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001589 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001590 goto unlock;
1591 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001592
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001593 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001594 wmb();
1595
Richard Cochrane0720922011-06-19 21:51:28 +00001596 skb_tx_timestamp(skb);
1597
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001598 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1599
Zach Brownb410d132016-10-19 09:56:57 -05001600 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001601 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001602
Soren Brinkmann92030902014-03-04 08:46:39 -08001603unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001604 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001605
Patrick McHardy6ed10652009-06-23 06:03:08 +00001606 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001607}
1608
Nicolas Ferre4df95132013-06-04 21:57:12 +00001609static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001610{
1611 if (!macb_is_gem(bp)) {
1612 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1613 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001614 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001615
Nicolas Ferre1b447912013-06-04 21:57:11 +00001616 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001617 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001618 "RX buffer must be multiple of %d bytes, expanding\n",
1619 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001620 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001621 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001622 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001623 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001624
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001625 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001626 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001627}
1628
Nicolas Ferre4df95132013-06-04 21:57:12 +00001629static void gem_free_rx_buffers(struct macb *bp)
1630{
1631 struct sk_buff *skb;
1632 struct macb_dma_desc *desc;
1633 dma_addr_t addr;
1634 int i;
1635
1636 if (!bp->rx_skbuff)
1637 return;
1638
Zach Brownb410d132016-10-19 09:56:57 -05001639 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001640 skb = bp->rx_skbuff[i];
1641
Moritz Fischeraa50b552016-03-29 19:11:13 -07001642 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001643 continue;
1644
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001645 desc = macb_rx_desc(bp, i);
1646 addr = macb_get_addr(bp, desc);
1647
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001648 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001649 DMA_FROM_DEVICE);
1650 dev_kfree_skb_any(skb);
1651 skb = NULL;
1652 }
1653
1654 kfree(bp->rx_skbuff);
1655 bp->rx_skbuff = NULL;
1656}
1657
1658static void macb_free_rx_buffers(struct macb *bp)
1659{
1660 if (bp->rx_buffers) {
1661 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001662 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001663 bp->rx_buffers, bp->rx_buffers_dma);
1664 bp->rx_buffers = NULL;
1665 }
1666}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001667
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001668static void macb_free_consistent(struct macb *bp)
1669{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001670 struct macb_queue *queue;
1671 unsigned int q;
1672
Nicolas Ferre4df95132013-06-04 21:57:12 +00001673 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001674 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001675 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001676 bp->rx_ring, bp->rx_ring_dma);
1677 bp->rx_ring = NULL;
1678 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001679
1680 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1681 kfree(queue->tx_skb);
1682 queue->tx_skb = NULL;
1683 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001684 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001685 queue->tx_ring, queue->tx_ring_dma);
1686 queue->tx_ring = NULL;
1687 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001688 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001689}
1690
1691static int gem_alloc_rx_buffers(struct macb *bp)
1692{
1693 int size;
1694
Zach Brownb410d132016-10-19 09:56:57 -05001695 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001696 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1697 if (!bp->rx_skbuff)
1698 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001699 else
1700 netdev_dbg(bp->dev,
1701 "Allocated %d RX struct sk_buff entries at %p\n",
1702 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001703 return 0;
1704}
1705
1706static int macb_alloc_rx_buffers(struct macb *bp)
1707{
1708 int size;
1709
Zach Brownb410d132016-10-19 09:56:57 -05001710 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001711 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1712 &bp->rx_buffers_dma, GFP_KERNEL);
1713 if (!bp->rx_buffers)
1714 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001715
1716 netdev_dbg(bp->dev,
1717 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1718 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001719 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001720}
1721
1722static int macb_alloc_consistent(struct macb *bp)
1723{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001724 struct macb_queue *queue;
1725 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001726 int size;
1727
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001728 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001729 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001730 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1731 &queue->tx_ring_dma,
1732 GFP_KERNEL);
1733 if (!queue->tx_ring)
1734 goto out_err;
1735 netdev_dbg(bp->dev,
1736 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1737 q, size, (unsigned long)queue->tx_ring_dma,
1738 queue->tx_ring);
1739
Zach Brownb410d132016-10-19 09:56:57 -05001740 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001741 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1742 if (!queue->tx_skb)
1743 goto out_err;
1744 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001745
Zach Brownb410d132016-10-19 09:56:57 -05001746 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001747 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1748 &bp->rx_ring_dma, GFP_KERNEL);
1749 if (!bp->rx_ring)
1750 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001751 netdev_dbg(bp->dev,
1752 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1753 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001754
Nicolas Ferre4df95132013-06-04 21:57:12 +00001755 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001756 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001757
1758 return 0;
1759
1760out_err:
1761 macb_free_consistent(bp);
1762 return -ENOMEM;
1763}
1764
Nicolas Ferre4df95132013-06-04 21:57:12 +00001765static void gem_init_rings(struct macb *bp)
1766{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001767 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001768 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001769 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001770 int i;
1771
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001772 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001773 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001774 desc = macb_tx_desc(queue, i);
1775 macb_set_addr(bp, desc, 0);
1776 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001777 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001778 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001779 queue->tx_head = 0;
1780 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001781 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001782
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001783 bp->rx_tail = 0;
1784 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001785
1786 gem_rx_refill(bp);
1787}
1788
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001789static void macb_init_rings(struct macb *bp)
1790{
1791 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001792 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001793
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001794 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001795
Zach Brownb410d132016-10-19 09:56:57 -05001796 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001797 desc = macb_tx_desc(&bp->queues[0], i);
1798 macb_set_addr(bp, desc, 0);
1799 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001800 }
Ben Shelton21d35152015-04-22 17:28:54 -05001801 bp->queues[0].tx_head = 0;
1802 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001803 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001804}
1805
1806static void macb_reset_hw(struct macb *bp)
1807{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001808 struct macb_queue *queue;
1809 unsigned int q;
1810
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001811 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001812 * more gracefully?)
1813 */
1814 macb_writel(bp, NCR, 0);
1815
1816 /* Clear the stats registers (XXX: Update stats first?) */
1817 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1818
1819 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001820 macb_writel(bp, TSR, -1);
1821 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001822
1823 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001824 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1825 queue_writel(queue, IDR, -1);
1826 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001827 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1828 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001829 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001830}
1831
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001832static u32 gem_mdc_clk_div(struct macb *bp)
1833{
1834 u32 config;
1835 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1836
1837 if (pclk_hz <= 20000000)
1838 config = GEM_BF(CLK, GEM_CLK_DIV8);
1839 else if (pclk_hz <= 40000000)
1840 config = GEM_BF(CLK, GEM_CLK_DIV16);
1841 else if (pclk_hz <= 80000000)
1842 config = GEM_BF(CLK, GEM_CLK_DIV32);
1843 else if (pclk_hz <= 120000000)
1844 config = GEM_BF(CLK, GEM_CLK_DIV48);
1845 else if (pclk_hz <= 160000000)
1846 config = GEM_BF(CLK, GEM_CLK_DIV64);
1847 else
1848 config = GEM_BF(CLK, GEM_CLK_DIV96);
1849
1850 return config;
1851}
1852
1853static u32 macb_mdc_clk_div(struct macb *bp)
1854{
1855 u32 config;
1856 unsigned long pclk_hz;
1857
1858 if (macb_is_gem(bp))
1859 return gem_mdc_clk_div(bp);
1860
1861 pclk_hz = clk_get_rate(bp->pclk);
1862 if (pclk_hz <= 20000000)
1863 config = MACB_BF(CLK, MACB_CLK_DIV8);
1864 else if (pclk_hz <= 40000000)
1865 config = MACB_BF(CLK, MACB_CLK_DIV16);
1866 else if (pclk_hz <= 80000000)
1867 config = MACB_BF(CLK, MACB_CLK_DIV32);
1868 else
1869 config = MACB_BF(CLK, MACB_CLK_DIV64);
1870
1871 return config;
1872}
1873
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001874/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001875 * should program. We find the width from decoding the design configuration
1876 * register to find the maximum supported data bus width.
1877 */
1878static u32 macb_dbw(struct macb *bp)
1879{
1880 if (!macb_is_gem(bp))
1881 return 0;
1882
1883 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1884 case 4:
1885 return GEM_BF(DBW, GEM_DBW128);
1886 case 2:
1887 return GEM_BF(DBW, GEM_DBW64);
1888 case 1:
1889 default:
1890 return GEM_BF(DBW, GEM_DBW32);
1891 }
1892}
1893
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001894/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001895 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001896 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001897 * (if not supported by FIFO, it will fallback to default)
1898 * - set both rx/tx packet buffers to full memory size
1899 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001900 */
1901static void macb_configure_dma(struct macb *bp)
1902{
1903 u32 dmacfg;
1904
1905 if (macb_is_gem(bp)) {
1906 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001907 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001908 if (bp->dma_burst_length)
1909 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001910 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301911 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301912
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001913 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301914 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1915 else
1916 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1917
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001918 if (bp->dev->features & NETIF_F_HW_CSUM)
1919 dmacfg |= GEM_BIT(TXCOEN);
1920 else
1921 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301922
1923#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001924 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1925 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05301926#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001927 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1928 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001929 gem_writel(bp, DMACFG, dmacfg);
1930 }
1931}
1932
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001933static void macb_init_hw(struct macb *bp)
1934{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001935 struct macb_queue *queue;
1936 unsigned int q;
1937
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001938 u32 config;
1939
1940 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001941 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001942
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001943 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301944 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1945 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001946 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001947 config |= MACB_BIT(PAE); /* PAuse Enable */
1948 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001949 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301950 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1951 else
1952 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001953 if (bp->dev->flags & IFF_PROMISC)
1954 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001955 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1956 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001957 if (!(bp->dev->flags & IFF_BROADCAST))
1958 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001959 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001960 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001961 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301962 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001963 bp->speed = SPEED_10;
1964 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301965 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001966 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301967 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001968
Jamie Iles0116da42011-03-14 17:38:30 +00001969 macb_configure_dma(bp);
1970
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001971 /* Initialize TX and RX buffers */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001972 macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301973#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001974 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1975 macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301976#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001977 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001978 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301979#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001980 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1981 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301982#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001983
1984 /* Enable interrupts */
1985 queue_writel(queue, IER,
1986 MACB_RX_INT_FLAGS |
1987 MACB_TX_INT_FLAGS |
1988 MACB_BIT(HRESP));
1989 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001990
1991 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001992 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001993}
1994
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001995/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001996 * locations in the memory map. The least significant bits are stored
1997 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1998 *
1999 * The unicast hash enable and the multicast hash enable bits in the
2000 * network configuration register enable the reception of hash matched
2001 * frames. The destination address is reduced to a 6 bit index into
2002 * the 64 bit hash register using the following hash function. The
2003 * hash function is an exclusive or of every sixth bit of the
2004 * destination address.
2005 *
2006 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2007 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2008 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2009 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2010 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2011 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2012 *
2013 * da[0] represents the least significant bit of the first byte
2014 * received, that is, the multicast/unicast indicator, and da[47]
2015 * represents the most significant bit of the last byte received. If
2016 * the hash index, hi[n], points to a bit that is set in the hash
2017 * register then the frame will be matched according to whether the
2018 * frame is multicast or unicast. A multicast match will be signalled
2019 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2020 * index points to a bit set in the hash register. A unicast match
2021 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2022 * and the hash index points to a bit set in the hash register. To
2023 * receive all multicast frames, the hash register should be set with
2024 * all ones and the multicast hash enable bit should be set in the
2025 * network configuration register.
2026 */
2027
2028static inline int hash_bit_value(int bitnr, __u8 *addr)
2029{
2030 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2031 return 1;
2032 return 0;
2033}
2034
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002035/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002036static int hash_get_index(__u8 *addr)
2037{
2038 int i, j, bitval;
2039 int hash_index = 0;
2040
2041 for (j = 0; j < 6; j++) {
2042 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002043 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002044
2045 hash_index |= (bitval << j);
2046 }
2047
2048 return hash_index;
2049}
2050
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002051/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002052static void macb_sethashtable(struct net_device *dev)
2053{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002054 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002055 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002056 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002057 struct macb *bp = netdev_priv(dev);
2058
Moritz Fischeraa50b552016-03-29 19:11:13 -07002059 mc_filter[0] = 0;
2060 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002061
Jiri Pirko22bedad32010-04-01 21:22:57 +00002062 netdev_for_each_mc_addr(ha, dev) {
2063 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002064 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2065 }
2066
Jamie Ilesf75ba502011-11-08 10:12:32 +00002067 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2068 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002069}
2070
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002071/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002072static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002073{
2074 unsigned long cfg;
2075 struct macb *bp = netdev_priv(dev);
2076
2077 cfg = macb_readl(bp, NCFGR);
2078
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002079 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002080 /* Enable promiscuous mode */
2081 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002082
2083 /* Disable RX checksum offload */
2084 if (macb_is_gem(bp))
2085 cfg &= ~GEM_BIT(RXCOEN);
2086 } else {
2087 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002088 cfg &= ~MACB_BIT(CAF);
2089
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002090 /* Enable RX checksum offload only if requested */
2091 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2092 cfg |= GEM_BIT(RXCOEN);
2093 }
2094
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002095 if (dev->flags & IFF_ALLMULTI) {
2096 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002097 macb_or_gem_writel(bp, HRB, -1);
2098 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002099 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002100 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002101 /* Enable specific multicasts */
2102 macb_sethashtable(dev);
2103 cfg |= MACB_BIT(NCFGR_MTI);
2104 } else if (dev->flags & (~IFF_ALLMULTI)) {
2105 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002106 macb_or_gem_writel(bp, HRB, 0);
2107 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002108 cfg &= ~MACB_BIT(NCFGR_MTI);
2109 }
2110
2111 macb_writel(bp, NCFGR, cfg);
2112}
2113
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002114static int macb_open(struct net_device *dev)
2115{
2116 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002117 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002118 int err;
2119
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002120 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002121
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002122 /* carrier starts down */
2123 netif_carrier_off(dev);
2124
frederic RODO6c36a702007-07-12 19:07:24 +02002125 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002126 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002127 return -EAGAIN;
2128
Nicolas Ferre1b447912013-06-04 21:57:11 +00002129 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002130 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002131
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002132 err = macb_alloc_consistent(bp);
2133 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002134 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2135 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002136 return err;
2137 }
2138
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002139 napi_enable(&bp->napi);
2140
Nicolas Ferre4df95132013-06-04 21:57:12 +00002141 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002142 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002143
frederic RODO6c36a702007-07-12 19:07:24 +02002144 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002145 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002146
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002147 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002148
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002149 if (bp->ptp_info)
2150 bp->ptp_info->ptp_init(dev);
2151
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002152 return 0;
2153}
2154
2155static int macb_close(struct net_device *dev)
2156{
2157 struct macb *bp = netdev_priv(dev);
2158 unsigned long flags;
2159
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002160 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002161 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002162
Philippe Reynes0a912812016-06-22 00:32:35 +02002163 if (dev->phydev)
2164 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002165
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002166 spin_lock_irqsave(&bp->lock, flags);
2167 macb_reset_hw(bp);
2168 netif_carrier_off(dev);
2169 spin_unlock_irqrestore(&bp->lock, flags);
2170
2171 macb_free_consistent(bp);
2172
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002173 if (bp->ptp_info)
2174 bp->ptp_info->ptp_remove(dev);
2175
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002176 return 0;
2177}
2178
Harini Katakama5898ea2015-05-06 22:27:18 +05302179static int macb_change_mtu(struct net_device *dev, int new_mtu)
2180{
Harini Katakama5898ea2015-05-06 22:27:18 +05302181 if (netif_running(dev))
2182 return -EBUSY;
2183
Harini Katakama5898ea2015-05-06 22:27:18 +05302184 dev->mtu = new_mtu;
2185
2186 return 0;
2187}
2188
Jamie Ilesa494ed82011-03-09 16:26:35 +00002189static void gem_update_stats(struct macb *bp)
2190{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002191 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002192 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002193
Xander Huff3ff13f12015-01-13 16:15:51 -06002194 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2195 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002196 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002197
2198 bp->ethtool_stats[i] += val;
2199 *p += val;
2200
2201 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2202 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002203 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002204 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002205 *(++p) += val;
2206 }
2207 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002208}
2209
2210static struct net_device_stats *gem_get_stats(struct macb *bp)
2211{
2212 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002213 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002214
2215 gem_update_stats(bp);
2216
2217 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2218 hwstat->rx_alignment_errors +
2219 hwstat->rx_resource_errors +
2220 hwstat->rx_overruns +
2221 hwstat->rx_oversize_frames +
2222 hwstat->rx_jabbers +
2223 hwstat->rx_undersized_frames +
2224 hwstat->rx_length_field_frame_errors);
2225 nstat->tx_errors = (hwstat->tx_late_collisions +
2226 hwstat->tx_excessive_collisions +
2227 hwstat->tx_underrun +
2228 hwstat->tx_carrier_sense_errors);
2229 nstat->multicast = hwstat->rx_multicast_frames;
2230 nstat->collisions = (hwstat->tx_single_collision_frames +
2231 hwstat->tx_multiple_collision_frames +
2232 hwstat->tx_excessive_collisions);
2233 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2234 hwstat->rx_jabbers +
2235 hwstat->rx_undersized_frames +
2236 hwstat->rx_length_field_frame_errors);
2237 nstat->rx_over_errors = hwstat->rx_resource_errors;
2238 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2239 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2240 nstat->rx_fifo_errors = hwstat->rx_overruns;
2241 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2242 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2243 nstat->tx_fifo_errors = hwstat->tx_underrun;
2244
2245 return nstat;
2246}
2247
Xander Huff3ff13f12015-01-13 16:15:51 -06002248static void gem_get_ethtool_stats(struct net_device *dev,
2249 struct ethtool_stats *stats, u64 *data)
2250{
2251 struct macb *bp;
2252
2253 bp = netdev_priv(dev);
2254 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002255 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002256}
2257
2258static int gem_get_sset_count(struct net_device *dev, int sset)
2259{
2260 switch (sset) {
2261 case ETH_SS_STATS:
2262 return GEM_STATS_LEN;
2263 default:
2264 return -EOPNOTSUPP;
2265 }
2266}
2267
2268static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2269{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002270 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002271
2272 switch (sset) {
2273 case ETH_SS_STATS:
2274 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2275 memcpy(p, gem_statistics[i].stat_string,
2276 ETH_GSTRING_LEN);
2277 break;
2278 }
2279}
2280
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002281static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002282{
2283 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002284 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002285 struct macb_stats *hwstat = &bp->hw_stats.macb;
2286
2287 if (macb_is_gem(bp))
2288 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002289
frederic RODO6c36a702007-07-12 19:07:24 +02002290 /* read stats from hardware */
2291 macb_update_stats(bp);
2292
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002293 /* Convert HW stats into netdevice stats */
2294 nstat->rx_errors = (hwstat->rx_fcs_errors +
2295 hwstat->rx_align_errors +
2296 hwstat->rx_resource_errors +
2297 hwstat->rx_overruns +
2298 hwstat->rx_oversize_pkts +
2299 hwstat->rx_jabbers +
2300 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002301 hwstat->rx_length_mismatch);
2302 nstat->tx_errors = (hwstat->tx_late_cols +
2303 hwstat->tx_excessive_cols +
2304 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002305 hwstat->tx_carrier_errors +
2306 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002307 nstat->collisions = (hwstat->tx_single_cols +
2308 hwstat->tx_multiple_cols +
2309 hwstat->tx_excessive_cols);
2310 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2311 hwstat->rx_jabbers +
2312 hwstat->rx_undersize_pkts +
2313 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002314 nstat->rx_over_errors = hwstat->rx_resource_errors +
2315 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002316 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2317 nstat->rx_frame_errors = hwstat->rx_align_errors;
2318 nstat->rx_fifo_errors = hwstat->rx_overruns;
2319 /* XXX: What does "missed" mean? */
2320 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2321 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2322 nstat->tx_fifo_errors = hwstat->tx_underruns;
2323 /* Don't know about heartbeat or window errors... */
2324
2325 return nstat;
2326}
2327
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002328static int macb_get_regs_len(struct net_device *netdev)
2329{
2330 return MACB_GREGS_NBR * sizeof(u32);
2331}
2332
2333static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2334 void *p)
2335{
2336 struct macb *bp = netdev_priv(dev);
2337 unsigned int tail, head;
2338 u32 *regs_buff = p;
2339
2340 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2341 | MACB_GREGS_VERSION;
2342
Zach Brownb410d132016-10-19 09:56:57 -05002343 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2344 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002345
2346 regs_buff[0] = macb_readl(bp, NCR);
2347 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2348 regs_buff[2] = macb_readl(bp, NSR);
2349 regs_buff[3] = macb_readl(bp, TSR);
2350 regs_buff[4] = macb_readl(bp, RBQP);
2351 regs_buff[5] = macb_readl(bp, TBQP);
2352 regs_buff[6] = macb_readl(bp, RSR);
2353 regs_buff[7] = macb_readl(bp, IMR);
2354
2355 regs_buff[8] = tail;
2356 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002357 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2358 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002359
Neil Armstrongce721a72016-01-05 14:39:16 +01002360 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2361 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002362 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002363 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002364}
2365
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002366static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2367{
2368 struct macb *bp = netdev_priv(netdev);
2369
2370 wol->supported = 0;
2371 wol->wolopts = 0;
2372
2373 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2374 wol->supported = WAKE_MAGIC;
2375
2376 if (bp->wol & MACB_WOL_ENABLED)
2377 wol->wolopts |= WAKE_MAGIC;
2378 }
2379}
2380
2381static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2382{
2383 struct macb *bp = netdev_priv(netdev);
2384
2385 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2386 (wol->wolopts & ~WAKE_MAGIC))
2387 return -EOPNOTSUPP;
2388
2389 if (wol->wolopts & WAKE_MAGIC)
2390 bp->wol |= MACB_WOL_ENABLED;
2391 else
2392 bp->wol &= ~MACB_WOL_ENABLED;
2393
2394 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2395
2396 return 0;
2397}
2398
Zach Brown8441bb32016-10-19 09:56:58 -05002399static void macb_get_ringparam(struct net_device *netdev,
2400 struct ethtool_ringparam *ring)
2401{
2402 struct macb *bp = netdev_priv(netdev);
2403
2404 ring->rx_max_pending = MAX_RX_RING_SIZE;
2405 ring->tx_max_pending = MAX_TX_RING_SIZE;
2406
2407 ring->rx_pending = bp->rx_ring_size;
2408 ring->tx_pending = bp->tx_ring_size;
2409}
2410
2411static int macb_set_ringparam(struct net_device *netdev,
2412 struct ethtool_ringparam *ring)
2413{
2414 struct macb *bp = netdev_priv(netdev);
2415 u32 new_rx_size, new_tx_size;
2416 unsigned int reset = 0;
2417
2418 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2419 return -EINVAL;
2420
2421 new_rx_size = clamp_t(u32, ring->rx_pending,
2422 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2423 new_rx_size = roundup_pow_of_two(new_rx_size);
2424
2425 new_tx_size = clamp_t(u32, ring->tx_pending,
2426 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2427 new_tx_size = roundup_pow_of_two(new_tx_size);
2428
2429 if ((new_tx_size == bp->tx_ring_size) &&
2430 (new_rx_size == bp->rx_ring_size)) {
2431 /* nothing to do */
2432 return 0;
2433 }
2434
2435 if (netif_running(bp->dev)) {
2436 reset = 1;
2437 macb_close(bp->dev);
2438 }
2439
2440 bp->rx_ring_size = new_rx_size;
2441 bp->tx_ring_size = new_tx_size;
2442
2443 if (reset)
2444 macb_open(bp->dev);
2445
2446 return 0;
2447}
2448
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002449static int macb_get_ts_info(struct net_device *netdev,
2450 struct ethtool_ts_info *info)
2451{
2452 struct macb *bp = netdev_priv(netdev);
2453
2454 if (bp->ptp_info)
2455 return bp->ptp_info->get_ts_info(netdev, info);
2456
2457 return ethtool_op_get_ts_info(netdev, info);
2458}
2459
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002460static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002461 .get_regs_len = macb_get_regs_len,
2462 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002463 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002464 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002465 .get_wol = macb_get_wol,
2466 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002467 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2468 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002469 .get_ringparam = macb_get_ringparam,
2470 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002471};
Xander Huff8cd5a562015-01-15 15:55:20 -06002472
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002473static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002474 .get_regs_len = macb_get_regs_len,
2475 .get_regs = macb_get_regs,
2476 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002477 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002478 .get_ethtool_stats = gem_get_ethtool_stats,
2479 .get_strings = gem_get_ethtool_strings,
2480 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002481 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2482 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002483 .get_ringparam = macb_get_ringparam,
2484 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002485};
2486
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002487static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002488{
Philippe Reynes0a912812016-06-22 00:32:35 +02002489 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002490 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002491
2492 if (!netif_running(dev))
2493 return -EINVAL;
2494
frederic RODO6c36a702007-07-12 19:07:24 +02002495 if (!phydev)
2496 return -ENODEV;
2497
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002498 if (!bp->ptp_info)
2499 return phy_mii_ioctl(phydev, rq, cmd);
2500
2501 switch (cmd) {
2502 case SIOCSHWTSTAMP:
2503 return bp->ptp_info->set_hwtst(dev, rq, cmd);
2504 case SIOCGHWTSTAMP:
2505 return bp->ptp_info->get_hwtst(dev, rq);
2506 default:
2507 return phy_mii_ioctl(phydev, rq, cmd);
2508 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002509}
2510
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002511static int macb_set_features(struct net_device *netdev,
2512 netdev_features_t features)
2513{
2514 struct macb *bp = netdev_priv(netdev);
2515 netdev_features_t changed = features ^ netdev->features;
2516
2517 /* TX checksum offload */
2518 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2519 u32 dmacfg;
2520
2521 dmacfg = gem_readl(bp, DMACFG);
2522 if (features & NETIF_F_HW_CSUM)
2523 dmacfg |= GEM_BIT(TXCOEN);
2524 else
2525 dmacfg &= ~GEM_BIT(TXCOEN);
2526 gem_writel(bp, DMACFG, dmacfg);
2527 }
2528
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002529 /* RX checksum offload */
2530 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2531 u32 netcfg;
2532
2533 netcfg = gem_readl(bp, NCFGR);
2534 if (features & NETIF_F_RXCSUM &&
2535 !(netdev->flags & IFF_PROMISC))
2536 netcfg |= GEM_BIT(RXCOEN);
2537 else
2538 netcfg &= ~GEM_BIT(RXCOEN);
2539 gem_writel(bp, NCFGR, netcfg);
2540 }
2541
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002542 return 0;
2543}
2544
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002545static const struct net_device_ops macb_netdev_ops = {
2546 .ndo_open = macb_open,
2547 .ndo_stop = macb_close,
2548 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002549 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002550 .ndo_get_stats = macb_get_stats,
2551 .ndo_do_ioctl = macb_ioctl,
2552 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302553 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002554 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002555#ifdef CONFIG_NET_POLL_CONTROLLER
2556 .ndo_poll_controller = macb_poll_controller,
2557#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002558 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002559 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002560};
2561
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002562/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002563 * and integration options used
2564 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002565static void macb_configure_caps(struct macb *bp,
2566 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002567{
2568 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002569
Nicolas Ferref6970502015-03-31 15:02:01 +02002570 if (dt_conf)
2571 bp->caps = dt_conf->caps;
2572
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002573 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002574 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2575
Nicolas Ferree1755872014-07-24 13:50:58 +02002576 dcfg = gem_readl(bp, DCFG1);
2577 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2578 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2579 dcfg = gem_readl(bp, DCFG2);
2580 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2581 bp->caps |= MACB_CAPS_FIFO_MODE;
2582 }
2583
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002584 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002585}
2586
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002587static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002588 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002589 unsigned int *queue_mask,
2590 unsigned int *num_queues)
2591{
2592 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002593
2594 *queue_mask = 0x1;
2595 *num_queues = 1;
2596
Nicolas Ferreda120112015-03-31 15:02:00 +02002597 /* is it macb or gem ?
2598 *
2599 * We need to read directly from the hardware here because
2600 * we are early in the probe process and don't have the
2601 * MACB_CAPS_MACB_IS_GEM flag positioned
2602 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002603 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002604 return;
2605
2606 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302607 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2608
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002609 *queue_mask |= 0x1;
2610
2611 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2612 if (*queue_mask & (1 << hw_q))
2613 (*num_queues)++;
2614}
2615
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002616static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302617 struct clk **hclk, struct clk **tx_clk,
2618 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002619{
Bartosz Folta83a77e92016-12-14 06:39:15 +00002620 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002621 int err;
2622
Bartosz Folta83a77e92016-12-14 06:39:15 +00002623 pdata = dev_get_platdata(&pdev->dev);
2624 if (pdata) {
2625 *pclk = pdata->pclk;
2626 *hclk = pdata->hclk;
2627 } else {
2628 *pclk = devm_clk_get(&pdev->dev, "pclk");
2629 *hclk = devm_clk_get(&pdev->dev, "hclk");
2630 }
2631
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002632 if (IS_ERR(*pclk)) {
2633 err = PTR_ERR(*pclk);
2634 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2635 return err;
2636 }
2637
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002638 if (IS_ERR(*hclk)) {
2639 err = PTR_ERR(*hclk);
2640 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2641 return err;
2642 }
2643
2644 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2645 if (IS_ERR(*tx_clk))
2646 *tx_clk = NULL;
2647
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302648 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2649 if (IS_ERR(*rx_clk))
2650 *rx_clk = NULL;
2651
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002652 err = clk_prepare_enable(*pclk);
2653 if (err) {
2654 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2655 return err;
2656 }
2657
2658 err = clk_prepare_enable(*hclk);
2659 if (err) {
2660 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2661 goto err_disable_pclk;
2662 }
2663
2664 err = clk_prepare_enable(*tx_clk);
2665 if (err) {
2666 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2667 goto err_disable_hclk;
2668 }
2669
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302670 err = clk_prepare_enable(*rx_clk);
2671 if (err) {
2672 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2673 goto err_disable_txclk;
2674 }
2675
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002676 return 0;
2677
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302678err_disable_txclk:
2679 clk_disable_unprepare(*tx_clk);
2680
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002681err_disable_hclk:
2682 clk_disable_unprepare(*hclk);
2683
2684err_disable_pclk:
2685 clk_disable_unprepare(*pclk);
2686
2687 return err;
2688}
2689
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002690static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002691{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002692 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002693 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002694 struct macb *bp = netdev_priv(dev);
2695 struct macb_queue *queue;
2696 int err;
2697 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002698
Zach Brownb410d132016-10-19 09:56:57 -05002699 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2700 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2701
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002702 /* set the queue register mapping once for all: queue0 has a special
2703 * register mapping but we don't want to test the queue index then
2704 * compute the corresponding register offset at run time.
2705 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002706 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002707 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002708 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002709
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002710 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002711 queue->bp = bp;
2712 if (hw_q) {
2713 queue->ISR = GEM_ISR(hw_q - 1);
2714 queue->IER = GEM_IER(hw_q - 1);
2715 queue->IDR = GEM_IDR(hw_q - 1);
2716 queue->IMR = GEM_IMR(hw_q - 1);
2717 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302718#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002719 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2720 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302721#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002722 } else {
2723 /* queue0 uses legacy registers */
2724 queue->ISR = MACB_ISR;
2725 queue->IER = MACB_IER;
2726 queue->IDR = MACB_IDR;
2727 queue->IMR = MACB_IMR;
2728 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302729#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002730 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2731 queue->TBQPH = MACB_TBQPH;
Harini Katakamfff80192016-08-09 13:15:53 +05302732#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002733 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002734
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002735 /* get irq: here we use the linux queue index, not the hardware
2736 * queue index. the queue irq definitions in the device tree
2737 * must remove the optional gaps that could exist in the
2738 * hardware queue mask.
2739 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002740 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002741 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002742 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002743 if (err) {
2744 dev_err(&pdev->dev,
2745 "Unable to request IRQ %d (error %d)\n",
2746 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002747 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002748 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002749
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002750 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002751 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002752 }
2753
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002754 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002755 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002756
Nicolas Ferre4df95132013-06-04 21:57:12 +00002757 /* setup appropriated routines according to adapter type */
2758 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002759 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002760 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2761 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2762 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2763 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002764 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002765 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002766 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002767 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2768 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2769 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2770 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002771 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002772 }
2773
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002774 /* Set features */
2775 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002776
2777 /* Check LSO capability */
2778 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
2779 dev->hw_features |= MACB_NETIF_LSO;
2780
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002781 /* Checksum offload is only available on gem with packet buffer */
2782 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002783 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002784 if (bp->caps & MACB_CAPS_SG_DISABLED)
2785 dev->hw_features &= ~NETIF_F_SG;
2786 dev->features = dev->hw_features;
2787
Neil Armstrongce721a72016-01-05 14:39:16 +01002788 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2789 val = 0;
2790 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2791 val = GEM_BIT(RGMII);
2792 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002793 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002794 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002795 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002796 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002797
Neil Armstrongce721a72016-01-05 14:39:16 +01002798 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2799 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002800
Neil Armstrongce721a72016-01-05 14:39:16 +01002801 macb_or_gem_writel(bp, USRIO, val);
2802 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002803
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002804 /* Set MII management clock divider */
2805 val = macb_mdc_clk_div(bp);
2806 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302807 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2808 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002809 macb_writel(bp, NCFGR, val);
2810
2811 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002812}
2813
2814#if defined(CONFIG_OF)
2815/* 1518 rounded up */
2816#define AT91ETHER_MAX_RBUFF_SZ 0x600
2817/* max number of receive buffers */
2818#define AT91ETHER_MAX_RX_DESCR 9
2819
2820/* Initialize and start the Receiver and Transmit subsystems */
2821static int at91ether_start(struct net_device *dev)
2822{
2823 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002824 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002825 dma_addr_t addr;
2826 u32 ctl;
2827 int i;
2828
2829 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2830 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002831 macb_dma_desc_get_size(lp)),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002832 &lp->rx_ring_dma, GFP_KERNEL);
2833 if (!lp->rx_ring)
2834 return -ENOMEM;
2835
2836 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2837 AT91ETHER_MAX_RX_DESCR *
2838 AT91ETHER_MAX_RBUFF_SZ,
2839 &lp->rx_buffers_dma, GFP_KERNEL);
2840 if (!lp->rx_buffers) {
2841 dma_free_coherent(&lp->pdev->dev,
2842 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002843 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002844 lp->rx_ring, lp->rx_ring_dma);
2845 lp->rx_ring = NULL;
2846 return -ENOMEM;
2847 }
2848
2849 addr = lp->rx_buffers_dma;
2850 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002851 desc = macb_rx_desc(lp, i);
2852 macb_set_addr(lp, desc, addr);
2853 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002854 addr += AT91ETHER_MAX_RBUFF_SZ;
2855 }
2856
2857 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002858 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002859
2860 /* Reset buffer index */
2861 lp->rx_tail = 0;
2862
2863 /* Program address of descriptor list in Rx Buffer Queue register */
2864 macb_writel(lp, RBQP, lp->rx_ring_dma);
2865
2866 /* Enable Receive and Transmit */
2867 ctl = macb_readl(lp, NCR);
2868 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2869
2870 return 0;
2871}
2872
2873/* Open the ethernet interface */
2874static int at91ether_open(struct net_device *dev)
2875{
2876 struct macb *lp = netdev_priv(dev);
2877 u32 ctl;
2878 int ret;
2879
2880 /* Clear internal statistics */
2881 ctl = macb_readl(lp, NCR);
2882 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2883
2884 macb_set_hwaddr(lp);
2885
2886 ret = at91ether_start(dev);
2887 if (ret)
2888 return ret;
2889
2890 /* Enable MAC interrupts */
2891 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2892 MACB_BIT(RXUBR) |
2893 MACB_BIT(ISR_TUND) |
2894 MACB_BIT(ISR_RLE) |
2895 MACB_BIT(TCOMP) |
2896 MACB_BIT(ISR_ROVR) |
2897 MACB_BIT(HRESP));
2898
2899 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002900 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002901
2902 netif_start_queue(dev);
2903
2904 return 0;
2905}
2906
2907/* Close the interface */
2908static int at91ether_close(struct net_device *dev)
2909{
2910 struct macb *lp = netdev_priv(dev);
2911 u32 ctl;
2912
2913 /* Disable Receiver and Transmitter */
2914 ctl = macb_readl(lp, NCR);
2915 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2916
2917 /* Disable MAC interrupts */
2918 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2919 MACB_BIT(RXUBR) |
2920 MACB_BIT(ISR_TUND) |
2921 MACB_BIT(ISR_RLE) |
2922 MACB_BIT(TCOMP) |
2923 MACB_BIT(ISR_ROVR) |
2924 MACB_BIT(HRESP));
2925
2926 netif_stop_queue(dev);
2927
2928 dma_free_coherent(&lp->pdev->dev,
2929 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002930 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002931 lp->rx_ring, lp->rx_ring_dma);
2932 lp->rx_ring = NULL;
2933
2934 dma_free_coherent(&lp->pdev->dev,
2935 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2936 lp->rx_buffers, lp->rx_buffers_dma);
2937 lp->rx_buffers = NULL;
2938
2939 return 0;
2940}
2941
2942/* Transmit packet */
2943static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2944{
2945 struct macb *lp = netdev_priv(dev);
2946
2947 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2948 netif_stop_queue(dev);
2949
2950 /* Store packet information (to free when Tx completed) */
2951 lp->skb = skb;
2952 lp->skb_length = skb->len;
2953 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2954 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03002955 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
2956 dev_kfree_skb_any(skb);
2957 dev->stats.tx_dropped++;
2958 netdev_err(dev, "%s: DMA mapping error\n", __func__);
2959 return NETDEV_TX_OK;
2960 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002961
2962 /* Set address of the data in the Transmit Address register */
2963 macb_writel(lp, TAR, lp->skb_physaddr);
2964 /* Set length of the packet in the Transmit Control register */
2965 macb_writel(lp, TCR, skb->len);
2966
2967 } else {
2968 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2969 return NETDEV_TX_BUSY;
2970 }
2971
2972 return NETDEV_TX_OK;
2973}
2974
2975/* Extract received frame from buffer descriptors and sent to upper layers.
2976 * (Called from interrupt context)
2977 */
2978static void at91ether_rx(struct net_device *dev)
2979{
2980 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002981 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002982 unsigned char *p_recv;
2983 struct sk_buff *skb;
2984 unsigned int pktlen;
2985
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002986 desc = macb_rx_desc(lp, lp->rx_tail);
2987 while (desc->addr & MACB_BIT(RX_USED)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002988 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002989 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002990 skb = netdev_alloc_skb(dev, pktlen + 2);
2991 if (skb) {
2992 skb_reserve(skb, 2);
2993 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2994
2995 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002996 dev->stats.rx_packets++;
2997 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002998 netif_rx(skb);
2999 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003000 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003001 }
3002
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003003 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003004 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003005
3006 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003007 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003008
3009 /* wrap after last buffer */
3010 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3011 lp->rx_tail = 0;
3012 else
3013 lp->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003014
3015 desc = macb_rx_desc(lp, lp->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003016 }
3017}
3018
3019/* MAC interrupt handler */
3020static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3021{
3022 struct net_device *dev = dev_id;
3023 struct macb *lp = netdev_priv(dev);
3024 u32 intstatus, ctl;
3025
3026 /* MAC Interrupt Status register indicates what interrupts are pending.
3027 * It is automatically cleared once read.
3028 */
3029 intstatus = macb_readl(lp, ISR);
3030
3031 /* Receive complete */
3032 if (intstatus & MACB_BIT(RCOMP))
3033 at91ether_rx(dev);
3034
3035 /* Transmit complete */
3036 if (intstatus & MACB_BIT(TCOMP)) {
3037 /* The TCOM bit is set even if the transmission failed */
3038 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003039 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003040
3041 if (lp->skb) {
3042 dev_kfree_skb_irq(lp->skb);
3043 lp->skb = NULL;
3044 dma_unmap_single(NULL, lp->skb_physaddr,
3045 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003046 dev->stats.tx_packets++;
3047 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003048 }
3049 netif_wake_queue(dev);
3050 }
3051
3052 /* Work-around for EMAC Errata section 41.3.1 */
3053 if (intstatus & MACB_BIT(RXUBR)) {
3054 ctl = macb_readl(lp, NCR);
3055 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003056 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003057 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3058 }
3059
3060 if (intstatus & MACB_BIT(ISR_ROVR))
3061 netdev_err(dev, "ROVR error\n");
3062
3063 return IRQ_HANDLED;
3064}
3065
3066#ifdef CONFIG_NET_POLL_CONTROLLER
3067static void at91ether_poll_controller(struct net_device *dev)
3068{
3069 unsigned long flags;
3070
3071 local_irq_save(flags);
3072 at91ether_interrupt(dev->irq, dev);
3073 local_irq_restore(flags);
3074}
3075#endif
3076
3077static const struct net_device_ops at91ether_netdev_ops = {
3078 .ndo_open = at91ether_open,
3079 .ndo_stop = at91ether_close,
3080 .ndo_start_xmit = at91ether_start_xmit,
3081 .ndo_get_stats = macb_get_stats,
3082 .ndo_set_rx_mode = macb_set_rx_mode,
3083 .ndo_set_mac_address = eth_mac_addr,
3084 .ndo_do_ioctl = macb_ioctl,
3085 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003086#ifdef CONFIG_NET_POLL_CONTROLLER
3087 .ndo_poll_controller = at91ether_poll_controller,
3088#endif
3089};
3090
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003091static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303092 struct clk **hclk, struct clk **tx_clk,
3093 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003094{
3095 int err;
3096
3097 *hclk = NULL;
3098 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303099 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003100
3101 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3102 if (IS_ERR(*pclk))
3103 return PTR_ERR(*pclk);
3104
3105 err = clk_prepare_enable(*pclk);
3106 if (err) {
3107 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3108 return err;
3109 }
3110
3111 return 0;
3112}
3113
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003114static int at91ether_init(struct platform_device *pdev)
3115{
3116 struct net_device *dev = platform_get_drvdata(pdev);
3117 struct macb *bp = netdev_priv(dev);
3118 int err;
3119 u32 reg;
3120
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003121 dev->netdev_ops = &at91ether_netdev_ops;
3122 dev->ethtool_ops = &macb_ethtool_ops;
3123
3124 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3125 0, dev->name, dev);
3126 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003127 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003128
3129 macb_writel(bp, NCR, 0);
3130
3131 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3132 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3133 reg |= MACB_BIT(RM9200_RMII);
3134
3135 macb_writel(bp, NCFGR, reg);
3136
3137 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003138}
3139
David S. Miller3cef5c52015-03-09 23:38:02 -04003140static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003141 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003142 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003143 .init = macb_init,
3144};
3145
David S. Miller3cef5c52015-03-09 23:38:02 -04003146static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003147 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3148 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003149 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003150 .init = macb_init,
3151};
3152
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003153static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003154 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003155 .dma_burst_length = 16,
3156 .clk_init = macb_clk_init,
3157 .init = macb_init,
3158};
3159
David S. Miller3cef5c52015-03-09 23:38:02 -04003160static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003161 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3162 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003163 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003164 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003165 .init = macb_init,
3166};
3167
David S. Miller3cef5c52015-03-09 23:38:02 -04003168static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003169 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003170 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003171 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003172 .init = macb_init,
3173};
3174
David S. Miller3cef5c52015-03-09 23:38:02 -04003175static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003176 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003177 .init = at91ether_init,
3178};
3179
Neil Armstronge611b5b2016-01-05 14:39:17 +01003180static const struct macb_config np4_config = {
3181 .caps = MACB_CAPS_USRIO_DISABLED,
3182 .clk_init = macb_clk_init,
3183 .init = macb_init,
3184};
David S. Miller36583eb2015-05-23 01:22:35 -04003185
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303186static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303187 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303188 .dma_burst_length = 16,
3189 .clk_init = macb_clk_init,
3190 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303191 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303192};
3193
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003194static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303195 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003196 .dma_burst_length = 16,
3197 .clk_init = macb_clk_init,
3198 .init = macb_init,
3199};
3200
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003201static const struct of_device_id macb_dt_ids[] = {
3202 { .compatible = "cdns,at32ap7000-macb" },
3203 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3204 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003205 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003206 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3207 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003208 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003209 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3210 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3211 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3212 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303213 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003214 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003215 { /* sentinel */ }
3216};
3217MODULE_DEVICE_TABLE(of, macb_dt_ids);
3218#endif /* CONFIG_OF */
3219
Bartosz Folta83a77e92016-12-14 06:39:15 +00003220static const struct macb_config default_gem_config = {
3221 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
3222 .dma_burst_length = 16,
3223 .clk_init = macb_clk_init,
3224 .init = macb_init,
3225 .jumbo_max_len = 10240,
3226};
3227
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003228static int macb_probe(struct platform_device *pdev)
3229{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003230 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003231 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303232 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003233 = macb_config->clk_init;
3234 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003235 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003236 struct device_node *phy_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303237 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003238 unsigned int queue_mask, num_queues;
3239 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003240 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003241 struct phy_device *phydev;
3242 struct net_device *dev;
3243 struct resource *regs;
3244 void __iomem *mem;
3245 const char *mac;
3246 struct macb *bp;
3247 int err;
3248
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003249 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3250 mem = devm_ioremap_resource(&pdev->dev, regs);
3251 if (IS_ERR(mem))
3252 return PTR_ERR(mem);
3253
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003254 if (np) {
3255 const struct of_device_id *match;
3256
3257 match = of_match_node(macb_dt_ids, np);
3258 if (match && match->data) {
3259 macb_config = match->data;
3260 clk_init = macb_config->clk_init;
3261 init = macb_config->init;
3262 }
3263 }
3264
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303265 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003266 if (err)
3267 return err;
3268
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003269 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003270
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003271 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003272 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003273 if (!dev) {
3274 err = -ENOMEM;
3275 goto err_disable_clocks;
3276 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003277
3278 dev->base_addr = regs->start;
3279
3280 SET_NETDEV_DEV(dev, &pdev->dev);
3281
3282 bp = netdev_priv(dev);
3283 bp->pdev = pdev;
3284 bp->dev = dev;
3285 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003286 bp->native_io = native_io;
3287 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003288 bp->macb_reg_readl = hw_readl_native;
3289 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003290 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003291 bp->macb_reg_readl = hw_readl;
3292 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003293 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003294 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003295 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003296 if (macb_config)
3297 bp->dma_burst_length = macb_config->dma_burst_length;
3298 bp->pclk = pclk;
3299 bp->hclk = hclk;
3300 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303301 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003302 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303303 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303304
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003305 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003306 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003307 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3308 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3309
Harini Katakamfff80192016-08-09 13:15:53 +05303310#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003311 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
Harini Katakamfff80192016-08-09 13:15:53 +05303312 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003313 bp->hw_dma_cap = HW_DMA_CAP_64B;
3314 } else
3315 bp->hw_dma_cap = HW_DMA_CAP_32B;
Harini Katakamfff80192016-08-09 13:15:53 +05303316#endif
3317
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003318 spin_lock_init(&bp->lock);
3319
Nicolas Ferread783472015-03-31 15:02:02 +02003320 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003321 macb_configure_caps(bp, macb_config);
3322
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003323 platform_set_drvdata(pdev, dev);
3324
3325 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003326 if (dev->irq < 0) {
3327 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003328 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003329 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003330
Jarod Wilson44770e12016-10-17 15:54:17 -04003331 /* MTU range: 68 - 1500 or 10240 */
3332 dev->min_mtu = GEM_MTU_MIN_SIZE;
3333 if (bp->caps & MACB_CAPS_JUMBO)
3334 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3335 else
3336 dev->max_mtu = ETH_DATA_LEN;
3337
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003338 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003339 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003340 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003341 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003342 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003343
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003344 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003345 phy_node = of_get_next_available_child(np, NULL);
3346 if (phy_node) {
3347 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003348
Charles Keepax0e3e7992016-03-28 13:47:42 +01003349 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003350 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003351 gpiod_direction_output(bp->reset_gpio, 1);
3352 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003353 }
3354 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003355
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003356 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003357 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003358 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003359 if (pdata && pdata->is_rmii)
3360 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3361 else
3362 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3363 } else {
3364 bp->phy_interface = err;
3365 }
3366
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003367 /* IP specific init */
3368 err = init(pdev);
3369 if (err)
3370 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003371
Florian Fainellicf669662016-05-02 18:38:45 -07003372 err = macb_mii_init(bp);
3373 if (err)
3374 goto err_out_free_netdev;
3375
Philippe Reynes0a912812016-06-22 00:32:35 +02003376 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003377
3378 netif_carrier_off(dev);
3379
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003380 err = register_netdev(dev);
3381 if (err) {
3382 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003383 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003384 }
3385
Florian Fainellicf669662016-05-02 18:38:45 -07003386 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003387
Bo Shen58798232014-09-13 01:57:49 +02003388 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3389 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3390 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003391
3392 return 0;
3393
Florian Fainellicf669662016-05-02 18:38:45 -07003394err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003395 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003396 mdiobus_unregister(bp->mii_bus);
3397 mdiobus_free(bp->mii_bus);
3398
3399 /* Shutdown the PHY if there is a GPIO reset */
3400 if (bp->reset_gpio)
3401 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003402
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003403err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003404 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003405
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003406err_disable_clocks:
3407 clk_disable_unprepare(tx_clk);
3408 clk_disable_unprepare(hclk);
3409 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303410 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003411
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003412 return err;
3413}
3414
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003415static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003416{
3417 struct net_device *dev;
3418 struct macb *bp;
3419
3420 dev = platform_get_drvdata(pdev);
3421
3422 if (dev) {
3423 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003424 if (dev->phydev)
3425 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003426 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003427 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003428 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003429
3430 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003431 if (bp->reset_gpio)
3432 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003433
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003434 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003435 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003436 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003437 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303438 clk_disable_unprepare(bp->rx_clk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003439 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003440 }
3441
3442 return 0;
3443}
3444
Michal Simekd23823d2015-01-23 09:36:03 +01003445static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003446{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003447 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003448 struct net_device *netdev = platform_get_drvdata(pdev);
3449 struct macb *bp = netdev_priv(netdev);
3450
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003451 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003452 netif_device_detach(netdev);
3453
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003454 if (bp->wol & MACB_WOL_ENABLED) {
3455 macb_writel(bp, IER, MACB_BIT(WOL));
3456 macb_writel(bp, WOL, MACB_BIT(MAG));
3457 enable_irq_wake(bp->queues[0].irq);
3458 } else {
3459 clk_disable_unprepare(bp->tx_clk);
3460 clk_disable_unprepare(bp->hclk);
3461 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303462 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003463 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003464
3465 return 0;
3466}
3467
Michal Simekd23823d2015-01-23 09:36:03 +01003468static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003469{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003470 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003471 struct net_device *netdev = platform_get_drvdata(pdev);
3472 struct macb *bp = netdev_priv(netdev);
3473
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003474 if (bp->wol & MACB_WOL_ENABLED) {
3475 macb_writel(bp, IDR, MACB_BIT(WOL));
3476 macb_writel(bp, WOL, 0);
3477 disable_irq_wake(bp->queues[0].irq);
3478 } else {
3479 clk_prepare_enable(bp->pclk);
3480 clk_prepare_enable(bp->hclk);
3481 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303482 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003483 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003484
3485 netif_device_attach(netdev);
3486
3487 return 0;
3488}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003489
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003490static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3491
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003492static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003493 .probe = macb_probe,
3494 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003495 .driver = {
3496 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003497 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003498 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003499 },
3500};
3501
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003502module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003503
3504MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003505MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003506MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003507MODULE_ALIAS("platform:macb");