blob: 0931cb7469dc2b0be99a0e56b99602803cf3a829 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000019#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010020#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010022#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000023#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010024#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020025#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080026#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010027#include <linux/of_device.h>
28#include <linux/of_net.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010029
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010030#include "macb.h"
31
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010032#define RX_BUFFER_SIZE 128
33#define RX_RING_SIZE 512
34#define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
35
36/* Make the IP header word-aligned (the ethernet header is 14 bytes) */
37#define RX_OFFSET 2
38
39#define TX_RING_SIZE 128
40#define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
41#define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
42
43#define TX_RING_GAP(bp) \
44 (TX_RING_SIZE - (bp)->tx_pending)
45#define TX_BUFFS_AVAIL(bp) \
46 (((bp)->tx_tail <= (bp)->tx_head) ? \
47 (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
48 (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
49#define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
50
51#define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
52
53/* minimum number of free TX descriptors before waking up TX process */
54#define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
55
56#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
57 | MACB_BIT(ISR_ROVR))
58
59static void __macb_set_hwaddr(struct macb *bp)
60{
61 u32 bottom;
62 u16 top;
63
64 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +000065 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010066 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +000067 macb_or_gem_writel(bp, SA1T, top);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010068}
69
70static void __init macb_get_hwaddr(struct macb *bp)
71{
72 u32 bottom;
73 u16 top;
74 u8 addr[6];
75
Jamie Ilesf75ba502011-11-08 10:12:32 +000076 bottom = macb_or_gem_readl(bp, SA1B);
77 top = macb_or_gem_readl(bp, SA1T);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010078
79 addr[0] = bottom & 0xff;
80 addr[1] = (bottom >> 8) & 0xff;
81 addr[2] = (bottom >> 16) & 0xff;
82 addr[3] = (bottom >> 24) & 0xff;
83 addr[4] = top & 0xff;
84 addr[5] = (top >> 8) & 0xff;
85
Sven Schnelled1d57412008-06-09 16:33:57 -070086 if (is_valid_ether_addr(addr)) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010087 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
Sven Schnelled1d57412008-06-09 16:33:57 -070088 } else {
Jamie Ilesc220f8c2011-03-08 20:27:08 +000089 netdev_info(bp->dev, "invalid hw address, using random\n");
Danny Kukawkaf2cedb62012-02-15 06:45:39 +000090 eth_hw_addr_random(bp->dev);
Sven Schnelled1d57412008-06-09 16:33:57 -070091 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010092}
93
frederic RODO6c36a702007-07-12 19:07:24 +020094static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010095{
frederic RODO6c36a702007-07-12 19:07:24 +020096 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010097 int value;
98
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010099 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
100 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200101 | MACB_BF(PHYA, mii_id)
102 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100103 | MACB_BF(CODE, MACB_MAN_CODE)));
104
frederic RODO6c36a702007-07-12 19:07:24 +0200105 /* wait for end of transfer */
106 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
107 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100108
109 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100110
111 return value;
112}
113
frederic RODO6c36a702007-07-12 19:07:24 +0200114static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
115 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100116{
frederic RODO6c36a702007-07-12 19:07:24 +0200117 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100118
119 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
120 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200121 | MACB_BF(PHYA, mii_id)
122 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100123 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200124 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100125
frederic RODO6c36a702007-07-12 19:07:24 +0200126 /* wait for end of transfer */
127 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
128 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100129
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100130 return 0;
131}
132
frederic RODO6c36a702007-07-12 19:07:24 +0200133static int macb_mdio_reset(struct mii_bus *bus)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100134{
frederic RODO6c36a702007-07-12 19:07:24 +0200135 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100136}
137
frederic RODO6c36a702007-07-12 19:07:24 +0200138static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100139{
frederic RODO6c36a702007-07-12 19:07:24 +0200140 struct macb *bp = netdev_priv(dev);
141 struct phy_device *phydev = bp->phy_dev;
142 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100143
frederic RODO6c36a702007-07-12 19:07:24 +0200144 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100145
frederic RODO6c36a702007-07-12 19:07:24 +0200146 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100147
frederic RODO6c36a702007-07-12 19:07:24 +0200148 if (phydev->link) {
149 if ((bp->speed != phydev->speed) ||
150 (bp->duplex != phydev->duplex)) {
151 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100152
frederic RODO6c36a702007-07-12 19:07:24 +0200153 reg = macb_readl(bp, NCFGR);
154 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000155 if (macb_is_gem(bp))
156 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200157
158 if (phydev->duplex)
159 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900160 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200161 reg |= MACB_BIT(SPD);
Patrice Vilchez140b7552012-10-31 06:04:50 +0000162 if (phydev->speed == SPEED_1000)
163 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200164
Patrice Vilchez140b7552012-10-31 06:04:50 +0000165 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200166
167 bp->speed = phydev->speed;
168 bp->duplex = phydev->duplex;
169 status_change = 1;
170 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100171 }
172
frederic RODO6c36a702007-07-12 19:07:24 +0200173 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700174 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200175 bp->speed = 0;
176 bp->duplex = -1;
177 }
178 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100179
frederic RODO6c36a702007-07-12 19:07:24 +0200180 status_change = 1;
181 }
182
183 spin_unlock_irqrestore(&bp->lock, flags);
184
185 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000186 if (phydev->link) {
187 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000188 netdev_info(dev, "link up (%d/%s)\n",
189 phydev->speed,
190 phydev->duplex == DUPLEX_FULL ?
191 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000192 } else {
193 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000194 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000195 }
frederic RODO6c36a702007-07-12 19:07:24 +0200196 }
197}
198
199/* based on au1000_eth. c*/
200static int macb_mii_probe(struct net_device *dev)
201{
202 struct macb *bp = netdev_priv(dev);
Jiri Pirko7455a762010-02-08 05:12:08 +0000203 struct phy_device *phydev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000204 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200205
Jiri Pirko7455a762010-02-08 05:12:08 +0000206 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200207 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000208 netdev_err(dev, "no PHY found\n");
frederic RODO6c36a702007-07-12 19:07:24 +0200209 return -1;
210 }
211
frederic RODO6c36a702007-07-12 19:07:24 +0200212 /* TODO : add pin_irq */
213
214 /* attach the mac to the phy */
Jiri Pirko7455a762010-02-08 05:12:08 +0000215 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100216 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000217 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000218 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000219 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200220 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100221
frederic RODO6c36a702007-07-12 19:07:24 +0200222 /* mask with MAC supported features */
Patrice Vilchez140b7552012-10-31 06:04:50 +0000223 if (macb_is_gem(bp))
224 phydev->supported &= PHY_GBIT_FEATURES;
225 else
226 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100227
frederic RODO6c36a702007-07-12 19:07:24 +0200228 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100229
frederic RODO6c36a702007-07-12 19:07:24 +0200230 bp->link = 0;
231 bp->speed = 0;
232 bp->duplex = -1;
233 bp->phy_dev = phydev;
234
235 return 0;
236}
237
Joachim Eastwood0005f542012-10-18 11:01:12 +0000238int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200239{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000240 struct macb_platform_data *pdata;
frederic RODO6c36a702007-07-12 19:07:24 +0200241 int err = -ENXIO, i;
242
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200243 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200244 macb_writel(bp, NCR, MACB_BIT(MPE));
245
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700246 bp->mii_bus = mdiobus_alloc();
247 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200248 err = -ENOMEM;
249 goto err_out;
250 }
251
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700252 bp->mii_bus->name = "MACB_mii_bus";
253 bp->mii_bus->read = &macb_mdio_read;
254 bp->mii_bus->write = &macb_mdio_write;
255 bp->mii_bus->reset = &macb_mdio_reset;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000256 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
257 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700258 bp->mii_bus->priv = bp;
259 bp->mii_bus->parent = &bp->dev->dev;
260 pdata = bp->pdev->dev.platform_data;
261
262 if (pdata)
263 bp->mii_bus->phy_mask = pdata->phy_mask;
264
265 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
266 if (!bp->mii_bus->irq) {
267 err = -ENOMEM;
268 goto err_out_free_mdiobus;
269 }
270
frederic RODO6c36a702007-07-12 19:07:24 +0200271 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700272 bp->mii_bus->irq[i] = PHY_POLL;
frederic RODO6c36a702007-07-12 19:07:24 +0200273
Jamie Iles91523942011-02-28 04:05:25 +0000274 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200275
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700276 if (mdiobus_register(bp->mii_bus))
frederic RODO6c36a702007-07-12 19:07:24 +0200277 goto err_out_free_mdio_irq;
278
279 if (macb_mii_probe(bp->dev) != 0) {
280 goto err_out_unregister_bus;
281 }
282
283 return 0;
284
285err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700286 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200287err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700288 kfree(bp->mii_bus->irq);
289err_out_free_mdiobus:
290 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200291err_out:
292 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100293}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000294EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100295
296static void macb_update_stats(struct macb *bp)
297{
298 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000299 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
300 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100301
302 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
303
304 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100305 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100306}
307
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100308static void macb_tx(struct macb *bp)
309{
310 unsigned int tail;
311 unsigned int head;
312 u32 status;
313
314 status = macb_readl(bp, TSR);
315 macb_writel(bp, TSR, status);
316
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000317 netdev_dbg(bp->dev, "macb_tx status = %02lx\n", (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100318
Erik Walingee33c582009-04-15 23:32:10 +0000319 if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) {
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100320 int i;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000321 netdev_err(bp->dev, "TX %s, resetting buffers\n",
322 status & MACB_BIT(UND) ?
323 "underrun" : "retry limit exceeded");
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100324
Richard Röjfors39eddb42009-01-18 21:57:35 -0800325 /* Transfer ongoing, disable transmitter, to avoid confusion */
326 if (status & MACB_BIT(TGO))
327 macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE));
328
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100329 head = bp->tx_head;
330
331 /*Mark all the buffer as used to avoid sending a lost buffer*/
332 for (i = 0; i < TX_RING_SIZE; i++)
333 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
334
Tord Anderssond3e61452011-08-03 22:11:47 +0000335 /* Add wrap bit */
336 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
337
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100338 /* free transmit buffer in upper layer*/
339 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
340 struct ring_info *rp = &bp->tx_skb[tail];
341 struct sk_buff *skb = rp->skb;
342
343 BUG_ON(skb == NULL);
344
345 rmb();
346
347 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
348 DMA_TO_DEVICE);
349 rp->skb = NULL;
350 dev_kfree_skb_irq(skb);
351 }
352
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100353 bp->tx_head = bp->tx_tail = 0;
Richard Röjfors39eddb42009-01-18 21:57:35 -0800354
355 /* Enable the transmitter again */
356 if (status & MACB_BIT(TGO))
357 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100358 }
359
360 if (!(status & MACB_BIT(COMP)))
361 /*
362 * This may happen when a buffer becomes complete
363 * between reading the ISR and scanning the
364 * descriptors. Nothing to worry about.
365 */
366 return;
367
368 head = bp->tx_head;
369 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
370 struct ring_info *rp = &bp->tx_skb[tail];
371 struct sk_buff *skb = rp->skb;
372 u32 bufstat;
373
374 BUG_ON(skb == NULL);
375
376 rmb();
377 bufstat = bp->tx_ring[tail].ctrl;
378
379 if (!(bufstat & MACB_BIT(TX_USED)))
380 break;
381
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000382 netdev_dbg(bp->dev, "skb %u (data %p) TX complete\n",
383 tail, skb->data);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100384 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
385 DMA_TO_DEVICE);
386 bp->stats.tx_packets++;
387 bp->stats.tx_bytes += skb->len;
388 rp->skb = NULL;
389 dev_kfree_skb_irq(skb);
390 }
391
392 bp->tx_tail = tail;
393 if (netif_queue_stopped(bp->dev) &&
394 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
395 netif_wake_queue(bp->dev);
396}
397
398static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
399 unsigned int last_frag)
400{
401 unsigned int len;
402 unsigned int frag;
403 unsigned int offset = 0;
404 struct sk_buff *skb;
405
406 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
407
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000408 netdev_dbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
409 first_frag, last_frag, len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100410
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +0000411 skb = netdev_alloc_skb(bp->dev, len + RX_OFFSET);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100412 if (!skb) {
413 bp->stats.rx_dropped++;
414 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
415 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
416 if (frag == last_frag)
417 break;
418 }
419 wmb();
420 return 1;
421 }
422
423 skb_reserve(skb, RX_OFFSET);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700424 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100425 skb_put(skb, len);
426
427 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
428 unsigned int frag_len = RX_BUFFER_SIZE;
429
430 if (offset + frag_len > len) {
431 BUG_ON(frag != last_frag);
432 frag_len = len - offset;
433 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300434 skb_copy_to_linear_data_offset(skb, offset,
435 (bp->rx_buffers +
436 (RX_BUFFER_SIZE * frag)),
437 frag_len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438 offset += RX_BUFFER_SIZE;
439 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
440 wmb();
441
442 if (frag == last_frag)
443 break;
444 }
445
446 skb->protocol = eth_type_trans(skb, bp->dev);
447
448 bp->stats.rx_packets++;
449 bp->stats.rx_bytes += len;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000450 netdev_dbg(bp->dev, "received skb of length %u, csum: %08x\n",
451 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100452 netif_receive_skb(skb);
453
454 return 0;
455}
456
457/* Mark DMA descriptors from begin up to and not including end as unused */
458static void discard_partial_frame(struct macb *bp, unsigned int begin,
459 unsigned int end)
460{
461 unsigned int frag;
462
463 for (frag = begin; frag != end; frag = NEXT_RX(frag))
464 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
465 wmb();
466
467 /*
468 * When this happens, the hardware stats registers for
469 * whatever caused this is updated, so we don't have to record
470 * anything.
471 */
472}
473
474static int macb_rx(struct macb *bp, int budget)
475{
476 int received = 0;
477 unsigned int tail = bp->rx_tail;
478 int first_frag = -1;
479
480 for (; budget > 0; tail = NEXT_RX(tail)) {
481 u32 addr, ctrl;
482
483 rmb();
484 addr = bp->rx_ring[tail].addr;
485 ctrl = bp->rx_ring[tail].ctrl;
486
487 if (!(addr & MACB_BIT(RX_USED)))
488 break;
489
490 if (ctrl & MACB_BIT(RX_SOF)) {
491 if (first_frag != -1)
492 discard_partial_frame(bp, first_frag, tail);
493 first_frag = tail;
494 }
495
496 if (ctrl & MACB_BIT(RX_EOF)) {
497 int dropped;
498 BUG_ON(first_frag == -1);
499
500 dropped = macb_rx_frame(bp, first_frag, tail);
501 first_frag = -1;
502 if (!dropped) {
503 received++;
504 budget--;
505 }
506 }
507 }
508
509 if (first_frag != -1)
510 bp->rx_tail = first_frag;
511 else
512 bp->rx_tail = tail;
513
514 return received;
515}
516
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700517static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100518{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700519 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700520 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100521 u32 status;
522
523 status = macb_readl(bp, RSR);
524 macb_writel(bp, RSR, status);
525
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700526 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100527
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000528 netdev_dbg(bp->dev, "poll: status = %08lx, budget = %d\n",
529 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100530
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700531 work_done = macb_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000532 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800533 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100534
Joshua Hokeb3363692010-10-25 01:44:22 +0000535 /*
536 * We've done what we can to clean the buffers. Make sure we
537 * get notified when new packets arrive.
538 */
539 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
540 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100541
542 /* TODO: Handle errors */
543
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700544 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100545}
546
547static irqreturn_t macb_interrupt(int irq, void *dev_id)
548{
549 struct net_device *dev = dev_id;
550 struct macb *bp = netdev_priv(dev);
551 u32 status;
552
553 status = macb_readl(bp, ISR);
554
555 if (unlikely(!status))
556 return IRQ_NONE;
557
558 spin_lock(&bp->lock);
559
560 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100561 /* close possible race with dev_close */
562 if (unlikely(!netif_running(dev))) {
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000563 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100564 break;
565 }
566
567 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000568 /*
569 * There's no point taking any more interrupts
570 * until we have processed the buffers. The
571 * scheduling call may fail if the poll routine
572 * is already scheduled, so disable interrupts
573 * now.
574 */
575 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
576
Ben Hutchings288379f2009-01-19 16:43:59 -0800577 if (napi_schedule_prep(&bp->napi)) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000578 netdev_dbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800579 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100580 }
581 }
582
Erik Walingee33c582009-04-15 23:32:10 +0000583 if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
584 MACB_BIT(ISR_RLE)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100585 macb_tx(bp);
586
587 /*
588 * Link change detection isn't possible with RMII, so we'll
589 * add that if/when we get our hands on a full-blown MII PHY.
590 */
591
Alexander Steinb19f7f72011-04-13 05:03:24 +0000592 if (status & MACB_BIT(ISR_ROVR)) {
593 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000594 if (macb_is_gem(bp))
595 bp->hw_stats.gem.rx_overruns++;
596 else
597 bp->hw_stats.macb.rx_overruns++;
Alexander Steinb19f7f72011-04-13 05:03:24 +0000598 }
599
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100600 if (status & MACB_BIT(HRESP)) {
601 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000602 * TODO: Reset the hardware, and maybe move the
603 * netdev_err to a lower-priority context as well
604 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100605 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000606 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100607 }
608
609 status = macb_readl(bp, ISR);
610 }
611
612 spin_unlock(&bp->lock);
613
614 return IRQ_HANDLED;
615}
616
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700617#ifdef CONFIG_NET_POLL_CONTROLLER
618/*
619 * Polling receive - used by netconsole and other diagnostic tools
620 * to allow network i/o with interrupts disabled.
621 */
622static void macb_poll_controller(struct net_device *dev)
623{
624 unsigned long flags;
625
626 local_irq_save(flags);
627 macb_interrupt(dev->irq, dev);
628 local_irq_restore(flags);
629}
630#endif
631
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100632static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
633{
634 struct macb *bp = netdev_priv(dev);
635 dma_addr_t mapping;
636 unsigned int len, entry;
637 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -0700638 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100639
640#ifdef DEBUG
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000641 netdev_dbg(bp->dev,
642 "start_xmit: len %u head %p data %p tail %p end %p\n",
643 skb->len, skb->head, skb->data,
644 skb_tail_pointer(skb), skb_end_pointer(skb));
645 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
646 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100647#endif
648
649 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -0700650 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100651
652 /* This is a hard error, log it. */
653 if (TX_BUFFS_AVAIL(bp) < 1) {
654 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -0700655 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000656 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
657 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
658 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +0000659 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100660 }
661
662 entry = bp->tx_head;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000663 netdev_dbg(bp->dev, "Allocated ring entry %u\n", entry);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100664 mapping = dma_map_single(&bp->pdev->dev, skb->data,
665 len, DMA_TO_DEVICE);
666 bp->tx_skb[entry].skb = skb;
667 bp->tx_skb[entry].mapping = mapping;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000668 netdev_dbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
669 skb->data, (unsigned long)mapping);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100670
671 ctrl = MACB_BF(TX_FRMLEN, len);
672 ctrl |= MACB_BIT(TX_LAST);
673 if (entry == (TX_RING_SIZE - 1))
674 ctrl |= MACB_BIT(TX_WRAP);
675
676 bp->tx_ring[entry].addr = mapping;
677 bp->tx_ring[entry].ctrl = ctrl;
678 wmb();
679
680 entry = NEXT_TX(entry);
681 bp->tx_head = entry;
682
Richard Cochrane0720922011-06-19 21:51:28 +0000683 skb_tx_timestamp(skb);
684
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100685 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
686
687 if (TX_BUFFS_AVAIL(bp) < 1)
688 netif_stop_queue(dev);
689
Dongdong Deng48719532009-08-23 19:49:07 -0700690 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100691
Patrick McHardy6ed10652009-06-23 06:03:08 +0000692 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100693}
694
695static void macb_free_consistent(struct macb *bp)
696{
697 if (bp->tx_skb) {
698 kfree(bp->tx_skb);
699 bp->tx_skb = NULL;
700 }
701 if (bp->rx_ring) {
702 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
703 bp->rx_ring, bp->rx_ring_dma);
704 bp->rx_ring = NULL;
705 }
706 if (bp->tx_ring) {
707 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
708 bp->tx_ring, bp->tx_ring_dma);
709 bp->tx_ring = NULL;
710 }
711 if (bp->rx_buffers) {
712 dma_free_coherent(&bp->pdev->dev,
713 RX_RING_SIZE * RX_BUFFER_SIZE,
714 bp->rx_buffers, bp->rx_buffers_dma);
715 bp->rx_buffers = NULL;
716 }
717}
718
719static int macb_alloc_consistent(struct macb *bp)
720{
721 int size;
722
723 size = TX_RING_SIZE * sizeof(struct ring_info);
724 bp->tx_skb = kmalloc(size, GFP_KERNEL);
725 if (!bp->tx_skb)
726 goto out_err;
727
728 size = RX_RING_BYTES;
729 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
730 &bp->rx_ring_dma, GFP_KERNEL);
731 if (!bp->rx_ring)
732 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000733 netdev_dbg(bp->dev,
734 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
735 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100736
737 size = TX_RING_BYTES;
738 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
739 &bp->tx_ring_dma, GFP_KERNEL);
740 if (!bp->tx_ring)
741 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000742 netdev_dbg(bp->dev,
743 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
744 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100745
746 size = RX_RING_SIZE * RX_BUFFER_SIZE;
747 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
748 &bp->rx_buffers_dma, GFP_KERNEL);
749 if (!bp->rx_buffers)
750 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000751 netdev_dbg(bp->dev,
752 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
753 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100754
755 return 0;
756
757out_err:
758 macb_free_consistent(bp);
759 return -ENOMEM;
760}
761
762static void macb_init_rings(struct macb *bp)
763{
764 int i;
765 dma_addr_t addr;
766
767 addr = bp->rx_buffers_dma;
768 for (i = 0; i < RX_RING_SIZE; i++) {
769 bp->rx_ring[i].addr = addr;
770 bp->rx_ring[i].ctrl = 0;
771 addr += RX_BUFFER_SIZE;
772 }
773 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
774
775 for (i = 0; i < TX_RING_SIZE; i++) {
776 bp->tx_ring[i].addr = 0;
777 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
778 }
779 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
780
781 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
782}
783
784static void macb_reset_hw(struct macb *bp)
785{
786 /* Make sure we have the write buffer for ourselves */
787 wmb();
788
789 /*
790 * Disable RX and TX (XXX: Should we halt the transmission
791 * more gracefully?)
792 */
793 macb_writel(bp, NCR, 0);
794
795 /* Clear the stats registers (XXX: Update stats first?) */
796 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
797
798 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000799 macb_writel(bp, TSR, -1);
800 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100801
802 /* Disable all interrupts */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000803 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100804 macb_readl(bp, ISR);
805}
806
Jamie Iles70c9f3d2011-03-09 16:22:54 +0000807static u32 gem_mdc_clk_div(struct macb *bp)
808{
809 u32 config;
810 unsigned long pclk_hz = clk_get_rate(bp->pclk);
811
812 if (pclk_hz <= 20000000)
813 config = GEM_BF(CLK, GEM_CLK_DIV8);
814 else if (pclk_hz <= 40000000)
815 config = GEM_BF(CLK, GEM_CLK_DIV16);
816 else if (pclk_hz <= 80000000)
817 config = GEM_BF(CLK, GEM_CLK_DIV32);
818 else if (pclk_hz <= 120000000)
819 config = GEM_BF(CLK, GEM_CLK_DIV48);
820 else if (pclk_hz <= 160000000)
821 config = GEM_BF(CLK, GEM_CLK_DIV64);
822 else
823 config = GEM_BF(CLK, GEM_CLK_DIV96);
824
825 return config;
826}
827
828static u32 macb_mdc_clk_div(struct macb *bp)
829{
830 u32 config;
831 unsigned long pclk_hz;
832
833 if (macb_is_gem(bp))
834 return gem_mdc_clk_div(bp);
835
836 pclk_hz = clk_get_rate(bp->pclk);
837 if (pclk_hz <= 20000000)
838 config = MACB_BF(CLK, MACB_CLK_DIV8);
839 else if (pclk_hz <= 40000000)
840 config = MACB_BF(CLK, MACB_CLK_DIV16);
841 else if (pclk_hz <= 80000000)
842 config = MACB_BF(CLK, MACB_CLK_DIV32);
843 else
844 config = MACB_BF(CLK, MACB_CLK_DIV64);
845
846 return config;
847}
848
Jamie Iles757a03c2011-03-09 16:29:59 +0000849/*
850 * Get the DMA bus width field of the network configuration register that we
851 * should program. We find the width from decoding the design configuration
852 * register to find the maximum supported data bus width.
853 */
854static u32 macb_dbw(struct macb *bp)
855{
856 if (!macb_is_gem(bp))
857 return 0;
858
859 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
860 case 4:
861 return GEM_BF(DBW, GEM_DBW128);
862 case 2:
863 return GEM_BF(DBW, GEM_DBW64);
864 case 1:
865 default:
866 return GEM_BF(DBW, GEM_DBW32);
867 }
868}
869
Jamie Iles0116da42011-03-14 17:38:30 +0000870/*
871 * Configure the receive DMA engine to use the correct receive buffer size.
872 * This is a configurable parameter for GEM.
873 */
874static void macb_configure_dma(struct macb *bp)
875{
876 u32 dmacfg;
877
878 if (macb_is_gem(bp)) {
879 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
880 dmacfg |= GEM_BF(RXBS, RX_BUFFER_SIZE / 64);
881 gem_writel(bp, DMACFG, dmacfg);
882 }
883}
884
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100885static void macb_init_hw(struct macb *bp)
886{
887 u32 config;
888
889 macb_reset_hw(bp);
890 __macb_set_hwaddr(bp);
891
Jamie Iles70c9f3d2011-03-09 16:22:54 +0000892 config = macb_mdc_clk_div(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100893 config |= MACB_BIT(PAE); /* PAuse Enable */
894 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -0700895 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100896 if (bp->dev->flags & IFF_PROMISC)
897 config |= MACB_BIT(CAF); /* Copy All Frames */
898 if (!(bp->dev->flags & IFF_BROADCAST))
899 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +0000900 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100901 macb_writel(bp, NCFGR, config);
902
Jamie Iles0116da42011-03-14 17:38:30 +0000903 macb_configure_dma(bp);
904
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100905 /* Initialize TX and RX buffers */
906 macb_writel(bp, RBQP, bp->rx_ring_dma);
907 macb_writel(bp, TBQP, bp->tx_ring_dma);
908
909 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +0200910 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100911
912 /* Enable interrupts */
913 macb_writel(bp, IER, (MACB_BIT(RCOMP)
914 | MACB_BIT(RXUBR)
915 | MACB_BIT(ISR_TUND)
916 | MACB_BIT(ISR_RLE)
917 | MACB_BIT(TXERR)
918 | MACB_BIT(TCOMP)
919 | MACB_BIT(ISR_ROVR)
920 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100921
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100922}
923
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200924/*
925 * The hash address register is 64 bits long and takes up two
926 * locations in the memory map. The least significant bits are stored
927 * in EMAC_HSL and the most significant bits in EMAC_HSH.
928 *
929 * The unicast hash enable and the multicast hash enable bits in the
930 * network configuration register enable the reception of hash matched
931 * frames. The destination address is reduced to a 6 bit index into
932 * the 64 bit hash register using the following hash function. The
933 * hash function is an exclusive or of every sixth bit of the
934 * destination address.
935 *
936 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
937 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
938 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
939 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
940 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
941 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
942 *
943 * da[0] represents the least significant bit of the first byte
944 * received, that is, the multicast/unicast indicator, and da[47]
945 * represents the most significant bit of the last byte received. If
946 * the hash index, hi[n], points to a bit that is set in the hash
947 * register then the frame will be matched according to whether the
948 * frame is multicast or unicast. A multicast match will be signalled
949 * if the multicast hash enable bit is set, da[0] is 1 and the hash
950 * index points to a bit set in the hash register. A unicast match
951 * will be signalled if the unicast hash enable bit is set, da[0] is 0
952 * and the hash index points to a bit set in the hash register. To
953 * receive all multicast frames, the hash register should be set with
954 * all ones and the multicast hash enable bit should be set in the
955 * network configuration register.
956 */
957
958static inline int hash_bit_value(int bitnr, __u8 *addr)
959{
960 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
961 return 1;
962 return 0;
963}
964
965/*
966 * Return the hash index value for the specified address.
967 */
968static int hash_get_index(__u8 *addr)
969{
970 int i, j, bitval;
971 int hash_index = 0;
972
973 for (j = 0; j < 6; j++) {
974 for (i = 0, bitval = 0; i < 8; i++)
975 bitval ^= hash_bit_value(i*6 + j, addr);
976
977 hash_index |= (bitval << j);
978 }
979
980 return hash_index;
981}
982
983/*
984 * Add multicast addresses to the internal multicast-hash table.
985 */
986static void macb_sethashtable(struct net_device *dev)
987{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000988 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200989 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000990 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200991 struct macb *bp = netdev_priv(dev);
992
993 mc_filter[0] = mc_filter[1] = 0;
994
Jiri Pirko22bedad32010-04-01 21:22:57 +0000995 netdev_for_each_mc_addr(ha, dev) {
996 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200997 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
998 }
999
Jamie Ilesf75ba502011-11-08 10:12:32 +00001000 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1001 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001002}
1003
1004/*
1005 * Enable/Disable promiscuous and multicast modes.
1006 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001007void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001008{
1009 unsigned long cfg;
1010 struct macb *bp = netdev_priv(dev);
1011
1012 cfg = macb_readl(bp, NCFGR);
1013
1014 if (dev->flags & IFF_PROMISC)
1015 /* Enable promiscuous mode */
1016 cfg |= MACB_BIT(CAF);
1017 else if (dev->flags & (~IFF_PROMISC))
1018 /* Disable promiscuous mode */
1019 cfg &= ~MACB_BIT(CAF);
1020
1021 if (dev->flags & IFF_ALLMULTI) {
1022 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001023 macb_or_gem_writel(bp, HRB, -1);
1024 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001025 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001026 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001027 /* Enable specific multicasts */
1028 macb_sethashtable(dev);
1029 cfg |= MACB_BIT(NCFGR_MTI);
1030 } else if (dev->flags & (~IFF_ALLMULTI)) {
1031 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001032 macb_or_gem_writel(bp, HRB, 0);
1033 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001034 cfg &= ~MACB_BIT(NCFGR_MTI);
1035 }
1036
1037 macb_writel(bp, NCFGR, cfg);
1038}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001039EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001040
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001041static int macb_open(struct net_device *dev)
1042{
1043 struct macb *bp = netdev_priv(dev);
1044 int err;
1045
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001046 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001047
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001048 /* carrier starts down */
1049 netif_carrier_off(dev);
1050
frederic RODO6c36a702007-07-12 19:07:24 +02001051 /* if the phy is not yet register, retry later*/
1052 if (!bp->phy_dev)
1053 return -EAGAIN;
1054
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001055 if (!is_valid_ether_addr(dev->dev_addr))
1056 return -EADDRNOTAVAIL;
1057
1058 err = macb_alloc_consistent(bp);
1059 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001060 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1061 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001062 return err;
1063 }
1064
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001065 napi_enable(&bp->napi);
1066
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001067 macb_init_rings(bp);
1068 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001069
frederic RODO6c36a702007-07-12 19:07:24 +02001070 /* schedule a link state check */
1071 phy_start(bp->phy_dev);
1072
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001073 netif_start_queue(dev);
1074
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001075 return 0;
1076}
1077
1078static int macb_close(struct net_device *dev)
1079{
1080 struct macb *bp = netdev_priv(dev);
1081 unsigned long flags;
1082
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001084 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001085
frederic RODO6c36a702007-07-12 19:07:24 +02001086 if (bp->phy_dev)
1087 phy_stop(bp->phy_dev);
1088
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001089 spin_lock_irqsave(&bp->lock, flags);
1090 macb_reset_hw(bp);
1091 netif_carrier_off(dev);
1092 spin_unlock_irqrestore(&bp->lock, flags);
1093
1094 macb_free_consistent(bp);
1095
1096 return 0;
1097}
1098
Jamie Ilesa494ed82011-03-09 16:26:35 +00001099static void gem_update_stats(struct macb *bp)
1100{
1101 u32 __iomem *reg = bp->regs + GEM_OTX;
1102 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1103 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1104
1105 for (; p < end; p++, reg++)
1106 *p += __raw_readl(reg);
1107}
1108
1109static struct net_device_stats *gem_get_stats(struct macb *bp)
1110{
1111 struct gem_stats *hwstat = &bp->hw_stats.gem;
1112 struct net_device_stats *nstat = &bp->stats;
1113
1114 gem_update_stats(bp);
1115
1116 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1117 hwstat->rx_alignment_errors +
1118 hwstat->rx_resource_errors +
1119 hwstat->rx_overruns +
1120 hwstat->rx_oversize_frames +
1121 hwstat->rx_jabbers +
1122 hwstat->rx_undersized_frames +
1123 hwstat->rx_length_field_frame_errors);
1124 nstat->tx_errors = (hwstat->tx_late_collisions +
1125 hwstat->tx_excessive_collisions +
1126 hwstat->tx_underrun +
1127 hwstat->tx_carrier_sense_errors);
1128 nstat->multicast = hwstat->rx_multicast_frames;
1129 nstat->collisions = (hwstat->tx_single_collision_frames +
1130 hwstat->tx_multiple_collision_frames +
1131 hwstat->tx_excessive_collisions);
1132 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1133 hwstat->rx_jabbers +
1134 hwstat->rx_undersized_frames +
1135 hwstat->rx_length_field_frame_errors);
1136 nstat->rx_over_errors = hwstat->rx_resource_errors;
1137 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1138 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1139 nstat->rx_fifo_errors = hwstat->rx_overruns;
1140 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1141 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1142 nstat->tx_fifo_errors = hwstat->tx_underrun;
1143
1144 return nstat;
1145}
1146
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001147static struct net_device_stats *macb_get_stats(struct net_device *dev)
1148{
1149 struct macb *bp = netdev_priv(dev);
1150 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001151 struct macb_stats *hwstat = &bp->hw_stats.macb;
1152
1153 if (macb_is_gem(bp))
1154 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001155
frederic RODO6c36a702007-07-12 19:07:24 +02001156 /* read stats from hardware */
1157 macb_update_stats(bp);
1158
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001159 /* Convert HW stats into netdevice stats */
1160 nstat->rx_errors = (hwstat->rx_fcs_errors +
1161 hwstat->rx_align_errors +
1162 hwstat->rx_resource_errors +
1163 hwstat->rx_overruns +
1164 hwstat->rx_oversize_pkts +
1165 hwstat->rx_jabbers +
1166 hwstat->rx_undersize_pkts +
1167 hwstat->sqe_test_errors +
1168 hwstat->rx_length_mismatch);
1169 nstat->tx_errors = (hwstat->tx_late_cols +
1170 hwstat->tx_excessive_cols +
1171 hwstat->tx_underruns +
1172 hwstat->tx_carrier_errors);
1173 nstat->collisions = (hwstat->tx_single_cols +
1174 hwstat->tx_multiple_cols +
1175 hwstat->tx_excessive_cols);
1176 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1177 hwstat->rx_jabbers +
1178 hwstat->rx_undersize_pkts +
1179 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001180 nstat->rx_over_errors = hwstat->rx_resource_errors +
1181 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001182 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1183 nstat->rx_frame_errors = hwstat->rx_align_errors;
1184 nstat->rx_fifo_errors = hwstat->rx_overruns;
1185 /* XXX: What does "missed" mean? */
1186 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1187 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1188 nstat->tx_fifo_errors = hwstat->tx_underruns;
1189 /* Don't know about heartbeat or window errors... */
1190
1191 return nstat;
1192}
1193
1194static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1195{
1196 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001197 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001198
frederic RODO6c36a702007-07-12 19:07:24 +02001199 if (!phydev)
1200 return -ENODEV;
1201
1202 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001203}
1204
1205static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1206{
1207 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001208 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001209
frederic RODO6c36a702007-07-12 19:07:24 +02001210 if (!phydev)
1211 return -ENODEV;
1212
1213 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214}
1215
frederic RODO6c36a702007-07-12 19:07:24 +02001216static void macb_get_drvinfo(struct net_device *dev,
1217 struct ethtool_drvinfo *info)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001218{
1219 struct macb *bp = netdev_priv(dev);
1220
1221 strcpy(info->driver, bp->pdev->dev.driver->name);
1222 strcpy(info->version, "$Revision: 1.14 $");
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08001223 strcpy(info->bus_info, dev_name(&bp->pdev->dev));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001224}
1225
Joachim Eastwood0005f542012-10-18 11:01:12 +00001226const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001227 .get_settings = macb_get_settings,
1228 .set_settings = macb_set_settings,
1229 .get_drvinfo = macb_get_drvinfo,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001230 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00001231 .get_ts_info = ethtool_op_get_ts_info,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001232};
Joachim Eastwood0005f542012-10-18 11:01:12 +00001233EXPORT_SYMBOL_GPL(macb_ethtool_ops);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001234
Joachim Eastwood0005f542012-10-18 11:01:12 +00001235int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001236{
1237 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001238 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001239
1240 if (!netif_running(dev))
1241 return -EINVAL;
1242
frederic RODO6c36a702007-07-12 19:07:24 +02001243 if (!phydev)
1244 return -ENODEV;
1245
Richard Cochran28b04112010-07-17 08:48:55 +00001246 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001247}
Joachim Eastwood0005f542012-10-18 11:01:12 +00001248EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001249
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001250static const struct net_device_ops macb_netdev_ops = {
1251 .ndo_open = macb_open,
1252 .ndo_stop = macb_close,
1253 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001254 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001255 .ndo_get_stats = macb_get_stats,
1256 .ndo_do_ioctl = macb_ioctl,
1257 .ndo_validate_addr = eth_validate_addr,
1258 .ndo_change_mtu = eth_change_mtu,
1259 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001260#ifdef CONFIG_NET_POLL_CONTROLLER
1261 .ndo_poll_controller = macb_poll_controller,
1262#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001263};
1264
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001265#if defined(CONFIG_OF)
1266static const struct of_device_id macb_dt_ids[] = {
1267 { .compatible = "cdns,at32ap7000-macb" },
1268 { .compatible = "cdns,at91sam9260-macb" },
1269 { .compatible = "cdns,macb" },
1270 { .compatible = "cdns,pc302-gem" },
1271 { .compatible = "cdns,gem" },
1272 { /* sentinel */ }
1273};
1274
1275MODULE_DEVICE_TABLE(of, macb_dt_ids);
1276
1277static int __devinit macb_get_phy_mode_dt(struct platform_device *pdev)
1278{
1279 struct device_node *np = pdev->dev.of_node;
1280
1281 if (np)
1282 return of_get_phy_mode(np);
1283
1284 return -ENODEV;
1285}
1286
1287static int __devinit macb_get_hwaddr_dt(struct macb *bp)
1288{
1289 struct device_node *np = bp->pdev->dev.of_node;
1290 if (np) {
1291 const char *mac = of_get_mac_address(np);
1292 if (mac) {
1293 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
1294 return 0;
1295 }
1296 }
1297
1298 return -ENODEV;
1299}
1300#else
1301static int __devinit macb_get_phy_mode_dt(struct platform_device *pdev)
1302{
1303 return -ENODEV;
1304}
1305static int __devinit macb_get_hwaddr_dt(struct macb *bp)
1306{
1307 return -ENODEV;
1308}
1309#endif
1310
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001311static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001312{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001313 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001314 struct resource *regs;
1315 struct net_device *dev;
1316 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001317 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001318 u32 config;
1319 int err = -ENXIO;
1320
1321 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1322 if (!regs) {
1323 dev_err(&pdev->dev, "no mmio resource defined\n");
1324 goto err_out;
1325 }
1326
1327 err = -ENOMEM;
1328 dev = alloc_etherdev(sizeof(*bp));
Joe Perches41de8d42012-01-29 13:47:52 +00001329 if (!dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001330 goto err_out;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001331
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001332 SET_NETDEV_DEV(dev, &pdev->dev);
1333
1334 /* TODO: Actually, we have some interesting features... */
1335 dev->features |= 0;
1336
1337 bp = netdev_priv(dev);
1338 bp->pdev = pdev;
1339 bp->dev = dev;
1340
1341 spin_lock_init(&bp->lock);
1342
Jamie Iles461845d2011-03-08 20:19:23 +00001343 bp->pclk = clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001344 if (IS_ERR(bp->pclk)) {
1345 dev_err(&pdev->dev, "failed to get macb_clk\n");
1346 goto err_out_free_dev;
1347 }
1348 clk_enable(bp->pclk);
Jamie Iles461845d2011-03-08 20:19:23 +00001349
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001350 bp->hclk = clk_get(&pdev->dev, "hclk");
1351 if (IS_ERR(bp->hclk)) {
1352 dev_err(&pdev->dev, "failed to get hclk\n");
1353 goto err_out_put_pclk;
1354 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001355 clk_enable(bp->hclk);
1356
Joe Perches28f65c112011-06-09 09:13:32 -07001357 bp->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001358 if (!bp->regs) {
1359 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1360 err = -ENOMEM;
1361 goto err_out_disable_clocks;
1362 }
1363
1364 dev->irq = platform_get_irq(pdev, 0);
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00001365 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001366 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001367 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1368 dev->irq, err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001369 goto err_out_iounmap;
1370 }
1371
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001372 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001373 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001374 dev->ethtool_ops = &macb_ethtool_ops;
1375
1376 dev->base_addr = regs->start;
1377
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001378 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001379 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00001380 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001381 macb_writel(bp, NCFGR, config);
1382
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001383 err = macb_get_hwaddr_dt(bp);
1384 if (err < 0)
1385 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02001386
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001387 err = macb_get_phy_mode_dt(pdev);
1388 if (err < 0) {
1389 pdata = pdev->dev.platform_data;
1390 if (pdata && pdata->is_rmii)
1391 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
1392 else
1393 bp->phy_interface = PHY_INTERFACE_MODE_MII;
1394 } else {
1395 bp->phy_interface = err;
1396 }
1397
Patrice Vilchez140b7552012-10-31 06:04:50 +00001398 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
1399 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
1400 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01001401#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001402 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1403 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01001404#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001405 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001406#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001407 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001408#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001409 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01001410#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001411 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001412#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001413
1414 bp->tx_pending = DEF_TX_RING_PENDING;
1415
1416 err = register_netdev(dev);
1417 if (err) {
1418 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1419 goto err_out_free_irq;
1420 }
1421
frederic RODO6c36a702007-07-12 19:07:24 +02001422 if (macb_mii_init(bp) != 0) {
1423 goto err_out_unregister_netdev;
1424 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001425
frederic RODO6c36a702007-07-12 19:07:24 +02001426 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001428 netif_carrier_off(dev);
1429
Jamie Ilesf75ba502011-11-08 10:12:32 +00001430 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1431 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1432 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001433
frederic RODO6c36a702007-07-12 19:07:24 +02001434 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001435 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1436 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02001437
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001438 return 0;
1439
frederic RODO6c36a702007-07-12 19:07:24 +02001440err_out_unregister_netdev:
1441 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001442err_out_free_irq:
1443 free_irq(dev->irq, dev);
1444err_out_iounmap:
1445 iounmap(bp->regs);
1446err_out_disable_clocks:
1447 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001448 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001449 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001450err_out_put_pclk:
1451 clk_put(bp->pclk);
1452err_out_free_dev:
1453 free_netdev(dev);
1454err_out:
1455 platform_set_drvdata(pdev, NULL);
1456 return err;
1457}
1458
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001459static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001460{
1461 struct net_device *dev;
1462 struct macb *bp;
1463
1464 dev = platform_get_drvdata(pdev);
1465
1466 if (dev) {
1467 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09001468 if (bp->phy_dev)
1469 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001470 mdiobus_unregister(bp->mii_bus);
1471 kfree(bp->mii_bus->irq);
1472 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001473 unregister_netdev(dev);
1474 free_irq(dev->irq, dev);
1475 iounmap(bp->regs);
1476 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001477 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001478 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001479 clk_put(bp->pclk);
1480 free_netdev(dev);
1481 platform_set_drvdata(pdev, NULL);
1482 }
1483
1484 return 0;
1485}
1486
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001487#ifdef CONFIG_PM
1488static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1489{
1490 struct net_device *netdev = platform_get_drvdata(pdev);
1491 struct macb *bp = netdev_priv(netdev);
1492
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001493 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001494 netif_device_detach(netdev);
1495
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001496 clk_disable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001497 clk_disable(bp->pclk);
1498
1499 return 0;
1500}
1501
1502static int macb_resume(struct platform_device *pdev)
1503{
1504 struct net_device *netdev = platform_get_drvdata(pdev);
1505 struct macb *bp = netdev_priv(netdev);
1506
1507 clk_enable(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001508 clk_enable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001509
1510 netif_device_attach(netdev);
1511
1512 return 0;
1513}
1514#else
1515#define macb_suspend NULL
1516#define macb_resume NULL
1517#endif
1518
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001519static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001520 .remove = __exit_p(macb_remove),
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001521 .suspend = macb_suspend,
1522 .resume = macb_resume,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001523 .driver = {
1524 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07001525 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001526 .of_match_table = of_match_ptr(macb_dt_ids),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001527 },
1528};
1529
1530static int __init macb_init(void)
1531{
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001532 return platform_driver_probe(&macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001533}
1534
1535static void __exit macb_exit(void)
1536{
1537 platform_driver_unregister(&macb_driver);
1538}
1539
1540module_init(macb_init);
1541module_exit(macb_exit);
1542
1543MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00001544MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001545MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07001546MODULE_ALIAS("platform:macb");