blob: a4c5462c071a968713f36c0861bc1b8d78d54dae [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000026#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020028#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080029#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010030#include <linux/of_device.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010033
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010034#include "macb.h"
35
Nicolas Ferre1b447912013-06-04 21:57:11 +000036#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000037#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000038#define RX_RING_SIZE 512 /* must be power of 2 */
39#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010040
Havard Skinnemoen55054a12012-10-31 06:04:55 +000041#define TX_RING_SIZE 128 /* must be power of 2 */
42#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010043
Nicolas Ferre909a8582012-11-19 06:00:21 +000044/* level of occupied TX descriptors under which we wake up TX process */
45#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010046
47#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
48 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000049#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
50 | MACB_BIT(ISR_RLE) \
51 | MACB_BIT(TXERR))
52#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
53
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020054#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
55#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
56
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
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100105static void 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}
123
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100124static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100125{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000126 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100127 u32 bottom;
128 u16 top;
129 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000130 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100131
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900132 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000133
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000134 /* Check all 4 address register for vaild address */
135 for (i = 0; i < 4; i++) {
136 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
137 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100138
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000139 if (pdata && pdata->rev_eth_addr) {
140 addr[5] = bottom & 0xff;
141 addr[4] = (bottom >> 8) & 0xff;
142 addr[3] = (bottom >> 16) & 0xff;
143 addr[2] = (bottom >> 24) & 0xff;
144 addr[1] = top & 0xff;
145 addr[0] = (top & 0xff00) >> 8;
146 } else {
147 addr[0] = bottom & 0xff;
148 addr[1] = (bottom >> 8) & 0xff;
149 addr[2] = (bottom >> 16) & 0xff;
150 addr[3] = (bottom >> 24) & 0xff;
151 addr[4] = top & 0xff;
152 addr[5] = (top >> 8) & 0xff;
153 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100154
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000155 if (is_valid_ether_addr(addr)) {
156 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
157 return;
158 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700159 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000160
161 netdev_info(bp->dev, "invalid hw address, using random\n");
162 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100163}
164
frederic RODO6c36a702007-07-12 19:07:24 +0200165static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100166{
frederic RODO6c36a702007-07-12 19:07:24 +0200167 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100168 int value;
169
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100170 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
171 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200172 | MACB_BF(PHYA, mii_id)
173 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100174 | MACB_BF(CODE, MACB_MAN_CODE)));
175
frederic RODO6c36a702007-07-12 19:07:24 +0200176 /* wait for end of transfer */
177 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
178 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100179
180 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100181
182 return value;
183}
184
frederic RODO6c36a702007-07-12 19:07:24 +0200185static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
186 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100187{
frederic RODO6c36a702007-07-12 19:07:24 +0200188 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100189
190 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
191 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200192 | MACB_BF(PHYA, mii_id)
193 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100194 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200195 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100196
frederic RODO6c36a702007-07-12 19:07:24 +0200197 /* wait for end of transfer */
198 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
199 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100200
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100201 return 0;
202}
203
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800204/**
205 * macb_set_tx_clk() - Set a clock to a new frequency
206 * @clk Pointer to the clock to change
207 * @rate New frequency in Hz
208 * @dev Pointer to the struct net_device
209 */
210static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
211{
212 long ferr, rate, rate_rounded;
213
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100214 if (!clk)
215 return;
216
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800217 switch (speed) {
218 case SPEED_10:
219 rate = 2500000;
220 break;
221 case SPEED_100:
222 rate = 25000000;
223 break;
224 case SPEED_1000:
225 rate = 125000000;
226 break;
227 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800228 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800229 }
230
231 rate_rounded = clk_round_rate(clk, rate);
232 if (rate_rounded < 0)
233 return;
234
235 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
236 * is not satisfied.
237 */
238 ferr = abs(rate_rounded - rate);
239 ferr = DIV_ROUND_UP(ferr, rate / 100000);
240 if (ferr > 5)
241 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
242 rate);
243
244 if (clk_set_rate(clk, rate_rounded))
245 netdev_err(dev, "adjusting tx_clk failed.\n");
246}
247
frederic RODO6c36a702007-07-12 19:07:24 +0200248static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100249{
frederic RODO6c36a702007-07-12 19:07:24 +0200250 struct macb *bp = netdev_priv(dev);
251 struct phy_device *phydev = bp->phy_dev;
252 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100253
frederic RODO6c36a702007-07-12 19:07:24 +0200254 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100255
frederic RODO6c36a702007-07-12 19:07:24 +0200256 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100257
frederic RODO6c36a702007-07-12 19:07:24 +0200258 if (phydev->link) {
259 if ((bp->speed != phydev->speed) ||
260 (bp->duplex != phydev->duplex)) {
261 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100262
frederic RODO6c36a702007-07-12 19:07:24 +0200263 reg = macb_readl(bp, NCFGR);
264 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000265 if (macb_is_gem(bp))
266 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200267
268 if (phydev->duplex)
269 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900270 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200271 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200272 if (phydev->speed == SPEED_1000 &&
273 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000274 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200275
Patrice Vilchez140b7552012-10-31 06:04:50 +0000276 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200277
278 bp->speed = phydev->speed;
279 bp->duplex = phydev->duplex;
280 status_change = 1;
281 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282 }
283
frederic RODO6c36a702007-07-12 19:07:24 +0200284 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700285 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200286 bp->speed = 0;
287 bp->duplex = -1;
288 }
289 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100290
frederic RODO6c36a702007-07-12 19:07:24 +0200291 status_change = 1;
292 }
293
294 spin_unlock_irqrestore(&bp->lock, flags);
295
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100296 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800297
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 Eastwood2dbfdbb2012-11-11 13:56:27 +0000316 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000317 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-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 Eastwood2dbfdbb2012-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
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100360static int 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}
441
442static void macb_update_stats(struct macb *bp)
443{
444 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000445 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
446 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100447
448 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
449
450 for(; p < end; p++, reg++)
Arun Chandrana50dad32015-02-18 16:59:35 +0530451 *p += readl_relaxed(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100452}
453
Nicolas Ferree86cd532012-10-31 06:04:57 +0000454static int macb_halt_tx(struct macb *bp)
455{
456 unsigned long halt_time, timeout;
457 u32 status;
458
459 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
460
461 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
462 do {
463 halt_time = jiffies;
464 status = macb_readl(bp, TSR);
465 if (!(status & MACB_BIT(TGO)))
466 return 0;
467
468 usleep_range(10, 250);
469 } while (time_before(halt_time, timeout));
470
471 return -ETIMEDOUT;
472}
473
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200474static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
475{
476 if (tx_skb->mapping) {
477 if (tx_skb->mapped_as_page)
478 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
479 tx_skb->size, DMA_TO_DEVICE);
480 else
481 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
482 tx_skb->size, DMA_TO_DEVICE);
483 tx_skb->mapping = 0;
484 }
485
486 if (tx_skb->skb) {
487 dev_kfree_skb_any(tx_skb->skb);
488 tx_skb->skb = NULL;
489 }
490}
491
Nicolas Ferree86cd532012-10-31 06:04:57 +0000492static void macb_tx_error_task(struct work_struct *work)
493{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100494 struct macb_queue *queue = container_of(work, struct macb_queue,
495 tx_error_task);
496 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000497 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100498 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000499 struct sk_buff *skb;
500 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100501 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000502
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100503 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
504 (unsigned int)(queue - bp->queues),
505 queue->tx_tail, queue->tx_head);
506
507 /* Prevent the queue IRQ handlers from running: each of them may call
508 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
509 * As explained below, we have to halt the transmission before updating
510 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
511 * network engine about the macb/gem being halted.
512 */
513 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000514
515 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100516 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000517
518 /*
519 * Stop transmission now
520 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100521 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000522 */
523 if (macb_halt_tx(bp))
524 /* Just complain for now, reinitializing TX path can be good */
525 netdev_err(bp->dev, "BUG: halt tx timed out\n");
526
Nicolas Ferree86cd532012-10-31 06:04:57 +0000527 /*
528 * Treat frames in TX queue including the ones that caused the error.
529 * Free transmit buffers in upper layer.
530 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100531 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
532 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000533
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100534 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000535 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100536 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000537 skb = tx_skb->skb;
538
539 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200540 /* skb is set for the last buffer of the frame */
541 while (!skb) {
542 macb_tx_unmap(bp, tx_skb);
543 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100544 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200545 skb = tx_skb->skb;
546 }
547
548 /* ctrl still refers to the first buffer descriptor
549 * since it's the only one written back by the hardware
550 */
551 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
552 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
553 macb_tx_ring_wrap(tail), skb->data);
554 bp->stats.tx_packets++;
555 bp->stats.tx_bytes += skb->len;
556 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000557 } else {
558 /*
559 * "Buffers exhausted mid-frame" errors may only happen
560 * if the driver is buggy, so complain loudly about those.
561 * Statistics are updated by hardware.
562 */
563 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
564 netdev_err(bp->dev,
565 "BUG: TX buffers exhausted mid-frame\n");
566
567 desc->ctrl = ctrl | MACB_BIT(TX_USED);
568 }
569
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200570 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000571 }
572
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100573 /* Set end of TX queue */
574 desc = macb_tx_desc(queue, 0);
575 desc->addr = 0;
576 desc->ctrl = MACB_BIT(TX_USED);
577
Nicolas Ferree86cd532012-10-31 06:04:57 +0000578 /* Make descriptor updates visible to hardware */
579 wmb();
580
581 /* Reinitialize the TX desc queue */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100582 queue_writel(queue, TBQP, queue->tx_ring_dma);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000583 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100584 queue->tx_head = 0;
585 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000586
587 /* Housework before enabling TX IRQ */
588 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100589 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
590
591 /* Now we are ready to start transmission again */
592 netif_tx_start_all_queues(bp->dev);
593 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
594
595 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000596}
597
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100598static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100599{
600 unsigned int tail;
601 unsigned int head;
602 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100603 struct macb *bp = queue->bp;
604 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100605
606 status = macb_readl(bp, TSR);
607 macb_writel(bp, TSR, status);
608
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000609 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100610 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000611
Nicolas Ferree86cd532012-10-31 06:04:57 +0000612 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
613 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100614
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100615 head = queue->tx_head;
616 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000617 struct macb_tx_skb *tx_skb;
618 struct sk_buff *skb;
619 struct macb_dma_desc *desc;
620 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100621
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100622 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100623
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000624 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100625 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000626
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000627 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100628
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200629 /* TX_USED bit is only set by hardware on the very first buffer
630 * descriptor of the transmitted frame.
631 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000632 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100633 break;
634
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200635 /* Process all buffers of the current transmitted frame */
636 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100637 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200638 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000639
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200640 /* First, update TX stats if needed */
641 if (skb) {
642 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
643 macb_tx_ring_wrap(tail), skb->data);
644 bp->stats.tx_packets++;
645 bp->stats.tx_bytes += skb->len;
646 }
647
648 /* Now we can safely release resources */
649 macb_tx_unmap(bp, tx_skb);
650
651 /* skb is set only for the last buffer of the frame.
652 * WARNING: at this point skb has been freed by
653 * macb_tx_unmap().
654 */
655 if (skb)
656 break;
657 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100658 }
659
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100660 queue->tx_tail = tail;
661 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
662 CIRC_CNT(queue->tx_head, queue->tx_tail,
663 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
664 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100665}
666
Nicolas Ferre4df95132013-06-04 21:57:12 +0000667static void gem_rx_refill(struct macb *bp)
668{
669 unsigned int entry;
670 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000671 dma_addr_t paddr;
672
673 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000674 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000675
676 /* Make hw descriptor updates visible to CPU */
677 rmb();
678
Nicolas Ferre4df95132013-06-04 21:57:12 +0000679 bp->rx_prepared_head++;
680
Nicolas Ferre4df95132013-06-04 21:57:12 +0000681 if (bp->rx_skbuff[entry] == NULL) {
682 /* allocate sk_buff for this free entry in ring */
683 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
684 if (unlikely(skb == NULL)) {
685 netdev_err(bp->dev,
686 "Unable to allocate sk_buff\n");
687 break;
688 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000689
690 /* now fill corresponding descriptor entry */
691 paddr = dma_map_single(&bp->pdev->dev, skb->data,
692 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800693 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
694 dev_kfree_skb(skb);
695 break;
696 }
697
698 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000699
700 if (entry == RX_RING_SIZE - 1)
701 paddr |= MACB_BIT(RX_WRAP);
702 bp->rx_ring[entry].addr = paddr;
703 bp->rx_ring[entry].ctrl = 0;
704
705 /* properly align Ethernet header */
706 skb_reserve(skb, NET_IP_ALIGN);
707 }
708 }
709
710 /* Make descriptor updates visible to hardware */
711 wmb();
712
713 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
714 bp->rx_prepared_head, bp->rx_tail);
715}
716
717/* Mark DMA descriptors from begin up to and not including end as unused */
718static void discard_partial_frame(struct macb *bp, unsigned int begin,
719 unsigned int end)
720{
721 unsigned int frag;
722
723 for (frag = begin; frag != end; frag++) {
724 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
725 desc->addr &= ~MACB_BIT(RX_USED);
726 }
727
728 /* Make descriptor updates visible to hardware */
729 wmb();
730
731 /*
732 * When this happens, the hardware stats registers for
733 * whatever caused this is updated, so we don't have to record
734 * anything.
735 */
736}
737
738static int gem_rx(struct macb *bp, int budget)
739{
740 unsigned int len;
741 unsigned int entry;
742 struct sk_buff *skb;
743 struct macb_dma_desc *desc;
744 int count = 0;
745
746 while (count < budget) {
747 u32 addr, ctrl;
748
749 entry = macb_rx_ring_wrap(bp->rx_tail);
750 desc = &bp->rx_ring[entry];
751
752 /* Make hw descriptor updates visible to CPU */
753 rmb();
754
755 addr = desc->addr;
756 ctrl = desc->ctrl;
757
758 if (!(addr & MACB_BIT(RX_USED)))
759 break;
760
Nicolas Ferre4df95132013-06-04 21:57:12 +0000761 bp->rx_tail++;
762 count++;
763
764 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
765 netdev_err(bp->dev,
766 "not whole frame pointed by descriptor\n");
767 bp->stats.rx_dropped++;
768 break;
769 }
770 skb = bp->rx_skbuff[entry];
771 if (unlikely(!skb)) {
772 netdev_err(bp->dev,
773 "inconsistent Rx descriptor chain\n");
774 bp->stats.rx_dropped++;
775 break;
776 }
777 /* now everything is ready for receiving packet */
778 bp->rx_skbuff[entry] = NULL;
779 len = MACB_BFEXT(RX_FRMLEN, ctrl);
780
781 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
782
783 skb_put(skb, len);
784 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
785 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800786 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000787
788 skb->protocol = eth_type_trans(skb, bp->dev);
789 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200790 if (bp->dev->features & NETIF_F_RXCSUM &&
791 !(bp->dev->flags & IFF_PROMISC) &&
792 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
793 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000794
795 bp->stats.rx_packets++;
796 bp->stats.rx_bytes += skb->len;
797
798#if defined(DEBUG) && defined(VERBOSE_DEBUG)
799 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
800 skb->len, skb->csum);
801 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100802 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000803 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
804 skb->data, 32, true);
805#endif
806
807 netif_receive_skb(skb);
808 }
809
810 gem_rx_refill(bp);
811
812 return count;
813}
814
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100815static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
816 unsigned int last_frag)
817{
818 unsigned int len;
819 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000820 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100821 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000822 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100823
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000824 desc = macb_rx_desc(bp, last_frag);
825 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100826
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000827 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000828 macb_rx_ring_wrap(first_frag),
829 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100830
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000831 /*
832 * The ethernet header starts NET_IP_ALIGN bytes into the
833 * first buffer. Since the header is 14 bytes, this makes the
834 * payload word-aligned.
835 *
836 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
837 * the two padding bytes into the skb so that we avoid hitting
838 * the slowpath in memcpy(), and pull them off afterwards.
839 */
840 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100841 if (!skb) {
842 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000843 for (frag = first_frag; ; frag++) {
844 desc = macb_rx_desc(bp, frag);
845 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100846 if (frag == last_frag)
847 break;
848 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000849
850 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100851 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000852
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100853 return 1;
854 }
855
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000856 offset = 0;
857 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700858 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100859 skb_put(skb, len);
860
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000861 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000862 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100863
864 if (offset + frag_len > len) {
865 BUG_ON(frag != last_frag);
866 frag_len = len - offset;
867 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300868 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000869 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000870 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000871 desc = macb_rx_desc(bp, frag);
872 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100873
874 if (frag == last_frag)
875 break;
876 }
877
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000878 /* Make descriptor updates visible to hardware */
879 wmb();
880
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000881 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100882 skb->protocol = eth_type_trans(skb, bp->dev);
883
884 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000885 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000886 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000887 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100888 netif_receive_skb(skb);
889
890 return 0;
891}
892
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100893static int macb_rx(struct macb *bp, int budget)
894{
895 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000896 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100897 int first_frag = -1;
898
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000899 for (tail = bp->rx_tail; budget > 0; tail++) {
900 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100901 u32 addr, ctrl;
902
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000903 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100904 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000905
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000906 addr = desc->addr;
907 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100908
909 if (!(addr & MACB_BIT(RX_USED)))
910 break;
911
912 if (ctrl & MACB_BIT(RX_SOF)) {
913 if (first_frag != -1)
914 discard_partial_frame(bp, first_frag, tail);
915 first_frag = tail;
916 }
917
918 if (ctrl & MACB_BIT(RX_EOF)) {
919 int dropped;
920 BUG_ON(first_frag == -1);
921
922 dropped = macb_rx_frame(bp, first_frag, tail);
923 first_frag = -1;
924 if (!dropped) {
925 received++;
926 budget--;
927 }
928 }
929 }
930
931 if (first_frag != -1)
932 bp->rx_tail = first_frag;
933 else
934 bp->rx_tail = tail;
935
936 return received;
937}
938
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700939static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100940{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700941 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700942 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100943 u32 status;
944
945 status = macb_readl(bp, RSR);
946 macb_writel(bp, RSR, status);
947
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700948 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100949
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000950 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000951 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100952
Nicolas Ferre4df95132013-06-04 21:57:12 +0000953 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000954 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800955 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100956
Nicolas Ferre8770e912013-02-12 11:08:48 +0100957 /* Packets received while interrupts were disabled */
958 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -0700959 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700960 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
961 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +0100962 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700963 } else {
964 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
965 }
Joshua Hokeb3363692010-10-25 01:44:22 +0000966 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100967
968 /* TODO: Handle errors */
969
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700970 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100971}
972
973static irqreturn_t macb_interrupt(int irq, void *dev_id)
974{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100975 struct macb_queue *queue = dev_id;
976 struct macb *bp = queue->bp;
977 struct net_device *dev = bp->dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100978 u32 status;
979
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100980 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100981
982 if (unlikely(!status))
983 return IRQ_NONE;
984
985 spin_lock(&bp->lock);
986
987 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100988 /* close possible race with dev_close */
989 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100990 queue_writel(queue, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100991 break;
992 }
993
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100994 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
995 (unsigned int)(queue - bp->queues),
996 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000997
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100998 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000999 /*
1000 * There's no point taking any more interrupts
1001 * until we have processed the buffers. The
1002 * scheduling call may fail if the poll routine
1003 * is already scheduled, so disable interrupts
1004 * now.
1005 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001006 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001007 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001008 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001009
Ben Hutchings288379f2009-01-19 16:43:59 -08001010 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001011 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001012 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001013 }
1014 }
1015
Nicolas Ferree86cd532012-10-31 06:04:57 +00001016 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001017 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1018 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001019
1020 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001021 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001022
Nicolas Ferree86cd532012-10-31 06:04:57 +00001023 break;
1024 }
1025
1026 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001027 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001028
1029 /*
1030 * Link change detection isn't possible with RMII, so we'll
1031 * add that if/when we get our hands on a full-blown MII PHY.
1032 */
1033
Alexander Steinb19f7f72011-04-13 05:03:24 +00001034 if (status & MACB_BIT(ISR_ROVR)) {
1035 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001036 if (macb_is_gem(bp))
1037 bp->hw_stats.gem.rx_overruns++;
1038 else
1039 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001040
1041 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001042 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001043 }
1044
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001045 if (status & MACB_BIT(HRESP)) {
1046 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001047 * TODO: Reset the hardware, and maybe move the
1048 * netdev_err to a lower-priority context as well
1049 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001050 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001051 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001052
1053 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001054 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001055 }
1056
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001057 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001058 }
1059
1060 spin_unlock(&bp->lock);
1061
1062 return IRQ_HANDLED;
1063}
1064
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001065#ifdef CONFIG_NET_POLL_CONTROLLER
1066/*
1067 * Polling receive - used by netconsole and other diagnostic tools
1068 * to allow network i/o with interrupts disabled.
1069 */
1070static void macb_poll_controller(struct net_device *dev)
1071{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001072 struct macb *bp = netdev_priv(dev);
1073 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001074 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001075 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001076
1077 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001078 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1079 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001080 local_irq_restore(flags);
1081}
1082#endif
1083
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001084static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
1085 unsigned int len)
1086{
1087 return (len + bp->max_tx_length - 1) / bp->max_tx_length;
1088}
1089
1090static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001091 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001092 struct sk_buff *skb)
1093{
1094 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001095 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001096 struct macb_tx_skb *tx_skb = NULL;
1097 struct macb_dma_desc *desc;
1098 unsigned int offset, size, count = 0;
1099 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1100 unsigned int eof = 1;
1101 u32 ctrl;
1102
1103 /* First, map non-paged data */
1104 len = skb_headlen(skb);
1105 offset = 0;
1106 while (len) {
1107 size = min(len, bp->max_tx_length);
1108 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001109 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001110
1111 mapping = dma_map_single(&bp->pdev->dev,
1112 skb->data + offset,
1113 size, DMA_TO_DEVICE);
1114 if (dma_mapping_error(&bp->pdev->dev, mapping))
1115 goto dma_error;
1116
1117 /* Save info to properly release resources */
1118 tx_skb->skb = NULL;
1119 tx_skb->mapping = mapping;
1120 tx_skb->size = size;
1121 tx_skb->mapped_as_page = false;
1122
1123 len -= size;
1124 offset += size;
1125 count++;
1126 tx_head++;
1127 }
1128
1129 /* Then, map paged data from fragments */
1130 for (f = 0; f < nr_frags; f++) {
1131 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1132
1133 len = skb_frag_size(frag);
1134 offset = 0;
1135 while (len) {
1136 size = min(len, bp->max_tx_length);
1137 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001138 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001139
1140 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1141 offset, size, DMA_TO_DEVICE);
1142 if (dma_mapping_error(&bp->pdev->dev, mapping))
1143 goto dma_error;
1144
1145 /* Save info to properly release resources */
1146 tx_skb->skb = NULL;
1147 tx_skb->mapping = mapping;
1148 tx_skb->size = size;
1149 tx_skb->mapped_as_page = true;
1150
1151 len -= size;
1152 offset += size;
1153 count++;
1154 tx_head++;
1155 }
1156 }
1157
1158 /* Should never happen */
1159 if (unlikely(tx_skb == NULL)) {
1160 netdev_err(bp->dev, "BUG! empty skb!\n");
1161 return 0;
1162 }
1163
1164 /* This is the last buffer of the frame: save socket buffer */
1165 tx_skb->skb = skb;
1166
1167 /* Update TX ring: update buffer descriptors in reverse order
1168 * to avoid race condition
1169 */
1170
1171 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1172 * to set the end of TX queue
1173 */
1174 i = tx_head;
1175 entry = macb_tx_ring_wrap(i);
1176 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001177 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001178 desc->ctrl = ctrl;
1179
1180 do {
1181 i--;
1182 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001183 tx_skb = &queue->tx_skb[entry];
1184 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001185
1186 ctrl = (u32)tx_skb->size;
1187 if (eof) {
1188 ctrl |= MACB_BIT(TX_LAST);
1189 eof = 0;
1190 }
1191 if (unlikely(entry == (TX_RING_SIZE - 1)))
1192 ctrl |= MACB_BIT(TX_WRAP);
1193
1194 /* Set TX buffer descriptor */
1195 desc->addr = tx_skb->mapping;
1196 /* desc->addr must be visible to hardware before clearing
1197 * 'TX_USED' bit in desc->ctrl.
1198 */
1199 wmb();
1200 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001201 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001202
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001203 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001204
1205 return count;
1206
1207dma_error:
1208 netdev_err(bp->dev, "TX DMA map failed\n");
1209
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001210 for (i = queue->tx_head; i != tx_head; i++) {
1211 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001212
1213 macb_tx_unmap(bp, tx_skb);
1214 }
1215
1216 return 0;
1217}
1218
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001219static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1220{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001221 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001222 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001223 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001224 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001225 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001226
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001227#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1228 netdev_vdbg(bp->dev,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001229 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1230 queue_index, skb->len, skb->head, skb->data,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001231 skb_tail_pointer(skb), skb_end_pointer(skb));
1232 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1233 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001234#endif
1235
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001236 /* Count how many TX buffer descriptors are needed to send this
1237 * socket buffer: skb fragments of jumbo frames may need to be
1238 * splitted into many buffer descriptors.
1239 */
1240 count = macb_count_tx_descriptors(bp, skb_headlen(skb));
1241 nr_frags = skb_shinfo(skb)->nr_frags;
1242 for (f = 0; f < nr_frags; f++) {
1243 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1244 count += macb_count_tx_descriptors(bp, frag_size);
1245 }
1246
Dongdong Deng48719532009-08-23 19:49:07 -07001247 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001248
1249 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001250 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1251 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001252 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001253 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001254 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001255 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001256 }
1257
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001258 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001259 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001260 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001261 goto unlock;
1262 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001263
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001264 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001265 wmb();
1266
Richard Cochrane0720922011-06-19 21:51:28 +00001267 skb_tx_timestamp(skb);
1268
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001269 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1270
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001271 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1272 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001273
Soren Brinkmann92030902014-03-04 08:46:39 -08001274unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001275 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001276
Patrick McHardy6ed10652009-06-23 06:03:08 +00001277 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001278}
1279
Nicolas Ferre4df95132013-06-04 21:57:12 +00001280static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001281{
1282 if (!macb_is_gem(bp)) {
1283 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1284 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001285 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001286
Nicolas Ferre1b447912013-06-04 21:57:11 +00001287 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001288 netdev_dbg(bp->dev,
1289 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001290 RX_BUFFER_MULTIPLE);
1291 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001292 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001293 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001294 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001295
1296 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1297 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001298}
1299
Nicolas Ferre4df95132013-06-04 21:57:12 +00001300static void gem_free_rx_buffers(struct macb *bp)
1301{
1302 struct sk_buff *skb;
1303 struct macb_dma_desc *desc;
1304 dma_addr_t addr;
1305 int i;
1306
1307 if (!bp->rx_skbuff)
1308 return;
1309
1310 for (i = 0; i < RX_RING_SIZE; i++) {
1311 skb = bp->rx_skbuff[i];
1312
1313 if (skb == NULL)
1314 continue;
1315
1316 desc = &bp->rx_ring[i];
1317 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001318 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001319 DMA_FROM_DEVICE);
1320 dev_kfree_skb_any(skb);
1321 skb = NULL;
1322 }
1323
1324 kfree(bp->rx_skbuff);
1325 bp->rx_skbuff = NULL;
1326}
1327
1328static void macb_free_rx_buffers(struct macb *bp)
1329{
1330 if (bp->rx_buffers) {
1331 dma_free_coherent(&bp->pdev->dev,
1332 RX_RING_SIZE * bp->rx_buffer_size,
1333 bp->rx_buffers, bp->rx_buffers_dma);
1334 bp->rx_buffers = NULL;
1335 }
1336}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001337
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001338static void macb_free_consistent(struct macb *bp)
1339{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001340 struct macb_queue *queue;
1341 unsigned int q;
1342
Nicolas Ferre4df95132013-06-04 21:57:12 +00001343 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001344 if (bp->rx_ring) {
1345 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1346 bp->rx_ring, bp->rx_ring_dma);
1347 bp->rx_ring = NULL;
1348 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001349
1350 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1351 kfree(queue->tx_skb);
1352 queue->tx_skb = NULL;
1353 if (queue->tx_ring) {
1354 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1355 queue->tx_ring, queue->tx_ring_dma);
1356 queue->tx_ring = NULL;
1357 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001358 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001359}
1360
1361static int gem_alloc_rx_buffers(struct macb *bp)
1362{
1363 int size;
1364
1365 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1366 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1367 if (!bp->rx_skbuff)
1368 return -ENOMEM;
1369 else
1370 netdev_dbg(bp->dev,
1371 "Allocated %d RX struct sk_buff entries at %p\n",
1372 RX_RING_SIZE, bp->rx_skbuff);
1373 return 0;
1374}
1375
1376static int macb_alloc_rx_buffers(struct macb *bp)
1377{
1378 int size;
1379
1380 size = RX_RING_SIZE * bp->rx_buffer_size;
1381 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1382 &bp->rx_buffers_dma, GFP_KERNEL);
1383 if (!bp->rx_buffers)
1384 return -ENOMEM;
1385 else
1386 netdev_dbg(bp->dev,
1387 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1388 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1389 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001390}
1391
1392static int macb_alloc_consistent(struct macb *bp)
1393{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001394 struct macb_queue *queue;
1395 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001396 int size;
1397
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001398 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1399 size = TX_RING_BYTES;
1400 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1401 &queue->tx_ring_dma,
1402 GFP_KERNEL);
1403 if (!queue->tx_ring)
1404 goto out_err;
1405 netdev_dbg(bp->dev,
1406 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1407 q, size, (unsigned long)queue->tx_ring_dma,
1408 queue->tx_ring);
1409
1410 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1411 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1412 if (!queue->tx_skb)
1413 goto out_err;
1414 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001415
1416 size = RX_RING_BYTES;
1417 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1418 &bp->rx_ring_dma, GFP_KERNEL);
1419 if (!bp->rx_ring)
1420 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001421 netdev_dbg(bp->dev,
1422 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1423 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001424
Nicolas Ferre4df95132013-06-04 21:57:12 +00001425 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001426 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427
1428 return 0;
1429
1430out_err:
1431 macb_free_consistent(bp);
1432 return -ENOMEM;
1433}
1434
Nicolas Ferre4df95132013-06-04 21:57:12 +00001435static void gem_init_rings(struct macb *bp)
1436{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001437 struct macb_queue *queue;
1438 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001439 int i;
1440
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001441 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1442 for (i = 0; i < TX_RING_SIZE; i++) {
1443 queue->tx_ring[i].addr = 0;
1444 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1445 }
1446 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1447 queue->tx_head = 0;
1448 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001449 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001450
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001451 bp->rx_tail = 0;
1452 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001453
1454 gem_rx_refill(bp);
1455}
1456
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001457static void macb_init_rings(struct macb *bp)
1458{
1459 int i;
1460 dma_addr_t addr;
1461
1462 addr = bp->rx_buffers_dma;
1463 for (i = 0; i < RX_RING_SIZE; i++) {
1464 bp->rx_ring[i].addr = addr;
1465 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001466 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001467 }
1468 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1469
1470 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001471 bp->queues[0].tx_ring[i].addr = 0;
1472 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
1473 bp->queues[0].tx_head = 0;
1474 bp->queues[0].tx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001475 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001476 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001477
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001478 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001479}
1480
1481static void macb_reset_hw(struct macb *bp)
1482{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001483 struct macb_queue *queue;
1484 unsigned int q;
1485
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001486 /*
1487 * Disable RX and TX (XXX: Should we halt the transmission
1488 * more gracefully?)
1489 */
1490 macb_writel(bp, NCR, 0);
1491
1492 /* Clear the stats registers (XXX: Update stats first?) */
1493 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1494
1495 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001496 macb_writel(bp, TSR, -1);
1497 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001498
1499 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001500 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1501 queue_writel(queue, IDR, -1);
1502 queue_readl(queue, ISR);
1503 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001504}
1505
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001506static u32 gem_mdc_clk_div(struct macb *bp)
1507{
1508 u32 config;
1509 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1510
1511 if (pclk_hz <= 20000000)
1512 config = GEM_BF(CLK, GEM_CLK_DIV8);
1513 else if (pclk_hz <= 40000000)
1514 config = GEM_BF(CLK, GEM_CLK_DIV16);
1515 else if (pclk_hz <= 80000000)
1516 config = GEM_BF(CLK, GEM_CLK_DIV32);
1517 else if (pclk_hz <= 120000000)
1518 config = GEM_BF(CLK, GEM_CLK_DIV48);
1519 else if (pclk_hz <= 160000000)
1520 config = GEM_BF(CLK, GEM_CLK_DIV64);
1521 else
1522 config = GEM_BF(CLK, GEM_CLK_DIV96);
1523
1524 return config;
1525}
1526
1527static u32 macb_mdc_clk_div(struct macb *bp)
1528{
1529 u32 config;
1530 unsigned long pclk_hz;
1531
1532 if (macb_is_gem(bp))
1533 return gem_mdc_clk_div(bp);
1534
1535 pclk_hz = clk_get_rate(bp->pclk);
1536 if (pclk_hz <= 20000000)
1537 config = MACB_BF(CLK, MACB_CLK_DIV8);
1538 else if (pclk_hz <= 40000000)
1539 config = MACB_BF(CLK, MACB_CLK_DIV16);
1540 else if (pclk_hz <= 80000000)
1541 config = MACB_BF(CLK, MACB_CLK_DIV32);
1542 else
1543 config = MACB_BF(CLK, MACB_CLK_DIV64);
1544
1545 return config;
1546}
1547
Jamie Iles757a03c2011-03-09 16:29:59 +00001548/*
1549 * Get the DMA bus width field of the network configuration register that we
1550 * should program. We find the width from decoding the design configuration
1551 * register to find the maximum supported data bus width.
1552 */
1553static u32 macb_dbw(struct macb *bp)
1554{
1555 if (!macb_is_gem(bp))
1556 return 0;
1557
1558 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1559 case 4:
1560 return GEM_BF(DBW, GEM_DBW128);
1561 case 2:
1562 return GEM_BF(DBW, GEM_DBW64);
1563 case 1:
1564 default:
1565 return GEM_BF(DBW, GEM_DBW32);
1566 }
1567}
1568
Jamie Iles0116da42011-03-14 17:38:30 +00001569/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001570 * Configure the receive DMA engine
1571 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001572 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001573 * (if not supported by FIFO, it will fallback to default)
1574 * - set both rx/tx packet buffers to full memory size
1575 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001576 */
1577static void macb_configure_dma(struct macb *bp)
1578{
1579 u32 dmacfg;
Arun Chandran62f69242015-03-01 11:38:02 +05301580 u32 tmp, ncr;
Jamie Iles0116da42011-03-14 17:38:30 +00001581
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);
Arun Chandrana50dad32015-02-18 16:59:35 +05301588 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301589
1590 /* Find the CPU endianness by using the loopback bit of net_ctrl
1591 * register. save it first. When the CPU is in big endian we
1592 * need to program swaped mode for management descriptor access.
1593 */
1594 ncr = macb_readl(bp, NCR);
1595 __raw_writel(MACB_BIT(LLB), bp->regs + MACB_NCR);
1596 tmp = __raw_readl(bp->regs + MACB_NCR);
1597
1598 if (tmp == MACB_BIT(LLB))
1599 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1600 else
1601 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1602
1603 /* Restore net_ctrl */
1604 macb_writel(bp, NCR, ncr);
1605
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001606 if (bp->dev->features & NETIF_F_HW_CSUM)
1607 dmacfg |= GEM_BIT(TXCOEN);
1608 else
1609 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001610 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1611 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001612 gem_writel(bp, DMACFG, dmacfg);
1613 }
1614}
1615
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001616static void macb_init_hw(struct macb *bp)
1617{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001618 struct macb_queue *queue;
1619 unsigned int q;
1620
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001621 u32 config;
1622
1623 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001624 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001625
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001626 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001627 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001628 config |= MACB_BIT(PAE); /* PAuse Enable */
1629 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001630 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001631 if (bp->dev->flags & IFF_PROMISC)
1632 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001633 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1634 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001635 if (!(bp->dev->flags & IFF_BROADCAST))
1636 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001637 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001638 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001639 bp->speed = SPEED_10;
1640 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001641
Jamie Iles0116da42011-03-14 17:38:30 +00001642 macb_configure_dma(bp);
1643
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001644 /* Initialize TX and RX buffers */
1645 macb_writel(bp, RBQP, bp->rx_ring_dma);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001646 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1647 queue_writel(queue, TBQP, queue->tx_ring_dma);
1648
1649 /* Enable interrupts */
1650 queue_writel(queue, IER,
1651 MACB_RX_INT_FLAGS |
1652 MACB_TX_INT_FLAGS |
1653 MACB_BIT(HRESP));
1654 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001655
1656 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001657 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001658}
1659
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001660/*
1661 * The hash address register is 64 bits long and takes up two
1662 * locations in the memory map. The least significant bits are stored
1663 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1664 *
1665 * The unicast hash enable and the multicast hash enable bits in the
1666 * network configuration register enable the reception of hash matched
1667 * frames. The destination address is reduced to a 6 bit index into
1668 * the 64 bit hash register using the following hash function. The
1669 * hash function is an exclusive or of every sixth bit of the
1670 * destination address.
1671 *
1672 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1673 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1674 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1675 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1676 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1677 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1678 *
1679 * da[0] represents the least significant bit of the first byte
1680 * received, that is, the multicast/unicast indicator, and da[47]
1681 * represents the most significant bit of the last byte received. If
1682 * the hash index, hi[n], points to a bit that is set in the hash
1683 * register then the frame will be matched according to whether the
1684 * frame is multicast or unicast. A multicast match will be signalled
1685 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1686 * index points to a bit set in the hash register. A unicast match
1687 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1688 * and the hash index points to a bit set in the hash register. To
1689 * receive all multicast frames, the hash register should be set with
1690 * all ones and the multicast hash enable bit should be set in the
1691 * network configuration register.
1692 */
1693
1694static inline int hash_bit_value(int bitnr, __u8 *addr)
1695{
1696 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1697 return 1;
1698 return 0;
1699}
1700
1701/*
1702 * Return the hash index value for the specified address.
1703 */
1704static int hash_get_index(__u8 *addr)
1705{
1706 int i, j, bitval;
1707 int hash_index = 0;
1708
1709 for (j = 0; j < 6; j++) {
1710 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001711 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001712
1713 hash_index |= (bitval << j);
1714 }
1715
1716 return hash_index;
1717}
1718
1719/*
1720 * Add multicast addresses to the internal multicast-hash table.
1721 */
1722static void macb_sethashtable(struct net_device *dev)
1723{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001724 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001725 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001726 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001727 struct macb *bp = netdev_priv(dev);
1728
1729 mc_filter[0] = mc_filter[1] = 0;
1730
Jiri Pirko22bedad32010-04-01 21:22:57 +00001731 netdev_for_each_mc_addr(ha, dev) {
1732 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001733 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1734 }
1735
Jamie Ilesf75ba502011-11-08 10:12:32 +00001736 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1737 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001738}
1739
1740/*
1741 * Enable/Disable promiscuous and multicast modes.
1742 */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001743static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001744{
1745 unsigned long cfg;
1746 struct macb *bp = netdev_priv(dev);
1747
1748 cfg = macb_readl(bp, NCFGR);
1749
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001750 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001751 /* Enable promiscuous mode */
1752 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001753
1754 /* Disable RX checksum offload */
1755 if (macb_is_gem(bp))
1756 cfg &= ~GEM_BIT(RXCOEN);
1757 } else {
1758 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001759 cfg &= ~MACB_BIT(CAF);
1760
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001761 /* Enable RX checksum offload only if requested */
1762 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1763 cfg |= GEM_BIT(RXCOEN);
1764 }
1765
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001766 if (dev->flags & IFF_ALLMULTI) {
1767 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001768 macb_or_gem_writel(bp, HRB, -1);
1769 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001770 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001771 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001772 /* Enable specific multicasts */
1773 macb_sethashtable(dev);
1774 cfg |= MACB_BIT(NCFGR_MTI);
1775 } else if (dev->flags & (~IFF_ALLMULTI)) {
1776 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001777 macb_or_gem_writel(bp, HRB, 0);
1778 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001779 cfg &= ~MACB_BIT(NCFGR_MTI);
1780 }
1781
1782 macb_writel(bp, NCFGR, cfg);
1783}
1784
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001785static int macb_open(struct net_device *dev)
1786{
1787 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001788 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001789 int err;
1790
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001791 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001792
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001793 /* carrier starts down */
1794 netif_carrier_off(dev);
1795
frederic RODO6c36a702007-07-12 19:07:24 +02001796 /* if the phy is not yet register, retry later*/
1797 if (!bp->phy_dev)
1798 return -EAGAIN;
1799
Nicolas Ferre1b447912013-06-04 21:57:11 +00001800 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001801 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001802
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001803 err = macb_alloc_consistent(bp);
1804 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001805 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1806 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001807 return err;
1808 }
1809
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001810 napi_enable(&bp->napi);
1811
Nicolas Ferre4df95132013-06-04 21:57:12 +00001812 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001813 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814
frederic RODO6c36a702007-07-12 19:07:24 +02001815 /* schedule a link state check */
1816 phy_start(bp->phy_dev);
1817
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001818 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001819
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001820 return 0;
1821}
1822
1823static int macb_close(struct net_device *dev)
1824{
1825 struct macb *bp = netdev_priv(dev);
1826 unsigned long flags;
1827
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001828 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001829 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001830
frederic RODO6c36a702007-07-12 19:07:24 +02001831 if (bp->phy_dev)
1832 phy_stop(bp->phy_dev);
1833
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001834 spin_lock_irqsave(&bp->lock, flags);
1835 macb_reset_hw(bp);
1836 netif_carrier_off(dev);
1837 spin_unlock_irqrestore(&bp->lock, flags);
1838
1839 macb_free_consistent(bp);
1840
1841 return 0;
1842}
1843
Jamie Ilesa494ed82011-03-09 16:26:35 +00001844static void gem_update_stats(struct macb *bp)
1845{
Xander Huff3ff13f12015-01-13 16:15:51 -06001846 int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001847 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001848
Xander Huff3ff13f12015-01-13 16:15:51 -06001849 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1850 u32 offset = gem_statistics[i].offset;
Arun Chandrana50dad32015-02-18 16:59:35 +05301851 u64 val = readl_relaxed(bp->regs + offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001852
1853 bp->ethtool_stats[i] += val;
1854 *p += val;
1855
1856 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1857 /* Add GEM_OCTTXH, GEM_OCTRXH */
Arun Chandrana50dad32015-02-18 16:59:35 +05301858 val = readl_relaxed(bp->regs + offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06001859 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06001860 *(++p) += val;
1861 }
1862 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00001863}
1864
1865static struct net_device_stats *gem_get_stats(struct macb *bp)
1866{
1867 struct gem_stats *hwstat = &bp->hw_stats.gem;
1868 struct net_device_stats *nstat = &bp->stats;
1869
1870 gem_update_stats(bp);
1871
1872 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1873 hwstat->rx_alignment_errors +
1874 hwstat->rx_resource_errors +
1875 hwstat->rx_overruns +
1876 hwstat->rx_oversize_frames +
1877 hwstat->rx_jabbers +
1878 hwstat->rx_undersized_frames +
1879 hwstat->rx_length_field_frame_errors);
1880 nstat->tx_errors = (hwstat->tx_late_collisions +
1881 hwstat->tx_excessive_collisions +
1882 hwstat->tx_underrun +
1883 hwstat->tx_carrier_sense_errors);
1884 nstat->multicast = hwstat->rx_multicast_frames;
1885 nstat->collisions = (hwstat->tx_single_collision_frames +
1886 hwstat->tx_multiple_collision_frames +
1887 hwstat->tx_excessive_collisions);
1888 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1889 hwstat->rx_jabbers +
1890 hwstat->rx_undersized_frames +
1891 hwstat->rx_length_field_frame_errors);
1892 nstat->rx_over_errors = hwstat->rx_resource_errors;
1893 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1894 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1895 nstat->rx_fifo_errors = hwstat->rx_overruns;
1896 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1897 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1898 nstat->tx_fifo_errors = hwstat->tx_underrun;
1899
1900 return nstat;
1901}
1902
Xander Huff3ff13f12015-01-13 16:15:51 -06001903static void gem_get_ethtool_stats(struct net_device *dev,
1904 struct ethtool_stats *stats, u64 *data)
1905{
1906 struct macb *bp;
1907
1908 bp = netdev_priv(dev);
1909 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06001910 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06001911}
1912
1913static int gem_get_sset_count(struct net_device *dev, int sset)
1914{
1915 switch (sset) {
1916 case ETH_SS_STATS:
1917 return GEM_STATS_LEN;
1918 default:
1919 return -EOPNOTSUPP;
1920 }
1921}
1922
1923static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
1924{
1925 int i;
1926
1927 switch (sset) {
1928 case ETH_SS_STATS:
1929 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
1930 memcpy(p, gem_statistics[i].stat_string,
1931 ETH_GSTRING_LEN);
1932 break;
1933 }
1934}
1935
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001936static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001937{
1938 struct macb *bp = netdev_priv(dev);
1939 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001940 struct macb_stats *hwstat = &bp->hw_stats.macb;
1941
1942 if (macb_is_gem(bp))
1943 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001944
frederic RODO6c36a702007-07-12 19:07:24 +02001945 /* read stats from hardware */
1946 macb_update_stats(bp);
1947
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001948 /* Convert HW stats into netdevice stats */
1949 nstat->rx_errors = (hwstat->rx_fcs_errors +
1950 hwstat->rx_align_errors +
1951 hwstat->rx_resource_errors +
1952 hwstat->rx_overruns +
1953 hwstat->rx_oversize_pkts +
1954 hwstat->rx_jabbers +
1955 hwstat->rx_undersize_pkts +
1956 hwstat->sqe_test_errors +
1957 hwstat->rx_length_mismatch);
1958 nstat->tx_errors = (hwstat->tx_late_cols +
1959 hwstat->tx_excessive_cols +
1960 hwstat->tx_underruns +
1961 hwstat->tx_carrier_errors);
1962 nstat->collisions = (hwstat->tx_single_cols +
1963 hwstat->tx_multiple_cols +
1964 hwstat->tx_excessive_cols);
1965 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1966 hwstat->rx_jabbers +
1967 hwstat->rx_undersize_pkts +
1968 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001969 nstat->rx_over_errors = hwstat->rx_resource_errors +
1970 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001971 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1972 nstat->rx_frame_errors = hwstat->rx_align_errors;
1973 nstat->rx_fifo_errors = hwstat->rx_overruns;
1974 /* XXX: What does "missed" mean? */
1975 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1976 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1977 nstat->tx_fifo_errors = hwstat->tx_underruns;
1978 /* Don't know about heartbeat or window errors... */
1979
1980 return nstat;
1981}
1982
1983static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1984{
1985 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001986 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001987
frederic RODO6c36a702007-07-12 19:07:24 +02001988 if (!phydev)
1989 return -ENODEV;
1990
1991 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992}
1993
1994static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1995{
1996 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001997 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001998
frederic RODO6c36a702007-07-12 19:07:24 +02001999 if (!phydev)
2000 return -ENODEV;
2001
2002 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002003}
2004
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002005static int macb_get_regs_len(struct net_device *netdev)
2006{
2007 return MACB_GREGS_NBR * sizeof(u32);
2008}
2009
2010static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2011 void *p)
2012{
2013 struct macb *bp = netdev_priv(dev);
2014 unsigned int tail, head;
2015 u32 *regs_buff = p;
2016
2017 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2018 | MACB_GREGS_VERSION;
2019
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002020 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2021 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002022
2023 regs_buff[0] = macb_readl(bp, NCR);
2024 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2025 regs_buff[2] = macb_readl(bp, NSR);
2026 regs_buff[3] = macb_readl(bp, TSR);
2027 regs_buff[4] = macb_readl(bp, RBQP);
2028 regs_buff[5] = macb_readl(bp, TBQP);
2029 regs_buff[6] = macb_readl(bp, RSR);
2030 regs_buff[7] = macb_readl(bp, IMR);
2031
2032 regs_buff[8] = tail;
2033 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002034 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2035 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002036
2037 if (macb_is_gem(bp)) {
2038 regs_buff[12] = gem_readl(bp, USRIO);
2039 regs_buff[13] = gem_readl(bp, DMACFG);
2040 }
2041}
2042
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002043static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002044 .get_settings = macb_get_settings,
2045 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002046 .get_regs_len = macb_get_regs_len,
2047 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002048 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002049 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff8cd5a562015-01-15 15:55:20 -06002050};
Xander Huff8cd5a562015-01-15 15:55:20 -06002051
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002052static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002053 .get_settings = macb_get_settings,
2054 .set_settings = macb_set_settings,
2055 .get_regs_len = macb_get_regs_len,
2056 .get_regs = macb_get_regs,
2057 .get_link = ethtool_op_get_link,
2058 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002059 .get_ethtool_stats = gem_get_ethtool_stats,
2060 .get_strings = gem_get_ethtool_strings,
2061 .get_sset_count = gem_get_sset_count,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002062};
2063
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002064static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002065{
2066 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002067 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002068
2069 if (!netif_running(dev))
2070 return -EINVAL;
2071
frederic RODO6c36a702007-07-12 19:07:24 +02002072 if (!phydev)
2073 return -ENODEV;
2074
Richard Cochran28b04112010-07-17 08:48:55 +00002075 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002076}
2077
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002078static int macb_set_features(struct net_device *netdev,
2079 netdev_features_t features)
2080{
2081 struct macb *bp = netdev_priv(netdev);
2082 netdev_features_t changed = features ^ netdev->features;
2083
2084 /* TX checksum offload */
2085 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2086 u32 dmacfg;
2087
2088 dmacfg = gem_readl(bp, DMACFG);
2089 if (features & NETIF_F_HW_CSUM)
2090 dmacfg |= GEM_BIT(TXCOEN);
2091 else
2092 dmacfg &= ~GEM_BIT(TXCOEN);
2093 gem_writel(bp, DMACFG, dmacfg);
2094 }
2095
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002096 /* RX checksum offload */
2097 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2098 u32 netcfg;
2099
2100 netcfg = gem_readl(bp, NCFGR);
2101 if (features & NETIF_F_RXCSUM &&
2102 !(netdev->flags & IFF_PROMISC))
2103 netcfg |= GEM_BIT(RXCOEN);
2104 else
2105 netcfg &= ~GEM_BIT(RXCOEN);
2106 gem_writel(bp, NCFGR, netcfg);
2107 }
2108
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002109 return 0;
2110}
2111
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002112static const struct net_device_ops macb_netdev_ops = {
2113 .ndo_open = macb_open,
2114 .ndo_stop = macb_close,
2115 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002116 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002117 .ndo_get_stats = macb_get_stats,
2118 .ndo_do_ioctl = macb_ioctl,
2119 .ndo_validate_addr = eth_validate_addr,
2120 .ndo_change_mtu = eth_change_mtu,
2121 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002122#ifdef CONFIG_NET_POLL_CONTROLLER
2123 .ndo_poll_controller = macb_poll_controller,
2124#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002125 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002126};
2127
Nicolas Ferree1755872014-07-24 13:50:58 +02002128/*
2129 * Configure peripheral capacities according to device tree
2130 * and integration options used
2131 */
2132static void macb_configure_caps(struct macb *bp)
2133{
2134 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002135
2136 if (MACB_BFEXT(IDNUM, macb_readl(bp, MID)) == 0x2)
2137 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2138
2139 if (macb_is_gem(bp)) {
2140 dcfg = gem_readl(bp, DCFG1);
2141 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2142 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2143 dcfg = gem_readl(bp, DCFG2);
2144 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2145 bp->caps |= MACB_CAPS_FIFO_MODE;
2146 }
2147
2148 netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
2149}
2150
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002151static void macb_probe_queues(void __iomem *mem,
2152 unsigned int *queue_mask,
2153 unsigned int *num_queues)
2154{
2155 unsigned int hw_q;
2156 u32 mid;
2157
2158 *queue_mask = 0x1;
2159 *num_queues = 1;
2160
2161 /* is it macb or gem ? */
Arun Chandrana50dad32015-02-18 16:59:35 +05302162 mid = readl_relaxed(mem + MACB_MID);
2163
Punnaiah Choudary Kalluri8a013a92015-03-06 18:29:11 +01002164 if (MACB_BFEXT(IDNUM, mid) < 0x2)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002165 return;
2166
2167 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302168 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2169
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002170 *queue_mask |= 0x1;
2171
2172 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2173 if (*queue_mask & (1 << hw_q))
2174 (*num_queues)++;
2175}
2176
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002177static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002178{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002179 struct net_device *dev = platform_get_drvdata(pdev);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002180 unsigned int hw_q, queue_mask, q, num_queues;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002181 struct macb *bp = netdev_priv(dev);
2182 struct macb_queue *queue;
2183 int err;
2184 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002185
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002186 bp->pclk = devm_clk_get(&pdev->dev, "pclk");
2187 if (IS_ERR(bp->pclk)) {
2188 err = PTR_ERR(bp->pclk);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002189 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002190 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002191 }
2192
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002193 bp->hclk = devm_clk_get(&pdev->dev, "hclk");
2194 if (IS_ERR(bp->hclk)) {
2195 err = PTR_ERR(bp->hclk);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002196 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002197 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002198 }
2199
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002200 bp->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2201 if (IS_ERR(bp->tx_clk))
2202 bp->tx_clk = NULL;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002203
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002204 err = clk_prepare_enable(bp->pclk);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002205 if (err) {
2206 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002207 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002208 }
2209
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002210 err = clk_prepare_enable(bp->hclk);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002211 if (err) {
2212 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002213 goto err_disable_pclk;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002214 }
2215
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002216 err = clk_prepare_enable(bp->tx_clk);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002217 if (err) {
2218 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002219 goto err_disable_hclk;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002220 }
2221
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002222 /* set the queue register mapping once for all: queue0 has a special
2223 * register mapping but we don't want to test the queue index then
2224 * compute the corresponding register offset at run time.
2225 */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002226 macb_probe_queues(bp->regs, &queue_mask, &num_queues);
2227
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002228 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002229 if (!(queue_mask & (1 << hw_q)))
2230 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002231
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002232 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002233 queue->bp = bp;
2234 if (hw_q) {
2235 queue->ISR = GEM_ISR(hw_q - 1);
2236 queue->IER = GEM_IER(hw_q - 1);
2237 queue->IDR = GEM_IDR(hw_q - 1);
2238 queue->IMR = GEM_IMR(hw_q - 1);
2239 queue->TBQP = GEM_TBQP(hw_q - 1);
2240 } else {
2241 /* queue0 uses legacy registers */
2242 queue->ISR = MACB_ISR;
2243 queue->IER = MACB_IER;
2244 queue->IDR = MACB_IDR;
2245 queue->IMR = MACB_IMR;
2246 queue->TBQP = MACB_TBQP;
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002247 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002248
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002249 /* get irq: here we use the linux queue index, not the hardware
2250 * queue index. the queue irq definitions in the device tree
2251 * must remove the optional gaps that could exist in the
2252 * hardware queue mask.
2253 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002254 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002255 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002256 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002257 if (err) {
2258 dev_err(&pdev->dev,
2259 "Unable to request IRQ %d (error %d)\n",
2260 queue->irq, err);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002261 goto err_disable_tx_clk;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002262 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002263
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002264 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002265 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002266 }
2267
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002268 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002269 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002270
Nicolas Ferre4df95132013-06-04 21:57:12 +00002271 /* setup appropriated routines according to adapter type */
2272 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002273 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002274 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2275 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2276 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2277 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002278 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002279 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002280 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002281 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2282 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2283 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2284 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002285 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002286 }
2287
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002288 /* Set features */
2289 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002290 /* Checksum offload is only available on gem with packet buffer */
2291 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002292 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002293 if (bp->caps & MACB_CAPS_SG_DISABLED)
2294 dev->hw_features &= ~NETIF_F_SG;
2295 dev->features = dev->hw_features;
2296
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002297 val = 0;
2298 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2299 val = GEM_BIT(RGMII);
2300 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
2301 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2302 val = MACB_BIT(RMII);
2303 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2304 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002305
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002306 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2307 val |= MACB_BIT(CLKEN);
2308
2309 macb_or_gem_writel(bp, USRIO, val);
2310
2311 /* setup capacities */
2312 macb_configure_caps(bp);
2313
2314 /* Set MII management clock divider */
2315 val = macb_mdc_clk_div(bp);
2316 val |= macb_dbw(bp);
2317 macb_writel(bp, NCFGR, val);
2318
2319 return 0;
2320
2321err_disable_tx_clk:
2322 clk_disable_unprepare(bp->tx_clk);
2323
2324err_disable_hclk:
2325 clk_disable_unprepare(bp->hclk);
2326
2327err_disable_pclk:
2328 clk_disable_unprepare(bp->pclk);
2329
2330 return err;
2331}
2332
2333#if defined(CONFIG_OF)
2334/* 1518 rounded up */
2335#define AT91ETHER_MAX_RBUFF_SZ 0x600
2336/* max number of receive buffers */
2337#define AT91ETHER_MAX_RX_DESCR 9
2338
2339/* Initialize and start the Receiver and Transmit subsystems */
2340static int at91ether_start(struct net_device *dev)
2341{
2342 struct macb *lp = netdev_priv(dev);
2343 dma_addr_t addr;
2344 u32 ctl;
2345 int i;
2346
2347 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2348 (AT91ETHER_MAX_RX_DESCR *
2349 sizeof(struct macb_dma_desc)),
2350 &lp->rx_ring_dma, GFP_KERNEL);
2351 if (!lp->rx_ring)
2352 return -ENOMEM;
2353
2354 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2355 AT91ETHER_MAX_RX_DESCR *
2356 AT91ETHER_MAX_RBUFF_SZ,
2357 &lp->rx_buffers_dma, GFP_KERNEL);
2358 if (!lp->rx_buffers) {
2359 dma_free_coherent(&lp->pdev->dev,
2360 AT91ETHER_MAX_RX_DESCR *
2361 sizeof(struct macb_dma_desc),
2362 lp->rx_ring, lp->rx_ring_dma);
2363 lp->rx_ring = NULL;
2364 return -ENOMEM;
2365 }
2366
2367 addr = lp->rx_buffers_dma;
2368 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2369 lp->rx_ring[i].addr = addr;
2370 lp->rx_ring[i].ctrl = 0;
2371 addr += AT91ETHER_MAX_RBUFF_SZ;
2372 }
2373
2374 /* Set the Wrap bit on the last descriptor */
2375 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2376
2377 /* Reset buffer index */
2378 lp->rx_tail = 0;
2379
2380 /* Program address of descriptor list in Rx Buffer Queue register */
2381 macb_writel(lp, RBQP, lp->rx_ring_dma);
2382
2383 /* Enable Receive and Transmit */
2384 ctl = macb_readl(lp, NCR);
2385 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2386
2387 return 0;
2388}
2389
2390/* Open the ethernet interface */
2391static int at91ether_open(struct net_device *dev)
2392{
2393 struct macb *lp = netdev_priv(dev);
2394 u32 ctl;
2395 int ret;
2396
2397 /* Clear internal statistics */
2398 ctl = macb_readl(lp, NCR);
2399 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2400
2401 macb_set_hwaddr(lp);
2402
2403 ret = at91ether_start(dev);
2404 if (ret)
2405 return ret;
2406
2407 /* Enable MAC interrupts */
2408 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2409 MACB_BIT(RXUBR) |
2410 MACB_BIT(ISR_TUND) |
2411 MACB_BIT(ISR_RLE) |
2412 MACB_BIT(TCOMP) |
2413 MACB_BIT(ISR_ROVR) |
2414 MACB_BIT(HRESP));
2415
2416 /* schedule a link state check */
2417 phy_start(lp->phy_dev);
2418
2419 netif_start_queue(dev);
2420
2421 return 0;
2422}
2423
2424/* Close the interface */
2425static int at91ether_close(struct net_device *dev)
2426{
2427 struct macb *lp = netdev_priv(dev);
2428 u32 ctl;
2429
2430 /* Disable Receiver and Transmitter */
2431 ctl = macb_readl(lp, NCR);
2432 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2433
2434 /* Disable MAC interrupts */
2435 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2436 MACB_BIT(RXUBR) |
2437 MACB_BIT(ISR_TUND) |
2438 MACB_BIT(ISR_RLE) |
2439 MACB_BIT(TCOMP) |
2440 MACB_BIT(ISR_ROVR) |
2441 MACB_BIT(HRESP));
2442
2443 netif_stop_queue(dev);
2444
2445 dma_free_coherent(&lp->pdev->dev,
2446 AT91ETHER_MAX_RX_DESCR *
2447 sizeof(struct macb_dma_desc),
2448 lp->rx_ring, lp->rx_ring_dma);
2449 lp->rx_ring = NULL;
2450
2451 dma_free_coherent(&lp->pdev->dev,
2452 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2453 lp->rx_buffers, lp->rx_buffers_dma);
2454 lp->rx_buffers = NULL;
2455
2456 return 0;
2457}
2458
2459/* Transmit packet */
2460static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2461{
2462 struct macb *lp = netdev_priv(dev);
2463
2464 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2465 netif_stop_queue(dev);
2466
2467 /* Store packet information (to free when Tx completed) */
2468 lp->skb = skb;
2469 lp->skb_length = skb->len;
2470 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2471 DMA_TO_DEVICE);
2472
2473 /* Set address of the data in the Transmit Address register */
2474 macb_writel(lp, TAR, lp->skb_physaddr);
2475 /* Set length of the packet in the Transmit Control register */
2476 macb_writel(lp, TCR, skb->len);
2477
2478 } else {
2479 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2480 return NETDEV_TX_BUSY;
2481 }
2482
2483 return NETDEV_TX_OK;
2484}
2485
2486/* Extract received frame from buffer descriptors and sent to upper layers.
2487 * (Called from interrupt context)
2488 */
2489static void at91ether_rx(struct net_device *dev)
2490{
2491 struct macb *lp = netdev_priv(dev);
2492 unsigned char *p_recv;
2493 struct sk_buff *skb;
2494 unsigned int pktlen;
2495
2496 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2497 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2498 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2499 skb = netdev_alloc_skb(dev, pktlen + 2);
2500 if (skb) {
2501 skb_reserve(skb, 2);
2502 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2503
2504 skb->protocol = eth_type_trans(skb, dev);
2505 lp->stats.rx_packets++;
2506 lp->stats.rx_bytes += pktlen;
2507 netif_rx(skb);
2508 } else {
2509 lp->stats.rx_dropped++;
2510 }
2511
2512 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2513 lp->stats.multicast++;
2514
2515 /* reset ownership bit */
2516 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2517
2518 /* wrap after last buffer */
2519 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2520 lp->rx_tail = 0;
2521 else
2522 lp->rx_tail++;
2523 }
2524}
2525
2526/* MAC interrupt handler */
2527static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2528{
2529 struct net_device *dev = dev_id;
2530 struct macb *lp = netdev_priv(dev);
2531 u32 intstatus, ctl;
2532
2533 /* MAC Interrupt Status register indicates what interrupts are pending.
2534 * It is automatically cleared once read.
2535 */
2536 intstatus = macb_readl(lp, ISR);
2537
2538 /* Receive complete */
2539 if (intstatus & MACB_BIT(RCOMP))
2540 at91ether_rx(dev);
2541
2542 /* Transmit complete */
2543 if (intstatus & MACB_BIT(TCOMP)) {
2544 /* The TCOM bit is set even if the transmission failed */
2545 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2546 lp->stats.tx_errors++;
2547
2548 if (lp->skb) {
2549 dev_kfree_skb_irq(lp->skb);
2550 lp->skb = NULL;
2551 dma_unmap_single(NULL, lp->skb_physaddr,
2552 lp->skb_length, DMA_TO_DEVICE);
2553 lp->stats.tx_packets++;
2554 lp->stats.tx_bytes += lp->skb_length;
2555 }
2556 netif_wake_queue(dev);
2557 }
2558
2559 /* Work-around for EMAC Errata section 41.3.1 */
2560 if (intstatus & MACB_BIT(RXUBR)) {
2561 ctl = macb_readl(lp, NCR);
2562 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2563 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2564 }
2565
2566 if (intstatus & MACB_BIT(ISR_ROVR))
2567 netdev_err(dev, "ROVR error\n");
2568
2569 return IRQ_HANDLED;
2570}
2571
2572#ifdef CONFIG_NET_POLL_CONTROLLER
2573static void at91ether_poll_controller(struct net_device *dev)
2574{
2575 unsigned long flags;
2576
2577 local_irq_save(flags);
2578 at91ether_interrupt(dev->irq, dev);
2579 local_irq_restore(flags);
2580}
2581#endif
2582
2583static const struct net_device_ops at91ether_netdev_ops = {
2584 .ndo_open = at91ether_open,
2585 .ndo_stop = at91ether_close,
2586 .ndo_start_xmit = at91ether_start_xmit,
2587 .ndo_get_stats = macb_get_stats,
2588 .ndo_set_rx_mode = macb_set_rx_mode,
2589 .ndo_set_mac_address = eth_mac_addr,
2590 .ndo_do_ioctl = macb_ioctl,
2591 .ndo_validate_addr = eth_validate_addr,
2592 .ndo_change_mtu = eth_change_mtu,
2593#ifdef CONFIG_NET_POLL_CONTROLLER
2594 .ndo_poll_controller = at91ether_poll_controller,
2595#endif
2596};
2597
2598static int at91ether_init(struct platform_device *pdev)
2599{
2600 struct net_device *dev = platform_get_drvdata(pdev);
2601 struct macb *bp = netdev_priv(dev);
2602 int err;
2603 u32 reg;
2604
2605 bp->pclk = devm_clk_get(&pdev->dev, "ether_clk");
2606 if (IS_ERR(bp->pclk))
2607 return PTR_ERR(bp->pclk);
2608
2609 err = clk_prepare_enable(bp->pclk);
2610 if (err) {
2611 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2612 return err;
2613 }
2614
2615 dev->netdev_ops = &at91ether_netdev_ops;
2616 dev->ethtool_ops = &macb_ethtool_ops;
2617
2618 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2619 0, dev->name, dev);
2620 if (err)
2621 goto err_disable_clk;
2622
2623 macb_writel(bp, NCR, 0);
2624
2625 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2626 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2627 reg |= MACB_BIT(RM9200_RMII);
2628
2629 macb_writel(bp, NCFGR, reg);
2630
2631 return 0;
2632
2633err_disable_clk:
2634 clk_disable_unprepare(bp->pclk);
2635
2636 return err;
2637}
2638
2639static struct macb_config at91sam9260_config = {
2640 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII,
2641 .init = macb_init,
2642};
2643
2644static struct macb_config pc302gem_config = {
2645 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2646 .dma_burst_length = 16,
2647 .init = macb_init,
2648};
2649
2650static struct macb_config sama5d3_config = {
2651 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2652 .dma_burst_length = 16,
2653 .init = macb_init,
2654};
2655
2656static struct macb_config sama5d4_config = {
2657 .caps = 0,
2658 .dma_burst_length = 4,
2659 .init = macb_init,
2660};
2661
2662static struct macb_config emac_config = {
2663 .init = at91ether_init,
2664};
2665
2666static const struct of_device_id macb_dt_ids[] = {
2667 { .compatible = "cdns,at32ap7000-macb" },
2668 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2669 { .compatible = "cdns,macb" },
2670 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2671 { .compatible = "cdns,gem", .data = &pc302gem_config },
2672 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2673 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2674 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2675 { .compatible = "cdns,emac", .data = &emac_config },
2676 { /* sentinel */ }
2677};
2678MODULE_DEVICE_TABLE(of, macb_dt_ids);
2679#endif /* CONFIG_OF */
2680
2681static int macb_probe(struct platform_device *pdev)
2682{
2683 int (*init)(struct platform_device *) = macb_init;
2684 struct device_node *np = pdev->dev.of_node;
2685 const struct macb_config *macb_config = NULL;
2686 unsigned int queue_mask, num_queues;
2687 struct macb_platform_data *pdata;
2688 struct phy_device *phydev;
2689 struct net_device *dev;
2690 struct resource *regs;
2691 void __iomem *mem;
2692 const char *mac;
2693 struct macb *bp;
2694 int err;
2695
2696 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2697 mem = devm_ioremap_resource(&pdev->dev, regs);
2698 if (IS_ERR(mem))
2699 return PTR_ERR(mem);
2700
2701 macb_probe_queues(mem, &queue_mask, &num_queues);
2702 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
2703 if (!dev)
2704 return -ENOMEM;
2705
2706 dev->base_addr = regs->start;
2707
2708 SET_NETDEV_DEV(dev, &pdev->dev);
2709
2710 bp = netdev_priv(dev);
2711 bp->pdev = pdev;
2712 bp->dev = dev;
2713 bp->regs = mem;
2714 bp->num_queues = num_queues;
2715 spin_lock_init(&bp->lock);
2716
2717 platform_set_drvdata(pdev, dev);
2718
2719 dev->irq = platform_get_irq(pdev, 0);
2720 if (dev->irq < 0)
2721 return dev->irq;
2722
2723 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00002724 if (mac)
2725 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2726 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002727 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002728
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002729 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002730 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002731 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002732 if (pdata && pdata->is_rmii)
2733 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2734 else
2735 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2736 } else {
2737 bp->phy_interface = err;
2738 }
2739
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002740 if (np) {
2741 const struct of_device_id *match;
Boris BREZILLONa8487482015-03-07 07:23:30 +01002742
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002743 match = of_match_node(macb_dt_ids, np);
2744 if (match)
2745 macb_config = match->data;
2746 }
Boris BREZILLONa8487482015-03-07 07:23:30 +01002747
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002748 if (macb_config) {
2749 bp->caps = macb_config->caps;
2750 bp->dma_burst_length = macb_config->dma_burst_length;
2751 init = macb_config->init;
2752 }
2753
2754 /* IP specific init */
2755 err = init(pdev);
2756 if (err)
2757 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002758
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002759 err = register_netdev(dev);
2760 if (err) {
2761 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002762 goto err_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002763 }
2764
Nicolas Ferre72ca8202013-04-14 22:04:33 +00002765 err = macb_mii_init(bp);
2766 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02002767 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002768
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002769 netif_carrier_off(dev);
2770
Bo Shen58798232014-09-13 01:57:49 +02002771 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
2772 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
2773 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002774
frederic RODO6c36a702007-07-12 19:07:24 +02002775 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002776 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2777 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002778
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002779 return 0;
2780
frederic RODO6c36a702007-07-12 19:07:24 +02002781err_out_unregister_netdev:
2782 unregister_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002783
2784err_disable_clocks:
2785 clk_disable_unprepare(bp->tx_clk);
2786 clk_disable_unprepare(bp->hclk);
2787 clk_disable_unprepare(bp->pclk);
2788
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002789err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002790 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002791
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002792 return err;
2793}
2794
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002795static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002796{
2797 struct net_device *dev;
2798 struct macb *bp;
2799
2800 dev = platform_get_drvdata(pdev);
2801
2802 if (dev) {
2803 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002804 if (bp->phy_dev)
2805 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002806 mdiobus_unregister(bp->mii_bus);
2807 kfree(bp->mii_bus->irq);
2808 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002809 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002810 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002811 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002812 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01002813 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002814 }
2815
2816 return 0;
2817}
2818
Michal Simekd23823d2015-01-23 09:36:03 +01002819static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002820{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002821 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002822 struct net_device *netdev = platform_get_drvdata(pdev);
2823 struct macb *bp = netdev_priv(netdev);
2824
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002825 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002826 netif_device_detach(netdev);
2827
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002828 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002829 clk_disable_unprepare(bp->hclk);
2830 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002831
2832 return 0;
2833}
2834
Michal Simekd23823d2015-01-23 09:36:03 +01002835static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002836{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002837 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002838 struct net_device *netdev = platform_get_drvdata(pdev);
2839 struct macb *bp = netdev_priv(netdev);
2840
Steffen Trumtrarace58012013-03-27 23:07:07 +00002841 clk_prepare_enable(bp->pclk);
2842 clk_prepare_enable(bp->hclk);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002843 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002844
2845 netif_device_attach(netdev);
2846
2847 return 0;
2848}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002849
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002850static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
2851
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002852static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002853 .probe = macb_probe,
2854 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002855 .driver = {
2856 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002857 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002858 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002859 },
2860};
2861
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002862module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002863
2864MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00002865MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002866MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07002867MODULE_ALIAS("platform:macb");