blob: 97c664611e844706914e1193047e4465b7fa09be [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
Harini Katakama5898ea2015-05-06 22:27:18 +053057#define GEM_MTU_MIN_SIZE 68
58
Nicolas Ferree86cd532012-10-31 06:04:57 +000059/*
60 * Graceful stop timeouts in us. We should allow up to
61 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
62 */
63#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010064
Havard Skinnemoen55054a12012-10-31 06:04:55 +000065/* Ring buffer accessors */
66static unsigned int macb_tx_ring_wrap(unsigned int index)
67{
68 return index & (TX_RING_SIZE - 1);
69}
70
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010071static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
72 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000073{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010074 return &queue->tx_ring[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000075}
76
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010077static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
78 unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000079{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010080 return &queue->tx_skb[macb_tx_ring_wrap(index)];
Havard Skinnemoen55054a12012-10-31 06:04:55 +000081}
82
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010083static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
Havard Skinnemoen55054a12012-10-31 06:04:55 +000084{
85 dma_addr_t offset;
86
87 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
88
Cyrille Pitchen02c958d2014-12-12 13:26:44 +010089 return queue->tx_ring_dma + offset;
Havard Skinnemoen55054a12012-10-31 06:04:55 +000090}
91
92static unsigned int macb_rx_ring_wrap(unsigned int index)
93{
94 return index & (RX_RING_SIZE - 1);
95}
96
97static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
98{
99 return &bp->rx_ring[macb_rx_ring_wrap(index)];
100}
101
102static void *macb_rx_buffer(struct macb *bp, unsigned int index)
103{
Nicolas Ferre1b447912013-06-04 21:57:11 +0000104 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000105}
106
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100107static void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100108{
109 u32 bottom;
110 u16 top;
111
112 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000113 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100114 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000115 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000116
117 /* Clear unused address register sets */
118 macb_or_gem_writel(bp, SA2B, 0);
119 macb_or_gem_writel(bp, SA2T, 0);
120 macb_or_gem_writel(bp, SA3B, 0);
121 macb_or_gem_writel(bp, SA3T, 0);
122 macb_or_gem_writel(bp, SA4B, 0);
123 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100124}
125
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100126static void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100127{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000128 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100129 u32 bottom;
130 u16 top;
131 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000132 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100133
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900134 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000135
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000136 /* Check all 4 address register for vaild address */
137 for (i = 0; i < 4; i++) {
138 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
139 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100140
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000141 if (pdata && pdata->rev_eth_addr) {
142 addr[5] = bottom & 0xff;
143 addr[4] = (bottom >> 8) & 0xff;
144 addr[3] = (bottom >> 16) & 0xff;
145 addr[2] = (bottom >> 24) & 0xff;
146 addr[1] = top & 0xff;
147 addr[0] = (top & 0xff00) >> 8;
148 } else {
149 addr[0] = bottom & 0xff;
150 addr[1] = (bottom >> 8) & 0xff;
151 addr[2] = (bottom >> 16) & 0xff;
152 addr[3] = (bottom >> 24) & 0xff;
153 addr[4] = top & 0xff;
154 addr[5] = (top >> 8) & 0xff;
155 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100156
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000157 if (is_valid_ether_addr(addr)) {
158 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
159 return;
160 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700161 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000162
163 netdev_info(bp->dev, "invalid hw address, using random\n");
164 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100165}
166
frederic RODO6c36a702007-07-12 19:07:24 +0200167static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100168{
frederic RODO6c36a702007-07-12 19:07:24 +0200169 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100170 int value;
171
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100172 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
173 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200174 | MACB_BF(PHYA, mii_id)
175 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100176 | MACB_BF(CODE, MACB_MAN_CODE)));
177
frederic RODO6c36a702007-07-12 19:07:24 +0200178 /* wait for end of transfer */
179 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
180 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100181
182 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100183
184 return value;
185}
186
frederic RODO6c36a702007-07-12 19:07:24 +0200187static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
188 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100189{
frederic RODO6c36a702007-07-12 19:07:24 +0200190 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100191
192 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
193 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200194 | MACB_BF(PHYA, mii_id)
195 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100196 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200197 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100198
frederic RODO6c36a702007-07-12 19:07:24 +0200199 /* wait for end of transfer */
200 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
201 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100202
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100203 return 0;
204}
205
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800206/**
207 * macb_set_tx_clk() - Set a clock to a new frequency
208 * @clk Pointer to the clock to change
209 * @rate New frequency in Hz
210 * @dev Pointer to the struct net_device
211 */
212static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
213{
214 long ferr, rate, rate_rounded;
215
Cyrille Pitchen93b31f42015-03-07 07:23:31 +0100216 if (!clk)
217 return;
218
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800219 switch (speed) {
220 case SPEED_10:
221 rate = 2500000;
222 break;
223 case SPEED_100:
224 rate = 25000000;
225 break;
226 case SPEED_1000:
227 rate = 125000000;
228 break;
229 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800230 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800231 }
232
233 rate_rounded = clk_round_rate(clk, rate);
234 if (rate_rounded < 0)
235 return;
236
237 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
238 * is not satisfied.
239 */
240 ferr = abs(rate_rounded - rate);
241 ferr = DIV_ROUND_UP(ferr, rate / 100000);
242 if (ferr > 5)
243 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
244 rate);
245
246 if (clk_set_rate(clk, rate_rounded))
247 netdev_err(dev, "adjusting tx_clk failed.\n");
248}
249
frederic RODO6c36a702007-07-12 19:07:24 +0200250static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100251{
frederic RODO6c36a702007-07-12 19:07:24 +0200252 struct macb *bp = netdev_priv(dev);
253 struct phy_device *phydev = bp->phy_dev;
254 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100255
frederic RODO6c36a702007-07-12 19:07:24 +0200256 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100257
frederic RODO6c36a702007-07-12 19:07:24 +0200258 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100259
frederic RODO6c36a702007-07-12 19:07:24 +0200260 if (phydev->link) {
261 if ((bp->speed != phydev->speed) ||
262 (bp->duplex != phydev->duplex)) {
263 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100264
frederic RODO6c36a702007-07-12 19:07:24 +0200265 reg = macb_readl(bp, NCFGR);
266 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000267 if (macb_is_gem(bp))
268 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200269
270 if (phydev->duplex)
271 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900272 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200273 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200274 if (phydev->speed == SPEED_1000 &&
275 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000276 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200277
Patrice Vilchez140b7552012-10-31 06:04:50 +0000278 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200279
280 bp->speed = phydev->speed;
281 bp->duplex = phydev->duplex;
282 status_change = 1;
283 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100284 }
285
frederic RODO6c36a702007-07-12 19:07:24 +0200286 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700287 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200288 bp->speed = 0;
289 bp->duplex = -1;
290 }
291 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100292
frederic RODO6c36a702007-07-12 19:07:24 +0200293 status_change = 1;
294 }
295
296 spin_unlock_irqrestore(&bp->lock, flags);
297
298 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000299 if (phydev->link) {
Jaeden Amero2c29b232015-03-12 18:07:54 -0500300 /* Update the TX clock rate if and only if the link is
301 * up and there has been a link change.
302 */
303 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
304
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000305 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000306 netdev_info(dev, "link up (%d/%s)\n",
307 phydev->speed,
308 phydev->duplex == DUPLEX_FULL ?
309 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000310 } else {
311 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000312 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000313 }
frederic RODO6c36a702007-07-12 19:07:24 +0200314 }
315}
316
317/* based on au1000_eth. c*/
318static int macb_mii_probe(struct net_device *dev)
319{
320 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000321 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000322 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000323 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000324 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200325
Jiri Pirko7455a762010-02-08 05:12:08 +0000326 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200327 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000328 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200329 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200330 }
331
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000332 pdata = dev_get_platdata(&bp->pdev->dev);
333 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
334 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
335 if (!ret) {
336 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
337 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
338 }
339 }
frederic RODO6c36a702007-07-12 19:07:24 +0200340
341 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000342 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100343 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000344 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000345 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000346 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200347 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100348
frederic RODO6c36a702007-07-12 19:07:24 +0200349 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200350 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000351 phydev->supported &= PHY_GBIT_FEATURES;
352 else
353 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100354
frederic RODO6c36a702007-07-12 19:07:24 +0200355 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100356
frederic RODO6c36a702007-07-12 19:07:24 +0200357 bp->link = 0;
358 bp->speed = 0;
359 bp->duplex = -1;
360 bp->phy_dev = phydev;
361
362 return 0;
363}
364
Cyrille Pitchen421d9df2015-03-07 07:23:32 +0100365static int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200366{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000367 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200368 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200369 int err = -ENXIO, i;
370
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200371 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200372 macb_writel(bp, NCR, MACB_BIT(MPE));
373
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700374 bp->mii_bus = mdiobus_alloc();
375 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200376 err = -ENOMEM;
377 goto err_out;
378 }
379
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700380 bp->mii_bus->name = "MACB_mii_bus";
381 bp->mii_bus->read = &macb_mdio_read;
382 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000383 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
384 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700385 bp->mii_bus->priv = bp;
386 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900387 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700388
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700389 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
390 if (!bp->mii_bus->irq) {
391 err = -ENOMEM;
392 goto err_out_free_mdiobus;
393 }
394
Jamie Iles91523942011-02-28 04:05:25 +0000395 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200396
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200397 np = bp->pdev->dev.of_node;
398 if (np) {
399 /* try dt phy registration */
400 err = of_mdiobus_register(bp->mii_bus, np);
401
402 /* fallback to standard phy registration if no phy were
403 found during dt phy registration */
404 if (!err && !phy_find_first(bp->mii_bus)) {
405 for (i = 0; i < PHY_MAX_ADDR; i++) {
406 struct phy_device *phydev;
407
408 phydev = mdiobus_scan(bp->mii_bus, i);
409 if (IS_ERR(phydev)) {
410 err = PTR_ERR(phydev);
411 break;
412 }
413 }
414
415 if (err)
416 goto err_out_unregister_bus;
417 }
418 } else {
419 for (i = 0; i < PHY_MAX_ADDR; i++)
420 bp->mii_bus->irq[i] = PHY_POLL;
421
422 if (pdata)
423 bp->mii_bus->phy_mask = pdata->phy_mask;
424
425 err = mdiobus_register(bp->mii_bus);
426 }
427
428 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200429 goto err_out_free_mdio_irq;
430
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200431 err = macb_mii_probe(bp->dev);
432 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200433 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200434
435 return 0;
436
437err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700438 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200439err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700440 kfree(bp->mii_bus->irq);
441err_out_free_mdiobus:
442 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200443err_out:
444 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100445}
446
447static void macb_update_stats(struct macb *bp)
448{
449 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000450 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
451 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100452
453 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
454
455 for(; p < end; p++, reg++)
Arun Chandrana50dad32015-02-18 16:59:35 +0530456 *p += readl_relaxed(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100457}
458
Nicolas Ferree86cd532012-10-31 06:04:57 +0000459static int macb_halt_tx(struct macb *bp)
460{
461 unsigned long halt_time, timeout;
462 u32 status;
463
464 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
465
466 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
467 do {
468 halt_time = jiffies;
469 status = macb_readl(bp, TSR);
470 if (!(status & MACB_BIT(TGO)))
471 return 0;
472
473 usleep_range(10, 250);
474 } while (time_before(halt_time, timeout));
475
476 return -ETIMEDOUT;
477}
478
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200479static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
480{
481 if (tx_skb->mapping) {
482 if (tx_skb->mapped_as_page)
483 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
484 tx_skb->size, DMA_TO_DEVICE);
485 else
486 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
487 tx_skb->size, DMA_TO_DEVICE);
488 tx_skb->mapping = 0;
489 }
490
491 if (tx_skb->skb) {
492 dev_kfree_skb_any(tx_skb->skb);
493 tx_skb->skb = NULL;
494 }
495}
496
Nicolas Ferree86cd532012-10-31 06:04:57 +0000497static void macb_tx_error_task(struct work_struct *work)
498{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100499 struct macb_queue *queue = container_of(work, struct macb_queue,
500 tx_error_task);
501 struct macb *bp = queue->bp;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000502 struct macb_tx_skb *tx_skb;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100503 struct macb_dma_desc *desc;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000504 struct sk_buff *skb;
505 unsigned int tail;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100506 unsigned long flags;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000507
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100508 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
509 (unsigned int)(queue - bp->queues),
510 queue->tx_tail, queue->tx_head);
511
512 /* Prevent the queue IRQ handlers from running: each of them may call
513 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
514 * As explained below, we have to halt the transmission before updating
515 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
516 * network engine about the macb/gem being halted.
517 */
518 spin_lock_irqsave(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000519
520 /* Make sure nobody is trying to queue up new packets */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100521 netif_tx_stop_all_queues(bp->dev);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000522
523 /*
524 * Stop transmission now
525 * (in case we have just queued new packets)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100526 * macb/gem must be halted to write TBQP register
Nicolas Ferree86cd532012-10-31 06:04:57 +0000527 */
528 if (macb_halt_tx(bp))
529 /* Just complain for now, reinitializing TX path can be good */
530 netdev_err(bp->dev, "BUG: halt tx timed out\n");
531
Nicolas Ferree86cd532012-10-31 06:04:57 +0000532 /*
533 * Treat frames in TX queue including the ones that caused the error.
534 * Free transmit buffers in upper layer.
535 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100536 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
537 u32 ctrl;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000538
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100539 desc = macb_tx_desc(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000540 ctrl = desc->ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100541 tx_skb = macb_tx_skb(queue, tail);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000542 skb = tx_skb->skb;
543
544 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200545 /* skb is set for the last buffer of the frame */
546 while (!skb) {
547 macb_tx_unmap(bp, tx_skb);
548 tail++;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100549 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200550 skb = tx_skb->skb;
551 }
552
553 /* ctrl still refers to the first buffer descriptor
554 * since it's the only one written back by the hardware
555 */
556 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
557 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
558 macb_tx_ring_wrap(tail), skb->data);
559 bp->stats.tx_packets++;
560 bp->stats.tx_bytes += skb->len;
561 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000562 } else {
563 /*
564 * "Buffers exhausted mid-frame" errors may only happen
565 * if the driver is buggy, so complain loudly about those.
566 * Statistics are updated by hardware.
567 */
568 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
569 netdev_err(bp->dev,
570 "BUG: TX buffers exhausted mid-frame\n");
571
572 desc->ctrl = ctrl | MACB_BIT(TX_USED);
573 }
574
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200575 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000576 }
577
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100578 /* Set end of TX queue */
579 desc = macb_tx_desc(queue, 0);
580 desc->addr = 0;
581 desc->ctrl = MACB_BIT(TX_USED);
582
Nicolas Ferree86cd532012-10-31 06:04:57 +0000583 /* Make descriptor updates visible to hardware */
584 wmb();
585
586 /* Reinitialize the TX desc queue */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100587 queue_writel(queue, TBQP, queue->tx_ring_dma);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000588 /* Make TX ring reflect state of hardware */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100589 queue->tx_head = 0;
590 queue->tx_tail = 0;
Nicolas Ferree86cd532012-10-31 06:04:57 +0000591
592 /* Housework before enabling TX IRQ */
593 macb_writel(bp, TSR, macb_readl(bp, TSR));
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100594 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
595
596 /* Now we are ready to start transmission again */
597 netif_tx_start_all_queues(bp->dev);
598 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
599
600 spin_unlock_irqrestore(&bp->lock, flags);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000601}
602
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100603static void macb_tx_interrupt(struct macb_queue *queue)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100604{
605 unsigned int tail;
606 unsigned int head;
607 u32 status;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100608 struct macb *bp = queue->bp;
609 u16 queue_index = queue - bp->queues;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100610
611 status = macb_readl(bp, TSR);
612 macb_writel(bp, TSR, status);
613
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000614 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100615 queue_writel(queue, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000616
Nicolas Ferree86cd532012-10-31 06:04:57 +0000617 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
618 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100619
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100620 head = queue->tx_head;
621 for (tail = queue->tx_tail; tail != head; tail++) {
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000622 struct macb_tx_skb *tx_skb;
623 struct sk_buff *skb;
624 struct macb_dma_desc *desc;
625 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100626
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100627 desc = macb_tx_desc(queue, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100628
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000629 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100630 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000631
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000632 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100633
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200634 /* TX_USED bit is only set by hardware on the very first buffer
635 * descriptor of the transmitted frame.
636 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000637 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100638 break;
639
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200640 /* Process all buffers of the current transmitted frame */
641 for (;; tail++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100642 tx_skb = macb_tx_skb(queue, tail);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200643 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000644
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200645 /* First, update TX stats if needed */
646 if (skb) {
647 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
648 macb_tx_ring_wrap(tail), skb->data);
649 bp->stats.tx_packets++;
650 bp->stats.tx_bytes += skb->len;
651 }
652
653 /* Now we can safely release resources */
654 macb_tx_unmap(bp, tx_skb);
655
656 /* skb is set only for the last buffer of the frame.
657 * WARNING: at this point skb has been freed by
658 * macb_tx_unmap().
659 */
660 if (skb)
661 break;
662 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100663 }
664
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100665 queue->tx_tail = tail;
666 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
667 CIRC_CNT(queue->tx_head, queue->tx_tail,
668 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
669 netif_wake_subqueue(bp->dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100670}
671
Nicolas Ferre4df95132013-06-04 21:57:12 +0000672static void gem_rx_refill(struct macb *bp)
673{
674 unsigned int entry;
675 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000676 dma_addr_t paddr;
677
678 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000679 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000680
681 /* Make hw descriptor updates visible to CPU */
682 rmb();
683
Nicolas Ferre4df95132013-06-04 21:57:12 +0000684 bp->rx_prepared_head++;
685
Nicolas Ferre4df95132013-06-04 21:57:12 +0000686 if (bp->rx_skbuff[entry] == NULL) {
687 /* allocate sk_buff for this free entry in ring */
688 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
689 if (unlikely(skb == NULL)) {
690 netdev_err(bp->dev,
691 "Unable to allocate sk_buff\n");
692 break;
693 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000694
695 /* now fill corresponding descriptor entry */
696 paddr = dma_map_single(&bp->pdev->dev, skb->data,
697 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800698 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
699 dev_kfree_skb(skb);
700 break;
701 }
702
703 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000704
705 if (entry == RX_RING_SIZE - 1)
706 paddr |= MACB_BIT(RX_WRAP);
707 bp->rx_ring[entry].addr = paddr;
708 bp->rx_ring[entry].ctrl = 0;
709
710 /* properly align Ethernet header */
711 skb_reserve(skb, NET_IP_ALIGN);
Punnaiah Choudary Kallurid4c216c2015-04-29 08:34:46 +0530712 } else {
713 bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
714 bp->rx_ring[entry].ctrl = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000715 }
716 }
717
718 /* Make descriptor updates visible to hardware */
719 wmb();
720
721 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
722 bp->rx_prepared_head, bp->rx_tail);
723}
724
725/* Mark DMA descriptors from begin up to and not including end as unused */
726static void discard_partial_frame(struct macb *bp, unsigned int begin,
727 unsigned int end)
728{
729 unsigned int frag;
730
731 for (frag = begin; frag != end; frag++) {
732 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
733 desc->addr &= ~MACB_BIT(RX_USED);
734 }
735
736 /* Make descriptor updates visible to hardware */
737 wmb();
738
739 /*
740 * When this happens, the hardware stats registers for
741 * whatever caused this is updated, so we don't have to record
742 * anything.
743 */
744}
745
746static int gem_rx(struct macb *bp, int budget)
747{
748 unsigned int len;
749 unsigned int entry;
750 struct sk_buff *skb;
751 struct macb_dma_desc *desc;
752 int count = 0;
753
754 while (count < budget) {
755 u32 addr, ctrl;
756
757 entry = macb_rx_ring_wrap(bp->rx_tail);
758 desc = &bp->rx_ring[entry];
759
760 /* Make hw descriptor updates visible to CPU */
761 rmb();
762
763 addr = desc->addr;
764 ctrl = desc->ctrl;
765
766 if (!(addr & MACB_BIT(RX_USED)))
767 break;
768
Nicolas Ferre4df95132013-06-04 21:57:12 +0000769 bp->rx_tail++;
770 count++;
771
772 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
773 netdev_err(bp->dev,
774 "not whole frame pointed by descriptor\n");
775 bp->stats.rx_dropped++;
776 break;
777 }
778 skb = bp->rx_skbuff[entry];
779 if (unlikely(!skb)) {
780 netdev_err(bp->dev,
781 "inconsistent Rx descriptor chain\n");
782 bp->stats.rx_dropped++;
783 break;
784 }
785 /* now everything is ready for receiving packet */
786 bp->rx_skbuff[entry] = NULL;
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530787 len = ctrl & bp->rx_frm_len_mask;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000788
789 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
790
791 skb_put(skb, len);
792 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
793 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800794 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000795
796 skb->protocol = eth_type_trans(skb, bp->dev);
797 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200798 if (bp->dev->features & NETIF_F_RXCSUM &&
799 !(bp->dev->flags & IFF_PROMISC) &&
800 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
801 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000802
803 bp->stats.rx_packets++;
804 bp->stats.rx_bytes += skb->len;
805
806#if defined(DEBUG) && defined(VERBOSE_DEBUG)
807 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
808 skb->len, skb->csum);
809 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
Cyrille Pitchen51f83012014-12-11 11:15:54 +0100810 skb_mac_header(skb), 16, true);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000811 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
812 skb->data, 32, true);
813#endif
814
815 netif_receive_skb(skb);
816 }
817
818 gem_rx_refill(bp);
819
820 return count;
821}
822
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100823static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
824 unsigned int last_frag)
825{
826 unsigned int len;
827 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000828 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100829 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000830 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100831
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000832 desc = macb_rx_desc(bp, last_frag);
Harini Katakam98b5a0f42015-05-06 22:27:17 +0530833 len = desc->ctrl & bp->rx_frm_len_mask;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100834
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000835 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000836 macb_rx_ring_wrap(first_frag),
837 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100838
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000839 /*
840 * The ethernet header starts NET_IP_ALIGN bytes into the
841 * first buffer. Since the header is 14 bytes, this makes the
842 * payload word-aligned.
843 *
844 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
845 * the two padding bytes into the skb so that we avoid hitting
846 * the slowpath in memcpy(), and pull them off afterwards.
847 */
848 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100849 if (!skb) {
850 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000851 for (frag = first_frag; ; frag++) {
852 desc = macb_rx_desc(bp, frag);
853 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100854 if (frag == last_frag)
855 break;
856 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000857
858 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100859 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000860
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100861 return 1;
862 }
863
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000864 offset = 0;
865 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700866 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100867 skb_put(skb, len);
868
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000869 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000870 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100871
872 if (offset + frag_len > len) {
873 BUG_ON(frag != last_frag);
874 frag_len = len - offset;
875 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300876 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000877 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000878 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000879 desc = macb_rx_desc(bp, frag);
880 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100881
882 if (frag == last_frag)
883 break;
884 }
885
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000886 /* Make descriptor updates visible to hardware */
887 wmb();
888
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000889 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100890 skb->protocol = eth_type_trans(skb, bp->dev);
891
892 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000893 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000894 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000895 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100896 netif_receive_skb(skb);
897
898 return 0;
899}
900
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100901static int macb_rx(struct macb *bp, int budget)
902{
903 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000904 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100905 int first_frag = -1;
906
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000907 for (tail = bp->rx_tail; budget > 0; tail++) {
908 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100909 u32 addr, ctrl;
910
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000911 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100912 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000913
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000914 addr = desc->addr;
915 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100916
917 if (!(addr & MACB_BIT(RX_USED)))
918 break;
919
920 if (ctrl & MACB_BIT(RX_SOF)) {
921 if (first_frag != -1)
922 discard_partial_frame(bp, first_frag, tail);
923 first_frag = tail;
924 }
925
926 if (ctrl & MACB_BIT(RX_EOF)) {
927 int dropped;
928 BUG_ON(first_frag == -1);
929
930 dropped = macb_rx_frame(bp, first_frag, tail);
931 first_frag = -1;
932 if (!dropped) {
933 received++;
934 budget--;
935 }
936 }
937 }
938
939 if (first_frag != -1)
940 bp->rx_tail = first_frag;
941 else
942 bp->rx_tail = tail;
943
944 return received;
945}
946
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700947static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100948{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700949 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700950 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100951 u32 status;
952
953 status = macb_readl(bp, RSR);
954 macb_writel(bp, RSR, status);
955
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700956 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100957
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000958 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000959 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100960
Nicolas Ferre4df95132013-06-04 21:57:12 +0000961 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000962 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800963 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100964
Nicolas Ferre8770e912013-02-12 11:08:48 +0100965 /* Packets received while interrupts were disabled */
966 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -0700967 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700968 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
969 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +0100970 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700971 } else {
972 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
973 }
Joshua Hokeb3363692010-10-25 01:44:22 +0000974 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100975
976 /* TODO: Handle errors */
977
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700978 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100979}
980
981static irqreturn_t macb_interrupt(int irq, void *dev_id)
982{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100983 struct macb_queue *queue = dev_id;
984 struct macb *bp = queue->bp;
985 struct net_device *dev = bp->dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100986 u32 status;
987
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100988 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100989
990 if (unlikely(!status))
991 return IRQ_NONE;
992
993 spin_lock(&bp->lock);
994
995 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100996 /* close possible race with dev_close */
997 if (unlikely(!netif_running(dev))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +0100998 queue_writel(queue, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100999 break;
1000 }
1001
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001002 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1003 (unsigned int)(queue - bp->queues),
1004 (unsigned long)status);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001005
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001006 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +00001007 /*
1008 * There's no point taking any more interrupts
1009 * until we have processed the buffers. The
1010 * scheduling call may fail if the poll routine
1011 * is already scheduled, so disable interrupts
1012 * now.
1013 */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001014 queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001015 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001016 queue_writel(queue, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +00001017
Ben Hutchings288379f2009-01-19 16:43:59 -08001018 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001019 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -08001020 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001021 }
1022 }
1023
Nicolas Ferree86cd532012-10-31 06:04:57 +00001024 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001025 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1026 schedule_work(&queue->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001027
1028 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001029 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001030
Nicolas Ferree86cd532012-10-31 06:04:57 +00001031 break;
1032 }
1033
1034 if (status & MACB_BIT(TCOMP))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001035 macb_tx_interrupt(queue);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001036
1037 /*
1038 * Link change detection isn't possible with RMII, so we'll
1039 * add that if/when we get our hands on a full-blown MII PHY.
1040 */
1041
Alexander Steinb19f7f72011-04-13 05:03:24 +00001042 if (status & MACB_BIT(ISR_ROVR)) {
1043 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001044 if (macb_is_gem(bp))
1045 bp->hw_stats.gem.rx_overruns++;
1046 else
1047 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001048
1049 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001050 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001051 }
1052
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001053 if (status & MACB_BIT(HRESP)) {
1054 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001055 * TODO: Reset the hardware, and maybe move the
1056 * netdev_err to a lower-priority context as well
1057 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001058 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001059 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001060
1061 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001062 queue_writel(queue, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001063 }
1064
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001065 status = queue_readl(queue, ISR);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001066 }
1067
1068 spin_unlock(&bp->lock);
1069
1070 return IRQ_HANDLED;
1071}
1072
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001073#ifdef CONFIG_NET_POLL_CONTROLLER
1074/*
1075 * Polling receive - used by netconsole and other diagnostic tools
1076 * to allow network i/o with interrupts disabled.
1077 */
1078static void macb_poll_controller(struct net_device *dev)
1079{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001080 struct macb *bp = netdev_priv(dev);
1081 struct macb_queue *queue;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001082 unsigned long flags;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001083 unsigned int q;
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001084
1085 local_irq_save(flags);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001086 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1087 macb_interrupt(dev->irq, queue);
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001088 local_irq_restore(flags);
1089}
1090#endif
1091
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001092static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
1093 unsigned int len)
1094{
1095 return (len + bp->max_tx_length - 1) / bp->max_tx_length;
1096}
1097
1098static unsigned int macb_tx_map(struct macb *bp,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001099 struct macb_queue *queue,
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001100 struct sk_buff *skb)
1101{
1102 dma_addr_t mapping;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001103 unsigned int len, entry, i, tx_head = queue->tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001104 struct macb_tx_skb *tx_skb = NULL;
1105 struct macb_dma_desc *desc;
1106 unsigned int offset, size, count = 0;
1107 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1108 unsigned int eof = 1;
1109 u32 ctrl;
1110
1111 /* First, map non-paged data */
1112 len = skb_headlen(skb);
1113 offset = 0;
1114 while (len) {
1115 size = min(len, bp->max_tx_length);
1116 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001117 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001118
1119 mapping = dma_map_single(&bp->pdev->dev,
1120 skb->data + offset,
1121 size, DMA_TO_DEVICE);
1122 if (dma_mapping_error(&bp->pdev->dev, mapping))
1123 goto dma_error;
1124
1125 /* Save info to properly release resources */
1126 tx_skb->skb = NULL;
1127 tx_skb->mapping = mapping;
1128 tx_skb->size = size;
1129 tx_skb->mapped_as_page = false;
1130
1131 len -= size;
1132 offset += size;
1133 count++;
1134 tx_head++;
1135 }
1136
1137 /* Then, map paged data from fragments */
1138 for (f = 0; f < nr_frags; f++) {
1139 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1140
1141 len = skb_frag_size(frag);
1142 offset = 0;
1143 while (len) {
1144 size = min(len, bp->max_tx_length);
1145 entry = macb_tx_ring_wrap(tx_head);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001146 tx_skb = &queue->tx_skb[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001147
1148 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1149 offset, size, DMA_TO_DEVICE);
1150 if (dma_mapping_error(&bp->pdev->dev, mapping))
1151 goto dma_error;
1152
1153 /* Save info to properly release resources */
1154 tx_skb->skb = NULL;
1155 tx_skb->mapping = mapping;
1156 tx_skb->size = size;
1157 tx_skb->mapped_as_page = true;
1158
1159 len -= size;
1160 offset += size;
1161 count++;
1162 tx_head++;
1163 }
1164 }
1165
1166 /* Should never happen */
1167 if (unlikely(tx_skb == NULL)) {
1168 netdev_err(bp->dev, "BUG! empty skb!\n");
1169 return 0;
1170 }
1171
1172 /* This is the last buffer of the frame: save socket buffer */
1173 tx_skb->skb = skb;
1174
1175 /* Update TX ring: update buffer descriptors in reverse order
1176 * to avoid race condition
1177 */
1178
1179 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1180 * to set the end of TX queue
1181 */
1182 i = tx_head;
1183 entry = macb_tx_ring_wrap(i);
1184 ctrl = MACB_BIT(TX_USED);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001185 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001186 desc->ctrl = ctrl;
1187
1188 do {
1189 i--;
1190 entry = macb_tx_ring_wrap(i);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001191 tx_skb = &queue->tx_skb[entry];
1192 desc = &queue->tx_ring[entry];
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001193
1194 ctrl = (u32)tx_skb->size;
1195 if (eof) {
1196 ctrl |= MACB_BIT(TX_LAST);
1197 eof = 0;
1198 }
1199 if (unlikely(entry == (TX_RING_SIZE - 1)))
1200 ctrl |= MACB_BIT(TX_WRAP);
1201
1202 /* Set TX buffer descriptor */
1203 desc->addr = tx_skb->mapping;
1204 /* desc->addr must be visible to hardware before clearing
1205 * 'TX_USED' bit in desc->ctrl.
1206 */
1207 wmb();
1208 desc->ctrl = ctrl;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001209 } while (i != queue->tx_head);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001210
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001211 queue->tx_head = tx_head;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001212
1213 return count;
1214
1215dma_error:
1216 netdev_err(bp->dev, "TX DMA map failed\n");
1217
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001218 for (i = queue->tx_head; i != tx_head; i++) {
1219 tx_skb = macb_tx_skb(queue, i);
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001220
1221 macb_tx_unmap(bp, tx_skb);
1222 }
1223
1224 return 0;
1225}
1226
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001227static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1228{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001229 u16 queue_index = skb_get_queue_mapping(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001230 struct macb *bp = netdev_priv(dev);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001231 struct macb_queue *queue = &bp->queues[queue_index];
Dongdong Deng48719532009-08-23 19:49:07 -07001232 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001233 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001234
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001235#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1236 netdev_vdbg(bp->dev,
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001237 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1238 queue_index, skb->len, skb->head, skb->data,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001239 skb_tail_pointer(skb), skb_end_pointer(skb));
1240 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1241 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001242#endif
1243
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001244 /* Count how many TX buffer descriptors are needed to send this
1245 * socket buffer: skb fragments of jumbo frames may need to be
1246 * splitted into many buffer descriptors.
1247 */
1248 count = macb_count_tx_descriptors(bp, skb_headlen(skb));
1249 nr_frags = skb_shinfo(skb)->nr_frags;
1250 for (f = 0; f < nr_frags; f++) {
1251 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1252 count += macb_count_tx_descriptors(bp, frag_size);
1253 }
1254
Dongdong Deng48719532009-08-23 19:49:07 -07001255 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001256
1257 /* This is a hard error, log it. */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001258 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1259 netif_stop_subqueue(dev, queue_index);
Dongdong Deng48719532009-08-23 19:49:07 -07001260 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001261 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001262 queue->tx_head, queue->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001263 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001264 }
1265
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001266 /* Map socket buffer for DMA transfer */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001267 if (!macb_tx_map(bp, queue, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001268 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001269 goto unlock;
1270 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001271
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001272 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001273 wmb();
1274
Richard Cochrane0720922011-06-19 21:51:28 +00001275 skb_tx_timestamp(skb);
1276
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001277 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1278
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001279 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1280 netif_stop_subqueue(dev, queue_index);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001281
Soren Brinkmann92030902014-03-04 08:46:39 -08001282unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001283 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001284
Patrick McHardy6ed10652009-06-23 06:03:08 +00001285 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001286}
1287
Nicolas Ferre4df95132013-06-04 21:57:12 +00001288static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001289{
1290 if (!macb_is_gem(bp)) {
1291 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1292 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001293 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001294
Nicolas Ferre1b447912013-06-04 21:57:11 +00001295 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001296 netdev_dbg(bp->dev,
1297 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001298 RX_BUFFER_MULTIPLE);
1299 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001300 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001301 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001302 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001303
1304 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1305 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001306}
1307
Nicolas Ferre4df95132013-06-04 21:57:12 +00001308static void gem_free_rx_buffers(struct macb *bp)
1309{
1310 struct sk_buff *skb;
1311 struct macb_dma_desc *desc;
1312 dma_addr_t addr;
1313 int i;
1314
1315 if (!bp->rx_skbuff)
1316 return;
1317
1318 for (i = 0; i < RX_RING_SIZE; i++) {
1319 skb = bp->rx_skbuff[i];
1320
1321 if (skb == NULL)
1322 continue;
1323
1324 desc = &bp->rx_ring[i];
1325 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001326 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001327 DMA_FROM_DEVICE);
1328 dev_kfree_skb_any(skb);
1329 skb = NULL;
1330 }
1331
1332 kfree(bp->rx_skbuff);
1333 bp->rx_skbuff = NULL;
1334}
1335
1336static void macb_free_rx_buffers(struct macb *bp)
1337{
1338 if (bp->rx_buffers) {
1339 dma_free_coherent(&bp->pdev->dev,
1340 RX_RING_SIZE * bp->rx_buffer_size,
1341 bp->rx_buffers, bp->rx_buffers_dma);
1342 bp->rx_buffers = NULL;
1343 }
1344}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001345
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001346static void macb_free_consistent(struct macb *bp)
1347{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001348 struct macb_queue *queue;
1349 unsigned int q;
1350
Nicolas Ferre4df95132013-06-04 21:57:12 +00001351 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001352 if (bp->rx_ring) {
1353 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1354 bp->rx_ring, bp->rx_ring_dma);
1355 bp->rx_ring = NULL;
1356 }
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001357
1358 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1359 kfree(queue->tx_skb);
1360 queue->tx_skb = NULL;
1361 if (queue->tx_ring) {
1362 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1363 queue->tx_ring, queue->tx_ring_dma);
1364 queue->tx_ring = NULL;
1365 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001366 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001367}
1368
1369static int gem_alloc_rx_buffers(struct macb *bp)
1370{
1371 int size;
1372
1373 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1374 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1375 if (!bp->rx_skbuff)
1376 return -ENOMEM;
1377 else
1378 netdev_dbg(bp->dev,
1379 "Allocated %d RX struct sk_buff entries at %p\n",
1380 RX_RING_SIZE, bp->rx_skbuff);
1381 return 0;
1382}
1383
1384static int macb_alloc_rx_buffers(struct macb *bp)
1385{
1386 int size;
1387
1388 size = RX_RING_SIZE * bp->rx_buffer_size;
1389 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1390 &bp->rx_buffers_dma, GFP_KERNEL);
1391 if (!bp->rx_buffers)
1392 return -ENOMEM;
1393 else
1394 netdev_dbg(bp->dev,
1395 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1396 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1397 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001398}
1399
1400static int macb_alloc_consistent(struct macb *bp)
1401{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001402 struct macb_queue *queue;
1403 unsigned int q;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001404 int size;
1405
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001406 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1407 size = TX_RING_BYTES;
1408 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1409 &queue->tx_ring_dma,
1410 GFP_KERNEL);
1411 if (!queue->tx_ring)
1412 goto out_err;
1413 netdev_dbg(bp->dev,
1414 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1415 q, size, (unsigned long)queue->tx_ring_dma,
1416 queue->tx_ring);
1417
1418 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1419 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1420 if (!queue->tx_skb)
1421 goto out_err;
1422 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001423
1424 size = RX_RING_BYTES;
1425 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1426 &bp->rx_ring_dma, GFP_KERNEL);
1427 if (!bp->rx_ring)
1428 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001429 netdev_dbg(bp->dev,
1430 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1431 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001432
Nicolas Ferre4df95132013-06-04 21:57:12 +00001433 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001434 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001435
1436 return 0;
1437
1438out_err:
1439 macb_free_consistent(bp);
1440 return -ENOMEM;
1441}
1442
Nicolas Ferre4df95132013-06-04 21:57:12 +00001443static void gem_init_rings(struct macb *bp)
1444{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001445 struct macb_queue *queue;
1446 unsigned int q;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001447 int i;
1448
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001449 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1450 for (i = 0; i < TX_RING_SIZE; i++) {
1451 queue->tx_ring[i].addr = 0;
1452 queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1453 }
1454 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1455 queue->tx_head = 0;
1456 queue->tx_tail = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001457 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001458
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001459 bp->rx_tail = 0;
1460 bp->rx_prepared_head = 0;
Nicolas Ferre4df95132013-06-04 21:57:12 +00001461
1462 gem_rx_refill(bp);
1463}
1464
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001465static void macb_init_rings(struct macb *bp)
1466{
1467 int i;
1468 dma_addr_t addr;
1469
1470 addr = bp->rx_buffers_dma;
1471 for (i = 0; i < RX_RING_SIZE; i++) {
1472 bp->rx_ring[i].addr = addr;
1473 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001474 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001475 }
1476 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1477
1478 for (i = 0; i < TX_RING_SIZE; i++) {
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001479 bp->queues[0].tx_ring[i].addr = 0;
1480 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001481 }
Ben Shelton21d35152015-04-22 17:28:54 -05001482 bp->queues[0].tx_head = 0;
1483 bp->queues[0].tx_tail = 0;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001484 bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001485
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001486 bp->rx_tail = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001487}
1488
1489static void macb_reset_hw(struct macb *bp)
1490{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001491 struct macb_queue *queue;
1492 unsigned int q;
1493
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001494 /*
1495 * Disable RX and TX (XXX: Should we halt the transmission
1496 * more gracefully?)
1497 */
1498 macb_writel(bp, NCR, 0);
1499
1500 /* Clear the stats registers (XXX: Update stats first?) */
1501 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1502
1503 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001504 macb_writel(bp, TSR, -1);
1505 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001506
1507 /* Disable all interrupts */
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001508 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1509 queue_writel(queue, IDR, -1);
1510 queue_readl(queue, ISR);
1511 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001512}
1513
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001514static u32 gem_mdc_clk_div(struct macb *bp)
1515{
1516 u32 config;
1517 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1518
1519 if (pclk_hz <= 20000000)
1520 config = GEM_BF(CLK, GEM_CLK_DIV8);
1521 else if (pclk_hz <= 40000000)
1522 config = GEM_BF(CLK, GEM_CLK_DIV16);
1523 else if (pclk_hz <= 80000000)
1524 config = GEM_BF(CLK, GEM_CLK_DIV32);
1525 else if (pclk_hz <= 120000000)
1526 config = GEM_BF(CLK, GEM_CLK_DIV48);
1527 else if (pclk_hz <= 160000000)
1528 config = GEM_BF(CLK, GEM_CLK_DIV64);
1529 else
1530 config = GEM_BF(CLK, GEM_CLK_DIV96);
1531
1532 return config;
1533}
1534
1535static u32 macb_mdc_clk_div(struct macb *bp)
1536{
1537 u32 config;
1538 unsigned long pclk_hz;
1539
1540 if (macb_is_gem(bp))
1541 return gem_mdc_clk_div(bp);
1542
1543 pclk_hz = clk_get_rate(bp->pclk);
1544 if (pclk_hz <= 20000000)
1545 config = MACB_BF(CLK, MACB_CLK_DIV8);
1546 else if (pclk_hz <= 40000000)
1547 config = MACB_BF(CLK, MACB_CLK_DIV16);
1548 else if (pclk_hz <= 80000000)
1549 config = MACB_BF(CLK, MACB_CLK_DIV32);
1550 else
1551 config = MACB_BF(CLK, MACB_CLK_DIV64);
1552
1553 return config;
1554}
1555
Jamie Iles757a03c2011-03-09 16:29:59 +00001556/*
1557 * Get the DMA bus width field of the network configuration register that we
1558 * should program. We find the width from decoding the design configuration
1559 * register to find the maximum supported data bus width.
1560 */
1561static u32 macb_dbw(struct macb *bp)
1562{
1563 if (!macb_is_gem(bp))
1564 return 0;
1565
1566 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1567 case 4:
1568 return GEM_BF(DBW, GEM_DBW128);
1569 case 2:
1570 return GEM_BF(DBW, GEM_DBW64);
1571 case 1:
1572 default:
1573 return GEM_BF(DBW, GEM_DBW32);
1574 }
1575}
1576
Jamie Iles0116da42011-03-14 17:38:30 +00001577/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001578 * Configure the receive DMA engine
1579 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001580 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001581 * (if not supported by FIFO, it will fallback to default)
1582 * - set both rx/tx packet buffers to full memory size
1583 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001584 */
1585static void macb_configure_dma(struct macb *bp)
1586{
1587 u32 dmacfg;
Arun Chandran62f69242015-03-01 11:38:02 +05301588 u32 tmp, ncr;
Jamie Iles0116da42011-03-14 17:38:30 +00001589
1590 if (macb_is_gem(bp)) {
1591 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001592 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001593 if (bp->dma_burst_length)
1594 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001595 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Arun Chandrana50dad32015-02-18 16:59:35 +05301596 dmacfg &= ~GEM_BIT(ENDIA_PKT);
Arun Chandran62f69242015-03-01 11:38:02 +05301597
1598 /* Find the CPU endianness by using the loopback bit of net_ctrl
1599 * register. save it first. When the CPU is in big endian we
1600 * need to program swaped mode for management descriptor access.
1601 */
1602 ncr = macb_readl(bp, NCR);
1603 __raw_writel(MACB_BIT(LLB), bp->regs + MACB_NCR);
1604 tmp = __raw_readl(bp->regs + MACB_NCR);
1605
1606 if (tmp == MACB_BIT(LLB))
1607 dmacfg &= ~GEM_BIT(ENDIA_DESC);
1608 else
1609 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1610
1611 /* Restore net_ctrl */
1612 macb_writel(bp, NCR, ncr);
1613
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001614 if (bp->dev->features & NETIF_F_HW_CSUM)
1615 dmacfg |= GEM_BIT(TXCOEN);
1616 else
1617 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001618 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1619 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001620 gem_writel(bp, DMACFG, dmacfg);
1621 }
1622}
1623
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001624static void macb_init_hw(struct macb *bp)
1625{
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001626 struct macb_queue *queue;
1627 unsigned int q;
1628
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001629 u32 config;
1630
1631 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001632 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001633
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001634 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001635 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001636 config |= MACB_BIT(PAE); /* PAuse Enable */
1637 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301638 if (bp->caps | MACB_CAPS_JUMBO)
1639 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
1640 else
1641 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001642 if (bp->dev->flags & IFF_PROMISC)
1643 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001644 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1645 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001646 if (!(bp->dev->flags & IFF_BROADCAST))
1647 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001648 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001649 macb_writel(bp, NCFGR, config);
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301650 if ((bp->caps | MACB_CAPS_JUMBO) && bp->jumbo_max_len)
1651 gem_writel(bp, JML, bp->jumbo_max_len);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001652 bp->speed = SPEED_10;
1653 bp->duplex = DUPLEX_HALF;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05301654 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
1655 if (bp->caps | MACB_CAPS_JUMBO)
1656 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001657
Jamie Iles0116da42011-03-14 17:38:30 +00001658 macb_configure_dma(bp);
1659
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001660 /* Initialize TX and RX buffers */
1661 macb_writel(bp, RBQP, bp->rx_ring_dma);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001662 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1663 queue_writel(queue, TBQP, queue->tx_ring_dma);
1664
1665 /* Enable interrupts */
1666 queue_writel(queue, IER,
1667 MACB_RX_INT_FLAGS |
1668 MACB_TX_INT_FLAGS |
1669 MACB_BIT(HRESP));
1670 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001671
1672 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001673 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001674}
1675
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001676/*
1677 * The hash address register is 64 bits long and takes up two
1678 * locations in the memory map. The least significant bits are stored
1679 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1680 *
1681 * The unicast hash enable and the multicast hash enable bits in the
1682 * network configuration register enable the reception of hash matched
1683 * frames. The destination address is reduced to a 6 bit index into
1684 * the 64 bit hash register using the following hash function. The
1685 * hash function is an exclusive or of every sixth bit of the
1686 * destination address.
1687 *
1688 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1689 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1690 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1691 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1692 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1693 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1694 *
1695 * da[0] represents the least significant bit of the first byte
1696 * received, that is, the multicast/unicast indicator, and da[47]
1697 * represents the most significant bit of the last byte received. If
1698 * the hash index, hi[n], points to a bit that is set in the hash
1699 * register then the frame will be matched according to whether the
1700 * frame is multicast or unicast. A multicast match will be signalled
1701 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1702 * index points to a bit set in the hash register. A unicast match
1703 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1704 * and the hash index points to a bit set in the hash register. To
1705 * receive all multicast frames, the hash register should be set with
1706 * all ones and the multicast hash enable bit should be set in the
1707 * network configuration register.
1708 */
1709
1710static inline int hash_bit_value(int bitnr, __u8 *addr)
1711{
1712 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1713 return 1;
1714 return 0;
1715}
1716
1717/*
1718 * Return the hash index value for the specified address.
1719 */
1720static int hash_get_index(__u8 *addr)
1721{
1722 int i, j, bitval;
1723 int hash_index = 0;
1724
1725 for (j = 0; j < 6; j++) {
1726 for (i = 0, bitval = 0; i < 8; i++)
Xander Huff2fa45e22015-01-15 15:55:19 -06001727 bitval ^= hash_bit_value(i * 6 + j, addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001728
1729 hash_index |= (bitval << j);
1730 }
1731
1732 return hash_index;
1733}
1734
1735/*
1736 * Add multicast addresses to the internal multicast-hash table.
1737 */
1738static void macb_sethashtable(struct net_device *dev)
1739{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001740 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001741 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001742 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001743 struct macb *bp = netdev_priv(dev);
1744
1745 mc_filter[0] = mc_filter[1] = 0;
1746
Jiri Pirko22bedad32010-04-01 21:22:57 +00001747 netdev_for_each_mc_addr(ha, dev) {
1748 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001749 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1750 }
1751
Jamie Ilesf75ba502011-11-08 10:12:32 +00001752 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1753 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001754}
1755
1756/*
1757 * Enable/Disable promiscuous and multicast modes.
1758 */
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001759static void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001760{
1761 unsigned long cfg;
1762 struct macb *bp = netdev_priv(dev);
1763
1764 cfg = macb_readl(bp, NCFGR);
1765
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001766 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001767 /* Enable promiscuous mode */
1768 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001769
1770 /* Disable RX checksum offload */
1771 if (macb_is_gem(bp))
1772 cfg &= ~GEM_BIT(RXCOEN);
1773 } else {
1774 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001775 cfg &= ~MACB_BIT(CAF);
1776
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001777 /* Enable RX checksum offload only if requested */
1778 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1779 cfg |= GEM_BIT(RXCOEN);
1780 }
1781
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001782 if (dev->flags & IFF_ALLMULTI) {
1783 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001784 macb_or_gem_writel(bp, HRB, -1);
1785 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001786 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001787 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001788 /* Enable specific multicasts */
1789 macb_sethashtable(dev);
1790 cfg |= MACB_BIT(NCFGR_MTI);
1791 } else if (dev->flags & (~IFF_ALLMULTI)) {
1792 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001793 macb_or_gem_writel(bp, HRB, 0);
1794 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001795 cfg &= ~MACB_BIT(NCFGR_MTI);
1796 }
1797
1798 macb_writel(bp, NCFGR, cfg);
1799}
1800
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001801static int macb_open(struct net_device *dev)
1802{
1803 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001804 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001805 int err;
1806
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001807 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001808
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001809 /* carrier starts down */
1810 netif_carrier_off(dev);
1811
frederic RODO6c36a702007-07-12 19:07:24 +02001812 /* if the phy is not yet register, retry later*/
1813 if (!bp->phy_dev)
1814 return -EAGAIN;
1815
Nicolas Ferre1b447912013-06-04 21:57:11 +00001816 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001817 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001818
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001819 err = macb_alloc_consistent(bp);
1820 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001821 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1822 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001823 return err;
1824 }
1825
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001826 napi_enable(&bp->napi);
1827
Nicolas Ferre4df95132013-06-04 21:57:12 +00001828 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001829 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001830
frederic RODO6c36a702007-07-12 19:07:24 +02001831 /* schedule a link state check */
1832 phy_start(bp->phy_dev);
1833
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001834 netif_tx_start_all_queues(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001835
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001836 return 0;
1837}
1838
1839static int macb_close(struct net_device *dev)
1840{
1841 struct macb *bp = netdev_priv(dev);
1842 unsigned long flags;
1843
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01001844 netif_tx_stop_all_queues(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001845 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001846
frederic RODO6c36a702007-07-12 19:07:24 +02001847 if (bp->phy_dev)
1848 phy_stop(bp->phy_dev);
1849
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001850 spin_lock_irqsave(&bp->lock, flags);
1851 macb_reset_hw(bp);
1852 netif_carrier_off(dev);
1853 spin_unlock_irqrestore(&bp->lock, flags);
1854
1855 macb_free_consistent(bp);
1856
1857 return 0;
1858}
1859
Harini Katakama5898ea2015-05-06 22:27:18 +05301860static int macb_change_mtu(struct net_device *dev, int new_mtu)
1861{
1862 struct macb *bp = netdev_priv(dev);
1863 u32 max_mtu;
1864
1865 if (netif_running(dev))
1866 return -EBUSY;
1867
1868 max_mtu = ETH_DATA_LEN;
1869 if (bp->caps | MACB_CAPS_JUMBO)
1870 max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
1871
1872 if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE))
1873 return -EINVAL;
1874
1875 dev->mtu = new_mtu;
1876
1877 return 0;
1878}
1879
Jamie Ilesa494ed82011-03-09 16:26:35 +00001880static void gem_update_stats(struct macb *bp)
1881{
Xander Huff3ff13f12015-01-13 16:15:51 -06001882 int i;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001883 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001884
Xander Huff3ff13f12015-01-13 16:15:51 -06001885 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
1886 u32 offset = gem_statistics[i].offset;
Arun Chandrana50dad32015-02-18 16:59:35 +05301887 u64 val = readl_relaxed(bp->regs + offset);
Xander Huff3ff13f12015-01-13 16:15:51 -06001888
1889 bp->ethtool_stats[i] += val;
1890 *p += val;
1891
1892 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
1893 /* Add GEM_OCTTXH, GEM_OCTRXH */
Arun Chandrana50dad32015-02-18 16:59:35 +05301894 val = readl_relaxed(bp->regs + offset + 4);
Xander Huff2fa45e22015-01-15 15:55:19 -06001895 bp->ethtool_stats[i] += ((u64)val) << 32;
Xander Huff3ff13f12015-01-13 16:15:51 -06001896 *(++p) += val;
1897 }
1898 }
Jamie Ilesa494ed82011-03-09 16:26:35 +00001899}
1900
1901static struct net_device_stats *gem_get_stats(struct macb *bp)
1902{
1903 struct gem_stats *hwstat = &bp->hw_stats.gem;
1904 struct net_device_stats *nstat = &bp->stats;
1905
1906 gem_update_stats(bp);
1907
1908 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1909 hwstat->rx_alignment_errors +
1910 hwstat->rx_resource_errors +
1911 hwstat->rx_overruns +
1912 hwstat->rx_oversize_frames +
1913 hwstat->rx_jabbers +
1914 hwstat->rx_undersized_frames +
1915 hwstat->rx_length_field_frame_errors);
1916 nstat->tx_errors = (hwstat->tx_late_collisions +
1917 hwstat->tx_excessive_collisions +
1918 hwstat->tx_underrun +
1919 hwstat->tx_carrier_sense_errors);
1920 nstat->multicast = hwstat->rx_multicast_frames;
1921 nstat->collisions = (hwstat->tx_single_collision_frames +
1922 hwstat->tx_multiple_collision_frames +
1923 hwstat->tx_excessive_collisions);
1924 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1925 hwstat->rx_jabbers +
1926 hwstat->rx_undersized_frames +
1927 hwstat->rx_length_field_frame_errors);
1928 nstat->rx_over_errors = hwstat->rx_resource_errors;
1929 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1930 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1931 nstat->rx_fifo_errors = hwstat->rx_overruns;
1932 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1933 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1934 nstat->tx_fifo_errors = hwstat->tx_underrun;
1935
1936 return nstat;
1937}
1938
Xander Huff3ff13f12015-01-13 16:15:51 -06001939static void gem_get_ethtool_stats(struct net_device *dev,
1940 struct ethtool_stats *stats, u64 *data)
1941{
1942 struct macb *bp;
1943
1944 bp = netdev_priv(dev);
1945 gem_update_stats(bp);
Xander Huff2fa45e22015-01-15 15:55:19 -06001946 memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
Xander Huff3ff13f12015-01-13 16:15:51 -06001947}
1948
1949static int gem_get_sset_count(struct net_device *dev, int sset)
1950{
1951 switch (sset) {
1952 case ETH_SS_STATS:
1953 return GEM_STATS_LEN;
1954 default:
1955 return -EOPNOTSUPP;
1956 }
1957}
1958
1959static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
1960{
1961 int i;
1962
1963 switch (sset) {
1964 case ETH_SS_STATS:
1965 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
1966 memcpy(p, gem_statistics[i].stat_string,
1967 ETH_GSTRING_LEN);
1968 break;
1969 }
1970}
1971
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01001972static struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001973{
1974 struct macb *bp = netdev_priv(dev);
1975 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001976 struct macb_stats *hwstat = &bp->hw_stats.macb;
1977
1978 if (macb_is_gem(bp))
1979 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001980
frederic RODO6c36a702007-07-12 19:07:24 +02001981 /* read stats from hardware */
1982 macb_update_stats(bp);
1983
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001984 /* Convert HW stats into netdevice stats */
1985 nstat->rx_errors = (hwstat->rx_fcs_errors +
1986 hwstat->rx_align_errors +
1987 hwstat->rx_resource_errors +
1988 hwstat->rx_overruns +
1989 hwstat->rx_oversize_pkts +
1990 hwstat->rx_jabbers +
1991 hwstat->rx_undersize_pkts +
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992 hwstat->rx_length_mismatch);
1993 nstat->tx_errors = (hwstat->tx_late_cols +
1994 hwstat->tx_excessive_cols +
1995 hwstat->tx_underruns +
Wolfgang Steinwender716723c2015-04-10 11:42:56 +02001996 hwstat->tx_carrier_errors +
1997 hwstat->sqe_test_errors);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001998 nstat->collisions = (hwstat->tx_single_cols +
1999 hwstat->tx_multiple_cols +
2000 hwstat->tx_excessive_cols);
2001 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2002 hwstat->rx_jabbers +
2003 hwstat->rx_undersize_pkts +
2004 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00002005 nstat->rx_over_errors = hwstat->rx_resource_errors +
2006 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002007 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2008 nstat->rx_frame_errors = hwstat->rx_align_errors;
2009 nstat->rx_fifo_errors = hwstat->rx_overruns;
2010 /* XXX: What does "missed" mean? */
2011 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2012 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2013 nstat->tx_fifo_errors = hwstat->tx_underruns;
2014 /* Don't know about heartbeat or window errors... */
2015
2016 return nstat;
2017}
2018
2019static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2020{
2021 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002022 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002023
frederic RODO6c36a702007-07-12 19:07:24 +02002024 if (!phydev)
2025 return -ENODEV;
2026
2027 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002028}
2029
2030static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2031{
2032 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002033 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002034
frederic RODO6c36a702007-07-12 19:07:24 +02002035 if (!phydev)
2036 return -ENODEV;
2037
2038 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002039}
2040
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002041static int macb_get_regs_len(struct net_device *netdev)
2042{
2043 return MACB_GREGS_NBR * sizeof(u32);
2044}
2045
2046static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2047 void *p)
2048{
2049 struct macb *bp = netdev_priv(dev);
2050 unsigned int tail, head;
2051 u32 *regs_buff = p;
2052
2053 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2054 | MACB_GREGS_VERSION;
2055
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002056 tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2057 head = macb_tx_ring_wrap(bp->queues[0].tx_head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002058
2059 regs_buff[0] = macb_readl(bp, NCR);
2060 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2061 regs_buff[2] = macb_readl(bp, NSR);
2062 regs_buff[3] = macb_readl(bp, TSR);
2063 regs_buff[4] = macb_readl(bp, RBQP);
2064 regs_buff[5] = macb_readl(bp, TBQP);
2065 regs_buff[6] = macb_readl(bp, RSR);
2066 regs_buff[7] = macb_readl(bp, IMR);
2067
2068 regs_buff[8] = tail;
2069 regs_buff[9] = head;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002070 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2071 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002072
Nicolas Ferre7c399942015-03-31 15:02:04 +02002073 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002074 if (macb_is_gem(bp)) {
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002075 regs_buff[13] = gem_readl(bp, DMACFG);
2076 }
2077}
2078
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002079static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002080 .get_settings = macb_get_settings,
2081 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00002082 .get_regs_len = macb_get_regs_len,
2083 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00002085 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff8cd5a562015-01-15 15:55:20 -06002086};
Xander Huff8cd5a562015-01-15 15:55:20 -06002087
Lad, Prabhakar8093b1c2015-02-05 16:21:07 +00002088static const struct ethtool_ops gem_ethtool_ops = {
Xander Huff8cd5a562015-01-15 15:55:20 -06002089 .get_settings = macb_get_settings,
2090 .set_settings = macb_set_settings,
2091 .get_regs_len = macb_get_regs_len,
2092 .get_regs = macb_get_regs,
2093 .get_link = ethtool_op_get_link,
2094 .get_ts_info = ethtool_op_get_ts_info,
Xander Huff3ff13f12015-01-13 16:15:51 -06002095 .get_ethtool_stats = gem_get_ethtool_stats,
2096 .get_strings = gem_get_ethtool_strings,
2097 .get_sset_count = gem_get_sset_count,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002098};
2099
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002100static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002101{
2102 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02002103 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002104
2105 if (!netif_running(dev))
2106 return -EINVAL;
2107
frederic RODO6c36a702007-07-12 19:07:24 +02002108 if (!phydev)
2109 return -ENODEV;
2110
Richard Cochran28b04112010-07-17 08:48:55 +00002111 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002112}
2113
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002114static int macb_set_features(struct net_device *netdev,
2115 netdev_features_t features)
2116{
2117 struct macb *bp = netdev_priv(netdev);
2118 netdev_features_t changed = features ^ netdev->features;
2119
2120 /* TX checksum offload */
2121 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2122 u32 dmacfg;
2123
2124 dmacfg = gem_readl(bp, DMACFG);
2125 if (features & NETIF_F_HW_CSUM)
2126 dmacfg |= GEM_BIT(TXCOEN);
2127 else
2128 dmacfg &= ~GEM_BIT(TXCOEN);
2129 gem_writel(bp, DMACFG, dmacfg);
2130 }
2131
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002132 /* RX checksum offload */
2133 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2134 u32 netcfg;
2135
2136 netcfg = gem_readl(bp, NCFGR);
2137 if (features & NETIF_F_RXCSUM &&
2138 !(netdev->flags & IFF_PROMISC))
2139 netcfg |= GEM_BIT(RXCOEN);
2140 else
2141 netcfg &= ~GEM_BIT(RXCOEN);
2142 gem_writel(bp, NCFGR, netcfg);
2143 }
2144
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002145 return 0;
2146}
2147
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002148static const struct net_device_ops macb_netdev_ops = {
2149 .ndo_open = macb_open,
2150 .ndo_stop = macb_close,
2151 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002152 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002153 .ndo_get_stats = macb_get_stats,
2154 .ndo_do_ioctl = macb_ioctl,
2155 .ndo_validate_addr = eth_validate_addr,
Harini Katakama5898ea2015-05-06 22:27:18 +05302156 .ndo_change_mtu = macb_change_mtu,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002157 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07002158#ifdef CONFIG_NET_POLL_CONTROLLER
2159 .ndo_poll_controller = macb_poll_controller,
2160#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002161 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002162};
2163
Nicolas Ferree1755872014-07-24 13:50:58 +02002164/*
Nicolas Ferread783472015-03-31 15:02:02 +02002165 * Configure peripheral capabilities according to device tree
Nicolas Ferree1755872014-07-24 13:50:58 +02002166 * and integration options used
2167 */
Nicolas Ferref6970502015-03-31 15:02:01 +02002168static void macb_configure_caps(struct macb *bp, const struct macb_config *dt_conf)
Nicolas Ferree1755872014-07-24 13:50:58 +02002169{
2170 u32 dcfg;
Nicolas Ferree1755872014-07-24 13:50:58 +02002171
Nicolas Ferref6970502015-03-31 15:02:01 +02002172 if (dt_conf)
2173 bp->caps = dt_conf->caps;
2174
Nicolas Ferrefa693592015-03-31 15:02:06 +02002175 if (macb_is_gem_hw(bp->regs)) {
Nicolas Ferree1755872014-07-24 13:50:58 +02002176 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2177
Nicolas Ferree1755872014-07-24 13:50:58 +02002178 dcfg = gem_readl(bp, DCFG1);
2179 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2180 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2181 dcfg = gem_readl(bp, DCFG2);
2182 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2183 bp->caps |= MACB_CAPS_FIFO_MODE;
2184 }
2185
2186 netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
2187}
2188
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002189static void macb_probe_queues(void __iomem *mem,
2190 unsigned int *queue_mask,
2191 unsigned int *num_queues)
2192{
2193 unsigned int hw_q;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002194
2195 *queue_mask = 0x1;
2196 *num_queues = 1;
2197
Nicolas Ferreda120112015-03-31 15:02:00 +02002198 /* is it macb or gem ?
2199 *
2200 * We need to read directly from the hardware here because
2201 * we are early in the probe process and don't have the
2202 * MACB_CAPS_MACB_IS_GEM flag positioned
2203 */
Nicolas Ferrefa693592015-03-31 15:02:06 +02002204 if (!macb_is_gem_hw(mem))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002205 return;
2206
2207 /* bit 0 is never set but queue 0 always exists */
Arun Chandrana50dad32015-02-18 16:59:35 +05302208 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2209
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002210 *queue_mask |= 0x1;
2211
2212 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2213 if (*queue_mask & (1 << hw_q))
2214 (*num_queues)++;
2215}
2216
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002217static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
2218 struct clk **hclk, struct clk **tx_clk)
2219{
2220 int err;
2221
2222 *pclk = devm_clk_get(&pdev->dev, "pclk");
2223 if (IS_ERR(*pclk)) {
2224 err = PTR_ERR(*pclk);
2225 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
2226 return err;
2227 }
2228
2229 *hclk = devm_clk_get(&pdev->dev, "hclk");
2230 if (IS_ERR(*hclk)) {
2231 err = PTR_ERR(*hclk);
2232 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2233 return err;
2234 }
2235
2236 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2237 if (IS_ERR(*tx_clk))
2238 *tx_clk = NULL;
2239
2240 err = clk_prepare_enable(*pclk);
2241 if (err) {
2242 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2243 return err;
2244 }
2245
2246 err = clk_prepare_enable(*hclk);
2247 if (err) {
2248 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2249 goto err_disable_pclk;
2250 }
2251
2252 err = clk_prepare_enable(*tx_clk);
2253 if (err) {
2254 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
2255 goto err_disable_hclk;
2256 }
2257
2258 return 0;
2259
2260err_disable_hclk:
2261 clk_disable_unprepare(*hclk);
2262
2263err_disable_pclk:
2264 clk_disable_unprepare(*pclk);
2265
2266 return err;
2267}
2268
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002269static int macb_init(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002270{
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002271 struct net_device *dev = platform_get_drvdata(pdev);
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002272 unsigned int hw_q, q;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002273 struct macb *bp = netdev_priv(dev);
2274 struct macb_queue *queue;
2275 int err;
2276 u32 val;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002277
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002278 /* set the queue register mapping once for all: queue0 has a special
2279 * register mapping but we don't want to test the queue index then
2280 * compute the corresponding register offset at run time.
2281 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002282 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002283 if (!(bp->queue_mask & (1 << hw_q)))
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002284 continue;
Jamie Iles461845d2011-03-08 20:19:23 +00002285
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002286 queue = &bp->queues[q];
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002287 queue->bp = bp;
2288 if (hw_q) {
2289 queue->ISR = GEM_ISR(hw_q - 1);
2290 queue->IER = GEM_IER(hw_q - 1);
2291 queue->IDR = GEM_IDR(hw_q - 1);
2292 queue->IMR = GEM_IMR(hw_q - 1);
2293 queue->TBQP = GEM_TBQP(hw_q - 1);
2294 } else {
2295 /* queue0 uses legacy registers */
2296 queue->ISR = MACB_ISR;
2297 queue->IER = MACB_IER;
2298 queue->IDR = MACB_IDR;
2299 queue->IMR = MACB_IMR;
2300 queue->TBQP = MACB_TBQP;
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002301 }
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002302
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002303 /* get irq: here we use the linux queue index, not the hardware
2304 * queue index. the queue irq definitions in the device tree
2305 * must remove the optional gaps that could exist in the
2306 * hardware queue mask.
2307 */
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002308 queue->irq = platform_get_irq(pdev, q);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002309 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
Punnaiah Choudary Kalluri20488232015-03-06 18:29:12 +01002310 IRQF_SHARED, dev->name, queue);
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002311 if (err) {
2312 dev_err(&pdev->dev,
2313 "Unable to request IRQ %d (error %d)\n",
2314 queue->irq, err);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002315 return err;
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002316 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002317
Cyrille Pitchen02c958d2014-12-12 13:26:44 +01002318 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002319 q++;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002320 }
2321
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002322 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002323 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002324
Nicolas Ferre4df95132013-06-04 21:57:12 +00002325 /* setup appropriated routines according to adapter type */
2326 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002327 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002328 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2329 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2330 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2331 bp->macbgem_ops.mog_rx = gem_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002332 dev->ethtool_ops = &gem_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002333 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002334 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002335 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2336 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2337 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2338 bp->macbgem_ops.mog_rx = macb_rx;
Xander Huff8cd5a562015-01-15 15:55:20 -06002339 dev->ethtool_ops = &macb_ethtool_ops;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002340 }
2341
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002342 /* Set features */
2343 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002344 /* Checksum offload is only available on gem with packet buffer */
2345 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002346 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002347 if (bp->caps & MACB_CAPS_SG_DISABLED)
2348 dev->hw_features &= ~NETIF_F_SG;
2349 dev->features = dev->hw_features;
2350
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002351 val = 0;
2352 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2353 val = GEM_BIT(RGMII);
2354 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
2355 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2356 val = MACB_BIT(RMII);
2357 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII))
2358 val = MACB_BIT(MII);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002359
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002360 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2361 val |= MACB_BIT(CLKEN);
2362
2363 macb_or_gem_writel(bp, USRIO, val);
2364
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002365 /* Set MII management clock divider */
2366 val = macb_mdc_clk_div(bp);
2367 val |= macb_dbw(bp);
2368 macb_writel(bp, NCFGR, val);
2369
2370 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002371}
2372
2373#if defined(CONFIG_OF)
2374/* 1518 rounded up */
2375#define AT91ETHER_MAX_RBUFF_SZ 0x600
2376/* max number of receive buffers */
2377#define AT91ETHER_MAX_RX_DESCR 9
2378
2379/* Initialize and start the Receiver and Transmit subsystems */
2380static int at91ether_start(struct net_device *dev)
2381{
2382 struct macb *lp = netdev_priv(dev);
2383 dma_addr_t addr;
2384 u32 ctl;
2385 int i;
2386
2387 lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2388 (AT91ETHER_MAX_RX_DESCR *
2389 sizeof(struct macb_dma_desc)),
2390 &lp->rx_ring_dma, GFP_KERNEL);
2391 if (!lp->rx_ring)
2392 return -ENOMEM;
2393
2394 lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2395 AT91ETHER_MAX_RX_DESCR *
2396 AT91ETHER_MAX_RBUFF_SZ,
2397 &lp->rx_buffers_dma, GFP_KERNEL);
2398 if (!lp->rx_buffers) {
2399 dma_free_coherent(&lp->pdev->dev,
2400 AT91ETHER_MAX_RX_DESCR *
2401 sizeof(struct macb_dma_desc),
2402 lp->rx_ring, lp->rx_ring_dma);
2403 lp->rx_ring = NULL;
2404 return -ENOMEM;
2405 }
2406
2407 addr = lp->rx_buffers_dma;
2408 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2409 lp->rx_ring[i].addr = addr;
2410 lp->rx_ring[i].ctrl = 0;
2411 addr += AT91ETHER_MAX_RBUFF_SZ;
2412 }
2413
2414 /* Set the Wrap bit on the last descriptor */
2415 lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2416
2417 /* Reset buffer index */
2418 lp->rx_tail = 0;
2419
2420 /* Program address of descriptor list in Rx Buffer Queue register */
2421 macb_writel(lp, RBQP, lp->rx_ring_dma);
2422
2423 /* Enable Receive and Transmit */
2424 ctl = macb_readl(lp, NCR);
2425 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2426
2427 return 0;
2428}
2429
2430/* Open the ethernet interface */
2431static int at91ether_open(struct net_device *dev)
2432{
2433 struct macb *lp = netdev_priv(dev);
2434 u32 ctl;
2435 int ret;
2436
2437 /* Clear internal statistics */
2438 ctl = macb_readl(lp, NCR);
2439 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2440
2441 macb_set_hwaddr(lp);
2442
2443 ret = at91ether_start(dev);
2444 if (ret)
2445 return ret;
2446
2447 /* Enable MAC interrupts */
2448 macb_writel(lp, IER, MACB_BIT(RCOMP) |
2449 MACB_BIT(RXUBR) |
2450 MACB_BIT(ISR_TUND) |
2451 MACB_BIT(ISR_RLE) |
2452 MACB_BIT(TCOMP) |
2453 MACB_BIT(ISR_ROVR) |
2454 MACB_BIT(HRESP));
2455
2456 /* schedule a link state check */
2457 phy_start(lp->phy_dev);
2458
2459 netif_start_queue(dev);
2460
2461 return 0;
2462}
2463
2464/* Close the interface */
2465static int at91ether_close(struct net_device *dev)
2466{
2467 struct macb *lp = netdev_priv(dev);
2468 u32 ctl;
2469
2470 /* Disable Receiver and Transmitter */
2471 ctl = macb_readl(lp, NCR);
2472 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2473
2474 /* Disable MAC interrupts */
2475 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
2476 MACB_BIT(RXUBR) |
2477 MACB_BIT(ISR_TUND) |
2478 MACB_BIT(ISR_RLE) |
2479 MACB_BIT(TCOMP) |
2480 MACB_BIT(ISR_ROVR) |
2481 MACB_BIT(HRESP));
2482
2483 netif_stop_queue(dev);
2484
2485 dma_free_coherent(&lp->pdev->dev,
2486 AT91ETHER_MAX_RX_DESCR *
2487 sizeof(struct macb_dma_desc),
2488 lp->rx_ring, lp->rx_ring_dma);
2489 lp->rx_ring = NULL;
2490
2491 dma_free_coherent(&lp->pdev->dev,
2492 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2493 lp->rx_buffers, lp->rx_buffers_dma);
2494 lp->rx_buffers = NULL;
2495
2496 return 0;
2497}
2498
2499/* Transmit packet */
2500static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2501{
2502 struct macb *lp = netdev_priv(dev);
2503
2504 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2505 netif_stop_queue(dev);
2506
2507 /* Store packet information (to free when Tx completed) */
2508 lp->skb = skb;
2509 lp->skb_length = skb->len;
2510 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2511 DMA_TO_DEVICE);
2512
2513 /* Set address of the data in the Transmit Address register */
2514 macb_writel(lp, TAR, lp->skb_physaddr);
2515 /* Set length of the packet in the Transmit Control register */
2516 macb_writel(lp, TCR, skb->len);
2517
2518 } else {
2519 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2520 return NETDEV_TX_BUSY;
2521 }
2522
2523 return NETDEV_TX_OK;
2524}
2525
2526/* Extract received frame from buffer descriptors and sent to upper layers.
2527 * (Called from interrupt context)
2528 */
2529static void at91ether_rx(struct net_device *dev)
2530{
2531 struct macb *lp = netdev_priv(dev);
2532 unsigned char *p_recv;
2533 struct sk_buff *skb;
2534 unsigned int pktlen;
2535
2536 while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2537 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2538 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2539 skb = netdev_alloc_skb(dev, pktlen + 2);
2540 if (skb) {
2541 skb_reserve(skb, 2);
2542 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2543
2544 skb->protocol = eth_type_trans(skb, dev);
2545 lp->stats.rx_packets++;
2546 lp->stats.rx_bytes += pktlen;
2547 netif_rx(skb);
2548 } else {
2549 lp->stats.rx_dropped++;
2550 }
2551
2552 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2553 lp->stats.multicast++;
2554
2555 /* reset ownership bit */
2556 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2557
2558 /* wrap after last buffer */
2559 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2560 lp->rx_tail = 0;
2561 else
2562 lp->rx_tail++;
2563 }
2564}
2565
2566/* MAC interrupt handler */
2567static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2568{
2569 struct net_device *dev = dev_id;
2570 struct macb *lp = netdev_priv(dev);
2571 u32 intstatus, ctl;
2572
2573 /* MAC Interrupt Status register indicates what interrupts are pending.
2574 * It is automatically cleared once read.
2575 */
2576 intstatus = macb_readl(lp, ISR);
2577
2578 /* Receive complete */
2579 if (intstatus & MACB_BIT(RCOMP))
2580 at91ether_rx(dev);
2581
2582 /* Transmit complete */
2583 if (intstatus & MACB_BIT(TCOMP)) {
2584 /* The TCOM bit is set even if the transmission failed */
2585 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2586 lp->stats.tx_errors++;
2587
2588 if (lp->skb) {
2589 dev_kfree_skb_irq(lp->skb);
2590 lp->skb = NULL;
2591 dma_unmap_single(NULL, lp->skb_physaddr,
2592 lp->skb_length, DMA_TO_DEVICE);
2593 lp->stats.tx_packets++;
2594 lp->stats.tx_bytes += lp->skb_length;
2595 }
2596 netif_wake_queue(dev);
2597 }
2598
2599 /* Work-around for EMAC Errata section 41.3.1 */
2600 if (intstatus & MACB_BIT(RXUBR)) {
2601 ctl = macb_readl(lp, NCR);
2602 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2603 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2604 }
2605
2606 if (intstatus & MACB_BIT(ISR_ROVR))
2607 netdev_err(dev, "ROVR error\n");
2608
2609 return IRQ_HANDLED;
2610}
2611
2612#ifdef CONFIG_NET_POLL_CONTROLLER
2613static void at91ether_poll_controller(struct net_device *dev)
2614{
2615 unsigned long flags;
2616
2617 local_irq_save(flags);
2618 at91ether_interrupt(dev->irq, dev);
2619 local_irq_restore(flags);
2620}
2621#endif
2622
2623static const struct net_device_ops at91ether_netdev_ops = {
2624 .ndo_open = at91ether_open,
2625 .ndo_stop = at91ether_close,
2626 .ndo_start_xmit = at91ether_start_xmit,
2627 .ndo_get_stats = macb_get_stats,
2628 .ndo_set_rx_mode = macb_set_rx_mode,
2629 .ndo_set_mac_address = eth_mac_addr,
2630 .ndo_do_ioctl = macb_ioctl,
2631 .ndo_validate_addr = eth_validate_addr,
2632 .ndo_change_mtu = eth_change_mtu,
2633#ifdef CONFIG_NET_POLL_CONTROLLER
2634 .ndo_poll_controller = at91ether_poll_controller,
2635#endif
2636};
2637
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002638static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
2639 struct clk **hclk, struct clk **tx_clk)
2640{
2641 int err;
2642
2643 *hclk = NULL;
2644 *tx_clk = NULL;
2645
2646 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2647 if (IS_ERR(*pclk))
2648 return PTR_ERR(*pclk);
2649
2650 err = clk_prepare_enable(*pclk);
2651 if (err) {
2652 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2653 return err;
2654 }
2655
2656 return 0;
2657}
2658
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002659static int at91ether_init(struct platform_device *pdev)
2660{
2661 struct net_device *dev = platform_get_drvdata(pdev);
2662 struct macb *bp = netdev_priv(dev);
2663 int err;
2664 u32 reg;
2665
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002666 dev->netdev_ops = &at91ether_netdev_ops;
2667 dev->ethtool_ops = &macb_ethtool_ops;
2668
2669 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2670 0, dev->name, dev);
2671 if (err)
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002672 return err;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002673
2674 macb_writel(bp, NCR, 0);
2675
2676 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2677 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2678 reg |= MACB_BIT(RM9200_RMII);
2679
2680 macb_writel(bp, NCFGR, reg);
2681
2682 return 0;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002683}
2684
David S. Miller3cef5c52015-03-09 23:38:02 -04002685static const struct macb_config at91sam9260_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002686 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002687 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002688 .init = macb_init,
2689};
2690
David S. Miller3cef5c52015-03-09 23:38:02 -04002691static const struct macb_config pc302gem_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002692 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2693 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002694 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002695 .init = macb_init,
2696};
2697
David S. Miller3cef5c52015-03-09 23:38:02 -04002698static const struct macb_config sama5d3_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002699 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2700 .dma_burst_length = 16,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002701 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002702 .init = macb_init,
2703};
2704
David S. Miller3cef5c52015-03-09 23:38:02 -04002705static const struct macb_config sama5d4_config = {
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002706 .caps = 0,
2707 .dma_burst_length = 4,
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002708 .clk_init = macb_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002709 .init = macb_init,
2710};
2711
David S. Miller3cef5c52015-03-09 23:38:02 -04002712static const struct macb_config emac_config = {
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002713 .clk_init = at91ether_clk_init,
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002714 .init = at91ether_init,
2715};
2716
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302717static const struct macb_config zynqmp_config = {
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302718 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
2719 MACB_CAPS_JUMBO,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302720 .dma_burst_length = 16,
2721 .clk_init = macb_clk_init,
2722 .init = macb_init,
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302723 .jumbo_max_len = 10240,
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302724};
2725
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002726static const struct of_device_id macb_dt_ids[] = {
2727 { .compatible = "cdns,at32ap7000-macb" },
2728 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2729 { .compatible = "cdns,macb" },
2730 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2731 { .compatible = "cdns,gem", .data = &pc302gem_config },
2732 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2733 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2734 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2735 { .compatible = "cdns,emac", .data = &emac_config },
Harini Katakam7b61f9c2015-05-06 22:27:16 +05302736 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002737 { /* sentinel */ }
2738};
2739MODULE_DEVICE_TABLE(of, macb_dt_ids);
2740#endif /* CONFIG_OF */
2741
2742static int macb_probe(struct platform_device *pdev)
2743{
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002744 int (*clk_init)(struct platform_device *, struct clk **,
2745 struct clk **, struct clk **)
2746 = macb_clk_init;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002747 int (*init)(struct platform_device *) = macb_init;
2748 struct device_node *np = pdev->dev.of_node;
2749 const struct macb_config *macb_config = NULL;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002750 struct clk *pclk, *hclk, *tx_clk;
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002751 unsigned int queue_mask, num_queues;
2752 struct macb_platform_data *pdata;
2753 struct phy_device *phydev;
2754 struct net_device *dev;
2755 struct resource *regs;
2756 void __iomem *mem;
2757 const char *mac;
2758 struct macb *bp;
2759 int err;
2760
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002761 if (np) {
2762 const struct of_device_id *match;
2763
2764 match = of_match_node(macb_dt_ids, np);
2765 if (match && match->data) {
2766 macb_config = match->data;
2767 clk_init = macb_config->clk_init;
2768 init = macb_config->init;
2769 }
2770 }
2771
2772 err = clk_init(pdev, &pclk, &hclk, &tx_clk);
2773 if (err)
2774 return err;
2775
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002776 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2777 mem = devm_ioremap_resource(&pdev->dev, regs);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002778 if (IS_ERR(mem)) {
2779 err = PTR_ERR(mem);
2780 goto err_disable_clocks;
2781 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002782
2783 macb_probe_queues(mem, &queue_mask, &num_queues);
2784 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002785 if (!dev) {
2786 err = -ENOMEM;
2787 goto err_disable_clocks;
2788 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002789
2790 dev->base_addr = regs->start;
2791
2792 SET_NETDEV_DEV(dev, &pdev->dev);
2793
2794 bp = netdev_priv(dev);
2795 bp->pdev = pdev;
2796 bp->dev = dev;
2797 bp->regs = mem;
2798 bp->num_queues = num_queues;
Nicolas Ferrebfa09142015-03-31 15:01:59 +02002799 bp->queue_mask = queue_mask;
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002800 if (macb_config)
2801 bp->dma_burst_length = macb_config->dma_burst_length;
2802 bp->pclk = pclk;
2803 bp->hclk = hclk;
2804 bp->tx_clk = tx_clk;
Harini Katakam98b5a0f42015-05-06 22:27:17 +05302805 if (macb_config->jumbo_max_len) {
2806 bp->jumbo_max_len = macb_config->jumbo_max_len;
2807 }
2808
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002809 spin_lock_init(&bp->lock);
2810
Nicolas Ferread783472015-03-31 15:02:02 +02002811 /* setup capabilities */
Nicolas Ferref6970502015-03-31 15:02:01 +02002812 macb_configure_caps(bp, macb_config);
2813
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002814 platform_set_drvdata(pdev, dev);
2815
2816 dev->irq = platform_get_irq(pdev, 0);
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002817 if (dev->irq < 0) {
2818 err = dev->irq;
2819 goto err_disable_clocks;
2820 }
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002821
2822 mac = of_get_mac_address(np);
Guenter Roeck50907042013-04-02 09:35:09 +00002823 if (mac)
2824 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2825 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002826 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002827
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002828 err = of_get_phy_mode(np);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002829 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002830 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002831 if (pdata && pdata->is_rmii)
2832 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2833 else
2834 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2835 } else {
2836 bp->phy_interface = err;
2837 }
2838
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002839 /* IP specific init */
2840 err = init(pdev);
2841 if (err)
2842 goto err_out_free_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002843
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002844 err = register_netdev(dev);
2845 if (err) {
2846 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002847 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002848 }
2849
Nicolas Ferre72ca8202013-04-14 22:04:33 +00002850 err = macb_mii_init(bp);
2851 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02002852 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002853
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002854 netif_carrier_off(dev);
2855
Bo Shen58798232014-09-13 01:57:49 +02002856 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
2857 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
2858 dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002859
frederic RODO6c36a702007-07-12 19:07:24 +02002860 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002861 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2862 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002863
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002864 return 0;
2865
frederic RODO6c36a702007-07-12 19:07:24 +02002866err_out_unregister_netdev:
2867 unregister_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002868
Cyrille Pitchencf250de2014-12-15 15:13:32 +01002869err_out_free_netdev:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002870 free_netdev(dev);
Cyrille Pitchen421d9df2015-03-07 07:23:32 +01002871
Nicolas Ferrec69618b2015-03-31 15:02:03 +02002872err_disable_clocks:
2873 clk_disable_unprepare(tx_clk);
2874 clk_disable_unprepare(hclk);
2875 clk_disable_unprepare(pclk);
2876
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002877 return err;
2878}
2879
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002880static int macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002881{
2882 struct net_device *dev;
2883 struct macb *bp;
2884
2885 dev = platform_get_drvdata(pdev);
2886
2887 if (dev) {
2888 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002889 if (bp->phy_dev)
2890 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002891 mdiobus_unregister(bp->mii_bus);
2892 kfree(bp->mii_bus->irq);
2893 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002894 unregister_netdev(dev);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002895 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002896 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002897 clk_disable_unprepare(bp->pclk);
Cyrille Pitchene965be72014-12-15 15:13:31 +01002898 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002899 }
2900
2901 return 0;
2902}
2903
Michal Simekd23823d2015-01-23 09:36:03 +01002904static int __maybe_unused macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002905{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002906 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002907 struct net_device *netdev = platform_get_drvdata(pdev);
2908 struct macb *bp = netdev_priv(netdev);
2909
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002910 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002911 netif_device_detach(netdev);
2912
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002913 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002914 clk_disable_unprepare(bp->hclk);
2915 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002916
2917 return 0;
2918}
2919
Michal Simekd23823d2015-01-23 09:36:03 +01002920static int __maybe_unused macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002921{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002922 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002923 struct net_device *netdev = platform_get_drvdata(pdev);
2924 struct macb *bp = netdev_priv(netdev);
2925
Steffen Trumtrarace58012013-03-27 23:07:07 +00002926 clk_prepare_enable(bp->pclk);
2927 clk_prepare_enable(bp->hclk);
Cyrille Pitchen93b31f42015-03-07 07:23:31 +01002928 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002929
2930 netif_device_attach(netdev);
2931
2932 return 0;
2933}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002934
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002935static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
2936
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002937static struct platform_driver macb_driver = {
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002938 .probe = macb_probe,
2939 .remove = macb_remove,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002940 .driver = {
2941 .name = "macb",
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002942 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002943 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002944 },
2945};
2946
Nicolae Rosia9e86d7662015-01-22 17:31:05 +00002947module_platform_driver(macb_driver);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002948
2949MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00002950MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002951MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07002952MODULE_ALIAS("platform:macb");