blob: 8b45bc9ac29e848f392ff343f793c6200e4547b9 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010022#include <linux/gpio/consumer.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000027#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020029#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080030#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010031#include <linux/of_device.h>
Gregory CLEMENT270c4992015-12-17 10:51:04 +010032#include <linux/of_gpio.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020033#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010034#include <linux/of_net.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010035
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010036#include "macb.h"
37
Nicolas Ferre1b447912013-06-04 21:57:11 +000038#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000039#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000040#define RX_RING_SIZE 512 /* must be power of 2 */
41#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010042
Havard Skinnemoen55054a12012-10-31 06:04:55 +000043#define TX_RING_SIZE 128 /* must be power of 2 */
44#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010045
Nicolas Ferre909a8582012-11-19 06:00:21 +000046/* level of occupied TX descriptors under which we wake up TX process */
47#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010048
49#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
50 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000051#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
52 | MACB_BIT(ISR_RLE) \
53 | MACB_BIT(TXERR))
54#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
55
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020056#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
57#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
58
Harini Katakama5898ea2015-05-06 22:27:18 +053059#define GEM_MTU_MIN_SIZE 68
60
Nicolas Ferree86cd532012-10-31 06:04:57 +000061/*
62 * Graceful stop timeouts in us. We should allow up to
63 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
64 */
65#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010066
Havard Skinnemoen55054a12012-10-31 06:04:55 +000067/* Ring buffer accessors */
68static unsigned int macb_tx_ring_wrap(unsigned int index)
69{
70 return index & (TX_RING_SIZE - 1);
71}
72
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010073static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
74 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000075{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010076 return &queue->tx_ring[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000077}
78
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010079static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
80 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000081{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010082 return &queue->tx_skb[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000083}
84
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010085static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000086{
87 dma_addr_t offset;
88
89 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
90
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010091 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +000092}
93
94static unsigned int macb_rx_ring_wrap(unsigned int index)
95{
96 return index & (RX_RING_SIZE - 1);
97}
98
99static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
100{
101 return &bp->rx_ring[macb_rx_ring_wrap(index)];
102}
103
104static void *macb_rx_buffer(struct macb *bp, unsigned int index)
105{
Nicolas Ferre1b447912013-06-04 21:57:11 +0000106 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000107}
108
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300109/* I/O accessors */
110static u32 hw_readl_native(struct macb *bp, int offset)
111{
112 return __raw_readl(bp->regs + offset);
113}
114
115static void hw_writel_native(struct macb *bp, int offset, u32 value)
116{
117 __raw_writel(value, bp->regs + offset);
118}
119
120static u32 hw_readl(struct macb *bp, int offset)
121{
122 return readl_relaxed(bp->regs + offset);
123}
124
125static void hw_writel(struct macb *bp, int offset, u32 value)
126{
127 writel_relaxed(value, bp->regs + offset);
128}
129
130/*
131 * Find the CPU endianness by using the loopback bit of NCR register. When the
132 * CPU is in big endian we need to program swaped mode for management
133 * descriptor access.
134 */
135static bool hw_is_native_io(void __iomem *addr)
136{
137 u32 value = MACB_BIT(LLB);
138
139 __raw_writel(value, addr + MACB_NCR);
140 value = __raw_readl(addr + MACB_NCR);
141
142 /* Write 0 back to disable everything */
143 __raw_writel(0, addr + MACB_NCR);
144
145 return value == MACB_BIT(LLB);
146}
147
148static bool hw_is_gem(void __iomem *addr, bool native_io)
149{
150 u32 id;
151
152 if (native_io)
153 id = __raw_readl(addr + MACB_MID);
154 else
155 id = readl_relaxed(addr + MACB_MID);
156
157 return MACB_BFEXT(IDNUM, id) >= 0x2;
158}
159
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100160static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100161{
162 u32 bottom;
163 u16 top;
164
165 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000166 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100167 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000168 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000169
170 /* Clear unused address register sets */
171 macb_or_gem_writel(bp, SA2B, 0);
172 macb_or_gem_writel(bp, SA2T, 0);
173 macb_or_gem_writel(bp, SA3B, 0);
174 macb_or_gem_writel(bp, SA3T, 0);
175 macb_or_gem_writel(bp, SA4B, 0);
176 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100177}
178
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100179static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100180{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000181 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100182 u32 bottom;
183 u16 top;
184 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000185 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100186
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900187 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000188
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000189 /* Check all 4 address register for vaild address */
190 for (i = 0; i < 4; i++) {
191 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
192 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100193
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000194 if (pdata && pdata->rev_eth_addr) {
195 addr[5] = bottom & 0xff;
196 addr[4] = (bottom >> 8) & 0xff;
197 addr[3] = (bottom >> 16) & 0xff;
198 addr[2] = (bottom >> 24) & 0xff;
199 addr[1] = top & 0xff;
200 addr[0] = (top & 0xff00) >> 8;
201 } else {
202 addr[0] = bottom & 0xff;
203 addr[1] = (bottom >> 8) & 0xff;
204 addr[2] = (bottom >> 16) & 0xff;
205 addr[3] = (bottom >> 24) & 0xff;
206 addr[4] = top & 0xff;
207 addr[5] = (top >> 8) & 0xff;
208 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100209
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000210 if (is_valid_ether_addr(addr)) {
211 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
212 return;
213 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700214 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000215
Andy Shevchenkoa35919e2015-07-24 21:24:01 +0300216 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000217 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100218}
219
frederic RODO6c36a702007-07-12 19:07:24 +0200220static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100221{
frederic RODO6c36a702007-07-12 19:07:24 +0200222 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100223 int value;
224
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100225 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
226 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200227 | MACB_BF(PHYA, mii_id)
228 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100229 | MACB_BF(CODE, MACB_MAN_CODE)));
230
frederic RODO6c36a702007-07-12 19:07:24 +0200231 /* wait for end of transfer */
232 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
233 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100234
235 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100236
237 return value;
238}
239
frederic RODO6c36a702007-07-12 19:07:24 +0200240static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
241 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100242{
frederic RODO6c36a702007-07-12 19:07:24 +0200243 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100244
245 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
246 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200247 | MACB_BF(PHYA, mii_id)
248 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100249 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200250 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100251
frederic RODO6c36a702007-07-12 19:07:24 +0200252 /* wait for end of transfer */
253 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
254 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100255
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100256 return 0;
257}
258
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800259/**
260 * macb_set_tx_clk() - Set a clock to a new frequency
261 * @clk Pointer to the clock to change
262 * @rate New frequency in Hz
263 * @dev Pointer to the struct net_device
264 */
265static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
266{
267 long ferr, rate, rate_rounded;
268
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100269 if (!clk)
270 return;
271
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800272 switch (speed) {
273 case SPEED_10:
274 rate = 2500000;
275 break;
276 case SPEED_100:
277 rate = 25000000;
278 break;
279 case SPEED_1000:
280 rate = 125000000;
281 break;
282 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800283 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800284 }
285
286 rate_rounded = clk_round_rate(clk, rate);
287 if (rate_rounded < 0)
288 return;
289
290 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
291 * is not satisfied.
292 */
293 ferr = abs(rate_rounded - rate);
294 ferr = DIV_ROUND_UP(ferr, rate / 100000);
295 if (ferr > 5)
296 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
297 rate);
298
299 if (clk_set_rate(clk, rate_rounded))
300 netdev_err(dev, "adjusting tx_clk failed.\n");
301}
302
frederic RODO6c36a702007-07-12 19:07:24 +0200303static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100304{
frederic RODO6c36a702007-07-12 19:07:24 +0200305 struct macb *bp = netdev_priv(dev);
306 struct phy_device *phydev = bp->phy_dev;
307 unsigned long flags;
frederic RODO6c36a702007-07-12 19:07:24 +0200308 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100309
frederic RODO6c36a702007-07-12 19:07:24 +0200310 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100311
frederic RODO6c36a702007-07-12 19:07:24 +0200312 if (phydev->link) {
313 if ((bp->speed != phydev->speed) ||
314 (bp->duplex != phydev->duplex)) {
315 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100316
frederic RODO6c36a702007-07-12 19:07:24 +0200317 reg = macb_readl(bp, NCFGR);
318 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000319 if (macb_is_gem(bp))
320 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200321
322 if (phydev->duplex)
323 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900324 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200325 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200326 if (phydev->speed == SPEED_1000 &&
327 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000328 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200329
Patrice Vilchez140b7552012-10-31 06:04:50 +0000330 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200331
332 bp->speed = phydev->speed;
333 bp->duplex = phydev->duplex;
334 status_change = 1;
335 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100336 }
337
frederic RODO6c36a702007-07-12 19:07:24 +0200338 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700339 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200340 bp->speed = 0;
341 bp->duplex = -1;
342 }
343 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100344
frederic RODO6c36a702007-07-12 19:07:24 +0200345 status_change = 1;
346 }
347
348 spin_unlock_irqrestore(&bp->lock, flags);
349
350 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000351 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500352 /* Update the TX clock rate if and only if the link is
353 * up and there has been a link change.
354 */
355 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
356
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000357 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000358 netdev_info(dev, "link up (%d/%s)\n",
359 phydev->speed,
360 phydev->duplex == DUPLEX_FULL ?
361 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000362 } else {
363 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000364 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000365 }
frederic RODO6c36a702007-07-12 19:07:24 +0200366 }
367}
368
369/* based on au1000_eth. c*/
370static int macb_mii_probe(struct net_device *dev)
371{
372 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000373 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000374 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000375 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000376 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200377
Jiri Pirko7455a762010-02-08 05:12:08 +0000378 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200379 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000380 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200381 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200382 }
383
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000384 pdata = dev_get_platdata(&bp->pdev->dev);
385 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
386 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
387 if (!ret) {
388 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
389 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
390 }
391 }
frederic RODO6c36a702007-07-12 19:07:24 +0200392
393 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000394 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100395 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000396 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000397 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000398 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200399 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100400
frederic RODO6c36a702007-07-12 19:07:24 +0200401 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200402 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000403 phydev->supported &= PHY_GBIT_FEATURES;
404 else
405 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100406
Nathan Sullivan222ca8e2015-05-22 09:22:10 -0500407 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
408 phydev->supported &= ~SUPPORTED_1000baseT_Half;
409
frederic RODO6c36a702007-07-12 19:07:24 +0200410 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100411
frederic RODO6c36a702007-07-12 19:07:24 +0200412 bp->link = 0;
413 bp->speed = 0;
414 bp->duplex = -1;
415 bp->phy_dev = phydev;
416
417 return 0;
418}
419
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100420static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200421{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000422 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200423 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200424 int err = -ENXIO, i;
425
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200426 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200427 macb_writel(bp, NCR, MACB_BIT(MPE));
428
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700429 bp->mii_bus = mdiobus_alloc();
430 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200431 err = -ENOMEM;
432 goto err_out;
433 }
434
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700435 bp->mii_bus->name = "MACB_mii_bus";
436 bp->mii_bus->read = &macb_mdio_read;
437 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000438 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
439 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700440 bp->mii_bus->priv = bp;
441 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900442 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700443
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700444 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
445 if (!bp->mii_bus->irq) {
446 err = -ENOMEM;
447 goto err_out_free_mdiobus;
448 }
449
Jamie Iles91523942011-02-28 04:05:25 +0000450 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200451
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200452 np = bp->pdev->dev.of_node;
453 if (np) {
454 /* try dt phy registration */
455 err = of_mdiobus_register(bp->mii_bus, np);
456
457 /* fallback to standard phy registration if no phy were
458 found during dt phy registration */
459 if (!err && !phy_find_first(bp->mii_bus)) {
460 for (i = 0; i < PHY_MAX_ADDR; i++) {
461 struct phy_device *phydev;
462
463 phydev = mdiobus_scan(bp->mii_bus, i);
464 if (IS_ERR(phydev)) {
465 err = PTR_ERR(phydev);
466 break;
467 }
468 }
469
470 if (err)
471 goto err_out_unregister_bus;
472 }
473 } else {
474 for (i = 0; i < PHY_MAX_ADDR; i++)
475 bp->mii_bus->irq[i] = PHY_POLL;
476
477 if (pdata)
478 bp->mii_bus->phy_mask = pdata->phy_mask;
479
480 err = mdiobus_register(bp->mii_bus);
481 }
482
483 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200484 goto err_out_free_mdio_irq;
485
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200486 err = macb_mii_probe(bp->dev);
487 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200488 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200489
490 return 0;
491
492err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700493 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200494err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700495 kfree(bp->mii_bus->irq);
496err_out_free_mdiobus:
497 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200498err_out:
499 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100500}
501
502static void macb_update_stats(struct macb *bp)
503{
Jamie Ilesa494ed82011-03-09 16:26:35 +0000504 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
505 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300506 int offset = MACB_PFR;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100507
508 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
509
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +0300510 for(; p < end; p++, offset += 4)
David S. Miller7a6e0702015-07-27 14:24:48 -0700511 *p += bp->macb_reg_readl(bp, offset);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100512}
513
Nicolas Ferree86cd532012-10-31 06:04:57 +0000514static int macb_halt_tx(struct macb *bp)
515{
516 unsigned long halt_time, timeout;
517 u32 status;
518
519 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
520
521 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
522 do {
523 halt_time = jiffies;
524 status = macb_readl(bp, TSR);
525 if (!(status & MACB_BIT(TGO)))
526 return 0;
527
528 usleep_range(10, 250);
529 } while (time_before(halt_time, timeout));
530
531 return -ETIMEDOUT;
532}
533
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200534static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
535{
536 if (tx_skb->mapping) {
537 if (tx_skb->mapped_as_page)
538 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
539 tx_skb->size, DMA_TO_DEVICE);
540 else
541 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
542 tx_skb->size, DMA_TO_DEVICE);
543 tx_skb->mapping = 0;
544 }
545
546 if (tx_skb->skb) {
547 dev_kfree_skb_any(tx_skb->skb);
548 tx_skb->skb = NULL;
549 }
550}
551
Nicolas Ferree86cd532012-10-31 06:04:57 +0000552static void macb_tx_error_task(struct work_struct *work)
553{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100554 struct macb_queue *queue = container_of(work, struct macb_queue,
555 tx_error_task);
556 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000557 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100558 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000559 struct sk_buff *skb;
560 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100561 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000562
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100563 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
564 (unsigned int)(queue - bp->queues),
565 queue->tx_tail, queue->tx_head);
566
567 /* Prevent the queue IRQ handlers from running: each of them may call
568 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
569 * As explained below, we have to halt the transmission before updating
570 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
571 * network engine about the macb/gem being halted.
572 */
573 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000574
575 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100576 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000577
578 /*
579 * Stop transmission now
580 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100581 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000582 */
583 if (macb_halt_tx(bp))
584 /* Just complain for now, reinitializing TX path can be good */
585 netdev_err(bp->dev, "BUG: halt tx timed out\n");
586
Nicolas Ferree86cd532012-10-31 06:04:57 +0000587 /*
588 * Treat frames in TX queue including the ones that caused the error.
589 * Free transmit buffers in upper layer.
590 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100591 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
592 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000593
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100594 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000595 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100596 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000597 skb = tx_skb->skb;
598
599 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200600 /* skb is set for the last buffer of the frame */
601 while (!skb) {
602 macb_tx_unmap(bp, tx_skb);
603 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100604 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200605 skb = tx_skb->skb;
606 }
607
608 /* ctrl still refers to the first buffer descriptor
609 * since it's the only one written back by the hardware
610 */
611 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
612 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
613 macb_tx_ring_wrap(tail), skb->data);
614 bp->stats.tx_packets++;
615 bp->stats.tx_bytes += skb->len;
616 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000617 } else {
618 /*
619 * "Buffers exhausted mid-frame" errors may only happen
620 * if the driver is buggy, so complain loudly about those.
621 * Statistics are updated by hardware.
622 */
623 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
624 netdev_err(bp->dev,
625 "BUG: TX buffers exhausted mid-frame\n");
626
627 desc->ctrl = ctrl | MACB_BIT(TX_USED);
628 }
629
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200630 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000631 }
632
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100633 /* Set end of TX queue */
634 desc = macb_tx_desc(queue, 0);
635 desc->addr = 0;
636 desc->ctrl = MACB_BIT(TX_USED);
637
Nicolas Ferree86cd532012-10-31 06:04:57 +0000638 /* Make descriptor updates visible to hardware */
639 wmb();
640
641 /* Reinitialize the TX desc queue */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100642 queue_writel(queue, TBQP, queue->tx_ring_dma);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000643 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100644 queue->tx_head = 0;
645 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000646
647 /* Housework before enabling TX IRQ */
648 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100649 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
650
651 /* Now we are ready to start transmission again */
652 netif_tx_start_all_queues(bp->dev);
653 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
654
655 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000656}
657
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100658static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100659{
660 unsigned int tail;
661 unsigned int head;
662 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100663 struct macb *bp = queue->bp;
664 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100665
666 status = macb_readl(bp, TSR);
667 macb_writel(bp, TSR, status);
668
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000669 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100670 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000671
Nicolas Ferree86cd532012-10-31 06:04:57 +0000672 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
673 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100675 head = queue->tx_head;
676 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000677 struct macb_tx_skb *tx_skb;
678 struct sk_buff *skb;
679 struct macb_dma_desc *desc;
680 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100681
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100682 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100683
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000684 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100685 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000686
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000687 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100688
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200689 /* TX_USED bit is only set by hardware on the very first buffer
690 * descriptor of the transmitted frame.
691 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000692 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100693 break;
694
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200695 /* Process all buffers of the current transmitted frame */
696 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100697 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200698 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000699
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200700 /* First, update TX stats if needed */
701 if (skb) {
702 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
703 macb_tx_ring_wrap(tail), skb->data);
704 bp->stats.tx_packets++;
705 bp->stats.tx_bytes += skb->len;
706 }
707
708 /* Now we can safely release resources */
709 macb_tx_unmap(bp, tx_skb);
710
711 /* skb is set only for the last buffer of the frame.
712 * WARNING: at this point skb has been freed by
713 * macb_tx_unmap().
714 */
715 if (skb)
716 break;
717 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100718 }
719
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100720 queue->tx_tail = tail;
721 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
722 CIRC_CNT(queue->tx_head, queue->tx_tail,
723 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
724 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100725}
726
Nicolas Ferre4df95132013-06-04 21:57:12 +0000727static void gem_rx_refill(struct macb *bp)
728{
729 unsigned int entry;
730 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000731 dma_addr_t paddr;
732
733 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000734 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000735
736 /* Make hw descriptor updates visible to CPU */
737 rmb();
738
Nicolas Ferre4df95132013-06-04 21:57:12 +0000739 bp->rx_prepared_head++;
740
Nicolas Ferre4df95132013-06-04 21:57:12 +0000741 if (bp->rx_skbuff[entry] == NULL) {
742 /* allocate sk_buff for this free entry in ring */
743 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
744 if (unlikely(skb == NULL)) {
745 netdev_err(bp->dev,
746 "Unable to allocate sk_buff\n");
747 break;
748 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000749
750 /* now fill corresponding descriptor entry */
751 paddr = dma_map_single(&bp->pdev->dev, skb->data,
752 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800753 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
754 dev_kfree_skb(skb);
755 break;
756 }
757
758 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000759
760 if (entry == RX_RING_SIZE - 1)
761 paddr |= MACB_BIT(RX_WRAP);
762 bp->rx_ring[entry].addr = paddr;
763 bp->rx_ring[entry].ctrl = 0;
764
765 /* properly align Ethernet header */
766 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530767 } else {
768 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
769 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000770 }
771 }
772
773 /* Make descriptor updates visible to hardware */
774 wmb();
775
776 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
777 bp->rx_prepared_head, bp->rx_tail);
778}
779
780/* Mark DMA descriptors from begin up to and not including end as unused */
781static void discard_partial_frame(struct macb *bp, unsigned int begin,
782 unsigned int end)
783{
784 unsigned int frag;
785
786 for (frag = begin; frag != end; frag++) {
787 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
788 desc->addr &= ~MACB_BIT(RX_USED);
789 }
790
791 /* Make descriptor updates visible to hardware */
792 wmb();
793
794 /*
795 * When this happens, the hardware stats registers for
796 * whatever caused this is updated, so we don't have to record
797 * anything.
798 */
799}
800
801static int gem_rx(struct macb *bp, int budget)
802{
803 unsigned int len;
804 unsigned int entry;
805 struct sk_buff *skb;
806 struct macb_dma_desc *desc;
807 int count = 0;
808
809 while (count < budget) {
810 u32 addr, ctrl;
811
812 entry = macb_rx_ring_wrap(bp->rx_tail);
813 desc = &bp->rx_ring[entry];
814
815 /* Make hw descriptor updates visible to CPU */
816 rmb();
817
818 addr = desc->addr;
819 ctrl = desc->ctrl;
820
821 if (!(addr & MACB_BIT(RX_USED)))
822 break;
823
Nicolas Ferre4df95132013-06-04 21:57:12 +0000824 bp->rx_tail++;
825 count++;
826
827 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
828 netdev_err(bp->dev,
829 "not whole frame pointed by descriptor\n");
830 bp->stats.rx_dropped++;
831 break;
832 }
833 skb = bp->rx_skbuff[entry];
834 if (unlikely(!skb)) {
835 netdev_err(bp->dev,
836 "inconsistent Rx descriptor chain\n");
837 bp->stats.rx_dropped++;
838 break;
839 }
840 /* now everything is ready for receiving packet */
841 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530842 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000843
844 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
845
846 skb_put(skb, len);
847 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
848 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800849 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000850
851 skb->protocol = eth_type_trans(skb, bp->dev);
852 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200853 if (bp->dev->features & NETIF_F_RXCSUM &&
854 !(bp->dev->flags & IFF_PROMISC) &&
855 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
856 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000857
858 bp->stats.rx_packets++;
859 bp->stats.rx_bytes += skb->len;
860
861#if defined(DEBUG) && defined(VERBOSE_DEBUG)
862 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
863 skb->len, skb->csum);
864 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100865 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000866 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
867 skb->data, 32, true);
868#endif
869
870 netif_receive_skb(skb);
871 }
872
873 gem_rx_refill(bp);
874
875 return count;
876}
877
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100878static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
879 unsigned int last_frag)
880{
881 unsigned int len;
882 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000883 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100884 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000885 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100886
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000887 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530888 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100889
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000890 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000891 macb_rx_ring_wrap(first_frag),
892 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100893
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000894 /*
895 * The ethernet header starts NET_IP_ALIGN bytes into the
896 * first buffer. Since the header is 14 bytes, this makes the
897 * payload word-aligned.
898 *
899 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
900 * the two padding bytes into the skb so that we avoid hitting
901 * the slowpath in memcpy(), and pull them off afterwards.
902 */
903 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100904 if (!skb) {
905 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000906 for (frag = first_frag; ; frag++) {
907 desc = macb_rx_desc(bp, frag);
908 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100909 if (frag == last_frag)
910 break;
911 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000912
913 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100914 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000915
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100916 return 1;
917 }
918
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000919 offset = 0;
920 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700921 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100922 skb_put(skb, len);
923
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000924 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000925 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100926
927 if (offset + frag_len > len) {
928 BUG_ON(frag != last_frag);
929 frag_len = len - offset;
930 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300931 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000932 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000933 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000934 desc = macb_rx_desc(bp, frag);
935 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100936
937 if (frag == last_frag)
938 break;
939 }
940
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000941 /* Make descriptor updates visible to hardware */
942 wmb();
943
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000944 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100945 skb->protocol = eth_type_trans(skb, bp->dev);
946
947 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000948 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000949 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000950 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100951 netif_receive_skb(skb);
952
953 return 0;
954}
955
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100956static int macb_rx(struct macb *bp, int budget)
957{
958 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000959 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100960 int first_frag = -1;
961
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000962 for (tail = bp->rx_tail; budget > 0; tail++) {
963 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100964 u32 addr, ctrl;
965
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000966 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100967 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000968
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000969 addr = desc->addr;
970 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100971
972 if (!(addr & MACB_BIT(RX_USED)))
973 break;
974
975 if (ctrl & MACB_BIT(RX_SOF)) {
976 if (first_frag != -1)
977 discard_partial_frame(bp, first_frag, tail);
978 first_frag = tail;
979 }
980
981 if (ctrl & MACB_BIT(RX_EOF)) {
982 int dropped;
983 BUG_ON(first_frag == -1);
984
985 dropped = macb_rx_frame(bp, first_frag, tail);
986 first_frag = -1;
987 if (!dropped) {
988 received++;
989 budget--;
990 }
991 }
992 }
993
994 if (first_frag != -1)
995 bp->rx_tail = first_frag;
996 else
997 bp->rx_tail = tail;
998
999 return received;
1000}
1001
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001002static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001003{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001004 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001005 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001006 u32 status;
1007
1008 status = macb_readl(bp, RSR);
1009 macb_writel(bp, RSR, status);
1010
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001011 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001012
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001013 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001014 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001015
Nicolas Ferre4df95132013-06-04 21:57:12 +00001016 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +00001017 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001018 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001019
Nicolas Ferre8770e912013-02-12 11:08:48 +01001020 /* Packets received while interrupts were disabled */
1021 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -07001022 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001023 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1024 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +01001025 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -07001026 } else {
1027 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1028 }
Joshua Hokeb3363692010-10-25 01:44:22 +00001029 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001030
1031 /* TODO: Handle errors */
1032
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001033 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001034}
1035
1036static irqreturn_t macb_interrupt(int irq, void *dev_id)
1037{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001038 struct macb_queue *queue = dev_id;
1039 struct macb *bp = queue->bp;
1040 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001041 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001042
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001043 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001044
1045 if (unlikely(!status))
1046 return IRQ_NONE;
1047
1048 spin_lock(&bp->lock);
1049
1050 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001051 /* close possible race with dev_close */
1052 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001053 queue_writel(queue, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001054 break;
1055 }
1056
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001057 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1058 (unsigned int)(queue - bp->queues),
1059 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001060
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001061 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +00001062 /*
1063 * There's no point taking any more interrupts
1064 * until we have processed the buffers. The
1065 * scheduling call may fail if the poll routine
1066 * is already scheduled, so disable interrupts
1067 * now.
1068 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001069 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001070 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001071 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001072
Ben Hutchings288379f2009-01-19 16:43:59 -08001073 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001074 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001075 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076 }
1077 }
1078
Nicolas Ferree86cd532012-10-31 06:04:57 +00001079 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001080 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1081 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001082
1083 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001084 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001085
Nicolas Ferree86cd532012-10-31 06:04:57 +00001086 break;
1087 }
1088
1089 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001090 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001091
1092 /*
1093 * Link change detection isn't possible with RMII, so we'll
1094 * add that if/when we get our hands on a full-blown MII PHY.
1095 */
1096
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001097 /* There is a hardware issue under heavy load where DMA can
1098 * stop, this causes endless "used buffer descriptor read"
1099 * interrupts but it can be cleared by re-enabling RX. See
1100 * the at91 manual, section 41.3.1 or the Zynq manual
1101 * section 16.7.4 for details.
1102 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001103 if (status & MACB_BIT(RXUBR)) {
1104 ctrl = macb_readl(bp, NCR);
1105 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1106 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1107
1108 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1109 macb_writel(bp, ISR, MACB_BIT(RXUBR));
1110 }
1111
Alexander Steinb19f7f72011-04-13 05:03:24 +00001112 if (status & MACB_BIT(ISR_ROVR)) {
1113 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001114 if (macb_is_gem(bp))
1115 bp->hw_stats.gem.rx_overruns++;
1116 else
1117 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001118
1119 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001120 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001121 }
1122
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001123 if (status & MACB_BIT(HRESP)) {
1124 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001125 * TODO: Reset the hardware, and maybe move the
1126 * netdev_err to a lower-priority context as well
1127 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001128 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001129 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001130
1131 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001132 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001133 }
1134
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001135 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001136 }
1137
1138 spin_unlock(&bp->lock);
1139
1140 return IRQ_HANDLED;
1141}
1142
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001143#ifdef CONFIG_NET_POLL_CONTROLLER
1144/*
1145 * Polling receive - used by netconsole and other diagnostic tools
1146 * to allow network i/o with interrupts disabled.
1147 */
1148static void macb_poll_controller(struct net_device *dev)
1149{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001150 struct macb *bp = netdev_priv(dev);
1151 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001152 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001153 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001154
1155 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001156 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1157 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001158 local_irq_restore(flags);
1159}
1160#endif
1161
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001162static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001163 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001164 struct sk_buff *skb)
1165{
1166 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001167 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001168 struct macb_tx_skb *tx_skb = NULL;
1169 struct macb_dma_desc *desc;
1170 unsigned int offset, size, count = 0;
1171 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1172 unsigned int eof = 1;
1173 u32 ctrl;
1174
1175 /* First, map non-paged data */
1176 len = skb_headlen(skb);
1177 offset = 0;
1178 while (len) {
1179 size = min(len, bp->max_tx_length);
1180 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001181 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001182
1183 mapping = dma_map_single(&bp->pdev->dev,
1184 skb->data + offset,
1185 size, DMA_TO_DEVICE);
1186 if (dma_mapping_error(&bp->pdev->dev, mapping))
1187 goto dma_error;
1188
1189 /* Save info to properly release resources */
1190 tx_skb->skb = NULL;
1191 tx_skb->mapping = mapping;
1192 tx_skb->size = size;
1193 tx_skb->mapped_as_page = false;
1194
1195 len -= size;
1196 offset += size;
1197 count++;
1198 tx_head++;
1199 }
1200
1201 /* Then, map paged data from fragments */
1202 for (f = 0; f < nr_frags; f++) {
1203 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1204
1205 len = skb_frag_size(frag);
1206 offset = 0;
1207 while (len) {
1208 size = min(len, bp->max_tx_length);
1209 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001210 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001211
1212 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1213 offset, size, DMA_TO_DEVICE);
1214 if (dma_mapping_error(&bp->pdev->dev, mapping))
1215 goto dma_error;
1216
1217 /* Save info to properly release resources */
1218 tx_skb->skb = NULL;
1219 tx_skb->mapping = mapping;
1220 tx_skb->size = size;
1221 tx_skb->mapped_as_page = true;
1222
1223 len -= size;
1224 offset += size;
1225 count++;
1226 tx_head++;
1227 }
1228 }
1229
1230 /* Should never happen */
1231 if (unlikely(tx_skb == NULL)) {
1232 netdev_err(bp->dev, "BUG! empty skb!\n");
1233 return 0;
1234 }
1235
1236 /* This is the last buffer of the frame: save socket buffer */
1237 tx_skb->skb = skb;
1238
1239 /* Update TX ring: update buffer descriptors in reverse order
1240 * to avoid race condition
1241 */
1242
1243 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1244 * to set the end of TX queue
1245 */
1246 i = tx_head;
1247 entry = macb_tx_ring_wrap(i);
1248 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001249 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001250 desc->ctrl = ctrl;
1251
1252 do {
1253 i--;
1254 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001255 tx_skb = &queue->tx_skb[entry];
1256 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001257
1258 ctrl = (u32)tx_skb->size;
1259 if (eof) {
1260 ctrl |= MACB_BIT(TX_LAST);
1261 eof = 0;
1262 }
1263 if (unlikely(entry == (TX_RING_SIZE - 1)))
1264 ctrl |= MACB_BIT(TX_WRAP);
1265
1266 /* Set TX buffer descriptor */
1267 desc->addr = tx_skb->mapping;
1268 /* desc->addr must be visible to hardware before clearing
1269 * 'TX_USED' bit in desc->ctrl.
1270 */
1271 wmb();
1272 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001273 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001274
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001275 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001276
1277 return count;
1278
1279dma_error:
1280 netdev_err(bp->dev, "TX DMA map failed\n");
1281
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001282 for (i = queue->tx_head; i != tx_head; i++) {
1283 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001284
1285 macb_tx_unmap(bp, tx_skb);
1286 }
1287
1288 return 0;
1289}
1290
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001291static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1292{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001293 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001294 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001295 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001296 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001297 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001298
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001299#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1300 netdev_vdbg(bp->dev,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001301 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1302 queue_index, skb->len, skb->head, skb->data,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001303 skb_tail_pointer(skb), skb_end_pointer(skb));
1304 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1305 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001306#endif
1307
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001308 /* Count how many TX buffer descriptors are needed to send this
1309 * socket buffer: skb fragments of jumbo frames may need to be
1310 * splitted into many buffer descriptors.
1311 */
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001312 count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001313 nr_frags = skb_shinfo(skb)->nr_frags;
1314 for (f = 0; f < nr_frags; f++) {
1315 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
Andy Shevchenko94b295e2015-07-24 21:24:03 +03001316 count += DIV_ROUND_UP(frag_size, bp->max_tx_length);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001317 }
1318
Dongdong Deng48719532009-08-23 19:49:07 -07001319 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001320
1321 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001322 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1323 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001324 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001325 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001326 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001327 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001328 }
1329
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001330 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001331 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001332 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001333 goto unlock;
1334 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001335
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001336 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001337 wmb();
1338
Richard Cochrane0720922011-06-19 21:51:28 +00001339 skb_tx_timestamp(skb);
1340
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001341 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1342
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001343 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1344 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001345
Soren Brinkmann92030902014-03-04 08:46:39 -08001346unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001347 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001348
Patrick McHardy6ed10652009-06-23 06:03:08 +00001349 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001350}
1351
Nicolas Ferre4df95132013-06-04 21:57:12 +00001352static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001353{
1354 if (!macb_is_gem(bp)) {
1355 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1356 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001357 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001358
Nicolas Ferre1b447912013-06-04 21:57:11 +00001359 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001360 netdev_dbg(bp->dev,
1361 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001362 RX_BUFFER_MULTIPLE);
1363 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001364 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001365 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001366 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001367
1368 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1369 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001370}
1371
Nicolas Ferre4df95132013-06-04 21:57:12 +00001372static void gem_free_rx_buffers(struct macb *bp)
1373{
1374 struct sk_buff *skb;
1375 struct macb_dma_desc *desc;
1376 dma_addr_t addr;
1377 int i;
1378
1379 if (!bp->rx_skbuff)
1380 return;
1381
1382 for (i = 0; i < RX_RING_SIZE; i++) {
1383 skb = bp->rx_skbuff[i];
1384
1385 if (skb == NULL)
1386 continue;
1387
1388 desc = &bp->rx_ring[i];
1389 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001390 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001391 DMA_FROM_DEVICE);
1392 dev_kfree_skb_any(skb);
1393 skb = NULL;
1394 }
1395
1396 kfree(bp->rx_skbuff);
1397 bp->rx_skbuff = NULL;
1398}
1399
1400static void macb_free_rx_buffers(struct macb *bp)
1401{
1402 if (bp->rx_buffers) {
1403 dma_free_coherent(&bp->pdev->dev,
1404 RX_RING_SIZE * bp->rx_buffer_size,
1405 bp->rx_buffers, bp->rx_buffers_dma);
1406 bp->rx_buffers = NULL;
1407 }
1408}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001409
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001410static void macb_free_consistent(struct macb *bp)
1411{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001412 struct macb_queue *queue;
1413 unsigned int q;
1414
Nicolas Ferre4df95132013-06-04 21:57:12 +00001415 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001416 if (bp->rx_ring) {
1417 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1418 bp->rx_ring, bp->rx_ring_dma);
1419 bp->rx_ring = NULL;
1420 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001421
1422 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1423 kfree(queue->tx_skb);
1424 queue->tx_skb = NULL;
1425 if (queue->tx_ring) {
1426 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1427 queue->tx_ring, queue->tx_ring_dma);
1428 queue->tx_ring = NULL;
1429 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001430 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001431}
1432
1433static int gem_alloc_rx_buffers(struct macb *bp)
1434{
1435 int size;
1436
1437 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1438 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1439 if (!bp->rx_skbuff)
1440 return -ENOMEM;
1441 else
1442 netdev_dbg(bp->dev,
1443 "Allocated %d RX struct sk_buff entries at %p\n",
1444 RX_RING_SIZE, bp->rx_skbuff);
1445 return 0;
1446}
1447
1448static int macb_alloc_rx_buffers(struct macb *bp)
1449{
1450 int size;
1451
1452 size = RX_RING_SIZE * bp->rx_buffer_size;
1453 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1454 &bp->rx_buffers_dma, GFP_KERNEL);
1455 if (!bp->rx_buffers)
1456 return -ENOMEM;
1457 else
1458 netdev_dbg(bp->dev,
1459 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1460 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1461 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001462}
1463
1464static int macb_alloc_consistent(struct macb *bp)
1465{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001466 struct macb_queue *queue;
1467 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001468 int size;
1469
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001470 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1471 size = TX_RING_BYTES;
1472 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1473 &queue->tx_ring_dma,
1474 GFP_KERNEL);
1475 if (!queue->tx_ring)
1476 goto out_err;
1477 netdev_dbg(bp->dev,
1478 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1479 q, size, (unsigned long)queue->tx_ring_dma,
1480 queue->tx_ring);
1481
1482 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1483 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1484 if (!queue->tx_skb)
1485 goto out_err;
1486 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001487
1488 size = RX_RING_BYTES;
1489 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1490 &bp->rx_ring_dma, GFP_KERNEL);
1491 if (!bp->rx_ring)
1492 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001493 netdev_dbg(bp->dev,
1494 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1495 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001496
Nicolas Ferre4df95132013-06-04 21:57:12 +00001497 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001498 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001499
1500 return 0;
1501
1502out_err:
1503 macb_free_consistent(bp);
1504 return -ENOMEM;
1505}
1506
Nicolas Ferre4df95132013-06-04 21:57:12 +00001507static void gem_init_rings(struct macb *bp)
1508{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001509 struct macb_queue *queue;
1510 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001511 int i;
1512
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001513 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1514 for (i = 0; i < TX_RING_SIZE; i++) {
1515 queue->tx_ring[i].addr = 0;
1516 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1517 }
1518 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1519 queue->tx_head = 0;
1520 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001521 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001522
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001523 bp->rx_tail = 0;
1524 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001525
1526 gem_rx_refill(bp);
1527}
1528
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001529static void macb_init_rings(struct macb *bp)
1530{
1531 int i;
1532 dma_addr_t addr;
1533
1534 addr = bp->rx_buffers_dma;
1535 for (i = 0; i < RX_RING_SIZE; i++) {
1536 bp->rx_ring[i].addr = addr;
1537 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001538 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001539 }
1540 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1541
1542 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001543 bp->queues[0].tx_ring[i].addr = 0;
1544 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001545 }
Ben Shelton21d35152015-04-22 17:28:54 -05001546 bp->queues[0].tx_head = 0;
1547 bp->queues[0].tx_tail = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001548 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001549
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001550 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001551}
1552
1553static void macb_reset_hw(struct macb *bp)
1554{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001555 struct macb_queue *queue;
1556 unsigned int q;
1557
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001558 /*
1559 * Disable RX and TX (XXX: Should we halt the transmission
1560 * more gracefully?)
1561 */
1562 macb_writel(bp, NCR, 0);
1563
1564 /* Clear the stats registers (XXX: Update stats first?) */
1565 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1566
1567 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001568 macb_writel(bp, TSR, -1);
1569 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001570
1571 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001572 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1573 queue_writel(queue, IDR, -1);
1574 queue_readl(queue, ISR);
1575 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001576}
1577
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001578static u32 gem_mdc_clk_div(struct macb *bp)
1579{
1580 u32 config;
1581 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1582
1583 if (pclk_hz <= 20000000)
1584 config = GEM_BF(CLK, GEM_CLK_DIV8);
1585 else if (pclk_hz <= 40000000)
1586 config = GEM_BF(CLK, GEM_CLK_DIV16);
1587 else if (pclk_hz <= 80000000)
1588 config = GEM_BF(CLK, GEM_CLK_DIV32);
1589 else if (pclk_hz <= 120000000)
1590 config = GEM_BF(CLK, GEM_CLK_DIV48);
1591 else if (pclk_hz <= 160000000)
1592 config = GEM_BF(CLK, GEM_CLK_DIV64);
1593 else
1594 config = GEM_BF(CLK, GEM_CLK_DIV96);
1595
1596 return config;
1597}
1598
1599static u32 macb_mdc_clk_div(struct macb *bp)
1600{
1601 u32 config;
1602 unsigned long pclk_hz;
1603
1604 if (macb_is_gem(bp))
1605 return gem_mdc_clk_div(bp);
1606
1607 pclk_hz = clk_get_rate(bp->pclk);
1608 if (pclk_hz <= 20000000)
1609 config = MACB_BF(CLK, MACB_CLK_DIV8);
1610 else if (pclk_hz <= 40000000)
1611 config = MACB_BF(CLK, MACB_CLK_DIV16);
1612 else if (pclk_hz <= 80000000)
1613 config = MACB_BF(CLK, MACB_CLK_DIV32);
1614 else
1615 config = MACB_BF(CLK, MACB_CLK_DIV64);
1616
1617 return config;
1618}
1619
Jamie Iles757a03c2011-03-09 16:29:59 +00001620/*
1621 * Get the DMA bus width field of the network configuration register that we
1622 * should program. We find the width from decoding the design configuration
1623 * register to find the maximum supported data bus width.
1624 */
1625static u32 macb_dbw(struct macb *bp)
1626{
1627 if (!macb_is_gem(bp))
1628 return 0;
1629
1630 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1631 case 4:
1632 return GEM_BF(DBW, GEM_DBW128);
1633 case 2:
1634 return GEM_BF(DBW, GEM_DBW64);
1635 case 1:
1636 default:
1637 return GEM_BF(DBW, GEM_DBW32);
1638 }
1639}
1640
Jamie Iles0116da42011-03-14 17:38:30 +00001641/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001642 * Configure the receive DMA engine
1643 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001644 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001645 * (if not supported by FIFO, it will fallback to default)
1646 * - set both rx/tx packet buffers to full memory size
1647 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001648 */
1649static void macb_configure_dma(struct macb *bp)
1650{
1651 u32 dmacfg;
1652
1653 if (macb_is_gem(bp)) {
1654 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001655 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001656 if (bp->dma_burst_length)
1657 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001658 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301659 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301660
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03001661 if (bp->native_io)
Arun Chandran62f69242015-03-01 11:38:02 +05301662 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1663 else
1664 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1665
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001666 if (bp->dev->features & NETIF_F_HW_CSUM)
1667 dmacfg |= GEM_BIT(TXCOEN);
1668 else
1669 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001670 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1671 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001672 gem_writel(bp, DMACFG, dmacfg);
1673 }
1674}
1675
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001676static void macb_init_hw(struct macb *bp)
1677{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001678 struct macb_queue *queue;
1679 unsigned int q;
1680
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001681 u32 config;
1682
1683 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001684 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001685
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001686 config = macb_mdc_clk_div(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05301687 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1688 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001689 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001690 config |= MACB_BIT(PAE); /* PAuse Enable */
1691 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Dan Carpentera104a6b2015-05-12 21:15:24 +03001692 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301693 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1694 else
1695 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001696 if (bp->dev->flags & IFF_PROMISC)
1697 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001698 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1699 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001700 if (!(bp->dev->flags & IFF_BROADCAST))
1701 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001702 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001703 macb_writel(bp, NCFGR, config);
Dan Carpentera104a6b2015-05-12 21:15:24 +03001704 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301705 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001706 bp->speed = SPEED_10;
1707 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301708 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001709 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301710 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001711
Jamie Iles0116da42011-03-14 17:38:30 +00001712 macb_configure_dma(bp);
1713
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001714 /* Initialize TX and RX buffers */
1715 macb_writel(bp, RBQP, bp->rx_ring_dma);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001716 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1717 queue_writel(queue, TBQP, queue->tx_ring_dma);
1718
1719 /* Enable interrupts */
1720 queue_writel(queue, IER,
1721 MACB_RX_INT_FLAGS |
1722 MACB_TX_INT_FLAGS |
1723 MACB_BIT(HRESP));
1724 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001725
1726 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001727 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001728}
1729
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001730/*
1731 * The hash address register is 64 bits long and takes up two
1732 * locations in the memory map. The least significant bits are stored
1733 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1734 *
1735 * The unicast hash enable and the multicast hash enable bits in the
1736 * network configuration register enable the reception of hash matched
1737 * frames. The destination address is reduced to a 6 bit index into
1738 * the 64 bit hash register using the following hash function. The
1739 * hash function is an exclusive or of every sixth bit of the
1740 * destination address.
1741 *
1742 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1743 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1744 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1745 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1746 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1747 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1748 *
1749 * da[0] represents the least significant bit of the first byte
1750 * received, that is, the multicast/unicast indicator, and da[47]
1751 * represents the most significant bit of the last byte received. If
1752 * the hash index, hi[n], points to a bit that is set in the hash
1753 * register then the frame will be matched according to whether the
1754 * frame is multicast or unicast. A multicast match will be signalled
1755 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1756 * index points to a bit set in the hash register. A unicast match
1757 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1758 * and the hash index points to a bit set in the hash register. To
1759 * receive all multicast frames, the hash register should be set with
1760 * all ones and the multicast hash enable bit should be set in the
1761 * network configuration register.
1762 */
1763
1764static inline int hash_bit_value(int bitnr, __u8 *addr)
1765{
1766 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1767 return 1;
1768 return 0;
1769}
1770
1771/*
1772 * Return the hash index value for the specified address.
1773 */
1774static int hash_get_index(__u8 *addr)
1775{
1776 int i, j, bitval;
1777 int hash_index = 0;
1778
1779 for (j = 0; j < 6; j++) {
1780 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001781 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001782
1783 hash_index |= (bitval << j);
1784 }
1785
1786 return hash_index;
1787}
1788
1789/*
1790 * Add multicast addresses to the internal multicast-hash table.
1791 */
1792static void macb_sethashtable(struct net_device *dev)
1793{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001794 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001795 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001796 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001797 struct macb *bp = netdev_priv(dev);
1798
1799 mc_filter[0] = mc_filter[1] = 0;
1800
Jiri Pirko22bedad32010-04-01 21:22:57 +00001801 netdev_for_each_mc_addr(ha, dev) {
1802 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001803 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1804 }
1805
Jamie Ilesf75ba502011-11-08 10:12:32 +00001806 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1807 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001808}
1809
1810/*
1811 * Enable/Disable promiscuous and multicast modes.
1812 */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001813static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001814{
1815 unsigned long cfg;
1816 struct macb *bp = netdev_priv(dev);
1817
1818 cfg = macb_readl(bp, NCFGR);
1819
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001820 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001821 /* Enable promiscuous mode */
1822 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001823
1824 /* Disable RX checksum offload */
1825 if (macb_is_gem(bp))
1826 cfg &= ~GEM_BIT(RXCOEN);
1827 } else {
1828 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001829 cfg &= ~MACB_BIT(CAF);
1830
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001831 /* Enable RX checksum offload only if requested */
1832 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1833 cfg |= GEM_BIT(RXCOEN);
1834 }
1835
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001836 if (dev->flags & IFF_ALLMULTI) {
1837 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001838 macb_or_gem_writel(bp, HRB, -1);
1839 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001840 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001841 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001842 /* Enable specific multicasts */
1843 macb_sethashtable(dev);
1844 cfg |= MACB_BIT(NCFGR_MTI);
1845 } else if (dev->flags & (~IFF_ALLMULTI)) {
1846 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001847 macb_or_gem_writel(bp, HRB, 0);
1848 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001849 cfg &= ~MACB_BIT(NCFGR_MTI);
1850 }
1851
1852 macb_writel(bp, NCFGR, cfg);
1853}
1854
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001855static int macb_open(struct net_device *dev)
1856{
1857 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001858 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001859 int err;
1860
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001861 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001862
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001863 /* carrier starts down */
1864 netif_carrier_off(dev);
1865
frederic RODO6c36a702007-07-12 19:07:24 +02001866 /* if the phy is not yet register, retry later*/
1867 if (!bp->phy_dev)
1868 return -EAGAIN;
1869
Nicolas Ferre1b447912013-06-04 21:57:11 +00001870 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001871 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001872
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001873 err = macb_alloc_consistent(bp);
1874 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001875 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1876 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001877 return err;
1878 }
1879
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001880 napi_enable(&bp->napi);
1881
Nicolas Ferre4df95132013-06-04 21:57:12 +00001882 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001883 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001884
frederic RODO6c36a702007-07-12 19:07:24 +02001885 /* schedule a link state check */
1886 phy_start(bp->phy_dev);
1887
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001888 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001889
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001890 return 0;
1891}
1892
1893static int macb_close(struct net_device *dev)
1894{
1895 struct macb *bp = netdev_priv(dev);
1896 unsigned long flags;
1897
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001898 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001899 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001900
frederic RODO6c36a702007-07-12 19:07:24 +02001901 if (bp->phy_dev)
1902 phy_stop(bp->phy_dev);
1903
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001904 spin_lock_irqsave(&bp->lock, flags);
1905 macb_reset_hw(bp);
1906 netif_carrier_off(dev);
1907 spin_unlock_irqrestore(&bp->lock, flags);
1908
1909 macb_free_consistent(bp);
1910
1911 return 0;
1912}
1913
Harini Katakama5898ea2015-05-06 22:27:18 +05301914static int macb_change_mtu(struct net_device *dev, int new_mtu)
1915{
1916 struct macb *bp = netdev_priv(dev);
1917 u32 max_mtu;
1918
1919 if (netif_running(dev))
1920 return -EBUSY;
1921
1922 max_mtu = ETH_DATA_LEN;
Dan Carpentera104a6b2015-05-12 21:15:24 +03001923 if (bp->caps & MACB_CAPS_JUMBO)
Harini Katakama5898ea2015-05-06 22:27:18 +05301924 max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
1925
1926 if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE))
1927 return -EINVAL;
1928
1929 dev->mtu = new_mtu;
1930
1931 return 0;
1932}
1933
Jamie Ilesa494ed82011-03-09 16:26:35 +00001934static void gem_update_stats(struct macb *bp)
1935{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03001936 unsigned int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001937 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001938
Xander Huff3ff13f12015-01-13 16:15:51 -06001939 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1940 u32 offset = gem_statistics[i].offset;
David S. Miller7a6e0702015-07-27 14:24:48 -07001941 u64 val = bp->macb_reg_readl(bp, offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001942
1943 bp->ethtool_stats[i] += val;
1944 *p += val;
1945
1946 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1947 /* Add GEM_OCTTXH, GEM_OCTRXH */
David S. Miller7a6e0702015-07-27 14:24:48 -07001948 val = bp->macb_reg_readl(bp, offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06001949 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06001950 *(++p) += val;
1951 }
1952 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00001953}
1954
1955static struct net_device_stats *gem_get_stats(struct macb *bp)
1956{
1957 struct gem_stats *hwstat = &bp->hw_stats.gem;
1958 struct net_device_stats *nstat = &bp->stats;
1959
1960 gem_update_stats(bp);
1961
1962 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1963 hwstat->rx_alignment_errors +
1964 hwstat->rx_resource_errors +
1965 hwstat->rx_overruns +
1966 hwstat->rx_oversize_frames +
1967 hwstat->rx_jabbers +
1968 hwstat->rx_undersized_frames +
1969 hwstat->rx_length_field_frame_errors);
1970 nstat->tx_errors = (hwstat->tx_late_collisions +
1971 hwstat->tx_excessive_collisions +
1972 hwstat->tx_underrun +
1973 hwstat->tx_carrier_sense_errors);
1974 nstat->multicast = hwstat->rx_multicast_frames;
1975 nstat->collisions = (hwstat->tx_single_collision_frames +
1976 hwstat->tx_multiple_collision_frames +
1977 hwstat->tx_excessive_collisions);
1978 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1979 hwstat->rx_jabbers +
1980 hwstat->rx_undersized_frames +
1981 hwstat->rx_length_field_frame_errors);
1982 nstat->rx_over_errors = hwstat->rx_resource_errors;
1983 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1984 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1985 nstat->rx_fifo_errors = hwstat->rx_overruns;
1986 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1987 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1988 nstat->tx_fifo_errors = hwstat->tx_underrun;
1989
1990 return nstat;
1991}
1992
Xander Huff3ff13f12015-01-13 16:15:51 -06001993static void gem_get_ethtool_stats(struct net_device *dev,
1994 struct ethtool_stats *stats, u64 *data)
1995{
1996 struct macb *bp;
1997
1998 bp = netdev_priv(dev);
1999 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06002000 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06002001}
2002
2003static int gem_get_sset_count(struct net_device *dev, int sset)
2004{
2005 switch (sset) {
2006 case ETH_SS_STATS:
2007 return GEM_STATS_LEN;
2008 default:
2009 return -EOPNOTSUPP;
2010 }
2011}
2012
2013static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2014{
Andy Shevchenko8bcbf822015-07-24 21:24:02 +03002015 unsigned int i;
Xander Huff3ff13f12015-01-13 16:15:51 -06002016
2017 switch (sset) {
2018 case ETH_SS_STATS:
2019 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2020 memcpy(p, gem_statistics[i].stat_string,
2021 ETH_GSTRING_LEN);
2022 break;
2023 }
2024}
2025
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002026static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002027{
2028 struct macb *bp = netdev_priv(dev);
2029 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00002030 struct macb_stats *hwstat = &bp->hw_stats.macb;
2031
2032 if (macb_is_gem(bp))
2033 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002034
frederic RODO6c36a702007-07-12 19:07:24 +02002035 /* read stats from hardware */
2036 macb_update_stats(bp);
2037
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002038 /* Convert HW stats into netdevice stats */
2039 nstat->rx_errors = (hwstat->rx_fcs_errors +
2040 hwstat->rx_align_errors +
2041 hwstat->rx_resource_errors +
2042 hwstat->rx_overruns +
2043 hwstat->rx_oversize_pkts +
2044 hwstat->rx_jabbers +
2045 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002046 hwstat->rx_length_mismatch);
2047 nstat->tx_errors = (hwstat->tx_late_cols +
2048 hwstat->tx_excessive_cols +
2049 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02002050 hwstat->tx_carrier_errors +
2051 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002052 nstat->collisions = (hwstat->tx_single_cols +
2053 hwstat->tx_multiple_cols +
2054 hwstat->tx_excessive_cols);
2055 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2056 hwstat->rx_jabbers +
2057 hwstat->rx_undersize_pkts +
2058 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002059 nstat->rx_over_errors = hwstat->rx_resource_errors +
2060 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002061 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2062 nstat->rx_frame_errors = hwstat->rx_align_errors;
2063 nstat->rx_fifo_errors = hwstat->rx_overruns;
2064 /* XXX: What does "missed" mean? */
2065 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2066 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2067 nstat->tx_fifo_errors = hwstat->tx_underruns;
2068 /* Don't know about heartbeat or window errors... */
2069
2070 return nstat;
2071}
2072
2073static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2074{
2075 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002076 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002077
frederic RODO6c36a702007-07-12 19:07:24 +02002078 if (!phydev)
2079 return -ENODEV;
2080
2081 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002082}
2083
2084static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2085{
2086 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002087 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002088
frederic RODO6c36a702007-07-12 19:07:24 +02002089 if (!phydev)
2090 return -ENODEV;
2091
2092 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002093}
2094
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002095static int macb_get_regs_len(struct net_device *netdev)
2096{
2097 return MACB_GREGS_NBR * sizeof(u32);
2098}
2099
2100static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2101 void *p)
2102{
2103 struct macb *bp = netdev_priv(dev);
2104 unsigned int tail, head;
2105 u32 *regs_buff = p;
2106
2107 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2108 | MACB_GREGS_VERSION;
2109
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002110 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2111 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002112
2113 regs_buff[0] = macb_readl(bp, NCR);
2114 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2115 regs_buff[2] = macb_readl(bp, NSR);
2116 regs_buff[3] = macb_readl(bp, TSR);
2117 regs_buff[4] = macb_readl(bp, RBQP);
2118 regs_buff[5] = macb_readl(bp, TBQP);
2119 regs_buff[6] = macb_readl(bp, RSR);
2120 regs_buff[7] = macb_readl(bp, IMR);
2121
2122 regs_buff[8] = tail;
2123 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002124 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2125 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002126
Nicolas Ferre7c399942015-03-31 15:02:04 +02002127 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002128 if (macb_is_gem(bp)) {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002129 regs_buff[13] = gem_readl(bp, DMACFG);
2130 }
2131}
2132
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002133static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002134 .get_settings = macb_get_settings,
2135 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002136 .get_regs_len = macb_get_regs_len,
2137 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002138 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002139 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff8cd5a562015-01-15 15:55:20 -06002140};
Xander Huff8cd5a562015-01-15 15:55:20 -06002141
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002142static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002143 .get_settings = macb_get_settings,
2144 .set_settings = macb_set_settings,
2145 .get_regs_len = macb_get_regs_len,
2146 .get_regs = macb_get_regs,
2147 .get_link = ethtool_op_get_link,
2148 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002149 .get_ethtool_stats = gem_get_ethtool_stats,
2150 .get_strings = gem_get_ethtool_strings,
2151 .get_sset_count = gem_get_sset_count,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002152};
2153
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002154static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002155{
2156 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002157 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002158
2159 if (!netif_running(dev))
2160 return -EINVAL;
2161
frederic RODO6c36a702007-07-12 19:07:24 +02002162 if (!phydev)
2163 return -ENODEV;
2164
Richard Cochran28b04112010-07-17 08:48:55 +00002165 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002166}
2167
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002168static int macb_set_features(struct net_device *netdev,
2169 netdev_features_t features)
2170{
2171 struct macb *bp = netdev_priv(netdev);
2172 netdev_features_t changed = features ^ netdev->features;
2173
2174 /* TX checksum offload */
2175 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2176 u32 dmacfg;
2177
2178 dmacfg = gem_readl(bp, DMACFG);
2179 if (features & NETIF_F_HW_CSUM)
2180 dmacfg |= GEM_BIT(TXCOEN);
2181 else
2182 dmacfg &= ~GEM_BIT(TXCOEN);
2183 gem_writel(bp, DMACFG, dmacfg);
2184 }
2185
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002186 /* RX checksum offload */
2187 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2188 u32 netcfg;
2189
2190 netcfg = gem_readl(bp, NCFGR);
2191 if (features & NETIF_F_RXCSUM &&
2192 !(netdev->flags & IFF_PROMISC))
2193 netcfg |= GEM_BIT(RXCOEN);
2194 else
2195 netcfg &= ~GEM_BIT(RXCOEN);
2196 gem_writel(bp, NCFGR, netcfg);
2197 }
2198
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002199 return 0;
2200}
2201
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002202static const struct net_device_ops macb_netdev_ops = {
2203 .ndo_open = macb_open,
2204 .ndo_stop = macb_close,
2205 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002206 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002207 .ndo_get_stats = macb_get_stats,
2208 .ndo_do_ioctl = macb_ioctl,
2209 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302210 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002211 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002212#ifdef CONFIG_NET_POLL_CONTROLLER
2213 .ndo_poll_controller = macb_poll_controller,
2214#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002215 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002216};
2217
Nicolas Ferree1755872014-07-24 13:50:58 +02002218/*
Nicolas Ferread783472015-03-31 15:02:02 +02002219 * Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002220 * and integration options used
2221 */
Nicolas Ferref6970502015-03-31 15:02:01 +02002222static void macb_configure_caps(struct macb *bp, const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002223{
2224 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002225
Nicolas Ferref6970502015-03-31 15:02:01 +02002226 if (dt_conf)
2227 bp->caps = dt_conf->caps;
2228
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002229 if (hw_is_gem(bp->regs, bp->native_io)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002230 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2231
Nicolas Ferree1755872014-07-24 13:50:58 +02002232 dcfg = gem_readl(bp, DCFG1);
2233 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2234 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2235 dcfg = gem_readl(bp, DCFG2);
2236 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2237 bp->caps |= MACB_CAPS_FIFO_MODE;
2238 }
2239
Andy Shevchenkoa35919e2015-07-24 21:24:01 +03002240 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
Nicolas Ferree1755872014-07-24 13:50:58 +02002241}
2242
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002243static void macb_probe_queues(void __iomem *mem,
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002244 bool native_io,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002245 unsigned int *queue_mask,
2246 unsigned int *num_queues)
2247{
2248 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002249
2250 *queue_mask = 0x1;
2251 *num_queues = 1;
2252
Nicolas Ferreda120112015-03-31 15:02:00 +02002253 /* is it macb or gem ?
2254 *
2255 * We need to read directly from the hardware here because
2256 * we are early in the probe process and don't have the
2257 * MACB_CAPS_MACB_IS_GEM flag positioned
2258 */
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002259 if (!hw_is_gem(mem, native_io))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002260 return;
2261
2262 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302263 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2264
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002265 *queue_mask |= 0x1;
2266
2267 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2268 if (*queue_mask & (1 << hw_q))
2269 (*num_queues)++;
2270}
2271
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002272static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
2273 struct clk **hclk, struct clk **tx_clk)
2274{
2275 int err;
2276
2277 *pclk = devm_clk_get(&pdev->dev, "pclk");
2278 if (IS_ERR(*pclk)) {
2279 err = PTR_ERR(*pclk);
2280 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2281 return err;
2282 }
2283
2284 *hclk = devm_clk_get(&pdev->dev, "hclk");
2285 if (IS_ERR(*hclk)) {
2286 err = PTR_ERR(*hclk);
2287 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2288 return err;
2289 }
2290
2291 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2292 if (IS_ERR(*tx_clk))
2293 *tx_clk = NULL;
2294
2295 err = clk_prepare_enable(*pclk);
2296 if (err) {
2297 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2298 return err;
2299 }
2300
2301 err = clk_prepare_enable(*hclk);
2302 if (err) {
2303 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2304 goto err_disable_pclk;
2305 }
2306
2307 err = clk_prepare_enable(*tx_clk);
2308 if (err) {
2309 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2310 goto err_disable_hclk;
2311 }
2312
2313 return 0;
2314
2315err_disable_hclk:
2316 clk_disable_unprepare(*hclk);
2317
2318err_disable_pclk:
2319 clk_disable_unprepare(*pclk);
2320
2321 return err;
2322}
2323
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002324static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002325{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002326 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002327 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002328 struct macb *bp = netdev_priv(dev);
2329 struct macb_queue *queue;
2330 int err;
2331 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002332
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002333 /* set the queue register mapping once for all: queue0 has a special
2334 * register mapping but we don't want to test the queue index then
2335 * compute the corresponding register offset at run time.
2336 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002337 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002338 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002339 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002340
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002341 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002342 queue->bp = bp;
2343 if (hw_q) {
2344 queue->ISR = GEM_ISR(hw_q - 1);
2345 queue->IER = GEM_IER(hw_q - 1);
2346 queue->IDR = GEM_IDR(hw_q - 1);
2347 queue->IMR = GEM_IMR(hw_q - 1);
2348 queue->TBQP = GEM_TBQP(hw_q - 1);
2349 } else {
2350 /* queue0 uses legacy registers */
2351 queue->ISR = MACB_ISR;
2352 queue->IER = MACB_IER;
2353 queue->IDR = MACB_IDR;
2354 queue->IMR = MACB_IMR;
2355 queue->TBQP = MACB_TBQP;
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002356 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002357
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002358 /* get irq: here we use the linux queue index, not the hardware
2359 * queue index. the queue irq definitions in the device tree
2360 * must remove the optional gaps that could exist in the
2361 * hardware queue mask.
2362 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002363 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002364 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002365 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002366 if (err) {
2367 dev_err(&pdev->dev,
2368 "Unable to request IRQ %d (error %d)\n",
2369 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002370 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002371 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002372
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002373 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002374 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002375 }
2376
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002377 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002378 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002379
Nicolas Ferre4df95132013-06-04 21:57:12 +00002380 /* setup appropriated routines according to adapter type */
2381 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002382 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002383 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2384 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2385 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2386 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002387 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002388 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002389 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002390 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2391 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2392 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2393 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002394 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002395 }
2396
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002397 /* Set features */
2398 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002399 /* Checksum offload is only available on gem with packet buffer */
2400 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002401 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002402 if (bp->caps & MACB_CAPS_SG_DISABLED)
2403 dev->hw_features &= ~NETIF_F_SG;
2404 dev->features = dev->hw_features;
2405
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002406 val = 0;
2407 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2408 val = GEM_BIT(RGMII);
2409 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
2410 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2411 val = MACB_BIT(RMII);
2412 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2413 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002414
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002415 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2416 val |= MACB_BIT(CLKEN);
2417
2418 macb_or_gem_writel(bp, USRIO, val);
2419
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002420 /* Set MII management clock divider */
2421 val = macb_mdc_clk_div(bp);
2422 val |= macb_dbw(bp);
Punnaiah Choudary Kalluri022be252015-11-18 09:03:50 +05302423 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2424 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002425 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 = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302783 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302784 .dma_burst_length = 16,
2785 .clk_init = macb_clk_init,
2786 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302787 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302788};
2789
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002790static const struct macb_config zynq_config = {
Punnaiah Choudary Kalluri7baaa902015-07-06 10:02:53 +05302791 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002792 .dma_burst_length = 16,
2793 .clk_init = macb_clk_init,
2794 .init = macb_init,
2795};
2796
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002797static const struct of_device_id macb_dt_ids[] = {
2798 { .compatible = "cdns,at32ap7000-macb" },
2799 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2800 { .compatible = "cdns,macb" },
2801 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2802 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchen5c8fe712015-06-18 16:27:23 +02002803 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002804 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2805 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2806 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2807 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302808 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Nathan Sullivan222ca8e2015-05-22 09:22:10 -05002809 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002810 { /* sentinel */ }
2811};
2812MODULE_DEVICE_TABLE(of, macb_dt_ids);
2813#endif /* CONFIG_OF */
2814
2815static int macb_probe(struct platform_device *pdev)
2816{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002817 int (*clk_init)(struct platform_device *, struct clk **,
2818 struct clk **, struct clk **)
2819 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002820 int (*init)(struct platform_device *) = macb_init;
2821 struct device_node *np = pdev->dev.of_node;
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002822 struct device_node *phy_node;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002823 const struct macb_config *macb_config = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002824 struct clk *pclk, *hclk, *tx_clk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002825 unsigned int queue_mask, num_queues;
2826 struct macb_platform_data *pdata;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002827 bool native_io;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002828 struct phy_device *phydev;
2829 struct net_device *dev;
2830 struct resource *regs;
2831 void __iomem *mem;
2832 const char *mac;
2833 struct macb *bp;
2834 int err;
2835
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002836 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2837 mem = devm_ioremap_resource(&pdev->dev, regs);
2838 if (IS_ERR(mem))
2839 return PTR_ERR(mem);
2840
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002841 if (np) {
2842 const struct of_device_id *match;
2843
2844 match = of_match_node(macb_dt_ids, np);
2845 if (match && match->data) {
2846 macb_config = match->data;
2847 clk_init = macb_config->clk_init;
2848 init = macb_config->init;
2849 }
2850 }
2851
2852 err = clk_init(pdev, &pclk, &hclk, &tx_clk);
2853 if (err)
2854 return err;
2855
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002856 native_io = hw_is_native_io(mem);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002857
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002858 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002859 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002860 if (!dev) {
2861 err = -ENOMEM;
2862 goto err_disable_clocks;
2863 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002864
2865 dev->base_addr = regs->start;
2866
2867 SET_NETDEV_DEV(dev, &pdev->dev);
2868
2869 bp = netdev_priv(dev);
2870 bp->pdev = pdev;
2871 bp->dev = dev;
2872 bp->regs = mem;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002873 bp->native_io = native_io;
2874 if (native_io) {
David S. Miller7a6e0702015-07-27 14:24:48 -07002875 bp->macb_reg_readl = hw_readl_native;
2876 bp->macb_reg_writel = hw_writel_native;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002877 } else {
David S. Miller7a6e0702015-07-27 14:24:48 -07002878 bp->macb_reg_readl = hw_readl;
2879 bp->macb_reg_writel = hw_writel;
Andy Shevchenkof2ce8a92015-07-24 21:23:59 +03002880 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002881 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002882 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002883 if (macb_config)
2884 bp->dma_burst_length = macb_config->dma_burst_length;
2885 bp->pclk = pclk;
2886 bp->hclk = hclk;
2887 bp->tx_clk = tx_clk;
Andy Shevchenkof36dbe62015-07-24 21:24:00 +03002888 if (macb_config)
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302889 bp->jumbo_max_len = macb_config->jumbo_max_len;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302890
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002891 spin_lock_init(&bp->lock);
2892
Nicolas Ferread783472015-03-31 15:02:02 +02002893 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02002894 macb_configure_caps(bp, macb_config);
2895
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002896 platform_set_drvdata(pdev, dev);
2897
2898 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002899 if (dev->irq < 0) {
2900 err = dev->irq;
2901 goto err_disable_clocks;
2902 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002903
2904 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00002905 if (mac)
2906 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2907 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002908 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002909
Gregory CLEMENT5833e052015-12-11 11:34:53 +01002910 /* Power up the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002911 phy_node = of_get_next_available_child(np, NULL);
2912 if (phy_node) {
2913 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
2914 if (gpio_is_valid(gpio))
2915 bp->reset_gpio = gpio_to_desc(gpio);
2916 gpiod_set_value(bp->reset_gpio, GPIOD_OUT_HIGH);
2917 }
2918 of_node_put(phy_node);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01002919
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002920 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002921 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002922 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002923 if (pdata && pdata->is_rmii)
2924 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2925 else
2926 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2927 } else {
2928 bp->phy_interface = err;
2929 }
2930
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002931 /* IP specific init */
2932 err = init(pdev);
2933 if (err)
2934 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002935
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002936 err = register_netdev(dev);
2937 if (err) {
2938 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002939 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002940 }
2941
Nicolas Ferre72ca8202013-04-14 22:04:33 +00002942 err = macb_mii_init(bp);
2943 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02002944 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002945
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002946 netif_carrier_off(dev);
2947
Bo Shen58798232014-09-13 01:57:49 +02002948 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
2949 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
2950 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002951
frederic RODO6c36a702007-07-12 19:07:24 +02002952 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002953 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2954 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002955
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002956 return 0;
2957
frederic RODO6c36a702007-07-12 19:07:24 +02002958err_out_unregister_netdev:
2959 unregister_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002960
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002961err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002962 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002963
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002964err_disable_clocks:
2965 clk_disable_unprepare(tx_clk);
2966 clk_disable_unprepare(hclk);
2967 clk_disable_unprepare(pclk);
2968
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002969 return err;
2970}
2971
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002972static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002973{
2974 struct net_device *dev;
2975 struct macb *bp;
2976
2977 dev = platform_get_drvdata(pdev);
2978
2979 if (dev) {
2980 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002981 if (bp->phy_dev)
2982 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002983 mdiobus_unregister(bp->mii_bus);
2984 kfree(bp->mii_bus->irq);
2985 mdiobus_free(bp->mii_bus);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01002986
2987 /* Shutdown the PHY if there is a GPIO reset */
Gregory CLEMENT270c4992015-12-17 10:51:04 +01002988 gpiod_set_value(bp->reset_gpio, GPIOD_OUT_LOW);
Gregory CLEMENT5833e052015-12-11 11:34:53 +01002989
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002990 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002991 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002992 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002993 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01002994 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002995 }
2996
2997 return 0;
2998}
2999
Michal Simekd23823d2015-01-23 09:36:03 +01003000static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003001{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003002 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003003 struct net_device *netdev = platform_get_drvdata(pdev);
3004 struct macb *bp = netdev_priv(netdev);
3005
Nicolas Ferre03fc4722012-07-03 23:14:13 +00003006 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003007 netif_device_detach(netdev);
3008
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003009 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00003010 clk_disable_unprepare(bp->hclk);
3011 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003012
3013 return 0;
3014}
3015
Michal Simekd23823d2015-01-23 09:36:03 +01003016static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003017{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003018 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003019 struct net_device *netdev = platform_get_drvdata(pdev);
3020 struct macb *bp = netdev_priv(netdev);
3021
Steffen Trumtrarace58012013-03-27 23:07:07 +00003022 clk_prepare_enable(bp->pclk);
3023 clk_prepare_enable(bp->hclk);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01003024 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003025
3026 netif_device_attach(netdev);
3027
3028 return 0;
3029}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01003030
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003031static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3032
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003033static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003034 .probe = macb_probe,
3035 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003036 .driver = {
3037 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01003038 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08003039 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003040 },
3041};
3042
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00003043module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003044
3045MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00003046MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02003047MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07003048MODULE_ALIAS("platform:macb");