blob: c0fb80acc2dad4b91d3b3cb8198be50dc9597fbd [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 Eastwood2dbfdbb92012-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 Shevchenkof2ce8a9e2015-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 Shevchenkof2ce8a9e2015-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 Eastwood2dbfdbb92012-11-11 13:56:27 +0000388 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000389 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-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 Eastwood2dbfdbb92012-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 Eastwood2dbfdbb92012-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 }
Bartosz Folta83a77e92016-12-14 06:39:15 +0000407 } else {
408 phydev->irq = PHY_POLL;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000409 }
frederic RODO6c36a702007-07-12 19:07:24 +0200410
411 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000412 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100413 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000414 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000415 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000416 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200417 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100418
frederic RODO6c36a702007-07-12 19:07:24 +0200419 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200420 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000421 phydev->supported &= PHY_GBIT_FEATURES;
422 else
423 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100424
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500425 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
426 phydev->supported &= ~SUPPORTED_1000baseT_Half;
427
frederic RODO6c36a702007-07-12 19:07:24 +0200428 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100429
frederic RODO6c36a702007-07-12 19:07:24 +0200430 bp->link = 0;
431 bp->speed = 0;
432 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200433
434 return 0;
435}
436
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100437static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200438{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000439 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200440 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200441 int err = -ENXIO, i;
442
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200443 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200444 macb_writel(bp, NCR, MACB_BIT(MPE));
445
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700446 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700447 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200448 err = -ENOMEM;
449 goto err_out;
450 }
451
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700452 bp->mii_bus->name = "MACB_mii_bus";
453 bp->mii_bus->read = &macb_mdio_read;
454 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000455 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700456 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700457 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700458 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900459 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700460
Jamie Iles91523942011-02-28 04:05:25 +0000461 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200462
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200463 np = bp->pdev->dev.of_node;
464 if (np) {
465 /* try dt phy registration */
466 err = of_mdiobus_register(bp->mii_bus, np);
467
468 /* fallback to standard phy registration if no phy were
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700469 * found during dt phy registration
470 */
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200471 if (!err && !phy_find_first(bp->mii_bus)) {
472 for (i = 0; i < PHY_MAX_ADDR; i++) {
473 struct phy_device *phydev;
474
475 phydev = mdiobus_scan(bp->mii_bus, i);
Sergei Shtylyovce24c2b2016-05-01 01:47:36 +0300476 if (IS_ERR(phydev) &&
477 PTR_ERR(phydev) != -ENODEV) {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200478 err = PTR_ERR(phydev);
479 break;
480 }
481 }
482
483 if (err)
484 goto err_out_unregister_bus;
485 }
486 } else {
Bartosz Folta83a77e92016-12-14 06:39:15 +0000487 for (i = 0; i < PHY_MAX_ADDR; i++)
488 bp->mii_bus->irq[i] = PHY_POLL;
489
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200490 if (pdata)
491 bp->mii_bus->phy_mask = pdata->phy_mask;
492
493 err = mdiobus_register(bp->mii_bus);
494 }
495
496 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100497 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200498
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200499 err = macb_mii_probe(bp->dev);
500 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200501 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200502
503 return 0;
504
505err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700506 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700507err_out_free_mdiobus:
508 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200509err_out:
510 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100511}
512
513static void macb_update_stats(struct macb *bp)
514{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000515 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
516 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300517 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100518
519 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
520
Moritz Fischer96ec6312016-03-29 19:11:11 -0700521 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700522 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100523}
524
Nicolas Ferree86cd532012-10-31 06:04:57 +0000525static int macb_halt_tx(struct macb *bp)
526{
527 unsigned long halt_time, timeout;
528 u32 status;
529
530 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
531
532 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
533 do {
534 halt_time = jiffies;
535 status = macb_readl(bp, TSR);
536 if (!(status & MACB_BIT(TGO)))
537 return 0;
538
539 usleep_range(10, 250);
540 } while (time_before(halt_time, timeout));
541
542 return -ETIMEDOUT;
543}
544
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200545static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
546{
547 if (tx_skb->mapping) {
548 if (tx_skb->mapped_as_page)
549 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
550 tx_skb->size, DMA_TO_DEVICE);
551 else
552 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
553 tx_skb->size, DMA_TO_DEVICE);
554 tx_skb->mapping = 0;
555 }
556
557 if (tx_skb->skb) {
558 dev_kfree_skb_any(tx_skb->skb);
559 tx_skb->skb = NULL;
560 }
561}
562
Harini Katakamfff80192016-08-09 13:15:53 +0530563static inline void macb_set_addr(struct macb_dma_desc *desc, dma_addr_t addr)
564{
565 desc->addr = (u32)addr;
566#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
567 desc->addrh = (u32)(addr >> 32);
568#endif
569}
570
Nicolas Ferree86cd532012-10-31 06:04:57 +0000571static void macb_tx_error_task(struct work_struct *work)
572{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100573 struct macb_queue *queue = container_of(work, struct macb_queue,
574 tx_error_task);
575 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000576 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100577 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000578 struct sk_buff *skb;
579 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100580 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000581
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100582 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
583 (unsigned int)(queue - bp->queues),
584 queue->tx_tail, queue->tx_head);
585
586 /* Prevent the queue IRQ handlers from running: each of them may call
587 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
588 * As explained below, we have to halt the transmission before updating
589 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
590 * network engine about the macb/gem being halted.
591 */
592 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000593
594 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100595 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000596
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700597 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000598 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100599 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000600 */
601 if (macb_halt_tx(bp))
602 /* Just complain for now, reinitializing TX path can be good */
603 netdev_err(bp->dev, "BUG: halt tx timed out\n");
604
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700605 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000606 * Free transmit buffers in upper layer.
607 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100608 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
609 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000610
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100611 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000612 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100613 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000614 skb = tx_skb->skb;
615
616 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200617 /* skb is set for the last buffer of the frame */
618 while (!skb) {
619 macb_tx_unmap(bp, tx_skb);
620 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100621 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200622 skb = tx_skb->skb;
623 }
624
625 /* ctrl still refers to the first buffer descriptor
626 * since it's the only one written back by the hardware
627 */
628 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
629 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500630 macb_tx_ring_wrap(bp, tail),
631 skb->data);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200632 bp->stats.tx_packets++;
633 bp->stats.tx_bytes += skb->len;
634 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000635 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700636 /* "Buffers exhausted mid-frame" errors may only happen
637 * if the driver is buggy, so complain loudly about
638 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000639 */
640 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
641 netdev_err(bp->dev,
642 "BUG: TX buffers exhausted mid-frame\n");
643
644 desc->ctrl = ctrl | MACB_BIT(TX_USED);
645 }
646
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200647 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000648 }
649
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100650 /* Set end of TX queue */
651 desc = macb_tx_desc(queue, 0);
Harini Katakamfff80192016-08-09 13:15:53 +0530652 macb_set_addr(desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100653 desc->ctrl = MACB_BIT(TX_USED);
654
Nicolas Ferree86cd532012-10-31 06:04:57 +0000655 /* Make descriptor updates visible to hardware */
656 wmb();
657
658 /* Reinitialize the TX desc queue */
Harini Katakamfff80192016-08-09 13:15:53 +0530659 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
660#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
661 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
662#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000663 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100664 queue->tx_head = 0;
665 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000666
667 /* Housework before enabling TX IRQ */
668 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100669 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
670
671 /* Now we are ready to start transmission again */
672 netif_tx_start_all_queues(bp->dev);
673 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
674
675 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000676}
677
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100678static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100679{
680 unsigned int tail;
681 unsigned int head;
682 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100683 struct macb *bp = queue->bp;
684 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100685
686 status = macb_readl(bp, TSR);
687 macb_writel(bp, TSR, status);
688
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000689 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100690 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000691
Nicolas Ferree86cd532012-10-31 06:04:57 +0000692 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700693 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100694
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100695 head = queue->tx_head;
696 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000697 struct macb_tx_skb *tx_skb;
698 struct sk_buff *skb;
699 struct macb_dma_desc *desc;
700 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100701
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100702 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100703
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000704 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100705 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000706
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000707 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100708
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200709 /* TX_USED bit is only set by hardware on the very first buffer
710 * descriptor of the transmitted frame.
711 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000712 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100713 break;
714
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200715 /* Process all buffers of the current transmitted frame */
716 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100717 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200718 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000719
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200720 /* First, update TX stats if needed */
721 if (skb) {
722 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500723 macb_tx_ring_wrap(bp, tail),
724 skb->data);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200725 bp->stats.tx_packets++;
726 bp->stats.tx_bytes += skb->len;
727 }
728
729 /* Now we can safely release resources */
730 macb_tx_unmap(bp, tx_skb);
731
732 /* skb is set only for the last buffer of the frame.
733 * WARNING: at this point skb has been freed by
734 * macb_tx_unmap().
735 */
736 if (skb)
737 break;
738 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100739 }
740
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100741 queue->tx_tail = tail;
742 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
743 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500744 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100745 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100746}
747
Nicolas Ferre4df95132013-06-04 21:57:12 +0000748static void gem_rx_refill(struct macb *bp)
749{
750 unsigned int entry;
751 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000752 dma_addr_t paddr;
753
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700754 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500755 bp->rx_ring_size) > 0) {
756 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000757
758 /* Make hw descriptor updates visible to CPU */
759 rmb();
760
Nicolas Ferre4df95132013-06-04 21:57:12 +0000761 bp->rx_prepared_head++;
762
Moritz Fischeraa50b552016-03-29 19:11:13 -0700763 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000764 /* allocate sk_buff for this free entry in ring */
765 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700766 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000767 netdev_err(bp->dev,
768 "Unable to allocate sk_buff\n");
769 break;
770 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000771
772 /* now fill corresponding descriptor entry */
773 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700774 bp->rx_buffer_size,
775 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800776 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
777 dev_kfree_skb(skb);
778 break;
779 }
780
781 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000782
Zach Brownb410d132016-10-19 09:56:57 -0500783 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000784 paddr |= MACB_BIT(RX_WRAP);
Harini Katakamfff80192016-08-09 13:15:53 +0530785 macb_set_addr(&(bp->rx_ring[entry]), paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000786 bp->rx_ring[entry].ctrl = 0;
787
788 /* properly align Ethernet header */
789 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530790 } else {
791 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
792 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000793 }
794 }
795
796 /* Make descriptor updates visible to hardware */
797 wmb();
798
799 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700800 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000801}
802
803/* Mark DMA descriptors from begin up to and not including end as unused */
804static void discard_partial_frame(struct macb *bp, unsigned int begin,
805 unsigned int end)
806{
807 unsigned int frag;
808
809 for (frag = begin; frag != end; frag++) {
810 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700811
Nicolas Ferre4df95132013-06-04 21:57:12 +0000812 desc->addr &= ~MACB_BIT(RX_USED);
813 }
814
815 /* Make descriptor updates visible to hardware */
816 wmb();
817
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700818 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000819 * whatever caused this is updated, so we don't have to record
820 * anything.
821 */
822}
823
824static int gem_rx(struct macb *bp, int budget)
825{
826 unsigned int len;
827 unsigned int entry;
828 struct sk_buff *skb;
829 struct macb_dma_desc *desc;
830 int count = 0;
831
832 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530833 u32 ctrl;
834 dma_addr_t addr;
835 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000836
Zach Brownb410d132016-10-19 09:56:57 -0500837 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000838 desc = &bp->rx_ring[entry];
839
840 /* Make hw descriptor updates visible to CPU */
841 rmb();
842
Harini Katakamfff80192016-08-09 13:15:53 +0530843 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
844 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
845#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
846 addr |= ((u64)(desc->addrh) << 32);
847#endif
Nicolas Ferre4df95132013-06-04 21:57:12 +0000848 ctrl = desc->ctrl;
849
Harini Katakamfff80192016-08-09 13:15:53 +0530850 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000851 break;
852
Nicolas Ferre4df95132013-06-04 21:57:12 +0000853 bp->rx_tail++;
854 count++;
855
856 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
857 netdev_err(bp->dev,
858 "not whole frame pointed by descriptor\n");
859 bp->stats.rx_dropped++;
860 break;
861 }
862 skb = bp->rx_skbuff[entry];
863 if (unlikely(!skb)) {
864 netdev_err(bp->dev,
865 "inconsistent Rx descriptor chain\n");
866 bp->stats.rx_dropped++;
867 break;
868 }
869 /* now everything is ready for receiving packet */
870 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530871 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000872
873 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
874
875 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000876 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800877 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000878
879 skb->protocol = eth_type_trans(skb, bp->dev);
880 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200881 if (bp->dev->features & NETIF_F_RXCSUM &&
882 !(bp->dev->flags & IFF_PROMISC) &&
883 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
884 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000885
886 bp->stats.rx_packets++;
887 bp->stats.rx_bytes += skb->len;
888
889#if defined(DEBUG) && defined(VERBOSE_DEBUG)
890 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
891 skb->len, skb->csum);
892 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100893 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000894 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
895 skb->data, 32, true);
896#endif
897
898 netif_receive_skb(skb);
899 }
900
901 gem_rx_refill(bp);
902
903 return count;
904}
905
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100906static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
907 unsigned int last_frag)
908{
909 unsigned int len;
910 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000911 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100912 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000913 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100914
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000915 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530916 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100917
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000918 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -0500919 macb_rx_ring_wrap(bp, first_frag),
920 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100921
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700922 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000923 * first buffer. Since the header is 14 bytes, this makes the
924 * payload word-aligned.
925 *
926 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
927 * the two padding bytes into the skb so that we avoid hitting
928 * the slowpath in memcpy(), and pull them off afterwards.
929 */
930 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100931 if (!skb) {
932 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000933 for (frag = first_frag; ; frag++) {
934 desc = macb_rx_desc(bp, frag);
935 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100936 if (frag == last_frag)
937 break;
938 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000939
940 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100941 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000942
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100943 return 1;
944 }
945
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000946 offset = 0;
947 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700948 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100949 skb_put(skb, len);
950
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000951 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000952 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100953
954 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100955 if (unlikely(frag != last_frag)) {
956 dev_kfree_skb_any(skb);
957 return -1;
958 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100959 frag_len = len - offset;
960 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300961 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -0700962 macb_rx_buffer(bp, frag),
963 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000964 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000965 desc = macb_rx_desc(bp, frag);
966 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100967
968 if (frag == last_frag)
969 break;
970 }
971
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000972 /* Make descriptor updates visible to hardware */
973 wmb();
974
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000975 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100976 skb->protocol = eth_type_trans(skb, bp->dev);
977
978 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000979 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000980 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700981 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100982 netif_receive_skb(skb);
983
984 return 0;
985}
986
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100987static inline void macb_init_rx_ring(struct macb *bp)
988{
989 dma_addr_t addr;
990 int i;
991
992 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -0500993 for (i = 0; i < bp->rx_ring_size; i++) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100994 bp->rx_ring[i].addr = addr;
995 bp->rx_ring[i].ctrl = 0;
996 addr += bp->rx_buffer_size;
997 }
Zach Brownb410d132016-10-19 09:56:57 -0500998 bp->rx_ring[bp->rx_ring_size - 1].addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchena0b44ee2016-11-28 14:40:55 +0100999 bp->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001000}
1001
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001002static int macb_rx(struct macb *bp, int budget)
1003{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001004 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001005 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001006 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001007 int first_frag = -1;
1008
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001009 for (tail = bp->rx_tail; budget > 0; tail++) {
1010 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001011 u32 addr, ctrl;
1012
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001013 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001014 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001015
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001016 addr = desc->addr;
1017 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001018
1019 if (!(addr & MACB_BIT(RX_USED)))
1020 break;
1021
1022 if (ctrl & MACB_BIT(RX_SOF)) {
1023 if (first_frag != -1)
1024 discard_partial_frame(bp, first_frag, tail);
1025 first_frag = tail;
1026 }
1027
1028 if (ctrl & MACB_BIT(RX_EOF)) {
1029 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001030
1031 if (unlikely(first_frag == -1)) {
1032 reset_rx_queue = true;
1033 continue;
1034 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001035
1036 dropped = macb_rx_frame(bp, first_frag, tail);
1037 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001038 if (unlikely(dropped < 0)) {
1039 reset_rx_queue = true;
1040 continue;
1041 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001042 if (!dropped) {
1043 received++;
1044 budget--;
1045 }
1046 }
1047 }
1048
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001049 if (unlikely(reset_rx_queue)) {
1050 unsigned long flags;
1051 u32 ctrl;
1052
1053 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1054
1055 spin_lock_irqsave(&bp->lock, flags);
1056
1057 ctrl = macb_readl(bp, NCR);
1058 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1059
1060 macb_init_rx_ring(bp);
1061 macb_writel(bp, RBQP, bp->rx_ring_dma);
1062
1063 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1064
1065 spin_unlock_irqrestore(&bp->lock, flags);
1066 return received;
1067 }
1068
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001069 if (first_frag != -1)
1070 bp->rx_tail = first_frag;
1071 else
1072 bp->rx_tail = tail;
1073
1074 return received;
1075}
1076
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001077static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001078{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001079 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001080 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001081 u32 status;
1082
1083 status = macb_readl(bp, RSR);
1084 macb_writel(bp, RSR, status);
1085
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001086 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001087
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001088 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001089 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001090
Nicolas Ferre4df95132013-06-04 21:57:12 +00001091 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001092 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001093 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001094
Nicolas Ferre8770e912013-02-12 11:08:48 +01001095 /* Packets received while interrupts were disabled */
1096 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001097 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001098 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1099 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001100 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001101 } else {
1102 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1103 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001104 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001105
1106 /* TODO: Handle errors */
1107
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001108 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001109}
1110
1111static irqreturn_t macb_interrupt(int irq, void *dev_id)
1112{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001113 struct macb_queue *queue = dev_id;
1114 struct macb *bp = queue->bp;
1115 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001116 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001117
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001118 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001119
1120 if (unlikely(!status))
1121 return IRQ_NONE;
1122
1123 spin_lock(&bp->lock);
1124
1125 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001126 /* close possible race with dev_close */
1127 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001128 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001129 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1130 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001131 break;
1132 }
1133
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001134 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1135 (unsigned int)(queue - bp->queues),
1136 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001137
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001138 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001139 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001140 * until we have processed the buffers. The
1141 * scheduling call may fail if the poll routine
1142 * is already scheduled, so disable interrupts
1143 * now.
1144 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001145 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001146 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001147 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001148
Ben Hutchings288379f2009-01-19 16:43:59 -08001149 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001150 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001151 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001152 }
1153 }
1154
Nicolas Ferree86cd532012-10-31 06:04:57 +00001155 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001156 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1157 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001158
1159 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001160 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001161
Nicolas Ferree86cd532012-10-31 06:04:57 +00001162 break;
1163 }
1164
1165 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001166 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001167
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001168 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001169 * add that if/when we get our hands on a full-blown MII PHY.
1170 */
1171
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001172 /* There is a hardware issue under heavy load where DMA can
1173 * stop, this causes endless "used buffer descriptor read"
1174 * interrupts but it can be cleared by re-enabling RX. See
1175 * the at91 manual, section 41.3.1 or the Zynq manual
1176 * section 16.7.4 for details.
1177 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001178 if (status & MACB_BIT(RXUBR)) {
1179 ctrl = macb_readl(bp, NCR);
1180 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001181 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001182 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1183
1184 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001185 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001186 }
1187
Alexander Steinb19f7f72011-04-13 05:03:24 +00001188 if (status & MACB_BIT(ISR_ROVR)) {
1189 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001190 if (macb_is_gem(bp))
1191 bp->hw_stats.gem.rx_overruns++;
1192 else
1193 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001194
1195 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001196 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001197 }
1198
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001199 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001200 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001201 * netdev_err to a lower-priority context as well
1202 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001203 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001204 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001205
1206 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001207 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001208 }
1209
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001210 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001211 }
1212
1213 spin_unlock(&bp->lock);
1214
1215 return IRQ_HANDLED;
1216}
1217
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001218#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001219/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001220 * to allow network i/o with interrupts disabled.
1221 */
1222static void macb_poll_controller(struct net_device *dev)
1223{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001224 struct macb *bp = netdev_priv(dev);
1225 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001226 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001227 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001228
1229 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001230 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1231 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001232 local_irq_restore(flags);
1233}
1234#endif
1235
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001236static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001237 struct macb_queue *queue,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001238 struct sk_buff *skb,
1239 unsigned int hdrlen)
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001240{
1241 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001242 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001243 struct macb_tx_skb *tx_skb = NULL;
1244 struct macb_dma_desc *desc;
1245 unsigned int offset, size, count = 0;
1246 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001247 unsigned int eof = 1, mss_mfs = 0;
1248 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1249
1250 /* LSO */
1251 if (skb_shinfo(skb)->gso_size != 0) {
1252 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1253 /* UDP - UFO */
1254 lso_ctrl = MACB_LSO_UFO_ENABLE;
1255 else
1256 /* TCP - TSO */
1257 lso_ctrl = MACB_LSO_TSO_ENABLE;
1258 }
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001259
1260 /* First, map non-paged data */
1261 len = skb_headlen(skb);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001262
1263 /* first buffer length */
1264 size = hdrlen;
1265
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001266 offset = 0;
1267 while (len) {
Zach Brownb410d132016-10-19 09:56:57 -05001268 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001269 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001270
1271 mapping = dma_map_single(&bp->pdev->dev,
1272 skb->data + offset,
1273 size, DMA_TO_DEVICE);
1274 if (dma_mapping_error(&bp->pdev->dev, mapping))
1275 goto dma_error;
1276
1277 /* Save info to properly release resources */
1278 tx_skb->skb = NULL;
1279 tx_skb->mapping = mapping;
1280 tx_skb->size = size;
1281 tx_skb->mapped_as_page = false;
1282
1283 len -= size;
1284 offset += size;
1285 count++;
1286 tx_head++;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001287
1288 size = min(len, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001289 }
1290
1291 /* Then, map paged data from fragments */
1292 for (f = 0; f < nr_frags; f++) {
1293 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1294
1295 len = skb_frag_size(frag);
1296 offset = 0;
1297 while (len) {
1298 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001299 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001300 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001301
1302 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1303 offset, size, DMA_TO_DEVICE);
1304 if (dma_mapping_error(&bp->pdev->dev, mapping))
1305 goto dma_error;
1306
1307 /* Save info to properly release resources */
1308 tx_skb->skb = NULL;
1309 tx_skb->mapping = mapping;
1310 tx_skb->size = size;
1311 tx_skb->mapped_as_page = true;
1312
1313 len -= size;
1314 offset += size;
1315 count++;
1316 tx_head++;
1317 }
1318 }
1319
1320 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001321 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001322 netdev_err(bp->dev, "BUG! empty skb!\n");
1323 return 0;
1324 }
1325
1326 /* This is the last buffer of the frame: save socket buffer */
1327 tx_skb->skb = skb;
1328
1329 /* Update TX ring: update buffer descriptors in reverse order
1330 * to avoid race condition
1331 */
1332
1333 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1334 * to set the end of TX queue
1335 */
1336 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001337 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001338 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001339 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001340 desc->ctrl = ctrl;
1341
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001342 if (lso_ctrl) {
1343 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1344 /* include header and FCS in value given to h/w */
1345 mss_mfs = skb_shinfo(skb)->gso_size +
1346 skb_transport_offset(skb) +
1347 ETH_FCS_LEN;
1348 else /* TSO */ {
1349 mss_mfs = skb_shinfo(skb)->gso_size;
1350 /* TCP Sequence Number Source Select
1351 * can be set only for TSO
1352 */
1353 seq_ctrl = 0;
1354 }
1355 }
1356
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001357 do {
1358 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001359 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001360 tx_skb = &queue->tx_skb[entry];
1361 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001362
1363 ctrl = (u32)tx_skb->size;
1364 if (eof) {
1365 ctrl |= MACB_BIT(TX_LAST);
1366 eof = 0;
1367 }
Zach Brownb410d132016-10-19 09:56:57 -05001368 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001369 ctrl |= MACB_BIT(TX_WRAP);
1370
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001371 /* First descriptor is header descriptor */
1372 if (i == queue->tx_head) {
1373 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1374 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1375 } else
1376 /* Only set MSS/MFS on payload descriptors
1377 * (second or later descriptor)
1378 */
1379 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1380
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001381 /* Set TX buffer descriptor */
Harini Katakamfff80192016-08-09 13:15:53 +05301382 macb_set_addr(desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001383 /* desc->addr must be visible to hardware before clearing
1384 * 'TX_USED' bit in desc->ctrl.
1385 */
1386 wmb();
1387 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001388 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001389
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001390 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001391
1392 return count;
1393
1394dma_error:
1395 netdev_err(bp->dev, "TX DMA map failed\n");
1396
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001397 for (i = queue->tx_head; i != tx_head; i++) {
1398 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001399
1400 macb_tx_unmap(bp, tx_skb);
1401 }
1402
1403 return 0;
1404}
1405
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001406static netdev_features_t macb_features_check(struct sk_buff *skb,
1407 struct net_device *dev,
1408 netdev_features_t features)
1409{
1410 unsigned int nr_frags, f;
1411 unsigned int hdrlen;
1412
1413 /* Validate LSO compatibility */
1414
1415 /* there is only one buffer */
1416 if (!skb_is_nonlinear(skb))
1417 return features;
1418
1419 /* length of header */
1420 hdrlen = skb_transport_offset(skb);
1421 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1422 hdrlen += tcp_hdrlen(skb);
1423
1424 /* For LSO:
1425 * When software supplies two or more payload buffers all payload buffers
1426 * apart from the last must be a multiple of 8 bytes in size.
1427 */
1428 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1429 return features & ~MACB_NETIF_LSO;
1430
1431 nr_frags = skb_shinfo(skb)->nr_frags;
1432 /* No need to check last fragment */
1433 nr_frags--;
1434 for (f = 0; f < nr_frags; f++) {
1435 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1436
1437 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1438 return features & ~MACB_NETIF_LSO;
1439 }
1440 return features;
1441}
1442
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001443static inline int macb_clear_csum(struct sk_buff *skb)
1444{
1445 /* no change for packets without checksum offloading */
1446 if (skb->ip_summed != CHECKSUM_PARTIAL)
1447 return 0;
1448
1449 /* make sure we can modify the header */
1450 if (unlikely(skb_cow_head(skb, 0)))
1451 return -1;
1452
1453 /* initialize checksum field
1454 * This is required - at least for Zynq, which otherwise calculates
1455 * wrong UDP header checksums for UDP packets with UDP data len <=2
1456 */
1457 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1458 return 0;
1459}
1460
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001461static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1462{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001463 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001464 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001465 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001466 unsigned long flags;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001467 unsigned int desc_cnt, nr_frags, frag_size, f;
1468 unsigned int hdrlen;
1469 bool is_lso, is_udp = 0;
1470
1471 is_lso = (skb_shinfo(skb)->gso_size != 0);
1472
1473 if (is_lso) {
1474 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1475
1476 /* length of headers */
1477 if (is_udp)
1478 /* only queue eth + ip headers separately for UDP */
1479 hdrlen = skb_transport_offset(skb);
1480 else
1481 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1482 if (skb_headlen(skb) < hdrlen) {
1483 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1484 /* if this is required, would need to copy to single buffer */
1485 return NETDEV_TX_BUSY;
1486 }
1487 } else
1488 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001489
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001490#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1491 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001492 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1493 queue_index, skb->len, skb->head, skb->data,
1494 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001495 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1496 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001497#endif
1498
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001499 /* Count how many TX buffer descriptors are needed to send this
1500 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001501 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001502 */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001503 if (is_lso && (skb_headlen(skb) > hdrlen))
1504 /* extra header descriptor if also payload in first buffer */
1505 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1506 else
1507 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001508 nr_frags = skb_shinfo(skb)->nr_frags;
1509 for (f = 0; f < nr_frags; f++) {
1510 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001511 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001512 }
1513
Dongdong Deng48719532009-08-23 19:49:07 -07001514 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001515
1516 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001517 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001518 bp->tx_ring_size) < desc_cnt) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001519 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001520 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001521 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001522 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001523 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001524 }
1525
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001526 if (macb_clear_csum(skb)) {
1527 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001528 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001529 }
1530
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001531 /* Map socket buffer for DMA transfer */
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00001532 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001533 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001534 goto unlock;
1535 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001536
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001537 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001538 wmb();
1539
Richard Cochrane0720922011-06-19 21:51:28 +00001540 skb_tx_timestamp(skb);
1541
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001542 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1543
Zach Brownb410d132016-10-19 09:56:57 -05001544 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001545 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001546
Soren Brinkmann92030902014-03-04 08:46:39 -08001547unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001548 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001549
Patrick McHardy6ed10652009-06-23 06:03:08 +00001550 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001551}
1552
Nicolas Ferre4df95132013-06-04 21:57:12 +00001553static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001554{
1555 if (!macb_is_gem(bp)) {
1556 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1557 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001558 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001559
Nicolas Ferre1b447912013-06-04 21:57:11 +00001560 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001561 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001562 "RX buffer must be multiple of %d bytes, expanding\n",
1563 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001564 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001565 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001566 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001567 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001568
1569 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1570 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001571}
1572
Nicolas Ferre4df95132013-06-04 21:57:12 +00001573static void gem_free_rx_buffers(struct macb *bp)
1574{
1575 struct sk_buff *skb;
1576 struct macb_dma_desc *desc;
1577 dma_addr_t addr;
1578 int i;
1579
1580 if (!bp->rx_skbuff)
1581 return;
1582
Zach Brownb410d132016-10-19 09:56:57 -05001583 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001584 skb = bp->rx_skbuff[i];
1585
Moritz Fischeraa50b552016-03-29 19:11:13 -07001586 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001587 continue;
1588
1589 desc = &bp->rx_ring[i];
1590 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Harini Katakamfff80192016-08-09 13:15:53 +05301591#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1592 addr |= ((u64)(desc->addrh) << 32);
1593#endif
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001594 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001595 DMA_FROM_DEVICE);
1596 dev_kfree_skb_any(skb);
1597 skb = NULL;
1598 }
1599
1600 kfree(bp->rx_skbuff);
1601 bp->rx_skbuff = NULL;
1602}
1603
1604static void macb_free_rx_buffers(struct macb *bp)
1605{
1606 if (bp->rx_buffers) {
1607 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001608 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001609 bp->rx_buffers, bp->rx_buffers_dma);
1610 bp->rx_buffers = NULL;
1611 }
1612}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001613
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001614static void macb_free_consistent(struct macb *bp)
1615{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001616 struct macb_queue *queue;
1617 unsigned int q;
1618
Nicolas Ferre4df95132013-06-04 21:57:12 +00001619 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001620 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001621 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001622 bp->rx_ring, bp->rx_ring_dma);
1623 bp->rx_ring = NULL;
1624 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001625
1626 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1627 kfree(queue->tx_skb);
1628 queue->tx_skb = NULL;
1629 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001630 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001631 queue->tx_ring, queue->tx_ring_dma);
1632 queue->tx_ring = NULL;
1633 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001634 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001635}
1636
1637static int gem_alloc_rx_buffers(struct macb *bp)
1638{
1639 int size;
1640
Zach Brownb410d132016-10-19 09:56:57 -05001641 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001642 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1643 if (!bp->rx_skbuff)
1644 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001645 else
1646 netdev_dbg(bp->dev,
1647 "Allocated %d RX struct sk_buff entries at %p\n",
1648 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001649 return 0;
1650}
1651
1652static int macb_alloc_rx_buffers(struct macb *bp)
1653{
1654 int size;
1655
Zach Brownb410d132016-10-19 09:56:57 -05001656 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001657 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1658 &bp->rx_buffers_dma, GFP_KERNEL);
1659 if (!bp->rx_buffers)
1660 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001661
1662 netdev_dbg(bp->dev,
1663 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1664 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001665 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001666}
1667
1668static int macb_alloc_consistent(struct macb *bp)
1669{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001670 struct macb_queue *queue;
1671 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001672 int size;
1673
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001674 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001675 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001676 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1677 &queue->tx_ring_dma,
1678 GFP_KERNEL);
1679 if (!queue->tx_ring)
1680 goto out_err;
1681 netdev_dbg(bp->dev,
1682 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1683 q, size, (unsigned long)queue->tx_ring_dma,
1684 queue->tx_ring);
1685
Zach Brownb410d132016-10-19 09:56:57 -05001686 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001687 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1688 if (!queue->tx_skb)
1689 goto out_err;
1690 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001691
Zach Brownb410d132016-10-19 09:56:57 -05001692 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001693 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1694 &bp->rx_ring_dma, GFP_KERNEL);
1695 if (!bp->rx_ring)
1696 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001697 netdev_dbg(bp->dev,
1698 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1699 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001700
Nicolas Ferre4df95132013-06-04 21:57:12 +00001701 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001702 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001703
1704 return 0;
1705
1706out_err:
1707 macb_free_consistent(bp);
1708 return -ENOMEM;
1709}
1710
Nicolas Ferre4df95132013-06-04 21:57:12 +00001711static void gem_init_rings(struct macb *bp)
1712{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001713 struct macb_queue *queue;
1714 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001715 int i;
1716
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001717 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001718 for (i = 0; i < bp->tx_ring_size; i++) {
1719 queue->tx_ring[i].addr = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001720 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1721 }
Zach Brownb410d132016-10-19 09:56:57 -05001722 queue->tx_ring[bp->tx_ring_size - 1].ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001723 queue->tx_head = 0;
1724 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001725 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001726
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001727 bp->rx_tail = 0;
1728 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001729
1730 gem_rx_refill(bp);
1731}
1732
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001733static void macb_init_rings(struct macb *bp)
1734{
1735 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001736
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001737 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001738
Zach Brownb410d132016-10-19 09:56:57 -05001739 for (i = 0; i < bp->tx_ring_size; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001740 bp->queues[0].tx_ring[i].addr = 0;
1741 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001742 }
Ben Shelton21d35152015-04-22 17:28:54 -05001743 bp->queues[0].tx_head = 0;
1744 bp->queues[0].tx_tail = 0;
Zach Brownb410d132016-10-19 09:56:57 -05001745 bp->queues[0].tx_ring[bp->tx_ring_size - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001746}
1747
1748static void macb_reset_hw(struct macb *bp)
1749{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001750 struct macb_queue *queue;
1751 unsigned int q;
1752
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001753 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001754 * more gracefully?)
1755 */
1756 macb_writel(bp, NCR, 0);
1757
1758 /* Clear the stats registers (XXX: Update stats first?) */
1759 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1760
1761 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001762 macb_writel(bp, TSR, -1);
1763 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001764
1765 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001766 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1767 queue_writel(queue, IDR, -1);
1768 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001769 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1770 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001771 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001772}
1773
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001774static u32 gem_mdc_clk_div(struct macb *bp)
1775{
1776 u32 config;
1777 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1778
1779 if (pclk_hz <= 20000000)
1780 config = GEM_BF(CLK, GEM_CLK_DIV8);
1781 else if (pclk_hz <= 40000000)
1782 config = GEM_BF(CLK, GEM_CLK_DIV16);
1783 else if (pclk_hz <= 80000000)
1784 config = GEM_BF(CLK, GEM_CLK_DIV32);
1785 else if (pclk_hz <= 120000000)
1786 config = GEM_BF(CLK, GEM_CLK_DIV48);
1787 else if (pclk_hz <= 160000000)
1788 config = GEM_BF(CLK, GEM_CLK_DIV64);
1789 else
1790 config = GEM_BF(CLK, GEM_CLK_DIV96);
1791
1792 return config;
1793}
1794
1795static u32 macb_mdc_clk_div(struct macb *bp)
1796{
1797 u32 config;
1798 unsigned long pclk_hz;
1799
1800 if (macb_is_gem(bp))
1801 return gem_mdc_clk_div(bp);
1802
1803 pclk_hz = clk_get_rate(bp->pclk);
1804 if (pclk_hz <= 20000000)
1805 config = MACB_BF(CLK, MACB_CLK_DIV8);
1806 else if (pclk_hz <= 40000000)
1807 config = MACB_BF(CLK, MACB_CLK_DIV16);
1808 else if (pclk_hz <= 80000000)
1809 config = MACB_BF(CLK, MACB_CLK_DIV32);
1810 else
1811 config = MACB_BF(CLK, MACB_CLK_DIV64);
1812
1813 return config;
1814}
1815
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001816/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001817 * should program. We find the width from decoding the design configuration
1818 * register to find the maximum supported data bus width.
1819 */
1820static u32 macb_dbw(struct macb *bp)
1821{
1822 if (!macb_is_gem(bp))
1823 return 0;
1824
1825 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1826 case 4:
1827 return GEM_BF(DBW, GEM_DBW128);
1828 case 2:
1829 return GEM_BF(DBW, GEM_DBW64);
1830 case 1:
1831 default:
1832 return GEM_BF(DBW, GEM_DBW32);
1833 }
1834}
1835
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001836/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001837 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001838 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001839 * (if not supported by FIFO, it will fallback to default)
1840 * - set both rx/tx packet buffers to full memory size
1841 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001842 */
1843static void macb_configure_dma(struct macb *bp)
1844{
1845 u32 dmacfg;
1846
1847 if (macb_is_gem(bp)) {
1848 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001849 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001850 if (bp->dma_burst_length)
1851 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001852 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301853 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301854
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03001855 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301856 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1857 else
1858 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1859
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001860 if (bp->dev->features & NETIF_F_HW_CSUM)
1861 dmacfg |= GEM_BIT(TXCOEN);
1862 else
1863 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301864
1865#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1866 dmacfg |= GEM_BIT(ADDR64);
1867#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001868 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1869 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001870 gem_writel(bp, DMACFG, dmacfg);
1871 }
1872}
1873
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001874static void macb_init_hw(struct macb *bp)
1875{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001876 struct macb_queue *queue;
1877 unsigned int q;
1878
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001879 u32 config;
1880
1881 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001882 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001883
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001884 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301885 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1886 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001887 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001888 config |= MACB_BIT(PAE); /* PAuse Enable */
1889 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001890 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301891 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1892 else
1893 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001894 if (bp->dev->flags & IFF_PROMISC)
1895 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001896 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1897 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001898 if (!(bp->dev->flags & IFF_BROADCAST))
1899 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001900 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001901 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001902 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301903 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001904 bp->speed = SPEED_10;
1905 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301906 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001907 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301908 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001909
Jamie Iles0116da42011-03-14 17:38:30 +00001910 macb_configure_dma(bp);
1911
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001912 /* Initialize TX and RX buffers */
Harini Katakamfff80192016-08-09 13:15:53 +05301913 macb_writel(bp, RBQP, (u32)(bp->rx_ring_dma));
1914#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1915 macb_writel(bp, RBQPH, (u32)(bp->rx_ring_dma >> 32));
1916#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001917 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakamfff80192016-08-09 13:15:53 +05301918 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
1919#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1920 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
1921#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001922
1923 /* Enable interrupts */
1924 queue_writel(queue, IER,
1925 MACB_RX_INT_FLAGS |
1926 MACB_TX_INT_FLAGS |
1927 MACB_BIT(HRESP));
1928 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001929
1930 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001931 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001932}
1933
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001934/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001935 * locations in the memory map. The least significant bits are stored
1936 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1937 *
1938 * The unicast hash enable and the multicast hash enable bits in the
1939 * network configuration register enable the reception of hash matched
1940 * frames. The destination address is reduced to a 6 bit index into
1941 * the 64 bit hash register using the following hash function. The
1942 * hash function is an exclusive or of every sixth bit of the
1943 * destination address.
1944 *
1945 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1946 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1947 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1948 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1949 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1950 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1951 *
1952 * da[0] represents the least significant bit of the first byte
1953 * received, that is, the multicast/unicast indicator, and da[47]
1954 * represents the most significant bit of the last byte received. If
1955 * the hash index, hi[n], points to a bit that is set in the hash
1956 * register then the frame will be matched according to whether the
1957 * frame is multicast or unicast. A multicast match will be signalled
1958 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1959 * index points to a bit set in the hash register. A unicast match
1960 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1961 * and the hash index points to a bit set in the hash register. To
1962 * receive all multicast frames, the hash register should be set with
1963 * all ones and the multicast hash enable bit should be set in the
1964 * network configuration register.
1965 */
1966
1967static inline int hash_bit_value(int bitnr, __u8 *addr)
1968{
1969 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1970 return 1;
1971 return 0;
1972}
1973
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001974/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001975static int hash_get_index(__u8 *addr)
1976{
1977 int i, j, bitval;
1978 int hash_index = 0;
1979
1980 for (j = 0; j < 6; j++) {
1981 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001982 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001983
1984 hash_index |= (bitval << j);
1985 }
1986
1987 return hash_index;
1988}
1989
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001990/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001991static void macb_sethashtable(struct net_device *dev)
1992{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001993 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001994 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001995 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001996 struct macb *bp = netdev_priv(dev);
1997
Moritz Fischeraa50b552016-03-29 19:11:13 -07001998 mc_filter[0] = 0;
1999 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002000
Jiri Pirko22bedad32010-04-01 21:22:57 +00002001 netdev_for_each_mc_addr(ha, dev) {
2002 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002003 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2004 }
2005
Jamie Ilesf75ba502011-11-08 10:12:32 +00002006 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2007 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002008}
2009
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002010/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002011static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002012{
2013 unsigned long cfg;
2014 struct macb *bp = netdev_priv(dev);
2015
2016 cfg = macb_readl(bp, NCFGR);
2017
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002018 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002019 /* Enable promiscuous mode */
2020 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002021
2022 /* Disable RX checksum offload */
2023 if (macb_is_gem(bp))
2024 cfg &= ~GEM_BIT(RXCOEN);
2025 } else {
2026 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002027 cfg &= ~MACB_BIT(CAF);
2028
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002029 /* Enable RX checksum offload only if requested */
2030 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2031 cfg |= GEM_BIT(RXCOEN);
2032 }
2033
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002034 if (dev->flags & IFF_ALLMULTI) {
2035 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002036 macb_or_gem_writel(bp, HRB, -1);
2037 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002038 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00002039 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002040 /* Enable specific multicasts */
2041 macb_sethashtable(dev);
2042 cfg |= MACB_BIT(NCFGR_MTI);
2043 } else if (dev->flags & (~IFF_ALLMULTI)) {
2044 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00002045 macb_or_gem_writel(bp, HRB, 0);
2046 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02002047 cfg &= ~MACB_BIT(NCFGR_MTI);
2048 }
2049
2050 macb_writel(bp, NCFGR, cfg);
2051}
2052
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002053static int macb_open(struct net_device *dev)
2054{
2055 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00002056 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002057 int err;
2058
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002059 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002060
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002061 /* carrier starts down */
2062 netif_carrier_off(dev);
2063
frederic RODO6c36a702007-07-12 19:07:24 +02002064 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02002065 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02002066 return -EAGAIN;
2067
Nicolas Ferre1b447912013-06-04 21:57:11 +00002068 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00002069 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00002070
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002071 err = macb_alloc_consistent(bp);
2072 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002073 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2074 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002075 return err;
2076 }
2077
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002078 napi_enable(&bp->napi);
2079
Nicolas Ferre4df95132013-06-04 21:57:12 +00002080 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002081 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002082
frederic RODO6c36a702007-07-12 19:07:24 +02002083 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002084 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002085
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002086 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002087
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002088 return 0;
2089}
2090
2091static int macb_close(struct net_device *dev)
2092{
2093 struct macb *bp = netdev_priv(dev);
2094 unsigned long flags;
2095
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002096 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002097 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002098
Philippe Reynes0a912812016-06-22 00:32:35 +02002099 if (dev->phydev)
2100 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02002101
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002102 spin_lock_irqsave(&bp->lock, flags);
2103 macb_reset_hw(bp);
2104 netif_carrier_off(dev);
2105 spin_unlock_irqrestore(&bp->lock, flags);
2106
2107 macb_free_consistent(bp);
2108
2109 return 0;
2110}
2111
Harini Katakama5898ea2015-05-06 22:27:18 +05302112static int macb_change_mtu(struct net_device *dev, int new_mtu)
2113{
Harini Katakama5898ea2015-05-06 22:27:18 +05302114 if (netif_running(dev))
2115 return -EBUSY;
2116
Harini Katakama5898ea2015-05-06 22:27:18 +05302117 dev->mtu = new_mtu;
2118
2119 return 0;
2120}
2121
Jamie Ilesa494ed82011-03-09 16:26:35 +00002122static void gem_update_stats(struct macb *bp)
2123{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002124 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002125 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002126
Xander Huff3ff13f12015-01-13 16:15:51 -06002127 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2128 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002129 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002130
2131 bp->ethtool_stats[i] += val;
2132 *p += val;
2133
2134 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2135 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002136 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002137 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002138 *(++p) += val;
2139 }
2140 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002141}
2142
2143static struct net_device_stats *gem_get_stats(struct macb *bp)
2144{
2145 struct gem_stats *hwstat = &bp->hw_stats.gem;
2146 struct net_device_stats *nstat = &bp->stats;
2147
2148 gem_update_stats(bp);
2149
2150 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2151 hwstat->rx_alignment_errors +
2152 hwstat->rx_resource_errors +
2153 hwstat->rx_overruns +
2154 hwstat->rx_oversize_frames +
2155 hwstat->rx_jabbers +
2156 hwstat->rx_undersized_frames +
2157 hwstat->rx_length_field_frame_errors);
2158 nstat->tx_errors = (hwstat->tx_late_collisions +
2159 hwstat->tx_excessive_collisions +
2160 hwstat->tx_underrun +
2161 hwstat->tx_carrier_sense_errors);
2162 nstat->multicast = hwstat->rx_multicast_frames;
2163 nstat->collisions = (hwstat->tx_single_collision_frames +
2164 hwstat->tx_multiple_collision_frames +
2165 hwstat->tx_excessive_collisions);
2166 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2167 hwstat->rx_jabbers +
2168 hwstat->rx_undersized_frames +
2169 hwstat->rx_length_field_frame_errors);
2170 nstat->rx_over_errors = hwstat->rx_resource_errors;
2171 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2172 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2173 nstat->rx_fifo_errors = hwstat->rx_overruns;
2174 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2175 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2176 nstat->tx_fifo_errors = hwstat->tx_underrun;
2177
2178 return nstat;
2179}
2180
Xander Huff3ff13f12015-01-13 16:15:51 -06002181static void gem_get_ethtool_stats(struct net_device *dev,
2182 struct ethtool_stats *stats, u64 *data)
2183{
2184 struct macb *bp;
2185
2186 bp = netdev_priv(dev);
2187 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002188 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002189}
2190
2191static int gem_get_sset_count(struct net_device *dev, int sset)
2192{
2193 switch (sset) {
2194 case ETH_SS_STATS:
2195 return GEM_STATS_LEN;
2196 default:
2197 return -EOPNOTSUPP;
2198 }
2199}
2200
2201static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2202{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002203 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002204
2205 switch (sset) {
2206 case ETH_SS_STATS:
2207 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2208 memcpy(p, gem_statistics[i].stat_string,
2209 ETH_GSTRING_LEN);
2210 break;
2211 }
2212}
2213
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002214static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002215{
2216 struct macb *bp = netdev_priv(dev);
2217 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002218 struct macb_stats *hwstat = &bp->hw_stats.macb;
2219
2220 if (macb_is_gem(bp))
2221 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002222
frederic RODO6c36a702007-07-12 19:07:24 +02002223 /* read stats from hardware */
2224 macb_update_stats(bp);
2225
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002226 /* Convert HW stats into netdevice stats */
2227 nstat->rx_errors = (hwstat->rx_fcs_errors +
2228 hwstat->rx_align_errors +
2229 hwstat->rx_resource_errors +
2230 hwstat->rx_overruns +
2231 hwstat->rx_oversize_pkts +
2232 hwstat->rx_jabbers +
2233 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002234 hwstat->rx_length_mismatch);
2235 nstat->tx_errors = (hwstat->tx_late_cols +
2236 hwstat->tx_excessive_cols +
2237 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002238 hwstat->tx_carrier_errors +
2239 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002240 nstat->collisions = (hwstat->tx_single_cols +
2241 hwstat->tx_multiple_cols +
2242 hwstat->tx_excessive_cols);
2243 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2244 hwstat->rx_jabbers +
2245 hwstat->rx_undersize_pkts +
2246 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002247 nstat->rx_over_errors = hwstat->rx_resource_errors +
2248 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002249 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2250 nstat->rx_frame_errors = hwstat->rx_align_errors;
2251 nstat->rx_fifo_errors = hwstat->rx_overruns;
2252 /* XXX: What does "missed" mean? */
2253 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2254 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2255 nstat->tx_fifo_errors = hwstat->tx_underruns;
2256 /* Don't know about heartbeat or window errors... */
2257
2258 return nstat;
2259}
2260
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002261static int macb_get_regs_len(struct net_device *netdev)
2262{
2263 return MACB_GREGS_NBR * sizeof(u32);
2264}
2265
2266static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2267 void *p)
2268{
2269 struct macb *bp = netdev_priv(dev);
2270 unsigned int tail, head;
2271 u32 *regs_buff = p;
2272
2273 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2274 | MACB_GREGS_VERSION;
2275
Zach Brownb410d132016-10-19 09:56:57 -05002276 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2277 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002278
2279 regs_buff[0] = macb_readl(bp, NCR);
2280 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2281 regs_buff[2] = macb_readl(bp, NSR);
2282 regs_buff[3] = macb_readl(bp, TSR);
2283 regs_buff[4] = macb_readl(bp, RBQP);
2284 regs_buff[5] = macb_readl(bp, TBQP);
2285 regs_buff[6] = macb_readl(bp, RSR);
2286 regs_buff[7] = macb_readl(bp, IMR);
2287
2288 regs_buff[8] = tail;
2289 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002290 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2291 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002292
Neil Armstrongce721a72016-01-05 14:39:16 +01002293 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2294 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002295 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002296 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002297}
2298
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002299static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2300{
2301 struct macb *bp = netdev_priv(netdev);
2302
2303 wol->supported = 0;
2304 wol->wolopts = 0;
2305
2306 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2307 wol->supported = WAKE_MAGIC;
2308
2309 if (bp->wol & MACB_WOL_ENABLED)
2310 wol->wolopts |= WAKE_MAGIC;
2311 }
2312}
2313
2314static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2315{
2316 struct macb *bp = netdev_priv(netdev);
2317
2318 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2319 (wol->wolopts & ~WAKE_MAGIC))
2320 return -EOPNOTSUPP;
2321
2322 if (wol->wolopts & WAKE_MAGIC)
2323 bp->wol |= MACB_WOL_ENABLED;
2324 else
2325 bp->wol &= ~MACB_WOL_ENABLED;
2326
2327 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2328
2329 return 0;
2330}
2331
Zach Brown8441bb32016-10-19 09:56:58 -05002332static void macb_get_ringparam(struct net_device *netdev,
2333 struct ethtool_ringparam *ring)
2334{
2335 struct macb *bp = netdev_priv(netdev);
2336
2337 ring->rx_max_pending = MAX_RX_RING_SIZE;
2338 ring->tx_max_pending = MAX_TX_RING_SIZE;
2339
2340 ring->rx_pending = bp->rx_ring_size;
2341 ring->tx_pending = bp->tx_ring_size;
2342}
2343
2344static int macb_set_ringparam(struct net_device *netdev,
2345 struct ethtool_ringparam *ring)
2346{
2347 struct macb *bp = netdev_priv(netdev);
2348 u32 new_rx_size, new_tx_size;
2349 unsigned int reset = 0;
2350
2351 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2352 return -EINVAL;
2353
2354 new_rx_size = clamp_t(u32, ring->rx_pending,
2355 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2356 new_rx_size = roundup_pow_of_two(new_rx_size);
2357
2358 new_tx_size = clamp_t(u32, ring->tx_pending,
2359 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2360 new_tx_size = roundup_pow_of_two(new_tx_size);
2361
2362 if ((new_tx_size == bp->tx_ring_size) &&
2363 (new_rx_size == bp->rx_ring_size)) {
2364 /* nothing to do */
2365 return 0;
2366 }
2367
2368 if (netif_running(bp->dev)) {
2369 reset = 1;
2370 macb_close(bp->dev);
2371 }
2372
2373 bp->rx_ring_size = new_rx_size;
2374 bp->tx_ring_size = new_tx_size;
2375
2376 if (reset)
2377 macb_open(bp->dev);
2378
2379 return 0;
2380}
2381
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002382static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002383 .get_regs_len = macb_get_regs_len,
2384 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002385 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002386 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002387 .get_wol = macb_get_wol,
2388 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002389 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2390 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002391 .get_ringparam = macb_get_ringparam,
2392 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002393};
Xander Huff8cd5a562015-01-15 15:55:20 -06002394
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002395static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002396 .get_regs_len = macb_get_regs_len,
2397 .get_regs = macb_get_regs,
2398 .get_link = ethtool_op_get_link,
2399 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002400 .get_ethtool_stats = gem_get_ethtool_stats,
2401 .get_strings = gem_get_ethtool_strings,
2402 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002403 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2404 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002405 .get_ringparam = macb_get_ringparam,
2406 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002407};
2408
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002409static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002410{
Philippe Reynes0a912812016-06-22 00:32:35 +02002411 struct phy_device *phydev = dev->phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002412
2413 if (!netif_running(dev))
2414 return -EINVAL;
2415
frederic RODO6c36a702007-07-12 19:07:24 +02002416 if (!phydev)
2417 return -ENODEV;
2418
Richard Cochran28b04112010-07-17 08:48:55 +00002419 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002420}
2421
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002422static int macb_set_features(struct net_device *netdev,
2423 netdev_features_t features)
2424{
2425 struct macb *bp = netdev_priv(netdev);
2426 netdev_features_t changed = features ^ netdev->features;
2427
2428 /* TX checksum offload */
2429 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2430 u32 dmacfg;
2431
2432 dmacfg = gem_readl(bp, DMACFG);
2433 if (features & NETIF_F_HW_CSUM)
2434 dmacfg |= GEM_BIT(TXCOEN);
2435 else
2436 dmacfg &= ~GEM_BIT(TXCOEN);
2437 gem_writel(bp, DMACFG, dmacfg);
2438 }
2439
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002440 /* RX checksum offload */
2441 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2442 u32 netcfg;
2443
2444 netcfg = gem_readl(bp, NCFGR);
2445 if (features & NETIF_F_RXCSUM &&
2446 !(netdev->flags & IFF_PROMISC))
2447 netcfg |= GEM_BIT(RXCOEN);
2448 else
2449 netcfg &= ~GEM_BIT(RXCOEN);
2450 gem_writel(bp, NCFGR, netcfg);
2451 }
2452
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002453 return 0;
2454}
2455
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002456static const struct net_device_ops macb_netdev_ops = {
2457 .ndo_open = macb_open,
2458 .ndo_stop = macb_close,
2459 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002460 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002461 .ndo_get_stats = macb_get_stats,
2462 .ndo_do_ioctl = macb_ioctl,
2463 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302464 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002465 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002466#ifdef CONFIG_NET_POLL_CONTROLLER
2467 .ndo_poll_controller = macb_poll_controller,
2468#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002469 .ndo_set_features = macb_set_features,
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002470 .ndo_features_check = macb_features_check,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002471};
2472
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002473/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002474 * and integration options used
2475 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002476static void macb_configure_caps(struct macb *bp,
2477 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002478{
2479 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002480
Nicolas Ferref6970502015-03-31 15:02:01 +02002481 if (dt_conf)
2482 bp->caps = dt_conf->caps;
2483
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002484 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002485 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2486
Nicolas Ferree1755872014-07-24 13:50:58 +02002487 dcfg = gem_readl(bp, DCFG1);
2488 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2489 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2490 dcfg = gem_readl(bp, DCFG2);
2491 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2492 bp->caps |= MACB_CAPS_FIFO_MODE;
2493 }
2494
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002495 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002496}
2497
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002498static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002499 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002500 unsigned int *queue_mask,
2501 unsigned int *num_queues)
2502{
2503 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002504
2505 *queue_mask = 0x1;
2506 *num_queues = 1;
2507
Nicolas Ferreda120112015-03-31 15:02:00 +02002508 /* is it macb or gem ?
2509 *
2510 * We need to read directly from the hardware here because
2511 * we are early in the probe process and don't have the
2512 * MACB_CAPS_MACB_IS_GEM flag positioned
2513 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002514 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002515 return;
2516
2517 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302518 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2519
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002520 *queue_mask |= 0x1;
2521
2522 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2523 if (*queue_mask & (1 << hw_q))
2524 (*num_queues)++;
2525}
2526
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002527static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302528 struct clk **hclk, struct clk **tx_clk,
2529 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002530{
Bartosz Folta83a77e92016-12-14 06:39:15 +00002531 struct macb_platform_data *pdata;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002532 int err;
2533
Bartosz Folta83a77e92016-12-14 06:39:15 +00002534 pdata = dev_get_platdata(&pdev->dev);
2535 if (pdata) {
2536 *pclk = pdata->pclk;
2537 *hclk = pdata->hclk;
2538 } else {
2539 *pclk = devm_clk_get(&pdev->dev, "pclk");
2540 *hclk = devm_clk_get(&pdev->dev, "hclk");
2541 }
2542
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002543 if (IS_ERR(*pclk)) {
2544 err = PTR_ERR(*pclk);
2545 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2546 return err;
2547 }
2548
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002549 if (IS_ERR(*hclk)) {
2550 err = PTR_ERR(*hclk);
2551 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2552 return err;
2553 }
2554
2555 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2556 if (IS_ERR(*tx_clk))
2557 *tx_clk = NULL;
2558
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302559 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2560 if (IS_ERR(*rx_clk))
2561 *rx_clk = NULL;
2562
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002563 err = clk_prepare_enable(*pclk);
2564 if (err) {
2565 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2566 return err;
2567 }
2568
2569 err = clk_prepare_enable(*hclk);
2570 if (err) {
2571 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2572 goto err_disable_pclk;
2573 }
2574
2575 err = clk_prepare_enable(*tx_clk);
2576 if (err) {
2577 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2578 goto err_disable_hclk;
2579 }
2580
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302581 err = clk_prepare_enable(*rx_clk);
2582 if (err) {
2583 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2584 goto err_disable_txclk;
2585 }
2586
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002587 return 0;
2588
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302589err_disable_txclk:
2590 clk_disable_unprepare(*tx_clk);
2591
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002592err_disable_hclk:
2593 clk_disable_unprepare(*hclk);
2594
2595err_disable_pclk:
2596 clk_disable_unprepare(*pclk);
2597
2598 return err;
2599}
2600
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002601static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002602{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002603 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002604 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002605 struct macb *bp = netdev_priv(dev);
2606 struct macb_queue *queue;
2607 int err;
2608 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002609
Zach Brownb410d132016-10-19 09:56:57 -05002610 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2611 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2612
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002613 /* set the queue register mapping once for all: queue0 has a special
2614 * register mapping but we don't want to test the queue index then
2615 * compute the corresponding register offset at run time.
2616 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002617 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002618 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002619 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002620
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002621 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002622 queue->bp = bp;
2623 if (hw_q) {
2624 queue->ISR = GEM_ISR(hw_q - 1);
2625 queue->IER = GEM_IER(hw_q - 1);
2626 queue->IDR = GEM_IDR(hw_q - 1);
2627 queue->IMR = GEM_IMR(hw_q - 1);
2628 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302629#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2630 queue->TBQPH = GEM_TBQPH(hw_q -1);
2631#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002632 } else {
2633 /* queue0 uses legacy registers */
2634 queue->ISR = MACB_ISR;
2635 queue->IER = MACB_IER;
2636 queue->IDR = MACB_IDR;
2637 queue->IMR = MACB_IMR;
2638 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302639#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2640 queue->TBQPH = MACB_TBQPH;
2641#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002642 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002643
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002644 /* get irq: here we use the linux queue index, not the hardware
2645 * queue index. the queue irq definitions in the device tree
2646 * must remove the optional gaps that could exist in the
2647 * hardware queue mask.
2648 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002649 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002650 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002651 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002652 if (err) {
2653 dev_err(&pdev->dev,
2654 "Unable to request IRQ %d (error %d)\n",
2655 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002656 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002657 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002658
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002659 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002660 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002661 }
2662
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002663 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002664 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002665
Nicolas Ferre4df95132013-06-04 21:57:12 +00002666 /* setup appropriated routines according to adapter type */
2667 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002668 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002669 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2670 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2671 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2672 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002673 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002674 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002675 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002676 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2677 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2678 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2679 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002680 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002681 }
2682
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002683 /* Set features */
2684 dev->hw_features = NETIF_F_SG;
Rafal Ozieblo1629dd42016-11-16 10:02:34 +00002685
2686 /* Check LSO capability */
2687 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
2688 dev->hw_features |= MACB_NETIF_LSO;
2689
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002690 /* Checksum offload is only available on gem with packet buffer */
2691 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002692 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002693 if (bp->caps & MACB_CAPS_SG_DISABLED)
2694 dev->hw_features &= ~NETIF_F_SG;
2695 dev->features = dev->hw_features;
2696
Neil Armstrongce721a72016-01-05 14:39:16 +01002697 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2698 val = 0;
2699 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2700 val = GEM_BIT(RGMII);
2701 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002702 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002703 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002704 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002705 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002706
Neil Armstrongce721a72016-01-05 14:39:16 +01002707 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2708 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002709
Neil Armstrongce721a72016-01-05 14:39:16 +01002710 macb_or_gem_writel(bp, USRIO, val);
2711 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002712
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002713 /* Set MII management clock divider */
2714 val = macb_mdc_clk_div(bp);
2715 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302716 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2717 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002718 macb_writel(bp, NCFGR, val);
2719
2720 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002721}
2722
2723#if defined(CONFIG_OF)
2724/* 1518 rounded up */
2725#define AT91ETHER_MAX_RBUFF_SZ 0x600
2726/* max number of receive buffers */
2727#define AT91ETHER_MAX_RX_DESCR 9
2728
2729/* Initialize and start the Receiver and Transmit subsystems */
2730static int at91ether_start(struct net_device *dev)
2731{
2732 struct macb *lp = netdev_priv(dev);
2733 dma_addr_t addr;
2734 u32 ctl;
2735 int i;
2736
2737 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2738 (AT91ETHER_MAX_RX_DESCR *
2739 sizeof(struct macb_dma_desc)),
2740 &lp->rx_ring_dma, GFP_KERNEL);
2741 if (!lp->rx_ring)
2742 return -ENOMEM;
2743
2744 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2745 AT91ETHER_MAX_RX_DESCR *
2746 AT91ETHER_MAX_RBUFF_SZ,
2747 &lp->rx_buffers_dma, GFP_KERNEL);
2748 if (!lp->rx_buffers) {
2749 dma_free_coherent(&lp->pdev->dev,
2750 AT91ETHER_MAX_RX_DESCR *
2751 sizeof(struct macb_dma_desc),
2752 lp->rx_ring, lp->rx_ring_dma);
2753 lp->rx_ring = NULL;
2754 return -ENOMEM;
2755 }
2756
2757 addr = lp->rx_buffers_dma;
2758 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2759 lp->rx_ring[i].addr = addr;
2760 lp->rx_ring[i].ctrl = 0;
2761 addr += AT91ETHER_MAX_RBUFF_SZ;
2762 }
2763
2764 /* Set the Wrap bit on the last descriptor */
2765 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2766
2767 /* Reset buffer index */
2768 lp->rx_tail = 0;
2769
2770 /* Program address of descriptor list in Rx Buffer Queue register */
2771 macb_writel(lp, RBQP, lp->rx_ring_dma);
2772
2773 /* Enable Receive and Transmit */
2774 ctl = macb_readl(lp, NCR);
2775 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2776
2777 return 0;
2778}
2779
2780/* Open the ethernet interface */
2781static int at91ether_open(struct net_device *dev)
2782{
2783 struct macb *lp = netdev_priv(dev);
2784 u32 ctl;
2785 int ret;
2786
2787 /* Clear internal statistics */
2788 ctl = macb_readl(lp, NCR);
2789 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2790
2791 macb_set_hwaddr(lp);
2792
2793 ret = at91ether_start(dev);
2794 if (ret)
2795 return ret;
2796
2797 /* Enable MAC interrupts */
2798 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2799 MACB_BIT(RXUBR) |
2800 MACB_BIT(ISR_TUND) |
2801 MACB_BIT(ISR_RLE) |
2802 MACB_BIT(TCOMP) |
2803 MACB_BIT(ISR_ROVR) |
2804 MACB_BIT(HRESP));
2805
2806 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002807 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002808
2809 netif_start_queue(dev);
2810
2811 return 0;
2812}
2813
2814/* Close the interface */
2815static int at91ether_close(struct net_device *dev)
2816{
2817 struct macb *lp = netdev_priv(dev);
2818 u32 ctl;
2819
2820 /* Disable Receiver and Transmitter */
2821 ctl = macb_readl(lp, NCR);
2822 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2823
2824 /* Disable MAC interrupts */
2825 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2826 MACB_BIT(RXUBR) |
2827 MACB_BIT(ISR_TUND) |
2828 MACB_BIT(ISR_RLE) |
2829 MACB_BIT(TCOMP) |
2830 MACB_BIT(ISR_ROVR) |
2831 MACB_BIT(HRESP));
2832
2833 netif_stop_queue(dev);
2834
2835 dma_free_coherent(&lp->pdev->dev,
2836 AT91ETHER_MAX_RX_DESCR *
2837 sizeof(struct macb_dma_desc),
2838 lp->rx_ring, lp->rx_ring_dma);
2839 lp->rx_ring = NULL;
2840
2841 dma_free_coherent(&lp->pdev->dev,
2842 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2843 lp->rx_buffers, lp->rx_buffers_dma);
2844 lp->rx_buffers = NULL;
2845
2846 return 0;
2847}
2848
2849/* Transmit packet */
2850static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2851{
2852 struct macb *lp = netdev_priv(dev);
2853
2854 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2855 netif_stop_queue(dev);
2856
2857 /* Store packet information (to free when Tx completed) */
2858 lp->skb = skb;
2859 lp->skb_length = skb->len;
2860 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2861 DMA_TO_DEVICE);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03002862 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
2863 dev_kfree_skb_any(skb);
2864 dev->stats.tx_dropped++;
2865 netdev_err(dev, "%s: DMA mapping error\n", __func__);
2866 return NETDEV_TX_OK;
2867 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002868
2869 /* Set address of the data in the Transmit Address register */
2870 macb_writel(lp, TAR, lp->skb_physaddr);
2871 /* Set length of the packet in the Transmit Control register */
2872 macb_writel(lp, TCR, skb->len);
2873
2874 } else {
2875 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2876 return NETDEV_TX_BUSY;
2877 }
2878
2879 return NETDEV_TX_OK;
2880}
2881
2882/* Extract received frame from buffer descriptors and sent to upper layers.
2883 * (Called from interrupt context)
2884 */
2885static void at91ether_rx(struct net_device *dev)
2886{
2887 struct macb *lp = netdev_priv(dev);
2888 unsigned char *p_recv;
2889 struct sk_buff *skb;
2890 unsigned int pktlen;
2891
2892 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2893 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2894 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2895 skb = netdev_alloc_skb(dev, pktlen + 2);
2896 if (skb) {
2897 skb_reserve(skb, 2);
2898 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2899
2900 skb->protocol = eth_type_trans(skb, dev);
2901 lp->stats.rx_packets++;
2902 lp->stats.rx_bytes += pktlen;
2903 netif_rx(skb);
2904 } else {
2905 lp->stats.rx_dropped++;
2906 }
2907
2908 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2909 lp->stats.multicast++;
2910
2911 /* reset ownership bit */
2912 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2913
2914 /* wrap after last buffer */
2915 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2916 lp->rx_tail = 0;
2917 else
2918 lp->rx_tail++;
2919 }
2920}
2921
2922/* MAC interrupt handler */
2923static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2924{
2925 struct net_device *dev = dev_id;
2926 struct macb *lp = netdev_priv(dev);
2927 u32 intstatus, ctl;
2928
2929 /* MAC Interrupt Status register indicates what interrupts are pending.
2930 * It is automatically cleared once read.
2931 */
2932 intstatus = macb_readl(lp, ISR);
2933
2934 /* Receive complete */
2935 if (intstatus & MACB_BIT(RCOMP))
2936 at91ether_rx(dev);
2937
2938 /* Transmit complete */
2939 if (intstatus & MACB_BIT(TCOMP)) {
2940 /* The TCOM bit is set even if the transmission failed */
2941 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2942 lp->stats.tx_errors++;
2943
2944 if (lp->skb) {
2945 dev_kfree_skb_irq(lp->skb);
2946 lp->skb = NULL;
2947 dma_unmap_single(NULL, lp->skb_physaddr,
2948 lp->skb_length, DMA_TO_DEVICE);
2949 lp->stats.tx_packets++;
2950 lp->stats.tx_bytes += lp->skb_length;
2951 }
2952 netif_wake_queue(dev);
2953 }
2954
2955 /* Work-around for EMAC Errata section 41.3.1 */
2956 if (intstatus & MACB_BIT(RXUBR)) {
2957 ctl = macb_readl(lp, NCR);
2958 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08002959 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002960 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2961 }
2962
2963 if (intstatus & MACB_BIT(ISR_ROVR))
2964 netdev_err(dev, "ROVR error\n");
2965
2966 return IRQ_HANDLED;
2967}
2968
2969#ifdef CONFIG_NET_POLL_CONTROLLER
2970static void at91ether_poll_controller(struct net_device *dev)
2971{
2972 unsigned long flags;
2973
2974 local_irq_save(flags);
2975 at91ether_interrupt(dev->irq, dev);
2976 local_irq_restore(flags);
2977}
2978#endif
2979
2980static const struct net_device_ops at91ether_netdev_ops = {
2981 .ndo_open = at91ether_open,
2982 .ndo_stop = at91ether_close,
2983 .ndo_start_xmit = at91ether_start_xmit,
2984 .ndo_get_stats = macb_get_stats,
2985 .ndo_set_rx_mode = macb_set_rx_mode,
2986 .ndo_set_mac_address = eth_mac_addr,
2987 .ndo_do_ioctl = macb_ioctl,
2988 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002989#ifdef CONFIG_NET_POLL_CONTROLLER
2990 .ndo_poll_controller = at91ether_poll_controller,
2991#endif
2992};
2993
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002994static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302995 struct clk **hclk, struct clk **tx_clk,
2996 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002997{
2998 int err;
2999
3000 *hclk = NULL;
3001 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303002 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003003
3004 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3005 if (IS_ERR(*pclk))
3006 return PTR_ERR(*pclk);
3007
3008 err = clk_prepare_enable(*pclk);
3009 if (err) {
3010 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3011 return err;
3012 }
3013
3014 return 0;
3015}
3016
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003017static int at91ether_init(struct platform_device *pdev)
3018{
3019 struct net_device *dev = platform_get_drvdata(pdev);
3020 struct macb *bp = netdev_priv(dev);
3021 int err;
3022 u32 reg;
3023
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003024 dev->netdev_ops = &at91ether_netdev_ops;
3025 dev->ethtool_ops = &macb_ethtool_ops;
3026
3027 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3028 0, dev->name, dev);
3029 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003030 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003031
3032 macb_writel(bp, NCR, 0);
3033
3034 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3035 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3036 reg |= MACB_BIT(RM9200_RMII);
3037
3038 macb_writel(bp, NCFGR, reg);
3039
3040 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003041}
3042
David S. Miller3cef5c52015-03-09 23:38:02 -04003043static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003044 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003045 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003046 .init = macb_init,
3047};
3048
David S. Miller3cef5c52015-03-09 23:38:02 -04003049static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003050 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3051 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003052 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003053 .init = macb_init,
3054};
3055
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003056static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003057 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003058 .dma_burst_length = 16,
3059 .clk_init = macb_clk_init,
3060 .init = macb_init,
3061};
3062
David S. Miller3cef5c52015-03-09 23:38:02 -04003063static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003064 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3065 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003066 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003067 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003068 .init = macb_init,
3069};
3070
David S. Miller3cef5c52015-03-09 23:38:02 -04003071static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01003072 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003073 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003074 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003075 .init = macb_init,
3076};
3077
David S. Miller3cef5c52015-03-09 23:38:02 -04003078static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003079 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003080 .init = at91ether_init,
3081};
3082
Neil Armstronge611b5b2016-01-05 14:39:17 +01003083static const struct macb_config np4_config = {
3084 .caps = MACB_CAPS_USRIO_DISABLED,
3085 .clk_init = macb_clk_init,
3086 .init = macb_init,
3087};
David S. Miller36583eb2015-05-23 01:22:35 -04003088
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303089static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303090 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303091 .dma_burst_length = 16,
3092 .clk_init = macb_clk_init,
3093 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303094 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303095};
3096
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003097static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05303098 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003099 .dma_burst_length = 16,
3100 .clk_init = macb_clk_init,
3101 .init = macb_init,
3102};
3103
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003104static const struct of_device_id macb_dt_ids[] = {
3105 { .compatible = "cdns,at32ap7000-macb" },
3106 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3107 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01003108 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003109 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3110 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02003111 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003112 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3113 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
3114 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
3115 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05303116 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05003117 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003118 { /* sentinel */ }
3119};
3120MODULE_DEVICE_TABLE(of, macb_dt_ids);
3121#endif /* CONFIG_OF */
3122
Bartosz Folta83a77e92016-12-14 06:39:15 +00003123static const struct macb_config default_gem_config = {
3124 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
3125 .dma_burst_length = 16,
3126 .clk_init = macb_clk_init,
3127 .init = macb_init,
3128 .jumbo_max_len = 10240,
3129};
3130
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003131static int macb_probe(struct platform_device *pdev)
3132{
Bartosz Folta83a77e92016-12-14 06:39:15 +00003133 const struct macb_config *macb_config = &default_gem_config;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003134 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303135 struct clk **, struct clk **, struct clk **)
Bartosz Folta83a77e92016-12-14 06:39:15 +00003136 = macb_config->clk_init;
3137 int (*init)(struct platform_device *) = macb_config->init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003138 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003139 struct device_node *phy_node;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303140 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003141 unsigned int queue_mask, num_queues;
3142 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003143 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003144 struct phy_device *phydev;
3145 struct net_device *dev;
3146 struct resource *regs;
3147 void __iomem *mem;
3148 const char *mac;
3149 struct macb *bp;
3150 int err;
3151
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003152 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3153 mem = devm_ioremap_resource(&pdev->dev, regs);
3154 if (IS_ERR(mem))
3155 return PTR_ERR(mem);
3156
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003157 if (np) {
3158 const struct of_device_id *match;
3159
3160 match = of_match_node(macb_dt_ids, np);
3161 if (match && match->data) {
3162 macb_config = match->data;
3163 clk_init = macb_config->clk_init;
3164 init = macb_config->init;
3165 }
3166 }
3167
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303168 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003169 if (err)
3170 return err;
3171
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003172 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003173
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003174 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003175 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003176 if (!dev) {
3177 err = -ENOMEM;
3178 goto err_disable_clocks;
3179 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003180
3181 dev->base_addr = regs->start;
3182
3183 SET_NETDEV_DEV(dev, &pdev->dev);
3184
3185 bp = netdev_priv(dev);
3186 bp->pdev = pdev;
3187 bp->dev = dev;
3188 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003189 bp->native_io = native_io;
3190 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003191 bp->macb_reg_readl = hw_readl_native;
3192 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003193 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003194 bp->macb_reg_readl = hw_readl;
3195 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003196 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003197 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003198 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003199 if (macb_config)
3200 bp->dma_burst_length = macb_config->dma_burst_length;
3201 bp->pclk = pclk;
3202 bp->hclk = hclk;
3203 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303204 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003205 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303206 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303207
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003208 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003209 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003210 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3211 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3212
Harini Katakamfff80192016-08-09 13:15:53 +05303213#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3214 if (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1)) > GEM_DBW32)
3215 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3216#endif
3217
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003218 spin_lock_init(&bp->lock);
3219
Nicolas Ferread783472015-03-31 15:02:02 +02003220 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003221 macb_configure_caps(bp, macb_config);
3222
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003223 platform_set_drvdata(pdev, dev);
3224
3225 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003226 if (dev->irq < 0) {
3227 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003228 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003229 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003230
Jarod Wilson44770e12016-10-17 15:54:17 -04003231 /* MTU range: 68 - 1500 or 10240 */
3232 dev->min_mtu = GEM_MTU_MIN_SIZE;
3233 if (bp->caps & MACB_CAPS_JUMBO)
3234 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3235 else
3236 dev->max_mtu = ETH_DATA_LEN;
3237
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003238 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003239 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003240 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003241 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003242 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003243
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003244 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003245 phy_node = of_get_next_available_child(np, NULL);
3246 if (phy_node) {
3247 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003248
Charles Keepax0e3e7992016-03-28 13:47:42 +01003249 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003250 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003251 gpiod_direction_output(bp->reset_gpio, 1);
3252 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003253 }
3254 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003255
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003256 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003257 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003258 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003259 if (pdata && pdata->is_rmii)
3260 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3261 else
3262 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3263 } else {
3264 bp->phy_interface = err;
3265 }
3266
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003267 /* IP specific init */
3268 err = init(pdev);
3269 if (err)
3270 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003271
Florian Fainellicf669662016-05-02 18:38:45 -07003272 err = macb_mii_init(bp);
3273 if (err)
3274 goto err_out_free_netdev;
3275
Philippe Reynes0a912812016-06-22 00:32:35 +02003276 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003277
3278 netif_carrier_off(dev);
3279
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003280 err = register_netdev(dev);
3281 if (err) {
3282 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003283 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003284 }
3285
Florian Fainellicf669662016-05-02 18:38:45 -07003286 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003287
Bo Shen58798232014-09-13 01:57:49 +02003288 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3289 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3290 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003291
3292 return 0;
3293
Florian Fainellicf669662016-05-02 18:38:45 -07003294err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003295 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003296 mdiobus_unregister(bp->mii_bus);
3297 mdiobus_free(bp->mii_bus);
3298
3299 /* Shutdown the PHY if there is a GPIO reset */
3300 if (bp->reset_gpio)
3301 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003302
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003303err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003304 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003305
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003306err_disable_clocks:
3307 clk_disable_unprepare(tx_clk);
3308 clk_disable_unprepare(hclk);
3309 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303310 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003311
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003312 return err;
3313}
3314
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003315static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003316{
3317 struct net_device *dev;
3318 struct macb *bp;
3319
3320 dev = platform_get_drvdata(pdev);
3321
3322 if (dev) {
3323 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003324 if (dev->phydev)
3325 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003326 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003327 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003328 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003329
3330 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003331 if (bp->reset_gpio)
3332 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003333
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003334 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003335 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003336 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003337 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303338 clk_disable_unprepare(bp->rx_clk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003339 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003340 }
3341
3342 return 0;
3343}
3344
Michal Simekd23823d2015-01-23 09:36:03 +01003345static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003346{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003347 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003348 struct net_device *netdev = platform_get_drvdata(pdev);
3349 struct macb *bp = netdev_priv(netdev);
3350
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003351 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003352 netif_device_detach(netdev);
3353
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003354 if (bp->wol & MACB_WOL_ENABLED) {
3355 macb_writel(bp, IER, MACB_BIT(WOL));
3356 macb_writel(bp, WOL, MACB_BIT(MAG));
3357 enable_irq_wake(bp->queues[0].irq);
3358 } else {
3359 clk_disable_unprepare(bp->tx_clk);
3360 clk_disable_unprepare(bp->hclk);
3361 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303362 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003363 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003364
3365 return 0;
3366}
3367
Michal Simekd23823d2015-01-23 09:36:03 +01003368static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003369{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003370 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003371 struct net_device *netdev = platform_get_drvdata(pdev);
3372 struct macb *bp = netdev_priv(netdev);
3373
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003374 if (bp->wol & MACB_WOL_ENABLED) {
3375 macb_writel(bp, IDR, MACB_BIT(WOL));
3376 macb_writel(bp, WOL, 0);
3377 disable_irq_wake(bp->queues[0].irq);
3378 } else {
3379 clk_prepare_enable(bp->pclk);
3380 clk_prepare_enable(bp->hclk);
3381 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303382 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003383 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003384
3385 netif_device_attach(netdev);
3386
3387 return 0;
3388}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003389
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003390static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3391
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003392static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003393 .probe = macb_probe,
3394 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003395 .driver = {
3396 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003397 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003398 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003399 },
3400};
3401
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003402module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003403
3404MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003405MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003406MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003407MODULE_ALIAS("platform:macb");