blob: b9d4bb9530e5da5e2f30958a2fad6e5347b8af99 [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>
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +000020#include <linux/gpio.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000021#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010022#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000025#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020027#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080028#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010029#include <linux/of_device.h>
30#include <linux/of_net.h>
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +000031#include <linux/pinctrl/consumer.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010032
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010033#include "macb.h"
34
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010035#define RX_BUFFER_SIZE 128
Havard Skinnemoen55054a12012-10-31 06:04:55 +000036#define RX_RING_SIZE 512 /* must be power of 2 */
37#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010038
Havard Skinnemoen55054a12012-10-31 06:04:55 +000039#define TX_RING_SIZE 128 /* must be power of 2 */
40#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010041
Nicolas Ferre909a8582012-11-19 06:00:21 +000042/* level of occupied TX descriptors under which we wake up TX process */
43#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010044
45#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
46 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000047#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
48 | MACB_BIT(ISR_RLE) \
49 | MACB_BIT(TXERR))
50#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
51
52/*
53 * Graceful stop timeouts in us. We should allow up to
54 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
55 */
56#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010057
Havard Skinnemoen55054a12012-10-31 06:04:55 +000058/* Ring buffer accessors */
59static unsigned int macb_tx_ring_wrap(unsigned int index)
60{
61 return index & (TX_RING_SIZE - 1);
62}
63
Havard Skinnemoen55054a12012-10-31 06:04:55 +000064static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
65{
66 return &bp->tx_ring[macb_tx_ring_wrap(index)];
67}
68
69static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
70{
71 return &bp->tx_skb[macb_tx_ring_wrap(index)];
72}
73
74static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
75{
76 dma_addr_t offset;
77
78 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
79
80 return bp->tx_ring_dma + offset;
81}
82
83static unsigned int macb_rx_ring_wrap(unsigned int index)
84{
85 return index & (RX_RING_SIZE - 1);
86}
87
88static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
89{
90 return &bp->rx_ring[macb_rx_ring_wrap(index)];
91}
92
93static void *macb_rx_buffer(struct macb *bp, unsigned int index)
94{
95 return bp->rx_buffers + RX_BUFFER_SIZE * macb_rx_ring_wrap(index);
96}
97
Joachim Eastwood314bccc2012-11-07 08:14:52 +000098void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010099{
100 u32 bottom;
101 u16 top;
102
103 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000104 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100105 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000106 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000107
108 /* Clear unused address register sets */
109 macb_or_gem_writel(bp, SA2B, 0);
110 macb_or_gem_writel(bp, SA2T, 0);
111 macb_or_gem_writel(bp, SA3B, 0);
112 macb_or_gem_writel(bp, SA3T, 0);
113 macb_or_gem_writel(bp, SA4B, 0);
114 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100115}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000116EXPORT_SYMBOL_GPL(macb_set_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100117
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000118void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100119{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000120 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100121 u32 bottom;
122 u16 top;
123 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000124 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100125
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000126 pdata = bp->pdev->dev.platform_data;
127
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000128 /* Check all 4 address register for vaild address */
129 for (i = 0; i < 4; i++) {
130 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
131 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100132
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000133 if (pdata && pdata->rev_eth_addr) {
134 addr[5] = bottom & 0xff;
135 addr[4] = (bottom >> 8) & 0xff;
136 addr[3] = (bottom >> 16) & 0xff;
137 addr[2] = (bottom >> 24) & 0xff;
138 addr[1] = top & 0xff;
139 addr[0] = (top & 0xff00) >> 8;
140 } else {
141 addr[0] = bottom & 0xff;
142 addr[1] = (bottom >> 8) & 0xff;
143 addr[2] = (bottom >> 16) & 0xff;
144 addr[3] = (bottom >> 24) & 0xff;
145 addr[4] = top & 0xff;
146 addr[5] = (top >> 8) & 0xff;
147 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100148
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000149 if (is_valid_ether_addr(addr)) {
150 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
151 return;
152 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700153 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000154
155 netdev_info(bp->dev, "invalid hw address, using random\n");
156 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100157}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000158EXPORT_SYMBOL_GPL(macb_get_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100159
frederic RODO6c36a702007-07-12 19:07:24 +0200160static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100161{
frederic RODO6c36a702007-07-12 19:07:24 +0200162 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100163 int value;
164
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100165 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
166 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200167 | MACB_BF(PHYA, mii_id)
168 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100169 | MACB_BF(CODE, MACB_MAN_CODE)));
170
frederic RODO6c36a702007-07-12 19:07:24 +0200171 /* wait for end of transfer */
172 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
173 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100174
175 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100176
177 return value;
178}
179
frederic RODO6c36a702007-07-12 19:07:24 +0200180static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
181 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100182{
frederic RODO6c36a702007-07-12 19:07:24 +0200183 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100184
185 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
186 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200187 | MACB_BF(PHYA, mii_id)
188 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100189 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200190 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100191
frederic RODO6c36a702007-07-12 19:07:24 +0200192 /* wait for end of transfer */
193 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
194 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100195
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100196 return 0;
197}
198
frederic RODO6c36a702007-07-12 19:07:24 +0200199static int macb_mdio_reset(struct mii_bus *bus)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100200{
frederic RODO6c36a702007-07-12 19:07:24 +0200201 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100202}
203
frederic RODO6c36a702007-07-12 19:07:24 +0200204static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100205{
frederic RODO6c36a702007-07-12 19:07:24 +0200206 struct macb *bp = netdev_priv(dev);
207 struct phy_device *phydev = bp->phy_dev;
208 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100209
frederic RODO6c36a702007-07-12 19:07:24 +0200210 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100211
frederic RODO6c36a702007-07-12 19:07:24 +0200212 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100213
frederic RODO6c36a702007-07-12 19:07:24 +0200214 if (phydev->link) {
215 if ((bp->speed != phydev->speed) ||
216 (bp->duplex != phydev->duplex)) {
217 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100218
frederic RODO6c36a702007-07-12 19:07:24 +0200219 reg = macb_readl(bp, NCFGR);
220 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000221 if (macb_is_gem(bp))
222 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200223
224 if (phydev->duplex)
225 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900226 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200227 reg |= MACB_BIT(SPD);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000228 if (phydev->speed == SPEED_1000)
229 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200230
Patrice Vilchez140b7552012-10-31 06:04:50 +0000231 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200232
233 bp->speed = phydev->speed;
234 bp->duplex = phydev->duplex;
235 status_change = 1;
236 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100237 }
238
frederic RODO6c36a702007-07-12 19:07:24 +0200239 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700240 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200241 bp->speed = 0;
242 bp->duplex = -1;
243 }
244 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100245
frederic RODO6c36a702007-07-12 19:07:24 +0200246 status_change = 1;
247 }
248
249 spin_unlock_irqrestore(&bp->lock, flags);
250
251 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000252 if (phydev->link) {
253 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000254 netdev_info(dev, "link up (%d/%s)\n",
255 phydev->speed,
256 phydev->duplex == DUPLEX_FULL ?
257 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000258 } else {
259 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000260 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000261 }
frederic RODO6c36a702007-07-12 19:07:24 +0200262 }
263}
264
265/* based on au1000_eth. c*/
266static int macb_mii_probe(struct net_device *dev)
267{
268 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000269 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000270 struct phy_device *phydev;
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000271 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000272 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200273
Jiri Pirko7455a762010-02-08 05:12:08 +0000274 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200275 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000276 netdev_err(dev, "no PHY found\n");
frederic RODO6c36a702007-07-12 19:07:24 +0200277 return -1;
278 }
279
Joachim Eastwood2dbfdbb2012-11-11 13:56:27 +0000280 pdata = dev_get_platdata(&bp->pdev->dev);
281 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
282 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
283 if (!ret) {
284 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
285 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
286 }
287 }
frederic RODO6c36a702007-07-12 19:07:24 +0200288
289 /* attach the mac to the phy */
Jiri Pirko7455a762010-02-08 05:12:08 +0000290 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100291 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000292 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000293 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000294 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200295 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100296
frederic RODO6c36a702007-07-12 19:07:24 +0200297 /* mask with MAC supported features */
Patrice Vilchez140b7552012-10-31 06:04:50 +0000298 if (macb_is_gem(bp))
299 phydev->supported &= PHY_GBIT_FEATURES;
300 else
301 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100302
frederic RODO6c36a702007-07-12 19:07:24 +0200303 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100304
frederic RODO6c36a702007-07-12 19:07:24 +0200305 bp->link = 0;
306 bp->speed = 0;
307 bp->duplex = -1;
308 bp->phy_dev = phydev;
309
310 return 0;
311}
312
Joachim Eastwood0005f542012-10-18 11:01:12 +0000313int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200314{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000315 struct macb_platform_data *pdata;
frederic RODO6c36a702007-07-12 19:07:24 +0200316 int err = -ENXIO, i;
317
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200318 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200319 macb_writel(bp, NCR, MACB_BIT(MPE));
320
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700321 bp->mii_bus = mdiobus_alloc();
322 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200323 err = -ENOMEM;
324 goto err_out;
325 }
326
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700327 bp->mii_bus->name = "MACB_mii_bus";
328 bp->mii_bus->read = &macb_mdio_read;
329 bp->mii_bus->write = &macb_mdio_write;
330 bp->mii_bus->reset = &macb_mdio_reset;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000331 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
332 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700333 bp->mii_bus->priv = bp;
334 bp->mii_bus->parent = &bp->dev->dev;
335 pdata = bp->pdev->dev.platform_data;
336
337 if (pdata)
338 bp->mii_bus->phy_mask = pdata->phy_mask;
339
340 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
341 if (!bp->mii_bus->irq) {
342 err = -ENOMEM;
343 goto err_out_free_mdiobus;
344 }
345
frederic RODO6c36a702007-07-12 19:07:24 +0200346 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700347 bp->mii_bus->irq[i] = PHY_POLL;
frederic RODO6c36a702007-07-12 19:07:24 +0200348
Jamie Iles91523942011-02-28 04:05:25 +0000349 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200350
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700351 if (mdiobus_register(bp->mii_bus))
frederic RODO6c36a702007-07-12 19:07:24 +0200352 goto err_out_free_mdio_irq;
353
354 if (macb_mii_probe(bp->dev) != 0) {
355 goto err_out_unregister_bus;
356 }
357
358 return 0;
359
360err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700361 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200362err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700363 kfree(bp->mii_bus->irq);
364err_out_free_mdiobus:
365 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200366err_out:
367 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100368}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000369EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100370
371static void macb_update_stats(struct macb *bp)
372{
373 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000374 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
375 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100376
377 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
378
379 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100380 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100381}
382
Nicolas Ferree86cd532012-10-31 06:04:57 +0000383static int macb_halt_tx(struct macb *bp)
384{
385 unsigned long halt_time, timeout;
386 u32 status;
387
388 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
389
390 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
391 do {
392 halt_time = jiffies;
393 status = macb_readl(bp, TSR);
394 if (!(status & MACB_BIT(TGO)))
395 return 0;
396
397 usleep_range(10, 250);
398 } while (time_before(halt_time, timeout));
399
400 return -ETIMEDOUT;
401}
402
403static void macb_tx_error_task(struct work_struct *work)
404{
405 struct macb *bp = container_of(work, struct macb, tx_error_task);
406 struct macb_tx_skb *tx_skb;
407 struct sk_buff *skb;
408 unsigned int tail;
409
410 netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
411 bp->tx_tail, bp->tx_head);
412
413 /* Make sure nobody is trying to queue up new packets */
414 netif_stop_queue(bp->dev);
415
416 /*
417 * Stop transmission now
418 * (in case we have just queued new packets)
419 */
420 if (macb_halt_tx(bp))
421 /* Just complain for now, reinitializing TX path can be good */
422 netdev_err(bp->dev, "BUG: halt tx timed out\n");
423
424 /* No need for the lock here as nobody will interrupt us anymore */
425
426 /*
427 * Treat frames in TX queue including the ones that caused the error.
428 * Free transmit buffers in upper layer.
429 */
430 for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
431 struct macb_dma_desc *desc;
432 u32 ctrl;
433
434 desc = macb_tx_desc(bp, tail);
435 ctrl = desc->ctrl;
436 tx_skb = macb_tx_skb(bp, tail);
437 skb = tx_skb->skb;
438
439 if (ctrl & MACB_BIT(TX_USED)) {
440 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
441 macb_tx_ring_wrap(tail), skb->data);
442 bp->stats.tx_packets++;
443 bp->stats.tx_bytes += skb->len;
444 } else {
445 /*
446 * "Buffers exhausted mid-frame" errors may only happen
447 * if the driver is buggy, so complain loudly about those.
448 * Statistics are updated by hardware.
449 */
450 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
451 netdev_err(bp->dev,
452 "BUG: TX buffers exhausted mid-frame\n");
453
454 desc->ctrl = ctrl | MACB_BIT(TX_USED);
455 }
456
457 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
458 DMA_TO_DEVICE);
459 tx_skb->skb = NULL;
460 dev_kfree_skb(skb);
461 }
462
463 /* Make descriptor updates visible to hardware */
464 wmb();
465
466 /* Reinitialize the TX desc queue */
467 macb_writel(bp, TBQP, bp->tx_ring_dma);
468 /* Make TX ring reflect state of hardware */
469 bp->tx_head = bp->tx_tail = 0;
470
471 /* Now we are ready to start transmission again */
472 netif_wake_queue(bp->dev);
473
474 /* Housework before enabling TX IRQ */
475 macb_writel(bp, TSR, macb_readl(bp, TSR));
476 macb_writel(bp, IER, MACB_TX_INT_FLAGS);
477}
478
479static void macb_tx_interrupt(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100480{
481 unsigned int tail;
482 unsigned int head;
483 u32 status;
484
485 status = macb_readl(bp, TSR);
486 macb_writel(bp, TSR, status);
487
Nicolas Ferree86cd532012-10-31 06:04:57 +0000488 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
489 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100490
491 head = bp->tx_head;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000492 for (tail = bp->tx_tail; tail != head; tail++) {
493 struct macb_tx_skb *tx_skb;
494 struct sk_buff *skb;
495 struct macb_dma_desc *desc;
496 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100497
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000498 desc = macb_tx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100499
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000500 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100501 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000502
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000503 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100504
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000505 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100506 break;
507
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000508 tx_skb = macb_tx_skb(bp, tail);
509 skb = tx_skb->skb;
510
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000511 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000512 macb_tx_ring_wrap(tail), skb->data);
513 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100514 DMA_TO_DEVICE);
515 bp->stats.tx_packets++;
516 bp->stats.tx_bytes += skb->len;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000517 tx_skb->skb = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100518 dev_kfree_skb_irq(skb);
519 }
520
521 bp->tx_tail = tail;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000522 if (netif_queue_stopped(bp->dev)
Nicolas Ferre909a8582012-11-19 06:00:21 +0000523 && CIRC_CNT(bp->tx_head, bp->tx_tail,
524 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100525 netif_wake_queue(bp->dev);
526}
527
528static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
529 unsigned int last_frag)
530{
531 unsigned int len;
532 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000533 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100534 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000535 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100536
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000537 desc = macb_rx_desc(bp, last_frag);
538 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100539
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000540 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000541 macb_rx_ring_wrap(first_frag),
542 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100543
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000544 /*
545 * The ethernet header starts NET_IP_ALIGN bytes into the
546 * first buffer. Since the header is 14 bytes, this makes the
547 * payload word-aligned.
548 *
549 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
550 * the two padding bytes into the skb so that we avoid hitting
551 * the slowpath in memcpy(), and pull them off afterwards.
552 */
553 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100554 if (!skb) {
555 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000556 for (frag = first_frag; ; frag++) {
557 desc = macb_rx_desc(bp, frag);
558 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100559 if (frag == last_frag)
560 break;
561 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000562
563 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100564 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000565
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100566 return 1;
567 }
568
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000569 offset = 0;
570 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700571 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100572 skb_put(skb, len);
573
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000574 for (frag = first_frag; ; frag++) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100575 unsigned int frag_len = RX_BUFFER_SIZE;
576
577 if (offset + frag_len > len) {
578 BUG_ON(frag != last_frag);
579 frag_len = len - offset;
580 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300581 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000582 macb_rx_buffer(bp, frag), frag_len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100583 offset += RX_BUFFER_SIZE;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000584 desc = macb_rx_desc(bp, frag);
585 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100586
587 if (frag == last_frag)
588 break;
589 }
590
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000591 /* Make descriptor updates visible to hardware */
592 wmb();
593
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000594 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100595 skb->protocol = eth_type_trans(skb, bp->dev);
596
597 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000598 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000599 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000600 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100601 netif_receive_skb(skb);
602
603 return 0;
604}
605
606/* Mark DMA descriptors from begin up to and not including end as unused */
607static void discard_partial_frame(struct macb *bp, unsigned int begin,
608 unsigned int end)
609{
610 unsigned int frag;
611
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000612 for (frag = begin; frag != end; frag++) {
613 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
614 desc->addr &= ~MACB_BIT(RX_USED);
615 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000616
617 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100618 wmb();
619
620 /*
621 * When this happens, the hardware stats registers for
622 * whatever caused this is updated, so we don't have to record
623 * anything.
624 */
625}
626
627static int macb_rx(struct macb *bp, int budget)
628{
629 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000630 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100631 int first_frag = -1;
632
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000633 for (tail = bp->rx_tail; budget > 0; tail++) {
634 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100635 u32 addr, ctrl;
636
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000637 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100638 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000639
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000640 addr = desc->addr;
641 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100642
643 if (!(addr & MACB_BIT(RX_USED)))
644 break;
645
646 if (ctrl & MACB_BIT(RX_SOF)) {
647 if (first_frag != -1)
648 discard_partial_frame(bp, first_frag, tail);
649 first_frag = tail;
650 }
651
652 if (ctrl & MACB_BIT(RX_EOF)) {
653 int dropped;
654 BUG_ON(first_frag == -1);
655
656 dropped = macb_rx_frame(bp, first_frag, tail);
657 first_frag = -1;
658 if (!dropped) {
659 received++;
660 budget--;
661 }
662 }
663 }
664
665 if (first_frag != -1)
666 bp->rx_tail = first_frag;
667 else
668 bp->rx_tail = tail;
669
670 return received;
671}
672
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700673static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700675 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700676 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100677 u32 status;
678
679 status = macb_readl(bp, RSR);
680 macb_writel(bp, RSR, status);
681
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700682 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100683
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000684 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000685 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100686
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700687 work_done = macb_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000688 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800689 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100690
Joshua Hokeb3363692010-10-25 01:44:22 +0000691 /*
692 * We've done what we can to clean the buffers. Make sure we
693 * get notified when new packets arrive.
694 */
695 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
Nicolas Ferre8770e912013-02-12 11:08:48 +0100696
697 /* Packets received while interrupts were disabled */
698 status = macb_readl(bp, RSR);
699 if (unlikely(status))
700 napi_reschedule(napi);
Joshua Hokeb3363692010-10-25 01:44:22 +0000701 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100702
703 /* TODO: Handle errors */
704
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700705 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100706}
707
708static irqreturn_t macb_interrupt(int irq, void *dev_id)
709{
710 struct net_device *dev = dev_id;
711 struct macb *bp = netdev_priv(dev);
712 u32 status;
713
714 status = macb_readl(bp, ISR);
715
716 if (unlikely(!status))
717 return IRQ_NONE;
718
719 spin_lock(&bp->lock);
720
721 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100722 /* close possible race with dev_close */
723 if (unlikely(!netif_running(dev))) {
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000724 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100725 break;
726 }
727
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000728 netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
729
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100730 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000731 /*
732 * There's no point taking any more interrupts
733 * until we have processed the buffers. The
734 * scheduling call may fail if the poll routine
735 * is already scheduled, so disable interrupts
736 * now.
737 */
738 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
739
Ben Hutchings288379f2009-01-19 16:43:59 -0800740 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000741 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800742 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100743 }
744 }
745
Nicolas Ferree86cd532012-10-31 06:04:57 +0000746 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
747 macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
748 schedule_work(&bp->tx_error_task);
749 break;
750 }
751
752 if (status & MACB_BIT(TCOMP))
753 macb_tx_interrupt(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100754
755 /*
756 * Link change detection isn't possible with RMII, so we'll
757 * add that if/when we get our hands on a full-blown MII PHY.
758 */
759
Alexander Steinb19f7f72011-04-13 05:03:24 +0000760 if (status & MACB_BIT(ISR_ROVR)) {
761 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000762 if (macb_is_gem(bp))
763 bp->hw_stats.gem.rx_overruns++;
764 else
765 bp->hw_stats.macb.rx_overruns++;
Alexander Steinb19f7f72011-04-13 05:03:24 +0000766 }
767
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100768 if (status & MACB_BIT(HRESP)) {
769 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000770 * TODO: Reset the hardware, and maybe move the
771 * netdev_err to a lower-priority context as well
772 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100773 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000774 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100775 }
776
777 status = macb_readl(bp, ISR);
778 }
779
780 spin_unlock(&bp->lock);
781
782 return IRQ_HANDLED;
783}
784
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700785#ifdef CONFIG_NET_POLL_CONTROLLER
786/*
787 * Polling receive - used by netconsole and other diagnostic tools
788 * to allow network i/o with interrupts disabled.
789 */
790static void macb_poll_controller(struct net_device *dev)
791{
792 unsigned long flags;
793
794 local_irq_save(flags);
795 macb_interrupt(dev->irq, dev);
796 local_irq_restore(flags);
797}
798#endif
799
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100800static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
801{
802 struct macb *bp = netdev_priv(dev);
803 dma_addr_t mapping;
804 unsigned int len, entry;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000805 struct macb_dma_desc *desc;
806 struct macb_tx_skb *tx_skb;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100807 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -0700808 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100809
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000810#if defined(DEBUG) && defined(VERBOSE_DEBUG)
811 netdev_vdbg(bp->dev,
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000812 "start_xmit: len %u head %p data %p tail %p end %p\n",
813 skb->len, skb->head, skb->data,
814 skb_tail_pointer(skb), skb_end_pointer(skb));
815 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
816 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100817#endif
818
819 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -0700820 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100821
822 /* This is a hard error, log it. */
Nicolas Ferre909a8582012-11-19 06:00:21 +0000823 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100824 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -0700825 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000826 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
827 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
828 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +0000829 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100830 }
831
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000832 entry = macb_tx_ring_wrap(bp->tx_head);
833 bp->tx_head++;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000834 netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100835 mapping = dma_map_single(&bp->pdev->dev, skb->data,
836 len, DMA_TO_DEVICE);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000837
838 tx_skb = &bp->tx_skb[entry];
839 tx_skb->skb = skb;
840 tx_skb->mapping = mapping;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000841 netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000842 skb->data, (unsigned long)mapping);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100843
844 ctrl = MACB_BF(TX_FRMLEN, len);
845 ctrl |= MACB_BIT(TX_LAST);
846 if (entry == (TX_RING_SIZE - 1))
847 ctrl |= MACB_BIT(TX_WRAP);
848
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000849 desc = &bp->tx_ring[entry];
850 desc->addr = mapping;
851 desc->ctrl = ctrl;
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000852
853 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100854 wmb();
855
Richard Cochrane0720922011-06-19 21:51:28 +0000856 skb_tx_timestamp(skb);
857
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100858 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
859
Nicolas Ferre909a8582012-11-19 06:00:21 +0000860 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100861 netif_stop_queue(dev);
862
Dongdong Deng48719532009-08-23 19:49:07 -0700863 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100864
Patrick McHardy6ed10652009-06-23 06:03:08 +0000865 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100866}
867
868static void macb_free_consistent(struct macb *bp)
869{
870 if (bp->tx_skb) {
871 kfree(bp->tx_skb);
872 bp->tx_skb = NULL;
873 }
874 if (bp->rx_ring) {
875 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
876 bp->rx_ring, bp->rx_ring_dma);
877 bp->rx_ring = NULL;
878 }
879 if (bp->tx_ring) {
880 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
881 bp->tx_ring, bp->tx_ring_dma);
882 bp->tx_ring = NULL;
883 }
884 if (bp->rx_buffers) {
885 dma_free_coherent(&bp->pdev->dev,
886 RX_RING_SIZE * RX_BUFFER_SIZE,
887 bp->rx_buffers, bp->rx_buffers_dma);
888 bp->rx_buffers = NULL;
889 }
890}
891
892static int macb_alloc_consistent(struct macb *bp)
893{
894 int size;
895
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000896 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100897 bp->tx_skb = kmalloc(size, GFP_KERNEL);
898 if (!bp->tx_skb)
899 goto out_err;
900
901 size = RX_RING_BYTES;
902 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
903 &bp->rx_ring_dma, GFP_KERNEL);
904 if (!bp->rx_ring)
905 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000906 netdev_dbg(bp->dev,
907 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
908 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100909
910 size = TX_RING_BYTES;
911 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
912 &bp->tx_ring_dma, GFP_KERNEL);
913 if (!bp->tx_ring)
914 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000915 netdev_dbg(bp->dev,
916 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
917 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100918
919 size = RX_RING_SIZE * RX_BUFFER_SIZE;
920 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
921 &bp->rx_buffers_dma, GFP_KERNEL);
922 if (!bp->rx_buffers)
923 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000924 netdev_dbg(bp->dev,
925 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
926 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100927
928 return 0;
929
930out_err:
931 macb_free_consistent(bp);
932 return -ENOMEM;
933}
934
935static void macb_init_rings(struct macb *bp)
936{
937 int i;
938 dma_addr_t addr;
939
940 addr = bp->rx_buffers_dma;
941 for (i = 0; i < RX_RING_SIZE; i++) {
942 bp->rx_ring[i].addr = addr;
943 bp->rx_ring[i].ctrl = 0;
944 addr += RX_BUFFER_SIZE;
945 }
946 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
947
948 for (i = 0; i < TX_RING_SIZE; i++) {
949 bp->tx_ring[i].addr = 0;
950 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
951 }
952 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
953
954 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
955}
956
957static void macb_reset_hw(struct macb *bp)
958{
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100959 /*
960 * Disable RX and TX (XXX: Should we halt the transmission
961 * more gracefully?)
962 */
963 macb_writel(bp, NCR, 0);
964
965 /* Clear the stats registers (XXX: Update stats first?) */
966 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
967
968 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000969 macb_writel(bp, TSR, -1);
970 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100971
972 /* Disable all interrupts */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000973 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100974 macb_readl(bp, ISR);
975}
976
Jamie Iles70c9f3d2011-03-09 16:22:54 +0000977static u32 gem_mdc_clk_div(struct macb *bp)
978{
979 u32 config;
980 unsigned long pclk_hz = clk_get_rate(bp->pclk);
981
982 if (pclk_hz <= 20000000)
983 config = GEM_BF(CLK, GEM_CLK_DIV8);
984 else if (pclk_hz <= 40000000)
985 config = GEM_BF(CLK, GEM_CLK_DIV16);
986 else if (pclk_hz <= 80000000)
987 config = GEM_BF(CLK, GEM_CLK_DIV32);
988 else if (pclk_hz <= 120000000)
989 config = GEM_BF(CLK, GEM_CLK_DIV48);
990 else if (pclk_hz <= 160000000)
991 config = GEM_BF(CLK, GEM_CLK_DIV64);
992 else
993 config = GEM_BF(CLK, GEM_CLK_DIV96);
994
995 return config;
996}
997
998static u32 macb_mdc_clk_div(struct macb *bp)
999{
1000 u32 config;
1001 unsigned long pclk_hz;
1002
1003 if (macb_is_gem(bp))
1004 return gem_mdc_clk_div(bp);
1005
1006 pclk_hz = clk_get_rate(bp->pclk);
1007 if (pclk_hz <= 20000000)
1008 config = MACB_BF(CLK, MACB_CLK_DIV8);
1009 else if (pclk_hz <= 40000000)
1010 config = MACB_BF(CLK, MACB_CLK_DIV16);
1011 else if (pclk_hz <= 80000000)
1012 config = MACB_BF(CLK, MACB_CLK_DIV32);
1013 else
1014 config = MACB_BF(CLK, MACB_CLK_DIV64);
1015
1016 return config;
1017}
1018
Jamie Iles757a03c2011-03-09 16:29:59 +00001019/*
1020 * Get the DMA bus width field of the network configuration register that we
1021 * should program. We find the width from decoding the design configuration
1022 * register to find the maximum supported data bus width.
1023 */
1024static u32 macb_dbw(struct macb *bp)
1025{
1026 if (!macb_is_gem(bp))
1027 return 0;
1028
1029 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1030 case 4:
1031 return GEM_BF(DBW, GEM_DBW128);
1032 case 2:
1033 return GEM_BF(DBW, GEM_DBW64);
1034 case 1:
1035 default:
1036 return GEM_BF(DBW, GEM_DBW32);
1037 }
1038}
1039
Jamie Iles0116da42011-03-14 17:38:30 +00001040/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001041 * Configure the receive DMA engine
1042 * - use the correct receive buffer size
1043 * - set the possibility to use INCR16 bursts
1044 * (if not supported by FIFO, it will fallback to default)
1045 * - set both rx/tx packet buffers to full memory size
1046 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001047 */
1048static void macb_configure_dma(struct macb *bp)
1049{
1050 u32 dmacfg;
1051
1052 if (macb_is_gem(bp)) {
1053 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
1054 dmacfg |= GEM_BF(RXBS, RX_BUFFER_SIZE / 64);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001055 dmacfg |= GEM_BF(FBLDO, 16);
1056 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Jamie Iles0116da42011-03-14 17:38:30 +00001057 gem_writel(bp, DMACFG, dmacfg);
1058 }
1059}
1060
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001061static void macb_init_hw(struct macb *bp)
1062{
1063 u32 config;
1064
1065 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001066 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001067
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001068 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001069 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001070 config |= MACB_BIT(PAE); /* PAuse Enable */
1071 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001072 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001073 if (bp->dev->flags & IFF_PROMISC)
1074 config |= MACB_BIT(CAF); /* Copy All Frames */
1075 if (!(bp->dev->flags & IFF_BROADCAST))
1076 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001077 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001078 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001079 bp->speed = SPEED_10;
1080 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001081
Jamie Iles0116da42011-03-14 17:38:30 +00001082 macb_configure_dma(bp);
1083
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001084 /* Initialize TX and RX buffers */
1085 macb_writel(bp, RBQP, bp->rx_ring_dma);
1086 macb_writel(bp, TBQP, bp->tx_ring_dma);
1087
1088 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001089 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001090
1091 /* Enable interrupts */
Nicolas Ferree86cd532012-10-31 06:04:57 +00001092 macb_writel(bp, IER, (MACB_RX_INT_FLAGS
1093 | MACB_TX_INT_FLAGS
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001094 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001095
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001096}
1097
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001098/*
1099 * The hash address register is 64 bits long and takes up two
1100 * locations in the memory map. The least significant bits are stored
1101 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1102 *
1103 * The unicast hash enable and the multicast hash enable bits in the
1104 * network configuration register enable the reception of hash matched
1105 * frames. The destination address is reduced to a 6 bit index into
1106 * the 64 bit hash register using the following hash function. The
1107 * hash function is an exclusive or of every sixth bit of the
1108 * destination address.
1109 *
1110 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1111 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1112 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1113 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1114 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1115 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1116 *
1117 * da[0] represents the least significant bit of the first byte
1118 * received, that is, the multicast/unicast indicator, and da[47]
1119 * represents the most significant bit of the last byte received. If
1120 * the hash index, hi[n], points to a bit that is set in the hash
1121 * register then the frame will be matched according to whether the
1122 * frame is multicast or unicast. A multicast match will be signalled
1123 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1124 * index points to a bit set in the hash register. A unicast match
1125 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1126 * and the hash index points to a bit set in the hash register. To
1127 * receive all multicast frames, the hash register should be set with
1128 * all ones and the multicast hash enable bit should be set in the
1129 * network configuration register.
1130 */
1131
1132static inline int hash_bit_value(int bitnr, __u8 *addr)
1133{
1134 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1135 return 1;
1136 return 0;
1137}
1138
1139/*
1140 * Return the hash index value for the specified address.
1141 */
1142static int hash_get_index(__u8 *addr)
1143{
1144 int i, j, bitval;
1145 int hash_index = 0;
1146
1147 for (j = 0; j < 6; j++) {
1148 for (i = 0, bitval = 0; i < 8; i++)
1149 bitval ^= hash_bit_value(i*6 + j, addr);
1150
1151 hash_index |= (bitval << j);
1152 }
1153
1154 return hash_index;
1155}
1156
1157/*
1158 * Add multicast addresses to the internal multicast-hash table.
1159 */
1160static void macb_sethashtable(struct net_device *dev)
1161{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001162 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001163 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001164 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001165 struct macb *bp = netdev_priv(dev);
1166
1167 mc_filter[0] = mc_filter[1] = 0;
1168
Jiri Pirko22bedad32010-04-01 21:22:57 +00001169 netdev_for_each_mc_addr(ha, dev) {
1170 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001171 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1172 }
1173
Jamie Ilesf75ba502011-11-08 10:12:32 +00001174 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1175 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001176}
1177
1178/*
1179 * Enable/Disable promiscuous and multicast modes.
1180 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001181void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001182{
1183 unsigned long cfg;
1184 struct macb *bp = netdev_priv(dev);
1185
1186 cfg = macb_readl(bp, NCFGR);
1187
1188 if (dev->flags & IFF_PROMISC)
1189 /* Enable promiscuous mode */
1190 cfg |= MACB_BIT(CAF);
1191 else if (dev->flags & (~IFF_PROMISC))
1192 /* Disable promiscuous mode */
1193 cfg &= ~MACB_BIT(CAF);
1194
1195 if (dev->flags & IFF_ALLMULTI) {
1196 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001197 macb_or_gem_writel(bp, HRB, -1);
1198 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001199 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001200 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001201 /* Enable specific multicasts */
1202 macb_sethashtable(dev);
1203 cfg |= MACB_BIT(NCFGR_MTI);
1204 } else if (dev->flags & (~IFF_ALLMULTI)) {
1205 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001206 macb_or_gem_writel(bp, HRB, 0);
1207 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001208 cfg &= ~MACB_BIT(NCFGR_MTI);
1209 }
1210
1211 macb_writel(bp, NCFGR, cfg);
1212}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001213EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001214
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001215static int macb_open(struct net_device *dev)
1216{
1217 struct macb *bp = netdev_priv(dev);
1218 int err;
1219
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001220 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001221
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001222 /* carrier starts down */
1223 netif_carrier_off(dev);
1224
frederic RODO6c36a702007-07-12 19:07:24 +02001225 /* if the phy is not yet register, retry later*/
1226 if (!bp->phy_dev)
1227 return -EAGAIN;
1228
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229 err = macb_alloc_consistent(bp);
1230 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001231 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1232 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001233 return err;
1234 }
1235
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001236 napi_enable(&bp->napi);
1237
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001238 macb_init_rings(bp);
1239 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001240
frederic RODO6c36a702007-07-12 19:07:24 +02001241 /* schedule a link state check */
1242 phy_start(bp->phy_dev);
1243
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001244 netif_start_queue(dev);
1245
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001246 return 0;
1247}
1248
1249static int macb_close(struct net_device *dev)
1250{
1251 struct macb *bp = netdev_priv(dev);
1252 unsigned long flags;
1253
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001254 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001255 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001256
frederic RODO6c36a702007-07-12 19:07:24 +02001257 if (bp->phy_dev)
1258 phy_stop(bp->phy_dev);
1259
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001260 spin_lock_irqsave(&bp->lock, flags);
1261 macb_reset_hw(bp);
1262 netif_carrier_off(dev);
1263 spin_unlock_irqrestore(&bp->lock, flags);
1264
1265 macb_free_consistent(bp);
1266
1267 return 0;
1268}
1269
Jamie Ilesa494ed82011-03-09 16:26:35 +00001270static void gem_update_stats(struct macb *bp)
1271{
1272 u32 __iomem *reg = bp->regs + GEM_OTX;
1273 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1274 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1275
1276 for (; p < end; p++, reg++)
1277 *p += __raw_readl(reg);
1278}
1279
1280static struct net_device_stats *gem_get_stats(struct macb *bp)
1281{
1282 struct gem_stats *hwstat = &bp->hw_stats.gem;
1283 struct net_device_stats *nstat = &bp->stats;
1284
1285 gem_update_stats(bp);
1286
1287 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1288 hwstat->rx_alignment_errors +
1289 hwstat->rx_resource_errors +
1290 hwstat->rx_overruns +
1291 hwstat->rx_oversize_frames +
1292 hwstat->rx_jabbers +
1293 hwstat->rx_undersized_frames +
1294 hwstat->rx_length_field_frame_errors);
1295 nstat->tx_errors = (hwstat->tx_late_collisions +
1296 hwstat->tx_excessive_collisions +
1297 hwstat->tx_underrun +
1298 hwstat->tx_carrier_sense_errors);
1299 nstat->multicast = hwstat->rx_multicast_frames;
1300 nstat->collisions = (hwstat->tx_single_collision_frames +
1301 hwstat->tx_multiple_collision_frames +
1302 hwstat->tx_excessive_collisions);
1303 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1304 hwstat->rx_jabbers +
1305 hwstat->rx_undersized_frames +
1306 hwstat->rx_length_field_frame_errors);
1307 nstat->rx_over_errors = hwstat->rx_resource_errors;
1308 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1309 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1310 nstat->rx_fifo_errors = hwstat->rx_overruns;
1311 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1312 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1313 nstat->tx_fifo_errors = hwstat->tx_underrun;
1314
1315 return nstat;
1316}
1317
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001318struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001319{
1320 struct macb *bp = netdev_priv(dev);
1321 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001322 struct macb_stats *hwstat = &bp->hw_stats.macb;
1323
1324 if (macb_is_gem(bp))
1325 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001326
frederic RODO6c36a702007-07-12 19:07:24 +02001327 /* read stats from hardware */
1328 macb_update_stats(bp);
1329
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001330 /* Convert HW stats into netdevice stats */
1331 nstat->rx_errors = (hwstat->rx_fcs_errors +
1332 hwstat->rx_align_errors +
1333 hwstat->rx_resource_errors +
1334 hwstat->rx_overruns +
1335 hwstat->rx_oversize_pkts +
1336 hwstat->rx_jabbers +
1337 hwstat->rx_undersize_pkts +
1338 hwstat->sqe_test_errors +
1339 hwstat->rx_length_mismatch);
1340 nstat->tx_errors = (hwstat->tx_late_cols +
1341 hwstat->tx_excessive_cols +
1342 hwstat->tx_underruns +
1343 hwstat->tx_carrier_errors);
1344 nstat->collisions = (hwstat->tx_single_cols +
1345 hwstat->tx_multiple_cols +
1346 hwstat->tx_excessive_cols);
1347 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1348 hwstat->rx_jabbers +
1349 hwstat->rx_undersize_pkts +
1350 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001351 nstat->rx_over_errors = hwstat->rx_resource_errors +
1352 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001353 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1354 nstat->rx_frame_errors = hwstat->rx_align_errors;
1355 nstat->rx_fifo_errors = hwstat->rx_overruns;
1356 /* XXX: What does "missed" mean? */
1357 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1358 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1359 nstat->tx_fifo_errors = hwstat->tx_underruns;
1360 /* Don't know about heartbeat or window errors... */
1361
1362 return nstat;
1363}
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001364EXPORT_SYMBOL_GPL(macb_get_stats);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001365
1366static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1367{
1368 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001369 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001370
frederic RODO6c36a702007-07-12 19:07:24 +02001371 if (!phydev)
1372 return -ENODEV;
1373
1374 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001375}
1376
1377static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1378{
1379 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001380 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001381
frederic RODO6c36a702007-07-12 19:07:24 +02001382 if (!phydev)
1383 return -ENODEV;
1384
1385 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001386}
1387
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001388static int macb_get_regs_len(struct net_device *netdev)
1389{
1390 return MACB_GREGS_NBR * sizeof(u32);
1391}
1392
1393static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1394 void *p)
1395{
1396 struct macb *bp = netdev_priv(dev);
1397 unsigned int tail, head;
1398 u32 *regs_buff = p;
1399
1400 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
1401 | MACB_GREGS_VERSION;
1402
1403 tail = macb_tx_ring_wrap(bp->tx_tail);
1404 head = macb_tx_ring_wrap(bp->tx_head);
1405
1406 regs_buff[0] = macb_readl(bp, NCR);
1407 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
1408 regs_buff[2] = macb_readl(bp, NSR);
1409 regs_buff[3] = macb_readl(bp, TSR);
1410 regs_buff[4] = macb_readl(bp, RBQP);
1411 regs_buff[5] = macb_readl(bp, TBQP);
1412 regs_buff[6] = macb_readl(bp, RSR);
1413 regs_buff[7] = macb_readl(bp, IMR);
1414
1415 regs_buff[8] = tail;
1416 regs_buff[9] = head;
1417 regs_buff[10] = macb_tx_dma(bp, tail);
1418 regs_buff[11] = macb_tx_dma(bp, head);
1419
1420 if (macb_is_gem(bp)) {
1421 regs_buff[12] = gem_readl(bp, USRIO);
1422 regs_buff[13] = gem_readl(bp, DMACFG);
1423 }
1424}
1425
Joachim Eastwood0005f542012-10-18 11:01:12 +00001426const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427 .get_settings = macb_get_settings,
1428 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001429 .get_regs_len = macb_get_regs_len,
1430 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001431 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00001432 .get_ts_info = ethtool_op_get_ts_info,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001433};
Joachim Eastwood0005f542012-10-18 11:01:12 +00001434EXPORT_SYMBOL_GPL(macb_ethtool_ops);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001435
Joachim Eastwood0005f542012-10-18 11:01:12 +00001436int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001437{
1438 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001439 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001440
1441 if (!netif_running(dev))
1442 return -EINVAL;
1443
frederic RODO6c36a702007-07-12 19:07:24 +02001444 if (!phydev)
1445 return -ENODEV;
1446
Richard Cochran28b04112010-07-17 08:48:55 +00001447 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001448}
Joachim Eastwood0005f542012-10-18 11:01:12 +00001449EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001450
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001451static const struct net_device_ops macb_netdev_ops = {
1452 .ndo_open = macb_open,
1453 .ndo_stop = macb_close,
1454 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001455 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001456 .ndo_get_stats = macb_get_stats,
1457 .ndo_do_ioctl = macb_ioctl,
1458 .ndo_validate_addr = eth_validate_addr,
1459 .ndo_change_mtu = eth_change_mtu,
1460 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001461#ifdef CONFIG_NET_POLL_CONTROLLER
1462 .ndo_poll_controller = macb_poll_controller,
1463#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001464};
1465
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001466#if defined(CONFIG_OF)
1467static const struct of_device_id macb_dt_ids[] = {
1468 { .compatible = "cdns,at32ap7000-macb" },
1469 { .compatible = "cdns,at91sam9260-macb" },
1470 { .compatible = "cdns,macb" },
1471 { .compatible = "cdns,pc302-gem" },
1472 { .compatible = "cdns,gem" },
1473 { /* sentinel */ }
1474};
1475
1476MODULE_DEVICE_TABLE(of, macb_dt_ids);
1477
Bill Pemberton37a5b3d2012-12-03 09:23:57 -05001478static int macb_get_phy_mode_dt(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001479{
1480 struct device_node *np = pdev->dev.of_node;
1481
1482 if (np)
1483 return of_get_phy_mode(np);
1484
1485 return -ENODEV;
1486}
1487
Bill Pemberton37a5b3d2012-12-03 09:23:57 -05001488static int macb_get_hwaddr_dt(struct macb *bp)
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001489{
1490 struct device_node *np = bp->pdev->dev.of_node;
1491 if (np) {
1492 const char *mac = of_get_mac_address(np);
1493 if (mac) {
1494 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
1495 return 0;
1496 }
1497 }
1498
1499 return -ENODEV;
1500}
1501#else
Bill Pemberton37a5b3d2012-12-03 09:23:57 -05001502static int macb_get_phy_mode_dt(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001503{
1504 return -ENODEV;
1505}
Bill Pemberton37a5b3d2012-12-03 09:23:57 -05001506static int macb_get_hwaddr_dt(struct macb *bp)
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001507{
1508 return -ENODEV;
1509}
1510#endif
1511
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001512static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001513{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001514 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001515 struct resource *regs;
1516 struct net_device *dev;
1517 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001518 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001519 u32 config;
1520 int err = -ENXIO;
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001521 struct pinctrl *pinctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001522
1523 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1524 if (!regs) {
1525 dev_err(&pdev->dev, "no mmio resource defined\n");
1526 goto err_out;
1527 }
1528
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001529 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1530 if (IS_ERR(pinctrl)) {
1531 err = PTR_ERR(pinctrl);
1532 if (err == -EPROBE_DEFER)
1533 goto err_out;
1534
1535 dev_warn(&pdev->dev, "No pinctrl provided\n");
1536 }
1537
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001538 err = -ENOMEM;
1539 dev = alloc_etherdev(sizeof(*bp));
Joe Perches41de8d42012-01-29 13:47:52 +00001540 if (!dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001541 goto err_out;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001542
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001543 SET_NETDEV_DEV(dev, &pdev->dev);
1544
1545 /* TODO: Actually, we have some interesting features... */
1546 dev->features |= 0;
1547
1548 bp = netdev_priv(dev);
1549 bp->pdev = pdev;
1550 bp->dev = dev;
1551
1552 spin_lock_init(&bp->lock);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001553 INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001554
Jamie Iles461845d2011-03-08 20:19:23 +00001555 bp->pclk = clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001556 if (IS_ERR(bp->pclk)) {
1557 dev_err(&pdev->dev, "failed to get macb_clk\n");
1558 goto err_out_free_dev;
1559 }
1560 clk_enable(bp->pclk);
Jamie Iles461845d2011-03-08 20:19:23 +00001561
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001562 bp->hclk = clk_get(&pdev->dev, "hclk");
1563 if (IS_ERR(bp->hclk)) {
1564 dev_err(&pdev->dev, "failed to get hclk\n");
1565 goto err_out_put_pclk;
1566 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001567 clk_enable(bp->hclk);
1568
Joe Perches28f65c112011-06-09 09:13:32 -07001569 bp->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001570 if (!bp->regs) {
1571 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1572 err = -ENOMEM;
1573 goto err_out_disable_clocks;
1574 }
1575
1576 dev->irq = platform_get_irq(pdev, 0);
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00001577 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001578 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001579 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1580 dev->irq, err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001581 goto err_out_iounmap;
1582 }
1583
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001584 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001585 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001586 dev->ethtool_ops = &macb_ethtool_ops;
1587
1588 dev->base_addr = regs->start;
1589
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001590 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001591 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00001592 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001593 macb_writel(bp, NCFGR, config);
1594
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001595 err = macb_get_hwaddr_dt(bp);
1596 if (err < 0)
1597 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02001598
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001599 err = macb_get_phy_mode_dt(pdev);
1600 if (err < 0) {
1601 pdata = pdev->dev.platform_data;
1602 if (pdata && pdata->is_rmii)
1603 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
1604 else
1605 bp->phy_interface = PHY_INTERFACE_MODE_MII;
1606 } else {
1607 bp->phy_interface = err;
1608 }
1609
Patrice Vilchez140b7552012-10-31 06:04:50 +00001610 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
1611 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
1612 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01001613#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001614 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1615 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01001616#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001617 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001618#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001619 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001620#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001621 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01001622#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001623 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001624#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001625
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001626 err = register_netdev(dev);
1627 if (err) {
1628 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1629 goto err_out_free_irq;
1630 }
1631
frederic RODO6c36a702007-07-12 19:07:24 +02001632 if (macb_mii_init(bp) != 0) {
1633 goto err_out_unregister_netdev;
1634 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001635
frederic RODO6c36a702007-07-12 19:07:24 +02001636 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001637
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001638 netif_carrier_off(dev);
1639
Jamie Ilesf75ba502011-11-08 10:12:32 +00001640 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1641 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1642 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001643
frederic RODO6c36a702007-07-12 19:07:24 +02001644 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001645 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1646 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02001647
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001648 return 0;
1649
frederic RODO6c36a702007-07-12 19:07:24 +02001650err_out_unregister_netdev:
1651 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001652err_out_free_irq:
1653 free_irq(dev->irq, dev);
1654err_out_iounmap:
1655 iounmap(bp->regs);
1656err_out_disable_clocks:
1657 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001658 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001659 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001660err_out_put_pclk:
1661 clk_put(bp->pclk);
1662err_out_free_dev:
1663 free_netdev(dev);
1664err_out:
1665 platform_set_drvdata(pdev, NULL);
1666 return err;
1667}
1668
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001669static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001670{
1671 struct net_device *dev;
1672 struct macb *bp;
1673
1674 dev = platform_get_drvdata(pdev);
1675
1676 if (dev) {
1677 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09001678 if (bp->phy_dev)
1679 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001680 mdiobus_unregister(bp->mii_bus);
1681 kfree(bp->mii_bus->irq);
1682 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001683 unregister_netdev(dev);
1684 free_irq(dev->irq, dev);
1685 iounmap(bp->regs);
1686 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001687 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001688 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001689 clk_put(bp->pclk);
1690 free_netdev(dev);
1691 platform_set_drvdata(pdev, NULL);
1692 }
1693
1694 return 0;
1695}
1696
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001697#ifdef CONFIG_PM
1698static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1699{
1700 struct net_device *netdev = platform_get_drvdata(pdev);
1701 struct macb *bp = netdev_priv(netdev);
1702
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001703 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001704 netif_device_detach(netdev);
1705
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001706 clk_disable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001707 clk_disable(bp->pclk);
1708
1709 return 0;
1710}
1711
1712static int macb_resume(struct platform_device *pdev)
1713{
1714 struct net_device *netdev = platform_get_drvdata(pdev);
1715 struct macb *bp = netdev_priv(netdev);
1716
1717 clk_enable(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001718 clk_enable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001719
1720 netif_device_attach(netdev);
1721
1722 return 0;
1723}
1724#else
1725#define macb_suspend NULL
1726#define macb_resume NULL
1727#endif
1728
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001729static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001730 .remove = __exit_p(macb_remove),
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001731 .suspend = macb_suspend,
1732 .resume = macb_resume,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001733 .driver = {
1734 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07001735 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001736 .of_match_table = of_match_ptr(macb_dt_ids),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001737 },
1738};
1739
1740static int __init macb_init(void)
1741{
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001742 return platform_driver_probe(&macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001743}
1744
1745static void __exit macb_exit(void)
1746{
1747 platform_driver_unregister(&macb_driver);
1748}
1749
1750module_init(macb_init);
1751module_exit(macb_exit);
1752
1753MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00001754MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001755MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07001756MODULE_ALIAS("platform:macb");