blob: e1847ce6308deba39998fcdd81e9d3d272a23873 [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>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010035
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010036#include "macb.h"
37
Nicolas Ferre1b447912013-06-04 21:57:11 +000038#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000039#define RX_BUFFER_MULTIPLE 64 /* bytes */
Zach Brown8441bb32016-10-19 09:56:58 -050040
Zach Brownb410d132016-10-19 09:56:57 -050041#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050042#define MIN_RX_RING_SIZE 64
43#define MAX_RX_RING_SIZE 8192
Zach Brownb410d132016-10-19 09:56:57 -050044#define RX_RING_BYTES(bp) (sizeof(struct macb_dma_desc) \
45 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010046
Zach Brownb410d132016-10-19 09:56:57 -050047#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
Zach Brown8441bb32016-10-19 09:56:58 -050048#define MIN_TX_RING_SIZE 64
49#define MAX_TX_RING_SIZE 4096
Zach Brownb410d132016-10-19 09:56:57 -050050#define TX_RING_BYTES(bp) (sizeof(struct macb_dma_desc) \
51 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010052
Nicolas Ferre909a8582012-11-19 06:00:21 +000053/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050054#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010055
56#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
57 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000058#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
59 | MACB_BIT(ISR_RLE) \
60 | MACB_BIT(TXERR))
61#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
62
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020063#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
64#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
65
Jarod Wilson44770e12016-10-17 15:54:17 -040066#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
Harini Katakama5898ea2015-05-06 22:27:18 +053067
Sergio Prado3e2a5e12016-02-09 12:07:16 -020068#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
69#define MACB_WOL_ENABLED (0x1 << 1)
70
Moritz Fischer64ec42f2016-03-29 19:11:12 -070071/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000072 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
73 */
74#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010075
Havard Skinnemoen55054a12012-10-31 06:04:55 +000076/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -050077static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000078{
Zach Brownb410d132016-10-19 09:56:57 -050079 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +000080}
81
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010082static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
83 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000084{
Zach Brownb410d132016-10-19 09:56:57 -050085 return &queue->tx_ring[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000086}
87
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010088static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
89 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000090{
Zach Brownb410d132016-10-19 09:56:57 -050091 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000092}
93
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010094static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000095{
96 dma_addr_t offset;
97
Zach Brownb410d132016-10-19 09:56:57 -050098 offset = macb_tx_ring_wrap(queue->bp, index) *
99 sizeof(struct macb_dma_desc);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000100
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100101 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000102}
103
Zach Brownb410d132016-10-19 09:56:57 -0500104static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000105{
Zach Brownb410d132016-10-19 09:56:57 -0500106 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000107}
108
109static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
110{
Zach Brownb410d132016-10-19 09:56:57 -0500111 return &bp->rx_ring[macb_rx_ring_wrap(bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000112}
113
114static void *macb_rx_buffer(struct macb *bp, unsigned int index)
115{
Zach Brownb410d132016-10-19 09:56:57 -0500116 return bp->rx_buffers + bp->rx_buffer_size *
117 macb_rx_ring_wrap(bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000118}
119
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300120/* I/O accessors */
121static u32 hw_readl_native(struct macb *bp, int offset)
122{
123 return __raw_readl(bp->regs + offset);
124}
125
126static void hw_writel_native(struct macb *bp, int offset, u32 value)
127{
128 __raw_writel(value, bp->regs + offset);
129}
130
131static u32 hw_readl(struct macb *bp, int offset)
132{
133 return readl_relaxed(bp->regs + offset);
134}
135
136static void hw_writel(struct macb *bp, int offset, u32 value)
137{
138 writel_relaxed(value, bp->regs + offset);
139}
140
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700141/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700142 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300143 * descriptor access.
144 */
145static bool hw_is_native_io(void __iomem *addr)
146{
147 u32 value = MACB_BIT(LLB);
148
149 __raw_writel(value, addr + MACB_NCR);
150 value = __raw_readl(addr + MACB_NCR);
151
152 /* Write 0 back to disable everything */
153 __raw_writel(0, addr + MACB_NCR);
154
155 return value == MACB_BIT(LLB);
156}
157
158static bool hw_is_gem(void __iomem *addr, bool native_io)
159{
160 u32 id;
161
162 if (native_io)
163 id = __raw_readl(addr + MACB_MID);
164 else
165 id = readl_relaxed(addr + MACB_MID);
166
167 return MACB_BFEXT(IDNUM, id) >= 0x2;
168}
169
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100170static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100171{
172 u32 bottom;
173 u16 top;
174
175 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000176 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100177 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000178 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000179
180 /* Clear unused address register sets */
181 macb_or_gem_writel(bp, SA2B, 0);
182 macb_or_gem_writel(bp, SA2T, 0);
183 macb_or_gem_writel(bp, SA3B, 0);
184 macb_or_gem_writel(bp, SA3T, 0);
185 macb_or_gem_writel(bp, SA4B, 0);
186 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100187}
188
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100189static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100190{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000191 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100192 u32 bottom;
193 u16 top;
194 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000195 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100196
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900197 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000198
Moritz Fischeraa50b552016-03-29 19:11:13 -0700199 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000200 for (i = 0; i < 4; i++) {
201 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
202 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100203
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000204 if (pdata && pdata->rev_eth_addr) {
205 addr[5] = bottom & 0xff;
206 addr[4] = (bottom >> 8) & 0xff;
207 addr[3] = (bottom >> 16) & 0xff;
208 addr[2] = (bottom >> 24) & 0xff;
209 addr[1] = top & 0xff;
210 addr[0] = (top & 0xff00) >> 8;
211 } else {
212 addr[0] = bottom & 0xff;
213 addr[1] = (bottom >> 8) & 0xff;
214 addr[2] = (bottom >> 16) & 0xff;
215 addr[3] = (bottom >> 24) & 0xff;
216 addr[4] = top & 0xff;
217 addr[5] = (top >> 8) & 0xff;
218 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100219
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000220 if (is_valid_ether_addr(addr)) {
221 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
222 return;
223 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700224 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000225
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300226 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000227 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100228}
229
frederic RODO6c36a702007-07-12 19:07:24 +0200230static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100231{
frederic RODO6c36a702007-07-12 19:07:24 +0200232 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100233 int value;
234
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100235 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
236 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200237 | MACB_BF(PHYA, mii_id)
238 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100239 | MACB_BF(CODE, MACB_MAN_CODE)));
240
frederic RODO6c36a702007-07-12 19:07:24 +0200241 /* wait for end of transfer */
242 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
243 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100244
245 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100246
247 return value;
248}
249
frederic RODO6c36a702007-07-12 19:07:24 +0200250static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
251 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100252{
frederic RODO6c36a702007-07-12 19:07:24 +0200253 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100254
255 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
256 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200257 | MACB_BF(PHYA, mii_id)
258 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100259 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200260 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100261
frederic RODO6c36a702007-07-12 19:07:24 +0200262 /* wait for end of transfer */
263 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
264 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100265
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100266 return 0;
267}
268
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800269/**
270 * macb_set_tx_clk() - Set a clock to a new frequency
271 * @clk Pointer to the clock to change
272 * @rate New frequency in Hz
273 * @dev Pointer to the struct net_device
274 */
275static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
276{
277 long ferr, rate, rate_rounded;
278
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100279 if (!clk)
280 return;
281
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800282 switch (speed) {
283 case SPEED_10:
284 rate = 2500000;
285 break;
286 case SPEED_100:
287 rate = 25000000;
288 break;
289 case SPEED_1000:
290 rate = 125000000;
291 break;
292 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800293 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800294 }
295
296 rate_rounded = clk_round_rate(clk, rate);
297 if (rate_rounded < 0)
298 return;
299
300 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
301 * is not satisfied.
302 */
303 ferr = abs(rate_rounded - rate);
304 ferr = DIV_ROUND_UP(ferr, rate / 100000);
305 if (ferr > 5)
306 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700307 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800308
309 if (clk_set_rate(clk, rate_rounded))
310 netdev_err(dev, "adjusting tx_clk failed.\n");
311}
312
frederic RODO6c36a702007-07-12 19:07:24 +0200313static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100314{
frederic RODO6c36a702007-07-12 19:07:24 +0200315 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200316 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200317 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200318 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100319
frederic RODO6c36a702007-07-12 19:07:24 +0200320 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100321
frederic RODO6c36a702007-07-12 19:07:24 +0200322 if (phydev->link) {
323 if ((bp->speed != phydev->speed) ||
324 (bp->duplex != phydev->duplex)) {
325 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100326
frederic RODO6c36a702007-07-12 19:07:24 +0200327 reg = macb_readl(bp, NCFGR);
328 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000329 if (macb_is_gem(bp))
330 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200331
332 if (phydev->duplex)
333 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900334 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200335 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200336 if (phydev->speed == SPEED_1000 &&
337 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000338 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200339
Patrice Vilchez140b7552012-10-31 06:04:50 +0000340 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200341
342 bp->speed = phydev->speed;
343 bp->duplex = phydev->duplex;
344 status_change = 1;
345 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100346 }
347
frederic RODO6c36a702007-07-12 19:07:24 +0200348 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700349 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200350 bp->speed = 0;
351 bp->duplex = -1;
352 }
353 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100354
frederic RODO6c36a702007-07-12 19:07:24 +0200355 status_change = 1;
356 }
357
358 spin_unlock_irqrestore(&bp->lock, flags);
359
360 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000361 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500362 /* Update the TX clock rate if and only if the link is
363 * up and there has been a link change.
364 */
365 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
366
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000367 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000368 netdev_info(dev, "link up (%d/%s)\n",
369 phydev->speed,
370 phydev->duplex == DUPLEX_FULL ?
371 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000372 } else {
373 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000374 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000375 }
frederic RODO6c36a702007-07-12 19:07:24 +0200376 }
377}
378
379/* based on au1000_eth. c*/
380static int macb_mii_probe(struct net_device *dev)
381{
382 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000383 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000384 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000385 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000386 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200387
Jiri Pirko7455a762010-02-08 05:12:08 +0000388 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200389 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000390 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200391 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200392 }
393
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000394 pdata = dev_get_platdata(&bp->pdev->dev);
395 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700396 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin,
397 "phy int");
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000398 if (!ret) {
399 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
400 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
401 }
402 }
frederic RODO6c36a702007-07-12 19:07:24 +0200403
404 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000405 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100406 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000407 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000408 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000409 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200410 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200413 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000414 phydev->supported &= PHY_GBIT_FEATURES;
415 else
416 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100417
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500418 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
419 phydev->supported &= ~SUPPORTED_1000baseT_Half;
420
frederic RODO6c36a702007-07-12 19:07:24 +0200421 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100422
frederic RODO6c36a702007-07-12 19:07:24 +0200423 bp->link = 0;
424 bp->speed = 0;
425 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200426
427 return 0;
428}
429
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100430static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200431{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000432 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200433 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200434 int err = -ENXIO, i;
435
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200436 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200437 macb_writel(bp, NCR, MACB_BIT(MPE));
438
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700439 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700440 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200441 err = -ENOMEM;
442 goto err_out;
443 }
444
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700445 bp->mii_bus->name = "MACB_mii_bus";
446 bp->mii_bus->read = &macb_mdio_read;
447 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000448 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700449 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700450 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700451 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900452 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700453
Jamie Iles91523942011-02-28 04:05:25 +0000454 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200455
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200456 np = bp->pdev->dev.of_node;
457 if (np) {
458 /* try dt phy registration */
459 err = of_mdiobus_register(bp->mii_bus, np);
460
461 /* fallback to standard phy registration if no phy were
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700462 * found during dt phy registration
463 */
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200464 if (!err && !phy_find_first(bp->mii_bus)) {
465 for (i = 0; i < PHY_MAX_ADDR; i++) {
466 struct phy_device *phydev;
467
468 phydev = mdiobus_scan(bp->mii_bus, i);
Sergei Shtylyovce24c2b2016-05-01 01:47:36 +0300469 if (IS_ERR(phydev) &&
470 PTR_ERR(phydev) != -ENODEV) {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200471 err = PTR_ERR(phydev);
472 break;
473 }
474 }
475
476 if (err)
477 goto err_out_unregister_bus;
478 }
479 } else {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200480 if (pdata)
481 bp->mii_bus->phy_mask = pdata->phy_mask;
482
483 err = mdiobus_register(bp->mii_bus);
484 }
485
486 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100487 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200488
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200489 err = macb_mii_probe(bp->dev);
490 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200491 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200492
493 return 0;
494
495err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700496 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700497err_out_free_mdiobus:
498 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200499err_out:
500 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100501}
502
503static void macb_update_stats(struct macb *bp)
504{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000505 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
506 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300507 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100508
509 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
510
Moritz Fischer96ec6312016-03-29 19:11:11 -0700511 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700512 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100513}
514
Nicolas Ferree86cd532012-10-31 06:04:57 +0000515static int macb_halt_tx(struct macb *bp)
516{
517 unsigned long halt_time, timeout;
518 u32 status;
519
520 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
521
522 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
523 do {
524 halt_time = jiffies;
525 status = macb_readl(bp, TSR);
526 if (!(status & MACB_BIT(TGO)))
527 return 0;
528
529 usleep_range(10, 250);
530 } while (time_before(halt_time, timeout));
531
532 return -ETIMEDOUT;
533}
534
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200535static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
536{
537 if (tx_skb->mapping) {
538 if (tx_skb->mapped_as_page)
539 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
540 tx_skb->size, DMA_TO_DEVICE);
541 else
542 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
543 tx_skb->size, DMA_TO_DEVICE);
544 tx_skb->mapping = 0;
545 }
546
547 if (tx_skb->skb) {
548 dev_kfree_skb_any(tx_skb->skb);
549 tx_skb->skb = NULL;
550 }
551}
552
Harini Katakamfff80192016-08-09 13:15:53 +0530553static inline void macb_set_addr(struct macb_dma_desc *desc, dma_addr_t addr)
554{
555 desc->addr = (u32)addr;
556#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
557 desc->addrh = (u32)(addr >> 32);
558#endif
559}
560
Nicolas Ferree86cd532012-10-31 06:04:57 +0000561static void macb_tx_error_task(struct work_struct *work)
562{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100563 struct macb_queue *queue = container_of(work, struct macb_queue,
564 tx_error_task);
565 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000566 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100567 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000568 struct sk_buff *skb;
569 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100570 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000571
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100572 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
573 (unsigned int)(queue - bp->queues),
574 queue->tx_tail, queue->tx_head);
575
576 /* Prevent the queue IRQ handlers from running: each of them may call
577 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
578 * As explained below, we have to halt the transmission before updating
579 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
580 * network engine about the macb/gem being halted.
581 */
582 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000583
584 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100585 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000586
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700587 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000588 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100589 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000590 */
591 if (macb_halt_tx(bp))
592 /* Just complain for now, reinitializing TX path can be good */
593 netdev_err(bp->dev, "BUG: halt tx timed out\n");
594
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700595 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000596 * Free transmit buffers in upper layer.
597 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100598 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
599 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000600
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100601 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000602 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100603 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000604 skb = tx_skb->skb;
605
606 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200607 /* skb is set for the last buffer of the frame */
608 while (!skb) {
609 macb_tx_unmap(bp, tx_skb);
610 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100611 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200612 skb = tx_skb->skb;
613 }
614
615 /* ctrl still refers to the first buffer descriptor
616 * since it's the only one written back by the hardware
617 */
618 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
619 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500620 macb_tx_ring_wrap(bp, tail),
621 skb->data);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200622 bp->stats.tx_packets++;
623 bp->stats.tx_bytes += skb->len;
624 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000625 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700626 /* "Buffers exhausted mid-frame" errors may only happen
627 * if the driver is buggy, so complain loudly about
628 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000629 */
630 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
631 netdev_err(bp->dev,
632 "BUG: TX buffers exhausted mid-frame\n");
633
634 desc->ctrl = ctrl | MACB_BIT(TX_USED);
635 }
636
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200637 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000638 }
639
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100640 /* Set end of TX queue */
641 desc = macb_tx_desc(queue, 0);
Harini Katakamfff80192016-08-09 13:15:53 +0530642 macb_set_addr(desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100643 desc->ctrl = MACB_BIT(TX_USED);
644
Nicolas Ferree86cd532012-10-31 06:04:57 +0000645 /* Make descriptor updates visible to hardware */
646 wmb();
647
648 /* Reinitialize the TX desc queue */
Harini Katakamfff80192016-08-09 13:15:53 +0530649 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
650#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
651 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
652#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000653 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100654 queue->tx_head = 0;
655 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000656
657 /* Housework before enabling TX IRQ */
658 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100659 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
660
661 /* Now we are ready to start transmission again */
662 netif_tx_start_all_queues(bp->dev);
663 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
664
665 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000666}
667
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100668static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100669{
670 unsigned int tail;
671 unsigned int head;
672 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100673 struct macb *bp = queue->bp;
674 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100675
676 status = macb_readl(bp, TSR);
677 macb_writel(bp, TSR, status);
678
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000679 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100680 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000681
Nicolas Ferree86cd532012-10-31 06:04:57 +0000682 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700683 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100684
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100685 head = queue->tx_head;
686 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000687 struct macb_tx_skb *tx_skb;
688 struct sk_buff *skb;
689 struct macb_dma_desc *desc;
690 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100691
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100692 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100693
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000694 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100695 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000696
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000697 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100698
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200699 /* TX_USED bit is only set by hardware on the very first buffer
700 * descriptor of the transmitted frame.
701 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000702 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100703 break;
704
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200705 /* Process all buffers of the current transmitted frame */
706 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100707 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200708 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000709
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200710 /* First, update TX stats if needed */
711 if (skb) {
712 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500713 macb_tx_ring_wrap(bp, tail),
714 skb->data);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200715 bp->stats.tx_packets++;
716 bp->stats.tx_bytes += skb->len;
717 }
718
719 /* Now we can safely release resources */
720 macb_tx_unmap(bp, tx_skb);
721
722 /* skb is set only for the last buffer of the frame.
723 * WARNING: at this point skb has been freed by
724 * macb_tx_unmap().
725 */
726 if (skb)
727 break;
728 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100729 }
730
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100731 queue->tx_tail = tail;
732 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
733 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500734 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100735 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100736}
737
Nicolas Ferre4df95132013-06-04 21:57:12 +0000738static void gem_rx_refill(struct macb *bp)
739{
740 unsigned int entry;
741 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000742 dma_addr_t paddr;
743
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700744 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500745 bp->rx_ring_size) > 0) {
746 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000747
748 /* Make hw descriptor updates visible to CPU */
749 rmb();
750
Nicolas Ferre4df95132013-06-04 21:57:12 +0000751 bp->rx_prepared_head++;
752
Moritz Fischeraa50b552016-03-29 19:11:13 -0700753 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000754 /* allocate sk_buff for this free entry in ring */
755 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700756 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000757 netdev_err(bp->dev,
758 "Unable to allocate sk_buff\n");
759 break;
760 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000761
762 /* now fill corresponding descriptor entry */
763 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700764 bp->rx_buffer_size,
765 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800766 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
767 dev_kfree_skb(skb);
768 break;
769 }
770
771 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000772
Zach Brownb410d132016-10-19 09:56:57 -0500773 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000774 paddr |= MACB_BIT(RX_WRAP);
Harini Katakamfff80192016-08-09 13:15:53 +0530775 macb_set_addr(&(bp->rx_ring[entry]), paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000776 bp->rx_ring[entry].ctrl = 0;
777
778 /* properly align Ethernet header */
779 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530780 } else {
781 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
782 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000783 }
784 }
785
786 /* Make descriptor updates visible to hardware */
787 wmb();
788
789 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700790 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000791}
792
793/* Mark DMA descriptors from begin up to and not including end as unused */
794static void discard_partial_frame(struct macb *bp, unsigned int begin,
795 unsigned int end)
796{
797 unsigned int frag;
798
799 for (frag = begin; frag != end; frag++) {
800 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700801
Nicolas Ferre4df95132013-06-04 21:57:12 +0000802 desc->addr &= ~MACB_BIT(RX_USED);
803 }
804
805 /* Make descriptor updates visible to hardware */
806 wmb();
807
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700808 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000809 * whatever caused this is updated, so we don't have to record
810 * anything.
811 */
812}
813
814static int gem_rx(struct macb *bp, int budget)
815{
816 unsigned int len;
817 unsigned int entry;
818 struct sk_buff *skb;
819 struct macb_dma_desc *desc;
820 int count = 0;
821
822 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530823 u32 ctrl;
824 dma_addr_t addr;
825 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000826
Zach Brownb410d132016-10-19 09:56:57 -0500827 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000828 desc = &bp->rx_ring[entry];
829
830 /* Make hw descriptor updates visible to CPU */
831 rmb();
832
Harini Katakamfff80192016-08-09 13:15:53 +0530833 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
834 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
835#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
836 addr |= ((u64)(desc->addrh) << 32);
837#endif
Nicolas Ferre4df95132013-06-04 21:57:12 +0000838 ctrl = desc->ctrl;
839
Harini Katakamfff80192016-08-09 13:15:53 +0530840 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000841 break;
842
Nicolas Ferre4df95132013-06-04 21:57:12 +0000843 bp->rx_tail++;
844 count++;
845
846 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
847 netdev_err(bp->dev,
848 "not whole frame pointed by descriptor\n");
849 bp->stats.rx_dropped++;
850 break;
851 }
852 skb = bp->rx_skbuff[entry];
853 if (unlikely(!skb)) {
854 netdev_err(bp->dev,
855 "inconsistent Rx descriptor chain\n");
856 bp->stats.rx_dropped++;
857 break;
858 }
859 /* now everything is ready for receiving packet */
860 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530861 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000862
863 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
864
865 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000866 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800867 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000868
869 skb->protocol = eth_type_trans(skb, bp->dev);
870 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200871 if (bp->dev->features & NETIF_F_RXCSUM &&
872 !(bp->dev->flags & IFF_PROMISC) &&
873 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
874 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000875
876 bp->stats.rx_packets++;
877 bp->stats.rx_bytes += skb->len;
878
879#if defined(DEBUG) && defined(VERBOSE_DEBUG)
880 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
881 skb->len, skb->csum);
882 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100883 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000884 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
885 skb->data, 32, true);
886#endif
887
888 netif_receive_skb(skb);
889 }
890
891 gem_rx_refill(bp);
892
893 return count;
894}
895
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100896static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
897 unsigned int last_frag)
898{
899 unsigned int len;
900 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000901 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100902 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000903 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100904
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000905 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530906 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100907
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000908 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -0500909 macb_rx_ring_wrap(bp, first_frag),
910 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100911
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700912 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000913 * first buffer. Since the header is 14 bytes, this makes the
914 * payload word-aligned.
915 *
916 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
917 * the two padding bytes into the skb so that we avoid hitting
918 * the slowpath in memcpy(), and pull them off afterwards.
919 */
920 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100921 if (!skb) {
922 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000923 for (frag = first_frag; ; frag++) {
924 desc = macb_rx_desc(bp, frag);
925 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100926 if (frag == last_frag)
927 break;
928 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000929
930 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100931 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000932
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100933 return 1;
934 }
935
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000936 offset = 0;
937 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700938 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100939 skb_put(skb, len);
940
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000941 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000942 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100943
944 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100945 if (unlikely(frag != last_frag)) {
946 dev_kfree_skb_any(skb);
947 return -1;
948 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100949 frag_len = len - offset;
950 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300951 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -0700952 macb_rx_buffer(bp, frag),
953 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000954 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000955 desc = macb_rx_desc(bp, frag);
956 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100957
958 if (frag == last_frag)
959 break;
960 }
961
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000962 /* Make descriptor updates visible to hardware */
963 wmb();
964
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000965 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100966 skb->protocol = eth_type_trans(skb, bp->dev);
967
968 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000969 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000970 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700971 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100972 netif_receive_skb(skb);
973
974 return 0;
975}
976
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100977static inline void macb_init_rx_ring(struct macb *bp)
978{
979 dma_addr_t addr;
980 int i;
981
982 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -0500983 for (i = 0; i < bp->rx_ring_size; i++) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100984 bp->rx_ring[i].addr = addr;
985 bp->rx_ring[i].ctrl = 0;
986 addr += bp->rx_buffer_size;
987 }
Zach Brownb410d132016-10-19 09:56:57 -0500988 bp->rx_ring[bp->rx_ring_size - 1].addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100989}
990
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100991static int macb_rx(struct macb *bp, int budget)
992{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100993 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100994 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000995 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100996 int first_frag = -1;
997
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000998 for (tail = bp->rx_tail; budget > 0; tail++) {
999 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001000 u32 addr, ctrl;
1001
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001002 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001003 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001004
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001005 addr = desc->addr;
1006 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001007
1008 if (!(addr & MACB_BIT(RX_USED)))
1009 break;
1010
1011 if (ctrl & MACB_BIT(RX_SOF)) {
1012 if (first_frag != -1)
1013 discard_partial_frame(bp, first_frag, tail);
1014 first_frag = tail;
1015 }
1016
1017 if (ctrl & MACB_BIT(RX_EOF)) {
1018 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001019
1020 if (unlikely(first_frag == -1)) {
1021 reset_rx_queue = true;
1022 continue;
1023 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001024
1025 dropped = macb_rx_frame(bp, first_frag, tail);
1026 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001027 if (unlikely(dropped < 0)) {
1028 reset_rx_queue = true;
1029 continue;
1030 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001031 if (!dropped) {
1032 received++;
1033 budget--;
1034 }
1035 }
1036 }
1037
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001038 if (unlikely(reset_rx_queue)) {
1039 unsigned long flags;
1040 u32 ctrl;
1041
1042 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1043
1044 spin_lock_irqsave(&bp->lock, flags);
1045
1046 ctrl = macb_readl(bp, NCR);
1047 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1048
1049 macb_init_rx_ring(bp);
1050 macb_writel(bp, RBQP, bp->rx_ring_dma);
1051
1052 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1053
1054 spin_unlock_irqrestore(&bp->lock, flags);
1055 return received;
1056 }
1057
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001058 if (first_frag != -1)
1059 bp->rx_tail = first_frag;
1060 else
1061 bp->rx_tail = tail;
1062
1063 return received;
1064}
1065
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001066static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001067{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001068 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001069 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001070 u32 status;
1071
1072 status = macb_readl(bp, RSR);
1073 macb_writel(bp, RSR, status);
1074
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001075 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001077 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001078 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001079
Nicolas Ferre4df95132013-06-04 21:57:12 +00001080 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001081 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001082 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083
Nicolas Ferre8770e912013-02-12 11:08:48 +01001084 /* Packets received while interrupts were disabled */
1085 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001086 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001087 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1088 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001089 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001090 } else {
1091 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1092 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001093 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001094
1095 /* TODO: Handle errors */
1096
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001097 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001098}
1099
1100static irqreturn_t macb_interrupt(int irq, void *dev_id)
1101{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001102 struct macb_queue *queue = dev_id;
1103 struct macb *bp = queue->bp;
1104 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001105 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001106
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001107 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001108
1109 if (unlikely(!status))
1110 return IRQ_NONE;
1111
1112 spin_lock(&bp->lock);
1113
1114 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001115 /* close possible race with dev_close */
1116 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001117 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001118 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1119 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001120 break;
1121 }
1122
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001123 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1124 (unsigned int)(queue - bp->queues),
1125 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001126
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001127 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001128 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001129 * until we have processed the buffers. The
1130 * scheduling call may fail if the poll routine
1131 * is already scheduled, so disable interrupts
1132 * now.
1133 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001134 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001135 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001136 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001137
Ben Hutchings288379f2009-01-19 16:43:59 -08001138 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001139 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001140 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001141 }
1142 }
1143
Nicolas Ferree86cd532012-10-31 06:04:57 +00001144 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001145 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1146 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001147
1148 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001149 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001150
Nicolas Ferree86cd532012-10-31 06:04:57 +00001151 break;
1152 }
1153
1154 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001155 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001156
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001157 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001158 * add that if/when we get our hands on a full-blown MII PHY.
1159 */
1160
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001161 /* There is a hardware issue under heavy load where DMA can
1162 * stop, this causes endless "used buffer descriptor read"
1163 * interrupts but it can be cleared by re-enabling RX. See
1164 * the at91 manual, section 41.3.1 or the Zynq manual
1165 * section 16.7.4 for details.
1166 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001167 if (status & MACB_BIT(RXUBR)) {
1168 ctrl = macb_readl(bp, NCR);
1169 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1170 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1171
1172 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001173 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001174 }
1175
Alexander Steinb19f7f72011-04-13 05:03:24 +00001176 if (status & MACB_BIT(ISR_ROVR)) {
1177 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001178 if (macb_is_gem(bp))
1179 bp->hw_stats.gem.rx_overruns++;
1180 else
1181 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001182
1183 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001184 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001185 }
1186
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001187 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001188 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001189 * netdev_err to a lower-priority context as well
1190 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001191 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001192 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001193
1194 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001195 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 }
1197
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001198 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001199 }
1200
1201 spin_unlock(&bp->lock);
1202
1203 return IRQ_HANDLED;
1204}
1205
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001206#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001207/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001208 * to allow network i/o with interrupts disabled.
1209 */
1210static void macb_poll_controller(struct net_device *dev)
1211{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001212 struct macb *bp = netdev_priv(dev);
1213 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001214 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001215 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001216
1217 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001218 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1219 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001220 local_irq_restore(flags);
1221}
1222#endif
1223
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001224static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001225 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001226 struct sk_buff *skb)
1227{
1228 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001229 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001230 struct macb_tx_skb *tx_skb = NULL;
1231 struct macb_dma_desc *desc;
1232 unsigned int offset, size, count = 0;
1233 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1234 unsigned int eof = 1;
1235 u32 ctrl;
1236
1237 /* First, map non-paged data */
1238 len = skb_headlen(skb);
1239 offset = 0;
1240 while (len) {
1241 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001242 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001243 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001244
1245 mapping = dma_map_single(&bp->pdev->dev,
1246 skb->data + offset,
1247 size, DMA_TO_DEVICE);
1248 if (dma_mapping_error(&bp->pdev->dev, mapping))
1249 goto dma_error;
1250
1251 /* Save info to properly release resources */
1252 tx_skb->skb = NULL;
1253 tx_skb->mapping = mapping;
1254 tx_skb->size = size;
1255 tx_skb->mapped_as_page = false;
1256
1257 len -= size;
1258 offset += size;
1259 count++;
1260 tx_head++;
1261 }
1262
1263 /* Then, map paged data from fragments */
1264 for (f = 0; f < nr_frags; f++) {
1265 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1266
1267 len = skb_frag_size(frag);
1268 offset = 0;
1269 while (len) {
1270 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001271 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001272 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001273
1274 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1275 offset, size, DMA_TO_DEVICE);
1276 if (dma_mapping_error(&bp->pdev->dev, mapping))
1277 goto dma_error;
1278
1279 /* Save info to properly release resources */
1280 tx_skb->skb = NULL;
1281 tx_skb->mapping = mapping;
1282 tx_skb->size = size;
1283 tx_skb->mapped_as_page = true;
1284
1285 len -= size;
1286 offset += size;
1287 count++;
1288 tx_head++;
1289 }
1290 }
1291
1292 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001293 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001294 netdev_err(bp->dev, "BUG! empty skb!\n");
1295 return 0;
1296 }
1297
1298 /* This is the last buffer of the frame: save socket buffer */
1299 tx_skb->skb = skb;
1300
1301 /* Update TX ring: update buffer descriptors in reverse order
1302 * to avoid race condition
1303 */
1304
1305 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1306 * to set the end of TX queue
1307 */
1308 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001309 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001310 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001311 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001312 desc->ctrl = ctrl;
1313
1314 do {
1315 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001316 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001317 tx_skb = &queue->tx_skb[entry];
1318 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001319
1320 ctrl = (u32)tx_skb->size;
1321 if (eof) {
1322 ctrl |= MACB_BIT(TX_LAST);
1323 eof = 0;
1324 }
Zach Brownb410d132016-10-19 09:56:57 -05001325 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001326 ctrl |= MACB_BIT(TX_WRAP);
1327
1328 /* Set TX buffer descriptor */
Harini Katakamfff80192016-08-09 13:15:53 +05301329 macb_set_addr(desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001330 /* desc->addr must be visible to hardware before clearing
1331 * 'TX_USED' bit in desc->ctrl.
1332 */
1333 wmb();
1334 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001335 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001336
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001337 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001338
1339 return count;
1340
1341dma_error:
1342 netdev_err(bp->dev, "TX DMA map failed\n");
1343
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001344 for (i = queue->tx_head; i != tx_head; i++) {
1345 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001346
1347 macb_tx_unmap(bp, tx_skb);
1348 }
1349
1350 return 0;
1351}
1352
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001353static inline int macb_clear_csum(struct sk_buff *skb)
1354{
1355 /* no change for packets without checksum offloading */
1356 if (skb->ip_summed != CHECKSUM_PARTIAL)
1357 return 0;
1358
1359 /* make sure we can modify the header */
1360 if (unlikely(skb_cow_head(skb, 0)))
1361 return -1;
1362
1363 /* initialize checksum field
1364 * This is required - at least for Zynq, which otherwise calculates
1365 * wrong UDP header checksums for UDP packets with UDP data len <=2
1366 */
1367 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1368 return 0;
1369}
1370
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001371static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1372{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001373 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001374 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001375 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001376 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001377 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001378
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001379#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1380 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001381 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1382 queue_index, skb->len, skb->head, skb->data,
1383 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001384 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1385 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001386#endif
1387
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001388 /* Count how many TX buffer descriptors are needed to send this
1389 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001390 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001391 */
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001392 count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001393 nr_frags = skb_shinfo(skb)->nr_frags;
1394 for (f = 0; f < nr_frags; f++) {
1395 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001396 count += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001397 }
1398
Dongdong Deng48719532009-08-23 19:49:07 -07001399 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001400
1401 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001402 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
1403 bp->tx_ring_size) < count) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001404 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001405 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001406 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001407 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001408 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001409 }
1410
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001411 if (macb_clear_csum(skb)) {
1412 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001413 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001414 }
1415
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001416 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001417 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001418 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001419 goto unlock;
1420 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001421
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001422 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001423 wmb();
1424
Richard Cochrane0720922011-06-19 21:51:28 +00001425 skb_tx_timestamp(skb);
1426
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1428
Zach Brownb410d132016-10-19 09:56:57 -05001429 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001430 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001431
Soren Brinkmann92030902014-03-04 08:46:39 -08001432unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001433 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001434
Patrick McHardy6ed10652009-06-23 06:03:08 +00001435 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001436}
1437
Nicolas Ferre4df95132013-06-04 21:57:12 +00001438static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001439{
1440 if (!macb_is_gem(bp)) {
1441 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1442 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001443 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001444
Nicolas Ferre1b447912013-06-04 21:57:11 +00001445 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001446 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001447 "RX buffer must be multiple of %d bytes, expanding\n",
1448 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001449 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001450 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001451 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001452 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001453
1454 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1455 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001456}
1457
Nicolas Ferre4df95132013-06-04 21:57:12 +00001458static void gem_free_rx_buffers(struct macb *bp)
1459{
1460 struct sk_buff *skb;
1461 struct macb_dma_desc *desc;
1462 dma_addr_t addr;
1463 int i;
1464
1465 if (!bp->rx_skbuff)
1466 return;
1467
Zach Brownb410d132016-10-19 09:56:57 -05001468 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001469 skb = bp->rx_skbuff[i];
1470
Moritz Fischeraa50b552016-03-29 19:11:13 -07001471 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001472 continue;
1473
1474 desc = &bp->rx_ring[i];
1475 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Harini Katakamfff80192016-08-09 13:15:53 +05301476#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1477 addr |= ((u64)(desc->addrh) << 32);
1478#endif
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001479 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001480 DMA_FROM_DEVICE);
1481 dev_kfree_skb_any(skb);
1482 skb = NULL;
1483 }
1484
1485 kfree(bp->rx_skbuff);
1486 bp->rx_skbuff = NULL;
1487}
1488
1489static void macb_free_rx_buffers(struct macb *bp)
1490{
1491 if (bp->rx_buffers) {
1492 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001493 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001494 bp->rx_buffers, bp->rx_buffers_dma);
1495 bp->rx_buffers = NULL;
1496 }
1497}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001498
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001499static void macb_free_consistent(struct macb *bp)
1500{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001501 struct macb_queue *queue;
1502 unsigned int q;
1503
Nicolas Ferre4df95132013-06-04 21:57:12 +00001504 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001505 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001506 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001507 bp->rx_ring, bp->rx_ring_dma);
1508 bp->rx_ring = NULL;
1509 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001510
1511 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1512 kfree(queue->tx_skb);
1513 queue->tx_skb = NULL;
1514 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001515 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001516 queue->tx_ring, queue->tx_ring_dma);
1517 queue->tx_ring = NULL;
1518 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001519 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001520}
1521
1522static int gem_alloc_rx_buffers(struct macb *bp)
1523{
1524 int size;
1525
Zach Brownb410d132016-10-19 09:56:57 -05001526 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001527 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1528 if (!bp->rx_skbuff)
1529 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001530 else
1531 netdev_dbg(bp->dev,
1532 "Allocated %d RX struct sk_buff entries at %p\n",
1533 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001534 return 0;
1535}
1536
1537static int macb_alloc_rx_buffers(struct macb *bp)
1538{
1539 int size;
1540
Zach Brownb410d132016-10-19 09:56:57 -05001541 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001542 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1543 &bp->rx_buffers_dma, GFP_KERNEL);
1544 if (!bp->rx_buffers)
1545 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001546
1547 netdev_dbg(bp->dev,
1548 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1549 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001550 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001551}
1552
1553static int macb_alloc_consistent(struct macb *bp)
1554{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001555 struct macb_queue *queue;
1556 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001557 int size;
1558
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001559 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001560 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001561 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1562 &queue->tx_ring_dma,
1563 GFP_KERNEL);
1564 if (!queue->tx_ring)
1565 goto out_err;
1566 netdev_dbg(bp->dev,
1567 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1568 q, size, (unsigned long)queue->tx_ring_dma,
1569 queue->tx_ring);
1570
Zach Brownb410d132016-10-19 09:56:57 -05001571 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001572 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1573 if (!queue->tx_skb)
1574 goto out_err;
1575 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001576
Zach Brownb410d132016-10-19 09:56:57 -05001577 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001578 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1579 &bp->rx_ring_dma, GFP_KERNEL);
1580 if (!bp->rx_ring)
1581 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001582 netdev_dbg(bp->dev,
1583 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1584 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001585
Nicolas Ferre4df95132013-06-04 21:57:12 +00001586 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001587 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001588
1589 return 0;
1590
1591out_err:
1592 macb_free_consistent(bp);
1593 return -ENOMEM;
1594}
1595
Nicolas Ferre4df95132013-06-04 21:57:12 +00001596static void gem_init_rings(struct macb *bp)
1597{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001598 struct macb_queue *queue;
1599 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001600 int i;
1601
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001602 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001603 for (i = 0; i < bp->tx_ring_size; i++) {
1604 queue->tx_ring[i].addr = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001605 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1606 }
Zach Brownb410d132016-10-19 09:56:57 -05001607 queue->tx_ring[bp->tx_ring_size - 1].ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001608 queue->tx_head = 0;
1609 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001610 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001611
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001612 bp->rx_tail = 0;
1613 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001614
1615 gem_rx_refill(bp);
1616}
1617
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001618static void macb_init_rings(struct macb *bp)
1619{
1620 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001621
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001622 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001623
Zach Brownb410d132016-10-19 09:56:57 -05001624 for (i = 0; i < bp->tx_ring_size; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001625 bp->queues[0].tx_ring[i].addr = 0;
1626 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001627 }
Ben Shelton21d35152015-04-22 17:28:54 -05001628 bp->queues[0].tx_head = 0;
1629 bp->queues[0].tx_tail = 0;
Zach Brownb410d132016-10-19 09:56:57 -05001630 bp->queues[0].tx_ring[bp->tx_ring_size - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001631
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001632 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001633}
1634
1635static void macb_reset_hw(struct macb *bp)
1636{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001637 struct macb_queue *queue;
1638 unsigned int q;
1639
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001640 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001641 * more gracefully?)
1642 */
1643 macb_writel(bp, NCR, 0);
1644
1645 /* Clear the stats registers (XXX: Update stats first?) */
1646 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1647
1648 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001649 macb_writel(bp, TSR, -1);
1650 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001651
1652 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001653 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1654 queue_writel(queue, IDR, -1);
1655 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001656 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1657 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001658 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001659}
1660
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001661static u32 gem_mdc_clk_div(struct macb *bp)
1662{
1663 u32 config;
1664 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1665
1666 if (pclk_hz <= 20000000)
1667 config = GEM_BF(CLK, GEM_CLK_DIV8);
1668 else if (pclk_hz <= 40000000)
1669 config = GEM_BF(CLK, GEM_CLK_DIV16);
1670 else if (pclk_hz <= 80000000)
1671 config = GEM_BF(CLK, GEM_CLK_DIV32);
1672 else if (pclk_hz <= 120000000)
1673 config = GEM_BF(CLK, GEM_CLK_DIV48);
1674 else if (pclk_hz <= 160000000)
1675 config = GEM_BF(CLK, GEM_CLK_DIV64);
1676 else
1677 config = GEM_BF(CLK, GEM_CLK_DIV96);
1678
1679 return config;
1680}
1681
1682static u32 macb_mdc_clk_div(struct macb *bp)
1683{
1684 u32 config;
1685 unsigned long pclk_hz;
1686
1687 if (macb_is_gem(bp))
1688 return gem_mdc_clk_div(bp);
1689
1690 pclk_hz = clk_get_rate(bp->pclk);
1691 if (pclk_hz <= 20000000)
1692 config = MACB_BF(CLK, MACB_CLK_DIV8);
1693 else if (pclk_hz <= 40000000)
1694 config = MACB_BF(CLK, MACB_CLK_DIV16);
1695 else if (pclk_hz <= 80000000)
1696 config = MACB_BF(CLK, MACB_CLK_DIV32);
1697 else
1698 config = MACB_BF(CLK, MACB_CLK_DIV64);
1699
1700 return config;
1701}
1702
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001703/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001704 * should program. We find the width from decoding the design configuration
1705 * register to find the maximum supported data bus width.
1706 */
1707static u32 macb_dbw(struct macb *bp)
1708{
1709 if (!macb_is_gem(bp))
1710 return 0;
1711
1712 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1713 case 4:
1714 return GEM_BF(DBW, GEM_DBW128);
1715 case 2:
1716 return GEM_BF(DBW, GEM_DBW64);
1717 case 1:
1718 default:
1719 return GEM_BF(DBW, GEM_DBW32);
1720 }
1721}
1722
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001723/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001724 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001725 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001726 * (if not supported by FIFO, it will fallback to default)
1727 * - set both rx/tx packet buffers to full memory size
1728 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001729 */
1730static void macb_configure_dma(struct macb *bp)
1731{
1732 u32 dmacfg;
1733
1734 if (macb_is_gem(bp)) {
1735 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001736 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001737 if (bp->dma_burst_length)
1738 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001739 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301740 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301741
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03001742 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301743 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1744 else
1745 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1746
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001747 if (bp->dev->features & NETIF_F_HW_CSUM)
1748 dmacfg |= GEM_BIT(TXCOEN);
1749 else
1750 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301751
1752#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1753 dmacfg |= GEM_BIT(ADDR64);
1754#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001755 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1756 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001757 gem_writel(bp, DMACFG, dmacfg);
1758 }
1759}
1760
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001761static void macb_init_hw(struct macb *bp)
1762{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001763 struct macb_queue *queue;
1764 unsigned int q;
1765
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001766 u32 config;
1767
1768 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001769 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001770
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001771 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301772 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1773 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001774 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001775 config |= MACB_BIT(PAE); /* PAuse Enable */
1776 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001777 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301778 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1779 else
1780 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001781 if (bp->dev->flags & IFF_PROMISC)
1782 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001783 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1784 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001785 if (!(bp->dev->flags & IFF_BROADCAST))
1786 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001787 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001788 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001789 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301790 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001791 bp->speed = SPEED_10;
1792 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301793 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001794 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301795 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001796
Jamie Iles0116da42011-03-14 17:38:30 +00001797 macb_configure_dma(bp);
1798
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001799 /* Initialize TX and RX buffers */
Harini Katakamfff80192016-08-09 13:15:53 +05301800 macb_writel(bp, RBQP, (u32)(bp->rx_ring_dma));
1801#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1802 macb_writel(bp, RBQPH, (u32)(bp->rx_ring_dma >> 32));
1803#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001804 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakamfff80192016-08-09 13:15:53 +05301805 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
1806#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1807 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
1808#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001809
1810 /* Enable interrupts */
1811 queue_writel(queue, IER,
1812 MACB_RX_INT_FLAGS |
1813 MACB_TX_INT_FLAGS |
1814 MACB_BIT(HRESP));
1815 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001816
1817 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001818 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001819}
1820
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001821/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001822 * locations in the memory map. The least significant bits are stored
1823 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1824 *
1825 * The unicast hash enable and the multicast hash enable bits in the
1826 * network configuration register enable the reception of hash matched
1827 * frames. The destination address is reduced to a 6 bit index into
1828 * the 64 bit hash register using the following hash function. The
1829 * hash function is an exclusive or of every sixth bit of the
1830 * destination address.
1831 *
1832 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1833 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1834 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1835 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1836 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1837 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1838 *
1839 * da[0] represents the least significant bit of the first byte
1840 * received, that is, the multicast/unicast indicator, and da[47]
1841 * represents the most significant bit of the last byte received. If
1842 * the hash index, hi[n], points to a bit that is set in the hash
1843 * register then the frame will be matched according to whether the
1844 * frame is multicast or unicast. A multicast match will be signalled
1845 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1846 * index points to a bit set in the hash register. A unicast match
1847 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1848 * and the hash index points to a bit set in the hash register. To
1849 * receive all multicast frames, the hash register should be set with
1850 * all ones and the multicast hash enable bit should be set in the
1851 * network configuration register.
1852 */
1853
1854static inline int hash_bit_value(int bitnr, __u8 *addr)
1855{
1856 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1857 return 1;
1858 return 0;
1859}
1860
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001861/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001862static int hash_get_index(__u8 *addr)
1863{
1864 int i, j, bitval;
1865 int hash_index = 0;
1866
1867 for (j = 0; j < 6; j++) {
1868 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001869 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001870
1871 hash_index |= (bitval << j);
1872 }
1873
1874 return hash_index;
1875}
1876
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001877/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001878static void macb_sethashtable(struct net_device *dev)
1879{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001880 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001881 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001882 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001883 struct macb *bp = netdev_priv(dev);
1884
Moritz Fischeraa50b552016-03-29 19:11:13 -07001885 mc_filter[0] = 0;
1886 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001887
Jiri Pirko22bedad32010-04-01 21:22:57 +00001888 netdev_for_each_mc_addr(ha, dev) {
1889 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001890 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1891 }
1892
Jamie Ilesf75ba502011-11-08 10:12:32 +00001893 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1894 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001895}
1896
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001897/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001898static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001899{
1900 unsigned long cfg;
1901 struct macb *bp = netdev_priv(dev);
1902
1903 cfg = macb_readl(bp, NCFGR);
1904
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001905 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001906 /* Enable promiscuous mode */
1907 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001908
1909 /* Disable RX checksum offload */
1910 if (macb_is_gem(bp))
1911 cfg &= ~GEM_BIT(RXCOEN);
1912 } else {
1913 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001914 cfg &= ~MACB_BIT(CAF);
1915
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001916 /* Enable RX checksum offload only if requested */
1917 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1918 cfg |= GEM_BIT(RXCOEN);
1919 }
1920
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001921 if (dev->flags & IFF_ALLMULTI) {
1922 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001923 macb_or_gem_writel(bp, HRB, -1);
1924 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001925 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001926 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001927 /* Enable specific multicasts */
1928 macb_sethashtable(dev);
1929 cfg |= MACB_BIT(NCFGR_MTI);
1930 } else if (dev->flags & (~IFF_ALLMULTI)) {
1931 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001932 macb_or_gem_writel(bp, HRB, 0);
1933 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001934 cfg &= ~MACB_BIT(NCFGR_MTI);
1935 }
1936
1937 macb_writel(bp, NCFGR, cfg);
1938}
1939
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001940static int macb_open(struct net_device *dev)
1941{
1942 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001943 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001944 int err;
1945
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001946 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001947
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001948 /* carrier starts down */
1949 netif_carrier_off(dev);
1950
frederic RODO6c36a702007-07-12 19:07:24 +02001951 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02001952 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02001953 return -EAGAIN;
1954
Nicolas Ferre1b447912013-06-04 21:57:11 +00001955 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001956 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001957
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001958 err = macb_alloc_consistent(bp);
1959 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001960 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1961 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001962 return err;
1963 }
1964
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001965 napi_enable(&bp->napi);
1966
Nicolas Ferre4df95132013-06-04 21:57:12 +00001967 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001968 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001969
frederic RODO6c36a702007-07-12 19:07:24 +02001970 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02001971 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02001972
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001973 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001974
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001975 return 0;
1976}
1977
1978static int macb_close(struct net_device *dev)
1979{
1980 struct macb *bp = netdev_priv(dev);
1981 unsigned long flags;
1982
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001983 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001984 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001985
Philippe Reynes0a912812016-06-22 00:32:35 +02001986 if (dev->phydev)
1987 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02001988
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001989 spin_lock_irqsave(&bp->lock, flags);
1990 macb_reset_hw(bp);
1991 netif_carrier_off(dev);
1992 spin_unlock_irqrestore(&bp->lock, flags);
1993
1994 macb_free_consistent(bp);
1995
1996 return 0;
1997}
1998
Harini Katakama5898ea2015-05-06 22:27:18 +05301999static int macb_change_mtu(struct net_device *dev, int new_mtu)
2000{
Harini Katakama5898ea2015-05-06 22:27:18 +05302001 if (netif_running(dev))
2002 return -EBUSY;
2003
Harini Katakama5898ea2015-05-06 22:27:18 +05302004 dev->mtu = new_mtu;
2005
2006 return 0;
2007}
2008
Jamie Ilesa494ed82011-03-09 16:26:35 +00002009static void gem_update_stats(struct macb *bp)
2010{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002011 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002012 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002013
Xander Huff3ff13f12015-01-13 16:15:51 -06002014 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2015 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002016 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002017
2018 bp->ethtool_stats[i] += val;
2019 *p += val;
2020
2021 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2022 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002023 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002024 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002025 *(++p) += val;
2026 }
2027 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002028}
2029
2030static struct net_device_stats *gem_get_stats(struct macb *bp)
2031{
2032 struct gem_stats *hwstat = &bp->hw_stats.gem;
2033 struct net_device_stats *nstat = &bp->stats;
2034
2035 gem_update_stats(bp);
2036
2037 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2038 hwstat->rx_alignment_errors +
2039 hwstat->rx_resource_errors +
2040 hwstat->rx_overruns +
2041 hwstat->rx_oversize_frames +
2042 hwstat->rx_jabbers +
2043 hwstat->rx_undersized_frames +
2044 hwstat->rx_length_field_frame_errors);
2045 nstat->tx_errors = (hwstat->tx_late_collisions +
2046 hwstat->tx_excessive_collisions +
2047 hwstat->tx_underrun +
2048 hwstat->tx_carrier_sense_errors);
2049 nstat->multicast = hwstat->rx_multicast_frames;
2050 nstat->collisions = (hwstat->tx_single_collision_frames +
2051 hwstat->tx_multiple_collision_frames +
2052 hwstat->tx_excessive_collisions);
2053 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2054 hwstat->rx_jabbers +
2055 hwstat->rx_undersized_frames +
2056 hwstat->rx_length_field_frame_errors);
2057 nstat->rx_over_errors = hwstat->rx_resource_errors;
2058 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2059 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2060 nstat->rx_fifo_errors = hwstat->rx_overruns;
2061 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2062 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2063 nstat->tx_fifo_errors = hwstat->tx_underrun;
2064
2065 return nstat;
2066}
2067
Xander Huff3ff13f12015-01-13 16:15:51 -06002068static void gem_get_ethtool_stats(struct net_device *dev,
2069 struct ethtool_stats *stats, u64 *data)
2070{
2071 struct macb *bp;
2072
2073 bp = netdev_priv(dev);
2074 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002075 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002076}
2077
2078static int gem_get_sset_count(struct net_device *dev, int sset)
2079{
2080 switch (sset) {
2081 case ETH_SS_STATS:
2082 return GEM_STATS_LEN;
2083 default:
2084 return -EOPNOTSUPP;
2085 }
2086}
2087
2088static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2089{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002090 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002091
2092 switch (sset) {
2093 case ETH_SS_STATS:
2094 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2095 memcpy(p, gem_statistics[i].stat_string,
2096 ETH_GSTRING_LEN);
2097 break;
2098 }
2099}
2100
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002101static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002102{
2103 struct macb *bp = netdev_priv(dev);
2104 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002105 struct macb_stats *hwstat = &bp->hw_stats.macb;
2106
2107 if (macb_is_gem(bp))
2108 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002109
frederic RODO6c36a702007-07-12 19:07:24 +02002110 /* read stats from hardware */
2111 macb_update_stats(bp);
2112
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002113 /* Convert HW stats into netdevice stats */
2114 nstat->rx_errors = (hwstat->rx_fcs_errors +
2115 hwstat->rx_align_errors +
2116 hwstat->rx_resource_errors +
2117 hwstat->rx_overruns +
2118 hwstat->rx_oversize_pkts +
2119 hwstat->rx_jabbers +
2120 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002121 hwstat->rx_length_mismatch);
2122 nstat->tx_errors = (hwstat->tx_late_cols +
2123 hwstat->tx_excessive_cols +
2124 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002125 hwstat->tx_carrier_errors +
2126 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002127 nstat->collisions = (hwstat->tx_single_cols +
2128 hwstat->tx_multiple_cols +
2129 hwstat->tx_excessive_cols);
2130 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2131 hwstat->rx_jabbers +
2132 hwstat->rx_undersize_pkts +
2133 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002134 nstat->rx_over_errors = hwstat->rx_resource_errors +
2135 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002136 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2137 nstat->rx_frame_errors = hwstat->rx_align_errors;
2138 nstat->rx_fifo_errors = hwstat->rx_overruns;
2139 /* XXX: What does "missed" mean? */
2140 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2141 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2142 nstat->tx_fifo_errors = hwstat->tx_underruns;
2143 /* Don't know about heartbeat or window errors... */
2144
2145 return nstat;
2146}
2147
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002148static int macb_get_regs_len(struct net_device *netdev)
2149{
2150 return MACB_GREGS_NBR * sizeof(u32);
2151}
2152
2153static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2154 void *p)
2155{
2156 struct macb *bp = netdev_priv(dev);
2157 unsigned int tail, head;
2158 u32 *regs_buff = p;
2159
2160 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2161 | MACB_GREGS_VERSION;
2162
Zach Brownb410d132016-10-19 09:56:57 -05002163 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2164 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002165
2166 regs_buff[0] = macb_readl(bp, NCR);
2167 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2168 regs_buff[2] = macb_readl(bp, NSR);
2169 regs_buff[3] = macb_readl(bp, TSR);
2170 regs_buff[4] = macb_readl(bp, RBQP);
2171 regs_buff[5] = macb_readl(bp, TBQP);
2172 regs_buff[6] = macb_readl(bp, RSR);
2173 regs_buff[7] = macb_readl(bp, IMR);
2174
2175 regs_buff[8] = tail;
2176 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002177 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2178 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002179
Neil Armstrongce721a72016-01-05 14:39:16 +01002180 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2181 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002182 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002183 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002184}
2185
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002186static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2187{
2188 struct macb *bp = netdev_priv(netdev);
2189
2190 wol->supported = 0;
2191 wol->wolopts = 0;
2192
2193 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2194 wol->supported = WAKE_MAGIC;
2195
2196 if (bp->wol & MACB_WOL_ENABLED)
2197 wol->wolopts |= WAKE_MAGIC;
2198 }
2199}
2200
2201static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2202{
2203 struct macb *bp = netdev_priv(netdev);
2204
2205 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2206 (wol->wolopts & ~WAKE_MAGIC))
2207 return -EOPNOTSUPP;
2208
2209 if (wol->wolopts & WAKE_MAGIC)
2210 bp->wol |= MACB_WOL_ENABLED;
2211 else
2212 bp->wol &= ~MACB_WOL_ENABLED;
2213
2214 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2215
2216 return 0;
2217}
2218
Zach Brown8441bb32016-10-19 09:56:58 -05002219static void macb_get_ringparam(struct net_device *netdev,
2220 struct ethtool_ringparam *ring)
2221{
2222 struct macb *bp = netdev_priv(netdev);
2223
2224 ring->rx_max_pending = MAX_RX_RING_SIZE;
2225 ring->tx_max_pending = MAX_TX_RING_SIZE;
2226
2227 ring->rx_pending = bp->rx_ring_size;
2228 ring->tx_pending = bp->tx_ring_size;
2229}
2230
2231static int macb_set_ringparam(struct net_device *netdev,
2232 struct ethtool_ringparam *ring)
2233{
2234 struct macb *bp = netdev_priv(netdev);
2235 u32 new_rx_size, new_tx_size;
2236 unsigned int reset = 0;
2237
2238 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2239 return -EINVAL;
2240
2241 new_rx_size = clamp_t(u32, ring->rx_pending,
2242 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2243 new_rx_size = roundup_pow_of_two(new_rx_size);
2244
2245 new_tx_size = clamp_t(u32, ring->tx_pending,
2246 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2247 new_tx_size = roundup_pow_of_two(new_tx_size);
2248
2249 if ((new_tx_size == bp->tx_ring_size) &&
2250 (new_rx_size == bp->rx_ring_size)) {
2251 /* nothing to do */
2252 return 0;
2253 }
2254
2255 if (netif_running(bp->dev)) {
2256 reset = 1;
2257 macb_close(bp->dev);
2258 }
2259
2260 bp->rx_ring_size = new_rx_size;
2261 bp->tx_ring_size = new_tx_size;
2262
2263 if (reset)
2264 macb_open(bp->dev);
2265
2266 return 0;
2267}
2268
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002269static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002270 .get_regs_len = macb_get_regs_len,
2271 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002272 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002273 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002274 .get_wol = macb_get_wol,
2275 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002276 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2277 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002278 .get_ringparam = macb_get_ringparam,
2279 .set_ringparam = macb_set_ringparam,
Xander Huff8cd5a562015-01-15 15:55:20 -06002280};
Xander Huff8cd5a562015-01-15 15:55:20 -06002281
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002282static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002283 .get_regs_len = macb_get_regs_len,
2284 .get_regs = macb_get_regs,
2285 .get_link = ethtool_op_get_link,
2286 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002287 .get_ethtool_stats = gem_get_ethtool_stats,
2288 .get_strings = gem_get_ethtool_strings,
2289 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002290 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2291 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Zach Brown8441bb32016-10-19 09:56:58 -05002292 .get_ringparam = macb_get_ringparam,
2293 .set_ringparam = macb_set_ringparam,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002294};
2295
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002296static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002297{
Philippe Reynes0a912812016-06-22 00:32:35 +02002298 struct phy_device *phydev = dev->phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002299
2300 if (!netif_running(dev))
2301 return -EINVAL;
2302
frederic RODO6c36a702007-07-12 19:07:24 +02002303 if (!phydev)
2304 return -ENODEV;
2305
Richard Cochran28b04112010-07-17 08:48:55 +00002306 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002307}
2308
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002309static int macb_set_features(struct net_device *netdev,
2310 netdev_features_t features)
2311{
2312 struct macb *bp = netdev_priv(netdev);
2313 netdev_features_t changed = features ^ netdev->features;
2314
2315 /* TX checksum offload */
2316 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2317 u32 dmacfg;
2318
2319 dmacfg = gem_readl(bp, DMACFG);
2320 if (features & NETIF_F_HW_CSUM)
2321 dmacfg |= GEM_BIT(TXCOEN);
2322 else
2323 dmacfg &= ~GEM_BIT(TXCOEN);
2324 gem_writel(bp, DMACFG, dmacfg);
2325 }
2326
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002327 /* RX checksum offload */
2328 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2329 u32 netcfg;
2330
2331 netcfg = gem_readl(bp, NCFGR);
2332 if (features & NETIF_F_RXCSUM &&
2333 !(netdev->flags & IFF_PROMISC))
2334 netcfg |= GEM_BIT(RXCOEN);
2335 else
2336 netcfg &= ~GEM_BIT(RXCOEN);
2337 gem_writel(bp, NCFGR, netcfg);
2338 }
2339
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002340 return 0;
2341}
2342
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002343static const struct net_device_ops macb_netdev_ops = {
2344 .ndo_open = macb_open,
2345 .ndo_stop = macb_close,
2346 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002347 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002348 .ndo_get_stats = macb_get_stats,
2349 .ndo_do_ioctl = macb_ioctl,
2350 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302351 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002352 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002353#ifdef CONFIG_NET_POLL_CONTROLLER
2354 .ndo_poll_controller = macb_poll_controller,
2355#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002356 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002357};
2358
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002359/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002360 * and integration options used
2361 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002362static void macb_configure_caps(struct macb *bp,
2363 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002364{
2365 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002366
Nicolas Ferref6970502015-03-31 15:02:01 +02002367 if (dt_conf)
2368 bp->caps = dt_conf->caps;
2369
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002370 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002371 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2372
Nicolas Ferree1755872014-07-24 13:50:58 +02002373 dcfg = gem_readl(bp, DCFG1);
2374 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2375 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2376 dcfg = gem_readl(bp, DCFG2);
2377 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2378 bp->caps |= MACB_CAPS_FIFO_MODE;
2379 }
2380
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002381 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002382}
2383
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002384static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002385 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002386 unsigned int *queue_mask,
2387 unsigned int *num_queues)
2388{
2389 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002390
2391 *queue_mask = 0x1;
2392 *num_queues = 1;
2393
Nicolas Ferreda120112015-03-31 15:02:00 +02002394 /* is it macb or gem ?
2395 *
2396 * We need to read directly from the hardware here because
2397 * we are early in the probe process and don't have the
2398 * MACB_CAPS_MACB_IS_GEM flag positioned
2399 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002400 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002401 return;
2402
2403 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302404 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2405
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002406 *queue_mask |= 0x1;
2407
2408 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2409 if (*queue_mask & (1 << hw_q))
2410 (*num_queues)++;
2411}
2412
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002413static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302414 struct clk **hclk, struct clk **tx_clk,
2415 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002416{
2417 int err;
2418
2419 *pclk = devm_clk_get(&pdev->dev, "pclk");
2420 if (IS_ERR(*pclk)) {
2421 err = PTR_ERR(*pclk);
2422 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2423 return err;
2424 }
2425
2426 *hclk = devm_clk_get(&pdev->dev, "hclk");
2427 if (IS_ERR(*hclk)) {
2428 err = PTR_ERR(*hclk);
2429 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2430 return err;
2431 }
2432
2433 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2434 if (IS_ERR(*tx_clk))
2435 *tx_clk = NULL;
2436
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302437 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2438 if (IS_ERR(*rx_clk))
2439 *rx_clk = NULL;
2440
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002441 err = clk_prepare_enable(*pclk);
2442 if (err) {
2443 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2444 return err;
2445 }
2446
2447 err = clk_prepare_enable(*hclk);
2448 if (err) {
2449 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2450 goto err_disable_pclk;
2451 }
2452
2453 err = clk_prepare_enable(*tx_clk);
2454 if (err) {
2455 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2456 goto err_disable_hclk;
2457 }
2458
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302459 err = clk_prepare_enable(*rx_clk);
2460 if (err) {
2461 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2462 goto err_disable_txclk;
2463 }
2464
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002465 return 0;
2466
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302467err_disable_txclk:
2468 clk_disable_unprepare(*tx_clk);
2469
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002470err_disable_hclk:
2471 clk_disable_unprepare(*hclk);
2472
2473err_disable_pclk:
2474 clk_disable_unprepare(*pclk);
2475
2476 return err;
2477}
2478
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002479static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002480{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002481 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002482 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002483 struct macb *bp = netdev_priv(dev);
2484 struct macb_queue *queue;
2485 int err;
2486 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002487
Zach Brownb410d132016-10-19 09:56:57 -05002488 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2489 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2490
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002491 /* set the queue register mapping once for all: queue0 has a special
2492 * register mapping but we don't want to test the queue index then
2493 * compute the corresponding register offset at run time.
2494 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002495 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002496 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002497 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002498
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002499 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002500 queue->bp = bp;
2501 if (hw_q) {
2502 queue->ISR = GEM_ISR(hw_q - 1);
2503 queue->IER = GEM_IER(hw_q - 1);
2504 queue->IDR = GEM_IDR(hw_q - 1);
2505 queue->IMR = GEM_IMR(hw_q - 1);
2506 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302507#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2508 queue->TBQPH = GEM_TBQPH(hw_q -1);
2509#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002510 } else {
2511 /* queue0 uses legacy registers */
2512 queue->ISR = MACB_ISR;
2513 queue->IER = MACB_IER;
2514 queue->IDR = MACB_IDR;
2515 queue->IMR = MACB_IMR;
2516 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302517#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2518 queue->TBQPH = MACB_TBQPH;
2519#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002520 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002521
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002522 /* get irq: here we use the linux queue index, not the hardware
2523 * queue index. the queue irq definitions in the device tree
2524 * must remove the optional gaps that could exist in the
2525 * hardware queue mask.
2526 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002527 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002528 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002529 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002530 if (err) {
2531 dev_err(&pdev->dev,
2532 "Unable to request IRQ %d (error %d)\n",
2533 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002534 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002535 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002536
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002537 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002538 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002539 }
2540
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002541 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002542 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002543
Nicolas Ferre4df95132013-06-04 21:57:12 +00002544 /* setup appropriated routines according to adapter type */
2545 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002546 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002547 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2548 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2549 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2550 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002551 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002552 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002553 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002554 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2555 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2556 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2557 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002558 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002559 }
2560
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002561 /* Set features */
2562 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002563 /* Checksum offload is only available on gem with packet buffer */
2564 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002565 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002566 if (bp->caps & MACB_CAPS_SG_DISABLED)
2567 dev->hw_features &= ~NETIF_F_SG;
2568 dev->features = dev->hw_features;
2569
Neil Armstrongce721a72016-01-05 14:39:16 +01002570 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2571 val = 0;
2572 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2573 val = GEM_BIT(RGMII);
2574 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002575 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002576 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002577 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002578 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002579
Neil Armstrongce721a72016-01-05 14:39:16 +01002580 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2581 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002582
Neil Armstrongce721a72016-01-05 14:39:16 +01002583 macb_or_gem_writel(bp, USRIO, val);
2584 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002585
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002586 /* Set MII management clock divider */
2587 val = macb_mdc_clk_div(bp);
2588 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302589 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2590 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002591 macb_writel(bp, NCFGR, val);
2592
2593 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002594}
2595
2596#if defined(CONFIG_OF)
2597/* 1518 rounded up */
2598#define AT91ETHER_MAX_RBUFF_SZ 0x600
2599/* max number of receive buffers */
2600#define AT91ETHER_MAX_RX_DESCR 9
2601
2602/* Initialize and start the Receiver and Transmit subsystems */
2603static int at91ether_start(struct net_device *dev)
2604{
2605 struct macb *lp = netdev_priv(dev);
2606 dma_addr_t addr;
2607 u32 ctl;
2608 int i;
2609
2610 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2611 (AT91ETHER_MAX_RX_DESCR *
2612 sizeof(struct macb_dma_desc)),
2613 &lp->rx_ring_dma, GFP_KERNEL);
2614 if (!lp->rx_ring)
2615 return -ENOMEM;
2616
2617 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2618 AT91ETHER_MAX_RX_DESCR *
2619 AT91ETHER_MAX_RBUFF_SZ,
2620 &lp->rx_buffers_dma, GFP_KERNEL);
2621 if (!lp->rx_buffers) {
2622 dma_free_coherent(&lp->pdev->dev,
2623 AT91ETHER_MAX_RX_DESCR *
2624 sizeof(struct macb_dma_desc),
2625 lp->rx_ring, lp->rx_ring_dma);
2626 lp->rx_ring = NULL;
2627 return -ENOMEM;
2628 }
2629
2630 addr = lp->rx_buffers_dma;
2631 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2632 lp->rx_ring[i].addr = addr;
2633 lp->rx_ring[i].ctrl = 0;
2634 addr += AT91ETHER_MAX_RBUFF_SZ;
2635 }
2636
2637 /* Set the Wrap bit on the last descriptor */
2638 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2639
2640 /* Reset buffer index */
2641 lp->rx_tail = 0;
2642
2643 /* Program address of descriptor list in Rx Buffer Queue register */
2644 macb_writel(lp, RBQP, lp->rx_ring_dma);
2645
2646 /* Enable Receive and Transmit */
2647 ctl = macb_readl(lp, NCR);
2648 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2649
2650 return 0;
2651}
2652
2653/* Open the ethernet interface */
2654static int at91ether_open(struct net_device *dev)
2655{
2656 struct macb *lp = netdev_priv(dev);
2657 u32 ctl;
2658 int ret;
2659
2660 /* Clear internal statistics */
2661 ctl = macb_readl(lp, NCR);
2662 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2663
2664 macb_set_hwaddr(lp);
2665
2666 ret = at91ether_start(dev);
2667 if (ret)
2668 return ret;
2669
2670 /* Enable MAC interrupts */
2671 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2672 MACB_BIT(RXUBR) |
2673 MACB_BIT(ISR_TUND) |
2674 MACB_BIT(ISR_RLE) |
2675 MACB_BIT(TCOMP) |
2676 MACB_BIT(ISR_ROVR) |
2677 MACB_BIT(HRESP));
2678
2679 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002680 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002681
2682 netif_start_queue(dev);
2683
2684 return 0;
2685}
2686
2687/* Close the interface */
2688static int at91ether_close(struct net_device *dev)
2689{
2690 struct macb *lp = netdev_priv(dev);
2691 u32 ctl;
2692
2693 /* Disable Receiver and Transmitter */
2694 ctl = macb_readl(lp, NCR);
2695 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2696
2697 /* Disable MAC interrupts */
2698 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2699 MACB_BIT(RXUBR) |
2700 MACB_BIT(ISR_TUND) |
2701 MACB_BIT(ISR_RLE) |
2702 MACB_BIT(TCOMP) |
2703 MACB_BIT(ISR_ROVR) |
2704 MACB_BIT(HRESP));
2705
2706 netif_stop_queue(dev);
2707
2708 dma_free_coherent(&lp->pdev->dev,
2709 AT91ETHER_MAX_RX_DESCR *
2710 sizeof(struct macb_dma_desc),
2711 lp->rx_ring, lp->rx_ring_dma);
2712 lp->rx_ring = NULL;
2713
2714 dma_free_coherent(&lp->pdev->dev,
2715 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2716 lp->rx_buffers, lp->rx_buffers_dma);
2717 lp->rx_buffers = NULL;
2718
2719 return 0;
2720}
2721
2722/* Transmit packet */
2723static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2724{
2725 struct macb *lp = netdev_priv(dev);
2726
2727 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2728 netif_stop_queue(dev);
2729
2730 /* Store packet information (to free when Tx completed) */
2731 lp->skb = skb;
2732 lp->skb_length = skb->len;
2733 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2734 DMA_TO_DEVICE);
2735
2736 /* Set address of the data in the Transmit Address register */
2737 macb_writel(lp, TAR, lp->skb_physaddr);
2738 /* Set length of the packet in the Transmit Control register */
2739 macb_writel(lp, TCR, skb->len);
2740
2741 } else {
2742 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2743 return NETDEV_TX_BUSY;
2744 }
2745
2746 return NETDEV_TX_OK;
2747}
2748
2749/* Extract received frame from buffer descriptors and sent to upper layers.
2750 * (Called from interrupt context)
2751 */
2752static void at91ether_rx(struct net_device *dev)
2753{
2754 struct macb *lp = netdev_priv(dev);
2755 unsigned char *p_recv;
2756 struct sk_buff *skb;
2757 unsigned int pktlen;
2758
2759 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2760 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2761 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2762 skb = netdev_alloc_skb(dev, pktlen + 2);
2763 if (skb) {
2764 skb_reserve(skb, 2);
2765 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2766
2767 skb->protocol = eth_type_trans(skb, dev);
2768 lp->stats.rx_packets++;
2769 lp->stats.rx_bytes += pktlen;
2770 netif_rx(skb);
2771 } else {
2772 lp->stats.rx_dropped++;
2773 }
2774
2775 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2776 lp->stats.multicast++;
2777
2778 /* reset ownership bit */
2779 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2780
2781 /* wrap after last buffer */
2782 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2783 lp->rx_tail = 0;
2784 else
2785 lp->rx_tail++;
2786 }
2787}
2788
2789/* MAC interrupt handler */
2790static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2791{
2792 struct net_device *dev = dev_id;
2793 struct macb *lp = netdev_priv(dev);
2794 u32 intstatus, ctl;
2795
2796 /* MAC Interrupt Status register indicates what interrupts are pending.
2797 * It is automatically cleared once read.
2798 */
2799 intstatus = macb_readl(lp, ISR);
2800
2801 /* Receive complete */
2802 if (intstatus & MACB_BIT(RCOMP))
2803 at91ether_rx(dev);
2804
2805 /* Transmit complete */
2806 if (intstatus & MACB_BIT(TCOMP)) {
2807 /* The TCOM bit is set even if the transmission failed */
2808 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2809 lp->stats.tx_errors++;
2810
2811 if (lp->skb) {
2812 dev_kfree_skb_irq(lp->skb);
2813 lp->skb = NULL;
2814 dma_unmap_single(NULL, lp->skb_physaddr,
2815 lp->skb_length, DMA_TO_DEVICE);
2816 lp->stats.tx_packets++;
2817 lp->stats.tx_bytes += lp->skb_length;
2818 }
2819 netif_wake_queue(dev);
2820 }
2821
2822 /* Work-around for EMAC Errata section 41.3.1 */
2823 if (intstatus & MACB_BIT(RXUBR)) {
2824 ctl = macb_readl(lp, NCR);
2825 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2826 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2827 }
2828
2829 if (intstatus & MACB_BIT(ISR_ROVR))
2830 netdev_err(dev, "ROVR error\n");
2831
2832 return IRQ_HANDLED;
2833}
2834
2835#ifdef CONFIG_NET_POLL_CONTROLLER
2836static void at91ether_poll_controller(struct net_device *dev)
2837{
2838 unsigned long flags;
2839
2840 local_irq_save(flags);
2841 at91ether_interrupt(dev->irq, dev);
2842 local_irq_restore(flags);
2843}
2844#endif
2845
2846static const struct net_device_ops at91ether_netdev_ops = {
2847 .ndo_open = at91ether_open,
2848 .ndo_stop = at91ether_close,
2849 .ndo_start_xmit = at91ether_start_xmit,
2850 .ndo_get_stats = macb_get_stats,
2851 .ndo_set_rx_mode = macb_set_rx_mode,
2852 .ndo_set_mac_address = eth_mac_addr,
2853 .ndo_do_ioctl = macb_ioctl,
2854 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002855#ifdef CONFIG_NET_POLL_CONTROLLER
2856 .ndo_poll_controller = at91ether_poll_controller,
2857#endif
2858};
2859
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002860static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302861 struct clk **hclk, struct clk **tx_clk,
2862 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002863{
2864 int err;
2865
2866 *hclk = NULL;
2867 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302868 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002869
2870 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2871 if (IS_ERR(*pclk))
2872 return PTR_ERR(*pclk);
2873
2874 err = clk_prepare_enable(*pclk);
2875 if (err) {
2876 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2877 return err;
2878 }
2879
2880 return 0;
2881}
2882
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002883static int at91ether_init(struct platform_device *pdev)
2884{
2885 struct net_device *dev = platform_get_drvdata(pdev);
2886 struct macb *bp = netdev_priv(dev);
2887 int err;
2888 u32 reg;
2889
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002890 dev->netdev_ops = &at91ether_netdev_ops;
2891 dev->ethtool_ops = &macb_ethtool_ops;
2892
2893 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2894 0, dev->name, dev);
2895 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002896 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002897
2898 macb_writel(bp, NCR, 0);
2899
2900 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2901 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2902 reg |= MACB_BIT(RM9200_RMII);
2903
2904 macb_writel(bp, NCFGR, reg);
2905
2906 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002907}
2908
David S. Miller3cef5c52015-03-09 23:38:02 -04002909static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002910 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002911 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002912 .init = macb_init,
2913};
2914
David S. Miller3cef5c52015-03-09 23:38:02 -04002915static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002916 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2917 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002918 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002919 .init = macb_init,
2920};
2921
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002922static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002923 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002924 .dma_burst_length = 16,
2925 .clk_init = macb_clk_init,
2926 .init = macb_init,
2927};
2928
David S. Miller3cef5c52015-03-09 23:38:02 -04002929static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002930 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
2931 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002932 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002933 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002934 .init = macb_init,
2935};
2936
David S. Miller3cef5c52015-03-09 23:38:02 -04002937static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002938 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002939 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002940 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002941 .init = macb_init,
2942};
2943
David S. Miller3cef5c52015-03-09 23:38:02 -04002944static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002945 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002946 .init = at91ether_init,
2947};
2948
Neil Armstronge611b5b2016-01-05 14:39:17 +01002949static const struct macb_config np4_config = {
2950 .caps = MACB_CAPS_USRIO_DISABLED,
2951 .clk_init = macb_clk_init,
2952 .init = macb_init,
2953};
David S. Miller36583eb2015-05-23 01:22:35 -04002954
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302955static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302956 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302957 .dma_burst_length = 16,
2958 .clk_init = macb_clk_init,
2959 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302960 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302961};
2962
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002963static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302964 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002965 .dma_burst_length = 16,
2966 .clk_init = macb_clk_init,
2967 .init = macb_init,
2968};
2969
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002970static const struct of_device_id macb_dt_ids[] = {
2971 { .compatible = "cdns,at32ap7000-macb" },
2972 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2973 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01002974 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002975 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2976 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002977 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002978 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2979 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2980 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2981 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302982 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002983 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002984 { /* sentinel */ }
2985};
2986MODULE_DEVICE_TABLE(of, macb_dt_ids);
2987#endif /* CONFIG_OF */
2988
2989static int macb_probe(struct platform_device *pdev)
2990{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002991 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302992 struct clk **, struct clk **, struct clk **)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002993 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002994 int (*init)(struct platform_device *) = macb_init;
2995 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002996 struct device_node *phy_node;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002997 const struct macb_config *macb_config = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302998 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002999 unsigned int queue_mask, num_queues;
3000 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003001 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003002 struct phy_device *phydev;
3003 struct net_device *dev;
3004 struct resource *regs;
3005 void __iomem *mem;
3006 const char *mac;
3007 struct macb *bp;
3008 int err;
3009
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003010 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3011 mem = devm_ioremap_resource(&pdev->dev, regs);
3012 if (IS_ERR(mem))
3013 return PTR_ERR(mem);
3014
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003015 if (np) {
3016 const struct of_device_id *match;
3017
3018 match = of_match_node(macb_dt_ids, np);
3019 if (match && match->data) {
3020 macb_config = match->data;
3021 clk_init = macb_config->clk_init;
3022 init = macb_config->init;
3023 }
3024 }
3025
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303026 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003027 if (err)
3028 return err;
3029
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003030 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003031
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003032 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003033 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003034 if (!dev) {
3035 err = -ENOMEM;
3036 goto err_disable_clocks;
3037 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003038
3039 dev->base_addr = regs->start;
3040
3041 SET_NETDEV_DEV(dev, &pdev->dev);
3042
3043 bp = netdev_priv(dev);
3044 bp->pdev = pdev;
3045 bp->dev = dev;
3046 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003047 bp->native_io = native_io;
3048 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07003049 bp->macb_reg_readl = hw_readl_native;
3050 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003051 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003052 bp->macb_reg_readl = hw_readl;
3053 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03003054 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003055 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003056 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003057 if (macb_config)
3058 bp->dma_burst_length = macb_config->dma_burst_length;
3059 bp->pclk = pclk;
3060 bp->hclk = hclk;
3061 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303062 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003063 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303064 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303065
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003066 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003067 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003068 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3069 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3070
Harini Katakamfff80192016-08-09 13:15:53 +05303071#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3072 if (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1)) > GEM_DBW32)
3073 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3074#endif
3075
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003076 spin_lock_init(&bp->lock);
3077
Nicolas Ferread783472015-03-31 15:02:02 +02003078 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003079 macb_configure_caps(bp, macb_config);
3080
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003081 platform_set_drvdata(pdev, dev);
3082
3083 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003084 if (dev->irq < 0) {
3085 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003086 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003087 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003088
Jarod Wilson44770e12016-10-17 15:54:17 -04003089 /* MTU range: 68 - 1500 or 10240 */
3090 dev->min_mtu = GEM_MTU_MIN_SIZE;
3091 if (bp->caps & MACB_CAPS_JUMBO)
3092 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3093 else
3094 dev->max_mtu = ETH_DATA_LEN;
3095
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003096 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003097 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003098 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003099 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003100 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003101
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003102 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003103 phy_node = of_get_next_available_child(np, NULL);
3104 if (phy_node) {
3105 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003106
Charles Keepax0e3e7992016-03-28 13:47:42 +01003107 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003108 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003109 gpiod_direction_output(bp->reset_gpio, 1);
3110 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003111 }
3112 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003113
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003114 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003115 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003116 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003117 if (pdata && pdata->is_rmii)
3118 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3119 else
3120 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3121 } else {
3122 bp->phy_interface = err;
3123 }
3124
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003125 /* IP specific init */
3126 err = init(pdev);
3127 if (err)
3128 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003129
Florian Fainellicf669662016-05-02 18:38:45 -07003130 err = macb_mii_init(bp);
3131 if (err)
3132 goto err_out_free_netdev;
3133
Philippe Reynes0a912812016-06-22 00:32:35 +02003134 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003135
3136 netif_carrier_off(dev);
3137
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003138 err = register_netdev(dev);
3139 if (err) {
3140 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003141 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003142 }
3143
Florian Fainellicf669662016-05-02 18:38:45 -07003144 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003145
Bo Shen58798232014-09-13 01:57:49 +02003146 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3147 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3148 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003149
3150 return 0;
3151
Florian Fainellicf669662016-05-02 18:38:45 -07003152err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003153 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003154 mdiobus_unregister(bp->mii_bus);
3155 mdiobus_free(bp->mii_bus);
3156
3157 /* Shutdown the PHY if there is a GPIO reset */
3158 if (bp->reset_gpio)
3159 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003160
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003161err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003162 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003163
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003164err_disable_clocks:
3165 clk_disable_unprepare(tx_clk);
3166 clk_disable_unprepare(hclk);
3167 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303168 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003169
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003170 return err;
3171}
3172
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003173static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003174{
3175 struct net_device *dev;
3176 struct macb *bp;
3177
3178 dev = platform_get_drvdata(pdev);
3179
3180 if (dev) {
3181 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003182 if (dev->phydev)
3183 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003184 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003185 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003186 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003187
3188 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003189 if (bp->reset_gpio)
3190 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003191
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003192 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003193 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003194 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003195 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303196 clk_disable_unprepare(bp->rx_clk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003197 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003198 }
3199
3200 return 0;
3201}
3202
Michal Simekd23823d2015-01-23 09:36:03 +01003203static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003204{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003205 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003206 struct net_device *netdev = platform_get_drvdata(pdev);
3207 struct macb *bp = netdev_priv(netdev);
3208
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003209 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003210 netif_device_detach(netdev);
3211
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003212 if (bp->wol & MACB_WOL_ENABLED) {
3213 macb_writel(bp, IER, MACB_BIT(WOL));
3214 macb_writel(bp, WOL, MACB_BIT(MAG));
3215 enable_irq_wake(bp->queues[0].irq);
3216 } else {
3217 clk_disable_unprepare(bp->tx_clk);
3218 clk_disable_unprepare(bp->hclk);
3219 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303220 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003221 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003222
3223 return 0;
3224}
3225
Michal Simekd23823d2015-01-23 09:36:03 +01003226static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003227{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003228 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003229 struct net_device *netdev = platform_get_drvdata(pdev);
3230 struct macb *bp = netdev_priv(netdev);
3231
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003232 if (bp->wol & MACB_WOL_ENABLED) {
3233 macb_writel(bp, IDR, MACB_BIT(WOL));
3234 macb_writel(bp, WOL, 0);
3235 disable_irq_wake(bp->queues[0].irq);
3236 } else {
3237 clk_prepare_enable(bp->pclk);
3238 clk_prepare_enable(bp->hclk);
3239 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303240 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003241 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003242
3243 netif_device_attach(netdev);
3244
3245 return 0;
3246}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003247
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003248static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3249
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003250static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003251 .probe = macb_probe,
3252 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003253 .driver = {
3254 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003255 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003256 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003257 },
3258};
3259
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003260module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003261
3262MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003263MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003264MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003265MODULE_ALIAS("platform:macb");