blob: f2f9ca00567b2c1660fe6372c3b935d215989271 [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
216 switch (speed) {
217 case SPEED_10:
218 rate = 2500000;
219 break;
220 case SPEED_100:
221 rate = 25000000;
222 break;
223 case SPEED_1000:
224 rate = 125000000;
225 break;
226 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800227 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800228 }
229
230 rate_rounded = clk_round_rate(clk, rate);
231 if (rate_rounded < 0)
232 return;
233
234 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
235 * is not satisfied.
236 */
237 ferr = abs(rate_rounded - rate);
238 ferr = DIV_ROUND_UP(ferr, rate / 100000);
239 if (ferr > 5)
240 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
241 rate);
242
243 if (clk_set_rate(clk, rate_rounded))
244 netdev_err(dev, "adjusting tx_clk failed.\n");
245}
246
frederic RODO6c36a702007-07-12 19:07:24 +0200247static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100248{
frederic RODO6c36a702007-07-12 19:07:24 +0200249 struct macb *bp = netdev_priv(dev);
250 struct phy_device *phydev = bp->phy_dev;
251 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100252
frederic RODO6c36a702007-07-12 19:07:24 +0200253 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100254
frederic RODO6c36a702007-07-12 19:07:24 +0200255 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100256
frederic RODO6c36a702007-07-12 19:07:24 +0200257 if (phydev->link) {
258 if ((bp->speed != phydev->speed) ||
259 (bp->duplex != phydev->duplex)) {
260 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100261
frederic RODO6c36a702007-07-12 19:07:24 +0200262 reg = macb_readl(bp, NCFGR);
263 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000264 if (macb_is_gem(bp))
265 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200266
267 if (phydev->duplex)
268 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900269 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200270 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200271 if (phydev->speed == SPEED_1000 &&
272 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000273 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200274
Patrice Vilchez140b7552012-10-31 06:04:50 +0000275 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200276
277 bp->speed = phydev->speed;
278 bp->duplex = phydev->duplex;
279 status_change = 1;
280 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100281 }
282
frederic RODO6c36a702007-07-12 19:07:24 +0200283 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700284 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200285 bp->speed = 0;
286 bp->duplex = -1;
287 }
288 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100289
frederic RODO6c36a702007-07-12 19:07:24 +0200290 status_change = 1;
291 }
292
293 spin_unlock_irqrestore(&bp->lock, flags);
294
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800295 if (!IS_ERR(bp->tx_clk))
296 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
297
frederic RODO6c36a702007-07-12 19:07:24 +0200298 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000299 if (phydev->link) {
300 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000301 netdev_info(dev, "link up (%d/%s)\n",
302 phydev->speed,
303 phydev->duplex == DUPLEX_FULL ?
304 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000305 } else {
306 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000307 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000308 }
frederic RODO6c36a702007-07-12 19:07:24 +0200309 }
310}
311
312/* based on au1000_eth. c*/
313static int macb_mii_probe(struct net_device *dev)
314{
315 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000316 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000317 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000318 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000319 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200320
Jiri Pirko7455a762010-02-08 05:12:08 +0000321 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200322 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000323 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200324 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200325 }
326
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000327 pdata = dev_get_platdata(&bp->pdev->dev);
328 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
329 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
330 if (!ret) {
331 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
332 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
333 }
334 }
frederic RODO6c36a702007-07-12 19:07:24 +0200335
336 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000337 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100338 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000339 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000340 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000341 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200342 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100343
frederic RODO6c36a702007-07-12 19:07:24 +0200344 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200345 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000346 phydev->supported &= PHY_GBIT_FEATURES;
347 else
348 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100349
frederic RODO6c36a702007-07-12 19:07:24 +0200350 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100351
frederic RODO6c36a702007-07-12 19:07:24 +0200352 bp->link = 0;
353 bp->speed = 0;
354 bp->duplex = -1;
355 bp->phy_dev = phydev;
356
357 return 0;
358}
359
Joachim Eastwood0005f542012-10-18 11:01:12 +0000360int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200361{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000362 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200363 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200364 int err = -ENXIO, i;
365
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200366 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200367 macb_writel(bp, NCR, MACB_BIT(MPE));
368
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700369 bp->mii_bus = mdiobus_alloc();
370 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200371 err = -ENOMEM;
372 goto err_out;
373 }
374
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700375 bp->mii_bus->name = "MACB_mii_bus";
376 bp->mii_bus->read = &macb_mdio_read;
377 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000378 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
379 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700380 bp->mii_bus->priv = bp;
381 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900382 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700383
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700384 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
385 if (!bp->mii_bus->irq) {
386 err = -ENOMEM;
387 goto err_out_free_mdiobus;
388 }
389
Jamie Iles91523942011-02-28 04:05:25 +0000390 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200391
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200392 np = bp->pdev->dev.of_node;
393 if (np) {
394 /* try dt phy registration */
395 err = of_mdiobus_register(bp->mii_bus, np);
396
397 /* fallback to standard phy registration if no phy were
398 found during dt phy registration */
399 if (!err && !phy_find_first(bp->mii_bus)) {
400 for (i = 0; i < PHY_MAX_ADDR; i++) {
401 struct phy_device *phydev;
402
403 phydev = mdiobus_scan(bp->mii_bus, i);
404 if (IS_ERR(phydev)) {
405 err = PTR_ERR(phydev);
406 break;
407 }
408 }
409
410 if (err)
411 goto err_out_unregister_bus;
412 }
413 } else {
414 for (i = 0; i < PHY_MAX_ADDR; i++)
415 bp->mii_bus->irq[i] = PHY_POLL;
416
417 if (pdata)
418 bp->mii_bus->phy_mask = pdata->phy_mask;
419
420 err = mdiobus_register(bp->mii_bus);
421 }
422
423 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200424 goto err_out_free_mdio_irq;
425
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200426 err = macb_mii_probe(bp->dev);
427 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200428 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200429
430 return 0;
431
432err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700433 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200434err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700435 kfree(bp->mii_bus->irq);
436err_out_free_mdiobus:
437 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200438err_out:
439 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100440}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000441EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100442
443static void macb_update_stats(struct macb *bp)
444{
445 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000446 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
447 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100448
449 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
450
451 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100452 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100453}
454
Nicolas Ferree86cd532012-10-31 06:04:57 +0000455static int macb_halt_tx(struct macb *bp)
456{
457 unsigned long halt_time, timeout;
458 u32 status;
459
460 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
461
462 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
463 do {
464 halt_time = jiffies;
465 status = macb_readl(bp, TSR);
466 if (!(status & MACB_BIT(TGO)))
467 return 0;
468
469 usleep_range(10, 250);
470 } while (time_before(halt_time, timeout));
471
472 return -ETIMEDOUT;
473}
474
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200475static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
476{
477 if (tx_skb->mapping) {
478 if (tx_skb->mapped_as_page)
479 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
480 tx_skb->size, DMA_TO_DEVICE);
481 else
482 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
483 tx_skb->size, DMA_TO_DEVICE);
484 tx_skb->mapping = 0;
485 }
486
487 if (tx_skb->skb) {
488 dev_kfree_skb_any(tx_skb->skb);
489 tx_skb->skb = NULL;
490 }
491}
492
Nicolas Ferree86cd532012-10-31 06:04:57 +0000493static void macb_tx_error_task(struct work_struct *work)
494{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100495 struct macb_queue *queue = container_of(work, struct macb_queue,
496 tx_error_task);
497 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000498 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100499 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000500 struct sk_buff *skb;
501 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100502 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000503
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100504 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
505 (unsigned int)(queue - bp->queues),
506 queue->tx_tail, queue->tx_head);
507
508 /* Prevent the queue IRQ handlers from running: each of them may call
509 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
510 * As explained below, we have to halt the transmission before updating
511 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
512 * network engine about the macb/gem being halted.
513 */
514 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000515
516 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100517 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000518
519 /*
520 * Stop transmission now
521 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100522 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000523 */
524 if (macb_halt_tx(bp))
525 /* Just complain for now, reinitializing TX path can be good */
526 netdev_err(bp->dev, "BUG: halt tx timed out\n");
527
Nicolas Ferree86cd532012-10-31 06:04:57 +0000528 /*
529 * Treat frames in TX queue including the ones that caused the error.
530 * Free transmit buffers in upper layer.
531 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100532 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
533 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000534
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100535 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000536 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100537 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000538 skb = tx_skb->skb;
539
540 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200541 /* skb is set for the last buffer of the frame */
542 while (!skb) {
543 macb_tx_unmap(bp, tx_skb);
544 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100545 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200546 skb = tx_skb->skb;
547 }
548
549 /* ctrl still refers to the first buffer descriptor
550 * since it's the only one written back by the hardware
551 */
552 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
553 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
554 macb_tx_ring_wrap(tail), skb->data);
555 bp->stats.tx_packets++;
556 bp->stats.tx_bytes += skb->len;
557 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000558 } else {
559 /*
560 * "Buffers exhausted mid-frame" errors may only happen
561 * if the driver is buggy, so complain loudly about those.
562 * Statistics are updated by hardware.
563 */
564 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
565 netdev_err(bp->dev,
566 "BUG: TX buffers exhausted mid-frame\n");
567
568 desc->ctrl = ctrl | MACB_BIT(TX_USED);
569 }
570
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200571 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000572 }
573
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100574 /* Set end of TX queue */
575 desc = macb_tx_desc(queue, 0);
576 desc->addr = 0;
577 desc->ctrl = MACB_BIT(TX_USED);
578
Nicolas Ferree86cd532012-10-31 06:04:57 +0000579 /* Make descriptor updates visible to hardware */
580 wmb();
581
582 /* Reinitialize the TX desc queue */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100583 queue_writel(queue, TBQP, queue->tx_ring_dma);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000584 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100585 queue->tx_head = 0;
586 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000587
588 /* Housework before enabling TX IRQ */
589 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100590 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
591
592 /* Now we are ready to start transmission again */
593 netif_tx_start_all_queues(bp->dev);
594 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
595
596 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000597}
598
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100599static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100600{
601 unsigned int tail;
602 unsigned int head;
603 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100604 struct macb *bp = queue->bp;
605 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100606
607 status = macb_readl(bp, TSR);
608 macb_writel(bp, TSR, status);
609
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000610 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100611 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000612
Nicolas Ferree86cd532012-10-31 06:04:57 +0000613 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
614 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100615
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100616 head = queue->tx_head;
617 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000618 struct macb_tx_skb *tx_skb;
619 struct sk_buff *skb;
620 struct macb_dma_desc *desc;
621 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100622
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100623 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100624
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000625 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100626 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000627
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000628 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100629
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200630 /* TX_USED bit is only set by hardware on the very first buffer
631 * descriptor of the transmitted frame.
632 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000633 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100634 break;
635
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200636 /* Process all buffers of the current transmitted frame */
637 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100638 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200639 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000640
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200641 /* First, update TX stats if needed */
642 if (skb) {
643 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
644 macb_tx_ring_wrap(tail), skb->data);
645 bp->stats.tx_packets++;
646 bp->stats.tx_bytes += skb->len;
647 }
648
649 /* Now we can safely release resources */
650 macb_tx_unmap(bp, tx_skb);
651
652 /* skb is set only for the last buffer of the frame.
653 * WARNING: at this point skb has been freed by
654 * macb_tx_unmap().
655 */
656 if (skb)
657 break;
658 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100659 }
660
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100661 queue->tx_tail = tail;
662 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
663 CIRC_CNT(queue->tx_head, queue->tx_tail,
664 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
665 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100666}
667
Nicolas Ferre4df95132013-06-04 21:57:12 +0000668static void gem_rx_refill(struct macb *bp)
669{
670 unsigned int entry;
671 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000672 dma_addr_t paddr;
673
674 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000675 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000676
677 /* Make hw descriptor updates visible to CPU */
678 rmb();
679
Nicolas Ferre4df95132013-06-04 21:57:12 +0000680 bp->rx_prepared_head++;
681
Nicolas Ferre4df95132013-06-04 21:57:12 +0000682 if (bp->rx_skbuff[entry] == NULL) {
683 /* allocate sk_buff for this free entry in ring */
684 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
685 if (unlikely(skb == NULL)) {
686 netdev_err(bp->dev,
687 "Unable to allocate sk_buff\n");
688 break;
689 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000690
691 /* now fill corresponding descriptor entry */
692 paddr = dma_map_single(&bp->pdev->dev, skb->data,
693 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800694 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
695 dev_kfree_skb(skb);
696 break;
697 }
698
699 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000700
701 if (entry == RX_RING_SIZE - 1)
702 paddr |= MACB_BIT(RX_WRAP);
703 bp->rx_ring[entry].addr = paddr;
704 bp->rx_ring[entry].ctrl = 0;
705
706 /* properly align Ethernet header */
707 skb_reserve(skb, NET_IP_ALIGN);
708 }
709 }
710
711 /* Make descriptor updates visible to hardware */
712 wmb();
713
714 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
715 bp->rx_prepared_head, bp->rx_tail);
716}
717
718/* Mark DMA descriptors from begin up to and not including end as unused */
719static void discard_partial_frame(struct macb *bp, unsigned int begin,
720 unsigned int end)
721{
722 unsigned int frag;
723
724 for (frag = begin; frag != end; frag++) {
725 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
726 desc->addr &= ~MACB_BIT(RX_USED);
727 }
728
729 /* Make descriptor updates visible to hardware */
730 wmb();
731
732 /*
733 * When this happens, the hardware stats registers for
734 * whatever caused this is updated, so we don't have to record
735 * anything.
736 */
737}
738
739static int gem_rx(struct macb *bp, int budget)
740{
741 unsigned int len;
742 unsigned int entry;
743 struct sk_buff *skb;
744 struct macb_dma_desc *desc;
745 int count = 0;
746
747 while (count < budget) {
748 u32 addr, ctrl;
749
750 entry = macb_rx_ring_wrap(bp->rx_tail);
751 desc = &bp->rx_ring[entry];
752
753 /* Make hw descriptor updates visible to CPU */
754 rmb();
755
756 addr = desc->addr;
757 ctrl = desc->ctrl;
758
759 if (!(addr & MACB_BIT(RX_USED)))
760 break;
761
Nicolas Ferre4df95132013-06-04 21:57:12 +0000762 bp->rx_tail++;
763 count++;
764
765 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
766 netdev_err(bp->dev,
767 "not whole frame pointed by descriptor\n");
768 bp->stats.rx_dropped++;
769 break;
770 }
771 skb = bp->rx_skbuff[entry];
772 if (unlikely(!skb)) {
773 netdev_err(bp->dev,
774 "inconsistent Rx descriptor chain\n");
775 bp->stats.rx_dropped++;
776 break;
777 }
778 /* now everything is ready for receiving packet */
779 bp->rx_skbuff[entry] = NULL;
780 len = MACB_BFEXT(RX_FRMLEN, ctrl);
781
782 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
783
784 skb_put(skb, len);
785 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
786 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800787 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000788
789 skb->protocol = eth_type_trans(skb, bp->dev);
790 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200791 if (bp->dev->features & NETIF_F_RXCSUM &&
792 !(bp->dev->flags & IFF_PROMISC) &&
793 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
794 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000795
796 bp->stats.rx_packets++;
797 bp->stats.rx_bytes += skb->len;
798
799#if defined(DEBUG) && defined(VERBOSE_DEBUG)
800 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
801 skb->len, skb->csum);
802 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100803 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000804 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
805 skb->data, 32, true);
806#endif
807
808 netif_receive_skb(skb);
809 }
810
811 gem_rx_refill(bp);
812
813 return count;
814}
815
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100816static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
817 unsigned int last_frag)
818{
819 unsigned int len;
820 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000821 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100822 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000823 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100824
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000825 desc = macb_rx_desc(bp, last_frag);
826 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100827
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000828 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000829 macb_rx_ring_wrap(first_frag),
830 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100831
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000832 /*
833 * The ethernet header starts NET_IP_ALIGN bytes into the
834 * first buffer. Since the header is 14 bytes, this makes the
835 * payload word-aligned.
836 *
837 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
838 * the two padding bytes into the skb so that we avoid hitting
839 * the slowpath in memcpy(), and pull them off afterwards.
840 */
841 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100842 if (!skb) {
843 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000844 for (frag = first_frag; ; frag++) {
845 desc = macb_rx_desc(bp, frag);
846 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100847 if (frag == last_frag)
848 break;
849 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000850
851 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100852 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000853
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100854 return 1;
855 }
856
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000857 offset = 0;
858 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700859 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100860 skb_put(skb, len);
861
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000862 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000863 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100864
865 if (offset + frag_len > len) {
866 BUG_ON(frag != last_frag);
867 frag_len = len - offset;
868 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300869 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000870 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000871 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000872 desc = macb_rx_desc(bp, frag);
873 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100874
875 if (frag == last_frag)
876 break;
877 }
878
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000879 /* Make descriptor updates visible to hardware */
880 wmb();
881
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000882 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100883 skb->protocol = eth_type_trans(skb, bp->dev);
884
885 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000886 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000887 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000888 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100889 netif_receive_skb(skb);
890
891 return 0;
892}
893
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100894static int macb_rx(struct macb *bp, int budget)
895{
896 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000897 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100898 int first_frag = -1;
899
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000900 for (tail = bp->rx_tail; budget > 0; tail++) {
901 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100902 u32 addr, ctrl;
903
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000904 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100905 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000906
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000907 addr = desc->addr;
908 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100909
910 if (!(addr & MACB_BIT(RX_USED)))
911 break;
912
913 if (ctrl & MACB_BIT(RX_SOF)) {
914 if (first_frag != -1)
915 discard_partial_frame(bp, first_frag, tail);
916 first_frag = tail;
917 }
918
919 if (ctrl & MACB_BIT(RX_EOF)) {
920 int dropped;
921 BUG_ON(first_frag == -1);
922
923 dropped = macb_rx_frame(bp, first_frag, tail);
924 first_frag = -1;
925 if (!dropped) {
926 received++;
927 budget--;
928 }
929 }
930 }
931
932 if (first_frag != -1)
933 bp->rx_tail = first_frag;
934 else
935 bp->rx_tail = tail;
936
937 return received;
938}
939
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700940static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100941{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700942 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700943 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100944 u32 status;
945
946 status = macb_readl(bp, RSR);
947 macb_writel(bp, RSR, status);
948
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700949 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100950
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000951 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000952 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100953
Nicolas Ferre4df95132013-06-04 21:57:12 +0000954 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000955 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800956 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100957
Nicolas Ferre8770e912013-02-12 11:08:48 +0100958 /* Packets received while interrupts were disabled */
959 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -0700960 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700961 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
962 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +0100963 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700964 } else {
965 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
966 }
Joshua Hokeb3363692010-10-25 01:44:22 +0000967 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100968
969 /* TODO: Handle errors */
970
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700971 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100972}
973
974static irqreturn_t macb_interrupt(int irq, void *dev_id)
975{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100976 struct macb_queue *queue = dev_id;
977 struct macb *bp = queue->bp;
978 struct net_device *dev = bp->dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100979 u32 status;
980
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100981 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100982
983 if (unlikely(!status))
984 return IRQ_NONE;
985
986 spin_lock(&bp->lock);
987
988 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100989 /* close possible race with dev_close */
990 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100991 queue_writel(queue, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100992 break;
993 }
994
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100995 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
996 (unsigned int)(queue - bp->queues),
997 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000998
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100999 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +00001000 /*
1001 * There's no point taking any more interrupts
1002 * until we have processed the buffers. The
1003 * scheduling call may fail if the poll routine
1004 * is already scheduled, so disable interrupts
1005 * now.
1006 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001007 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001008 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001009 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001010
Ben Hutchings288379f2009-01-19 16:43:59 -08001011 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001012 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001013 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001014 }
1015 }
1016
Nicolas Ferree86cd532012-10-31 06:04:57 +00001017 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001018 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1019 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001020
1021 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001022 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001023
Nicolas Ferree86cd532012-10-31 06:04:57 +00001024 break;
1025 }
1026
1027 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001028 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001029
1030 /*
1031 * Link change detection isn't possible with RMII, so we'll
1032 * add that if/when we get our hands on a full-blown MII PHY.
1033 */
1034
Alexander Steinb19f7f72011-04-13 05:03:24 +00001035 if (status & MACB_BIT(ISR_ROVR)) {
1036 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001037 if (macb_is_gem(bp))
1038 bp->hw_stats.gem.rx_overruns++;
1039 else
1040 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001041
1042 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001043 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001044 }
1045
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001046 if (status & MACB_BIT(HRESP)) {
1047 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001048 * TODO: Reset the hardware, and maybe move the
1049 * netdev_err to a lower-priority context as well
1050 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001051 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001052 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001053
1054 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001055 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001056 }
1057
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001058 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001059 }
1060
1061 spin_unlock(&bp->lock);
1062
1063 return IRQ_HANDLED;
1064}
1065
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001066#ifdef CONFIG_NET_POLL_CONTROLLER
1067/*
1068 * Polling receive - used by netconsole and other diagnostic tools
1069 * to allow network i/o with interrupts disabled.
1070 */
1071static void macb_poll_controller(struct net_device *dev)
1072{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001073 struct macb *bp = netdev_priv(dev);
1074 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001075 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001076 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001077
1078 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001079 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1080 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001081 local_irq_restore(flags);
1082}
1083#endif
1084
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001085static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
1086 unsigned int len)
1087{
1088 return (len + bp->max_tx_length - 1) / bp->max_tx_length;
1089}
1090
1091static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001092 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001093 struct sk_buff *skb)
1094{
1095 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001096 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001097 struct macb_tx_skb *tx_skb = NULL;
1098 struct macb_dma_desc *desc;
1099 unsigned int offset, size, count = 0;
1100 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1101 unsigned int eof = 1;
1102 u32 ctrl;
1103
1104 /* First, map non-paged data */
1105 len = skb_headlen(skb);
1106 offset = 0;
1107 while (len) {
1108 size = min(len, bp->max_tx_length);
1109 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001110 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001111
1112 mapping = dma_map_single(&bp->pdev->dev,
1113 skb->data + offset,
1114 size, DMA_TO_DEVICE);
1115 if (dma_mapping_error(&bp->pdev->dev, mapping))
1116 goto dma_error;
1117
1118 /* Save info to properly release resources */
1119 tx_skb->skb = NULL;
1120 tx_skb->mapping = mapping;
1121 tx_skb->size = size;
1122 tx_skb->mapped_as_page = false;
1123
1124 len -= size;
1125 offset += size;
1126 count++;
1127 tx_head++;
1128 }
1129
1130 /* Then, map paged data from fragments */
1131 for (f = 0; f < nr_frags; f++) {
1132 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1133
1134 len = skb_frag_size(frag);
1135 offset = 0;
1136 while (len) {
1137 size = min(len, bp->max_tx_length);
1138 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001139 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001140
1141 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1142 offset, size, DMA_TO_DEVICE);
1143 if (dma_mapping_error(&bp->pdev->dev, mapping))
1144 goto dma_error;
1145
1146 /* Save info to properly release resources */
1147 tx_skb->skb = NULL;
1148 tx_skb->mapping = mapping;
1149 tx_skb->size = size;
1150 tx_skb->mapped_as_page = true;
1151
1152 len -= size;
1153 offset += size;
1154 count++;
1155 tx_head++;
1156 }
1157 }
1158
1159 /* Should never happen */
1160 if (unlikely(tx_skb == NULL)) {
1161 netdev_err(bp->dev, "BUG! empty skb!\n");
1162 return 0;
1163 }
1164
1165 /* This is the last buffer of the frame: save socket buffer */
1166 tx_skb->skb = skb;
1167
1168 /* Update TX ring: update buffer descriptors in reverse order
1169 * to avoid race condition
1170 */
1171
1172 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1173 * to set the end of TX queue
1174 */
1175 i = tx_head;
1176 entry = macb_tx_ring_wrap(i);
1177 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001178 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001179 desc->ctrl = ctrl;
1180
1181 do {
1182 i--;
1183 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001184 tx_skb = &queue->tx_skb[entry];
1185 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001186
1187 ctrl = (u32)tx_skb->size;
1188 if (eof) {
1189 ctrl |= MACB_BIT(TX_LAST);
1190 eof = 0;
1191 }
1192 if (unlikely(entry == (TX_RING_SIZE - 1)))
1193 ctrl |= MACB_BIT(TX_WRAP);
1194
1195 /* Set TX buffer descriptor */
1196 desc->addr = tx_skb->mapping;
1197 /* desc->addr must be visible to hardware before clearing
1198 * 'TX_USED' bit in desc->ctrl.
1199 */
1200 wmb();
1201 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001202 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001203
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001204 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001205
1206 return count;
1207
1208dma_error:
1209 netdev_err(bp->dev, "TX DMA map failed\n");
1210
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001211 for (i = queue->tx_head; i != tx_head; i++) {
1212 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001213
1214 macb_tx_unmap(bp, tx_skb);
1215 }
1216
1217 return 0;
1218}
1219
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001220static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1221{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001222 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001223 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001224 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001225 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001226 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001227
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001228#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1229 netdev_vdbg(bp->dev,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001230 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1231 queue_index, skb->len, skb->head, skb->data,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001232 skb_tail_pointer(skb), skb_end_pointer(skb));
1233 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1234 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001235#endif
1236
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001237 /* Count how many TX buffer descriptors are needed to send this
1238 * socket buffer: skb fragments of jumbo frames may need to be
1239 * splitted into many buffer descriptors.
1240 */
1241 count = macb_count_tx_descriptors(bp, skb_headlen(skb));
1242 nr_frags = skb_shinfo(skb)->nr_frags;
1243 for (f = 0; f < nr_frags; f++) {
1244 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1245 count += macb_count_tx_descriptors(bp, frag_size);
1246 }
1247
Dongdong Deng48719532009-08-23 19:49:07 -07001248 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001249
1250 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001251 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1252 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001253 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001254 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001255 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001256 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001257 }
1258
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001259 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001260 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001261 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001262 goto unlock;
1263 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001264
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001265 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001266 wmb();
1267
Richard Cochrane0720922011-06-19 21:51:28 +00001268 skb_tx_timestamp(skb);
1269
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001270 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1271
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001272 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1273 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001274
Soren Brinkmann92030902014-03-04 08:46:39 -08001275unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001276 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001277
Patrick McHardy6ed10652009-06-23 06:03:08 +00001278 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001279}
1280
Nicolas Ferre4df95132013-06-04 21:57:12 +00001281static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001282{
1283 if (!macb_is_gem(bp)) {
1284 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1285 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001286 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001287
Nicolas Ferre1b447912013-06-04 21:57:11 +00001288 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001289 netdev_dbg(bp->dev,
1290 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001291 RX_BUFFER_MULTIPLE);
1292 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001293 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001294 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001295 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001296
1297 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1298 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001299}
1300
Nicolas Ferre4df95132013-06-04 21:57:12 +00001301static void gem_free_rx_buffers(struct macb *bp)
1302{
1303 struct sk_buff *skb;
1304 struct macb_dma_desc *desc;
1305 dma_addr_t addr;
1306 int i;
1307
1308 if (!bp->rx_skbuff)
1309 return;
1310
1311 for (i = 0; i < RX_RING_SIZE; i++) {
1312 skb = bp->rx_skbuff[i];
1313
1314 if (skb == NULL)
1315 continue;
1316
1317 desc = &bp->rx_ring[i];
1318 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001319 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001320 DMA_FROM_DEVICE);
1321 dev_kfree_skb_any(skb);
1322 skb = NULL;
1323 }
1324
1325 kfree(bp->rx_skbuff);
1326 bp->rx_skbuff = NULL;
1327}
1328
1329static void macb_free_rx_buffers(struct macb *bp)
1330{
1331 if (bp->rx_buffers) {
1332 dma_free_coherent(&bp->pdev->dev,
1333 RX_RING_SIZE * bp->rx_buffer_size,
1334 bp->rx_buffers, bp->rx_buffers_dma);
1335 bp->rx_buffers = NULL;
1336 }
1337}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001338
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001339static void macb_free_consistent(struct macb *bp)
1340{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001341 struct macb_queue *queue;
1342 unsigned int q;
1343
Nicolas Ferre4df95132013-06-04 21:57:12 +00001344 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001345 if (bp->rx_ring) {
1346 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1347 bp->rx_ring, bp->rx_ring_dma);
1348 bp->rx_ring = NULL;
1349 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001350
1351 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1352 kfree(queue->tx_skb);
1353 queue->tx_skb = NULL;
1354 if (queue->tx_ring) {
1355 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1356 queue->tx_ring, queue->tx_ring_dma);
1357 queue->tx_ring = NULL;
1358 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001359 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001360}
1361
1362static int gem_alloc_rx_buffers(struct macb *bp)
1363{
1364 int size;
1365
1366 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1367 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1368 if (!bp->rx_skbuff)
1369 return -ENOMEM;
1370 else
1371 netdev_dbg(bp->dev,
1372 "Allocated %d RX struct sk_buff entries at %p\n",
1373 RX_RING_SIZE, bp->rx_skbuff);
1374 return 0;
1375}
1376
1377static int macb_alloc_rx_buffers(struct macb *bp)
1378{
1379 int size;
1380
1381 size = RX_RING_SIZE * bp->rx_buffer_size;
1382 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1383 &bp->rx_buffers_dma, GFP_KERNEL);
1384 if (!bp->rx_buffers)
1385 return -ENOMEM;
1386 else
1387 netdev_dbg(bp->dev,
1388 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1389 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1390 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001391}
1392
1393static int macb_alloc_consistent(struct macb *bp)
1394{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001395 struct macb_queue *queue;
1396 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001397 int size;
1398
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001399 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1400 size = TX_RING_BYTES;
1401 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1402 &queue->tx_ring_dma,
1403 GFP_KERNEL);
1404 if (!queue->tx_ring)
1405 goto out_err;
1406 netdev_dbg(bp->dev,
1407 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1408 q, size, (unsigned long)queue->tx_ring_dma,
1409 queue->tx_ring);
1410
1411 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1412 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1413 if (!queue->tx_skb)
1414 goto out_err;
1415 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001416
1417 size = RX_RING_BYTES;
1418 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1419 &bp->rx_ring_dma, GFP_KERNEL);
1420 if (!bp->rx_ring)
1421 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001422 netdev_dbg(bp->dev,
1423 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1424 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001425
Nicolas Ferre4df95132013-06-04 21:57:12 +00001426 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001428
1429 return 0;
1430
1431out_err:
1432 macb_free_consistent(bp);
1433 return -ENOMEM;
1434}
1435
Nicolas Ferre4df95132013-06-04 21:57:12 +00001436static void gem_init_rings(struct macb *bp)
1437{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001438 struct macb_queue *queue;
1439 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001440 int i;
1441
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001442 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1443 for (i = 0; i < TX_RING_SIZE; i++) {
1444 queue->tx_ring[i].addr = 0;
1445 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1446 }
1447 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1448 queue->tx_head = 0;
1449 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001450 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001451
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001452 bp->rx_tail = 0;
1453 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001454
1455 gem_rx_refill(bp);
1456}
1457
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001458static void macb_init_rings(struct macb *bp)
1459{
1460 int i;
1461 dma_addr_t addr;
1462
1463 addr = bp->rx_buffers_dma;
1464 for (i = 0; i < RX_RING_SIZE; i++) {
1465 bp->rx_ring[i].addr = addr;
1466 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001467 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001468 }
1469 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1470
1471 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001472 bp->queues[0].tx_ring[i].addr = 0;
1473 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
1474 bp->queues[0].tx_head = 0;
1475 bp->queues[0].tx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001476 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001477 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001478
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001479 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001480}
1481
1482static void macb_reset_hw(struct macb *bp)
1483{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001484 struct macb_queue *queue;
1485 unsigned int q;
1486
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001487 /*
1488 * Disable RX and TX (XXX: Should we halt the transmission
1489 * more gracefully?)
1490 */
1491 macb_writel(bp, NCR, 0);
1492
1493 /* Clear the stats registers (XXX: Update stats first?) */
1494 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1495
1496 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001497 macb_writel(bp, TSR, -1);
1498 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001499
1500 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001501 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1502 queue_writel(queue, IDR, -1);
1503 queue_readl(queue, ISR);
1504 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001505}
1506
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001507static u32 gem_mdc_clk_div(struct macb *bp)
1508{
1509 u32 config;
1510 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1511
1512 if (pclk_hz <= 20000000)
1513 config = GEM_BF(CLK, GEM_CLK_DIV8);
1514 else if (pclk_hz <= 40000000)
1515 config = GEM_BF(CLK, GEM_CLK_DIV16);
1516 else if (pclk_hz <= 80000000)
1517 config = GEM_BF(CLK, GEM_CLK_DIV32);
1518 else if (pclk_hz <= 120000000)
1519 config = GEM_BF(CLK, GEM_CLK_DIV48);
1520 else if (pclk_hz <= 160000000)
1521 config = GEM_BF(CLK, GEM_CLK_DIV64);
1522 else
1523 config = GEM_BF(CLK, GEM_CLK_DIV96);
1524
1525 return config;
1526}
1527
1528static u32 macb_mdc_clk_div(struct macb *bp)
1529{
1530 u32 config;
1531 unsigned long pclk_hz;
1532
1533 if (macb_is_gem(bp))
1534 return gem_mdc_clk_div(bp);
1535
1536 pclk_hz = clk_get_rate(bp->pclk);
1537 if (pclk_hz <= 20000000)
1538 config = MACB_BF(CLK, MACB_CLK_DIV8);
1539 else if (pclk_hz <= 40000000)
1540 config = MACB_BF(CLK, MACB_CLK_DIV16);
1541 else if (pclk_hz <= 80000000)
1542 config = MACB_BF(CLK, MACB_CLK_DIV32);
1543 else
1544 config = MACB_BF(CLK, MACB_CLK_DIV64);
1545
1546 return config;
1547}
1548
Jamie Iles757a03c2011-03-09 16:29:59 +00001549/*
1550 * Get the DMA bus width field of the network configuration register that we
1551 * should program. We find the width from decoding the design configuration
1552 * register to find the maximum supported data bus width.
1553 */
1554static u32 macb_dbw(struct macb *bp)
1555{
1556 if (!macb_is_gem(bp))
1557 return 0;
1558
1559 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1560 case 4:
1561 return GEM_BF(DBW, GEM_DBW128);
1562 case 2:
1563 return GEM_BF(DBW, GEM_DBW64);
1564 case 1:
1565 default:
1566 return GEM_BF(DBW, GEM_DBW32);
1567 }
1568}
1569
Jamie Iles0116da42011-03-14 17:38:30 +00001570/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001571 * Configure the receive DMA engine
1572 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001573 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001574 * (if not supported by FIFO, it will fallback to default)
1575 * - set both rx/tx packet buffers to full memory size
1576 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001577 */
1578static void macb_configure_dma(struct macb *bp)
1579{
1580 u32 dmacfg;
1581
1582 if (macb_is_gem(bp)) {
1583 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001584 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001585 if (bp->dma_burst_length)
1586 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001587 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Steffen Trumtrara1ae3852013-03-27 23:07:06 +00001588 dmacfg &= ~GEM_BIT(ENDIA);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001589 if (bp->dev->features & NETIF_F_HW_CSUM)
1590 dmacfg |= GEM_BIT(TXCOEN);
1591 else
1592 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001593 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1594 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001595 gem_writel(bp, DMACFG, dmacfg);
1596 }
1597}
1598
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001599static void macb_init_hw(struct macb *bp)
1600{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001601 struct macb_queue *queue;
1602 unsigned int q;
1603
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001604 u32 config;
1605
1606 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001607 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001608
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001609 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001610 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001611 config |= MACB_BIT(PAE); /* PAuse Enable */
1612 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001613 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001614 if (bp->dev->flags & IFF_PROMISC)
1615 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001616 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1617 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001618 if (!(bp->dev->flags & IFF_BROADCAST))
1619 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001620 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001621 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001622 bp->speed = SPEED_10;
1623 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001624
Jamie Iles0116da42011-03-14 17:38:30 +00001625 macb_configure_dma(bp);
1626
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001627 /* Initialize TX and RX buffers */
1628 macb_writel(bp, RBQP, bp->rx_ring_dma);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001629 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1630 queue_writel(queue, TBQP, queue->tx_ring_dma);
1631
1632 /* Enable interrupts */
1633 queue_writel(queue, IER,
1634 MACB_RX_INT_FLAGS |
1635 MACB_TX_INT_FLAGS |
1636 MACB_BIT(HRESP));
1637 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001638
1639 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001640 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001641}
1642
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001643/*
1644 * The hash address register is 64 bits long and takes up two
1645 * locations in the memory map. The least significant bits are stored
1646 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1647 *
1648 * The unicast hash enable and the multicast hash enable bits in the
1649 * network configuration register enable the reception of hash matched
1650 * frames. The destination address is reduced to a 6 bit index into
1651 * the 64 bit hash register using the following hash function. The
1652 * hash function is an exclusive or of every sixth bit of the
1653 * destination address.
1654 *
1655 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1656 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1657 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1658 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1659 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1660 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1661 *
1662 * da[0] represents the least significant bit of the first byte
1663 * received, that is, the multicast/unicast indicator, and da[47]
1664 * represents the most significant bit of the last byte received. If
1665 * the hash index, hi[n], points to a bit that is set in the hash
1666 * register then the frame will be matched according to whether the
1667 * frame is multicast or unicast. A multicast match will be signalled
1668 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1669 * index points to a bit set in the hash register. A unicast match
1670 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1671 * and the hash index points to a bit set in the hash register. To
1672 * receive all multicast frames, the hash register should be set with
1673 * all ones and the multicast hash enable bit should be set in the
1674 * network configuration register.
1675 */
1676
1677static inline int hash_bit_value(int bitnr, __u8 *addr)
1678{
1679 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1680 return 1;
1681 return 0;
1682}
1683
1684/*
1685 * Return the hash index value for the specified address.
1686 */
1687static int hash_get_index(__u8 *addr)
1688{
1689 int i, j, bitval;
1690 int hash_index = 0;
1691
1692 for (j = 0; j < 6; j++) {
1693 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001694 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001695
1696 hash_index |= (bitval << j);
1697 }
1698
1699 return hash_index;
1700}
1701
1702/*
1703 * Add multicast addresses to the internal multicast-hash table.
1704 */
1705static void macb_sethashtable(struct net_device *dev)
1706{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001707 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001708 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001709 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001710 struct macb *bp = netdev_priv(dev);
1711
1712 mc_filter[0] = mc_filter[1] = 0;
1713
Jiri Pirko22bedad32010-04-01 21:22:57 +00001714 netdev_for_each_mc_addr(ha, dev) {
1715 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001716 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1717 }
1718
Jamie Ilesf75ba502011-11-08 10:12:32 +00001719 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1720 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001721}
1722
1723/*
1724 * Enable/Disable promiscuous and multicast modes.
1725 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001726void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001727{
1728 unsigned long cfg;
1729 struct macb *bp = netdev_priv(dev);
1730
1731 cfg = macb_readl(bp, NCFGR);
1732
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001733 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001734 /* Enable promiscuous mode */
1735 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001736
1737 /* Disable RX checksum offload */
1738 if (macb_is_gem(bp))
1739 cfg &= ~GEM_BIT(RXCOEN);
1740 } else {
1741 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001742 cfg &= ~MACB_BIT(CAF);
1743
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001744 /* Enable RX checksum offload only if requested */
1745 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1746 cfg |= GEM_BIT(RXCOEN);
1747 }
1748
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001749 if (dev->flags & IFF_ALLMULTI) {
1750 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001751 macb_or_gem_writel(bp, HRB, -1);
1752 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001753 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001754 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001755 /* Enable specific multicasts */
1756 macb_sethashtable(dev);
1757 cfg |= MACB_BIT(NCFGR_MTI);
1758 } else if (dev->flags & (~IFF_ALLMULTI)) {
1759 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001760 macb_or_gem_writel(bp, HRB, 0);
1761 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001762 cfg &= ~MACB_BIT(NCFGR_MTI);
1763 }
1764
1765 macb_writel(bp, NCFGR, cfg);
1766}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001767EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001768
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001769static int macb_open(struct net_device *dev)
1770{
1771 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001772 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001773 int err;
1774
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001775 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001776
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001777 /* carrier starts down */
1778 netif_carrier_off(dev);
1779
frederic RODO6c36a702007-07-12 19:07:24 +02001780 /* if the phy is not yet register, retry later*/
1781 if (!bp->phy_dev)
1782 return -EAGAIN;
1783
Nicolas Ferre1b447912013-06-04 21:57:11 +00001784 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001785 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001786
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001787 err = macb_alloc_consistent(bp);
1788 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001789 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1790 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001791 return err;
1792 }
1793
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001794 napi_enable(&bp->napi);
1795
Nicolas Ferre4df95132013-06-04 21:57:12 +00001796 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001797 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001798
frederic RODO6c36a702007-07-12 19:07:24 +02001799 /* schedule a link state check */
1800 phy_start(bp->phy_dev);
1801
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001802 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001803
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001804 return 0;
1805}
1806
1807static int macb_close(struct net_device *dev)
1808{
1809 struct macb *bp = netdev_priv(dev);
1810 unsigned long flags;
1811
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001812 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001813 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814
frederic RODO6c36a702007-07-12 19:07:24 +02001815 if (bp->phy_dev)
1816 phy_stop(bp->phy_dev);
1817
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001818 spin_lock_irqsave(&bp->lock, flags);
1819 macb_reset_hw(bp);
1820 netif_carrier_off(dev);
1821 spin_unlock_irqrestore(&bp->lock, flags);
1822
1823 macb_free_consistent(bp);
1824
1825 return 0;
1826}
1827
Jamie Ilesa494ed82011-03-09 16:26:35 +00001828static void gem_update_stats(struct macb *bp)
1829{
Xander Huff3ff13f12015-01-13 16:15:51 -06001830 int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001831 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001832
Xander Huff3ff13f12015-01-13 16:15:51 -06001833 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1834 u32 offset = gem_statistics[i].offset;
Xander Huff2fa45e22015-01-15 15:55:19 -06001835 u64 val = __raw_readl(bp->regs + offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001836
1837 bp->ethtool_stats[i] += val;
1838 *p += val;
1839
1840 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1841 /* Add GEM_OCTTXH, GEM_OCTRXH */
Xander Huff2fa45e22015-01-15 15:55:19 -06001842 val = __raw_readl(bp->regs + offset + 4);
1843 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06001844 *(++p) += val;
1845 }
1846 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00001847}
1848
1849static struct net_device_stats *gem_get_stats(struct macb *bp)
1850{
1851 struct gem_stats *hwstat = &bp->hw_stats.gem;
1852 struct net_device_stats *nstat = &bp->stats;
1853
1854 gem_update_stats(bp);
1855
1856 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1857 hwstat->rx_alignment_errors +
1858 hwstat->rx_resource_errors +
1859 hwstat->rx_overruns +
1860 hwstat->rx_oversize_frames +
1861 hwstat->rx_jabbers +
1862 hwstat->rx_undersized_frames +
1863 hwstat->rx_length_field_frame_errors);
1864 nstat->tx_errors = (hwstat->tx_late_collisions +
1865 hwstat->tx_excessive_collisions +
1866 hwstat->tx_underrun +
1867 hwstat->tx_carrier_sense_errors);
1868 nstat->multicast = hwstat->rx_multicast_frames;
1869 nstat->collisions = (hwstat->tx_single_collision_frames +
1870 hwstat->tx_multiple_collision_frames +
1871 hwstat->tx_excessive_collisions);
1872 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1873 hwstat->rx_jabbers +
1874 hwstat->rx_undersized_frames +
1875 hwstat->rx_length_field_frame_errors);
1876 nstat->rx_over_errors = hwstat->rx_resource_errors;
1877 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1878 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1879 nstat->rx_fifo_errors = hwstat->rx_overruns;
1880 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1881 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1882 nstat->tx_fifo_errors = hwstat->tx_underrun;
1883
1884 return nstat;
1885}
1886
Xander Huff3ff13f12015-01-13 16:15:51 -06001887static void gem_get_ethtool_stats(struct net_device *dev,
1888 struct ethtool_stats *stats, u64 *data)
1889{
1890 struct macb *bp;
1891
1892 bp = netdev_priv(dev);
1893 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06001894 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06001895}
1896
1897static int gem_get_sset_count(struct net_device *dev, int sset)
1898{
1899 switch (sset) {
1900 case ETH_SS_STATS:
1901 return GEM_STATS_LEN;
1902 default:
1903 return -EOPNOTSUPP;
1904 }
1905}
1906
1907static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
1908{
1909 int i;
1910
1911 switch (sset) {
1912 case ETH_SS_STATS:
1913 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
1914 memcpy(p, gem_statistics[i].stat_string,
1915 ETH_GSTRING_LEN);
1916 break;
1917 }
1918}
1919
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001920struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001921{
1922 struct macb *bp = netdev_priv(dev);
1923 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001924 struct macb_stats *hwstat = &bp->hw_stats.macb;
1925
1926 if (macb_is_gem(bp))
1927 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001928
frederic RODO6c36a702007-07-12 19:07:24 +02001929 /* read stats from hardware */
1930 macb_update_stats(bp);
1931
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001932 /* Convert HW stats into netdevice stats */
1933 nstat->rx_errors = (hwstat->rx_fcs_errors +
1934 hwstat->rx_align_errors +
1935 hwstat->rx_resource_errors +
1936 hwstat->rx_overruns +
1937 hwstat->rx_oversize_pkts +
1938 hwstat->rx_jabbers +
1939 hwstat->rx_undersize_pkts +
1940 hwstat->sqe_test_errors +
1941 hwstat->rx_length_mismatch);
1942 nstat->tx_errors = (hwstat->tx_late_cols +
1943 hwstat->tx_excessive_cols +
1944 hwstat->tx_underruns +
1945 hwstat->tx_carrier_errors);
1946 nstat->collisions = (hwstat->tx_single_cols +
1947 hwstat->tx_multiple_cols +
1948 hwstat->tx_excessive_cols);
1949 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1950 hwstat->rx_jabbers +
1951 hwstat->rx_undersize_pkts +
1952 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001953 nstat->rx_over_errors = hwstat->rx_resource_errors +
1954 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001955 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1956 nstat->rx_frame_errors = hwstat->rx_align_errors;
1957 nstat->rx_fifo_errors = hwstat->rx_overruns;
1958 /* XXX: What does "missed" mean? */
1959 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1960 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1961 nstat->tx_fifo_errors = hwstat->tx_underruns;
1962 /* Don't know about heartbeat or window errors... */
1963
1964 return nstat;
1965}
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001966EXPORT_SYMBOL_GPL(macb_get_stats);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001967
1968static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1969{
1970 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001971 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001972
frederic RODO6c36a702007-07-12 19:07:24 +02001973 if (!phydev)
1974 return -ENODEV;
1975
1976 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001977}
1978
1979static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1980{
1981 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001982 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001983
frederic RODO6c36a702007-07-12 19:07:24 +02001984 if (!phydev)
1985 return -ENODEV;
1986
1987 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001988}
1989
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001990static int macb_get_regs_len(struct net_device *netdev)
1991{
1992 return MACB_GREGS_NBR * sizeof(u32);
1993}
1994
1995static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1996 void *p)
1997{
1998 struct macb *bp = netdev_priv(dev);
1999 unsigned int tail, head;
2000 u32 *regs_buff = p;
2001
2002 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2003 | MACB_GREGS_VERSION;
2004
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002005 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2006 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002007
2008 regs_buff[0] = macb_readl(bp, NCR);
2009 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2010 regs_buff[2] = macb_readl(bp, NSR);
2011 regs_buff[3] = macb_readl(bp, TSR);
2012 regs_buff[4] = macb_readl(bp, RBQP);
2013 regs_buff[5] = macb_readl(bp, TBQP);
2014 regs_buff[6] = macb_readl(bp, RSR);
2015 regs_buff[7] = macb_readl(bp, IMR);
2016
2017 regs_buff[8] = tail;
2018 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002019 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2020 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002021
2022 if (macb_is_gem(bp)) {
2023 regs_buff[12] = gem_readl(bp, USRIO);
2024 regs_buff[13] = gem_readl(bp, DMACFG);
2025 }
2026}
2027
Joachim Eastwood0005f542012-10-18 11:01:12 +00002028const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002029 .get_settings = macb_get_settings,
2030 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002031 .get_regs_len = macb_get_regs_len,
2032 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002033 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002034 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff8cd5a562015-01-15 15:55:20 -06002035};
2036EXPORT_SYMBOL_GPL(macb_ethtool_ops);
2037
2038const struct ethtool_ops gem_ethtool_ops = {
2039 .get_settings = macb_get_settings,
2040 .set_settings = macb_set_settings,
2041 .get_regs_len = macb_get_regs_len,
2042 .get_regs = macb_get_regs,
2043 .get_link = ethtool_op_get_link,
2044 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002045 .get_ethtool_stats = gem_get_ethtool_stats,
2046 .get_strings = gem_get_ethtool_strings,
2047 .get_sset_count = gem_get_sset_count,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002048};
Xander Huff8cd5a562015-01-15 15:55:20 -06002049EXPORT_SYMBOL_GPL(gem_ethtool_ops);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002050
Joachim Eastwood0005f542012-10-18 11:01:12 +00002051int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002052{
2053 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002054 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002055
2056 if (!netif_running(dev))
2057 return -EINVAL;
2058
frederic RODO6c36a702007-07-12 19:07:24 +02002059 if (!phydev)
2060 return -ENODEV;
2061
Richard Cochran28b04112010-07-17 08:48:55 +00002062 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002063}
Joachim Eastwood0005f542012-10-18 11:01:12 +00002064EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002065
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002066static int macb_set_features(struct net_device *netdev,
2067 netdev_features_t features)
2068{
2069 struct macb *bp = netdev_priv(netdev);
2070 netdev_features_t changed = features ^ netdev->features;
2071
2072 /* TX checksum offload */
2073 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2074 u32 dmacfg;
2075
2076 dmacfg = gem_readl(bp, DMACFG);
2077 if (features & NETIF_F_HW_CSUM)
2078 dmacfg |= GEM_BIT(TXCOEN);
2079 else
2080 dmacfg &= ~GEM_BIT(TXCOEN);
2081 gem_writel(bp, DMACFG, dmacfg);
2082 }
2083
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002084 /* RX checksum offload */
2085 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2086 u32 netcfg;
2087
2088 netcfg = gem_readl(bp, NCFGR);
2089 if (features & NETIF_F_RXCSUM &&
2090 !(netdev->flags & IFF_PROMISC))
2091 netcfg |= GEM_BIT(RXCOEN);
2092 else
2093 netcfg &= ~GEM_BIT(RXCOEN);
2094 gem_writel(bp, NCFGR, netcfg);
2095 }
2096
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002097 return 0;
2098}
2099
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002100static const struct net_device_ops macb_netdev_ops = {
2101 .ndo_open = macb_open,
2102 .ndo_stop = macb_close,
2103 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002104 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002105 .ndo_get_stats = macb_get_stats,
2106 .ndo_do_ioctl = macb_ioctl,
2107 .ndo_validate_addr = eth_validate_addr,
2108 .ndo_change_mtu = eth_change_mtu,
2109 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002110#ifdef CONFIG_NET_POLL_CONTROLLER
2111 .ndo_poll_controller = macb_poll_controller,
2112#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002113 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002114};
2115
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002116#if defined(CONFIG_OF)
Nicolas Ferree1755872014-07-24 13:50:58 +02002117static struct macb_config pc302gem_config = {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002118 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2119 .dma_burst_length = 16,
2120};
2121
2122static struct macb_config sama5d3_config = {
2123 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
Nicolas Ferree1755872014-07-24 13:50:58 +02002124 .dma_burst_length = 16,
2125};
2126
Cyrille Pitchen4b7b0e42014-07-24 13:51:03 +02002127static struct macb_config sama5d4_config = {
2128 .caps = 0,
2129 .dma_burst_length = 4,
2130};
2131
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002132static const struct of_device_id macb_dt_ids[] = {
2133 { .compatible = "cdns,at32ap7000-macb" },
2134 { .compatible = "cdns,at91sam9260-macb" },
2135 { .compatible = "cdns,macb" },
Nicolas Ferree1755872014-07-24 13:50:58 +02002136 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2137 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002138 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Cyrille Pitchen4b7b0e42014-07-24 13:51:03 +02002139 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002140 { /* sentinel */ }
2141};
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002142MODULE_DEVICE_TABLE(of, macb_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002143#endif
2144
Nicolas Ferree1755872014-07-24 13:50:58 +02002145/*
2146 * Configure peripheral capacities according to device tree
2147 * and integration options used
2148 */
2149static void macb_configure_caps(struct macb *bp)
2150{
2151 u32 dcfg;
2152 const struct of_device_id *match;
2153 const struct macb_config *config;
2154
2155 if (bp->pdev->dev.of_node) {
2156 match = of_match_node(macb_dt_ids, bp->pdev->dev.of_node);
2157 if (match && match->data) {
2158 config = (const struct macb_config *)match->data;
2159
2160 bp->caps = config->caps;
2161 /*
2162 * As we have access to the matching node, configure
2163 * DMA burst length as well
2164 */
2165 bp->dma_burst_length = config->dma_burst_length;
2166 }
2167 }
2168
2169 if (MACB_BFEXT(IDNUM, macb_readl(bp, MID)) == 0x2)
2170 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2171
2172 if (macb_is_gem(bp)) {
2173 dcfg = gem_readl(bp, DCFG1);
2174 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2175 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2176 dcfg = gem_readl(bp, DCFG2);
2177 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2178 bp->caps |= MACB_CAPS_FIFO_MODE;
2179 }
2180
2181 netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
2182}
2183
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002184static void macb_probe_queues(void __iomem *mem,
2185 unsigned int *queue_mask,
2186 unsigned int *num_queues)
2187{
2188 unsigned int hw_q;
2189 u32 mid;
2190
2191 *queue_mask = 0x1;
2192 *num_queues = 1;
2193
2194 /* is it macb or gem ? */
2195 mid = __raw_readl(mem + MACB_MID);
2196 if (MACB_BFEXT(IDNUM, mid) != 0x2)
2197 return;
2198
2199 /* bit 0 is never set but queue 0 always exists */
2200 *queue_mask = __raw_readl(mem + GEM_DCFG6) & 0xff;
2201 *queue_mask |= 0x1;
2202
2203 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2204 if (*queue_mask & (1 << hw_q))
2205 (*num_queues)++;
2206}
2207
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01002208static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002209{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00002210 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002211 struct resource *regs;
2212 struct net_device *dev;
2213 struct macb *bp;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002214 struct macb_queue *queue;
frederic RODO6c36a702007-07-12 19:07:24 +02002215 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002216 u32 config;
2217 int err = -ENXIO;
Guenter Roeck50907042013-04-02 09:35:09 +00002218 const char *mac;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002219 void __iomem *mem;
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002220 unsigned int hw_q, queue_mask, q, num_queues;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002221 struct clk *pclk, *hclk, *tx_clk;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002222
2223 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2224 if (!regs) {
2225 dev_err(&pdev->dev, "no mmio resource defined\n");
2226 goto err_out;
2227 }
2228
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002229 pclk = devm_clk_get(&pdev->dev, "pclk");
2230 if (IS_ERR(pclk)) {
2231 err = PTR_ERR(pclk);
2232 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002233 goto err_out;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002234 }
2235
2236 hclk = devm_clk_get(&pdev->dev, "hclk");
2237 if (IS_ERR(hclk)) {
2238 err = PTR_ERR(hclk);
2239 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2240 goto err_out;
2241 }
2242
2243 tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2244
2245 err = clk_prepare_enable(pclk);
2246 if (err) {
2247 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2248 goto err_out;
2249 }
2250
2251 err = clk_prepare_enable(hclk);
2252 if (err) {
2253 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2254 goto err_out_disable_pclk;
2255 }
2256
2257 if (!IS_ERR(tx_clk)) {
2258 err = clk_prepare_enable(tx_clk);
2259 if (err) {
2260 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n",
2261 err);
2262 goto err_out_disable_hclk;
2263 }
2264 }
2265
2266 err = -ENOMEM;
2267 mem = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
2268 if (!mem) {
2269 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
2270 goto err_out_disable_clocks;
2271 }
2272
2273 macb_probe_queues(mem, &queue_mask, &num_queues);
2274 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
2275 if (!dev)
2276 goto err_out_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002277
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002278 SET_NETDEV_DEV(dev, &pdev->dev);
2279
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002280 bp = netdev_priv(dev);
2281 bp->pdev = pdev;
2282 bp->dev = dev;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002283 bp->regs = mem;
2284 bp->num_queues = num_queues;
2285 bp->pclk = pclk;
2286 bp->hclk = hclk;
2287 bp->tx_clk = tx_clk;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002288
2289 spin_lock_init(&bp->lock);
2290
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002291 /* set the queue register mapping once for all: queue0 has a special
2292 * register mapping but we don't want to test the queue index then
2293 * compute the corresponding register offset at run time.
2294 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002295 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002296 if (!(queue_mask & (1 << hw_q)))
2297 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002298
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002299 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002300 queue->bp = bp;
2301 if (hw_q) {
2302 queue->ISR = GEM_ISR(hw_q - 1);
2303 queue->IER = GEM_IER(hw_q - 1);
2304 queue->IDR = GEM_IDR(hw_q - 1);
2305 queue->IMR = GEM_IMR(hw_q - 1);
2306 queue->TBQP = GEM_TBQP(hw_q - 1);
2307 } else {
2308 /* queue0 uses legacy registers */
2309 queue->ISR = MACB_ISR;
2310 queue->IER = MACB_IER;
2311 queue->IDR = MACB_IDR;
2312 queue->IMR = MACB_IMR;
2313 queue->TBQP = MACB_TBQP;
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002314 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002315
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002316 /* get irq: here we use the linux queue index, not the hardware
2317 * queue index. the queue irq definitions in the device tree
2318 * must remove the optional gaps that could exist in the
2319 * hardware queue mask.
2320 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002321 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002322 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
2323 0, dev->name, queue);
2324 if (err) {
2325 dev_err(&pdev->dev,
2326 "Unable to request IRQ %d (error %d)\n",
2327 queue->irq, err);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002328 goto err_out_free_netdev;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002329 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002330
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002331 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002332 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002333 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002334 dev->irq = bp->queues[0].irq;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002335
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002336 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002337 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002338
2339 dev->base_addr = regs->start;
2340
Nicolas Ferree1755872014-07-24 13:50:58 +02002341 /* setup capacities */
2342 macb_configure_caps(bp);
2343
Nicolas Ferre4df95132013-06-04 21:57:12 +00002344 /* setup appropriated routines according to adapter type */
2345 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002346 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002347 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2348 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2349 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2350 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002351 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002352 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002353 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002354 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2355 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2356 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2357 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002358 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002359 }
2360
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002361 /* Set features */
2362 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002363 /* Checksum offload is only available on gem with packet buffer */
2364 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002365 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002366 if (bp->caps & MACB_CAPS_SG_DISABLED)
2367 dev->hw_features &= ~NETIF_F_SG;
2368 dev->features = dev->hw_features;
2369
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002370 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002371 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00002372 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002373 macb_writel(bp, NCFGR, config);
2374
Guenter Roeck50907042013-04-02 09:35:09 +00002375 mac = of_get_mac_address(pdev->dev.of_node);
2376 if (mac)
2377 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2378 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002379 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002380
Guenter Roeck50907042013-04-02 09:35:09 +00002381 err = of_get_phy_mode(pdev->dev.of_node);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002382 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002383 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002384 if (pdata && pdata->is_rmii)
2385 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2386 else
2387 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2388 } else {
2389 bp->phy_interface = err;
2390 }
2391
Patrice Vilchez140b7552012-10-31 06:04:50 +00002392 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2393 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
2394 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01002395#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00002396 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
2397 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01002398#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00002399 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01002400#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002401 else
Andrew Victor0cc86742007-02-07 16:40:44 +01002402#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00002403 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01002404#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00002405 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01002406#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002407
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002408 err = register_netdev(dev);
2409 if (err) {
2410 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002411 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002412 }
2413
Nicolas Ferre72ca8202013-04-14 22:04:33 +00002414 err = macb_mii_init(bp);
2415 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02002416 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002417
frederic RODO6c36a702007-07-12 19:07:24 +02002418 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002419
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002420 netif_carrier_off(dev);
2421
Bo Shen58798232014-09-13 01:57:49 +02002422 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
2423 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
2424 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002425
frederic RODO6c36a702007-07-12 19:07:24 +02002426 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002427 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2428 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002429
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002430 return 0;
2431
frederic RODO6c36a702007-07-12 19:07:24 +02002432err_out_unregister_netdev:
2433 unregister_netdev(dev);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002434err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002435 free_netdev(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002436err_out_disable_clocks:
2437 if (!IS_ERR(tx_clk))
2438 clk_disable_unprepare(tx_clk);
2439err_out_disable_hclk:
2440 clk_disable_unprepare(hclk);
2441err_out_disable_pclk:
2442 clk_disable_unprepare(pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002443err_out:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002444 return err;
2445}
2446
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01002447static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002448{
2449 struct net_device *dev;
2450 struct macb *bp;
2451
2452 dev = platform_get_drvdata(pdev);
2453
2454 if (dev) {
2455 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002456 if (bp->phy_dev)
2457 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002458 mdiobus_unregister(bp->mii_bus);
2459 kfree(bp->mii_bus->irq);
2460 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002461 unregister_netdev(dev);
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002462 if (!IS_ERR(bp->tx_clk))
2463 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002464 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002465 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01002466 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002467 }
2468
2469 return 0;
2470}
2471
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002472#ifdef CONFIG_PM
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002473static int macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002474{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002475 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002476 struct net_device *netdev = platform_get_drvdata(pdev);
2477 struct macb *bp = netdev_priv(netdev);
2478
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002479 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002480 netif_device_detach(netdev);
2481
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002482 if (!IS_ERR(bp->tx_clk))
2483 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002484 clk_disable_unprepare(bp->hclk);
2485 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002486
2487 return 0;
2488}
2489
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002490static int macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002491{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002492 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002493 struct net_device *netdev = platform_get_drvdata(pdev);
2494 struct macb *bp = netdev_priv(netdev);
2495
Steffen Trumtrarace58012013-03-27 23:07:07 +00002496 clk_prepare_enable(bp->pclk);
2497 clk_prepare_enable(bp->hclk);
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002498 if (!IS_ERR(bp->tx_clk))
2499 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002500
2501 netif_device_attach(netdev);
2502
2503 return 0;
2504}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002505#endif
2506
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002507static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
2508
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002509static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01002510 .remove = __exit_p(macb_remove),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002511 .driver = {
2512 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002513 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002514 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002515 },
2516};
2517
Jingoo Hanb543a8d2013-03-04 16:43:18 +00002518module_platform_driver_probe(macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002519
2520MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00002521MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002522MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07002523MODULE_ALIAS("platform:macb");