blob: 5f10dfc631d794ef13861b706e5c11445c9243b8 [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
296 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000297 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500298 /* Update the TX clock rate if and only if the link is
299 * up and there has been a link change.
300 */
301 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
302
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000303 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000304 netdev_info(dev, "link up (%d/%s)\n",
305 phydev->speed,
306 phydev->duplex == DUPLEX_FULL ?
307 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000308 } else {
309 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000310 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000311 }
frederic RODO6c36a702007-07-12 19:07:24 +0200312 }
313}
314
315/* based on au1000_eth. c*/
316static int macb_mii_probe(struct net_device *dev)
317{
318 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000319 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000320 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000321 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000322 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200323
Jiri Pirko7455a762010-02-08 05:12:08 +0000324 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200325 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000326 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200327 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200328 }
329
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000330 pdata = dev_get_platdata(&bp->pdev->dev);
331 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
332 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
333 if (!ret) {
334 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
335 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
336 }
337 }
frederic RODO6c36a702007-07-12 19:07:24 +0200338
339 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000340 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100341 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000342 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000343 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000344 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200345 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100346
frederic RODO6c36a702007-07-12 19:07:24 +0200347 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200348 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000349 phydev->supported &= PHY_GBIT_FEATURES;
350 else
351 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100352
frederic RODO6c36a702007-07-12 19:07:24 +0200353 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100354
frederic RODO6c36a702007-07-12 19:07:24 +0200355 bp->link = 0;
356 bp->speed = 0;
357 bp->duplex = -1;
358 bp->phy_dev = phydev;
359
360 return 0;
361}
362
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100363static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200364{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000365 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200366 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200367 int err = -ENXIO, i;
368
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200369 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200370 macb_writel(bp, NCR, MACB_BIT(MPE));
371
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700372 bp->mii_bus = mdiobus_alloc();
373 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200374 err = -ENOMEM;
375 goto err_out;
376 }
377
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700378 bp->mii_bus->name = "MACB_mii_bus";
379 bp->mii_bus->read = &macb_mdio_read;
380 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000381 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
382 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700383 bp->mii_bus->priv = bp;
384 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900385 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700386
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700387 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
388 if (!bp->mii_bus->irq) {
389 err = -ENOMEM;
390 goto err_out_free_mdiobus;
391 }
392
Jamie Iles91523942011-02-28 04:05:25 +0000393 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200394
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200395 np = bp->pdev->dev.of_node;
396 if (np) {
397 /* try dt phy registration */
398 err = of_mdiobus_register(bp->mii_bus, np);
399
400 /* fallback to standard phy registration if no phy were
401 found during dt phy registration */
402 if (!err && !phy_find_first(bp->mii_bus)) {
403 for (i = 0; i < PHY_MAX_ADDR; i++) {
404 struct phy_device *phydev;
405
406 phydev = mdiobus_scan(bp->mii_bus, i);
407 if (IS_ERR(phydev)) {
408 err = PTR_ERR(phydev);
409 break;
410 }
411 }
412
413 if (err)
414 goto err_out_unregister_bus;
415 }
416 } else {
417 for (i = 0; i < PHY_MAX_ADDR; i++)
418 bp->mii_bus->irq[i] = PHY_POLL;
419
420 if (pdata)
421 bp->mii_bus->phy_mask = pdata->phy_mask;
422
423 err = mdiobus_register(bp->mii_bus);
424 }
425
426 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200427 goto err_out_free_mdio_irq;
428
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200429 err = macb_mii_probe(bp->dev);
430 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200431 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200432
433 return 0;
434
435err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700436 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200437err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700438 kfree(bp->mii_bus->irq);
439err_out_free_mdiobus:
440 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200441err_out:
442 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100443}
444
445static void macb_update_stats(struct macb *bp)
446{
447 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000448 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
449 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100450
451 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
452
453 for(; p < end; p++, reg++)
Arun Chandrana50dad32015-02-18 16:59:35 +0530454 *p += readl_relaxed(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100455}
456
Nicolas Ferree86cd532012-10-31 06:04:57 +0000457static int macb_halt_tx(struct macb *bp)
458{
459 unsigned long halt_time, timeout;
460 u32 status;
461
462 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
463
464 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
465 do {
466 halt_time = jiffies;
467 status = macb_readl(bp, TSR);
468 if (!(status & MACB_BIT(TGO)))
469 return 0;
470
471 usleep_range(10, 250);
472 } while (time_before(halt_time, timeout));
473
474 return -ETIMEDOUT;
475}
476
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200477static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
478{
479 if (tx_skb->mapping) {
480 if (tx_skb->mapped_as_page)
481 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
482 tx_skb->size, DMA_TO_DEVICE);
483 else
484 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
485 tx_skb->size, DMA_TO_DEVICE);
486 tx_skb->mapping = 0;
487 }
488
489 if (tx_skb->skb) {
490 dev_kfree_skb_any(tx_skb->skb);
491 tx_skb->skb = NULL;
492 }
493}
494
Nicolas Ferree86cd532012-10-31 06:04:57 +0000495static void macb_tx_error_task(struct work_struct *work)
496{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100497 struct macb_queue *queue = container_of(work, struct macb_queue,
498 tx_error_task);
499 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000500 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100501 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000502 struct sk_buff *skb;
503 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100504 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000505
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100506 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
507 (unsigned int)(queue - bp->queues),
508 queue->tx_tail, queue->tx_head);
509
510 /* Prevent the queue IRQ handlers from running: each of them may call
511 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
512 * As explained below, we have to halt the transmission before updating
513 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
514 * network engine about the macb/gem being halted.
515 */
516 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000517
518 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100519 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000520
521 /*
522 * Stop transmission now
523 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100524 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000525 */
526 if (macb_halt_tx(bp))
527 /* Just complain for now, reinitializing TX path can be good */
528 netdev_err(bp->dev, "BUG: halt tx timed out\n");
529
Nicolas Ferree86cd532012-10-31 06:04:57 +0000530 /*
531 * Treat frames in TX queue including the ones that caused the error.
532 * Free transmit buffers in upper layer.
533 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100534 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
535 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000536
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100537 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000538 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100539 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000540 skb = tx_skb->skb;
541
542 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200543 /* skb is set for the last buffer of the frame */
544 while (!skb) {
545 macb_tx_unmap(bp, tx_skb);
546 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100547 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200548 skb = tx_skb->skb;
549 }
550
551 /* ctrl still refers to the first buffer descriptor
552 * since it's the only one written back by the hardware
553 */
554 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
555 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
556 macb_tx_ring_wrap(tail), skb->data);
557 bp->stats.tx_packets++;
558 bp->stats.tx_bytes += skb->len;
559 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000560 } else {
561 /*
562 * "Buffers exhausted mid-frame" errors may only happen
563 * if the driver is buggy, so complain loudly about those.
564 * Statistics are updated by hardware.
565 */
566 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
567 netdev_err(bp->dev,
568 "BUG: TX buffers exhausted mid-frame\n");
569
570 desc->ctrl = ctrl | MACB_BIT(TX_USED);
571 }
572
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200573 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000574 }
575
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100576 /* Set end of TX queue */
577 desc = macb_tx_desc(queue, 0);
578 desc->addr = 0;
579 desc->ctrl = MACB_BIT(TX_USED);
580
Nicolas Ferree86cd532012-10-31 06:04:57 +0000581 /* Make descriptor updates visible to hardware */
582 wmb();
583
584 /* Reinitialize the TX desc queue */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100585 queue_writel(queue, TBQP, queue->tx_ring_dma);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000586 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100587 queue->tx_head = 0;
588 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000589
590 /* Housework before enabling TX IRQ */
591 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100592 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
593
594 /* Now we are ready to start transmission again */
595 netif_tx_start_all_queues(bp->dev);
596 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
597
598 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000599}
600
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100601static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100602{
603 unsigned int tail;
604 unsigned int head;
605 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100606 struct macb *bp = queue->bp;
607 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100608
609 status = macb_readl(bp, TSR);
610 macb_writel(bp, TSR, status);
611
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000612 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100613 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000614
Nicolas Ferree86cd532012-10-31 06:04:57 +0000615 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
616 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100617
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100618 head = queue->tx_head;
619 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000620 struct macb_tx_skb *tx_skb;
621 struct sk_buff *skb;
622 struct macb_dma_desc *desc;
623 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100624
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100625 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100626
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000627 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100628 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000629
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000630 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100631
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200632 /* TX_USED bit is only set by hardware on the very first buffer
633 * descriptor of the transmitted frame.
634 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000635 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100636 break;
637
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200638 /* Process all buffers of the current transmitted frame */
639 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100640 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200641 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000642
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200643 /* First, update TX stats if needed */
644 if (skb) {
645 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
646 macb_tx_ring_wrap(tail), skb->data);
647 bp->stats.tx_packets++;
648 bp->stats.tx_bytes += skb->len;
649 }
650
651 /* Now we can safely release resources */
652 macb_tx_unmap(bp, tx_skb);
653
654 /* skb is set only for the last buffer of the frame.
655 * WARNING: at this point skb has been freed by
656 * macb_tx_unmap().
657 */
658 if (skb)
659 break;
660 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100661 }
662
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100663 queue->tx_tail = tail;
664 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
665 CIRC_CNT(queue->tx_head, queue->tx_tail,
666 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
667 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100668}
669
Nicolas Ferre4df95132013-06-04 21:57:12 +0000670static void gem_rx_refill(struct macb *bp)
671{
672 unsigned int entry;
673 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000674 dma_addr_t paddr;
675
676 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000677 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000678
679 /* Make hw descriptor updates visible to CPU */
680 rmb();
681
Nicolas Ferre4df95132013-06-04 21:57:12 +0000682 bp->rx_prepared_head++;
683
Nicolas Ferre4df95132013-06-04 21:57:12 +0000684 if (bp->rx_skbuff[entry] == NULL) {
685 /* allocate sk_buff for this free entry in ring */
686 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
687 if (unlikely(skb == NULL)) {
688 netdev_err(bp->dev,
689 "Unable to allocate sk_buff\n");
690 break;
691 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000692
693 /* now fill corresponding descriptor entry */
694 paddr = dma_map_single(&bp->pdev->dev, skb->data,
695 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800696 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
697 dev_kfree_skb(skb);
698 break;
699 }
700
701 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000702
703 if (entry == RX_RING_SIZE - 1)
704 paddr |= MACB_BIT(RX_WRAP);
705 bp->rx_ring[entry].addr = paddr;
706 bp->rx_ring[entry].ctrl = 0;
707
708 /* properly align Ethernet header */
709 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530710 } else {
711 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
712 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000713 }
714 }
715
716 /* Make descriptor updates visible to hardware */
717 wmb();
718
719 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
720 bp->rx_prepared_head, bp->rx_tail);
721}
722
723/* Mark DMA descriptors from begin up to and not including end as unused */
724static void discard_partial_frame(struct macb *bp, unsigned int begin,
725 unsigned int end)
726{
727 unsigned int frag;
728
729 for (frag = begin; frag != end; frag++) {
730 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
731 desc->addr &= ~MACB_BIT(RX_USED);
732 }
733
734 /* Make descriptor updates visible to hardware */
735 wmb();
736
737 /*
738 * When this happens, the hardware stats registers for
739 * whatever caused this is updated, so we don't have to record
740 * anything.
741 */
742}
743
744static int gem_rx(struct macb *bp, int budget)
745{
746 unsigned int len;
747 unsigned int entry;
748 struct sk_buff *skb;
749 struct macb_dma_desc *desc;
750 int count = 0;
751
752 while (count < budget) {
753 u32 addr, ctrl;
754
755 entry = macb_rx_ring_wrap(bp->rx_tail);
756 desc = &bp->rx_ring[entry];
757
758 /* Make hw descriptor updates visible to CPU */
759 rmb();
760
761 addr = desc->addr;
762 ctrl = desc->ctrl;
763
764 if (!(addr & MACB_BIT(RX_USED)))
765 break;
766
Nicolas Ferre4df95132013-06-04 21:57:12 +0000767 bp->rx_tail++;
768 count++;
769
770 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
771 netdev_err(bp->dev,
772 "not whole frame pointed by descriptor\n");
773 bp->stats.rx_dropped++;
774 break;
775 }
776 skb = bp->rx_skbuff[entry];
777 if (unlikely(!skb)) {
778 netdev_err(bp->dev,
779 "inconsistent Rx descriptor chain\n");
780 bp->stats.rx_dropped++;
781 break;
782 }
783 /* now everything is ready for receiving packet */
784 bp->rx_skbuff[entry] = NULL;
785 len = MACB_BFEXT(RX_FRMLEN, ctrl);
786
787 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
788
789 skb_put(skb, len);
790 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
791 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800792 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000793
794 skb->protocol = eth_type_trans(skb, bp->dev);
795 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200796 if (bp->dev->features & NETIF_F_RXCSUM &&
797 !(bp->dev->flags & IFF_PROMISC) &&
798 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
799 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000800
801 bp->stats.rx_packets++;
802 bp->stats.rx_bytes += skb->len;
803
804#if defined(DEBUG) && defined(VERBOSE_DEBUG)
805 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
806 skb->len, skb->csum);
807 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100808 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000809 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
810 skb->data, 32, true);
811#endif
812
813 netif_receive_skb(skb);
814 }
815
816 gem_rx_refill(bp);
817
818 return count;
819}
820
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100821static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
822 unsigned int last_frag)
823{
824 unsigned int len;
825 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000826 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100827 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000828 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100829
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000830 desc = macb_rx_desc(bp, last_frag);
831 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100832
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000833 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000834 macb_rx_ring_wrap(first_frag),
835 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100836
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000837 /*
838 * The ethernet header starts NET_IP_ALIGN bytes into the
839 * first buffer. Since the header is 14 bytes, this makes the
840 * payload word-aligned.
841 *
842 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
843 * the two padding bytes into the skb so that we avoid hitting
844 * the slowpath in memcpy(), and pull them off afterwards.
845 */
846 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100847 if (!skb) {
848 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000849 for (frag = first_frag; ; frag++) {
850 desc = macb_rx_desc(bp, frag);
851 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100852 if (frag == last_frag)
853 break;
854 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000855
856 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100857 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000858
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100859 return 1;
860 }
861
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000862 offset = 0;
863 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700864 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100865 skb_put(skb, len);
866
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000867 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000868 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100869
870 if (offset + frag_len > len) {
871 BUG_ON(frag != last_frag);
872 frag_len = len - offset;
873 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300874 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000875 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000876 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000877 desc = macb_rx_desc(bp, frag);
878 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100879
880 if (frag == last_frag)
881 break;
882 }
883
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000884 /* Make descriptor updates visible to hardware */
885 wmb();
886
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000887 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100888 skb->protocol = eth_type_trans(skb, bp->dev);
889
890 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000891 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000892 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000893 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100894 netif_receive_skb(skb);
895
896 return 0;
897}
898
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100899static int macb_rx(struct macb *bp, int budget)
900{
901 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000902 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100903 int first_frag = -1;
904
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000905 for (tail = bp->rx_tail; budget > 0; tail++) {
906 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100907 u32 addr, ctrl;
908
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000909 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100910 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000911
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000912 addr = desc->addr;
913 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100914
915 if (!(addr & MACB_BIT(RX_USED)))
916 break;
917
918 if (ctrl & MACB_BIT(RX_SOF)) {
919 if (first_frag != -1)
920 discard_partial_frame(bp, first_frag, tail);
921 first_frag = tail;
922 }
923
924 if (ctrl & MACB_BIT(RX_EOF)) {
925 int dropped;
926 BUG_ON(first_frag == -1);
927
928 dropped = macb_rx_frame(bp, first_frag, tail);
929 first_frag = -1;
930 if (!dropped) {
931 received++;
932 budget--;
933 }
934 }
935 }
936
937 if (first_frag != -1)
938 bp->rx_tail = first_frag;
939 else
940 bp->rx_tail = tail;
941
942 return received;
943}
944
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700945static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100946{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700947 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700948 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100949 u32 status;
950
951 status = macb_readl(bp, RSR);
952 macb_writel(bp, RSR, status);
953
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700954 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100955
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000956 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000957 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100958
Nicolas Ferre4df95132013-06-04 21:57:12 +0000959 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000960 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800961 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100962
Nicolas Ferre8770e912013-02-12 11:08:48 +0100963 /* Packets received while interrupts were disabled */
964 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -0700965 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700966 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
967 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +0100968 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700969 } else {
970 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
971 }
Joshua Hokeb3363692010-10-25 01:44:22 +0000972 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100973
974 /* TODO: Handle errors */
975
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700976 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100977}
978
979static irqreturn_t macb_interrupt(int irq, void *dev_id)
980{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100981 struct macb_queue *queue = dev_id;
982 struct macb *bp = queue->bp;
983 struct net_device *dev = bp->dev;
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -0500984 u32 status, ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100985
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100986 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100987
988 if (unlikely(!status))
989 return IRQ_NONE;
990
991 spin_lock(&bp->lock);
992
993 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100994 /* close possible race with dev_close */
995 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100996 queue_writel(queue, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100997 break;
998 }
999
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001000 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1001 (unsigned int)(queue - bp->queues),
1002 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001003
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001004 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +00001005 /*
1006 * There's no point taking any more interrupts
1007 * until we have processed the buffers. The
1008 * scheduling call may fail if the poll routine
1009 * is already scheduled, so disable interrupts
1010 * now.
1011 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001012 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001013 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001014 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001015
Ben Hutchings288379f2009-01-19 16:43:59 -08001016 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001017 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001018 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001019 }
1020 }
1021
Nicolas Ferree86cd532012-10-31 06:04:57 +00001022 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001023 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1024 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001025
1026 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001027 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001028
Nicolas Ferree86cd532012-10-31 06:04:57 +00001029 break;
1030 }
1031
1032 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001033 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001034
1035 /*
1036 * Link change detection isn't possible with RMII, so we'll
1037 * add that if/when we get our hands on a full-blown MII PHY.
1038 */
1039
Nathan Sullivan86b5e7d2015-05-13 17:01:36 -05001040 /* There is a hardware issue under heavy load where DMA can
1041 * stop, this causes endless "used buffer descriptor read"
1042 * interrupts but it can be cleared by re-enabling RX. See
1043 * the at91 manual, section 41.3.1 or the Zynq manual
1044 * section 16.7.4 for details.
1045 */
Nathan Sullivanbfbb92c2015-05-05 15:00:25 -05001046 if (status & MACB_BIT(RXUBR)) {
1047 ctrl = macb_readl(bp, NCR);
1048 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1049 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1050
1051 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1052 macb_writel(bp, ISR, MACB_BIT(RXUBR));
1053 }
1054
Alexander Steinb19f7f72011-04-13 05:03:24 +00001055 if (status & MACB_BIT(ISR_ROVR)) {
1056 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001057 if (macb_is_gem(bp))
1058 bp->hw_stats.gem.rx_overruns++;
1059 else
1060 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001061
1062 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001063 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001064 }
1065
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001066 if (status & MACB_BIT(HRESP)) {
1067 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001068 * TODO: Reset the hardware, and maybe move the
1069 * netdev_err to a lower-priority context as well
1070 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001071 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001072 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001073
1074 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001075 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076 }
1077
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001078 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001079 }
1080
1081 spin_unlock(&bp->lock);
1082
1083 return IRQ_HANDLED;
1084}
1085
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001086#ifdef CONFIG_NET_POLL_CONTROLLER
1087/*
1088 * Polling receive - used by netconsole and other diagnostic tools
1089 * to allow network i/o with interrupts disabled.
1090 */
1091static void macb_poll_controller(struct net_device *dev)
1092{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001093 struct macb *bp = netdev_priv(dev);
1094 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001095 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001096 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001097
1098 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001099 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1100 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001101 local_irq_restore(flags);
1102}
1103#endif
1104
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001105static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
1106 unsigned int len)
1107{
1108 return (len + bp->max_tx_length - 1) / bp->max_tx_length;
1109}
1110
1111static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001112 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001113 struct sk_buff *skb)
1114{
1115 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001116 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001117 struct macb_tx_skb *tx_skb = NULL;
1118 struct macb_dma_desc *desc;
1119 unsigned int offset, size, count = 0;
1120 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1121 unsigned int eof = 1;
1122 u32 ctrl;
1123
1124 /* First, map non-paged data */
1125 len = skb_headlen(skb);
1126 offset = 0;
1127 while (len) {
1128 size = min(len, bp->max_tx_length);
1129 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001130 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001131
1132 mapping = dma_map_single(&bp->pdev->dev,
1133 skb->data + offset,
1134 size, DMA_TO_DEVICE);
1135 if (dma_mapping_error(&bp->pdev->dev, mapping))
1136 goto dma_error;
1137
1138 /* Save info to properly release resources */
1139 tx_skb->skb = NULL;
1140 tx_skb->mapping = mapping;
1141 tx_skb->size = size;
1142 tx_skb->mapped_as_page = false;
1143
1144 len -= size;
1145 offset += size;
1146 count++;
1147 tx_head++;
1148 }
1149
1150 /* Then, map paged data from fragments */
1151 for (f = 0; f < nr_frags; f++) {
1152 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1153
1154 len = skb_frag_size(frag);
1155 offset = 0;
1156 while (len) {
1157 size = min(len, bp->max_tx_length);
1158 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001159 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001160
1161 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1162 offset, size, DMA_TO_DEVICE);
1163 if (dma_mapping_error(&bp->pdev->dev, mapping))
1164 goto dma_error;
1165
1166 /* Save info to properly release resources */
1167 tx_skb->skb = NULL;
1168 tx_skb->mapping = mapping;
1169 tx_skb->size = size;
1170 tx_skb->mapped_as_page = true;
1171
1172 len -= size;
1173 offset += size;
1174 count++;
1175 tx_head++;
1176 }
1177 }
1178
1179 /* Should never happen */
1180 if (unlikely(tx_skb == NULL)) {
1181 netdev_err(bp->dev, "BUG! empty skb!\n");
1182 return 0;
1183 }
1184
1185 /* This is the last buffer of the frame: save socket buffer */
1186 tx_skb->skb = skb;
1187
1188 /* Update TX ring: update buffer descriptors in reverse order
1189 * to avoid race condition
1190 */
1191
1192 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1193 * to set the end of TX queue
1194 */
1195 i = tx_head;
1196 entry = macb_tx_ring_wrap(i);
1197 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001198 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001199 desc->ctrl = ctrl;
1200
1201 do {
1202 i--;
1203 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001204 tx_skb = &queue->tx_skb[entry];
1205 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001206
1207 ctrl = (u32)tx_skb->size;
1208 if (eof) {
1209 ctrl |= MACB_BIT(TX_LAST);
1210 eof = 0;
1211 }
1212 if (unlikely(entry == (TX_RING_SIZE - 1)))
1213 ctrl |= MACB_BIT(TX_WRAP);
1214
1215 /* Set TX buffer descriptor */
1216 desc->addr = tx_skb->mapping;
1217 /* desc->addr must be visible to hardware before clearing
1218 * 'TX_USED' bit in desc->ctrl.
1219 */
1220 wmb();
1221 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001222 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001223
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001224 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001225
1226 return count;
1227
1228dma_error:
1229 netdev_err(bp->dev, "TX DMA map failed\n");
1230
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001231 for (i = queue->tx_head; i != tx_head; i++) {
1232 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001233
1234 macb_tx_unmap(bp, tx_skb);
1235 }
1236
1237 return 0;
1238}
1239
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001240static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1241{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001242 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001243 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001244 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001245 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001246 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001247
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001248#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1249 netdev_vdbg(bp->dev,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001250 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1251 queue_index, skb->len, skb->head, skb->data,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001252 skb_tail_pointer(skb), skb_end_pointer(skb));
1253 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1254 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001255#endif
1256
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001257 /* Count how many TX buffer descriptors are needed to send this
1258 * socket buffer: skb fragments of jumbo frames may need to be
1259 * splitted into many buffer descriptors.
1260 */
1261 count = macb_count_tx_descriptors(bp, skb_headlen(skb));
1262 nr_frags = skb_shinfo(skb)->nr_frags;
1263 for (f = 0; f < nr_frags; f++) {
1264 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1265 count += macb_count_tx_descriptors(bp, frag_size);
1266 }
1267
Dongdong Deng48719532009-08-23 19:49:07 -07001268 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001269
1270 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001271 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1272 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001273 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001274 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001275 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001276 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001277 }
1278
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001279 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001280 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001281 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001282 goto unlock;
1283 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001284
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001285 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001286 wmb();
1287
Richard Cochrane0720922011-06-19 21:51:28 +00001288 skb_tx_timestamp(skb);
1289
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001290 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1291
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001292 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1293 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001294
Soren Brinkmann92030902014-03-04 08:46:39 -08001295unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001296 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001297
Patrick McHardy6ed10652009-06-23 06:03:08 +00001298 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001299}
1300
Nicolas Ferre4df95132013-06-04 21:57:12 +00001301static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001302{
1303 if (!macb_is_gem(bp)) {
1304 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1305 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001306 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001307
Nicolas Ferre1b447912013-06-04 21:57:11 +00001308 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001309 netdev_dbg(bp->dev,
1310 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001311 RX_BUFFER_MULTIPLE);
1312 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001313 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001314 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001315 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001316
1317 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1318 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001319}
1320
Nicolas Ferre4df95132013-06-04 21:57:12 +00001321static void gem_free_rx_buffers(struct macb *bp)
1322{
1323 struct sk_buff *skb;
1324 struct macb_dma_desc *desc;
1325 dma_addr_t addr;
1326 int i;
1327
1328 if (!bp->rx_skbuff)
1329 return;
1330
1331 for (i = 0; i < RX_RING_SIZE; i++) {
1332 skb = bp->rx_skbuff[i];
1333
1334 if (skb == NULL)
1335 continue;
1336
1337 desc = &bp->rx_ring[i];
1338 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001339 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001340 DMA_FROM_DEVICE);
1341 dev_kfree_skb_any(skb);
1342 skb = NULL;
1343 }
1344
1345 kfree(bp->rx_skbuff);
1346 bp->rx_skbuff = NULL;
1347}
1348
1349static void macb_free_rx_buffers(struct macb *bp)
1350{
1351 if (bp->rx_buffers) {
1352 dma_free_coherent(&bp->pdev->dev,
1353 RX_RING_SIZE * bp->rx_buffer_size,
1354 bp->rx_buffers, bp->rx_buffers_dma);
1355 bp->rx_buffers = NULL;
1356 }
1357}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001358
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001359static void macb_free_consistent(struct macb *bp)
1360{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001361 struct macb_queue *queue;
1362 unsigned int q;
1363
Nicolas Ferre4df95132013-06-04 21:57:12 +00001364 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001365 if (bp->rx_ring) {
1366 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1367 bp->rx_ring, bp->rx_ring_dma);
1368 bp->rx_ring = NULL;
1369 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001370
1371 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1372 kfree(queue->tx_skb);
1373 queue->tx_skb = NULL;
1374 if (queue->tx_ring) {
1375 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1376 queue->tx_ring, queue->tx_ring_dma);
1377 queue->tx_ring = NULL;
1378 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001379 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001380}
1381
1382static int gem_alloc_rx_buffers(struct macb *bp)
1383{
1384 int size;
1385
1386 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1387 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1388 if (!bp->rx_skbuff)
1389 return -ENOMEM;
1390 else
1391 netdev_dbg(bp->dev,
1392 "Allocated %d RX struct sk_buff entries at %p\n",
1393 RX_RING_SIZE, bp->rx_skbuff);
1394 return 0;
1395}
1396
1397static int macb_alloc_rx_buffers(struct macb *bp)
1398{
1399 int size;
1400
1401 size = RX_RING_SIZE * bp->rx_buffer_size;
1402 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1403 &bp->rx_buffers_dma, GFP_KERNEL);
1404 if (!bp->rx_buffers)
1405 return -ENOMEM;
1406 else
1407 netdev_dbg(bp->dev,
1408 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1409 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1410 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001411}
1412
1413static int macb_alloc_consistent(struct macb *bp)
1414{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001415 struct macb_queue *queue;
1416 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001417 int size;
1418
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001419 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1420 size = TX_RING_BYTES;
1421 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1422 &queue->tx_ring_dma,
1423 GFP_KERNEL);
1424 if (!queue->tx_ring)
1425 goto out_err;
1426 netdev_dbg(bp->dev,
1427 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1428 q, size, (unsigned long)queue->tx_ring_dma,
1429 queue->tx_ring);
1430
1431 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1432 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1433 if (!queue->tx_skb)
1434 goto out_err;
1435 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001436
1437 size = RX_RING_BYTES;
1438 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1439 &bp->rx_ring_dma, GFP_KERNEL);
1440 if (!bp->rx_ring)
1441 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001442 netdev_dbg(bp->dev,
1443 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1444 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001445
Nicolas Ferre4df95132013-06-04 21:57:12 +00001446 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001447 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001448
1449 return 0;
1450
1451out_err:
1452 macb_free_consistent(bp);
1453 return -ENOMEM;
1454}
1455
Nicolas Ferre4df95132013-06-04 21:57:12 +00001456static void gem_init_rings(struct macb *bp)
1457{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001458 struct macb_queue *queue;
1459 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001460 int i;
1461
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001462 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1463 for (i = 0; i < TX_RING_SIZE; i++) {
1464 queue->tx_ring[i].addr = 0;
1465 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1466 }
1467 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1468 queue->tx_head = 0;
1469 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001470 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001471
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001472 bp->rx_tail = 0;
1473 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001474
1475 gem_rx_refill(bp);
1476}
1477
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001478static void macb_init_rings(struct macb *bp)
1479{
1480 int i;
1481 dma_addr_t addr;
1482
1483 addr = bp->rx_buffers_dma;
1484 for (i = 0; i < RX_RING_SIZE; i++) {
1485 bp->rx_ring[i].addr = addr;
1486 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001487 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001488 }
1489 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1490
1491 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001492 bp->queues[0].tx_ring[i].addr = 0;
1493 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001494 }
Ben Shelton21d35152015-04-22 17:28:54 -05001495 bp->queues[0].tx_head = 0;
1496 bp->queues[0].tx_tail = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001497 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001498
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001499 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001500}
1501
1502static void macb_reset_hw(struct macb *bp)
1503{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001504 struct macb_queue *queue;
1505 unsigned int q;
1506
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001507 /*
1508 * Disable RX and TX (XXX: Should we halt the transmission
1509 * more gracefully?)
1510 */
1511 macb_writel(bp, NCR, 0);
1512
1513 /* Clear the stats registers (XXX: Update stats first?) */
1514 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1515
1516 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001517 macb_writel(bp, TSR, -1);
1518 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001519
1520 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001521 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1522 queue_writel(queue, IDR, -1);
1523 queue_readl(queue, ISR);
1524 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001525}
1526
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001527static u32 gem_mdc_clk_div(struct macb *bp)
1528{
1529 u32 config;
1530 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1531
1532 if (pclk_hz <= 20000000)
1533 config = GEM_BF(CLK, GEM_CLK_DIV8);
1534 else if (pclk_hz <= 40000000)
1535 config = GEM_BF(CLK, GEM_CLK_DIV16);
1536 else if (pclk_hz <= 80000000)
1537 config = GEM_BF(CLK, GEM_CLK_DIV32);
1538 else if (pclk_hz <= 120000000)
1539 config = GEM_BF(CLK, GEM_CLK_DIV48);
1540 else if (pclk_hz <= 160000000)
1541 config = GEM_BF(CLK, GEM_CLK_DIV64);
1542 else
1543 config = GEM_BF(CLK, GEM_CLK_DIV96);
1544
1545 return config;
1546}
1547
1548static u32 macb_mdc_clk_div(struct macb *bp)
1549{
1550 u32 config;
1551 unsigned long pclk_hz;
1552
1553 if (macb_is_gem(bp))
1554 return gem_mdc_clk_div(bp);
1555
1556 pclk_hz = clk_get_rate(bp->pclk);
1557 if (pclk_hz <= 20000000)
1558 config = MACB_BF(CLK, MACB_CLK_DIV8);
1559 else if (pclk_hz <= 40000000)
1560 config = MACB_BF(CLK, MACB_CLK_DIV16);
1561 else if (pclk_hz <= 80000000)
1562 config = MACB_BF(CLK, MACB_CLK_DIV32);
1563 else
1564 config = MACB_BF(CLK, MACB_CLK_DIV64);
1565
1566 return config;
1567}
1568
Jamie Iles757a03c2011-03-09 16:29:59 +00001569/*
1570 * Get the DMA bus width field of the network configuration register that we
1571 * should program. We find the width from decoding the design configuration
1572 * register to find the maximum supported data bus width.
1573 */
1574static u32 macb_dbw(struct macb *bp)
1575{
1576 if (!macb_is_gem(bp))
1577 return 0;
1578
1579 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1580 case 4:
1581 return GEM_BF(DBW, GEM_DBW128);
1582 case 2:
1583 return GEM_BF(DBW, GEM_DBW64);
1584 case 1:
1585 default:
1586 return GEM_BF(DBW, GEM_DBW32);
1587 }
1588}
1589
Jamie Iles0116da42011-03-14 17:38:30 +00001590/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001591 * Configure the receive DMA engine
1592 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001593 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001594 * (if not supported by FIFO, it will fallback to default)
1595 * - set both rx/tx packet buffers to full memory size
1596 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001597 */
1598static void macb_configure_dma(struct macb *bp)
1599{
1600 u32 dmacfg;
Arun Chandran62f69242015-03-01 11:38:02 +05301601 u32 tmp, ncr;
Jamie Iles0116da42011-03-14 17:38:30 +00001602
1603 if (macb_is_gem(bp)) {
1604 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001605 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001606 if (bp->dma_burst_length)
1607 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001608 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301609 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301610
1611 /* Find the CPU endianness by using the loopback bit of net_ctrl
1612 * register. save it first. When the CPU is in big endian we
1613 * need to program swaped mode for management descriptor access.
1614 */
1615 ncr = macb_readl(bp, NCR);
1616 __raw_writel(MACB_BIT(LLB), bp->regs + MACB_NCR);
1617 tmp = __raw_readl(bp->regs + MACB_NCR);
1618
1619 if (tmp == MACB_BIT(LLB))
1620 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1621 else
1622 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1623
1624 /* Restore net_ctrl */
1625 macb_writel(bp, NCR, ncr);
1626
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001627 if (bp->dev->features & NETIF_F_HW_CSUM)
1628 dmacfg |= GEM_BIT(TXCOEN);
1629 else
1630 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001631 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1632 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001633 gem_writel(bp, DMACFG, dmacfg);
1634 }
1635}
1636
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001637static void macb_init_hw(struct macb *bp)
1638{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001639 struct macb_queue *queue;
1640 unsigned int q;
1641
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001642 u32 config;
1643
1644 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001645 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001646
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001647 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001648 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001649 config |= MACB_BIT(PAE); /* PAuse Enable */
1650 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001651 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001652 if (bp->dev->flags & IFF_PROMISC)
1653 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001654 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1655 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001656 if (!(bp->dev->flags & IFF_BROADCAST))
1657 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001658 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001659 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001660 bp->speed = SPEED_10;
1661 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001662
Jamie Iles0116da42011-03-14 17:38:30 +00001663 macb_configure_dma(bp);
1664
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001665 /* Initialize TX and RX buffers */
1666 macb_writel(bp, RBQP, bp->rx_ring_dma);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001667 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1668 queue_writel(queue, TBQP, queue->tx_ring_dma);
1669
1670 /* Enable interrupts */
1671 queue_writel(queue, IER,
1672 MACB_RX_INT_FLAGS |
1673 MACB_TX_INT_FLAGS |
1674 MACB_BIT(HRESP));
1675 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001676
1677 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001678 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001679}
1680
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001681/*
1682 * The hash address register is 64 bits long and takes up two
1683 * locations in the memory map. The least significant bits are stored
1684 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1685 *
1686 * The unicast hash enable and the multicast hash enable bits in the
1687 * network configuration register enable the reception of hash matched
1688 * frames. The destination address is reduced to a 6 bit index into
1689 * the 64 bit hash register using the following hash function. The
1690 * hash function is an exclusive or of every sixth bit of the
1691 * destination address.
1692 *
1693 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1694 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1695 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1696 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1697 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1698 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1699 *
1700 * da[0] represents the least significant bit of the first byte
1701 * received, that is, the multicast/unicast indicator, and da[47]
1702 * represents the most significant bit of the last byte received. If
1703 * the hash index, hi[n], points to a bit that is set in the hash
1704 * register then the frame will be matched according to whether the
1705 * frame is multicast or unicast. A multicast match will be signalled
1706 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1707 * index points to a bit set in the hash register. A unicast match
1708 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1709 * and the hash index points to a bit set in the hash register. To
1710 * receive all multicast frames, the hash register should be set with
1711 * all ones and the multicast hash enable bit should be set in the
1712 * network configuration register.
1713 */
1714
1715static inline int hash_bit_value(int bitnr, __u8 *addr)
1716{
1717 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1718 return 1;
1719 return 0;
1720}
1721
1722/*
1723 * Return the hash index value for the specified address.
1724 */
1725static int hash_get_index(__u8 *addr)
1726{
1727 int i, j, bitval;
1728 int hash_index = 0;
1729
1730 for (j = 0; j < 6; j++) {
1731 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001732 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001733
1734 hash_index |= (bitval << j);
1735 }
1736
1737 return hash_index;
1738}
1739
1740/*
1741 * Add multicast addresses to the internal multicast-hash table.
1742 */
1743static void macb_sethashtable(struct net_device *dev)
1744{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001745 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001746 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001747 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001748 struct macb *bp = netdev_priv(dev);
1749
1750 mc_filter[0] = mc_filter[1] = 0;
1751
Jiri Pirko22bedad32010-04-01 21:22:57 +00001752 netdev_for_each_mc_addr(ha, dev) {
1753 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001754 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1755 }
1756
Jamie Ilesf75ba502011-11-08 10:12:32 +00001757 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1758 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001759}
1760
1761/*
1762 * Enable/Disable promiscuous and multicast modes.
1763 */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001764static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001765{
1766 unsigned long cfg;
1767 struct macb *bp = netdev_priv(dev);
1768
1769 cfg = macb_readl(bp, NCFGR);
1770
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001771 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001772 /* Enable promiscuous mode */
1773 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001774
1775 /* Disable RX checksum offload */
1776 if (macb_is_gem(bp))
1777 cfg &= ~GEM_BIT(RXCOEN);
1778 } else {
1779 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001780 cfg &= ~MACB_BIT(CAF);
1781
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001782 /* Enable RX checksum offload only if requested */
1783 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1784 cfg |= GEM_BIT(RXCOEN);
1785 }
1786
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001787 if (dev->flags & IFF_ALLMULTI) {
1788 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001789 macb_or_gem_writel(bp, HRB, -1);
1790 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001791 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001792 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001793 /* Enable specific multicasts */
1794 macb_sethashtable(dev);
1795 cfg |= MACB_BIT(NCFGR_MTI);
1796 } else if (dev->flags & (~IFF_ALLMULTI)) {
1797 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001798 macb_or_gem_writel(bp, HRB, 0);
1799 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001800 cfg &= ~MACB_BIT(NCFGR_MTI);
1801 }
1802
1803 macb_writel(bp, NCFGR, cfg);
1804}
1805
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001806static int macb_open(struct net_device *dev)
1807{
1808 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001809 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001810 int err;
1811
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001812 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001813
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001814 /* carrier starts down */
1815 netif_carrier_off(dev);
1816
frederic RODO6c36a702007-07-12 19:07:24 +02001817 /* if the phy is not yet register, retry later*/
1818 if (!bp->phy_dev)
1819 return -EAGAIN;
1820
Nicolas Ferre1b447912013-06-04 21:57:11 +00001821 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001822 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001823
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001824 err = macb_alloc_consistent(bp);
1825 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001826 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1827 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001828 return err;
1829 }
1830
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001831 napi_enable(&bp->napi);
1832
Nicolas Ferre4df95132013-06-04 21:57:12 +00001833 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001834 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001835
frederic RODO6c36a702007-07-12 19:07:24 +02001836 /* schedule a link state check */
1837 phy_start(bp->phy_dev);
1838
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001839 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001840
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001841 return 0;
1842}
1843
1844static int macb_close(struct net_device *dev)
1845{
1846 struct macb *bp = netdev_priv(dev);
1847 unsigned long flags;
1848
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001849 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001850 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001851
frederic RODO6c36a702007-07-12 19:07:24 +02001852 if (bp->phy_dev)
1853 phy_stop(bp->phy_dev);
1854
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001855 spin_lock_irqsave(&bp->lock, flags);
1856 macb_reset_hw(bp);
1857 netif_carrier_off(dev);
1858 spin_unlock_irqrestore(&bp->lock, flags);
1859
1860 macb_free_consistent(bp);
1861
1862 return 0;
1863}
1864
Jamie Ilesa494ed82011-03-09 16:26:35 +00001865static void gem_update_stats(struct macb *bp)
1866{
Xander Huff3ff13f12015-01-13 16:15:51 -06001867 int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001868 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001869
Xander Huff3ff13f12015-01-13 16:15:51 -06001870 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1871 u32 offset = gem_statistics[i].offset;
Arun Chandrana50dad32015-02-18 16:59:35 +05301872 u64 val = readl_relaxed(bp->regs + offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001873
1874 bp->ethtool_stats[i] += val;
1875 *p += val;
1876
1877 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1878 /* Add GEM_OCTTXH, GEM_OCTRXH */
Arun Chandrana50dad32015-02-18 16:59:35 +05301879 val = readl_relaxed(bp->regs + offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06001880 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06001881 *(++p) += val;
1882 }
1883 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00001884}
1885
1886static struct net_device_stats *gem_get_stats(struct macb *bp)
1887{
1888 struct gem_stats *hwstat = &bp->hw_stats.gem;
1889 struct net_device_stats *nstat = &bp->stats;
1890
1891 gem_update_stats(bp);
1892
1893 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1894 hwstat->rx_alignment_errors +
1895 hwstat->rx_resource_errors +
1896 hwstat->rx_overruns +
1897 hwstat->rx_oversize_frames +
1898 hwstat->rx_jabbers +
1899 hwstat->rx_undersized_frames +
1900 hwstat->rx_length_field_frame_errors);
1901 nstat->tx_errors = (hwstat->tx_late_collisions +
1902 hwstat->tx_excessive_collisions +
1903 hwstat->tx_underrun +
1904 hwstat->tx_carrier_sense_errors);
1905 nstat->multicast = hwstat->rx_multicast_frames;
1906 nstat->collisions = (hwstat->tx_single_collision_frames +
1907 hwstat->tx_multiple_collision_frames +
1908 hwstat->tx_excessive_collisions);
1909 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1910 hwstat->rx_jabbers +
1911 hwstat->rx_undersized_frames +
1912 hwstat->rx_length_field_frame_errors);
1913 nstat->rx_over_errors = hwstat->rx_resource_errors;
1914 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1915 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1916 nstat->rx_fifo_errors = hwstat->rx_overruns;
1917 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1918 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1919 nstat->tx_fifo_errors = hwstat->tx_underrun;
1920
1921 return nstat;
1922}
1923
Xander Huff3ff13f12015-01-13 16:15:51 -06001924static void gem_get_ethtool_stats(struct net_device *dev,
1925 struct ethtool_stats *stats, u64 *data)
1926{
1927 struct macb *bp;
1928
1929 bp = netdev_priv(dev);
1930 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06001931 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06001932}
1933
1934static int gem_get_sset_count(struct net_device *dev, int sset)
1935{
1936 switch (sset) {
1937 case ETH_SS_STATS:
1938 return GEM_STATS_LEN;
1939 default:
1940 return -EOPNOTSUPP;
1941 }
1942}
1943
1944static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
1945{
1946 int i;
1947
1948 switch (sset) {
1949 case ETH_SS_STATS:
1950 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
1951 memcpy(p, gem_statistics[i].stat_string,
1952 ETH_GSTRING_LEN);
1953 break;
1954 }
1955}
1956
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001957static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001958{
1959 struct macb *bp = netdev_priv(dev);
1960 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001961 struct macb_stats *hwstat = &bp->hw_stats.macb;
1962
1963 if (macb_is_gem(bp))
1964 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001965
frederic RODO6c36a702007-07-12 19:07:24 +02001966 /* read stats from hardware */
1967 macb_update_stats(bp);
1968
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001969 /* Convert HW stats into netdevice stats */
1970 nstat->rx_errors = (hwstat->rx_fcs_errors +
1971 hwstat->rx_align_errors +
1972 hwstat->rx_resource_errors +
1973 hwstat->rx_overruns +
1974 hwstat->rx_oversize_pkts +
1975 hwstat->rx_jabbers +
1976 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001977 hwstat->rx_length_mismatch);
1978 nstat->tx_errors = (hwstat->tx_late_cols +
1979 hwstat->tx_excessive_cols +
1980 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02001981 hwstat->tx_carrier_errors +
1982 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001983 nstat->collisions = (hwstat->tx_single_cols +
1984 hwstat->tx_multiple_cols +
1985 hwstat->tx_excessive_cols);
1986 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1987 hwstat->rx_jabbers +
1988 hwstat->rx_undersize_pkts +
1989 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001990 nstat->rx_over_errors = hwstat->rx_resource_errors +
1991 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1993 nstat->rx_frame_errors = hwstat->rx_align_errors;
1994 nstat->rx_fifo_errors = hwstat->rx_overruns;
1995 /* XXX: What does "missed" mean? */
1996 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1997 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1998 nstat->tx_fifo_errors = hwstat->tx_underruns;
1999 /* Don't know about heartbeat or window errors... */
2000
2001 return nstat;
2002}
2003
2004static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2005{
2006 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002007 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002008
frederic RODO6c36a702007-07-12 19:07:24 +02002009 if (!phydev)
2010 return -ENODEV;
2011
2012 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002013}
2014
2015static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2016{
2017 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002018 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002019
frederic RODO6c36a702007-07-12 19:07:24 +02002020 if (!phydev)
2021 return -ENODEV;
2022
2023 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002024}
2025
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002026static int macb_get_regs_len(struct net_device *netdev)
2027{
2028 return MACB_GREGS_NBR * sizeof(u32);
2029}
2030
2031static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2032 void *p)
2033{
2034 struct macb *bp = netdev_priv(dev);
2035 unsigned int tail, head;
2036 u32 *regs_buff = p;
2037
2038 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2039 | MACB_GREGS_VERSION;
2040
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002041 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2042 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002043
2044 regs_buff[0] = macb_readl(bp, NCR);
2045 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2046 regs_buff[2] = macb_readl(bp, NSR);
2047 regs_buff[3] = macb_readl(bp, TSR);
2048 regs_buff[4] = macb_readl(bp, RBQP);
2049 regs_buff[5] = macb_readl(bp, TBQP);
2050 regs_buff[6] = macb_readl(bp, RSR);
2051 regs_buff[7] = macb_readl(bp, IMR);
2052
2053 regs_buff[8] = tail;
2054 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002055 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2056 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002057
Nicolas Ferre7c399942015-03-31 15:02:04 +02002058 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002059 if (macb_is_gem(bp)) {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002060 regs_buff[13] = gem_readl(bp, DMACFG);
2061 }
2062}
2063
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002064static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002065 .get_settings = macb_get_settings,
2066 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002067 .get_regs_len = macb_get_regs_len,
2068 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002069 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002070 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff8cd5a562015-01-15 15:55:20 -06002071};
Xander Huff8cd5a562015-01-15 15:55:20 -06002072
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002073static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002074 .get_settings = macb_get_settings,
2075 .set_settings = macb_set_settings,
2076 .get_regs_len = macb_get_regs_len,
2077 .get_regs = macb_get_regs,
2078 .get_link = ethtool_op_get_link,
2079 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002080 .get_ethtool_stats = gem_get_ethtool_stats,
2081 .get_strings = gem_get_ethtool_strings,
2082 .get_sset_count = gem_get_sset_count,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002083};
2084
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002085static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002086{
2087 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002088 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002089
2090 if (!netif_running(dev))
2091 return -EINVAL;
2092
frederic RODO6c36a702007-07-12 19:07:24 +02002093 if (!phydev)
2094 return -ENODEV;
2095
Richard Cochran28b04112010-07-17 08:48:55 +00002096 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002097}
2098
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002099static int macb_set_features(struct net_device *netdev,
2100 netdev_features_t features)
2101{
2102 struct macb *bp = netdev_priv(netdev);
2103 netdev_features_t changed = features ^ netdev->features;
2104
2105 /* TX checksum offload */
2106 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2107 u32 dmacfg;
2108
2109 dmacfg = gem_readl(bp, DMACFG);
2110 if (features & NETIF_F_HW_CSUM)
2111 dmacfg |= GEM_BIT(TXCOEN);
2112 else
2113 dmacfg &= ~GEM_BIT(TXCOEN);
2114 gem_writel(bp, DMACFG, dmacfg);
2115 }
2116
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002117 /* RX checksum offload */
2118 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2119 u32 netcfg;
2120
2121 netcfg = gem_readl(bp, NCFGR);
2122 if (features & NETIF_F_RXCSUM &&
2123 !(netdev->flags & IFF_PROMISC))
2124 netcfg |= GEM_BIT(RXCOEN);
2125 else
2126 netcfg &= ~GEM_BIT(RXCOEN);
2127 gem_writel(bp, NCFGR, netcfg);
2128 }
2129
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002130 return 0;
2131}
2132
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002133static const struct net_device_ops macb_netdev_ops = {
2134 .ndo_open = macb_open,
2135 .ndo_stop = macb_close,
2136 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002137 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002138 .ndo_get_stats = macb_get_stats,
2139 .ndo_do_ioctl = macb_ioctl,
2140 .ndo_validate_addr = eth_validate_addr,
2141 .ndo_change_mtu = eth_change_mtu,
2142 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002143#ifdef CONFIG_NET_POLL_CONTROLLER
2144 .ndo_poll_controller = macb_poll_controller,
2145#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002146 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002147};
2148
Nicolas Ferree1755872014-07-24 13:50:58 +02002149/*
Nicolas Ferread783472015-03-31 15:02:02 +02002150 * Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002151 * and integration options used
2152 */
Nicolas Ferref6970502015-03-31 15:02:01 +02002153static void macb_configure_caps(struct macb *bp, const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002154{
2155 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002156
Nicolas Ferref6970502015-03-31 15:02:01 +02002157 if (dt_conf)
2158 bp->caps = dt_conf->caps;
2159
Nicolas Ferrefa693592015-03-31 15:02:06 +02002160 if (macb_is_gem_hw(bp->regs)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002161 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2162
Nicolas Ferree1755872014-07-24 13:50:58 +02002163 dcfg = gem_readl(bp, DCFG1);
2164 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2165 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2166 dcfg = gem_readl(bp, DCFG2);
2167 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2168 bp->caps |= MACB_CAPS_FIFO_MODE;
2169 }
2170
2171 netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
2172}
2173
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002174static void macb_probe_queues(void __iomem *mem,
2175 unsigned int *queue_mask,
2176 unsigned int *num_queues)
2177{
2178 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002179
2180 *queue_mask = 0x1;
2181 *num_queues = 1;
2182
Nicolas Ferreda120112015-03-31 15:02:00 +02002183 /* is it macb or gem ?
2184 *
2185 * We need to read directly from the hardware here because
2186 * we are early in the probe process and don't have the
2187 * MACB_CAPS_MACB_IS_GEM flag positioned
2188 */
Nicolas Ferrefa693592015-03-31 15:02:06 +02002189 if (!macb_is_gem_hw(mem))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002190 return;
2191
2192 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302193 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2194
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002195 *queue_mask |= 0x1;
2196
2197 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2198 if (*queue_mask & (1 << hw_q))
2199 (*num_queues)++;
2200}
2201
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002202static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
2203 struct clk **hclk, struct clk **tx_clk)
2204{
2205 int err;
2206
2207 *pclk = devm_clk_get(&pdev->dev, "pclk");
2208 if (IS_ERR(*pclk)) {
2209 err = PTR_ERR(*pclk);
2210 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2211 return err;
2212 }
2213
2214 *hclk = devm_clk_get(&pdev->dev, "hclk");
2215 if (IS_ERR(*hclk)) {
2216 err = PTR_ERR(*hclk);
2217 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2218 return err;
2219 }
2220
2221 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2222 if (IS_ERR(*tx_clk))
2223 *tx_clk = NULL;
2224
2225 err = clk_prepare_enable(*pclk);
2226 if (err) {
2227 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2228 return err;
2229 }
2230
2231 err = clk_prepare_enable(*hclk);
2232 if (err) {
2233 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2234 goto err_disable_pclk;
2235 }
2236
2237 err = clk_prepare_enable(*tx_clk);
2238 if (err) {
2239 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2240 goto err_disable_hclk;
2241 }
2242
2243 return 0;
2244
2245err_disable_hclk:
2246 clk_disable_unprepare(*hclk);
2247
2248err_disable_pclk:
2249 clk_disable_unprepare(*pclk);
2250
2251 return err;
2252}
2253
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002254static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002255{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002256 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002257 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002258 struct macb *bp = netdev_priv(dev);
2259 struct macb_queue *queue;
2260 int err;
2261 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002262
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002263 /* set the queue register mapping once for all: queue0 has a special
2264 * register mapping but we don't want to test the queue index then
2265 * compute the corresponding register offset at run time.
2266 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002267 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002268 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002269 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002270
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002271 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002272 queue->bp = bp;
2273 if (hw_q) {
2274 queue->ISR = GEM_ISR(hw_q - 1);
2275 queue->IER = GEM_IER(hw_q - 1);
2276 queue->IDR = GEM_IDR(hw_q - 1);
2277 queue->IMR = GEM_IMR(hw_q - 1);
2278 queue->TBQP = GEM_TBQP(hw_q - 1);
2279 } else {
2280 /* queue0 uses legacy registers */
2281 queue->ISR = MACB_ISR;
2282 queue->IER = MACB_IER;
2283 queue->IDR = MACB_IDR;
2284 queue->IMR = MACB_IMR;
2285 queue->TBQP = MACB_TBQP;
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002286 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002287
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002288 /* get irq: here we use the linux queue index, not the hardware
2289 * queue index. the queue irq definitions in the device tree
2290 * must remove the optional gaps that could exist in the
2291 * hardware queue mask.
2292 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002293 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002294 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002295 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002296 if (err) {
2297 dev_err(&pdev->dev,
2298 "Unable to request IRQ %d (error %d)\n",
2299 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002300 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002301 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002302
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002303 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002304 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002305 }
2306
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002307 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002308 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002309
Nicolas Ferre4df95132013-06-04 21:57:12 +00002310 /* setup appropriated routines according to adapter type */
2311 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002312 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002313 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2314 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2315 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2316 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002317 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002318 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002319 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002320 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2321 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2322 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2323 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002324 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002325 }
2326
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002327 /* Set features */
2328 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002329 /* Checksum offload is only available on gem with packet buffer */
2330 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002331 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002332 if (bp->caps & MACB_CAPS_SG_DISABLED)
2333 dev->hw_features &= ~NETIF_F_SG;
2334 dev->features = dev->hw_features;
2335
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002336 val = 0;
2337 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2338 val = GEM_BIT(RGMII);
2339 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
2340 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2341 val = MACB_BIT(RMII);
2342 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2343 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002344
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002345 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2346 val |= MACB_BIT(CLKEN);
2347
2348 macb_or_gem_writel(bp, USRIO, val);
2349
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002350 /* Set MII management clock divider */
2351 val = macb_mdc_clk_div(bp);
2352 val |= macb_dbw(bp);
2353 macb_writel(bp, NCFGR, val);
2354
2355 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002356}
2357
2358#if defined(CONFIG_OF)
2359/* 1518 rounded up */
2360#define AT91ETHER_MAX_RBUFF_SZ 0x600
2361/* max number of receive buffers */
2362#define AT91ETHER_MAX_RX_DESCR 9
2363
2364/* Initialize and start the Receiver and Transmit subsystems */
2365static int at91ether_start(struct net_device *dev)
2366{
2367 struct macb *lp = netdev_priv(dev);
2368 dma_addr_t addr;
2369 u32 ctl;
2370 int i;
2371
2372 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2373 (AT91ETHER_MAX_RX_DESCR *
2374 sizeof(struct macb_dma_desc)),
2375 &lp->rx_ring_dma, GFP_KERNEL);
2376 if (!lp->rx_ring)
2377 return -ENOMEM;
2378
2379 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2380 AT91ETHER_MAX_RX_DESCR *
2381 AT91ETHER_MAX_RBUFF_SZ,
2382 &lp->rx_buffers_dma, GFP_KERNEL);
2383 if (!lp->rx_buffers) {
2384 dma_free_coherent(&lp->pdev->dev,
2385 AT91ETHER_MAX_RX_DESCR *
2386 sizeof(struct macb_dma_desc),
2387 lp->rx_ring, lp->rx_ring_dma);
2388 lp->rx_ring = NULL;
2389 return -ENOMEM;
2390 }
2391
2392 addr = lp->rx_buffers_dma;
2393 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2394 lp->rx_ring[i].addr = addr;
2395 lp->rx_ring[i].ctrl = 0;
2396 addr += AT91ETHER_MAX_RBUFF_SZ;
2397 }
2398
2399 /* Set the Wrap bit on the last descriptor */
2400 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2401
2402 /* Reset buffer index */
2403 lp->rx_tail = 0;
2404
2405 /* Program address of descriptor list in Rx Buffer Queue register */
2406 macb_writel(lp, RBQP, lp->rx_ring_dma);
2407
2408 /* Enable Receive and Transmit */
2409 ctl = macb_readl(lp, NCR);
2410 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2411
2412 return 0;
2413}
2414
2415/* Open the ethernet interface */
2416static int at91ether_open(struct net_device *dev)
2417{
2418 struct macb *lp = netdev_priv(dev);
2419 u32 ctl;
2420 int ret;
2421
2422 /* Clear internal statistics */
2423 ctl = macb_readl(lp, NCR);
2424 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2425
2426 macb_set_hwaddr(lp);
2427
2428 ret = at91ether_start(dev);
2429 if (ret)
2430 return ret;
2431
2432 /* Enable MAC interrupts */
2433 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2434 MACB_BIT(RXUBR) |
2435 MACB_BIT(ISR_TUND) |
2436 MACB_BIT(ISR_RLE) |
2437 MACB_BIT(TCOMP) |
2438 MACB_BIT(ISR_ROVR) |
2439 MACB_BIT(HRESP));
2440
2441 /* schedule a link state check */
2442 phy_start(lp->phy_dev);
2443
2444 netif_start_queue(dev);
2445
2446 return 0;
2447}
2448
2449/* Close the interface */
2450static int at91ether_close(struct net_device *dev)
2451{
2452 struct macb *lp = netdev_priv(dev);
2453 u32 ctl;
2454
2455 /* Disable Receiver and Transmitter */
2456 ctl = macb_readl(lp, NCR);
2457 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2458
2459 /* Disable MAC interrupts */
2460 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2461 MACB_BIT(RXUBR) |
2462 MACB_BIT(ISR_TUND) |
2463 MACB_BIT(ISR_RLE) |
2464 MACB_BIT(TCOMP) |
2465 MACB_BIT(ISR_ROVR) |
2466 MACB_BIT(HRESP));
2467
2468 netif_stop_queue(dev);
2469
2470 dma_free_coherent(&lp->pdev->dev,
2471 AT91ETHER_MAX_RX_DESCR *
2472 sizeof(struct macb_dma_desc),
2473 lp->rx_ring, lp->rx_ring_dma);
2474 lp->rx_ring = NULL;
2475
2476 dma_free_coherent(&lp->pdev->dev,
2477 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2478 lp->rx_buffers, lp->rx_buffers_dma);
2479 lp->rx_buffers = NULL;
2480
2481 return 0;
2482}
2483
2484/* Transmit packet */
2485static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2486{
2487 struct macb *lp = netdev_priv(dev);
2488
2489 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2490 netif_stop_queue(dev);
2491
2492 /* Store packet information (to free when Tx completed) */
2493 lp->skb = skb;
2494 lp->skb_length = skb->len;
2495 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2496 DMA_TO_DEVICE);
2497
2498 /* Set address of the data in the Transmit Address register */
2499 macb_writel(lp, TAR, lp->skb_physaddr);
2500 /* Set length of the packet in the Transmit Control register */
2501 macb_writel(lp, TCR, skb->len);
2502
2503 } else {
2504 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2505 return NETDEV_TX_BUSY;
2506 }
2507
2508 return NETDEV_TX_OK;
2509}
2510
2511/* Extract received frame from buffer descriptors and sent to upper layers.
2512 * (Called from interrupt context)
2513 */
2514static void at91ether_rx(struct net_device *dev)
2515{
2516 struct macb *lp = netdev_priv(dev);
2517 unsigned char *p_recv;
2518 struct sk_buff *skb;
2519 unsigned int pktlen;
2520
2521 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2522 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2523 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2524 skb = netdev_alloc_skb(dev, pktlen + 2);
2525 if (skb) {
2526 skb_reserve(skb, 2);
2527 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2528
2529 skb->protocol = eth_type_trans(skb, dev);
2530 lp->stats.rx_packets++;
2531 lp->stats.rx_bytes += pktlen;
2532 netif_rx(skb);
2533 } else {
2534 lp->stats.rx_dropped++;
2535 }
2536
2537 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2538 lp->stats.multicast++;
2539
2540 /* reset ownership bit */
2541 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2542
2543 /* wrap after last buffer */
2544 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2545 lp->rx_tail = 0;
2546 else
2547 lp->rx_tail++;
2548 }
2549}
2550
2551/* MAC interrupt handler */
2552static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2553{
2554 struct net_device *dev = dev_id;
2555 struct macb *lp = netdev_priv(dev);
2556 u32 intstatus, ctl;
2557
2558 /* MAC Interrupt Status register indicates what interrupts are pending.
2559 * It is automatically cleared once read.
2560 */
2561 intstatus = macb_readl(lp, ISR);
2562
2563 /* Receive complete */
2564 if (intstatus & MACB_BIT(RCOMP))
2565 at91ether_rx(dev);
2566
2567 /* Transmit complete */
2568 if (intstatus & MACB_BIT(TCOMP)) {
2569 /* The TCOM bit is set even if the transmission failed */
2570 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2571 lp->stats.tx_errors++;
2572
2573 if (lp->skb) {
2574 dev_kfree_skb_irq(lp->skb);
2575 lp->skb = NULL;
2576 dma_unmap_single(NULL, lp->skb_physaddr,
2577 lp->skb_length, DMA_TO_DEVICE);
2578 lp->stats.tx_packets++;
2579 lp->stats.tx_bytes += lp->skb_length;
2580 }
2581 netif_wake_queue(dev);
2582 }
2583
2584 /* Work-around for EMAC Errata section 41.3.1 */
2585 if (intstatus & MACB_BIT(RXUBR)) {
2586 ctl = macb_readl(lp, NCR);
2587 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2588 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2589 }
2590
2591 if (intstatus & MACB_BIT(ISR_ROVR))
2592 netdev_err(dev, "ROVR error\n");
2593
2594 return IRQ_HANDLED;
2595}
2596
2597#ifdef CONFIG_NET_POLL_CONTROLLER
2598static void at91ether_poll_controller(struct net_device *dev)
2599{
2600 unsigned long flags;
2601
2602 local_irq_save(flags);
2603 at91ether_interrupt(dev->irq, dev);
2604 local_irq_restore(flags);
2605}
2606#endif
2607
2608static const struct net_device_ops at91ether_netdev_ops = {
2609 .ndo_open = at91ether_open,
2610 .ndo_stop = at91ether_close,
2611 .ndo_start_xmit = at91ether_start_xmit,
2612 .ndo_get_stats = macb_get_stats,
2613 .ndo_set_rx_mode = macb_set_rx_mode,
2614 .ndo_set_mac_address = eth_mac_addr,
2615 .ndo_do_ioctl = macb_ioctl,
2616 .ndo_validate_addr = eth_validate_addr,
2617 .ndo_change_mtu = eth_change_mtu,
2618#ifdef CONFIG_NET_POLL_CONTROLLER
2619 .ndo_poll_controller = at91ether_poll_controller,
2620#endif
2621};
2622
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002623static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
2624 struct clk **hclk, struct clk **tx_clk)
2625{
2626 int err;
2627
2628 *hclk = NULL;
2629 *tx_clk = NULL;
2630
2631 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2632 if (IS_ERR(*pclk))
2633 return PTR_ERR(*pclk);
2634
2635 err = clk_prepare_enable(*pclk);
2636 if (err) {
2637 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2638 return err;
2639 }
2640
2641 return 0;
2642}
2643
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002644static int at91ether_init(struct platform_device *pdev)
2645{
2646 struct net_device *dev = platform_get_drvdata(pdev);
2647 struct macb *bp = netdev_priv(dev);
2648 int err;
2649 u32 reg;
2650
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002651 dev->netdev_ops = &at91ether_netdev_ops;
2652 dev->ethtool_ops = &macb_ethtool_ops;
2653
2654 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2655 0, dev->name, dev);
2656 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002657 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002658
2659 macb_writel(bp, NCR, 0);
2660
2661 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2662 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2663 reg |= MACB_BIT(RM9200_RMII);
2664
2665 macb_writel(bp, NCFGR, reg);
2666
2667 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002668}
2669
David S. Miller3cef5c52015-03-09 23:38:02 -04002670static const struct macb_config at91sam9260_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002671 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002672 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002673 .init = macb_init,
2674};
2675
David S. Miller3cef5c52015-03-09 23:38:02 -04002676static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002677 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2678 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002679 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002680 .init = macb_init,
2681};
2682
David S. Miller3cef5c52015-03-09 23:38:02 -04002683static const struct macb_config sama5d3_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002684 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2685 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002686 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002687 .init = macb_init,
2688};
2689
David S. Miller3cef5c52015-03-09 23:38:02 -04002690static const struct macb_config sama5d4_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002691 .caps = 0,
2692 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002693 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002694 .init = macb_init,
2695};
2696
David S. Miller3cef5c52015-03-09 23:38:02 -04002697static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002698 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002699 .init = at91ether_init,
2700};
2701
2702static const struct of_device_id macb_dt_ids[] = {
2703 { .compatible = "cdns,at32ap7000-macb" },
2704 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2705 { .compatible = "cdns,macb" },
2706 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2707 { .compatible = "cdns,gem", .data = &pc302gem_config },
2708 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2709 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2710 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2711 { .compatible = "cdns,emac", .data = &emac_config },
2712 { /* sentinel */ }
2713};
2714MODULE_DEVICE_TABLE(of, macb_dt_ids);
2715#endif /* CONFIG_OF */
2716
2717static int macb_probe(struct platform_device *pdev)
2718{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002719 int (*clk_init)(struct platform_device *, struct clk **,
2720 struct clk **, struct clk **)
2721 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002722 int (*init)(struct platform_device *) = macb_init;
2723 struct device_node *np = pdev->dev.of_node;
2724 const struct macb_config *macb_config = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002725 struct clk *pclk, *hclk, *tx_clk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002726 unsigned int queue_mask, num_queues;
2727 struct macb_platform_data *pdata;
2728 struct phy_device *phydev;
2729 struct net_device *dev;
2730 struct resource *regs;
2731 void __iomem *mem;
2732 const char *mac;
2733 struct macb *bp;
2734 int err;
2735
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002736 if (np) {
2737 const struct of_device_id *match;
2738
2739 match = of_match_node(macb_dt_ids, np);
2740 if (match && match->data) {
2741 macb_config = match->data;
2742 clk_init = macb_config->clk_init;
2743 init = macb_config->init;
2744 }
2745 }
2746
2747 err = clk_init(pdev, &pclk, &hclk, &tx_clk);
2748 if (err)
2749 return err;
2750
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002751 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2752 mem = devm_ioremap_resource(&pdev->dev, regs);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002753 if (IS_ERR(mem)) {
2754 err = PTR_ERR(mem);
2755 goto err_disable_clocks;
2756 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002757
2758 macb_probe_queues(mem, &queue_mask, &num_queues);
2759 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002760 if (!dev) {
2761 err = -ENOMEM;
2762 goto err_disable_clocks;
2763 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002764
2765 dev->base_addr = regs->start;
2766
2767 SET_NETDEV_DEV(dev, &pdev->dev);
2768
2769 bp = netdev_priv(dev);
2770 bp->pdev = pdev;
2771 bp->dev = dev;
2772 bp->regs = mem;
2773 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002774 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002775 if (macb_config)
2776 bp->dma_burst_length = macb_config->dma_burst_length;
2777 bp->pclk = pclk;
2778 bp->hclk = hclk;
2779 bp->tx_clk = tx_clk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002780 spin_lock_init(&bp->lock);
2781
Nicolas Ferread783472015-03-31 15:02:02 +02002782 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02002783 macb_configure_caps(bp, macb_config);
2784
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002785 platform_set_drvdata(pdev, dev);
2786
2787 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002788 if (dev->irq < 0) {
2789 err = dev->irq;
2790 goto err_disable_clocks;
2791 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002792
2793 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00002794 if (mac)
2795 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2796 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002797 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002798
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002799 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002800 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002801 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002802 if (pdata && pdata->is_rmii)
2803 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2804 else
2805 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2806 } else {
2807 bp->phy_interface = err;
2808 }
2809
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002810 /* IP specific init */
2811 err = init(pdev);
2812 if (err)
2813 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002814
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002815 err = register_netdev(dev);
2816 if (err) {
2817 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002818 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002819 }
2820
Nicolas Ferre72ca8202013-04-14 22:04:33 +00002821 err = macb_mii_init(bp);
2822 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02002823 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002824
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002825 netif_carrier_off(dev);
2826
Bo Shen58798232014-09-13 01:57:49 +02002827 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
2828 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
2829 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002830
frederic RODO6c36a702007-07-12 19:07:24 +02002831 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002832 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2833 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002834
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002835 return 0;
2836
frederic RODO6c36a702007-07-12 19:07:24 +02002837err_out_unregister_netdev:
2838 unregister_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002839
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002840err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002841 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002842
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002843err_disable_clocks:
2844 clk_disable_unprepare(tx_clk);
2845 clk_disable_unprepare(hclk);
2846 clk_disable_unprepare(pclk);
2847
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002848 return err;
2849}
2850
Nicolae Rosia9e86d762015-01-22 17:31:05 +00002851static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002852{
2853 struct net_device *dev;
2854 struct macb *bp;
2855
2856 dev = platform_get_drvdata(pdev);
2857
2858 if (dev) {
2859 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002860 if (bp->phy_dev)
2861 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002862 mdiobus_unregister(bp->mii_bus);
2863 kfree(bp->mii_bus->irq);
2864 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002865 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002866 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002867 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002868 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01002869 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002870 }
2871
2872 return 0;
2873}
2874
Michal Simekd23823d2015-01-23 09:36:03 +01002875static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002876{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002877 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002878 struct net_device *netdev = platform_get_drvdata(pdev);
2879 struct macb *bp = netdev_priv(netdev);
2880
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002881 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002882 netif_device_detach(netdev);
2883
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002884 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002885 clk_disable_unprepare(bp->hclk);
2886 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002887
2888 return 0;
2889}
2890
Michal Simekd23823d2015-01-23 09:36:03 +01002891static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002892{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002893 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002894 struct net_device *netdev = platform_get_drvdata(pdev);
2895 struct macb *bp = netdev_priv(netdev);
2896
Steffen Trumtrarace58012013-03-27 23:07:07 +00002897 clk_prepare_enable(bp->pclk);
2898 clk_prepare_enable(bp->hclk);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002899 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002900
2901 netif_device_attach(netdev);
2902
2903 return 0;
2904}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002905
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002906static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
2907
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002908static struct platform_driver macb_driver = {
Nicolae Rosia9e86d762015-01-22 17:31:05 +00002909 .probe = macb_probe,
2910 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002911 .driver = {
2912 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002913 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002914 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002915 },
2916};
2917
Nicolae Rosia9e86d762015-01-22 17:31:05 +00002918module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002919
2920MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00002921MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002922MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07002923MODULE_ALIAS("platform:macb");