blob: e1e02fba4fcc55e663dfc31777f281f26463896c [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000021#include <linux/gpio.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000026#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020028#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080029#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010030#include <linux/of_device.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010033
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010034#include "macb.h"
35
Nicolas Ferre1b447912013-06-04 21:57:11 +000036#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000037#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000038#define RX_RING_SIZE 512 /* must be power of 2 */
39#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010040
Havard Skinnemoen55054a12012-10-31 06:04:55 +000041#define TX_RING_SIZE 128 /* must be power of 2 */
42#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010043
Nicolas Ferre909a8582012-11-19 06:00:21 +000044/* level of occupied TX descriptors under which we wake up TX process */
45#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010046
47#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
48 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000049#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
50 | MACB_BIT(ISR_RLE) \
51 | MACB_BIT(TXERR))
52#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
53
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +020054#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
55#define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
56
Nicolas Ferree86cd532012-10-31 06:04:57 +000057/*
58 * Graceful stop timeouts in us. We should allow up to
59 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
60 */
61#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010062
Havard Skinnemoen55054a12012-10-31 06:04:55 +000063/* Ring buffer accessors */
64static unsigned int macb_tx_ring_wrap(unsigned int index)
65{
66 return index & (TX_RING_SIZE - 1);
67}
68
Havard Skinnemoen55054a12012-10-31 06:04:55 +000069static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
70{
71 return &bp->tx_ring[macb_tx_ring_wrap(index)];
72}
73
74static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
75{
76 return &bp->tx_skb[macb_tx_ring_wrap(index)];
77}
78
79static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
80{
81 dma_addr_t offset;
82
83 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
84
85 return bp->tx_ring_dma + offset;
86}
87
88static unsigned int macb_rx_ring_wrap(unsigned int index)
89{
90 return index & (RX_RING_SIZE - 1);
91}
92
93static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
94{
95 return &bp->rx_ring[macb_rx_ring_wrap(index)];
96}
97
98static void *macb_rx_buffer(struct macb *bp, unsigned int index)
99{
Nicolas Ferre1b447912013-06-04 21:57:11 +0000100 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000101}
102
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000103void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100104{
105 u32 bottom;
106 u16 top;
107
108 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000109 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100110 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000111 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000112
113 /* Clear unused address register sets */
114 macb_or_gem_writel(bp, SA2B, 0);
115 macb_or_gem_writel(bp, SA2T, 0);
116 macb_or_gem_writel(bp, SA3B, 0);
117 macb_or_gem_writel(bp, SA3T, 0);
118 macb_or_gem_writel(bp, SA4B, 0);
119 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100120}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000121EXPORT_SYMBOL_GPL(macb_set_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100122
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000123void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100124{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000125 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100126 u32 bottom;
127 u16 top;
128 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000129 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100130
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900131 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000132
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000133 /* Check all 4 address register for vaild address */
134 for (i = 0; i < 4; i++) {
135 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
136 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100137
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000138 if (pdata && pdata->rev_eth_addr) {
139 addr[5] = bottom & 0xff;
140 addr[4] = (bottom >> 8) & 0xff;
141 addr[3] = (bottom >> 16) & 0xff;
142 addr[2] = (bottom >> 24) & 0xff;
143 addr[1] = top & 0xff;
144 addr[0] = (top & 0xff00) >> 8;
145 } else {
146 addr[0] = bottom & 0xff;
147 addr[1] = (bottom >> 8) & 0xff;
148 addr[2] = (bottom >> 16) & 0xff;
149 addr[3] = (bottom >> 24) & 0xff;
150 addr[4] = top & 0xff;
151 addr[5] = (top >> 8) & 0xff;
152 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100153
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000154 if (is_valid_ether_addr(addr)) {
155 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
156 return;
157 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700158 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000159
160 netdev_info(bp->dev, "invalid hw address, using random\n");
161 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100162}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000163EXPORT_SYMBOL_GPL(macb_get_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100164
frederic RODO6c36a702007-07-12 19:07:24 +0200165static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100166{
frederic RODO6c36a702007-07-12 19:07:24 +0200167 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100168 int value;
169
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100170 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
171 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200172 | MACB_BF(PHYA, mii_id)
173 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100174 | MACB_BF(CODE, MACB_MAN_CODE)));
175
frederic RODO6c36a702007-07-12 19:07:24 +0200176 /* wait for end of transfer */
177 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
178 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100179
180 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100181
182 return value;
183}
184
frederic RODO6c36a702007-07-12 19:07:24 +0200185static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
186 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100187{
frederic RODO6c36a702007-07-12 19:07:24 +0200188 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100189
190 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
191 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200192 | MACB_BF(PHYA, mii_id)
193 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100194 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200195 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100196
frederic RODO6c36a702007-07-12 19:07:24 +0200197 /* wait for end of transfer */
198 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
199 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100200
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100201 return 0;
202}
203
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800204/**
205 * macb_set_tx_clk() - Set a clock to a new frequency
206 * @clk Pointer to the clock to change
207 * @rate New frequency in Hz
208 * @dev Pointer to the struct net_device
209 */
210static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
211{
212 long ferr, rate, rate_rounded;
213
214 switch (speed) {
215 case SPEED_10:
216 rate = 2500000;
217 break;
218 case SPEED_100:
219 rate = 25000000;
220 break;
221 case SPEED_1000:
222 rate = 125000000;
223 break;
224 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800225 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800226 }
227
228 rate_rounded = clk_round_rate(clk, rate);
229 if (rate_rounded < 0)
230 return;
231
232 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
233 * is not satisfied.
234 */
235 ferr = abs(rate_rounded - rate);
236 ferr = DIV_ROUND_UP(ferr, rate / 100000);
237 if (ferr > 5)
238 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
239 rate);
240
241 if (clk_set_rate(clk, rate_rounded))
242 netdev_err(dev, "adjusting tx_clk failed.\n");
243}
244
frederic RODO6c36a702007-07-12 19:07:24 +0200245static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100246{
frederic RODO6c36a702007-07-12 19:07:24 +0200247 struct macb *bp = netdev_priv(dev);
248 struct phy_device *phydev = bp->phy_dev;
249 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100250
frederic RODO6c36a702007-07-12 19:07:24 +0200251 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100252
frederic RODO6c36a702007-07-12 19:07:24 +0200253 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100254
frederic RODO6c36a702007-07-12 19:07:24 +0200255 if (phydev->link) {
256 if ((bp->speed != phydev->speed) ||
257 (bp->duplex != phydev->duplex)) {
258 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100259
frederic RODO6c36a702007-07-12 19:07:24 +0200260 reg = macb_readl(bp, NCFGR);
261 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000262 if (macb_is_gem(bp))
263 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200264
265 if (phydev->duplex)
266 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900267 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200268 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200269 if (phydev->speed == SPEED_1000 &&
270 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000271 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200272
Patrice Vilchez140b7552012-10-31 06:04:50 +0000273 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200274
275 bp->speed = phydev->speed;
276 bp->duplex = phydev->duplex;
277 status_change = 1;
278 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100279 }
280
frederic RODO6c36a702007-07-12 19:07:24 +0200281 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700282 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200283 bp->speed = 0;
284 bp->duplex = -1;
285 }
286 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100287
frederic RODO6c36a702007-07-12 19:07:24 +0200288 status_change = 1;
289 }
290
291 spin_unlock_irqrestore(&bp->lock, flags);
292
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800293 if (!IS_ERR(bp->tx_clk))
294 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
295
frederic RODO6c36a702007-07-12 19:07:24 +0200296 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000297 if (phydev->link) {
298 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000299 netdev_info(dev, "link up (%d/%s)\n",
300 phydev->speed,
301 phydev->duplex == DUPLEX_FULL ?
302 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000303 } else {
304 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000305 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000306 }
frederic RODO6c36a702007-07-12 19:07:24 +0200307 }
308}
309
310/* based on au1000_eth. c*/
311static int macb_mii_probe(struct net_device *dev)
312{
313 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000314 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000315 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000316 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000317 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200318
Jiri Pirko7455a762010-02-08 05:12:08 +0000319 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200320 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000321 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200322 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200323 }
324
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000325 pdata = dev_get_platdata(&bp->pdev->dev);
326 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
327 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
328 if (!ret) {
329 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
330 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
331 }
332 }
frederic RODO6c36a702007-07-12 19:07:24 +0200333
334 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000335 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100336 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000337 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000338 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000339 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200340 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100341
frederic RODO6c36a702007-07-12 19:07:24 +0200342 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200343 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000344 phydev->supported &= PHY_GBIT_FEATURES;
345 else
346 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100347
frederic RODO6c36a702007-07-12 19:07:24 +0200348 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100349
frederic RODO6c36a702007-07-12 19:07:24 +0200350 bp->link = 0;
351 bp->speed = 0;
352 bp->duplex = -1;
353 bp->phy_dev = phydev;
354
355 return 0;
356}
357
Joachim Eastwood0005f542012-10-18 11:01:12 +0000358int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200359{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000360 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200361 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200362 int err = -ENXIO, i;
363
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200364 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200365 macb_writel(bp, NCR, MACB_BIT(MPE));
366
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700367 bp->mii_bus = mdiobus_alloc();
368 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200369 err = -ENOMEM;
370 goto err_out;
371 }
372
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700373 bp->mii_bus->name = "MACB_mii_bus";
374 bp->mii_bus->read = &macb_mdio_read;
375 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000376 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
377 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700378 bp->mii_bus->priv = bp;
379 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900380 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700381
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700382 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
383 if (!bp->mii_bus->irq) {
384 err = -ENOMEM;
385 goto err_out_free_mdiobus;
386 }
387
Jamie Iles91523942011-02-28 04:05:25 +0000388 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200389
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200390 np = bp->pdev->dev.of_node;
391 if (np) {
392 /* try dt phy registration */
393 err = of_mdiobus_register(bp->mii_bus, np);
394
395 /* fallback to standard phy registration if no phy were
396 found during dt phy registration */
397 if (!err && !phy_find_first(bp->mii_bus)) {
398 for (i = 0; i < PHY_MAX_ADDR; i++) {
399 struct phy_device *phydev;
400
401 phydev = mdiobus_scan(bp->mii_bus, i);
402 if (IS_ERR(phydev)) {
403 err = PTR_ERR(phydev);
404 break;
405 }
406 }
407
408 if (err)
409 goto err_out_unregister_bus;
410 }
411 } else {
412 for (i = 0; i < PHY_MAX_ADDR; i++)
413 bp->mii_bus->irq[i] = PHY_POLL;
414
415 if (pdata)
416 bp->mii_bus->phy_mask = pdata->phy_mask;
417
418 err = mdiobus_register(bp->mii_bus);
419 }
420
421 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200422 goto err_out_free_mdio_irq;
423
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200424 err = macb_mii_probe(bp->dev);
425 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200426 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200427
428 return 0;
429
430err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700431 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200432err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700433 kfree(bp->mii_bus->irq);
434err_out_free_mdiobus:
435 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200436err_out:
437 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000439EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100440
441static void macb_update_stats(struct macb *bp)
442{
443 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000444 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
445 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100446
447 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
448
449 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100450 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100451}
452
Nicolas Ferree86cd532012-10-31 06:04:57 +0000453static int macb_halt_tx(struct macb *bp)
454{
455 unsigned long halt_time, timeout;
456 u32 status;
457
458 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
459
460 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
461 do {
462 halt_time = jiffies;
463 status = macb_readl(bp, TSR);
464 if (!(status & MACB_BIT(TGO)))
465 return 0;
466
467 usleep_range(10, 250);
468 } while (time_before(halt_time, timeout));
469
470 return -ETIMEDOUT;
471}
472
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200473static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
474{
475 if (tx_skb->mapping) {
476 if (tx_skb->mapped_as_page)
477 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
478 tx_skb->size, DMA_TO_DEVICE);
479 else
480 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
481 tx_skb->size, DMA_TO_DEVICE);
482 tx_skb->mapping = 0;
483 }
484
485 if (tx_skb->skb) {
486 dev_kfree_skb_any(tx_skb->skb);
487 tx_skb->skb = NULL;
488 }
489}
490
Nicolas Ferree86cd532012-10-31 06:04:57 +0000491static void macb_tx_error_task(struct work_struct *work)
492{
493 struct macb *bp = container_of(work, struct macb, tx_error_task);
494 struct macb_tx_skb *tx_skb;
495 struct sk_buff *skb;
496 unsigned int tail;
497
498 netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
499 bp->tx_tail, bp->tx_head);
500
501 /* Make sure nobody is trying to queue up new packets */
502 netif_stop_queue(bp->dev);
503
504 /*
505 * Stop transmission now
506 * (in case we have just queued new packets)
507 */
508 if (macb_halt_tx(bp))
509 /* Just complain for now, reinitializing TX path can be good */
510 netdev_err(bp->dev, "BUG: halt tx timed out\n");
511
512 /* No need for the lock here as nobody will interrupt us anymore */
513
514 /*
515 * Treat frames in TX queue including the ones that caused the error.
516 * Free transmit buffers in upper layer.
517 */
518 for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
519 struct macb_dma_desc *desc;
520 u32 ctrl;
521
522 desc = macb_tx_desc(bp, tail);
523 ctrl = desc->ctrl;
524 tx_skb = macb_tx_skb(bp, tail);
525 skb = tx_skb->skb;
526
527 if (ctrl & MACB_BIT(TX_USED)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200528 /* skb is set for the last buffer of the frame */
529 while (!skb) {
530 macb_tx_unmap(bp, tx_skb);
531 tail++;
532 tx_skb = macb_tx_skb(bp, tail);
533 skb = tx_skb->skb;
534 }
535
536 /* ctrl still refers to the first buffer descriptor
537 * since it's the only one written back by the hardware
538 */
539 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
540 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
541 macb_tx_ring_wrap(tail), skb->data);
542 bp->stats.tx_packets++;
543 bp->stats.tx_bytes += skb->len;
544 }
Nicolas Ferree86cd532012-10-31 06:04:57 +0000545 } else {
546 /*
547 * "Buffers exhausted mid-frame" errors may only happen
548 * if the driver is buggy, so complain loudly about those.
549 * Statistics are updated by hardware.
550 */
551 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
552 netdev_err(bp->dev,
553 "BUG: TX buffers exhausted mid-frame\n");
554
555 desc->ctrl = ctrl | MACB_BIT(TX_USED);
556 }
557
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200558 macb_tx_unmap(bp, tx_skb);
Nicolas Ferree86cd532012-10-31 06:04:57 +0000559 }
560
561 /* Make descriptor updates visible to hardware */
562 wmb();
563
564 /* Reinitialize the TX desc queue */
565 macb_writel(bp, TBQP, bp->tx_ring_dma);
566 /* Make TX ring reflect state of hardware */
567 bp->tx_head = bp->tx_tail = 0;
568
569 /* Now we are ready to start transmission again */
570 netif_wake_queue(bp->dev);
571
572 /* Housework before enabling TX IRQ */
573 macb_writel(bp, TSR, macb_readl(bp, TSR));
574 macb_writel(bp, IER, MACB_TX_INT_FLAGS);
575}
576
577static void macb_tx_interrupt(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100578{
579 unsigned int tail;
580 unsigned int head;
581 u32 status;
582
583 status = macb_readl(bp, TSR);
584 macb_writel(bp, TSR, status);
585
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000586 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
587 macb_writel(bp, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000588
Nicolas Ferree86cd532012-10-31 06:04:57 +0000589 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
590 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100591
592 head = bp->tx_head;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000593 for (tail = bp->tx_tail; tail != head; tail++) {
594 struct macb_tx_skb *tx_skb;
595 struct sk_buff *skb;
596 struct macb_dma_desc *desc;
597 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100598
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000599 desc = macb_tx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100600
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000601 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100602 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000603
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000604 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100605
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200606 /* TX_USED bit is only set by hardware on the very first buffer
607 * descriptor of the transmitted frame.
608 */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000609 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100610 break;
611
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200612 /* Process all buffers of the current transmitted frame */
613 for (;; tail++) {
614 tx_skb = macb_tx_skb(bp, tail);
615 skb = tx_skb->skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000616
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +0200617 /* First, update TX stats if needed */
618 if (skb) {
619 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
620 macb_tx_ring_wrap(tail), skb->data);
621 bp->stats.tx_packets++;
622 bp->stats.tx_bytes += skb->len;
623 }
624
625 /* Now we can safely release resources */
626 macb_tx_unmap(bp, tx_skb);
627
628 /* skb is set only for the last buffer of the frame.
629 * WARNING: at this point skb has been freed by
630 * macb_tx_unmap().
631 */
632 if (skb)
633 break;
634 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100635 }
636
637 bp->tx_tail = tail;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000638 if (netif_queue_stopped(bp->dev)
Nicolas Ferre909a8582012-11-19 06:00:21 +0000639 && CIRC_CNT(bp->tx_head, bp->tx_tail,
640 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100641 netif_wake_queue(bp->dev);
642}
643
Nicolas Ferre4df95132013-06-04 21:57:12 +0000644static void gem_rx_refill(struct macb *bp)
645{
646 unsigned int entry;
647 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000648 dma_addr_t paddr;
649
650 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000651 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000652
653 /* Make hw descriptor updates visible to CPU */
654 rmb();
655
Nicolas Ferre4df95132013-06-04 21:57:12 +0000656 bp->rx_prepared_head++;
657
Nicolas Ferre4df95132013-06-04 21:57:12 +0000658 if (bp->rx_skbuff[entry] == NULL) {
659 /* allocate sk_buff for this free entry in ring */
660 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
661 if (unlikely(skb == NULL)) {
662 netdev_err(bp->dev,
663 "Unable to allocate sk_buff\n");
664 break;
665 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000666
667 /* now fill corresponding descriptor entry */
668 paddr = dma_map_single(&bp->pdev->dev, skb->data,
669 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800670 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
671 dev_kfree_skb(skb);
672 break;
673 }
674
675 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000676
677 if (entry == RX_RING_SIZE - 1)
678 paddr |= MACB_BIT(RX_WRAP);
679 bp->rx_ring[entry].addr = paddr;
680 bp->rx_ring[entry].ctrl = 0;
681
682 /* properly align Ethernet header */
683 skb_reserve(skb, NET_IP_ALIGN);
684 }
685 }
686
687 /* Make descriptor updates visible to hardware */
688 wmb();
689
690 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
691 bp->rx_prepared_head, bp->rx_tail);
692}
693
694/* Mark DMA descriptors from begin up to and not including end as unused */
695static void discard_partial_frame(struct macb *bp, unsigned int begin,
696 unsigned int end)
697{
698 unsigned int frag;
699
700 for (frag = begin; frag != end; frag++) {
701 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
702 desc->addr &= ~MACB_BIT(RX_USED);
703 }
704
705 /* Make descriptor updates visible to hardware */
706 wmb();
707
708 /*
709 * When this happens, the hardware stats registers for
710 * whatever caused this is updated, so we don't have to record
711 * anything.
712 */
713}
714
715static int gem_rx(struct macb *bp, int budget)
716{
717 unsigned int len;
718 unsigned int entry;
719 struct sk_buff *skb;
720 struct macb_dma_desc *desc;
721 int count = 0;
722
723 while (count < budget) {
724 u32 addr, ctrl;
725
726 entry = macb_rx_ring_wrap(bp->rx_tail);
727 desc = &bp->rx_ring[entry];
728
729 /* Make hw descriptor updates visible to CPU */
730 rmb();
731
732 addr = desc->addr;
733 ctrl = desc->ctrl;
734
735 if (!(addr & MACB_BIT(RX_USED)))
736 break;
737
Nicolas Ferre4df95132013-06-04 21:57:12 +0000738 bp->rx_tail++;
739 count++;
740
741 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
742 netdev_err(bp->dev,
743 "not whole frame pointed by descriptor\n");
744 bp->stats.rx_dropped++;
745 break;
746 }
747 skb = bp->rx_skbuff[entry];
748 if (unlikely(!skb)) {
749 netdev_err(bp->dev,
750 "inconsistent Rx descriptor chain\n");
751 bp->stats.rx_dropped++;
752 break;
753 }
754 /* now everything is ready for receiving packet */
755 bp->rx_skbuff[entry] = NULL;
756 len = MACB_BFEXT(RX_FRMLEN, ctrl);
757
758 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
759
760 skb_put(skb, len);
761 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
762 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800763 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000764
765 skb->protocol = eth_type_trans(skb, bp->dev);
766 skb_checksum_none_assert(skb);
Cyrille Pitchen924ec532014-07-24 13:51:01 +0200767 if (bp->dev->features & NETIF_F_RXCSUM &&
768 !(bp->dev->flags & IFF_PROMISC) &&
769 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
770 skb->ip_summed = CHECKSUM_UNNECESSARY;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000771
772 bp->stats.rx_packets++;
773 bp->stats.rx_bytes += skb->len;
774
775#if defined(DEBUG) && defined(VERBOSE_DEBUG)
776 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
777 skb->len, skb->csum);
778 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
779 skb->mac_header, 16, true);
780 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
781 skb->data, 32, true);
782#endif
783
784 netif_receive_skb(skb);
785 }
786
787 gem_rx_refill(bp);
788
789 return count;
790}
791
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100792static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
793 unsigned int last_frag)
794{
795 unsigned int len;
796 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000797 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100798 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000799 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100800
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000801 desc = macb_rx_desc(bp, last_frag);
802 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100803
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000804 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000805 macb_rx_ring_wrap(first_frag),
806 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100807
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000808 /*
809 * The ethernet header starts NET_IP_ALIGN bytes into the
810 * first buffer. Since the header is 14 bytes, this makes the
811 * payload word-aligned.
812 *
813 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
814 * the two padding bytes into the skb so that we avoid hitting
815 * the slowpath in memcpy(), and pull them off afterwards.
816 */
817 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100818 if (!skb) {
819 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000820 for (frag = first_frag; ; frag++) {
821 desc = macb_rx_desc(bp, frag);
822 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100823 if (frag == last_frag)
824 break;
825 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000826
827 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100828 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000829
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100830 return 1;
831 }
832
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000833 offset = 0;
834 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700835 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100836 skb_put(skb, len);
837
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000838 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000839 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100840
841 if (offset + frag_len > len) {
842 BUG_ON(frag != last_frag);
843 frag_len = len - offset;
844 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300845 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000846 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000847 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000848 desc = macb_rx_desc(bp, frag);
849 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100850
851 if (frag == last_frag)
852 break;
853 }
854
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000855 /* Make descriptor updates visible to hardware */
856 wmb();
857
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000858 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100859 skb->protocol = eth_type_trans(skb, bp->dev);
860
861 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000862 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000863 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000864 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100865 netif_receive_skb(skb);
866
867 return 0;
868}
869
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100870static int macb_rx(struct macb *bp, int budget)
871{
872 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000873 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100874 int first_frag = -1;
875
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000876 for (tail = bp->rx_tail; budget > 0; tail++) {
877 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100878 u32 addr, ctrl;
879
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000880 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100881 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000882
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000883 addr = desc->addr;
884 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100885
886 if (!(addr & MACB_BIT(RX_USED)))
887 break;
888
889 if (ctrl & MACB_BIT(RX_SOF)) {
890 if (first_frag != -1)
891 discard_partial_frame(bp, first_frag, tail);
892 first_frag = tail;
893 }
894
895 if (ctrl & MACB_BIT(RX_EOF)) {
896 int dropped;
897 BUG_ON(first_frag == -1);
898
899 dropped = macb_rx_frame(bp, first_frag, tail);
900 first_frag = -1;
901 if (!dropped) {
902 received++;
903 budget--;
904 }
905 }
906 }
907
908 if (first_frag != -1)
909 bp->rx_tail = first_frag;
910 else
911 bp->rx_tail = tail;
912
913 return received;
914}
915
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700916static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100917{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700918 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700919 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100920 u32 status;
921
922 status = macb_readl(bp, RSR);
923 macb_writel(bp, RSR, status);
924
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700925 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100926
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000927 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000928 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100929
Nicolas Ferre4df95132013-06-04 21:57:12 +0000930 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000931 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800932 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100933
Nicolas Ferre8770e912013-02-12 11:08:48 +0100934 /* Packets received while interrupts were disabled */
935 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -0700936 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700937 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
938 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +0100939 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700940 } else {
941 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
942 }
Joshua Hokeb3363692010-10-25 01:44:22 +0000943 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100944
945 /* TODO: Handle errors */
946
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700947 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100948}
949
950static irqreturn_t macb_interrupt(int irq, void *dev_id)
951{
952 struct net_device *dev = dev_id;
953 struct macb *bp = netdev_priv(dev);
954 u32 status;
955
956 status = macb_readl(bp, ISR);
957
958 if (unlikely(!status))
959 return IRQ_NONE;
960
961 spin_lock(&bp->lock);
962
963 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100964 /* close possible race with dev_close */
965 if (unlikely(!netif_running(dev))) {
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000966 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100967 break;
968 }
969
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000970 netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
971
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100972 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000973 /*
974 * There's no point taking any more interrupts
975 * until we have processed the buffers. The
976 * scheduling call may fail if the poll routine
977 * is already scheduled, so disable interrupts
978 * now.
979 */
980 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000981 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
982 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +0000983
Ben Hutchings288379f2009-01-19 16:43:59 -0800984 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000985 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800986 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100987 }
988 }
989
Nicolas Ferree86cd532012-10-31 06:04:57 +0000990 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
991 macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
992 schedule_work(&bp->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -0700993
994 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
995 macb_writel(bp, ISR, MACB_TX_ERR_FLAGS);
996
Nicolas Ferree86cd532012-10-31 06:04:57 +0000997 break;
998 }
999
1000 if (status & MACB_BIT(TCOMP))
1001 macb_tx_interrupt(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001002
1003 /*
1004 * Link change detection isn't possible with RMII, so we'll
1005 * add that if/when we get our hands on a full-blown MII PHY.
1006 */
1007
Alexander Steinb19f7f72011-04-13 05:03:24 +00001008 if (status & MACB_BIT(ISR_ROVR)) {
1009 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001010 if (macb_is_gem(bp))
1011 bp->hw_stats.gem.rx_overruns++;
1012 else
1013 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001014
1015 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1016 macb_writel(bp, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +00001017 }
1018
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001019 if (status & MACB_BIT(HRESP)) {
1020 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001021 * TODO: Reset the hardware, and maybe move the
1022 * netdev_err to a lower-priority context as well
1023 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001024 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001025 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -07001026
1027 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1028 macb_writel(bp, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001029 }
1030
1031 status = macb_readl(bp, ISR);
1032 }
1033
1034 spin_unlock(&bp->lock);
1035
1036 return IRQ_HANDLED;
1037}
1038
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001039#ifdef CONFIG_NET_POLL_CONTROLLER
1040/*
1041 * Polling receive - used by netconsole and other diagnostic tools
1042 * to allow network i/o with interrupts disabled.
1043 */
1044static void macb_poll_controller(struct net_device *dev)
1045{
1046 unsigned long flags;
1047
1048 local_irq_save(flags);
1049 macb_interrupt(dev->irq, dev);
1050 local_irq_restore(flags);
1051}
1052#endif
1053
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001054static inline unsigned int macb_count_tx_descriptors(struct macb *bp,
1055 unsigned int len)
1056{
1057 return (len + bp->max_tx_length - 1) / bp->max_tx_length;
1058}
1059
1060static unsigned int macb_tx_map(struct macb *bp,
1061 struct sk_buff *skb)
1062{
1063 dma_addr_t mapping;
1064 unsigned int len, entry, i, tx_head = bp->tx_head;
1065 struct macb_tx_skb *tx_skb = NULL;
1066 struct macb_dma_desc *desc;
1067 unsigned int offset, size, count = 0;
1068 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1069 unsigned int eof = 1;
1070 u32 ctrl;
1071
1072 /* First, map non-paged data */
1073 len = skb_headlen(skb);
1074 offset = 0;
1075 while (len) {
1076 size = min(len, bp->max_tx_length);
1077 entry = macb_tx_ring_wrap(tx_head);
1078 tx_skb = &bp->tx_skb[entry];
1079
1080 mapping = dma_map_single(&bp->pdev->dev,
1081 skb->data + offset,
1082 size, DMA_TO_DEVICE);
1083 if (dma_mapping_error(&bp->pdev->dev, mapping))
1084 goto dma_error;
1085
1086 /* Save info to properly release resources */
1087 tx_skb->skb = NULL;
1088 tx_skb->mapping = mapping;
1089 tx_skb->size = size;
1090 tx_skb->mapped_as_page = false;
1091
1092 len -= size;
1093 offset += size;
1094 count++;
1095 tx_head++;
1096 }
1097
1098 /* Then, map paged data from fragments */
1099 for (f = 0; f < nr_frags; f++) {
1100 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1101
1102 len = skb_frag_size(frag);
1103 offset = 0;
1104 while (len) {
1105 size = min(len, bp->max_tx_length);
1106 entry = macb_tx_ring_wrap(tx_head);
1107 tx_skb = &bp->tx_skb[entry];
1108
1109 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1110 offset, size, DMA_TO_DEVICE);
1111 if (dma_mapping_error(&bp->pdev->dev, mapping))
1112 goto dma_error;
1113
1114 /* Save info to properly release resources */
1115 tx_skb->skb = NULL;
1116 tx_skb->mapping = mapping;
1117 tx_skb->size = size;
1118 tx_skb->mapped_as_page = true;
1119
1120 len -= size;
1121 offset += size;
1122 count++;
1123 tx_head++;
1124 }
1125 }
1126
1127 /* Should never happen */
1128 if (unlikely(tx_skb == NULL)) {
1129 netdev_err(bp->dev, "BUG! empty skb!\n");
1130 return 0;
1131 }
1132
1133 /* This is the last buffer of the frame: save socket buffer */
1134 tx_skb->skb = skb;
1135
1136 /* Update TX ring: update buffer descriptors in reverse order
1137 * to avoid race condition
1138 */
1139
1140 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1141 * to set the end of TX queue
1142 */
1143 i = tx_head;
1144 entry = macb_tx_ring_wrap(i);
1145 ctrl = MACB_BIT(TX_USED);
1146 desc = &bp->tx_ring[entry];
1147 desc->ctrl = ctrl;
1148
1149 do {
1150 i--;
1151 entry = macb_tx_ring_wrap(i);
1152 tx_skb = &bp->tx_skb[entry];
1153 desc = &bp->tx_ring[entry];
1154
1155 ctrl = (u32)tx_skb->size;
1156 if (eof) {
1157 ctrl |= MACB_BIT(TX_LAST);
1158 eof = 0;
1159 }
1160 if (unlikely(entry == (TX_RING_SIZE - 1)))
1161 ctrl |= MACB_BIT(TX_WRAP);
1162
1163 /* Set TX buffer descriptor */
1164 desc->addr = tx_skb->mapping;
1165 /* desc->addr must be visible to hardware before clearing
1166 * 'TX_USED' bit in desc->ctrl.
1167 */
1168 wmb();
1169 desc->ctrl = ctrl;
1170 } while (i != bp->tx_head);
1171
1172 bp->tx_head = tx_head;
1173
1174 return count;
1175
1176dma_error:
1177 netdev_err(bp->dev, "TX DMA map failed\n");
1178
1179 for (i = bp->tx_head; i != tx_head; i++) {
1180 tx_skb = macb_tx_skb(bp, i);
1181
1182 macb_tx_unmap(bp, tx_skb);
1183 }
1184
1185 return 0;
1186}
1187
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001188static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1189{
1190 struct macb *bp = netdev_priv(dev);
Dongdong Deng48719532009-08-23 19:49:07 -07001191 unsigned long flags;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001192 unsigned int count, nr_frags, frag_size, f;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001193
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001194#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1195 netdev_vdbg(bp->dev,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001196 "start_xmit: len %u head %p data %p tail %p end %p\n",
1197 skb->len, skb->head, skb->data,
1198 skb_tail_pointer(skb), skb_end_pointer(skb));
1199 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1200 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001201#endif
1202
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001203 /* Count how many TX buffer descriptors are needed to send this
1204 * socket buffer: skb fragments of jumbo frames may need to be
1205 * splitted into many buffer descriptors.
1206 */
1207 count = macb_count_tx_descriptors(bp, skb_headlen(skb));
1208 nr_frags = skb_shinfo(skb)->nr_frags;
1209 for (f = 0; f < nr_frags; f++) {
1210 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1211 count += macb_count_tx_descriptors(bp, frag_size);
1212 }
1213
Dongdong Deng48719532009-08-23 19:49:07 -07001214 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001215
1216 /* This is a hard error, log it. */
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001217 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < count) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001218 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -07001219 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001220 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
1221 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001222 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001223 }
1224
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001225 /* Map socket buffer for DMA transfer */
1226 if (!macb_tx_map(bp, skb)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001227 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001228 goto unlock;
1229 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001230
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001231 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001232 wmb();
1233
Richard Cochrane0720922011-06-19 21:51:28 +00001234 skb_tx_timestamp(skb);
1235
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001236 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1237
Nicolas Ferre909a8582012-11-19 06:00:21 +00001238 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001239 netif_stop_queue(dev);
1240
Soren Brinkmann92030902014-03-04 08:46:39 -08001241unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001242 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001243
Patrick McHardy6ed10652009-06-23 06:03:08 +00001244 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001245}
1246
Nicolas Ferre4df95132013-06-04 21:57:12 +00001247static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001248{
1249 if (!macb_is_gem(bp)) {
1250 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1251 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001252 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001253
Nicolas Ferre1b447912013-06-04 21:57:11 +00001254 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001255 netdev_dbg(bp->dev,
1256 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001257 RX_BUFFER_MULTIPLE);
1258 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001259 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001260 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001261 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001262
1263 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1264 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001265}
1266
Nicolas Ferre4df95132013-06-04 21:57:12 +00001267static void gem_free_rx_buffers(struct macb *bp)
1268{
1269 struct sk_buff *skb;
1270 struct macb_dma_desc *desc;
1271 dma_addr_t addr;
1272 int i;
1273
1274 if (!bp->rx_skbuff)
1275 return;
1276
1277 for (i = 0; i < RX_RING_SIZE; i++) {
1278 skb = bp->rx_skbuff[i];
1279
1280 if (skb == NULL)
1281 continue;
1282
1283 desc = &bp->rx_ring[i];
1284 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001285 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001286 DMA_FROM_DEVICE);
1287 dev_kfree_skb_any(skb);
1288 skb = NULL;
1289 }
1290
1291 kfree(bp->rx_skbuff);
1292 bp->rx_skbuff = NULL;
1293}
1294
1295static void macb_free_rx_buffers(struct macb *bp)
1296{
1297 if (bp->rx_buffers) {
1298 dma_free_coherent(&bp->pdev->dev,
1299 RX_RING_SIZE * bp->rx_buffer_size,
1300 bp->rx_buffers, bp->rx_buffers_dma);
1301 bp->rx_buffers = NULL;
1302 }
1303}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001304
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001305static void macb_free_consistent(struct macb *bp)
1306{
1307 if (bp->tx_skb) {
1308 kfree(bp->tx_skb);
1309 bp->tx_skb = NULL;
1310 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001311 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001312 if (bp->rx_ring) {
1313 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1314 bp->rx_ring, bp->rx_ring_dma);
1315 bp->rx_ring = NULL;
1316 }
1317 if (bp->tx_ring) {
1318 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1319 bp->tx_ring, bp->tx_ring_dma);
1320 bp->tx_ring = NULL;
1321 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001322}
1323
1324static int gem_alloc_rx_buffers(struct macb *bp)
1325{
1326 int size;
1327
1328 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1329 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1330 if (!bp->rx_skbuff)
1331 return -ENOMEM;
1332 else
1333 netdev_dbg(bp->dev,
1334 "Allocated %d RX struct sk_buff entries at %p\n",
1335 RX_RING_SIZE, bp->rx_skbuff);
1336 return 0;
1337}
1338
1339static int macb_alloc_rx_buffers(struct macb *bp)
1340{
1341 int size;
1342
1343 size = RX_RING_SIZE * bp->rx_buffer_size;
1344 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1345 &bp->rx_buffers_dma, GFP_KERNEL);
1346 if (!bp->rx_buffers)
1347 return -ENOMEM;
1348 else
1349 netdev_dbg(bp->dev,
1350 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1351 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1352 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001353}
1354
1355static int macb_alloc_consistent(struct macb *bp)
1356{
1357 int size;
1358
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001359 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001360 bp->tx_skb = kmalloc(size, GFP_KERNEL);
1361 if (!bp->tx_skb)
1362 goto out_err;
1363
1364 size = RX_RING_BYTES;
1365 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1366 &bp->rx_ring_dma, GFP_KERNEL);
1367 if (!bp->rx_ring)
1368 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001369 netdev_dbg(bp->dev,
1370 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1371 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001372
1373 size = TX_RING_BYTES;
1374 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1375 &bp->tx_ring_dma, GFP_KERNEL);
1376 if (!bp->tx_ring)
1377 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001378 netdev_dbg(bp->dev,
1379 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
1380 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001381
Nicolas Ferre4df95132013-06-04 21:57:12 +00001382 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001383 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001384
1385 return 0;
1386
1387out_err:
1388 macb_free_consistent(bp);
1389 return -ENOMEM;
1390}
1391
Nicolas Ferre4df95132013-06-04 21:57:12 +00001392static void gem_init_rings(struct macb *bp)
1393{
1394 int i;
1395
1396 for (i = 0; i < TX_RING_SIZE; i++) {
1397 bp->tx_ring[i].addr = 0;
1398 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1399 }
1400 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1401
1402 bp->rx_tail = bp->rx_prepared_head = bp->tx_head = bp->tx_tail = 0;
1403
1404 gem_rx_refill(bp);
1405}
1406
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001407static void macb_init_rings(struct macb *bp)
1408{
1409 int i;
1410 dma_addr_t addr;
1411
1412 addr = bp->rx_buffers_dma;
1413 for (i = 0; i < RX_RING_SIZE; i++) {
1414 bp->rx_ring[i].addr = addr;
1415 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001416 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001417 }
1418 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1419
1420 for (i = 0; i < TX_RING_SIZE; i++) {
1421 bp->tx_ring[i].addr = 0;
1422 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1423 }
1424 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1425
1426 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
1427}
1428
1429static void macb_reset_hw(struct macb *bp)
1430{
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001431 /*
1432 * Disable RX and TX (XXX: Should we halt the transmission
1433 * more gracefully?)
1434 */
1435 macb_writel(bp, NCR, 0);
1436
1437 /* Clear the stats registers (XXX: Update stats first?) */
1438 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1439
1440 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001441 macb_writel(bp, TSR, -1);
1442 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001443
1444 /* Disable all interrupts */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001445 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001446 macb_readl(bp, ISR);
1447}
1448
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001449static u32 gem_mdc_clk_div(struct macb *bp)
1450{
1451 u32 config;
1452 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1453
1454 if (pclk_hz <= 20000000)
1455 config = GEM_BF(CLK, GEM_CLK_DIV8);
1456 else if (pclk_hz <= 40000000)
1457 config = GEM_BF(CLK, GEM_CLK_DIV16);
1458 else if (pclk_hz <= 80000000)
1459 config = GEM_BF(CLK, GEM_CLK_DIV32);
1460 else if (pclk_hz <= 120000000)
1461 config = GEM_BF(CLK, GEM_CLK_DIV48);
1462 else if (pclk_hz <= 160000000)
1463 config = GEM_BF(CLK, GEM_CLK_DIV64);
1464 else
1465 config = GEM_BF(CLK, GEM_CLK_DIV96);
1466
1467 return config;
1468}
1469
1470static u32 macb_mdc_clk_div(struct macb *bp)
1471{
1472 u32 config;
1473 unsigned long pclk_hz;
1474
1475 if (macb_is_gem(bp))
1476 return gem_mdc_clk_div(bp);
1477
1478 pclk_hz = clk_get_rate(bp->pclk);
1479 if (pclk_hz <= 20000000)
1480 config = MACB_BF(CLK, MACB_CLK_DIV8);
1481 else if (pclk_hz <= 40000000)
1482 config = MACB_BF(CLK, MACB_CLK_DIV16);
1483 else if (pclk_hz <= 80000000)
1484 config = MACB_BF(CLK, MACB_CLK_DIV32);
1485 else
1486 config = MACB_BF(CLK, MACB_CLK_DIV64);
1487
1488 return config;
1489}
1490
Jamie Iles757a03c2011-03-09 16:29:59 +00001491/*
1492 * Get the DMA bus width field of the network configuration register that we
1493 * should program. We find the width from decoding the design configuration
1494 * register to find the maximum supported data bus width.
1495 */
1496static u32 macb_dbw(struct macb *bp)
1497{
1498 if (!macb_is_gem(bp))
1499 return 0;
1500
1501 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1502 case 4:
1503 return GEM_BF(DBW, GEM_DBW128);
1504 case 2:
1505 return GEM_BF(DBW, GEM_DBW64);
1506 case 1:
1507 default:
1508 return GEM_BF(DBW, GEM_DBW32);
1509 }
1510}
1511
Jamie Iles0116da42011-03-14 17:38:30 +00001512/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001513 * Configure the receive DMA engine
1514 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001515 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001516 * (if not supported by FIFO, it will fallback to default)
1517 * - set both rx/tx packet buffers to full memory size
1518 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001519 */
1520static void macb_configure_dma(struct macb *bp)
1521{
1522 u32 dmacfg;
1523
1524 if (macb_is_gem(bp)) {
1525 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001526 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001527 if (bp->dma_burst_length)
1528 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001529 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Steffen Trumtrara1ae3852013-03-27 23:07:06 +00001530 dmacfg &= ~GEM_BIT(ENDIA);
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001531 if (bp->dev->features & NETIF_F_HW_CSUM)
1532 dmacfg |= GEM_BIT(TXCOEN);
1533 else
1534 dmacfg &= ~GEM_BIT(TXCOEN);
Nicolas Ferree1755872014-07-24 13:50:58 +02001535 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1536 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001537 gem_writel(bp, DMACFG, dmacfg);
1538 }
1539}
1540
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001541static void macb_init_hw(struct macb *bp)
1542{
1543 u32 config;
1544
1545 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001546 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001547
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001548 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001549 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001550 config |= MACB_BIT(PAE); /* PAuse Enable */
1551 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001552 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001553 if (bp->dev->flags & IFF_PROMISC)
1554 config |= MACB_BIT(CAF); /* Copy All Frames */
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001555 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1556 config |= GEM_BIT(RXCOEN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001557 if (!(bp->dev->flags & IFF_BROADCAST))
1558 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001559 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001560 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001561 bp->speed = SPEED_10;
1562 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001563
Jamie Iles0116da42011-03-14 17:38:30 +00001564 macb_configure_dma(bp);
1565
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001566 /* Initialize TX and RX buffers */
1567 macb_writel(bp, RBQP, bp->rx_ring_dma);
1568 macb_writel(bp, TBQP, bp->tx_ring_dma);
1569
1570 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001571 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001572
1573 /* Enable interrupts */
Nicolas Ferree86cd532012-10-31 06:04:57 +00001574 macb_writel(bp, IER, (MACB_RX_INT_FLAGS
1575 | MACB_TX_INT_FLAGS
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001576 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001577
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001578}
1579
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001580/*
1581 * The hash address register is 64 bits long and takes up two
1582 * locations in the memory map. The least significant bits are stored
1583 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1584 *
1585 * The unicast hash enable and the multicast hash enable bits in the
1586 * network configuration register enable the reception of hash matched
1587 * frames. The destination address is reduced to a 6 bit index into
1588 * the 64 bit hash register using the following hash function. The
1589 * hash function is an exclusive or of every sixth bit of the
1590 * destination address.
1591 *
1592 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1593 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1594 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1595 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1596 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1597 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1598 *
1599 * da[0] represents the least significant bit of the first byte
1600 * received, that is, the multicast/unicast indicator, and da[47]
1601 * represents the most significant bit of the last byte received. If
1602 * the hash index, hi[n], points to a bit that is set in the hash
1603 * register then the frame will be matched according to whether the
1604 * frame is multicast or unicast. A multicast match will be signalled
1605 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1606 * index points to a bit set in the hash register. A unicast match
1607 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1608 * and the hash index points to a bit set in the hash register. To
1609 * receive all multicast frames, the hash register should be set with
1610 * all ones and the multicast hash enable bit should be set in the
1611 * network configuration register.
1612 */
1613
1614static inline int hash_bit_value(int bitnr, __u8 *addr)
1615{
1616 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1617 return 1;
1618 return 0;
1619}
1620
1621/*
1622 * Return the hash index value for the specified address.
1623 */
1624static int hash_get_index(__u8 *addr)
1625{
1626 int i, j, bitval;
1627 int hash_index = 0;
1628
1629 for (j = 0; j < 6; j++) {
1630 for (i = 0, bitval = 0; i < 8; i++)
1631 bitval ^= hash_bit_value(i*6 + j, addr);
1632
1633 hash_index |= (bitval << j);
1634 }
1635
1636 return hash_index;
1637}
1638
1639/*
1640 * Add multicast addresses to the internal multicast-hash table.
1641 */
1642static void macb_sethashtable(struct net_device *dev)
1643{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001644 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001645 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001646 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001647 struct macb *bp = netdev_priv(dev);
1648
1649 mc_filter[0] = mc_filter[1] = 0;
1650
Jiri Pirko22bedad32010-04-01 21:22:57 +00001651 netdev_for_each_mc_addr(ha, dev) {
1652 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001653 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1654 }
1655
Jamie Ilesf75ba502011-11-08 10:12:32 +00001656 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1657 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001658}
1659
1660/*
1661 * Enable/Disable promiscuous and multicast modes.
1662 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001663void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001664{
1665 unsigned long cfg;
1666 struct macb *bp = netdev_priv(dev);
1667
1668 cfg = macb_readl(bp, NCFGR);
1669
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001670 if (dev->flags & IFF_PROMISC) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001671 /* Enable promiscuous mode */
1672 cfg |= MACB_BIT(CAF);
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001673
1674 /* Disable RX checksum offload */
1675 if (macb_is_gem(bp))
1676 cfg &= ~GEM_BIT(RXCOEN);
1677 } else {
1678 /* Disable promiscuous mode */
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001679 cfg &= ~MACB_BIT(CAF);
1680
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001681 /* Enable RX checksum offload only if requested */
1682 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1683 cfg |= GEM_BIT(RXCOEN);
1684 }
1685
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001686 if (dev->flags & IFF_ALLMULTI) {
1687 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001688 macb_or_gem_writel(bp, HRB, -1);
1689 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001690 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001691 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001692 /* Enable specific multicasts */
1693 macb_sethashtable(dev);
1694 cfg |= MACB_BIT(NCFGR_MTI);
1695 } else if (dev->flags & (~IFF_ALLMULTI)) {
1696 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001697 macb_or_gem_writel(bp, HRB, 0);
1698 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001699 cfg &= ~MACB_BIT(NCFGR_MTI);
1700 }
1701
1702 macb_writel(bp, NCFGR, cfg);
1703}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001704EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001705
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001706static int macb_open(struct net_device *dev)
1707{
1708 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001709 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001710 int err;
1711
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001712 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001713
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001714 /* carrier starts down */
1715 netif_carrier_off(dev);
1716
frederic RODO6c36a702007-07-12 19:07:24 +02001717 /* if the phy is not yet register, retry later*/
1718 if (!bp->phy_dev)
1719 return -EAGAIN;
1720
Nicolas Ferre1b447912013-06-04 21:57:11 +00001721 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001722 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001723
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001724 err = macb_alloc_consistent(bp);
1725 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001726 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1727 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001728 return err;
1729 }
1730
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001731 napi_enable(&bp->napi);
1732
Nicolas Ferre4df95132013-06-04 21:57:12 +00001733 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001734 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001735
frederic RODO6c36a702007-07-12 19:07:24 +02001736 /* schedule a link state check */
1737 phy_start(bp->phy_dev);
1738
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001739 netif_start_queue(dev);
1740
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001741 return 0;
1742}
1743
1744static int macb_close(struct net_device *dev)
1745{
1746 struct macb *bp = netdev_priv(dev);
1747 unsigned long flags;
1748
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001749 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001750 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001751
frederic RODO6c36a702007-07-12 19:07:24 +02001752 if (bp->phy_dev)
1753 phy_stop(bp->phy_dev);
1754
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001755 spin_lock_irqsave(&bp->lock, flags);
1756 macb_reset_hw(bp);
1757 netif_carrier_off(dev);
1758 spin_unlock_irqrestore(&bp->lock, flags);
1759
1760 macb_free_consistent(bp);
1761
1762 return 0;
1763}
1764
Jamie Ilesa494ed82011-03-09 16:26:35 +00001765static void gem_update_stats(struct macb *bp)
1766{
1767 u32 __iomem *reg = bp->regs + GEM_OTX;
1768 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1769 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1770
1771 for (; p < end; p++, reg++)
1772 *p += __raw_readl(reg);
1773}
1774
1775static struct net_device_stats *gem_get_stats(struct macb *bp)
1776{
1777 struct gem_stats *hwstat = &bp->hw_stats.gem;
1778 struct net_device_stats *nstat = &bp->stats;
1779
1780 gem_update_stats(bp);
1781
1782 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1783 hwstat->rx_alignment_errors +
1784 hwstat->rx_resource_errors +
1785 hwstat->rx_overruns +
1786 hwstat->rx_oversize_frames +
1787 hwstat->rx_jabbers +
1788 hwstat->rx_undersized_frames +
1789 hwstat->rx_length_field_frame_errors);
1790 nstat->tx_errors = (hwstat->tx_late_collisions +
1791 hwstat->tx_excessive_collisions +
1792 hwstat->tx_underrun +
1793 hwstat->tx_carrier_sense_errors);
1794 nstat->multicast = hwstat->rx_multicast_frames;
1795 nstat->collisions = (hwstat->tx_single_collision_frames +
1796 hwstat->tx_multiple_collision_frames +
1797 hwstat->tx_excessive_collisions);
1798 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1799 hwstat->rx_jabbers +
1800 hwstat->rx_undersized_frames +
1801 hwstat->rx_length_field_frame_errors);
1802 nstat->rx_over_errors = hwstat->rx_resource_errors;
1803 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1804 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1805 nstat->rx_fifo_errors = hwstat->rx_overruns;
1806 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1807 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1808 nstat->tx_fifo_errors = hwstat->tx_underrun;
1809
1810 return nstat;
1811}
1812
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001813struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814{
1815 struct macb *bp = netdev_priv(dev);
1816 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001817 struct macb_stats *hwstat = &bp->hw_stats.macb;
1818
1819 if (macb_is_gem(bp))
1820 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001821
frederic RODO6c36a702007-07-12 19:07:24 +02001822 /* read stats from hardware */
1823 macb_update_stats(bp);
1824
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001825 /* Convert HW stats into netdevice stats */
1826 nstat->rx_errors = (hwstat->rx_fcs_errors +
1827 hwstat->rx_align_errors +
1828 hwstat->rx_resource_errors +
1829 hwstat->rx_overruns +
1830 hwstat->rx_oversize_pkts +
1831 hwstat->rx_jabbers +
1832 hwstat->rx_undersize_pkts +
1833 hwstat->sqe_test_errors +
1834 hwstat->rx_length_mismatch);
1835 nstat->tx_errors = (hwstat->tx_late_cols +
1836 hwstat->tx_excessive_cols +
1837 hwstat->tx_underruns +
1838 hwstat->tx_carrier_errors);
1839 nstat->collisions = (hwstat->tx_single_cols +
1840 hwstat->tx_multiple_cols +
1841 hwstat->tx_excessive_cols);
1842 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1843 hwstat->rx_jabbers +
1844 hwstat->rx_undersize_pkts +
1845 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001846 nstat->rx_over_errors = hwstat->rx_resource_errors +
1847 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001848 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1849 nstat->rx_frame_errors = hwstat->rx_align_errors;
1850 nstat->rx_fifo_errors = hwstat->rx_overruns;
1851 /* XXX: What does "missed" mean? */
1852 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1853 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1854 nstat->tx_fifo_errors = hwstat->tx_underruns;
1855 /* Don't know about heartbeat or window errors... */
1856
1857 return nstat;
1858}
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001859EXPORT_SYMBOL_GPL(macb_get_stats);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001860
1861static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1862{
1863 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001864 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001865
frederic RODO6c36a702007-07-12 19:07:24 +02001866 if (!phydev)
1867 return -ENODEV;
1868
1869 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001870}
1871
1872static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1873{
1874 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001875 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001876
frederic RODO6c36a702007-07-12 19:07:24 +02001877 if (!phydev)
1878 return -ENODEV;
1879
1880 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001881}
1882
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001883static int macb_get_regs_len(struct net_device *netdev)
1884{
1885 return MACB_GREGS_NBR * sizeof(u32);
1886}
1887
1888static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1889 void *p)
1890{
1891 struct macb *bp = netdev_priv(dev);
1892 unsigned int tail, head;
1893 u32 *regs_buff = p;
1894
1895 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
1896 | MACB_GREGS_VERSION;
1897
1898 tail = macb_tx_ring_wrap(bp->tx_tail);
1899 head = macb_tx_ring_wrap(bp->tx_head);
1900
1901 regs_buff[0] = macb_readl(bp, NCR);
1902 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
1903 regs_buff[2] = macb_readl(bp, NSR);
1904 regs_buff[3] = macb_readl(bp, TSR);
1905 regs_buff[4] = macb_readl(bp, RBQP);
1906 regs_buff[5] = macb_readl(bp, TBQP);
1907 regs_buff[6] = macb_readl(bp, RSR);
1908 regs_buff[7] = macb_readl(bp, IMR);
1909
1910 regs_buff[8] = tail;
1911 regs_buff[9] = head;
1912 regs_buff[10] = macb_tx_dma(bp, tail);
1913 regs_buff[11] = macb_tx_dma(bp, head);
1914
1915 if (macb_is_gem(bp)) {
1916 regs_buff[12] = gem_readl(bp, USRIO);
1917 regs_buff[13] = gem_readl(bp, DMACFG);
1918 }
1919}
1920
Joachim Eastwood0005f542012-10-18 11:01:12 +00001921const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001922 .get_settings = macb_get_settings,
1923 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001924 .get_regs_len = macb_get_regs_len,
1925 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001926 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00001927 .get_ts_info = ethtool_op_get_ts_info,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001928};
Joachim Eastwood0005f542012-10-18 11:01:12 +00001929EXPORT_SYMBOL_GPL(macb_ethtool_ops);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001930
Joachim Eastwood0005f542012-10-18 11:01:12 +00001931int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001932{
1933 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001934 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001935
1936 if (!netif_running(dev))
1937 return -EINVAL;
1938
frederic RODO6c36a702007-07-12 19:07:24 +02001939 if (!phydev)
1940 return -ENODEV;
1941
Richard Cochran28b04112010-07-17 08:48:55 +00001942 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001943}
Joachim Eastwood0005f542012-10-18 11:01:12 +00001944EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001945
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001946static int macb_set_features(struct net_device *netdev,
1947 netdev_features_t features)
1948{
1949 struct macb *bp = netdev_priv(netdev);
1950 netdev_features_t changed = features ^ netdev->features;
1951
1952 /* TX checksum offload */
1953 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
1954 u32 dmacfg;
1955
1956 dmacfg = gem_readl(bp, DMACFG);
1957 if (features & NETIF_F_HW_CSUM)
1958 dmacfg |= GEM_BIT(TXCOEN);
1959 else
1960 dmacfg &= ~GEM_BIT(TXCOEN);
1961 gem_writel(bp, DMACFG, dmacfg);
1962 }
1963
Cyrille Pitchen924ec532014-07-24 13:51:01 +02001964 /* RX checksum offload */
1965 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
1966 u32 netcfg;
1967
1968 netcfg = gem_readl(bp, NCFGR);
1969 if (features & NETIF_F_RXCSUM &&
1970 !(netdev->flags & IFF_PROMISC))
1971 netcfg |= GEM_BIT(RXCOEN);
1972 else
1973 netcfg &= ~GEM_BIT(RXCOEN);
1974 gem_writel(bp, NCFGR, netcfg);
1975 }
1976
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001977 return 0;
1978}
1979
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001980static const struct net_device_ops macb_netdev_ops = {
1981 .ndo_open = macb_open,
1982 .ndo_stop = macb_close,
1983 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001984 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001985 .ndo_get_stats = macb_get_stats,
1986 .ndo_do_ioctl = macb_ioctl,
1987 .ndo_validate_addr = eth_validate_addr,
1988 .ndo_change_mtu = eth_change_mtu,
1989 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001990#ifdef CONFIG_NET_POLL_CONTROLLER
1991 .ndo_poll_controller = macb_poll_controller,
1992#endif
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02001993 .ndo_set_features = macb_set_features,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001994};
1995
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001996#if defined(CONFIG_OF)
Nicolas Ferree1755872014-07-24 13:50:58 +02001997static struct macb_config pc302gem_config = {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02001998 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
1999 .dma_burst_length = 16,
2000};
2001
2002static struct macb_config sama5d3_config = {
2003 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
Nicolas Ferree1755872014-07-24 13:50:58 +02002004 .dma_burst_length = 16,
2005};
2006
Cyrille Pitchen4b7b0e42014-07-24 13:51:03 +02002007static struct macb_config sama5d4_config = {
2008 .caps = 0,
2009 .dma_burst_length = 4,
2010};
2011
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002012static const struct of_device_id macb_dt_ids[] = {
2013 { .compatible = "cdns,at32ap7000-macb" },
2014 { .compatible = "cdns,at91sam9260-macb" },
2015 { .compatible = "cdns,macb" },
Nicolas Ferree1755872014-07-24 13:50:58 +02002016 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2017 { .compatible = "cdns,gem", .data = &pc302gem_config },
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002018 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
Cyrille Pitchen4b7b0e42014-07-24 13:51:03 +02002019 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002020 { /* sentinel */ }
2021};
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002022MODULE_DEVICE_TABLE(of, macb_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002023#endif
2024
Nicolas Ferree1755872014-07-24 13:50:58 +02002025/*
2026 * Configure peripheral capacities according to device tree
2027 * and integration options used
2028 */
2029static void macb_configure_caps(struct macb *bp)
2030{
2031 u32 dcfg;
2032 const struct of_device_id *match;
2033 const struct macb_config *config;
2034
2035 if (bp->pdev->dev.of_node) {
2036 match = of_match_node(macb_dt_ids, bp->pdev->dev.of_node);
2037 if (match && match->data) {
2038 config = (const struct macb_config *)match->data;
2039
2040 bp->caps = config->caps;
2041 /*
2042 * As we have access to the matching node, configure
2043 * DMA burst length as well
2044 */
2045 bp->dma_burst_length = config->dma_burst_length;
2046 }
2047 }
2048
2049 if (MACB_BFEXT(IDNUM, macb_readl(bp, MID)) == 0x2)
2050 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2051
2052 if (macb_is_gem(bp)) {
2053 dcfg = gem_readl(bp, DCFG1);
2054 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2055 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2056 dcfg = gem_readl(bp, DCFG2);
2057 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2058 bp->caps |= MACB_CAPS_FIFO_MODE;
2059 }
2060
2061 netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
2062}
2063
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01002064static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002065{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00002066 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002067 struct resource *regs;
2068 struct net_device *dev;
2069 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02002070 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002071 u32 config;
2072 int err = -ENXIO;
Guenter Roeck50907042013-04-02 09:35:09 +00002073 const char *mac;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002074
2075 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2076 if (!regs) {
2077 dev_err(&pdev->dev, "no mmio resource defined\n");
2078 goto err_out;
2079 }
2080
2081 err = -ENOMEM;
2082 dev = alloc_etherdev(sizeof(*bp));
Joe Perches41de8d42012-01-29 13:47:52 +00002083 if (!dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002084 goto err_out;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002085
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002086 SET_NETDEV_DEV(dev, &pdev->dev);
2087
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002088 bp = netdev_priv(dev);
2089 bp->pdev = pdev;
2090 bp->dev = dev;
2091
2092 spin_lock_init(&bp->lock);
Nicolas Ferree86cd532012-10-31 06:04:57 +00002093 INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002094
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08002095 bp->pclk = devm_clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01002096 if (IS_ERR(bp->pclk)) {
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08002097 err = PTR_ERR(bp->pclk);
2098 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
Andrew Victor0cc86742007-02-07 16:40:44 +01002099 goto err_out_free_dev;
2100 }
Jamie Iles461845d2011-03-08 20:19:23 +00002101
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08002102 bp->hclk = devm_clk_get(&pdev->dev, "hclk");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002103 if (IS_ERR(bp->hclk)) {
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08002104 err = PTR_ERR(bp->hclk);
2105 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
2106 goto err_out_free_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002107 }
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08002108
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002109 bp->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2110
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08002111 err = clk_prepare_enable(bp->pclk);
2112 if (err) {
2113 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
2114 goto err_out_free_dev;
2115 }
2116
2117 err = clk_prepare_enable(bp->hclk);
2118 if (err) {
2119 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
2120 goto err_out_disable_pclk;
2121 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002122
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002123 if (!IS_ERR(bp->tx_clk)) {
2124 err = clk_prepare_enable(bp->tx_clk);
2125 if (err) {
2126 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n",
2127 err);
2128 goto err_out_disable_hclk;
2129 }
2130 }
2131
Soren Brinkmann60fe7162013-12-10 16:07:21 -08002132 bp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002133 if (!bp->regs) {
2134 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
2135 err = -ENOMEM;
2136 goto err_out_disable_clocks;
2137 }
2138
2139 dev->irq = platform_get_irq(pdev, 0);
Soren Brinkmann0a4acf02013-12-10 16:07:22 -08002140 err = devm_request_irq(&pdev->dev, dev->irq, macb_interrupt, 0,
2141 dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002142 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002143 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
2144 dev->irq, err);
Soren Brinkmann60fe7162013-12-10 16:07:21 -08002145 goto err_out_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002146 }
2147
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00002148 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002149 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002150 dev->ethtool_ops = &macb_ethtool_ops;
2151
2152 dev->base_addr = regs->start;
2153
Nicolas Ferree1755872014-07-24 13:50:58 +02002154 /* setup capacities */
2155 macb_configure_caps(bp);
2156
Nicolas Ferre4df95132013-06-04 21:57:12 +00002157 /* setup appropriated routines according to adapter type */
2158 if (macb_is_gem(bp)) {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002159 bp->max_tx_length = GEM_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002160 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2161 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2162 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2163 bp->macbgem_ops.mog_rx = gem_rx;
2164 } else {
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002165 bp->max_tx_length = MACB_MAX_TX_LEN;
Nicolas Ferre4df95132013-06-04 21:57:12 +00002166 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2167 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2168 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2169 bp->macbgem_ops.mog_rx = macb_rx;
2170 }
2171
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002172 /* Set features */
2173 dev->hw_features = NETIF_F_SG;
Cyrille Pitchen85ff3d82014-07-24 13:51:00 +02002174 /* Checksum offload is only available on gem with packet buffer */
2175 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
Cyrille Pitchen924ec532014-07-24 13:51:01 +02002176 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Cyrille Pitchena4c35ed32014-07-24 13:50:59 +02002177 if (bp->caps & MACB_CAPS_SG_DISABLED)
2178 dev->hw_features &= ~NETIF_F_SG;
2179 dev->features = dev->hw_features;
2180
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002181 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00002182 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00002183 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002184 macb_writel(bp, NCFGR, config);
2185
Guenter Roeck50907042013-04-02 09:35:09 +00002186 mac = of_get_mac_address(pdev->dev.of_node);
2187 if (mac)
2188 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
2189 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002190 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02002191
Guenter Roeck50907042013-04-02 09:35:09 +00002192 err = of_get_phy_mode(pdev->dev.of_node);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002193 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09002194 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002195 if (pdata && pdata->is_rmii)
2196 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
2197 else
2198 bp->phy_interface = PHY_INTERFACE_MODE_MII;
2199 } else {
2200 bp->phy_interface = err;
2201 }
2202
Patrice Vilchez140b7552012-10-31 06:04:50 +00002203 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2204 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
2205 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01002206#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00002207 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
2208 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01002209#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00002210 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01002211#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002212 else
Andrew Victor0cc86742007-02-07 16:40:44 +01002213#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00002214 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01002215#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00002216 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01002217#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002218
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002219 err = register_netdev(dev);
2220 if (err) {
2221 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Soren Brinkmann0a4acf02013-12-10 16:07:22 -08002222 goto err_out_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002223 }
2224
Nicolas Ferre72ca8202013-04-14 22:04:33 +00002225 err = macb_mii_init(bp);
2226 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02002227 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002228
frederic RODO6c36a702007-07-12 19:07:24 +02002229 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002230
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002231 netif_carrier_off(dev);
2232
Jamie Ilesf75ba502011-11-08 10:12:32 +00002233 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
2234 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
2235 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002236
frederic RODO6c36a702007-07-12 19:07:24 +02002237 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002238 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2239 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002240
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002241 return 0;
2242
frederic RODO6c36a702007-07-12 19:07:24 +02002243err_out_unregister_netdev:
2244 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002245err_out_disable_clocks:
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002246 if (!IS_ERR(bp->tx_clk))
2247 clk_disable_unprepare(bp->tx_clk);
2248err_out_disable_hclk:
Steffen Trumtrarace58012013-03-27 23:07:07 +00002249 clk_disable_unprepare(bp->hclk);
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08002250err_out_disable_pclk:
Steffen Trumtrarace58012013-03-27 23:07:07 +00002251 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002252err_out_free_dev:
2253 free_netdev(dev);
2254err_out:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002255 return err;
2256}
2257
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01002258static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002259{
2260 struct net_device *dev;
2261 struct macb *bp;
2262
2263 dev = platform_get_drvdata(pdev);
2264
2265 if (dev) {
2266 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002267 if (bp->phy_dev)
2268 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002269 mdiobus_unregister(bp->mii_bus);
2270 kfree(bp->mii_bus->irq);
2271 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002272 unregister_netdev(dev);
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002273 if (!IS_ERR(bp->tx_clk))
2274 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002275 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002276 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002277 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002278 }
2279
2280 return 0;
2281}
2282
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002283#ifdef CONFIG_PM
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002284static int macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002285{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002286 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002287 struct net_device *netdev = platform_get_drvdata(pdev);
2288 struct macb *bp = netdev_priv(netdev);
2289
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002290 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002291 netif_device_detach(netdev);
2292
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002293 if (!IS_ERR(bp->tx_clk))
2294 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002295 clk_disable_unprepare(bp->hclk);
2296 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002297
2298 return 0;
2299}
2300
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002301static int macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002302{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002303 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002304 struct net_device *netdev = platform_get_drvdata(pdev);
2305 struct macb *bp = netdev_priv(netdev);
2306
Steffen Trumtrarace58012013-03-27 23:07:07 +00002307 clk_prepare_enable(bp->pclk);
2308 clk_prepare_enable(bp->hclk);
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002309 if (!IS_ERR(bp->tx_clk))
2310 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002311
2312 netif_device_attach(netdev);
2313
2314 return 0;
2315}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002316#endif
2317
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002318static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
2319
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002320static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01002321 .remove = __exit_p(macb_remove),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002322 .driver = {
2323 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07002324 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002325 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002326 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002327 },
2328};
2329
Jingoo Hanb543a8d2013-03-04 16:43:18 +00002330module_platform_driver_probe(macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002331
2332MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00002333MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002334MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07002335MODULE_ALIAS("platform:macb");