blob: 654b5bf0e2c370f5380ace3dc50f98c1f30db66b [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
Zach Brownb410d132016-10-19 09:56:57 -050046#define RX_RING_BYTES(bp) (sizeof(struct macb_dma_desc) \
47 * (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
Zach Brownb410d132016-10-19 09:56:57 -050052#define TX_RING_BYTES(bp) (sizeof(struct macb_dma_desc) \
53 * (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
Havard Skinnemoen55054a12012-10-31 06:04:55 +000081/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -050082static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000083{
Zach Brownb410d132016-10-19 09:56:57 -050084 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +000085}
86
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010087static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
88 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000089{
Zach Brownb410d132016-10-19 09:56:57 -050090 return &queue->tx_ring[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000091}
92
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010093static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
94 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000095{
Zach Brownb410d132016-10-19 09:56:57 -050096 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000097}
98
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010099static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000100{
101 dma_addr_t offset;
102
Zach Brownb410d132016-10-19 09:56:57 -0500103 offset = macb_tx_ring_wrap(queue->bp, index) *
104 sizeof(struct macb_dma_desc);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000105
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100106 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000107}
108
Zach Brownb410d132016-10-19 09:56:57 -0500109static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000110{
Zach Brownb410d132016-10-19 09:56:57 -0500111 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000112}
113
114static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
115{
Zach Brownb410d132016-10-19 09:56:57 -0500116 return &bp->rx_ring[macb_rx_ring_wrap(bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000117}
118
119static void *macb_rx_buffer(struct macb *bp, unsigned int index)
120{
Zach Brownb410d132016-10-19 09:56:57 -0500121 return bp->rx_buffers + bp->rx_buffer_size *
122 macb_rx_ring_wrap(bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000123}
124
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300125/* I/O accessors */
126static u32 hw_readl_native(struct macb *bp, int offset)
127{
128 return __raw_readl(bp->regs + offset);
129}
130
131static void hw_writel_native(struct macb *bp, int offset, u32 value)
132{
133 __raw_writel(value, bp->regs + offset);
134}
135
136static u32 hw_readl(struct macb *bp, int offset)
137{
138 return readl_relaxed(bp->regs + offset);
139}
140
141static void hw_writel(struct macb *bp, int offset, u32 value)
142{
143 writel_relaxed(value, bp->regs + offset);
144}
145
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700146/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700147 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300148 * descriptor access.
149 */
150static bool hw_is_native_io(void __iomem *addr)
151{
152 u32 value = MACB_BIT(LLB);
153
154 __raw_writel(value, addr + MACB_NCR);
155 value = __raw_readl(addr + MACB_NCR);
156
157 /* Write 0 back to disable everything */
158 __raw_writel(0, addr + MACB_NCR);
159
160 return value == MACB_BIT(LLB);
161}
162
163static bool hw_is_gem(void __iomem *addr, bool native_io)
164{
165 u32 id;
166
167 if (native_io)
168 id = __raw_readl(addr + MACB_MID);
169 else
170 id = readl_relaxed(addr + MACB_MID);
171
172 return MACB_BFEXT(IDNUM, id) >= 0x2;
173}
174
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100175static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100176{
177 u32 bottom;
178 u16 top;
179
180 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000181 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100182 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000183 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000184
185 /* Clear unused address register sets */
186 macb_or_gem_writel(bp, SA2B, 0);
187 macb_or_gem_writel(bp, SA2T, 0);
188 macb_or_gem_writel(bp, SA3B, 0);
189 macb_or_gem_writel(bp, SA3T, 0);
190 macb_or_gem_writel(bp, SA4B, 0);
191 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100192}
193
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100194static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100195{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000196 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100197 u32 bottom;
198 u16 top;
199 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000200 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100201
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900202 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000203
Moritz Fischeraa50b552016-03-29 19:11:13 -0700204 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000205 for (i = 0; i < 4; i++) {
206 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
207 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100208
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000209 if (pdata && pdata->rev_eth_addr) {
210 addr[5] = bottom & 0xff;
211 addr[4] = (bottom >> 8) & 0xff;
212 addr[3] = (bottom >> 16) & 0xff;
213 addr[2] = (bottom >> 24) & 0xff;
214 addr[1] = top & 0xff;
215 addr[0] = (top & 0xff00) >> 8;
216 } else {
217 addr[0] = bottom & 0xff;
218 addr[1] = (bottom >> 8) & 0xff;
219 addr[2] = (bottom >> 16) & 0xff;
220 addr[3] = (bottom >> 24) & 0xff;
221 addr[4] = top & 0xff;
222 addr[5] = (top >> 8) & 0xff;
223 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100224
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000225 if (is_valid_ether_addr(addr)) {
226 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
227 return;
228 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700229 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000230
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300231 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000232 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100233}
234
frederic RODO6c36a702007-07-12 19:07:24 +0200235static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100236{
frederic RODO6c36a702007-07-12 19:07:24 +0200237 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100238 int value;
239
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100240 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
241 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200242 | MACB_BF(PHYA, mii_id)
243 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100244 | MACB_BF(CODE, MACB_MAN_CODE)));
245
frederic RODO6c36a702007-07-12 19:07:24 +0200246 /* wait for end of transfer */
247 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
248 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100249
250 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100251
252 return value;
253}
254
frederic RODO6c36a702007-07-12 19:07:24 +0200255static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
256 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100257{
frederic RODO6c36a702007-07-12 19:07:24 +0200258 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100259
260 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
261 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200262 | MACB_BF(PHYA, mii_id)
263 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100264 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200265 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100266
frederic RODO6c36a702007-07-12 19:07:24 +0200267 /* wait for end of transfer */
268 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
269 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100270
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100271 return 0;
272}
273
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800274/**
275 * macb_set_tx_clk() - Set a clock to a new frequency
276 * @clk Pointer to the clock to change
277 * @rate New frequency in Hz
278 * @dev Pointer to the struct net_device
279 */
280static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
281{
282 long ferr, rate, rate_rounded;
283
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100284 if (!clk)
285 return;
286
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800287 switch (speed) {
288 case SPEED_10:
289 rate = 2500000;
290 break;
291 case SPEED_100:
292 rate = 25000000;
293 break;
294 case SPEED_1000:
295 rate = 125000000;
296 break;
297 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800298 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800299 }
300
301 rate_rounded = clk_round_rate(clk, rate);
302 if (rate_rounded < 0)
303 return;
304
305 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
306 * is not satisfied.
307 */
308 ferr = abs(rate_rounded - rate);
309 ferr = DIV_ROUND_UP(ferr, rate / 100000);
310 if (ferr > 5)
311 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700312 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800313
314 if (clk_set_rate(clk, rate_rounded))
315 netdev_err(dev, "adjusting tx_clk failed.\n");
316}
317
frederic RODO6c36a702007-07-12 19:07:24 +0200318static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100319{
frederic RODO6c36a702007-07-12 19:07:24 +0200320 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200321 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200322 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200323 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100324
frederic RODO6c36a702007-07-12 19:07:24 +0200325 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100326
frederic RODO6c36a702007-07-12 19:07:24 +0200327 if (phydev->link) {
328 if ((bp->speed != phydev->speed) ||
329 (bp->duplex != phydev->duplex)) {
330 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100331
frederic RODO6c36a702007-07-12 19:07:24 +0200332 reg = macb_readl(bp, NCFGR);
333 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000334 if (macb_is_gem(bp))
335 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200336
337 if (phydev->duplex)
338 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900339 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200340 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200341 if (phydev->speed == SPEED_1000 &&
342 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000343 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200344
Patrice Vilchez140b7552012-10-31 06:04:50 +0000345 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200346
347 bp->speed = phydev->speed;
348 bp->duplex = phydev->duplex;
349 status_change = 1;
350 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351 }
352
frederic RODO6c36a702007-07-12 19:07:24 +0200353 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700354 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200355 bp->speed = 0;
356 bp->duplex = -1;
357 }
358 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100359
frederic RODO6c36a702007-07-12 19:07:24 +0200360 status_change = 1;
361 }
362
363 spin_unlock_irqrestore(&bp->lock, flags);
364
365 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000366 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500367 /* Update the TX clock rate if and only if the link is
368 * up and there has been a link change.
369 */
370 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
371
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000372 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000373 netdev_info(dev, "link up (%d/%s)\n",
374 phydev->speed,
375 phydev->duplex == DUPLEX_FULL ?
376 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000377 } else {
378 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000379 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000380 }
frederic RODO6c36a702007-07-12 19:07:24 +0200381 }
382}
383
384/* based on au1000_eth. c*/
385static int macb_mii_probe(struct net_device *dev)
386{
387 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000388 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000389 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000390 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000391 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200392
Jiri Pirko7455a762010-02-08 05:12:08 +0000393 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200394 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000395 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200396 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200397 }
398
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000399 pdata = dev_get_platdata(&bp->pdev->dev);
400 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700401 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin,
402 "phy int");
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000403 if (!ret) {
404 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
405 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
406 }
407 }
frederic RODO6c36a702007-07-12 19:07:24 +0200408
409 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000410 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100411 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000412 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000413 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000414 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200415 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100416
frederic RODO6c36a702007-07-12 19:07:24 +0200417 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200418 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000419 phydev->supported &= PHY_GBIT_FEATURES;
420 else
421 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100422
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500423 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
424 phydev->supported &= ~SUPPORTED_1000baseT_Half;
425
frederic RODO6c36a702007-07-12 19:07:24 +0200426 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100427
frederic RODO6c36a702007-07-12 19:07:24 +0200428 bp->link = 0;
429 bp->speed = 0;
430 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200431
432 return 0;
433}
434
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100435static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200436{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000437 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200438 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200439 int err = -ENXIO, i;
440
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200441 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200442 macb_writel(bp, NCR, MACB_BIT(MPE));
443
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700444 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700445 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200446 err = -ENOMEM;
447 goto err_out;
448 }
449
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700450 bp->mii_bus->name = "MACB_mii_bus";
451 bp->mii_bus->read = &macb_mdio_read;
452 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000453 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700454 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700455 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700456 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900457 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700458
Jamie Iles91523942011-02-28 04:05:25 +0000459 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200460
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200461 np = bp->pdev->dev.of_node;
462 if (np) {
463 /* try dt phy registration */
464 err = of_mdiobus_register(bp->mii_bus, np);
465
466 /* fallback to standard phy registration if no phy were
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700467 * found during dt phy registration
468 */
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200469 if (!err && !phy_find_first(bp->mii_bus)) {
470 for (i = 0; i < PHY_MAX_ADDR; i++) {
471 struct phy_device *phydev;
472
473 phydev = mdiobus_scan(bp->mii_bus, i);
Sergei Shtylyovce24c2b2016-05-01 01:47:36 +0300474 if (IS_ERR(phydev) &&
475 PTR_ERR(phydev) != -ENODEV) {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200476 err = PTR_ERR(phydev);
477 break;
478 }
479 }
480
481 if (err)
482 goto err_out_unregister_bus;
483 }
484 } else {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200485 if (pdata)
486 bp->mii_bus->phy_mask = pdata->phy_mask;
487
488 err = mdiobus_register(bp->mii_bus);
489 }
490
491 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100492 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200493
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200494 err = macb_mii_probe(bp->dev);
495 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200496 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200497
498 return 0;
499
500err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700501 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700502err_out_free_mdiobus:
503 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200504err_out:
505 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100506}
507
508static void macb_update_stats(struct macb *bp)
509{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000510 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
511 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300512 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100513
514 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
515
Moritz Fischer96ec6312016-03-29 19:11:11 -0700516 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700517 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100518}
519
Nicolas Ferree86cd532012-10-31 06:04:57 +0000520static int macb_halt_tx(struct macb *bp)
521{
522 unsigned long halt_time, timeout;
523 u32 status;
524
525 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
526
527 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
528 do {
529 halt_time = jiffies;
530 status = macb_readl(bp, TSR);
531 if (!(status & MACB_BIT(TGO)))
532 return 0;
533
534 usleep_range(10, 250);
535 } while (time_before(halt_time, timeout));
536
537 return -ETIMEDOUT;
538}
539
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200540static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
541{
542 if (tx_skb->mapping) {
543 if (tx_skb->mapped_as_page)
544 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
545 tx_skb->size, DMA_TO_DEVICE);
546 else
547 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
548 tx_skb->size, DMA_TO_DEVICE);
549 tx_skb->mapping = 0;
550 }
551
552 if (tx_skb->skb) {
553 dev_kfree_skb_any(tx_skb->skb);
554 tx_skb->skb = NULL;
555 }
556}
557
Harini Katakamfff80192016-08-09 13:15:53 +0530558static inline void macb_set_addr(struct macb_dma_desc *desc, dma_addr_t addr)
559{
560 desc->addr = (u32)addr;
561#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
562 desc->addrh = (u32)(addr >> 32);
563#endif
564}
565
Nicolas Ferree86cd532012-10-31 06:04:57 +0000566static void macb_tx_error_task(struct work_struct *work)
567{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100568 struct macb_queue *queue = container_of(work, struct macb_queue,
569 tx_error_task);
570 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000571 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100572 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000573 struct sk_buff *skb;
574 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100575 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000576
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100577 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
578 (unsigned int)(queue - bp->queues),
579 queue->tx_tail, queue->tx_head);
580
581 /* Prevent the queue IRQ handlers from running: each of them may call
582 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
583 * As explained below, we have to halt the transmission before updating
584 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
585 * network engine about the macb/gem being halted.
586 */
587 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000588
589 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100590 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000591
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700592 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000593 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100594 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000595 */
596 if (macb_halt_tx(bp))
597 /* Just complain for now, reinitializing TX path can be good */
598 netdev_err(bp->dev, "BUG: halt tx timed out\n");
599
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700600 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000601 * Free transmit buffers in upper layer.
602 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100603 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
604 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000605
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100606 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000607 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100608 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000609 skb = tx_skb->skb;
610
611 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200612 /* skb is set for the last buffer of the frame */
613 while (!skb) {
614 macb_tx_unmap(bp, tx_skb);
615 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100616 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200617 skb = tx_skb->skb;
618 }
619
620 /* ctrl still refers to the first buffer descriptor
621 * since it's the only one written back by the hardware
622 */
623 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
624 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500625 macb_tx_ring_wrap(bp, tail),
626 skb->data);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200627 bp->stats.tx_packets++;
628 bp->stats.tx_bytes += skb->len;
629 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000630 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700631 /* "Buffers exhausted mid-frame" errors may only happen
632 * if the driver is buggy, so complain loudly about
633 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000634 */
635 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
636 netdev_err(bp->dev,
637 "BUG: TX buffers exhausted mid-frame\n");
638
639 desc->ctrl = ctrl | MACB_BIT(TX_USED);
640 }
641
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200642 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000643 }
644
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100645 /* Set end of TX queue */
646 desc = macb_tx_desc(queue, 0);
Harini Katakamfff80192016-08-09 13:15:53 +0530647 macb_set_addr(desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100648 desc->ctrl = MACB_BIT(TX_USED);
649
Nicolas Ferree86cd532012-10-31 06:04:57 +0000650 /* Make descriptor updates visible to hardware */
651 wmb();
652
653 /* Reinitialize the TX desc queue */
Harini Katakamfff80192016-08-09 13:15:53 +0530654 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
655#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
656 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
657#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000658 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100659 queue->tx_head = 0;
660 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000661
662 /* Housework before enabling TX IRQ */
663 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100664 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
665
666 /* Now we are ready to start transmission again */
667 netif_tx_start_all_queues(bp->dev);
668 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
669
670 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000671}
672
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100673static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674{
675 unsigned int tail;
676 unsigned int head;
677 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100678 struct macb *bp = queue->bp;
679 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100680
681 status = macb_readl(bp, TSR);
682 macb_writel(bp, TSR, status);
683
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000684 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100685 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000686
Nicolas Ferree86cd532012-10-31 06:04:57 +0000687 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700688 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100689
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100690 head = queue->tx_head;
691 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000692 struct macb_tx_skb *tx_skb;
693 struct sk_buff *skb;
694 struct macb_dma_desc *desc;
695 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100696
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100697 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100698
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000699 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100700 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000701
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000702 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100703
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200704 /* TX_USED bit is only set by hardware on the very first buffer
705 * descriptor of the transmitted frame.
706 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000707 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100708 break;
709
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200710 /* Process all buffers of the current transmitted frame */
711 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100712 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200713 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000714
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200715 /* First, update TX stats if needed */
716 if (skb) {
717 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500718 macb_tx_ring_wrap(bp, tail),
719 skb->data);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200720 bp->stats.tx_packets++;
721 bp->stats.tx_bytes += skb->len;
722 }
723
724 /* Now we can safely release resources */
725 macb_tx_unmap(bp, tx_skb);
726
727 /* skb is set only for the last buffer of the frame.
728 * WARNING: at this point skb has been freed by
729 * macb_tx_unmap().
730 */
731 if (skb)
732 break;
733 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100734 }
735
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100736 queue->tx_tail = tail;
737 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
738 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500739 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100740 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100741}
742
Nicolas Ferre4df95132013-06-04 21:57:12 +0000743static void gem_rx_refill(struct macb *bp)
744{
745 unsigned int entry;
746 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000747 dma_addr_t paddr;
748
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700749 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500750 bp->rx_ring_size) > 0) {
751 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000752
753 /* Make hw descriptor updates visible to CPU */
754 rmb();
755
Nicolas Ferre4df95132013-06-04 21:57:12 +0000756 bp->rx_prepared_head++;
757
Moritz Fischeraa50b552016-03-29 19:11:13 -0700758 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000759 /* allocate sk_buff for this free entry in ring */
760 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700761 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000762 netdev_err(bp->dev,
763 "Unable to allocate sk_buff\n");
764 break;
765 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000766
767 /* now fill corresponding descriptor entry */
768 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700769 bp->rx_buffer_size,
770 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800771 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
772 dev_kfree_skb(skb);
773 break;
774 }
775
776 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000777
Zach Brownb410d132016-10-19 09:56:57 -0500778 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000779 paddr |= MACB_BIT(RX_WRAP);
Harini Katakamfff80192016-08-09 13:15:53 +0530780 macb_set_addr(&(bp->rx_ring[entry]), paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000781 bp->rx_ring[entry].ctrl = 0;
782
783 /* properly align Ethernet header */
784 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530785 } else {
786 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
787 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000788 }
789 }
790
791 /* Make descriptor updates visible to hardware */
792 wmb();
793
794 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700795 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000796}
797
798/* Mark DMA descriptors from begin up to and not including end as unused */
799static void discard_partial_frame(struct macb *bp, unsigned int begin,
800 unsigned int end)
801{
802 unsigned int frag;
803
804 for (frag = begin; frag != end; frag++) {
805 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700806
Nicolas Ferre4df95132013-06-04 21:57:12 +0000807 desc->addr &= ~MACB_BIT(RX_USED);
808 }
809
810 /* Make descriptor updates visible to hardware */
811 wmb();
812
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700813 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000814 * whatever caused this is updated, so we don't have to record
815 * anything.
816 */
817}
818
819static int gem_rx(struct macb *bp, int budget)
820{
821 unsigned int len;
822 unsigned int entry;
823 struct sk_buff *skb;
824 struct macb_dma_desc *desc;
825 int count = 0;
826
827 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530828 u32 ctrl;
829 dma_addr_t addr;
830 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000831
Zach Brownb410d132016-10-19 09:56:57 -0500832 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000833 desc = &bp->rx_ring[entry];
834
835 /* Make hw descriptor updates visible to CPU */
836 rmb();
837
Harini Katakamfff80192016-08-09 13:15:53 +0530838 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
839 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
840#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
841 addr |= ((u64)(desc->addrh) << 32);
842#endif
Nicolas Ferre4df95132013-06-04 21:57:12 +0000843 ctrl = desc->ctrl;
844
Harini Katakamfff80192016-08-09 13:15:53 +0530845 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000846 break;
847
Nicolas Ferre4df95132013-06-04 21:57:12 +0000848 bp->rx_tail++;
849 count++;
850
851 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
852 netdev_err(bp->dev,
853 "not whole frame pointed by descriptor\n");
854 bp->stats.rx_dropped++;
855 break;
856 }
857 skb = bp->rx_skbuff[entry];
858 if (unlikely(!skb)) {
859 netdev_err(bp->dev,
860 "inconsistent Rx descriptor chain\n");
861 bp->stats.rx_dropped++;
862 break;
863 }
864 /* now everything is ready for receiving packet */
865 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530866 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000867
868 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
869
870 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000871 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800872 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000873
874 skb->protocol = eth_type_trans(skb, bp->dev);
875 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200876 if (bp->dev->features & NETIF_F_RXCSUM &&
877 !(bp->dev->flags & IFF_PROMISC) &&
878 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
879 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000880
881 bp->stats.rx_packets++;
882 bp->stats.rx_bytes += skb->len;
883
884#if defined(DEBUG) && defined(VERBOSE_DEBUG)
885 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
886 skb->len, skb->csum);
887 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100888 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000889 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
890 skb->data, 32, true);
891#endif
892
893 netif_receive_skb(skb);
894 }
895
896 gem_rx_refill(bp);
897
898 return count;
899}
900
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100901static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
902 unsigned int last_frag)
903{
904 unsigned int len;
905 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000906 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100907 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000908 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100909
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000910 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530911 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100912
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000913 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -0500914 macb_rx_ring_wrap(bp, first_frag),
915 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100916
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700917 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000918 * first buffer. Since the header is 14 bytes, this makes the
919 * payload word-aligned.
920 *
921 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
922 * the two padding bytes into the skb so that we avoid hitting
923 * the slowpath in memcpy(), and pull them off afterwards.
924 */
925 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100926 if (!skb) {
927 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000928 for (frag = first_frag; ; frag++) {
929 desc = macb_rx_desc(bp, frag);
930 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100931 if (frag == last_frag)
932 break;
933 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000934
935 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100936 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000937
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100938 return 1;
939 }
940
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000941 offset = 0;
942 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700943 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100944 skb_put(skb, len);
945
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000946 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000947 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100948
949 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100950 if (unlikely(frag != last_frag)) {
951 dev_kfree_skb_any(skb);
952 return -1;
953 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100954 frag_len = len - offset;
955 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300956 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -0700957 macb_rx_buffer(bp, frag),
958 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000959 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000960 desc = macb_rx_desc(bp, frag);
961 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100962
963 if (frag == last_frag)
964 break;
965 }
966
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000967 /* Make descriptor updates visible to hardware */
968 wmb();
969
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000970 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100971 skb->protocol = eth_type_trans(skb, bp->dev);
972
973 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000974 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000975 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700976 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100977 netif_receive_skb(skb);
978
979 return 0;
980}
981
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100982static inline void macb_init_rx_ring(struct macb *bp)
983{
984 dma_addr_t addr;
985 int i;
986
987 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -0500988 for (i = 0; i < bp->rx_ring_size; i++) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100989 bp->rx_ring[i].addr = addr;
990 bp->rx_ring[i].ctrl = 0;
991 addr += bp->rx_buffer_size;
992 }
Zach Brownb410d132016-10-19 09:56:57 -0500993 bp->rx_ring[bp->rx_ring_size - 1].addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100994}
995
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100996static int macb_rx(struct macb *bp, int budget)
997{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100998 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100999 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001000 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001001 int first_frag = -1;
1002
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001003 for (tail = bp->rx_tail; budget > 0; tail++) {
1004 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001005 u32 addr, ctrl;
1006
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001007 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001008 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001009
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001010 addr = desc->addr;
1011 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001012
1013 if (!(addr & MACB_BIT(RX_USED)))
1014 break;
1015
1016 if (ctrl & MACB_BIT(RX_SOF)) {
1017 if (first_frag != -1)
1018 discard_partial_frame(bp, first_frag, tail);
1019 first_frag = tail;
1020 }
1021
1022 if (ctrl & MACB_BIT(RX_EOF)) {
1023 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001024
1025 if (unlikely(first_frag == -1)) {
1026 reset_rx_queue = true;
1027 continue;
1028 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001029
1030 dropped = macb_rx_frame(bp, first_frag, tail);
1031 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001032 if (unlikely(dropped < 0)) {
1033 reset_rx_queue = true;
1034 continue;
1035 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001036 if (!dropped) {
1037 received++;
1038 budget--;
1039 }
1040 }
1041 }
1042
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001043 if (unlikely(reset_rx_queue)) {
1044 unsigned long flags;
1045 u32 ctrl;
1046
1047 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1048
1049 spin_lock_irqsave(&bp->lock, flags);
1050
1051 ctrl = macb_readl(bp, NCR);
1052 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1053
1054 macb_init_rx_ring(bp);
1055 macb_writel(bp, RBQP, bp->rx_ring_dma);
1056
1057 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1058
1059 spin_unlock_irqrestore(&bp->lock, flags);
1060 return received;
1061 }
1062
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001063 if (first_frag != -1)
1064 bp->rx_tail = first_frag;
1065 else
1066 bp->rx_tail = tail;
1067
1068 return received;
1069}
1070
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001071static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001072{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001073 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001074 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001075 u32 status;
1076
1077 status = macb_readl(bp, RSR);
1078 macb_writel(bp, RSR, status);
1079
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001080 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001081
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001082 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001083 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001084
Nicolas Ferre4df95132013-06-04 21:57:12 +00001085 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001086 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001087 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001088
Nicolas Ferre8770e912013-02-12 11:08:48 +01001089 /* Packets received while interrupts were disabled */
1090 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001091 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001092 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1093 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001094 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001095 } else {
1096 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1097 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001098 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001099
1100 /* TODO: Handle errors */
1101
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001102 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001103}
1104
1105static irqreturn_t macb_interrupt(int irq, void *dev_id)
1106{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001107 struct macb_queue *queue = dev_id;
1108 struct macb *bp = queue->bp;
1109 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001110 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001111
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001112 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001113
1114 if (unlikely(!status))
1115 return IRQ_NONE;
1116
1117 spin_lock(&bp->lock);
1118
1119 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001120 /* close possible race with dev_close */
1121 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001122 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001123 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1124 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001125 break;
1126 }
1127
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001128 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1129 (unsigned int)(queue - bp->queues),
1130 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001131
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001132 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001133 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001134 * until we have processed the buffers. The
1135 * scheduling call may fail if the poll routine
1136 * is already scheduled, so disable interrupts
1137 * now.
1138 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001139 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001140 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001141 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001142
Ben Hutchings288379f2009-01-19 16:43:59 -08001143 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001144 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001145 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001146 }
1147 }
1148
Nicolas Ferree86cd532012-10-31 06:04:57 +00001149 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001150 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1151 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001152
1153 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001154 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001155
Nicolas Ferree86cd532012-10-31 06:04:57 +00001156 break;
1157 }
1158
1159 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001160 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001161
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001162 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001163 * add that if/when we get our hands on a full-blown MII PHY.
1164 */
1165
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001166 /* There is a hardware issue under heavy load where DMA can
1167 * stop, this causes endless "used buffer descriptor read"
1168 * interrupts but it can be cleared by re-enabling RX. See
1169 * the at91 manual, section 41.3.1 or the Zynq manual
1170 * section 16.7.4 for details.
1171 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001172 if (status & MACB_BIT(RXUBR)) {
1173 ctrl = macb_readl(bp, NCR);
1174 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1175 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1176
1177 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001178 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001179 }
1180
Alexander Steinb19f7f72011-04-13 05:03:24 +00001181 if (status & MACB_BIT(ISR_ROVR)) {
1182 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001183 if (macb_is_gem(bp))
1184 bp->hw_stats.gem.rx_overruns++;
1185 else
1186 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001187
1188 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001189 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001190 }
1191
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001192 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001193 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001194 * netdev_err to a lower-priority context as well
1195 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001197 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001198
1199 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001200 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001201 }
1202
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001203 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001204 }
1205
1206 spin_unlock(&bp->lock);
1207
1208 return IRQ_HANDLED;
1209}
1210
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001211#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001212/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001213 * to allow network i/o with interrupts disabled.
1214 */
1215static void macb_poll_controller(struct net_device *dev)
1216{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001217 struct macb *bp = netdev_priv(dev);
1218 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001219 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001220 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001221
1222 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001223 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1224 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001225 local_irq_restore(flags);
1226}
1227#endif
1228
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001229static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001230 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001231 struct sk_buff *skb,
1232 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001233{
1234 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001235 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001236 struct macb_tx_skb *tx_skb = NULL;
1237 struct macb_dma_desc *desc;
1238 unsigned int offset, size, count = 0;
1239 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001240 unsigned int eof = 1, mss_mfs = 0;
1241 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1242
1243 /* LSO */
1244 if (skb_shinfo(skb)->gso_size != 0) {
1245 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1246 /* UDP - UFO */
1247 lso_ctrl = MACB_LSO_UFO_ENABLE;
1248 else
1249 /* TCP - TSO */
1250 lso_ctrl = MACB_LSO_TSO_ENABLE;
1251 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001252
1253 /* First, map non-paged data */
1254 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001255
1256 /* first buffer length */
1257 size = hdrlen;
1258
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001259 offset = 0;
1260 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001261 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001262 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001263
1264 mapping = dma_map_single(&bp->pdev->dev,
1265 skb->data + offset,
1266 size, DMA_TO_DEVICE);
1267 if (dma_mapping_error(&bp->pdev->dev, mapping))
1268 goto dma_error;
1269
1270 /* Save info to properly release resources */
1271 tx_skb->skb = NULL;
1272 tx_skb->mapping = mapping;
1273 tx_skb->size = size;
1274 tx_skb->mapped_as_page = false;
1275
1276 len -= size;
1277 offset += size;
1278 count++;
1279 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001280
1281 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001282 }
1283
1284 /* Then, map paged data from fragments */
1285 for (f = 0; f < nr_frags; f++) {
1286 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1287
1288 len = skb_frag_size(frag);
1289 offset = 0;
1290 while (len) {
1291 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001292 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001293 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001294
1295 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1296 offset, size, DMA_TO_DEVICE);
1297 if (dma_mapping_error(&bp->pdev->dev, mapping))
1298 goto dma_error;
1299
1300 /* Save info to properly release resources */
1301 tx_skb->skb = NULL;
1302 tx_skb->mapping = mapping;
1303 tx_skb->size = size;
1304 tx_skb->mapped_as_page = true;
1305
1306 len -= size;
1307 offset += size;
1308 count++;
1309 tx_head++;
1310 }
1311 }
1312
1313 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001314 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001315 netdev_err(bp->dev, "BUG! empty skb!\n");
1316 return 0;
1317 }
1318
1319 /* This is the last buffer of the frame: save socket buffer */
1320 tx_skb->skb = skb;
1321
1322 /* Update TX ring: update buffer descriptors in reverse order
1323 * to avoid race condition
1324 */
1325
1326 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1327 * to set the end of TX queue
1328 */
1329 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001330 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001331 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001332 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001333 desc->ctrl = ctrl;
1334
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001335 if (lso_ctrl) {
1336 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1337 /* include header and FCS in value given to h/w */
1338 mss_mfs = skb_shinfo(skb)->gso_size +
1339 skb_transport_offset(skb) +
1340 ETH_FCS_LEN;
1341 else /* TSO */ {
1342 mss_mfs = skb_shinfo(skb)->gso_size;
1343 /* TCP Sequence Number Source Select
1344 * can be set only for TSO
1345 */
1346 seq_ctrl = 0;
1347 }
1348 }
1349
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001350 do {
1351 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001352 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001353 tx_skb = &queue->tx_skb[entry];
1354 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001355
1356 ctrl = (u32)tx_skb->size;
1357 if (eof) {
1358 ctrl |= MACB_BIT(TX_LAST);
1359 eof = 0;
1360 }
Zach Brownb410d132016-10-19 09:56:57 -05001361 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001362 ctrl |= MACB_BIT(TX_WRAP);
1363
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001364 /* First descriptor is header descriptor */
1365 if (i == queue->tx_head) {
1366 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1367 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1368 } else
1369 /* Only set MSS/MFS on payload descriptors
1370 * (second or later descriptor)
1371 */
1372 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1373
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001374 /* Set TX buffer descriptor */
Harini Katakamfff80192016-08-09 13:15:53 +05301375 macb_set_addr(desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001376 /* desc->addr must be visible to hardware before clearing
1377 * 'TX_USED' bit in desc->ctrl.
1378 */
1379 wmb();
1380 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001381 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001382
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001383 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001384
1385 return count;
1386
1387dma_error:
1388 netdev_err(bp->dev, "TX DMA map failed\n");
1389
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001390 for (i = queue->tx_head; i != tx_head; i++) {
1391 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001392
1393 macb_tx_unmap(bp, tx_skb);
1394 }
1395
1396 return 0;
1397}
1398
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001399static netdev_features_t macb_features_check(struct sk_buff *skb,
1400 struct net_device *dev,
1401 netdev_features_t features)
1402{
1403 unsigned int nr_frags, f;
1404 unsigned int hdrlen;
1405
1406 /* Validate LSO compatibility */
1407
1408 /* there is only one buffer */
1409 if (!skb_is_nonlinear(skb))
1410 return features;
1411
1412 /* length of header */
1413 hdrlen = skb_transport_offset(skb);
1414 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1415 hdrlen += tcp_hdrlen(skb);
1416
1417 /* For LSO:
1418 * When software supplies two or more payload buffers all payload buffers
1419 * apart from the last must be a multiple of 8 bytes in size.
1420 */
1421 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1422 return features & ~MACB_NETIF_LSO;
1423
1424 nr_frags = skb_shinfo(skb)->nr_frags;
1425 /* No need to check last fragment */
1426 nr_frags--;
1427 for (f = 0; f < nr_frags; f++) {
1428 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1429
1430 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1431 return features & ~MACB_NETIF_LSO;
1432 }
1433 return features;
1434}
1435
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001436static inline int macb_clear_csum(struct sk_buff *skb)
1437{
1438 /* no change for packets without checksum offloading */
1439 if (skb->ip_summed != CHECKSUM_PARTIAL)
1440 return 0;
1441
1442 /* make sure we can modify the header */
1443 if (unlikely(skb_cow_head(skb, 0)))
1444 return -1;
1445
1446 /* initialize checksum field
1447 * This is required - at least for Zynq, which otherwise calculates
1448 * wrong UDP header checksums for UDP packets with UDP data len <=2
1449 */
1450 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1451 return 0;
1452}
1453
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001454static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1455{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001456 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001457 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001458 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001459 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001460 unsigned int desc_cnt, nr_frags, frag_size, f;
1461 unsigned int hdrlen;
1462 bool is_lso, is_udp = 0;
1463
1464 is_lso = (skb_shinfo(skb)->gso_size != 0);
1465
1466 if (is_lso) {
1467 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1468
1469 /* length of headers */
1470 if (is_udp)
1471 /* only queue eth + ip headers separately for UDP */
1472 hdrlen = skb_transport_offset(skb);
1473 else
1474 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1475 if (skb_headlen(skb) < hdrlen) {
1476 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1477 /* if this is required, would need to copy to single buffer */
1478 return NETDEV_TX_BUSY;
1479 }
1480 } else
1481 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001482
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001483#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1484 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001485 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1486 queue_index, skb->len, skb->head, skb->data,
1487 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001488 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1489 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001490#endif
1491
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001492 /* Count how many TX buffer descriptors are needed to send this
1493 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001494 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001495 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001496 if (is_lso && (skb_headlen(skb) > hdrlen))
1497 /* extra header descriptor if also payload in first buffer */
1498 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1499 else
1500 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001501 nr_frags = skb_shinfo(skb)->nr_frags;
1502 for (f = 0; f < nr_frags; f++) {
1503 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001504 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001505 }
1506
Dongdong Deng48719532009-08-23 19:49:07 -07001507 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001508
1509 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001510 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001511 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001512 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001513 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001514 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001515 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001516 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001517 }
1518
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001519 if (macb_clear_csum(skb)) {
1520 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001521 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001522 }
1523
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001524 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001525 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001526 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001527 goto unlock;
1528 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001529
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001530 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001531 wmb();
1532
Richard Cochrane0720922011-06-19 21:51:28 +00001533 skb_tx_timestamp(skb);
1534
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001535 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1536
Zach Brownb410d132016-10-19 09:56:57 -05001537 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001538 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001539
Soren Brinkmann92030902014-03-04 08:46:39 -08001540unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001541 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001542
Patrick McHardy6ed10652009-06-23 06:03:08 +00001543 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001544}
1545
Nicolas Ferre4df95132013-06-04 21:57:12 +00001546static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001547{
1548 if (!macb_is_gem(bp)) {
1549 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1550 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001551 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001552
Nicolas Ferre1b447912013-06-04 21:57:11 +00001553 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001554 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001555 "RX buffer must be multiple of %d bytes, expanding\n",
1556 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001557 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001558 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001559 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001560 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001561
1562 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1563 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001564}
1565
Nicolas Ferre4df95132013-06-04 21:57:12 +00001566static void gem_free_rx_buffers(struct macb *bp)
1567{
1568 struct sk_buff *skb;
1569 struct macb_dma_desc *desc;
1570 dma_addr_t addr;
1571 int i;
1572
1573 if (!bp->rx_skbuff)
1574 return;
1575
Zach Brownb410d132016-10-19 09:56:57 -05001576 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001577 skb = bp->rx_skbuff[i];
1578
Moritz Fischeraa50b552016-03-29 19:11:13 -07001579 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001580 continue;
1581
1582 desc = &bp->rx_ring[i];
1583 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Harini Katakamfff80192016-08-09 13:15:53 +05301584#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1585 addr |= ((u64)(desc->addrh) << 32);
1586#endif
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001587 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001588 DMA_FROM_DEVICE);
1589 dev_kfree_skb_any(skb);
1590 skb = NULL;
1591 }
1592
1593 kfree(bp->rx_skbuff);
1594 bp->rx_skbuff = NULL;
1595}
1596
1597static void macb_free_rx_buffers(struct macb *bp)
1598{
1599 if (bp->rx_buffers) {
1600 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001601 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001602 bp->rx_buffers, bp->rx_buffers_dma);
1603 bp->rx_buffers = NULL;
1604 }
1605}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001606
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001607static void macb_free_consistent(struct macb *bp)
1608{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001609 struct macb_queue *queue;
1610 unsigned int q;
1611
Nicolas Ferre4df95132013-06-04 21:57:12 +00001612 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001613 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001614 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001615 bp->rx_ring, bp->rx_ring_dma);
1616 bp->rx_ring = NULL;
1617 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001618
1619 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1620 kfree(queue->tx_skb);
1621 queue->tx_skb = NULL;
1622 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001623 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001624 queue->tx_ring, queue->tx_ring_dma);
1625 queue->tx_ring = NULL;
1626 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001627 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001628}
1629
1630static int gem_alloc_rx_buffers(struct macb *bp)
1631{
1632 int size;
1633
Zach Brownb410d132016-10-19 09:56:57 -05001634 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001635 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1636 if (!bp->rx_skbuff)
1637 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001638 else
1639 netdev_dbg(bp->dev,
1640 "Allocated %d RX struct sk_buff entries at %p\n",
1641 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001642 return 0;
1643}
1644
1645static int macb_alloc_rx_buffers(struct macb *bp)
1646{
1647 int size;
1648
Zach Brownb410d132016-10-19 09:56:57 -05001649 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001650 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1651 &bp->rx_buffers_dma, GFP_KERNEL);
1652 if (!bp->rx_buffers)
1653 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001654
1655 netdev_dbg(bp->dev,
1656 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1657 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001658 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001659}
1660
1661static int macb_alloc_consistent(struct macb *bp)
1662{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001663 struct macb_queue *queue;
1664 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001665 int size;
1666
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001667 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001668 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001669 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1670 &queue->tx_ring_dma,
1671 GFP_KERNEL);
1672 if (!queue->tx_ring)
1673 goto out_err;
1674 netdev_dbg(bp->dev,
1675 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1676 q, size, (unsigned long)queue->tx_ring_dma,
1677 queue->tx_ring);
1678
Zach Brownb410d132016-10-19 09:56:57 -05001679 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001680 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1681 if (!queue->tx_skb)
1682 goto out_err;
1683 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001684
Zach Brownb410d132016-10-19 09:56:57 -05001685 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001686 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1687 &bp->rx_ring_dma, GFP_KERNEL);
1688 if (!bp->rx_ring)
1689 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001690 netdev_dbg(bp->dev,
1691 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1692 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001693
Nicolas Ferre4df95132013-06-04 21:57:12 +00001694 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001695 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001696
1697 return 0;
1698
1699out_err:
1700 macb_free_consistent(bp);
1701 return -ENOMEM;
1702}
1703
Nicolas Ferre4df95132013-06-04 21:57:12 +00001704static void gem_init_rings(struct macb *bp)
1705{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001706 struct macb_queue *queue;
1707 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001708 int i;
1709
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001710 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001711 for (i = 0; i < bp->tx_ring_size; i++) {
1712 queue->tx_ring[i].addr = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001713 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1714 }
Zach Brownb410d132016-10-19 09:56:57 -05001715 queue->tx_ring[bp->tx_ring_size - 1].ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001716 queue->tx_head = 0;
1717 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001718 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001719
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001720 bp->rx_tail = 0;
1721 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001722
1723 gem_rx_refill(bp);
1724}
1725
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001726static void macb_init_rings(struct macb *bp)
1727{
1728 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001729
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001730 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001731
Zach Brownb410d132016-10-19 09:56:57 -05001732 for (i = 0; i < bp->tx_ring_size; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001733 bp->queues[0].tx_ring[i].addr = 0;
1734 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001735 }
Ben Shelton21d35152015-04-22 17:28:54 -05001736 bp->queues[0].tx_head = 0;
1737 bp->queues[0].tx_tail = 0;
Zach Brownb410d132016-10-19 09:56:57 -05001738 bp->queues[0].tx_ring[bp->tx_ring_size - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001739
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001740 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001741}
1742
1743static void macb_reset_hw(struct macb *bp)
1744{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001745 struct macb_queue *queue;
1746 unsigned int q;
1747
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001748 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001749 * more gracefully?)
1750 */
1751 macb_writel(bp, NCR, 0);
1752
1753 /* Clear the stats registers (XXX: Update stats first?) */
1754 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1755
1756 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001757 macb_writel(bp, TSR, -1);
1758 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001759
1760 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001761 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1762 queue_writel(queue, IDR, -1);
1763 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001764 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1765 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001766 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001767}
1768
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001769static u32 gem_mdc_clk_div(struct macb *bp)
1770{
1771 u32 config;
1772 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1773
1774 if (pclk_hz <= 20000000)
1775 config = GEM_BF(CLK, GEM_CLK_DIV8);
1776 else if (pclk_hz <= 40000000)
1777 config = GEM_BF(CLK, GEM_CLK_DIV16);
1778 else if (pclk_hz <= 80000000)
1779 config = GEM_BF(CLK, GEM_CLK_DIV32);
1780 else if (pclk_hz <= 120000000)
1781 config = GEM_BF(CLK, GEM_CLK_DIV48);
1782 else if (pclk_hz <= 160000000)
1783 config = GEM_BF(CLK, GEM_CLK_DIV64);
1784 else
1785 config = GEM_BF(CLK, GEM_CLK_DIV96);
1786
1787 return config;
1788}
1789
1790static u32 macb_mdc_clk_div(struct macb *bp)
1791{
1792 u32 config;
1793 unsigned long pclk_hz;
1794
1795 if (macb_is_gem(bp))
1796 return gem_mdc_clk_div(bp);
1797
1798 pclk_hz = clk_get_rate(bp->pclk);
1799 if (pclk_hz <= 20000000)
1800 config = MACB_BF(CLK, MACB_CLK_DIV8);
1801 else if (pclk_hz <= 40000000)
1802 config = MACB_BF(CLK, MACB_CLK_DIV16);
1803 else if (pclk_hz <= 80000000)
1804 config = MACB_BF(CLK, MACB_CLK_DIV32);
1805 else
1806 config = MACB_BF(CLK, MACB_CLK_DIV64);
1807
1808 return config;
1809}
1810
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001811/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001812 * should program. We find the width from decoding the design configuration
1813 * register to find the maximum supported data bus width.
1814 */
1815static u32 macb_dbw(struct macb *bp)
1816{
1817 if (!macb_is_gem(bp))
1818 return 0;
1819
1820 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1821 case 4:
1822 return GEM_BF(DBW, GEM_DBW128);
1823 case 2:
1824 return GEM_BF(DBW, GEM_DBW64);
1825 case 1:
1826 default:
1827 return GEM_BF(DBW, GEM_DBW32);
1828 }
1829}
1830
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001831/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001832 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001833 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001834 * (if not supported by FIFO, it will fallback to default)
1835 * - set both rx/tx packet buffers to full memory size
1836 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001837 */
1838static void macb_configure_dma(struct macb *bp)
1839{
1840 u32 dmacfg;
1841
1842 if (macb_is_gem(bp)) {
1843 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001844 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001845 if (bp->dma_burst_length)
1846 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001847 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301848 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301849
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001850 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301851 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1852 else
1853 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1854
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001855 if (bp->dev->features & NETIF_F_HW_CSUM)
1856 dmacfg |= GEM_BIT(TXCOEN);
1857 else
1858 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301859
1860#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1861 dmacfg |= GEM_BIT(ADDR64);
1862#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001863 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1864 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001865 gem_writel(bp, DMACFG, dmacfg);
1866 }
1867}
1868
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001869static void macb_init_hw(struct macb *bp)
1870{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001871 struct macb_queue *queue;
1872 unsigned int q;
1873
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001874 u32 config;
1875
1876 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001877 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001878
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001879 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301880 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1881 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001882 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001883 config |= MACB_BIT(PAE); /* PAuse Enable */
1884 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001885 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301886 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1887 else
1888 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001889 if (bp->dev->flags & IFF_PROMISC)
1890 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001891 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1892 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001893 if (!(bp->dev->flags & IFF_BROADCAST))
1894 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001895 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001896 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001897 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301898 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001899 bp->speed = SPEED_10;
1900 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301901 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001902 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301903 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001904
Jamie Iles0116da42011-03-14 17:38:30 +00001905 macb_configure_dma(bp);
1906
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001907 /* Initialize TX and RX buffers */
Harini Katakamfff80192016-08-09 13:15:53 +05301908 macb_writel(bp, RBQP, (u32)(bp->rx_ring_dma));
1909#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1910 macb_writel(bp, RBQPH, (u32)(bp->rx_ring_dma >> 32));
1911#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001912 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakamfff80192016-08-09 13:15:53 +05301913 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
1914#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1915 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
1916#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001917
1918 /* Enable interrupts */
1919 queue_writel(queue, IER,
1920 MACB_RX_INT_FLAGS |
1921 MACB_TX_INT_FLAGS |
1922 MACB_BIT(HRESP));
1923 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001924
1925 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001926 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001927}
1928
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001929/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001930 * locations in the memory map. The least significant bits are stored
1931 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1932 *
1933 * The unicast hash enable and the multicast hash enable bits in the
1934 * network configuration register enable the reception of hash matched
1935 * frames. The destination address is reduced to a 6 bit index into
1936 * the 64 bit hash register using the following hash function. The
1937 * hash function is an exclusive or of every sixth bit of the
1938 * destination address.
1939 *
1940 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1941 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1942 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1943 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1944 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1945 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1946 *
1947 * da[0] represents the least significant bit of the first byte
1948 * received, that is, the multicast/unicast indicator, and da[47]
1949 * represents the most significant bit of the last byte received. If
1950 * the hash index, hi[n], points to a bit that is set in the hash
1951 * register then the frame will be matched according to whether the
1952 * frame is multicast or unicast. A multicast match will be signalled
1953 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1954 * index points to a bit set in the hash register. A unicast match
1955 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1956 * and the hash index points to a bit set in the hash register. To
1957 * receive all multicast frames, the hash register should be set with
1958 * all ones and the multicast hash enable bit should be set in the
1959 * network configuration register.
1960 */
1961
1962static inline int hash_bit_value(int bitnr, __u8 *addr)
1963{
1964 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1965 return 1;
1966 return 0;
1967}
1968
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001969/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001970static int hash_get_index(__u8 *addr)
1971{
1972 int i, j, bitval;
1973 int hash_index = 0;
1974
1975 for (j = 0; j < 6; j++) {
1976 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001977 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001978
1979 hash_index |= (bitval << j);
1980 }
1981
1982 return hash_index;
1983}
1984
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001985/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001986static void macb_sethashtable(struct net_device *dev)
1987{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001988 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001989 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001990 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001991 struct macb *bp = netdev_priv(dev);
1992
Moritz Fischeraa50b552016-03-29 19:11:13 -07001993 mc_filter[0] = 0;
1994 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001995
Jiri Pirko22bedad32010-04-01 21:22:57 +00001996 netdev_for_each_mc_addr(ha, dev) {
1997 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001998 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1999 }
2000
Jamie Ilesf75ba502011-11-08 10:12:32 +00002001 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2002 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002003}
2004
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002005/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002006static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002007{
2008 unsigned long cfg;
2009 struct macb *bp = netdev_priv(dev);
2010
2011 cfg = macb_readl(bp, NCFGR);
2012
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002013 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002014 /* Enable promiscuous mode */
2015 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002016
2017 /* Disable RX checksum offload */
2018 if (macb_is_gem(bp))
2019 cfg &= ~GEM_BIT(RXCOEN);
2020 } else {
2021 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002022 cfg &= ~MACB_BIT(CAF);
2023
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002024 /* Enable RX checksum offload only if requested */
2025 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2026 cfg |= GEM_BIT(RXCOEN);
2027 }
2028
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002029 if (dev->flags & IFF_ALLMULTI) {
2030 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002031 macb_or_gem_writel(bp, HRB, -1);
2032 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002033 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002034 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002035 /* Enable specific multicasts */
2036 macb_sethashtable(dev);
2037 cfg |= MACB_BIT(NCFGR_MTI);
2038 } else if (dev->flags & (~IFF_ALLMULTI)) {
2039 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002040 macb_or_gem_writel(bp, HRB, 0);
2041 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002042 cfg &= ~MACB_BIT(NCFGR_MTI);
2043 }
2044
2045 macb_writel(bp, NCFGR, cfg);
2046}
2047
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002048static int macb_open(struct net_device *dev)
2049{
2050 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002051 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002052 int err;
2053
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002054 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002055
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002056 /* carrier starts down */
2057 netif_carrier_off(dev);
2058
frederic RODO6c36a702007-07-12 19:07:24 +02002059 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002060 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002061 return -EAGAIN;
2062
Nicolas Ferre1b447912013-06-04 21:57:11 +00002063 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002064 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002065
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002066 err = macb_alloc_consistent(bp);
2067 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002068 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2069 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002070 return err;
2071 }
2072
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002073 napi_enable(&bp->napi);
2074
Nicolas Ferre4df95132013-06-04 21:57:12 +00002075 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002076 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002077
frederic RODO6c36a702007-07-12 19:07:24 +02002078 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002079 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002080
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002081 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002082
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002083 return 0;
2084}
2085
2086static int macb_close(struct net_device *dev)
2087{
2088 struct macb *bp = netdev_priv(dev);
2089 unsigned long flags;
2090
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002091 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002092 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002093
Philippe Reynes0a912812016-06-22 00:32:35 +02002094 if (dev->phydev)
2095 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002096
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002097 spin_lock_irqsave(&bp->lock, flags);
2098 macb_reset_hw(bp);
2099 netif_carrier_off(dev);
2100 spin_unlock_irqrestore(&bp->lock, flags);
2101
2102 macb_free_consistent(bp);
2103
2104 return 0;
2105}
2106
Harini Katakama5898ea2015-05-06 22:27:18 +05302107static int macb_change_mtu(struct net_device *dev, int new_mtu)
2108{
Harini Katakama5898ea2015-05-06 22:27:18 +05302109 if (netif_running(dev))
2110 return -EBUSY;
2111
Harini Katakama5898ea2015-05-06 22:27:18 +05302112 dev->mtu = new_mtu;
2113
2114 return 0;
2115}
2116
Jamie Ilesa494ed82011-03-09 16:26:35 +00002117static void gem_update_stats(struct macb *bp)
2118{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002119 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002120 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002121
Xander Huff3ff13f12015-01-13 16:15:51 -06002122 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2123 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002124 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002125
2126 bp->ethtool_stats[i] += val;
2127 *p += val;
2128
2129 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2130 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002131 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002132 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002133 *(++p) += val;
2134 }
2135 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002136}
2137
2138static struct net_device_stats *gem_get_stats(struct macb *bp)
2139{
2140 struct gem_stats *hwstat = &bp->hw_stats.gem;
2141 struct net_device_stats *nstat = &bp->stats;
2142
2143 gem_update_stats(bp);
2144
2145 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2146 hwstat->rx_alignment_errors +
2147 hwstat->rx_resource_errors +
2148 hwstat->rx_overruns +
2149 hwstat->rx_oversize_frames +
2150 hwstat->rx_jabbers +
2151 hwstat->rx_undersized_frames +
2152 hwstat->rx_length_field_frame_errors);
2153 nstat->tx_errors = (hwstat->tx_late_collisions +
2154 hwstat->tx_excessive_collisions +
2155 hwstat->tx_underrun +
2156 hwstat->tx_carrier_sense_errors);
2157 nstat->multicast = hwstat->rx_multicast_frames;
2158 nstat->collisions = (hwstat->tx_single_collision_frames +
2159 hwstat->tx_multiple_collision_frames +
2160 hwstat->tx_excessive_collisions);
2161 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2162 hwstat->rx_jabbers +
2163 hwstat->rx_undersized_frames +
2164 hwstat->rx_length_field_frame_errors);
2165 nstat->rx_over_errors = hwstat->rx_resource_errors;
2166 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2167 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2168 nstat->rx_fifo_errors = hwstat->rx_overruns;
2169 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2170 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2171 nstat->tx_fifo_errors = hwstat->tx_underrun;
2172
2173 return nstat;
2174}
2175
Xander Huff3ff13f12015-01-13 16:15:51 -06002176static void gem_get_ethtool_stats(struct net_device *dev,
2177 struct ethtool_stats *stats, u64 *data)
2178{
2179 struct macb *bp;
2180
2181 bp = netdev_priv(dev);
2182 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002183 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002184}
2185
2186static int gem_get_sset_count(struct net_device *dev, int sset)
2187{
2188 switch (sset) {
2189 case ETH_SS_STATS:
2190 return GEM_STATS_LEN;
2191 default:
2192 return -EOPNOTSUPP;
2193 }
2194}
2195
2196static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2197{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002198 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002199
2200 switch (sset) {
2201 case ETH_SS_STATS:
2202 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2203 memcpy(p, gem_statistics[i].stat_string,
2204 ETH_GSTRING_LEN);
2205 break;
2206 }
2207}
2208
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002209static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002210{
2211 struct macb *bp = netdev_priv(dev);
2212 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002213 struct macb_stats *hwstat = &bp->hw_stats.macb;
2214
2215 if (macb_is_gem(bp))
2216 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002217
frederic RODO6c36a702007-07-12 19:07:24 +02002218 /* read stats from hardware */
2219 macb_update_stats(bp);
2220
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002221 /* Convert HW stats into netdevice stats */
2222 nstat->rx_errors = (hwstat->rx_fcs_errors +
2223 hwstat->rx_align_errors +
2224 hwstat->rx_resource_errors +
2225 hwstat->rx_overruns +
2226 hwstat->rx_oversize_pkts +
2227 hwstat->rx_jabbers +
2228 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002229 hwstat->rx_length_mismatch);
2230 nstat->tx_errors = (hwstat->tx_late_cols +
2231 hwstat->tx_excessive_cols +
2232 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002233 hwstat->tx_carrier_errors +
2234 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002235 nstat->collisions = (hwstat->tx_single_cols +
2236 hwstat->tx_multiple_cols +
2237 hwstat->tx_excessive_cols);
2238 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2239 hwstat->rx_jabbers +
2240 hwstat->rx_undersize_pkts +
2241 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002242 nstat->rx_over_errors = hwstat->rx_resource_errors +
2243 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002244 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2245 nstat->rx_frame_errors = hwstat->rx_align_errors;
2246 nstat->rx_fifo_errors = hwstat->rx_overruns;
2247 /* XXX: What does "missed" mean? */
2248 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2249 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2250 nstat->tx_fifo_errors = hwstat->tx_underruns;
2251 /* Don't know about heartbeat or window errors... */
2252
2253 return nstat;
2254}
2255
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002256static int macb_get_regs_len(struct net_device *netdev)
2257{
2258 return MACB_GREGS_NBR * sizeof(u32);
2259}
2260
2261static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2262 void *p)
2263{
2264 struct macb *bp = netdev_priv(dev);
2265 unsigned int tail, head;
2266 u32 *regs_buff = p;
2267
2268 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2269 | MACB_GREGS_VERSION;
2270
Zach Brownb410d132016-10-19 09:56:57 -05002271 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2272 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002273
2274 regs_buff[0] = macb_readl(bp, NCR);
2275 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2276 regs_buff[2] = macb_readl(bp, NSR);
2277 regs_buff[3] = macb_readl(bp, TSR);
2278 regs_buff[4] = macb_readl(bp, RBQP);
2279 regs_buff[5] = macb_readl(bp, TBQP);
2280 regs_buff[6] = macb_readl(bp, RSR);
2281 regs_buff[7] = macb_readl(bp, IMR);
2282
2283 regs_buff[8] = tail;
2284 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002285 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2286 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002287
Neil Armstrongce721a72016-01-05 14:39:16 +01002288 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2289 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002290 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002291 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002292}
2293
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002294static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2295{
2296 struct macb *bp = netdev_priv(netdev);
2297
2298 wol->supported = 0;
2299 wol->wolopts = 0;
2300
2301 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2302 wol->supported = WAKE_MAGIC;
2303
2304 if (bp->wol & MACB_WOL_ENABLED)
2305 wol->wolopts |= WAKE_MAGIC;
2306 }
2307}
2308
2309static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2310{
2311 struct macb *bp = netdev_priv(netdev);
2312
2313 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2314 (wol->wolopts & ~WAKE_MAGIC))
2315 return -EOPNOTSUPP;
2316
2317 if (wol->wolopts & WAKE_MAGIC)
2318 bp->wol |= MACB_WOL_ENABLED;
2319 else
2320 bp->wol &= ~MACB_WOL_ENABLED;
2321
2322 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2323
2324 return 0;
2325}
2326
Zach Brown8441bb32016-10-19 09:56:58 -05002327static void macb_get_ringparam(struct net_device *netdev,
2328 struct ethtool_ringparam *ring)
2329{
2330 struct macb *bp = netdev_priv(netdev);
2331
2332 ring->rx_max_pending = MAX_RX_RING_SIZE;
2333 ring->tx_max_pending = MAX_TX_RING_SIZE;
2334
2335 ring->rx_pending = bp->rx_ring_size;
2336 ring->tx_pending = bp->tx_ring_size;
2337}
2338
2339static int macb_set_ringparam(struct net_device *netdev,
2340 struct ethtool_ringparam *ring)
2341{
2342 struct macb *bp = netdev_priv(netdev);
2343 u32 new_rx_size, new_tx_size;
2344 unsigned int reset = 0;
2345
2346 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2347 return -EINVAL;
2348
2349 new_rx_size = clamp_t(u32, ring->rx_pending,
2350 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2351 new_rx_size = roundup_pow_of_two(new_rx_size);
2352
2353 new_tx_size = clamp_t(u32, ring->tx_pending,
2354 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2355 new_tx_size = roundup_pow_of_two(new_tx_size);
2356
2357 if ((new_tx_size == bp->tx_ring_size) &&
2358 (new_rx_size == bp->rx_ring_size)) {
2359 /* nothing to do */
2360 return 0;
2361 }
2362
2363 if (netif_running(bp->dev)) {
2364 reset = 1;
2365 macb_close(bp->dev);
2366 }
2367
2368 bp->rx_ring_size = new_rx_size;
2369 bp->tx_ring_size = new_tx_size;
2370
2371 if (reset)
2372 macb_open(bp->dev);
2373
2374 return 0;
2375}
2376
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002377static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002378 .get_regs_len = macb_get_regs_len,
2379 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002380 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002381 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002382 .get_wol = macb_get_wol,
2383 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002384 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2385 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002386 .get_ringparam = macb_get_ringparam,
2387 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002388};
Xander Huff8cd5a562015-01-15 15:55:20 -06002389
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002390static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002391 .get_regs_len = macb_get_regs_len,
2392 .get_regs = macb_get_regs,
2393 .get_link = ethtool_op_get_link,
2394 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002395 .get_ethtool_stats = gem_get_ethtool_stats,
2396 .get_strings = gem_get_ethtool_strings,
2397 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002398 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2399 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002400 .get_ringparam = macb_get_ringparam,
2401 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002402};
2403
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002404static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002405{
Philippe Reynes0a912812016-06-22 00:32:35 +02002406 struct phy_device *phydev = dev->phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002407
2408 if (!netif_running(dev))
2409 return -EINVAL;
2410
frederic RODO6c36a702007-07-12 19:07:24 +02002411 if (!phydev)
2412 return -ENODEV;
2413
Richard Cochran28b04112010-07-17 08:48:55 +00002414 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002415}
2416
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002417static int macb_set_features(struct net_device *netdev,
2418 netdev_features_t features)
2419{
2420 struct macb *bp = netdev_priv(netdev);
2421 netdev_features_t changed = features ^ netdev->features;
2422
2423 /* TX checksum offload */
2424 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2425 u32 dmacfg;
2426
2427 dmacfg = gem_readl(bp, DMACFG);
2428 if (features & NETIF_F_HW_CSUM)
2429 dmacfg |= GEM_BIT(TXCOEN);
2430 else
2431 dmacfg &= ~GEM_BIT(TXCOEN);
2432 gem_writel(bp, DMACFG, dmacfg);
2433 }
2434
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002435 /* RX checksum offload */
2436 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2437 u32 netcfg;
2438
2439 netcfg = gem_readl(bp, NCFGR);
2440 if (features & NETIF_F_RXCSUM &&
2441 !(netdev->flags & IFF_PROMISC))
2442 netcfg |= GEM_BIT(RXCOEN);
2443 else
2444 netcfg &= ~GEM_BIT(RXCOEN);
2445 gem_writel(bp, NCFGR, netcfg);
2446 }
2447
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002448 return 0;
2449}
2450
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002451static const struct net_device_ops macb_netdev_ops = {
2452 .ndo_open = macb_open,
2453 .ndo_stop = macb_close,
2454 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002455 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002456 .ndo_get_stats = macb_get_stats,
2457 .ndo_do_ioctl = macb_ioctl,
2458 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302459 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002460 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002461#ifdef CONFIG_NET_POLL_CONTROLLER
2462 .ndo_poll_controller = macb_poll_controller,
2463#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002464 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002465 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002466};
2467
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002468/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002469 * and integration options used
2470 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002471static void macb_configure_caps(struct macb *bp,
2472 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002473{
2474 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002475
Nicolas Ferref6970502015-03-31 15:02:01 +02002476 if (dt_conf)
2477 bp->caps = dt_conf->caps;
2478
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002479 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002480 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2481
Nicolas Ferree1755872014-07-24 13:50:58 +02002482 dcfg = gem_readl(bp, DCFG1);
2483 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2484 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2485 dcfg = gem_readl(bp, DCFG2);
2486 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2487 bp->caps |= MACB_CAPS_FIFO_MODE;
2488 }
2489
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002490 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002491}
2492
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002493static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002494 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002495 unsigned int *queue_mask,
2496 unsigned int *num_queues)
2497{
2498 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002499
2500 *queue_mask = 0x1;
2501 *num_queues = 1;
2502
Nicolas Ferreda120112015-03-31 15:02:00 +02002503 /* is it macb or gem ?
2504 *
2505 * We need to read directly from the hardware here because
2506 * we are early in the probe process and don't have the
2507 * MACB_CAPS_MACB_IS_GEM flag positioned
2508 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002509 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002510 return;
2511
2512 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302513 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2514
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002515 *queue_mask |= 0x1;
2516
2517 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2518 if (*queue_mask & (1 << hw_q))
2519 (*num_queues)++;
2520}
2521
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002522static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302523 struct clk **hclk, struct clk **tx_clk,
2524 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002525{
2526 int err;
2527
2528 *pclk = devm_clk_get(&pdev->dev, "pclk");
2529 if (IS_ERR(*pclk)) {
2530 err = PTR_ERR(*pclk);
2531 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2532 return err;
2533 }
2534
2535 *hclk = devm_clk_get(&pdev->dev, "hclk");
2536 if (IS_ERR(*hclk)) {
2537 err = PTR_ERR(*hclk);
2538 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2539 return err;
2540 }
2541
2542 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2543 if (IS_ERR(*tx_clk))
2544 *tx_clk = NULL;
2545
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302546 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2547 if (IS_ERR(*rx_clk))
2548 *rx_clk = NULL;
2549
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002550 err = clk_prepare_enable(*pclk);
2551 if (err) {
2552 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2553 return err;
2554 }
2555
2556 err = clk_prepare_enable(*hclk);
2557 if (err) {
2558 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2559 goto err_disable_pclk;
2560 }
2561
2562 err = clk_prepare_enable(*tx_clk);
2563 if (err) {
2564 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2565 goto err_disable_hclk;
2566 }
2567
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302568 err = clk_prepare_enable(*rx_clk);
2569 if (err) {
2570 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2571 goto err_disable_txclk;
2572 }
2573
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002574 return 0;
2575
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302576err_disable_txclk:
2577 clk_disable_unprepare(*tx_clk);
2578
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002579err_disable_hclk:
2580 clk_disable_unprepare(*hclk);
2581
2582err_disable_pclk:
2583 clk_disable_unprepare(*pclk);
2584
2585 return err;
2586}
2587
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002588static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002589{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002590 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002591 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002592 struct macb *bp = netdev_priv(dev);
2593 struct macb_queue *queue;
2594 int err;
2595 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002596
Zach Brownb410d132016-10-19 09:56:57 -05002597 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2598 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2599
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002600 /* set the queue register mapping once for all: queue0 has a special
2601 * register mapping but we don't want to test the queue index then
2602 * compute the corresponding register offset at run time.
2603 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002604 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002605 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002606 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002607
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002608 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002609 queue->bp = bp;
2610 if (hw_q) {
2611 queue->ISR = GEM_ISR(hw_q - 1);
2612 queue->IER = GEM_IER(hw_q - 1);
2613 queue->IDR = GEM_IDR(hw_q - 1);
2614 queue->IMR = GEM_IMR(hw_q - 1);
2615 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302616#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2617 queue->TBQPH = GEM_TBQPH(hw_q -1);
2618#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002619 } else {
2620 /* queue0 uses legacy registers */
2621 queue->ISR = MACB_ISR;
2622 queue->IER = MACB_IER;
2623 queue->IDR = MACB_IDR;
2624 queue->IMR = MACB_IMR;
2625 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302626#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2627 queue->TBQPH = MACB_TBQPH;
2628#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002629 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002630
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002631 /* get irq: here we use the linux queue index, not the hardware
2632 * queue index. the queue irq definitions in the device tree
2633 * must remove the optional gaps that could exist in the
2634 * hardware queue mask.
2635 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002636 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002637 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002638 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002639 if (err) {
2640 dev_err(&pdev->dev,
2641 "Unable to request IRQ %d (error %d)\n",
2642 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002643 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002644 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002645
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002646 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002647 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002648 }
2649
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002650 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002651 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002652
Nicolas Ferre4df95132013-06-04 21:57:12 +00002653 /* setup appropriated routines according to adapter type */
2654 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002655 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002656 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2657 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2658 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2659 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002660 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002661 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002662 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002663 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2664 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2665 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2666 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002667 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002668 }
2669
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002670 /* Set features */
2671 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002672
2673 /* Check LSO capability */
2674 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
2675 dev->hw_features |= MACB_NETIF_LSO;
2676
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002677 /* Checksum offload is only available on gem with packet buffer */
2678 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002679 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002680 if (bp->caps & MACB_CAPS_SG_DISABLED)
2681 dev->hw_features &= ~NETIF_F_SG;
2682 dev->features = dev->hw_features;
2683
Neil Armstrongce721a72016-01-05 14:39:16 +01002684 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2685 val = 0;
2686 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2687 val = GEM_BIT(RGMII);
2688 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002689 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002690 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002691 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002692 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002693
Neil Armstrongce721a72016-01-05 14:39:16 +01002694 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2695 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002696
Neil Armstrongce721a72016-01-05 14:39:16 +01002697 macb_or_gem_writel(bp, USRIO, val);
2698 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002699
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002700 /* Set MII management clock divider */
2701 val = macb_mdc_clk_div(bp);
2702 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302703 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2704 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002705 macb_writel(bp, NCFGR, val);
2706
2707 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002708}
2709
2710#if defined(CONFIG_OF)
2711/* 1518 rounded up */
2712#define AT91ETHER_MAX_RBUFF_SZ 0x600
2713/* max number of receive buffers */
2714#define AT91ETHER_MAX_RX_DESCR 9
2715
2716/* Initialize and start the Receiver and Transmit subsystems */
2717static int at91ether_start(struct net_device *dev)
2718{
2719 struct macb *lp = netdev_priv(dev);
2720 dma_addr_t addr;
2721 u32 ctl;
2722 int i;
2723
2724 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2725 (AT91ETHER_MAX_RX_DESCR *
2726 sizeof(struct macb_dma_desc)),
2727 &lp->rx_ring_dma, GFP_KERNEL);
2728 if (!lp->rx_ring)
2729 return -ENOMEM;
2730
2731 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2732 AT91ETHER_MAX_RX_DESCR *
2733 AT91ETHER_MAX_RBUFF_SZ,
2734 &lp->rx_buffers_dma, GFP_KERNEL);
2735 if (!lp->rx_buffers) {
2736 dma_free_coherent(&lp->pdev->dev,
2737 AT91ETHER_MAX_RX_DESCR *
2738 sizeof(struct macb_dma_desc),
2739 lp->rx_ring, lp->rx_ring_dma);
2740 lp->rx_ring = NULL;
2741 return -ENOMEM;
2742 }
2743
2744 addr = lp->rx_buffers_dma;
2745 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2746 lp->rx_ring[i].addr = addr;
2747 lp->rx_ring[i].ctrl = 0;
2748 addr += AT91ETHER_MAX_RBUFF_SZ;
2749 }
2750
2751 /* Set the Wrap bit on the last descriptor */
2752 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2753
2754 /* Reset buffer index */
2755 lp->rx_tail = 0;
2756
2757 /* Program address of descriptor list in Rx Buffer Queue register */
2758 macb_writel(lp, RBQP, lp->rx_ring_dma);
2759
2760 /* Enable Receive and Transmit */
2761 ctl = macb_readl(lp, NCR);
2762 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2763
2764 return 0;
2765}
2766
2767/* Open the ethernet interface */
2768static int at91ether_open(struct net_device *dev)
2769{
2770 struct macb *lp = netdev_priv(dev);
2771 u32 ctl;
2772 int ret;
2773
2774 /* Clear internal statistics */
2775 ctl = macb_readl(lp, NCR);
2776 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2777
2778 macb_set_hwaddr(lp);
2779
2780 ret = at91ether_start(dev);
2781 if (ret)
2782 return ret;
2783
2784 /* Enable MAC interrupts */
2785 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2786 MACB_BIT(RXUBR) |
2787 MACB_BIT(ISR_TUND) |
2788 MACB_BIT(ISR_RLE) |
2789 MACB_BIT(TCOMP) |
2790 MACB_BIT(ISR_ROVR) |
2791 MACB_BIT(HRESP));
2792
2793 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002794 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002795
2796 netif_start_queue(dev);
2797
2798 return 0;
2799}
2800
2801/* Close the interface */
2802static int at91ether_close(struct net_device *dev)
2803{
2804 struct macb *lp = netdev_priv(dev);
2805 u32 ctl;
2806
2807 /* Disable Receiver and Transmitter */
2808 ctl = macb_readl(lp, NCR);
2809 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2810
2811 /* Disable MAC interrupts */
2812 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2813 MACB_BIT(RXUBR) |
2814 MACB_BIT(ISR_TUND) |
2815 MACB_BIT(ISR_RLE) |
2816 MACB_BIT(TCOMP) |
2817 MACB_BIT(ISR_ROVR) |
2818 MACB_BIT(HRESP));
2819
2820 netif_stop_queue(dev);
2821
2822 dma_free_coherent(&lp->pdev->dev,
2823 AT91ETHER_MAX_RX_DESCR *
2824 sizeof(struct macb_dma_desc),
2825 lp->rx_ring, lp->rx_ring_dma);
2826 lp->rx_ring = NULL;
2827
2828 dma_free_coherent(&lp->pdev->dev,
2829 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2830 lp->rx_buffers, lp->rx_buffers_dma);
2831 lp->rx_buffers = NULL;
2832
2833 return 0;
2834}
2835
2836/* Transmit packet */
2837static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2838{
2839 struct macb *lp = netdev_priv(dev);
2840
2841 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2842 netif_stop_queue(dev);
2843
2844 /* Store packet information (to free when Tx completed) */
2845 lp->skb = skb;
2846 lp->skb_length = skb->len;
2847 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2848 DMA_TO_DEVICE);
2849
2850 /* Set address of the data in the Transmit Address register */
2851 macb_writel(lp, TAR, lp->skb_physaddr);
2852 /* Set length of the packet in the Transmit Control register */
2853 macb_writel(lp, TCR, skb->len);
2854
2855 } else {
2856 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2857 return NETDEV_TX_BUSY;
2858 }
2859
2860 return NETDEV_TX_OK;
2861}
2862
2863/* Extract received frame from buffer descriptors and sent to upper layers.
2864 * (Called from interrupt context)
2865 */
2866static void at91ether_rx(struct net_device *dev)
2867{
2868 struct macb *lp = netdev_priv(dev);
2869 unsigned char *p_recv;
2870 struct sk_buff *skb;
2871 unsigned int pktlen;
2872
2873 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2874 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2875 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2876 skb = netdev_alloc_skb(dev, pktlen + 2);
2877 if (skb) {
2878 skb_reserve(skb, 2);
2879 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2880
2881 skb->protocol = eth_type_trans(skb, dev);
2882 lp->stats.rx_packets++;
2883 lp->stats.rx_bytes += pktlen;
2884 netif_rx(skb);
2885 } else {
2886 lp->stats.rx_dropped++;
2887 }
2888
2889 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2890 lp->stats.multicast++;
2891
2892 /* reset ownership bit */
2893 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2894
2895 /* wrap after last buffer */
2896 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2897 lp->rx_tail = 0;
2898 else
2899 lp->rx_tail++;
2900 }
2901}
2902
2903/* MAC interrupt handler */
2904static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2905{
2906 struct net_device *dev = dev_id;
2907 struct macb *lp = netdev_priv(dev);
2908 u32 intstatus, ctl;
2909
2910 /* MAC Interrupt Status register indicates what interrupts are pending.
2911 * It is automatically cleared once read.
2912 */
2913 intstatus = macb_readl(lp, ISR);
2914
2915 /* Receive complete */
2916 if (intstatus & MACB_BIT(RCOMP))
2917 at91ether_rx(dev);
2918
2919 /* Transmit complete */
2920 if (intstatus & MACB_BIT(TCOMP)) {
2921 /* The TCOM bit is set even if the transmission failed */
2922 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2923 lp->stats.tx_errors++;
2924
2925 if (lp->skb) {
2926 dev_kfree_skb_irq(lp->skb);
2927 lp->skb = NULL;
2928 dma_unmap_single(NULL, lp->skb_physaddr,
2929 lp->skb_length, DMA_TO_DEVICE);
2930 lp->stats.tx_packets++;
2931 lp->stats.tx_bytes += lp->skb_length;
2932 }
2933 netif_wake_queue(dev);
2934 }
2935
2936 /* Work-around for EMAC Errata section 41.3.1 */
2937 if (intstatus & MACB_BIT(RXUBR)) {
2938 ctl = macb_readl(lp, NCR);
2939 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2940 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2941 }
2942
2943 if (intstatus & MACB_BIT(ISR_ROVR))
2944 netdev_err(dev, "ROVR error\n");
2945
2946 return IRQ_HANDLED;
2947}
2948
2949#ifdef CONFIG_NET_POLL_CONTROLLER
2950static void at91ether_poll_controller(struct net_device *dev)
2951{
2952 unsigned long flags;
2953
2954 local_irq_save(flags);
2955 at91ether_interrupt(dev->irq, dev);
2956 local_irq_restore(flags);
2957}
2958#endif
2959
2960static const struct net_device_ops at91ether_netdev_ops = {
2961 .ndo_open = at91ether_open,
2962 .ndo_stop = at91ether_close,
2963 .ndo_start_xmit = at91ether_start_xmit,
2964 .ndo_get_stats = macb_get_stats,
2965 .ndo_set_rx_mode = macb_set_rx_mode,
2966 .ndo_set_mac_address = eth_mac_addr,
2967 .ndo_do_ioctl = macb_ioctl,
2968 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002969#ifdef CONFIG_NET_POLL_CONTROLLER
2970 .ndo_poll_controller = at91ether_poll_controller,
2971#endif
2972};
2973
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002974static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302975 struct clk **hclk, struct clk **tx_clk,
2976 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002977{
2978 int err;
2979
2980 *hclk = NULL;
2981 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302982 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002983
2984 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2985 if (IS_ERR(*pclk))
2986 return PTR_ERR(*pclk);
2987
2988 err = clk_prepare_enable(*pclk);
2989 if (err) {
2990 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2991 return err;
2992 }
2993
2994 return 0;
2995}
2996
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002997static int at91ether_init(struct platform_device *pdev)
2998{
2999 struct net_device *dev = platform_get_drvdata(pdev);
3000 struct macb *bp = netdev_priv(dev);
3001 int err;
3002 u32 reg;
3003
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003004 dev->netdev_ops = &at91ether_netdev_ops;
3005 dev->ethtool_ops = &macb_ethtool_ops;
3006
3007 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3008 0, dev->name, dev);
3009 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003010 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003011
3012 macb_writel(bp, NCR, 0);
3013
3014 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3015 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3016 reg |= MACB_BIT(RM9200_RMII);
3017
3018 macb_writel(bp, NCFGR, reg);
3019
3020 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003021}
3022
David S. Miller3cef5c52015-03-09 23:38:02 -04003023static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003024 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003025 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003026 .init = macb_init,
3027};
3028
David S. Miller3cef5c52015-03-09 23:38:02 -04003029static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003030 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3031 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003032 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003033 .init = macb_init,
3034};
3035
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003036static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003037 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003038 .dma_burst_length = 16,
3039 .clk_init = macb_clk_init,
3040 .init = macb_init,
3041};
3042
David S. Miller3cef5c52015-03-09 23:38:02 -04003043static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003044 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3045 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003046 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003047 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003048 .init = macb_init,
3049};
3050
David S. Miller3cef5c52015-03-09 23:38:02 -04003051static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003052 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003053 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003054 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003055 .init = macb_init,
3056};
3057
David S. Miller3cef5c52015-03-09 23:38:02 -04003058static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003059 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003060 .init = at91ether_init,
3061};
3062
Neil Armstronge611b5b2016-01-05 14:39:17 +01003063static const struct macb_config np4_config = {
3064 .caps = MACB_CAPS_USRIO_DISABLED,
3065 .clk_init = macb_clk_init,
3066 .init = macb_init,
3067};
David S. Miller36583eb2015-05-23 01:22:35 -04003068
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303069static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303070 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303071 .dma_burst_length = 16,
3072 .clk_init = macb_clk_init,
3073 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303074 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303075};
3076
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003077static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303078 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003079 .dma_burst_length = 16,
3080 .clk_init = macb_clk_init,
3081 .init = macb_init,
3082};
3083
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003084static const struct of_device_id macb_dt_ids[] = {
3085 { .compatible = "cdns,at32ap7000-macb" },
3086 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3087 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003088 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003089 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3090 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003091 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003092 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3093 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3094 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3095 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303096 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003097 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003098 { /* sentinel */ }
3099};
3100MODULE_DEVICE_TABLE(of, macb_dt_ids);
3101#endif /* CONFIG_OF */
3102
3103static int macb_probe(struct platform_device *pdev)
3104{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003105 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303106 struct clk **, struct clk **, struct clk **)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003107 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003108 int (*init)(struct platform_device *) = macb_init;
3109 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003110 struct device_node *phy_node;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003111 const struct macb_config *macb_config = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303112 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003113 unsigned int queue_mask, num_queues;
3114 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003115 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003116 struct phy_device *phydev;
3117 struct net_device *dev;
3118 struct resource *regs;
3119 void __iomem *mem;
3120 const char *mac;
3121 struct macb *bp;
3122 int err;
3123
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003124 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3125 mem = devm_ioremap_resource(&pdev->dev, regs);
3126 if (IS_ERR(mem))
3127 return PTR_ERR(mem);
3128
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003129 if (np) {
3130 const struct of_device_id *match;
3131
3132 match = of_match_node(macb_dt_ids, np);
3133 if (match && match->data) {
3134 macb_config = match->data;
3135 clk_init = macb_config->clk_init;
3136 init = macb_config->init;
3137 }
3138 }
3139
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303140 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003141 if (err)
3142 return err;
3143
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003144 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003145
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003146 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003147 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003148 if (!dev) {
3149 err = -ENOMEM;
3150 goto err_disable_clocks;
3151 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003152
3153 dev->base_addr = regs->start;
3154
3155 SET_NETDEV_DEV(dev, &pdev->dev);
3156
3157 bp = netdev_priv(dev);
3158 bp->pdev = pdev;
3159 bp->dev = dev;
3160 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003161 bp->native_io = native_io;
3162 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003163 bp->macb_reg_readl = hw_readl_native;
3164 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003165 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003166 bp->macb_reg_readl = hw_readl;
3167 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003168 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003169 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003170 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003171 if (macb_config)
3172 bp->dma_burst_length = macb_config->dma_burst_length;
3173 bp->pclk = pclk;
3174 bp->hclk = hclk;
3175 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303176 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003177 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303178 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303179
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003180 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003181 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003182 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3183 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3184
Harini Katakamfff80192016-08-09 13:15:53 +05303185#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3186 if (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1)) > GEM_DBW32)
3187 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3188#endif
3189
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003190 spin_lock_init(&bp->lock);
3191
Nicolas Ferread783472015-03-31 15:02:02 +02003192 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003193 macb_configure_caps(bp, macb_config);
3194
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003195 platform_set_drvdata(pdev, dev);
3196
3197 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003198 if (dev->irq < 0) {
3199 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003200 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003201 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003202
Jarod Wilson44770e12016-10-17 15:54:17 -04003203 /* MTU range: 68 - 1500 or 10240 */
3204 dev->min_mtu = GEM_MTU_MIN_SIZE;
3205 if (bp->caps & MACB_CAPS_JUMBO)
3206 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3207 else
3208 dev->max_mtu = ETH_DATA_LEN;
3209
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003210 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003211 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003212 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003213 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003214 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003215
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003216 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003217 phy_node = of_get_next_available_child(np, NULL);
3218 if (phy_node) {
3219 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003220
Charles Keepax0e3e7992016-03-28 13:47:42 +01003221 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003222 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003223 gpiod_direction_output(bp->reset_gpio, 1);
3224 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003225 }
3226 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003227
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003228 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003229 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003230 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003231 if (pdata && pdata->is_rmii)
3232 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3233 else
3234 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3235 } else {
3236 bp->phy_interface = err;
3237 }
3238
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003239 /* IP specific init */
3240 err = init(pdev);
3241 if (err)
3242 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003243
Florian Fainellicf669662016-05-02 18:38:45 -07003244 err = macb_mii_init(bp);
3245 if (err)
3246 goto err_out_free_netdev;
3247
Philippe Reynes0a912812016-06-22 00:32:35 +02003248 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003249
3250 netif_carrier_off(dev);
3251
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003252 err = register_netdev(dev);
3253 if (err) {
3254 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003255 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003256 }
3257
Florian Fainellicf669662016-05-02 18:38:45 -07003258 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003259
Bo Shen58798232014-09-13 01:57:49 +02003260 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3261 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3262 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003263
3264 return 0;
3265
Florian Fainellicf669662016-05-02 18:38:45 -07003266err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003267 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003268 mdiobus_unregister(bp->mii_bus);
3269 mdiobus_free(bp->mii_bus);
3270
3271 /* Shutdown the PHY if there is a GPIO reset */
3272 if (bp->reset_gpio)
3273 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003274
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003275err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003276 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003277
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003278err_disable_clocks:
3279 clk_disable_unprepare(tx_clk);
3280 clk_disable_unprepare(hclk);
3281 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303282 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003283
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003284 return err;
3285}
3286
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003287static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003288{
3289 struct net_device *dev;
3290 struct macb *bp;
3291
3292 dev = platform_get_drvdata(pdev);
3293
3294 if (dev) {
3295 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003296 if (dev->phydev)
3297 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003298 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003299 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003300 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003301
3302 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003303 if (bp->reset_gpio)
3304 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003305
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003306 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003307 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003308 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003309 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303310 clk_disable_unprepare(bp->rx_clk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003311 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003312 }
3313
3314 return 0;
3315}
3316
Michal Simekd23823d2015-01-23 09:36:03 +01003317static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003318{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003319 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003320 struct net_device *netdev = platform_get_drvdata(pdev);
3321 struct macb *bp = netdev_priv(netdev);
3322
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003323 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003324 netif_device_detach(netdev);
3325
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003326 if (bp->wol & MACB_WOL_ENABLED) {
3327 macb_writel(bp, IER, MACB_BIT(WOL));
3328 macb_writel(bp, WOL, MACB_BIT(MAG));
3329 enable_irq_wake(bp->queues[0].irq);
3330 } else {
3331 clk_disable_unprepare(bp->tx_clk);
3332 clk_disable_unprepare(bp->hclk);
3333 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303334 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003335 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003336
3337 return 0;
3338}
3339
Michal Simekd23823d2015-01-23 09:36:03 +01003340static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003341{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003342 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003343 struct net_device *netdev = platform_get_drvdata(pdev);
3344 struct macb *bp = netdev_priv(netdev);
3345
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003346 if (bp->wol & MACB_WOL_ENABLED) {
3347 macb_writel(bp, IDR, MACB_BIT(WOL));
3348 macb_writel(bp, WOL, 0);
3349 disable_irq_wake(bp->queues[0].irq);
3350 } else {
3351 clk_prepare_enable(bp->pclk);
3352 clk_prepare_enable(bp->hclk);
3353 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303354 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003355 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003356
3357 netif_device_attach(netdev);
3358
3359 return 0;
3360}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003361
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003362static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3363
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003364static struct platform_driver macb_driver = {
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003365 .probe = macb_probe,
3366 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003367 .driver = {
3368 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003369 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003370 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003371 },
3372};
3373
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003374module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003375
3376MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003377MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003378MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003379MODULE_ALIAS("platform:macb");