blob: 603844b1d48375345b4780b82acde9c4b421acb7 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000021#include <linux/gpio.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000026#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020028#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080029#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010030#include <linux/of_device.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +000033#include <linux/pinctrl/consumer.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010034
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010035#include "macb.h"
36
Nicolas Ferre1b447912013-06-04 21:57:11 +000037#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000038#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000039#define RX_RING_SIZE 512 /* must be power of 2 */
40#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010041
Havard Skinnemoen55054a12012-10-31 06:04:55 +000042#define TX_RING_SIZE 128 /* must be power of 2 */
43#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010044
Nicolas Ferre909a8582012-11-19 06:00:21 +000045/* level of occupied TX descriptors under which we wake up TX process */
46#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010047
48#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
49 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000050#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
51 | MACB_BIT(ISR_RLE) \
52 | MACB_BIT(TXERR))
53#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
54
55/*
56 * Graceful stop timeouts in us. We should allow up to
57 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
58 */
59#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010060
Havard Skinnemoen55054a12012-10-31 06:04:55 +000061/* Ring buffer accessors */
62static unsigned int macb_tx_ring_wrap(unsigned int index)
63{
64 return index & (TX_RING_SIZE - 1);
65}
66
Havard Skinnemoen55054a12012-10-31 06:04:55 +000067static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
68{
69 return &bp->tx_ring[macb_tx_ring_wrap(index)];
70}
71
72static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
73{
74 return &bp->tx_skb[macb_tx_ring_wrap(index)];
75}
76
77static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
78{
79 dma_addr_t offset;
80
81 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
82
83 return bp->tx_ring_dma + offset;
84}
85
86static unsigned int macb_rx_ring_wrap(unsigned int index)
87{
88 return index & (RX_RING_SIZE - 1);
89}
90
91static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
92{
93 return &bp->rx_ring[macb_rx_ring_wrap(index)];
94}
95
96static void *macb_rx_buffer(struct macb *bp, unsigned int index)
97{
Nicolas Ferre1b447912013-06-04 21:57:11 +000098 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +000099}
100
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000101void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100102{
103 u32 bottom;
104 u16 top;
105
106 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000107 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100108 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000109 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000110
111 /* Clear unused address register sets */
112 macb_or_gem_writel(bp, SA2B, 0);
113 macb_or_gem_writel(bp, SA2T, 0);
114 macb_or_gem_writel(bp, SA3B, 0);
115 macb_or_gem_writel(bp, SA3T, 0);
116 macb_or_gem_writel(bp, SA4B, 0);
117 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100118}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000119EXPORT_SYMBOL_GPL(macb_set_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100120
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000121void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100122{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000123 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100124 u32 bottom;
125 u16 top;
126 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000127 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100128
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900129 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000130
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000131 /* Check all 4 address register for vaild address */
132 for (i = 0; i < 4; i++) {
133 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
134 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100135
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000136 if (pdata && pdata->rev_eth_addr) {
137 addr[5] = bottom & 0xff;
138 addr[4] = (bottom >> 8) & 0xff;
139 addr[3] = (bottom >> 16) & 0xff;
140 addr[2] = (bottom >> 24) & 0xff;
141 addr[1] = top & 0xff;
142 addr[0] = (top & 0xff00) >> 8;
143 } else {
144 addr[0] = bottom & 0xff;
145 addr[1] = (bottom >> 8) & 0xff;
146 addr[2] = (bottom >> 16) & 0xff;
147 addr[3] = (bottom >> 24) & 0xff;
148 addr[4] = top & 0xff;
149 addr[5] = (top >> 8) & 0xff;
150 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100151
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000152 if (is_valid_ether_addr(addr)) {
153 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
154 return;
155 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700156 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000157
158 netdev_info(bp->dev, "invalid hw address, using random\n");
159 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100160}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000161EXPORT_SYMBOL_GPL(macb_get_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100162
frederic RODO6c36a702007-07-12 19:07:24 +0200163static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100164{
frederic RODO6c36a702007-07-12 19:07:24 +0200165 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100166 int value;
167
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100168 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
169 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200170 | MACB_BF(PHYA, mii_id)
171 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100172 | MACB_BF(CODE, MACB_MAN_CODE)));
173
frederic RODO6c36a702007-07-12 19:07:24 +0200174 /* wait for end of transfer */
175 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
176 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100177
178 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100179
180 return value;
181}
182
frederic RODO6c36a702007-07-12 19:07:24 +0200183static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
184 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100185{
frederic RODO6c36a702007-07-12 19:07:24 +0200186 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100187
188 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
189 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200190 | MACB_BF(PHYA, mii_id)
191 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100192 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200193 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100194
frederic RODO6c36a702007-07-12 19:07:24 +0200195 /* wait for end of transfer */
196 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
197 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100198
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100199 return 0;
200}
201
frederic RODO6c36a702007-07-12 19:07:24 +0200202static int macb_mdio_reset(struct mii_bus *bus)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100203{
frederic RODO6c36a702007-07-12 19:07:24 +0200204 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100205}
206
frederic RODO6c36a702007-07-12 19:07:24 +0200207static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100208{
frederic RODO6c36a702007-07-12 19:07:24 +0200209 struct macb *bp = netdev_priv(dev);
210 struct phy_device *phydev = bp->phy_dev;
211 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100212
frederic RODO6c36a702007-07-12 19:07:24 +0200213 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100214
frederic RODO6c36a702007-07-12 19:07:24 +0200215 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100216
frederic RODO6c36a702007-07-12 19:07:24 +0200217 if (phydev->link) {
218 if ((bp->speed != phydev->speed) ||
219 (bp->duplex != phydev->duplex)) {
220 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100221
frederic RODO6c36a702007-07-12 19:07:24 +0200222 reg = macb_readl(bp, NCFGR);
223 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000224 if (macb_is_gem(bp))
225 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200226
227 if (phydev->duplex)
228 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900229 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200230 reg |= MACB_BIT(SPD);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000231 if (phydev->speed == SPEED_1000)
232 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200233
Patrice Vilchez140b7552012-10-31 06:04:50 +0000234 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200235
236 bp->speed = phydev->speed;
237 bp->duplex = phydev->duplex;
238 status_change = 1;
239 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100240 }
241
frederic RODO6c36a702007-07-12 19:07:24 +0200242 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700243 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200244 bp->speed = 0;
245 bp->duplex = -1;
246 }
247 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100248
frederic RODO6c36a702007-07-12 19:07:24 +0200249 status_change = 1;
250 }
251
252 spin_unlock_irqrestore(&bp->lock, flags);
253
254 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000255 if (phydev->link) {
256 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000257 netdev_info(dev, "link up (%d/%s)\n",
258 phydev->speed,
259 phydev->duplex == DUPLEX_FULL ?
260 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000261 } else {
262 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000263 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000264 }
frederic RODO6c36a702007-07-12 19:07:24 +0200265 }
266}
267
268/* based on au1000_eth. c*/
269static int macb_mii_probe(struct net_device *dev)
270{
271 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000272 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000273 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000274 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000275 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200276
Jiri Pirko7455a762010-02-08 05:12:08 +0000277 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200278 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000279 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200280 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200281 }
282
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000283 pdata = dev_get_platdata(&bp->pdev->dev);
284 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
285 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
286 if (!ret) {
287 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
288 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
289 }
290 }
frederic RODO6c36a702007-07-12 19:07:24 +0200291
292 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000293 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100294 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000295 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000296 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000297 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200298 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100299
frederic RODO6c36a702007-07-12 19:07:24 +0200300 /* mask with MAC supported features */
Patrice Vilchez140b7552012-10-31 06:04:50 +0000301 if (macb_is_gem(bp))
302 phydev->supported &= PHY_GBIT_FEATURES;
303 else
304 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100305
frederic RODO6c36a702007-07-12 19:07:24 +0200306 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100307
frederic RODO6c36a702007-07-12 19:07:24 +0200308 bp->link = 0;
309 bp->speed = 0;
310 bp->duplex = -1;
311 bp->phy_dev = phydev;
312
313 return 0;
314}
315
Joachim Eastwood0005f542012-10-18 11:01:12 +0000316int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200317{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000318 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200319 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200320 int err = -ENXIO, i;
321
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200322 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200323 macb_writel(bp, NCR, MACB_BIT(MPE));
324
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700325 bp->mii_bus = mdiobus_alloc();
326 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200327 err = -ENOMEM;
328 goto err_out;
329 }
330
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700331 bp->mii_bus->name = "MACB_mii_bus";
332 bp->mii_bus->read = &macb_mdio_read;
333 bp->mii_bus->write = &macb_mdio_write;
334 bp->mii_bus->reset = &macb_mdio_reset;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000335 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
336 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700337 bp->mii_bus->priv = bp;
338 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900339 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700340
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700341 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
342 if (!bp->mii_bus->irq) {
343 err = -ENOMEM;
344 goto err_out_free_mdiobus;
345 }
346
Jamie Iles91523942011-02-28 04:05:25 +0000347 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200348
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200349 np = bp->pdev->dev.of_node;
350 if (np) {
351 /* try dt phy registration */
352 err = of_mdiobus_register(bp->mii_bus, np);
353
354 /* fallback to standard phy registration if no phy were
355 found during dt phy registration */
356 if (!err && !phy_find_first(bp->mii_bus)) {
357 for (i = 0; i < PHY_MAX_ADDR; i++) {
358 struct phy_device *phydev;
359
360 phydev = mdiobus_scan(bp->mii_bus, i);
361 if (IS_ERR(phydev)) {
362 err = PTR_ERR(phydev);
363 break;
364 }
365 }
366
367 if (err)
368 goto err_out_unregister_bus;
369 }
370 } else {
371 for (i = 0; i < PHY_MAX_ADDR; i++)
372 bp->mii_bus->irq[i] = PHY_POLL;
373
374 if (pdata)
375 bp->mii_bus->phy_mask = pdata->phy_mask;
376
377 err = mdiobus_register(bp->mii_bus);
378 }
379
380 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200381 goto err_out_free_mdio_irq;
382
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200383 err = macb_mii_probe(bp->dev);
384 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200385 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200386
387 return 0;
388
389err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700390 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200391err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700392 kfree(bp->mii_bus->irq);
393err_out_free_mdiobus:
394 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200395err_out:
396 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100397}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000398EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100399
400static void macb_update_stats(struct macb *bp)
401{
402 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000403 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
404 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100405
406 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
407
408 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100409 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100410}
411
Nicolas Ferree86cd532012-10-31 06:04:57 +0000412static int macb_halt_tx(struct macb *bp)
413{
414 unsigned long halt_time, timeout;
415 u32 status;
416
417 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
418
419 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
420 do {
421 halt_time = jiffies;
422 status = macb_readl(bp, TSR);
423 if (!(status & MACB_BIT(TGO)))
424 return 0;
425
426 usleep_range(10, 250);
427 } while (time_before(halt_time, timeout));
428
429 return -ETIMEDOUT;
430}
431
432static void macb_tx_error_task(struct work_struct *work)
433{
434 struct macb *bp = container_of(work, struct macb, tx_error_task);
435 struct macb_tx_skb *tx_skb;
436 struct sk_buff *skb;
437 unsigned int tail;
438
439 netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
440 bp->tx_tail, bp->tx_head);
441
442 /* Make sure nobody is trying to queue up new packets */
443 netif_stop_queue(bp->dev);
444
445 /*
446 * Stop transmission now
447 * (in case we have just queued new packets)
448 */
449 if (macb_halt_tx(bp))
450 /* Just complain for now, reinitializing TX path can be good */
451 netdev_err(bp->dev, "BUG: halt tx timed out\n");
452
453 /* No need for the lock here as nobody will interrupt us anymore */
454
455 /*
456 * Treat frames in TX queue including the ones that caused the error.
457 * Free transmit buffers in upper layer.
458 */
459 for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
460 struct macb_dma_desc *desc;
461 u32 ctrl;
462
463 desc = macb_tx_desc(bp, tail);
464 ctrl = desc->ctrl;
465 tx_skb = macb_tx_skb(bp, tail);
466 skb = tx_skb->skb;
467
468 if (ctrl & MACB_BIT(TX_USED)) {
469 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
470 macb_tx_ring_wrap(tail), skb->data);
471 bp->stats.tx_packets++;
472 bp->stats.tx_bytes += skb->len;
473 } else {
474 /*
475 * "Buffers exhausted mid-frame" errors may only happen
476 * if the driver is buggy, so complain loudly about those.
477 * Statistics are updated by hardware.
478 */
479 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
480 netdev_err(bp->dev,
481 "BUG: TX buffers exhausted mid-frame\n");
482
483 desc->ctrl = ctrl | MACB_BIT(TX_USED);
484 }
485
486 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
487 DMA_TO_DEVICE);
488 tx_skb->skb = NULL;
489 dev_kfree_skb(skb);
490 }
491
492 /* Make descriptor updates visible to hardware */
493 wmb();
494
495 /* Reinitialize the TX desc queue */
496 macb_writel(bp, TBQP, bp->tx_ring_dma);
497 /* Make TX ring reflect state of hardware */
498 bp->tx_head = bp->tx_tail = 0;
499
500 /* Now we are ready to start transmission again */
501 netif_wake_queue(bp->dev);
502
503 /* Housework before enabling TX IRQ */
504 macb_writel(bp, TSR, macb_readl(bp, TSR));
505 macb_writel(bp, IER, MACB_TX_INT_FLAGS);
506}
507
508static void macb_tx_interrupt(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100509{
510 unsigned int tail;
511 unsigned int head;
512 u32 status;
513
514 status = macb_readl(bp, TSR);
515 macb_writel(bp, TSR, status);
516
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000517 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
518 macb_writel(bp, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000519
Nicolas Ferree86cd532012-10-31 06:04:57 +0000520 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
521 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100522
523 head = bp->tx_head;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000524 for (tail = bp->tx_tail; tail != head; tail++) {
525 struct macb_tx_skb *tx_skb;
526 struct sk_buff *skb;
527 struct macb_dma_desc *desc;
528 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100529
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000530 desc = macb_tx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100531
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000532 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100533 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000534
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000535 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100536
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000537 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100538 break;
539
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000540 tx_skb = macb_tx_skb(bp, tail);
541 skb = tx_skb->skb;
542
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000543 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000544 macb_tx_ring_wrap(tail), skb->data);
545 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100546 DMA_TO_DEVICE);
547 bp->stats.tx_packets++;
548 bp->stats.tx_bytes += skb->len;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000549 tx_skb->skb = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100550 dev_kfree_skb_irq(skb);
551 }
552
553 bp->tx_tail = tail;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000554 if (netif_queue_stopped(bp->dev)
Nicolas Ferre909a8582012-11-19 06:00:21 +0000555 && CIRC_CNT(bp->tx_head, bp->tx_tail,
556 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100557 netif_wake_queue(bp->dev);
558}
559
Nicolas Ferre4df95132013-06-04 21:57:12 +0000560static void gem_rx_refill(struct macb *bp)
561{
562 unsigned int entry;
563 struct sk_buff *skb;
564 struct macb_dma_desc *desc;
565 dma_addr_t paddr;
566
567 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
568 u32 addr, ctrl;
569
570 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
571 desc = &bp->rx_ring[entry];
572
573 /* Make hw descriptor updates visible to CPU */
574 rmb();
575
576 addr = desc->addr;
577 ctrl = desc->ctrl;
578 bp->rx_prepared_head++;
579
580 if ((addr & MACB_BIT(RX_USED)))
581 continue;
582
583 if (bp->rx_skbuff[entry] == NULL) {
584 /* allocate sk_buff for this free entry in ring */
585 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
586 if (unlikely(skb == NULL)) {
587 netdev_err(bp->dev,
588 "Unable to allocate sk_buff\n");
589 break;
590 }
591 bp->rx_skbuff[entry] = skb;
592
593 /* now fill corresponding descriptor entry */
594 paddr = dma_map_single(&bp->pdev->dev, skb->data,
595 bp->rx_buffer_size, DMA_FROM_DEVICE);
596
597 if (entry == RX_RING_SIZE - 1)
598 paddr |= MACB_BIT(RX_WRAP);
599 bp->rx_ring[entry].addr = paddr;
600 bp->rx_ring[entry].ctrl = 0;
601
602 /* properly align Ethernet header */
603 skb_reserve(skb, NET_IP_ALIGN);
604 }
605 }
606
607 /* Make descriptor updates visible to hardware */
608 wmb();
609
610 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
611 bp->rx_prepared_head, bp->rx_tail);
612}
613
614/* Mark DMA descriptors from begin up to and not including end as unused */
615static void discard_partial_frame(struct macb *bp, unsigned int begin,
616 unsigned int end)
617{
618 unsigned int frag;
619
620 for (frag = begin; frag != end; frag++) {
621 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
622 desc->addr &= ~MACB_BIT(RX_USED);
623 }
624
625 /* Make descriptor updates visible to hardware */
626 wmb();
627
628 /*
629 * When this happens, the hardware stats registers for
630 * whatever caused this is updated, so we don't have to record
631 * anything.
632 */
633}
634
635static int gem_rx(struct macb *bp, int budget)
636{
637 unsigned int len;
638 unsigned int entry;
639 struct sk_buff *skb;
640 struct macb_dma_desc *desc;
641 int count = 0;
642
643 while (count < budget) {
644 u32 addr, ctrl;
645
646 entry = macb_rx_ring_wrap(bp->rx_tail);
647 desc = &bp->rx_ring[entry];
648
649 /* Make hw descriptor updates visible to CPU */
650 rmb();
651
652 addr = desc->addr;
653 ctrl = desc->ctrl;
654
655 if (!(addr & MACB_BIT(RX_USED)))
656 break;
657
658 desc->addr &= ~MACB_BIT(RX_USED);
659 bp->rx_tail++;
660 count++;
661
662 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
663 netdev_err(bp->dev,
664 "not whole frame pointed by descriptor\n");
665 bp->stats.rx_dropped++;
666 break;
667 }
668 skb = bp->rx_skbuff[entry];
669 if (unlikely(!skb)) {
670 netdev_err(bp->dev,
671 "inconsistent Rx descriptor chain\n");
672 bp->stats.rx_dropped++;
673 break;
674 }
675 /* now everything is ready for receiving packet */
676 bp->rx_skbuff[entry] = NULL;
677 len = MACB_BFEXT(RX_FRMLEN, ctrl);
678
679 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
680
681 skb_put(skb, len);
682 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
683 dma_unmap_single(&bp->pdev->dev, addr,
684 len, DMA_FROM_DEVICE);
685
686 skb->protocol = eth_type_trans(skb, bp->dev);
687 skb_checksum_none_assert(skb);
688
689 bp->stats.rx_packets++;
690 bp->stats.rx_bytes += skb->len;
691
692#if defined(DEBUG) && defined(VERBOSE_DEBUG)
693 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
694 skb->len, skb->csum);
695 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
696 skb->mac_header, 16, true);
697 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
698 skb->data, 32, true);
699#endif
700
701 netif_receive_skb(skb);
702 }
703
704 gem_rx_refill(bp);
705
706 return count;
707}
708
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100709static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
710 unsigned int last_frag)
711{
712 unsigned int len;
713 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000714 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100715 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000716 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100717
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000718 desc = macb_rx_desc(bp, last_frag);
719 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100720
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000721 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000722 macb_rx_ring_wrap(first_frag),
723 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100724
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000725 /*
726 * The ethernet header starts NET_IP_ALIGN bytes into the
727 * first buffer. Since the header is 14 bytes, this makes the
728 * payload word-aligned.
729 *
730 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
731 * the two padding bytes into the skb so that we avoid hitting
732 * the slowpath in memcpy(), and pull them off afterwards.
733 */
734 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100735 if (!skb) {
736 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000737 for (frag = first_frag; ; frag++) {
738 desc = macb_rx_desc(bp, frag);
739 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100740 if (frag == last_frag)
741 break;
742 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000743
744 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100745 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000746
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100747 return 1;
748 }
749
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000750 offset = 0;
751 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700752 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100753 skb_put(skb, len);
754
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000755 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000756 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100757
758 if (offset + frag_len > len) {
759 BUG_ON(frag != last_frag);
760 frag_len = len - offset;
761 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300762 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000763 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000764 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000765 desc = macb_rx_desc(bp, frag);
766 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100767
768 if (frag == last_frag)
769 break;
770 }
771
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000772 /* Make descriptor updates visible to hardware */
773 wmb();
774
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000775 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100776 skb->protocol = eth_type_trans(skb, bp->dev);
777
778 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000779 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000780 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000781 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100782 netif_receive_skb(skb);
783
784 return 0;
785}
786
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100787static int macb_rx(struct macb *bp, int budget)
788{
789 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000790 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100791 int first_frag = -1;
792
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000793 for (tail = bp->rx_tail; budget > 0; tail++) {
794 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100795 u32 addr, ctrl;
796
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000797 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100798 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000799
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000800 addr = desc->addr;
801 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100802
803 if (!(addr & MACB_BIT(RX_USED)))
804 break;
805
806 if (ctrl & MACB_BIT(RX_SOF)) {
807 if (first_frag != -1)
808 discard_partial_frame(bp, first_frag, tail);
809 first_frag = tail;
810 }
811
812 if (ctrl & MACB_BIT(RX_EOF)) {
813 int dropped;
814 BUG_ON(first_frag == -1);
815
816 dropped = macb_rx_frame(bp, first_frag, tail);
817 first_frag = -1;
818 if (!dropped) {
819 received++;
820 budget--;
821 }
822 }
823 }
824
825 if (first_frag != -1)
826 bp->rx_tail = first_frag;
827 else
828 bp->rx_tail = tail;
829
830 return received;
831}
832
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700833static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100834{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700835 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700836 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100837 u32 status;
838
839 status = macb_readl(bp, RSR);
840 macb_writel(bp, RSR, status);
841
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700842 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100843
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000844 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000845 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100846
Nicolas Ferre4df95132013-06-04 21:57:12 +0000847 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000848 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800849 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100850
Joshua Hokeb3363692010-10-25 01:44:22 +0000851 /*
852 * We've done what we can to clean the buffers. Make sure we
853 * get notified when new packets arrive.
854 */
855 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
Nicolas Ferre8770e912013-02-12 11:08:48 +0100856
857 /* Packets received while interrupts were disabled */
858 status = macb_readl(bp, RSR);
859 if (unlikely(status))
860 napi_reschedule(napi);
Joshua Hokeb3363692010-10-25 01:44:22 +0000861 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100862
863 /* TODO: Handle errors */
864
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700865 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100866}
867
868static irqreturn_t macb_interrupt(int irq, void *dev_id)
869{
870 struct net_device *dev = dev_id;
871 struct macb *bp = netdev_priv(dev);
872 u32 status;
873
874 status = macb_readl(bp, ISR);
875
876 if (unlikely(!status))
877 return IRQ_NONE;
878
879 spin_lock(&bp->lock);
880
881 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100882 /* close possible race with dev_close */
883 if (unlikely(!netif_running(dev))) {
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000884 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100885 break;
886 }
887
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000888 netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
889
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100890 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000891 /*
892 * There's no point taking any more interrupts
893 * until we have processed the buffers. The
894 * scheduling call may fail if the poll routine
895 * is already scheduled, so disable interrupts
896 * now.
897 */
898 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000899 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
900 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +0000901
Ben Hutchings288379f2009-01-19 16:43:59 -0800902 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000903 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800904 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100905 }
906 }
907
Nicolas Ferree86cd532012-10-31 06:04:57 +0000908 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
909 macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
910 schedule_work(&bp->tx_error_task);
911 break;
912 }
913
914 if (status & MACB_BIT(TCOMP))
915 macb_tx_interrupt(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100916
917 /*
918 * Link change detection isn't possible with RMII, so we'll
919 * add that if/when we get our hands on a full-blown MII PHY.
920 */
921
Alexander Steinb19f7f72011-04-13 05:03:24 +0000922 if (status & MACB_BIT(ISR_ROVR)) {
923 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000924 if (macb_is_gem(bp))
925 bp->hw_stats.gem.rx_overruns++;
926 else
927 bp->hw_stats.macb.rx_overruns++;
Alexander Steinb19f7f72011-04-13 05:03:24 +0000928 }
929
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100930 if (status & MACB_BIT(HRESP)) {
931 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000932 * TODO: Reset the hardware, and maybe move the
933 * netdev_err to a lower-priority context as well
934 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100935 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000936 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100937 }
938
939 status = macb_readl(bp, ISR);
940 }
941
942 spin_unlock(&bp->lock);
943
944 return IRQ_HANDLED;
945}
946
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700947#ifdef CONFIG_NET_POLL_CONTROLLER
948/*
949 * Polling receive - used by netconsole and other diagnostic tools
950 * to allow network i/o with interrupts disabled.
951 */
952static void macb_poll_controller(struct net_device *dev)
953{
954 unsigned long flags;
955
956 local_irq_save(flags);
957 macb_interrupt(dev->irq, dev);
958 local_irq_restore(flags);
959}
960#endif
961
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100962static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
963{
964 struct macb *bp = netdev_priv(dev);
965 dma_addr_t mapping;
966 unsigned int len, entry;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000967 struct macb_dma_desc *desc;
968 struct macb_tx_skb *tx_skb;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100969 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -0700970 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100971
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000972#if defined(DEBUG) && defined(VERBOSE_DEBUG)
973 netdev_vdbg(bp->dev,
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000974 "start_xmit: len %u head %p data %p tail %p end %p\n",
975 skb->len, skb->head, skb->data,
976 skb_tail_pointer(skb), skb_end_pointer(skb));
977 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
978 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100979#endif
980
981 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -0700982 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100983
984 /* This is a hard error, log it. */
Nicolas Ferre909a8582012-11-19 06:00:21 +0000985 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100986 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -0700987 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000988 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
989 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
990 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +0000991 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100992 }
993
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000994 entry = macb_tx_ring_wrap(bp->tx_head);
995 bp->tx_head++;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000996 netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100997 mapping = dma_map_single(&bp->pdev->dev, skb->data,
998 len, DMA_TO_DEVICE);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000999
1000 tx_skb = &bp->tx_skb[entry];
1001 tx_skb->skb = skb;
1002 tx_skb->mapping = mapping;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001003 netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001004 skb->data, (unsigned long)mapping);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001005
1006 ctrl = MACB_BF(TX_FRMLEN, len);
1007 ctrl |= MACB_BIT(TX_LAST);
1008 if (entry == (TX_RING_SIZE - 1))
1009 ctrl |= MACB_BIT(TX_WRAP);
1010
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001011 desc = &bp->tx_ring[entry];
1012 desc->addr = mapping;
1013 desc->ctrl = ctrl;
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001014
1015 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001016 wmb();
1017
Richard Cochrane0720922011-06-19 21:51:28 +00001018 skb_tx_timestamp(skb);
1019
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001020 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1021
Nicolas Ferre909a8582012-11-19 06:00:21 +00001022 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001023 netif_stop_queue(dev);
1024
Dongdong Deng48719532009-08-23 19:49:07 -07001025 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001026
Patrick McHardy6ed10652009-06-23 06:03:08 +00001027 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001028}
1029
Nicolas Ferre4df95132013-06-04 21:57:12 +00001030static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001031{
1032 if (!macb_is_gem(bp)) {
1033 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1034 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001035 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001036
Nicolas Ferre1b447912013-06-04 21:57:11 +00001037 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001038 netdev_dbg(bp->dev,
1039 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001040 RX_BUFFER_MULTIPLE);
1041 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001042 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001043 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001044 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001045
1046 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1047 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001048}
1049
Nicolas Ferre4df95132013-06-04 21:57:12 +00001050static void gem_free_rx_buffers(struct macb *bp)
1051{
1052 struct sk_buff *skb;
1053 struct macb_dma_desc *desc;
1054 dma_addr_t addr;
1055 int i;
1056
1057 if (!bp->rx_skbuff)
1058 return;
1059
1060 for (i = 0; i < RX_RING_SIZE; i++) {
1061 skb = bp->rx_skbuff[i];
1062
1063 if (skb == NULL)
1064 continue;
1065
1066 desc = &bp->rx_ring[i];
1067 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1068 dma_unmap_single(&bp->pdev->dev, addr, skb->len,
1069 DMA_FROM_DEVICE);
1070 dev_kfree_skb_any(skb);
1071 skb = NULL;
1072 }
1073
1074 kfree(bp->rx_skbuff);
1075 bp->rx_skbuff = NULL;
1076}
1077
1078static void macb_free_rx_buffers(struct macb *bp)
1079{
1080 if (bp->rx_buffers) {
1081 dma_free_coherent(&bp->pdev->dev,
1082 RX_RING_SIZE * bp->rx_buffer_size,
1083 bp->rx_buffers, bp->rx_buffers_dma);
1084 bp->rx_buffers = NULL;
1085 }
1086}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001087
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001088static void macb_free_consistent(struct macb *bp)
1089{
1090 if (bp->tx_skb) {
1091 kfree(bp->tx_skb);
1092 bp->tx_skb = NULL;
1093 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001094 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001095 if (bp->rx_ring) {
1096 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1097 bp->rx_ring, bp->rx_ring_dma);
1098 bp->rx_ring = NULL;
1099 }
1100 if (bp->tx_ring) {
1101 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1102 bp->tx_ring, bp->tx_ring_dma);
1103 bp->tx_ring = NULL;
1104 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001105}
1106
1107static int gem_alloc_rx_buffers(struct macb *bp)
1108{
1109 int size;
1110
1111 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1112 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1113 if (!bp->rx_skbuff)
1114 return -ENOMEM;
1115 else
1116 netdev_dbg(bp->dev,
1117 "Allocated %d RX struct sk_buff entries at %p\n",
1118 RX_RING_SIZE, bp->rx_skbuff);
1119 return 0;
1120}
1121
1122static int macb_alloc_rx_buffers(struct macb *bp)
1123{
1124 int size;
1125
1126 size = RX_RING_SIZE * bp->rx_buffer_size;
1127 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1128 &bp->rx_buffers_dma, GFP_KERNEL);
1129 if (!bp->rx_buffers)
1130 return -ENOMEM;
1131 else
1132 netdev_dbg(bp->dev,
1133 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1134 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1135 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001136}
1137
1138static int macb_alloc_consistent(struct macb *bp)
1139{
1140 int size;
1141
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001142 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001143 bp->tx_skb = kmalloc(size, GFP_KERNEL);
1144 if (!bp->tx_skb)
1145 goto out_err;
1146
1147 size = RX_RING_BYTES;
1148 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1149 &bp->rx_ring_dma, GFP_KERNEL);
1150 if (!bp->rx_ring)
1151 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001152 netdev_dbg(bp->dev,
1153 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1154 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001155
1156 size = TX_RING_BYTES;
1157 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1158 &bp->tx_ring_dma, GFP_KERNEL);
1159 if (!bp->tx_ring)
1160 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001161 netdev_dbg(bp->dev,
1162 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
1163 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001164
Nicolas Ferre4df95132013-06-04 21:57:12 +00001165 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001166 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001167
1168 return 0;
1169
1170out_err:
1171 macb_free_consistent(bp);
1172 return -ENOMEM;
1173}
1174
Nicolas Ferre4df95132013-06-04 21:57:12 +00001175static void gem_init_rings(struct macb *bp)
1176{
1177 int i;
1178
1179 for (i = 0; i < TX_RING_SIZE; i++) {
1180 bp->tx_ring[i].addr = 0;
1181 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1182 }
1183 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1184
1185 bp->rx_tail = bp->rx_prepared_head = bp->tx_head = bp->tx_tail = 0;
1186
1187 gem_rx_refill(bp);
1188}
1189
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001190static void macb_init_rings(struct macb *bp)
1191{
1192 int i;
1193 dma_addr_t addr;
1194
1195 addr = bp->rx_buffers_dma;
1196 for (i = 0; i < RX_RING_SIZE; i++) {
1197 bp->rx_ring[i].addr = addr;
1198 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001199 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001200 }
1201 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1202
1203 for (i = 0; i < TX_RING_SIZE; i++) {
1204 bp->tx_ring[i].addr = 0;
1205 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1206 }
1207 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1208
1209 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
1210}
1211
1212static void macb_reset_hw(struct macb *bp)
1213{
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214 /*
1215 * Disable RX and TX (XXX: Should we halt the transmission
1216 * more gracefully?)
1217 */
1218 macb_writel(bp, NCR, 0);
1219
1220 /* Clear the stats registers (XXX: Update stats first?) */
1221 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1222
1223 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001224 macb_writel(bp, TSR, -1);
1225 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001226
1227 /* Disable all interrupts */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001228 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229 macb_readl(bp, ISR);
1230}
1231
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001232static u32 gem_mdc_clk_div(struct macb *bp)
1233{
1234 u32 config;
1235 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1236
1237 if (pclk_hz <= 20000000)
1238 config = GEM_BF(CLK, GEM_CLK_DIV8);
1239 else if (pclk_hz <= 40000000)
1240 config = GEM_BF(CLK, GEM_CLK_DIV16);
1241 else if (pclk_hz <= 80000000)
1242 config = GEM_BF(CLK, GEM_CLK_DIV32);
1243 else if (pclk_hz <= 120000000)
1244 config = GEM_BF(CLK, GEM_CLK_DIV48);
1245 else if (pclk_hz <= 160000000)
1246 config = GEM_BF(CLK, GEM_CLK_DIV64);
1247 else
1248 config = GEM_BF(CLK, GEM_CLK_DIV96);
1249
1250 return config;
1251}
1252
1253static u32 macb_mdc_clk_div(struct macb *bp)
1254{
1255 u32 config;
1256 unsigned long pclk_hz;
1257
1258 if (macb_is_gem(bp))
1259 return gem_mdc_clk_div(bp);
1260
1261 pclk_hz = clk_get_rate(bp->pclk);
1262 if (pclk_hz <= 20000000)
1263 config = MACB_BF(CLK, MACB_CLK_DIV8);
1264 else if (pclk_hz <= 40000000)
1265 config = MACB_BF(CLK, MACB_CLK_DIV16);
1266 else if (pclk_hz <= 80000000)
1267 config = MACB_BF(CLK, MACB_CLK_DIV32);
1268 else
1269 config = MACB_BF(CLK, MACB_CLK_DIV64);
1270
1271 return config;
1272}
1273
Jamie Iles757a03c2011-03-09 16:29:59 +00001274/*
1275 * Get the DMA bus width field of the network configuration register that we
1276 * should program. We find the width from decoding the design configuration
1277 * register to find the maximum supported data bus width.
1278 */
1279static u32 macb_dbw(struct macb *bp)
1280{
1281 if (!macb_is_gem(bp))
1282 return 0;
1283
1284 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1285 case 4:
1286 return GEM_BF(DBW, GEM_DBW128);
1287 case 2:
1288 return GEM_BF(DBW, GEM_DBW64);
1289 case 1:
1290 default:
1291 return GEM_BF(DBW, GEM_DBW32);
1292 }
1293}
1294
Jamie Iles0116da42011-03-14 17:38:30 +00001295/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001296 * Configure the receive DMA engine
1297 * - use the correct receive buffer size
1298 * - set the possibility to use INCR16 bursts
1299 * (if not supported by FIFO, it will fallback to default)
1300 * - set both rx/tx packet buffers to full memory size
1301 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001302 */
1303static void macb_configure_dma(struct macb *bp)
1304{
1305 u32 dmacfg;
1306
1307 if (macb_is_gem(bp)) {
1308 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001309 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001310 dmacfg |= GEM_BF(FBLDO, 16);
1311 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Steffen Trumtrara1ae3852013-03-27 23:07:06 +00001312 dmacfg &= ~GEM_BIT(ENDIA);
Jamie Iles0116da42011-03-14 17:38:30 +00001313 gem_writel(bp, DMACFG, dmacfg);
1314 }
1315}
1316
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001317/*
1318 * Configure peripheral capacities according to integration options used
1319 */
1320static void macb_configure_caps(struct macb *bp)
1321{
1322 if (macb_is_gem(bp)) {
Jongsung Kim01276ed2013-07-09 17:36:00 +09001323 if (GEM_BFEXT(IRQCOR, gem_readl(bp, DCFG1)) == 0)
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001324 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
1325 }
1326}
1327
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001328static void macb_init_hw(struct macb *bp)
1329{
1330 u32 config;
1331
1332 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001333 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001334
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001335 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001336 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001337 config |= MACB_BIT(PAE); /* PAuse Enable */
1338 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001339 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001340 if (bp->dev->flags & IFF_PROMISC)
1341 config |= MACB_BIT(CAF); /* Copy All Frames */
1342 if (!(bp->dev->flags & IFF_BROADCAST))
1343 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001344 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001345 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001346 bp->speed = SPEED_10;
1347 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001348
Jamie Iles0116da42011-03-14 17:38:30 +00001349 macb_configure_dma(bp);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001350 macb_configure_caps(bp);
Jamie Iles0116da42011-03-14 17:38:30 +00001351
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001352 /* Initialize TX and RX buffers */
1353 macb_writel(bp, RBQP, bp->rx_ring_dma);
1354 macb_writel(bp, TBQP, bp->tx_ring_dma);
1355
1356 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001357 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001358
1359 /* Enable interrupts */
Nicolas Ferree86cd532012-10-31 06:04:57 +00001360 macb_writel(bp, IER, (MACB_RX_INT_FLAGS
1361 | MACB_TX_INT_FLAGS
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001362 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001363
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001364}
1365
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001366/*
1367 * The hash address register is 64 bits long and takes up two
1368 * locations in the memory map. The least significant bits are stored
1369 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1370 *
1371 * The unicast hash enable and the multicast hash enable bits in the
1372 * network configuration register enable the reception of hash matched
1373 * frames. The destination address is reduced to a 6 bit index into
1374 * the 64 bit hash register using the following hash function. The
1375 * hash function is an exclusive or of every sixth bit of the
1376 * destination address.
1377 *
1378 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1379 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1380 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1381 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1382 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1383 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1384 *
1385 * da[0] represents the least significant bit of the first byte
1386 * received, that is, the multicast/unicast indicator, and da[47]
1387 * represents the most significant bit of the last byte received. If
1388 * the hash index, hi[n], points to a bit that is set in the hash
1389 * register then the frame will be matched according to whether the
1390 * frame is multicast or unicast. A multicast match will be signalled
1391 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1392 * index points to a bit set in the hash register. A unicast match
1393 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1394 * and the hash index points to a bit set in the hash register. To
1395 * receive all multicast frames, the hash register should be set with
1396 * all ones and the multicast hash enable bit should be set in the
1397 * network configuration register.
1398 */
1399
1400static inline int hash_bit_value(int bitnr, __u8 *addr)
1401{
1402 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1403 return 1;
1404 return 0;
1405}
1406
1407/*
1408 * Return the hash index value for the specified address.
1409 */
1410static int hash_get_index(__u8 *addr)
1411{
1412 int i, j, bitval;
1413 int hash_index = 0;
1414
1415 for (j = 0; j < 6; j++) {
1416 for (i = 0, bitval = 0; i < 8; i++)
1417 bitval ^= hash_bit_value(i*6 + j, addr);
1418
1419 hash_index |= (bitval << j);
1420 }
1421
1422 return hash_index;
1423}
1424
1425/*
1426 * Add multicast addresses to the internal multicast-hash table.
1427 */
1428static void macb_sethashtable(struct net_device *dev)
1429{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001430 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001431 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001432 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001433 struct macb *bp = netdev_priv(dev);
1434
1435 mc_filter[0] = mc_filter[1] = 0;
1436
Jiri Pirko22bedad32010-04-01 21:22:57 +00001437 netdev_for_each_mc_addr(ha, dev) {
1438 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001439 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1440 }
1441
Jamie Ilesf75ba502011-11-08 10:12:32 +00001442 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1443 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001444}
1445
1446/*
1447 * Enable/Disable promiscuous and multicast modes.
1448 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001449void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001450{
1451 unsigned long cfg;
1452 struct macb *bp = netdev_priv(dev);
1453
1454 cfg = macb_readl(bp, NCFGR);
1455
1456 if (dev->flags & IFF_PROMISC)
1457 /* Enable promiscuous mode */
1458 cfg |= MACB_BIT(CAF);
1459 else if (dev->flags & (~IFF_PROMISC))
1460 /* Disable promiscuous mode */
1461 cfg &= ~MACB_BIT(CAF);
1462
1463 if (dev->flags & IFF_ALLMULTI) {
1464 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001465 macb_or_gem_writel(bp, HRB, -1);
1466 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001467 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001468 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001469 /* Enable specific multicasts */
1470 macb_sethashtable(dev);
1471 cfg |= MACB_BIT(NCFGR_MTI);
1472 } else if (dev->flags & (~IFF_ALLMULTI)) {
1473 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001474 macb_or_gem_writel(bp, HRB, 0);
1475 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001476 cfg &= ~MACB_BIT(NCFGR_MTI);
1477 }
1478
1479 macb_writel(bp, NCFGR, cfg);
1480}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001481EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001482
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001483static int macb_open(struct net_device *dev)
1484{
1485 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001486 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001487 int err;
1488
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001489 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001490
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001491 /* carrier starts down */
1492 netif_carrier_off(dev);
1493
frederic RODO6c36a702007-07-12 19:07:24 +02001494 /* if the phy is not yet register, retry later*/
1495 if (!bp->phy_dev)
1496 return -EAGAIN;
1497
Nicolas Ferre1b447912013-06-04 21:57:11 +00001498 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001499 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001500
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001501 err = macb_alloc_consistent(bp);
1502 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001503 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1504 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001505 return err;
1506 }
1507
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001508 napi_enable(&bp->napi);
1509
Nicolas Ferre4df95132013-06-04 21:57:12 +00001510 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001511 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001512
frederic RODO6c36a702007-07-12 19:07:24 +02001513 /* schedule a link state check */
1514 phy_start(bp->phy_dev);
1515
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001516 netif_start_queue(dev);
1517
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001518 return 0;
1519}
1520
1521static int macb_close(struct net_device *dev)
1522{
1523 struct macb *bp = netdev_priv(dev);
1524 unsigned long flags;
1525
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001526 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001527 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001528
frederic RODO6c36a702007-07-12 19:07:24 +02001529 if (bp->phy_dev)
1530 phy_stop(bp->phy_dev);
1531
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001532 spin_lock_irqsave(&bp->lock, flags);
1533 macb_reset_hw(bp);
1534 netif_carrier_off(dev);
1535 spin_unlock_irqrestore(&bp->lock, flags);
1536
1537 macb_free_consistent(bp);
1538
1539 return 0;
1540}
1541
Jamie Ilesa494ed82011-03-09 16:26:35 +00001542static void gem_update_stats(struct macb *bp)
1543{
1544 u32 __iomem *reg = bp->regs + GEM_OTX;
1545 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1546 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1547
1548 for (; p < end; p++, reg++)
1549 *p += __raw_readl(reg);
1550}
1551
1552static struct net_device_stats *gem_get_stats(struct macb *bp)
1553{
1554 struct gem_stats *hwstat = &bp->hw_stats.gem;
1555 struct net_device_stats *nstat = &bp->stats;
1556
1557 gem_update_stats(bp);
1558
1559 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1560 hwstat->rx_alignment_errors +
1561 hwstat->rx_resource_errors +
1562 hwstat->rx_overruns +
1563 hwstat->rx_oversize_frames +
1564 hwstat->rx_jabbers +
1565 hwstat->rx_undersized_frames +
1566 hwstat->rx_length_field_frame_errors);
1567 nstat->tx_errors = (hwstat->tx_late_collisions +
1568 hwstat->tx_excessive_collisions +
1569 hwstat->tx_underrun +
1570 hwstat->tx_carrier_sense_errors);
1571 nstat->multicast = hwstat->rx_multicast_frames;
1572 nstat->collisions = (hwstat->tx_single_collision_frames +
1573 hwstat->tx_multiple_collision_frames +
1574 hwstat->tx_excessive_collisions);
1575 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1576 hwstat->rx_jabbers +
1577 hwstat->rx_undersized_frames +
1578 hwstat->rx_length_field_frame_errors);
1579 nstat->rx_over_errors = hwstat->rx_resource_errors;
1580 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1581 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1582 nstat->rx_fifo_errors = hwstat->rx_overruns;
1583 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1584 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1585 nstat->tx_fifo_errors = hwstat->tx_underrun;
1586
1587 return nstat;
1588}
1589
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001590struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001591{
1592 struct macb *bp = netdev_priv(dev);
1593 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001594 struct macb_stats *hwstat = &bp->hw_stats.macb;
1595
1596 if (macb_is_gem(bp))
1597 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001598
frederic RODO6c36a702007-07-12 19:07:24 +02001599 /* read stats from hardware */
1600 macb_update_stats(bp);
1601
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001602 /* Convert HW stats into netdevice stats */
1603 nstat->rx_errors = (hwstat->rx_fcs_errors +
1604 hwstat->rx_align_errors +
1605 hwstat->rx_resource_errors +
1606 hwstat->rx_overruns +
1607 hwstat->rx_oversize_pkts +
1608 hwstat->rx_jabbers +
1609 hwstat->rx_undersize_pkts +
1610 hwstat->sqe_test_errors +
1611 hwstat->rx_length_mismatch);
1612 nstat->tx_errors = (hwstat->tx_late_cols +
1613 hwstat->tx_excessive_cols +
1614 hwstat->tx_underruns +
1615 hwstat->tx_carrier_errors);
1616 nstat->collisions = (hwstat->tx_single_cols +
1617 hwstat->tx_multiple_cols +
1618 hwstat->tx_excessive_cols);
1619 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1620 hwstat->rx_jabbers +
1621 hwstat->rx_undersize_pkts +
1622 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001623 nstat->rx_over_errors = hwstat->rx_resource_errors +
1624 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001625 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1626 nstat->rx_frame_errors = hwstat->rx_align_errors;
1627 nstat->rx_fifo_errors = hwstat->rx_overruns;
1628 /* XXX: What does "missed" mean? */
1629 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1630 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1631 nstat->tx_fifo_errors = hwstat->tx_underruns;
1632 /* Don't know about heartbeat or window errors... */
1633
1634 return nstat;
1635}
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001636EXPORT_SYMBOL_GPL(macb_get_stats);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001637
1638static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1639{
1640 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001641 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001642
frederic RODO6c36a702007-07-12 19:07:24 +02001643 if (!phydev)
1644 return -ENODEV;
1645
1646 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001647}
1648
1649static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1650{
1651 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001652 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001653
frederic RODO6c36a702007-07-12 19:07:24 +02001654 if (!phydev)
1655 return -ENODEV;
1656
1657 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001658}
1659
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001660static int macb_get_regs_len(struct net_device *netdev)
1661{
1662 return MACB_GREGS_NBR * sizeof(u32);
1663}
1664
1665static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1666 void *p)
1667{
1668 struct macb *bp = netdev_priv(dev);
1669 unsigned int tail, head;
1670 u32 *regs_buff = p;
1671
1672 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
1673 | MACB_GREGS_VERSION;
1674
1675 tail = macb_tx_ring_wrap(bp->tx_tail);
1676 head = macb_tx_ring_wrap(bp->tx_head);
1677
1678 regs_buff[0] = macb_readl(bp, NCR);
1679 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
1680 regs_buff[2] = macb_readl(bp, NSR);
1681 regs_buff[3] = macb_readl(bp, TSR);
1682 regs_buff[4] = macb_readl(bp, RBQP);
1683 regs_buff[5] = macb_readl(bp, TBQP);
1684 regs_buff[6] = macb_readl(bp, RSR);
1685 regs_buff[7] = macb_readl(bp, IMR);
1686
1687 regs_buff[8] = tail;
1688 regs_buff[9] = head;
1689 regs_buff[10] = macb_tx_dma(bp, tail);
1690 regs_buff[11] = macb_tx_dma(bp, head);
1691
1692 if (macb_is_gem(bp)) {
1693 regs_buff[12] = gem_readl(bp, USRIO);
1694 regs_buff[13] = gem_readl(bp, DMACFG);
1695 }
1696}
1697
Joachim Eastwood0005f542012-10-18 11:01:12 +00001698const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001699 .get_settings = macb_get_settings,
1700 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001701 .get_regs_len = macb_get_regs_len,
1702 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001703 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00001704 .get_ts_info = ethtool_op_get_ts_info,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001705};
Joachim Eastwood0005f542012-10-18 11:01:12 +00001706EXPORT_SYMBOL_GPL(macb_ethtool_ops);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001707
Joachim Eastwood0005f542012-10-18 11:01:12 +00001708int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001709{
1710 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001711 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001712
1713 if (!netif_running(dev))
1714 return -EINVAL;
1715
frederic RODO6c36a702007-07-12 19:07:24 +02001716 if (!phydev)
1717 return -ENODEV;
1718
Richard Cochran28b04112010-07-17 08:48:55 +00001719 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001720}
Joachim Eastwood0005f542012-10-18 11:01:12 +00001721EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001722
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001723static const struct net_device_ops macb_netdev_ops = {
1724 .ndo_open = macb_open,
1725 .ndo_stop = macb_close,
1726 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001727 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001728 .ndo_get_stats = macb_get_stats,
1729 .ndo_do_ioctl = macb_ioctl,
1730 .ndo_validate_addr = eth_validate_addr,
1731 .ndo_change_mtu = eth_change_mtu,
1732 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001733#ifdef CONFIG_NET_POLL_CONTROLLER
1734 .ndo_poll_controller = macb_poll_controller,
1735#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001736};
1737
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001738#if defined(CONFIG_OF)
1739static const struct of_device_id macb_dt_ids[] = {
1740 { .compatible = "cdns,at32ap7000-macb" },
1741 { .compatible = "cdns,at91sam9260-macb" },
1742 { .compatible = "cdns,macb" },
1743 { .compatible = "cdns,pc302-gem" },
1744 { .compatible = "cdns,gem" },
1745 { /* sentinel */ }
1746};
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001747MODULE_DEVICE_TABLE(of, macb_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001748#endif
1749
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001750static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001751{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001752 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001753 struct resource *regs;
1754 struct net_device *dev;
1755 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001756 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001757 u32 config;
1758 int err = -ENXIO;
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001759 struct pinctrl *pinctrl;
Guenter Roeck50907042013-04-02 09:35:09 +00001760 const char *mac;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001761
1762 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1763 if (!regs) {
1764 dev_err(&pdev->dev, "no mmio resource defined\n");
1765 goto err_out;
1766 }
1767
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001768 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1769 if (IS_ERR(pinctrl)) {
1770 err = PTR_ERR(pinctrl);
1771 if (err == -EPROBE_DEFER)
1772 goto err_out;
1773
1774 dev_warn(&pdev->dev, "No pinctrl provided\n");
1775 }
1776
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001777 err = -ENOMEM;
1778 dev = alloc_etherdev(sizeof(*bp));
Joe Perches41de8d42012-01-29 13:47:52 +00001779 if (!dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001780 goto err_out;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001781
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001782 SET_NETDEV_DEV(dev, &pdev->dev);
1783
1784 /* TODO: Actually, we have some interesting features... */
1785 dev->features |= 0;
1786
1787 bp = netdev_priv(dev);
1788 bp->pdev = pdev;
1789 bp->dev = dev;
1790
1791 spin_lock_init(&bp->lock);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001792 INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001793
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001794 bp->pclk = devm_clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001795 if (IS_ERR(bp->pclk)) {
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001796 err = PTR_ERR(bp->pclk);
1797 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
Andrew Victor0cc86742007-02-07 16:40:44 +01001798 goto err_out_free_dev;
1799 }
Jamie Iles461845d2011-03-08 20:19:23 +00001800
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001801 bp->hclk = devm_clk_get(&pdev->dev, "hclk");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001802 if (IS_ERR(bp->hclk)) {
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001803 err = PTR_ERR(bp->hclk);
1804 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
1805 goto err_out_free_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001806 }
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001807
1808 err = clk_prepare_enable(bp->pclk);
1809 if (err) {
1810 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
1811 goto err_out_free_dev;
1812 }
1813
1814 err = clk_prepare_enable(bp->hclk);
1815 if (err) {
1816 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
1817 goto err_out_disable_pclk;
1818 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001819
Soren Brinkmann60fe7162013-12-10 16:07:21 -08001820 bp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001821 if (!bp->regs) {
1822 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1823 err = -ENOMEM;
1824 goto err_out_disable_clocks;
1825 }
1826
1827 dev->irq = platform_get_irq(pdev, 0);
Soren Brinkmann0a4acf02013-12-10 16:07:22 -08001828 err = devm_request_irq(&pdev->dev, dev->irq, macb_interrupt, 0,
1829 dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001830 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001831 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1832 dev->irq, err);
Soren Brinkmann60fe7162013-12-10 16:07:21 -08001833 goto err_out_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001834 }
1835
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001836 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001837 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001838 dev->ethtool_ops = &macb_ethtool_ops;
1839
1840 dev->base_addr = regs->start;
1841
Nicolas Ferre4df95132013-06-04 21:57:12 +00001842 /* setup appropriated routines according to adapter type */
1843 if (macb_is_gem(bp)) {
1844 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
1845 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
1846 bp->macbgem_ops.mog_init_rings = gem_init_rings;
1847 bp->macbgem_ops.mog_rx = gem_rx;
1848 } else {
1849 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
1850 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
1851 bp->macbgem_ops.mog_init_rings = macb_init_rings;
1852 bp->macbgem_ops.mog_rx = macb_rx;
1853 }
1854
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001855 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001856 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00001857 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001858 macb_writel(bp, NCFGR, config);
1859
Guenter Roeck50907042013-04-02 09:35:09 +00001860 mac = of_get_mac_address(pdev->dev.of_node);
1861 if (mac)
1862 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
1863 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001864 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02001865
Guenter Roeck50907042013-04-02 09:35:09 +00001866 err = of_get_phy_mode(pdev->dev.of_node);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001867 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09001868 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001869 if (pdata && pdata->is_rmii)
1870 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
1871 else
1872 bp->phy_interface = PHY_INTERFACE_MODE_MII;
1873 } else {
1874 bp->phy_interface = err;
1875 }
1876
Patrice Vilchez140b7552012-10-31 06:04:50 +00001877 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
1878 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
1879 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01001880#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001881 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1882 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01001883#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001884 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001885#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001886 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001887#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001888 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01001889#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001890 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001891#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001892
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001893 err = register_netdev(dev);
1894 if (err) {
1895 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Soren Brinkmann0a4acf02013-12-10 16:07:22 -08001896 goto err_out_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001897 }
1898
Nicolas Ferre72ca8202013-04-14 22:04:33 +00001899 err = macb_mii_init(bp);
1900 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02001901 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001902
frederic RODO6c36a702007-07-12 19:07:24 +02001903 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001904
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001905 netif_carrier_off(dev);
1906
Jamie Ilesf75ba502011-11-08 10:12:32 +00001907 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1908 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1909 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001910
frederic RODO6c36a702007-07-12 19:07:24 +02001911 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001912 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1913 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02001914
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001915 return 0;
1916
frederic RODO6c36a702007-07-12 19:07:24 +02001917err_out_unregister_netdev:
1918 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001919err_out_disable_clocks:
Steffen Trumtrarace58012013-03-27 23:07:07 +00001920 clk_disable_unprepare(bp->hclk);
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001921err_out_disable_pclk:
Steffen Trumtrarace58012013-03-27 23:07:07 +00001922 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001923err_out_free_dev:
1924 free_netdev(dev);
1925err_out:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001926 return err;
1927}
1928
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001929static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001930{
1931 struct net_device *dev;
1932 struct macb *bp;
1933
1934 dev = platform_get_drvdata(pdev);
1935
1936 if (dev) {
1937 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09001938 if (bp->phy_dev)
1939 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001940 mdiobus_unregister(bp->mii_bus);
1941 kfree(bp->mii_bus->irq);
1942 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001943 unregister_netdev(dev);
Steffen Trumtrarace58012013-03-27 23:07:07 +00001944 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00001945 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001946 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001947 }
1948
1949 return 0;
1950}
1951
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001952#ifdef CONFIG_PM
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08001953static int macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001954{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08001955 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001956 struct net_device *netdev = platform_get_drvdata(pdev);
1957 struct macb *bp = netdev_priv(netdev);
1958
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001959 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001960 netif_device_detach(netdev);
1961
Steffen Trumtrarace58012013-03-27 23:07:07 +00001962 clk_disable_unprepare(bp->hclk);
1963 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001964
1965 return 0;
1966}
1967
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08001968static int macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001969{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08001970 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001971 struct net_device *netdev = platform_get_drvdata(pdev);
1972 struct macb *bp = netdev_priv(netdev);
1973
Steffen Trumtrarace58012013-03-27 23:07:07 +00001974 clk_prepare_enable(bp->pclk);
1975 clk_prepare_enable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001976
1977 netif_device_attach(netdev);
1978
1979 return 0;
1980}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001981#endif
1982
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08001983static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
1984
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001985static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001986 .remove = __exit_p(macb_remove),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001987 .driver = {
1988 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07001989 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001990 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08001991 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001992 },
1993};
1994
Jingoo Hanb543a8d2013-03-04 16:43:18 +00001995module_platform_driver_probe(macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001996
1997MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00001998MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001999MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07002000MODULE_ALIAS("platform:macb");