blob: 6b797e301e3f82836e1e1e34a9c5e586e4128b48 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010035
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010036#include "macb.h"
37
Nicolas Ferre1b447912013-06-04 21:57:11 +000038#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000039#define RX_BUFFER_MULTIPLE 64 /* bytes */
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 Shevchenkof2ce8a9e2015-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 Shevchenkof2ce8a9e2015-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 Eastwood2dbfdbb92012-11-11 13:56:27 +0000374 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000375 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-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 Eastwood2dbfdbb92012-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 Eastwood2dbfdbb92012-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-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 Buytenhek298cf9b2008-10-08 16:29:57 -0700487 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-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 Shevchenkof2ce8a9e2015-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);
978}
979
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100980static int macb_rx(struct macb *bp, int budget)
981{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100982 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100983 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000984 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100985 int first_frag = -1;
986
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000987 for (tail = bp->rx_tail; budget > 0; tail++) {
988 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100989 u32 addr, ctrl;
990
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000991 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100992 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000993
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000994 addr = desc->addr;
995 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100996
997 if (!(addr & MACB_BIT(RX_USED)))
998 break;
999
1000 if (ctrl & MACB_BIT(RX_SOF)) {
1001 if (first_frag != -1)
1002 discard_partial_frame(bp, first_frag, tail);
1003 first_frag = tail;
1004 }
1005
1006 if (ctrl & MACB_BIT(RX_EOF)) {
1007 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001008
1009 if (unlikely(first_frag == -1)) {
1010 reset_rx_queue = true;
1011 continue;
1012 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001013
1014 dropped = macb_rx_frame(bp, first_frag, tail);
1015 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001016 if (unlikely(dropped < 0)) {
1017 reset_rx_queue = true;
1018 continue;
1019 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001020 if (!dropped) {
1021 received++;
1022 budget--;
1023 }
1024 }
1025 }
1026
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001027 if (unlikely(reset_rx_queue)) {
1028 unsigned long flags;
1029 u32 ctrl;
1030
1031 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1032
1033 spin_lock_irqsave(&bp->lock, flags);
1034
1035 ctrl = macb_readl(bp, NCR);
1036 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1037
1038 macb_init_rx_ring(bp);
1039 macb_writel(bp, RBQP, bp->rx_ring_dma);
1040
1041 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1042
1043 spin_unlock_irqrestore(&bp->lock, flags);
1044 return received;
1045 }
1046
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001047 if (first_frag != -1)
1048 bp->rx_tail = first_frag;
1049 else
1050 bp->rx_tail = tail;
1051
1052 return received;
1053}
1054
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001055static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001056{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001057 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001058 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001059 u32 status;
1060
1061 status = macb_readl(bp, RSR);
1062 macb_writel(bp, RSR, status);
1063
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001064 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001065
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001066 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Moritz Fischeraa50b552016-03-29 19:11:13 -07001067 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001068
Nicolas Ferre4df95132013-06-04 21:57:12 +00001069 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001070 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001071 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001072
Nicolas Ferre8770e912013-02-12 11:08:48 +01001073 /* Packets received while interrupts were disabled */
1074 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001075 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001076 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1077 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001078 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001079 } else {
1080 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1081 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001082 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083
1084 /* TODO: Handle errors */
1085
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001086 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001087}
1088
1089static irqreturn_t macb_interrupt(int irq, void *dev_id)
1090{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001091 struct macb_queue *queue = dev_id;
1092 struct macb *bp = queue->bp;
1093 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001094 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001095
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001096 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001097
1098 if (unlikely(!status))
1099 return IRQ_NONE;
1100
1101 spin_lock(&bp->lock);
1102
1103 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001104 /* close possible race with dev_close */
1105 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001106 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001107 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1108 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001109 break;
1110 }
1111
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001112 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1113 (unsigned int)(queue - bp->queues),
1114 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001115
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001116 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001117 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001118 * until we have processed the buffers. The
1119 * scheduling call may fail if the poll routine
1120 * is already scheduled, so disable interrupts
1121 * now.
1122 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001123 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001124 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001125 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001126
Ben Hutchings288379f2009-01-19 16:43:59 -08001127 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001128 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001129 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001130 }
1131 }
1132
Nicolas Ferree86cd532012-10-31 06:04:57 +00001133 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001134 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1135 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001136
1137 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001138 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001139
Nicolas Ferree86cd532012-10-31 06:04:57 +00001140 break;
1141 }
1142
1143 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001144 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001145
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001146 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001147 * add that if/when we get our hands on a full-blown MII PHY.
1148 */
1149
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001150 /* There is a hardware issue under heavy load where DMA can
1151 * stop, this causes endless "used buffer descriptor read"
1152 * interrupts but it can be cleared by re-enabling RX. See
1153 * the at91 manual, section 41.3.1 or the Zynq manual
1154 * section 16.7.4 for details.
1155 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001156 if (status & MACB_BIT(RXUBR)) {
1157 ctrl = macb_readl(bp, NCR);
1158 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1159 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1160
1161 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001162 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001163 }
1164
Alexander Steinb19f7f72011-04-13 05:03:24 +00001165 if (status & MACB_BIT(ISR_ROVR)) {
1166 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001167 if (macb_is_gem(bp))
1168 bp->hw_stats.gem.rx_overruns++;
1169 else
1170 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001171
1172 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001173 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001174 }
1175
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001176 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001177 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001178 * netdev_err to a lower-priority context as well
1179 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001180 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001181 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001182
1183 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001184 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001185 }
1186
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001187 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001188 }
1189
1190 spin_unlock(&bp->lock);
1191
1192 return IRQ_HANDLED;
1193}
1194
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001195#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001196/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001197 * to allow network i/o with interrupts disabled.
1198 */
1199static void macb_poll_controller(struct net_device *dev)
1200{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001201 struct macb *bp = netdev_priv(dev);
1202 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001203 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001204 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001205
1206 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001207 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1208 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001209 local_irq_restore(flags);
1210}
1211#endif
1212
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001213static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001214 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001215 struct sk_buff *skb)
1216{
1217 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001218 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001219 struct macb_tx_skb *tx_skb = NULL;
1220 struct macb_dma_desc *desc;
1221 unsigned int offset, size, count = 0;
1222 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1223 unsigned int eof = 1;
1224 u32 ctrl;
1225
1226 /* First, map non-paged data */
1227 len = skb_headlen(skb);
1228 offset = 0;
1229 while (len) {
1230 size = min(len, bp->max_tx_length);
1231 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001232 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001233
1234 mapping = dma_map_single(&bp->pdev->dev,
1235 skb->data + offset,
1236 size, DMA_TO_DEVICE);
1237 if (dma_mapping_error(&bp->pdev->dev, mapping))
1238 goto dma_error;
1239
1240 /* Save info to properly release resources */
1241 tx_skb->skb = NULL;
1242 tx_skb->mapping = mapping;
1243 tx_skb->size = size;
1244 tx_skb->mapped_as_page = false;
1245
1246 len -= size;
1247 offset += size;
1248 count++;
1249 tx_head++;
1250 }
1251
1252 /* Then, map paged data from fragments */
1253 for (f = 0; f < nr_frags; f++) {
1254 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1255
1256 len = skb_frag_size(frag);
1257 offset = 0;
1258 while (len) {
1259 size = min(len, bp->max_tx_length);
1260 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001261 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001262
1263 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1264 offset, size, DMA_TO_DEVICE);
1265 if (dma_mapping_error(&bp->pdev->dev, mapping))
1266 goto dma_error;
1267
1268 /* Save info to properly release resources */
1269 tx_skb->skb = NULL;
1270 tx_skb->mapping = mapping;
1271 tx_skb->size = size;
1272 tx_skb->mapped_as_page = true;
1273
1274 len -= size;
1275 offset += size;
1276 count++;
1277 tx_head++;
1278 }
1279 }
1280
1281 /* Should never happen */
Moritz Fischeraa50b552016-03-29 19:11:13 -07001282 if (unlikely(!tx_skb)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001283 netdev_err(bp->dev, "BUG! empty skb!\n");
1284 return 0;
1285 }
1286
1287 /* This is the last buffer of the frame: save socket buffer */
1288 tx_skb->skb = skb;
1289
1290 /* Update TX ring: update buffer descriptors in reverse order
1291 * to avoid race condition
1292 */
1293
1294 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1295 * to set the end of TX queue
1296 */
1297 i = tx_head;
1298 entry = macb_tx_ring_wrap(i);
1299 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001300 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001301 desc->ctrl = ctrl;
1302
1303 do {
1304 i--;
1305 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001306 tx_skb = &queue->tx_skb[entry];
1307 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001308
1309 ctrl = (u32)tx_skb->size;
1310 if (eof) {
1311 ctrl |= MACB_BIT(TX_LAST);
1312 eof = 0;
1313 }
1314 if (unlikely(entry == (TX_RING_SIZE - 1)))
1315 ctrl |= MACB_BIT(TX_WRAP);
1316
1317 /* Set TX buffer descriptor */
Harini Katakamfff80192016-08-09 13:15:53 +05301318 macb_set_addr(desc, tx_skb->mapping);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001319 /* desc->addr must be visible to hardware before clearing
1320 * 'TX_USED' bit in desc->ctrl.
1321 */
1322 wmb();
1323 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001324 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001325
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001326 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001327
1328 return count;
1329
1330dma_error:
1331 netdev_err(bp->dev, "TX DMA map failed\n");
1332
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001333 for (i = queue->tx_head; i != tx_head; i++) {
1334 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001335
1336 macb_tx_unmap(bp, tx_skb);
1337 }
1338
1339 return 0;
1340}
1341
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001342static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1343{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001344 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001345 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001346 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001347 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001348 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001349
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001350#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1351 netdev_vdbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001352 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1353 queue_index, skb->len, skb->head, skb->data,
1354 skb_tail_pointer(skb), skb_end_pointer(skb));
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001355 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1356 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001357#endif
1358
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001359 /* Count how many TX buffer descriptors are needed to send this
1360 * socket buffer: skb fragments of jumbo frames may need to be
Moritz Fischeraa50b552016-03-29 19:11:13 -07001361 * split into many buffer descriptors.
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001362 */
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001363 count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001364 nr_frags = skb_shinfo(skb)->nr_frags;
1365 for (f = 0; f < nr_frags; f++) {
1366 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001367 count += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001368 }
1369
Dongdong Deng48719532009-08-23 19:49:07 -07001370 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001371
1372 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001373 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1374 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001375 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001376 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001377 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001378 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001379 }
1380
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001381 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001382 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001383 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001384 goto unlock;
1385 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001386
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001387 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001388 wmb();
1389
Richard Cochrane0720922011-06-19 21:51:28 +00001390 skb_tx_timestamp(skb);
1391
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001392 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1393
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001394 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1395 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001396
Soren Brinkmann92030902014-03-04 08:46:39 -08001397unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001398 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001399
Patrick McHardy6ed10652009-06-23 06:03:08 +00001400 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001401}
1402
Nicolas Ferre4df95132013-06-04 21:57:12 +00001403static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001404{
1405 if (!macb_is_gem(bp)) {
1406 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1407 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001408 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001409
Nicolas Ferre1b447912013-06-04 21:57:11 +00001410 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001411 netdev_dbg(bp->dev,
Moritz Fischeraa50b552016-03-29 19:11:13 -07001412 "RX buffer must be multiple of %d bytes, expanding\n",
1413 RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001414 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001415 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001416 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001417 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001418
1419 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1420 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001421}
1422
Nicolas Ferre4df95132013-06-04 21:57:12 +00001423static void gem_free_rx_buffers(struct macb *bp)
1424{
1425 struct sk_buff *skb;
1426 struct macb_dma_desc *desc;
1427 dma_addr_t addr;
1428 int i;
1429
1430 if (!bp->rx_skbuff)
1431 return;
1432
1433 for (i = 0; i < RX_RING_SIZE; i++) {
1434 skb = bp->rx_skbuff[i];
1435
Moritz Fischeraa50b552016-03-29 19:11:13 -07001436 if (!skb)
Nicolas Ferre4df95132013-06-04 21:57:12 +00001437 continue;
1438
1439 desc = &bp->rx_ring[i];
1440 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Harini Katakamfff80192016-08-09 13:15:53 +05301441#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1442 addr |= ((u64)(desc->addrh) << 32);
1443#endif
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001444 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001445 DMA_FROM_DEVICE);
1446 dev_kfree_skb_any(skb);
1447 skb = NULL;
1448 }
1449
1450 kfree(bp->rx_skbuff);
1451 bp->rx_skbuff = NULL;
1452}
1453
1454static void macb_free_rx_buffers(struct macb *bp)
1455{
1456 if (bp->rx_buffers) {
1457 dma_free_coherent(&bp->pdev->dev,
1458 RX_RING_SIZE * bp->rx_buffer_size,
1459 bp->rx_buffers, bp->rx_buffers_dma);
1460 bp->rx_buffers = NULL;
1461 }
1462}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001463
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001464static void macb_free_consistent(struct macb *bp)
1465{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001466 struct macb_queue *queue;
1467 unsigned int q;
1468
Nicolas Ferre4df95132013-06-04 21:57:12 +00001469 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001470 if (bp->rx_ring) {
1471 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1472 bp->rx_ring, bp->rx_ring_dma);
1473 bp->rx_ring = NULL;
1474 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001475
1476 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1477 kfree(queue->tx_skb);
1478 queue->tx_skb = NULL;
1479 if (queue->tx_ring) {
1480 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1481 queue->tx_ring, queue->tx_ring_dma);
1482 queue->tx_ring = NULL;
1483 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001484 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001485}
1486
1487static int gem_alloc_rx_buffers(struct macb *bp)
1488{
1489 int size;
1490
1491 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1492 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1493 if (!bp->rx_skbuff)
1494 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001495
1496 netdev_dbg(bp->dev,
1497 "Allocated %d RX struct sk_buff entries at %p\n",
1498 RX_RING_SIZE, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001499 return 0;
1500}
1501
1502static int macb_alloc_rx_buffers(struct macb *bp)
1503{
1504 int size;
1505
1506 size = RX_RING_SIZE * bp->rx_buffer_size;
1507 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1508 &bp->rx_buffers_dma, GFP_KERNEL);
1509 if (!bp->rx_buffers)
1510 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001511
1512 netdev_dbg(bp->dev,
1513 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1514 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001515 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001516}
1517
1518static int macb_alloc_consistent(struct macb *bp)
1519{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001520 struct macb_queue *queue;
1521 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001522 int size;
1523
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001524 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1525 size = TX_RING_BYTES;
1526 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1527 &queue->tx_ring_dma,
1528 GFP_KERNEL);
1529 if (!queue->tx_ring)
1530 goto out_err;
1531 netdev_dbg(bp->dev,
1532 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1533 q, size, (unsigned long)queue->tx_ring_dma,
1534 queue->tx_ring);
1535
1536 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1537 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1538 if (!queue->tx_skb)
1539 goto out_err;
1540 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001541
1542 size = RX_RING_BYTES;
1543 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1544 &bp->rx_ring_dma, GFP_KERNEL);
1545 if (!bp->rx_ring)
1546 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001547 netdev_dbg(bp->dev,
1548 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1549 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001550
Nicolas Ferre4df95132013-06-04 21:57:12 +00001551 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001552 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001553
1554 return 0;
1555
1556out_err:
1557 macb_free_consistent(bp);
1558 return -ENOMEM;
1559}
1560
Nicolas Ferre4df95132013-06-04 21:57:12 +00001561static void gem_init_rings(struct macb *bp)
1562{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001563 struct macb_queue *queue;
1564 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001565 int i;
1566
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001567 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1568 for (i = 0; i < TX_RING_SIZE; i++) {
Harini Katakamfff80192016-08-09 13:15:53 +05301569 macb_set_addr(&(queue->tx_ring[i]), 0);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001570 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1571 }
1572 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1573 queue->tx_head = 0;
1574 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001575 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001576
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001577 bp->rx_tail = 0;
1578 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001579
1580 gem_rx_refill(bp);
1581}
1582
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001583static void macb_init_rings(struct macb *bp)
1584{
1585 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001586
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001587 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001588
1589 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001590 bp->queues[0].tx_ring[i].addr = 0;
1591 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001592 }
Ben Shelton21d35152015-04-22 17:28:54 -05001593 bp->queues[0].tx_head = 0;
1594 bp->queues[0].tx_tail = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001595 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001596
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001597 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001598}
1599
1600static void macb_reset_hw(struct macb *bp)
1601{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001602 struct macb_queue *queue;
1603 unsigned int q;
1604
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001605 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001606 * more gracefully?)
1607 */
1608 macb_writel(bp, NCR, 0);
1609
1610 /* Clear the stats registers (XXX: Update stats first?) */
1611 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1612
1613 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001614 macb_writel(bp, TSR, -1);
1615 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001616
1617 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001618 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1619 queue_writel(queue, IDR, -1);
1620 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001621 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1622 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001623 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001624}
1625
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001626static u32 gem_mdc_clk_div(struct macb *bp)
1627{
1628 u32 config;
1629 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1630
1631 if (pclk_hz <= 20000000)
1632 config = GEM_BF(CLK, GEM_CLK_DIV8);
1633 else if (pclk_hz <= 40000000)
1634 config = GEM_BF(CLK, GEM_CLK_DIV16);
1635 else if (pclk_hz <= 80000000)
1636 config = GEM_BF(CLK, GEM_CLK_DIV32);
1637 else if (pclk_hz <= 120000000)
1638 config = GEM_BF(CLK, GEM_CLK_DIV48);
1639 else if (pclk_hz <= 160000000)
1640 config = GEM_BF(CLK, GEM_CLK_DIV64);
1641 else
1642 config = GEM_BF(CLK, GEM_CLK_DIV96);
1643
1644 return config;
1645}
1646
1647static u32 macb_mdc_clk_div(struct macb *bp)
1648{
1649 u32 config;
1650 unsigned long pclk_hz;
1651
1652 if (macb_is_gem(bp))
1653 return gem_mdc_clk_div(bp);
1654
1655 pclk_hz = clk_get_rate(bp->pclk);
1656 if (pclk_hz <= 20000000)
1657 config = MACB_BF(CLK, MACB_CLK_DIV8);
1658 else if (pclk_hz <= 40000000)
1659 config = MACB_BF(CLK, MACB_CLK_DIV16);
1660 else if (pclk_hz <= 80000000)
1661 config = MACB_BF(CLK, MACB_CLK_DIV32);
1662 else
1663 config = MACB_BF(CLK, MACB_CLK_DIV64);
1664
1665 return config;
1666}
1667
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001668/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001669 * should program. We find the width from decoding the design configuration
1670 * register to find the maximum supported data bus width.
1671 */
1672static u32 macb_dbw(struct macb *bp)
1673{
1674 if (!macb_is_gem(bp))
1675 return 0;
1676
1677 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1678 case 4:
1679 return GEM_BF(DBW, GEM_DBW128);
1680 case 2:
1681 return GEM_BF(DBW, GEM_DBW64);
1682 case 1:
1683 default:
1684 return GEM_BF(DBW, GEM_DBW32);
1685 }
1686}
1687
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001688/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001689 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001690 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001691 * (if not supported by FIFO, it will fallback to default)
1692 * - set both rx/tx packet buffers to full memory size
1693 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001694 */
1695static void macb_configure_dma(struct macb *bp)
1696{
1697 u32 dmacfg;
1698
1699 if (macb_is_gem(bp)) {
1700 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001701 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001702 if (bp->dma_burst_length)
1703 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001704 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301705 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301706
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03001707 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301708 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1709 else
1710 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1711
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001712 if (bp->dev->features & NETIF_F_HW_CSUM)
1713 dmacfg |= GEM_BIT(TXCOEN);
1714 else
1715 dmacfg &= ~GEM_BIT(TXCOEN);
Harini Katakamfff80192016-08-09 13:15:53 +05301716
1717#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1718 dmacfg |= GEM_BIT(ADDR64);
1719#endif
Nicolas Ferree1755872014-07-24 13:50:58 +02001720 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1721 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001722 gem_writel(bp, DMACFG, dmacfg);
1723 }
1724}
1725
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001726static void macb_init_hw(struct macb *bp)
1727{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001728 struct macb_queue *queue;
1729 unsigned int q;
1730
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001731 u32 config;
1732
1733 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001734 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001735
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001736 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301737 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1738 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001739 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001740 config |= MACB_BIT(PAE); /* PAuse Enable */
1741 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001742 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301743 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1744 else
1745 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001746 if (bp->dev->flags & IFF_PROMISC)
1747 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001748 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1749 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001750 if (!(bp->dev->flags & IFF_BROADCAST))
1751 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001752 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001753 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001754 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301755 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001756 bp->speed = SPEED_10;
1757 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301758 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001759 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301760 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001761
Jamie Iles0116da42011-03-14 17:38:30 +00001762 macb_configure_dma(bp);
1763
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001764 /* Initialize TX and RX buffers */
Harini Katakamfff80192016-08-09 13:15:53 +05301765 macb_writel(bp, RBQP, (u32)(bp->rx_ring_dma));
1766#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1767 macb_writel(bp, RBQPH, (u32)(bp->rx_ring_dma >> 32));
1768#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001769 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
Harini Katakamfff80192016-08-09 13:15:53 +05301770 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
1771#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1772 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
1773#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001774
1775 /* Enable interrupts */
1776 queue_writel(queue, IER,
1777 MACB_RX_INT_FLAGS |
1778 MACB_TX_INT_FLAGS |
1779 MACB_BIT(HRESP));
1780 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001781
1782 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001783 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001784}
1785
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001786/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001787 * locations in the memory map. The least significant bits are stored
1788 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1789 *
1790 * The unicast hash enable and the multicast hash enable bits in the
1791 * network configuration register enable the reception of hash matched
1792 * frames. The destination address is reduced to a 6 bit index into
1793 * the 64 bit hash register using the following hash function. The
1794 * hash function is an exclusive or of every sixth bit of the
1795 * destination address.
1796 *
1797 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1798 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1799 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1800 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1801 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1802 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1803 *
1804 * da[0] represents the least significant bit of the first byte
1805 * received, that is, the multicast/unicast indicator, and da[47]
1806 * represents the most significant bit of the last byte received. If
1807 * the hash index, hi[n], points to a bit that is set in the hash
1808 * register then the frame will be matched according to whether the
1809 * frame is multicast or unicast. A multicast match will be signalled
1810 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1811 * index points to a bit set in the hash register. A unicast match
1812 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1813 * and the hash index points to a bit set in the hash register. To
1814 * receive all multicast frames, the hash register should be set with
1815 * all ones and the multicast hash enable bit should be set in the
1816 * network configuration register.
1817 */
1818
1819static inline int hash_bit_value(int bitnr, __u8 *addr)
1820{
1821 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1822 return 1;
1823 return 0;
1824}
1825
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001826/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001827static int hash_get_index(__u8 *addr)
1828{
1829 int i, j, bitval;
1830 int hash_index = 0;
1831
1832 for (j = 0; j < 6; j++) {
1833 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001834 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001835
1836 hash_index |= (bitval << j);
1837 }
1838
1839 return hash_index;
1840}
1841
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001842/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001843static void macb_sethashtable(struct net_device *dev)
1844{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001845 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001846 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001847 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001848 struct macb *bp = netdev_priv(dev);
1849
Moritz Fischeraa50b552016-03-29 19:11:13 -07001850 mc_filter[0] = 0;
1851 mc_filter[1] = 0;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001852
Jiri Pirko22bedad32010-04-01 21:22:57 +00001853 netdev_for_each_mc_addr(ha, dev) {
1854 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001855 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1856 }
1857
Jamie Ilesf75ba502011-11-08 10:12:32 +00001858 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1859 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001860}
1861
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001862/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001863static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001864{
1865 unsigned long cfg;
1866 struct macb *bp = netdev_priv(dev);
1867
1868 cfg = macb_readl(bp, NCFGR);
1869
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001870 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001871 /* Enable promiscuous mode */
1872 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001873
1874 /* Disable RX checksum offload */
1875 if (macb_is_gem(bp))
1876 cfg &= ~GEM_BIT(RXCOEN);
1877 } else {
1878 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001879 cfg &= ~MACB_BIT(CAF);
1880
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001881 /* Enable RX checksum offload only if requested */
1882 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1883 cfg |= GEM_BIT(RXCOEN);
1884 }
1885
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001886 if (dev->flags & IFF_ALLMULTI) {
1887 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001888 macb_or_gem_writel(bp, HRB, -1);
1889 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001890 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001891 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001892 /* Enable specific multicasts */
1893 macb_sethashtable(dev);
1894 cfg |= MACB_BIT(NCFGR_MTI);
1895 } else if (dev->flags & (~IFF_ALLMULTI)) {
1896 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001897 macb_or_gem_writel(bp, HRB, 0);
1898 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001899 cfg &= ~MACB_BIT(NCFGR_MTI);
1900 }
1901
1902 macb_writel(bp, NCFGR, cfg);
1903}
1904
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001905static int macb_open(struct net_device *dev)
1906{
1907 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001908 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001909 int err;
1910
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001911 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001912
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001913 /* carrier starts down */
1914 netif_carrier_off(dev);
1915
frederic RODO6c36a702007-07-12 19:07:24 +02001916 /* if the phy is not yet register, retry later*/
Philippe Reynes0a912812016-06-22 00:32:35 +02001917 if (!dev->phydev)
frederic RODO6c36a702007-07-12 19:07:24 +02001918 return -EAGAIN;
1919
Nicolas Ferre1b447912013-06-04 21:57:11 +00001920 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001921 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001922
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001923 err = macb_alloc_consistent(bp);
1924 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001925 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1926 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001927 return err;
1928 }
1929
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001930 napi_enable(&bp->napi);
1931
Nicolas Ferre4df95132013-06-04 21:57:12 +00001932 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001933 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001934
frederic RODO6c36a702007-07-12 19:07:24 +02001935 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02001936 phy_start(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02001937
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001938 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001939
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001940 return 0;
1941}
1942
1943static int macb_close(struct net_device *dev)
1944{
1945 struct macb *bp = netdev_priv(dev);
1946 unsigned long flags;
1947
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001948 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001949 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001950
Philippe Reynes0a912812016-06-22 00:32:35 +02001951 if (dev->phydev)
1952 phy_stop(dev->phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02001953
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001954 spin_lock_irqsave(&bp->lock, flags);
1955 macb_reset_hw(bp);
1956 netif_carrier_off(dev);
1957 spin_unlock_irqrestore(&bp->lock, flags);
1958
1959 macb_free_consistent(bp);
1960
1961 return 0;
1962}
1963
Harini Katakama5898ea2015-05-06 22:27:18 +05301964static int macb_change_mtu(struct net_device *dev, int new_mtu)
1965{
1966 struct macb *bp = netdev_priv(dev);
1967 u32 max_mtu;
1968
1969 if (netif_running(dev))
1970 return -EBUSY;
1971
1972 max_mtu = ETH_DATA_LEN;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001973 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakama5898ea2015-05-06 22:27:18 +05301974 max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
1975
1976 if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE))
1977 return -EINVAL;
1978
1979 dev->mtu = new_mtu;
1980
1981 return 0;
1982}
1983
Jamie Ilesa494ed82011-03-09 16:26:35 +00001984static void gem_update_stats(struct macb *bp)
1985{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03001986 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001987 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001988
Xander Huff3ff13f12015-01-13 16:15:51 -06001989 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1990 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07001991 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001992
1993 bp->ethtool_stats[i] += val;
1994 *p += val;
1995
1996 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1997 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07001998 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06001999 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06002000 *(++p) += val;
2001 }
2002 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00002003}
2004
2005static struct net_device_stats *gem_get_stats(struct macb *bp)
2006{
2007 struct gem_stats *hwstat = &bp->hw_stats.gem;
2008 struct net_device_stats *nstat = &bp->stats;
2009
2010 gem_update_stats(bp);
2011
2012 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2013 hwstat->rx_alignment_errors +
2014 hwstat->rx_resource_errors +
2015 hwstat->rx_overruns +
2016 hwstat->rx_oversize_frames +
2017 hwstat->rx_jabbers +
2018 hwstat->rx_undersized_frames +
2019 hwstat->rx_length_field_frame_errors);
2020 nstat->tx_errors = (hwstat->tx_late_collisions +
2021 hwstat->tx_excessive_collisions +
2022 hwstat->tx_underrun +
2023 hwstat->tx_carrier_sense_errors);
2024 nstat->multicast = hwstat->rx_multicast_frames;
2025 nstat->collisions = (hwstat->tx_single_collision_frames +
2026 hwstat->tx_multiple_collision_frames +
2027 hwstat->tx_excessive_collisions);
2028 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2029 hwstat->rx_jabbers +
2030 hwstat->rx_undersized_frames +
2031 hwstat->rx_length_field_frame_errors);
2032 nstat->rx_over_errors = hwstat->rx_resource_errors;
2033 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2034 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2035 nstat->rx_fifo_errors = hwstat->rx_overruns;
2036 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2037 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2038 nstat->tx_fifo_errors = hwstat->tx_underrun;
2039
2040 return nstat;
2041}
2042
Xander Huff3ff13f12015-01-13 16:15:51 -06002043static void gem_get_ethtool_stats(struct net_device *dev,
2044 struct ethtool_stats *stats, u64 *data)
2045{
2046 struct macb *bp;
2047
2048 bp = netdev_priv(dev);
2049 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002050 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002051}
2052
2053static int gem_get_sset_count(struct net_device *dev, int sset)
2054{
2055 switch (sset) {
2056 case ETH_SS_STATS:
2057 return GEM_STATS_LEN;
2058 default:
2059 return -EOPNOTSUPP;
2060 }
2061}
2062
2063static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2064{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002065 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002066
2067 switch (sset) {
2068 case ETH_SS_STATS:
2069 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2070 memcpy(p, gem_statistics[i].stat_string,
2071 ETH_GSTRING_LEN);
2072 break;
2073 }
2074}
2075
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002076static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002077{
2078 struct macb *bp = netdev_priv(dev);
2079 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002080 struct macb_stats *hwstat = &bp->hw_stats.macb;
2081
2082 if (macb_is_gem(bp))
2083 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084
frederic RODO6c36a702007-07-12 19:07:24 +02002085 /* read stats from hardware */
2086 macb_update_stats(bp);
2087
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002088 /* Convert HW stats into netdevice stats */
2089 nstat->rx_errors = (hwstat->rx_fcs_errors +
2090 hwstat->rx_align_errors +
2091 hwstat->rx_resource_errors +
2092 hwstat->rx_overruns +
2093 hwstat->rx_oversize_pkts +
2094 hwstat->rx_jabbers +
2095 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002096 hwstat->rx_length_mismatch);
2097 nstat->tx_errors = (hwstat->tx_late_cols +
2098 hwstat->tx_excessive_cols +
2099 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002100 hwstat->tx_carrier_errors +
2101 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002102 nstat->collisions = (hwstat->tx_single_cols +
2103 hwstat->tx_multiple_cols +
2104 hwstat->tx_excessive_cols);
2105 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2106 hwstat->rx_jabbers +
2107 hwstat->rx_undersize_pkts +
2108 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002109 nstat->rx_over_errors = hwstat->rx_resource_errors +
2110 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002111 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2112 nstat->rx_frame_errors = hwstat->rx_align_errors;
2113 nstat->rx_fifo_errors = hwstat->rx_overruns;
2114 /* XXX: What does "missed" mean? */
2115 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2116 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2117 nstat->tx_fifo_errors = hwstat->tx_underruns;
2118 /* Don't know about heartbeat or window errors... */
2119
2120 return nstat;
2121}
2122
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002123static int macb_get_regs_len(struct net_device *netdev)
2124{
2125 return MACB_GREGS_NBR * sizeof(u32);
2126}
2127
2128static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2129 void *p)
2130{
2131 struct macb *bp = netdev_priv(dev);
2132 unsigned int tail, head;
2133 u32 *regs_buff = p;
2134
2135 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2136 | MACB_GREGS_VERSION;
2137
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002138 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2139 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002140
2141 regs_buff[0] = macb_readl(bp, NCR);
2142 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2143 regs_buff[2] = macb_readl(bp, NSR);
2144 regs_buff[3] = macb_readl(bp, TSR);
2145 regs_buff[4] = macb_readl(bp, RBQP);
2146 regs_buff[5] = macb_readl(bp, TBQP);
2147 regs_buff[6] = macb_readl(bp, RSR);
2148 regs_buff[7] = macb_readl(bp, IMR);
2149
2150 regs_buff[8] = tail;
2151 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002152 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2153 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002154
Neil Armstrongce721a72016-01-05 14:39:16 +01002155 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2156 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002157 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002158 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002159}
2160
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002161static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2162{
2163 struct macb *bp = netdev_priv(netdev);
2164
2165 wol->supported = 0;
2166 wol->wolopts = 0;
2167
2168 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2169 wol->supported = WAKE_MAGIC;
2170
2171 if (bp->wol & MACB_WOL_ENABLED)
2172 wol->wolopts |= WAKE_MAGIC;
2173 }
2174}
2175
2176static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2177{
2178 struct macb *bp = netdev_priv(netdev);
2179
2180 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2181 (wol->wolopts & ~WAKE_MAGIC))
2182 return -EOPNOTSUPP;
2183
2184 if (wol->wolopts & WAKE_MAGIC)
2185 bp->wol |= MACB_WOL_ENABLED;
2186 else
2187 bp->wol &= ~MACB_WOL_ENABLED;
2188
2189 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2190
2191 return 0;
2192}
2193
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002194static const struct ethtool_ops macb_ethtool_ops = {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002195 .get_regs_len = macb_get_regs_len,
2196 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002197 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002198 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002199 .get_wol = macb_get_wol,
2200 .set_wol = macb_set_wol,
Philippe Reynes176275a2016-06-22 00:32:36 +02002201 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2202 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Xander Huff8cd5a562015-01-15 15:55:20 -06002203};
Xander Huff8cd5a562015-01-15 15:55:20 -06002204
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002205static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002206 .get_regs_len = macb_get_regs_len,
2207 .get_regs = macb_get_regs,
2208 .get_link = ethtool_op_get_link,
2209 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002210 .get_ethtool_stats = gem_get_ethtool_stats,
2211 .get_strings = gem_get_ethtool_strings,
2212 .get_sset_count = gem_get_sset_count,
Philippe Reynes176275a2016-06-22 00:32:36 +02002213 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2214 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002215};
2216
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002217static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002218{
Philippe Reynes0a912812016-06-22 00:32:35 +02002219 struct phy_device *phydev = dev->phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002220
2221 if (!netif_running(dev))
2222 return -EINVAL;
2223
frederic RODO6c36a702007-07-12 19:07:24 +02002224 if (!phydev)
2225 return -ENODEV;
2226
Richard Cochran28b04112010-07-17 08:48:55 +00002227 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002228}
2229
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002230static int macb_set_features(struct net_device *netdev,
2231 netdev_features_t features)
2232{
2233 struct macb *bp = netdev_priv(netdev);
2234 netdev_features_t changed = features ^ netdev->features;
2235
2236 /* TX checksum offload */
2237 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2238 u32 dmacfg;
2239
2240 dmacfg = gem_readl(bp, DMACFG);
2241 if (features & NETIF_F_HW_CSUM)
2242 dmacfg |= GEM_BIT(TXCOEN);
2243 else
2244 dmacfg &= ~GEM_BIT(TXCOEN);
2245 gem_writel(bp, DMACFG, dmacfg);
2246 }
2247
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002248 /* RX checksum offload */
2249 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2250 u32 netcfg;
2251
2252 netcfg = gem_readl(bp, NCFGR);
2253 if (features & NETIF_F_RXCSUM &&
2254 !(netdev->flags & IFF_PROMISC))
2255 netcfg |= GEM_BIT(RXCOEN);
2256 else
2257 netcfg &= ~GEM_BIT(RXCOEN);
2258 gem_writel(bp, NCFGR, netcfg);
2259 }
2260
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002261 return 0;
2262}
2263
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002264static const struct net_device_ops macb_netdev_ops = {
2265 .ndo_open = macb_open,
2266 .ndo_stop = macb_close,
2267 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002268 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002269 .ndo_get_stats = macb_get_stats,
2270 .ndo_do_ioctl = macb_ioctl,
2271 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302272 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002273 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002274#ifdef CONFIG_NET_POLL_CONTROLLER
2275 .ndo_poll_controller = macb_poll_controller,
2276#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002277 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002278};
2279
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002280/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002281 * and integration options used
2282 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002283static void macb_configure_caps(struct macb *bp,
2284 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002285{
2286 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002287
Nicolas Ferref6970502015-03-31 15:02:01 +02002288 if (dt_conf)
2289 bp->caps = dt_conf->caps;
2290
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002291 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002292 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2293
Nicolas Ferree1755872014-07-24 13:50:58 +02002294 dcfg = gem_readl(bp, DCFG1);
2295 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2296 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2297 dcfg = gem_readl(bp, DCFG2);
2298 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2299 bp->caps |= MACB_CAPS_FIFO_MODE;
2300 }
2301
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002302 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002303}
2304
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002305static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002306 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002307 unsigned int *queue_mask,
2308 unsigned int *num_queues)
2309{
2310 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002311
2312 *queue_mask = 0x1;
2313 *num_queues = 1;
2314
Nicolas Ferreda120112015-03-31 15:02:00 +02002315 /* is it macb or gem ?
2316 *
2317 * We need to read directly from the hardware here because
2318 * we are early in the probe process and don't have the
2319 * MACB_CAPS_MACB_IS_GEM flag positioned
2320 */
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002321 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002322 return;
2323
2324 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302325 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2326
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002327 *queue_mask |= 0x1;
2328
2329 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2330 if (*queue_mask & (1 << hw_q))
2331 (*num_queues)++;
2332}
2333
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002334static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
2335 struct clk **hclk, struct clk **tx_clk)
2336{
2337 int err;
2338
2339 *pclk = devm_clk_get(&pdev->dev, "pclk");
2340 if (IS_ERR(*pclk)) {
2341 err = PTR_ERR(*pclk);
2342 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2343 return err;
2344 }
2345
2346 *hclk = devm_clk_get(&pdev->dev, "hclk");
2347 if (IS_ERR(*hclk)) {
2348 err = PTR_ERR(*hclk);
2349 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2350 return err;
2351 }
2352
2353 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2354 if (IS_ERR(*tx_clk))
2355 *tx_clk = NULL;
2356
2357 err = clk_prepare_enable(*pclk);
2358 if (err) {
2359 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2360 return err;
2361 }
2362
2363 err = clk_prepare_enable(*hclk);
2364 if (err) {
2365 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2366 goto err_disable_pclk;
2367 }
2368
2369 err = clk_prepare_enable(*tx_clk);
2370 if (err) {
2371 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2372 goto err_disable_hclk;
2373 }
2374
2375 return 0;
2376
2377err_disable_hclk:
2378 clk_disable_unprepare(*hclk);
2379
2380err_disable_pclk:
2381 clk_disable_unprepare(*pclk);
2382
2383 return err;
2384}
2385
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002386static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002387{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002388 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002389 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002390 struct macb *bp = netdev_priv(dev);
2391 struct macb_queue *queue;
2392 int err;
2393 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002394
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002395 /* set the queue register mapping once for all: queue0 has a special
2396 * register mapping but we don't want to test the queue index then
2397 * compute the corresponding register offset at run time.
2398 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002399 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002400 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002401 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002402
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002403 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002404 queue->bp = bp;
2405 if (hw_q) {
2406 queue->ISR = GEM_ISR(hw_q - 1);
2407 queue->IER = GEM_IER(hw_q - 1);
2408 queue->IDR = GEM_IDR(hw_q - 1);
2409 queue->IMR = GEM_IMR(hw_q - 1);
2410 queue->TBQP = GEM_TBQP(hw_q - 1);
Harini Katakamfff80192016-08-09 13:15:53 +05302411#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2412 queue->TBQPH = GEM_TBQPH(hw_q -1);
2413#endif
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002414 } else {
2415 /* queue0 uses legacy registers */
2416 queue->ISR = MACB_ISR;
2417 queue->IER = MACB_IER;
2418 queue->IDR = MACB_IDR;
2419 queue->IMR = MACB_IMR;
2420 queue->TBQP = MACB_TBQP;
Harini Katakamfff80192016-08-09 13:15:53 +05302421#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2422 queue->TBQPH = MACB_TBQPH;
2423#endif
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002424 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002425
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002426 /* get irq: here we use the linux queue index, not the hardware
2427 * queue index. the queue irq definitions in the device tree
2428 * must remove the optional gaps that could exist in the
2429 * hardware queue mask.
2430 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002431 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002432 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002433 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002434 if (err) {
2435 dev_err(&pdev->dev,
2436 "Unable to request IRQ %d (error %d)\n",
2437 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002438 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002439 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002440
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002441 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002442 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002443 }
2444
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002445 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002446 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002447
Nicolas Ferre4df95132013-06-04 21:57:12 +00002448 /* setup appropriated routines according to adapter type */
2449 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002450 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002451 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2452 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2453 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2454 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002455 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002456 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002457 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002458 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2459 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2460 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2461 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002462 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002463 }
2464
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002465 /* Set features */
2466 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002467 /* Checksum offload is only available on gem with packet buffer */
2468 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002469 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002470 if (bp->caps & MACB_CAPS_SG_DISABLED)
2471 dev->hw_features &= ~NETIF_F_SG;
2472 dev->features = dev->hw_features;
2473
Neil Armstrongce721a72016-01-05 14:39:16 +01002474 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2475 val = 0;
2476 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2477 val = GEM_BIT(RGMII);
2478 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002479 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002480 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002481 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002482 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002483
Neil Armstrongce721a72016-01-05 14:39:16 +01002484 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2485 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002486
Neil Armstrongce721a72016-01-05 14:39:16 +01002487 macb_or_gem_writel(bp, USRIO, val);
2488 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002489
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002490 /* Set MII management clock divider */
2491 val = macb_mdc_clk_div(bp);
2492 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302493 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2494 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002495 macb_writel(bp, NCFGR, val);
2496
2497 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002498}
2499
2500#if defined(CONFIG_OF)
2501/* 1518 rounded up */
2502#define AT91ETHER_MAX_RBUFF_SZ 0x600
2503/* max number of receive buffers */
2504#define AT91ETHER_MAX_RX_DESCR 9
2505
2506/* Initialize and start the Receiver and Transmit subsystems */
2507static int at91ether_start(struct net_device *dev)
2508{
2509 struct macb *lp = netdev_priv(dev);
2510 dma_addr_t addr;
2511 u32 ctl;
2512 int i;
2513
2514 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2515 (AT91ETHER_MAX_RX_DESCR *
2516 sizeof(struct macb_dma_desc)),
2517 &lp->rx_ring_dma, GFP_KERNEL);
2518 if (!lp->rx_ring)
2519 return -ENOMEM;
2520
2521 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2522 AT91ETHER_MAX_RX_DESCR *
2523 AT91ETHER_MAX_RBUFF_SZ,
2524 &lp->rx_buffers_dma, GFP_KERNEL);
2525 if (!lp->rx_buffers) {
2526 dma_free_coherent(&lp->pdev->dev,
2527 AT91ETHER_MAX_RX_DESCR *
2528 sizeof(struct macb_dma_desc),
2529 lp->rx_ring, lp->rx_ring_dma);
2530 lp->rx_ring = NULL;
2531 return -ENOMEM;
2532 }
2533
2534 addr = lp->rx_buffers_dma;
2535 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2536 lp->rx_ring[i].addr = addr;
2537 lp->rx_ring[i].ctrl = 0;
2538 addr += AT91ETHER_MAX_RBUFF_SZ;
2539 }
2540
2541 /* Set the Wrap bit on the last descriptor */
2542 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2543
2544 /* Reset buffer index */
2545 lp->rx_tail = 0;
2546
2547 /* Program address of descriptor list in Rx Buffer Queue register */
2548 macb_writel(lp, RBQP, lp->rx_ring_dma);
2549
2550 /* Enable Receive and Transmit */
2551 ctl = macb_readl(lp, NCR);
2552 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2553
2554 return 0;
2555}
2556
2557/* Open the ethernet interface */
2558static int at91ether_open(struct net_device *dev)
2559{
2560 struct macb *lp = netdev_priv(dev);
2561 u32 ctl;
2562 int ret;
2563
2564 /* Clear internal statistics */
2565 ctl = macb_readl(lp, NCR);
2566 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2567
2568 macb_set_hwaddr(lp);
2569
2570 ret = at91ether_start(dev);
2571 if (ret)
2572 return ret;
2573
2574 /* Enable MAC interrupts */
2575 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2576 MACB_BIT(RXUBR) |
2577 MACB_BIT(ISR_TUND) |
2578 MACB_BIT(ISR_RLE) |
2579 MACB_BIT(TCOMP) |
2580 MACB_BIT(ISR_ROVR) |
2581 MACB_BIT(HRESP));
2582
2583 /* schedule a link state check */
Philippe Reynes0a912812016-06-22 00:32:35 +02002584 phy_start(dev->phydev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002585
2586 netif_start_queue(dev);
2587
2588 return 0;
2589}
2590
2591/* Close the interface */
2592static int at91ether_close(struct net_device *dev)
2593{
2594 struct macb *lp = netdev_priv(dev);
2595 u32 ctl;
2596
2597 /* Disable Receiver and Transmitter */
2598 ctl = macb_readl(lp, NCR);
2599 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2600
2601 /* Disable MAC interrupts */
2602 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2603 MACB_BIT(RXUBR) |
2604 MACB_BIT(ISR_TUND) |
2605 MACB_BIT(ISR_RLE) |
2606 MACB_BIT(TCOMP) |
2607 MACB_BIT(ISR_ROVR) |
2608 MACB_BIT(HRESP));
2609
2610 netif_stop_queue(dev);
2611
2612 dma_free_coherent(&lp->pdev->dev,
2613 AT91ETHER_MAX_RX_DESCR *
2614 sizeof(struct macb_dma_desc),
2615 lp->rx_ring, lp->rx_ring_dma);
2616 lp->rx_ring = NULL;
2617
2618 dma_free_coherent(&lp->pdev->dev,
2619 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2620 lp->rx_buffers, lp->rx_buffers_dma);
2621 lp->rx_buffers = NULL;
2622
2623 return 0;
2624}
2625
2626/* Transmit packet */
2627static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2628{
2629 struct macb *lp = netdev_priv(dev);
2630
2631 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2632 netif_stop_queue(dev);
2633
2634 /* Store packet information (to free when Tx completed) */
2635 lp->skb = skb;
2636 lp->skb_length = skb->len;
2637 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2638 DMA_TO_DEVICE);
2639
2640 /* Set address of the data in the Transmit Address register */
2641 macb_writel(lp, TAR, lp->skb_physaddr);
2642 /* Set length of the packet in the Transmit Control register */
2643 macb_writel(lp, TCR, skb->len);
2644
2645 } else {
2646 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2647 return NETDEV_TX_BUSY;
2648 }
2649
2650 return NETDEV_TX_OK;
2651}
2652
2653/* Extract received frame from buffer descriptors and sent to upper layers.
2654 * (Called from interrupt context)
2655 */
2656static void at91ether_rx(struct net_device *dev)
2657{
2658 struct macb *lp = netdev_priv(dev);
2659 unsigned char *p_recv;
2660 struct sk_buff *skb;
2661 unsigned int pktlen;
2662
2663 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2664 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2665 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2666 skb = netdev_alloc_skb(dev, pktlen + 2);
2667 if (skb) {
2668 skb_reserve(skb, 2);
2669 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2670
2671 skb->protocol = eth_type_trans(skb, dev);
2672 lp->stats.rx_packets++;
2673 lp->stats.rx_bytes += pktlen;
2674 netif_rx(skb);
2675 } else {
2676 lp->stats.rx_dropped++;
2677 }
2678
2679 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2680 lp->stats.multicast++;
2681
2682 /* reset ownership bit */
2683 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2684
2685 /* wrap after last buffer */
2686 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2687 lp->rx_tail = 0;
2688 else
2689 lp->rx_tail++;
2690 }
2691}
2692
2693/* MAC interrupt handler */
2694static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2695{
2696 struct net_device *dev = dev_id;
2697 struct macb *lp = netdev_priv(dev);
2698 u32 intstatus, ctl;
2699
2700 /* MAC Interrupt Status register indicates what interrupts are pending.
2701 * It is automatically cleared once read.
2702 */
2703 intstatus = macb_readl(lp, ISR);
2704
2705 /* Receive complete */
2706 if (intstatus & MACB_BIT(RCOMP))
2707 at91ether_rx(dev);
2708
2709 /* Transmit complete */
2710 if (intstatus & MACB_BIT(TCOMP)) {
2711 /* The TCOM bit is set even if the transmission failed */
2712 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2713 lp->stats.tx_errors++;
2714
2715 if (lp->skb) {
2716 dev_kfree_skb_irq(lp->skb);
2717 lp->skb = NULL;
2718 dma_unmap_single(NULL, lp->skb_physaddr,
2719 lp->skb_length, DMA_TO_DEVICE);
2720 lp->stats.tx_packets++;
2721 lp->stats.tx_bytes += lp->skb_length;
2722 }
2723 netif_wake_queue(dev);
2724 }
2725
2726 /* Work-around for EMAC Errata section 41.3.1 */
2727 if (intstatus & MACB_BIT(RXUBR)) {
2728 ctl = macb_readl(lp, NCR);
2729 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2730 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2731 }
2732
2733 if (intstatus & MACB_BIT(ISR_ROVR))
2734 netdev_err(dev, "ROVR error\n");
2735
2736 return IRQ_HANDLED;
2737}
2738
2739#ifdef CONFIG_NET_POLL_CONTROLLER
2740static void at91ether_poll_controller(struct net_device *dev)
2741{
2742 unsigned long flags;
2743
2744 local_irq_save(flags);
2745 at91ether_interrupt(dev->irq, dev);
2746 local_irq_restore(flags);
2747}
2748#endif
2749
2750static const struct net_device_ops at91ether_netdev_ops = {
2751 .ndo_open = at91ether_open,
2752 .ndo_stop = at91ether_close,
2753 .ndo_start_xmit = at91ether_start_xmit,
2754 .ndo_get_stats = macb_get_stats,
2755 .ndo_set_rx_mode = macb_set_rx_mode,
2756 .ndo_set_mac_address = eth_mac_addr,
2757 .ndo_do_ioctl = macb_ioctl,
2758 .ndo_validate_addr = eth_validate_addr,
2759 .ndo_change_mtu = eth_change_mtu,
2760#ifdef CONFIG_NET_POLL_CONTROLLER
2761 .ndo_poll_controller = at91ether_poll_controller,
2762#endif
2763};
2764
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002765static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
2766 struct clk **hclk, struct clk **tx_clk)
2767{
2768 int err;
2769
2770 *hclk = NULL;
2771 *tx_clk = NULL;
2772
2773 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2774 if (IS_ERR(*pclk))
2775 return PTR_ERR(*pclk);
2776
2777 err = clk_prepare_enable(*pclk);
2778 if (err) {
2779 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2780 return err;
2781 }
2782
2783 return 0;
2784}
2785
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002786static int at91ether_init(struct platform_device *pdev)
2787{
2788 struct net_device *dev = platform_get_drvdata(pdev);
2789 struct macb *bp = netdev_priv(dev);
2790 int err;
2791 u32 reg;
2792
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002793 dev->netdev_ops = &at91ether_netdev_ops;
2794 dev->ethtool_ops = &macb_ethtool_ops;
2795
2796 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2797 0, dev->name, dev);
2798 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002799 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002800
2801 macb_writel(bp, NCR, 0);
2802
2803 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2804 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2805 reg |= MACB_BIT(RM9200_RMII);
2806
2807 macb_writel(bp, NCFGR, reg);
2808
2809 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002810}
2811
David S. Miller3cef5c52015-03-09 23:38:02 -04002812static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002813 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002814 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002815 .init = macb_init,
2816};
2817
David S. Miller3cef5c52015-03-09 23:38:02 -04002818static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002819 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2820 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002821 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002822 .init = macb_init,
2823};
2824
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002825static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002826 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002827 .dma_burst_length = 16,
2828 .clk_init = macb_clk_init,
2829 .init = macb_init,
2830};
2831
David S. Miller3cef5c52015-03-09 23:38:02 -04002832static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002833 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
2834 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002835 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002836 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002837 .init = macb_init,
2838};
2839
David S. Miller3cef5c52015-03-09 23:38:02 -04002840static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002841 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002842 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002843 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002844 .init = macb_init,
2845};
2846
David S. Miller3cef5c52015-03-09 23:38:02 -04002847static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002848 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002849 .init = at91ether_init,
2850};
2851
Neil Armstronge611b5b2016-01-05 14:39:17 +01002852static const struct macb_config np4_config = {
2853 .caps = MACB_CAPS_USRIO_DISABLED,
2854 .clk_init = macb_clk_init,
2855 .init = macb_init,
2856};
David S. Miller36583eb2015-05-23 01:22:35 -04002857
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302858static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302859 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302860 .dma_burst_length = 16,
2861 .clk_init = macb_clk_init,
2862 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302863 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302864};
2865
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002866static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302867 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002868 .dma_burst_length = 16,
2869 .clk_init = macb_clk_init,
2870 .init = macb_init,
2871};
2872
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002873static const struct of_device_id macb_dt_ids[] = {
2874 { .compatible = "cdns,at32ap7000-macb" },
2875 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2876 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01002877 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002878 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2879 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002880 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002881 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2882 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2883 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2884 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302885 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002886 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002887 { /* sentinel */ }
2888};
2889MODULE_DEVICE_TABLE(of, macb_dt_ids);
2890#endif /* CONFIG_OF */
2891
2892static int macb_probe(struct platform_device *pdev)
2893{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002894 int (*clk_init)(struct platform_device *, struct clk **,
2895 struct clk **, struct clk **)
2896 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002897 int (*init)(struct platform_device *) = macb_init;
2898 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002899 struct device_node *phy_node;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002900 const struct macb_config *macb_config = NULL;
Sudip Mukherjee36df7452016-01-25 11:43:09 +05302901 struct clk *pclk, *hclk = NULL, *tx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002902 unsigned int queue_mask, num_queues;
2903 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002904 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002905 struct phy_device *phydev;
2906 struct net_device *dev;
2907 struct resource *regs;
2908 void __iomem *mem;
2909 const char *mac;
2910 struct macb *bp;
2911 int err;
2912
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002913 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2914 mem = devm_ioremap_resource(&pdev->dev, regs);
2915 if (IS_ERR(mem))
2916 return PTR_ERR(mem);
2917
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002918 if (np) {
2919 const struct of_device_id *match;
2920
2921 match = of_match_node(macb_dt_ids, np);
2922 if (match && match->data) {
2923 macb_config = match->data;
2924 clk_init = macb_config->clk_init;
2925 init = macb_config->init;
2926 }
2927 }
2928
2929 err = clk_init(pdev, &pclk, &hclk, &tx_clk);
2930 if (err)
2931 return err;
2932
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002933 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002934
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002935 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002936 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002937 if (!dev) {
2938 err = -ENOMEM;
2939 goto err_disable_clocks;
2940 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002941
2942 dev->base_addr = regs->start;
2943
2944 SET_NETDEV_DEV(dev, &pdev->dev);
2945
2946 bp = netdev_priv(dev);
2947 bp->pdev = pdev;
2948 bp->dev = dev;
2949 bp->regs = mem;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002950 bp->native_io = native_io;
2951 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07002952 bp->macb_reg_readl = hw_readl_native;
2953 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002954 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07002955 bp->macb_reg_readl = hw_readl;
2956 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a9e2015-07-24 21:23:59 +03002957 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002958 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002959 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002960 if (macb_config)
2961 bp->dma_burst_length = macb_config->dma_burst_length;
2962 bp->pclk = pclk;
2963 bp->hclk = hclk;
2964 bp->tx_clk = tx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03002965 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302966 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302967
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002968 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02002969 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002970 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
2971 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
2972
Harini Katakamfff80192016-08-09 13:15:53 +05302973#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2974 if (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1)) > GEM_DBW32)
2975 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
2976#endif
2977
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002978 spin_lock_init(&bp->lock);
2979
Nicolas Ferread783472015-03-31 15:02:02 +02002980 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02002981 macb_configure_caps(bp, macb_config);
2982
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002983 platform_set_drvdata(pdev, dev);
2984
2985 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002986 if (dev->irq < 0) {
2987 err = dev->irq;
2988 goto err_disable_clocks;
2989 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002990
2991 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00002992 if (mac)
Moritz Fischereefb52d2016-03-29 19:11:14 -07002993 ether_addr_copy(bp->dev->dev_addr, mac);
Guenter Roeck50907042013-04-02 09:35:09 +00002994 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002995 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002996
Gregory CLEMENT5833e052015-12-11 11:34:53 +01002997 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002998 phy_node = of_get_next_available_child(np, NULL);
2999 if (phy_node) {
3000 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07003001
Charles Keepax0e3e7992016-03-28 13:47:42 +01003002 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003003 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01003004 gpiod_direction_output(bp->reset_gpio, 1);
3005 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01003006 }
3007 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003008
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003009 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003010 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09003011 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003012 if (pdata && pdata->is_rmii)
3013 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3014 else
3015 bp->phy_interface = PHY_INTERFACE_MODE_MII;
3016 } else {
3017 bp->phy_interface = err;
3018 }
3019
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003020 /* IP specific init */
3021 err = init(pdev);
3022 if (err)
3023 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003024
Florian Fainellicf669662016-05-02 18:38:45 -07003025 err = macb_mii_init(bp);
3026 if (err)
3027 goto err_out_free_netdev;
3028
Philippe Reynes0a912812016-06-22 00:32:35 +02003029 phydev = dev->phydev;
Florian Fainellicf669662016-05-02 18:38:45 -07003030
3031 netif_carrier_off(dev);
3032
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003033 err = register_netdev(dev);
3034 if (err) {
3035 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Florian Fainellicf669662016-05-02 18:38:45 -07003036 goto err_out_unregister_mdio;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003037 }
3038
Florian Fainellicf669662016-05-02 18:38:45 -07003039 phy_attached_info(phydev);
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003040
Bo Shen58798232014-09-13 01:57:49 +02003041 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3042 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3043 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003044
3045 return 0;
3046
Florian Fainellicf669662016-05-02 18:38:45 -07003047err_out_unregister_mdio:
Philippe Reynes0a912812016-06-22 00:32:35 +02003048 phy_disconnect(dev->phydev);
Florian Fainellicf669662016-05-02 18:38:45 -07003049 mdiobus_unregister(bp->mii_bus);
3050 mdiobus_free(bp->mii_bus);
3051
3052 /* Shutdown the PHY if there is a GPIO reset */
3053 if (bp->reset_gpio)
3054 gpiod_set_value(bp->reset_gpio, 0);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003055
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003056err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003057 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003058
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003059err_disable_clocks:
3060 clk_disable_unprepare(tx_clk);
3061 clk_disable_unprepare(hclk);
3062 clk_disable_unprepare(pclk);
3063
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003064 return err;
3065}
3066
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003067static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003068{
3069 struct net_device *dev;
3070 struct macb *bp;
3071
3072 dev = platform_get_drvdata(pdev);
3073
3074 if (dev) {
3075 bp = netdev_priv(dev);
Philippe Reynes0a912812016-06-22 00:32:35 +02003076 if (dev->phydev)
3077 phy_disconnect(dev->phydev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003078 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003079 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003080
3081 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003082 if (bp->reset_gpio)
3083 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003084
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003085 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003086 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003087 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003088 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003089 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003090 }
3091
3092 return 0;
3093}
3094
Michal Simekd23823d2015-01-23 09:36:03 +01003095static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003096{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003097 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003098 struct net_device *netdev = platform_get_drvdata(pdev);
3099 struct macb *bp = netdev_priv(netdev);
3100
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003101 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003102 netif_device_detach(netdev);
3103
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003104 if (bp->wol & MACB_WOL_ENABLED) {
3105 macb_writel(bp, IER, MACB_BIT(WOL));
3106 macb_writel(bp, WOL, MACB_BIT(MAG));
3107 enable_irq_wake(bp->queues[0].irq);
3108 } else {
3109 clk_disable_unprepare(bp->tx_clk);
3110 clk_disable_unprepare(bp->hclk);
3111 clk_disable_unprepare(bp->pclk);
3112 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003113
3114 return 0;
3115}
3116
Michal Simekd23823d2015-01-23 09:36:03 +01003117static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003118{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003119 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003120 struct net_device *netdev = platform_get_drvdata(pdev);
3121 struct macb *bp = netdev_priv(netdev);
3122
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003123 if (bp->wol & MACB_WOL_ENABLED) {
3124 macb_writel(bp, IDR, MACB_BIT(WOL));
3125 macb_writel(bp, WOL, 0);
3126 disable_irq_wake(bp->queues[0].irq);
3127 } else {
3128 clk_prepare_enable(bp->pclk);
3129 clk_prepare_enable(bp->hclk);
3130 clk_prepare_enable(bp->tx_clk);
3131 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003132
3133 netif_device_attach(netdev);
3134
3135 return 0;
3136}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003137
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003138static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3139
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003140static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003141 .probe = macb_probe,
3142 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003143 .driver = {
3144 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003145 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003146 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003147 },
3148};
3149
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003150module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003151
3152MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003153MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003154MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003155MODULE_ALIAS("platform:macb");