blob: 91f7492623d3f1d614afeb7f525ecfb491e5965e [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);
Alexandre Belloniae3696c2017-04-26 12:06:28 +0200435 if (pdata) {
436 if (gpio_is_valid(pdata->phy_irq_pin)) {
437 ret = devm_gpio_request(&bp->pdev->dev,
438 pdata->phy_irq_pin, "phy int");
439 if (!ret) {
440 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
441 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
442 }
443 } else {
444 phydev->irq = PHY_POLL;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000445 }
446 }
frederic RODO6c36a702007-07-12 19:07:24 +0200447
448 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000449 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100450 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000451 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000452 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000453 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200454 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100455
frederic RODO6c36a702007-07-12 19:07:24 +0200456 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200457 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000458 phydev->supported &= PHY_GBIT_FEATURES;
459 else
460 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100461
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500462 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
463 phydev->supported &= ~SUPPORTED_1000baseT_Half;
464
frederic RODO6c36a702007-07-12 19:07:24 +0200465 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100466
frederic RODO6c36a702007-07-12 19:07:24 +0200467 bp->link = 0;
468 bp->speed = 0;
469 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200470
471 return 0;
472}
473
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100474static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200475{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000476 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200477 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200478 int err = -ENXIO, i;
479
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200480 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200481 macb_writel(bp, NCR, MACB_BIT(MPE));
482
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700483 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700484 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200485 err = -ENOMEM;
486 goto err_out;
487 }
488
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700489 bp->mii_bus->name = "MACB_mii_bus";
490 bp->mii_bus->read = &macb_mdio_read;
491 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000492 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700493 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700494 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700495 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900496 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700497
Jamie Iles91523942011-02-28 04:05:25 +0000498 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200499
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200500 np = bp->pdev->dev.of_node;
501 if (np) {
502 /* try dt phy registration */
503 err = of_mdiobus_register(bp->mii_bus, np);
504
505 /* fallback to standard phy registration if no phy were
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700506 * found during dt phy registration
507 */
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200508 if (!err && !phy_find_first(bp->mii_bus)) {
509 for (i = 0; i < PHY_MAX_ADDR; i++) {
510 struct phy_device *phydev;
511
512 phydev = mdiobus_scan(bp->mii_bus, i);
Sergei Shtylyovce24c2b2016-05-01 01:47:36 +0300513 if (IS_ERR(phydev) &&
514 PTR_ERR(phydev) != -ENODEV) {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200515 err = PTR_ERR(phydev);
516 break;
517 }
518 }
519
520 if (err)
521 goto err_out_unregister_bus;
522 }
523 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000524 for (i = 0; i < PHY_MAX_ADDR; i++)
525 bp->mii_bus->irq[i] = PHY_POLL;
526
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200527 if (pdata)
528 bp->mii_bus->phy_mask = pdata->phy_mask;
529
530 err = mdiobus_register(bp->mii_bus);
531 }
532
533 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100534 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200535
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200536 err = macb_mii_probe(bp->dev);
537 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200538 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200539
540 return 0;
541
542err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700543 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700544err_out_free_mdiobus:
545 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200546err_out:
547 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100548}
549
550static void macb_update_stats(struct macb *bp)
551{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000552 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
553 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300554 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100555
556 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
557
Moritz Fischer96ec6312016-03-29 19:11:11 -0700558 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700559 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100560}
561
Nicolas Ferree86cd532012-10-31 06:04:57 +0000562static int macb_halt_tx(struct macb *bp)
563{
564 unsigned long halt_time, timeout;
565 u32 status;
566
567 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
568
569 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
570 do {
571 halt_time = jiffies;
572 status = macb_readl(bp, TSR);
573 if (!(status & MACB_BIT(TGO)))
574 return 0;
575
576 usleep_range(10, 250);
577 } while (time_before(halt_time, timeout));
578
579 return -ETIMEDOUT;
580}
581
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200582static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
583{
584 if (tx_skb->mapping) {
585 if (tx_skb->mapped_as_page)
586 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
587 tx_skb->size, DMA_TO_DEVICE);
588 else
589 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
590 tx_skb->size, DMA_TO_DEVICE);
591 tx_skb->mapping = 0;
592 }
593
594 if (tx_skb->skb) {
595 dev_kfree_skb_any(tx_skb->skb);
596 tx_skb->skb = NULL;
597 }
598}
599
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000600static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
Harini Katakamfff80192016-08-09 13:15:53 +0530601{
Harini Katakamfff80192016-08-09 13:15:53 +0530602#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000603 struct macb_dma_desc_64 *desc_64;
604
605 if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
606 desc_64 = macb_64b_desc(bp, desc);
607 desc_64->addrh = upper_32_bits(addr);
608 }
Harini Katakamfff80192016-08-09 13:15:53 +0530609#endif
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000610 desc->addr = lower_32_bits(addr);
611}
612
613static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
614{
615 dma_addr_t addr = 0;
616#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
617 struct macb_dma_desc_64 *desc_64;
618
619 if (bp->hw_dma_cap == HW_DMA_CAP_64B) {
620 desc_64 = macb_64b_desc(bp, desc);
621 addr = ((u64)(desc_64->addrh) << 32);
622 }
623#endif
624 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
625 return addr;
Harini Katakamfff80192016-08-09 13:15:53 +0530626}
627
Nicolas Ferree86cd532012-10-31 06:04:57 +0000628static void macb_tx_error_task(struct work_struct *work)
629{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100630 struct macb_queue *queue = container_of(work, struct macb_queue,
631 tx_error_task);
632 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000633 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100634 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000635 struct sk_buff *skb;
636 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100637 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000638
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100639 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
640 (unsigned int)(queue - bp->queues),
641 queue->tx_tail, queue->tx_head);
642
643 /* Prevent the queue IRQ handlers from running: each of them may call
644 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
645 * As explained below, we have to halt the transmission before updating
646 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
647 * network engine about the macb/gem being halted.
648 */
649 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000650
651 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100652 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000653
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700654 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000655 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100656 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000657 */
658 if (macb_halt_tx(bp))
659 /* Just complain for now, reinitializing TX path can be good */
660 netdev_err(bp->dev, "BUG: halt tx timed out\n");
661
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700662 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000663 * Free transmit buffers in upper layer.
664 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100665 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
666 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000667
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100668 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000669 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100670 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000671 skb = tx_skb->skb;
672
673 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200674 /* skb is set for the last buffer of the frame */
675 while (!skb) {
676 macb_tx_unmap(bp, tx_skb);
677 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100678 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200679 skb = tx_skb->skb;
680 }
681
682 /* ctrl still refers to the first buffer descriptor
683 * since it's the only one written back by the hardware
684 */
685 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
686 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500687 macb_tx_ring_wrap(bp, tail),
688 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200689 bp->dev->stats.tx_packets++;
690 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200691 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000692 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700693 /* "Buffers exhausted mid-frame" errors may only happen
694 * if the driver is buggy, so complain loudly about
695 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000696 */
697 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
698 netdev_err(bp->dev,
699 "BUG: TX buffers exhausted mid-frame\n");
700
701 desc->ctrl = ctrl | MACB_BIT(TX_USED);
702 }
703
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200704 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000705 }
706
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100707 /* Set end of TX queue */
708 desc = macb_tx_desc(queue, 0);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000709 macb_set_addr(bp, desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100710 desc->ctrl = MACB_BIT(TX_USED);
711
Nicolas Ferree86cd532012-10-31 06:04:57 +0000712 /* Make descriptor updates visible to hardware */
713 wmb();
714
715 /* Reinitialize the TX desc queue */
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000716 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530717#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000718 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
719 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +0530720#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000721 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100722 queue->tx_head = 0;
723 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000724
725 /* Housework before enabling TX IRQ */
726 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100727 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
728
729 /* Now we are ready to start transmission again */
730 netif_tx_start_all_queues(bp->dev);
731 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
732
733 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000734}
735
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100736static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100737{
738 unsigned int tail;
739 unsigned int head;
740 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100741 struct macb *bp = queue->bp;
742 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100743
744 status = macb_readl(bp, TSR);
745 macb_writel(bp, TSR, status);
746
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000747 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100748 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000749
Nicolas Ferree86cd532012-10-31 06:04:57 +0000750 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700751 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100752
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100753 head = queue->tx_head;
754 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000755 struct macb_tx_skb *tx_skb;
756 struct sk_buff *skb;
757 struct macb_dma_desc *desc;
758 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100759
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100760 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100761
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000762 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100763 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000764
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000765 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100766
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200767 /* TX_USED bit is only set by hardware on the very first buffer
768 * descriptor of the transmitted frame.
769 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000770 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100771 break;
772
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200773 /* Process all buffers of the current transmitted frame */
774 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100775 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200776 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000777
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200778 /* First, update TX stats if needed */
779 if (skb) {
780 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500781 macb_tx_ring_wrap(bp, tail),
782 skb->data);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200783 bp->dev->stats.tx_packets++;
784 bp->dev->stats.tx_bytes += skb->len;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200785 }
786
787 /* Now we can safely release resources */
788 macb_tx_unmap(bp, tx_skb);
789
790 /* skb is set only for the last buffer of the frame.
791 * WARNING: at this point skb has been freed by
792 * macb_tx_unmap().
793 */
794 if (skb)
795 break;
796 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100797 }
798
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100799 queue->tx_tail = tail;
800 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
801 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500802 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100803 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100804}
805
Nicolas Ferre4df95132013-06-04 21:57:12 +0000806static void gem_rx_refill(struct macb *bp)
807{
808 unsigned int entry;
809 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000810 dma_addr_t paddr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000811 struct macb_dma_desc *desc;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000812
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700813 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500814 bp->rx_ring_size) > 0) {
815 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000816
817 /* Make hw descriptor updates visible to CPU */
818 rmb();
819
Nicolas Ferre4df95132013-06-04 21:57:12 +0000820 bp->rx_prepared_head++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000821 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000822
Moritz Fischeraa50b552016-03-29 19:11:13 -0700823 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000824 /* allocate sk_buff for this free entry in ring */
825 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700826 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000827 netdev_err(bp->dev,
828 "Unable to allocate sk_buff\n");
829 break;
830 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000831
832 /* now fill corresponding descriptor entry */
833 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700834 bp->rx_buffer_size,
835 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800836 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
837 dev_kfree_skb(skb);
838 break;
839 }
840
841 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000842
Zach Brownb410d132016-10-19 09:56:57 -0500843 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000844 paddr |= MACB_BIT(RX_WRAP);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000845 macb_set_addr(bp, desc, paddr);
846 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000847
848 /* properly align Ethernet header */
849 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530850 } else {
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000851 desc->addr &= ~MACB_BIT(RX_USED);
852 desc->ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000853 }
854 }
855
856 /* Make descriptor updates visible to hardware */
857 wmb();
858
859 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700860 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000861}
862
863/* Mark DMA descriptors from begin up to and not including end as unused */
864static void discard_partial_frame(struct macb *bp, unsigned int begin,
865 unsigned int end)
866{
867 unsigned int frag;
868
869 for (frag = begin; frag != end; frag++) {
870 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700871
Nicolas Ferre4df95132013-06-04 21:57:12 +0000872 desc->addr &= ~MACB_BIT(RX_USED);
873 }
874
875 /* Make descriptor updates visible to hardware */
876 wmb();
877
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700878 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000879 * whatever caused this is updated, so we don't have to record
880 * anything.
881 */
882}
883
884static int gem_rx(struct macb *bp, int budget)
885{
886 unsigned int len;
887 unsigned int entry;
888 struct sk_buff *skb;
889 struct macb_dma_desc *desc;
890 int count = 0;
891
892 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530893 u32 ctrl;
894 dma_addr_t addr;
895 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000896
Zach Brownb410d132016-10-19 09:56:57 -0500897 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000898 desc = macb_rx_desc(bp, entry);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000899
900 /* Make hw descriptor updates visible to CPU */
901 rmb();
902
Harini Katakamfff80192016-08-09 13:15:53 +0530903 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
Rafal Ozieblodc97a892017-01-27 15:08:20 +0000904 addr = macb_get_addr(bp, desc);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000905 ctrl = desc->ctrl;
906
Harini Katakamfff80192016-08-09 13:15:53 +0530907 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000908 break;
909
Nicolas Ferre4df95132013-06-04 21:57:12 +0000910 bp->rx_tail++;
911 count++;
912
913 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
914 netdev_err(bp->dev,
915 "not whole frame pointed by descriptor\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200916 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000917 break;
918 }
919 skb = bp->rx_skbuff[entry];
920 if (unlikely(!skb)) {
921 netdev_err(bp->dev,
922 "inconsistent Rx descriptor chain\n");
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200923 bp->dev->stats.rx_dropped++;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000924 break;
925 }
926 /* now everything is ready for receiving packet */
927 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530928 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000929
930 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
931
932 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000933 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800934 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000935
936 skb->protocol = eth_type_trans(skb, bp->dev);
937 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200938 if (bp->dev->features & NETIF_F_RXCSUM &&
939 !(bp->dev->flags & IFF_PROMISC) &&
940 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
941 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000942
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200943 bp->dev->stats.rx_packets++;
944 bp->dev->stats.rx_bytes += skb->len;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000945
946#if defined(DEBUG) && defined(VERBOSE_DEBUG)
947 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
948 skb->len, skb->csum);
949 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100950 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000951 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
952 skb->data, 32, true);
953#endif
954
955 netif_receive_skb(skb);
956 }
957
958 gem_rx_refill(bp);
959
960 return count;
961}
962
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100963static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
964 unsigned int last_frag)
965{
966 unsigned int len;
967 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000968 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100969 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000970 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100971
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000972 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530973 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100974
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000975 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -0500976 macb_rx_ring_wrap(bp, first_frag),
977 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100978
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700979 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000980 * first buffer. Since the header is 14 bytes, this makes the
981 * payload word-aligned.
982 *
983 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
984 * the two padding bytes into the skb so that we avoid hitting
985 * the slowpath in memcpy(), and pull them off afterwards.
986 */
987 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100988 if (!skb) {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +0200989 bp->dev->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000990 for (frag = first_frag; ; frag++) {
991 desc = macb_rx_desc(bp, frag);
992 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100993 if (frag == last_frag)
994 break;
995 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000996
997 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100998 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000999
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001000 return 1;
1001 }
1002
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001003 offset = 0;
1004 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001005 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001006 skb_put(skb, len);
1007
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001008 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +00001009 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001010
1011 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001012 if (unlikely(frag != last_frag)) {
1013 dev_kfree_skb_any(skb);
1014 return -1;
1015 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001016 frag_len = len - offset;
1017 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001018 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001019 macb_rx_buffer(bp, frag),
1020 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001021 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001022 desc = macb_rx_desc(bp, frag);
1023 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001024
1025 if (frag == last_frag)
1026 break;
1027 }
1028
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001029 /* Make descriptor updates visible to hardware */
1030 wmb();
1031
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001032 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001033 skb->protocol = eth_type_trans(skb, bp->dev);
1034
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02001035 bp->dev->stats.rx_packets++;
1036 bp->dev->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001037 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001038 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001039 netif_receive_skb(skb);
1040
1041 return 0;
1042}
1043
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001044static inline void macb_init_rx_ring(struct macb *bp)
1045{
1046 dma_addr_t addr;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001047 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001048 int i;
1049
1050 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -05001051 for (i = 0; i < bp->rx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001052 desc = macb_rx_desc(bp, i);
1053 macb_set_addr(bp, desc, addr);
1054 desc->ctrl = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001055 addr += bp->rx_buffer_size;
1056 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001057 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchena0b44ee2016-11-28 14:40:55 +01001058 bp->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001059}
1060
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001061static int macb_rx(struct macb *bp, int budget)
1062{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001063 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001064 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001065 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001066 int first_frag = -1;
1067
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001068 for (tail = bp->rx_tail; budget > 0; tail++) {
1069 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001070 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001071
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001072 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001073 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001074
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001075 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001077 if (!(desc->addr & MACB_BIT(RX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001078 break;
1079
1080 if (ctrl & MACB_BIT(RX_SOF)) {
1081 if (first_frag != -1)
1082 discard_partial_frame(bp, first_frag, tail);
1083 first_frag = tail;
1084 }
1085
1086 if (ctrl & MACB_BIT(RX_EOF)) {
1087 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001088
1089 if (unlikely(first_frag == -1)) {
1090 reset_rx_queue = true;
1091 continue;
1092 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001093
1094 dropped = macb_rx_frame(bp, first_frag, tail);
1095 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001096 if (unlikely(dropped < 0)) {
1097 reset_rx_queue = true;
1098 continue;
1099 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001100 if (!dropped) {
1101 received++;
1102 budget--;
1103 }
1104 }
1105 }
1106
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001107 if (unlikely(reset_rx_queue)) {
1108 unsigned long flags;
1109 u32 ctrl;
1110
1111 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1112
1113 spin_lock_irqsave(&bp->lock, flags);
1114
1115 ctrl = macb_readl(bp, NCR);
1116 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1117
1118 macb_init_rx_ring(bp);
1119 macb_writel(bp, RBQP, bp->rx_ring_dma);
1120
1121 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1122
1123 spin_unlock_irqrestore(&bp->lock, flags);
1124 return received;
1125 }
1126
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001127 if (first_frag != -1)
1128 bp->rx_tail = first_frag;
1129 else
1130 bp->rx_tail = tail;
1131
1132 return received;
1133}
1134
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001135static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001136{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001137 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001138 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001139 u32 status;
1140
1141 status = macb_readl(bp, RSR);
1142 macb_writel(bp, RSR, status);
1143
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001144 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001145
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001146 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001147 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001148
Nicolas Ferre4df95132013-06-04 21:57:12 +00001149 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001150 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001151 napi_complete_done(napi, work_done);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001152
Nicolas Ferre8770e912013-02-12 11:08:48 +01001153 /* Packets received while interrupts were disabled */
1154 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001155 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001156 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1157 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001158 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001159 } else {
1160 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1161 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001162 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001163
1164 /* TODO: Handle errors */
1165
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001166 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001167}
1168
1169static irqreturn_t macb_interrupt(int irq, void *dev_id)
1170{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001171 struct macb_queue *queue = dev_id;
1172 struct macb *bp = queue->bp;
1173 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001174 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001175
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001176 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001177
1178 if (unlikely(!status))
1179 return IRQ_NONE;
1180
1181 spin_lock(&bp->lock);
1182
1183 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001184 /* close possible race with dev_close */
1185 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001186 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001187 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1188 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001189 break;
1190 }
1191
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001192 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1193 (unsigned int)(queue - bp->queues),
1194 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001195
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001197 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001198 * until we have processed the buffers. The
1199 * scheduling call may fail if the poll routine
1200 * is already scheduled, so disable interrupts
1201 * now.
1202 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001203 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001204 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001205 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001206
Ben Hutchings288379f2009-01-19 16:43:59 -08001207 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001208 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001209 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001210 }
1211 }
1212
Nicolas Ferree86cd532012-10-31 06:04:57 +00001213 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001214 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1215 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001216
1217 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001218 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001219
Nicolas Ferree86cd532012-10-31 06:04:57 +00001220 break;
1221 }
1222
1223 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001224 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001225
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001226 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001227 * add that if/when we get our hands on a full-blown MII PHY.
1228 */
1229
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001230 /* There is a hardware issue under heavy load where DMA can
1231 * stop, this causes endless "used buffer descriptor read"
1232 * interrupts but it can be cleared by re-enabling RX. See
1233 * the at91 manual, section 41.3.1 or the Zynq manual
1234 * section 16.7.4 for details.
1235 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001236 if (status & MACB_BIT(RXUBR)) {
1237 ctrl = macb_readl(bp, NCR);
1238 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001239 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001240 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1241
1242 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001243 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001244 }
1245
Alexander Steinb19f7f72011-04-13 05:03:24 +00001246 if (status & MACB_BIT(ISR_ROVR)) {
1247 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001248 if (macb_is_gem(bp))
1249 bp->hw_stats.gem.rx_overruns++;
1250 else
1251 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001252
1253 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001254 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001255 }
1256
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001257 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001258 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001259 * netdev_err to a lower-priority context as well
1260 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001261 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001262 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001263
1264 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001265 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001266 }
1267
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001268 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001269 }
1270
1271 spin_unlock(&bp->lock);
1272
1273 return IRQ_HANDLED;
1274}
1275
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001276#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001277/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001278 * to allow network i/o with interrupts disabled.
1279 */
1280static void macb_poll_controller(struct net_device *dev)
1281{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001282 struct macb *bp = netdev_priv(dev);
1283 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001284 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001285 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001286
1287 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001288 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1289 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001290 local_irq_restore(flags);
1291}
1292#endif
1293
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001294static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001295 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001296 struct sk_buff *skb,
1297 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001298{
1299 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001300 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001301 struct macb_tx_skb *tx_skb = NULL;
1302 struct macb_dma_desc *desc;
1303 unsigned int offset, size, count = 0;
1304 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001305 unsigned int eof = 1, mss_mfs = 0;
1306 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1307
1308 /* LSO */
1309 if (skb_shinfo(skb)->gso_size != 0) {
1310 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1311 /* UDP - UFO */
1312 lso_ctrl = MACB_LSO_UFO_ENABLE;
1313 else
1314 /* TCP - TSO */
1315 lso_ctrl = MACB_LSO_TSO_ENABLE;
1316 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001317
1318 /* First, map non-paged data */
1319 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001320
1321 /* first buffer length */
1322 size = hdrlen;
1323
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001324 offset = 0;
1325 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001326 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001327 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001328
1329 mapping = dma_map_single(&bp->pdev->dev,
1330 skb->data + offset,
1331 size, DMA_TO_DEVICE);
1332 if (dma_mapping_error(&bp->pdev->dev, mapping))
1333 goto dma_error;
1334
1335 /* Save info to properly release resources */
1336 tx_skb->skb = NULL;
1337 tx_skb->mapping = mapping;
1338 tx_skb->size = size;
1339 tx_skb->mapped_as_page = false;
1340
1341 len -= size;
1342 offset += size;
1343 count++;
1344 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001345
1346 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001347 }
1348
1349 /* Then, map paged data from fragments */
1350 for (f = 0; f < nr_frags; f++) {
1351 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1352
1353 len = skb_frag_size(frag);
1354 offset = 0;
1355 while (len) {
1356 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001357 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001358 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001359
1360 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1361 offset, size, DMA_TO_DEVICE);
1362 if (dma_mapping_error(&bp->pdev->dev, mapping))
1363 goto dma_error;
1364
1365 /* Save info to properly release resources */
1366 tx_skb->skb = NULL;
1367 tx_skb->mapping = mapping;
1368 tx_skb->size = size;
1369 tx_skb->mapped_as_page = true;
1370
1371 len -= size;
1372 offset += size;
1373 count++;
1374 tx_head++;
1375 }
1376 }
1377
1378 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001379 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001380 netdev_err(bp->dev, "BUG! empty skb!\n");
1381 return 0;
1382 }
1383
1384 /* This is the last buffer of the frame: save socket buffer */
1385 tx_skb->skb = skb;
1386
1387 /* Update TX ring: update buffer descriptors in reverse order
1388 * to avoid race condition
1389 */
1390
1391 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1392 * to set the end of TX queue
1393 */
1394 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001395 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001396 ctrl = MACB_BIT(TX_USED);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001397 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001398 desc->ctrl = ctrl;
1399
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001400 if (lso_ctrl) {
1401 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1402 /* include header and FCS in value given to h/w */
1403 mss_mfs = skb_shinfo(skb)->gso_size +
1404 skb_transport_offset(skb) +
1405 ETH_FCS_LEN;
1406 else /* TSO */ {
1407 mss_mfs = skb_shinfo(skb)->gso_size;
1408 /* TCP Sequence Number Source Select
1409 * can be set only for TSO
1410 */
1411 seq_ctrl = 0;
1412 }
1413 }
1414
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001415 do {
1416 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001417 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001418 tx_skb = &queue->tx_skb[entry];
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001419 desc = macb_tx_desc(queue, entry);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001420
1421 ctrl = (u32)tx_skb->size;
1422 if (eof) {
1423 ctrl |= MACB_BIT(TX_LAST);
1424 eof = 0;
1425 }
Zach Brownb410d132016-10-19 09:56:57 -05001426 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001427 ctrl |= MACB_BIT(TX_WRAP);
1428
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001429 /* First descriptor is header descriptor */
1430 if (i == queue->tx_head) {
1431 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1432 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1433 } else
1434 /* Only set MSS/MFS on payload descriptors
1435 * (second or later descriptor)
1436 */
1437 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1438
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001439 /* Set TX buffer descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001440 macb_set_addr(bp, desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001441 /* desc->addr must be visible to hardware before clearing
1442 * 'TX_USED' bit in desc->ctrl.
1443 */
1444 wmb();
1445 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001446 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001447
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001448 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001449
1450 return count;
1451
1452dma_error:
1453 netdev_err(bp->dev, "TX DMA map failed\n");
1454
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001455 for (i = queue->tx_head; i != tx_head; i++) {
1456 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001457
1458 macb_tx_unmap(bp, tx_skb);
1459 }
1460
1461 return 0;
1462}
1463
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001464static netdev_features_t macb_features_check(struct sk_buff *skb,
1465 struct net_device *dev,
1466 netdev_features_t features)
1467{
1468 unsigned int nr_frags, f;
1469 unsigned int hdrlen;
1470
1471 /* Validate LSO compatibility */
1472
1473 /* there is only one buffer */
1474 if (!skb_is_nonlinear(skb))
1475 return features;
1476
1477 /* length of header */
1478 hdrlen = skb_transport_offset(skb);
1479 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1480 hdrlen += tcp_hdrlen(skb);
1481
1482 /* For LSO:
1483 * When software supplies two or more payload buffers all payload buffers
1484 * apart from the last must be a multiple of 8 bytes in size.
1485 */
1486 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1487 return features & ~MACB_NETIF_LSO;
1488
1489 nr_frags = skb_shinfo(skb)->nr_frags;
1490 /* No need to check last fragment */
1491 nr_frags--;
1492 for (f = 0; f < nr_frags; f++) {
1493 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1494
1495 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1496 return features & ~MACB_NETIF_LSO;
1497 }
1498 return features;
1499}
1500
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001501static inline int macb_clear_csum(struct sk_buff *skb)
1502{
1503 /* no change for packets without checksum offloading */
1504 if (skb->ip_summed != CHECKSUM_PARTIAL)
1505 return 0;
1506
1507 /* make sure we can modify the header */
1508 if (unlikely(skb_cow_head(skb, 0)))
1509 return -1;
1510
1511 /* initialize checksum field
1512 * This is required - at least for Zynq, which otherwise calculates
1513 * wrong UDP header checksums for UDP packets with UDP data len <=2
1514 */
1515 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1516 return 0;
1517}
1518
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001519static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1520{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001521 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001522 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001523 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001524 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001525 unsigned int desc_cnt, nr_frags, frag_size, f;
1526 unsigned int hdrlen;
1527 bool is_lso, is_udp = 0;
1528
1529 is_lso = (skb_shinfo(skb)->gso_size != 0);
1530
1531 if (is_lso) {
1532 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1533
1534 /* length of headers */
1535 if (is_udp)
1536 /* only queue eth + ip headers separately for UDP */
1537 hdrlen = skb_transport_offset(skb);
1538 else
1539 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1540 if (skb_headlen(skb) < hdrlen) {
1541 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1542 /* if this is required, would need to copy to single buffer */
1543 return NETDEV_TX_BUSY;
1544 }
1545 } else
1546 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001547
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001548#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1549 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001550 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1551 queue_index, skb->len, skb->head, skb->data,
1552 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001553 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1554 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001555#endif
1556
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001557 /* Count how many TX buffer descriptors are needed to send this
1558 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001559 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001560 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001561 if (is_lso && (skb_headlen(skb) > hdrlen))
1562 /* extra header descriptor if also payload in first buffer */
1563 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1564 else
1565 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001566 nr_frags = skb_shinfo(skb)->nr_frags;
1567 for (f = 0; f < nr_frags; f++) {
1568 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001569 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001570 }
1571
Dongdong Deng48719532009-08-23 19:49:07 -07001572 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001573
1574 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001575 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001576 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001577 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001578 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001579 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001580 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001581 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001582 }
1583
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001584 if (macb_clear_csum(skb)) {
1585 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001586 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001587 }
1588
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001589 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001590 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001591 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001592 goto unlock;
1593 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001594
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001595 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001596 wmb();
1597
Richard Cochrane0720922011-06-19 21:51:28 +00001598 skb_tx_timestamp(skb);
1599
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001600 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1601
Zach Brownb410d132016-10-19 09:56:57 -05001602 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001603 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001604
Soren Brinkmann92030902014-03-04 08:46:39 -08001605unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001606 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001607
Patrick McHardy6ed10652009-06-23 06:03:08 +00001608 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001609}
1610
Nicolas Ferre4df95132013-06-04 21:57:12 +00001611static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001612{
1613 if (!macb_is_gem(bp)) {
1614 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1615 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001616 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001617
Nicolas Ferre1b447912013-06-04 21:57:11 +00001618 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001619 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001620 "RX buffer must be multiple of %d bytes, expanding\n",
1621 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001622 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001623 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001624 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001625 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001626
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001627 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
Nicolas Ferre4df95132013-06-04 21:57:12 +00001628 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001629}
1630
Nicolas Ferre4df95132013-06-04 21:57:12 +00001631static void gem_free_rx_buffers(struct macb *bp)
1632{
1633 struct sk_buff *skb;
1634 struct macb_dma_desc *desc;
1635 dma_addr_t addr;
1636 int i;
1637
1638 if (!bp->rx_skbuff)
1639 return;
1640
Zach Brownb410d132016-10-19 09:56:57 -05001641 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001642 skb = bp->rx_skbuff[i];
1643
Moritz Fischeraa50b552016-03-29 19:11:13 -07001644 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001645 continue;
1646
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001647 desc = macb_rx_desc(bp, i);
1648 addr = macb_get_addr(bp, desc);
1649
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001650 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001651 DMA_FROM_DEVICE);
1652 dev_kfree_skb_any(skb);
1653 skb = NULL;
1654 }
1655
1656 kfree(bp->rx_skbuff);
1657 bp->rx_skbuff = NULL;
1658}
1659
1660static void macb_free_rx_buffers(struct macb *bp)
1661{
1662 if (bp->rx_buffers) {
1663 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001664 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001665 bp->rx_buffers, bp->rx_buffers_dma);
1666 bp->rx_buffers = NULL;
1667 }
1668}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001669
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001670static void macb_free_consistent(struct macb *bp)
1671{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001672 struct macb_queue *queue;
1673 unsigned int q;
1674
Nicolas Ferre4df95132013-06-04 21:57:12 +00001675 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001676 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001677 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001678 bp->rx_ring, bp->rx_ring_dma);
1679 bp->rx_ring = NULL;
1680 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001681
1682 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1683 kfree(queue->tx_skb);
1684 queue->tx_skb = NULL;
1685 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001686 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001687 queue->tx_ring, queue->tx_ring_dma);
1688 queue->tx_ring = NULL;
1689 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001690 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001691}
1692
1693static int gem_alloc_rx_buffers(struct macb *bp)
1694{
1695 int size;
1696
Zach Brownb410d132016-10-19 09:56:57 -05001697 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001698 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1699 if (!bp->rx_skbuff)
1700 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001701 else
1702 netdev_dbg(bp->dev,
1703 "Allocated %d RX struct sk_buff entries at %p\n",
1704 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001705 return 0;
1706}
1707
1708static int macb_alloc_rx_buffers(struct macb *bp)
1709{
1710 int size;
1711
Zach Brownb410d132016-10-19 09:56:57 -05001712 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001713 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1714 &bp->rx_buffers_dma, GFP_KERNEL);
1715 if (!bp->rx_buffers)
1716 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001717
1718 netdev_dbg(bp->dev,
1719 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1720 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001721 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001722}
1723
1724static int macb_alloc_consistent(struct macb *bp)
1725{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001726 struct macb_queue *queue;
1727 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001728 int size;
1729
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001730 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001731 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001732 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1733 &queue->tx_ring_dma,
1734 GFP_KERNEL);
1735 if (!queue->tx_ring)
1736 goto out_err;
1737 netdev_dbg(bp->dev,
1738 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1739 q, size, (unsigned long)queue->tx_ring_dma,
1740 queue->tx_ring);
1741
Zach Brownb410d132016-10-19 09:56:57 -05001742 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001743 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1744 if (!queue->tx_skb)
1745 goto out_err;
1746 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001747
Zach Brownb410d132016-10-19 09:56:57 -05001748 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001749 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1750 &bp->rx_ring_dma, GFP_KERNEL);
1751 if (!bp->rx_ring)
1752 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001753 netdev_dbg(bp->dev,
1754 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1755 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001756
Nicolas Ferre4df95132013-06-04 21:57:12 +00001757 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001758 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001759
1760 return 0;
1761
1762out_err:
1763 macb_free_consistent(bp);
1764 return -ENOMEM;
1765}
1766
Nicolas Ferre4df95132013-06-04 21:57:12 +00001767static void gem_init_rings(struct macb *bp)
1768{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001769 struct macb_queue *queue;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001770 struct macb_dma_desc *desc = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001771 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001772 int i;
1773
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001774 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001775 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001776 desc = macb_tx_desc(queue, i);
1777 macb_set_addr(bp, desc, 0);
1778 desc->ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001779 }
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001780 desc->ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001781 queue->tx_head = 0;
1782 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001783 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001784
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001785 bp->rx_tail = 0;
1786 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001787
1788 gem_rx_refill(bp);
1789}
1790
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001791static void macb_init_rings(struct macb *bp)
1792{
1793 int i;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001794 struct macb_dma_desc *desc = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001795
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001796 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001797
Zach Brownb410d132016-10-19 09:56:57 -05001798 for (i = 0; i < bp->tx_ring_size; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001799 desc = macb_tx_desc(&bp->queues[0], i);
1800 macb_set_addr(bp, desc, 0);
1801 desc->ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001802 }
Ben Shelton21d35152015-04-22 17:28:54 -05001803 bp->queues[0].tx_head = 0;
1804 bp->queues[0].tx_tail = 0;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001805 desc->ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001806}
1807
1808static void macb_reset_hw(struct macb *bp)
1809{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001810 struct macb_queue *queue;
1811 unsigned int q;
1812
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001813 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814 * more gracefully?)
1815 */
1816 macb_writel(bp, NCR, 0);
1817
1818 /* Clear the stats registers (XXX: Update stats first?) */
1819 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1820
1821 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001822 macb_writel(bp, TSR, -1);
1823 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001824
1825 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001826 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1827 queue_writel(queue, IDR, -1);
1828 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001829 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1830 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001831 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001832}
1833
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001834static u32 gem_mdc_clk_div(struct macb *bp)
1835{
1836 u32 config;
1837 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1838
1839 if (pclk_hz <= 20000000)
1840 config = GEM_BF(CLK, GEM_CLK_DIV8);
1841 else if (pclk_hz <= 40000000)
1842 config = GEM_BF(CLK, GEM_CLK_DIV16);
1843 else if (pclk_hz <= 80000000)
1844 config = GEM_BF(CLK, GEM_CLK_DIV32);
1845 else if (pclk_hz <= 120000000)
1846 config = GEM_BF(CLK, GEM_CLK_DIV48);
1847 else if (pclk_hz <= 160000000)
1848 config = GEM_BF(CLK, GEM_CLK_DIV64);
1849 else
1850 config = GEM_BF(CLK, GEM_CLK_DIV96);
1851
1852 return config;
1853}
1854
1855static u32 macb_mdc_clk_div(struct macb *bp)
1856{
1857 u32 config;
1858 unsigned long pclk_hz;
1859
1860 if (macb_is_gem(bp))
1861 return gem_mdc_clk_div(bp);
1862
1863 pclk_hz = clk_get_rate(bp->pclk);
1864 if (pclk_hz <= 20000000)
1865 config = MACB_BF(CLK, MACB_CLK_DIV8);
1866 else if (pclk_hz <= 40000000)
1867 config = MACB_BF(CLK, MACB_CLK_DIV16);
1868 else if (pclk_hz <= 80000000)
1869 config = MACB_BF(CLK, MACB_CLK_DIV32);
1870 else
1871 config = MACB_BF(CLK, MACB_CLK_DIV64);
1872
1873 return config;
1874}
1875
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001876/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001877 * should program. We find the width from decoding the design configuration
1878 * register to find the maximum supported data bus width.
1879 */
1880static u32 macb_dbw(struct macb *bp)
1881{
1882 if (!macb_is_gem(bp))
1883 return 0;
1884
1885 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1886 case 4:
1887 return GEM_BF(DBW, GEM_DBW128);
1888 case 2:
1889 return GEM_BF(DBW, GEM_DBW64);
1890 case 1:
1891 default:
1892 return GEM_BF(DBW, GEM_DBW32);
1893 }
1894}
1895
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001896/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001897 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001898 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001899 * (if not supported by FIFO, it will fallback to default)
1900 * - set both rx/tx packet buffers to full memory size
1901 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001902 */
1903static void macb_configure_dma(struct macb *bp)
1904{
1905 u32 dmacfg;
1906
1907 if (macb_is_gem(bp)) {
1908 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001909 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001910 if (bp->dma_burst_length)
1911 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001912 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301913 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301914
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001915 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301916 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1917 else
1918 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1919
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001920 if (bp->dev->features & NETIF_F_HW_CSUM)
1921 dmacfg |= GEM_BIT(TXCOEN);
1922 else
1923 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301924
1925#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001926 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1927 dmacfg |= GEM_BIT(ADDR64);
Harini Katakamfff80192016-08-09 13:15:53 +05301928#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001929 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1930 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001931 gem_writel(bp, DMACFG, dmacfg);
1932 }
1933}
1934
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001935static void macb_init_hw(struct macb *bp)
1936{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001937 struct macb_queue *queue;
1938 unsigned int q;
1939
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001940 u32 config;
1941
1942 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001943 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001944
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001945 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301946 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1947 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001948 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001949 config |= MACB_BIT(PAE); /* PAuse Enable */
1950 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001951 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301952 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1953 else
1954 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001955 if (bp->dev->flags & IFF_PROMISC)
1956 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001957 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1958 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001959 if (!(bp->dev->flags & IFF_BROADCAST))
1960 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001961 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001962 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001963 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301964 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001965 bp->speed = SPEED_10;
1966 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301967 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001968 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301969 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001970
Jamie Iles0116da42011-03-14 17:38:30 +00001971 macb_configure_dma(bp);
1972
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001973 /* Initialize TX and RX buffers */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001974 macb_writel(bp, RBQP, lower_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301975#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001976 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1977 macb_writel(bp, RBQPH, upper_32_bits(bp->rx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301978#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001979 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001980 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301981#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00001982 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
1983 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
Harini Katakamfff80192016-08-09 13:15:53 +05301984#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001985
1986 /* Enable interrupts */
1987 queue_writel(queue, IER,
1988 MACB_RX_INT_FLAGS |
1989 MACB_TX_INT_FLAGS |
1990 MACB_BIT(HRESP));
1991 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992
1993 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001994 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001995}
1996
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001997/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001998 * locations in the memory map. The least significant bits are stored
1999 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2000 *
2001 * The unicast hash enable and the multicast hash enable bits in the
2002 * network configuration register enable the reception of hash matched
2003 * frames. The destination address is reduced to a 6 bit index into
2004 * the 64 bit hash register using the following hash function. The
2005 * hash function is an exclusive or of every sixth bit of the
2006 * destination address.
2007 *
2008 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2009 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2010 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2011 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2012 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2013 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2014 *
2015 * da[0] represents the least significant bit of the first byte
2016 * received, that is, the multicast/unicast indicator, and da[47]
2017 * represents the most significant bit of the last byte received. If
2018 * the hash index, hi[n], points to a bit that is set in the hash
2019 * register then the frame will be matched according to whether the
2020 * frame is multicast or unicast. A multicast match will be signalled
2021 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2022 * index points to a bit set in the hash register. A unicast match
2023 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2024 * and the hash index points to a bit set in the hash register. To
2025 * receive all multicast frames, the hash register should be set with
2026 * all ones and the multicast hash enable bit should be set in the
2027 * network configuration register.
2028 */
2029
2030static inline int hash_bit_value(int bitnr, __u8 *addr)
2031{
2032 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2033 return 1;
2034 return 0;
2035}
2036
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002037/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002038static int hash_get_index(__u8 *addr)
2039{
2040 int i, j, bitval;
2041 int hash_index = 0;
2042
2043 for (j = 0; j < 6; j++) {
2044 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06002045 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002046
2047 hash_index |= (bitval << j);
2048 }
2049
2050 return hash_index;
2051}
2052
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002053/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002054static void macb_sethashtable(struct net_device *dev)
2055{
Jiri Pirko22bedad32010-04-01 21:22:57 +00002056 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002057 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00002058 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002059 struct macb *bp = netdev_priv(dev);
2060
Moritz Fischeraa50b552016-03-29 19:11:13 -07002061 mc_filter[0] = 0;
2062 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002063
Jiri Pirko22bedad32010-04-01 21:22:57 +00002064 netdev_for_each_mc_addr(ha, dev) {
2065 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002066 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2067 }
2068
Jamie Ilesf75ba502011-11-08 10:12:32 +00002069 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2070 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002071}
2072
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002073/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002074static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002075{
2076 unsigned long cfg;
2077 struct macb *bp = netdev_priv(dev);
2078
2079 cfg = macb_readl(bp, NCFGR);
2080
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002081 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002082 /* Enable promiscuous mode */
2083 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002084
2085 /* Disable RX checksum offload */
2086 if (macb_is_gem(bp))
2087 cfg &= ~GEM_BIT(RXCOEN);
2088 } else {
2089 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002090 cfg &= ~MACB_BIT(CAF);
2091
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002092 /* Enable RX checksum offload only if requested */
2093 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2094 cfg |= GEM_BIT(RXCOEN);
2095 }
2096
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002097 if (dev->flags & IFF_ALLMULTI) {
2098 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002099 macb_or_gem_writel(bp, HRB, -1);
2100 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002101 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002102 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002103 /* Enable specific multicasts */
2104 macb_sethashtable(dev);
2105 cfg |= MACB_BIT(NCFGR_MTI);
2106 } else if (dev->flags & (~IFF_ALLMULTI)) {
2107 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002108 macb_or_gem_writel(bp, HRB, 0);
2109 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002110 cfg &= ~MACB_BIT(NCFGR_MTI);
2111 }
2112
2113 macb_writel(bp, NCFGR, cfg);
2114}
2115
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002116static int macb_open(struct net_device *dev)
2117{
2118 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002119 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002120 int err;
2121
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002122 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002123
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002124 /* carrier starts down */
2125 netif_carrier_off(dev);
2126
frederic RODO6c36a702007-07-12 19:07:24 +02002127 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002128 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002129 return -EAGAIN;
2130
Nicolas Ferre1b447912013-06-04 21:57:11 +00002131 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002132 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002133
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002134 err = macb_alloc_consistent(bp);
2135 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002136 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2137 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002138 return err;
2139 }
2140
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002141 napi_enable(&bp->napi);
2142
Nicolas Ferre4df95132013-06-04 21:57:12 +00002143 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002144 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002145
frederic RODO6c36a702007-07-12 19:07:24 +02002146 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002147 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002148
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002149 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002150
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002151 if (bp->ptp_info)
2152 bp->ptp_info->ptp_init(dev);
2153
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002154 return 0;
2155}
2156
2157static int macb_close(struct net_device *dev)
2158{
2159 struct macb *bp = netdev_priv(dev);
2160 unsigned long flags;
2161
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002162 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002163 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002164
Philippe Reynes0a912812016-06-22 00:32:35 +02002165 if (dev->phydev)
2166 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002167
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002168 spin_lock_irqsave(&bp->lock, flags);
2169 macb_reset_hw(bp);
2170 netif_carrier_off(dev);
2171 spin_unlock_irqrestore(&bp->lock, flags);
2172
2173 macb_free_consistent(bp);
2174
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002175 if (bp->ptp_info)
2176 bp->ptp_info->ptp_remove(dev);
2177
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002178 return 0;
2179}
2180
Harini Katakama5898ea2015-05-06 22:27:18 +05302181static int macb_change_mtu(struct net_device *dev, int new_mtu)
2182{
Harini Katakama5898ea2015-05-06 22:27:18 +05302183 if (netif_running(dev))
2184 return -EBUSY;
2185
Harini Katakama5898ea2015-05-06 22:27:18 +05302186 dev->mtu = new_mtu;
2187
2188 return 0;
2189}
2190
Jamie Ilesa494ed82011-03-09 16:26:35 +00002191static void gem_update_stats(struct macb *bp)
2192{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002193 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002194 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002195
Xander Huff3ff13f12015-01-13 16:15:51 -06002196 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2197 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002198 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002199
2200 bp->ethtool_stats[i] += val;
2201 *p += val;
2202
2203 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2204 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002205 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002206 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002207 *(++p) += val;
2208 }
2209 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002210}
2211
2212static struct net_device_stats *gem_get_stats(struct macb *bp)
2213{
2214 struct gem_stats *hwstat = &bp->hw_stats.gem;
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002215 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002216
2217 gem_update_stats(bp);
2218
2219 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2220 hwstat->rx_alignment_errors +
2221 hwstat->rx_resource_errors +
2222 hwstat->rx_overruns +
2223 hwstat->rx_oversize_frames +
2224 hwstat->rx_jabbers +
2225 hwstat->rx_undersized_frames +
2226 hwstat->rx_length_field_frame_errors);
2227 nstat->tx_errors = (hwstat->tx_late_collisions +
2228 hwstat->tx_excessive_collisions +
2229 hwstat->tx_underrun +
2230 hwstat->tx_carrier_sense_errors);
2231 nstat->multicast = hwstat->rx_multicast_frames;
2232 nstat->collisions = (hwstat->tx_single_collision_frames +
2233 hwstat->tx_multiple_collision_frames +
2234 hwstat->tx_excessive_collisions);
2235 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2236 hwstat->rx_jabbers +
2237 hwstat->rx_undersized_frames +
2238 hwstat->rx_length_field_frame_errors);
2239 nstat->rx_over_errors = hwstat->rx_resource_errors;
2240 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2241 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2242 nstat->rx_fifo_errors = hwstat->rx_overruns;
2243 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2244 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2245 nstat->tx_fifo_errors = hwstat->tx_underrun;
2246
2247 return nstat;
2248}
2249
Xander Huff3ff13f12015-01-13 16:15:51 -06002250static void gem_get_ethtool_stats(struct net_device *dev,
2251 struct ethtool_stats *stats, u64 *data)
2252{
2253 struct macb *bp;
2254
2255 bp = netdev_priv(dev);
2256 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002257 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002258}
2259
2260static int gem_get_sset_count(struct net_device *dev, int sset)
2261{
2262 switch (sset) {
2263 case ETH_SS_STATS:
2264 return GEM_STATS_LEN;
2265 default:
2266 return -EOPNOTSUPP;
2267 }
2268}
2269
2270static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2271{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002272 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002273
2274 switch (sset) {
2275 case ETH_SS_STATS:
2276 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2277 memcpy(p, gem_statistics[i].stat_string,
2278 ETH_GSTRING_LEN);
2279 break;
2280 }
2281}
2282
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002283static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002284{
2285 struct macb *bp = netdev_priv(dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002286 struct net_device_stats *nstat = &bp->dev->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002287 struct macb_stats *hwstat = &bp->hw_stats.macb;
2288
2289 if (macb_is_gem(bp))
2290 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002291
frederic RODO6c36a702007-07-12 19:07:24 +02002292 /* read stats from hardware */
2293 macb_update_stats(bp);
2294
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002295 /* Convert HW stats into netdevice stats */
2296 nstat->rx_errors = (hwstat->rx_fcs_errors +
2297 hwstat->rx_align_errors +
2298 hwstat->rx_resource_errors +
2299 hwstat->rx_overruns +
2300 hwstat->rx_oversize_pkts +
2301 hwstat->rx_jabbers +
2302 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002303 hwstat->rx_length_mismatch);
2304 nstat->tx_errors = (hwstat->tx_late_cols +
2305 hwstat->tx_excessive_cols +
2306 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002307 hwstat->tx_carrier_errors +
2308 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002309 nstat->collisions = (hwstat->tx_single_cols +
2310 hwstat->tx_multiple_cols +
2311 hwstat->tx_excessive_cols);
2312 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2313 hwstat->rx_jabbers +
2314 hwstat->rx_undersize_pkts +
2315 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002316 nstat->rx_over_errors = hwstat->rx_resource_errors +
2317 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002318 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2319 nstat->rx_frame_errors = hwstat->rx_align_errors;
2320 nstat->rx_fifo_errors = hwstat->rx_overruns;
2321 /* XXX: What does "missed" mean? */
2322 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2323 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2324 nstat->tx_fifo_errors = hwstat->tx_underruns;
2325 /* Don't know about heartbeat or window errors... */
2326
2327 return nstat;
2328}
2329
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002330static int macb_get_regs_len(struct net_device *netdev)
2331{
2332 return MACB_GREGS_NBR * sizeof(u32);
2333}
2334
2335static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2336 void *p)
2337{
2338 struct macb *bp = netdev_priv(dev);
2339 unsigned int tail, head;
2340 u32 *regs_buff = p;
2341
2342 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2343 | MACB_GREGS_VERSION;
2344
Zach Brownb410d132016-10-19 09:56:57 -05002345 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2346 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002347
2348 regs_buff[0] = macb_readl(bp, NCR);
2349 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2350 regs_buff[2] = macb_readl(bp, NSR);
2351 regs_buff[3] = macb_readl(bp, TSR);
2352 regs_buff[4] = macb_readl(bp, RBQP);
2353 regs_buff[5] = macb_readl(bp, TBQP);
2354 regs_buff[6] = macb_readl(bp, RSR);
2355 regs_buff[7] = macb_readl(bp, IMR);
2356
2357 regs_buff[8] = tail;
2358 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002359 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2360 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002361
Neil Armstrongce721a72016-01-05 14:39:16 +01002362 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2363 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002364 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002365 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002366}
2367
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002368static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2369{
2370 struct macb *bp = netdev_priv(netdev);
2371
2372 wol->supported = 0;
2373 wol->wolopts = 0;
2374
2375 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2376 wol->supported = WAKE_MAGIC;
2377
2378 if (bp->wol & MACB_WOL_ENABLED)
2379 wol->wolopts |= WAKE_MAGIC;
2380 }
2381}
2382
2383static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2384{
2385 struct macb *bp = netdev_priv(netdev);
2386
2387 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2388 (wol->wolopts & ~WAKE_MAGIC))
2389 return -EOPNOTSUPP;
2390
2391 if (wol->wolopts & WAKE_MAGIC)
2392 bp->wol |= MACB_WOL_ENABLED;
2393 else
2394 bp->wol &= ~MACB_WOL_ENABLED;
2395
2396 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2397
2398 return 0;
2399}
2400
Zach Brown8441bb32016-10-19 09:56:58 -05002401static void macb_get_ringparam(struct net_device *netdev,
2402 struct ethtool_ringparam *ring)
2403{
2404 struct macb *bp = netdev_priv(netdev);
2405
2406 ring->rx_max_pending = MAX_RX_RING_SIZE;
2407 ring->tx_max_pending = MAX_TX_RING_SIZE;
2408
2409 ring->rx_pending = bp->rx_ring_size;
2410 ring->tx_pending = bp->tx_ring_size;
2411}
2412
2413static int macb_set_ringparam(struct net_device *netdev,
2414 struct ethtool_ringparam *ring)
2415{
2416 struct macb *bp = netdev_priv(netdev);
2417 u32 new_rx_size, new_tx_size;
2418 unsigned int reset = 0;
2419
2420 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2421 return -EINVAL;
2422
2423 new_rx_size = clamp_t(u32, ring->rx_pending,
2424 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2425 new_rx_size = roundup_pow_of_two(new_rx_size);
2426
2427 new_tx_size = clamp_t(u32, ring->tx_pending,
2428 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2429 new_tx_size = roundup_pow_of_two(new_tx_size);
2430
2431 if ((new_tx_size == bp->tx_ring_size) &&
2432 (new_rx_size == bp->rx_ring_size)) {
2433 /* nothing to do */
2434 return 0;
2435 }
2436
2437 if (netif_running(bp->dev)) {
2438 reset = 1;
2439 macb_close(bp->dev);
2440 }
2441
2442 bp->rx_ring_size = new_rx_size;
2443 bp->tx_ring_size = new_tx_size;
2444
2445 if (reset)
2446 macb_open(bp->dev);
2447
2448 return 0;
2449}
2450
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002451static int macb_get_ts_info(struct net_device *netdev,
2452 struct ethtool_ts_info *info)
2453{
2454 struct macb *bp = netdev_priv(netdev);
2455
2456 if (bp->ptp_info)
2457 return bp->ptp_info->get_ts_info(netdev, info);
2458
2459 return ethtool_op_get_ts_info(netdev, info);
2460}
2461
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002462static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002463 .get_regs_len = macb_get_regs_len,
2464 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002465 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002466 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002467 .get_wol = macb_get_wol,
2468 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002469 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2470 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002471 .get_ringparam = macb_get_ringparam,
2472 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002473};
Xander Huff8cd5a562015-01-15 15:55:20 -06002474
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002475static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002476 .get_regs_len = macb_get_regs_len,
2477 .get_regs = macb_get_regs,
2478 .get_link = ethtool_op_get_link,
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002479 .get_ts_info = macb_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002480 .get_ethtool_stats = gem_get_ethtool_stats,
2481 .get_strings = gem_get_ethtool_strings,
2482 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002483 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2484 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002485 .get_ringparam = macb_get_ringparam,
2486 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002487};
2488
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002489static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002490{
Philippe Reynes0a912812016-06-22 00:32:35 +02002491 struct phy_device *phydev = dev->phydev;
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002492 struct macb *bp = netdev_priv(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002493
2494 if (!netif_running(dev))
2495 return -EINVAL;
2496
frederic RODO6c36a702007-07-12 19:07:24 +02002497 if (!phydev)
2498 return -ENODEV;
2499
Andrei.Pistirica@microchip.comc2594d82017-01-19 17:56:15 +02002500 if (!bp->ptp_info)
2501 return phy_mii_ioctl(phydev, rq, cmd);
2502
2503 switch (cmd) {
2504 case SIOCSHWTSTAMP:
2505 return bp->ptp_info->set_hwtst(dev, rq, cmd);
2506 case SIOCGHWTSTAMP:
2507 return bp->ptp_info->get_hwtst(dev, rq);
2508 default:
2509 return phy_mii_ioctl(phydev, rq, cmd);
2510 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002511}
2512
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002513static int macb_set_features(struct net_device *netdev,
2514 netdev_features_t features)
2515{
2516 struct macb *bp = netdev_priv(netdev);
2517 netdev_features_t changed = features ^ netdev->features;
2518
2519 /* TX checksum offload */
2520 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2521 u32 dmacfg;
2522
2523 dmacfg = gem_readl(bp, DMACFG);
2524 if (features & NETIF_F_HW_CSUM)
2525 dmacfg |= GEM_BIT(TXCOEN);
2526 else
2527 dmacfg &= ~GEM_BIT(TXCOEN);
2528 gem_writel(bp, DMACFG, dmacfg);
2529 }
2530
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002531 /* RX checksum offload */
2532 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2533 u32 netcfg;
2534
2535 netcfg = gem_readl(bp, NCFGR);
2536 if (features & NETIF_F_RXCSUM &&
2537 !(netdev->flags & IFF_PROMISC))
2538 netcfg |= GEM_BIT(RXCOEN);
2539 else
2540 netcfg &= ~GEM_BIT(RXCOEN);
2541 gem_writel(bp, NCFGR, netcfg);
2542 }
2543
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002544 return 0;
2545}
2546
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002547static const struct net_device_ops macb_netdev_ops = {
2548 .ndo_open = macb_open,
2549 .ndo_stop = macb_close,
2550 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002551 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002552 .ndo_get_stats = macb_get_stats,
2553 .ndo_do_ioctl = macb_ioctl,
2554 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302555 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002556 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002557#ifdef CONFIG_NET_POLL_CONTROLLER
2558 .ndo_poll_controller = macb_poll_controller,
2559#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002560 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002561 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002562};
2563
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002564/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002565 * and integration options used
2566 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002567static void macb_configure_caps(struct macb *bp,
2568 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002569{
2570 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002571
Nicolas Ferref6970502015-03-31 15:02:01 +02002572 if (dt_conf)
2573 bp->caps = dt_conf->caps;
2574
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002575 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002576 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2577
Nicolas Ferree1755872014-07-24 13:50:58 +02002578 dcfg = gem_readl(bp, DCFG1);
2579 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2580 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2581 dcfg = gem_readl(bp, DCFG2);
2582 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2583 bp->caps |= MACB_CAPS_FIFO_MODE;
2584 }
2585
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002586 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002587}
2588
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002589static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002590 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002591 unsigned int *queue_mask,
2592 unsigned int *num_queues)
2593{
2594 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002595
2596 *queue_mask = 0x1;
2597 *num_queues = 1;
2598
Nicolas Ferreda120112015-03-31 15:02:00 +02002599 /* is it macb or gem ?
2600 *
2601 * We need to read directly from the hardware here because
2602 * we are early in the probe process and don't have the
2603 * MACB_CAPS_MACB_IS_GEM flag positioned
2604 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002605 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002606 return;
2607
2608 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302609 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2610
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002611 *queue_mask |= 0x1;
2612
2613 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2614 if (*queue_mask & (1 << hw_q))
2615 (*num_queues)++;
2616}
2617
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002618static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302619 struct clk **hclk, struct clk **tx_clk,
2620 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002621{
Bartosz Folta83a77e92016-12-14 06:39:15 +00002622 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002623 int err;
2624
Bartosz Folta83a77e92016-12-14 06:39:15 +00002625 pdata = dev_get_platdata(&pdev->dev);
2626 if (pdata) {
2627 *pclk = pdata->pclk;
2628 *hclk = pdata->hclk;
2629 } else {
2630 *pclk = devm_clk_get(&pdev->dev, "pclk");
2631 *hclk = devm_clk_get(&pdev->dev, "hclk");
2632 }
2633
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002634 if (IS_ERR(*pclk)) {
2635 err = PTR_ERR(*pclk);
2636 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2637 return err;
2638 }
2639
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002640 if (IS_ERR(*hclk)) {
2641 err = PTR_ERR(*hclk);
2642 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2643 return err;
2644 }
2645
2646 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2647 if (IS_ERR(*tx_clk))
2648 *tx_clk = NULL;
2649
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302650 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2651 if (IS_ERR(*rx_clk))
2652 *rx_clk = NULL;
2653
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002654 err = clk_prepare_enable(*pclk);
2655 if (err) {
2656 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2657 return err;
2658 }
2659
2660 err = clk_prepare_enable(*hclk);
2661 if (err) {
2662 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2663 goto err_disable_pclk;
2664 }
2665
2666 err = clk_prepare_enable(*tx_clk);
2667 if (err) {
2668 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2669 goto err_disable_hclk;
2670 }
2671
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302672 err = clk_prepare_enable(*rx_clk);
2673 if (err) {
2674 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2675 goto err_disable_txclk;
2676 }
2677
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002678 return 0;
2679
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302680err_disable_txclk:
2681 clk_disable_unprepare(*tx_clk);
2682
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002683err_disable_hclk:
2684 clk_disable_unprepare(*hclk);
2685
2686err_disable_pclk:
2687 clk_disable_unprepare(*pclk);
2688
2689 return err;
2690}
2691
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002692static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002693{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002694 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002695 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002696 struct macb *bp = netdev_priv(dev);
2697 struct macb_queue *queue;
2698 int err;
2699 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002700
Zach Brownb410d132016-10-19 09:56:57 -05002701 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2702 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2703
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002704 /* set the queue register mapping once for all: queue0 has a special
2705 * register mapping but we don't want to test the queue index then
2706 * compute the corresponding register offset at run time.
2707 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002708 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002709 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002710 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002711
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002712 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002713 queue->bp = bp;
2714 if (hw_q) {
2715 queue->ISR = GEM_ISR(hw_q - 1);
2716 queue->IER = GEM_IER(hw_q - 1);
2717 queue->IDR = GEM_IDR(hw_q - 1);
2718 queue->IMR = GEM_IMR(hw_q - 1);
2719 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302720#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002721 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2722 queue->TBQPH = GEM_TBQPH(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302723#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002724 } else {
2725 /* queue0 uses legacy registers */
2726 queue->ISR = MACB_ISR;
2727 queue->IER = MACB_IER;
2728 queue->IDR = MACB_IDR;
2729 queue->IMR = MACB_IMR;
2730 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302731#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002732 if (bp->hw_dma_cap == HW_DMA_CAP_64B)
2733 queue->TBQPH = MACB_TBQPH;
Harini Katakamfff80192016-08-09 13:15:53 +05302734#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002735 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002736
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002737 /* get irq: here we use the linux queue index, not the hardware
2738 * queue index. the queue irq definitions in the device tree
2739 * must remove the optional gaps that could exist in the
2740 * hardware queue mask.
2741 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002742 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002743 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002744 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002745 if (err) {
2746 dev_err(&pdev->dev,
2747 "Unable to request IRQ %d (error %d)\n",
2748 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002749 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002750 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002751
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002752 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002753 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002754 }
2755
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002756 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002757 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002758
Nicolas Ferre4df95132013-06-04 21:57:12 +00002759 /* setup appropriated routines according to adapter type */
2760 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002761 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002762 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2763 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2764 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2765 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002766 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002767 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002768 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002769 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2770 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2771 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2772 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002773 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002774 }
2775
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002776 /* Set features */
2777 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002778
2779 /* Check LSO capability */
2780 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
2781 dev->hw_features |= MACB_NETIF_LSO;
2782
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002783 /* Checksum offload is only available on gem with packet buffer */
2784 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002785 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002786 if (bp->caps & MACB_CAPS_SG_DISABLED)
2787 dev->hw_features &= ~NETIF_F_SG;
2788 dev->features = dev->hw_features;
2789
Neil Armstrongce721a72016-01-05 14:39:16 +01002790 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2791 val = 0;
2792 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2793 val = GEM_BIT(RGMII);
2794 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002795 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002796 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002797 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002798 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002799
Neil Armstrongce721a72016-01-05 14:39:16 +01002800 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2801 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002802
Neil Armstrongce721a72016-01-05 14:39:16 +01002803 macb_or_gem_writel(bp, USRIO, val);
2804 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002805
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002806 /* Set MII management clock divider */
2807 val = macb_mdc_clk_div(bp);
2808 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302809 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2810 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002811 macb_writel(bp, NCFGR, val);
2812
2813 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002814}
2815
2816#if defined(CONFIG_OF)
2817/* 1518 rounded up */
2818#define AT91ETHER_MAX_RBUFF_SZ 0x600
2819/* max number of receive buffers */
2820#define AT91ETHER_MAX_RX_DESCR 9
2821
2822/* Initialize and start the Receiver and Transmit subsystems */
2823static int at91ether_start(struct net_device *dev)
2824{
2825 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002826 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002827 dma_addr_t addr;
2828 u32 ctl;
2829 int i;
2830
2831 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2832 (AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002833 macb_dma_desc_get_size(lp)),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002834 &lp->rx_ring_dma, GFP_KERNEL);
2835 if (!lp->rx_ring)
2836 return -ENOMEM;
2837
2838 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2839 AT91ETHER_MAX_RX_DESCR *
2840 AT91ETHER_MAX_RBUFF_SZ,
2841 &lp->rx_buffers_dma, GFP_KERNEL);
2842 if (!lp->rx_buffers) {
2843 dma_free_coherent(&lp->pdev->dev,
2844 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002845 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002846 lp->rx_ring, lp->rx_ring_dma);
2847 lp->rx_ring = NULL;
2848 return -ENOMEM;
2849 }
2850
2851 addr = lp->rx_buffers_dma;
2852 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002853 desc = macb_rx_desc(lp, i);
2854 macb_set_addr(lp, desc, addr);
2855 desc->ctrl = 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002856 addr += AT91ETHER_MAX_RBUFF_SZ;
2857 }
2858
2859 /* Set the Wrap bit on the last descriptor */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002860 desc->addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002861
2862 /* Reset buffer index */
2863 lp->rx_tail = 0;
2864
2865 /* Program address of descriptor list in Rx Buffer Queue register */
2866 macb_writel(lp, RBQP, lp->rx_ring_dma);
2867
2868 /* Enable Receive and Transmit */
2869 ctl = macb_readl(lp, NCR);
2870 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2871
2872 return 0;
2873}
2874
2875/* Open the ethernet interface */
2876static int at91ether_open(struct net_device *dev)
2877{
2878 struct macb *lp = netdev_priv(dev);
2879 u32 ctl;
2880 int ret;
2881
2882 /* Clear internal statistics */
2883 ctl = macb_readl(lp, NCR);
2884 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2885
2886 macb_set_hwaddr(lp);
2887
2888 ret = at91ether_start(dev);
2889 if (ret)
2890 return ret;
2891
2892 /* Enable MAC interrupts */
2893 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2894 MACB_BIT(RXUBR) |
2895 MACB_BIT(ISR_TUND) |
2896 MACB_BIT(ISR_RLE) |
2897 MACB_BIT(TCOMP) |
2898 MACB_BIT(ISR_ROVR) |
2899 MACB_BIT(HRESP));
2900
2901 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002902 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002903
2904 netif_start_queue(dev);
2905
2906 return 0;
2907}
2908
2909/* Close the interface */
2910static int at91ether_close(struct net_device *dev)
2911{
2912 struct macb *lp = netdev_priv(dev);
2913 u32 ctl;
2914
2915 /* Disable Receiver and Transmitter */
2916 ctl = macb_readl(lp, NCR);
2917 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2918
2919 /* Disable MAC interrupts */
2920 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2921 MACB_BIT(RXUBR) |
2922 MACB_BIT(ISR_TUND) |
2923 MACB_BIT(ISR_RLE) |
2924 MACB_BIT(TCOMP) |
2925 MACB_BIT(ISR_ROVR) |
2926 MACB_BIT(HRESP));
2927
2928 netif_stop_queue(dev);
2929
2930 dma_free_coherent(&lp->pdev->dev,
2931 AT91ETHER_MAX_RX_DESCR *
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002932 macb_dma_desc_get_size(lp),
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002933 lp->rx_ring, lp->rx_ring_dma);
2934 lp->rx_ring = NULL;
2935
2936 dma_free_coherent(&lp->pdev->dev,
2937 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2938 lp->rx_buffers, lp->rx_buffers_dma);
2939 lp->rx_buffers = NULL;
2940
2941 return 0;
2942}
2943
2944/* Transmit packet */
2945static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2946{
2947 struct macb *lp = netdev_priv(dev);
2948
2949 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2950 netif_stop_queue(dev);
2951
2952 /* Store packet information (to free when Tx completed) */
2953 lp->skb = skb;
2954 lp->skb_length = skb->len;
2955 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2956 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03002957 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
2958 dev_kfree_skb_any(skb);
2959 dev->stats.tx_dropped++;
2960 netdev_err(dev, "%s: DMA mapping error\n", __func__);
2961 return NETDEV_TX_OK;
2962 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002963
2964 /* Set address of the data in the Transmit Address register */
2965 macb_writel(lp, TAR, lp->skb_physaddr);
2966 /* Set length of the packet in the Transmit Control register */
2967 macb_writel(lp, TCR, skb->len);
2968
2969 } else {
2970 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2971 return NETDEV_TX_BUSY;
2972 }
2973
2974 return NETDEV_TX_OK;
2975}
2976
2977/* Extract received frame from buffer descriptors and sent to upper layers.
2978 * (Called from interrupt context)
2979 */
2980static void at91ether_rx(struct net_device *dev)
2981{
2982 struct macb *lp = netdev_priv(dev);
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002983 struct macb_dma_desc *desc;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002984 unsigned char *p_recv;
2985 struct sk_buff *skb;
2986 unsigned int pktlen;
2987
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002988 desc = macb_rx_desc(lp, lp->rx_tail);
2989 while (desc->addr & MACB_BIT(RX_USED)) {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002990 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00002991 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002992 skb = netdev_alloc_skb(dev, pktlen + 2);
2993 if (skb) {
2994 skb_reserve(skb, 2);
2995 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2996
2997 skb->protocol = eth_type_trans(skb, dev);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02002998 dev->stats.rx_packets++;
2999 dev->stats.rx_bytes += pktlen;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003000 netif_rx(skb);
3001 } else {
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003002 dev->stats.rx_dropped++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003003 }
3004
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003005 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003006 dev->stats.multicast++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003007
3008 /* reset ownership bit */
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003009 desc->addr &= ~MACB_BIT(RX_USED);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003010
3011 /* wrap after last buffer */
3012 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3013 lp->rx_tail = 0;
3014 else
3015 lp->rx_tail++;
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003016
3017 desc = macb_rx_desc(lp, lp->rx_tail);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003018 }
3019}
3020
3021/* MAC interrupt handler */
3022static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3023{
3024 struct net_device *dev = dev_id;
3025 struct macb *lp = netdev_priv(dev);
3026 u32 intstatus, ctl;
3027
3028 /* MAC Interrupt Status register indicates what interrupts are pending.
3029 * It is automatically cleared once read.
3030 */
3031 intstatus = macb_readl(lp, ISR);
3032
3033 /* Receive complete */
3034 if (intstatus & MACB_BIT(RCOMP))
3035 at91ether_rx(dev);
3036
3037 /* Transmit complete */
3038 if (intstatus & MACB_BIT(TCOMP)) {
3039 /* The TCOM bit is set even if the transmission failed */
3040 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003041 dev->stats.tx_errors++;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003042
3043 if (lp->skb) {
3044 dev_kfree_skb_irq(lp->skb);
3045 lp->skb = NULL;
3046 dma_unmap_single(NULL, lp->skb_physaddr,
3047 lp->skb_length, DMA_TO_DEVICE);
Tobias Klauser5f1d3a52017-04-07 10:17:30 +02003048 dev->stats.tx_packets++;
3049 dev->stats.tx_bytes += lp->skb_length;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003050 }
3051 netif_wake_queue(dev);
3052 }
3053
3054 /* Work-around for EMAC Errata section 41.3.1 */
3055 if (intstatus & MACB_BIT(RXUBR)) {
3056 ctl = macb_readl(lp, NCR);
3057 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08003058 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003059 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3060 }
3061
3062 if (intstatus & MACB_BIT(ISR_ROVR))
3063 netdev_err(dev, "ROVR error\n");
3064
3065 return IRQ_HANDLED;
3066}
3067
3068#ifdef CONFIG_NET_POLL_CONTROLLER
3069static void at91ether_poll_controller(struct net_device *dev)
3070{
3071 unsigned long flags;
3072
3073 local_irq_save(flags);
3074 at91ether_interrupt(dev->irq, dev);
3075 local_irq_restore(flags);
3076}
3077#endif
3078
3079static const struct net_device_ops at91ether_netdev_ops = {
3080 .ndo_open = at91ether_open,
3081 .ndo_stop = at91ether_close,
3082 .ndo_start_xmit = at91ether_start_xmit,
3083 .ndo_get_stats = macb_get_stats,
3084 .ndo_set_rx_mode = macb_set_rx_mode,
3085 .ndo_set_mac_address = eth_mac_addr,
3086 .ndo_do_ioctl = macb_ioctl,
3087 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003088#ifdef CONFIG_NET_POLL_CONTROLLER
3089 .ndo_poll_controller = at91ether_poll_controller,
3090#endif
3091};
3092
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003093static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303094 struct clk **hclk, struct clk **tx_clk,
3095 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003096{
3097 int err;
3098
3099 *hclk = NULL;
3100 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303101 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003102
3103 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3104 if (IS_ERR(*pclk))
3105 return PTR_ERR(*pclk);
3106
3107 err = clk_prepare_enable(*pclk);
3108 if (err) {
3109 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3110 return err;
3111 }
3112
3113 return 0;
3114}
3115
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003116static int at91ether_init(struct platform_device *pdev)
3117{
3118 struct net_device *dev = platform_get_drvdata(pdev);
3119 struct macb *bp = netdev_priv(dev);
3120 int err;
3121 u32 reg;
3122
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003123 dev->netdev_ops = &at91ether_netdev_ops;
3124 dev->ethtool_ops = &macb_ethtool_ops;
3125
3126 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3127 0, dev->name, dev);
3128 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003129 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003130
3131 macb_writel(bp, NCR, 0);
3132
3133 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3134 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3135 reg |= MACB_BIT(RM9200_RMII);
3136
3137 macb_writel(bp, NCFGR, reg);
3138
3139 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003140}
3141
David S. Miller3cef5c52015-03-09 23:38:02 -04003142static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003143 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003144 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003145 .init = macb_init,
3146};
3147
David S. Miller3cef5c52015-03-09 23:38:02 -04003148static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003149 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3150 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003151 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003152 .init = macb_init,
3153};
3154
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003155static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003156 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003157 .dma_burst_length = 16,
3158 .clk_init = macb_clk_init,
3159 .init = macb_init,
3160};
3161
David S. Miller3cef5c52015-03-09 23:38:02 -04003162static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003163 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3164 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003165 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003166 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003167 .init = macb_init,
3168};
3169
David S. Miller3cef5c52015-03-09 23:38:02 -04003170static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003171 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003172 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003173 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003174 .init = macb_init,
3175};
3176
David S. Miller3cef5c52015-03-09 23:38:02 -04003177static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003178 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003179 .init = at91ether_init,
3180};
3181
Neil Armstronge611b5b2016-01-05 14:39:17 +01003182static const struct macb_config np4_config = {
3183 .caps = MACB_CAPS_USRIO_DISABLED,
3184 .clk_init = macb_clk_init,
3185 .init = macb_init,
3186};
David S. Miller36583eb2015-05-23 01:22:35 -04003187
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303188static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303189 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303190 .dma_burst_length = 16,
3191 .clk_init = macb_clk_init,
3192 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303193 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303194};
3195
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003196static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303197 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003198 .dma_burst_length = 16,
3199 .clk_init = macb_clk_init,
3200 .init = macb_init,
3201};
3202
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003203static const struct of_device_id macb_dt_ids[] = {
3204 { .compatible = "cdns,at32ap7000-macb" },
3205 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3206 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003207 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003208 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3209 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003210 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003211 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3212 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3213 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3214 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303215 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003216 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003217 { /* sentinel */ }
3218};
3219MODULE_DEVICE_TABLE(of, macb_dt_ids);
3220#endif /* CONFIG_OF */
3221
Bartosz Folta83a77e92016-12-14 06:39:15 +00003222static const struct macb_config default_gem_config = {
3223 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
3224 .dma_burst_length = 16,
3225 .clk_init = macb_clk_init,
3226 .init = macb_init,
3227 .jumbo_max_len = 10240,
3228};
3229
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003230static int macb_probe(struct platform_device *pdev)
3231{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003232 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003233 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303234 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003235 = macb_config->clk_init;
3236 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003237 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003238 struct device_node *phy_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303239 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003240 unsigned int queue_mask, num_queues;
3241 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003242 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003243 struct phy_device *phydev;
3244 struct net_device *dev;
3245 struct resource *regs;
3246 void __iomem *mem;
3247 const char *mac;
3248 struct macb *bp;
3249 int err;
3250
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003251 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3252 mem = devm_ioremap_resource(&pdev->dev, regs);
3253 if (IS_ERR(mem))
3254 return PTR_ERR(mem);
3255
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003256 if (np) {
3257 const struct of_device_id *match;
3258
3259 match = of_match_node(macb_dt_ids, np);
3260 if (match && match->data) {
3261 macb_config = match->data;
3262 clk_init = macb_config->clk_init;
3263 init = macb_config->init;
3264 }
3265 }
3266
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303267 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003268 if (err)
3269 return err;
3270
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003271 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003272
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003273 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003274 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003275 if (!dev) {
3276 err = -ENOMEM;
3277 goto err_disable_clocks;
3278 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003279
3280 dev->base_addr = regs->start;
3281
3282 SET_NETDEV_DEV(dev, &pdev->dev);
3283
3284 bp = netdev_priv(dev);
3285 bp->pdev = pdev;
3286 bp->dev = dev;
3287 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003288 bp->native_io = native_io;
3289 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003290 bp->macb_reg_readl = hw_readl_native;
3291 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003292 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003293 bp->macb_reg_readl = hw_readl;
3294 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003295 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003296 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003297 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003298 if (macb_config)
3299 bp->dma_burst_length = macb_config->dma_burst_length;
3300 bp->pclk = pclk;
3301 bp->hclk = hclk;
3302 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303303 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003304 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303305 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303306
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003307 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003308 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003309 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3310 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3311
Harini Katakamfff80192016-08-09 13:15:53 +05303312#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003313 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
Harini Katakamfff80192016-08-09 13:15:53 +05303314 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
Rafal Ozieblodc97a892017-01-27 15:08:20 +00003315 bp->hw_dma_cap = HW_DMA_CAP_64B;
3316 } else
3317 bp->hw_dma_cap = HW_DMA_CAP_32B;
Harini Katakamfff80192016-08-09 13:15:53 +05303318#endif
3319
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003320 spin_lock_init(&bp->lock);
3321
Nicolas Ferread783472015-03-31 15:02:02 +02003322 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003323 macb_configure_caps(bp, macb_config);
3324
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003325 platform_set_drvdata(pdev, dev);
3326
3327 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003328 if (dev->irq < 0) {
3329 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003330 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003331 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003332
Jarod Wilson44770e12016-10-17 15:54:17 -04003333 /* MTU range: 68 - 1500 or 10240 */
3334 dev->min_mtu = GEM_MTU_MIN_SIZE;
3335 if (bp->caps & MACB_CAPS_JUMBO)
3336 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3337 else
3338 dev->max_mtu = ETH_DATA_LEN;
3339
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003340 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003341 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003342 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003343 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003344 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003345
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003346 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003347 phy_node = of_get_next_available_child(np, NULL);
3348 if (phy_node) {
3349 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003350
Charles Keepax0e3e7992016-03-28 13:47:42 +01003351 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003352 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003353 gpiod_direction_output(bp->reset_gpio, 1);
3354 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003355 }
3356 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003357
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003358 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003359 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003360 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003361 if (pdata && pdata->is_rmii)
3362 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3363 else
3364 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3365 } else {
3366 bp->phy_interface = err;
3367 }
3368
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003369 /* IP specific init */
3370 err = init(pdev);
3371 if (err)
3372 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003373
Florian Fainellicf669662016-05-02 18:38:45 -07003374 err = macb_mii_init(bp);
3375 if (err)
3376 goto err_out_free_netdev;
3377
Philippe Reynes0a912812016-06-22 00:32:35 +02003378 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003379
3380 netif_carrier_off(dev);
3381
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003382 err = register_netdev(dev);
3383 if (err) {
3384 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003385 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003386 }
3387
Florian Fainellicf669662016-05-02 18:38:45 -07003388 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003389
Bo Shen58798232014-09-13 01:57:49 +02003390 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3391 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3392 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003393
3394 return 0;
3395
Florian Fainellicf669662016-05-02 18:38:45 -07003396err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003397 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003398 mdiobus_unregister(bp->mii_bus);
3399 mdiobus_free(bp->mii_bus);
3400
3401 /* Shutdown the PHY if there is a GPIO reset */
3402 if (bp->reset_gpio)
3403 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003404
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003405err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003406 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003407
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003408err_disable_clocks:
3409 clk_disable_unprepare(tx_clk);
3410 clk_disable_unprepare(hclk);
3411 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303412 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003413
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003414 return err;
3415}
3416
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003417static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003418{
3419 struct net_device *dev;
3420 struct macb *bp;
3421
3422 dev = platform_get_drvdata(pdev);
3423
3424 if (dev) {
3425 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003426 if (dev->phydev)
3427 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003428 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003429 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003430 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003431
3432 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003433 if (bp->reset_gpio)
3434 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003435
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003436 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003437 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003438 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003439 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303440 clk_disable_unprepare(bp->rx_clk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003441 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003442 }
3443
3444 return 0;
3445}
3446
Michal Simekd23823d2015-01-23 09:36:03 +01003447static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003448{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003449 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003450 struct net_device *netdev = platform_get_drvdata(pdev);
3451 struct macb *bp = netdev_priv(netdev);
3452
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003453 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003454 netif_device_detach(netdev);
3455
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003456 if (bp->wol & MACB_WOL_ENABLED) {
3457 macb_writel(bp, IER, MACB_BIT(WOL));
3458 macb_writel(bp, WOL, MACB_BIT(MAG));
3459 enable_irq_wake(bp->queues[0].irq);
3460 } else {
3461 clk_disable_unprepare(bp->tx_clk);
3462 clk_disable_unprepare(bp->hclk);
3463 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303464 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003465 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003466
3467 return 0;
3468}
3469
Michal Simekd23823d2015-01-23 09:36:03 +01003470static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003471{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003472 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003473 struct net_device *netdev = platform_get_drvdata(pdev);
3474 struct macb *bp = netdev_priv(netdev);
3475
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003476 if (bp->wol & MACB_WOL_ENABLED) {
3477 macb_writel(bp, IDR, MACB_BIT(WOL));
3478 macb_writel(bp, WOL, 0);
3479 disable_irq_wake(bp->queues[0].irq);
3480 } else {
3481 clk_prepare_enable(bp->pclk);
3482 clk_prepare_enable(bp->hclk);
3483 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303484 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003485 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003486
3487 netif_device_attach(netdev);
3488
3489 return 0;
3490}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003491
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003492static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3493
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003494static struct platform_driver macb_driver = {
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003495 .probe = macb_probe,
3496 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003497 .driver = {
3498 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003499 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003500 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003501 },
3502};
3503
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003504module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003505
3506MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003507MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003508MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003509MODULE_ALIAS("platform:macb");