blob: f25681ba64180706702378af0673744138de59e6 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010035
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010036#include "macb.h"
37
Nicolas Ferre1b447912013-06-04 21:57:11 +000038#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000039#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000040#define RX_RING_SIZE 512 /* must be power of 2 */
41#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010042
Havard Skinnemoen55054a12012-10-31 06:04:55 +000043#define TX_RING_SIZE 128 /* must be power of 2 */
44#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010045
Nicolas Ferre909a8582012-11-19 06:00:21 +000046/* level of occupied TX descriptors under which we wake up TX process */
47#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010048
49#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
50 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000051#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
52 | MACB_BIT(ISR_RLE) \
53 | MACB_BIT(TXERR))
54#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
55
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020056#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
57#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
58
Harini Katakama5898ea2015-05-06 22:27:18 +053059#define GEM_MTU_MIN_SIZE 68
60
Sergio Prado3e2a5e12016-02-09 12:07:16 -020061#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
62#define MACB_WOL_ENABLED (0x1 << 1)
63
Moritz Fischer64ec42f2016-03-29 19:11:12 -070064/* Graceful stop timeouts in us. We should allow up to
Nicolas Ferree86cd532012-10-31 06:04:57 +000065 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
66 */
67#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010068
Havard Skinnemoen55054a12012-10-31 06:04:55 +000069/* Ring buffer accessors */
70static unsigned int macb_tx_ring_wrap(unsigned int index)
71{
72 return index & (TX_RING_SIZE - 1);
73}
74
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010075static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
76 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000077{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010078 return &queue->tx_ring[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000079}
80
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010081static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
82 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000083{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010084 return &queue->tx_skb[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000085}
86
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010087static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000088{
89 dma_addr_t offset;
90
91 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
92
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010093 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +000094}
95
96static unsigned int macb_rx_ring_wrap(unsigned int index)
97{
98 return index & (RX_RING_SIZE - 1);
99}
100
101static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
102{
103 return &bp->rx_ring[macb_rx_ring_wrap(index)];
104}
105
106static void *macb_rx_buffer(struct macb *bp, unsigned int index)
107{
Nicolas Ferre1b447912013-06-04 21:57:11 +0000108 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000109}
110
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300111/* I/O accessors */
112static u32 hw_readl_native(struct macb *bp, int offset)
113{
114 return __raw_readl(bp->regs + offset);
115}
116
117static void hw_writel_native(struct macb *bp, int offset, u32 value)
118{
119 __raw_writel(value, bp->regs + offset);
120}
121
122static u32 hw_readl(struct macb *bp, int offset)
123{
124 return readl_relaxed(bp->regs + offset);
125}
126
127static void hw_writel(struct macb *bp, int offset, u32 value)
128{
129 writel_relaxed(value, bp->regs + offset);
130}
131
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700132/* Find the CPU endianness by using the loopback bit of NCR register. When the
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300133 * CPU is in big endian we need to program swaped mode for management
134 * 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
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000190 /* Check all 4 address register for vaild address */
191 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",
298 rate);
299
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);
307 struct phy_device *phydev = bp->phy_dev;
308 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200309 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100310
frederic RODO6c36a702007-07-12 19:07:24 +0200311 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100312
frederic RODO6c36a702007-07-12 19:07:24 +0200313 if (phydev->link) {
314 if ((bp->speed != phydev->speed) ||
315 (bp->duplex != phydev->duplex)) {
316 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100317
frederic RODO6c36a702007-07-12 19:07:24 +0200318 reg = macb_readl(bp, NCFGR);
319 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000320 if (macb_is_gem(bp))
321 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200322
323 if (phydev->duplex)
324 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900325 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200326 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200327 if (phydev->speed == SPEED_1000 &&
328 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000329 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200330
Patrice Vilchez140b7552012-10-31 06:04:50 +0000331 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200332
333 bp->speed = phydev->speed;
334 bp->duplex = phydev->duplex;
335 status_change = 1;
336 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100337 }
338
frederic RODO6c36a702007-07-12 19:07:24 +0200339 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700340 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200341 bp->speed = 0;
342 bp->duplex = -1;
343 }
344 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100345
frederic RODO6c36a702007-07-12 19:07:24 +0200346 status_change = 1;
347 }
348
349 spin_unlock_irqrestore(&bp->lock, flags);
350
351 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000352 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500353 /* Update the TX clock rate if and only if the link is
354 * up and there has been a link change.
355 */
356 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
357
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000358 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000359 netdev_info(dev, "link up (%d/%s)\n",
360 phydev->speed,
361 phydev->duplex == DUPLEX_FULL ?
362 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000363 } else {
364 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000365 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000366 }
frederic RODO6c36a702007-07-12 19:07:24 +0200367 }
368}
369
370/* based on au1000_eth. c*/
371static int macb_mii_probe(struct net_device *dev)
372{
373 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000374 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000375 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000376 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000377 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200378
Jiri Pirko7455a762010-02-08 05:12:08 +0000379 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200380 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000381 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200382 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200383 }
384
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000385 pdata = dev_get_platdata(&bp->pdev->dev);
386 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700387 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin,
388 "phy int");
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000389 if (!ret) {
390 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
391 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
392 }
393 }
frederic RODO6c36a702007-07-12 19:07:24 +0200394
395 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000396 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100397 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000398 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000399 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000400 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200401 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100402
frederic RODO6c36a702007-07-12 19:07:24 +0200403 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200404 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000405 phydev->supported &= PHY_GBIT_FEATURES;
406 else
407 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100408
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500409 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
410 phydev->supported &= ~SUPPORTED_1000baseT_Half;
411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100413
frederic RODO6c36a702007-07-12 19:07:24 +0200414 bp->link = 0;
415 bp->speed = 0;
416 bp->duplex = -1;
417 bp->phy_dev = phydev;
418
419 return 0;
420}
421
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100422static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200423{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000424 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200425 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200426 int err = -ENXIO, i;
427
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200428 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200429 macb_writel(bp, NCR, MACB_BIT(MPE));
430
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700431 bp->mii_bus = mdiobus_alloc();
432 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200433 err = -ENOMEM;
434 goto err_out;
435 }
436
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700437 bp->mii_bus->name = "MACB_mii_bus";
438 bp->mii_bus->read = &macb_mdio_read;
439 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000440 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
441 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700442 bp->mii_bus->priv = bp;
443 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900444 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700445
Jamie Iles91523942011-02-28 04:05:25 +0000446 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200447
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200448 np = bp->pdev->dev.of_node;
449 if (np) {
450 /* try dt phy registration */
451 err = of_mdiobus_register(bp->mii_bus, np);
452
453 /* fallback to standard phy registration if no phy were
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700454 * found during dt phy registration
455 */
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200456 if (!err && !phy_find_first(bp->mii_bus)) {
457 for (i = 0; i < PHY_MAX_ADDR; i++) {
458 struct phy_device *phydev;
459
460 phydev = mdiobus_scan(bp->mii_bus, i);
461 if (IS_ERR(phydev)) {
462 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 Shevchenkof2ce8a92015-07-24 21:23:59 +0300498 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100499
500 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
501
Moritz Fischer96ec6312016-03-29 19:11:11 -0700502 for (; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700503 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100504}
505
Nicolas Ferree86cd532012-10-31 06:04:57 +0000506static int macb_halt_tx(struct macb *bp)
507{
508 unsigned long halt_time, timeout;
509 u32 status;
510
511 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
512
513 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
514 do {
515 halt_time = jiffies;
516 status = macb_readl(bp, TSR);
517 if (!(status & MACB_BIT(TGO)))
518 return 0;
519
520 usleep_range(10, 250);
521 } while (time_before(halt_time, timeout));
522
523 return -ETIMEDOUT;
524}
525
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200526static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
527{
528 if (tx_skb->mapping) {
529 if (tx_skb->mapped_as_page)
530 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
531 tx_skb->size, DMA_TO_DEVICE);
532 else
533 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
534 tx_skb->size, DMA_TO_DEVICE);
535 tx_skb->mapping = 0;
536 }
537
538 if (tx_skb->skb) {
539 dev_kfree_skb_any(tx_skb->skb);
540 tx_skb->skb = NULL;
541 }
542}
543
Nicolas Ferree86cd532012-10-31 06:04:57 +0000544static void macb_tx_error_task(struct work_struct *work)
545{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100546 struct macb_queue *queue = container_of(work, struct macb_queue,
547 tx_error_task);
548 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000549 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100550 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000551 struct sk_buff *skb;
552 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100553 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000554
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100555 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
556 (unsigned int)(queue - bp->queues),
557 queue->tx_tail, queue->tx_head);
558
559 /* Prevent the queue IRQ handlers from running: each of them may call
560 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
561 * As explained below, we have to halt the transmission before updating
562 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
563 * network engine about the macb/gem being halted.
564 */
565 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000566
567 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100568 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000569
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700570 /* Stop transmission now
Nicolas Ferree86cd532012-10-31 06:04:57 +0000571 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100572 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000573 */
574 if (macb_halt_tx(bp))
575 /* Just complain for now, reinitializing TX path can be good */
576 netdev_err(bp->dev, "BUG: halt tx timed out\n");
577
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700578 /* Treat frames in TX queue including the ones that caused the error.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000579 * Free transmit buffers in upper layer.
580 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100581 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
582 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000583
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100584 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000585 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100586 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000587 skb = tx_skb->skb;
588
589 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200590 /* skb is set for the last buffer of the frame */
591 while (!skb) {
592 macb_tx_unmap(bp, tx_skb);
593 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100594 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200595 skb = tx_skb->skb;
596 }
597
598 /* ctrl still refers to the first buffer descriptor
599 * since it's the only one written back by the hardware
600 */
601 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
602 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
603 macb_tx_ring_wrap(tail), skb->data);
604 bp->stats.tx_packets++;
605 bp->stats.tx_bytes += skb->len;
606 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000607 } else {
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700608 /* "Buffers exhausted mid-frame" errors may only happen
609 * if the driver is buggy, so complain loudly about
610 * those. Statistics are updated by hardware.
Nicolas Ferree86cd532012-10-31 06:04:57 +0000611 */
612 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
613 netdev_err(bp->dev,
614 "BUG: TX buffers exhausted mid-frame\n");
615
616 desc->ctrl = ctrl | MACB_BIT(TX_USED);
617 }
618
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200619 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000620 }
621
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100622 /* Set end of TX queue */
623 desc = macb_tx_desc(queue, 0);
624 desc->addr = 0;
625 desc->ctrl = MACB_BIT(TX_USED);
626
Nicolas Ferree86cd532012-10-31 06:04:57 +0000627 /* Make descriptor updates visible to hardware */
628 wmb();
629
630 /* Reinitialize the TX desc queue */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100631 queue_writel(queue, TBQP, queue->tx_ring_dma);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000632 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100633 queue->tx_head = 0;
634 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000635
636 /* Housework before enabling TX IRQ */
637 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100638 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
639
640 /* Now we are ready to start transmission again */
641 netif_tx_start_all_queues(bp->dev);
642 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
643
644 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000645}
646
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100647static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100648{
649 unsigned int tail;
650 unsigned int head;
651 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100652 struct macb *bp = queue->bp;
653 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100654
655 status = macb_readl(bp, TSR);
656 macb_writel(bp, TSR, status);
657
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000658 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100659 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000660
Nicolas Ferree86cd532012-10-31 06:04:57 +0000661 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
662 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100663
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100664 head = queue->tx_head;
665 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000666 struct macb_tx_skb *tx_skb;
667 struct sk_buff *skb;
668 struct macb_dma_desc *desc;
669 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100670
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100671 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100672
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000673 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000675
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000676 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100677
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200678 /* TX_USED bit is only set by hardware on the very first buffer
679 * descriptor of the transmitted frame.
680 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000681 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100682 break;
683
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200684 /* Process all buffers of the current transmitted frame */
685 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100686 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200687 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000688
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200689 /* First, update TX stats if needed */
690 if (skb) {
691 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
692 macb_tx_ring_wrap(tail), skb->data);
693 bp->stats.tx_packets++;
694 bp->stats.tx_bytes += skb->len;
695 }
696
697 /* Now we can safely release resources */
698 macb_tx_unmap(bp, tx_skb);
699
700 /* skb is set only for the last buffer of the frame.
701 * WARNING: at this point skb has been freed by
702 * macb_tx_unmap().
703 */
704 if (skb)
705 break;
706 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100707 }
708
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100709 queue->tx_tail = tail;
710 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
711 CIRC_CNT(queue->tx_head, queue->tx_tail,
712 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
713 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100714}
715
Nicolas Ferre4df95132013-06-04 21:57:12 +0000716static void gem_rx_refill(struct macb *bp)
717{
718 unsigned int entry;
719 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000720 dma_addr_t paddr;
721
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700722 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
723 RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000724 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000725
726 /* Make hw descriptor updates visible to CPU */
727 rmb();
728
Nicolas Ferre4df95132013-06-04 21:57:12 +0000729 bp->rx_prepared_head++;
730
Nicolas Ferre4df95132013-06-04 21:57:12 +0000731 if (bp->rx_skbuff[entry] == NULL) {
732 /* allocate sk_buff for this free entry in ring */
733 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
734 if (unlikely(skb == NULL)) {
735 netdev_err(bp->dev,
736 "Unable to allocate sk_buff\n");
737 break;
738 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000739
740 /* now fill corresponding descriptor entry */
741 paddr = dma_map_single(&bp->pdev->dev, skb->data,
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700742 bp->rx_buffer_size,
743 DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800744 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
745 dev_kfree_skb(skb);
746 break;
747 }
748
749 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000750
751 if (entry == RX_RING_SIZE - 1)
752 paddr |= MACB_BIT(RX_WRAP);
753 bp->rx_ring[entry].addr = paddr;
754 bp->rx_ring[entry].ctrl = 0;
755
756 /* properly align Ethernet header */
757 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530758 } else {
759 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
760 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000761 }
762 }
763
764 /* Make descriptor updates visible to hardware */
765 wmb();
766
767 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
768 bp->rx_prepared_head, bp->rx_tail);
769}
770
771/* Mark DMA descriptors from begin up to and not including end as unused */
772static void discard_partial_frame(struct macb *bp, unsigned int begin,
773 unsigned int end)
774{
775 unsigned int frag;
776
777 for (frag = begin; frag != end; frag++) {
778 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700779
Nicolas Ferre4df95132013-06-04 21:57:12 +0000780 desc->addr &= ~MACB_BIT(RX_USED);
781 }
782
783 /* Make descriptor updates visible to hardware */
784 wmb();
785
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700786 /* When this happens, the hardware stats registers for
Nicolas Ferre4df95132013-06-04 21:57:12 +0000787 * whatever caused this is updated, so we don't have to record
788 * anything.
789 */
790}
791
792static int gem_rx(struct macb *bp, int budget)
793{
794 unsigned int len;
795 unsigned int entry;
796 struct sk_buff *skb;
797 struct macb_dma_desc *desc;
798 int count = 0;
799
800 while (count < budget) {
801 u32 addr, ctrl;
802
803 entry = macb_rx_ring_wrap(bp->rx_tail);
804 desc = &bp->rx_ring[entry];
805
806 /* Make hw descriptor updates visible to CPU */
807 rmb();
808
809 addr = desc->addr;
810 ctrl = desc->ctrl;
811
812 if (!(addr & MACB_BIT(RX_USED)))
813 break;
814
Nicolas Ferre4df95132013-06-04 21:57:12 +0000815 bp->rx_tail++;
816 count++;
817
818 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
819 netdev_err(bp->dev,
820 "not whole frame pointed by descriptor\n");
821 bp->stats.rx_dropped++;
822 break;
823 }
824 skb = bp->rx_skbuff[entry];
825 if (unlikely(!skb)) {
826 netdev_err(bp->dev,
827 "inconsistent Rx descriptor chain\n");
828 bp->stats.rx_dropped++;
829 break;
830 }
831 /* now everything is ready for receiving packet */
832 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530833 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000834
835 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
836
837 skb_put(skb, len);
838 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
839 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800840 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000841
842 skb->protocol = eth_type_trans(skb, bp->dev);
843 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200844 if (bp->dev->features & NETIF_F_RXCSUM &&
845 !(bp->dev->flags & IFF_PROMISC) &&
846 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
847 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000848
849 bp->stats.rx_packets++;
850 bp->stats.rx_bytes += skb->len;
851
852#if defined(DEBUG) && defined(VERBOSE_DEBUG)
853 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
854 skb->len, skb->csum);
855 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100856 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000857 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
858 skb->data, 32, true);
859#endif
860
861 netif_receive_skb(skb);
862 }
863
864 gem_rx_refill(bp);
865
866 return count;
867}
868
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100869static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
870 unsigned int last_frag)
871{
872 unsigned int len;
873 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000874 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100875 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000876 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100877
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000878 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530879 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100880
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000881 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000882 macb_rx_ring_wrap(first_frag),
883 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100884
Moritz Fischer64ec42f2016-03-29 19:11:12 -0700885 /* The ethernet header starts NET_IP_ALIGN bytes into the
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000886 * first buffer. Since the header is 14 bytes, this makes the
887 * payload word-aligned.
888 *
889 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
890 * the two padding bytes into the skb so that we avoid hitting
891 * the slowpath in memcpy(), and pull them off afterwards.
892 */
893 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100894 if (!skb) {
895 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000896 for (frag = first_frag; ; frag++) {
897 desc = macb_rx_desc(bp, frag);
898 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100899 if (frag == last_frag)
900 break;
901 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000902
903 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100904 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000905
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100906 return 1;
907 }
908
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000909 offset = 0;
910 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700911 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100912 skb_put(skb, len);
913
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000914 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000915 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100916
917 if (offset + frag_len > len) {
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100918 if (unlikely(frag != last_frag)) {
919 dev_kfree_skb_any(skb);
920 return -1;
921 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100922 frag_len = len - offset;
923 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300924 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000925 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000926 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000927 desc = macb_rx_desc(bp, frag);
928 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100929
930 if (frag == last_frag)
931 break;
932 }
933
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000934 /* Make descriptor updates visible to hardware */
935 wmb();
936
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000937 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100938 skb->protocol = eth_type_trans(skb, bp->dev);
939
940 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000941 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000942 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000943 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100944 netif_receive_skb(skb);
945
946 return 0;
947}
948
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100949static inline void macb_init_rx_ring(struct macb *bp)
950{
951 dma_addr_t addr;
952 int i;
953
954 addr = bp->rx_buffers_dma;
955 for (i = 0; i < RX_RING_SIZE; i++) {
956 bp->rx_ring[i].addr = addr;
957 bp->rx_ring[i].ctrl = 0;
958 addr += bp->rx_buffer_size;
959 }
960 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
961}
962
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100963static int macb_rx(struct macb *bp, int budget)
964{
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100965 bool reset_rx_queue = false;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100966 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000967 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100968 int first_frag = -1;
969
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000970 for (tail = bp->rx_tail; budget > 0; tail++) {
971 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100972 u32 addr, ctrl;
973
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000974 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100975 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000976
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000977 addr = desc->addr;
978 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100979
980 if (!(addr & MACB_BIT(RX_USED)))
981 break;
982
983 if (ctrl & MACB_BIT(RX_SOF)) {
984 if (first_frag != -1)
985 discard_partial_frame(bp, first_frag, tail);
986 first_frag = tail;
987 }
988
989 if (ctrl & MACB_BIT(RX_EOF)) {
990 int dropped;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100991
992 if (unlikely(first_frag == -1)) {
993 reset_rx_queue = true;
994 continue;
995 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100996
997 dropped = macb_rx_frame(bp, first_frag, tail);
998 first_frag = -1;
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +0100999 if (unlikely(dropped < 0)) {
1000 reset_rx_queue = true;
1001 continue;
1002 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001003 if (!dropped) {
1004 received++;
1005 budget--;
1006 }
1007 }
1008 }
1009
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001010 if (unlikely(reset_rx_queue)) {
1011 unsigned long flags;
1012 u32 ctrl;
1013
1014 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1015
1016 spin_lock_irqsave(&bp->lock, flags);
1017
1018 ctrl = macb_readl(bp, NCR);
1019 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1020
1021 macb_init_rx_ring(bp);
1022 macb_writel(bp, RBQP, bp->rx_ring_dma);
1023
1024 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1025
1026 spin_unlock_irqrestore(&bp->lock, flags);
1027 return received;
1028 }
1029
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001030 if (first_frag != -1)
1031 bp->rx_tail = first_frag;
1032 else
1033 bp->rx_tail = tail;
1034
1035 return received;
1036}
1037
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001038static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001039{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001040 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001041 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001042 u32 status;
1043
1044 status = macb_readl(bp, RSR);
1045 macb_writel(bp, RSR, status);
1046
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001047 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001048
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001049 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001050 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001051
Nicolas Ferre4df95132013-06-04 21:57:12 +00001052 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001053 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001054 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001055
Nicolas Ferre8770e912013-02-12 11:08:48 +01001056 /* Packets received while interrupts were disabled */
1057 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001058 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001059 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1060 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001061 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001062 } else {
1063 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1064 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001065 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001066
1067 /* TODO: Handle errors */
1068
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001069 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001070}
1071
1072static irqreturn_t macb_interrupt(int irq, void *dev_id)
1073{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001074 struct macb_queue *queue = dev_id;
1075 struct macb *bp = queue->bp;
1076 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001077 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001078
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001079 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001080
1081 if (unlikely(!status))
1082 return IRQ_NONE;
1083
1084 spin_lock(&bp->lock);
1085
1086 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001087 /* close possible race with dev_close */
1088 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001089 queue_writel(queue, IDR, -1);
Nathan Sullivan24468372016-01-14 13:27:27 -06001090 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1091 queue_writel(queue, ISR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001092 break;
1093 }
1094
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001095 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1096 (unsigned int)(queue - bp->queues),
1097 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001098
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001099 if (status & MACB_RX_INT_FLAGS) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001100 /* There's no point taking any more interrupts
Joshua Hokeb3363692010-10-25 01:44:22 +00001101 * until we have processed the buffers. The
1102 * scheduling call may fail if the poll routine
1103 * is already scheduled, so disable interrupts
1104 * now.
1105 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001106 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001107 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001108 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001109
Ben Hutchings288379f2009-01-19 16:43:59 -08001110 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001111 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001112 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001113 }
1114 }
1115
Nicolas Ferree86cd532012-10-31 06:04:57 +00001116 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001117 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1118 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001119
1120 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001121 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001122
Nicolas Ferree86cd532012-10-31 06:04:57 +00001123 break;
1124 }
1125
1126 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001127 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001128
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001129 /* Link change detection isn't possible with RMII, so we'll
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001130 * add that if/when we get our hands on a full-blown MII PHY.
1131 */
1132
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001133 /* There is a hardware issue under heavy load where DMA can
1134 * stop, this causes endless "used buffer descriptor read"
1135 * interrupts but it can be cleared by re-enabling RX. See
1136 * the at91 manual, section 41.3.1 or the Zynq manual
1137 * section 16.7.4 for details.
1138 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001139 if (status & MACB_BIT(RXUBR)) {
1140 ctrl = macb_readl(bp, NCR);
1141 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1142 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1143
1144 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchenba504992016-03-24 15:40:04 +01001145 queue_writel(queue, ISR, MACB_BIT(RXUBR));
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001146 }
1147
Alexander Steinb19f7f72011-04-13 05:03:24 +00001148 if (status & MACB_BIT(ISR_ROVR)) {
1149 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001150 if (macb_is_gem(bp))
1151 bp->hw_stats.gem.rx_overruns++;
1152 else
1153 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001154
1155 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001156 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001157 }
1158
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001159 if (status & MACB_BIT(HRESP)) {
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001160 /* TODO: Reset the hardware, and maybe move the
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001161 * netdev_err to a lower-priority context as well
1162 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001163 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001164 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001165
1166 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001167 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001168 }
1169
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001170 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001171 }
1172
1173 spin_unlock(&bp->lock);
1174
1175 return IRQ_HANDLED;
1176}
1177
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001178#ifdef CONFIG_NET_POLL_CONTROLLER
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001179/* Polling receive - used by netconsole and other diagnostic tools
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001180 * to allow network i/o with interrupts disabled.
1181 */
1182static void macb_poll_controller(struct net_device *dev)
1183{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001184 struct macb *bp = netdev_priv(dev);
1185 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001186 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001187 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001188
1189 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001190 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1191 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001192 local_irq_restore(flags);
1193}
1194#endif
1195
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001196static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001197 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001198 struct sk_buff *skb)
1199{
1200 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001201 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001202 struct macb_tx_skb *tx_skb = NULL;
1203 struct macb_dma_desc *desc;
1204 unsigned int offset, size, count = 0;
1205 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1206 unsigned int eof = 1;
1207 u32 ctrl;
1208
1209 /* First, map non-paged data */
1210 len = skb_headlen(skb);
1211 offset = 0;
1212 while (len) {
1213 size = min(len, bp->max_tx_length);
1214 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001215 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001216
1217 mapping = dma_map_single(&bp->pdev->dev,
1218 skb->data + offset,
1219 size, DMA_TO_DEVICE);
1220 if (dma_mapping_error(&bp->pdev->dev, mapping))
1221 goto dma_error;
1222
1223 /* Save info to properly release resources */
1224 tx_skb->skb = NULL;
1225 tx_skb->mapping = mapping;
1226 tx_skb->size = size;
1227 tx_skb->mapped_as_page = false;
1228
1229 len -= size;
1230 offset += size;
1231 count++;
1232 tx_head++;
1233 }
1234
1235 /* Then, map paged data from fragments */
1236 for (f = 0; f < nr_frags; f++) {
1237 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1238
1239 len = skb_frag_size(frag);
1240 offset = 0;
1241 while (len) {
1242 size = min(len, bp->max_tx_length);
1243 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001244 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001245
1246 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1247 offset, size, DMA_TO_DEVICE);
1248 if (dma_mapping_error(&bp->pdev->dev, mapping))
1249 goto dma_error;
1250
1251 /* Save info to properly release resources */
1252 tx_skb->skb = NULL;
1253 tx_skb->mapping = mapping;
1254 tx_skb->size = size;
1255 tx_skb->mapped_as_page = true;
1256
1257 len -= size;
1258 offset += size;
1259 count++;
1260 tx_head++;
1261 }
1262 }
1263
1264 /* Should never happen */
1265 if (unlikely(tx_skb == NULL)) {
1266 netdev_err(bp->dev, "BUG! empty skb!\n");
1267 return 0;
1268 }
1269
1270 /* This is the last buffer of the frame: save socket buffer */
1271 tx_skb->skb = skb;
1272
1273 /* Update TX ring: update buffer descriptors in reverse order
1274 * to avoid race condition
1275 */
1276
1277 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1278 * to set the end of TX queue
1279 */
1280 i = tx_head;
1281 entry = macb_tx_ring_wrap(i);
1282 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001283 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001284 desc->ctrl = ctrl;
1285
1286 do {
1287 i--;
1288 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001289 tx_skb = &queue->tx_skb[entry];
1290 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001291
1292 ctrl = (u32)tx_skb->size;
1293 if (eof) {
1294 ctrl |= MACB_BIT(TX_LAST);
1295 eof = 0;
1296 }
1297 if (unlikely(entry == (TX_RING_SIZE - 1)))
1298 ctrl |= MACB_BIT(TX_WRAP);
1299
1300 /* Set TX buffer descriptor */
1301 desc->addr = tx_skb->mapping;
1302 /* desc->addr must be visible to hardware before clearing
1303 * 'TX_USED' bit in desc->ctrl.
1304 */
1305 wmb();
1306 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001307 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001308
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001309 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001310
1311 return count;
1312
1313dma_error:
1314 netdev_err(bp->dev, "TX DMA map failed\n");
1315
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001316 for (i = queue->tx_head; i != tx_head; i++) {
1317 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001318
1319 macb_tx_unmap(bp, tx_skb);
1320 }
1321
1322 return 0;
1323}
1324
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001325static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1326{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001327 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001328 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001329 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001330 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001331 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001332
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001333#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1334 netdev_vdbg(bp->dev,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001335 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1336 queue_index, skb->len, skb->head, skb->data,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001337 skb_tail_pointer(skb), skb_end_pointer(skb));
1338 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1339 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001340#endif
1341
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001342 /* Count how many TX buffer descriptors are needed to send this
1343 * socket buffer: skb fragments of jumbo frames may need to be
1344 * splitted into many buffer descriptors.
1345 */
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001346 count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001347 nr_frags = skb_shinfo(skb)->nr_frags;
1348 for (f = 0; f < nr_frags; f++) {
1349 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001350 count += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001351 }
1352
Dongdong Deng48719532009-08-23 19:49:07 -07001353 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001354
1355 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001356 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1357 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001358 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001359 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001360 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001361 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001362 }
1363
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001364 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001365 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001366 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001367 goto unlock;
1368 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001369
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001370 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001371 wmb();
1372
Richard Cochrane0720922011-06-19 21:51:28 +00001373 skb_tx_timestamp(skb);
1374
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001375 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1376
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001377 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1378 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001379
Soren Brinkmann92030902014-03-04 08:46:39 -08001380unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001381 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001382
Patrick McHardy6ed10652009-06-23 06:03:08 +00001383 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001384}
1385
Nicolas Ferre4df95132013-06-04 21:57:12 +00001386static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001387{
1388 if (!macb_is_gem(bp)) {
1389 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1390 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001391 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001392
Nicolas Ferre1b447912013-06-04 21:57:11 +00001393 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001394 netdev_dbg(bp->dev,
1395 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001396 RX_BUFFER_MULTIPLE);
1397 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001398 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001399 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001400 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001401
1402 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1403 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001404}
1405
Nicolas Ferre4df95132013-06-04 21:57:12 +00001406static void gem_free_rx_buffers(struct macb *bp)
1407{
1408 struct sk_buff *skb;
1409 struct macb_dma_desc *desc;
1410 dma_addr_t addr;
1411 int i;
1412
1413 if (!bp->rx_skbuff)
1414 return;
1415
1416 for (i = 0; i < RX_RING_SIZE; i++) {
1417 skb = bp->rx_skbuff[i];
1418
1419 if (skb == NULL)
1420 continue;
1421
1422 desc = &bp->rx_ring[i];
1423 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001424 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001425 DMA_FROM_DEVICE);
1426 dev_kfree_skb_any(skb);
1427 skb = NULL;
1428 }
1429
1430 kfree(bp->rx_skbuff);
1431 bp->rx_skbuff = NULL;
1432}
1433
1434static void macb_free_rx_buffers(struct macb *bp)
1435{
1436 if (bp->rx_buffers) {
1437 dma_free_coherent(&bp->pdev->dev,
1438 RX_RING_SIZE * bp->rx_buffer_size,
1439 bp->rx_buffers, bp->rx_buffers_dma);
1440 bp->rx_buffers = NULL;
1441 }
1442}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001443
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001444static void macb_free_consistent(struct macb *bp)
1445{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001446 struct macb_queue *queue;
1447 unsigned int q;
1448
Nicolas Ferre4df95132013-06-04 21:57:12 +00001449 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001450 if (bp->rx_ring) {
1451 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1452 bp->rx_ring, bp->rx_ring_dma);
1453 bp->rx_ring = NULL;
1454 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001455
1456 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1457 kfree(queue->tx_skb);
1458 queue->tx_skb = NULL;
1459 if (queue->tx_ring) {
1460 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1461 queue->tx_ring, queue->tx_ring_dma);
1462 queue->tx_ring = NULL;
1463 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001464 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001465}
1466
1467static int gem_alloc_rx_buffers(struct macb *bp)
1468{
1469 int size;
1470
1471 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1472 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1473 if (!bp->rx_skbuff)
1474 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001475
1476 netdev_dbg(bp->dev,
1477 "Allocated %d RX struct sk_buff entries at %p\n",
1478 RX_RING_SIZE, bp->rx_skbuff);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001479 return 0;
1480}
1481
1482static int macb_alloc_rx_buffers(struct macb *bp)
1483{
1484 int size;
1485
1486 size = RX_RING_SIZE * bp->rx_buffer_size;
1487 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1488 &bp->rx_buffers_dma, GFP_KERNEL);
1489 if (!bp->rx_buffers)
1490 return -ENOMEM;
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001491
1492 netdev_dbg(bp->dev,
1493 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1494 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001495 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001496}
1497
1498static int macb_alloc_consistent(struct macb *bp)
1499{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001500 struct macb_queue *queue;
1501 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001502 int size;
1503
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001504 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1505 size = TX_RING_BYTES;
1506 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1507 &queue->tx_ring_dma,
1508 GFP_KERNEL);
1509 if (!queue->tx_ring)
1510 goto out_err;
1511 netdev_dbg(bp->dev,
1512 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1513 q, size, (unsigned long)queue->tx_ring_dma,
1514 queue->tx_ring);
1515
1516 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1517 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1518 if (!queue->tx_skb)
1519 goto out_err;
1520 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001521
1522 size = RX_RING_BYTES;
1523 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1524 &bp->rx_ring_dma, GFP_KERNEL);
1525 if (!bp->rx_ring)
1526 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001527 netdev_dbg(bp->dev,
1528 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1529 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001530
Nicolas Ferre4df95132013-06-04 21:57:12 +00001531 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001532 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001533
1534 return 0;
1535
1536out_err:
1537 macb_free_consistent(bp);
1538 return -ENOMEM;
1539}
1540
Nicolas Ferre4df95132013-06-04 21:57:12 +00001541static void gem_init_rings(struct macb *bp)
1542{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001543 struct macb_queue *queue;
1544 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001545 int i;
1546
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001547 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1548 for (i = 0; i < TX_RING_SIZE; i++) {
1549 queue->tx_ring[i].addr = 0;
1550 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1551 }
1552 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1553 queue->tx_head = 0;
1554 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001555 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001556
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001557 bp->rx_tail = 0;
1558 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001559
1560 gem_rx_refill(bp);
1561}
1562
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001563static void macb_init_rings(struct macb *bp)
1564{
1565 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001566
Cyrille Pitchen9ba723b2016-03-25 10:37:34 +01001567 macb_init_rx_ring(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001568
1569 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001570 bp->queues[0].tx_ring[i].addr = 0;
1571 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001572 }
Ben Shelton21d35152015-04-22 17:28:54 -05001573 bp->queues[0].tx_head = 0;
1574 bp->queues[0].tx_tail = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001575 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001576
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001577 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001578}
1579
1580static void macb_reset_hw(struct macb *bp)
1581{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001582 struct macb_queue *queue;
1583 unsigned int q;
1584
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001585 /* Disable RX and TX (XXX: Should we halt the transmission
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001586 * more gracefully?)
1587 */
1588 macb_writel(bp, NCR, 0);
1589
1590 /* Clear the stats registers (XXX: Update stats first?) */
1591 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1592
1593 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001594 macb_writel(bp, TSR, -1);
1595 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001596
1597 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001598 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1599 queue_writel(queue, IDR, -1);
1600 queue_readl(queue, ISR);
Nathan Sullivan24468372016-01-14 13:27:27 -06001601 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1602 queue_writel(queue, ISR, -1);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001603 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001604}
1605
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001606static u32 gem_mdc_clk_div(struct macb *bp)
1607{
1608 u32 config;
1609 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1610
1611 if (pclk_hz <= 20000000)
1612 config = GEM_BF(CLK, GEM_CLK_DIV8);
1613 else if (pclk_hz <= 40000000)
1614 config = GEM_BF(CLK, GEM_CLK_DIV16);
1615 else if (pclk_hz <= 80000000)
1616 config = GEM_BF(CLK, GEM_CLK_DIV32);
1617 else if (pclk_hz <= 120000000)
1618 config = GEM_BF(CLK, GEM_CLK_DIV48);
1619 else if (pclk_hz <= 160000000)
1620 config = GEM_BF(CLK, GEM_CLK_DIV64);
1621 else
1622 config = GEM_BF(CLK, GEM_CLK_DIV96);
1623
1624 return config;
1625}
1626
1627static u32 macb_mdc_clk_div(struct macb *bp)
1628{
1629 u32 config;
1630 unsigned long pclk_hz;
1631
1632 if (macb_is_gem(bp))
1633 return gem_mdc_clk_div(bp);
1634
1635 pclk_hz = clk_get_rate(bp->pclk);
1636 if (pclk_hz <= 20000000)
1637 config = MACB_BF(CLK, MACB_CLK_DIV8);
1638 else if (pclk_hz <= 40000000)
1639 config = MACB_BF(CLK, MACB_CLK_DIV16);
1640 else if (pclk_hz <= 80000000)
1641 config = MACB_BF(CLK, MACB_CLK_DIV32);
1642 else
1643 config = MACB_BF(CLK, MACB_CLK_DIV64);
1644
1645 return config;
1646}
1647
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001648/* Get the DMA bus width field of the network configuration register that we
Jamie Iles757a03c2011-03-09 16:29:59 +00001649 * should program. We find the width from decoding the design configuration
1650 * register to find the maximum supported data bus width.
1651 */
1652static u32 macb_dbw(struct macb *bp)
1653{
1654 if (!macb_is_gem(bp))
1655 return 0;
1656
1657 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1658 case 4:
1659 return GEM_BF(DBW, GEM_DBW128);
1660 case 2:
1661 return GEM_BF(DBW, GEM_DBW64);
1662 case 1:
1663 default:
1664 return GEM_BF(DBW, GEM_DBW32);
1665 }
1666}
1667
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001668/* Configure the receive DMA engine
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001669 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001670 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001671 * (if not supported by FIFO, it will fallback to default)
1672 * - set both rx/tx packet buffers to full memory size
1673 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001674 */
1675static void macb_configure_dma(struct macb *bp)
1676{
1677 u32 dmacfg;
1678
1679 if (macb_is_gem(bp)) {
1680 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001681 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001682 if (bp->dma_burst_length)
1683 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001684 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301685 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301686
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001687 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301688 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1689 else
1690 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1691
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001692 if (bp->dev->features & NETIF_F_HW_CSUM)
1693 dmacfg |= GEM_BIT(TXCOEN);
1694 else
1695 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001696 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1697 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001698 gem_writel(bp, DMACFG, dmacfg);
1699 }
1700}
1701
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001702static void macb_init_hw(struct macb *bp)
1703{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001704 struct macb_queue *queue;
1705 unsigned int q;
1706
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001707 u32 config;
1708
1709 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001710 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001711
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001712 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301713 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1714 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001715 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001716 config |= MACB_BIT(PAE); /* PAuse Enable */
1717 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001718 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301719 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1720 else
1721 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001722 if (bp->dev->flags & IFF_PROMISC)
1723 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001724 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1725 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001726 if (!(bp->dev->flags & IFF_BROADCAST))
1727 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001728 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001729 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001730 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301731 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001732 bp->speed = SPEED_10;
1733 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301734 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001735 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301736 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001737
Jamie Iles0116da42011-03-14 17:38:30 +00001738 macb_configure_dma(bp);
1739
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001740 /* Initialize TX and RX buffers */
1741 macb_writel(bp, RBQP, bp->rx_ring_dma);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001742 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1743 queue_writel(queue, TBQP, queue->tx_ring_dma);
1744
1745 /* Enable interrupts */
1746 queue_writel(queue, IER,
1747 MACB_RX_INT_FLAGS |
1748 MACB_TX_INT_FLAGS |
1749 MACB_BIT(HRESP));
1750 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001751
1752 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001753 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001754}
1755
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001756/* The hash address register is 64 bits long and takes up two
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001757 * locations in the memory map. The least significant bits are stored
1758 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1759 *
1760 * The unicast hash enable and the multicast hash enable bits in the
1761 * network configuration register enable the reception of hash matched
1762 * frames. The destination address is reduced to a 6 bit index into
1763 * the 64 bit hash register using the following hash function. The
1764 * hash function is an exclusive or of every sixth bit of the
1765 * destination address.
1766 *
1767 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1768 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1769 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1770 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1771 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1772 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1773 *
1774 * da[0] represents the least significant bit of the first byte
1775 * received, that is, the multicast/unicast indicator, and da[47]
1776 * represents the most significant bit of the last byte received. If
1777 * the hash index, hi[n], points to a bit that is set in the hash
1778 * register then the frame will be matched according to whether the
1779 * frame is multicast or unicast. A multicast match will be signalled
1780 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1781 * index points to a bit set in the hash register. A unicast match
1782 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1783 * and the hash index points to a bit set in the hash register. To
1784 * receive all multicast frames, the hash register should be set with
1785 * all ones and the multicast hash enable bit should be set in the
1786 * network configuration register.
1787 */
1788
1789static inline int hash_bit_value(int bitnr, __u8 *addr)
1790{
1791 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1792 return 1;
1793 return 0;
1794}
1795
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001796/* Return the hash index value for the specified address. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001797static int hash_get_index(__u8 *addr)
1798{
1799 int i, j, bitval;
1800 int hash_index = 0;
1801
1802 for (j = 0; j < 6; j++) {
1803 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001804 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001805
1806 hash_index |= (bitval << j);
1807 }
1808
1809 return hash_index;
1810}
1811
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001812/* Add multicast addresses to the internal multicast-hash table. */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001813static void macb_sethashtable(struct net_device *dev)
1814{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001815 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001816 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001817 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001818 struct macb *bp = netdev_priv(dev);
1819
1820 mc_filter[0] = mc_filter[1] = 0;
1821
Jiri Pirko22bedad32010-04-01 21:22:57 +00001822 netdev_for_each_mc_addr(ha, dev) {
1823 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001824 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1825 }
1826
Jamie Ilesf75ba502011-11-08 10:12:32 +00001827 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1828 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001829}
1830
Moritz Fischer64ec42f2016-03-29 19:11:12 -07001831/* Enable/Disable promiscuous and multicast modes. */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001832static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001833{
1834 unsigned long cfg;
1835 struct macb *bp = netdev_priv(dev);
1836
1837 cfg = macb_readl(bp, NCFGR);
1838
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001839 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001840 /* Enable promiscuous mode */
1841 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001842
1843 /* Disable RX checksum offload */
1844 if (macb_is_gem(bp))
1845 cfg &= ~GEM_BIT(RXCOEN);
1846 } else {
1847 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001848 cfg &= ~MACB_BIT(CAF);
1849
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001850 /* Enable RX checksum offload only if requested */
1851 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1852 cfg |= GEM_BIT(RXCOEN);
1853 }
1854
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001855 if (dev->flags & IFF_ALLMULTI) {
1856 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001857 macb_or_gem_writel(bp, HRB, -1);
1858 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001859 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001860 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001861 /* Enable specific multicasts */
1862 macb_sethashtable(dev);
1863 cfg |= MACB_BIT(NCFGR_MTI);
1864 } else if (dev->flags & (~IFF_ALLMULTI)) {
1865 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001866 macb_or_gem_writel(bp, HRB, 0);
1867 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001868 cfg &= ~MACB_BIT(NCFGR_MTI);
1869 }
1870
1871 macb_writel(bp, NCFGR, cfg);
1872}
1873
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001874static int macb_open(struct net_device *dev)
1875{
1876 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001877 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001878 int err;
1879
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001880 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001881
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001882 /* carrier starts down */
1883 netif_carrier_off(dev);
1884
frederic RODO6c36a702007-07-12 19:07:24 +02001885 /* if the phy is not yet register, retry later*/
1886 if (!bp->phy_dev)
1887 return -EAGAIN;
1888
Nicolas Ferre1b447912013-06-04 21:57:11 +00001889 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001890 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001891
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001892 err = macb_alloc_consistent(bp);
1893 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001894 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1895 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001896 return err;
1897 }
1898
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001899 napi_enable(&bp->napi);
1900
Nicolas Ferre4df95132013-06-04 21:57:12 +00001901 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001902 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001903
frederic RODO6c36a702007-07-12 19:07:24 +02001904 /* schedule a link state check */
1905 phy_start(bp->phy_dev);
1906
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001907 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001908
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001909 return 0;
1910}
1911
1912static int macb_close(struct net_device *dev)
1913{
1914 struct macb *bp = netdev_priv(dev);
1915 unsigned long flags;
1916
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001917 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001918 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001919
frederic RODO6c36a702007-07-12 19:07:24 +02001920 if (bp->phy_dev)
1921 phy_stop(bp->phy_dev);
1922
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001923 spin_lock_irqsave(&bp->lock, flags);
1924 macb_reset_hw(bp);
1925 netif_carrier_off(dev);
1926 spin_unlock_irqrestore(&bp->lock, flags);
1927
1928 macb_free_consistent(bp);
1929
1930 return 0;
1931}
1932
Harini Katakama5898ea2015-05-06 22:27:18 +05301933static int macb_change_mtu(struct net_device *dev, int new_mtu)
1934{
1935 struct macb *bp = netdev_priv(dev);
1936 u32 max_mtu;
1937
1938 if (netif_running(dev))
1939 return -EBUSY;
1940
1941 max_mtu = ETH_DATA_LEN;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001942 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakama5898ea2015-05-06 22:27:18 +05301943 max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
1944
1945 if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE))
1946 return -EINVAL;
1947
1948 dev->mtu = new_mtu;
1949
1950 return 0;
1951}
1952
Jamie Ilesa494ed82011-03-09 16:26:35 +00001953static void gem_update_stats(struct macb *bp)
1954{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03001955 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001956 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001957
Xander Huff3ff13f12015-01-13 16:15:51 -06001958 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1959 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07001960 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001961
1962 bp->ethtool_stats[i] += val;
1963 *p += val;
1964
1965 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1966 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07001967 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06001968 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06001969 *(++p) += val;
1970 }
1971 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00001972}
1973
1974static struct net_device_stats *gem_get_stats(struct macb *bp)
1975{
1976 struct gem_stats *hwstat = &bp->hw_stats.gem;
1977 struct net_device_stats *nstat = &bp->stats;
1978
1979 gem_update_stats(bp);
1980
1981 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1982 hwstat->rx_alignment_errors +
1983 hwstat->rx_resource_errors +
1984 hwstat->rx_overruns +
1985 hwstat->rx_oversize_frames +
1986 hwstat->rx_jabbers +
1987 hwstat->rx_undersized_frames +
1988 hwstat->rx_length_field_frame_errors);
1989 nstat->tx_errors = (hwstat->tx_late_collisions +
1990 hwstat->tx_excessive_collisions +
1991 hwstat->tx_underrun +
1992 hwstat->tx_carrier_sense_errors);
1993 nstat->multicast = hwstat->rx_multicast_frames;
1994 nstat->collisions = (hwstat->tx_single_collision_frames +
1995 hwstat->tx_multiple_collision_frames +
1996 hwstat->tx_excessive_collisions);
1997 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1998 hwstat->rx_jabbers +
1999 hwstat->rx_undersized_frames +
2000 hwstat->rx_length_field_frame_errors);
2001 nstat->rx_over_errors = hwstat->rx_resource_errors;
2002 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2003 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2004 nstat->rx_fifo_errors = hwstat->rx_overruns;
2005 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2006 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2007 nstat->tx_fifo_errors = hwstat->tx_underrun;
2008
2009 return nstat;
2010}
2011
Xander Huff3ff13f12015-01-13 16:15:51 -06002012static void gem_get_ethtool_stats(struct net_device *dev,
2013 struct ethtool_stats *stats, u64 *data)
2014{
2015 struct macb *bp;
2016
2017 bp = netdev_priv(dev);
2018 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002019 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002020}
2021
2022static int gem_get_sset_count(struct net_device *dev, int sset)
2023{
2024 switch (sset) {
2025 case ETH_SS_STATS:
2026 return GEM_STATS_LEN;
2027 default:
2028 return -EOPNOTSUPP;
2029 }
2030}
2031
2032static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2033{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002034 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002035
2036 switch (sset) {
2037 case ETH_SS_STATS:
2038 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2039 memcpy(p, gem_statistics[i].stat_string,
2040 ETH_GSTRING_LEN);
2041 break;
2042 }
2043}
2044
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002045static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002046{
2047 struct macb *bp = netdev_priv(dev);
2048 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002049 struct macb_stats *hwstat = &bp->hw_stats.macb;
2050
2051 if (macb_is_gem(bp))
2052 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002053
frederic RODO6c36a702007-07-12 19:07:24 +02002054 /* read stats from hardware */
2055 macb_update_stats(bp);
2056
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002057 /* Convert HW stats into netdevice stats */
2058 nstat->rx_errors = (hwstat->rx_fcs_errors +
2059 hwstat->rx_align_errors +
2060 hwstat->rx_resource_errors +
2061 hwstat->rx_overruns +
2062 hwstat->rx_oversize_pkts +
2063 hwstat->rx_jabbers +
2064 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002065 hwstat->rx_length_mismatch);
2066 nstat->tx_errors = (hwstat->tx_late_cols +
2067 hwstat->tx_excessive_cols +
2068 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002069 hwstat->tx_carrier_errors +
2070 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002071 nstat->collisions = (hwstat->tx_single_cols +
2072 hwstat->tx_multiple_cols +
2073 hwstat->tx_excessive_cols);
2074 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2075 hwstat->rx_jabbers +
2076 hwstat->rx_undersize_pkts +
2077 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002078 nstat->rx_over_errors = hwstat->rx_resource_errors +
2079 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002080 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2081 nstat->rx_frame_errors = hwstat->rx_align_errors;
2082 nstat->rx_fifo_errors = hwstat->rx_overruns;
2083 /* XXX: What does "missed" mean? */
2084 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2085 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2086 nstat->tx_fifo_errors = hwstat->tx_underruns;
2087 /* Don't know about heartbeat or window errors... */
2088
2089 return nstat;
2090}
2091
2092static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2093{
2094 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002095 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002096
frederic RODO6c36a702007-07-12 19:07:24 +02002097 if (!phydev)
2098 return -ENODEV;
2099
2100 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002101}
2102
2103static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2104{
2105 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002106 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002107
frederic RODO6c36a702007-07-12 19:07:24 +02002108 if (!phydev)
2109 return -ENODEV;
2110
2111 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002112}
2113
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002114static int macb_get_regs_len(struct net_device *netdev)
2115{
2116 return MACB_GREGS_NBR * sizeof(u32);
2117}
2118
2119static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2120 void *p)
2121{
2122 struct macb *bp = netdev_priv(dev);
2123 unsigned int tail, head;
2124 u32 *regs_buff = p;
2125
2126 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2127 | MACB_GREGS_VERSION;
2128
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002129 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2130 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002131
2132 regs_buff[0] = macb_readl(bp, NCR);
2133 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2134 regs_buff[2] = macb_readl(bp, NSR);
2135 regs_buff[3] = macb_readl(bp, TSR);
2136 regs_buff[4] = macb_readl(bp, RBQP);
2137 regs_buff[5] = macb_readl(bp, TBQP);
2138 regs_buff[6] = macb_readl(bp, RSR);
2139 regs_buff[7] = macb_readl(bp, IMR);
2140
2141 regs_buff[8] = tail;
2142 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002143 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2144 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002145
Neil Armstrongce721a72016-01-05 14:39:16 +01002146 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2147 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002148 if (macb_is_gem(bp))
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002149 regs_buff[13] = gem_readl(bp, DMACFG);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002150}
2151
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002152static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2153{
2154 struct macb *bp = netdev_priv(netdev);
2155
2156 wol->supported = 0;
2157 wol->wolopts = 0;
2158
2159 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2160 wol->supported = WAKE_MAGIC;
2161
2162 if (bp->wol & MACB_WOL_ENABLED)
2163 wol->wolopts |= WAKE_MAGIC;
2164 }
2165}
2166
2167static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2168{
2169 struct macb *bp = netdev_priv(netdev);
2170
2171 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2172 (wol->wolopts & ~WAKE_MAGIC))
2173 return -EOPNOTSUPP;
2174
2175 if (wol->wolopts & WAKE_MAGIC)
2176 bp->wol |= MACB_WOL_ENABLED;
2177 else
2178 bp->wol &= ~MACB_WOL_ENABLED;
2179
2180 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2181
2182 return 0;
2183}
2184
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002185static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002186 .get_settings = macb_get_settings,
2187 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002188 .get_regs_len = macb_get_regs_len,
2189 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002190 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002191 .get_ts_info = ethtool_op_get_ts_info,
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002192 .get_wol = macb_get_wol,
2193 .set_wol = macb_set_wol,
Xander Huff8cd5a562015-01-15 15:55:20 -06002194};
Xander Huff8cd5a562015-01-15 15:55:20 -06002195
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002196static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002197 .get_settings = macb_get_settings,
2198 .set_settings = macb_set_settings,
2199 .get_regs_len = macb_get_regs_len,
2200 .get_regs = macb_get_regs,
2201 .get_link = ethtool_op_get_link,
2202 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002203 .get_ethtool_stats = gem_get_ethtool_stats,
2204 .get_strings = gem_get_ethtool_strings,
2205 .get_sset_count = gem_get_sset_count,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002206};
2207
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002208static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002209{
2210 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002211 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002212
2213 if (!netif_running(dev))
2214 return -EINVAL;
2215
frederic RODO6c36a702007-07-12 19:07:24 +02002216 if (!phydev)
2217 return -ENODEV;
2218
Richard Cochran28b04112010-07-17 08:48:55 +00002219 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002220}
2221
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002222static int macb_set_features(struct net_device *netdev,
2223 netdev_features_t features)
2224{
2225 struct macb *bp = netdev_priv(netdev);
2226 netdev_features_t changed = features ^ netdev->features;
2227
2228 /* TX checksum offload */
2229 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2230 u32 dmacfg;
2231
2232 dmacfg = gem_readl(bp, DMACFG);
2233 if (features & NETIF_F_HW_CSUM)
2234 dmacfg |= GEM_BIT(TXCOEN);
2235 else
2236 dmacfg &= ~GEM_BIT(TXCOEN);
2237 gem_writel(bp, DMACFG, dmacfg);
2238 }
2239
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002240 /* RX checksum offload */
2241 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2242 u32 netcfg;
2243
2244 netcfg = gem_readl(bp, NCFGR);
2245 if (features & NETIF_F_RXCSUM &&
2246 !(netdev->flags & IFF_PROMISC))
2247 netcfg |= GEM_BIT(RXCOEN);
2248 else
2249 netcfg &= ~GEM_BIT(RXCOEN);
2250 gem_writel(bp, NCFGR, netcfg);
2251 }
2252
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002253 return 0;
2254}
2255
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002256static const struct net_device_ops macb_netdev_ops = {
2257 .ndo_open = macb_open,
2258 .ndo_stop = macb_close,
2259 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002260 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002261 .ndo_get_stats = macb_get_stats,
2262 .ndo_do_ioctl = macb_ioctl,
2263 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302264 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002265 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002266#ifdef CONFIG_NET_POLL_CONTROLLER
2267 .ndo_poll_controller = macb_poll_controller,
2268#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002269 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002270};
2271
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002272/* Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002273 * and integration options used
2274 */
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002275static void macb_configure_caps(struct macb *bp,
2276 const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002277{
2278 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002279
Nicolas Ferref6970502015-03-31 15:02:01 +02002280 if (dt_conf)
2281 bp->caps = dt_conf->caps;
2282
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002283 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002284 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2285
Nicolas Ferree1755872014-07-24 13:50:58 +02002286 dcfg = gem_readl(bp, DCFG1);
2287 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2288 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2289 dcfg = gem_readl(bp, DCFG2);
2290 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2291 bp->caps |= MACB_CAPS_FIFO_MODE;
2292 }
2293
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002294 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002295}
2296
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002297static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002298 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002299 unsigned int *queue_mask,
2300 unsigned int *num_queues)
2301{
2302 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002303
2304 *queue_mask = 0x1;
2305 *num_queues = 1;
2306
Nicolas Ferreda120112015-03-31 15:02:00 +02002307 /* is it macb or gem ?
2308 *
2309 * We need to read directly from the hardware here because
2310 * we are early in the probe process and don't have the
2311 * MACB_CAPS_MACB_IS_GEM flag positioned
2312 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002313 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002314 return;
2315
2316 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302317 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2318
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002319 *queue_mask |= 0x1;
2320
2321 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2322 if (*queue_mask & (1 << hw_q))
2323 (*num_queues)++;
2324}
2325
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002326static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
2327 struct clk **hclk, struct clk **tx_clk)
2328{
2329 int err;
2330
2331 *pclk = devm_clk_get(&pdev->dev, "pclk");
2332 if (IS_ERR(*pclk)) {
2333 err = PTR_ERR(*pclk);
2334 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2335 return err;
2336 }
2337
2338 *hclk = devm_clk_get(&pdev->dev, "hclk");
2339 if (IS_ERR(*hclk)) {
2340 err = PTR_ERR(*hclk);
2341 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2342 return err;
2343 }
2344
2345 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2346 if (IS_ERR(*tx_clk))
2347 *tx_clk = NULL;
2348
2349 err = clk_prepare_enable(*pclk);
2350 if (err) {
2351 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2352 return err;
2353 }
2354
2355 err = clk_prepare_enable(*hclk);
2356 if (err) {
2357 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2358 goto err_disable_pclk;
2359 }
2360
2361 err = clk_prepare_enable(*tx_clk);
2362 if (err) {
2363 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2364 goto err_disable_hclk;
2365 }
2366
2367 return 0;
2368
2369err_disable_hclk:
2370 clk_disable_unprepare(*hclk);
2371
2372err_disable_pclk:
2373 clk_disable_unprepare(*pclk);
2374
2375 return err;
2376}
2377
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002378static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002379{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002380 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002381 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002382 struct macb *bp = netdev_priv(dev);
2383 struct macb_queue *queue;
2384 int err;
2385 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002386
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002387 /* set the queue register mapping once for all: queue0 has a special
2388 * register mapping but we don't want to test the queue index then
2389 * compute the corresponding register offset at run time.
2390 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002391 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002392 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002393 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002394
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002395 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002396 queue->bp = bp;
2397 if (hw_q) {
2398 queue->ISR = GEM_ISR(hw_q - 1);
2399 queue->IER = GEM_IER(hw_q - 1);
2400 queue->IDR = GEM_IDR(hw_q - 1);
2401 queue->IMR = GEM_IMR(hw_q - 1);
2402 queue->TBQP = GEM_TBQP(hw_q - 1);
2403 } else {
2404 /* queue0 uses legacy registers */
2405 queue->ISR = MACB_ISR;
2406 queue->IER = MACB_IER;
2407 queue->IDR = MACB_IDR;
2408 queue->IMR = MACB_IMR;
2409 queue->TBQP = MACB_TBQP;
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002410 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002411
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002412 /* get irq: here we use the linux queue index, not the hardware
2413 * queue index. the queue irq definitions in the device tree
2414 * must remove the optional gaps that could exist in the
2415 * hardware queue mask.
2416 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002417 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002418 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002419 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002420 if (err) {
2421 dev_err(&pdev->dev,
2422 "Unable to request IRQ %d (error %d)\n",
2423 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002424 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002425 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002426
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002427 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002428 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002429 }
2430
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002431 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002432 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002433
Nicolas Ferre4df95132013-06-04 21:57:12 +00002434 /* setup appropriated routines according to adapter type */
2435 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002436 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002437 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2438 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2439 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2440 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002441 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002442 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002443 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002444 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2445 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2446 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2447 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002448 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002449 }
2450
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002451 /* Set features */
2452 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002453 /* Checksum offload is only available on gem with packet buffer */
2454 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002455 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002456 if (bp->caps & MACB_CAPS_SG_DISABLED)
2457 dev->hw_features &= ~NETIF_F_SG;
2458 dev->features = dev->hw_features;
2459
Neil Armstrongce721a72016-01-05 14:39:16 +01002460 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2461 val = 0;
2462 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2463 val = GEM_BIT(RGMII);
2464 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002465 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002466 val = MACB_BIT(RMII);
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002467 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
Neil Armstrongce721a72016-01-05 14:39:16 +01002468 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002469
Neil Armstrongce721a72016-01-05 14:39:16 +01002470 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2471 val |= MACB_BIT(CLKEN);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002472
Neil Armstrongce721a72016-01-05 14:39:16 +01002473 macb_or_gem_writel(bp, USRIO, val);
2474 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002475
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002476 /* Set MII management clock divider */
2477 val = macb_mdc_clk_div(bp);
2478 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302479 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2480 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002481 macb_writel(bp, NCFGR, val);
2482
2483 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002484}
2485
2486#if defined(CONFIG_OF)
2487/* 1518 rounded up */
2488#define AT91ETHER_MAX_RBUFF_SZ 0x600
2489/* max number of receive buffers */
2490#define AT91ETHER_MAX_RX_DESCR 9
2491
2492/* Initialize and start the Receiver and Transmit subsystems */
2493static int at91ether_start(struct net_device *dev)
2494{
2495 struct macb *lp = netdev_priv(dev);
2496 dma_addr_t addr;
2497 u32 ctl;
2498 int i;
2499
2500 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2501 (AT91ETHER_MAX_RX_DESCR *
2502 sizeof(struct macb_dma_desc)),
2503 &lp->rx_ring_dma, GFP_KERNEL);
2504 if (!lp->rx_ring)
2505 return -ENOMEM;
2506
2507 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2508 AT91ETHER_MAX_RX_DESCR *
2509 AT91ETHER_MAX_RBUFF_SZ,
2510 &lp->rx_buffers_dma, GFP_KERNEL);
2511 if (!lp->rx_buffers) {
2512 dma_free_coherent(&lp->pdev->dev,
2513 AT91ETHER_MAX_RX_DESCR *
2514 sizeof(struct macb_dma_desc),
2515 lp->rx_ring, lp->rx_ring_dma);
2516 lp->rx_ring = NULL;
2517 return -ENOMEM;
2518 }
2519
2520 addr = lp->rx_buffers_dma;
2521 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2522 lp->rx_ring[i].addr = addr;
2523 lp->rx_ring[i].ctrl = 0;
2524 addr += AT91ETHER_MAX_RBUFF_SZ;
2525 }
2526
2527 /* Set the Wrap bit on the last descriptor */
2528 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2529
2530 /* Reset buffer index */
2531 lp->rx_tail = 0;
2532
2533 /* Program address of descriptor list in Rx Buffer Queue register */
2534 macb_writel(lp, RBQP, lp->rx_ring_dma);
2535
2536 /* Enable Receive and Transmit */
2537 ctl = macb_readl(lp, NCR);
2538 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2539
2540 return 0;
2541}
2542
2543/* Open the ethernet interface */
2544static int at91ether_open(struct net_device *dev)
2545{
2546 struct macb *lp = netdev_priv(dev);
2547 u32 ctl;
2548 int ret;
2549
2550 /* Clear internal statistics */
2551 ctl = macb_readl(lp, NCR);
2552 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2553
2554 macb_set_hwaddr(lp);
2555
2556 ret = at91ether_start(dev);
2557 if (ret)
2558 return ret;
2559
2560 /* Enable MAC interrupts */
2561 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2562 MACB_BIT(RXUBR) |
2563 MACB_BIT(ISR_TUND) |
2564 MACB_BIT(ISR_RLE) |
2565 MACB_BIT(TCOMP) |
2566 MACB_BIT(ISR_ROVR) |
2567 MACB_BIT(HRESP));
2568
2569 /* schedule a link state check */
2570 phy_start(lp->phy_dev);
2571
2572 netif_start_queue(dev);
2573
2574 return 0;
2575}
2576
2577/* Close the interface */
2578static int at91ether_close(struct net_device *dev)
2579{
2580 struct macb *lp = netdev_priv(dev);
2581 u32 ctl;
2582
2583 /* Disable Receiver and Transmitter */
2584 ctl = macb_readl(lp, NCR);
2585 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2586
2587 /* Disable MAC interrupts */
2588 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2589 MACB_BIT(RXUBR) |
2590 MACB_BIT(ISR_TUND) |
2591 MACB_BIT(ISR_RLE) |
2592 MACB_BIT(TCOMP) |
2593 MACB_BIT(ISR_ROVR) |
2594 MACB_BIT(HRESP));
2595
2596 netif_stop_queue(dev);
2597
2598 dma_free_coherent(&lp->pdev->dev,
2599 AT91ETHER_MAX_RX_DESCR *
2600 sizeof(struct macb_dma_desc),
2601 lp->rx_ring, lp->rx_ring_dma);
2602 lp->rx_ring = NULL;
2603
2604 dma_free_coherent(&lp->pdev->dev,
2605 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2606 lp->rx_buffers, lp->rx_buffers_dma);
2607 lp->rx_buffers = NULL;
2608
2609 return 0;
2610}
2611
2612/* Transmit packet */
2613static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2614{
2615 struct macb *lp = netdev_priv(dev);
2616
2617 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2618 netif_stop_queue(dev);
2619
2620 /* Store packet information (to free when Tx completed) */
2621 lp->skb = skb;
2622 lp->skb_length = skb->len;
2623 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2624 DMA_TO_DEVICE);
2625
2626 /* Set address of the data in the Transmit Address register */
2627 macb_writel(lp, TAR, lp->skb_physaddr);
2628 /* Set length of the packet in the Transmit Control register */
2629 macb_writel(lp, TCR, skb->len);
2630
2631 } else {
2632 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2633 return NETDEV_TX_BUSY;
2634 }
2635
2636 return NETDEV_TX_OK;
2637}
2638
2639/* Extract received frame from buffer descriptors and sent to upper layers.
2640 * (Called from interrupt context)
2641 */
2642static void at91ether_rx(struct net_device *dev)
2643{
2644 struct macb *lp = netdev_priv(dev);
2645 unsigned char *p_recv;
2646 struct sk_buff *skb;
2647 unsigned int pktlen;
2648
2649 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2650 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2651 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2652 skb = netdev_alloc_skb(dev, pktlen + 2);
2653 if (skb) {
2654 skb_reserve(skb, 2);
2655 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2656
2657 skb->protocol = eth_type_trans(skb, dev);
2658 lp->stats.rx_packets++;
2659 lp->stats.rx_bytes += pktlen;
2660 netif_rx(skb);
2661 } else {
2662 lp->stats.rx_dropped++;
2663 }
2664
2665 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2666 lp->stats.multicast++;
2667
2668 /* reset ownership bit */
2669 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2670
2671 /* wrap after last buffer */
2672 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2673 lp->rx_tail = 0;
2674 else
2675 lp->rx_tail++;
2676 }
2677}
2678
2679/* MAC interrupt handler */
2680static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2681{
2682 struct net_device *dev = dev_id;
2683 struct macb *lp = netdev_priv(dev);
2684 u32 intstatus, ctl;
2685
2686 /* MAC Interrupt Status register indicates what interrupts are pending.
2687 * It is automatically cleared once read.
2688 */
2689 intstatus = macb_readl(lp, ISR);
2690
2691 /* Receive complete */
2692 if (intstatus & MACB_BIT(RCOMP))
2693 at91ether_rx(dev);
2694
2695 /* Transmit complete */
2696 if (intstatus & MACB_BIT(TCOMP)) {
2697 /* The TCOM bit is set even if the transmission failed */
2698 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2699 lp->stats.tx_errors++;
2700
2701 if (lp->skb) {
2702 dev_kfree_skb_irq(lp->skb);
2703 lp->skb = NULL;
2704 dma_unmap_single(NULL, lp->skb_physaddr,
2705 lp->skb_length, DMA_TO_DEVICE);
2706 lp->stats.tx_packets++;
2707 lp->stats.tx_bytes += lp->skb_length;
2708 }
2709 netif_wake_queue(dev);
2710 }
2711
2712 /* Work-around for EMAC Errata section 41.3.1 */
2713 if (intstatus & MACB_BIT(RXUBR)) {
2714 ctl = macb_readl(lp, NCR);
2715 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2716 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2717 }
2718
2719 if (intstatus & MACB_BIT(ISR_ROVR))
2720 netdev_err(dev, "ROVR error\n");
2721
2722 return IRQ_HANDLED;
2723}
2724
2725#ifdef CONFIG_NET_POLL_CONTROLLER
2726static void at91ether_poll_controller(struct net_device *dev)
2727{
2728 unsigned long flags;
2729
2730 local_irq_save(flags);
2731 at91ether_interrupt(dev->irq, dev);
2732 local_irq_restore(flags);
2733}
2734#endif
2735
2736static const struct net_device_ops at91ether_netdev_ops = {
2737 .ndo_open = at91ether_open,
2738 .ndo_stop = at91ether_close,
2739 .ndo_start_xmit = at91ether_start_xmit,
2740 .ndo_get_stats = macb_get_stats,
2741 .ndo_set_rx_mode = macb_set_rx_mode,
2742 .ndo_set_mac_address = eth_mac_addr,
2743 .ndo_do_ioctl = macb_ioctl,
2744 .ndo_validate_addr = eth_validate_addr,
2745 .ndo_change_mtu = eth_change_mtu,
2746#ifdef CONFIG_NET_POLL_CONTROLLER
2747 .ndo_poll_controller = at91ether_poll_controller,
2748#endif
2749};
2750
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002751static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
2752 struct clk **hclk, struct clk **tx_clk)
2753{
2754 int err;
2755
2756 *hclk = NULL;
2757 *tx_clk = NULL;
2758
2759 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2760 if (IS_ERR(*pclk))
2761 return PTR_ERR(*pclk);
2762
2763 err = clk_prepare_enable(*pclk);
2764 if (err) {
2765 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2766 return err;
2767 }
2768
2769 return 0;
2770}
2771
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002772static int at91ether_init(struct platform_device *pdev)
2773{
2774 struct net_device *dev = platform_get_drvdata(pdev);
2775 struct macb *bp = netdev_priv(dev);
2776 int err;
2777 u32 reg;
2778
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002779 dev->netdev_ops = &at91ether_netdev_ops;
2780 dev->ethtool_ops = &macb_ethtool_ops;
2781
2782 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2783 0, dev->name, dev);
2784 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002785 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002786
2787 macb_writel(bp, NCR, 0);
2788
2789 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2790 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2791 reg |= MACB_BIT(RM9200_RMII);
2792
2793 macb_writel(bp, NCFGR, reg);
2794
2795 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002796}
2797
David S. Miller3cef5c52015-03-09 23:38:02 -04002798static const struct macb_config at91sam9260_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002799 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002800 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002801 .init = macb_init,
2802};
2803
David S. Miller3cef5c52015-03-09 23:38:02 -04002804static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002805 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2806 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002807 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002808 .init = macb_init,
2809};
2810
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002811static const struct macb_config sama5d2_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002812 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002813 .dma_burst_length = 16,
2814 .clk_init = macb_clk_init,
2815 .init = macb_init,
2816};
2817
David S. Miller3cef5c52015-03-09 23:38:02 -04002818static const struct macb_config sama5d3_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002819 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
2820 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002821 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002822 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002823 .init = macb_init,
2824};
2825
David S. Miller3cef5c52015-03-09 23:38:02 -04002826static const struct macb_config sama5d4_config = {
Nicolas Ferre6bdaa5e2016-03-10 16:44:32 +01002827 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002828 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002829 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002830 .init = macb_init,
2831};
2832
David S. Miller3cef5c52015-03-09 23:38:02 -04002833static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002834 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002835 .init = at91ether_init,
2836};
2837
Neil Armstronge611b5b2016-01-05 14:39:17 +01002838static const struct macb_config np4_config = {
2839 .caps = MACB_CAPS_USRIO_DISABLED,
2840 .clk_init = macb_clk_init,
2841 .init = macb_init,
2842};
David S. Miller36583eb2015-05-23 01:22:35 -04002843
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302844static const struct macb_config zynqmp_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302845 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302846 .dma_burst_length = 16,
2847 .clk_init = macb_clk_init,
2848 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302849 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302850};
2851
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002852static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302853 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002854 .dma_burst_length = 16,
2855 .clk_init = macb_clk_init,
2856 .init = macb_init,
2857};
2858
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002859static const struct of_device_id macb_dt_ids[] = {
2860 { .compatible = "cdns,at32ap7000-macb" },
2861 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2862 { .compatible = "cdns,macb" },
Neil Armstronge611b5b2016-01-05 14:39:17 +01002863 { .compatible = "cdns,np4-macb", .data = &np4_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002864 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2865 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002866 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002867 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2868 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2869 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2870 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302871 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002872 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002873 { /* sentinel */ }
2874};
2875MODULE_DEVICE_TABLE(of, macb_dt_ids);
2876#endif /* CONFIG_OF */
2877
2878static int macb_probe(struct platform_device *pdev)
2879{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002880 int (*clk_init)(struct platform_device *, struct clk **,
2881 struct clk **, struct clk **)
2882 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002883 int (*init)(struct platform_device *) = macb_init;
2884 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002885 struct device_node *phy_node;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002886 const struct macb_config *macb_config = NULL;
Sudip Mukherjee36df7452016-01-25 11:43:09 +05302887 struct clk *pclk, *hclk = NULL, *tx_clk = NULL;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002888 unsigned int queue_mask, num_queues;
2889 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002890 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002891 struct phy_device *phydev;
2892 struct net_device *dev;
2893 struct resource *regs;
2894 void __iomem *mem;
2895 const char *mac;
2896 struct macb *bp;
2897 int err;
2898
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002899 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2900 mem = devm_ioremap_resource(&pdev->dev, regs);
2901 if (IS_ERR(mem))
2902 return PTR_ERR(mem);
2903
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002904 if (np) {
2905 const struct of_device_id *match;
2906
2907 match = of_match_node(macb_dt_ids, np);
2908 if (match && match->data) {
2909 macb_config = match->data;
2910 clk_init = macb_config->clk_init;
2911 init = macb_config->init;
2912 }
2913 }
2914
2915 err = clk_init(pdev, &pclk, &hclk, &tx_clk);
2916 if (err)
2917 return err;
2918
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002919 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002920
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002921 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002922 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002923 if (!dev) {
2924 err = -ENOMEM;
2925 goto err_disable_clocks;
2926 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002927
2928 dev->base_addr = regs->start;
2929
2930 SET_NETDEV_DEV(dev, &pdev->dev);
2931
2932 bp = netdev_priv(dev);
2933 bp->pdev = pdev;
2934 bp->dev = dev;
2935 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002936 bp->native_io = native_io;
2937 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07002938 bp->macb_reg_readl = hw_readl_native;
2939 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002940 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07002941 bp->macb_reg_readl = hw_readl;
2942 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002943 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002944 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002945 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002946 if (macb_config)
2947 bp->dma_burst_length = macb_config->dma_burst_length;
2948 bp->pclk = pclk;
2949 bp->hclk = hclk;
2950 bp->tx_clk = tx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03002951 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302952 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302953
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002954 bp->wol = 0;
Sergio Prado7c4a1d02016-02-16 21:10:45 -02002955 if (of_get_property(np, "magic-packet", NULL))
Sergio Prado3e2a5e12016-02-09 12:07:16 -02002956 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
2957 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
2958
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002959 spin_lock_init(&bp->lock);
2960
Nicolas Ferread783472015-03-31 15:02:02 +02002961 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02002962 macb_configure_caps(bp, macb_config);
2963
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002964 platform_set_drvdata(pdev, dev);
2965
2966 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002967 if (dev->irq < 0) {
2968 err = dev->irq;
2969 goto err_disable_clocks;
2970 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002971
2972 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00002973 if (mac)
2974 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2975 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002976 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002977
Gregory CLEMENT5833e052015-12-11 11:34:53 +01002978 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002979 phy_node = of_get_next_available_child(np, NULL);
2980 if (phy_node) {
2981 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
Moritz Fischer64ec42f2016-03-29 19:11:12 -07002982
Charles Keepax0e3e7992016-03-28 13:47:42 +01002983 if (gpio_is_valid(gpio)) {
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002984 bp->reset_gpio = gpio_to_desc(gpio);
Charles Keepax0e3e7992016-03-28 13:47:42 +01002985 gpiod_direction_output(bp->reset_gpio, 1);
2986 }
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002987 }
2988 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01002989
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002990 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002991 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002992 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002993 if (pdata && pdata->is_rmii)
2994 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2995 else
2996 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2997 } else {
2998 bp->phy_interface = err;
2999 }
3000
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003001 /* IP specific init */
3002 err = init(pdev);
3003 if (err)
3004 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003005
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003006 err = register_netdev(dev);
3007 if (err) {
3008 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003009 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003010 }
3011
Nicolas Ferre72ca8202013-04-14 22:04:33 +00003012 err = macb_mii_init(bp);
3013 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02003014 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003015
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003016 netif_carrier_off(dev);
3017
Bo Shen58798232014-09-13 01:57:49 +02003018 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3019 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3020 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003021
frederic RODO6c36a702007-07-12 19:07:24 +02003022 phydev = bp->phy_dev;
Andrew Lunn22209432016-01-06 20:11:13 +01003023 phy_attached_info(phydev);
frederic RODO6c36a702007-07-12 19:07:24 +02003024
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003025 return 0;
3026
frederic RODO6c36a702007-07-12 19:07:24 +02003027err_out_unregister_netdev:
3028 unregister_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003029
Cyrille Pitchencf250de2014-12-15 15:13:32 +01003030err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003031 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01003032
Nicolas Ferrec69618b2015-03-31 15:02:03 +02003033err_disable_clocks:
3034 clk_disable_unprepare(tx_clk);
3035 clk_disable_unprepare(hclk);
3036 clk_disable_unprepare(pclk);
3037
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003038 return err;
3039}
3040
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003041static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003042{
3043 struct net_device *dev;
3044 struct macb *bp;
3045
3046 dev = platform_get_drvdata(pdev);
3047
3048 if (dev) {
3049 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09003050 if (bp->phy_dev)
3051 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003052 mdiobus_unregister(bp->mii_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003053 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003054
3055 /* Shutdown the PHY if there is a GPIO reset */
Charles Keepax0e3e7992016-03-28 13:47:42 +01003056 if (bp->reset_gpio)
3057 gpiod_set_value(bp->reset_gpio, 0);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01003058
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003059 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003060 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003061 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003062 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01003063 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003064 }
3065
3066 return 0;
3067}
3068
Michal Simekd23823d2015-01-23 09:36:03 +01003069static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003070{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003071 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003072 struct net_device *netdev = platform_get_drvdata(pdev);
3073 struct macb *bp = netdev_priv(netdev);
3074
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003075 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003076 netif_device_detach(netdev);
3077
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003078 if (bp->wol & MACB_WOL_ENABLED) {
3079 macb_writel(bp, IER, MACB_BIT(WOL));
3080 macb_writel(bp, WOL, MACB_BIT(MAG));
3081 enable_irq_wake(bp->queues[0].irq);
3082 } else {
3083 clk_disable_unprepare(bp->tx_clk);
3084 clk_disable_unprepare(bp->hclk);
3085 clk_disable_unprepare(bp->pclk);
3086 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003087
3088 return 0;
3089}
3090
Michal Simekd23823d2015-01-23 09:36:03 +01003091static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003092{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003093 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003094 struct net_device *netdev = platform_get_drvdata(pdev);
3095 struct macb *bp = netdev_priv(netdev);
3096
Sergio Prado3e2a5e12016-02-09 12:07:16 -02003097 if (bp->wol & MACB_WOL_ENABLED) {
3098 macb_writel(bp, IDR, MACB_BIT(WOL));
3099 macb_writel(bp, WOL, 0);
3100 disable_irq_wake(bp->queues[0].irq);
3101 } else {
3102 clk_prepare_enable(bp->pclk);
3103 clk_prepare_enable(bp->hclk);
3104 clk_prepare_enable(bp->tx_clk);
3105 }
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003106
3107 netif_device_attach(netdev);
3108
3109 return 0;
3110}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003111
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003112static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3113
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003114static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003115 .probe = macb_probe,
3116 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003117 .driver = {
3118 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003119 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003120 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003121 },
3122};
3123
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003124module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003125
3126MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003127MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003128MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003129MODULE_ALIAS("platform:macb");