blob: 77a5270ccae5fff98568830952836aa1ad8f225b [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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000026#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020028#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080029#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010030#include <linux/of_device.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010033
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010034#include "macb.h"
35
Nicolas Ferre1b447912013-06-04 21:57:11 +000036#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000037#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000038#define RX_RING_SIZE 512 /* must be power of 2 */
39#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010040
Havard Skinnemoen55054a12012-10-31 06:04:55 +000041#define TX_RING_SIZE 128 /* must be power of 2 */
42#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010043
Nicolas Ferre909a8582012-11-19 06:00:21 +000044/* level of occupied TX descriptors under which we wake up TX process */
45#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010046
47#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
48 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000049#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
50 | MACB_BIT(ISR_RLE) \
51 | MACB_BIT(TXERR))
52#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
53
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020054#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
55#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
56
Harini Katakama5898ea2015-05-06 22:27:18 +053057#define GEM_MTU_MIN_SIZE 68
58
Nicolas Ferree86cd532012-10-31 06:04:57 +000059/*
60 * Graceful stop timeouts in us. We should allow up to
61 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
62 */
63#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010064
Havard Skinnemoen55054a12012-10-31 06:04:55 +000065/* Ring buffer accessors */
66static unsigned int macb_tx_ring_wrap(unsigned int index)
67{
68 return index & (TX_RING_SIZE - 1);
69}
70
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010071static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
72 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000073{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010074 return &queue->tx_ring[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000075}
76
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010077static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
78 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000079{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010080 return &queue->tx_skb[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000081}
82
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010083static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000084{
85 dma_addr_t offset;
86
87 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
88
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010089 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +000090}
91
92static unsigned int macb_rx_ring_wrap(unsigned int index)
93{
94 return index & (RX_RING_SIZE - 1);
95}
96
97static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
98{
99 return &bp->rx_ring[macb_rx_ring_wrap(index)];
100}
101
102static void *macb_rx_buffer(struct macb *bp, unsigned int index)
103{
Nicolas Ferre1b447912013-06-04 21:57:11 +0000104 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000105}
106
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300107/* I/O accessors */
108static u32 hw_readl_native(struct macb *bp, int offset)
109{
110 return __raw_readl(bp->regs + offset);
111}
112
113static void hw_writel_native(struct macb *bp, int offset, u32 value)
114{
115 __raw_writel(value, bp->regs + offset);
116}
117
118static u32 hw_readl(struct macb *bp, int offset)
119{
120 return readl_relaxed(bp->regs + offset);
121}
122
123static void hw_writel(struct macb *bp, int offset, u32 value)
124{
125 writel_relaxed(value, bp->regs + offset);
126}
127
128/*
129 * Find the CPU endianness by using the loopback bit of NCR register. When the
130 * CPU is in big endian we need to program swaped mode for management
131 * descriptor access.
132 */
133static bool hw_is_native_io(void __iomem *addr)
134{
135 u32 value = MACB_BIT(LLB);
136
137 __raw_writel(value, addr + MACB_NCR);
138 value = __raw_readl(addr + MACB_NCR);
139
140 /* Write 0 back to disable everything */
141 __raw_writel(0, addr + MACB_NCR);
142
143 return value == MACB_BIT(LLB);
144}
145
146static bool hw_is_gem(void __iomem *addr, bool native_io)
147{
148 u32 id;
149
150 if (native_io)
151 id = __raw_readl(addr + MACB_MID);
152 else
153 id = readl_relaxed(addr + MACB_MID);
154
155 return MACB_BFEXT(IDNUM, id) >= 0x2;
156}
157
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100158static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100159{
160 u32 bottom;
161 u16 top;
162
163 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000164 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100165 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000166 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000167
168 /* Clear unused address register sets */
169 macb_or_gem_writel(bp, SA2B, 0);
170 macb_or_gem_writel(bp, SA2T, 0);
171 macb_or_gem_writel(bp, SA3B, 0);
172 macb_or_gem_writel(bp, SA3T, 0);
173 macb_or_gem_writel(bp, SA4B, 0);
174 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100175}
176
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100177static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100178{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000179 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100180 u32 bottom;
181 u16 top;
182 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000183 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100184
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900185 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000186
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000187 /* Check all 4 address register for vaild address */
188 for (i = 0; i < 4; i++) {
189 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
190 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100191
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000192 if (pdata && pdata->rev_eth_addr) {
193 addr[5] = bottom & 0xff;
194 addr[4] = (bottom >> 8) & 0xff;
195 addr[3] = (bottom >> 16) & 0xff;
196 addr[2] = (bottom >> 24) & 0xff;
197 addr[1] = top & 0xff;
198 addr[0] = (top & 0xff00) >> 8;
199 } else {
200 addr[0] = bottom & 0xff;
201 addr[1] = (bottom >> 8) & 0xff;
202 addr[2] = (bottom >> 16) & 0xff;
203 addr[3] = (bottom >> 24) & 0xff;
204 addr[4] = top & 0xff;
205 addr[5] = (top >> 8) & 0xff;
206 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100207
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000208 if (is_valid_ether_addr(addr)) {
209 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
210 return;
211 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700212 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000213
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300214 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000215 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100216}
217
frederic RODO6c36a702007-07-12 19:07:24 +0200218static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100219{
frederic RODO6c36a702007-07-12 19:07:24 +0200220 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100221 int value;
222
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100223 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
224 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200225 | MACB_BF(PHYA, mii_id)
226 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100227 | MACB_BF(CODE, MACB_MAN_CODE)));
228
frederic RODO6c36a702007-07-12 19:07:24 +0200229 /* wait for end of transfer */
230 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
231 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100232
233 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100234
235 return value;
236}
237
frederic RODO6c36a702007-07-12 19:07:24 +0200238static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
239 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100240{
frederic RODO6c36a702007-07-12 19:07:24 +0200241 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100242
243 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
244 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200245 | MACB_BF(PHYA, mii_id)
246 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100247 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200248 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100249
frederic RODO6c36a702007-07-12 19:07:24 +0200250 /* wait for end of transfer */
251 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
252 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100253
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100254 return 0;
255}
256
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800257/**
258 * macb_set_tx_clk() - Set a clock to a new frequency
259 * @clk Pointer to the clock to change
260 * @rate New frequency in Hz
261 * @dev Pointer to the struct net_device
262 */
263static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
264{
265 long ferr, rate, rate_rounded;
266
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100267 if (!clk)
268 return;
269
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800270 switch (speed) {
271 case SPEED_10:
272 rate = 2500000;
273 break;
274 case SPEED_100:
275 rate = 25000000;
276 break;
277 case SPEED_1000:
278 rate = 125000000;
279 break;
280 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800281 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800282 }
283
284 rate_rounded = clk_round_rate(clk, rate);
285 if (rate_rounded < 0)
286 return;
287
288 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
289 * is not satisfied.
290 */
291 ferr = abs(rate_rounded - rate);
292 ferr = DIV_ROUND_UP(ferr, rate / 100000);
293 if (ferr > 5)
294 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
295 rate);
296
297 if (clk_set_rate(clk, rate_rounded))
298 netdev_err(dev, "adjusting tx_clk failed.\n");
299}
300
frederic RODO6c36a702007-07-12 19:07:24 +0200301static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100302{
frederic RODO6c36a702007-07-12 19:07:24 +0200303 struct macb *bp = netdev_priv(dev);
304 struct phy_device *phydev = bp->phy_dev;
305 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200306 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100307
frederic RODO6c36a702007-07-12 19:07:24 +0200308 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100309
frederic RODO6c36a702007-07-12 19:07:24 +0200310 if (phydev->link) {
311 if ((bp->speed != phydev->speed) ||
312 (bp->duplex != phydev->duplex)) {
313 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100314
frederic RODO6c36a702007-07-12 19:07:24 +0200315 reg = macb_readl(bp, NCFGR);
316 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000317 if (macb_is_gem(bp))
318 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200319
320 if (phydev->duplex)
321 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900322 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200323 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200324 if (phydev->speed == SPEED_1000 &&
325 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000326 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200327
Patrice Vilchez140b7552012-10-31 06:04:50 +0000328 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200329
330 bp->speed = phydev->speed;
331 bp->duplex = phydev->duplex;
332 status_change = 1;
333 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100334 }
335
frederic RODO6c36a702007-07-12 19:07:24 +0200336 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700337 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200338 bp->speed = 0;
339 bp->duplex = -1;
340 }
341 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100342
frederic RODO6c36a702007-07-12 19:07:24 +0200343 status_change = 1;
344 }
345
346 spin_unlock_irqrestore(&bp->lock, flags);
347
348 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000349 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500350 /* Update the TX clock rate if and only if the link is
351 * up and there has been a link change.
352 */
353 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
354
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000355 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000356 netdev_info(dev, "link up (%d/%s)\n",
357 phydev->speed,
358 phydev->duplex == DUPLEX_FULL ?
359 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000360 } else {
361 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000362 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000363 }
frederic RODO6c36a702007-07-12 19:07:24 +0200364 }
365}
366
367/* based on au1000_eth. c*/
368static int macb_mii_probe(struct net_device *dev)
369{
370 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000371 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000372 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000373 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000374 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200375
Jiri Pirko7455a762010-02-08 05:12:08 +0000376 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200377 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000378 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200379 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200380 }
381
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000382 pdata = dev_get_platdata(&bp->pdev->dev);
383 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
384 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
385 if (!ret) {
386 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
387 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
388 }
389 }
frederic RODO6c36a702007-07-12 19:07:24 +0200390
391 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000392 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100393 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000394 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000395 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000396 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200397 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100398
frederic RODO6c36a702007-07-12 19:07:24 +0200399 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200400 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000401 phydev->supported &= PHY_GBIT_FEATURES;
402 else
403 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100404
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500405 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
406 phydev->supported &= ~SUPPORTED_1000baseT_Half;
407
frederic RODO6c36a702007-07-12 19:07:24 +0200408 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100409
frederic RODO6c36a702007-07-12 19:07:24 +0200410 bp->link = 0;
411 bp->speed = 0;
412 bp->duplex = -1;
413 bp->phy_dev = phydev;
414
415 return 0;
416}
417
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100418static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200419{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000420 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200421 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200422 int err = -ENXIO, i;
423
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200424 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200425 macb_writel(bp, NCR, MACB_BIT(MPE));
426
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700427 bp->mii_bus = mdiobus_alloc();
428 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200429 err = -ENOMEM;
430 goto err_out;
431 }
432
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700433 bp->mii_bus->name = "MACB_mii_bus";
434 bp->mii_bus->read = &macb_mdio_read;
435 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000436 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
437 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700438 bp->mii_bus->priv = bp;
439 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900440 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700441
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700442 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
443 if (!bp->mii_bus->irq) {
444 err = -ENOMEM;
445 goto err_out_free_mdiobus;
446 }
447
Jamie Iles91523942011-02-28 04:05:25 +0000448 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200449
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200450 np = bp->pdev->dev.of_node;
451 if (np) {
452 /* try dt phy registration */
453 err = of_mdiobus_register(bp->mii_bus, np);
454
455 /* fallback to standard phy registration if no phy were
456 found during dt phy registration */
457 if (!err && !phy_find_first(bp->mii_bus)) {
458 for (i = 0; i < PHY_MAX_ADDR; i++) {
459 struct phy_device *phydev;
460
461 phydev = mdiobus_scan(bp->mii_bus, i);
462 if (IS_ERR(phydev)) {
463 err = PTR_ERR(phydev);
464 break;
465 }
466 }
467
468 if (err)
469 goto err_out_unregister_bus;
470 }
471 } else {
472 for (i = 0; i < PHY_MAX_ADDR; i++)
473 bp->mii_bus->irq[i] = PHY_POLL;
474
475 if (pdata)
476 bp->mii_bus->phy_mask = pdata->phy_mask;
477
478 err = mdiobus_register(bp->mii_bus);
479 }
480
481 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200482 goto err_out_free_mdio_irq;
483
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200484 err = macb_mii_probe(bp->dev);
485 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200486 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200487
488 return 0;
489
490err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700491 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200492err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700493 kfree(bp->mii_bus->irq);
494err_out_free_mdiobus:
495 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200496err_out:
497 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100498}
499
500static void macb_update_stats(struct macb *bp)
501{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000502 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
503 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300504 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100505
506 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
507
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300508 for(; p < end; p++, offset += 4)
509 *p += bp->readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100510}
511
Nicolas Ferree86cd532012-10-31 06:04:57 +0000512static int macb_halt_tx(struct macb *bp)
513{
514 unsigned long halt_time, timeout;
515 u32 status;
516
517 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
518
519 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
520 do {
521 halt_time = jiffies;
522 status = macb_readl(bp, TSR);
523 if (!(status & MACB_BIT(TGO)))
524 return 0;
525
526 usleep_range(10, 250);
527 } while (time_before(halt_time, timeout));
528
529 return -ETIMEDOUT;
530}
531
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200532static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
533{
534 if (tx_skb->mapping) {
535 if (tx_skb->mapped_as_page)
536 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
537 tx_skb->size, DMA_TO_DEVICE);
538 else
539 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
540 tx_skb->size, DMA_TO_DEVICE);
541 tx_skb->mapping = 0;
542 }
543
544 if (tx_skb->skb) {
545 dev_kfree_skb_any(tx_skb->skb);
546 tx_skb->skb = NULL;
547 }
548}
549
Nicolas Ferree86cd532012-10-31 06:04:57 +0000550static void macb_tx_error_task(struct work_struct *work)
551{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100552 struct macb_queue *queue = container_of(work, struct macb_queue,
553 tx_error_task);
554 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000555 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100556 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000557 struct sk_buff *skb;
558 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100559 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000560
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100561 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
562 (unsigned int)(queue - bp->queues),
563 queue->tx_tail, queue->tx_head);
564
565 /* Prevent the queue IRQ handlers from running: each of them may call
566 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
567 * As explained below, we have to halt the transmission before updating
568 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
569 * network engine about the macb/gem being halted.
570 */
571 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000572
573 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100574 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000575
576 /*
577 * Stop transmission now
578 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100579 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000580 */
581 if (macb_halt_tx(bp))
582 /* Just complain for now, reinitializing TX path can be good */
583 netdev_err(bp->dev, "BUG: halt tx timed out\n");
584
Nicolas Ferree86cd532012-10-31 06:04:57 +0000585 /*
586 * Treat frames in TX queue including the ones that caused the error.
587 * Free transmit buffers in upper layer.
588 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100589 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
590 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000591
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100592 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000593 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100594 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000595 skb = tx_skb->skb;
596
597 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200598 /* skb is set for the last buffer of the frame */
599 while (!skb) {
600 macb_tx_unmap(bp, tx_skb);
601 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100602 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200603 skb = tx_skb->skb;
604 }
605
606 /* ctrl still refers to the first buffer descriptor
607 * since it's the only one written back by the hardware
608 */
609 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
610 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
611 macb_tx_ring_wrap(tail), skb->data);
612 bp->stats.tx_packets++;
613 bp->stats.tx_bytes += skb->len;
614 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000615 } else {
616 /*
617 * "Buffers exhausted mid-frame" errors may only happen
618 * if the driver is buggy, so complain loudly about those.
619 * Statistics are updated by hardware.
620 */
621 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
622 netdev_err(bp->dev,
623 "BUG: TX buffers exhausted mid-frame\n");
624
625 desc->ctrl = ctrl | MACB_BIT(TX_USED);
626 }
627
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200628 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000629 }
630
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100631 /* Set end of TX queue */
632 desc = macb_tx_desc(queue, 0);
633 desc->addr = 0;
634 desc->ctrl = MACB_BIT(TX_USED);
635
Nicolas Ferree86cd532012-10-31 06:04:57 +0000636 /* Make descriptor updates visible to hardware */
637 wmb();
638
639 /* Reinitialize the TX desc queue */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100640 queue_writel(queue, TBQP, queue->tx_ring_dma);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000641 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100642 queue->tx_head = 0;
643 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000644
645 /* Housework before enabling TX IRQ */
646 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100647 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
648
649 /* Now we are ready to start transmission again */
650 netif_tx_start_all_queues(bp->dev);
651 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
652
653 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000654}
655
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100656static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100657{
658 unsigned int tail;
659 unsigned int head;
660 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100661 struct macb *bp = queue->bp;
662 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100663
664 status = macb_readl(bp, TSR);
665 macb_writel(bp, TSR, status);
666
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000667 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100668 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000669
Nicolas Ferree86cd532012-10-31 06:04:57 +0000670 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
671 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100672
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100673 head = queue->tx_head;
674 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000675 struct macb_tx_skb *tx_skb;
676 struct sk_buff *skb;
677 struct macb_dma_desc *desc;
678 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100679
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100680 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100681
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000682 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100683 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000684
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000685 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100686
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200687 /* TX_USED bit is only set by hardware on the very first buffer
688 * descriptor of the transmitted frame.
689 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000690 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100691 break;
692
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200693 /* Process all buffers of the current transmitted frame */
694 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100695 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200696 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000697
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200698 /* First, update TX stats if needed */
699 if (skb) {
700 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
701 macb_tx_ring_wrap(tail), skb->data);
702 bp->stats.tx_packets++;
703 bp->stats.tx_bytes += skb->len;
704 }
705
706 /* Now we can safely release resources */
707 macb_tx_unmap(bp, tx_skb);
708
709 /* skb is set only for the last buffer of the frame.
710 * WARNING: at this point skb has been freed by
711 * macb_tx_unmap().
712 */
713 if (skb)
714 break;
715 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100716 }
717
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100718 queue->tx_tail = tail;
719 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
720 CIRC_CNT(queue->tx_head, queue->tx_tail,
721 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
722 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100723}
724
Nicolas Ferre4df95132013-06-04 21:57:12 +0000725static void gem_rx_refill(struct macb *bp)
726{
727 unsigned int entry;
728 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000729 dma_addr_t paddr;
730
731 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000732 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000733
734 /* Make hw descriptor updates visible to CPU */
735 rmb();
736
Nicolas Ferre4df95132013-06-04 21:57:12 +0000737 bp->rx_prepared_head++;
738
Nicolas Ferre4df95132013-06-04 21:57:12 +0000739 if (bp->rx_skbuff[entry] == NULL) {
740 /* allocate sk_buff for this free entry in ring */
741 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
742 if (unlikely(skb == NULL)) {
743 netdev_err(bp->dev,
744 "Unable to allocate sk_buff\n");
745 break;
746 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000747
748 /* now fill corresponding descriptor entry */
749 paddr = dma_map_single(&bp->pdev->dev, skb->data,
750 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800751 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
752 dev_kfree_skb(skb);
753 break;
754 }
755
756 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000757
758 if (entry == RX_RING_SIZE - 1)
759 paddr |= MACB_BIT(RX_WRAP);
760 bp->rx_ring[entry].addr = paddr;
761 bp->rx_ring[entry].ctrl = 0;
762
763 /* properly align Ethernet header */
764 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530765 } else {
766 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
767 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000768 }
769 }
770
771 /* Make descriptor updates visible to hardware */
772 wmb();
773
774 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
775 bp->rx_prepared_head, bp->rx_tail);
776}
777
778/* Mark DMA descriptors from begin up to and not including end as unused */
779static void discard_partial_frame(struct macb *bp, unsigned int begin,
780 unsigned int end)
781{
782 unsigned int frag;
783
784 for (frag = begin; frag != end; frag++) {
785 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
786 desc->addr &= ~MACB_BIT(RX_USED);
787 }
788
789 /* Make descriptor updates visible to hardware */
790 wmb();
791
792 /*
793 * When this happens, the hardware stats registers for
794 * whatever caused this is updated, so we don't have to record
795 * anything.
796 */
797}
798
799static int gem_rx(struct macb *bp, int budget)
800{
801 unsigned int len;
802 unsigned int entry;
803 struct sk_buff *skb;
804 struct macb_dma_desc *desc;
805 int count = 0;
806
807 while (count < budget) {
808 u32 addr, ctrl;
809
810 entry = macb_rx_ring_wrap(bp->rx_tail);
811 desc = &bp->rx_ring[entry];
812
813 /* Make hw descriptor updates visible to CPU */
814 rmb();
815
816 addr = desc->addr;
817 ctrl = desc->ctrl;
818
819 if (!(addr & MACB_BIT(RX_USED)))
820 break;
821
Nicolas Ferre4df95132013-06-04 21:57:12 +0000822 bp->rx_tail++;
823 count++;
824
825 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
826 netdev_err(bp->dev,
827 "not whole frame pointed by descriptor\n");
828 bp->stats.rx_dropped++;
829 break;
830 }
831 skb = bp->rx_skbuff[entry];
832 if (unlikely(!skb)) {
833 netdev_err(bp->dev,
834 "inconsistent Rx descriptor chain\n");
835 bp->stats.rx_dropped++;
836 break;
837 }
838 /* now everything is ready for receiving packet */
839 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530840 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000841
842 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
843
844 skb_put(skb, len);
845 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
846 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800847 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000848
849 skb->protocol = eth_type_trans(skb, bp->dev);
850 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200851 if (bp->dev->features & NETIF_F_RXCSUM &&
852 !(bp->dev->flags & IFF_PROMISC) &&
853 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
854 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000855
856 bp->stats.rx_packets++;
857 bp->stats.rx_bytes += skb->len;
858
859#if defined(DEBUG) && defined(VERBOSE_DEBUG)
860 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
861 skb->len, skb->csum);
862 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100863 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000864 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
865 skb->data, 32, true);
866#endif
867
868 netif_receive_skb(skb);
869 }
870
871 gem_rx_refill(bp);
872
873 return count;
874}
875
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100876static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
877 unsigned int last_frag)
878{
879 unsigned int len;
880 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000881 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100882 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000883 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100884
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000885 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530886 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100887
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000888 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000889 macb_rx_ring_wrap(first_frag),
890 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100891
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000892 /*
893 * The ethernet header starts NET_IP_ALIGN bytes into the
894 * first buffer. Since the header is 14 bytes, this makes the
895 * payload word-aligned.
896 *
897 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
898 * the two padding bytes into the skb so that we avoid hitting
899 * the slowpath in memcpy(), and pull them off afterwards.
900 */
901 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100902 if (!skb) {
903 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000904 for (frag = first_frag; ; frag++) {
905 desc = macb_rx_desc(bp, frag);
906 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100907 if (frag == last_frag)
908 break;
909 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000910
911 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100912 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000913
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100914 return 1;
915 }
916
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000917 offset = 0;
918 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700919 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100920 skb_put(skb, len);
921
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000922 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000923 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100924
925 if (offset + frag_len > len) {
926 BUG_ON(frag != last_frag);
927 frag_len = len - offset;
928 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300929 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000930 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000931 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000932 desc = macb_rx_desc(bp, frag);
933 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100934
935 if (frag == last_frag)
936 break;
937 }
938
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000939 /* Make descriptor updates visible to hardware */
940 wmb();
941
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000942 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100943 skb->protocol = eth_type_trans(skb, bp->dev);
944
945 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000946 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000947 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000948 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100949 netif_receive_skb(skb);
950
951 return 0;
952}
953
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100954static int macb_rx(struct macb *bp, int budget)
955{
956 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000957 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100958 int first_frag = -1;
959
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000960 for (tail = bp->rx_tail; budget > 0; tail++) {
961 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100962 u32 addr, ctrl;
963
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000964 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100965 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000966
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000967 addr = desc->addr;
968 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100969
970 if (!(addr & MACB_BIT(RX_USED)))
971 break;
972
973 if (ctrl & MACB_BIT(RX_SOF)) {
974 if (first_frag != -1)
975 discard_partial_frame(bp, first_frag, tail);
976 first_frag = tail;
977 }
978
979 if (ctrl & MACB_BIT(RX_EOF)) {
980 int dropped;
981 BUG_ON(first_frag == -1);
982
983 dropped = macb_rx_frame(bp, first_frag, tail);
984 first_frag = -1;
985 if (!dropped) {
986 received++;
987 budget--;
988 }
989 }
990 }
991
992 if (first_frag != -1)
993 bp->rx_tail = first_frag;
994 else
995 bp->rx_tail = tail;
996
997 return received;
998}
999
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001000static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001001{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001002 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001003 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001004 u32 status;
1005
1006 status = macb_readl(bp, RSR);
1007 macb_writel(bp, RSR, status);
1008
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001009 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001010
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001011 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001012 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001013
Nicolas Ferre4df95132013-06-04 21:57:12 +00001014 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001015 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001016 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001017
Nicolas Ferre8770e912013-02-12 11:08:48 +01001018 /* Packets received while interrupts were disabled */
1019 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001020 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001021 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1022 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001023 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001024 } else {
1025 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1026 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001027 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001028
1029 /* TODO: Handle errors */
1030
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001031 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001032}
1033
1034static irqreturn_t macb_interrupt(int irq, void *dev_id)
1035{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001036 struct macb_queue *queue = dev_id;
1037 struct macb *bp = queue->bp;
1038 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001039 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001040
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001041 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001042
1043 if (unlikely(!status))
1044 return IRQ_NONE;
1045
1046 spin_lock(&bp->lock);
1047
1048 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001049 /* close possible race with dev_close */
1050 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001051 queue_writel(queue, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001052 break;
1053 }
1054
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001055 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1056 (unsigned int)(queue - bp->queues),
1057 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001058
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001059 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +00001060 /*
1061 * There's no point taking any more interrupts
1062 * until we have processed the buffers. The
1063 * scheduling call may fail if the poll routine
1064 * is already scheduled, so disable interrupts
1065 * now.
1066 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001067 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001068 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001069 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001070
Ben Hutchings288379f2009-01-19 16:43:59 -08001071 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001072 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001073 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001074 }
1075 }
1076
Nicolas Ferree86cd532012-10-31 06:04:57 +00001077 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001078 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1079 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001080
1081 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001082 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001083
Nicolas Ferree86cd532012-10-31 06:04:57 +00001084 break;
1085 }
1086
1087 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001088 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001089
1090 /*
1091 * Link change detection isn't possible with RMII, so we'll
1092 * add that if/when we get our hands on a full-blown MII PHY.
1093 */
1094
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001095 /* There is a hardware issue under heavy load where DMA can
1096 * stop, this causes endless "used buffer descriptor read"
1097 * interrupts but it can be cleared by re-enabling RX. See
1098 * the at91 manual, section 41.3.1 or the Zynq manual
1099 * section 16.7.4 for details.
1100 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001101 if (status & MACB_BIT(RXUBR)) {
1102 ctrl = macb_readl(bp, NCR);
1103 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1104 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1105
1106 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1107 macb_writel(bp, ISR, MACB_BIT(RXUBR));
1108 }
1109
Alexander Steinb19f7f72011-04-13 05:03:24 +00001110 if (status & MACB_BIT(ISR_ROVR)) {
1111 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001112 if (macb_is_gem(bp))
1113 bp->hw_stats.gem.rx_overruns++;
1114 else
1115 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001116
1117 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001118 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001119 }
1120
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001121 if (status & MACB_BIT(HRESP)) {
1122 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001123 * TODO: Reset the hardware, and maybe move the
1124 * netdev_err to a lower-priority context as well
1125 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001126 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001127 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001128
1129 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001130 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001131 }
1132
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001133 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001134 }
1135
1136 spin_unlock(&bp->lock);
1137
1138 return IRQ_HANDLED;
1139}
1140
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001141#ifdef CONFIG_NET_POLL_CONTROLLER
1142/*
1143 * Polling receive - used by netconsole and other diagnostic tools
1144 * to allow network i/o with interrupts disabled.
1145 */
1146static void macb_poll_controller(struct net_device *dev)
1147{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001148 struct macb *bp = netdev_priv(dev);
1149 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001150 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001151 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001152
1153 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001154 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1155 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001156 local_irq_restore(flags);
1157}
1158#endif
1159
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001160static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
1161 unsigned int len)
1162{
1163 return (len + bp->max_tx_length - 1) / bp->max_tx_length;
1164}
1165
1166static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001167 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001168 struct sk_buff *skb)
1169{
1170 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001171 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001172 struct macb_tx_skb *tx_skb = NULL;
1173 struct macb_dma_desc *desc;
1174 unsigned int offset, size, count = 0;
1175 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1176 unsigned int eof = 1;
1177 u32 ctrl;
1178
1179 /* First, map non-paged data */
1180 len = skb_headlen(skb);
1181 offset = 0;
1182 while (len) {
1183 size = min(len, bp->max_tx_length);
1184 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001185 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001186
1187 mapping = dma_map_single(&bp->pdev->dev,
1188 skb->data + offset,
1189 size, DMA_TO_DEVICE);
1190 if (dma_mapping_error(&bp->pdev->dev, mapping))
1191 goto dma_error;
1192
1193 /* Save info to properly release resources */
1194 tx_skb->skb = NULL;
1195 tx_skb->mapping = mapping;
1196 tx_skb->size = size;
1197 tx_skb->mapped_as_page = false;
1198
1199 len -= size;
1200 offset += size;
1201 count++;
1202 tx_head++;
1203 }
1204
1205 /* Then, map paged data from fragments */
1206 for (f = 0; f < nr_frags; f++) {
1207 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1208
1209 len = skb_frag_size(frag);
1210 offset = 0;
1211 while (len) {
1212 size = min(len, bp->max_tx_length);
1213 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001214 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001215
1216 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1217 offset, size, DMA_TO_DEVICE);
1218 if (dma_mapping_error(&bp->pdev->dev, mapping))
1219 goto dma_error;
1220
1221 /* Save info to properly release resources */
1222 tx_skb->skb = NULL;
1223 tx_skb->mapping = mapping;
1224 tx_skb->size = size;
1225 tx_skb->mapped_as_page = true;
1226
1227 len -= size;
1228 offset += size;
1229 count++;
1230 tx_head++;
1231 }
1232 }
1233
1234 /* Should never happen */
1235 if (unlikely(tx_skb == NULL)) {
1236 netdev_err(bp->dev, "BUG! empty skb!\n");
1237 return 0;
1238 }
1239
1240 /* This is the last buffer of the frame: save socket buffer */
1241 tx_skb->skb = skb;
1242
1243 /* Update TX ring: update buffer descriptors in reverse order
1244 * to avoid race condition
1245 */
1246
1247 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1248 * to set the end of TX queue
1249 */
1250 i = tx_head;
1251 entry = macb_tx_ring_wrap(i);
1252 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001253 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001254 desc->ctrl = ctrl;
1255
1256 do {
1257 i--;
1258 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001259 tx_skb = &queue->tx_skb[entry];
1260 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001261
1262 ctrl = (u32)tx_skb->size;
1263 if (eof) {
1264 ctrl |= MACB_BIT(TX_LAST);
1265 eof = 0;
1266 }
1267 if (unlikely(entry == (TX_RING_SIZE - 1)))
1268 ctrl |= MACB_BIT(TX_WRAP);
1269
1270 /* Set TX buffer descriptor */
1271 desc->addr = tx_skb->mapping;
1272 /* desc->addr must be visible to hardware before clearing
1273 * 'TX_USED' bit in desc->ctrl.
1274 */
1275 wmb();
1276 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001277 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001278
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001279 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001280
1281 return count;
1282
1283dma_error:
1284 netdev_err(bp->dev, "TX DMA map failed\n");
1285
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001286 for (i = queue->tx_head; i != tx_head; i++) {
1287 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001288
1289 macb_tx_unmap(bp, tx_skb);
1290 }
1291
1292 return 0;
1293}
1294
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001295static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1296{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001297 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001298 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001299 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001300 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001301 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001302
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001303#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1304 netdev_vdbg(bp->dev,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001305 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1306 queue_index, skb->len, skb->head, skb->data,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001307 skb_tail_pointer(skb), skb_end_pointer(skb));
1308 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1309 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001310#endif
1311
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001312 /* Count how many TX buffer descriptors are needed to send this
1313 * socket buffer: skb fragments of jumbo frames may need to be
1314 * splitted into many buffer descriptors.
1315 */
1316 count = macb_count_tx_descriptors(bp, skb_headlen(skb));
1317 nr_frags = skb_shinfo(skb)->nr_frags;
1318 for (f = 0; f < nr_frags; f++) {
1319 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1320 count += macb_count_tx_descriptors(bp, frag_size);
1321 }
1322
Dongdong Deng48719532009-08-23 19:49:07 -07001323 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001324
1325 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001326 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1327 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001328 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001329 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001330 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001331 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001332 }
1333
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001334 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001335 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001336 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001337 goto unlock;
1338 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001339
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001340 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001341 wmb();
1342
Richard Cochrane0720922011-06-19 21:51:28 +00001343 skb_tx_timestamp(skb);
1344
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001345 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1346
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001347 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1348 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001349
Soren Brinkmann92030902014-03-04 08:46:39 -08001350unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001351 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001352
Patrick McHardy6ed10652009-06-23 06:03:08 +00001353 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001354}
1355
Nicolas Ferre4df95132013-06-04 21:57:12 +00001356static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001357{
1358 if (!macb_is_gem(bp)) {
1359 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1360 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001361 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001362
Nicolas Ferre1b447912013-06-04 21:57:11 +00001363 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001364 netdev_dbg(bp->dev,
1365 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001366 RX_BUFFER_MULTIPLE);
1367 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001368 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001369 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001370 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001371
1372 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1373 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001374}
1375
Nicolas Ferre4df95132013-06-04 21:57:12 +00001376static void gem_free_rx_buffers(struct macb *bp)
1377{
1378 struct sk_buff *skb;
1379 struct macb_dma_desc *desc;
1380 dma_addr_t addr;
1381 int i;
1382
1383 if (!bp->rx_skbuff)
1384 return;
1385
1386 for (i = 0; i < RX_RING_SIZE; i++) {
1387 skb = bp->rx_skbuff[i];
1388
1389 if (skb == NULL)
1390 continue;
1391
1392 desc = &bp->rx_ring[i];
1393 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001394 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001395 DMA_FROM_DEVICE);
1396 dev_kfree_skb_any(skb);
1397 skb = NULL;
1398 }
1399
1400 kfree(bp->rx_skbuff);
1401 bp->rx_skbuff = NULL;
1402}
1403
1404static void macb_free_rx_buffers(struct macb *bp)
1405{
1406 if (bp->rx_buffers) {
1407 dma_free_coherent(&bp->pdev->dev,
1408 RX_RING_SIZE * bp->rx_buffer_size,
1409 bp->rx_buffers, bp->rx_buffers_dma);
1410 bp->rx_buffers = NULL;
1411 }
1412}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001413
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001414static void macb_free_consistent(struct macb *bp)
1415{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001416 struct macb_queue *queue;
1417 unsigned int q;
1418
Nicolas Ferre4df95132013-06-04 21:57:12 +00001419 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001420 if (bp->rx_ring) {
1421 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1422 bp->rx_ring, bp->rx_ring_dma);
1423 bp->rx_ring = NULL;
1424 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001425
1426 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1427 kfree(queue->tx_skb);
1428 queue->tx_skb = NULL;
1429 if (queue->tx_ring) {
1430 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1431 queue->tx_ring, queue->tx_ring_dma);
1432 queue->tx_ring = NULL;
1433 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001434 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001435}
1436
1437static int gem_alloc_rx_buffers(struct macb *bp)
1438{
1439 int size;
1440
1441 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1442 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1443 if (!bp->rx_skbuff)
1444 return -ENOMEM;
1445 else
1446 netdev_dbg(bp->dev,
1447 "Allocated %d RX struct sk_buff entries at %p\n",
1448 RX_RING_SIZE, bp->rx_skbuff);
1449 return 0;
1450}
1451
1452static int macb_alloc_rx_buffers(struct macb *bp)
1453{
1454 int size;
1455
1456 size = RX_RING_SIZE * bp->rx_buffer_size;
1457 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1458 &bp->rx_buffers_dma, GFP_KERNEL);
1459 if (!bp->rx_buffers)
1460 return -ENOMEM;
1461 else
1462 netdev_dbg(bp->dev,
1463 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1464 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1465 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001466}
1467
1468static int macb_alloc_consistent(struct macb *bp)
1469{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001470 struct macb_queue *queue;
1471 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001472 int size;
1473
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001474 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1475 size = TX_RING_BYTES;
1476 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1477 &queue->tx_ring_dma,
1478 GFP_KERNEL);
1479 if (!queue->tx_ring)
1480 goto out_err;
1481 netdev_dbg(bp->dev,
1482 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1483 q, size, (unsigned long)queue->tx_ring_dma,
1484 queue->tx_ring);
1485
1486 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1487 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1488 if (!queue->tx_skb)
1489 goto out_err;
1490 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001491
1492 size = RX_RING_BYTES;
1493 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1494 &bp->rx_ring_dma, GFP_KERNEL);
1495 if (!bp->rx_ring)
1496 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001497 netdev_dbg(bp->dev,
1498 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1499 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001500
Nicolas Ferre4df95132013-06-04 21:57:12 +00001501 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001502 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001503
1504 return 0;
1505
1506out_err:
1507 macb_free_consistent(bp);
1508 return -ENOMEM;
1509}
1510
Nicolas Ferre4df95132013-06-04 21:57:12 +00001511static void gem_init_rings(struct macb *bp)
1512{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001513 struct macb_queue *queue;
1514 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001515 int i;
1516
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001517 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1518 for (i = 0; i < TX_RING_SIZE; i++) {
1519 queue->tx_ring[i].addr = 0;
1520 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1521 }
1522 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1523 queue->tx_head = 0;
1524 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001525 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001526
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001527 bp->rx_tail = 0;
1528 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001529
1530 gem_rx_refill(bp);
1531}
1532
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001533static void macb_init_rings(struct macb *bp)
1534{
1535 int i;
1536 dma_addr_t addr;
1537
1538 addr = bp->rx_buffers_dma;
1539 for (i = 0; i < RX_RING_SIZE; i++) {
1540 bp->rx_ring[i].addr = addr;
1541 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001542 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001543 }
1544 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1545
1546 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001547 bp->queues[0].tx_ring[i].addr = 0;
1548 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001549 }
Ben Shelton21d35152015-04-22 17:28:54 -05001550 bp->queues[0].tx_head = 0;
1551 bp->queues[0].tx_tail = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001552 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001553
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001554 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001555}
1556
1557static void macb_reset_hw(struct macb *bp)
1558{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001559 struct macb_queue *queue;
1560 unsigned int q;
1561
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001562 /*
1563 * Disable RX and TX (XXX: Should we halt the transmission
1564 * more gracefully?)
1565 */
1566 macb_writel(bp, NCR, 0);
1567
1568 /* Clear the stats registers (XXX: Update stats first?) */
1569 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1570
1571 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001572 macb_writel(bp, TSR, -1);
1573 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001574
1575 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001576 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1577 queue_writel(queue, IDR, -1);
1578 queue_readl(queue, ISR);
1579 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001580}
1581
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001582static u32 gem_mdc_clk_div(struct macb *bp)
1583{
1584 u32 config;
1585 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1586
1587 if (pclk_hz <= 20000000)
1588 config = GEM_BF(CLK, GEM_CLK_DIV8);
1589 else if (pclk_hz <= 40000000)
1590 config = GEM_BF(CLK, GEM_CLK_DIV16);
1591 else if (pclk_hz <= 80000000)
1592 config = GEM_BF(CLK, GEM_CLK_DIV32);
1593 else if (pclk_hz <= 120000000)
1594 config = GEM_BF(CLK, GEM_CLK_DIV48);
1595 else if (pclk_hz <= 160000000)
1596 config = GEM_BF(CLK, GEM_CLK_DIV64);
1597 else
1598 config = GEM_BF(CLK, GEM_CLK_DIV96);
1599
1600 return config;
1601}
1602
1603static u32 macb_mdc_clk_div(struct macb *bp)
1604{
1605 u32 config;
1606 unsigned long pclk_hz;
1607
1608 if (macb_is_gem(bp))
1609 return gem_mdc_clk_div(bp);
1610
1611 pclk_hz = clk_get_rate(bp->pclk);
1612 if (pclk_hz <= 20000000)
1613 config = MACB_BF(CLK, MACB_CLK_DIV8);
1614 else if (pclk_hz <= 40000000)
1615 config = MACB_BF(CLK, MACB_CLK_DIV16);
1616 else if (pclk_hz <= 80000000)
1617 config = MACB_BF(CLK, MACB_CLK_DIV32);
1618 else
1619 config = MACB_BF(CLK, MACB_CLK_DIV64);
1620
1621 return config;
1622}
1623
Jamie Iles757a03c2011-03-09 16:29:59 +00001624/*
1625 * Get the DMA bus width field of the network configuration register that we
1626 * should program. We find the width from decoding the design configuration
1627 * register to find the maximum supported data bus width.
1628 */
1629static u32 macb_dbw(struct macb *bp)
1630{
1631 if (!macb_is_gem(bp))
1632 return 0;
1633
1634 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1635 case 4:
1636 return GEM_BF(DBW, GEM_DBW128);
1637 case 2:
1638 return GEM_BF(DBW, GEM_DBW64);
1639 case 1:
1640 default:
1641 return GEM_BF(DBW, GEM_DBW32);
1642 }
1643}
1644
Jamie Iles0116da42011-03-14 17:38:30 +00001645/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001646 * Configure the receive DMA engine
1647 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001648 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001649 * (if not supported by FIFO, it will fallback to default)
1650 * - set both rx/tx packet buffers to full memory size
1651 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001652 */
1653static void macb_configure_dma(struct macb *bp)
1654{
1655 u32 dmacfg;
1656
1657 if (macb_is_gem(bp)) {
1658 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001659 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001660 if (bp->dma_burst_length)
1661 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001662 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301663 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301664
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001665 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301666 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1667 else
1668 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1669
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001670 if (bp->dev->features & NETIF_F_HW_CSUM)
1671 dmacfg |= GEM_BIT(TXCOEN);
1672 else
1673 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001674 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1675 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001676 gem_writel(bp, DMACFG, dmacfg);
1677 }
1678}
1679
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001680static void macb_init_hw(struct macb *bp)
1681{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001682 struct macb_queue *queue;
1683 unsigned int q;
1684
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001685 u32 config;
1686
1687 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001688 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001689
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001690 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001691 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001692 config |= MACB_BIT(PAE); /* PAuse Enable */
1693 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001694 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301695 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1696 else
1697 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001698 if (bp->dev->flags & IFF_PROMISC)
1699 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001700 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1701 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001702 if (!(bp->dev->flags & IFF_BROADCAST))
1703 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001704 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001705 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001706 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301707 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001708 bp->speed = SPEED_10;
1709 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301710 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001711 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301712 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001713
Jamie Iles0116da42011-03-14 17:38:30 +00001714 macb_configure_dma(bp);
1715
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001716 /* Initialize TX and RX buffers */
1717 macb_writel(bp, RBQP, bp->rx_ring_dma);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001718 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1719 queue_writel(queue, TBQP, queue->tx_ring_dma);
1720
1721 /* Enable interrupts */
1722 queue_writel(queue, IER,
1723 MACB_RX_INT_FLAGS |
1724 MACB_TX_INT_FLAGS |
1725 MACB_BIT(HRESP));
1726 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001727
1728 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001729 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001730}
1731
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001732/*
1733 * The hash address register is 64 bits long and takes up two
1734 * locations in the memory map. The least significant bits are stored
1735 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1736 *
1737 * The unicast hash enable and the multicast hash enable bits in the
1738 * network configuration register enable the reception of hash matched
1739 * frames. The destination address is reduced to a 6 bit index into
1740 * the 64 bit hash register using the following hash function. The
1741 * hash function is an exclusive or of every sixth bit of the
1742 * destination address.
1743 *
1744 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1745 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1746 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1747 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1748 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1749 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1750 *
1751 * da[0] represents the least significant bit of the first byte
1752 * received, that is, the multicast/unicast indicator, and da[47]
1753 * represents the most significant bit of the last byte received. If
1754 * the hash index, hi[n], points to a bit that is set in the hash
1755 * register then the frame will be matched according to whether the
1756 * frame is multicast or unicast. A multicast match will be signalled
1757 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1758 * index points to a bit set in the hash register. A unicast match
1759 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1760 * and the hash index points to a bit set in the hash register. To
1761 * receive all multicast frames, the hash register should be set with
1762 * all ones and the multicast hash enable bit should be set in the
1763 * network configuration register.
1764 */
1765
1766static inline int hash_bit_value(int bitnr, __u8 *addr)
1767{
1768 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1769 return 1;
1770 return 0;
1771}
1772
1773/*
1774 * Return the hash index value for the specified address.
1775 */
1776static int hash_get_index(__u8 *addr)
1777{
1778 int i, j, bitval;
1779 int hash_index = 0;
1780
1781 for (j = 0; j < 6; j++) {
1782 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001783 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001784
1785 hash_index |= (bitval << j);
1786 }
1787
1788 return hash_index;
1789}
1790
1791/*
1792 * Add multicast addresses to the internal multicast-hash table.
1793 */
1794static void macb_sethashtable(struct net_device *dev)
1795{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001796 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001797 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001798 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001799 struct macb *bp = netdev_priv(dev);
1800
1801 mc_filter[0] = mc_filter[1] = 0;
1802
Jiri Pirko22bedad32010-04-01 21:22:57 +00001803 netdev_for_each_mc_addr(ha, dev) {
1804 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001805 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1806 }
1807
Jamie Ilesf75ba502011-11-08 10:12:32 +00001808 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1809 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001810}
1811
1812/*
1813 * Enable/Disable promiscuous and multicast modes.
1814 */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001815static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001816{
1817 unsigned long cfg;
1818 struct macb *bp = netdev_priv(dev);
1819
1820 cfg = macb_readl(bp, NCFGR);
1821
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001822 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001823 /* Enable promiscuous mode */
1824 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001825
1826 /* Disable RX checksum offload */
1827 if (macb_is_gem(bp))
1828 cfg &= ~GEM_BIT(RXCOEN);
1829 } else {
1830 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001831 cfg &= ~MACB_BIT(CAF);
1832
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001833 /* Enable RX checksum offload only if requested */
1834 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1835 cfg |= GEM_BIT(RXCOEN);
1836 }
1837
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001838 if (dev->flags & IFF_ALLMULTI) {
1839 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001840 macb_or_gem_writel(bp, HRB, -1);
1841 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001842 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001843 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001844 /* Enable specific multicasts */
1845 macb_sethashtable(dev);
1846 cfg |= MACB_BIT(NCFGR_MTI);
1847 } else if (dev->flags & (~IFF_ALLMULTI)) {
1848 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001849 macb_or_gem_writel(bp, HRB, 0);
1850 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001851 cfg &= ~MACB_BIT(NCFGR_MTI);
1852 }
1853
1854 macb_writel(bp, NCFGR, cfg);
1855}
1856
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001857static int macb_open(struct net_device *dev)
1858{
1859 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001860 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001861 int err;
1862
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001863 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001864
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001865 /* carrier starts down */
1866 netif_carrier_off(dev);
1867
frederic RODO6c36a702007-07-12 19:07:24 +02001868 /* if the phy is not yet register, retry later*/
1869 if (!bp->phy_dev)
1870 return -EAGAIN;
1871
Nicolas Ferre1b447912013-06-04 21:57:11 +00001872 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001873 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001874
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001875 err = macb_alloc_consistent(bp);
1876 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001877 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1878 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001879 return err;
1880 }
1881
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001882 napi_enable(&bp->napi);
1883
Nicolas Ferre4df95132013-06-04 21:57:12 +00001884 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001885 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001886
frederic RODO6c36a702007-07-12 19:07:24 +02001887 /* schedule a link state check */
1888 phy_start(bp->phy_dev);
1889
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001890 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001891
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001892 return 0;
1893}
1894
1895static int macb_close(struct net_device *dev)
1896{
1897 struct macb *bp = netdev_priv(dev);
1898 unsigned long flags;
1899
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001900 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001901 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001902
frederic RODO6c36a702007-07-12 19:07:24 +02001903 if (bp->phy_dev)
1904 phy_stop(bp->phy_dev);
1905
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001906 spin_lock_irqsave(&bp->lock, flags);
1907 macb_reset_hw(bp);
1908 netif_carrier_off(dev);
1909 spin_unlock_irqrestore(&bp->lock, flags);
1910
1911 macb_free_consistent(bp);
1912
1913 return 0;
1914}
1915
Harini Katakama5898ea2015-05-06 22:27:18 +05301916static int macb_change_mtu(struct net_device *dev, int new_mtu)
1917{
1918 struct macb *bp = netdev_priv(dev);
1919 u32 max_mtu;
1920
1921 if (netif_running(dev))
1922 return -EBUSY;
1923
1924 max_mtu = ETH_DATA_LEN;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001925 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakama5898ea2015-05-06 22:27:18 +05301926 max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
1927
1928 if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE))
1929 return -EINVAL;
1930
1931 dev->mtu = new_mtu;
1932
1933 return 0;
1934}
1935
Jamie Ilesa494ed82011-03-09 16:26:35 +00001936static void gem_update_stats(struct macb *bp)
1937{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03001938 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001939 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001940
Xander Huff3ff13f12015-01-13 16:15:51 -06001941 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1942 u32 offset = gem_statistics[i].offset;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001943 u64 val = bp->readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001944
1945 bp->ethtool_stats[i] += val;
1946 *p += val;
1947
1948 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1949 /* Add GEM_OCTTXH, GEM_OCTRXH */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001950 val = bp->readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06001951 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06001952 *(++p) += val;
1953 }
1954 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00001955}
1956
1957static struct net_device_stats *gem_get_stats(struct macb *bp)
1958{
1959 struct gem_stats *hwstat = &bp->hw_stats.gem;
1960 struct net_device_stats *nstat = &bp->stats;
1961
1962 gem_update_stats(bp);
1963
1964 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1965 hwstat->rx_alignment_errors +
1966 hwstat->rx_resource_errors +
1967 hwstat->rx_overruns +
1968 hwstat->rx_oversize_frames +
1969 hwstat->rx_jabbers +
1970 hwstat->rx_undersized_frames +
1971 hwstat->rx_length_field_frame_errors);
1972 nstat->tx_errors = (hwstat->tx_late_collisions +
1973 hwstat->tx_excessive_collisions +
1974 hwstat->tx_underrun +
1975 hwstat->tx_carrier_sense_errors);
1976 nstat->multicast = hwstat->rx_multicast_frames;
1977 nstat->collisions = (hwstat->tx_single_collision_frames +
1978 hwstat->tx_multiple_collision_frames +
1979 hwstat->tx_excessive_collisions);
1980 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1981 hwstat->rx_jabbers +
1982 hwstat->rx_undersized_frames +
1983 hwstat->rx_length_field_frame_errors);
1984 nstat->rx_over_errors = hwstat->rx_resource_errors;
1985 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1986 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1987 nstat->rx_fifo_errors = hwstat->rx_overruns;
1988 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1989 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1990 nstat->tx_fifo_errors = hwstat->tx_underrun;
1991
1992 return nstat;
1993}
1994
Xander Huff3ff13f12015-01-13 16:15:51 -06001995static void gem_get_ethtool_stats(struct net_device *dev,
1996 struct ethtool_stats *stats, u64 *data)
1997{
1998 struct macb *bp;
1999
2000 bp = netdev_priv(dev);
2001 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002002 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002003}
2004
2005static int gem_get_sset_count(struct net_device *dev, int sset)
2006{
2007 switch (sset) {
2008 case ETH_SS_STATS:
2009 return GEM_STATS_LEN;
2010 default:
2011 return -EOPNOTSUPP;
2012 }
2013}
2014
2015static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2016{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002017 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002018
2019 switch (sset) {
2020 case ETH_SS_STATS:
2021 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2022 memcpy(p, gem_statistics[i].stat_string,
2023 ETH_GSTRING_LEN);
2024 break;
2025 }
2026}
2027
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002028static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002029{
2030 struct macb *bp = netdev_priv(dev);
2031 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002032 struct macb_stats *hwstat = &bp->hw_stats.macb;
2033
2034 if (macb_is_gem(bp))
2035 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002036
frederic RODO6c36a702007-07-12 19:07:24 +02002037 /* read stats from hardware */
2038 macb_update_stats(bp);
2039
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002040 /* Convert HW stats into netdevice stats */
2041 nstat->rx_errors = (hwstat->rx_fcs_errors +
2042 hwstat->rx_align_errors +
2043 hwstat->rx_resource_errors +
2044 hwstat->rx_overruns +
2045 hwstat->rx_oversize_pkts +
2046 hwstat->rx_jabbers +
2047 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002048 hwstat->rx_length_mismatch);
2049 nstat->tx_errors = (hwstat->tx_late_cols +
2050 hwstat->tx_excessive_cols +
2051 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002052 hwstat->tx_carrier_errors +
2053 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002054 nstat->collisions = (hwstat->tx_single_cols +
2055 hwstat->tx_multiple_cols +
2056 hwstat->tx_excessive_cols);
2057 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2058 hwstat->rx_jabbers +
2059 hwstat->rx_undersize_pkts +
2060 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002061 nstat->rx_over_errors = hwstat->rx_resource_errors +
2062 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002063 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2064 nstat->rx_frame_errors = hwstat->rx_align_errors;
2065 nstat->rx_fifo_errors = hwstat->rx_overruns;
2066 /* XXX: What does "missed" mean? */
2067 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2068 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2069 nstat->tx_fifo_errors = hwstat->tx_underruns;
2070 /* Don't know about heartbeat or window errors... */
2071
2072 return nstat;
2073}
2074
2075static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2076{
2077 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002078 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002079
frederic RODO6c36a702007-07-12 19:07:24 +02002080 if (!phydev)
2081 return -ENODEV;
2082
2083 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084}
2085
2086static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2087{
2088 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002089 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002090
frederic RODO6c36a702007-07-12 19:07:24 +02002091 if (!phydev)
2092 return -ENODEV;
2093
2094 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002095}
2096
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002097static int macb_get_regs_len(struct net_device *netdev)
2098{
2099 return MACB_GREGS_NBR * sizeof(u32);
2100}
2101
2102static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2103 void *p)
2104{
2105 struct macb *bp = netdev_priv(dev);
2106 unsigned int tail, head;
2107 u32 *regs_buff = p;
2108
2109 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2110 | MACB_GREGS_VERSION;
2111
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002112 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2113 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002114
2115 regs_buff[0] = macb_readl(bp, NCR);
2116 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2117 regs_buff[2] = macb_readl(bp, NSR);
2118 regs_buff[3] = macb_readl(bp, TSR);
2119 regs_buff[4] = macb_readl(bp, RBQP);
2120 regs_buff[5] = macb_readl(bp, TBQP);
2121 regs_buff[6] = macb_readl(bp, RSR);
2122 regs_buff[7] = macb_readl(bp, IMR);
2123
2124 regs_buff[8] = tail;
2125 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002126 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2127 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002128
Nicolas Ferre7c399942015-03-31 15:02:04 +02002129 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002130 if (macb_is_gem(bp)) {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002131 regs_buff[13] = gem_readl(bp, DMACFG);
2132 }
2133}
2134
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002135static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002136 .get_settings = macb_get_settings,
2137 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002138 .get_regs_len = macb_get_regs_len,
2139 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002140 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002141 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff8cd5a562015-01-15 15:55:20 -06002142};
Xander Huff8cd5a562015-01-15 15:55:20 -06002143
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002144static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002145 .get_settings = macb_get_settings,
2146 .set_settings = macb_set_settings,
2147 .get_regs_len = macb_get_regs_len,
2148 .get_regs = macb_get_regs,
2149 .get_link = ethtool_op_get_link,
2150 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002151 .get_ethtool_stats = gem_get_ethtool_stats,
2152 .get_strings = gem_get_ethtool_strings,
2153 .get_sset_count = gem_get_sset_count,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002154};
2155
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002156static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002157{
2158 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002159 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002160
2161 if (!netif_running(dev))
2162 return -EINVAL;
2163
frederic RODO6c36a702007-07-12 19:07:24 +02002164 if (!phydev)
2165 return -ENODEV;
2166
Richard Cochran28b04112010-07-17 08:48:55 +00002167 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002168}
2169
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002170static int macb_set_features(struct net_device *netdev,
2171 netdev_features_t features)
2172{
2173 struct macb *bp = netdev_priv(netdev);
2174 netdev_features_t changed = features ^ netdev->features;
2175
2176 /* TX checksum offload */
2177 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2178 u32 dmacfg;
2179
2180 dmacfg = gem_readl(bp, DMACFG);
2181 if (features & NETIF_F_HW_CSUM)
2182 dmacfg |= GEM_BIT(TXCOEN);
2183 else
2184 dmacfg &= ~GEM_BIT(TXCOEN);
2185 gem_writel(bp, DMACFG, dmacfg);
2186 }
2187
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002188 /* RX checksum offload */
2189 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2190 u32 netcfg;
2191
2192 netcfg = gem_readl(bp, NCFGR);
2193 if (features & NETIF_F_RXCSUM &&
2194 !(netdev->flags & IFF_PROMISC))
2195 netcfg |= GEM_BIT(RXCOEN);
2196 else
2197 netcfg &= ~GEM_BIT(RXCOEN);
2198 gem_writel(bp, NCFGR, netcfg);
2199 }
2200
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002201 return 0;
2202}
2203
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002204static const struct net_device_ops macb_netdev_ops = {
2205 .ndo_open = macb_open,
2206 .ndo_stop = macb_close,
2207 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002208 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002209 .ndo_get_stats = macb_get_stats,
2210 .ndo_do_ioctl = macb_ioctl,
2211 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302212 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002213 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002214#ifdef CONFIG_NET_POLL_CONTROLLER
2215 .ndo_poll_controller = macb_poll_controller,
2216#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002217 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002218};
2219
Nicolas Ferree1755872014-07-24 13:50:58 +02002220/*
Nicolas Ferread783472015-03-31 15:02:02 +02002221 * Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002222 * and integration options used
2223 */
Nicolas Ferref6970502015-03-31 15:02:01 +02002224static void macb_configure_caps(struct macb *bp, const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002225{
2226 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002227
Nicolas Ferref6970502015-03-31 15:02:01 +02002228 if (dt_conf)
2229 bp->caps = dt_conf->caps;
2230
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002231 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002232 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2233
Nicolas Ferree1755872014-07-24 13:50:58 +02002234 dcfg = gem_readl(bp, DCFG1);
2235 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2236 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2237 dcfg = gem_readl(bp, DCFG2);
2238 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2239 bp->caps |= MACB_CAPS_FIFO_MODE;
2240 }
2241
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002242 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002243}
2244
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002245static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002246 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002247 unsigned int *queue_mask,
2248 unsigned int *num_queues)
2249{
2250 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002251
2252 *queue_mask = 0x1;
2253 *num_queues = 1;
2254
Nicolas Ferreda120112015-03-31 15:02:00 +02002255 /* is it macb or gem ?
2256 *
2257 * We need to read directly from the hardware here because
2258 * we are early in the probe process and don't have the
2259 * MACB_CAPS_MACB_IS_GEM flag positioned
2260 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002261 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002262 return;
2263
2264 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302265 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2266
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002267 *queue_mask |= 0x1;
2268
2269 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2270 if (*queue_mask & (1 << hw_q))
2271 (*num_queues)++;
2272}
2273
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002274static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
2275 struct clk **hclk, struct clk **tx_clk)
2276{
2277 int err;
2278
2279 *pclk = devm_clk_get(&pdev->dev, "pclk");
2280 if (IS_ERR(*pclk)) {
2281 err = PTR_ERR(*pclk);
2282 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2283 return err;
2284 }
2285
2286 *hclk = devm_clk_get(&pdev->dev, "hclk");
2287 if (IS_ERR(*hclk)) {
2288 err = PTR_ERR(*hclk);
2289 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2290 return err;
2291 }
2292
2293 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2294 if (IS_ERR(*tx_clk))
2295 *tx_clk = NULL;
2296
2297 err = clk_prepare_enable(*pclk);
2298 if (err) {
2299 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2300 return err;
2301 }
2302
2303 err = clk_prepare_enable(*hclk);
2304 if (err) {
2305 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2306 goto err_disable_pclk;
2307 }
2308
2309 err = clk_prepare_enable(*tx_clk);
2310 if (err) {
2311 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2312 goto err_disable_hclk;
2313 }
2314
2315 return 0;
2316
2317err_disable_hclk:
2318 clk_disable_unprepare(*hclk);
2319
2320err_disable_pclk:
2321 clk_disable_unprepare(*pclk);
2322
2323 return err;
2324}
2325
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002326static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002327{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002328 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002329 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002330 struct macb *bp = netdev_priv(dev);
2331 struct macb_queue *queue;
2332 int err;
2333 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002334
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002335 /* set the queue register mapping once for all: queue0 has a special
2336 * register mapping but we don't want to test the queue index then
2337 * compute the corresponding register offset at run time.
2338 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002339 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002340 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002341 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002342
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002343 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002344 queue->bp = bp;
2345 if (hw_q) {
2346 queue->ISR = GEM_ISR(hw_q - 1);
2347 queue->IER = GEM_IER(hw_q - 1);
2348 queue->IDR = GEM_IDR(hw_q - 1);
2349 queue->IMR = GEM_IMR(hw_q - 1);
2350 queue->TBQP = GEM_TBQP(hw_q - 1);
2351 } else {
2352 /* queue0 uses legacy registers */
2353 queue->ISR = MACB_ISR;
2354 queue->IER = MACB_IER;
2355 queue->IDR = MACB_IDR;
2356 queue->IMR = MACB_IMR;
2357 queue->TBQP = MACB_TBQP;
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002358 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002359
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002360 /* get irq: here we use the linux queue index, not the hardware
2361 * queue index. the queue irq definitions in the device tree
2362 * must remove the optional gaps that could exist in the
2363 * hardware queue mask.
2364 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002365 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002366 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002367 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002368 if (err) {
2369 dev_err(&pdev->dev,
2370 "Unable to request IRQ %d (error %d)\n",
2371 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002372 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002373 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002374
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002375 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002376 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002377 }
2378
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002379 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002380 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002381
Nicolas Ferre4df95132013-06-04 21:57:12 +00002382 /* setup appropriated routines according to adapter type */
2383 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002384 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002385 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2386 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2387 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2388 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002389 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002390 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002391 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002392 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2393 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2394 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2395 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002396 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002397 }
2398
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002399 /* Set features */
2400 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002401 /* Checksum offload is only available on gem with packet buffer */
2402 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002403 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002404 if (bp->caps & MACB_CAPS_SG_DISABLED)
2405 dev->hw_features &= ~NETIF_F_SG;
2406 dev->features = dev->hw_features;
2407
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002408 val = 0;
2409 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2410 val = GEM_BIT(RGMII);
2411 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
2412 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2413 val = MACB_BIT(RMII);
2414 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2415 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002416
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002417 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2418 val |= MACB_BIT(CLKEN);
2419
2420 macb_or_gem_writel(bp, USRIO, val);
2421
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002422 /* Set MII management clock divider */
2423 val = macb_mdc_clk_div(bp);
2424 val |= macb_dbw(bp);
2425 macb_writel(bp, NCFGR, val);
2426
2427 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002428}
2429
2430#if defined(CONFIG_OF)
2431/* 1518 rounded up */
2432#define AT91ETHER_MAX_RBUFF_SZ 0x600
2433/* max number of receive buffers */
2434#define AT91ETHER_MAX_RX_DESCR 9
2435
2436/* Initialize and start the Receiver and Transmit subsystems */
2437static int at91ether_start(struct net_device *dev)
2438{
2439 struct macb *lp = netdev_priv(dev);
2440 dma_addr_t addr;
2441 u32 ctl;
2442 int i;
2443
2444 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2445 (AT91ETHER_MAX_RX_DESCR *
2446 sizeof(struct macb_dma_desc)),
2447 &lp->rx_ring_dma, GFP_KERNEL);
2448 if (!lp->rx_ring)
2449 return -ENOMEM;
2450
2451 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2452 AT91ETHER_MAX_RX_DESCR *
2453 AT91ETHER_MAX_RBUFF_SZ,
2454 &lp->rx_buffers_dma, GFP_KERNEL);
2455 if (!lp->rx_buffers) {
2456 dma_free_coherent(&lp->pdev->dev,
2457 AT91ETHER_MAX_RX_DESCR *
2458 sizeof(struct macb_dma_desc),
2459 lp->rx_ring, lp->rx_ring_dma);
2460 lp->rx_ring = NULL;
2461 return -ENOMEM;
2462 }
2463
2464 addr = lp->rx_buffers_dma;
2465 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2466 lp->rx_ring[i].addr = addr;
2467 lp->rx_ring[i].ctrl = 0;
2468 addr += AT91ETHER_MAX_RBUFF_SZ;
2469 }
2470
2471 /* Set the Wrap bit on the last descriptor */
2472 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2473
2474 /* Reset buffer index */
2475 lp->rx_tail = 0;
2476
2477 /* Program address of descriptor list in Rx Buffer Queue register */
2478 macb_writel(lp, RBQP, lp->rx_ring_dma);
2479
2480 /* Enable Receive and Transmit */
2481 ctl = macb_readl(lp, NCR);
2482 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2483
2484 return 0;
2485}
2486
2487/* Open the ethernet interface */
2488static int at91ether_open(struct net_device *dev)
2489{
2490 struct macb *lp = netdev_priv(dev);
2491 u32 ctl;
2492 int ret;
2493
2494 /* Clear internal statistics */
2495 ctl = macb_readl(lp, NCR);
2496 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2497
2498 macb_set_hwaddr(lp);
2499
2500 ret = at91ether_start(dev);
2501 if (ret)
2502 return ret;
2503
2504 /* Enable MAC interrupts */
2505 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2506 MACB_BIT(RXUBR) |
2507 MACB_BIT(ISR_TUND) |
2508 MACB_BIT(ISR_RLE) |
2509 MACB_BIT(TCOMP) |
2510 MACB_BIT(ISR_ROVR) |
2511 MACB_BIT(HRESP));
2512
2513 /* schedule a link state check */
2514 phy_start(lp->phy_dev);
2515
2516 netif_start_queue(dev);
2517
2518 return 0;
2519}
2520
2521/* Close the interface */
2522static int at91ether_close(struct net_device *dev)
2523{
2524 struct macb *lp = netdev_priv(dev);
2525 u32 ctl;
2526
2527 /* Disable Receiver and Transmitter */
2528 ctl = macb_readl(lp, NCR);
2529 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2530
2531 /* Disable MAC interrupts */
2532 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2533 MACB_BIT(RXUBR) |
2534 MACB_BIT(ISR_TUND) |
2535 MACB_BIT(ISR_RLE) |
2536 MACB_BIT(TCOMP) |
2537 MACB_BIT(ISR_ROVR) |
2538 MACB_BIT(HRESP));
2539
2540 netif_stop_queue(dev);
2541
2542 dma_free_coherent(&lp->pdev->dev,
2543 AT91ETHER_MAX_RX_DESCR *
2544 sizeof(struct macb_dma_desc),
2545 lp->rx_ring, lp->rx_ring_dma);
2546 lp->rx_ring = NULL;
2547
2548 dma_free_coherent(&lp->pdev->dev,
2549 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2550 lp->rx_buffers, lp->rx_buffers_dma);
2551 lp->rx_buffers = NULL;
2552
2553 return 0;
2554}
2555
2556/* Transmit packet */
2557static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2558{
2559 struct macb *lp = netdev_priv(dev);
2560
2561 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2562 netif_stop_queue(dev);
2563
2564 /* Store packet information (to free when Tx completed) */
2565 lp->skb = skb;
2566 lp->skb_length = skb->len;
2567 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2568 DMA_TO_DEVICE);
2569
2570 /* Set address of the data in the Transmit Address register */
2571 macb_writel(lp, TAR, lp->skb_physaddr);
2572 /* Set length of the packet in the Transmit Control register */
2573 macb_writel(lp, TCR, skb->len);
2574
2575 } else {
2576 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2577 return NETDEV_TX_BUSY;
2578 }
2579
2580 return NETDEV_TX_OK;
2581}
2582
2583/* Extract received frame from buffer descriptors and sent to upper layers.
2584 * (Called from interrupt context)
2585 */
2586static void at91ether_rx(struct net_device *dev)
2587{
2588 struct macb *lp = netdev_priv(dev);
2589 unsigned char *p_recv;
2590 struct sk_buff *skb;
2591 unsigned int pktlen;
2592
2593 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2594 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2595 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2596 skb = netdev_alloc_skb(dev, pktlen + 2);
2597 if (skb) {
2598 skb_reserve(skb, 2);
2599 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2600
2601 skb->protocol = eth_type_trans(skb, dev);
2602 lp->stats.rx_packets++;
2603 lp->stats.rx_bytes += pktlen;
2604 netif_rx(skb);
2605 } else {
2606 lp->stats.rx_dropped++;
2607 }
2608
2609 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2610 lp->stats.multicast++;
2611
2612 /* reset ownership bit */
2613 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2614
2615 /* wrap after last buffer */
2616 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2617 lp->rx_tail = 0;
2618 else
2619 lp->rx_tail++;
2620 }
2621}
2622
2623/* MAC interrupt handler */
2624static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2625{
2626 struct net_device *dev = dev_id;
2627 struct macb *lp = netdev_priv(dev);
2628 u32 intstatus, ctl;
2629
2630 /* MAC Interrupt Status register indicates what interrupts are pending.
2631 * It is automatically cleared once read.
2632 */
2633 intstatus = macb_readl(lp, ISR);
2634
2635 /* Receive complete */
2636 if (intstatus & MACB_BIT(RCOMP))
2637 at91ether_rx(dev);
2638
2639 /* Transmit complete */
2640 if (intstatus & MACB_BIT(TCOMP)) {
2641 /* The TCOM bit is set even if the transmission failed */
2642 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2643 lp->stats.tx_errors++;
2644
2645 if (lp->skb) {
2646 dev_kfree_skb_irq(lp->skb);
2647 lp->skb = NULL;
2648 dma_unmap_single(NULL, lp->skb_physaddr,
2649 lp->skb_length, DMA_TO_DEVICE);
2650 lp->stats.tx_packets++;
2651 lp->stats.tx_bytes += lp->skb_length;
2652 }
2653 netif_wake_queue(dev);
2654 }
2655
2656 /* Work-around for EMAC Errata section 41.3.1 */
2657 if (intstatus & MACB_BIT(RXUBR)) {
2658 ctl = macb_readl(lp, NCR);
2659 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2660 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2661 }
2662
2663 if (intstatus & MACB_BIT(ISR_ROVR))
2664 netdev_err(dev, "ROVR error\n");
2665
2666 return IRQ_HANDLED;
2667}
2668
2669#ifdef CONFIG_NET_POLL_CONTROLLER
2670static void at91ether_poll_controller(struct net_device *dev)
2671{
2672 unsigned long flags;
2673
2674 local_irq_save(flags);
2675 at91ether_interrupt(dev->irq, dev);
2676 local_irq_restore(flags);
2677}
2678#endif
2679
2680static const struct net_device_ops at91ether_netdev_ops = {
2681 .ndo_open = at91ether_open,
2682 .ndo_stop = at91ether_close,
2683 .ndo_start_xmit = at91ether_start_xmit,
2684 .ndo_get_stats = macb_get_stats,
2685 .ndo_set_rx_mode = macb_set_rx_mode,
2686 .ndo_set_mac_address = eth_mac_addr,
2687 .ndo_do_ioctl = macb_ioctl,
2688 .ndo_validate_addr = eth_validate_addr,
2689 .ndo_change_mtu = eth_change_mtu,
2690#ifdef CONFIG_NET_POLL_CONTROLLER
2691 .ndo_poll_controller = at91ether_poll_controller,
2692#endif
2693};
2694
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002695static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
2696 struct clk **hclk, struct clk **tx_clk)
2697{
2698 int err;
2699
2700 *hclk = NULL;
2701 *tx_clk = NULL;
2702
2703 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2704 if (IS_ERR(*pclk))
2705 return PTR_ERR(*pclk);
2706
2707 err = clk_prepare_enable(*pclk);
2708 if (err) {
2709 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2710 return err;
2711 }
2712
2713 return 0;
2714}
2715
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002716static int at91ether_init(struct platform_device *pdev)
2717{
2718 struct net_device *dev = platform_get_drvdata(pdev);
2719 struct macb *bp = netdev_priv(dev);
2720 int err;
2721 u32 reg;
2722
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002723 dev->netdev_ops = &at91ether_netdev_ops;
2724 dev->ethtool_ops = &macb_ethtool_ops;
2725
2726 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2727 0, dev->name, dev);
2728 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002729 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002730
2731 macb_writel(bp, NCR, 0);
2732
2733 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2734 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2735 reg |= MACB_BIT(RM9200_RMII);
2736
2737 macb_writel(bp, NCFGR, reg);
2738
2739 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002740}
2741
David S. Miller3cef5c52015-03-09 23:38:02 -04002742static const struct macb_config at91sam9260_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002743 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002744 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002745 .init = macb_init,
2746};
2747
David S. Miller3cef5c52015-03-09 23:38:02 -04002748static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002749 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2750 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002751 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002752 .init = macb_init,
2753};
2754
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002755static const struct macb_config sama5d2_config = {
2756 .caps = 0,
2757 .dma_burst_length = 16,
2758 .clk_init = macb_clk_init,
2759 .init = macb_init,
2760};
2761
David S. Miller3cef5c52015-03-09 23:38:02 -04002762static const struct macb_config sama5d3_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002763 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2764 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002765 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002766 .init = macb_init,
2767};
2768
David S. Miller3cef5c52015-03-09 23:38:02 -04002769static const struct macb_config sama5d4_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002770 .caps = 0,
2771 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002772 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002773 .init = macb_init,
2774};
2775
David S. Miller3cef5c52015-03-09 23:38:02 -04002776static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002777 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002778 .init = at91ether_init,
2779};
2780
David S. Miller36583eb2015-05-23 01:22:35 -04002781
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302782static const struct macb_config zynqmp_config = {
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302783 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
2784 MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302785 .dma_burst_length = 16,
2786 .clk_init = macb_clk_init,
2787 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302788 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302789};
2790
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002791static const struct macb_config zynq_config = {
2792 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
2793 MACB_CAPS_NO_GIGABIT_HALF,
2794 .dma_burst_length = 16,
2795 .clk_init = macb_clk_init,
2796 .init = macb_init,
2797};
2798
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002799static const struct of_device_id macb_dt_ids[] = {
2800 { .compatible = "cdns,at32ap7000-macb" },
2801 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2802 { .compatible = "cdns,macb" },
2803 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2804 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002805 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002806 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2807 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2808 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2809 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302810 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002811 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002812 { /* sentinel */ }
2813};
2814MODULE_DEVICE_TABLE(of, macb_dt_ids);
2815#endif /* CONFIG_OF */
2816
2817static int macb_probe(struct platform_device *pdev)
2818{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002819 int (*clk_init)(struct platform_device *, struct clk **,
2820 struct clk **, struct clk **)
2821 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002822 int (*init)(struct platform_device *) = macb_init;
2823 struct device_node *np = pdev->dev.of_node;
2824 const struct macb_config *macb_config = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002825 struct clk *pclk, *hclk, *tx_clk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002826 unsigned int queue_mask, num_queues;
2827 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002828 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002829 struct phy_device *phydev;
2830 struct net_device *dev;
2831 struct resource *regs;
2832 void __iomem *mem;
2833 const char *mac;
2834 struct macb *bp;
2835 int err;
2836
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002837 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2838 mem = devm_ioremap_resource(&pdev->dev, regs);
2839 if (IS_ERR(mem))
2840 return PTR_ERR(mem);
2841
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002842 if (np) {
2843 const struct of_device_id *match;
2844
2845 match = of_match_node(macb_dt_ids, np);
2846 if (match && match->data) {
2847 macb_config = match->data;
2848 clk_init = macb_config->clk_init;
2849 init = macb_config->init;
2850 }
2851 }
2852
2853 err = clk_init(pdev, &pclk, &hclk, &tx_clk);
2854 if (err)
2855 return err;
2856
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002857 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002858
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002859 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002860 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002861 if (!dev) {
2862 err = -ENOMEM;
2863 goto err_disable_clocks;
2864 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002865
2866 dev->base_addr = regs->start;
2867
2868 SET_NETDEV_DEV(dev, &pdev->dev);
2869
2870 bp = netdev_priv(dev);
2871 bp->pdev = pdev;
2872 bp->dev = dev;
2873 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002874 bp->native_io = native_io;
2875 if (native_io) {
2876 bp->readl = hw_readl_native;
2877 bp->writel = hw_writel_native;
2878 } else {
2879 bp->readl = hw_readl;
2880 bp->writel = hw_writel;
2881 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002882 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002883 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002884 if (macb_config)
2885 bp->dma_burst_length = macb_config->dma_burst_length;
2886 bp->pclk = pclk;
2887 bp->hclk = hclk;
2888 bp->tx_clk = tx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03002889 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302890 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302891
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002892 spin_lock_init(&bp->lock);
2893
Nicolas Ferread783472015-03-31 15:02:02 +02002894 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02002895 macb_configure_caps(bp, macb_config);
2896
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002897 platform_set_drvdata(pdev, dev);
2898
2899 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002900 if (dev->irq < 0) {
2901 err = dev->irq;
2902 goto err_disable_clocks;
2903 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002904
2905 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00002906 if (mac)
2907 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2908 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002909 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002910
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002911 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002912 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002913 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002914 if (pdata && pdata->is_rmii)
2915 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2916 else
2917 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2918 } else {
2919 bp->phy_interface = err;
2920 }
2921
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002922 /* IP specific init */
2923 err = init(pdev);
2924 if (err)
2925 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002926
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002927 err = register_netdev(dev);
2928 if (err) {
2929 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002930 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002931 }
2932
Nicolas Ferre72ca8202013-04-14 22:04:33 +00002933 err = macb_mii_init(bp);
2934 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02002935 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002936
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002937 netif_carrier_off(dev);
2938
Bo Shen58798232014-09-13 01:57:49 +02002939 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
2940 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
2941 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002942
frederic RODO6c36a702007-07-12 19:07:24 +02002943 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002944 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2945 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002946
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002947 return 0;
2948
frederic RODO6c36a702007-07-12 19:07:24 +02002949err_out_unregister_netdev:
2950 unregister_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002951
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002952err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002953 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002954
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002955err_disable_clocks:
2956 clk_disable_unprepare(tx_clk);
2957 clk_disable_unprepare(hclk);
2958 clk_disable_unprepare(pclk);
2959
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002960 return err;
2961}
2962
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002963static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002964{
2965 struct net_device *dev;
2966 struct macb *bp;
2967
2968 dev = platform_get_drvdata(pdev);
2969
2970 if (dev) {
2971 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002972 if (bp->phy_dev)
2973 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002974 mdiobus_unregister(bp->mii_bus);
2975 kfree(bp->mii_bus->irq);
2976 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002977 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002978 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002979 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002980 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01002981 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002982 }
2983
2984 return 0;
2985}
2986
Michal Simekd23823d2015-01-23 09:36:03 +01002987static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002988{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002989 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002990 struct net_device *netdev = platform_get_drvdata(pdev);
2991 struct macb *bp = netdev_priv(netdev);
2992
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002993 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002994 netif_device_detach(netdev);
2995
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002996 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002997 clk_disable_unprepare(bp->hclk);
2998 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002999
3000 return 0;
3001}
3002
Michal Simekd23823d2015-01-23 09:36:03 +01003003static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003004{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003005 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003006 struct net_device *netdev = platform_get_drvdata(pdev);
3007 struct macb *bp = netdev_priv(netdev);
3008
Steffen Trumtrarace58012013-03-27 23:07:07 +00003009 clk_prepare_enable(bp->pclk);
3010 clk_prepare_enable(bp->hclk);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003011 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003012
3013 netif_device_attach(netdev);
3014
3015 return 0;
3016}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003017
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003018static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3019
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003020static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003021 .probe = macb_probe,
3022 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003023 .driver = {
3024 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003025 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003026 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003027 },
3028};
3029
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003030module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003031
3032MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003033MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003034MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003035MODULE_ALIAS("platform:macb");