blob: f7e21f278e0571fd76386be899e392a61965b0a1 [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 Eastwood2dbfdbb92012-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
Nicolas Ferre1b447912013-06-04 21:57:11 +000035#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000036#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000037#define RX_RING_SIZE 512 /* must be power of 2 */
38#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010039
Havard Skinnemoen55054a12012-10-31 06:04:55 +000040#define TX_RING_SIZE 128 /* must be power of 2 */
41#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010042
Nicolas Ferre909a8582012-11-19 06:00:21 +000043/* level of occupied TX descriptors under which we wake up TX process */
44#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010045
46#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
47 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000048#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
49 | MACB_BIT(ISR_RLE) \
50 | MACB_BIT(TXERR))
51#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
52
53/*
54 * Graceful stop timeouts in us. We should allow up to
55 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
56 */
57#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010058
Havard Skinnemoen55054a12012-10-31 06:04:55 +000059/* Ring buffer accessors */
60static unsigned int macb_tx_ring_wrap(unsigned int index)
61{
62 return index & (TX_RING_SIZE - 1);
63}
64
Havard Skinnemoen55054a12012-10-31 06:04:55 +000065static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
66{
67 return &bp->tx_ring[macb_tx_ring_wrap(index)];
68}
69
70static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
71{
72 return &bp->tx_skb[macb_tx_ring_wrap(index)];
73}
74
75static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
76{
77 dma_addr_t offset;
78
79 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
80
81 return bp->tx_ring_dma + offset;
82}
83
84static unsigned int macb_rx_ring_wrap(unsigned int index)
85{
86 return index & (RX_RING_SIZE - 1);
87}
88
89static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
90{
91 return &bp->rx_ring[macb_rx_ring_wrap(index)];
92}
93
94static void *macb_rx_buffer(struct macb *bp, unsigned int index)
95{
Nicolas Ferre1b447912013-06-04 21:57:11 +000096 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +000097}
98
Joachim Eastwood314bccc2012-11-07 08:14:52 +000099void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100100{
101 u32 bottom;
102 u16 top;
103
104 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000105 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100106 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000107 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000108
109 /* Clear unused address register sets */
110 macb_or_gem_writel(bp, SA2B, 0);
111 macb_or_gem_writel(bp, SA2T, 0);
112 macb_or_gem_writel(bp, SA3B, 0);
113 macb_or_gem_writel(bp, SA3T, 0);
114 macb_or_gem_writel(bp, SA4B, 0);
115 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100116}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000117EXPORT_SYMBOL_GPL(macb_set_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100118
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000119void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100120{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000121 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100122 u32 bottom;
123 u16 top;
124 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000125 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100126
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000127 pdata = bp->pdev->dev.platform_data;
128
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000129 /* Check all 4 address register for vaild address */
130 for (i = 0; i < 4; i++) {
131 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
132 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100133
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000134 if (pdata && pdata->rev_eth_addr) {
135 addr[5] = bottom & 0xff;
136 addr[4] = (bottom >> 8) & 0xff;
137 addr[3] = (bottom >> 16) & 0xff;
138 addr[2] = (bottom >> 24) & 0xff;
139 addr[1] = top & 0xff;
140 addr[0] = (top & 0xff00) >> 8;
141 } else {
142 addr[0] = bottom & 0xff;
143 addr[1] = (bottom >> 8) & 0xff;
144 addr[2] = (bottom >> 16) & 0xff;
145 addr[3] = (bottom >> 24) & 0xff;
146 addr[4] = top & 0xff;
147 addr[5] = (top >> 8) & 0xff;
148 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100149
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000150 if (is_valid_ether_addr(addr)) {
151 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
152 return;
153 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700154 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000155
156 netdev_info(bp->dev, "invalid hw address, using random\n");
157 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100158}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000159EXPORT_SYMBOL_GPL(macb_get_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100160
frederic RODO6c36a702007-07-12 19:07:24 +0200161static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100162{
frederic RODO6c36a702007-07-12 19:07:24 +0200163 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100164 int value;
165
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100166 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
167 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200168 | MACB_BF(PHYA, mii_id)
169 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100170 | MACB_BF(CODE, MACB_MAN_CODE)));
171
frederic RODO6c36a702007-07-12 19:07:24 +0200172 /* wait for end of transfer */
173 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
174 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100175
176 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100177
178 return value;
179}
180
frederic RODO6c36a702007-07-12 19:07:24 +0200181static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
182 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100183{
frederic RODO6c36a702007-07-12 19:07:24 +0200184 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100185
186 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
187 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200188 | MACB_BF(PHYA, mii_id)
189 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100190 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200191 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100192
frederic RODO6c36a702007-07-12 19:07:24 +0200193 /* wait for end of transfer */
194 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
195 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100196
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100197 return 0;
198}
199
frederic RODO6c36a702007-07-12 19:07:24 +0200200static int macb_mdio_reset(struct mii_bus *bus)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100201{
frederic RODO6c36a702007-07-12 19:07:24 +0200202 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100203}
204
frederic RODO6c36a702007-07-12 19:07:24 +0200205static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100206{
frederic RODO6c36a702007-07-12 19:07:24 +0200207 struct macb *bp = netdev_priv(dev);
208 struct phy_device *phydev = bp->phy_dev;
209 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100210
frederic RODO6c36a702007-07-12 19:07:24 +0200211 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100212
frederic RODO6c36a702007-07-12 19:07:24 +0200213 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100214
frederic RODO6c36a702007-07-12 19:07:24 +0200215 if (phydev->link) {
216 if ((bp->speed != phydev->speed) ||
217 (bp->duplex != phydev->duplex)) {
218 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100219
frederic RODO6c36a702007-07-12 19:07:24 +0200220 reg = macb_readl(bp, NCFGR);
221 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000222 if (macb_is_gem(bp))
223 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200224
225 if (phydev->duplex)
226 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900227 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200228 reg |= MACB_BIT(SPD);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000229 if (phydev->speed == SPEED_1000)
230 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200231
Patrice Vilchez140b7552012-10-31 06:04:50 +0000232 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200233
234 bp->speed = phydev->speed;
235 bp->duplex = phydev->duplex;
236 status_change = 1;
237 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100238 }
239
frederic RODO6c36a702007-07-12 19:07:24 +0200240 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700241 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200242 bp->speed = 0;
243 bp->duplex = -1;
244 }
245 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100246
frederic RODO6c36a702007-07-12 19:07:24 +0200247 status_change = 1;
248 }
249
250 spin_unlock_irqrestore(&bp->lock, flags);
251
252 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000253 if (phydev->link) {
254 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000255 netdev_info(dev, "link up (%d/%s)\n",
256 phydev->speed,
257 phydev->duplex == DUPLEX_FULL ?
258 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000259 } else {
260 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000261 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000262 }
frederic RODO6c36a702007-07-12 19:07:24 +0200263 }
264}
265
266/* based on au1000_eth. c*/
267static int macb_mii_probe(struct net_device *dev)
268{
269 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000270 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000271 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000272 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000273 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200274
Jiri Pirko7455a762010-02-08 05:12:08 +0000275 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200276 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000277 netdev_err(dev, "no PHY found\n");
frederic RODO6c36a702007-07-12 19:07:24 +0200278 return -1;
279 }
280
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000281 pdata = dev_get_platdata(&bp->pdev->dev);
282 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
283 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
284 if (!ret) {
285 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
286 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
287 }
288 }
frederic RODO6c36a702007-07-12 19:07:24 +0200289
290 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000291 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100292 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000293 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000294 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000295 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200296 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100297
frederic RODO6c36a702007-07-12 19:07:24 +0200298 /* mask with MAC supported features */
Patrice Vilchez140b7552012-10-31 06:04:50 +0000299 if (macb_is_gem(bp))
300 phydev->supported &= PHY_GBIT_FEATURES;
301 else
302 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100303
frederic RODO6c36a702007-07-12 19:07:24 +0200304 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100305
frederic RODO6c36a702007-07-12 19:07:24 +0200306 bp->link = 0;
307 bp->speed = 0;
308 bp->duplex = -1;
309 bp->phy_dev = phydev;
310
311 return 0;
312}
313
Joachim Eastwood0005f542012-10-18 11:01:12 +0000314int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200315{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000316 struct macb_platform_data *pdata;
frederic RODO6c36a702007-07-12 19:07:24 +0200317 int err = -ENXIO, i;
318
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200319 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200320 macb_writel(bp, NCR, MACB_BIT(MPE));
321
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700322 bp->mii_bus = mdiobus_alloc();
323 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200324 err = -ENOMEM;
325 goto err_out;
326 }
327
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700328 bp->mii_bus->name = "MACB_mii_bus";
329 bp->mii_bus->read = &macb_mdio_read;
330 bp->mii_bus->write = &macb_mdio_write;
331 bp->mii_bus->reset = &macb_mdio_reset;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000332 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
333 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700334 bp->mii_bus->priv = bp;
335 bp->mii_bus->parent = &bp->dev->dev;
336 pdata = bp->pdev->dev.platform_data;
337
338 if (pdata)
339 bp->mii_bus->phy_mask = pdata->phy_mask;
340
341 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
342 if (!bp->mii_bus->irq) {
343 err = -ENOMEM;
344 goto err_out_free_mdiobus;
345 }
346
frederic RODO6c36a702007-07-12 19:07:24 +0200347 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700348 bp->mii_bus->irq[i] = PHY_POLL;
frederic RODO6c36a702007-07-12 19:07:24 +0200349
Jamie Iles91523942011-02-28 04:05:25 +0000350 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200351
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700352 if (mdiobus_register(bp->mii_bus))
frederic RODO6c36a702007-07-12 19:07:24 +0200353 goto err_out_free_mdio_irq;
354
355 if (macb_mii_probe(bp->dev) != 0) {
356 goto err_out_unregister_bus;
357 }
358
359 return 0;
360
361err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700362 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200363err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700364 kfree(bp->mii_bus->irq);
365err_out_free_mdiobus:
366 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200367err_out:
368 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100369}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000370EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100371
372static void macb_update_stats(struct macb *bp)
373{
374 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000375 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
376 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100377
378 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
379
380 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100381 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100382}
383
Nicolas Ferree86cd532012-10-31 06:04:57 +0000384static int macb_halt_tx(struct macb *bp)
385{
386 unsigned long halt_time, timeout;
387 u32 status;
388
389 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
390
391 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
392 do {
393 halt_time = jiffies;
394 status = macb_readl(bp, TSR);
395 if (!(status & MACB_BIT(TGO)))
396 return 0;
397
398 usleep_range(10, 250);
399 } while (time_before(halt_time, timeout));
400
401 return -ETIMEDOUT;
402}
403
404static void macb_tx_error_task(struct work_struct *work)
405{
406 struct macb *bp = container_of(work, struct macb, tx_error_task);
407 struct macb_tx_skb *tx_skb;
408 struct sk_buff *skb;
409 unsigned int tail;
410
411 netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
412 bp->tx_tail, bp->tx_head);
413
414 /* Make sure nobody is trying to queue up new packets */
415 netif_stop_queue(bp->dev);
416
417 /*
418 * Stop transmission now
419 * (in case we have just queued new packets)
420 */
421 if (macb_halt_tx(bp))
422 /* Just complain for now, reinitializing TX path can be good */
423 netdev_err(bp->dev, "BUG: halt tx timed out\n");
424
425 /* No need for the lock here as nobody will interrupt us anymore */
426
427 /*
428 * Treat frames in TX queue including the ones that caused the error.
429 * Free transmit buffers in upper layer.
430 */
431 for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
432 struct macb_dma_desc *desc;
433 u32 ctrl;
434
435 desc = macb_tx_desc(bp, tail);
436 ctrl = desc->ctrl;
437 tx_skb = macb_tx_skb(bp, tail);
438 skb = tx_skb->skb;
439
440 if (ctrl & MACB_BIT(TX_USED)) {
441 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
442 macb_tx_ring_wrap(tail), skb->data);
443 bp->stats.tx_packets++;
444 bp->stats.tx_bytes += skb->len;
445 } else {
446 /*
447 * "Buffers exhausted mid-frame" errors may only happen
448 * if the driver is buggy, so complain loudly about those.
449 * Statistics are updated by hardware.
450 */
451 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
452 netdev_err(bp->dev,
453 "BUG: TX buffers exhausted mid-frame\n");
454
455 desc->ctrl = ctrl | MACB_BIT(TX_USED);
456 }
457
458 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
459 DMA_TO_DEVICE);
460 tx_skb->skb = NULL;
461 dev_kfree_skb(skb);
462 }
463
464 /* Make descriptor updates visible to hardware */
465 wmb();
466
467 /* Reinitialize the TX desc queue */
468 macb_writel(bp, TBQP, bp->tx_ring_dma);
469 /* Make TX ring reflect state of hardware */
470 bp->tx_head = bp->tx_tail = 0;
471
472 /* Now we are ready to start transmission again */
473 netif_wake_queue(bp->dev);
474
475 /* Housework before enabling TX IRQ */
476 macb_writel(bp, TSR, macb_readl(bp, TSR));
477 macb_writel(bp, IER, MACB_TX_INT_FLAGS);
478}
479
480static void macb_tx_interrupt(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100481{
482 unsigned int tail;
483 unsigned int head;
484 u32 status;
485
486 status = macb_readl(bp, TSR);
487 macb_writel(bp, TSR, status);
488
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000489 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
490 macb_writel(bp, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000491
Nicolas Ferree86cd532012-10-31 06:04:57 +0000492 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
493 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100494
495 head = bp->tx_head;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000496 for (tail = bp->tx_tail; tail != head; tail++) {
497 struct macb_tx_skb *tx_skb;
498 struct sk_buff *skb;
499 struct macb_dma_desc *desc;
500 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100501
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000502 desc = macb_tx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100503
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000504 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100505 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000506
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000507 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100508
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000509 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100510 break;
511
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000512 tx_skb = macb_tx_skb(bp, tail);
513 skb = tx_skb->skb;
514
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000515 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000516 macb_tx_ring_wrap(tail), skb->data);
517 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100518 DMA_TO_DEVICE);
519 bp->stats.tx_packets++;
520 bp->stats.tx_bytes += skb->len;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000521 tx_skb->skb = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100522 dev_kfree_skb_irq(skb);
523 }
524
525 bp->tx_tail = tail;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000526 if (netif_queue_stopped(bp->dev)
Nicolas Ferre909a8582012-11-19 06:00:21 +0000527 && CIRC_CNT(bp->tx_head, bp->tx_tail,
528 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100529 netif_wake_queue(bp->dev);
530}
531
Nicolas Ferre4df95132013-06-04 21:57:12 +0000532static void gem_rx_refill(struct macb *bp)
533{
534 unsigned int entry;
535 struct sk_buff *skb;
536 struct macb_dma_desc *desc;
537 dma_addr_t paddr;
538
539 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
540 u32 addr, ctrl;
541
542 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
543 desc = &bp->rx_ring[entry];
544
545 /* Make hw descriptor updates visible to CPU */
546 rmb();
547
548 addr = desc->addr;
549 ctrl = desc->ctrl;
550 bp->rx_prepared_head++;
551
552 if ((addr & MACB_BIT(RX_USED)))
553 continue;
554
555 if (bp->rx_skbuff[entry] == NULL) {
556 /* allocate sk_buff for this free entry in ring */
557 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
558 if (unlikely(skb == NULL)) {
559 netdev_err(bp->dev,
560 "Unable to allocate sk_buff\n");
561 break;
562 }
563 bp->rx_skbuff[entry] = skb;
564
565 /* now fill corresponding descriptor entry */
566 paddr = dma_map_single(&bp->pdev->dev, skb->data,
567 bp->rx_buffer_size, DMA_FROM_DEVICE);
568
569 if (entry == RX_RING_SIZE - 1)
570 paddr |= MACB_BIT(RX_WRAP);
571 bp->rx_ring[entry].addr = paddr;
572 bp->rx_ring[entry].ctrl = 0;
573
574 /* properly align Ethernet header */
575 skb_reserve(skb, NET_IP_ALIGN);
576 }
577 }
578
579 /* Make descriptor updates visible to hardware */
580 wmb();
581
582 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
583 bp->rx_prepared_head, bp->rx_tail);
584}
585
586/* Mark DMA descriptors from begin up to and not including end as unused */
587static void discard_partial_frame(struct macb *bp, unsigned int begin,
588 unsigned int end)
589{
590 unsigned int frag;
591
592 for (frag = begin; frag != end; frag++) {
593 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
594 desc->addr &= ~MACB_BIT(RX_USED);
595 }
596
597 /* Make descriptor updates visible to hardware */
598 wmb();
599
600 /*
601 * When this happens, the hardware stats registers for
602 * whatever caused this is updated, so we don't have to record
603 * anything.
604 */
605}
606
607static int gem_rx(struct macb *bp, int budget)
608{
609 unsigned int len;
610 unsigned int entry;
611 struct sk_buff *skb;
612 struct macb_dma_desc *desc;
613 int count = 0;
614
615 while (count < budget) {
616 u32 addr, ctrl;
617
618 entry = macb_rx_ring_wrap(bp->rx_tail);
619 desc = &bp->rx_ring[entry];
620
621 /* Make hw descriptor updates visible to CPU */
622 rmb();
623
624 addr = desc->addr;
625 ctrl = desc->ctrl;
626
627 if (!(addr & MACB_BIT(RX_USED)))
628 break;
629
630 desc->addr &= ~MACB_BIT(RX_USED);
631 bp->rx_tail++;
632 count++;
633
634 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
635 netdev_err(bp->dev,
636 "not whole frame pointed by descriptor\n");
637 bp->stats.rx_dropped++;
638 break;
639 }
640 skb = bp->rx_skbuff[entry];
641 if (unlikely(!skb)) {
642 netdev_err(bp->dev,
643 "inconsistent Rx descriptor chain\n");
644 bp->stats.rx_dropped++;
645 break;
646 }
647 /* now everything is ready for receiving packet */
648 bp->rx_skbuff[entry] = NULL;
649 len = MACB_BFEXT(RX_FRMLEN, ctrl);
650
651 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
652
653 skb_put(skb, len);
654 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
655 dma_unmap_single(&bp->pdev->dev, addr,
656 len, DMA_FROM_DEVICE);
657
658 skb->protocol = eth_type_trans(skb, bp->dev);
659 skb_checksum_none_assert(skb);
660
661 bp->stats.rx_packets++;
662 bp->stats.rx_bytes += skb->len;
663
664#if defined(DEBUG) && defined(VERBOSE_DEBUG)
665 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
666 skb->len, skb->csum);
667 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
668 skb->mac_header, 16, true);
669 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
670 skb->data, 32, true);
671#endif
672
673 netif_receive_skb(skb);
674 }
675
676 gem_rx_refill(bp);
677
678 return count;
679}
680
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100681static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
682 unsigned int last_frag)
683{
684 unsigned int len;
685 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000686 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100687 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000688 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100689
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000690 desc = macb_rx_desc(bp, last_frag);
691 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100692
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000693 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000694 macb_rx_ring_wrap(first_frag),
695 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100696
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000697 /*
698 * The ethernet header starts NET_IP_ALIGN bytes into the
699 * first buffer. Since the header is 14 bytes, this makes the
700 * payload word-aligned.
701 *
702 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
703 * the two padding bytes into the skb so that we avoid hitting
704 * the slowpath in memcpy(), and pull them off afterwards.
705 */
706 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100707 if (!skb) {
708 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000709 for (frag = first_frag; ; frag++) {
710 desc = macb_rx_desc(bp, frag);
711 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100712 if (frag == last_frag)
713 break;
714 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000715
716 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100717 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000718
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100719 return 1;
720 }
721
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000722 offset = 0;
723 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700724 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100725 skb_put(skb, len);
726
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000727 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000728 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100729
730 if (offset + frag_len > len) {
731 BUG_ON(frag != last_frag);
732 frag_len = len - offset;
733 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300734 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000735 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000736 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000737 desc = macb_rx_desc(bp, frag);
738 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100739
740 if (frag == last_frag)
741 break;
742 }
743
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000744 /* Make descriptor updates visible to hardware */
745 wmb();
746
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000747 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100748 skb->protocol = eth_type_trans(skb, bp->dev);
749
750 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000751 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000752 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000753 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100754 netif_receive_skb(skb);
755
756 return 0;
757}
758
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100759static int macb_rx(struct macb *bp, int budget)
760{
761 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000762 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100763 int first_frag = -1;
764
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000765 for (tail = bp->rx_tail; budget > 0; tail++) {
766 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100767 u32 addr, ctrl;
768
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000769 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100770 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000771
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000772 addr = desc->addr;
773 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100774
775 if (!(addr & MACB_BIT(RX_USED)))
776 break;
777
778 if (ctrl & MACB_BIT(RX_SOF)) {
779 if (first_frag != -1)
780 discard_partial_frame(bp, first_frag, tail);
781 first_frag = tail;
782 }
783
784 if (ctrl & MACB_BIT(RX_EOF)) {
785 int dropped;
786 BUG_ON(first_frag == -1);
787
788 dropped = macb_rx_frame(bp, first_frag, tail);
789 first_frag = -1;
790 if (!dropped) {
791 received++;
792 budget--;
793 }
794 }
795 }
796
797 if (first_frag != -1)
798 bp->rx_tail = first_frag;
799 else
800 bp->rx_tail = tail;
801
802 return received;
803}
804
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700805static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100806{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700807 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700808 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100809 u32 status;
810
811 status = macb_readl(bp, RSR);
812 macb_writel(bp, RSR, status);
813
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700814 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100815
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000816 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000817 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100818
Nicolas Ferre4df95132013-06-04 21:57:12 +0000819 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000820 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800821 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100822
Joshua Hokeb3363692010-10-25 01:44:22 +0000823 /*
824 * We've done what we can to clean the buffers. Make sure we
825 * get notified when new packets arrive.
826 */
827 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
Nicolas Ferre8770e912013-02-12 11:08:48 +0100828
829 /* Packets received while interrupts were disabled */
830 status = macb_readl(bp, RSR);
831 if (unlikely(status))
832 napi_reschedule(napi);
Joshua Hokeb3363692010-10-25 01:44:22 +0000833 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100834
835 /* TODO: Handle errors */
836
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700837 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100838}
839
840static irqreturn_t macb_interrupt(int irq, void *dev_id)
841{
842 struct net_device *dev = dev_id;
843 struct macb *bp = netdev_priv(dev);
844 u32 status;
845
846 status = macb_readl(bp, ISR);
847
848 if (unlikely(!status))
849 return IRQ_NONE;
850
851 spin_lock(&bp->lock);
852
853 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100854 /* close possible race with dev_close */
855 if (unlikely(!netif_running(dev))) {
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000856 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100857 break;
858 }
859
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000860 netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
861
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100862 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000863 /*
864 * There's no point taking any more interrupts
865 * until we have processed the buffers. The
866 * scheduling call may fail if the poll routine
867 * is already scheduled, so disable interrupts
868 * now.
869 */
870 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000871 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
872 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +0000873
Ben Hutchings288379f2009-01-19 16:43:59 -0800874 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000875 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800876 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100877 }
878 }
879
Nicolas Ferree86cd532012-10-31 06:04:57 +0000880 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
881 macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
882 schedule_work(&bp->tx_error_task);
883 break;
884 }
885
886 if (status & MACB_BIT(TCOMP))
887 macb_tx_interrupt(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100888
889 /*
890 * Link change detection isn't possible with RMII, so we'll
891 * add that if/when we get our hands on a full-blown MII PHY.
892 */
893
Alexander Steinb19f7f72011-04-13 05:03:24 +0000894 if (status & MACB_BIT(ISR_ROVR)) {
895 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000896 if (macb_is_gem(bp))
897 bp->hw_stats.gem.rx_overruns++;
898 else
899 bp->hw_stats.macb.rx_overruns++;
Alexander Steinb19f7f72011-04-13 05:03:24 +0000900 }
901
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100902 if (status & MACB_BIT(HRESP)) {
903 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000904 * TODO: Reset the hardware, and maybe move the
905 * netdev_err to a lower-priority context as well
906 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100907 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000908 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100909 }
910
911 status = macb_readl(bp, ISR);
912 }
913
914 spin_unlock(&bp->lock);
915
916 return IRQ_HANDLED;
917}
918
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700919#ifdef CONFIG_NET_POLL_CONTROLLER
920/*
921 * Polling receive - used by netconsole and other diagnostic tools
922 * to allow network i/o with interrupts disabled.
923 */
924static void macb_poll_controller(struct net_device *dev)
925{
926 unsigned long flags;
927
928 local_irq_save(flags);
929 macb_interrupt(dev->irq, dev);
930 local_irq_restore(flags);
931}
932#endif
933
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100934static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
935{
936 struct macb *bp = netdev_priv(dev);
937 dma_addr_t mapping;
938 unsigned int len, entry;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000939 struct macb_dma_desc *desc;
940 struct macb_tx_skb *tx_skb;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100941 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -0700942 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100943
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000944#if defined(DEBUG) && defined(VERBOSE_DEBUG)
945 netdev_vdbg(bp->dev,
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000946 "start_xmit: len %u head %p data %p tail %p end %p\n",
947 skb->len, skb->head, skb->data,
948 skb_tail_pointer(skb), skb_end_pointer(skb));
949 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
950 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100951#endif
952
953 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -0700954 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100955
956 /* This is a hard error, log it. */
Nicolas Ferre909a8582012-11-19 06:00:21 +0000957 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100958 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -0700959 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000960 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
961 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
962 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +0000963 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100964 }
965
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000966 entry = macb_tx_ring_wrap(bp->tx_head);
967 bp->tx_head++;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000968 netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100969 mapping = dma_map_single(&bp->pdev->dev, skb->data,
970 len, DMA_TO_DEVICE);
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000971
972 tx_skb = &bp->tx_skb[entry];
973 tx_skb->skb = skb;
974 tx_skb->mapping = mapping;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000975 netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000976 skb->data, (unsigned long)mapping);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100977
978 ctrl = MACB_BF(TX_FRMLEN, len);
979 ctrl |= MACB_BIT(TX_LAST);
980 if (entry == (TX_RING_SIZE - 1))
981 ctrl |= MACB_BIT(TX_WRAP);
982
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000983 desc = &bp->tx_ring[entry];
984 desc->addr = mapping;
985 desc->ctrl = ctrl;
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000986
987 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100988 wmb();
989
Richard Cochrane0720922011-06-19 21:51:28 +0000990 skb_tx_timestamp(skb);
991
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100992 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
993
Nicolas Ferre909a8582012-11-19 06:00:21 +0000994 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100995 netif_stop_queue(dev);
996
Dongdong Deng48719532009-08-23 19:49:07 -0700997 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100998
Patrick McHardy6ed10652009-06-23 06:03:08 +0000999 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001000}
1001
Nicolas Ferre4df95132013-06-04 21:57:12 +00001002static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001003{
1004 if (!macb_is_gem(bp)) {
1005 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1006 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001007 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001008
Nicolas Ferre1b447912013-06-04 21:57:11 +00001009 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001010 netdev_dbg(bp->dev,
1011 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001012 RX_BUFFER_MULTIPLE);
1013 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001014 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001015 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001016 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001017
1018 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1019 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001020}
1021
Nicolas Ferre4df95132013-06-04 21:57:12 +00001022static void gem_free_rx_buffers(struct macb *bp)
1023{
1024 struct sk_buff *skb;
1025 struct macb_dma_desc *desc;
1026 dma_addr_t addr;
1027 int i;
1028
1029 if (!bp->rx_skbuff)
1030 return;
1031
1032 for (i = 0; i < RX_RING_SIZE; i++) {
1033 skb = bp->rx_skbuff[i];
1034
1035 if (skb == NULL)
1036 continue;
1037
1038 desc = &bp->rx_ring[i];
1039 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1040 dma_unmap_single(&bp->pdev->dev, addr, skb->len,
1041 DMA_FROM_DEVICE);
1042 dev_kfree_skb_any(skb);
1043 skb = NULL;
1044 }
1045
1046 kfree(bp->rx_skbuff);
1047 bp->rx_skbuff = NULL;
1048}
1049
1050static void macb_free_rx_buffers(struct macb *bp)
1051{
1052 if (bp->rx_buffers) {
1053 dma_free_coherent(&bp->pdev->dev,
1054 RX_RING_SIZE * bp->rx_buffer_size,
1055 bp->rx_buffers, bp->rx_buffers_dma);
1056 bp->rx_buffers = NULL;
1057 }
1058}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001059
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001060static void macb_free_consistent(struct macb *bp)
1061{
1062 if (bp->tx_skb) {
1063 kfree(bp->tx_skb);
1064 bp->tx_skb = NULL;
1065 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001066 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001067 if (bp->rx_ring) {
1068 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1069 bp->rx_ring, bp->rx_ring_dma);
1070 bp->rx_ring = NULL;
1071 }
1072 if (bp->tx_ring) {
1073 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1074 bp->tx_ring, bp->tx_ring_dma);
1075 bp->tx_ring = NULL;
1076 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001077}
1078
1079static int gem_alloc_rx_buffers(struct macb *bp)
1080{
1081 int size;
1082
1083 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1084 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1085 if (!bp->rx_skbuff)
1086 return -ENOMEM;
1087 else
1088 netdev_dbg(bp->dev,
1089 "Allocated %d RX struct sk_buff entries at %p\n",
1090 RX_RING_SIZE, bp->rx_skbuff);
1091 return 0;
1092}
1093
1094static int macb_alloc_rx_buffers(struct macb *bp)
1095{
1096 int size;
1097
1098 size = RX_RING_SIZE * bp->rx_buffer_size;
1099 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1100 &bp->rx_buffers_dma, GFP_KERNEL);
1101 if (!bp->rx_buffers)
1102 return -ENOMEM;
1103 else
1104 netdev_dbg(bp->dev,
1105 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1106 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1107 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001108}
1109
1110static int macb_alloc_consistent(struct macb *bp)
1111{
1112 int size;
1113
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001114 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001115 bp->tx_skb = kmalloc(size, GFP_KERNEL);
1116 if (!bp->tx_skb)
1117 goto out_err;
1118
1119 size = RX_RING_BYTES;
1120 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1121 &bp->rx_ring_dma, GFP_KERNEL);
1122 if (!bp->rx_ring)
1123 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001124 netdev_dbg(bp->dev,
1125 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1126 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001127
1128 size = TX_RING_BYTES;
1129 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1130 &bp->tx_ring_dma, GFP_KERNEL);
1131 if (!bp->tx_ring)
1132 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001133 netdev_dbg(bp->dev,
1134 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
1135 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001136
Nicolas Ferre4df95132013-06-04 21:57:12 +00001137 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001138 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001139
1140 return 0;
1141
1142out_err:
1143 macb_free_consistent(bp);
1144 return -ENOMEM;
1145}
1146
Nicolas Ferre4df95132013-06-04 21:57:12 +00001147static void gem_init_rings(struct macb *bp)
1148{
1149 int i;
1150
1151 for (i = 0; i < TX_RING_SIZE; i++) {
1152 bp->tx_ring[i].addr = 0;
1153 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1154 }
1155 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1156
1157 bp->rx_tail = bp->rx_prepared_head = bp->tx_head = bp->tx_tail = 0;
1158
1159 gem_rx_refill(bp);
1160}
1161
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001162static void macb_init_rings(struct macb *bp)
1163{
1164 int i;
1165 dma_addr_t addr;
1166
1167 addr = bp->rx_buffers_dma;
1168 for (i = 0; i < RX_RING_SIZE; i++) {
1169 bp->rx_ring[i].addr = addr;
1170 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001171 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001172 }
1173 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1174
1175 for (i = 0; i < TX_RING_SIZE; i++) {
1176 bp->tx_ring[i].addr = 0;
1177 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1178 }
1179 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1180
1181 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
1182}
1183
1184static void macb_reset_hw(struct macb *bp)
1185{
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001186 /*
1187 * Disable RX and TX (XXX: Should we halt the transmission
1188 * more gracefully?)
1189 */
1190 macb_writel(bp, NCR, 0);
1191
1192 /* Clear the stats registers (XXX: Update stats first?) */
1193 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1194
1195 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001196 macb_writel(bp, TSR, -1);
1197 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001198
1199 /* Disable all interrupts */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001200 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001201 macb_readl(bp, ISR);
1202}
1203
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001204static u32 gem_mdc_clk_div(struct macb *bp)
1205{
1206 u32 config;
1207 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1208
1209 if (pclk_hz <= 20000000)
1210 config = GEM_BF(CLK, GEM_CLK_DIV8);
1211 else if (pclk_hz <= 40000000)
1212 config = GEM_BF(CLK, GEM_CLK_DIV16);
1213 else if (pclk_hz <= 80000000)
1214 config = GEM_BF(CLK, GEM_CLK_DIV32);
1215 else if (pclk_hz <= 120000000)
1216 config = GEM_BF(CLK, GEM_CLK_DIV48);
1217 else if (pclk_hz <= 160000000)
1218 config = GEM_BF(CLK, GEM_CLK_DIV64);
1219 else
1220 config = GEM_BF(CLK, GEM_CLK_DIV96);
1221
1222 return config;
1223}
1224
1225static u32 macb_mdc_clk_div(struct macb *bp)
1226{
1227 u32 config;
1228 unsigned long pclk_hz;
1229
1230 if (macb_is_gem(bp))
1231 return gem_mdc_clk_div(bp);
1232
1233 pclk_hz = clk_get_rate(bp->pclk);
1234 if (pclk_hz <= 20000000)
1235 config = MACB_BF(CLK, MACB_CLK_DIV8);
1236 else if (pclk_hz <= 40000000)
1237 config = MACB_BF(CLK, MACB_CLK_DIV16);
1238 else if (pclk_hz <= 80000000)
1239 config = MACB_BF(CLK, MACB_CLK_DIV32);
1240 else
1241 config = MACB_BF(CLK, MACB_CLK_DIV64);
1242
1243 return config;
1244}
1245
Jamie Iles757a03c2011-03-09 16:29:59 +00001246/*
1247 * Get the DMA bus width field of the network configuration register that we
1248 * should program. We find the width from decoding the design configuration
1249 * register to find the maximum supported data bus width.
1250 */
1251static u32 macb_dbw(struct macb *bp)
1252{
1253 if (!macb_is_gem(bp))
1254 return 0;
1255
1256 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1257 case 4:
1258 return GEM_BF(DBW, GEM_DBW128);
1259 case 2:
1260 return GEM_BF(DBW, GEM_DBW64);
1261 case 1:
1262 default:
1263 return GEM_BF(DBW, GEM_DBW32);
1264 }
1265}
1266
Jamie Iles0116da42011-03-14 17:38:30 +00001267/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001268 * Configure the receive DMA engine
1269 * - use the correct receive buffer size
1270 * - set the possibility to use INCR16 bursts
1271 * (if not supported by FIFO, it will fallback to default)
1272 * - set both rx/tx packet buffers to full memory size
1273 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001274 */
1275static void macb_configure_dma(struct macb *bp)
1276{
1277 u32 dmacfg;
1278
1279 if (macb_is_gem(bp)) {
1280 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001281 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001282 dmacfg |= GEM_BF(FBLDO, 16);
1283 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Steffen Trumtrara1ae3852013-03-27 23:07:06 +00001284 dmacfg &= ~GEM_BIT(ENDIA);
Jamie Iles0116da42011-03-14 17:38:30 +00001285 gem_writel(bp, DMACFG, dmacfg);
1286 }
1287}
1288
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001289/*
1290 * Configure peripheral capacities according to integration options used
1291 */
1292static void macb_configure_caps(struct macb *bp)
1293{
1294 if (macb_is_gem(bp)) {
1295 if (GEM_BF(IRQCOR, gem_readl(bp, DCFG1)) == 0)
1296 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
1297 }
1298}
1299
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001300static void macb_init_hw(struct macb *bp)
1301{
1302 u32 config;
1303
1304 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001305 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001306
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001307 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001308 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001309 config |= MACB_BIT(PAE); /* PAuse Enable */
1310 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001311 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001312 if (bp->dev->flags & IFF_PROMISC)
1313 config |= MACB_BIT(CAF); /* Copy All Frames */
1314 if (!(bp->dev->flags & IFF_BROADCAST))
1315 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001316 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001317 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001318 bp->speed = SPEED_10;
1319 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001320
Jamie Iles0116da42011-03-14 17:38:30 +00001321 macb_configure_dma(bp);
Nicolas Ferre581df9e2013-05-14 03:00:16 +00001322 macb_configure_caps(bp);
Jamie Iles0116da42011-03-14 17:38:30 +00001323
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001324 /* Initialize TX and RX buffers */
1325 macb_writel(bp, RBQP, bp->rx_ring_dma);
1326 macb_writel(bp, TBQP, bp->tx_ring_dma);
1327
1328 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001329 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001330
1331 /* Enable interrupts */
Nicolas Ferree86cd532012-10-31 06:04:57 +00001332 macb_writel(bp, IER, (MACB_RX_INT_FLAGS
1333 | MACB_TX_INT_FLAGS
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001334 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001335
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001336}
1337
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001338/*
1339 * The hash address register is 64 bits long and takes up two
1340 * locations in the memory map. The least significant bits are stored
1341 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1342 *
1343 * The unicast hash enable and the multicast hash enable bits in the
1344 * network configuration register enable the reception of hash matched
1345 * frames. The destination address is reduced to a 6 bit index into
1346 * the 64 bit hash register using the following hash function. The
1347 * hash function is an exclusive or of every sixth bit of the
1348 * destination address.
1349 *
1350 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1351 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1352 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1353 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1354 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1355 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1356 *
1357 * da[0] represents the least significant bit of the first byte
1358 * received, that is, the multicast/unicast indicator, and da[47]
1359 * represents the most significant bit of the last byte received. If
1360 * the hash index, hi[n], points to a bit that is set in the hash
1361 * register then the frame will be matched according to whether the
1362 * frame is multicast or unicast. A multicast match will be signalled
1363 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1364 * index points to a bit set in the hash register. A unicast match
1365 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1366 * and the hash index points to a bit set in the hash register. To
1367 * receive all multicast frames, the hash register should be set with
1368 * all ones and the multicast hash enable bit should be set in the
1369 * network configuration register.
1370 */
1371
1372static inline int hash_bit_value(int bitnr, __u8 *addr)
1373{
1374 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1375 return 1;
1376 return 0;
1377}
1378
1379/*
1380 * Return the hash index value for the specified address.
1381 */
1382static int hash_get_index(__u8 *addr)
1383{
1384 int i, j, bitval;
1385 int hash_index = 0;
1386
1387 for (j = 0; j < 6; j++) {
1388 for (i = 0, bitval = 0; i < 8; i++)
1389 bitval ^= hash_bit_value(i*6 + j, addr);
1390
1391 hash_index |= (bitval << j);
1392 }
1393
1394 return hash_index;
1395}
1396
1397/*
1398 * Add multicast addresses to the internal multicast-hash table.
1399 */
1400static void macb_sethashtable(struct net_device *dev)
1401{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001402 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001403 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001404 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001405 struct macb *bp = netdev_priv(dev);
1406
1407 mc_filter[0] = mc_filter[1] = 0;
1408
Jiri Pirko22bedad32010-04-01 21:22:57 +00001409 netdev_for_each_mc_addr(ha, dev) {
1410 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001411 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1412 }
1413
Jamie Ilesf75ba502011-11-08 10:12:32 +00001414 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1415 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001416}
1417
1418/*
1419 * Enable/Disable promiscuous and multicast modes.
1420 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001421void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001422{
1423 unsigned long cfg;
1424 struct macb *bp = netdev_priv(dev);
1425
1426 cfg = macb_readl(bp, NCFGR);
1427
1428 if (dev->flags & IFF_PROMISC)
1429 /* Enable promiscuous mode */
1430 cfg |= MACB_BIT(CAF);
1431 else if (dev->flags & (~IFF_PROMISC))
1432 /* Disable promiscuous mode */
1433 cfg &= ~MACB_BIT(CAF);
1434
1435 if (dev->flags & IFF_ALLMULTI) {
1436 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001437 macb_or_gem_writel(bp, HRB, -1);
1438 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001439 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001440 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001441 /* Enable specific multicasts */
1442 macb_sethashtable(dev);
1443 cfg |= MACB_BIT(NCFGR_MTI);
1444 } else if (dev->flags & (~IFF_ALLMULTI)) {
1445 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001446 macb_or_gem_writel(bp, HRB, 0);
1447 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001448 cfg &= ~MACB_BIT(NCFGR_MTI);
1449 }
1450
1451 macb_writel(bp, NCFGR, cfg);
1452}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001453EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001454
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001455static int macb_open(struct net_device *dev)
1456{
1457 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001458 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001459 int err;
1460
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001461 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001462
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001463 /* carrier starts down */
1464 netif_carrier_off(dev);
1465
frederic RODO6c36a702007-07-12 19:07:24 +02001466 /* if the phy is not yet register, retry later*/
1467 if (!bp->phy_dev)
1468 return -EAGAIN;
1469
Nicolas Ferre1b447912013-06-04 21:57:11 +00001470 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001471 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001472
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001473 err = macb_alloc_consistent(bp);
1474 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001475 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1476 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001477 return err;
1478 }
1479
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001480 napi_enable(&bp->napi);
1481
Nicolas Ferre4df95132013-06-04 21:57:12 +00001482 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001483 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001484
frederic RODO6c36a702007-07-12 19:07:24 +02001485 /* schedule a link state check */
1486 phy_start(bp->phy_dev);
1487
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001488 netif_start_queue(dev);
1489
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001490 return 0;
1491}
1492
1493static int macb_close(struct net_device *dev)
1494{
1495 struct macb *bp = netdev_priv(dev);
1496 unsigned long flags;
1497
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001498 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001499 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001500
frederic RODO6c36a702007-07-12 19:07:24 +02001501 if (bp->phy_dev)
1502 phy_stop(bp->phy_dev);
1503
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001504 spin_lock_irqsave(&bp->lock, flags);
1505 macb_reset_hw(bp);
1506 netif_carrier_off(dev);
1507 spin_unlock_irqrestore(&bp->lock, flags);
1508
1509 macb_free_consistent(bp);
1510
1511 return 0;
1512}
1513
Jamie Ilesa494ed82011-03-09 16:26:35 +00001514static void gem_update_stats(struct macb *bp)
1515{
1516 u32 __iomem *reg = bp->regs + GEM_OTX;
1517 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1518 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1519
1520 for (; p < end; p++, reg++)
1521 *p += __raw_readl(reg);
1522}
1523
1524static struct net_device_stats *gem_get_stats(struct macb *bp)
1525{
1526 struct gem_stats *hwstat = &bp->hw_stats.gem;
1527 struct net_device_stats *nstat = &bp->stats;
1528
1529 gem_update_stats(bp);
1530
1531 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1532 hwstat->rx_alignment_errors +
1533 hwstat->rx_resource_errors +
1534 hwstat->rx_overruns +
1535 hwstat->rx_oversize_frames +
1536 hwstat->rx_jabbers +
1537 hwstat->rx_undersized_frames +
1538 hwstat->rx_length_field_frame_errors);
1539 nstat->tx_errors = (hwstat->tx_late_collisions +
1540 hwstat->tx_excessive_collisions +
1541 hwstat->tx_underrun +
1542 hwstat->tx_carrier_sense_errors);
1543 nstat->multicast = hwstat->rx_multicast_frames;
1544 nstat->collisions = (hwstat->tx_single_collision_frames +
1545 hwstat->tx_multiple_collision_frames +
1546 hwstat->tx_excessive_collisions);
1547 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1548 hwstat->rx_jabbers +
1549 hwstat->rx_undersized_frames +
1550 hwstat->rx_length_field_frame_errors);
1551 nstat->rx_over_errors = hwstat->rx_resource_errors;
1552 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1553 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1554 nstat->rx_fifo_errors = hwstat->rx_overruns;
1555 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1556 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1557 nstat->tx_fifo_errors = hwstat->tx_underrun;
1558
1559 return nstat;
1560}
1561
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001562struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001563{
1564 struct macb *bp = netdev_priv(dev);
1565 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001566 struct macb_stats *hwstat = &bp->hw_stats.macb;
1567
1568 if (macb_is_gem(bp))
1569 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001570
frederic RODO6c36a702007-07-12 19:07:24 +02001571 /* read stats from hardware */
1572 macb_update_stats(bp);
1573
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001574 /* Convert HW stats into netdevice stats */
1575 nstat->rx_errors = (hwstat->rx_fcs_errors +
1576 hwstat->rx_align_errors +
1577 hwstat->rx_resource_errors +
1578 hwstat->rx_overruns +
1579 hwstat->rx_oversize_pkts +
1580 hwstat->rx_jabbers +
1581 hwstat->rx_undersize_pkts +
1582 hwstat->sqe_test_errors +
1583 hwstat->rx_length_mismatch);
1584 nstat->tx_errors = (hwstat->tx_late_cols +
1585 hwstat->tx_excessive_cols +
1586 hwstat->tx_underruns +
1587 hwstat->tx_carrier_errors);
1588 nstat->collisions = (hwstat->tx_single_cols +
1589 hwstat->tx_multiple_cols +
1590 hwstat->tx_excessive_cols);
1591 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1592 hwstat->rx_jabbers +
1593 hwstat->rx_undersize_pkts +
1594 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001595 nstat->rx_over_errors = hwstat->rx_resource_errors +
1596 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001597 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1598 nstat->rx_frame_errors = hwstat->rx_align_errors;
1599 nstat->rx_fifo_errors = hwstat->rx_overruns;
1600 /* XXX: What does "missed" mean? */
1601 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1602 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1603 nstat->tx_fifo_errors = hwstat->tx_underruns;
1604 /* Don't know about heartbeat or window errors... */
1605
1606 return nstat;
1607}
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001608EXPORT_SYMBOL_GPL(macb_get_stats);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001609
1610static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1611{
1612 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001613 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001614
frederic RODO6c36a702007-07-12 19:07:24 +02001615 if (!phydev)
1616 return -ENODEV;
1617
1618 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001619}
1620
1621static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1622{
1623 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001624 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001625
frederic RODO6c36a702007-07-12 19:07:24 +02001626 if (!phydev)
1627 return -ENODEV;
1628
1629 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001630}
1631
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001632static int macb_get_regs_len(struct net_device *netdev)
1633{
1634 return MACB_GREGS_NBR * sizeof(u32);
1635}
1636
1637static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1638 void *p)
1639{
1640 struct macb *bp = netdev_priv(dev);
1641 unsigned int tail, head;
1642 u32 *regs_buff = p;
1643
1644 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
1645 | MACB_GREGS_VERSION;
1646
1647 tail = macb_tx_ring_wrap(bp->tx_tail);
1648 head = macb_tx_ring_wrap(bp->tx_head);
1649
1650 regs_buff[0] = macb_readl(bp, NCR);
1651 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
1652 regs_buff[2] = macb_readl(bp, NSR);
1653 regs_buff[3] = macb_readl(bp, TSR);
1654 regs_buff[4] = macb_readl(bp, RBQP);
1655 regs_buff[5] = macb_readl(bp, TBQP);
1656 regs_buff[6] = macb_readl(bp, RSR);
1657 regs_buff[7] = macb_readl(bp, IMR);
1658
1659 regs_buff[8] = tail;
1660 regs_buff[9] = head;
1661 regs_buff[10] = macb_tx_dma(bp, tail);
1662 regs_buff[11] = macb_tx_dma(bp, head);
1663
1664 if (macb_is_gem(bp)) {
1665 regs_buff[12] = gem_readl(bp, USRIO);
1666 regs_buff[13] = gem_readl(bp, DMACFG);
1667 }
1668}
1669
Joachim Eastwood0005f542012-10-18 11:01:12 +00001670const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001671 .get_settings = macb_get_settings,
1672 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001673 .get_regs_len = macb_get_regs_len,
1674 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001675 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00001676 .get_ts_info = ethtool_op_get_ts_info,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001677};
Joachim Eastwood0005f542012-10-18 11:01:12 +00001678EXPORT_SYMBOL_GPL(macb_ethtool_ops);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001679
Joachim Eastwood0005f542012-10-18 11:01:12 +00001680int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001681{
1682 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001683 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001684
1685 if (!netif_running(dev))
1686 return -EINVAL;
1687
frederic RODO6c36a702007-07-12 19:07:24 +02001688 if (!phydev)
1689 return -ENODEV;
1690
Richard Cochran28b04112010-07-17 08:48:55 +00001691 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001692}
Joachim Eastwood0005f542012-10-18 11:01:12 +00001693EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001694
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001695static const struct net_device_ops macb_netdev_ops = {
1696 .ndo_open = macb_open,
1697 .ndo_stop = macb_close,
1698 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001699 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001700 .ndo_get_stats = macb_get_stats,
1701 .ndo_do_ioctl = macb_ioctl,
1702 .ndo_validate_addr = eth_validate_addr,
1703 .ndo_change_mtu = eth_change_mtu,
1704 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001705#ifdef CONFIG_NET_POLL_CONTROLLER
1706 .ndo_poll_controller = macb_poll_controller,
1707#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001708};
1709
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001710#if defined(CONFIG_OF)
1711static const struct of_device_id macb_dt_ids[] = {
1712 { .compatible = "cdns,at32ap7000-macb" },
1713 { .compatible = "cdns,at91sam9260-macb" },
1714 { .compatible = "cdns,macb" },
1715 { .compatible = "cdns,pc302-gem" },
1716 { .compatible = "cdns,gem" },
1717 { /* sentinel */ }
1718};
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001719MODULE_DEVICE_TABLE(of, macb_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001720#endif
1721
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001722static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001723{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001724 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001725 struct resource *regs;
1726 struct net_device *dev;
1727 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001728 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001729 u32 config;
1730 int err = -ENXIO;
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001731 struct pinctrl *pinctrl;
Guenter Roeck50907042013-04-02 09:35:09 +00001732 const char *mac;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001733
1734 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1735 if (!regs) {
1736 dev_err(&pdev->dev, "no mmio resource defined\n");
1737 goto err_out;
1738 }
1739
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001740 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1741 if (IS_ERR(pinctrl)) {
1742 err = PTR_ERR(pinctrl);
1743 if (err == -EPROBE_DEFER)
1744 goto err_out;
1745
1746 dev_warn(&pdev->dev, "No pinctrl provided\n");
1747 }
1748
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001749 err = -ENOMEM;
1750 dev = alloc_etherdev(sizeof(*bp));
Joe Perches41de8d42012-01-29 13:47:52 +00001751 if (!dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001752 goto err_out;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001753
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001754 SET_NETDEV_DEV(dev, &pdev->dev);
1755
1756 /* TODO: Actually, we have some interesting features... */
1757 dev->features |= 0;
1758
1759 bp = netdev_priv(dev);
1760 bp->pdev = pdev;
1761 bp->dev = dev;
1762
1763 spin_lock_init(&bp->lock);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001764 INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001765
Jamie Iles461845d2011-03-08 20:19:23 +00001766 bp->pclk = clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001767 if (IS_ERR(bp->pclk)) {
1768 dev_err(&pdev->dev, "failed to get macb_clk\n");
1769 goto err_out_free_dev;
1770 }
Steffen Trumtrarace58012013-03-27 23:07:07 +00001771 clk_prepare_enable(bp->pclk);
Jamie Iles461845d2011-03-08 20:19:23 +00001772
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001773 bp->hclk = clk_get(&pdev->dev, "hclk");
1774 if (IS_ERR(bp->hclk)) {
1775 dev_err(&pdev->dev, "failed to get hclk\n");
1776 goto err_out_put_pclk;
1777 }
Steffen Trumtrarace58012013-03-27 23:07:07 +00001778 clk_prepare_enable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001779
Joe Perches28f65c112011-06-09 09:13:32 -07001780 bp->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001781 if (!bp->regs) {
1782 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1783 err = -ENOMEM;
1784 goto err_out_disable_clocks;
1785 }
1786
1787 dev->irq = platform_get_irq(pdev, 0);
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00001788 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001789 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001790 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1791 dev->irq, err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001792 goto err_out_iounmap;
1793 }
1794
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001795 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001796 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001797 dev->ethtool_ops = &macb_ethtool_ops;
1798
1799 dev->base_addr = regs->start;
1800
Nicolas Ferre4df95132013-06-04 21:57:12 +00001801 /* setup appropriated routines according to adapter type */
1802 if (macb_is_gem(bp)) {
1803 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
1804 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
1805 bp->macbgem_ops.mog_init_rings = gem_init_rings;
1806 bp->macbgem_ops.mog_rx = gem_rx;
1807 } else {
1808 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
1809 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
1810 bp->macbgem_ops.mog_init_rings = macb_init_rings;
1811 bp->macbgem_ops.mog_rx = macb_rx;
1812 }
1813
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001814 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001815 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00001816 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001817 macb_writel(bp, NCFGR, config);
1818
Guenter Roeck50907042013-04-02 09:35:09 +00001819 mac = of_get_mac_address(pdev->dev.of_node);
1820 if (mac)
1821 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
1822 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001823 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02001824
Guenter Roeck50907042013-04-02 09:35:09 +00001825 err = of_get_phy_mode(pdev->dev.of_node);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001826 if (err < 0) {
1827 pdata = pdev->dev.platform_data;
1828 if (pdata && pdata->is_rmii)
1829 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
1830 else
1831 bp->phy_interface = PHY_INTERFACE_MODE_MII;
1832 } else {
1833 bp->phy_interface = err;
1834 }
1835
Patrice Vilchez140b7552012-10-31 06:04:50 +00001836 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
1837 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
1838 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01001839#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001840 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1841 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01001842#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001843 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001844#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001845 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001846#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001847 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01001848#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001849 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001850#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001851
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001852 err = register_netdev(dev);
1853 if (err) {
1854 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1855 goto err_out_free_irq;
1856 }
1857
Nicolas Ferre72ca8202013-04-14 22:04:33 +00001858 err = macb_mii_init(bp);
1859 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02001860 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001861
frederic RODO6c36a702007-07-12 19:07:24 +02001862 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001863
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001864 netif_carrier_off(dev);
1865
Jamie Ilesf75ba502011-11-08 10:12:32 +00001866 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1867 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1868 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001869
frederic RODO6c36a702007-07-12 19:07:24 +02001870 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001871 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1872 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02001873
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001874 return 0;
1875
frederic RODO6c36a702007-07-12 19:07:24 +02001876err_out_unregister_netdev:
1877 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001878err_out_free_irq:
1879 free_irq(dev->irq, dev);
1880err_out_iounmap:
1881 iounmap(bp->regs);
1882err_out_disable_clocks:
Steffen Trumtrarace58012013-03-27 23:07:07 +00001883 clk_disable_unprepare(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001884 clk_put(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00001885 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001886err_out_put_pclk:
1887 clk_put(bp->pclk);
1888err_out_free_dev:
1889 free_netdev(dev);
1890err_out:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001891 return err;
1892}
1893
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001894static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001895{
1896 struct net_device *dev;
1897 struct macb *bp;
1898
1899 dev = platform_get_drvdata(pdev);
1900
1901 if (dev) {
1902 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09001903 if (bp->phy_dev)
1904 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001905 mdiobus_unregister(bp->mii_bus);
1906 kfree(bp->mii_bus->irq);
1907 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001908 unregister_netdev(dev);
1909 free_irq(dev->irq, dev);
1910 iounmap(bp->regs);
Steffen Trumtrarace58012013-03-27 23:07:07 +00001911 clk_disable_unprepare(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001912 clk_put(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00001913 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001914 clk_put(bp->pclk);
1915 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001916 }
1917
1918 return 0;
1919}
1920
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001921#ifdef CONFIG_PM
1922static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1923{
1924 struct net_device *netdev = platform_get_drvdata(pdev);
1925 struct macb *bp = netdev_priv(netdev);
1926
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001927 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001928 netif_device_detach(netdev);
1929
Steffen Trumtrarace58012013-03-27 23:07:07 +00001930 clk_disable_unprepare(bp->hclk);
1931 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001932
1933 return 0;
1934}
1935
1936static int macb_resume(struct platform_device *pdev)
1937{
1938 struct net_device *netdev = platform_get_drvdata(pdev);
1939 struct macb *bp = netdev_priv(netdev);
1940
Steffen Trumtrarace58012013-03-27 23:07:07 +00001941 clk_prepare_enable(bp->pclk);
1942 clk_prepare_enable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001943
1944 netif_device_attach(netdev);
1945
1946 return 0;
1947}
1948#else
1949#define macb_suspend NULL
1950#define macb_resume NULL
1951#endif
1952
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001953static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001954 .remove = __exit_p(macb_remove),
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001955 .suspend = macb_suspend,
1956 .resume = macb_resume,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001957 .driver = {
1958 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07001959 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001960 .of_match_table = of_match_ptr(macb_dt_ids),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001961 },
1962};
1963
Jingoo Hanb543a8d2013-03-04 16:43:18 +00001964module_platform_driver_probe(macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001965
1966MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00001967MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001968MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07001969MODULE_ALIAS("platform:macb");