blob: 2bb3c1c06e605c7165755bfbbeb20dada80b2945 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000021#include <linux/gpio.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000026#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020028#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080029#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010030#include <linux/of_device.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010033
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010034#include "macb.h"
35
Nicolas Ferre1b447912013-06-04 21:57:11 +000036#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000037#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000038#define RX_RING_SIZE 512 /* must be power of 2 */
39#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010040
Havard Skinnemoen55054a12012-10-31 06:04:55 +000041#define TX_RING_SIZE 128 /* must be power of 2 */
42#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010043
Nicolas Ferre909a8582012-11-19 06:00:21 +000044/* level of occupied TX descriptors under which we wake up TX process */
45#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010046
47#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
48 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000049#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
50 | MACB_BIT(ISR_RLE) \
51 | MACB_BIT(TXERR))
52#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
53
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020054#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
55#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
56
Nicolas Ferree86cd532012-10-31 06:04:57 +000057/*
58 * Graceful stop timeouts in us. We should allow up to
59 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
60 */
61#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010062
Havard Skinnemoen55054a12012-10-31 06:04:55 +000063/* Ring buffer accessors */
64static unsigned int macb_tx_ring_wrap(unsigned int index)
65{
66 return index & (TX_RING_SIZE - 1);
67}
68
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010069static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
70 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000071{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010072 return &queue->tx_ring[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000073}
74
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010075static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
76 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000077{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010078 return &queue->tx_skb[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000079}
80
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010081static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000082{
83 dma_addr_t offset;
84
85 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
86
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010087 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +000088}
89
90static unsigned int macb_rx_ring_wrap(unsigned int index)
91{
92 return index & (RX_RING_SIZE - 1);
93}
94
95static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
96{
97 return &bp->rx_ring[macb_rx_ring_wrap(index)];
98}
99
100static void *macb_rx_buffer(struct macb *bp, unsigned int index)
101{
Nicolas Ferre1b447912013-06-04 21:57:11 +0000102 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000103}
104
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000105void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100106{
107 u32 bottom;
108 u16 top;
109
110 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000111 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100112 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000113 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000114
115 /* Clear unused address register sets */
116 macb_or_gem_writel(bp, SA2B, 0);
117 macb_or_gem_writel(bp, SA2T, 0);
118 macb_or_gem_writel(bp, SA3B, 0);
119 macb_or_gem_writel(bp, SA3T, 0);
120 macb_or_gem_writel(bp, SA4B, 0);
121 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100122}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000123EXPORT_SYMBOL_GPL(macb_set_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100124
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000125void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100126{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000127 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100128 u32 bottom;
129 u16 top;
130 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000131 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100132
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900133 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000134
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000135 /* Check all 4 address register for vaild address */
136 for (i = 0; i < 4; i++) {
137 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
138 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100139
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000140 if (pdata && pdata->rev_eth_addr) {
141 addr[5] = bottom & 0xff;
142 addr[4] = (bottom >> 8) & 0xff;
143 addr[3] = (bottom >> 16) & 0xff;
144 addr[2] = (bottom >> 24) & 0xff;
145 addr[1] = top & 0xff;
146 addr[0] = (top & 0xff00) >> 8;
147 } else {
148 addr[0] = bottom & 0xff;
149 addr[1] = (bottom >> 8) & 0xff;
150 addr[2] = (bottom >> 16) & 0xff;
151 addr[3] = (bottom >> 24) & 0xff;
152 addr[4] = top & 0xff;
153 addr[5] = (top >> 8) & 0xff;
154 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100155
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000156 if (is_valid_ether_addr(addr)) {
157 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
158 return;
159 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700160 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000161
162 netdev_info(bp->dev, "invalid hw address, using random\n");
163 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100164}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000165EXPORT_SYMBOL_GPL(macb_get_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100166
frederic RODO6c36a702007-07-12 19:07:24 +0200167static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100168{
frederic RODO6c36a702007-07-12 19:07:24 +0200169 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100170 int value;
171
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100172 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
173 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200174 | MACB_BF(PHYA, mii_id)
175 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100176 | MACB_BF(CODE, MACB_MAN_CODE)));
177
frederic RODO6c36a702007-07-12 19:07:24 +0200178 /* wait for end of transfer */
179 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
180 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100181
182 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100183
184 return value;
185}
186
frederic RODO6c36a702007-07-12 19:07:24 +0200187static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
188 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100189{
frederic RODO6c36a702007-07-12 19:07:24 +0200190 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100191
192 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
193 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200194 | MACB_BF(PHYA, mii_id)
195 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100196 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200197 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100198
frederic RODO6c36a702007-07-12 19:07:24 +0200199 /* wait for end of transfer */
200 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
201 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100202
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100203 return 0;
204}
205
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800206/**
207 * macb_set_tx_clk() - Set a clock to a new frequency
208 * @clk Pointer to the clock to change
209 * @rate New frequency in Hz
210 * @dev Pointer to the struct net_device
211 */
212static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
213{
214 long ferr, rate, rate_rounded;
215
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100216 if (!clk)
217 return;
218
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800219 switch (speed) {
220 case SPEED_10:
221 rate = 2500000;
222 break;
223 case SPEED_100:
224 rate = 25000000;
225 break;
226 case SPEED_1000:
227 rate = 125000000;
228 break;
229 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800230 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800231 }
232
233 rate_rounded = clk_round_rate(clk, rate);
234 if (rate_rounded < 0)
235 return;
236
237 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
238 * is not satisfied.
239 */
240 ferr = abs(rate_rounded - rate);
241 ferr = DIV_ROUND_UP(ferr, rate / 100000);
242 if (ferr > 5)
243 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
244 rate);
245
246 if (clk_set_rate(clk, rate_rounded))
247 netdev_err(dev, "adjusting tx_clk failed.\n");
248}
249
frederic RODO6c36a702007-07-12 19:07:24 +0200250static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100251{
frederic RODO6c36a702007-07-12 19:07:24 +0200252 struct macb *bp = netdev_priv(dev);
253 struct phy_device *phydev = bp->phy_dev;
254 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100255
frederic RODO6c36a702007-07-12 19:07:24 +0200256 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100257
frederic RODO6c36a702007-07-12 19:07:24 +0200258 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100259
frederic RODO6c36a702007-07-12 19:07:24 +0200260 if (phydev->link) {
261 if ((bp->speed != phydev->speed) ||
262 (bp->duplex != phydev->duplex)) {
263 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100264
frederic RODO6c36a702007-07-12 19:07:24 +0200265 reg = macb_readl(bp, NCFGR);
266 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000267 if (macb_is_gem(bp))
268 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200269
270 if (phydev->duplex)
271 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900272 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200273 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200274 if (phydev->speed == SPEED_1000 &&
275 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000276 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200277
Patrice Vilchez140b7552012-10-31 06:04:50 +0000278 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200279
280 bp->speed = phydev->speed;
281 bp->duplex = phydev->duplex;
282 status_change = 1;
283 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284 }
285
frederic RODO6c36a702007-07-12 19:07:24 +0200286 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700287 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200288 bp->speed = 0;
289 bp->duplex = -1;
290 }
291 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100292
frederic RODO6c36a702007-07-12 19:07:24 +0200293 status_change = 1;
294 }
295
296 spin_unlock_irqrestore(&bp->lock, flags);
297
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100298 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800299
frederic RODO6c36a702007-07-12 19:07:24 +0200300 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000301 if (phydev->link) {
302 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000303 netdev_info(dev, "link up (%d/%s)\n",
304 phydev->speed,
305 phydev->duplex == DUPLEX_FULL ?
306 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000307 } else {
308 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000309 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000310 }
frederic RODO6c36a702007-07-12 19:07:24 +0200311 }
312}
313
314/* based on au1000_eth. c*/
315static int macb_mii_probe(struct net_device *dev)
316{
317 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000318 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000319 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000320 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000321 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200322
Jiri Pirko7455a762010-02-08 05:12:08 +0000323 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200324 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000325 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200326 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200327 }
328
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000329 pdata = dev_get_platdata(&bp->pdev->dev);
330 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
331 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
332 if (!ret) {
333 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
334 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
335 }
336 }
frederic RODO6c36a702007-07-12 19:07:24 +0200337
338 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000339 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100340 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000341 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000342 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000343 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200344 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100345
frederic RODO6c36a702007-07-12 19:07:24 +0200346 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200347 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000348 phydev->supported &= PHY_GBIT_FEATURES;
349 else
350 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351
frederic RODO6c36a702007-07-12 19:07:24 +0200352 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100353
frederic RODO6c36a702007-07-12 19:07:24 +0200354 bp->link = 0;
355 bp->speed = 0;
356 bp->duplex = -1;
357 bp->phy_dev = phydev;
358
359 return 0;
360}
361
Joachim Eastwood0005f542012-10-18 11:01:12 +0000362int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200363{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000364 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200365 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200366 int err = -ENXIO, i;
367
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200368 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200369 macb_writel(bp, NCR, MACB_BIT(MPE));
370
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700371 bp->mii_bus = mdiobus_alloc();
372 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200373 err = -ENOMEM;
374 goto err_out;
375 }
376
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700377 bp->mii_bus->name = "MACB_mii_bus";
378 bp->mii_bus->read = &macb_mdio_read;
379 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000380 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
381 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700382 bp->mii_bus->priv = bp;
383 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900384 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700385
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700386 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
387 if (!bp->mii_bus->irq) {
388 err = -ENOMEM;
389 goto err_out_free_mdiobus;
390 }
391
Jamie Iles91523942011-02-28 04:05:25 +0000392 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200393
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200394 np = bp->pdev->dev.of_node;
395 if (np) {
396 /* try dt phy registration */
397 err = of_mdiobus_register(bp->mii_bus, np);
398
399 /* fallback to standard phy registration if no phy were
400 found during dt phy registration */
401 if (!err && !phy_find_first(bp->mii_bus)) {
402 for (i = 0; i < PHY_MAX_ADDR; i++) {
403 struct phy_device *phydev;
404
405 phydev = mdiobus_scan(bp->mii_bus, i);
406 if (IS_ERR(phydev)) {
407 err = PTR_ERR(phydev);
408 break;
409 }
410 }
411
412 if (err)
413 goto err_out_unregister_bus;
414 }
415 } else {
416 for (i = 0; i < PHY_MAX_ADDR; i++)
417 bp->mii_bus->irq[i] = PHY_POLL;
418
419 if (pdata)
420 bp->mii_bus->phy_mask = pdata->phy_mask;
421
422 err = mdiobus_register(bp->mii_bus);
423 }
424
425 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200426 goto err_out_free_mdio_irq;
427
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200428 err = macb_mii_probe(bp->dev);
429 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200430 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200431
432 return 0;
433
434err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700435 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200436err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700437 kfree(bp->mii_bus->irq);
438err_out_free_mdiobus:
439 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200440err_out:
441 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100442}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000443EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100444
445static void macb_update_stats(struct macb *bp)
446{
447 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000448 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
449 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100450
451 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
452
453 for(; p < end; p++, reg++)
Arun Chandrana50dad32015-02-18 16:59:35 +0530454 *p += readl_relaxed(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100455}
456
Nicolas Ferree86cd532012-10-31 06:04:57 +0000457static int macb_halt_tx(struct macb *bp)
458{
459 unsigned long halt_time, timeout;
460 u32 status;
461
462 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
463
464 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
465 do {
466 halt_time = jiffies;
467 status = macb_readl(bp, TSR);
468 if (!(status & MACB_BIT(TGO)))
469 return 0;
470
471 usleep_range(10, 250);
472 } while (time_before(halt_time, timeout));
473
474 return -ETIMEDOUT;
475}
476
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200477static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
478{
479 if (tx_skb->mapping) {
480 if (tx_skb->mapped_as_page)
481 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
482 tx_skb->size, DMA_TO_DEVICE);
483 else
484 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
485 tx_skb->size, DMA_TO_DEVICE);
486 tx_skb->mapping = 0;
487 }
488
489 if (tx_skb->skb) {
490 dev_kfree_skb_any(tx_skb->skb);
491 tx_skb->skb = NULL;
492 }
493}
494
Nicolas Ferree86cd532012-10-31 06:04:57 +0000495static void macb_tx_error_task(struct work_struct *work)
496{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100497 struct macb_queue *queue = container_of(work, struct macb_queue,
498 tx_error_task);
499 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000500 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100501 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000502 struct sk_buff *skb;
503 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100504 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000505
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100506 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
507 (unsigned int)(queue - bp->queues),
508 queue->tx_tail, queue->tx_head);
509
510 /* Prevent the queue IRQ handlers from running: each of them may call
511 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
512 * As explained below, we have to halt the transmission before updating
513 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
514 * network engine about the macb/gem being halted.
515 */
516 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000517
518 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100519 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000520
521 /*
522 * Stop transmission now
523 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100524 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000525 */
526 if (macb_halt_tx(bp))
527 /* Just complain for now, reinitializing TX path can be good */
528 netdev_err(bp->dev, "BUG: halt tx timed out\n");
529
Nicolas Ferree86cd532012-10-31 06:04:57 +0000530 /*
531 * Treat frames in TX queue including the ones that caused the error.
532 * Free transmit buffers in upper layer.
533 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100534 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
535 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000536
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100537 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000538 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100539 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000540 skb = tx_skb->skb;
541
542 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200543 /* skb is set for the last buffer of the frame */
544 while (!skb) {
545 macb_tx_unmap(bp, tx_skb);
546 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100547 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200548 skb = tx_skb->skb;
549 }
550
551 /* ctrl still refers to the first buffer descriptor
552 * since it's the only one written back by the hardware
553 */
554 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
555 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
556 macb_tx_ring_wrap(tail), skb->data);
557 bp->stats.tx_packets++;
558 bp->stats.tx_bytes += skb->len;
559 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000560 } else {
561 /*
562 * "Buffers exhausted mid-frame" errors may only happen
563 * if the driver is buggy, so complain loudly about those.
564 * Statistics are updated by hardware.
565 */
566 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
567 netdev_err(bp->dev,
568 "BUG: TX buffers exhausted mid-frame\n");
569
570 desc->ctrl = ctrl | MACB_BIT(TX_USED);
571 }
572
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200573 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000574 }
575
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100576 /* Set end of TX queue */
577 desc = macb_tx_desc(queue, 0);
578 desc->addr = 0;
579 desc->ctrl = MACB_BIT(TX_USED);
580
Nicolas Ferree86cd532012-10-31 06:04:57 +0000581 /* Make descriptor updates visible to hardware */
582 wmb();
583
584 /* Reinitialize the TX desc queue */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100585 queue_writel(queue, TBQP, queue->tx_ring_dma);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000586 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100587 queue->tx_head = 0;
588 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000589
590 /* Housework before enabling TX IRQ */
591 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100592 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
593
594 /* Now we are ready to start transmission again */
595 netif_tx_start_all_queues(bp->dev);
596 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
597
598 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000599}
600
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100601static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100602{
603 unsigned int tail;
604 unsigned int head;
605 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100606 struct macb *bp = queue->bp;
607 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100608
609 status = macb_readl(bp, TSR);
610 macb_writel(bp, TSR, status);
611
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000612 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100613 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000614
Nicolas Ferree86cd532012-10-31 06:04:57 +0000615 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
616 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100617
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100618 head = queue->tx_head;
619 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000620 struct macb_tx_skb *tx_skb;
621 struct sk_buff *skb;
622 struct macb_dma_desc *desc;
623 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100624
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100625 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100626
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000627 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100628 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000629
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000630 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100631
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200632 /* TX_USED bit is only set by hardware on the very first buffer
633 * descriptor of the transmitted frame.
634 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000635 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100636 break;
637
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200638 /* Process all buffers of the current transmitted frame */
639 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100640 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200641 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000642
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200643 /* First, update TX stats if needed */
644 if (skb) {
645 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
646 macb_tx_ring_wrap(tail), skb->data);
647 bp->stats.tx_packets++;
648 bp->stats.tx_bytes += skb->len;
649 }
650
651 /* Now we can safely release resources */
652 macb_tx_unmap(bp, tx_skb);
653
654 /* skb is set only for the last buffer of the frame.
655 * WARNING: at this point skb has been freed by
656 * macb_tx_unmap().
657 */
658 if (skb)
659 break;
660 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100661 }
662
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100663 queue->tx_tail = tail;
664 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
665 CIRC_CNT(queue->tx_head, queue->tx_tail,
666 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
667 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100668}
669
Nicolas Ferre4df95132013-06-04 21:57:12 +0000670static void gem_rx_refill(struct macb *bp)
671{
672 unsigned int entry;
673 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000674 dma_addr_t paddr;
675
676 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000677 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000678
679 /* Make hw descriptor updates visible to CPU */
680 rmb();
681
Nicolas Ferre4df95132013-06-04 21:57:12 +0000682 bp->rx_prepared_head++;
683
Nicolas Ferre4df95132013-06-04 21:57:12 +0000684 if (bp->rx_skbuff[entry] == NULL) {
685 /* allocate sk_buff for this free entry in ring */
686 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
687 if (unlikely(skb == NULL)) {
688 netdev_err(bp->dev,
689 "Unable to allocate sk_buff\n");
690 break;
691 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000692
693 /* now fill corresponding descriptor entry */
694 paddr = dma_map_single(&bp->pdev->dev, skb->data,
695 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800696 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
697 dev_kfree_skb(skb);
698 break;
699 }
700
701 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000702
703 if (entry == RX_RING_SIZE - 1)
704 paddr |= MACB_BIT(RX_WRAP);
705 bp->rx_ring[entry].addr = paddr;
706 bp->rx_ring[entry].ctrl = 0;
707
708 /* properly align Ethernet header */
709 skb_reserve(skb, NET_IP_ALIGN);
710 }
711 }
712
713 /* Make descriptor updates visible to hardware */
714 wmb();
715
716 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
717 bp->rx_prepared_head, bp->rx_tail);
718}
719
720/* Mark DMA descriptors from begin up to and not including end as unused */
721static void discard_partial_frame(struct macb *bp, unsigned int begin,
722 unsigned int end)
723{
724 unsigned int frag;
725
726 for (frag = begin; frag != end; frag++) {
727 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
728 desc->addr &= ~MACB_BIT(RX_USED);
729 }
730
731 /* Make descriptor updates visible to hardware */
732 wmb();
733
734 /*
735 * When this happens, the hardware stats registers for
736 * whatever caused this is updated, so we don't have to record
737 * anything.
738 */
739}
740
741static int gem_rx(struct macb *bp, int budget)
742{
743 unsigned int len;
744 unsigned int entry;
745 struct sk_buff *skb;
746 struct macb_dma_desc *desc;
747 int count = 0;
748
749 while (count < budget) {
750 u32 addr, ctrl;
751
752 entry = macb_rx_ring_wrap(bp->rx_tail);
753 desc = &bp->rx_ring[entry];
754
755 /* Make hw descriptor updates visible to CPU */
756 rmb();
757
758 addr = desc->addr;
759 ctrl = desc->ctrl;
760
761 if (!(addr & MACB_BIT(RX_USED)))
762 break;
763
Nicolas Ferre4df95132013-06-04 21:57:12 +0000764 bp->rx_tail++;
765 count++;
766
767 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
768 netdev_err(bp->dev,
769 "not whole frame pointed by descriptor\n");
770 bp->stats.rx_dropped++;
771 break;
772 }
773 skb = bp->rx_skbuff[entry];
774 if (unlikely(!skb)) {
775 netdev_err(bp->dev,
776 "inconsistent Rx descriptor chain\n");
777 bp->stats.rx_dropped++;
778 break;
779 }
780 /* now everything is ready for receiving packet */
781 bp->rx_skbuff[entry] = NULL;
782 len = MACB_BFEXT(RX_FRMLEN, ctrl);
783
784 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
785
786 skb_put(skb, len);
787 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
788 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800789 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000790
791 skb->protocol = eth_type_trans(skb, bp->dev);
792 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200793 if (bp->dev->features & NETIF_F_RXCSUM &&
794 !(bp->dev->flags & IFF_PROMISC) &&
795 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
796 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000797
798 bp->stats.rx_packets++;
799 bp->stats.rx_bytes += skb->len;
800
801#if defined(DEBUG) && defined(VERBOSE_DEBUG)
802 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
803 skb->len, skb->csum);
804 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100805 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000806 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
807 skb->data, 32, true);
808#endif
809
810 netif_receive_skb(skb);
811 }
812
813 gem_rx_refill(bp);
814
815 return count;
816}
817
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100818static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
819 unsigned int last_frag)
820{
821 unsigned int len;
822 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000823 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100824 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000825 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100826
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000827 desc = macb_rx_desc(bp, last_frag);
828 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100829
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000830 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000831 macb_rx_ring_wrap(first_frag),
832 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100833
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000834 /*
835 * The ethernet header starts NET_IP_ALIGN bytes into the
836 * first buffer. Since the header is 14 bytes, this makes the
837 * payload word-aligned.
838 *
839 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
840 * the two padding bytes into the skb so that we avoid hitting
841 * the slowpath in memcpy(), and pull them off afterwards.
842 */
843 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100844 if (!skb) {
845 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000846 for (frag = first_frag; ; frag++) {
847 desc = macb_rx_desc(bp, frag);
848 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100849 if (frag == last_frag)
850 break;
851 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000852
853 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100854 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000855
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100856 return 1;
857 }
858
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000859 offset = 0;
860 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700861 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100862 skb_put(skb, len);
863
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000864 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000865 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100866
867 if (offset + frag_len > len) {
868 BUG_ON(frag != last_frag);
869 frag_len = len - offset;
870 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300871 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000872 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000873 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000874 desc = macb_rx_desc(bp, frag);
875 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100876
877 if (frag == last_frag)
878 break;
879 }
880
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000881 /* Make descriptor updates visible to hardware */
882 wmb();
883
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000884 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100885 skb->protocol = eth_type_trans(skb, bp->dev);
886
887 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000888 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000889 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000890 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100891 netif_receive_skb(skb);
892
893 return 0;
894}
895
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100896static int macb_rx(struct macb *bp, int budget)
897{
898 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000899 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100900 int first_frag = -1;
901
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000902 for (tail = bp->rx_tail; budget > 0; tail++) {
903 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100904 u32 addr, ctrl;
905
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000906 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100907 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000908
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000909 addr = desc->addr;
910 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100911
912 if (!(addr & MACB_BIT(RX_USED)))
913 break;
914
915 if (ctrl & MACB_BIT(RX_SOF)) {
916 if (first_frag != -1)
917 discard_partial_frame(bp, first_frag, tail);
918 first_frag = tail;
919 }
920
921 if (ctrl & MACB_BIT(RX_EOF)) {
922 int dropped;
923 BUG_ON(first_frag == -1);
924
925 dropped = macb_rx_frame(bp, first_frag, tail);
926 first_frag = -1;
927 if (!dropped) {
928 received++;
929 budget--;
930 }
931 }
932 }
933
934 if (first_frag != -1)
935 bp->rx_tail = first_frag;
936 else
937 bp->rx_tail = tail;
938
939 return received;
940}
941
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700942static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100943{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700944 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700945 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100946 u32 status;
947
948 status = macb_readl(bp, RSR);
949 macb_writel(bp, RSR, status);
950
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700951 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100952
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000953 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000954 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100955
Nicolas Ferre4df95132013-06-04 21:57:12 +0000956 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000957 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800958 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100959
Nicolas Ferre8770e912013-02-12 11:08:48 +0100960 /* Packets received while interrupts were disabled */
961 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -0700962 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700963 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
964 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +0100965 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700966 } else {
967 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
968 }
Joshua Hokeb3363692010-10-25 01:44:22 +0000969 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100970
971 /* TODO: Handle errors */
972
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700973 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100974}
975
976static irqreturn_t macb_interrupt(int irq, void *dev_id)
977{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100978 struct macb_queue *queue = dev_id;
979 struct macb *bp = queue->bp;
980 struct net_device *dev = bp->dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100981 u32 status;
982
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100983 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100984
985 if (unlikely(!status))
986 return IRQ_NONE;
987
988 spin_lock(&bp->lock);
989
990 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100991 /* close possible race with dev_close */
992 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100993 queue_writel(queue, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100994 break;
995 }
996
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100997 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
998 (unsigned int)(queue - bp->queues),
999 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001000
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001001 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +00001002 /*
1003 * There's no point taking any more interrupts
1004 * until we have processed the buffers. The
1005 * scheduling call may fail if the poll routine
1006 * is already scheduled, so disable interrupts
1007 * now.
1008 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001009 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001010 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001011 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001012
Ben Hutchings288379f2009-01-19 16:43:59 -08001013 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001014 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001015 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001016 }
1017 }
1018
Nicolas Ferree86cd532012-10-31 06:04:57 +00001019 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001020 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1021 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001022
1023 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001024 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001025
Nicolas Ferree86cd532012-10-31 06:04:57 +00001026 break;
1027 }
1028
1029 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001030 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001031
1032 /*
1033 * Link change detection isn't possible with RMII, so we'll
1034 * add that if/when we get our hands on a full-blown MII PHY.
1035 */
1036
Alexander Steinb19f7f72011-04-13 05:03:24 +00001037 if (status & MACB_BIT(ISR_ROVR)) {
1038 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001039 if (macb_is_gem(bp))
1040 bp->hw_stats.gem.rx_overruns++;
1041 else
1042 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001043
1044 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001045 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001046 }
1047
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001048 if (status & MACB_BIT(HRESP)) {
1049 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001050 * TODO: Reset the hardware, and maybe move the
1051 * netdev_err to a lower-priority context as well
1052 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001053 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001054 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001055
1056 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001057 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001058 }
1059
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001060 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001061 }
1062
1063 spin_unlock(&bp->lock);
1064
1065 return IRQ_HANDLED;
1066}
1067
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001068#ifdef CONFIG_NET_POLL_CONTROLLER
1069/*
1070 * Polling receive - used by netconsole and other diagnostic tools
1071 * to allow network i/o with interrupts disabled.
1072 */
1073static void macb_poll_controller(struct net_device *dev)
1074{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001075 struct macb *bp = netdev_priv(dev);
1076 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001077 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001078 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001079
1080 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001081 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1082 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001083 local_irq_restore(flags);
1084}
1085#endif
1086
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001087static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
1088 unsigned int len)
1089{
1090 return (len + bp->max_tx_length - 1) / bp->max_tx_length;
1091}
1092
1093static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001094 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001095 struct sk_buff *skb)
1096{
1097 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001098 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001099 struct macb_tx_skb *tx_skb = NULL;
1100 struct macb_dma_desc *desc;
1101 unsigned int offset, size, count = 0;
1102 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1103 unsigned int eof = 1;
1104 u32 ctrl;
1105
1106 /* First, map non-paged data */
1107 len = skb_headlen(skb);
1108 offset = 0;
1109 while (len) {
1110 size = min(len, bp->max_tx_length);
1111 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001112 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001113
1114 mapping = dma_map_single(&bp->pdev->dev,
1115 skb->data + offset,
1116 size, DMA_TO_DEVICE);
1117 if (dma_mapping_error(&bp->pdev->dev, mapping))
1118 goto dma_error;
1119
1120 /* Save info to properly release resources */
1121 tx_skb->skb = NULL;
1122 tx_skb->mapping = mapping;
1123 tx_skb->size = size;
1124 tx_skb->mapped_as_page = false;
1125
1126 len -= size;
1127 offset += size;
1128 count++;
1129 tx_head++;
1130 }
1131
1132 /* Then, map paged data from fragments */
1133 for (f = 0; f < nr_frags; f++) {
1134 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1135
1136 len = skb_frag_size(frag);
1137 offset = 0;
1138 while (len) {
1139 size = min(len, bp->max_tx_length);
1140 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001141 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001142
1143 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1144 offset, size, DMA_TO_DEVICE);
1145 if (dma_mapping_error(&bp->pdev->dev, mapping))
1146 goto dma_error;
1147
1148 /* Save info to properly release resources */
1149 tx_skb->skb = NULL;
1150 tx_skb->mapping = mapping;
1151 tx_skb->size = size;
1152 tx_skb->mapped_as_page = true;
1153
1154 len -= size;
1155 offset += size;
1156 count++;
1157 tx_head++;
1158 }
1159 }
1160
1161 /* Should never happen */
1162 if (unlikely(tx_skb == NULL)) {
1163 netdev_err(bp->dev, "BUG! empty skb!\n");
1164 return 0;
1165 }
1166
1167 /* This is the last buffer of the frame: save socket buffer */
1168 tx_skb->skb = skb;
1169
1170 /* Update TX ring: update buffer descriptors in reverse order
1171 * to avoid race condition
1172 */
1173
1174 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1175 * to set the end of TX queue
1176 */
1177 i = tx_head;
1178 entry = macb_tx_ring_wrap(i);
1179 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001180 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001181 desc->ctrl = ctrl;
1182
1183 do {
1184 i--;
1185 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001186 tx_skb = &queue->tx_skb[entry];
1187 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001188
1189 ctrl = (u32)tx_skb->size;
1190 if (eof) {
1191 ctrl |= MACB_BIT(TX_LAST);
1192 eof = 0;
1193 }
1194 if (unlikely(entry == (TX_RING_SIZE - 1)))
1195 ctrl |= MACB_BIT(TX_WRAP);
1196
1197 /* Set TX buffer descriptor */
1198 desc->addr = tx_skb->mapping;
1199 /* desc->addr must be visible to hardware before clearing
1200 * 'TX_USED' bit in desc->ctrl.
1201 */
1202 wmb();
1203 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001204 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001205
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001206 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001207
1208 return count;
1209
1210dma_error:
1211 netdev_err(bp->dev, "TX DMA map failed\n");
1212
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001213 for (i = queue->tx_head; i != tx_head; i++) {
1214 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001215
1216 macb_tx_unmap(bp, tx_skb);
1217 }
1218
1219 return 0;
1220}
1221
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001222static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1223{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001224 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001225 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001226 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001227 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001228 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001230#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1231 netdev_vdbg(bp->dev,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001232 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1233 queue_index, skb->len, skb->head, skb->data,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001234 skb_tail_pointer(skb), skb_end_pointer(skb));
1235 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1236 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001237#endif
1238
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001239 /* Count how many TX buffer descriptors are needed to send this
1240 * socket buffer: skb fragments of jumbo frames may need to be
1241 * splitted into many buffer descriptors.
1242 */
1243 count = macb_count_tx_descriptors(bp, skb_headlen(skb));
1244 nr_frags = skb_shinfo(skb)->nr_frags;
1245 for (f = 0; f < nr_frags; f++) {
1246 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1247 count += macb_count_tx_descriptors(bp, frag_size);
1248 }
1249
Dongdong Deng48719532009-08-23 19:49:07 -07001250 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001251
1252 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001253 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1254 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001255 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001256 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001257 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001258 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001259 }
1260
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001261 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001262 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001263 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001264 goto unlock;
1265 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001266
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001267 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001268 wmb();
1269
Richard Cochrane0720922011-06-19 21:51:28 +00001270 skb_tx_timestamp(skb);
1271
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001272 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1273
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001274 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1275 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001276
Soren Brinkmann92030902014-03-04 08:46:39 -08001277unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001278 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001279
Patrick McHardy6ed10652009-06-23 06:03:08 +00001280 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001281}
1282
Nicolas Ferre4df95132013-06-04 21:57:12 +00001283static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001284{
1285 if (!macb_is_gem(bp)) {
1286 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1287 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001288 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001289
Nicolas Ferre1b447912013-06-04 21:57:11 +00001290 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001291 netdev_dbg(bp->dev,
1292 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001293 RX_BUFFER_MULTIPLE);
1294 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001295 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001296 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001297 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001298
1299 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1300 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001301}
1302
Nicolas Ferre4df95132013-06-04 21:57:12 +00001303static void gem_free_rx_buffers(struct macb *bp)
1304{
1305 struct sk_buff *skb;
1306 struct macb_dma_desc *desc;
1307 dma_addr_t addr;
1308 int i;
1309
1310 if (!bp->rx_skbuff)
1311 return;
1312
1313 for (i = 0; i < RX_RING_SIZE; i++) {
1314 skb = bp->rx_skbuff[i];
1315
1316 if (skb == NULL)
1317 continue;
1318
1319 desc = &bp->rx_ring[i];
1320 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001321 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001322 DMA_FROM_DEVICE);
1323 dev_kfree_skb_any(skb);
1324 skb = NULL;
1325 }
1326
1327 kfree(bp->rx_skbuff);
1328 bp->rx_skbuff = NULL;
1329}
1330
1331static void macb_free_rx_buffers(struct macb *bp)
1332{
1333 if (bp->rx_buffers) {
1334 dma_free_coherent(&bp->pdev->dev,
1335 RX_RING_SIZE * bp->rx_buffer_size,
1336 bp->rx_buffers, bp->rx_buffers_dma);
1337 bp->rx_buffers = NULL;
1338 }
1339}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001340
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001341static void macb_free_consistent(struct macb *bp)
1342{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001343 struct macb_queue *queue;
1344 unsigned int q;
1345
Nicolas Ferre4df95132013-06-04 21:57:12 +00001346 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001347 if (bp->rx_ring) {
1348 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1349 bp->rx_ring, bp->rx_ring_dma);
1350 bp->rx_ring = NULL;
1351 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001352
1353 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1354 kfree(queue->tx_skb);
1355 queue->tx_skb = NULL;
1356 if (queue->tx_ring) {
1357 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1358 queue->tx_ring, queue->tx_ring_dma);
1359 queue->tx_ring = NULL;
1360 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001361 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001362}
1363
1364static int gem_alloc_rx_buffers(struct macb *bp)
1365{
1366 int size;
1367
1368 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1369 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1370 if (!bp->rx_skbuff)
1371 return -ENOMEM;
1372 else
1373 netdev_dbg(bp->dev,
1374 "Allocated %d RX struct sk_buff entries at %p\n",
1375 RX_RING_SIZE, bp->rx_skbuff);
1376 return 0;
1377}
1378
1379static int macb_alloc_rx_buffers(struct macb *bp)
1380{
1381 int size;
1382
1383 size = RX_RING_SIZE * bp->rx_buffer_size;
1384 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1385 &bp->rx_buffers_dma, GFP_KERNEL);
1386 if (!bp->rx_buffers)
1387 return -ENOMEM;
1388 else
1389 netdev_dbg(bp->dev,
1390 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1391 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1392 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001393}
1394
1395static int macb_alloc_consistent(struct macb *bp)
1396{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001397 struct macb_queue *queue;
1398 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001399 int size;
1400
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001401 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1402 size = TX_RING_BYTES;
1403 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1404 &queue->tx_ring_dma,
1405 GFP_KERNEL);
1406 if (!queue->tx_ring)
1407 goto out_err;
1408 netdev_dbg(bp->dev,
1409 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1410 q, size, (unsigned long)queue->tx_ring_dma,
1411 queue->tx_ring);
1412
1413 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1414 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1415 if (!queue->tx_skb)
1416 goto out_err;
1417 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001418
1419 size = RX_RING_BYTES;
1420 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1421 &bp->rx_ring_dma, GFP_KERNEL);
1422 if (!bp->rx_ring)
1423 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001424 netdev_dbg(bp->dev,
1425 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1426 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427
Nicolas Ferre4df95132013-06-04 21:57:12 +00001428 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001429 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001430
1431 return 0;
1432
1433out_err:
1434 macb_free_consistent(bp);
1435 return -ENOMEM;
1436}
1437
Nicolas Ferre4df95132013-06-04 21:57:12 +00001438static void gem_init_rings(struct macb *bp)
1439{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001440 struct macb_queue *queue;
1441 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001442 int i;
1443
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001444 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1445 for (i = 0; i < TX_RING_SIZE; i++) {
1446 queue->tx_ring[i].addr = 0;
1447 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1448 }
1449 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1450 queue->tx_head = 0;
1451 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001452 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001453
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001454 bp->rx_tail = 0;
1455 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001456
1457 gem_rx_refill(bp);
1458}
1459
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001460static void macb_init_rings(struct macb *bp)
1461{
1462 int i;
1463 dma_addr_t addr;
1464
1465 addr = bp->rx_buffers_dma;
1466 for (i = 0; i < RX_RING_SIZE; i++) {
1467 bp->rx_ring[i].addr = addr;
1468 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001469 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001470 }
1471 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1472
1473 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001474 bp->queues[0].tx_ring[i].addr = 0;
1475 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
1476 bp->queues[0].tx_head = 0;
1477 bp->queues[0].tx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001478 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001479 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001480
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001481 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001482}
1483
1484static void macb_reset_hw(struct macb *bp)
1485{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001486 struct macb_queue *queue;
1487 unsigned int q;
1488
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001489 /*
1490 * Disable RX and TX (XXX: Should we halt the transmission
1491 * more gracefully?)
1492 */
1493 macb_writel(bp, NCR, 0);
1494
1495 /* Clear the stats registers (XXX: Update stats first?) */
1496 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1497
1498 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001499 macb_writel(bp, TSR, -1);
1500 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001501
1502 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001503 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1504 queue_writel(queue, IDR, -1);
1505 queue_readl(queue, ISR);
1506 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001507}
1508
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001509static u32 gem_mdc_clk_div(struct macb *bp)
1510{
1511 u32 config;
1512 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1513
1514 if (pclk_hz <= 20000000)
1515 config = GEM_BF(CLK, GEM_CLK_DIV8);
1516 else if (pclk_hz <= 40000000)
1517 config = GEM_BF(CLK, GEM_CLK_DIV16);
1518 else if (pclk_hz <= 80000000)
1519 config = GEM_BF(CLK, GEM_CLK_DIV32);
1520 else if (pclk_hz <= 120000000)
1521 config = GEM_BF(CLK, GEM_CLK_DIV48);
1522 else if (pclk_hz <= 160000000)
1523 config = GEM_BF(CLK, GEM_CLK_DIV64);
1524 else
1525 config = GEM_BF(CLK, GEM_CLK_DIV96);
1526
1527 return config;
1528}
1529
1530static u32 macb_mdc_clk_div(struct macb *bp)
1531{
1532 u32 config;
1533 unsigned long pclk_hz;
1534
1535 if (macb_is_gem(bp))
1536 return gem_mdc_clk_div(bp);
1537
1538 pclk_hz = clk_get_rate(bp->pclk);
1539 if (pclk_hz <= 20000000)
1540 config = MACB_BF(CLK, MACB_CLK_DIV8);
1541 else if (pclk_hz <= 40000000)
1542 config = MACB_BF(CLK, MACB_CLK_DIV16);
1543 else if (pclk_hz <= 80000000)
1544 config = MACB_BF(CLK, MACB_CLK_DIV32);
1545 else
1546 config = MACB_BF(CLK, MACB_CLK_DIV64);
1547
1548 return config;
1549}
1550
Jamie Iles757a03c2011-03-09 16:29:59 +00001551/*
1552 * Get the DMA bus width field of the network configuration register that we
1553 * should program. We find the width from decoding the design configuration
1554 * register to find the maximum supported data bus width.
1555 */
1556static u32 macb_dbw(struct macb *bp)
1557{
1558 if (!macb_is_gem(bp))
1559 return 0;
1560
1561 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1562 case 4:
1563 return GEM_BF(DBW, GEM_DBW128);
1564 case 2:
1565 return GEM_BF(DBW, GEM_DBW64);
1566 case 1:
1567 default:
1568 return GEM_BF(DBW, GEM_DBW32);
1569 }
1570}
1571
Jamie Iles0116da42011-03-14 17:38:30 +00001572/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001573 * Configure the receive DMA engine
1574 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001575 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001576 * (if not supported by FIFO, it will fallback to default)
1577 * - set both rx/tx packet buffers to full memory size
1578 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001579 */
1580static void macb_configure_dma(struct macb *bp)
1581{
1582 u32 dmacfg;
Arun Chandran62f69242015-03-01 11:38:02 +05301583 u32 tmp, ncr;
Jamie Iles0116da42011-03-14 17:38:30 +00001584
1585 if (macb_is_gem(bp)) {
1586 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001587 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001588 if (bp->dma_burst_length)
1589 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001590 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301591 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301592
1593 /* Find the CPU endianness by using the loopback bit of net_ctrl
1594 * register. save it first. When the CPU is in big endian we
1595 * need to program swaped mode for management descriptor access.
1596 */
1597 ncr = macb_readl(bp, NCR);
1598 __raw_writel(MACB_BIT(LLB), bp->regs + MACB_NCR);
1599 tmp = __raw_readl(bp->regs + MACB_NCR);
1600
1601 if (tmp == MACB_BIT(LLB))
1602 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1603 else
1604 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1605
1606 /* Restore net_ctrl */
1607 macb_writel(bp, NCR, ncr);
1608
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001609 if (bp->dev->features & NETIF_F_HW_CSUM)
1610 dmacfg |= GEM_BIT(TXCOEN);
1611 else
1612 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001613 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1614 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001615 gem_writel(bp, DMACFG, dmacfg);
1616 }
1617}
1618
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001619static void macb_init_hw(struct macb *bp)
1620{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001621 struct macb_queue *queue;
1622 unsigned int q;
1623
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001624 u32 config;
1625
1626 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001627 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001628
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001629 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001630 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001631 config |= MACB_BIT(PAE); /* PAuse Enable */
1632 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001633 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001634 if (bp->dev->flags & IFF_PROMISC)
1635 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001636 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1637 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001638 if (!(bp->dev->flags & IFF_BROADCAST))
1639 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001640 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001641 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001642 bp->speed = SPEED_10;
1643 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001644
Jamie Iles0116da42011-03-14 17:38:30 +00001645 macb_configure_dma(bp);
1646
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001647 /* Initialize TX and RX buffers */
1648 macb_writel(bp, RBQP, bp->rx_ring_dma);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001649 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1650 queue_writel(queue, TBQP, queue->tx_ring_dma);
1651
1652 /* Enable interrupts */
1653 queue_writel(queue, IER,
1654 MACB_RX_INT_FLAGS |
1655 MACB_TX_INT_FLAGS |
1656 MACB_BIT(HRESP));
1657 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001658
1659 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001660 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001661}
1662
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001663/*
1664 * The hash address register is 64 bits long and takes up two
1665 * locations in the memory map. The least significant bits are stored
1666 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1667 *
1668 * The unicast hash enable and the multicast hash enable bits in the
1669 * network configuration register enable the reception of hash matched
1670 * frames. The destination address is reduced to a 6 bit index into
1671 * the 64 bit hash register using the following hash function. The
1672 * hash function is an exclusive or of every sixth bit of the
1673 * destination address.
1674 *
1675 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1676 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1677 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1678 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1679 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1680 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1681 *
1682 * da[0] represents the least significant bit of the first byte
1683 * received, that is, the multicast/unicast indicator, and da[47]
1684 * represents the most significant bit of the last byte received. If
1685 * the hash index, hi[n], points to a bit that is set in the hash
1686 * register then the frame will be matched according to whether the
1687 * frame is multicast or unicast. A multicast match will be signalled
1688 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1689 * index points to a bit set in the hash register. A unicast match
1690 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1691 * and the hash index points to a bit set in the hash register. To
1692 * receive all multicast frames, the hash register should be set with
1693 * all ones and the multicast hash enable bit should be set in the
1694 * network configuration register.
1695 */
1696
1697static inline int hash_bit_value(int bitnr, __u8 *addr)
1698{
1699 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1700 return 1;
1701 return 0;
1702}
1703
1704/*
1705 * Return the hash index value for the specified address.
1706 */
1707static int hash_get_index(__u8 *addr)
1708{
1709 int i, j, bitval;
1710 int hash_index = 0;
1711
1712 for (j = 0; j < 6; j++) {
1713 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001714 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001715
1716 hash_index |= (bitval << j);
1717 }
1718
1719 return hash_index;
1720}
1721
1722/*
1723 * Add multicast addresses to the internal multicast-hash table.
1724 */
1725static void macb_sethashtable(struct net_device *dev)
1726{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001727 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001728 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001729 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001730 struct macb *bp = netdev_priv(dev);
1731
1732 mc_filter[0] = mc_filter[1] = 0;
1733
Jiri Pirko22bedad32010-04-01 21:22:57 +00001734 netdev_for_each_mc_addr(ha, dev) {
1735 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001736 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1737 }
1738
Jamie Ilesf75ba502011-11-08 10:12:32 +00001739 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1740 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001741}
1742
1743/*
1744 * Enable/Disable promiscuous and multicast modes.
1745 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001746void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001747{
1748 unsigned long cfg;
1749 struct macb *bp = netdev_priv(dev);
1750
1751 cfg = macb_readl(bp, NCFGR);
1752
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001753 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001754 /* Enable promiscuous mode */
1755 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001756
1757 /* Disable RX checksum offload */
1758 if (macb_is_gem(bp))
1759 cfg &= ~GEM_BIT(RXCOEN);
1760 } else {
1761 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001762 cfg &= ~MACB_BIT(CAF);
1763
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001764 /* Enable RX checksum offload only if requested */
1765 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1766 cfg |= GEM_BIT(RXCOEN);
1767 }
1768
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001769 if (dev->flags & IFF_ALLMULTI) {
1770 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001771 macb_or_gem_writel(bp, HRB, -1);
1772 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001773 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001774 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001775 /* Enable specific multicasts */
1776 macb_sethashtable(dev);
1777 cfg |= MACB_BIT(NCFGR_MTI);
1778 } else if (dev->flags & (~IFF_ALLMULTI)) {
1779 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001780 macb_or_gem_writel(bp, HRB, 0);
1781 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001782 cfg &= ~MACB_BIT(NCFGR_MTI);
1783 }
1784
1785 macb_writel(bp, NCFGR, cfg);
1786}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001787EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001788
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001789static int macb_open(struct net_device *dev)
1790{
1791 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001792 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001793 int err;
1794
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001795 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001796
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001797 /* carrier starts down */
1798 netif_carrier_off(dev);
1799
frederic RODO6c36a702007-07-12 19:07:24 +02001800 /* if the phy is not yet register, retry later*/
1801 if (!bp->phy_dev)
1802 return -EAGAIN;
1803
Nicolas Ferre1b447912013-06-04 21:57:11 +00001804 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001805 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001806
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001807 err = macb_alloc_consistent(bp);
1808 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001809 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1810 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001811 return err;
1812 }
1813
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001814 napi_enable(&bp->napi);
1815
Nicolas Ferre4df95132013-06-04 21:57:12 +00001816 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001817 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001818
frederic RODO6c36a702007-07-12 19:07:24 +02001819 /* schedule a link state check */
1820 phy_start(bp->phy_dev);
1821
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001822 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001823
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001824 return 0;
1825}
1826
1827static int macb_close(struct net_device *dev)
1828{
1829 struct macb *bp = netdev_priv(dev);
1830 unsigned long flags;
1831
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001832 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001833 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001834
frederic RODO6c36a702007-07-12 19:07:24 +02001835 if (bp->phy_dev)
1836 phy_stop(bp->phy_dev);
1837
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001838 spin_lock_irqsave(&bp->lock, flags);
1839 macb_reset_hw(bp);
1840 netif_carrier_off(dev);
1841 spin_unlock_irqrestore(&bp->lock, flags);
1842
1843 macb_free_consistent(bp);
1844
1845 return 0;
1846}
1847
Jamie Ilesa494ed82011-03-09 16:26:35 +00001848static void gem_update_stats(struct macb *bp)
1849{
Xander Huff3ff13f12015-01-13 16:15:51 -06001850 int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001851 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001852
Xander Huff3ff13f12015-01-13 16:15:51 -06001853 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1854 u32 offset = gem_statistics[i].offset;
Arun Chandrana50dad32015-02-18 16:59:35 +05301855 u64 val = readl_relaxed(bp->regs + offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001856
1857 bp->ethtool_stats[i] += val;
1858 *p += val;
1859
1860 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1861 /* Add GEM_OCTTXH, GEM_OCTRXH */
Arun Chandrana50dad32015-02-18 16:59:35 +05301862 val = readl_relaxed(bp->regs + offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06001863 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06001864 *(++p) += val;
1865 }
1866 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00001867}
1868
1869static struct net_device_stats *gem_get_stats(struct macb *bp)
1870{
1871 struct gem_stats *hwstat = &bp->hw_stats.gem;
1872 struct net_device_stats *nstat = &bp->stats;
1873
1874 gem_update_stats(bp);
1875
1876 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1877 hwstat->rx_alignment_errors +
1878 hwstat->rx_resource_errors +
1879 hwstat->rx_overruns +
1880 hwstat->rx_oversize_frames +
1881 hwstat->rx_jabbers +
1882 hwstat->rx_undersized_frames +
1883 hwstat->rx_length_field_frame_errors);
1884 nstat->tx_errors = (hwstat->tx_late_collisions +
1885 hwstat->tx_excessive_collisions +
1886 hwstat->tx_underrun +
1887 hwstat->tx_carrier_sense_errors);
1888 nstat->multicast = hwstat->rx_multicast_frames;
1889 nstat->collisions = (hwstat->tx_single_collision_frames +
1890 hwstat->tx_multiple_collision_frames +
1891 hwstat->tx_excessive_collisions);
1892 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1893 hwstat->rx_jabbers +
1894 hwstat->rx_undersized_frames +
1895 hwstat->rx_length_field_frame_errors);
1896 nstat->rx_over_errors = hwstat->rx_resource_errors;
1897 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1898 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1899 nstat->rx_fifo_errors = hwstat->rx_overruns;
1900 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1901 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1902 nstat->tx_fifo_errors = hwstat->tx_underrun;
1903
1904 return nstat;
1905}
1906
Xander Huff3ff13f12015-01-13 16:15:51 -06001907static void gem_get_ethtool_stats(struct net_device *dev,
1908 struct ethtool_stats *stats, u64 *data)
1909{
1910 struct macb *bp;
1911
1912 bp = netdev_priv(dev);
1913 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06001914 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06001915}
1916
1917static int gem_get_sset_count(struct net_device *dev, int sset)
1918{
1919 switch (sset) {
1920 case ETH_SS_STATS:
1921 return GEM_STATS_LEN;
1922 default:
1923 return -EOPNOTSUPP;
1924 }
1925}
1926
1927static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
1928{
1929 int i;
1930
1931 switch (sset) {
1932 case ETH_SS_STATS:
1933 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
1934 memcpy(p, gem_statistics[i].stat_string,
1935 ETH_GSTRING_LEN);
1936 break;
1937 }
1938}
1939
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001940struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001941{
1942 struct macb *bp = netdev_priv(dev);
1943 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001944 struct macb_stats *hwstat = &bp->hw_stats.macb;
1945
1946 if (macb_is_gem(bp))
1947 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001948
frederic RODO6c36a702007-07-12 19:07:24 +02001949 /* read stats from hardware */
1950 macb_update_stats(bp);
1951
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001952 /* Convert HW stats into netdevice stats */
1953 nstat->rx_errors = (hwstat->rx_fcs_errors +
1954 hwstat->rx_align_errors +
1955 hwstat->rx_resource_errors +
1956 hwstat->rx_overruns +
1957 hwstat->rx_oversize_pkts +
1958 hwstat->rx_jabbers +
1959 hwstat->rx_undersize_pkts +
1960 hwstat->sqe_test_errors +
1961 hwstat->rx_length_mismatch);
1962 nstat->tx_errors = (hwstat->tx_late_cols +
1963 hwstat->tx_excessive_cols +
1964 hwstat->tx_underruns +
1965 hwstat->tx_carrier_errors);
1966 nstat->collisions = (hwstat->tx_single_cols +
1967 hwstat->tx_multiple_cols +
1968 hwstat->tx_excessive_cols);
1969 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1970 hwstat->rx_jabbers +
1971 hwstat->rx_undersize_pkts +
1972 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001973 nstat->rx_over_errors = hwstat->rx_resource_errors +
1974 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001975 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1976 nstat->rx_frame_errors = hwstat->rx_align_errors;
1977 nstat->rx_fifo_errors = hwstat->rx_overruns;
1978 /* XXX: What does "missed" mean? */
1979 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1980 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1981 nstat->tx_fifo_errors = hwstat->tx_underruns;
1982 /* Don't know about heartbeat or window errors... */
1983
1984 return nstat;
1985}
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001986EXPORT_SYMBOL_GPL(macb_get_stats);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001987
1988static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1989{
1990 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001991 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992
frederic RODO6c36a702007-07-12 19:07:24 +02001993 if (!phydev)
1994 return -ENODEV;
1995
1996 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001997}
1998
1999static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2000{
2001 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002002 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002003
frederic RODO6c36a702007-07-12 19:07:24 +02002004 if (!phydev)
2005 return -ENODEV;
2006
2007 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002008}
2009
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002010static int macb_get_regs_len(struct net_device *netdev)
2011{
2012 return MACB_GREGS_NBR * sizeof(u32);
2013}
2014
2015static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2016 void *p)
2017{
2018 struct macb *bp = netdev_priv(dev);
2019 unsigned int tail, head;
2020 u32 *regs_buff = p;
2021
2022 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2023 | MACB_GREGS_VERSION;
2024
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002025 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2026 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002027
2028 regs_buff[0] = macb_readl(bp, NCR);
2029 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2030 regs_buff[2] = macb_readl(bp, NSR);
2031 regs_buff[3] = macb_readl(bp, TSR);
2032 regs_buff[4] = macb_readl(bp, RBQP);
2033 regs_buff[5] = macb_readl(bp, TBQP);
2034 regs_buff[6] = macb_readl(bp, RSR);
2035 regs_buff[7] = macb_readl(bp, IMR);
2036
2037 regs_buff[8] = tail;
2038 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002039 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2040 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002041
2042 if (macb_is_gem(bp)) {
2043 regs_buff[12] = gem_readl(bp, USRIO);
2044 regs_buff[13] = gem_readl(bp, DMACFG);
2045 }
2046}
2047
Joachim Eastwood0005f542012-10-18 11:01:12 +00002048const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002049 .get_settings = macb_get_settings,
2050 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002051 .get_regs_len = macb_get_regs_len,
2052 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002053 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002054 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff8cd5a562015-01-15 15:55:20 -06002055};
2056EXPORT_SYMBOL_GPL(macb_ethtool_ops);
2057
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002058static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002059 .get_settings = macb_get_settings,
2060 .set_settings = macb_set_settings,
2061 .get_regs_len = macb_get_regs_len,
2062 .get_regs = macb_get_regs,
2063 .get_link = ethtool_op_get_link,
2064 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002065 .get_ethtool_stats = gem_get_ethtool_stats,
2066 .get_strings = gem_get_ethtool_strings,
2067 .get_sset_count = gem_get_sset_count,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002068};
2069
Joachim Eastwood0005f542012-10-18 11:01:12 +00002070int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002071{
2072 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002073 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002074
2075 if (!netif_running(dev))
2076 return -EINVAL;
2077
frederic RODO6c36a702007-07-12 19:07:24 +02002078 if (!phydev)
2079 return -ENODEV;
2080
Richard Cochran28b04112010-07-17 08:48:55 +00002081 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002082}
Joachim Eastwood0005f542012-10-18 11:01:12 +00002083EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002085static int macb_set_features(struct net_device *netdev,
2086 netdev_features_t features)
2087{
2088 struct macb *bp = netdev_priv(netdev);
2089 netdev_features_t changed = features ^ netdev->features;
2090
2091 /* TX checksum offload */
2092 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2093 u32 dmacfg;
2094
2095 dmacfg = gem_readl(bp, DMACFG);
2096 if (features & NETIF_F_HW_CSUM)
2097 dmacfg |= GEM_BIT(TXCOEN);
2098 else
2099 dmacfg &= ~GEM_BIT(TXCOEN);
2100 gem_writel(bp, DMACFG, dmacfg);
2101 }
2102
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002103 /* RX checksum offload */
2104 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2105 u32 netcfg;
2106
2107 netcfg = gem_readl(bp, NCFGR);
2108 if (features & NETIF_F_RXCSUM &&
2109 !(netdev->flags & IFF_PROMISC))
2110 netcfg |= GEM_BIT(RXCOEN);
2111 else
2112 netcfg &= ~GEM_BIT(RXCOEN);
2113 gem_writel(bp, NCFGR, netcfg);
2114 }
2115
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002116 return 0;
2117}
2118
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002119static const struct net_device_ops macb_netdev_ops = {
2120 .ndo_open = macb_open,
2121 .ndo_stop = macb_close,
2122 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002123 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002124 .ndo_get_stats = macb_get_stats,
2125 .ndo_do_ioctl = macb_ioctl,
2126 .ndo_validate_addr = eth_validate_addr,
2127 .ndo_change_mtu = eth_change_mtu,
2128 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002129#ifdef CONFIG_NET_POLL_CONTROLLER
2130 .ndo_poll_controller = macb_poll_controller,
2131#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002132 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002133};
2134
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002135#if defined(CONFIG_OF)
Boris BREZILLONa8487482015-03-07 07:23:30 +01002136static struct macb_config at91sam9260_config = {
2137 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII,
2138};
2139
Nicolas Ferree1755872014-07-24 13:50:58 +02002140static struct macb_config pc302gem_config = {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002141 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2142 .dma_burst_length = 16,
2143};
2144
2145static struct macb_config sama5d3_config = {
2146 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
Nicolas Ferree1755872014-07-24 13:50:58 +02002147 .dma_burst_length = 16,
2148};
2149
Cyrille Pitchen4b7b0e42014-07-24 13:51:03 +02002150static struct macb_config sama5d4_config = {
2151 .caps = 0,
2152 .dma_burst_length = 4,
2153};
2154
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002155static const struct of_device_id macb_dt_ids[] = {
2156 { .compatible = "cdns,at32ap7000-macb" },
Boris BREZILLONa8487482015-03-07 07:23:30 +01002157 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002158 { .compatible = "cdns,macb" },
Nicolas Ferree1755872014-07-24 13:50:58 +02002159 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2160 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002161 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Cyrille Pitchen4b7b0e42014-07-24 13:51:03 +02002162 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002163 { /* sentinel */ }
2164};
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002165MODULE_DEVICE_TABLE(of, macb_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002166#endif
2167
Nicolas Ferree1755872014-07-24 13:50:58 +02002168/*
2169 * Configure peripheral capacities according to device tree
2170 * and integration options used
2171 */
2172static void macb_configure_caps(struct macb *bp)
2173{
2174 u32 dcfg;
2175 const struct of_device_id *match;
2176 const struct macb_config *config;
2177
2178 if (bp->pdev->dev.of_node) {
2179 match = of_match_node(macb_dt_ids, bp->pdev->dev.of_node);
2180 if (match && match->data) {
2181 config = (const struct macb_config *)match->data;
2182
2183 bp->caps = config->caps;
2184 /*
2185 * As we have access to the matching node, configure
2186 * DMA burst length as well
2187 */
2188 bp->dma_burst_length = config->dma_burst_length;
2189 }
2190 }
2191
2192 if (MACB_BFEXT(IDNUM, macb_readl(bp, MID)) == 0x2)
2193 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2194
2195 if (macb_is_gem(bp)) {
2196 dcfg = gem_readl(bp, DCFG1);
2197 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2198 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2199 dcfg = gem_readl(bp, DCFG2);
2200 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2201 bp->caps |= MACB_CAPS_FIFO_MODE;
2202 }
2203
2204 netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
2205}
2206
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002207static void macb_probe_queues(void __iomem *mem,
2208 unsigned int *queue_mask,
2209 unsigned int *num_queues)
2210{
2211 unsigned int hw_q;
2212 u32 mid;
2213
2214 *queue_mask = 0x1;
2215 *num_queues = 1;
2216
2217 /* is it macb or gem ? */
Arun Chandrana50dad32015-02-18 16:59:35 +05302218 mid = readl_relaxed(mem + MACB_MID);
2219
Punnaiah Choudary Kalluri8a013a92015-03-06 18:29:11 +01002220 if (MACB_BFEXT(IDNUM, mid) < 0x2)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002221 return;
2222
2223 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302224 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2225
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002226 *queue_mask |= 0x1;
2227
2228 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2229 if (*queue_mask & (1 << hw_q))
2230 (*num_queues)++;
2231}
2232
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002233static int macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002234{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00002235 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002236 struct resource *regs;
2237 struct net_device *dev;
2238 struct macb *bp;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002239 struct macb_queue *queue;
frederic RODO6c36a702007-07-12 19:07:24 +02002240 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002241 u32 config;
2242 int err = -ENXIO;
Guenter Roeck50907042013-04-02 09:35:09 +00002243 const char *mac;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002244 void __iomem *mem;
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002245 unsigned int hw_q, queue_mask, q, num_queues;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002246 struct clk *pclk, *hclk, *tx_clk;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002247
2248 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2249 if (!regs) {
2250 dev_err(&pdev->dev, "no mmio resource defined\n");
2251 goto err_out;
2252 }
2253
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002254 pclk = devm_clk_get(&pdev->dev, "pclk");
2255 if (IS_ERR(pclk)) {
2256 err = PTR_ERR(pclk);
2257 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002258 goto err_out;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002259 }
2260
2261 hclk = devm_clk_get(&pdev->dev, "hclk");
2262 if (IS_ERR(hclk)) {
2263 err = PTR_ERR(hclk);
2264 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2265 goto err_out;
2266 }
2267
2268 tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002269 if (IS_ERR(tx_clk))
2270 tx_clk = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002271
2272 err = clk_prepare_enable(pclk);
2273 if (err) {
2274 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2275 goto err_out;
2276 }
2277
2278 err = clk_prepare_enable(hclk);
2279 if (err) {
2280 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2281 goto err_out_disable_pclk;
2282 }
2283
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002284 err = clk_prepare_enable(tx_clk);
2285 if (err) {
2286 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2287 goto err_out_disable_hclk;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002288 }
2289
2290 err = -ENOMEM;
2291 mem = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
2292 if (!mem) {
2293 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
2294 goto err_out_disable_clocks;
2295 }
2296
2297 macb_probe_queues(mem, &queue_mask, &num_queues);
2298 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
2299 if (!dev)
2300 goto err_out_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002301
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002302 SET_NETDEV_DEV(dev, &pdev->dev);
2303
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002304 bp = netdev_priv(dev);
2305 bp->pdev = pdev;
2306 bp->dev = dev;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002307 bp->regs = mem;
2308 bp->num_queues = num_queues;
2309 bp->pclk = pclk;
2310 bp->hclk = hclk;
2311 bp->tx_clk = tx_clk;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002312
2313 spin_lock_init(&bp->lock);
2314
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002315 /* set the queue register mapping once for all: queue0 has a special
2316 * register mapping but we don't want to test the queue index then
2317 * compute the corresponding register offset at run time.
2318 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002319 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002320 if (!(queue_mask & (1 << hw_q)))
2321 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002322
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002323 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002324 queue->bp = bp;
2325 if (hw_q) {
2326 queue->ISR = GEM_ISR(hw_q - 1);
2327 queue->IER = GEM_IER(hw_q - 1);
2328 queue->IDR = GEM_IDR(hw_q - 1);
2329 queue->IMR = GEM_IMR(hw_q - 1);
2330 queue->TBQP = GEM_TBQP(hw_q - 1);
2331 } else {
2332 /* queue0 uses legacy registers */
2333 queue->ISR = MACB_ISR;
2334 queue->IER = MACB_IER;
2335 queue->IDR = MACB_IDR;
2336 queue->IMR = MACB_IMR;
2337 queue->TBQP = MACB_TBQP;
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002338 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002339
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002340 /* get irq: here we use the linux queue index, not the hardware
2341 * queue index. the queue irq definitions in the device tree
2342 * must remove the optional gaps that could exist in the
2343 * hardware queue mask.
2344 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002345 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002346 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002347 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002348 if (err) {
2349 dev_err(&pdev->dev,
2350 "Unable to request IRQ %d (error %d)\n",
2351 queue->irq, err);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002352 goto err_out_free_netdev;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002353 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002354
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002355 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002356 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002357 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002358 dev->irq = bp->queues[0].irq;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002359
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002360 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002361 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002362
2363 dev->base_addr = regs->start;
2364
Nicolas Ferree1755872014-07-24 13:50:58 +02002365 /* setup capacities */
2366 macb_configure_caps(bp);
2367
Nicolas Ferre4df95132013-06-04 21:57:12 +00002368 /* setup appropriated routines according to adapter type */
2369 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002370 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002371 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2372 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2373 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2374 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002375 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002376 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002377 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002378 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2379 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2380 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2381 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002382 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002383 }
2384
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002385 /* Set features */
2386 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002387 /* Checksum offload is only available on gem with packet buffer */
2388 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002389 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002390 if (bp->caps & MACB_CAPS_SG_DISABLED)
2391 dev->hw_features &= ~NETIF_F_SG;
2392 dev->features = dev->hw_features;
2393
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002394 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002395 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00002396 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002397 macb_writel(bp, NCFGR, config);
2398
Guenter Roeck50907042013-04-02 09:35:09 +00002399 mac = of_get_mac_address(pdev->dev.of_node);
2400 if (mac)
2401 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2402 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002403 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002404
Guenter Roeck50907042013-04-02 09:35:09 +00002405 err = of_get_phy_mode(pdev->dev.of_node);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002406 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002407 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002408 if (pdata && pdata->is_rmii)
2409 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2410 else
2411 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2412 } else {
2413 bp->phy_interface = err;
2414 }
2415
Boris BREZILLONa8487482015-03-07 07:23:30 +01002416 config = 0;
Patrice Vilchez140b7552012-10-31 06:04:50 +00002417 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
Boris BREZILLONa8487482015-03-07 07:23:30 +01002418 config = GEM_BIT(RGMII);
2419 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
2420 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2421 config = MACB_BIT(RMII);
2422 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2423 config = MACB_BIT(MII);
2424
2425 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2426 config |= MACB_BIT(CLKEN);
2427
2428 macb_or_gem_writel(bp, USRIO, config);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002429
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002430 err = register_netdev(dev);
2431 if (err) {
2432 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002433 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002434 }
2435
Nicolas Ferre72ca8202013-04-14 22:04:33 +00002436 err = macb_mii_init(bp);
2437 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02002438 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002439
frederic RODO6c36a702007-07-12 19:07:24 +02002440 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002441
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002442 netif_carrier_off(dev);
2443
Bo Shen58798232014-09-13 01:57:49 +02002444 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
2445 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
2446 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002447
frederic RODO6c36a702007-07-12 19:07:24 +02002448 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002449 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2450 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002451
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002452 return 0;
2453
frederic RODO6c36a702007-07-12 19:07:24 +02002454err_out_unregister_netdev:
2455 unregister_netdev(dev);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002456err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002457 free_netdev(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002458err_out_disable_clocks:
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002459 clk_disable_unprepare(tx_clk);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002460err_out_disable_hclk:
2461 clk_disable_unprepare(hclk);
2462err_out_disable_pclk:
2463 clk_disable_unprepare(pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002464err_out:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002465 return err;
2466}
2467
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002468static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002469{
2470 struct net_device *dev;
2471 struct macb *bp;
2472
2473 dev = platform_get_drvdata(pdev);
2474
2475 if (dev) {
2476 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002477 if (bp->phy_dev)
2478 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002479 mdiobus_unregister(bp->mii_bus);
2480 kfree(bp->mii_bus->irq);
2481 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002482 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002483 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002484 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002485 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01002486 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002487 }
2488
2489 return 0;
2490}
2491
Michal Simekd23823d2015-01-23 09:36:03 +01002492static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002493{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002494 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002495 struct net_device *netdev = platform_get_drvdata(pdev);
2496 struct macb *bp = netdev_priv(netdev);
2497
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002498 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002499 netif_device_detach(netdev);
2500
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002501 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002502 clk_disable_unprepare(bp->hclk);
2503 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002504
2505 return 0;
2506}
2507
Michal Simekd23823d2015-01-23 09:36:03 +01002508static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002509{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002510 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002511 struct net_device *netdev = platform_get_drvdata(pdev);
2512 struct macb *bp = netdev_priv(netdev);
2513
Steffen Trumtrarace58012013-03-27 23:07:07 +00002514 clk_prepare_enable(bp->pclk);
2515 clk_prepare_enable(bp->hclk);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002516 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002517
2518 netif_device_attach(netdev);
2519
2520 return 0;
2521}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002522
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002523static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
2524
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002525static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002526 .probe = macb_probe,
2527 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002528 .driver = {
2529 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002530 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002531 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002532 },
2533};
2534
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002535module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002536
2537MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00002538MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002539MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07002540MODULE_ALIAS("platform:macb");