blob: 644975da2857e8b92b34a8df67d1e0061eed6e53 [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 Brownb410d132016-10-19 09:56:57 -050040#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
41#define RX_RING_BYTES(bp) (sizeof(struct macb_dma_desc) \
42 * (bp)->rx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010043
Zach Brownb410d132016-10-19 09:56:57 -050044#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
45#define TX_RING_BYTES(bp) (sizeof(struct macb_dma_desc) \
46 * (bp)->tx_ring_size)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010047
Nicolas Ferre909a8582012-11-19 06:00:21 +000048/* level of occupied TX descriptors under which we wake up TX process */
Zach Brownb410d132016-10-19 09:56:57 -050049#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010050
51#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
52 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000053#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
54 | MACB_BIT(ISR_RLE) \
55 | MACB_BIT(TXERR))
56#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
57
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020058#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
59#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
60
Jarod Wilson44770e12016-10-17 15:54:17 -040061#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
Harini Katakama5898ea2015-05-06 22:27:18 +053062
Sergio Prado3e2a5e12016-02-09 12:07:16 -020063#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
64#define MACB_WOL_ENABLED (0x1 << 1)
65
Moritz Fischer64ec42f2016-03-29 19:11:12 -070066/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000067 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
68 */
69#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010070
Havard Skinnemoen55054a12012-10-31 06:04:55 +000071/* Ring buffer accessors */
Zach Brownb410d132016-10-19 09:56:57 -050072static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000073{
Zach Brownb410d132016-10-19 09:56:57 -050074 return index & (bp->tx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +000075}
76
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010077static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
78 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000079{
Zach Brownb410d132016-10-19 09:56:57 -050080 return &queue->tx_ring[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000081}
82
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010083static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
84 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000085{
Zach Brownb410d132016-10-19 09:56:57 -050086 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000087}
88
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010089static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000090{
91 dma_addr_t offset;
92
Zach Brownb410d132016-10-19 09:56:57 -050093 offset = macb_tx_ring_wrap(queue->bp, index) *
94 sizeof(struct macb_dma_desc);
Havard Skinnemoen55054a12012-10-31 06:04:55 +000095
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010096 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +000097}
98
Zach Brownb410d132016-10-19 09:56:57 -050099static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000100{
Zach Brownb410d132016-10-19 09:56:57 -0500101 return index & (bp->rx_ring_size - 1);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000102}
103
104static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
105{
Zach Brownb410d132016-10-19 09:56:57 -0500106 return &bp->rx_ring[macb_rx_ring_wrap(bp, index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000107}
108
109static void *macb_rx_buffer(struct macb *bp, unsigned int index)
110{
Zach Brownb410d132016-10-19 09:56:57 -0500111 return bp->rx_buffers + bp->rx_buffer_size *
112 macb_rx_ring_wrap(bp, index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000113}
114
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300115/* I/O accessors */
116static u32 hw_readl_native(struct macb *bp, int offset)
117{
118 return __raw_readl(bp->regs + offset);
119}
120
121static void hw_writel_native(struct macb *bp, int offset, u32 value)
122{
123 __raw_writel(value, bp->regs + offset);
124}
125
126static u32 hw_readl(struct macb *bp, int offset)
127{
128 return readl_relaxed(bp->regs + offset);
129}
130
131static void hw_writel(struct macb *bp, int offset, u32 value)
132{
133 writel_relaxed(value, bp->regs + offset);
134}
135
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700136/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700137 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300138 * descriptor access.
139 */
140static bool hw_is_native_io(void __iomem *addr)
141{
142 u32 value = MACB_BIT(LLB);
143
144 __raw_writel(value, addr + MACB_NCR);
145 value = __raw_readl(addr + MACB_NCR);
146
147 /* Write 0 back to disable everything */
148 __raw_writel(0, addr + MACB_NCR);
149
150 return value == MACB_BIT(LLB);
151}
152
153static bool hw_is_gem(void __iomem *addr, bool native_io)
154{
155 u32 id;
156
157 if (native_io)
158 id = __raw_readl(addr + MACB_MID);
159 else
160 id = readl_relaxed(addr + MACB_MID);
161
162 return MACB_BFEXT(IDNUM, id) >= 0x2;
163}
164
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100165static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100166{
167 u32 bottom;
168 u16 top;
169
170 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000171 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100172 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000173 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000174
175 /* Clear unused address register sets */
176 macb_or_gem_writel(bp, SA2B, 0);
177 macb_or_gem_writel(bp, SA2T, 0);
178 macb_or_gem_writel(bp, SA3B, 0);
179 macb_or_gem_writel(bp, SA3T, 0);
180 macb_or_gem_writel(bp, SA4B, 0);
181 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100182}
183
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100184static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100185{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000186 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100187 u32 bottom;
188 u16 top;
189 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000190 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100191
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900192 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000193
Moritz Fischeraa50b552016-03-29 19:11:13 -0700194 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000195 for (i = 0; i < 4; i++) {
196 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
197 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100198
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000199 if (pdata && pdata->rev_eth_addr) {
200 addr[5] = bottom & 0xff;
201 addr[4] = (bottom >> 8) & 0xff;
202 addr[3] = (bottom >> 16) & 0xff;
203 addr[2] = (bottom >> 24) & 0xff;
204 addr[1] = top & 0xff;
205 addr[0] = (top & 0xff00) >> 8;
206 } else {
207 addr[0] = bottom & 0xff;
208 addr[1] = (bottom >> 8) & 0xff;
209 addr[2] = (bottom >> 16) & 0xff;
210 addr[3] = (bottom >> 24) & 0xff;
211 addr[4] = top & 0xff;
212 addr[5] = (top >> 8) & 0xff;
213 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100214
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000215 if (is_valid_ether_addr(addr)) {
216 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
217 return;
218 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700219 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000220
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300221 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000222 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100223}
224
frederic RODO6c36a702007-07-12 19:07:24 +0200225static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100226{
frederic RODO6c36a702007-07-12 19:07:24 +0200227 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100228 int value;
229
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100230 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
231 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200232 | MACB_BF(PHYA, mii_id)
233 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100234 | MACB_BF(CODE, MACB_MAN_CODE)));
235
frederic RODO6c36a702007-07-12 19:07:24 +0200236 /* wait for end of transfer */
237 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
238 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100239
240 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100241
242 return value;
243}
244
frederic RODO6c36a702007-07-12 19:07:24 +0200245static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
246 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100247{
frederic RODO6c36a702007-07-12 19:07:24 +0200248 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100249
250 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
251 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200252 | MACB_BF(PHYA, mii_id)
253 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100254 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200255 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100256
frederic RODO6c36a702007-07-12 19:07:24 +0200257 /* wait for end of transfer */
258 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
259 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100260
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100261 return 0;
262}
263
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800264/**
265 * macb_set_tx_clk() - Set a clock to a new frequency
266 * @clk Pointer to the clock to change
267 * @rate New frequency in Hz
268 * @dev Pointer to the struct net_device
269 */
270static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
271{
272 long ferr, rate, rate_rounded;
273
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100274 if (!clk)
275 return;
276
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800277 switch (speed) {
278 case SPEED_10:
279 rate = 2500000;
280 break;
281 case SPEED_100:
282 rate = 25000000;
283 break;
284 case SPEED_1000:
285 rate = 125000000;
286 break;
287 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800288 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800289 }
290
291 rate_rounded = clk_round_rate(clk, rate);
292 if (rate_rounded < 0)
293 return;
294
295 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
296 * is not satisfied.
297 */
298 ferr = abs(rate_rounded - rate);
299 ferr = DIV_ROUND_UP(ferr, rate / 100000);
300 if (ferr > 5)
301 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700302 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800303
304 if (clk_set_rate(clk, rate_rounded))
305 netdev_err(dev, "adjusting tx_clk failed.\n");
306}
307
frederic RODO6c36a702007-07-12 19:07:24 +0200308static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100309{
frederic RODO6c36a702007-07-12 19:07:24 +0200310 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200311 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200312 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200313 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100314
frederic RODO6c36a702007-07-12 19:07:24 +0200315 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100316
frederic RODO6c36a702007-07-12 19:07:24 +0200317 if (phydev->link) {
318 if ((bp->speed != phydev->speed) ||
319 (bp->duplex != phydev->duplex)) {
320 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100321
frederic RODO6c36a702007-07-12 19:07:24 +0200322 reg = macb_readl(bp, NCFGR);
323 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000324 if (macb_is_gem(bp))
325 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200326
327 if (phydev->duplex)
328 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900329 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200330 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200331 if (phydev->speed == SPEED_1000 &&
332 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000333 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200334
Patrice Vilchez140b7552012-10-31 06:04:50 +0000335 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200336
337 bp->speed = phydev->speed;
338 bp->duplex = phydev->duplex;
339 status_change = 1;
340 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100341 }
342
frederic RODO6c36a702007-07-12 19:07:24 +0200343 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700344 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200345 bp->speed = 0;
346 bp->duplex = -1;
347 }
348 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100349
frederic RODO6c36a702007-07-12 19:07:24 +0200350 status_change = 1;
351 }
352
353 spin_unlock_irqrestore(&bp->lock, flags);
354
355 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000356 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500357 /* Update the TX clock rate if and only if the link is
358 * up and there has been a link change.
359 */
360 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
361
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000362 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000363 netdev_info(dev, "link up (%d/%s)\n",
364 phydev->speed,
365 phydev->duplex == DUPLEX_FULL ?
366 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000367 } else {
368 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000369 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000370 }
frederic RODO6c36a702007-07-12 19:07:24 +0200371 }
372}
373
374/* based on au1000_eth. c*/
375static int macb_mii_probe(struct net_device *dev)
376{
377 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000378 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000379 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000380 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000381 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200382
Jiri Pirko7455a762010-02-08 05:12:08 +0000383 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200384 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000385 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200386 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200387 }
388
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000389 pdata = dev_get_platdata(&bp->pdev->dev);
390 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700391 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin,
392 "phy int");
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000393 if (!ret) {
394 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
395 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
396 }
397 }
frederic RODO6c36a702007-07-12 19:07:24 +0200398
399 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000400 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100401 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000402 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000403 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000404 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200405 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100406
frederic RODO6c36a702007-07-12 19:07:24 +0200407 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200408 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000409 phydev->supported &= PHY_GBIT_FEATURES;
410 else
411 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100412
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500413 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
414 phydev->supported &= ~SUPPORTED_1000baseT_Half;
415
frederic RODO6c36a702007-07-12 19:07:24 +0200416 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100417
frederic RODO6c36a702007-07-12 19:07:24 +0200418 bp->link = 0;
419 bp->speed = 0;
420 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200421
422 return 0;
423}
424
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100425static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200426{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000427 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200428 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200429 int err = -ENXIO, i;
430
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200431 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200432 macb_writel(bp, NCR, MACB_BIT(MPE));
433
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700434 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700435 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200436 err = -ENOMEM;
437 goto err_out;
438 }
439
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700440 bp->mii_bus->name = "MACB_mii_bus";
441 bp->mii_bus->read = &macb_mdio_read;
442 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000443 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700444 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700445 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700446 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900447 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700448
Jamie Iles91523942011-02-28 04:05:25 +0000449 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200450
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200451 np = bp->pdev->dev.of_node;
452 if (np) {
453 /* try dt phy registration */
454 err = of_mdiobus_register(bp->mii_bus, np);
455
456 /* fallback to standard phy registration if no phy were
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700457 * found during dt phy registration
458 */
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200459 if (!err && !phy_find_first(bp->mii_bus)) {
460 for (i = 0; i < PHY_MAX_ADDR; i++) {
461 struct phy_device *phydev;
462
463 phydev = mdiobus_scan(bp->mii_bus, i);
Sergei Shtylyovce24c2b2016-05-01 01:47:36 +0300464 if (IS_ERR(phydev) &&
465 PTR_ERR(phydev) != -ENODEV) {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200466 err = PTR_ERR(phydev);
467 break;
468 }
469 }
470
471 if (err)
472 goto err_out_unregister_bus;
473 }
474 } else {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200475 if (pdata)
476 bp->mii_bus->phy_mask = pdata->phy_mask;
477
478 err = mdiobus_register(bp->mii_bus);
479 }
480
481 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100482 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200483
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200484 err = macb_mii_probe(bp->dev);
485 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200486 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200487
488 return 0;
489
490err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700491 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700492err_out_free_mdiobus:
493 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200494err_out:
495 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100496}
497
498static void macb_update_stats(struct macb *bp)
499{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000500 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
501 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +0300502 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100503
504 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
505
Moritz Fischer96ec6312016-03-29 19:11:11 -0700506 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700507 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100508}
509
Nicolas Ferree86cd532012-10-31 06:04:57 +0000510static int macb_halt_tx(struct macb *bp)
511{
512 unsigned long halt_time, timeout;
513 u32 status;
514
515 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
516
517 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
518 do {
519 halt_time = jiffies;
520 status = macb_readl(bp, TSR);
521 if (!(status & MACB_BIT(TGO)))
522 return 0;
523
524 usleep_range(10, 250);
525 } while (time_before(halt_time, timeout));
526
527 return -ETIMEDOUT;
528}
529
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200530static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
531{
532 if (tx_skb->mapping) {
533 if (tx_skb->mapped_as_page)
534 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
535 tx_skb->size, DMA_TO_DEVICE);
536 else
537 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
538 tx_skb->size, DMA_TO_DEVICE);
539 tx_skb->mapping = 0;
540 }
541
542 if (tx_skb->skb) {
543 dev_kfree_skb_any(tx_skb->skb);
544 tx_skb->skb = NULL;
545 }
546}
547
Harini Katakamfff80192016-08-09 13:15:53 +0530548static inline void macb_set_addr(struct macb_dma_desc *desc, dma_addr_t addr)
549{
550 desc->addr = (u32)addr;
551#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
552 desc->addrh = (u32)(addr >> 32);
553#endif
554}
555
Nicolas Ferree86cd532012-10-31 06:04:57 +0000556static void macb_tx_error_task(struct work_struct *work)
557{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100558 struct macb_queue *queue = container_of(work, struct macb_queue,
559 tx_error_task);
560 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000561 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100562 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000563 struct sk_buff *skb;
564 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100565 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000566
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100567 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
568 (unsigned int)(queue - bp->queues),
569 queue->tx_tail, queue->tx_head);
570
571 /* Prevent the queue IRQ handlers from running: each of them may call
572 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
573 * As explained below, we have to halt the transmission before updating
574 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
575 * network engine about the macb/gem being halted.
576 */
577 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000578
579 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100580 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000581
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700582 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000583 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100584 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000585 */
586 if (macb_halt_tx(bp))
587 /* Just complain for now, reinitializing TX path can be good */
588 netdev_err(bp->dev, "BUG: halt tx timed out\n");
589
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700590 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000591 * Free transmit buffers in upper layer.
592 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100593 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
594 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000595
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100596 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000597 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100598 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000599 skb = tx_skb->skb;
600
601 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200602 /* skb is set for the last buffer of the frame */
603 while (!skb) {
604 macb_tx_unmap(bp, tx_skb);
605 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100606 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200607 skb = tx_skb->skb;
608 }
609
610 /* ctrl still refers to the first buffer descriptor
611 * since it's the only one written back by the hardware
612 */
613 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
614 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500615 macb_tx_ring_wrap(bp, tail),
616 skb->data);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200617 bp->stats.tx_packets++;
618 bp->stats.tx_bytes += skb->len;
619 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000620 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700621 /* "Buffers exhausted mid-frame" errors may only happen
622 * if the driver is buggy, so complain loudly about
623 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000624 */
625 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
626 netdev_err(bp->dev,
627 "BUG: TX buffers exhausted mid-frame\n");
628
629 desc->ctrl = ctrl | MACB_BIT(TX_USED);
630 }
631
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200632 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000633 }
634
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100635 /* Set end of TX queue */
636 desc = macb_tx_desc(queue, 0);
Harini Katakamfff80192016-08-09 13:15:53 +0530637 macb_set_addr(desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100638 desc->ctrl = MACB_BIT(TX_USED);
639
Nicolas Ferree86cd532012-10-31 06:04:57 +0000640 /* Make descriptor updates visible to hardware */
641 wmb();
642
643 /* Reinitialize the TX desc queue */
Harini Katakamfff80192016-08-09 13:15:53 +0530644 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
645#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
646 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
647#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000648 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100649 queue->tx_head = 0;
650 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000651
652 /* Housework before enabling TX IRQ */
653 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100654 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
655
656 /* Now we are ready to start transmission again */
657 netif_tx_start_all_queues(bp->dev);
658 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
659
660 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000661}
662
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100663static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100664{
665 unsigned int tail;
666 unsigned int head;
667 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100668 struct macb *bp = queue->bp;
669 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100670
671 status = macb_readl(bp, TSR);
672 macb_writel(bp, TSR, status);
673
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000674 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100675 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000676
Nicolas Ferree86cd532012-10-31 06:04:57 +0000677 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700678 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100679
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100680 head = queue->tx_head;
681 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000682 struct macb_tx_skb *tx_skb;
683 struct sk_buff *skb;
684 struct macb_dma_desc *desc;
685 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100686
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100687 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100688
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000689 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100690 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000691
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000692 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100693
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200694 /* TX_USED bit is only set by hardware on the very first buffer
695 * descriptor of the transmitted frame.
696 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000697 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100698 break;
699
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200700 /* Process all buffers of the current transmitted frame */
701 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100702 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200703 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000704
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200705 /* First, update TX stats if needed */
706 if (skb) {
707 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Zach Brownb410d132016-10-19 09:56:57 -0500708 macb_tx_ring_wrap(bp, tail),
709 skb->data);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200710 bp->stats.tx_packets++;
711 bp->stats.tx_bytes += skb->len;
712 }
713
714 /* Now we can safely release resources */
715 macb_tx_unmap(bp, tx_skb);
716
717 /* skb is set only for the last buffer of the frame.
718 * WARNING: at this point skb has been freed by
719 * macb_tx_unmap().
720 */
721 if (skb)
722 break;
723 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100724 }
725
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100726 queue->tx_tail = tail;
727 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
728 CIRC_CNT(queue->tx_head, queue->tx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500729 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100730 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100731}
732
Nicolas Ferre4df95132013-06-04 21:57:12 +0000733static void gem_rx_refill(struct macb *bp)
734{
735 unsigned int entry;
736 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000737 dma_addr_t paddr;
738
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700739 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
Zach Brownb410d132016-10-19 09:56:57 -0500740 bp->rx_ring_size) > 0) {
741 entry = macb_rx_ring_wrap(bp, bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000742
743 /* Make hw descriptor updates visible to CPU */
744 rmb();
745
Nicolas Ferre4df95132013-06-04 21:57:12 +0000746 bp->rx_prepared_head++;
747
Moritz Fischeraa50b552016-03-29 19:11:13 -0700748 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000749 /* allocate sk_buff for this free entry in ring */
750 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700751 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000752 netdev_err(bp->dev,
753 "Unable to allocate sk_buff\n");
754 break;
755 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000756
757 /* now fill corresponding descriptor entry */
758 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700759 bp->rx_buffer_size,
760 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800761 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
762 dev_kfree_skb(skb);
763 break;
764 }
765
766 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000767
Zach Brownb410d132016-10-19 09:56:57 -0500768 if (entry == bp->rx_ring_size - 1)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000769 paddr |= MACB_BIT(RX_WRAP);
Harini Katakamfff80192016-08-09 13:15:53 +0530770 macb_set_addr(&(bp->rx_ring[entry]), paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000771 bp->rx_ring[entry].ctrl = 0;
772
773 /* properly align Ethernet header */
774 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530775 } else {
776 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
777 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000778 }
779 }
780
781 /* Make descriptor updates visible to hardware */
782 wmb();
783
784 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700785 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000786}
787
788/* Mark DMA descriptors from begin up to and not including end as unused */
789static void discard_partial_frame(struct macb *bp, unsigned int begin,
790 unsigned int end)
791{
792 unsigned int frag;
793
794 for (frag = begin; frag != end; frag++) {
795 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700796
Nicolas Ferre4df95132013-06-04 21:57:12 +0000797 desc->addr &= ~MACB_BIT(RX_USED);
798 }
799
800 /* Make descriptor updates visible to hardware */
801 wmb();
802
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700803 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000804 * whatever caused this is updated, so we don't have to record
805 * anything.
806 */
807}
808
809static int gem_rx(struct macb *bp, int budget)
810{
811 unsigned int len;
812 unsigned int entry;
813 struct sk_buff *skb;
814 struct macb_dma_desc *desc;
815 int count = 0;
816
817 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530818 u32 ctrl;
819 dma_addr_t addr;
820 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000821
Zach Brownb410d132016-10-19 09:56:57 -0500822 entry = macb_rx_ring_wrap(bp, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000823 desc = &bp->rx_ring[entry];
824
825 /* Make hw descriptor updates visible to CPU */
826 rmb();
827
Harini Katakamfff80192016-08-09 13:15:53 +0530828 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
829 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
830#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
831 addr |= ((u64)(desc->addrh) << 32);
832#endif
Nicolas Ferre4df95132013-06-04 21:57:12 +0000833 ctrl = desc->ctrl;
834
Harini Katakamfff80192016-08-09 13:15:53 +0530835 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000836 break;
837
Nicolas Ferre4df95132013-06-04 21:57:12 +0000838 bp->rx_tail++;
839 count++;
840
841 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
842 netdev_err(bp->dev,
843 "not whole frame pointed by descriptor\n");
844 bp->stats.rx_dropped++;
845 break;
846 }
847 skb = bp->rx_skbuff[entry];
848 if (unlikely(!skb)) {
849 netdev_err(bp->dev,
850 "inconsistent Rx descriptor chain\n");
851 bp->stats.rx_dropped++;
852 break;
853 }
854 /* now everything is ready for receiving packet */
855 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530856 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000857
858 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
859
860 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000861 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800862 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000863
864 skb->protocol = eth_type_trans(skb, bp->dev);
865 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200866 if (bp->dev->features & NETIF_F_RXCSUM &&
867 !(bp->dev->flags & IFF_PROMISC) &&
868 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
869 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000870
871 bp->stats.rx_packets++;
872 bp->stats.rx_bytes += skb->len;
873
874#if defined(DEBUG) && defined(VERBOSE_DEBUG)
875 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
876 skb->len, skb->csum);
877 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100878 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000879 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
880 skb->data, 32, true);
881#endif
882
883 netif_receive_skb(skb);
884 }
885
886 gem_rx_refill(bp);
887
888 return count;
889}
890
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100891static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
892 unsigned int last_frag)
893{
894 unsigned int len;
895 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000896 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100897 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000898 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100899
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000900 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530901 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100902
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000903 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Zach Brownb410d132016-10-19 09:56:57 -0500904 macb_rx_ring_wrap(bp, first_frag),
905 macb_rx_ring_wrap(bp, last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100906
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700907 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000908 * first buffer. Since the header is 14 bytes, this makes the
909 * payload word-aligned.
910 *
911 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
912 * the two padding bytes into the skb so that we avoid hitting
913 * the slowpath in memcpy(), and pull them off afterwards.
914 */
915 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100916 if (!skb) {
917 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000918 for (frag = first_frag; ; frag++) {
919 desc = macb_rx_desc(bp, frag);
920 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100921 if (frag == last_frag)
922 break;
923 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000924
925 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100926 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000927
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100928 return 1;
929 }
930
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000931 offset = 0;
932 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700933 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100934 skb_put(skb, len);
935
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000936 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000937 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100938
939 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100940 if (unlikely(frag != last_frag)) {
941 dev_kfree_skb_any(skb);
942 return -1;
943 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100944 frag_len = len - offset;
945 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300946 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -0700947 macb_rx_buffer(bp, frag),
948 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000949 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000950 desc = macb_rx_desc(bp, frag);
951 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100952
953 if (frag == last_frag)
954 break;
955 }
956
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000957 /* Make descriptor updates visible to hardware */
958 wmb();
959
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000960 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100961 skb->protocol = eth_type_trans(skb, bp->dev);
962
963 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000964 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000965 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700966 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100967 netif_receive_skb(skb);
968
969 return 0;
970}
971
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100972static inline void macb_init_rx_ring(struct macb *bp)
973{
974 dma_addr_t addr;
975 int i;
976
977 addr = bp->rx_buffers_dma;
Zach Brownb410d132016-10-19 09:56:57 -0500978 for (i = 0; i < bp->rx_ring_size; i++) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100979 bp->rx_ring[i].addr = addr;
980 bp->rx_ring[i].ctrl = 0;
981 addr += bp->rx_buffer_size;
982 }
Zach Brownb410d132016-10-19 09:56:57 -0500983 bp->rx_ring[bp->rx_ring_size - 1].addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100984}
985
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100986static int macb_rx(struct macb *bp, int budget)
987{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100988 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100989 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000990 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100991 int first_frag = -1;
992
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000993 for (tail = bp->rx_tail; budget > 0; tail++) {
994 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100995 u32 addr, ctrl;
996
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000997 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100998 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000999
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001000 addr = desc->addr;
1001 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001002
1003 if (!(addr & MACB_BIT(RX_USED)))
1004 break;
1005
1006 if (ctrl & MACB_BIT(RX_SOF)) {
1007 if (first_frag != -1)
1008 discard_partial_frame(bp, first_frag, tail);
1009 first_frag = tail;
1010 }
1011
1012 if (ctrl & MACB_BIT(RX_EOF)) {
1013 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001014
1015 if (unlikely(first_frag == -1)) {
1016 reset_rx_queue = true;
1017 continue;
1018 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001019
1020 dropped = macb_rx_frame(bp, first_frag, tail);
1021 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001022 if (unlikely(dropped < 0)) {
1023 reset_rx_queue = true;
1024 continue;
1025 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001026 if (!dropped) {
1027 received++;
1028 budget--;
1029 }
1030 }
1031 }
1032
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001033 if (unlikely(reset_rx_queue)) {
1034 unsigned long flags;
1035 u32 ctrl;
1036
1037 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1038
1039 spin_lock_irqsave(&bp->lock, flags);
1040
1041 ctrl = macb_readl(bp, NCR);
1042 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1043
1044 macb_init_rx_ring(bp);
1045 macb_writel(bp, RBQP, bp->rx_ring_dma);
1046
1047 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1048
1049 spin_unlock_irqrestore(&bp->lock, flags);
1050 return received;
1051 }
1052
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001053 if (first_frag != -1)
1054 bp->rx_tail = first_frag;
1055 else
1056 bp->rx_tail = tail;
1057
1058 return received;
1059}
1060
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001061static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001062{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001063 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001064 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001065 u32 status;
1066
1067 status = macb_readl(bp, RSR);
1068 macb_writel(bp, RSR, status);
1069
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001070 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001071
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001072 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001073 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001074
Nicolas Ferre4df95132013-06-04 21:57:12 +00001075 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001076 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001077 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001078
Nicolas Ferre8770e912013-02-12 11:08:48 +01001079 /* Packets received while interrupts were disabled */
1080 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001081 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001082 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1083 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001084 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001085 } else {
1086 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1087 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001088 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001089
1090 /* TODO: Handle errors */
1091
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001092 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001093}
1094
1095static irqreturn_t macb_interrupt(int irq, void *dev_id)
1096{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001097 struct macb_queue *queue = dev_id;
1098 struct macb *bp = queue->bp;
1099 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001100 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001101
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001102 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001103
1104 if (unlikely(!status))
1105 return IRQ_NONE;
1106
1107 spin_lock(&bp->lock);
1108
1109 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001110 /* close possible race with dev_close */
1111 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001112 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001113 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1114 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001115 break;
1116 }
1117
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001118 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1119 (unsigned int)(queue - bp->queues),
1120 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001121
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001122 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001123 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001124 * until we have processed the buffers. The
1125 * scheduling call may fail if the poll routine
1126 * is already scheduled, so disable interrupts
1127 * now.
1128 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001129 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001130 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001131 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001132
Ben Hutchings288379f2009-01-19 16:43:59 -08001133 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001134 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001135 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001136 }
1137 }
1138
Nicolas Ferree86cd532012-10-31 06:04:57 +00001139 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001140 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1141 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001142
1143 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001144 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001145
Nicolas Ferree86cd532012-10-31 06:04:57 +00001146 break;
1147 }
1148
1149 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001150 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001151
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001152 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001153 * add that if/when we get our hands on a full-blown MII PHY.
1154 */
1155
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001156 /* There is a hardware issue under heavy load where DMA can
1157 * stop, this causes endless "used buffer descriptor read"
1158 * interrupts but it can be cleared by re-enabling RX. See
1159 * the at91 manual, section 41.3.1 or the Zynq manual
1160 * section 16.7.4 for details.
1161 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001162 if (status & MACB_BIT(RXUBR)) {
1163 ctrl = macb_readl(bp, NCR);
1164 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1165 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1166
1167 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001168 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001169 }
1170
Alexander Steinb19f7f72011-04-13 05:03:24 +00001171 if (status & MACB_BIT(ISR_ROVR)) {
1172 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001173 if (macb_is_gem(bp))
1174 bp->hw_stats.gem.rx_overruns++;
1175 else
1176 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001177
1178 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001179 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001180 }
1181
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001182 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001183 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001184 * netdev_err to a lower-priority context as well
1185 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001186 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001187 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001188
1189 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001190 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001191 }
1192
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001193 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001194 }
1195
1196 spin_unlock(&bp->lock);
1197
1198 return IRQ_HANDLED;
1199}
1200
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001201#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001202/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001203 * to allow network i/o with interrupts disabled.
1204 */
1205static void macb_poll_controller(struct net_device *dev)
1206{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001207 struct macb *bp = netdev_priv(dev);
1208 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001209 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001210 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001211
1212 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001213 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1214 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001215 local_irq_restore(flags);
1216}
1217#endif
1218
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001219static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001220 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001221 struct sk_buff *skb)
1222{
1223 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001224 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001225 struct macb_tx_skb *tx_skb = NULL;
1226 struct macb_dma_desc *desc;
1227 unsigned int offset, size, count = 0;
1228 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1229 unsigned int eof = 1;
1230 u32 ctrl;
1231
1232 /* First, map non-paged data */
1233 len = skb_headlen(skb);
1234 offset = 0;
1235 while (len) {
1236 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001237 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001238 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001239
1240 mapping = dma_map_single(&bp->pdev->dev,
1241 skb->data + offset,
1242 size, DMA_TO_DEVICE);
1243 if (dma_mapping_error(&bp->pdev->dev, mapping))
1244 goto dma_error;
1245
1246 /* Save info to properly release resources */
1247 tx_skb->skb = NULL;
1248 tx_skb->mapping = mapping;
1249 tx_skb->size = size;
1250 tx_skb->mapped_as_page = false;
1251
1252 len -= size;
1253 offset += size;
1254 count++;
1255 tx_head++;
1256 }
1257
1258 /* Then, map paged data from fragments */
1259 for (f = 0; f < nr_frags; f++) {
1260 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1261
1262 len = skb_frag_size(frag);
1263 offset = 0;
1264 while (len) {
1265 size = min(len, bp->max_tx_length);
Zach Brownb410d132016-10-19 09:56:57 -05001266 entry = macb_tx_ring_wrap(bp, tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001267 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001268
1269 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1270 offset, size, DMA_TO_DEVICE);
1271 if (dma_mapping_error(&bp->pdev->dev, mapping))
1272 goto dma_error;
1273
1274 /* Save info to properly release resources */
1275 tx_skb->skb = NULL;
1276 tx_skb->mapping = mapping;
1277 tx_skb->size = size;
1278 tx_skb->mapped_as_page = true;
1279
1280 len -= size;
1281 offset += size;
1282 count++;
1283 tx_head++;
1284 }
1285 }
1286
1287 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001288 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001289 netdev_err(bp->dev, "BUG! empty skb!\n");
1290 return 0;
1291 }
1292
1293 /* This is the last buffer of the frame: save socket buffer */
1294 tx_skb->skb = skb;
1295
1296 /* Update TX ring: update buffer descriptors in reverse order
1297 * to avoid race condition
1298 */
1299
1300 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1301 * to set the end of TX queue
1302 */
1303 i = tx_head;
Zach Brownb410d132016-10-19 09:56:57 -05001304 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001305 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001306 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001307 desc->ctrl = ctrl;
1308
1309 do {
1310 i--;
Zach Brownb410d132016-10-19 09:56:57 -05001311 entry = macb_tx_ring_wrap(bp, i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001312 tx_skb = &queue->tx_skb[entry];
1313 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001314
1315 ctrl = (u32)tx_skb->size;
1316 if (eof) {
1317 ctrl |= MACB_BIT(TX_LAST);
1318 eof = 0;
1319 }
Zach Brownb410d132016-10-19 09:56:57 -05001320 if (unlikely(entry == (bp->tx_ring_size - 1)))
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001321 ctrl |= MACB_BIT(TX_WRAP);
1322
1323 /* Set TX buffer descriptor */
Harini Katakamfff80192016-08-09 13:15:53 +05301324 macb_set_addr(desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001325 /* desc->addr must be visible to hardware before clearing
1326 * 'TX_USED' bit in desc->ctrl.
1327 */
1328 wmb();
1329 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001330 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001331
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001332 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001333
1334 return count;
1335
1336dma_error:
1337 netdev_err(bp->dev, "TX DMA map failed\n");
1338
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001339 for (i = queue->tx_head; i != tx_head; i++) {
1340 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001341
1342 macb_tx_unmap(bp, tx_skb);
1343 }
1344
1345 return 0;
1346}
1347
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001348static inline int macb_clear_csum(struct sk_buff *skb)
1349{
1350 /* no change for packets without checksum offloading */
1351 if (skb->ip_summed != CHECKSUM_PARTIAL)
1352 return 0;
1353
1354 /* make sure we can modify the header */
1355 if (unlikely(skb_cow_head(skb, 0)))
1356 return -1;
1357
1358 /* initialize checksum field
1359 * This is required - at least for Zynq, which otherwise calculates
1360 * wrong UDP header checksums for UDP packets with UDP data len <=2
1361 */
1362 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1363 return 0;
1364}
1365
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001366static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1367{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001368 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001369 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001370 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001371 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001372 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001373
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001374#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1375 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001376 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1377 queue_index, skb->len, skb->head, skb->data,
1378 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001379 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1380 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001381#endif
1382
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001383 /* Count how many TX buffer descriptors are needed to send this
1384 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001385 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001386 */
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001387 count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001388 nr_frags = skb_shinfo(skb)->nr_frags;
1389 for (f = 0; f < nr_frags; f++) {
1390 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001391 count += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001392 }
1393
Dongdong Deng48719532009-08-23 19:49:07 -07001394 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001395
1396 /* This is a hard error, log it. */
Zach Brownb410d132016-10-19 09:56:57 -05001397 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
1398 bp->tx_ring_size) < count) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001399 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001400 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001401 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001402 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001403 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001404 }
1405
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001406 if (macb_clear_csum(skb)) {
1407 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001408 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001409 }
1410
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001411 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001412 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001413 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001414 goto unlock;
1415 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001416
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001417 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001418 wmb();
1419
Richard Cochrane0720922011-06-19 21:51:28 +00001420 skb_tx_timestamp(skb);
1421
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001422 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1423
Zach Brownb410d132016-10-19 09:56:57 -05001424 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001425 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001426
Soren Brinkmann92030902014-03-04 08:46:39 -08001427unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001428 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001429
Patrick McHardy6ed10652009-06-23 06:03:08 +00001430 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001431}
1432
Nicolas Ferre4df95132013-06-04 21:57:12 +00001433static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001434{
1435 if (!macb_is_gem(bp)) {
1436 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1437 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001438 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001439
Nicolas Ferre1b447912013-06-04 21:57:11 +00001440 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001441 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001442 "RX buffer must be multiple of %d bytes, expanding\n",
1443 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001444 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001445 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001446 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001447 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001448
1449 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1450 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001451}
1452
Nicolas Ferre4df95132013-06-04 21:57:12 +00001453static void gem_free_rx_buffers(struct macb *bp)
1454{
1455 struct sk_buff *skb;
1456 struct macb_dma_desc *desc;
1457 dma_addr_t addr;
1458 int i;
1459
1460 if (!bp->rx_skbuff)
1461 return;
1462
Zach Brownb410d132016-10-19 09:56:57 -05001463 for (i = 0; i < bp->rx_ring_size; i++) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001464 skb = bp->rx_skbuff[i];
1465
Moritz Fischeraa50b552016-03-29 19:11:13 -07001466 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001467 continue;
1468
1469 desc = &bp->rx_ring[i];
1470 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Harini Katakamfff80192016-08-09 13:15:53 +05301471#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1472 addr |= ((u64)(desc->addrh) << 32);
1473#endif
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001474 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001475 DMA_FROM_DEVICE);
1476 dev_kfree_skb_any(skb);
1477 skb = NULL;
1478 }
1479
1480 kfree(bp->rx_skbuff);
1481 bp->rx_skbuff = NULL;
1482}
1483
1484static void macb_free_rx_buffers(struct macb *bp)
1485{
1486 if (bp->rx_buffers) {
1487 dma_free_coherent(&bp->pdev->dev,
Zach Brownb410d132016-10-19 09:56:57 -05001488 bp->rx_ring_size * bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001489 bp->rx_buffers, bp->rx_buffers_dma);
1490 bp->rx_buffers = NULL;
1491 }
1492}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001493
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001494static void macb_free_consistent(struct macb *bp)
1495{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001496 struct macb_queue *queue;
1497 unsigned int q;
1498
Nicolas Ferre4df95132013-06-04 21:57:12 +00001499 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001500 if (bp->rx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001501 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES(bp),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001502 bp->rx_ring, bp->rx_ring_dma);
1503 bp->rx_ring = NULL;
1504 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001505
1506 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1507 kfree(queue->tx_skb);
1508 queue->tx_skb = NULL;
1509 if (queue->tx_ring) {
Zach Brownb410d132016-10-19 09:56:57 -05001510 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES(bp),
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001511 queue->tx_ring, queue->tx_ring_dma);
1512 queue->tx_ring = NULL;
1513 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001514 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001515}
1516
1517static int gem_alloc_rx_buffers(struct macb *bp)
1518{
1519 int size;
1520
Zach Brownb410d132016-10-19 09:56:57 -05001521 size = bp->rx_ring_size * sizeof(struct sk_buff *);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001522 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1523 if (!bp->rx_skbuff)
1524 return -ENOMEM;
Zach Brownb410d132016-10-19 09:56:57 -05001525 else
1526 netdev_dbg(bp->dev,
1527 "Allocated %d RX struct sk_buff entries at %p\n",
1528 bp->rx_ring_size, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001529 return 0;
1530}
1531
1532static int macb_alloc_rx_buffers(struct macb *bp)
1533{
1534 int size;
1535
Zach Brownb410d132016-10-19 09:56:57 -05001536 size = bp->rx_ring_size * bp->rx_buffer_size;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001537 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1538 &bp->rx_buffers_dma, GFP_KERNEL);
1539 if (!bp->rx_buffers)
1540 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001541
1542 netdev_dbg(bp->dev,
1543 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1544 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001545 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001546}
1547
1548static int macb_alloc_consistent(struct macb *bp)
1549{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001550 struct macb_queue *queue;
1551 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001552 int size;
1553
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001554 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001555 size = TX_RING_BYTES(bp);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001556 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1557 &queue->tx_ring_dma,
1558 GFP_KERNEL);
1559 if (!queue->tx_ring)
1560 goto out_err;
1561 netdev_dbg(bp->dev,
1562 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1563 q, size, (unsigned long)queue->tx_ring_dma,
1564 queue->tx_ring);
1565
Zach Brownb410d132016-10-19 09:56:57 -05001566 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001567 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1568 if (!queue->tx_skb)
1569 goto out_err;
1570 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001571
Zach Brownb410d132016-10-19 09:56:57 -05001572 size = RX_RING_BYTES(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001573 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1574 &bp->rx_ring_dma, GFP_KERNEL);
1575 if (!bp->rx_ring)
1576 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001577 netdev_dbg(bp->dev,
1578 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1579 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001580
Nicolas Ferre4df95132013-06-04 21:57:12 +00001581 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001582 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001583
1584 return 0;
1585
1586out_err:
1587 macb_free_consistent(bp);
1588 return -ENOMEM;
1589}
1590
Nicolas Ferre4df95132013-06-04 21:57:12 +00001591static void gem_init_rings(struct macb *bp)
1592{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001593 struct macb_queue *queue;
1594 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001595 int i;
1596
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001597 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Zach Brownb410d132016-10-19 09:56:57 -05001598 for (i = 0; i < bp->tx_ring_size; i++) {
1599 queue->tx_ring[i].addr = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001600 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1601 }
Zach Brownb410d132016-10-19 09:56:57 -05001602 queue->tx_ring[bp->tx_ring_size - 1].ctrl |= MACB_BIT(TX_WRAP);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001603 queue->tx_head = 0;
1604 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001605 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001606
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001607 bp->rx_tail = 0;
1608 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001609
1610 gem_rx_refill(bp);
1611}
1612
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001613static void macb_init_rings(struct macb *bp)
1614{
1615 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001616
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001617 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001618
Zach Brownb410d132016-10-19 09:56:57 -05001619 for (i = 0; i < bp->tx_ring_size; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001620 bp->queues[0].tx_ring[i].addr = 0;
1621 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001622 }
Ben Shelton21d35152015-04-22 17:28:54 -05001623 bp->queues[0].tx_head = 0;
1624 bp->queues[0].tx_tail = 0;
Zach Brownb410d132016-10-19 09:56:57 -05001625 bp->queues[0].tx_ring[bp->tx_ring_size - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001626
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001627 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001628}
1629
1630static void macb_reset_hw(struct macb *bp)
1631{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001632 struct macb_queue *queue;
1633 unsigned int q;
1634
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001635 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001636 * more gracefully?)
1637 */
1638 macb_writel(bp, NCR, 0);
1639
1640 /* Clear the stats registers (XXX: Update stats first?) */
1641 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1642
1643 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001644 macb_writel(bp, TSR, -1);
1645 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001646
1647 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001648 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1649 queue_writel(queue, IDR, -1);
1650 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001651 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1652 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001653 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001654}
1655
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001656static u32 gem_mdc_clk_div(struct macb *bp)
1657{
1658 u32 config;
1659 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1660
1661 if (pclk_hz <= 20000000)
1662 config = GEM_BF(CLK, GEM_CLK_DIV8);
1663 else if (pclk_hz <= 40000000)
1664 config = GEM_BF(CLK, GEM_CLK_DIV16);
1665 else if (pclk_hz <= 80000000)
1666 config = GEM_BF(CLK, GEM_CLK_DIV32);
1667 else if (pclk_hz <= 120000000)
1668 config = GEM_BF(CLK, GEM_CLK_DIV48);
1669 else if (pclk_hz <= 160000000)
1670 config = GEM_BF(CLK, GEM_CLK_DIV64);
1671 else
1672 config = GEM_BF(CLK, GEM_CLK_DIV96);
1673
1674 return config;
1675}
1676
1677static u32 macb_mdc_clk_div(struct macb *bp)
1678{
1679 u32 config;
1680 unsigned long pclk_hz;
1681
1682 if (macb_is_gem(bp))
1683 return gem_mdc_clk_div(bp);
1684
1685 pclk_hz = clk_get_rate(bp->pclk);
1686 if (pclk_hz <= 20000000)
1687 config = MACB_BF(CLK, MACB_CLK_DIV8);
1688 else if (pclk_hz <= 40000000)
1689 config = MACB_BF(CLK, MACB_CLK_DIV16);
1690 else if (pclk_hz <= 80000000)
1691 config = MACB_BF(CLK, MACB_CLK_DIV32);
1692 else
1693 config = MACB_BF(CLK, MACB_CLK_DIV64);
1694
1695 return config;
1696}
1697
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001698/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001699 * should program. We find the width from decoding the design configuration
1700 * register to find the maximum supported data bus width.
1701 */
1702static u32 macb_dbw(struct macb *bp)
1703{
1704 if (!macb_is_gem(bp))
1705 return 0;
1706
1707 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1708 case 4:
1709 return GEM_BF(DBW, GEM_DBW128);
1710 case 2:
1711 return GEM_BF(DBW, GEM_DBW64);
1712 case 1:
1713 default:
1714 return GEM_BF(DBW, GEM_DBW32);
1715 }
1716}
1717
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001718/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001719 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001720 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001721 * (if not supported by FIFO, it will fallback to default)
1722 * - set both rx/tx packet buffers to full memory size
1723 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001724 */
1725static void macb_configure_dma(struct macb *bp)
1726{
1727 u32 dmacfg;
1728
1729 if (macb_is_gem(bp)) {
1730 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001731 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001732 if (bp->dma_burst_length)
1733 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001734 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301735 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301736
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03001737 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301738 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1739 else
1740 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1741
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001742 if (bp->dev->features & NETIF_F_HW_CSUM)
1743 dmacfg |= GEM_BIT(TXCOEN);
1744 else
1745 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301746
1747#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1748 dmacfg |= GEM_BIT(ADDR64);
1749#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001750 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1751 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001752 gem_writel(bp, DMACFG, dmacfg);
1753 }
1754}
1755
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001756static void macb_init_hw(struct macb *bp)
1757{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001758 struct macb_queue *queue;
1759 unsigned int q;
1760
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001761 u32 config;
1762
1763 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001764 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001765
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001766 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301767 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1768 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001769 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001770 config |= MACB_BIT(PAE); /* PAuse Enable */
1771 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001772 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301773 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1774 else
1775 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001776 if (bp->dev->flags & IFF_PROMISC)
1777 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001778 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1779 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001780 if (!(bp->dev->flags & IFF_BROADCAST))
1781 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001782 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001783 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001784 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301785 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001786 bp->speed = SPEED_10;
1787 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301788 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001789 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301790 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001791
Jamie Iles0116da42011-03-14 17:38:30 +00001792 macb_configure_dma(bp);
1793
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001794 /* Initialize TX and RX buffers */
Harini Katakamfff80192016-08-09 13:15:53 +05301795 macb_writel(bp, RBQP, (u32)(bp->rx_ring_dma));
1796#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1797 macb_writel(bp, RBQPH, (u32)(bp->rx_ring_dma >> 32));
1798#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001799 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakamfff80192016-08-09 13:15:53 +05301800 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
1801#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1802 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
1803#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001804
1805 /* Enable interrupts */
1806 queue_writel(queue, IER,
1807 MACB_RX_INT_FLAGS |
1808 MACB_TX_INT_FLAGS |
1809 MACB_BIT(HRESP));
1810 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001811
1812 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001813 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814}
1815
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001816/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001817 * locations in the memory map. The least significant bits are stored
1818 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1819 *
1820 * The unicast hash enable and the multicast hash enable bits in the
1821 * network configuration register enable the reception of hash matched
1822 * frames. The destination address is reduced to a 6 bit index into
1823 * the 64 bit hash register using the following hash function. The
1824 * hash function is an exclusive or of every sixth bit of the
1825 * destination address.
1826 *
1827 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1828 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1829 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1830 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1831 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1832 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1833 *
1834 * da[0] represents the least significant bit of the first byte
1835 * received, that is, the multicast/unicast indicator, and da[47]
1836 * represents the most significant bit of the last byte received. If
1837 * the hash index, hi[n], points to a bit that is set in the hash
1838 * register then the frame will be matched according to whether the
1839 * frame is multicast or unicast. A multicast match will be signalled
1840 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1841 * index points to a bit set in the hash register. A unicast match
1842 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1843 * and the hash index points to a bit set in the hash register. To
1844 * receive all multicast frames, the hash register should be set with
1845 * all ones and the multicast hash enable bit should be set in the
1846 * network configuration register.
1847 */
1848
1849static inline int hash_bit_value(int bitnr, __u8 *addr)
1850{
1851 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1852 return 1;
1853 return 0;
1854}
1855
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001856/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001857static int hash_get_index(__u8 *addr)
1858{
1859 int i, j, bitval;
1860 int hash_index = 0;
1861
1862 for (j = 0; j < 6; j++) {
1863 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001864 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001865
1866 hash_index |= (bitval << j);
1867 }
1868
1869 return hash_index;
1870}
1871
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001872/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001873static void macb_sethashtable(struct net_device *dev)
1874{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001875 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001876 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001877 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001878 struct macb *bp = netdev_priv(dev);
1879
Moritz Fischeraa50b552016-03-29 19:11:13 -07001880 mc_filter[0] = 0;
1881 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001882
Jiri Pirko22bedad32010-04-01 21:22:57 +00001883 netdev_for_each_mc_addr(ha, dev) {
1884 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001885 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1886 }
1887
Jamie Ilesf75ba502011-11-08 10:12:32 +00001888 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1889 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001890}
1891
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001892/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001893static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001894{
1895 unsigned long cfg;
1896 struct macb *bp = netdev_priv(dev);
1897
1898 cfg = macb_readl(bp, NCFGR);
1899
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001900 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001901 /* Enable promiscuous mode */
1902 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001903
1904 /* Disable RX checksum offload */
1905 if (macb_is_gem(bp))
1906 cfg &= ~GEM_BIT(RXCOEN);
1907 } else {
1908 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001909 cfg &= ~MACB_BIT(CAF);
1910
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001911 /* Enable RX checksum offload only if requested */
1912 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1913 cfg |= GEM_BIT(RXCOEN);
1914 }
1915
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001916 if (dev->flags & IFF_ALLMULTI) {
1917 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001918 macb_or_gem_writel(bp, HRB, -1);
1919 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001920 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001921 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001922 /* Enable specific multicasts */
1923 macb_sethashtable(dev);
1924 cfg |= MACB_BIT(NCFGR_MTI);
1925 } else if (dev->flags & (~IFF_ALLMULTI)) {
1926 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001927 macb_or_gem_writel(bp, HRB, 0);
1928 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001929 cfg &= ~MACB_BIT(NCFGR_MTI);
1930 }
1931
1932 macb_writel(bp, NCFGR, cfg);
1933}
1934
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001935static int macb_open(struct net_device *dev)
1936{
1937 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001938 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001939 int err;
1940
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001941 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001942
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001943 /* carrier starts down */
1944 netif_carrier_off(dev);
1945
frederic RODO6c36a702007-07-12 19:07:24 +02001946 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02001947 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02001948 return -EAGAIN;
1949
Nicolas Ferre1b447912013-06-04 21:57:11 +00001950 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001951 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001952
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001953 err = macb_alloc_consistent(bp);
1954 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001955 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1956 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001957 return err;
1958 }
1959
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001960 napi_enable(&bp->napi);
1961
Nicolas Ferre4df95132013-06-04 21:57:12 +00001962 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001963 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001964
frederic RODO6c36a702007-07-12 19:07:24 +02001965 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02001966 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02001967
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001968 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001969
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001970 return 0;
1971}
1972
1973static int macb_close(struct net_device *dev)
1974{
1975 struct macb *bp = netdev_priv(dev);
1976 unsigned long flags;
1977
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001978 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001979 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001980
Philippe Reynes0a912812016-06-22 00:32:35 +02001981 if (dev->phydev)
1982 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02001983
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001984 spin_lock_irqsave(&bp->lock, flags);
1985 macb_reset_hw(bp);
1986 netif_carrier_off(dev);
1987 spin_unlock_irqrestore(&bp->lock, flags);
1988
1989 macb_free_consistent(bp);
1990
1991 return 0;
1992}
1993
Harini Katakama5898ea2015-05-06 22:27:18 +05301994static int macb_change_mtu(struct net_device *dev, int new_mtu)
1995{
Harini Katakama5898ea2015-05-06 22:27:18 +05301996 if (netif_running(dev))
1997 return -EBUSY;
1998
Harini Katakama5898ea2015-05-06 22:27:18 +05301999 dev->mtu = new_mtu;
2000
2001 return 0;
2002}
2003
Jamie Ilesa494ed82011-03-09 16:26:35 +00002004static void gem_update_stats(struct macb *bp)
2005{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002006 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002007 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002008
Xander Huff3ff13f12015-01-13 16:15:51 -06002009 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2010 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002011 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002012
2013 bp->ethtool_stats[i] += val;
2014 *p += val;
2015
2016 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2017 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002018 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002019 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002020 *(++p) += val;
2021 }
2022 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002023}
2024
2025static struct net_device_stats *gem_get_stats(struct macb *bp)
2026{
2027 struct gem_stats *hwstat = &bp->hw_stats.gem;
2028 struct net_device_stats *nstat = &bp->stats;
2029
2030 gem_update_stats(bp);
2031
2032 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2033 hwstat->rx_alignment_errors +
2034 hwstat->rx_resource_errors +
2035 hwstat->rx_overruns +
2036 hwstat->rx_oversize_frames +
2037 hwstat->rx_jabbers +
2038 hwstat->rx_undersized_frames +
2039 hwstat->rx_length_field_frame_errors);
2040 nstat->tx_errors = (hwstat->tx_late_collisions +
2041 hwstat->tx_excessive_collisions +
2042 hwstat->tx_underrun +
2043 hwstat->tx_carrier_sense_errors);
2044 nstat->multicast = hwstat->rx_multicast_frames;
2045 nstat->collisions = (hwstat->tx_single_collision_frames +
2046 hwstat->tx_multiple_collision_frames +
2047 hwstat->tx_excessive_collisions);
2048 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2049 hwstat->rx_jabbers +
2050 hwstat->rx_undersized_frames +
2051 hwstat->rx_length_field_frame_errors);
2052 nstat->rx_over_errors = hwstat->rx_resource_errors;
2053 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2054 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2055 nstat->rx_fifo_errors = hwstat->rx_overruns;
2056 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2057 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2058 nstat->tx_fifo_errors = hwstat->tx_underrun;
2059
2060 return nstat;
2061}
2062
Xander Huff3ff13f12015-01-13 16:15:51 -06002063static void gem_get_ethtool_stats(struct net_device *dev,
2064 struct ethtool_stats *stats, u64 *data)
2065{
2066 struct macb *bp;
2067
2068 bp = netdev_priv(dev);
2069 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002070 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002071}
2072
2073static int gem_get_sset_count(struct net_device *dev, int sset)
2074{
2075 switch (sset) {
2076 case ETH_SS_STATS:
2077 return GEM_STATS_LEN;
2078 default:
2079 return -EOPNOTSUPP;
2080 }
2081}
2082
2083static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2084{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002085 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002086
2087 switch (sset) {
2088 case ETH_SS_STATS:
2089 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2090 memcpy(p, gem_statistics[i].stat_string,
2091 ETH_GSTRING_LEN);
2092 break;
2093 }
2094}
2095
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002096static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002097{
2098 struct macb *bp = netdev_priv(dev);
2099 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002100 struct macb_stats *hwstat = &bp->hw_stats.macb;
2101
2102 if (macb_is_gem(bp))
2103 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002104
frederic RODO6c36a702007-07-12 19:07:24 +02002105 /* read stats from hardware */
2106 macb_update_stats(bp);
2107
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002108 /* Convert HW stats into netdevice stats */
2109 nstat->rx_errors = (hwstat->rx_fcs_errors +
2110 hwstat->rx_align_errors +
2111 hwstat->rx_resource_errors +
2112 hwstat->rx_overruns +
2113 hwstat->rx_oversize_pkts +
2114 hwstat->rx_jabbers +
2115 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002116 hwstat->rx_length_mismatch);
2117 nstat->tx_errors = (hwstat->tx_late_cols +
2118 hwstat->tx_excessive_cols +
2119 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002120 hwstat->tx_carrier_errors +
2121 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002122 nstat->collisions = (hwstat->tx_single_cols +
2123 hwstat->tx_multiple_cols +
2124 hwstat->tx_excessive_cols);
2125 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2126 hwstat->rx_jabbers +
2127 hwstat->rx_undersize_pkts +
2128 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002129 nstat->rx_over_errors = hwstat->rx_resource_errors +
2130 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002131 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2132 nstat->rx_frame_errors = hwstat->rx_align_errors;
2133 nstat->rx_fifo_errors = hwstat->rx_overruns;
2134 /* XXX: What does "missed" mean? */
2135 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2136 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2137 nstat->tx_fifo_errors = hwstat->tx_underruns;
2138 /* Don't know about heartbeat or window errors... */
2139
2140 return nstat;
2141}
2142
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002143static int macb_get_regs_len(struct net_device *netdev)
2144{
2145 return MACB_GREGS_NBR * sizeof(u32);
2146}
2147
2148static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2149 void *p)
2150{
2151 struct macb *bp = netdev_priv(dev);
2152 unsigned int tail, head;
2153 u32 *regs_buff = p;
2154
2155 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2156 | MACB_GREGS_VERSION;
2157
Zach Brownb410d132016-10-19 09:56:57 -05002158 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2159 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002160
2161 regs_buff[0] = macb_readl(bp, NCR);
2162 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2163 regs_buff[2] = macb_readl(bp, NSR);
2164 regs_buff[3] = macb_readl(bp, TSR);
2165 regs_buff[4] = macb_readl(bp, RBQP);
2166 regs_buff[5] = macb_readl(bp, TBQP);
2167 regs_buff[6] = macb_readl(bp, RSR);
2168 regs_buff[7] = macb_readl(bp, IMR);
2169
2170 regs_buff[8] = tail;
2171 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002172 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2173 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002174
Neil Armstrongce721a72016-01-05 14:39:16 +01002175 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2176 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002177 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002178 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002179}
2180
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002181static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2182{
2183 struct macb *bp = netdev_priv(netdev);
2184
2185 wol->supported = 0;
2186 wol->wolopts = 0;
2187
2188 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2189 wol->supported = WAKE_MAGIC;
2190
2191 if (bp->wol & MACB_WOL_ENABLED)
2192 wol->wolopts |= WAKE_MAGIC;
2193 }
2194}
2195
2196static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2197{
2198 struct macb *bp = netdev_priv(netdev);
2199
2200 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2201 (wol->wolopts & ~WAKE_MAGIC))
2202 return -EOPNOTSUPP;
2203
2204 if (wol->wolopts & WAKE_MAGIC)
2205 bp->wol |= MACB_WOL_ENABLED;
2206 else
2207 bp->wol &= ~MACB_WOL_ENABLED;
2208
2209 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2210
2211 return 0;
2212}
2213
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002214static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002215 .get_regs_len = macb_get_regs_len,
2216 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002217 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002218 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002219 .get_wol = macb_get_wol,
2220 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002221 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2222 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Xander Huff8cd5a562015-01-15 15:55:20 -06002223};
Xander Huff8cd5a562015-01-15 15:55:20 -06002224
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002225static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002226 .get_regs_len = macb_get_regs_len,
2227 .get_regs = macb_get_regs,
2228 .get_link = ethtool_op_get_link,
2229 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002230 .get_ethtool_stats = gem_get_ethtool_stats,
2231 .get_strings = gem_get_ethtool_strings,
2232 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002233 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2234 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002235};
2236
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002237static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002238{
Philippe Reynes0a912812016-06-22 00:32:35 +02002239 struct phy_device *phydev = dev->phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002240
2241 if (!netif_running(dev))
2242 return -EINVAL;
2243
frederic RODO6c36a702007-07-12 19:07:24 +02002244 if (!phydev)
2245 return -ENODEV;
2246
Richard Cochran28b04112010-07-17 08:48:55 +00002247 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002248}
2249
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002250static int macb_set_features(struct net_device *netdev,
2251 netdev_features_t features)
2252{
2253 struct macb *bp = netdev_priv(netdev);
2254 netdev_features_t changed = features ^ netdev->features;
2255
2256 /* TX checksum offload */
2257 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2258 u32 dmacfg;
2259
2260 dmacfg = gem_readl(bp, DMACFG);
2261 if (features & NETIF_F_HW_CSUM)
2262 dmacfg |= GEM_BIT(TXCOEN);
2263 else
2264 dmacfg &= ~GEM_BIT(TXCOEN);
2265 gem_writel(bp, DMACFG, dmacfg);
2266 }
2267
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002268 /* RX checksum offload */
2269 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2270 u32 netcfg;
2271
2272 netcfg = gem_readl(bp, NCFGR);
2273 if (features & NETIF_F_RXCSUM &&
2274 !(netdev->flags & IFF_PROMISC))
2275 netcfg |= GEM_BIT(RXCOEN);
2276 else
2277 netcfg &= ~GEM_BIT(RXCOEN);
2278 gem_writel(bp, NCFGR, netcfg);
2279 }
2280
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002281 return 0;
2282}
2283
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002284static const struct net_device_ops macb_netdev_ops = {
2285 .ndo_open = macb_open,
2286 .ndo_stop = macb_close,
2287 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002288 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002289 .ndo_get_stats = macb_get_stats,
2290 .ndo_do_ioctl = macb_ioctl,
2291 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302292 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002293 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002294#ifdef CONFIG_NET_POLL_CONTROLLER
2295 .ndo_poll_controller = macb_poll_controller,
2296#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002297 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002298};
2299
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002300/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002301 * and integration options used
2302 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002303static void macb_configure_caps(struct macb *bp,
2304 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002305{
2306 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002307
Nicolas Ferref6970502015-03-31 15:02:01 +02002308 if (dt_conf)
2309 bp->caps = dt_conf->caps;
2310
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002311 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002312 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2313
Nicolas Ferree1755872014-07-24 13:50:58 +02002314 dcfg = gem_readl(bp, DCFG1);
2315 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2316 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2317 dcfg = gem_readl(bp, DCFG2);
2318 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2319 bp->caps |= MACB_CAPS_FIFO_MODE;
2320 }
2321
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002322 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002323}
2324
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002325static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002326 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002327 unsigned int *queue_mask,
2328 unsigned int *num_queues)
2329{
2330 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002331
2332 *queue_mask = 0x1;
2333 *num_queues = 1;
2334
Nicolas Ferreda120112015-03-31 15:02:00 +02002335 /* is it macb or gem ?
2336 *
2337 * We need to read directly from the hardware here because
2338 * we are early in the probe process and don't have the
2339 * MACB_CAPS_MACB_IS_GEM flag positioned
2340 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002341 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002342 return;
2343
2344 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302345 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2346
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002347 *queue_mask |= 0x1;
2348
2349 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2350 if (*queue_mask & (1 << hw_q))
2351 (*num_queues)++;
2352}
2353
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002354static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302355 struct clk **hclk, struct clk **tx_clk,
2356 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002357{
2358 int err;
2359
2360 *pclk = devm_clk_get(&pdev->dev, "pclk");
2361 if (IS_ERR(*pclk)) {
2362 err = PTR_ERR(*pclk);
2363 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2364 return err;
2365 }
2366
2367 *hclk = devm_clk_get(&pdev->dev, "hclk");
2368 if (IS_ERR(*hclk)) {
2369 err = PTR_ERR(*hclk);
2370 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2371 return err;
2372 }
2373
2374 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2375 if (IS_ERR(*tx_clk))
2376 *tx_clk = NULL;
2377
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302378 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2379 if (IS_ERR(*rx_clk))
2380 *rx_clk = NULL;
2381
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002382 err = clk_prepare_enable(*pclk);
2383 if (err) {
2384 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2385 return err;
2386 }
2387
2388 err = clk_prepare_enable(*hclk);
2389 if (err) {
2390 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2391 goto err_disable_pclk;
2392 }
2393
2394 err = clk_prepare_enable(*tx_clk);
2395 if (err) {
2396 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2397 goto err_disable_hclk;
2398 }
2399
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302400 err = clk_prepare_enable(*rx_clk);
2401 if (err) {
2402 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2403 goto err_disable_txclk;
2404 }
2405
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002406 return 0;
2407
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302408err_disable_txclk:
2409 clk_disable_unprepare(*tx_clk);
2410
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002411err_disable_hclk:
2412 clk_disable_unprepare(*hclk);
2413
2414err_disable_pclk:
2415 clk_disable_unprepare(*pclk);
2416
2417 return err;
2418}
2419
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002420static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002421{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002422 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002423 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002424 struct macb *bp = netdev_priv(dev);
2425 struct macb_queue *queue;
2426 int err;
2427 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002428
Zach Brownb410d132016-10-19 09:56:57 -05002429 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
2430 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
2431
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002432 /* set the queue register mapping once for all: queue0 has a special
2433 * register mapping but we don't want to test the queue index then
2434 * compute the corresponding register offset at run time.
2435 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002436 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002437 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002438 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002439
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002440 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002441 queue->bp = bp;
2442 if (hw_q) {
2443 queue->ISR = GEM_ISR(hw_q - 1);
2444 queue->IER = GEM_IER(hw_q - 1);
2445 queue->IDR = GEM_IDR(hw_q - 1);
2446 queue->IMR = GEM_IMR(hw_q - 1);
2447 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302448#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2449 queue->TBQPH = GEM_TBQPH(hw_q -1);
2450#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002451 } else {
2452 /* queue0 uses legacy registers */
2453 queue->ISR = MACB_ISR;
2454 queue->IER = MACB_IER;
2455 queue->IDR = MACB_IDR;
2456 queue->IMR = MACB_IMR;
2457 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302458#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2459 queue->TBQPH = MACB_TBQPH;
2460#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002461 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002462
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002463 /* get irq: here we use the linux queue index, not the hardware
2464 * queue index. the queue irq definitions in the device tree
2465 * must remove the optional gaps that could exist in the
2466 * hardware queue mask.
2467 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002468 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002469 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002470 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002471 if (err) {
2472 dev_err(&pdev->dev,
2473 "Unable to request IRQ %d (error %d)\n",
2474 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002475 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002476 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002477
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002478 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002479 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002480 }
2481
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002482 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002483 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002484
Nicolas Ferre4df95132013-06-04 21:57:12 +00002485 /* setup appropriated routines according to adapter type */
2486 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002487 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002488 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2489 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2490 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2491 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002492 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002493 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002494 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002495 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2496 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2497 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2498 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002499 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002500 }
2501
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002502 /* Set features */
2503 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002504 /* Checksum offload is only available on gem with packet buffer */
2505 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002506 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002507 if (bp->caps & MACB_CAPS_SG_DISABLED)
2508 dev->hw_features &= ~NETIF_F_SG;
2509 dev->features = dev->hw_features;
2510
Neil Armstrongce721a72016-01-05 14:39:16 +01002511 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2512 val = 0;
2513 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2514 val = GEM_BIT(RGMII);
2515 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002516 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002517 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002518 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002519 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002520
Neil Armstrongce721a72016-01-05 14:39:16 +01002521 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2522 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002523
Neil Armstrongce721a72016-01-05 14:39:16 +01002524 macb_or_gem_writel(bp, USRIO, val);
2525 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002526
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002527 /* Set MII management clock divider */
2528 val = macb_mdc_clk_div(bp);
2529 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302530 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2531 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002532 macb_writel(bp, NCFGR, val);
2533
2534 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002535}
2536
2537#if defined(CONFIG_OF)
2538/* 1518 rounded up */
2539#define AT91ETHER_MAX_RBUFF_SZ 0x600
2540/* max number of receive buffers */
2541#define AT91ETHER_MAX_RX_DESCR 9
2542
2543/* Initialize and start the Receiver and Transmit subsystems */
2544static int at91ether_start(struct net_device *dev)
2545{
2546 struct macb *lp = netdev_priv(dev);
2547 dma_addr_t addr;
2548 u32 ctl;
2549 int i;
2550
2551 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2552 (AT91ETHER_MAX_RX_DESCR *
2553 sizeof(struct macb_dma_desc)),
2554 &lp->rx_ring_dma, GFP_KERNEL);
2555 if (!lp->rx_ring)
2556 return -ENOMEM;
2557
2558 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2559 AT91ETHER_MAX_RX_DESCR *
2560 AT91ETHER_MAX_RBUFF_SZ,
2561 &lp->rx_buffers_dma, GFP_KERNEL);
2562 if (!lp->rx_buffers) {
2563 dma_free_coherent(&lp->pdev->dev,
2564 AT91ETHER_MAX_RX_DESCR *
2565 sizeof(struct macb_dma_desc),
2566 lp->rx_ring, lp->rx_ring_dma);
2567 lp->rx_ring = NULL;
2568 return -ENOMEM;
2569 }
2570
2571 addr = lp->rx_buffers_dma;
2572 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2573 lp->rx_ring[i].addr = addr;
2574 lp->rx_ring[i].ctrl = 0;
2575 addr += AT91ETHER_MAX_RBUFF_SZ;
2576 }
2577
2578 /* Set the Wrap bit on the last descriptor */
2579 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2580
2581 /* Reset buffer index */
2582 lp->rx_tail = 0;
2583
2584 /* Program address of descriptor list in Rx Buffer Queue register */
2585 macb_writel(lp, RBQP, lp->rx_ring_dma);
2586
2587 /* Enable Receive and Transmit */
2588 ctl = macb_readl(lp, NCR);
2589 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2590
2591 return 0;
2592}
2593
2594/* Open the ethernet interface */
2595static int at91ether_open(struct net_device *dev)
2596{
2597 struct macb *lp = netdev_priv(dev);
2598 u32 ctl;
2599 int ret;
2600
2601 /* Clear internal statistics */
2602 ctl = macb_readl(lp, NCR);
2603 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2604
2605 macb_set_hwaddr(lp);
2606
2607 ret = at91ether_start(dev);
2608 if (ret)
2609 return ret;
2610
2611 /* Enable MAC interrupts */
2612 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2613 MACB_BIT(RXUBR) |
2614 MACB_BIT(ISR_TUND) |
2615 MACB_BIT(ISR_RLE) |
2616 MACB_BIT(TCOMP) |
2617 MACB_BIT(ISR_ROVR) |
2618 MACB_BIT(HRESP));
2619
2620 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002621 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002622
2623 netif_start_queue(dev);
2624
2625 return 0;
2626}
2627
2628/* Close the interface */
2629static int at91ether_close(struct net_device *dev)
2630{
2631 struct macb *lp = netdev_priv(dev);
2632 u32 ctl;
2633
2634 /* Disable Receiver and Transmitter */
2635 ctl = macb_readl(lp, NCR);
2636 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2637
2638 /* Disable MAC interrupts */
2639 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2640 MACB_BIT(RXUBR) |
2641 MACB_BIT(ISR_TUND) |
2642 MACB_BIT(ISR_RLE) |
2643 MACB_BIT(TCOMP) |
2644 MACB_BIT(ISR_ROVR) |
2645 MACB_BIT(HRESP));
2646
2647 netif_stop_queue(dev);
2648
2649 dma_free_coherent(&lp->pdev->dev,
2650 AT91ETHER_MAX_RX_DESCR *
2651 sizeof(struct macb_dma_desc),
2652 lp->rx_ring, lp->rx_ring_dma);
2653 lp->rx_ring = NULL;
2654
2655 dma_free_coherent(&lp->pdev->dev,
2656 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2657 lp->rx_buffers, lp->rx_buffers_dma);
2658 lp->rx_buffers = NULL;
2659
2660 return 0;
2661}
2662
2663/* Transmit packet */
2664static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2665{
2666 struct macb *lp = netdev_priv(dev);
2667
2668 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2669 netif_stop_queue(dev);
2670
2671 /* Store packet information (to free when Tx completed) */
2672 lp->skb = skb;
2673 lp->skb_length = skb->len;
2674 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2675 DMA_TO_DEVICE);
2676
2677 /* Set address of the data in the Transmit Address register */
2678 macb_writel(lp, TAR, lp->skb_physaddr);
2679 /* Set length of the packet in the Transmit Control register */
2680 macb_writel(lp, TCR, skb->len);
2681
2682 } else {
2683 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2684 return NETDEV_TX_BUSY;
2685 }
2686
2687 return NETDEV_TX_OK;
2688}
2689
2690/* Extract received frame from buffer descriptors and sent to upper layers.
2691 * (Called from interrupt context)
2692 */
2693static void at91ether_rx(struct net_device *dev)
2694{
2695 struct macb *lp = netdev_priv(dev);
2696 unsigned char *p_recv;
2697 struct sk_buff *skb;
2698 unsigned int pktlen;
2699
2700 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2701 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2702 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2703 skb = netdev_alloc_skb(dev, pktlen + 2);
2704 if (skb) {
2705 skb_reserve(skb, 2);
2706 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2707
2708 skb->protocol = eth_type_trans(skb, dev);
2709 lp->stats.rx_packets++;
2710 lp->stats.rx_bytes += pktlen;
2711 netif_rx(skb);
2712 } else {
2713 lp->stats.rx_dropped++;
2714 }
2715
2716 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2717 lp->stats.multicast++;
2718
2719 /* reset ownership bit */
2720 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2721
2722 /* wrap after last buffer */
2723 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2724 lp->rx_tail = 0;
2725 else
2726 lp->rx_tail++;
2727 }
2728}
2729
2730/* MAC interrupt handler */
2731static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2732{
2733 struct net_device *dev = dev_id;
2734 struct macb *lp = netdev_priv(dev);
2735 u32 intstatus, ctl;
2736
2737 /* MAC Interrupt Status register indicates what interrupts are pending.
2738 * It is automatically cleared once read.
2739 */
2740 intstatus = macb_readl(lp, ISR);
2741
2742 /* Receive complete */
2743 if (intstatus & MACB_BIT(RCOMP))
2744 at91ether_rx(dev);
2745
2746 /* Transmit complete */
2747 if (intstatus & MACB_BIT(TCOMP)) {
2748 /* The TCOM bit is set even if the transmission failed */
2749 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2750 lp->stats.tx_errors++;
2751
2752 if (lp->skb) {
2753 dev_kfree_skb_irq(lp->skb);
2754 lp->skb = NULL;
2755 dma_unmap_single(NULL, lp->skb_physaddr,
2756 lp->skb_length, DMA_TO_DEVICE);
2757 lp->stats.tx_packets++;
2758 lp->stats.tx_bytes += lp->skb_length;
2759 }
2760 netif_wake_queue(dev);
2761 }
2762
2763 /* Work-around for EMAC Errata section 41.3.1 */
2764 if (intstatus & MACB_BIT(RXUBR)) {
2765 ctl = macb_readl(lp, NCR);
2766 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2767 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2768 }
2769
2770 if (intstatus & MACB_BIT(ISR_ROVR))
2771 netdev_err(dev, "ROVR error\n");
2772
2773 return IRQ_HANDLED;
2774}
2775
2776#ifdef CONFIG_NET_POLL_CONTROLLER
2777static void at91ether_poll_controller(struct net_device *dev)
2778{
2779 unsigned long flags;
2780
2781 local_irq_save(flags);
2782 at91ether_interrupt(dev->irq, dev);
2783 local_irq_restore(flags);
2784}
2785#endif
2786
2787static const struct net_device_ops at91ether_netdev_ops = {
2788 .ndo_open = at91ether_open,
2789 .ndo_stop = at91ether_close,
2790 .ndo_start_xmit = at91ether_start_xmit,
2791 .ndo_get_stats = macb_get_stats,
2792 .ndo_set_rx_mode = macb_set_rx_mode,
2793 .ndo_set_mac_address = eth_mac_addr,
2794 .ndo_do_ioctl = macb_ioctl,
2795 .ndo_validate_addr = eth_validate_addr,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002796#ifdef CONFIG_NET_POLL_CONTROLLER
2797 .ndo_poll_controller = at91ether_poll_controller,
2798#endif
2799};
2800
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002801static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302802 struct clk **hclk, struct clk **tx_clk,
2803 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002804{
2805 int err;
2806
2807 *hclk = NULL;
2808 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302809 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002810
2811 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2812 if (IS_ERR(*pclk))
2813 return PTR_ERR(*pclk);
2814
2815 err = clk_prepare_enable(*pclk);
2816 if (err) {
2817 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2818 return err;
2819 }
2820
2821 return 0;
2822}
2823
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002824static int at91ether_init(struct platform_device *pdev)
2825{
2826 struct net_device *dev = platform_get_drvdata(pdev);
2827 struct macb *bp = netdev_priv(dev);
2828 int err;
2829 u32 reg;
2830
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002831 dev->netdev_ops = &at91ether_netdev_ops;
2832 dev->ethtool_ops = &macb_ethtool_ops;
2833
2834 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2835 0, dev->name, dev);
2836 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002837 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002838
2839 macb_writel(bp, NCR, 0);
2840
2841 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2842 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2843 reg |= MACB_BIT(RM9200_RMII);
2844
2845 macb_writel(bp, NCFGR, reg);
2846
2847 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002848}
2849
David S. Miller3cef5c52015-03-09 23:38:02 -04002850static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002851 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002852 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002853 .init = macb_init,
2854};
2855
David S. Miller3cef5c52015-03-09 23:38:02 -04002856static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002857 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2858 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002859 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002860 .init = macb_init,
2861};
2862
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002863static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002864 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002865 .dma_burst_length = 16,
2866 .clk_init = macb_clk_init,
2867 .init = macb_init,
2868};
2869
David S. Miller3cef5c52015-03-09 23:38:02 -04002870static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002871 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
2872 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002873 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002874 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002875 .init = macb_init,
2876};
2877
David S. Miller3cef5c52015-03-09 23:38:02 -04002878static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002879 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002880 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002881 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002882 .init = macb_init,
2883};
2884
David S. Miller3cef5c52015-03-09 23:38:02 -04002885static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002886 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002887 .init = at91ether_init,
2888};
2889
Neil Armstronge611b5b2016-01-05 14:39:17 +01002890static const struct macb_config np4_config = {
2891 .caps = MACB_CAPS_USRIO_DISABLED,
2892 .clk_init = macb_clk_init,
2893 .init = macb_init,
2894};
David S. Miller36583eb2015-05-23 01:22:35 -04002895
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302896static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302897 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302898 .dma_burst_length = 16,
2899 .clk_init = macb_clk_init,
2900 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302901 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302902};
2903
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002904static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302905 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002906 .dma_burst_length = 16,
2907 .clk_init = macb_clk_init,
2908 .init = macb_init,
2909};
2910
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002911static const struct of_device_id macb_dt_ids[] = {
2912 { .compatible = "cdns,at32ap7000-macb" },
2913 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2914 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01002915 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002916 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2917 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002918 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002919 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2920 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2921 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2922 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302923 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002924 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002925 { /* sentinel */ }
2926};
2927MODULE_DEVICE_TABLE(of, macb_dt_ids);
2928#endif /* CONFIG_OF */
2929
2930static int macb_probe(struct platform_device *pdev)
2931{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002932 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302933 struct clk **, struct clk **, struct clk **)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002934 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002935 int (*init)(struct platform_device *) = macb_init;
2936 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002937 struct device_node *phy_node;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002938 const struct macb_config *macb_config = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302939 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002940 unsigned int queue_mask, num_queues;
2941 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002942 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002943 struct phy_device *phydev;
2944 struct net_device *dev;
2945 struct resource *regs;
2946 void __iomem *mem;
2947 const char *mac;
2948 struct macb *bp;
2949 int err;
2950
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002951 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2952 mem = devm_ioremap_resource(&pdev->dev, regs);
2953 if (IS_ERR(mem))
2954 return PTR_ERR(mem);
2955
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002956 if (np) {
2957 const struct of_device_id *match;
2958
2959 match = of_match_node(macb_dt_ids, np);
2960 if (match && match->data) {
2961 macb_config = match->data;
2962 clk_init = macb_config->clk_init;
2963 init = macb_config->init;
2964 }
2965 }
2966
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302967 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002968 if (err)
2969 return err;
2970
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002971 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002972
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002973 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002974 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002975 if (!dev) {
2976 err = -ENOMEM;
2977 goto err_disable_clocks;
2978 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002979
2980 dev->base_addr = regs->start;
2981
2982 SET_NETDEV_DEV(dev, &pdev->dev);
2983
2984 bp = netdev_priv(dev);
2985 bp->pdev = pdev;
2986 bp->dev = dev;
2987 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002988 bp->native_io = native_io;
2989 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07002990 bp->macb_reg_readl = hw_readl_native;
2991 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002992 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07002993 bp->macb_reg_readl = hw_readl;
2994 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002995 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002996 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002997 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002998 if (macb_config)
2999 bp->dma_burst_length = macb_config->dma_burst_length;
3000 bp->pclk = pclk;
3001 bp->hclk = hclk;
3002 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303003 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003004 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303005 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303006
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003007 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003008 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003009 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3010 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3011
Harini Katakamfff80192016-08-09 13:15:53 +05303012#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3013 if (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1)) > GEM_DBW32)
3014 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3015#endif
3016
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003017 spin_lock_init(&bp->lock);
3018
Nicolas Ferread783472015-03-31 15:02:02 +02003019 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003020 macb_configure_caps(bp, macb_config);
3021
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003022 platform_set_drvdata(pdev, dev);
3023
3024 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003025 if (dev->irq < 0) {
3026 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003027 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003028 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003029
Jarod Wilson44770e12016-10-17 15:54:17 -04003030 /* MTU range: 68 - 1500 or 10240 */
3031 dev->min_mtu = GEM_MTU_MIN_SIZE;
3032 if (bp->caps & MACB_CAPS_JUMBO)
3033 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
3034 else
3035 dev->max_mtu = ETH_DATA_LEN;
3036
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003037 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003038 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003039 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003040 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003041 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003042
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003043 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003044 phy_node = of_get_next_available_child(np, NULL);
3045 if (phy_node) {
3046 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003047
Charles Keepax0e3e7992016-03-28 13:47:42 +01003048 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003049 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003050 gpiod_direction_output(bp->reset_gpio, 1);
3051 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003052 }
3053 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003054
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003055 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003056 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003057 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003058 if (pdata && pdata->is_rmii)
3059 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3060 else
3061 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3062 } else {
3063 bp->phy_interface = err;
3064 }
3065
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003066 /* IP specific init */
3067 err = init(pdev);
3068 if (err)
3069 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003070
Florian Fainellicf669662016-05-02 18:38:45 -07003071 err = macb_mii_init(bp);
3072 if (err)
3073 goto err_out_free_netdev;
3074
Philippe Reynes0a912812016-06-22 00:32:35 +02003075 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003076
3077 netif_carrier_off(dev);
3078
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003079 err = register_netdev(dev);
3080 if (err) {
3081 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003082 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003083 }
3084
Florian Fainellicf669662016-05-02 18:38:45 -07003085 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003086
Bo Shen58798232014-09-13 01:57:49 +02003087 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3088 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3089 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003090
3091 return 0;
3092
Florian Fainellicf669662016-05-02 18:38:45 -07003093err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003094 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003095 mdiobus_unregister(bp->mii_bus);
3096 mdiobus_free(bp->mii_bus);
3097
3098 /* Shutdown the PHY if there is a GPIO reset */
3099 if (bp->reset_gpio)
3100 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003101
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003102err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003103 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003104
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003105err_disable_clocks:
3106 clk_disable_unprepare(tx_clk);
3107 clk_disable_unprepare(hclk);
3108 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303109 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003110
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003111 return err;
3112}
3113
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003114static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003115{
3116 struct net_device *dev;
3117 struct macb *bp;
3118
3119 dev = platform_get_drvdata(pdev);
3120
3121 if (dev) {
3122 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003123 if (dev->phydev)
3124 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003125 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003126 dev->phydev = NULL;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003127 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003128
3129 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003130 if (bp->reset_gpio)
3131 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003132
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003133 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003134 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003135 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003136 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303137 clk_disable_unprepare(bp->rx_clk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003138 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003139 }
3140
3141 return 0;
3142}
3143
Michal Simekd23823d2015-01-23 09:36:03 +01003144static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003145{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003146 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003147 struct net_device *netdev = platform_get_drvdata(pdev);
3148 struct macb *bp = netdev_priv(netdev);
3149
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003150 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003151 netif_device_detach(netdev);
3152
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003153 if (bp->wol & MACB_WOL_ENABLED) {
3154 macb_writel(bp, IER, MACB_BIT(WOL));
3155 macb_writel(bp, WOL, MACB_BIT(MAG));
3156 enable_irq_wake(bp->queues[0].irq);
3157 } else {
3158 clk_disable_unprepare(bp->tx_clk);
3159 clk_disable_unprepare(bp->hclk);
3160 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303161 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003162 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003163
3164 return 0;
3165}
3166
Michal Simekd23823d2015-01-23 09:36:03 +01003167static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003168{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003169 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003170 struct net_device *netdev = platform_get_drvdata(pdev);
3171 struct macb *bp = netdev_priv(netdev);
3172
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003173 if (bp->wol & MACB_WOL_ENABLED) {
3174 macb_writel(bp, IDR, MACB_BIT(WOL));
3175 macb_writel(bp, WOL, 0);
3176 disable_irq_wake(bp->queues[0].irq);
3177 } else {
3178 clk_prepare_enable(bp->pclk);
3179 clk_prepare_enable(bp->hclk);
3180 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303181 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003182 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003183
3184 netif_device_attach(netdev);
3185
3186 return 0;
3187}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003188
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003189static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3190
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003191static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003192 .probe = macb_probe,
3193 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003194 .driver = {
3195 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003196 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003197 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003198 },
3199};
3200
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003201module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003202
3203MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003204MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003205MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003206MODULE_ALIAS("platform:macb");