blob: ec09fcece711dc326dca2c63db8440e193eb2b31 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
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 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000040#define RX_RING_SIZE 512 /* must be power of 2 */
41#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010042
Havard Skinnemoen55054a12012-10-31 06:04:55 +000043#define TX_RING_SIZE 128 /* must be power of 2 */
44#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010045
Nicolas Ferre909a8582012-11-19 06:00:21 +000046/* level of occupied TX descriptors under which we wake up TX process */
47#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010048
49#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
50 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000051#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
52 | MACB_BIT(ISR_RLE) \
53 | MACB_BIT(TXERR))
54#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
55
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020056#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
57#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
58
Harini Katakama5898ea2015-05-06 22:27:18 +053059#define GEM_MTU_MIN_SIZE 68
60
Sergio Prado3e2a5e12016-02-09 12:07:16 -020061#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
62#define MACB_WOL_ENABLED (0x1 << 1)
63
Moritz Fischer64ec42f2016-03-29 19:11:12 -070064/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000065 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
66 */
67#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010068
Havard Skinnemoen55054a12012-10-31 06:04:55 +000069/* Ring buffer accessors */
70static unsigned int macb_tx_ring_wrap(unsigned int index)
71{
72 return index & (TX_RING_SIZE - 1);
73}
74
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010075static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
76 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000077{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010078 return &queue->tx_ring[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000079}
80
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010081static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
82 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000083{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010084 return &queue->tx_skb[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000085}
86
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010087static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000088{
89 dma_addr_t offset;
90
91 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
92
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010093 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +000094}
95
96static unsigned int macb_rx_ring_wrap(unsigned int index)
97{
98 return index & (RX_RING_SIZE - 1);
99}
100
101static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
102{
103 return &bp->rx_ring[macb_rx_ring_wrap(index)];
104}
105
106static void *macb_rx_buffer(struct macb *bp, unsigned int index)
107{
Nicolas Ferre1b447912013-06-04 21:57:11 +0000108 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000109}
110
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300111/* I/O accessors */
112static u32 hw_readl_native(struct macb *bp, int offset)
113{
114 return __raw_readl(bp->regs + offset);
115}
116
117static void hw_writel_native(struct macb *bp, int offset, u32 value)
118{
119 __raw_writel(value, bp->regs + offset);
120}
121
122static u32 hw_readl(struct macb *bp, int offset)
123{
124 return readl_relaxed(bp->regs + offset);
125}
126
127static void hw_writel(struct macb *bp, int offset, u32 value)
128{
129 writel_relaxed(value, bp->regs + offset);
130}
131
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700132/* Find the CPU endianness by using the loopback bit of NCR register. When the
Moritz Fischer88023be2016-03-29 19:11:15 -0700133 * CPU is in big endian we need to program swapped mode for management
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300134 * descriptor access.
135 */
136static bool hw_is_native_io(void __iomem *addr)
137{
138 u32 value = MACB_BIT(LLB);
139
140 __raw_writel(value, addr + MACB_NCR);
141 value = __raw_readl(addr + MACB_NCR);
142
143 /* Write 0 back to disable everything */
144 __raw_writel(0, addr + MACB_NCR);
145
146 return value == MACB_BIT(LLB);
147}
148
149static bool hw_is_gem(void __iomem *addr, bool native_io)
150{
151 u32 id;
152
153 if (native_io)
154 id = __raw_readl(addr + MACB_MID);
155 else
156 id = readl_relaxed(addr + MACB_MID);
157
158 return MACB_BFEXT(IDNUM, id) >= 0x2;
159}
160
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100161static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100162{
163 u32 bottom;
164 u16 top;
165
166 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000167 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100168 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000169 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000170
171 /* Clear unused address register sets */
172 macb_or_gem_writel(bp, SA2B, 0);
173 macb_or_gem_writel(bp, SA2T, 0);
174 macb_or_gem_writel(bp, SA3B, 0);
175 macb_or_gem_writel(bp, SA3T, 0);
176 macb_or_gem_writel(bp, SA4B, 0);
177 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100178}
179
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100180static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100181{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000182 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100183 u32 bottom;
184 u16 top;
185 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000186 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100187
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900188 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000189
Moritz Fischeraa50b552016-03-29 19:11:13 -0700190 /* Check all 4 address register for valid address */
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000191 for (i = 0; i < 4; i++) {
192 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
193 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100194
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000195 if (pdata && pdata->rev_eth_addr) {
196 addr[5] = bottom & 0xff;
197 addr[4] = (bottom >> 8) & 0xff;
198 addr[3] = (bottom >> 16) & 0xff;
199 addr[2] = (bottom >> 24) & 0xff;
200 addr[1] = top & 0xff;
201 addr[0] = (top & 0xff00) >> 8;
202 } else {
203 addr[0] = bottom & 0xff;
204 addr[1] = (bottom >> 8) & 0xff;
205 addr[2] = (bottom >> 16) & 0xff;
206 addr[3] = (bottom >> 24) & 0xff;
207 addr[4] = top & 0xff;
208 addr[5] = (top >> 8) & 0xff;
209 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100210
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000211 if (is_valid_ether_addr(addr)) {
212 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
213 return;
214 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700215 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000216
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300217 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000218 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100219}
220
frederic RODO6c36a702007-07-12 19:07:24 +0200221static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100222{
frederic RODO6c36a702007-07-12 19:07:24 +0200223 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100224 int value;
225
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100226 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
227 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200228 | MACB_BF(PHYA, mii_id)
229 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100230 | MACB_BF(CODE, MACB_MAN_CODE)));
231
frederic RODO6c36a702007-07-12 19:07:24 +0200232 /* wait for end of transfer */
233 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
234 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100235
236 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100237
238 return value;
239}
240
frederic RODO6c36a702007-07-12 19:07:24 +0200241static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
242 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100243{
frederic RODO6c36a702007-07-12 19:07:24 +0200244 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100245
246 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
247 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200248 | MACB_BF(PHYA, mii_id)
249 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100250 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200251 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100252
frederic RODO6c36a702007-07-12 19:07:24 +0200253 /* wait for end of transfer */
254 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
255 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100256
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100257 return 0;
258}
259
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800260/**
261 * macb_set_tx_clk() - Set a clock to a new frequency
262 * @clk Pointer to the clock to change
263 * @rate New frequency in Hz
264 * @dev Pointer to the struct net_device
265 */
266static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
267{
268 long ferr, rate, rate_rounded;
269
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100270 if (!clk)
271 return;
272
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800273 switch (speed) {
274 case SPEED_10:
275 rate = 2500000;
276 break;
277 case SPEED_100:
278 rate = 25000000;
279 break;
280 case SPEED_1000:
281 rate = 125000000;
282 break;
283 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800284 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800285 }
286
287 rate_rounded = clk_round_rate(clk, rate);
288 if (rate_rounded < 0)
289 return;
290
291 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
292 * is not satisfied.
293 */
294 ferr = abs(rate_rounded - rate);
295 ferr = DIV_ROUND_UP(ferr, rate / 100000);
296 if (ferr > 5)
297 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700298 rate);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800299
300 if (clk_set_rate(clk, rate_rounded))
301 netdev_err(dev, "adjusting tx_clk failed.\n");
302}
303
frederic RODO6c36a702007-07-12 19:07:24 +0200304static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100305{
frederic RODO6c36a702007-07-12 19:07:24 +0200306 struct macb *bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +0200307 struct phy_device *phydev = dev->phydev;
frederic RODO6c36a702007-07-12 19:07:24 +0200308 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200309 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100310
frederic RODO6c36a702007-07-12 19:07:24 +0200311 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100312
frederic RODO6c36a702007-07-12 19:07:24 +0200313 if (phydev->link) {
314 if ((bp->speed != phydev->speed) ||
315 (bp->duplex != phydev->duplex)) {
316 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100317
frederic RODO6c36a702007-07-12 19:07:24 +0200318 reg = macb_readl(bp, NCFGR);
319 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000320 if (macb_is_gem(bp))
321 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200322
323 if (phydev->duplex)
324 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900325 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200326 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200327 if (phydev->speed == SPEED_1000 &&
328 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000329 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200330
Patrice Vilchez140b7552012-10-31 06:04:50 +0000331 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200332
333 bp->speed = phydev->speed;
334 bp->duplex = phydev->duplex;
335 status_change = 1;
336 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100337 }
338
frederic RODO6c36a702007-07-12 19:07:24 +0200339 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700340 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200341 bp->speed = 0;
342 bp->duplex = -1;
343 }
344 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100345
frederic RODO6c36a702007-07-12 19:07:24 +0200346 status_change = 1;
347 }
348
349 spin_unlock_irqrestore(&bp->lock, flags);
350
351 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000352 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500353 /* Update the TX clock rate if and only if the link is
354 * up and there has been a link change.
355 */
356 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
357
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000358 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000359 netdev_info(dev, "link up (%d/%s)\n",
360 phydev->speed,
361 phydev->duplex == DUPLEX_FULL ?
362 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000363 } else {
364 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000365 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000366 }
frederic RODO6c36a702007-07-12 19:07:24 +0200367 }
368}
369
370/* based on au1000_eth. c*/
371static int macb_mii_probe(struct net_device *dev)
372{
373 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000374 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000375 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000376 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000377 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200378
Jiri Pirko7455a762010-02-08 05:12:08 +0000379 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200380 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000381 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200382 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200383 }
384
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000385 pdata = dev_get_platdata(&bp->pdev->dev);
386 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700387 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin,
388 "phy int");
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000389 if (!ret) {
390 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
391 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
392 }
393 }
frederic RODO6c36a702007-07-12 19:07:24 +0200394
395 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000396 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100397 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000398 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000399 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000400 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200401 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100402
frederic RODO6c36a702007-07-12 19:07:24 +0200403 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200404 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000405 phydev->supported &= PHY_GBIT_FEATURES;
406 else
407 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100408
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500409 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
410 phydev->supported &= ~SUPPORTED_1000baseT_Half;
411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100413
frederic RODO6c36a702007-07-12 19:07:24 +0200414 bp->link = 0;
415 bp->speed = 0;
416 bp->duplex = -1;
frederic RODO6c36a702007-07-12 19:07:24 +0200417
418 return 0;
419}
420
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100421static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200422{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000423 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200424 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200425 int err = -ENXIO, i;
426
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200427 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200428 macb_writel(bp, NCR, MACB_BIT(MPE));
429
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700430 bp->mii_bus = mdiobus_alloc();
Moritz Fischeraa50b552016-03-29 19:11:13 -0700431 if (!bp->mii_bus) {
frederic RODO6c36a702007-07-12 19:07:24 +0200432 err = -ENOMEM;
433 goto err_out;
434 }
435
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700436 bp->mii_bus->name = "MACB_mii_bus";
437 bp->mii_bus->read = &macb_mdio_read;
438 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000439 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700440 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700441 bp->mii_bus->priv = bp;
Florian Fainellicf669662016-05-02 18:38:45 -0700442 bp->mii_bus->parent = &bp->pdev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900443 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700444
Jamie Iles91523942011-02-28 04:05:25 +0000445 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200446
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200447 np = bp->pdev->dev.of_node;
448 if (np) {
449 /* try dt phy registration */
450 err = of_mdiobus_register(bp->mii_bus, np);
451
452 /* fallback to standard phy registration if no phy were
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700453 * found during dt phy registration
454 */
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200455 if (!err && !phy_find_first(bp->mii_bus)) {
456 for (i = 0; i < PHY_MAX_ADDR; i++) {
457 struct phy_device *phydev;
458
459 phydev = mdiobus_scan(bp->mii_bus, i);
Sergei Shtylyovce24c2b2016-05-01 01:47:36 +0300460 if (IS_ERR(phydev) &&
461 PTR_ERR(phydev) != -ENODEV) {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200462 err = PTR_ERR(phydev);
463 break;
464 }
465 }
466
467 if (err)
468 goto err_out_unregister_bus;
469 }
470 } else {
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200471 if (pdata)
472 bp->mii_bus->phy_mask = pdata->phy_mask;
473
474 err = mdiobus_register(bp->mii_bus);
475 }
476
477 if (err)
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100478 goto err_out_free_mdiobus;
frederic RODO6c36a702007-07-12 19:07:24 +0200479
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200480 err = macb_mii_probe(bp->dev);
481 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200482 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200483
484 return 0;
485
486err_out_unregister_bus:
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700487 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700488err_out_free_mdiobus:
489 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200490err_out:
491 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100492}
493
494static void macb_update_stats(struct macb *bp)
495{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000496 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
497 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300498 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100499
500 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
501
Moritz Fischer96ec6312016-03-29 19:11:11 -0700502 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700503 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100504}
505
Nicolas Ferree86cd532012-10-31 06:04:57 +0000506static int macb_halt_tx(struct macb *bp)
507{
508 unsigned long halt_time, timeout;
509 u32 status;
510
511 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
512
513 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
514 do {
515 halt_time = jiffies;
516 status = macb_readl(bp, TSR);
517 if (!(status & MACB_BIT(TGO)))
518 return 0;
519
520 usleep_range(10, 250);
521 } while (time_before(halt_time, timeout));
522
523 return -ETIMEDOUT;
524}
525
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200526static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
527{
528 if (tx_skb->mapping) {
529 if (tx_skb->mapped_as_page)
530 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
531 tx_skb->size, DMA_TO_DEVICE);
532 else
533 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
534 tx_skb->size, DMA_TO_DEVICE);
535 tx_skb->mapping = 0;
536 }
537
538 if (tx_skb->skb) {
539 dev_kfree_skb_any(tx_skb->skb);
540 tx_skb->skb = NULL;
541 }
542}
543
Harini Katakamfff80192016-08-09 13:15:53 +0530544static inline void macb_set_addr(struct macb_dma_desc *desc, dma_addr_t addr)
545{
546 desc->addr = (u32)addr;
547#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
548 desc->addrh = (u32)(addr >> 32);
549#endif
550}
551
Nicolas Ferree86cd532012-10-31 06:04:57 +0000552static void macb_tx_error_task(struct work_struct *work)
553{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100554 struct macb_queue *queue = container_of(work, struct macb_queue,
555 tx_error_task);
556 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000557 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100558 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000559 struct sk_buff *skb;
560 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100561 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000562
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100563 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
564 (unsigned int)(queue - bp->queues),
565 queue->tx_tail, queue->tx_head);
566
567 /* Prevent the queue IRQ handlers from running: each of them may call
568 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
569 * As explained below, we have to halt the transmission before updating
570 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
571 * network engine about the macb/gem being halted.
572 */
573 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000574
575 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100576 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000577
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700578 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000579 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100580 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000581 */
582 if (macb_halt_tx(bp))
583 /* Just complain for now, reinitializing TX path can be good */
584 netdev_err(bp->dev, "BUG: halt tx timed out\n");
585
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700586 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000587 * Free transmit buffers in upper layer.
588 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100589 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
590 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000591
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100592 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000593 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100594 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000595 skb = tx_skb->skb;
596
597 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200598 /* skb is set for the last buffer of the frame */
599 while (!skb) {
600 macb_tx_unmap(bp, tx_skb);
601 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100602 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200603 skb = tx_skb->skb;
604 }
605
606 /* ctrl still refers to the first buffer descriptor
607 * since it's the only one written back by the hardware
608 */
609 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
610 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
611 macb_tx_ring_wrap(tail), skb->data);
612 bp->stats.tx_packets++;
613 bp->stats.tx_bytes += skb->len;
614 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000615 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700616 /* "Buffers exhausted mid-frame" errors may only happen
617 * if the driver is buggy, so complain loudly about
618 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000619 */
620 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
621 netdev_err(bp->dev,
622 "BUG: TX buffers exhausted mid-frame\n");
623
624 desc->ctrl = ctrl | MACB_BIT(TX_USED);
625 }
626
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200627 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000628 }
629
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100630 /* Set end of TX queue */
631 desc = macb_tx_desc(queue, 0);
Harini Katakamfff80192016-08-09 13:15:53 +0530632 macb_set_addr(desc, 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100633 desc->ctrl = MACB_BIT(TX_USED);
634
Nicolas Ferree86cd532012-10-31 06:04:57 +0000635 /* Make descriptor updates visible to hardware */
636 wmb();
637
638 /* Reinitialize the TX desc queue */
Harini Katakamfff80192016-08-09 13:15:53 +0530639 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
640#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
641 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
642#endif
Nicolas Ferree86cd532012-10-31 06:04:57 +0000643 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100644 queue->tx_head = 0;
645 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000646
647 /* Housework before enabling TX IRQ */
648 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100649 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
650
651 /* Now we are ready to start transmission again */
652 netif_tx_start_all_queues(bp->dev);
653 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
654
655 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000656}
657
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100658static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100659{
660 unsigned int tail;
661 unsigned int head;
662 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100663 struct macb *bp = queue->bp;
664 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100665
666 status = macb_readl(bp, TSR);
667 macb_writel(bp, TSR, status);
668
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000669 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100670 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000671
Nicolas Ferree86cd532012-10-31 06:04:57 +0000672 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700673 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100675 head = queue->tx_head;
676 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000677 struct macb_tx_skb *tx_skb;
678 struct sk_buff *skb;
679 struct macb_dma_desc *desc;
680 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100681
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100682 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100683
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000684 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100685 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000686
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000687 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100688
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200689 /* TX_USED bit is only set by hardware on the very first buffer
690 * descriptor of the transmitted frame.
691 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000692 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100693 break;
694
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200695 /* Process all buffers of the current transmitted frame */
696 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100697 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200698 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000699
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200700 /* First, update TX stats if needed */
701 if (skb) {
702 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
703 macb_tx_ring_wrap(tail), skb->data);
704 bp->stats.tx_packets++;
705 bp->stats.tx_bytes += skb->len;
706 }
707
708 /* Now we can safely release resources */
709 macb_tx_unmap(bp, tx_skb);
710
711 /* skb is set only for the last buffer of the frame.
712 * WARNING: at this point skb has been freed by
713 * macb_tx_unmap().
714 */
715 if (skb)
716 break;
717 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100718 }
719
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100720 queue->tx_tail = tail;
721 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
722 CIRC_CNT(queue->tx_head, queue->tx_tail,
723 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
724 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100725}
726
Nicolas Ferre4df95132013-06-04 21:57:12 +0000727static void gem_rx_refill(struct macb *bp)
728{
729 unsigned int entry;
730 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000731 dma_addr_t paddr;
732
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700733 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
734 RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000735 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000736
737 /* Make hw descriptor updates visible to CPU */
738 rmb();
739
Nicolas Ferre4df95132013-06-04 21:57:12 +0000740 bp->rx_prepared_head++;
741
Moritz Fischeraa50b552016-03-29 19:11:13 -0700742 if (!bp->rx_skbuff[entry]) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000743 /* allocate sk_buff for this free entry in ring */
744 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
Moritz Fischeraa50b552016-03-29 19:11:13 -0700745 if (unlikely(!skb)) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000746 netdev_err(bp->dev,
747 "Unable to allocate sk_buff\n");
748 break;
749 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000750
751 /* now fill corresponding descriptor entry */
752 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700753 bp->rx_buffer_size,
754 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800755 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
756 dev_kfree_skb(skb);
757 break;
758 }
759
760 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000761
762 if (entry == RX_RING_SIZE - 1)
763 paddr |= MACB_BIT(RX_WRAP);
Harini Katakamfff80192016-08-09 13:15:53 +0530764 macb_set_addr(&(bp->rx_ring[entry]), paddr);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000765 bp->rx_ring[entry].ctrl = 0;
766
767 /* properly align Ethernet header */
768 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530769 } else {
770 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
771 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000772 }
773 }
774
775 /* Make descriptor updates visible to hardware */
776 wmb();
777
778 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700779 bp->rx_prepared_head, bp->rx_tail);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000780}
781
782/* Mark DMA descriptors from begin up to and not including end as unused */
783static void discard_partial_frame(struct macb *bp, unsigned int begin,
784 unsigned int end)
785{
786 unsigned int frag;
787
788 for (frag = begin; frag != end; frag++) {
789 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700790
Nicolas Ferre4df95132013-06-04 21:57:12 +0000791 desc->addr &= ~MACB_BIT(RX_USED);
792 }
793
794 /* Make descriptor updates visible to hardware */
795 wmb();
796
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700797 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000798 * whatever caused this is updated, so we don't have to record
799 * anything.
800 */
801}
802
803static int gem_rx(struct macb *bp, int budget)
804{
805 unsigned int len;
806 unsigned int entry;
807 struct sk_buff *skb;
808 struct macb_dma_desc *desc;
809 int count = 0;
810
811 while (count < budget) {
Harini Katakamfff80192016-08-09 13:15:53 +0530812 u32 ctrl;
813 dma_addr_t addr;
814 bool rxused;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000815
816 entry = macb_rx_ring_wrap(bp->rx_tail);
817 desc = &bp->rx_ring[entry];
818
819 /* Make hw descriptor updates visible to CPU */
820 rmb();
821
Harini Katakamfff80192016-08-09 13:15:53 +0530822 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
823 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
824#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
825 addr |= ((u64)(desc->addrh) << 32);
826#endif
Nicolas Ferre4df95132013-06-04 21:57:12 +0000827 ctrl = desc->ctrl;
828
Harini Katakamfff80192016-08-09 13:15:53 +0530829 if (!rxused)
Nicolas Ferre4df95132013-06-04 21:57:12 +0000830 break;
831
Nicolas Ferre4df95132013-06-04 21:57:12 +0000832 bp->rx_tail++;
833 count++;
834
835 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
836 netdev_err(bp->dev,
837 "not whole frame pointed by descriptor\n");
838 bp->stats.rx_dropped++;
839 break;
840 }
841 skb = bp->rx_skbuff[entry];
842 if (unlikely(!skb)) {
843 netdev_err(bp->dev,
844 "inconsistent Rx descriptor chain\n");
845 bp->stats.rx_dropped++;
846 break;
847 }
848 /* now everything is ready for receiving packet */
849 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530850 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000851
852 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
853
854 skb_put(skb, len);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000855 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800856 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000857
858 skb->protocol = eth_type_trans(skb, bp->dev);
859 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200860 if (bp->dev->features & NETIF_F_RXCSUM &&
861 !(bp->dev->flags & IFF_PROMISC) &&
862 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
863 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000864
865 bp->stats.rx_packets++;
866 bp->stats.rx_bytes += skb->len;
867
868#if defined(DEBUG) && defined(VERBOSE_DEBUG)
869 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
870 skb->len, skb->csum);
871 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100872 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000873 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
874 skb->data, 32, true);
875#endif
876
877 netif_receive_skb(skb);
878 }
879
880 gem_rx_refill(bp);
881
882 return count;
883}
884
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100885static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
886 unsigned int last_frag)
887{
888 unsigned int len;
889 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000890 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100891 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000892 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100893
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000894 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530895 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100896
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000897 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700898 macb_rx_ring_wrap(first_frag),
899 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100900
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700901 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000902 * first buffer. Since the header is 14 bytes, this makes the
903 * payload word-aligned.
904 *
905 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
906 * the two padding bytes into the skb so that we avoid hitting
907 * the slowpath in memcpy(), and pull them off afterwards.
908 */
909 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100910 if (!skb) {
911 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000912 for (frag = first_frag; ; frag++) {
913 desc = macb_rx_desc(bp, frag);
914 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100915 if (frag == last_frag)
916 break;
917 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000918
919 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100920 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000921
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100922 return 1;
923 }
924
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000925 offset = 0;
926 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700927 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100928 skb_put(skb, len);
929
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000930 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000931 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100932
933 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100934 if (unlikely(frag != last_frag)) {
935 dev_kfree_skb_any(skb);
936 return -1;
937 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100938 frag_len = len - offset;
939 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300940 skb_copy_to_linear_data_offset(skb, offset,
Moritz Fischeraa50b552016-03-29 19:11:13 -0700941 macb_rx_buffer(bp, frag),
942 frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000943 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000944 desc = macb_rx_desc(bp, frag);
945 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100946
947 if (frag == last_frag)
948 break;
949 }
950
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000951 /* Make descriptor updates visible to hardware */
952 wmb();
953
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000954 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100955 skb->protocol = eth_type_trans(skb, bp->dev);
956
957 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000958 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000959 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -0700960 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100961 netif_receive_skb(skb);
962
963 return 0;
964}
965
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100966static inline void macb_init_rx_ring(struct macb *bp)
967{
968 dma_addr_t addr;
969 int i;
970
971 addr = bp->rx_buffers_dma;
972 for (i = 0; i < RX_RING_SIZE; i++) {
973 bp->rx_ring[i].addr = addr;
974 bp->rx_ring[i].ctrl = 0;
975 addr += bp->rx_buffer_size;
976 }
977 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
Cyrille Pitchena0b44ee2016-11-28 14:40:55 +0100978 bp->rx_tail = 0;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100979}
980
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100981static int macb_rx(struct macb *bp, int budget)
982{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100983 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100984 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000985 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100986 int first_frag = -1;
987
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000988 for (tail = bp->rx_tail; budget > 0; tail++) {
989 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100990 u32 addr, ctrl;
991
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000992 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100993 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000994
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000995 addr = desc->addr;
996 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100997
998 if (!(addr & MACB_BIT(RX_USED)))
999 break;
1000
1001 if (ctrl & MACB_BIT(RX_SOF)) {
1002 if (first_frag != -1)
1003 discard_partial_frame(bp, first_frag, tail);
1004 first_frag = tail;
1005 }
1006
1007 if (ctrl & MACB_BIT(RX_EOF)) {
1008 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001009
1010 if (unlikely(first_frag == -1)) {
1011 reset_rx_queue = true;
1012 continue;
1013 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001014
1015 dropped = macb_rx_frame(bp, first_frag, tail);
1016 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001017 if (unlikely(dropped < 0)) {
1018 reset_rx_queue = true;
1019 continue;
1020 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001021 if (!dropped) {
1022 received++;
1023 budget--;
1024 }
1025 }
1026 }
1027
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001028 if (unlikely(reset_rx_queue)) {
1029 unsigned long flags;
1030 u32 ctrl;
1031
1032 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1033
1034 spin_lock_irqsave(&bp->lock, flags);
1035
1036 ctrl = macb_readl(bp, NCR);
1037 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1038
1039 macb_init_rx_ring(bp);
1040 macb_writel(bp, RBQP, bp->rx_ring_dma);
1041
1042 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1043
1044 spin_unlock_irqrestore(&bp->lock, flags);
1045 return received;
1046 }
1047
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001048 if (first_frag != -1)
1049 bp->rx_tail = first_frag;
1050 else
1051 bp->rx_tail = tail;
1052
1053 return received;
1054}
1055
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001056static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001057{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001058 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001059 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001060 u32 status;
1061
1062 status = macb_readl(bp, RSR);
1063 macb_writel(bp, RSR, status);
1064
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001065 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001066
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001067 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001068 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001069
Nicolas Ferre4df95132013-06-04 21:57:12 +00001070 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001071 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001072 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001073
Nicolas Ferre8770e912013-02-12 11:08:48 +01001074 /* Packets received while interrupts were disabled */
1075 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001076 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001077 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1078 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001079 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001080 } else {
1081 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1082 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001083 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001084
1085 /* TODO: Handle errors */
1086
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001087 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001088}
1089
1090static irqreturn_t macb_interrupt(int irq, void *dev_id)
1091{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001092 struct macb_queue *queue = dev_id;
1093 struct macb *bp = queue->bp;
1094 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001095 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001096
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001097 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001098
1099 if (unlikely(!status))
1100 return IRQ_NONE;
1101
1102 spin_lock(&bp->lock);
1103
1104 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001105 /* close possible race with dev_close */
1106 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001107 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001108 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1109 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001110 break;
1111 }
1112
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001113 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1114 (unsigned int)(queue - bp->queues),
1115 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001116
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001117 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001118 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001119 * until we have processed the buffers. The
1120 * scheduling call may fail if the poll routine
1121 * is already scheduled, so disable interrupts
1122 * now.
1123 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001124 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001125 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001126 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001127
Ben Hutchings288379f2009-01-19 16:43:59 -08001128 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001129 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001130 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001131 }
1132 }
1133
Nicolas Ferree86cd532012-10-31 06:04:57 +00001134 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001135 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1136 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001137
1138 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001139 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001140
Nicolas Ferree86cd532012-10-31 06:04:57 +00001141 break;
1142 }
1143
1144 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001145 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001146
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001147 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001148 * add that if/when we get our hands on a full-blown MII PHY.
1149 */
1150
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001151 /* There is a hardware issue under heavy load where DMA can
1152 * stop, this causes endless "used buffer descriptor read"
1153 * interrupts but it can be cleared by re-enabling RX. See
1154 * the at91 manual, section 41.3.1 or the Zynq manual
1155 * section 16.7.4 for details.
1156 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001157 if (status & MACB_BIT(RXUBR)) {
1158 ctrl = macb_readl(bp, NCR);
1159 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08001160 wmb();
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001161 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1162
1163 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001164 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001165 }
1166
Alexander Steinb19f7f72011-04-13 05:03:24 +00001167 if (status & MACB_BIT(ISR_ROVR)) {
1168 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001169 if (macb_is_gem(bp))
1170 bp->hw_stats.gem.rx_overruns++;
1171 else
1172 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001173
1174 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001175 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001176 }
1177
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001178 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001179 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001180 * netdev_err to a lower-priority context as well
1181 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001182 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001183 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001184
1185 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001186 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001187 }
1188
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001189 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001190 }
1191
1192 spin_unlock(&bp->lock);
1193
1194 return IRQ_HANDLED;
1195}
1196
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001197#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001198/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001199 * to allow network i/o with interrupts disabled.
1200 */
1201static void macb_poll_controller(struct net_device *dev)
1202{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001203 struct macb *bp = netdev_priv(dev);
1204 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001205 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001206 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001207
1208 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001209 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1210 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001211 local_irq_restore(flags);
1212}
1213#endif
1214
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001215static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001216 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001217 struct sk_buff *skb)
1218{
1219 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001220 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001221 struct macb_tx_skb *tx_skb = NULL;
1222 struct macb_dma_desc *desc;
1223 unsigned int offset, size, count = 0;
1224 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1225 unsigned int eof = 1;
1226 u32 ctrl;
1227
1228 /* First, map non-paged data */
1229 len = skb_headlen(skb);
1230 offset = 0;
1231 while (len) {
1232 size = min(len, bp->max_tx_length);
1233 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001234 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001235
1236 mapping = dma_map_single(&bp->pdev->dev,
1237 skb->data + offset,
1238 size, DMA_TO_DEVICE);
1239 if (dma_mapping_error(&bp->pdev->dev, mapping))
1240 goto dma_error;
1241
1242 /* Save info to properly release resources */
1243 tx_skb->skb = NULL;
1244 tx_skb->mapping = mapping;
1245 tx_skb->size = size;
1246 tx_skb->mapped_as_page = false;
1247
1248 len -= size;
1249 offset += size;
1250 count++;
1251 tx_head++;
1252 }
1253
1254 /* Then, map paged data from fragments */
1255 for (f = 0; f < nr_frags; f++) {
1256 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1257
1258 len = skb_frag_size(frag);
1259 offset = 0;
1260 while (len) {
1261 size = min(len, bp->max_tx_length);
1262 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001263 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001264
1265 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1266 offset, size, DMA_TO_DEVICE);
1267 if (dma_mapping_error(&bp->pdev->dev, mapping))
1268 goto dma_error;
1269
1270 /* Save info to properly release resources */
1271 tx_skb->skb = NULL;
1272 tx_skb->mapping = mapping;
1273 tx_skb->size = size;
1274 tx_skb->mapped_as_page = true;
1275
1276 len -= size;
1277 offset += size;
1278 count++;
1279 tx_head++;
1280 }
1281 }
1282
1283 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001284 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001285 netdev_err(bp->dev, "BUG! empty skb!\n");
1286 return 0;
1287 }
1288
1289 /* This is the last buffer of the frame: save socket buffer */
1290 tx_skb->skb = skb;
1291
1292 /* Update TX ring: update buffer descriptors in reverse order
1293 * to avoid race condition
1294 */
1295
1296 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1297 * to set the end of TX queue
1298 */
1299 i = tx_head;
1300 entry = macb_tx_ring_wrap(i);
1301 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001302 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001303 desc->ctrl = ctrl;
1304
1305 do {
1306 i--;
1307 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001308 tx_skb = &queue->tx_skb[entry];
1309 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001310
1311 ctrl = (u32)tx_skb->size;
1312 if (eof) {
1313 ctrl |= MACB_BIT(TX_LAST);
1314 eof = 0;
1315 }
1316 if (unlikely(entry == (TX_RING_SIZE - 1)))
1317 ctrl |= MACB_BIT(TX_WRAP);
1318
1319 /* Set TX buffer descriptor */
Harini Katakamfff80192016-08-09 13:15:53 +05301320 macb_set_addr(desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001321 /* desc->addr must be visible to hardware before clearing
1322 * 'TX_USED' bit in desc->ctrl.
1323 */
1324 wmb();
1325 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001326 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001327
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001328 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001329
1330 return count;
1331
1332dma_error:
1333 netdev_err(bp->dev, "TX DMA map failed\n");
1334
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001335 for (i = queue->tx_head; i != tx_head; i++) {
1336 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001337
1338 macb_tx_unmap(bp, tx_skb);
1339 }
1340
1341 return 0;
1342}
1343
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001344static inline int macb_clear_csum(struct sk_buff *skb)
1345{
1346 /* no change for packets without checksum offloading */
1347 if (skb->ip_summed != CHECKSUM_PARTIAL)
1348 return 0;
1349
1350 /* make sure we can modify the header */
1351 if (unlikely(skb_cow_head(skb, 0)))
1352 return -1;
1353
1354 /* initialize checksum field
1355 * This is required - at least for Zynq, which otherwise calculates
1356 * wrong UDP header checksums for UDP packets with UDP data len <=2
1357 */
1358 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1359 return 0;
1360}
1361
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001362static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1363{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001364 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001365 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001366 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001367 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001368 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001369
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001370#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1371 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001372 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1373 queue_index, skb->len, skb->head, skb->data,
1374 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001375 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1376 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001377#endif
1378
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001379 /* Count how many TX buffer descriptors are needed to send this
1380 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001381 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001382 */
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001383 count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001384 nr_frags = skb_shinfo(skb)->nr_frags;
1385 for (f = 0; f < nr_frags; f++) {
1386 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001387 count += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001388 }
1389
Dongdong Deng48719532009-08-23 19:49:07 -07001390 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001391
1392 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001393 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1394 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001395 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001396 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001397 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001398 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001399 }
1400
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001401 if (macb_clear_csum(skb)) {
1402 dev_kfree_skb_any(skb);
Wei Yongjuna7c22bd2016-09-10 11:17:57 +00001403 goto unlock;
Helmut Buchsbaum007e4ba2016-09-04 18:09:47 +02001404 }
1405
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001406 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001407 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001408 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001409 goto unlock;
1410 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001411
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001412 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001413 wmb();
1414
Richard Cochrane0720922011-06-19 21:51:28 +00001415 skb_tx_timestamp(skb);
1416
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001417 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1418
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001419 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1420 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001421
Soren Brinkmann92030902014-03-04 08:46:39 -08001422unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001423 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001424
Patrick McHardy6ed10652009-06-23 06:03:08 +00001425 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001426}
1427
Nicolas Ferre4df95132013-06-04 21:57:12 +00001428static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001429{
1430 if (!macb_is_gem(bp)) {
1431 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1432 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001433 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001434
Nicolas Ferre1b447912013-06-04 21:57:11 +00001435 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001436 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001437 "RX buffer must be multiple of %d bytes, expanding\n",
1438 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001439 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001440 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001441 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001442 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001443
1444 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1445 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001446}
1447
Nicolas Ferre4df95132013-06-04 21:57:12 +00001448static void gem_free_rx_buffers(struct macb *bp)
1449{
1450 struct sk_buff *skb;
1451 struct macb_dma_desc *desc;
1452 dma_addr_t addr;
1453 int i;
1454
1455 if (!bp->rx_skbuff)
1456 return;
1457
1458 for (i = 0; i < RX_RING_SIZE; i++) {
1459 skb = bp->rx_skbuff[i];
1460
Moritz Fischeraa50b552016-03-29 19:11:13 -07001461 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001462 continue;
1463
1464 desc = &bp->rx_ring[i];
1465 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Harini Katakamfff80192016-08-09 13:15:53 +05301466#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1467 addr |= ((u64)(desc->addrh) << 32);
1468#endif
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001469 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001470 DMA_FROM_DEVICE);
1471 dev_kfree_skb_any(skb);
1472 skb = NULL;
1473 }
1474
1475 kfree(bp->rx_skbuff);
1476 bp->rx_skbuff = NULL;
1477}
1478
1479static void macb_free_rx_buffers(struct macb *bp)
1480{
1481 if (bp->rx_buffers) {
1482 dma_free_coherent(&bp->pdev->dev,
1483 RX_RING_SIZE * bp->rx_buffer_size,
1484 bp->rx_buffers, bp->rx_buffers_dma);
1485 bp->rx_buffers = NULL;
1486 }
1487}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001488
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001489static void macb_free_consistent(struct macb *bp)
1490{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001491 struct macb_queue *queue;
1492 unsigned int q;
1493
Nicolas Ferre4df95132013-06-04 21:57:12 +00001494 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001495 if (bp->rx_ring) {
1496 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1497 bp->rx_ring, bp->rx_ring_dma);
1498 bp->rx_ring = NULL;
1499 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001500
1501 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1502 kfree(queue->tx_skb);
1503 queue->tx_skb = NULL;
1504 if (queue->tx_ring) {
1505 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1506 queue->tx_ring, queue->tx_ring_dma);
1507 queue->tx_ring = NULL;
1508 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001509 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001510}
1511
1512static int gem_alloc_rx_buffers(struct macb *bp)
1513{
1514 int size;
1515
1516 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1517 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1518 if (!bp->rx_skbuff)
1519 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001520
1521 netdev_dbg(bp->dev,
1522 "Allocated %d RX struct sk_buff entries at %p\n",
1523 RX_RING_SIZE, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001524 return 0;
1525}
1526
1527static int macb_alloc_rx_buffers(struct macb *bp)
1528{
1529 int size;
1530
1531 size = RX_RING_SIZE * bp->rx_buffer_size;
1532 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1533 &bp->rx_buffers_dma, GFP_KERNEL);
1534 if (!bp->rx_buffers)
1535 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001536
1537 netdev_dbg(bp->dev,
1538 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1539 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001540 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001541}
1542
1543static int macb_alloc_consistent(struct macb *bp)
1544{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001545 struct macb_queue *queue;
1546 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001547 int size;
1548
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001549 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1550 size = TX_RING_BYTES;
1551 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1552 &queue->tx_ring_dma,
1553 GFP_KERNEL);
1554 if (!queue->tx_ring)
1555 goto out_err;
1556 netdev_dbg(bp->dev,
1557 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1558 q, size, (unsigned long)queue->tx_ring_dma,
1559 queue->tx_ring);
1560
1561 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1562 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1563 if (!queue->tx_skb)
1564 goto out_err;
1565 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001566
1567 size = RX_RING_BYTES;
1568 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1569 &bp->rx_ring_dma, GFP_KERNEL);
1570 if (!bp->rx_ring)
1571 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001572 netdev_dbg(bp->dev,
1573 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1574 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001575
Nicolas Ferre4df95132013-06-04 21:57:12 +00001576 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001577 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001578
1579 return 0;
1580
1581out_err:
1582 macb_free_consistent(bp);
1583 return -ENOMEM;
1584}
1585
Nicolas Ferre4df95132013-06-04 21:57:12 +00001586static void gem_init_rings(struct macb *bp)
1587{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001588 struct macb_queue *queue;
1589 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001590 int i;
1591
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001592 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1593 for (i = 0; i < TX_RING_SIZE; i++) {
Harini Katakamfff80192016-08-09 13:15:53 +05301594 macb_set_addr(&(queue->tx_ring[i]), 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001595 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1596 }
1597 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1598 queue->tx_head = 0;
1599 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001600 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001601
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001602 bp->rx_tail = 0;
1603 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001604
1605 gem_rx_refill(bp);
1606}
1607
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001608static void macb_init_rings(struct macb *bp)
1609{
1610 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001611
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001612 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001613
1614 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001615 bp->queues[0].tx_ring[i].addr = 0;
1616 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001617 }
Ben Shelton21d35152015-04-22 17:28:54 -05001618 bp->queues[0].tx_head = 0;
1619 bp->queues[0].tx_tail = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001620 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001621}
1622
1623static void macb_reset_hw(struct macb *bp)
1624{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001625 struct macb_queue *queue;
1626 unsigned int q;
1627
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001628 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001629 * more gracefully?)
1630 */
1631 macb_writel(bp, NCR, 0);
1632
1633 /* Clear the stats registers (XXX: Update stats first?) */
1634 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1635
1636 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001637 macb_writel(bp, TSR, -1);
1638 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001639
1640 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001641 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1642 queue_writel(queue, IDR, -1);
1643 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001644 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1645 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001646 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001647}
1648
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001649static u32 gem_mdc_clk_div(struct macb *bp)
1650{
1651 u32 config;
1652 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1653
1654 if (pclk_hz <= 20000000)
1655 config = GEM_BF(CLK, GEM_CLK_DIV8);
1656 else if (pclk_hz <= 40000000)
1657 config = GEM_BF(CLK, GEM_CLK_DIV16);
1658 else if (pclk_hz <= 80000000)
1659 config = GEM_BF(CLK, GEM_CLK_DIV32);
1660 else if (pclk_hz <= 120000000)
1661 config = GEM_BF(CLK, GEM_CLK_DIV48);
1662 else if (pclk_hz <= 160000000)
1663 config = GEM_BF(CLK, GEM_CLK_DIV64);
1664 else
1665 config = GEM_BF(CLK, GEM_CLK_DIV96);
1666
1667 return config;
1668}
1669
1670static u32 macb_mdc_clk_div(struct macb *bp)
1671{
1672 u32 config;
1673 unsigned long pclk_hz;
1674
1675 if (macb_is_gem(bp))
1676 return gem_mdc_clk_div(bp);
1677
1678 pclk_hz = clk_get_rate(bp->pclk);
1679 if (pclk_hz <= 20000000)
1680 config = MACB_BF(CLK, MACB_CLK_DIV8);
1681 else if (pclk_hz <= 40000000)
1682 config = MACB_BF(CLK, MACB_CLK_DIV16);
1683 else if (pclk_hz <= 80000000)
1684 config = MACB_BF(CLK, MACB_CLK_DIV32);
1685 else
1686 config = MACB_BF(CLK, MACB_CLK_DIV64);
1687
1688 return config;
1689}
1690
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001691/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001692 * should program. We find the width from decoding the design configuration
1693 * register to find the maximum supported data bus width.
1694 */
1695static u32 macb_dbw(struct macb *bp)
1696{
1697 if (!macb_is_gem(bp))
1698 return 0;
1699
1700 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1701 case 4:
1702 return GEM_BF(DBW, GEM_DBW128);
1703 case 2:
1704 return GEM_BF(DBW, GEM_DBW64);
1705 case 1:
1706 default:
1707 return GEM_BF(DBW, GEM_DBW32);
1708 }
1709}
1710
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001711/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001712 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001713 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001714 * (if not supported by FIFO, it will fallback to default)
1715 * - set both rx/tx packet buffers to full memory size
1716 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001717 */
1718static void macb_configure_dma(struct macb *bp)
1719{
1720 u32 dmacfg;
1721
1722 if (macb_is_gem(bp)) {
1723 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001724 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001725 if (bp->dma_burst_length)
1726 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001727 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301728 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301729
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001730 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301731 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1732 else
1733 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1734
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001735 if (bp->dev->features & NETIF_F_HW_CSUM)
1736 dmacfg |= GEM_BIT(TXCOEN);
1737 else
1738 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301739
1740#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1741 dmacfg |= GEM_BIT(ADDR64);
1742#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001743 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1744 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001745 gem_writel(bp, DMACFG, dmacfg);
1746 }
1747}
1748
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001749static void macb_init_hw(struct macb *bp)
1750{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001751 struct macb_queue *queue;
1752 unsigned int q;
1753
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001754 u32 config;
1755
1756 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001757 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001758
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001759 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301760 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1761 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001762 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001763 config |= MACB_BIT(PAE); /* PAuse Enable */
1764 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001765 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301766 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1767 else
1768 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001769 if (bp->dev->flags & IFF_PROMISC)
1770 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001771 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1772 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001773 if (!(bp->dev->flags & IFF_BROADCAST))
1774 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001775 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001776 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001777 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301778 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001779 bp->speed = SPEED_10;
1780 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301781 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001782 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301783 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001784
Jamie Iles0116da42011-03-14 17:38:30 +00001785 macb_configure_dma(bp);
1786
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001787 /* Initialize TX and RX buffers */
Harini Katakamfff80192016-08-09 13:15:53 +05301788 macb_writel(bp, RBQP, (u32)(bp->rx_ring_dma));
1789#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1790 macb_writel(bp, RBQPH, (u32)(bp->rx_ring_dma >> 32));
1791#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001792 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakamfff80192016-08-09 13:15:53 +05301793 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
1794#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1795 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
1796#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001797
1798 /* Enable interrupts */
1799 queue_writel(queue, IER,
1800 MACB_RX_INT_FLAGS |
1801 MACB_TX_INT_FLAGS |
1802 MACB_BIT(HRESP));
1803 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001804
1805 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001806 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001807}
1808
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001809/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001810 * locations in the memory map. The least significant bits are stored
1811 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1812 *
1813 * The unicast hash enable and the multicast hash enable bits in the
1814 * network configuration register enable the reception of hash matched
1815 * frames. The destination address is reduced to a 6 bit index into
1816 * the 64 bit hash register using the following hash function. The
1817 * hash function is an exclusive or of every sixth bit of the
1818 * destination address.
1819 *
1820 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1821 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1822 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1823 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1824 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1825 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1826 *
1827 * da[0] represents the least significant bit of the first byte
1828 * received, that is, the multicast/unicast indicator, and da[47]
1829 * represents the most significant bit of the last byte received. If
1830 * the hash index, hi[n], points to a bit that is set in the hash
1831 * register then the frame will be matched according to whether the
1832 * frame is multicast or unicast. A multicast match will be signalled
1833 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1834 * index points to a bit set in the hash register. A unicast match
1835 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1836 * and the hash index points to a bit set in the hash register. To
1837 * receive all multicast frames, the hash register should be set with
1838 * all ones and the multicast hash enable bit should be set in the
1839 * network configuration register.
1840 */
1841
1842static inline int hash_bit_value(int bitnr, __u8 *addr)
1843{
1844 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1845 return 1;
1846 return 0;
1847}
1848
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001849/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001850static int hash_get_index(__u8 *addr)
1851{
1852 int i, j, bitval;
1853 int hash_index = 0;
1854
1855 for (j = 0; j < 6; j++) {
1856 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001857 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001858
1859 hash_index |= (bitval << j);
1860 }
1861
1862 return hash_index;
1863}
1864
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001865/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001866static void macb_sethashtable(struct net_device *dev)
1867{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001868 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001869 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001870 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001871 struct macb *bp = netdev_priv(dev);
1872
Moritz Fischeraa50b552016-03-29 19:11:13 -07001873 mc_filter[0] = 0;
1874 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001875
Jiri Pirko22bedad32010-04-01 21:22:57 +00001876 netdev_for_each_mc_addr(ha, dev) {
1877 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001878 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1879 }
1880
Jamie Ilesf75ba502011-11-08 10:12:32 +00001881 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1882 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001883}
1884
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001885/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001886static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001887{
1888 unsigned long cfg;
1889 struct macb *bp = netdev_priv(dev);
1890
1891 cfg = macb_readl(bp, NCFGR);
1892
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001893 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001894 /* Enable promiscuous mode */
1895 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001896
1897 /* Disable RX checksum offload */
1898 if (macb_is_gem(bp))
1899 cfg &= ~GEM_BIT(RXCOEN);
1900 } else {
1901 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001902 cfg &= ~MACB_BIT(CAF);
1903
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001904 /* Enable RX checksum offload only if requested */
1905 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1906 cfg |= GEM_BIT(RXCOEN);
1907 }
1908
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001909 if (dev->flags & IFF_ALLMULTI) {
1910 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001911 macb_or_gem_writel(bp, HRB, -1);
1912 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001913 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001914 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001915 /* Enable specific multicasts */
1916 macb_sethashtable(dev);
1917 cfg |= MACB_BIT(NCFGR_MTI);
1918 } else if (dev->flags & (~IFF_ALLMULTI)) {
1919 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001920 macb_or_gem_writel(bp, HRB, 0);
1921 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001922 cfg &= ~MACB_BIT(NCFGR_MTI);
1923 }
1924
1925 macb_writel(bp, NCFGR, cfg);
1926}
1927
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001928static int macb_open(struct net_device *dev)
1929{
1930 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001931 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001932 int err;
1933
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001934 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001935
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001936 /* carrier starts down */
1937 netif_carrier_off(dev);
1938
frederic RODO6c36a702007-07-12 19:07:24 +02001939 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02001940 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02001941 return -EAGAIN;
1942
Nicolas Ferre1b447912013-06-04 21:57:11 +00001943 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001944 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001945
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001946 err = macb_alloc_consistent(bp);
1947 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001948 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1949 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001950 return err;
1951 }
1952
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001953 napi_enable(&bp->napi);
1954
Nicolas Ferre4df95132013-06-04 21:57:12 +00001955 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001956 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001957
frederic RODO6c36a702007-07-12 19:07:24 +02001958 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02001959 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02001960
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001961 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001962
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001963 return 0;
1964}
1965
1966static int macb_close(struct net_device *dev)
1967{
1968 struct macb *bp = netdev_priv(dev);
1969 unsigned long flags;
1970
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001971 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001972 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001973
Philippe Reynes0a912812016-06-22 00:32:35 +02001974 if (dev->phydev)
1975 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02001976
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001977 spin_lock_irqsave(&bp->lock, flags);
1978 macb_reset_hw(bp);
1979 netif_carrier_off(dev);
1980 spin_unlock_irqrestore(&bp->lock, flags);
1981
1982 macb_free_consistent(bp);
1983
1984 return 0;
1985}
1986
Harini Katakama5898ea2015-05-06 22:27:18 +05301987static int macb_change_mtu(struct net_device *dev, int new_mtu)
1988{
1989 struct macb *bp = netdev_priv(dev);
1990 u32 max_mtu;
1991
1992 if (netif_running(dev))
1993 return -EBUSY;
1994
1995 max_mtu = ETH_DATA_LEN;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001996 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakama5898ea2015-05-06 22:27:18 +05301997 max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
1998
1999 if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE))
2000 return -EINVAL;
2001
2002 dev->mtu = new_mtu;
2003
2004 return 0;
2005}
2006
Jamie Ilesa494ed82011-03-09 16:26:35 +00002007static void gem_update_stats(struct macb *bp)
2008{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002009 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002010 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002011
Xander Huff3ff13f12015-01-13 16:15:51 -06002012 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2013 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07002014 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06002015
2016 bp->ethtool_stats[i] += val;
2017 *p += val;
2018
2019 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2020 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07002021 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06002022 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002023 *(++p) += val;
2024 }
2025 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002026}
2027
2028static struct net_device_stats *gem_get_stats(struct macb *bp)
2029{
2030 struct gem_stats *hwstat = &bp->hw_stats.gem;
2031 struct net_device_stats *nstat = &bp->stats;
2032
2033 gem_update_stats(bp);
2034
2035 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2036 hwstat->rx_alignment_errors +
2037 hwstat->rx_resource_errors +
2038 hwstat->rx_overruns +
2039 hwstat->rx_oversize_frames +
2040 hwstat->rx_jabbers +
2041 hwstat->rx_undersized_frames +
2042 hwstat->rx_length_field_frame_errors);
2043 nstat->tx_errors = (hwstat->tx_late_collisions +
2044 hwstat->tx_excessive_collisions +
2045 hwstat->tx_underrun +
2046 hwstat->tx_carrier_sense_errors);
2047 nstat->multicast = hwstat->rx_multicast_frames;
2048 nstat->collisions = (hwstat->tx_single_collision_frames +
2049 hwstat->tx_multiple_collision_frames +
2050 hwstat->tx_excessive_collisions);
2051 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2052 hwstat->rx_jabbers +
2053 hwstat->rx_undersized_frames +
2054 hwstat->rx_length_field_frame_errors);
2055 nstat->rx_over_errors = hwstat->rx_resource_errors;
2056 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2057 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2058 nstat->rx_fifo_errors = hwstat->rx_overruns;
2059 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2060 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2061 nstat->tx_fifo_errors = hwstat->tx_underrun;
2062
2063 return nstat;
2064}
2065
Xander Huff3ff13f12015-01-13 16:15:51 -06002066static void gem_get_ethtool_stats(struct net_device *dev,
2067 struct ethtool_stats *stats, u64 *data)
2068{
2069 struct macb *bp;
2070
2071 bp = netdev_priv(dev);
2072 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002073 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002074}
2075
2076static int gem_get_sset_count(struct net_device *dev, int sset)
2077{
2078 switch (sset) {
2079 case ETH_SS_STATS:
2080 return GEM_STATS_LEN;
2081 default:
2082 return -EOPNOTSUPP;
2083 }
2084}
2085
2086static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2087{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002088 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002089
2090 switch (sset) {
2091 case ETH_SS_STATS:
2092 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2093 memcpy(p, gem_statistics[i].stat_string,
2094 ETH_GSTRING_LEN);
2095 break;
2096 }
2097}
2098
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002099static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002100{
2101 struct macb *bp = netdev_priv(dev);
2102 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002103 struct macb_stats *hwstat = &bp->hw_stats.macb;
2104
2105 if (macb_is_gem(bp))
2106 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002107
frederic RODO6c36a702007-07-12 19:07:24 +02002108 /* read stats from hardware */
2109 macb_update_stats(bp);
2110
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002111 /* Convert HW stats into netdevice stats */
2112 nstat->rx_errors = (hwstat->rx_fcs_errors +
2113 hwstat->rx_align_errors +
2114 hwstat->rx_resource_errors +
2115 hwstat->rx_overruns +
2116 hwstat->rx_oversize_pkts +
2117 hwstat->rx_jabbers +
2118 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002119 hwstat->rx_length_mismatch);
2120 nstat->tx_errors = (hwstat->tx_late_cols +
2121 hwstat->tx_excessive_cols +
2122 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002123 hwstat->tx_carrier_errors +
2124 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002125 nstat->collisions = (hwstat->tx_single_cols +
2126 hwstat->tx_multiple_cols +
2127 hwstat->tx_excessive_cols);
2128 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2129 hwstat->rx_jabbers +
2130 hwstat->rx_undersize_pkts +
2131 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002132 nstat->rx_over_errors = hwstat->rx_resource_errors +
2133 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002134 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2135 nstat->rx_frame_errors = hwstat->rx_align_errors;
2136 nstat->rx_fifo_errors = hwstat->rx_overruns;
2137 /* XXX: What does "missed" mean? */
2138 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2139 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2140 nstat->tx_fifo_errors = hwstat->tx_underruns;
2141 /* Don't know about heartbeat or window errors... */
2142
2143 return nstat;
2144}
2145
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002146static int macb_get_regs_len(struct net_device *netdev)
2147{
2148 return MACB_GREGS_NBR * sizeof(u32);
2149}
2150
2151static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2152 void *p)
2153{
2154 struct macb *bp = netdev_priv(dev);
2155 unsigned int tail, head;
2156 u32 *regs_buff = p;
2157
2158 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2159 | MACB_GREGS_VERSION;
2160
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002161 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2162 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002163
2164 regs_buff[0] = macb_readl(bp, NCR);
2165 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2166 regs_buff[2] = macb_readl(bp, NSR);
2167 regs_buff[3] = macb_readl(bp, TSR);
2168 regs_buff[4] = macb_readl(bp, RBQP);
2169 regs_buff[5] = macb_readl(bp, TBQP);
2170 regs_buff[6] = macb_readl(bp, RSR);
2171 regs_buff[7] = macb_readl(bp, IMR);
2172
2173 regs_buff[8] = tail;
2174 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002175 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2176 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002177
Neil Armstrongce721a72016-01-05 14:39:16 +01002178 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2179 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002180 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002181 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002182}
2183
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002184static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2185{
2186 struct macb *bp = netdev_priv(netdev);
2187
2188 wol->supported = 0;
2189 wol->wolopts = 0;
2190
2191 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2192 wol->supported = WAKE_MAGIC;
2193
2194 if (bp->wol & MACB_WOL_ENABLED)
2195 wol->wolopts |= WAKE_MAGIC;
2196 }
2197}
2198
2199static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2200{
2201 struct macb *bp = netdev_priv(netdev);
2202
2203 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2204 (wol->wolopts & ~WAKE_MAGIC))
2205 return -EOPNOTSUPP;
2206
2207 if (wol->wolopts & WAKE_MAGIC)
2208 bp->wol |= MACB_WOL_ENABLED;
2209 else
2210 bp->wol &= ~MACB_WOL_ENABLED;
2211
2212 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2213
2214 return 0;
2215}
2216
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002217static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002218 .get_regs_len = macb_get_regs_len,
2219 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002220 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002221 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002222 .get_wol = macb_get_wol,
2223 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002224 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2225 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Xander Huff8cd5a562015-01-15 15:55:20 -06002226};
Xander Huff8cd5a562015-01-15 15:55:20 -06002227
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002228static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002229 .get_regs_len = macb_get_regs_len,
2230 .get_regs = macb_get_regs,
2231 .get_link = ethtool_op_get_link,
2232 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002233 .get_ethtool_stats = gem_get_ethtool_stats,
2234 .get_strings = gem_get_ethtool_strings,
2235 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002236 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2237 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002238};
2239
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002240static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002241{
Philippe Reynes0a912812016-06-22 00:32:35 +02002242 struct phy_device *phydev = dev->phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002243
2244 if (!netif_running(dev))
2245 return -EINVAL;
2246
frederic RODO6c36a702007-07-12 19:07:24 +02002247 if (!phydev)
2248 return -ENODEV;
2249
Richard Cochran28b04112010-07-17 08:48:55 +00002250 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002251}
2252
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002253static int macb_set_features(struct net_device *netdev,
2254 netdev_features_t features)
2255{
2256 struct macb *bp = netdev_priv(netdev);
2257 netdev_features_t changed = features ^ netdev->features;
2258
2259 /* TX checksum offload */
2260 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2261 u32 dmacfg;
2262
2263 dmacfg = gem_readl(bp, DMACFG);
2264 if (features & NETIF_F_HW_CSUM)
2265 dmacfg |= GEM_BIT(TXCOEN);
2266 else
2267 dmacfg &= ~GEM_BIT(TXCOEN);
2268 gem_writel(bp, DMACFG, dmacfg);
2269 }
2270
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002271 /* RX checksum offload */
2272 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2273 u32 netcfg;
2274
2275 netcfg = gem_readl(bp, NCFGR);
2276 if (features & NETIF_F_RXCSUM &&
2277 !(netdev->flags & IFF_PROMISC))
2278 netcfg |= GEM_BIT(RXCOEN);
2279 else
2280 netcfg &= ~GEM_BIT(RXCOEN);
2281 gem_writel(bp, NCFGR, netcfg);
2282 }
2283
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002284 return 0;
2285}
2286
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002287static const struct net_device_ops macb_netdev_ops = {
2288 .ndo_open = macb_open,
2289 .ndo_stop = macb_close,
2290 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002291 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002292 .ndo_get_stats = macb_get_stats,
2293 .ndo_do_ioctl = macb_ioctl,
2294 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302295 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002296 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002297#ifdef CONFIG_NET_POLL_CONTROLLER
2298 .ndo_poll_controller = macb_poll_controller,
2299#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002300 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002301};
2302
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002303/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002304 * and integration options used
2305 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002306static void macb_configure_caps(struct macb *bp,
2307 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002308{
2309 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002310
Nicolas Ferref6970502015-03-31 15:02:01 +02002311 if (dt_conf)
2312 bp->caps = dt_conf->caps;
2313
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002314 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002315 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2316
Nicolas Ferree1755872014-07-24 13:50:58 +02002317 dcfg = gem_readl(bp, DCFG1);
2318 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2319 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2320 dcfg = gem_readl(bp, DCFG2);
2321 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2322 bp->caps |= MACB_CAPS_FIFO_MODE;
2323 }
2324
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002325 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002326}
2327
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002328static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002329 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002330 unsigned int *queue_mask,
2331 unsigned int *num_queues)
2332{
2333 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002334
2335 *queue_mask = 0x1;
2336 *num_queues = 1;
2337
Nicolas Ferreda120112015-03-31 15:02:00 +02002338 /* is it macb or gem ?
2339 *
2340 * We need to read directly from the hardware here because
2341 * we are early in the probe process and don't have the
2342 * MACB_CAPS_MACB_IS_GEM flag positioned
2343 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002344 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002345 return;
2346
2347 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302348 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2349
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002350 *queue_mask |= 0x1;
2351
2352 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2353 if (*queue_mask & (1 << hw_q))
2354 (*num_queues)++;
2355}
2356
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002357static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302358 struct clk **hclk, struct clk **tx_clk,
2359 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002360{
2361 int err;
2362
2363 *pclk = devm_clk_get(&pdev->dev, "pclk");
2364 if (IS_ERR(*pclk)) {
2365 err = PTR_ERR(*pclk);
2366 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2367 return err;
2368 }
2369
2370 *hclk = devm_clk_get(&pdev->dev, "hclk");
2371 if (IS_ERR(*hclk)) {
2372 err = PTR_ERR(*hclk);
2373 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2374 return err;
2375 }
2376
2377 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2378 if (IS_ERR(*tx_clk))
2379 *tx_clk = NULL;
2380
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302381 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2382 if (IS_ERR(*rx_clk))
2383 *rx_clk = NULL;
2384
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002385 err = clk_prepare_enable(*pclk);
2386 if (err) {
2387 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2388 return err;
2389 }
2390
2391 err = clk_prepare_enable(*hclk);
2392 if (err) {
2393 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2394 goto err_disable_pclk;
2395 }
2396
2397 err = clk_prepare_enable(*tx_clk);
2398 if (err) {
2399 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2400 goto err_disable_hclk;
2401 }
2402
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302403 err = clk_prepare_enable(*rx_clk);
2404 if (err) {
2405 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
2406 goto err_disable_txclk;
2407 }
2408
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002409 return 0;
2410
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302411err_disable_txclk:
2412 clk_disable_unprepare(*tx_clk);
2413
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002414err_disable_hclk:
2415 clk_disable_unprepare(*hclk);
2416
2417err_disable_pclk:
2418 clk_disable_unprepare(*pclk);
2419
2420 return err;
2421}
2422
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002423static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002424{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002425 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002426 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002427 struct macb *bp = netdev_priv(dev);
2428 struct macb_queue *queue;
2429 int err;
2430 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002431
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);
Alexey Khoroshilov178c7ae2016-11-19 01:40:10 +03002676 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
2677 dev_kfree_skb_any(skb);
2678 dev->stats.tx_dropped++;
2679 netdev_err(dev, "%s: DMA mapping error\n", __func__);
2680 return NETDEV_TX_OK;
2681 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002682
2683 /* Set address of the data in the Transmit Address register */
2684 macb_writel(lp, TAR, lp->skb_physaddr);
2685 /* Set length of the packet in the Transmit Control register */
2686 macb_writel(lp, TCR, skb->len);
2687
2688 } else {
2689 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2690 return NETDEV_TX_BUSY;
2691 }
2692
2693 return NETDEV_TX_OK;
2694}
2695
2696/* Extract received frame from buffer descriptors and sent to upper layers.
2697 * (Called from interrupt context)
2698 */
2699static void at91ether_rx(struct net_device *dev)
2700{
2701 struct macb *lp = netdev_priv(dev);
2702 unsigned char *p_recv;
2703 struct sk_buff *skb;
2704 unsigned int pktlen;
2705
2706 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2707 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2708 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2709 skb = netdev_alloc_skb(dev, pktlen + 2);
2710 if (skb) {
2711 skb_reserve(skb, 2);
2712 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2713
2714 skb->protocol = eth_type_trans(skb, dev);
2715 lp->stats.rx_packets++;
2716 lp->stats.rx_bytes += pktlen;
2717 netif_rx(skb);
2718 } else {
2719 lp->stats.rx_dropped++;
2720 }
2721
2722 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2723 lp->stats.multicast++;
2724
2725 /* reset ownership bit */
2726 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2727
2728 /* wrap after last buffer */
2729 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2730 lp->rx_tail = 0;
2731 else
2732 lp->rx_tail++;
2733 }
2734}
2735
2736/* MAC interrupt handler */
2737static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2738{
2739 struct net_device *dev = dev_id;
2740 struct macb *lp = netdev_priv(dev);
2741 u32 intstatus, ctl;
2742
2743 /* MAC Interrupt Status register indicates what interrupts are pending.
2744 * It is automatically cleared once read.
2745 */
2746 intstatus = macb_readl(lp, ISR);
2747
2748 /* Receive complete */
2749 if (intstatus & MACB_BIT(RCOMP))
2750 at91ether_rx(dev);
2751
2752 /* Transmit complete */
2753 if (intstatus & MACB_BIT(TCOMP)) {
2754 /* The TCOM bit is set even if the transmission failed */
2755 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2756 lp->stats.tx_errors++;
2757
2758 if (lp->skb) {
2759 dev_kfree_skb_irq(lp->skb);
2760 lp->skb = NULL;
2761 dma_unmap_single(NULL, lp->skb_physaddr,
2762 lp->skb_length, DMA_TO_DEVICE);
2763 lp->stats.tx_packets++;
2764 lp->stats.tx_bytes += lp->skb_length;
2765 }
2766 netif_wake_queue(dev);
2767 }
2768
2769 /* Work-around for EMAC Errata section 41.3.1 */
2770 if (intstatus & MACB_BIT(RXUBR)) {
2771 ctl = macb_readl(lp, NCR);
2772 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
Zumeng Chenffac0e92016-11-28 21:55:00 +08002773 wmb();
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002774 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2775 }
2776
2777 if (intstatus & MACB_BIT(ISR_ROVR))
2778 netdev_err(dev, "ROVR error\n");
2779
2780 return IRQ_HANDLED;
2781}
2782
2783#ifdef CONFIG_NET_POLL_CONTROLLER
2784static void at91ether_poll_controller(struct net_device *dev)
2785{
2786 unsigned long flags;
2787
2788 local_irq_save(flags);
2789 at91ether_interrupt(dev->irq, dev);
2790 local_irq_restore(flags);
2791}
2792#endif
2793
2794static const struct net_device_ops at91ether_netdev_ops = {
2795 .ndo_open = at91ether_open,
2796 .ndo_stop = at91ether_close,
2797 .ndo_start_xmit = at91ether_start_xmit,
2798 .ndo_get_stats = macb_get_stats,
2799 .ndo_set_rx_mode = macb_set_rx_mode,
2800 .ndo_set_mac_address = eth_mac_addr,
2801 .ndo_do_ioctl = macb_ioctl,
2802 .ndo_validate_addr = eth_validate_addr,
2803 .ndo_change_mtu = eth_change_mtu,
2804#ifdef CONFIG_NET_POLL_CONTROLLER
2805 .ndo_poll_controller = at91ether_poll_controller,
2806#endif
2807};
2808
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002809static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302810 struct clk **hclk, struct clk **tx_clk,
2811 struct clk **rx_clk)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002812{
2813 int err;
2814
2815 *hclk = NULL;
2816 *tx_clk = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302817 *rx_clk = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002818
2819 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2820 if (IS_ERR(*pclk))
2821 return PTR_ERR(*pclk);
2822
2823 err = clk_prepare_enable(*pclk);
2824 if (err) {
2825 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2826 return err;
2827 }
2828
2829 return 0;
2830}
2831
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002832static int at91ether_init(struct platform_device *pdev)
2833{
2834 struct net_device *dev = platform_get_drvdata(pdev);
2835 struct macb *bp = netdev_priv(dev);
2836 int err;
2837 u32 reg;
2838
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002839 dev->netdev_ops = &at91ether_netdev_ops;
2840 dev->ethtool_ops = &macb_ethtool_ops;
2841
2842 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2843 0, dev->name, dev);
2844 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002845 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002846
2847 macb_writel(bp, NCR, 0);
2848
2849 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2850 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2851 reg |= MACB_BIT(RM9200_RMII);
2852
2853 macb_writel(bp, NCFGR, reg);
2854
2855 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002856}
2857
David S. Miller3cef5c52015-03-09 23:38:02 -04002858static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002859 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002860 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002861 .init = macb_init,
2862};
2863
David S. Miller3cef5c52015-03-09 23:38:02 -04002864static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002865 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2866 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002867 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002868 .init = macb_init,
2869};
2870
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002871static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002872 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002873 .dma_burst_length = 16,
2874 .clk_init = macb_clk_init,
2875 .init = macb_init,
2876};
2877
David S. Miller3cef5c52015-03-09 23:38:02 -04002878static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002879 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
2880 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002881 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002882 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002883 .init = macb_init,
2884};
2885
David S. Miller3cef5c52015-03-09 23:38:02 -04002886static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002887 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002888 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002889 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002890 .init = macb_init,
2891};
2892
David S. Miller3cef5c52015-03-09 23:38:02 -04002893static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002894 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002895 .init = at91ether_init,
2896};
2897
Neil Armstronge611b5b2016-01-05 14:39:17 +01002898static const struct macb_config np4_config = {
2899 .caps = MACB_CAPS_USRIO_DISABLED,
2900 .clk_init = macb_clk_init,
2901 .init = macb_init,
2902};
David S. Miller36583eb2015-05-23 01:22:35 -04002903
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302904static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302905 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302906 .dma_burst_length = 16,
2907 .clk_init = macb_clk_init,
2908 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302909 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302910};
2911
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002912static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302913 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002914 .dma_burst_length = 16,
2915 .clk_init = macb_clk_init,
2916 .init = macb_init,
2917};
2918
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002919static const struct of_device_id macb_dt_ids[] = {
2920 { .compatible = "cdns,at32ap7000-macb" },
2921 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2922 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01002923 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002924 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2925 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002926 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002927 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2928 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2929 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2930 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302931 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002932 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002933 { /* sentinel */ }
2934};
2935MODULE_DEVICE_TABLE(of, macb_dt_ids);
2936#endif /* CONFIG_OF */
2937
2938static int macb_probe(struct platform_device *pdev)
2939{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002940 int (*clk_init)(struct platform_device *, struct clk **,
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302941 struct clk **, struct clk **, struct clk **)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002942 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002943 int (*init)(struct platform_device *) = macb_init;
2944 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002945 struct device_node *phy_node;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002946 const struct macb_config *macb_config = NULL;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302947 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002948 unsigned int queue_mask, num_queues;
2949 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002950 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002951 struct phy_device *phydev;
2952 struct net_device *dev;
2953 struct resource *regs;
2954 void __iomem *mem;
2955 const char *mac;
2956 struct macb *bp;
2957 int err;
2958
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002959 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2960 mem = devm_ioremap_resource(&pdev->dev, regs);
2961 if (IS_ERR(mem))
2962 return PTR_ERR(mem);
2963
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002964 if (np) {
2965 const struct of_device_id *match;
2966
2967 match = of_match_node(macb_dt_ids, np);
2968 if (match && match->data) {
2969 macb_config = match->data;
2970 clk_init = macb_config->clk_init;
2971 init = macb_config->init;
2972 }
2973 }
2974
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05302975 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002976 if (err)
2977 return err;
2978
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002979 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002980
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002981 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002982 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002983 if (!dev) {
2984 err = -ENOMEM;
2985 goto err_disable_clocks;
2986 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002987
2988 dev->base_addr = regs->start;
2989
2990 SET_NETDEV_DEV(dev, &pdev->dev);
2991
2992 bp = netdev_priv(dev);
2993 bp->pdev = pdev;
2994 bp->dev = dev;
2995 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002996 bp->native_io = native_io;
2997 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07002998 bp->macb_reg_readl = hw_readl_native;
2999 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003000 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07003001 bp->macb_reg_readl = hw_readl;
3002 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03003003 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003004 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02003005 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003006 if (macb_config)
3007 bp->dma_burst_length = macb_config->dma_burst_length;
3008 bp->pclk = pclk;
3009 bp->hclk = hclk;
3010 bp->tx_clk = tx_clk;
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303011 bp->rx_clk = rx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03003012 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303013 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05303014
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003015 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02003016 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003017 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3018 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3019
Harini Katakamfff80192016-08-09 13:15:53 +05303020#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3021 if (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1)) > GEM_DBW32)
3022 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3023#endif
3024
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003025 spin_lock_init(&bp->lock);
3026
Nicolas Ferread783472015-03-31 15:02:02 +02003027 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02003028 macb_configure_caps(bp, macb_config);
3029
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003030 platform_set_drvdata(pdev, dev);
3031
3032 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003033 if (dev->irq < 0) {
3034 err = dev->irq;
Wei Yongjunb22ae0b2016-08-12 15:43:54 +00003035 goto err_out_free_netdev;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003036 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003037
3038 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00003039 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07003040 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00003041 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003042 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02003043
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003044 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003045 phy_node = of_get_next_available_child(np, NULL);
3046 if (phy_node) {
3047 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003048
Charles Keepax0e3e7992016-03-28 13:47:42 +01003049 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003050 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003051 gpiod_direction_output(bp->reset_gpio, 1);
3052 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003053 }
3054 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003055
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003056 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003057 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003058 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003059 if (pdata && pdata->is_rmii)
3060 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3061 else
3062 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3063 } else {
3064 bp->phy_interface = err;
3065 }
3066
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003067 /* IP specific init */
3068 err = init(pdev);
3069 if (err)
3070 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003071
Florian Fainellicf669662016-05-02 18:38:45 -07003072 err = macb_mii_init(bp);
3073 if (err)
3074 goto err_out_free_netdev;
3075
Philippe Reynes0a912812016-06-22 00:32:35 +02003076 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003077
3078 netif_carrier_off(dev);
3079
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003080 err = register_netdev(dev);
3081 if (err) {
3082 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003083 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003084 }
3085
Florian Fainellicf669662016-05-02 18:38:45 -07003086 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003087
Bo Shen58798232014-09-13 01:57:49 +02003088 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3089 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3090 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003091
3092 return 0;
3093
Florian Fainellicf669662016-05-02 18:38:45 -07003094err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003095 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003096 mdiobus_unregister(bp->mii_bus);
3097 mdiobus_free(bp->mii_bus);
3098
3099 /* Shutdown the PHY if there is a GPIO reset */
3100 if (bp->reset_gpio)
3101 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003102
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003103err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003104 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003105
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003106err_disable_clocks:
3107 clk_disable_unprepare(tx_clk);
3108 clk_disable_unprepare(hclk);
3109 clk_disable_unprepare(pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303110 clk_disable_unprepare(rx_clk);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003111
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003112 return err;
3113}
3114
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003115static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003116{
3117 struct net_device *dev;
3118 struct macb *bp;
3119
3120 dev = platform_get_drvdata(pdev);
3121
3122 if (dev) {
3123 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003124 if (dev->phydev)
3125 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07003126 mdiobus_unregister(bp->mii_bus);
Nathan Sullivanfa6114d2016-10-07 10:13:22 -05003127 dev->phydev = NULL;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07003128 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003129
3130 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003131 if (bp->reset_gpio)
3132 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003133
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003134 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003135 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003136 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003137 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303138 clk_disable_unprepare(bp->rx_clk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003139 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003140 }
3141
3142 return 0;
3143}
3144
Michal Simekd23823d2015-01-23 09:36:03 +01003145static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003146{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003147 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003148 struct net_device *netdev = platform_get_drvdata(pdev);
3149 struct macb *bp = netdev_priv(netdev);
3150
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003151 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003152 netif_device_detach(netdev);
3153
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003154 if (bp->wol & MACB_WOL_ENABLED) {
3155 macb_writel(bp, IER, MACB_BIT(WOL));
3156 macb_writel(bp, WOL, MACB_BIT(MAG));
3157 enable_irq_wake(bp->queues[0].irq);
3158 } else {
3159 clk_disable_unprepare(bp->tx_clk);
3160 clk_disable_unprepare(bp->hclk);
3161 clk_disable_unprepare(bp->pclk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303162 clk_disable_unprepare(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003163 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003164
3165 return 0;
3166}
3167
Michal Simekd23823d2015-01-23 09:36:03 +01003168static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003169{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003170 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003171 struct net_device *netdev = platform_get_drvdata(pdev);
3172 struct macb *bp = netdev_priv(netdev);
3173
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003174 if (bp->wol & MACB_WOL_ENABLED) {
3175 macb_writel(bp, IDR, MACB_BIT(WOL));
3176 macb_writel(bp, WOL, 0);
3177 disable_irq_wake(bp->queues[0].irq);
3178 } else {
3179 clk_prepare_enable(bp->pclk);
3180 clk_prepare_enable(bp->hclk);
3181 clk_prepare_enable(bp->tx_clk);
shubhrajyoti.datta@xilinx.comaead88b2016-08-16 10:14:50 +05303182 clk_prepare_enable(bp->rx_clk);
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003183 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003184
3185 netif_device_attach(netdev);
3186
3187 return 0;
3188}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003189
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003190static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3191
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003192static struct platform_driver macb_driver = {
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003193 .probe = macb_probe,
3194 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003195 .driver = {
3196 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003197 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003198 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003199 },
3200};
3201
Nicolae Rosia9e86d762015-01-22 17:31:05 +00003202module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003203
3204MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003205MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003206MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003207MODULE_ALIAS("platform:macb");