blob: 13c3c332f58d1e10425ffc849e8b4efebf04e2ed [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>
17#include <linux/slab.h>
18#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000019#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010020#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010022#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000023#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020025#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080026#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010027#include <linux/of_device.h>
28#include <linux/of_net.h>
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +000029#include <linux/pinctrl/consumer.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010030
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010031#include "macb.h"
32
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010033#define RX_BUFFER_SIZE 128
Havard Skinnemoen55054a12012-10-31 06:04:55 +000034#define RX_RING_SIZE 512 /* must be power of 2 */
35#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010036
Havard Skinnemoen55054a12012-10-31 06:04:55 +000037#define TX_RING_SIZE 128 /* must be power of 2 */
38#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010039
40/* minimum number of free TX descriptors before waking up TX process */
41#define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
42
43#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
44 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000045#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
46 | MACB_BIT(ISR_RLE) \
47 | MACB_BIT(TXERR))
48#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
49
50/*
51 * Graceful stop timeouts in us. We should allow up to
52 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
53 */
54#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010055
Havard Skinnemoen55054a12012-10-31 06:04:55 +000056/* Ring buffer accessors */
57static unsigned int macb_tx_ring_wrap(unsigned int index)
58{
59 return index & (TX_RING_SIZE - 1);
60}
61
62static unsigned int macb_tx_ring_avail(struct macb *bp)
63{
64 return (bp->tx_tail - bp->tx_head) & (TX_RING_SIZE - 1);
65}
66
67static 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{
98 return bp->rx_buffers + RX_BUFFER_SIZE * macb_rx_ring_wrap(index);
99}
100
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100101static void __macb_set_hwaddr(struct macb *bp)
102{
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);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100110}
111
112static void __init macb_get_hwaddr(struct macb *bp)
113{
114 u32 bottom;
115 u16 top;
116 u8 addr[6];
117
Jamie Ilesf75ba502011-11-08 10:12:32 +0000118 bottom = macb_or_gem_readl(bp, SA1B);
119 top = macb_or_gem_readl(bp, SA1T);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100120
121 addr[0] = bottom & 0xff;
122 addr[1] = (bottom >> 8) & 0xff;
123 addr[2] = (bottom >> 16) & 0xff;
124 addr[3] = (bottom >> 24) & 0xff;
125 addr[4] = top & 0xff;
126 addr[5] = (top >> 8) & 0xff;
127
Sven Schnelled1d57412008-06-09 16:33:57 -0700128 if (is_valid_ether_addr(addr)) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100129 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
Sven Schnelled1d57412008-06-09 16:33:57 -0700130 } else {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000131 netdev_info(bp->dev, "invalid hw address, using random\n");
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000132 eth_hw_addr_random(bp->dev);
Sven Schnelled1d57412008-06-09 16:33:57 -0700133 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100134}
135
frederic RODO6c36a702007-07-12 19:07:24 +0200136static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100137{
frederic RODO6c36a702007-07-12 19:07:24 +0200138 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100139 int value;
140
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100141 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
142 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200143 | MACB_BF(PHYA, mii_id)
144 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100145 | MACB_BF(CODE, MACB_MAN_CODE)));
146
frederic RODO6c36a702007-07-12 19:07:24 +0200147 /* wait for end of transfer */
148 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
149 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100150
151 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100152
153 return value;
154}
155
frederic RODO6c36a702007-07-12 19:07:24 +0200156static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
157 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100158{
frederic RODO6c36a702007-07-12 19:07:24 +0200159 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100160
161 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
162 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200163 | MACB_BF(PHYA, mii_id)
164 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100165 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200166 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100167
frederic RODO6c36a702007-07-12 19:07:24 +0200168 /* wait for end of transfer */
169 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
170 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100171
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100172 return 0;
173}
174
frederic RODO6c36a702007-07-12 19:07:24 +0200175static int macb_mdio_reset(struct mii_bus *bus)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100176{
frederic RODO6c36a702007-07-12 19:07:24 +0200177 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100178}
179
frederic RODO6c36a702007-07-12 19:07:24 +0200180static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100181{
frederic RODO6c36a702007-07-12 19:07:24 +0200182 struct macb *bp = netdev_priv(dev);
183 struct phy_device *phydev = bp->phy_dev;
184 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100185
frederic RODO6c36a702007-07-12 19:07:24 +0200186 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100187
frederic RODO6c36a702007-07-12 19:07:24 +0200188 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100189
frederic RODO6c36a702007-07-12 19:07:24 +0200190 if (phydev->link) {
191 if ((bp->speed != phydev->speed) ||
192 (bp->duplex != phydev->duplex)) {
193 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100194
frederic RODO6c36a702007-07-12 19:07:24 +0200195 reg = macb_readl(bp, NCFGR);
196 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000197 if (macb_is_gem(bp))
198 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200199
200 if (phydev->duplex)
201 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900202 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200203 reg |= MACB_BIT(SPD);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000204 if (phydev->speed == SPEED_1000)
205 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200206
Patrice Vilchez140b7552012-10-31 06:04:50 +0000207 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200208
209 bp->speed = phydev->speed;
210 bp->duplex = phydev->duplex;
211 status_change = 1;
212 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100213 }
214
frederic RODO6c36a702007-07-12 19:07:24 +0200215 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700216 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200217 bp->speed = 0;
218 bp->duplex = -1;
219 }
220 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100221
frederic RODO6c36a702007-07-12 19:07:24 +0200222 status_change = 1;
223 }
224
225 spin_unlock_irqrestore(&bp->lock, flags);
226
227 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000228 if (phydev->link) {
229 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000230 netdev_info(dev, "link up (%d/%s)\n",
231 phydev->speed,
232 phydev->duplex == DUPLEX_FULL ?
233 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000234 } else {
235 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000236 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000237 }
frederic RODO6c36a702007-07-12 19:07:24 +0200238 }
239}
240
241/* based on au1000_eth. c*/
242static int macb_mii_probe(struct net_device *dev)
243{
244 struct macb *bp = netdev_priv(dev);
Jiri Pirko7455a762010-02-08 05:12:08 +0000245 struct phy_device *phydev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000246 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200247
Jiri Pirko7455a762010-02-08 05:12:08 +0000248 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200249 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000250 netdev_err(dev, "no PHY found\n");
frederic RODO6c36a702007-07-12 19:07:24 +0200251 return -1;
252 }
253
frederic RODO6c36a702007-07-12 19:07:24 +0200254 /* TODO : add pin_irq */
255
256 /* attach the mac to the phy */
Jiri Pirko7455a762010-02-08 05:12:08 +0000257 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100258 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000259 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000260 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000261 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200262 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100263
frederic RODO6c36a702007-07-12 19:07:24 +0200264 /* mask with MAC supported features */
Patrice Vilchez140b7552012-10-31 06:04:50 +0000265 if (macb_is_gem(bp))
266 phydev->supported &= PHY_GBIT_FEATURES;
267 else
268 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100269
frederic RODO6c36a702007-07-12 19:07:24 +0200270 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100271
frederic RODO6c36a702007-07-12 19:07:24 +0200272 bp->link = 0;
273 bp->speed = 0;
274 bp->duplex = -1;
275 bp->phy_dev = phydev;
276
277 return 0;
278}
279
Joachim Eastwood0005f542012-10-18 11:01:12 +0000280int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200281{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000282 struct macb_platform_data *pdata;
frederic RODO6c36a702007-07-12 19:07:24 +0200283 int err = -ENXIO, i;
284
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200285 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200286 macb_writel(bp, NCR, MACB_BIT(MPE));
287
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700288 bp->mii_bus = mdiobus_alloc();
289 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200290 err = -ENOMEM;
291 goto err_out;
292 }
293
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700294 bp->mii_bus->name = "MACB_mii_bus";
295 bp->mii_bus->read = &macb_mdio_read;
296 bp->mii_bus->write = &macb_mdio_write;
297 bp->mii_bus->reset = &macb_mdio_reset;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000298 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
299 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700300 bp->mii_bus->priv = bp;
301 bp->mii_bus->parent = &bp->dev->dev;
302 pdata = bp->pdev->dev.platform_data;
303
304 if (pdata)
305 bp->mii_bus->phy_mask = pdata->phy_mask;
306
307 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
308 if (!bp->mii_bus->irq) {
309 err = -ENOMEM;
310 goto err_out_free_mdiobus;
311 }
312
frederic RODO6c36a702007-07-12 19:07:24 +0200313 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700314 bp->mii_bus->irq[i] = PHY_POLL;
frederic RODO6c36a702007-07-12 19:07:24 +0200315
Jamie Iles91523942011-02-28 04:05:25 +0000316 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200317
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700318 if (mdiobus_register(bp->mii_bus))
frederic RODO6c36a702007-07-12 19:07:24 +0200319 goto err_out_free_mdio_irq;
320
321 if (macb_mii_probe(bp->dev) != 0) {
322 goto err_out_unregister_bus;
323 }
324
325 return 0;
326
327err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700328 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200329err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700330 kfree(bp->mii_bus->irq);
331err_out_free_mdiobus:
332 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200333err_out:
334 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100335}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000336EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100337
338static void macb_update_stats(struct macb *bp)
339{
340 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000341 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
342 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100343
344 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
345
346 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100347 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100348}
349
Nicolas Ferree86cd532012-10-31 06:04:57 +0000350static int macb_halt_tx(struct macb *bp)
351{
352 unsigned long halt_time, timeout;
353 u32 status;
354
355 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
356
357 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
358 do {
359 halt_time = jiffies;
360 status = macb_readl(bp, TSR);
361 if (!(status & MACB_BIT(TGO)))
362 return 0;
363
364 usleep_range(10, 250);
365 } while (time_before(halt_time, timeout));
366
367 return -ETIMEDOUT;
368}
369
370static void macb_tx_error_task(struct work_struct *work)
371{
372 struct macb *bp = container_of(work, struct macb, tx_error_task);
373 struct macb_tx_skb *tx_skb;
374 struct sk_buff *skb;
375 unsigned int tail;
376
377 netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
378 bp->tx_tail, bp->tx_head);
379
380 /* Make sure nobody is trying to queue up new packets */
381 netif_stop_queue(bp->dev);
382
383 /*
384 * Stop transmission now
385 * (in case we have just queued new packets)
386 */
387 if (macb_halt_tx(bp))
388 /* Just complain for now, reinitializing TX path can be good */
389 netdev_err(bp->dev, "BUG: halt tx timed out\n");
390
391 /* No need for the lock here as nobody will interrupt us anymore */
392
393 /*
394 * Treat frames in TX queue including the ones that caused the error.
395 * Free transmit buffers in upper layer.
396 */
397 for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
398 struct macb_dma_desc *desc;
399 u32 ctrl;
400
401 desc = macb_tx_desc(bp, tail);
402 ctrl = desc->ctrl;
403 tx_skb = macb_tx_skb(bp, tail);
404 skb = tx_skb->skb;
405
406 if (ctrl & MACB_BIT(TX_USED)) {
407 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
408 macb_tx_ring_wrap(tail), skb->data);
409 bp->stats.tx_packets++;
410 bp->stats.tx_bytes += skb->len;
411 } else {
412 /*
413 * "Buffers exhausted mid-frame" errors may only happen
414 * if the driver is buggy, so complain loudly about those.
415 * Statistics are updated by hardware.
416 */
417 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
418 netdev_err(bp->dev,
419 "BUG: TX buffers exhausted mid-frame\n");
420
421 desc->ctrl = ctrl | MACB_BIT(TX_USED);
422 }
423
424 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
425 DMA_TO_DEVICE);
426 tx_skb->skb = NULL;
427 dev_kfree_skb(skb);
428 }
429
430 /* Make descriptor updates visible to hardware */
431 wmb();
432
433 /* Reinitialize the TX desc queue */
434 macb_writel(bp, TBQP, bp->tx_ring_dma);
435 /* Make TX ring reflect state of hardware */
436 bp->tx_head = bp->tx_tail = 0;
437
438 /* Now we are ready to start transmission again */
439 netif_wake_queue(bp->dev);
440
441 /* Housework before enabling TX IRQ */
442 macb_writel(bp, TSR, macb_readl(bp, TSR));
443 macb_writel(bp, IER, MACB_TX_INT_FLAGS);
444}
445
446static void macb_tx_interrupt(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100447{
448 unsigned int tail;
449 unsigned int head;
450 u32 status;
451
452 status = macb_readl(bp, TSR);
453 macb_writel(bp, TSR, status);
454
Nicolas Ferree86cd532012-10-31 06:04:57 +0000455 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
456 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100457
458 head = bp->tx_head;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000459 for (tail = bp->tx_tail; tail != head; tail++) {
460 struct macb_tx_skb *tx_skb;
461 struct sk_buff *skb;
462 struct macb_dma_desc *desc;
463 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100464
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000465 desc = macb_tx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100466
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000467 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100468 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000469
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000470 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100471
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000472 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100473 break;
474
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000475 tx_skb = macb_tx_skb(bp, tail);
476 skb = tx_skb->skb;
477
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000478 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000479 macb_tx_ring_wrap(tail), skb->data);
480 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100481 DMA_TO_DEVICE);
482 bp->stats.tx_packets++;
483 bp->stats.tx_bytes += skb->len;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000484 tx_skb->skb = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100485 dev_kfree_skb_irq(skb);
486 }
487
488 bp->tx_tail = tail;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000489 if (netif_queue_stopped(bp->dev)
490 && macb_tx_ring_avail(bp) > MACB_TX_WAKEUP_THRESH)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100491 netif_wake_queue(bp->dev);
492}
493
494static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
495 unsigned int last_frag)
496{
497 unsigned int len;
498 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000499 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100500 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000501 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100502
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000503 desc = macb_rx_desc(bp, last_frag);
504 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100505
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000506 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000507 macb_rx_ring_wrap(first_frag),
508 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100509
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000510 /*
511 * The ethernet header starts NET_IP_ALIGN bytes into the
512 * first buffer. Since the header is 14 bytes, this makes the
513 * payload word-aligned.
514 *
515 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
516 * the two padding bytes into the skb so that we avoid hitting
517 * the slowpath in memcpy(), and pull them off afterwards.
518 */
519 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100520 if (!skb) {
521 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000522 for (frag = first_frag; ; frag++) {
523 desc = macb_rx_desc(bp, frag);
524 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100525 if (frag == last_frag)
526 break;
527 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000528
529 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100530 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000531
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100532 return 1;
533 }
534
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000535 offset = 0;
536 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700537 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100538 skb_put(skb, len);
539
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000540 for (frag = first_frag; ; frag++) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100541 unsigned int frag_len = RX_BUFFER_SIZE;
542
543 if (offset + frag_len > len) {
544 BUG_ON(frag != last_frag);
545 frag_len = len - offset;
546 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300547 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000548 macb_rx_buffer(bp, frag), frag_len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100549 offset += RX_BUFFER_SIZE;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000550 desc = macb_rx_desc(bp, frag);
551 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100552
553 if (frag == last_frag)
554 break;
555 }
556
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000557 /* Make descriptor updates visible to hardware */
558 wmb();
559
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000560 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100561 skb->protocol = eth_type_trans(skb, bp->dev);
562
563 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000564 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000565 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000566 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100567 netif_receive_skb(skb);
568
569 return 0;
570}
571
572/* Mark DMA descriptors from begin up to and not including end as unused */
573static void discard_partial_frame(struct macb *bp, unsigned int begin,
574 unsigned int end)
575{
576 unsigned int frag;
577
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000578 for (frag = begin; frag != end; frag++) {
579 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
580 desc->addr &= ~MACB_BIT(RX_USED);
581 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000582
583 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100584 wmb();
585
586 /*
587 * When this happens, the hardware stats registers for
588 * whatever caused this is updated, so we don't have to record
589 * anything.
590 */
591}
592
593static int macb_rx(struct macb *bp, int budget)
594{
595 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000596 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100597 int first_frag = -1;
598
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000599 for (tail = bp->rx_tail; budget > 0; tail++) {
600 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100601 u32 addr, ctrl;
602
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000603 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100604 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000605
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000606 addr = desc->addr;
607 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100608
609 if (!(addr & MACB_BIT(RX_USED)))
610 break;
611
612 if (ctrl & MACB_BIT(RX_SOF)) {
613 if (first_frag != -1)
614 discard_partial_frame(bp, first_frag, tail);
615 first_frag = tail;
616 }
617
618 if (ctrl & MACB_BIT(RX_EOF)) {
619 int dropped;
620 BUG_ON(first_frag == -1);
621
622 dropped = macb_rx_frame(bp, first_frag, tail);
623 first_frag = -1;
624 if (!dropped) {
625 received++;
626 budget--;
627 }
628 }
629 }
630
631 if (first_frag != -1)
632 bp->rx_tail = first_frag;
633 else
634 bp->rx_tail = tail;
635
636 return received;
637}
638
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700639static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100640{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700641 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700642 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100643 u32 status;
644
645 status = macb_readl(bp, RSR);
646 macb_writel(bp, RSR, status);
647
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700648 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100649
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000650 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000651 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100652
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700653 work_done = macb_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000654 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800655 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100656
Joshua Hokeb3363692010-10-25 01:44:22 +0000657 /*
658 * We've done what we can to clean the buffers. Make sure we
659 * get notified when new packets arrive.
660 */
661 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
662 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100663
664 /* TODO: Handle errors */
665
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700666 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100667}
668
669static irqreturn_t macb_interrupt(int irq, void *dev_id)
670{
671 struct net_device *dev = dev_id;
672 struct macb *bp = netdev_priv(dev);
673 u32 status;
674
675 status = macb_readl(bp, ISR);
676
677 if (unlikely(!status))
678 return IRQ_NONE;
679
680 spin_lock(&bp->lock);
681
682 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100683 /* close possible race with dev_close */
684 if (unlikely(!netif_running(dev))) {
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000685 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100686 break;
687 }
688
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000689 netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
690
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100691 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000692 /*
693 * There's no point taking any more interrupts
694 * until we have processed the buffers. The
695 * scheduling call may fail if the poll routine
696 * is already scheduled, so disable interrupts
697 * now.
698 */
699 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
700
Ben Hutchings288379f2009-01-19 16:43:59 -0800701 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000702 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800703 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100704 }
705 }
706
Nicolas Ferree86cd532012-10-31 06:04:57 +0000707 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
708 macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
709 schedule_work(&bp->tx_error_task);
710 break;
711 }
712
713 if (status & MACB_BIT(TCOMP))
714 macb_tx_interrupt(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100715
716 /*
717 * Link change detection isn't possible with RMII, so we'll
718 * add that if/when we get our hands on a full-blown MII PHY.
719 */
720
Alexander Steinb19f7f72011-04-13 05:03:24 +0000721 if (status & MACB_BIT(ISR_ROVR)) {
722 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000723 if (macb_is_gem(bp))
724 bp->hw_stats.gem.rx_overruns++;
725 else
726 bp->hw_stats.macb.rx_overruns++;
Alexander Steinb19f7f72011-04-13 05:03:24 +0000727 }
728
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100729 if (status & MACB_BIT(HRESP)) {
730 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000731 * TODO: Reset the hardware, and maybe move the
732 * netdev_err to a lower-priority context as well
733 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100734 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000735 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100736 }
737
738 status = macb_readl(bp, ISR);
739 }
740
741 spin_unlock(&bp->lock);
742
743 return IRQ_HANDLED;
744}
745
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700746#ifdef CONFIG_NET_POLL_CONTROLLER
747/*
748 * Polling receive - used by netconsole and other diagnostic tools
749 * to allow network i/o with interrupts disabled.
750 */
751static void macb_poll_controller(struct net_device *dev)
752{
753 unsigned long flags;
754
755 local_irq_save(flags);
756 macb_interrupt(dev->irq, dev);
757 local_irq_restore(flags);
758}
759#endif
760
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100761static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
762{
763 struct macb *bp = netdev_priv(dev);
764 dma_addr_t mapping;
765 unsigned int len, entry;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000766 struct macb_dma_desc *desc;
767 struct macb_tx_skb *tx_skb;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100768 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -0700769 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100770
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000771#if defined(DEBUG) && defined(VERBOSE_DEBUG)
772 netdev_vdbg(bp->dev,
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000773 "start_xmit: len %u head %p data %p tail %p end %p\n",
774 skb->len, skb->head, skb->data,
775 skb_tail_pointer(skb), skb_end_pointer(skb));
776 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
777 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100778#endif
779
780 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -0700781 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100782
783 /* This is a hard error, log it. */
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000784 if (macb_tx_ring_avail(bp) < 1) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100785 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -0700786 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000787 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
788 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
789 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +0000790 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100791 }
792
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000793 entry = macb_tx_ring_wrap(bp->tx_head);
794 bp->tx_head++;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000795 netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100796 mapping = dma_map_single(&bp->pdev->dev, skb->data,
797 len, DMA_TO_DEVICE);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000798
799 tx_skb = &bp->tx_skb[entry];
800 tx_skb->skb = skb;
801 tx_skb->mapping = mapping;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000802 netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000803 skb->data, (unsigned long)mapping);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100804
805 ctrl = MACB_BF(TX_FRMLEN, len);
806 ctrl |= MACB_BIT(TX_LAST);
807 if (entry == (TX_RING_SIZE - 1))
808 ctrl |= MACB_BIT(TX_WRAP);
809
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000810 desc = &bp->tx_ring[entry];
811 desc->addr = mapping;
812 desc->ctrl = ctrl;
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000813
814 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100815 wmb();
816
Richard Cochrane0720922011-06-19 21:51:28 +0000817 skb_tx_timestamp(skb);
818
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100819 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
820
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000821 if (macb_tx_ring_avail(bp) < 1)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100822 netif_stop_queue(dev);
823
Dongdong Deng48719532009-08-23 19:49:07 -0700824 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100825
Patrick McHardy6ed10652009-06-23 06:03:08 +0000826 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100827}
828
829static void macb_free_consistent(struct macb *bp)
830{
831 if (bp->tx_skb) {
832 kfree(bp->tx_skb);
833 bp->tx_skb = NULL;
834 }
835 if (bp->rx_ring) {
836 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
837 bp->rx_ring, bp->rx_ring_dma);
838 bp->rx_ring = NULL;
839 }
840 if (bp->tx_ring) {
841 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
842 bp->tx_ring, bp->tx_ring_dma);
843 bp->tx_ring = NULL;
844 }
845 if (bp->rx_buffers) {
846 dma_free_coherent(&bp->pdev->dev,
847 RX_RING_SIZE * RX_BUFFER_SIZE,
848 bp->rx_buffers, bp->rx_buffers_dma);
849 bp->rx_buffers = NULL;
850 }
851}
852
853static int macb_alloc_consistent(struct macb *bp)
854{
855 int size;
856
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000857 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100858 bp->tx_skb = kmalloc(size, GFP_KERNEL);
859 if (!bp->tx_skb)
860 goto out_err;
861
862 size = RX_RING_BYTES;
863 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
864 &bp->rx_ring_dma, GFP_KERNEL);
865 if (!bp->rx_ring)
866 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000867 netdev_dbg(bp->dev,
868 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
869 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100870
871 size = TX_RING_BYTES;
872 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
873 &bp->tx_ring_dma, GFP_KERNEL);
874 if (!bp->tx_ring)
875 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000876 netdev_dbg(bp->dev,
877 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
878 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100879
880 size = RX_RING_SIZE * RX_BUFFER_SIZE;
881 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
882 &bp->rx_buffers_dma, GFP_KERNEL);
883 if (!bp->rx_buffers)
884 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000885 netdev_dbg(bp->dev,
886 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
887 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100888
889 return 0;
890
891out_err:
892 macb_free_consistent(bp);
893 return -ENOMEM;
894}
895
896static void macb_init_rings(struct macb *bp)
897{
898 int i;
899 dma_addr_t addr;
900
901 addr = bp->rx_buffers_dma;
902 for (i = 0; i < RX_RING_SIZE; i++) {
903 bp->rx_ring[i].addr = addr;
904 bp->rx_ring[i].ctrl = 0;
905 addr += RX_BUFFER_SIZE;
906 }
907 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
908
909 for (i = 0; i < TX_RING_SIZE; i++) {
910 bp->tx_ring[i].addr = 0;
911 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
912 }
913 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
914
915 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
916}
917
918static void macb_reset_hw(struct macb *bp)
919{
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100920 /*
921 * Disable RX and TX (XXX: Should we halt the transmission
922 * more gracefully?)
923 */
924 macb_writel(bp, NCR, 0);
925
926 /* Clear the stats registers (XXX: Update stats first?) */
927 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
928
929 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000930 macb_writel(bp, TSR, -1);
931 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100932
933 /* Disable all interrupts */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000934 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100935 macb_readl(bp, ISR);
936}
937
Jamie Iles70c9f3d2011-03-09 16:22:54 +0000938static u32 gem_mdc_clk_div(struct macb *bp)
939{
940 u32 config;
941 unsigned long pclk_hz = clk_get_rate(bp->pclk);
942
943 if (pclk_hz <= 20000000)
944 config = GEM_BF(CLK, GEM_CLK_DIV8);
945 else if (pclk_hz <= 40000000)
946 config = GEM_BF(CLK, GEM_CLK_DIV16);
947 else if (pclk_hz <= 80000000)
948 config = GEM_BF(CLK, GEM_CLK_DIV32);
949 else if (pclk_hz <= 120000000)
950 config = GEM_BF(CLK, GEM_CLK_DIV48);
951 else if (pclk_hz <= 160000000)
952 config = GEM_BF(CLK, GEM_CLK_DIV64);
953 else
954 config = GEM_BF(CLK, GEM_CLK_DIV96);
955
956 return config;
957}
958
959static u32 macb_mdc_clk_div(struct macb *bp)
960{
961 u32 config;
962 unsigned long pclk_hz;
963
964 if (macb_is_gem(bp))
965 return gem_mdc_clk_div(bp);
966
967 pclk_hz = clk_get_rate(bp->pclk);
968 if (pclk_hz <= 20000000)
969 config = MACB_BF(CLK, MACB_CLK_DIV8);
970 else if (pclk_hz <= 40000000)
971 config = MACB_BF(CLK, MACB_CLK_DIV16);
972 else if (pclk_hz <= 80000000)
973 config = MACB_BF(CLK, MACB_CLK_DIV32);
974 else
975 config = MACB_BF(CLK, MACB_CLK_DIV64);
976
977 return config;
978}
979
Jamie Iles757a03c2011-03-09 16:29:59 +0000980/*
981 * Get the DMA bus width field of the network configuration register that we
982 * should program. We find the width from decoding the design configuration
983 * register to find the maximum supported data bus width.
984 */
985static u32 macb_dbw(struct macb *bp)
986{
987 if (!macb_is_gem(bp))
988 return 0;
989
990 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
991 case 4:
992 return GEM_BF(DBW, GEM_DBW128);
993 case 2:
994 return GEM_BF(DBW, GEM_DBW64);
995 case 1:
996 default:
997 return GEM_BF(DBW, GEM_DBW32);
998 }
999}
1000
Jamie Iles0116da42011-03-14 17:38:30 +00001001/*
1002 * Configure the receive DMA engine to use the correct receive buffer size.
1003 * This is a configurable parameter for GEM.
1004 */
1005static void macb_configure_dma(struct macb *bp)
1006{
1007 u32 dmacfg;
1008
1009 if (macb_is_gem(bp)) {
1010 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
1011 dmacfg |= GEM_BF(RXBS, RX_BUFFER_SIZE / 64);
1012 gem_writel(bp, DMACFG, dmacfg);
1013 }
1014}
1015
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001016static void macb_init_hw(struct macb *bp)
1017{
1018 u32 config;
1019
1020 macb_reset_hw(bp);
1021 __macb_set_hwaddr(bp);
1022
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001023 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001024 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001025 config |= MACB_BIT(PAE); /* PAuse Enable */
1026 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001027 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001028 if (bp->dev->flags & IFF_PROMISC)
1029 config |= MACB_BIT(CAF); /* Copy All Frames */
1030 if (!(bp->dev->flags & IFF_BROADCAST))
1031 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001032 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001033 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001034 bp->speed = SPEED_10;
1035 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001036
Jamie Iles0116da42011-03-14 17:38:30 +00001037 macb_configure_dma(bp);
1038
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001039 /* Initialize TX and RX buffers */
1040 macb_writel(bp, RBQP, bp->rx_ring_dma);
1041 macb_writel(bp, TBQP, bp->tx_ring_dma);
1042
1043 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001044 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001045
1046 /* Enable interrupts */
Nicolas Ferree86cd532012-10-31 06:04:57 +00001047 macb_writel(bp, IER, (MACB_RX_INT_FLAGS
1048 | MACB_TX_INT_FLAGS
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001049 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001050
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001051}
1052
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001053/*
1054 * The hash address register is 64 bits long and takes up two
1055 * locations in the memory map. The least significant bits are stored
1056 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1057 *
1058 * The unicast hash enable and the multicast hash enable bits in the
1059 * network configuration register enable the reception of hash matched
1060 * frames. The destination address is reduced to a 6 bit index into
1061 * the 64 bit hash register using the following hash function. The
1062 * hash function is an exclusive or of every sixth bit of the
1063 * destination address.
1064 *
1065 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1066 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1067 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1068 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1069 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1070 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1071 *
1072 * da[0] represents the least significant bit of the first byte
1073 * received, that is, the multicast/unicast indicator, and da[47]
1074 * represents the most significant bit of the last byte received. If
1075 * the hash index, hi[n], points to a bit that is set in the hash
1076 * register then the frame will be matched according to whether the
1077 * frame is multicast or unicast. A multicast match will be signalled
1078 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1079 * index points to a bit set in the hash register. A unicast match
1080 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1081 * and the hash index points to a bit set in the hash register. To
1082 * receive all multicast frames, the hash register should be set with
1083 * all ones and the multicast hash enable bit should be set in the
1084 * network configuration register.
1085 */
1086
1087static inline int hash_bit_value(int bitnr, __u8 *addr)
1088{
1089 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1090 return 1;
1091 return 0;
1092}
1093
1094/*
1095 * Return the hash index value for the specified address.
1096 */
1097static int hash_get_index(__u8 *addr)
1098{
1099 int i, j, bitval;
1100 int hash_index = 0;
1101
1102 for (j = 0; j < 6; j++) {
1103 for (i = 0, bitval = 0; i < 8; i++)
1104 bitval ^= hash_bit_value(i*6 + j, addr);
1105
1106 hash_index |= (bitval << j);
1107 }
1108
1109 return hash_index;
1110}
1111
1112/*
1113 * Add multicast addresses to the internal multicast-hash table.
1114 */
1115static void macb_sethashtable(struct net_device *dev)
1116{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001117 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001118 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001119 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001120 struct macb *bp = netdev_priv(dev);
1121
1122 mc_filter[0] = mc_filter[1] = 0;
1123
Jiri Pirko22bedad32010-04-01 21:22:57 +00001124 netdev_for_each_mc_addr(ha, dev) {
1125 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001126 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1127 }
1128
Jamie Ilesf75ba502011-11-08 10:12:32 +00001129 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1130 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001131}
1132
1133/*
1134 * Enable/Disable promiscuous and multicast modes.
1135 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001136void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001137{
1138 unsigned long cfg;
1139 struct macb *bp = netdev_priv(dev);
1140
1141 cfg = macb_readl(bp, NCFGR);
1142
1143 if (dev->flags & IFF_PROMISC)
1144 /* Enable promiscuous mode */
1145 cfg |= MACB_BIT(CAF);
1146 else if (dev->flags & (~IFF_PROMISC))
1147 /* Disable promiscuous mode */
1148 cfg &= ~MACB_BIT(CAF);
1149
1150 if (dev->flags & IFF_ALLMULTI) {
1151 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001152 macb_or_gem_writel(bp, HRB, -1);
1153 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001154 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001155 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001156 /* Enable specific multicasts */
1157 macb_sethashtable(dev);
1158 cfg |= MACB_BIT(NCFGR_MTI);
1159 } else if (dev->flags & (~IFF_ALLMULTI)) {
1160 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001161 macb_or_gem_writel(bp, HRB, 0);
1162 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001163 cfg &= ~MACB_BIT(NCFGR_MTI);
1164 }
1165
1166 macb_writel(bp, NCFGR, cfg);
1167}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001168EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001169
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001170static int macb_open(struct net_device *dev)
1171{
1172 struct macb *bp = netdev_priv(dev);
1173 int err;
1174
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001175 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001176
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001177 /* carrier starts down */
1178 netif_carrier_off(dev);
1179
frederic RODO6c36a702007-07-12 19:07:24 +02001180 /* if the phy is not yet register, retry later*/
1181 if (!bp->phy_dev)
1182 return -EAGAIN;
1183
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001184 if (!is_valid_ether_addr(dev->dev_addr))
1185 return -EADDRNOTAVAIL;
1186
1187 err = macb_alloc_consistent(bp);
1188 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001189 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1190 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001191 return err;
1192 }
1193
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001194 napi_enable(&bp->napi);
1195
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001196 macb_init_rings(bp);
1197 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001198
frederic RODO6c36a702007-07-12 19:07:24 +02001199 /* schedule a link state check */
1200 phy_start(bp->phy_dev);
1201
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001202 netif_start_queue(dev);
1203
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001204 return 0;
1205}
1206
1207static int macb_close(struct net_device *dev)
1208{
1209 struct macb *bp = netdev_priv(dev);
1210 unsigned long flags;
1211
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001212 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001213 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214
frederic RODO6c36a702007-07-12 19:07:24 +02001215 if (bp->phy_dev)
1216 phy_stop(bp->phy_dev);
1217
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001218 spin_lock_irqsave(&bp->lock, flags);
1219 macb_reset_hw(bp);
1220 netif_carrier_off(dev);
1221 spin_unlock_irqrestore(&bp->lock, flags);
1222
1223 macb_free_consistent(bp);
1224
1225 return 0;
1226}
1227
Jamie Ilesa494ed82011-03-09 16:26:35 +00001228static void gem_update_stats(struct macb *bp)
1229{
1230 u32 __iomem *reg = bp->regs + GEM_OTX;
1231 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1232 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1233
1234 for (; p < end; p++, reg++)
1235 *p += __raw_readl(reg);
1236}
1237
1238static struct net_device_stats *gem_get_stats(struct macb *bp)
1239{
1240 struct gem_stats *hwstat = &bp->hw_stats.gem;
1241 struct net_device_stats *nstat = &bp->stats;
1242
1243 gem_update_stats(bp);
1244
1245 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1246 hwstat->rx_alignment_errors +
1247 hwstat->rx_resource_errors +
1248 hwstat->rx_overruns +
1249 hwstat->rx_oversize_frames +
1250 hwstat->rx_jabbers +
1251 hwstat->rx_undersized_frames +
1252 hwstat->rx_length_field_frame_errors);
1253 nstat->tx_errors = (hwstat->tx_late_collisions +
1254 hwstat->tx_excessive_collisions +
1255 hwstat->tx_underrun +
1256 hwstat->tx_carrier_sense_errors);
1257 nstat->multicast = hwstat->rx_multicast_frames;
1258 nstat->collisions = (hwstat->tx_single_collision_frames +
1259 hwstat->tx_multiple_collision_frames +
1260 hwstat->tx_excessive_collisions);
1261 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1262 hwstat->rx_jabbers +
1263 hwstat->rx_undersized_frames +
1264 hwstat->rx_length_field_frame_errors);
1265 nstat->rx_over_errors = hwstat->rx_resource_errors;
1266 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1267 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1268 nstat->rx_fifo_errors = hwstat->rx_overruns;
1269 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1270 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1271 nstat->tx_fifo_errors = hwstat->tx_underrun;
1272
1273 return nstat;
1274}
1275
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001276static struct net_device_stats *macb_get_stats(struct net_device *dev)
1277{
1278 struct macb *bp = netdev_priv(dev);
1279 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001280 struct macb_stats *hwstat = &bp->hw_stats.macb;
1281
1282 if (macb_is_gem(bp))
1283 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001284
frederic RODO6c36a702007-07-12 19:07:24 +02001285 /* read stats from hardware */
1286 macb_update_stats(bp);
1287
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001288 /* Convert HW stats into netdevice stats */
1289 nstat->rx_errors = (hwstat->rx_fcs_errors +
1290 hwstat->rx_align_errors +
1291 hwstat->rx_resource_errors +
1292 hwstat->rx_overruns +
1293 hwstat->rx_oversize_pkts +
1294 hwstat->rx_jabbers +
1295 hwstat->rx_undersize_pkts +
1296 hwstat->sqe_test_errors +
1297 hwstat->rx_length_mismatch);
1298 nstat->tx_errors = (hwstat->tx_late_cols +
1299 hwstat->tx_excessive_cols +
1300 hwstat->tx_underruns +
1301 hwstat->tx_carrier_errors);
1302 nstat->collisions = (hwstat->tx_single_cols +
1303 hwstat->tx_multiple_cols +
1304 hwstat->tx_excessive_cols);
1305 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1306 hwstat->rx_jabbers +
1307 hwstat->rx_undersize_pkts +
1308 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001309 nstat->rx_over_errors = hwstat->rx_resource_errors +
1310 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001311 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1312 nstat->rx_frame_errors = hwstat->rx_align_errors;
1313 nstat->rx_fifo_errors = hwstat->rx_overruns;
1314 /* XXX: What does "missed" mean? */
1315 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1316 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1317 nstat->tx_fifo_errors = hwstat->tx_underruns;
1318 /* Don't know about heartbeat or window errors... */
1319
1320 return nstat;
1321}
1322
1323static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1324{
1325 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001326 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001327
frederic RODO6c36a702007-07-12 19:07:24 +02001328 if (!phydev)
1329 return -ENODEV;
1330
1331 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001332}
1333
1334static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1335{
1336 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001337 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001338
frederic RODO6c36a702007-07-12 19:07:24 +02001339 if (!phydev)
1340 return -ENODEV;
1341
1342 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001343}
1344
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001345static int macb_get_regs_len(struct net_device *netdev)
1346{
1347 return MACB_GREGS_NBR * sizeof(u32);
1348}
1349
1350static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1351 void *p)
1352{
1353 struct macb *bp = netdev_priv(dev);
1354 unsigned int tail, head;
1355 u32 *regs_buff = p;
1356
1357 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
1358 | MACB_GREGS_VERSION;
1359
1360 tail = macb_tx_ring_wrap(bp->tx_tail);
1361 head = macb_tx_ring_wrap(bp->tx_head);
1362
1363 regs_buff[0] = macb_readl(bp, NCR);
1364 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
1365 regs_buff[2] = macb_readl(bp, NSR);
1366 regs_buff[3] = macb_readl(bp, TSR);
1367 regs_buff[4] = macb_readl(bp, RBQP);
1368 regs_buff[5] = macb_readl(bp, TBQP);
1369 regs_buff[6] = macb_readl(bp, RSR);
1370 regs_buff[7] = macb_readl(bp, IMR);
1371
1372 regs_buff[8] = tail;
1373 regs_buff[9] = head;
1374 regs_buff[10] = macb_tx_dma(bp, tail);
1375 regs_buff[11] = macb_tx_dma(bp, head);
1376
1377 if (macb_is_gem(bp)) {
1378 regs_buff[12] = gem_readl(bp, USRIO);
1379 regs_buff[13] = gem_readl(bp, DMACFG);
1380 }
1381}
1382
Joachim Eastwood0005f542012-10-18 11:01:12 +00001383const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001384 .get_settings = macb_get_settings,
1385 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001386 .get_regs_len = macb_get_regs_len,
1387 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001388 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00001389 .get_ts_info = ethtool_op_get_ts_info,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001390};
Joachim Eastwood0005f542012-10-18 11:01:12 +00001391EXPORT_SYMBOL_GPL(macb_ethtool_ops);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001392
Joachim Eastwood0005f542012-10-18 11:01:12 +00001393int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001394{
1395 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001396 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001397
1398 if (!netif_running(dev))
1399 return -EINVAL;
1400
frederic RODO6c36a702007-07-12 19:07:24 +02001401 if (!phydev)
1402 return -ENODEV;
1403
Richard Cochran28b04112010-07-17 08:48:55 +00001404 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001405}
Joachim Eastwood0005f542012-10-18 11:01:12 +00001406EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001407
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001408static const struct net_device_ops macb_netdev_ops = {
1409 .ndo_open = macb_open,
1410 .ndo_stop = macb_close,
1411 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001412 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001413 .ndo_get_stats = macb_get_stats,
1414 .ndo_do_ioctl = macb_ioctl,
1415 .ndo_validate_addr = eth_validate_addr,
1416 .ndo_change_mtu = eth_change_mtu,
1417 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001418#ifdef CONFIG_NET_POLL_CONTROLLER
1419 .ndo_poll_controller = macb_poll_controller,
1420#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001421};
1422
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001423#if defined(CONFIG_OF)
1424static const struct of_device_id macb_dt_ids[] = {
1425 { .compatible = "cdns,at32ap7000-macb" },
1426 { .compatible = "cdns,at91sam9260-macb" },
1427 { .compatible = "cdns,macb" },
1428 { .compatible = "cdns,pc302-gem" },
1429 { .compatible = "cdns,gem" },
1430 { /* sentinel */ }
1431};
1432
1433MODULE_DEVICE_TABLE(of, macb_dt_ids);
1434
1435static int __devinit macb_get_phy_mode_dt(struct platform_device *pdev)
1436{
1437 struct device_node *np = pdev->dev.of_node;
1438
1439 if (np)
1440 return of_get_phy_mode(np);
1441
1442 return -ENODEV;
1443}
1444
1445static int __devinit macb_get_hwaddr_dt(struct macb *bp)
1446{
1447 struct device_node *np = bp->pdev->dev.of_node;
1448 if (np) {
1449 const char *mac = of_get_mac_address(np);
1450 if (mac) {
1451 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
1452 return 0;
1453 }
1454 }
1455
1456 return -ENODEV;
1457}
1458#else
1459static int __devinit macb_get_phy_mode_dt(struct platform_device *pdev)
1460{
1461 return -ENODEV;
1462}
1463static int __devinit macb_get_hwaddr_dt(struct macb *bp)
1464{
1465 return -ENODEV;
1466}
1467#endif
1468
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001469static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001470{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001471 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001472 struct resource *regs;
1473 struct net_device *dev;
1474 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001475 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001476 u32 config;
1477 int err = -ENXIO;
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001478 struct pinctrl *pinctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001479
1480 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1481 if (!regs) {
1482 dev_err(&pdev->dev, "no mmio resource defined\n");
1483 goto err_out;
1484 }
1485
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001486 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1487 if (IS_ERR(pinctrl)) {
1488 err = PTR_ERR(pinctrl);
1489 if (err == -EPROBE_DEFER)
1490 goto err_out;
1491
1492 dev_warn(&pdev->dev, "No pinctrl provided\n");
1493 }
1494
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001495 err = -ENOMEM;
1496 dev = alloc_etherdev(sizeof(*bp));
Joe Perches41de8d42012-01-29 13:47:52 +00001497 if (!dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001498 goto err_out;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001499
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001500 SET_NETDEV_DEV(dev, &pdev->dev);
1501
1502 /* TODO: Actually, we have some interesting features... */
1503 dev->features |= 0;
1504
1505 bp = netdev_priv(dev);
1506 bp->pdev = pdev;
1507 bp->dev = dev;
1508
1509 spin_lock_init(&bp->lock);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001510 INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001511
Jamie Iles461845d2011-03-08 20:19:23 +00001512 bp->pclk = clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001513 if (IS_ERR(bp->pclk)) {
1514 dev_err(&pdev->dev, "failed to get macb_clk\n");
1515 goto err_out_free_dev;
1516 }
1517 clk_enable(bp->pclk);
Jamie Iles461845d2011-03-08 20:19:23 +00001518
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001519 bp->hclk = clk_get(&pdev->dev, "hclk");
1520 if (IS_ERR(bp->hclk)) {
1521 dev_err(&pdev->dev, "failed to get hclk\n");
1522 goto err_out_put_pclk;
1523 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001524 clk_enable(bp->hclk);
1525
Joe Perches28f65c112011-06-09 09:13:32 -07001526 bp->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001527 if (!bp->regs) {
1528 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1529 err = -ENOMEM;
1530 goto err_out_disable_clocks;
1531 }
1532
1533 dev->irq = platform_get_irq(pdev, 0);
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00001534 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001535 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001536 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1537 dev->irq, err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001538 goto err_out_iounmap;
1539 }
1540
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001541 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001542 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001543 dev->ethtool_ops = &macb_ethtool_ops;
1544
1545 dev->base_addr = regs->start;
1546
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001547 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001548 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00001549 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001550 macb_writel(bp, NCFGR, config);
1551
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001552 err = macb_get_hwaddr_dt(bp);
1553 if (err < 0)
1554 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02001555
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001556 err = macb_get_phy_mode_dt(pdev);
1557 if (err < 0) {
1558 pdata = pdev->dev.platform_data;
1559 if (pdata && pdata->is_rmii)
1560 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
1561 else
1562 bp->phy_interface = PHY_INTERFACE_MODE_MII;
1563 } else {
1564 bp->phy_interface = err;
1565 }
1566
Patrice Vilchez140b7552012-10-31 06:04:50 +00001567 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
1568 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
1569 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01001570#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001571 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1572 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01001573#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001574 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001575#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001576 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001577#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001578 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01001579#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001580 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001581#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001582
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001583 err = register_netdev(dev);
1584 if (err) {
1585 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1586 goto err_out_free_irq;
1587 }
1588
frederic RODO6c36a702007-07-12 19:07:24 +02001589 if (macb_mii_init(bp) != 0) {
1590 goto err_out_unregister_netdev;
1591 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001592
frederic RODO6c36a702007-07-12 19:07:24 +02001593 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001594
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001595 netif_carrier_off(dev);
1596
Jamie Ilesf75ba502011-11-08 10:12:32 +00001597 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1598 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1599 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001600
frederic RODO6c36a702007-07-12 19:07:24 +02001601 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001602 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1603 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02001604
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001605 return 0;
1606
frederic RODO6c36a702007-07-12 19:07:24 +02001607err_out_unregister_netdev:
1608 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001609err_out_free_irq:
1610 free_irq(dev->irq, dev);
1611err_out_iounmap:
1612 iounmap(bp->regs);
1613err_out_disable_clocks:
1614 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001615 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001616 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001617err_out_put_pclk:
1618 clk_put(bp->pclk);
1619err_out_free_dev:
1620 free_netdev(dev);
1621err_out:
1622 platform_set_drvdata(pdev, NULL);
1623 return err;
1624}
1625
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001626static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001627{
1628 struct net_device *dev;
1629 struct macb *bp;
1630
1631 dev = platform_get_drvdata(pdev);
1632
1633 if (dev) {
1634 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09001635 if (bp->phy_dev)
1636 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001637 mdiobus_unregister(bp->mii_bus);
1638 kfree(bp->mii_bus->irq);
1639 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001640 unregister_netdev(dev);
1641 free_irq(dev->irq, dev);
1642 iounmap(bp->regs);
1643 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001644 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001645 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001646 clk_put(bp->pclk);
1647 free_netdev(dev);
1648 platform_set_drvdata(pdev, NULL);
1649 }
1650
1651 return 0;
1652}
1653
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001654#ifdef CONFIG_PM
1655static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1656{
1657 struct net_device *netdev = platform_get_drvdata(pdev);
1658 struct macb *bp = netdev_priv(netdev);
1659
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001660 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001661 netif_device_detach(netdev);
1662
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001663 clk_disable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001664 clk_disable(bp->pclk);
1665
1666 return 0;
1667}
1668
1669static int macb_resume(struct platform_device *pdev)
1670{
1671 struct net_device *netdev = platform_get_drvdata(pdev);
1672 struct macb *bp = netdev_priv(netdev);
1673
1674 clk_enable(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001675 clk_enable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001676
1677 netif_device_attach(netdev);
1678
1679 return 0;
1680}
1681#else
1682#define macb_suspend NULL
1683#define macb_resume NULL
1684#endif
1685
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001686static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001687 .remove = __exit_p(macb_remove),
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001688 .suspend = macb_suspend,
1689 .resume = macb_resume,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001690 .driver = {
1691 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07001692 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001693 .of_match_table = of_match_ptr(macb_dt_ids),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001694 },
1695};
1696
1697static int __init macb_init(void)
1698{
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001699 return platform_driver_probe(&macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001700}
1701
1702static void __exit macb_exit(void)
1703{
1704 platform_driver_unregister(&macb_driver);
1705}
1706
1707module_init(macb_init);
1708module_exit(macb_exit);
1709
1710MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00001711MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001712MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07001713MODULE_ALIAS("platform:macb");