blob: c5c3eb450c1be124b3b7b753b62ddb3122c981e6 [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>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include "macb.h"
28
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010029#define RX_BUFFER_SIZE 128
30#define RX_RING_SIZE 512
31#define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
32
33/* Make the IP header word-aligned (the ethernet header is 14 bytes) */
34#define RX_OFFSET 2
35
36#define TX_RING_SIZE 128
37#define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
38#define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
39
40#define TX_RING_GAP(bp) \
41 (TX_RING_SIZE - (bp)->tx_pending)
42#define TX_BUFFS_AVAIL(bp) \
43 (((bp)->tx_tail <= (bp)->tx_head) ? \
44 (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
45 (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
46#define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
47
48#define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
49
50/* minimum number of free TX descriptors before waking up TX process */
51#define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
52
53#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
54 | MACB_BIT(ISR_ROVR))
55
56static void __macb_set_hwaddr(struct macb *bp)
57{
58 u32 bottom;
59 u16 top;
60
61 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +000062 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010063 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +000064 macb_or_gem_writel(bp, SA1T, top);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010065}
66
67static void __init macb_get_hwaddr(struct macb *bp)
68{
69 u32 bottom;
70 u16 top;
71 u8 addr[6];
72
Jamie Ilesf75ba502011-11-08 10:12:32 +000073 bottom = macb_or_gem_readl(bp, SA1B);
74 top = macb_or_gem_readl(bp, SA1T);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010075
76 addr[0] = bottom & 0xff;
77 addr[1] = (bottom >> 8) & 0xff;
78 addr[2] = (bottom >> 16) & 0xff;
79 addr[3] = (bottom >> 24) & 0xff;
80 addr[4] = top & 0xff;
81 addr[5] = (top >> 8) & 0xff;
82
Sven Schnelled1d57412008-06-09 16:33:57 -070083 if (is_valid_ether_addr(addr)) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010084 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
Sven Schnelled1d57412008-06-09 16:33:57 -070085 } else {
Jamie Ilesc220f8c2011-03-08 20:27:08 +000086 netdev_info(bp->dev, "invalid hw address, using random\n");
Sven Schnelled1d57412008-06-09 16:33:57 -070087 random_ether_addr(bp->dev->dev_addr);
88 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010089}
90
frederic RODO6c36a702007-07-12 19:07:24 +020091static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010092{
frederic RODO6c36a702007-07-12 19:07:24 +020093 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010094 int value;
95
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010096 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
97 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +020098 | MACB_BF(PHYA, mii_id)
99 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100100 | MACB_BF(CODE, MACB_MAN_CODE)));
101
frederic RODO6c36a702007-07-12 19:07:24 +0200102 /* wait for end of transfer */
103 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
104 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100105
106 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100107
108 return value;
109}
110
frederic RODO6c36a702007-07-12 19:07:24 +0200111static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
112 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100113{
frederic RODO6c36a702007-07-12 19:07:24 +0200114 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100115
116 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
117 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200118 | MACB_BF(PHYA, mii_id)
119 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100120 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200121 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100122
frederic RODO6c36a702007-07-12 19:07:24 +0200123 /* wait for end of transfer */
124 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
125 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100126
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100127 return 0;
128}
129
frederic RODO6c36a702007-07-12 19:07:24 +0200130static int macb_mdio_reset(struct mii_bus *bus)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100131{
frederic RODO6c36a702007-07-12 19:07:24 +0200132 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100133}
134
frederic RODO6c36a702007-07-12 19:07:24 +0200135static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100136{
frederic RODO6c36a702007-07-12 19:07:24 +0200137 struct macb *bp = netdev_priv(dev);
138 struct phy_device *phydev = bp->phy_dev;
139 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100140
frederic RODO6c36a702007-07-12 19:07:24 +0200141 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100142
frederic RODO6c36a702007-07-12 19:07:24 +0200143 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100144
frederic RODO6c36a702007-07-12 19:07:24 +0200145 if (phydev->link) {
146 if ((bp->speed != phydev->speed) ||
147 (bp->duplex != phydev->duplex)) {
148 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100149
frederic RODO6c36a702007-07-12 19:07:24 +0200150 reg = macb_readl(bp, NCFGR);
151 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
152
153 if (phydev->duplex)
154 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900155 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200156 reg |= MACB_BIT(SPD);
157
158 macb_writel(bp, NCFGR, reg);
159
160 bp->speed = phydev->speed;
161 bp->duplex = phydev->duplex;
162 status_change = 1;
163 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100164 }
165
frederic RODO6c36a702007-07-12 19:07:24 +0200166 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700167 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200168 bp->speed = 0;
169 bp->duplex = -1;
170 }
171 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100172
frederic RODO6c36a702007-07-12 19:07:24 +0200173 status_change = 1;
174 }
175
176 spin_unlock_irqrestore(&bp->lock, flags);
177
178 if (status_change) {
179 if (phydev->link)
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000180 netdev_info(dev, "link up (%d/%s)\n",
181 phydev->speed,
182 phydev->duplex == DUPLEX_FULL ?
183 "Full" : "Half");
frederic RODO6c36a702007-07-12 19:07:24 +0200184 else
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000185 netdev_info(dev, "link down\n");
frederic RODO6c36a702007-07-12 19:07:24 +0200186 }
187}
188
189/* based on au1000_eth. c*/
190static int macb_mii_probe(struct net_device *dev)
191{
192 struct macb *bp = netdev_priv(dev);
Jiri Pirko7455a762010-02-08 05:12:08 +0000193 struct phy_device *phydev;
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000194 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000195 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200196
Jiri Pirko7455a762010-02-08 05:12:08 +0000197 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200198 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000199 netdev_err(dev, "no PHY found\n");
frederic RODO6c36a702007-07-12 19:07:24 +0200200 return -1;
201 }
202
203 pdata = bp->pdev->dev.platform_data;
204 /* TODO : add pin_irq */
205
206 /* attach the mac to the phy */
Jiri Pirko7455a762010-02-08 05:12:08 +0000207 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
208 pdata && pdata->is_rmii ?
209 PHY_INTERFACE_MODE_RMII :
210 PHY_INTERFACE_MODE_MII);
211 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000212 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000213 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200214 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100215
frederic RODO6c36a702007-07-12 19:07:24 +0200216 /* mask with MAC supported features */
217 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100218
frederic RODO6c36a702007-07-12 19:07:24 +0200219 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100220
frederic RODO6c36a702007-07-12 19:07:24 +0200221 bp->link = 0;
222 bp->speed = 0;
223 bp->duplex = -1;
224 bp->phy_dev = phydev;
225
226 return 0;
227}
228
229static int macb_mii_init(struct macb *bp)
230{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000231 struct macb_platform_data *pdata;
frederic RODO6c36a702007-07-12 19:07:24 +0200232 int err = -ENXIO, i;
233
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200234 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200235 macb_writel(bp, NCR, MACB_BIT(MPE));
236
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700237 bp->mii_bus = mdiobus_alloc();
238 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200239 err = -ENOMEM;
240 goto err_out;
241 }
242
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700243 bp->mii_bus->name = "MACB_mii_bus";
244 bp->mii_bus->read = &macb_mdio_read;
245 bp->mii_bus->write = &macb_mdio_write;
246 bp->mii_bus->reset = &macb_mdio_reset;
247 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
248 bp->mii_bus->priv = bp;
249 bp->mii_bus->parent = &bp->dev->dev;
250 pdata = bp->pdev->dev.platform_data;
251
252 if (pdata)
253 bp->mii_bus->phy_mask = pdata->phy_mask;
254
255 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
256 if (!bp->mii_bus->irq) {
257 err = -ENOMEM;
258 goto err_out_free_mdiobus;
259 }
260
frederic RODO6c36a702007-07-12 19:07:24 +0200261 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700262 bp->mii_bus->irq[i] = PHY_POLL;
frederic RODO6c36a702007-07-12 19:07:24 +0200263
Jamie Iles91523942011-02-28 04:05:25 +0000264 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200265
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700266 if (mdiobus_register(bp->mii_bus))
frederic RODO6c36a702007-07-12 19:07:24 +0200267 goto err_out_free_mdio_irq;
268
269 if (macb_mii_probe(bp->dev) != 0) {
270 goto err_out_unregister_bus;
271 }
272
273 return 0;
274
275err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700276 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200277err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700278 kfree(bp->mii_bus->irq);
279err_out_free_mdiobus:
280 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200281err_out:
282 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100283}
284
285static void macb_update_stats(struct macb *bp)
286{
287 u32 __iomem *reg = bp->regs + MACB_PFR;
288 u32 *p = &bp->hw_stats.rx_pause_frames;
289 u32 *end = &bp->hw_stats.tx_pause_frames + 1;
290
291 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
292
293 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100294 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100295}
296
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100297static void macb_tx(struct macb *bp)
298{
299 unsigned int tail;
300 unsigned int head;
301 u32 status;
302
303 status = macb_readl(bp, TSR);
304 macb_writel(bp, TSR, status);
305
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000306 netdev_dbg(bp->dev, "macb_tx status = %02lx\n", (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100307
Erik Walingee33c582009-04-15 23:32:10 +0000308 if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) {
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100309 int i;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000310 netdev_err(bp->dev, "TX %s, resetting buffers\n",
311 status & MACB_BIT(UND) ?
312 "underrun" : "retry limit exceeded");
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100313
Richard Röjfors39eddb42009-01-18 21:57:35 -0800314 /* Transfer ongoing, disable transmitter, to avoid confusion */
315 if (status & MACB_BIT(TGO))
316 macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE));
317
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100318 head = bp->tx_head;
319
320 /*Mark all the buffer as used to avoid sending a lost buffer*/
321 for (i = 0; i < TX_RING_SIZE; i++)
322 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
323
Tord Anderssond3e61452011-08-03 22:11:47 +0000324 /* Add wrap bit */
325 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
326
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100327 /* free transmit buffer in upper layer*/
328 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
329 struct ring_info *rp = &bp->tx_skb[tail];
330 struct sk_buff *skb = rp->skb;
331
332 BUG_ON(skb == NULL);
333
334 rmb();
335
336 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
337 DMA_TO_DEVICE);
338 rp->skb = NULL;
339 dev_kfree_skb_irq(skb);
340 }
341
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100342 bp->tx_head = bp->tx_tail = 0;
Richard Röjfors39eddb42009-01-18 21:57:35 -0800343
344 /* Enable the transmitter again */
345 if (status & MACB_BIT(TGO))
346 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100347 }
348
349 if (!(status & MACB_BIT(COMP)))
350 /*
351 * This may happen when a buffer becomes complete
352 * between reading the ISR and scanning the
353 * descriptors. Nothing to worry about.
354 */
355 return;
356
357 head = bp->tx_head;
358 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
359 struct ring_info *rp = &bp->tx_skb[tail];
360 struct sk_buff *skb = rp->skb;
361 u32 bufstat;
362
363 BUG_ON(skb == NULL);
364
365 rmb();
366 bufstat = bp->tx_ring[tail].ctrl;
367
368 if (!(bufstat & MACB_BIT(TX_USED)))
369 break;
370
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000371 netdev_dbg(bp->dev, "skb %u (data %p) TX complete\n",
372 tail, skb->data);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100373 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
374 DMA_TO_DEVICE);
375 bp->stats.tx_packets++;
376 bp->stats.tx_bytes += skb->len;
377 rp->skb = NULL;
378 dev_kfree_skb_irq(skb);
379 }
380
381 bp->tx_tail = tail;
382 if (netif_queue_stopped(bp->dev) &&
383 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
384 netif_wake_queue(bp->dev);
385}
386
387static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
388 unsigned int last_frag)
389{
390 unsigned int len;
391 unsigned int frag;
392 unsigned int offset = 0;
393 struct sk_buff *skb;
394
395 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
396
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000397 netdev_dbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
398 first_frag, last_frag, len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100399
400 skb = dev_alloc_skb(len + RX_OFFSET);
401 if (!skb) {
402 bp->stats.rx_dropped++;
403 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
404 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
405 if (frag == last_frag)
406 break;
407 }
408 wmb();
409 return 1;
410 }
411
412 skb_reserve(skb, RX_OFFSET);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700413 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100414 skb_put(skb, len);
415
416 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
417 unsigned int frag_len = RX_BUFFER_SIZE;
418
419 if (offset + frag_len > len) {
420 BUG_ON(frag != last_frag);
421 frag_len = len - offset;
422 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300423 skb_copy_to_linear_data_offset(skb, offset,
424 (bp->rx_buffers +
425 (RX_BUFFER_SIZE * frag)),
426 frag_len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100427 offset += RX_BUFFER_SIZE;
428 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
429 wmb();
430
431 if (frag == last_frag)
432 break;
433 }
434
435 skb->protocol = eth_type_trans(skb, bp->dev);
436
437 bp->stats.rx_packets++;
438 bp->stats.rx_bytes += len;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000439 netdev_dbg(bp->dev, "received skb of length %u, csum: %08x\n",
440 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100441 netif_receive_skb(skb);
442
443 return 0;
444}
445
446/* Mark DMA descriptors from begin up to and not including end as unused */
447static void discard_partial_frame(struct macb *bp, unsigned int begin,
448 unsigned int end)
449{
450 unsigned int frag;
451
452 for (frag = begin; frag != end; frag = NEXT_RX(frag))
453 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
454 wmb();
455
456 /*
457 * When this happens, the hardware stats registers for
458 * whatever caused this is updated, so we don't have to record
459 * anything.
460 */
461}
462
463static int macb_rx(struct macb *bp, int budget)
464{
465 int received = 0;
466 unsigned int tail = bp->rx_tail;
467 int first_frag = -1;
468
469 for (; budget > 0; tail = NEXT_RX(tail)) {
470 u32 addr, ctrl;
471
472 rmb();
473 addr = bp->rx_ring[tail].addr;
474 ctrl = bp->rx_ring[tail].ctrl;
475
476 if (!(addr & MACB_BIT(RX_USED)))
477 break;
478
479 if (ctrl & MACB_BIT(RX_SOF)) {
480 if (first_frag != -1)
481 discard_partial_frame(bp, first_frag, tail);
482 first_frag = tail;
483 }
484
485 if (ctrl & MACB_BIT(RX_EOF)) {
486 int dropped;
487 BUG_ON(first_frag == -1);
488
489 dropped = macb_rx_frame(bp, first_frag, tail);
490 first_frag = -1;
491 if (!dropped) {
492 received++;
493 budget--;
494 }
495 }
496 }
497
498 if (first_frag != -1)
499 bp->rx_tail = first_frag;
500 else
501 bp->rx_tail = tail;
502
503 return received;
504}
505
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700506static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100507{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700508 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700509 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100510 u32 status;
511
512 status = macb_readl(bp, RSR);
513 macb_writel(bp, RSR, status);
514
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700515 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100516
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000517 netdev_dbg(bp->dev, "poll: status = %08lx, budget = %d\n",
518 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100519
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700520 work_done = macb_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000521 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800522 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100523
Joshua Hokeb3363692010-10-25 01:44:22 +0000524 /*
525 * We've done what we can to clean the buffers. Make sure we
526 * get notified when new packets arrive.
527 */
528 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
529 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100530
531 /* TODO: Handle errors */
532
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700533 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100534}
535
536static irqreturn_t macb_interrupt(int irq, void *dev_id)
537{
538 struct net_device *dev = dev_id;
539 struct macb *bp = netdev_priv(dev);
540 u32 status;
541
542 status = macb_readl(bp, ISR);
543
544 if (unlikely(!status))
545 return IRQ_NONE;
546
547 spin_lock(&bp->lock);
548
549 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100550 /* close possible race with dev_close */
551 if (unlikely(!netif_running(dev))) {
552 macb_writel(bp, IDR, ~0UL);
553 break;
554 }
555
556 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000557 /*
558 * There's no point taking any more interrupts
559 * until we have processed the buffers. The
560 * scheduling call may fail if the poll routine
561 * is already scheduled, so disable interrupts
562 * now.
563 */
564 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
565
Ben Hutchings288379f2009-01-19 16:43:59 -0800566 if (napi_schedule_prep(&bp->napi)) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000567 netdev_dbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800568 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100569 }
570 }
571
Erik Walingee33c582009-04-15 23:32:10 +0000572 if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
573 MACB_BIT(ISR_RLE)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100574 macb_tx(bp);
575
576 /*
577 * Link change detection isn't possible with RMII, so we'll
578 * add that if/when we get our hands on a full-blown MII PHY.
579 */
580
Alexander Steinb19f7f72011-04-13 05:03:24 +0000581 if (status & MACB_BIT(ISR_ROVR)) {
582 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000583 if (macb_is_gem(bp))
584 bp->hw_stats.gem.rx_overruns++;
585 else
586 bp->hw_stats.macb.rx_overruns++;
Alexander Steinb19f7f72011-04-13 05:03:24 +0000587 }
588
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100589 if (status & MACB_BIT(HRESP)) {
590 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000591 * TODO: Reset the hardware, and maybe move the
592 * netdev_err to a lower-priority context as well
593 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100594 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000595 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100596 }
597
598 status = macb_readl(bp, ISR);
599 }
600
601 spin_unlock(&bp->lock);
602
603 return IRQ_HANDLED;
604}
605
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700606#ifdef CONFIG_NET_POLL_CONTROLLER
607/*
608 * Polling receive - used by netconsole and other diagnostic tools
609 * to allow network i/o with interrupts disabled.
610 */
611static void macb_poll_controller(struct net_device *dev)
612{
613 unsigned long flags;
614
615 local_irq_save(flags);
616 macb_interrupt(dev->irq, dev);
617 local_irq_restore(flags);
618}
619#endif
620
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100621static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
622{
623 struct macb *bp = netdev_priv(dev);
624 dma_addr_t mapping;
625 unsigned int len, entry;
626 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -0700627 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100628
629#ifdef DEBUG
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000630 netdev_dbg(bp->dev,
631 "start_xmit: len %u head %p data %p tail %p end %p\n",
632 skb->len, skb->head, skb->data,
633 skb_tail_pointer(skb), skb_end_pointer(skb));
634 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
635 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100636#endif
637
638 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -0700639 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100640
641 /* This is a hard error, log it. */
642 if (TX_BUFFS_AVAIL(bp) < 1) {
643 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -0700644 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000645 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
646 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
647 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +0000648 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100649 }
650
651 entry = bp->tx_head;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000652 netdev_dbg(bp->dev, "Allocated ring entry %u\n", entry);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100653 mapping = dma_map_single(&bp->pdev->dev, skb->data,
654 len, DMA_TO_DEVICE);
655 bp->tx_skb[entry].skb = skb;
656 bp->tx_skb[entry].mapping = mapping;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000657 netdev_dbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
658 skb->data, (unsigned long)mapping);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100659
660 ctrl = MACB_BF(TX_FRMLEN, len);
661 ctrl |= MACB_BIT(TX_LAST);
662 if (entry == (TX_RING_SIZE - 1))
663 ctrl |= MACB_BIT(TX_WRAP);
664
665 bp->tx_ring[entry].addr = mapping;
666 bp->tx_ring[entry].ctrl = ctrl;
667 wmb();
668
669 entry = NEXT_TX(entry);
670 bp->tx_head = entry;
671
Richard Cochrane0720922011-06-19 21:51:28 +0000672 skb_tx_timestamp(skb);
673
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100674 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
675
676 if (TX_BUFFS_AVAIL(bp) < 1)
677 netif_stop_queue(dev);
678
Dongdong Deng48719532009-08-23 19:49:07 -0700679 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100680
Patrick McHardy6ed10652009-06-23 06:03:08 +0000681 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100682}
683
684static void macb_free_consistent(struct macb *bp)
685{
686 if (bp->tx_skb) {
687 kfree(bp->tx_skb);
688 bp->tx_skb = NULL;
689 }
690 if (bp->rx_ring) {
691 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
692 bp->rx_ring, bp->rx_ring_dma);
693 bp->rx_ring = NULL;
694 }
695 if (bp->tx_ring) {
696 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
697 bp->tx_ring, bp->tx_ring_dma);
698 bp->tx_ring = NULL;
699 }
700 if (bp->rx_buffers) {
701 dma_free_coherent(&bp->pdev->dev,
702 RX_RING_SIZE * RX_BUFFER_SIZE,
703 bp->rx_buffers, bp->rx_buffers_dma);
704 bp->rx_buffers = NULL;
705 }
706}
707
708static int macb_alloc_consistent(struct macb *bp)
709{
710 int size;
711
712 size = TX_RING_SIZE * sizeof(struct ring_info);
713 bp->tx_skb = kmalloc(size, GFP_KERNEL);
714 if (!bp->tx_skb)
715 goto out_err;
716
717 size = RX_RING_BYTES;
718 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
719 &bp->rx_ring_dma, GFP_KERNEL);
720 if (!bp->rx_ring)
721 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000722 netdev_dbg(bp->dev,
723 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
724 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100725
726 size = TX_RING_BYTES;
727 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
728 &bp->tx_ring_dma, GFP_KERNEL);
729 if (!bp->tx_ring)
730 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000731 netdev_dbg(bp->dev,
732 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
733 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100734
735 size = RX_RING_SIZE * RX_BUFFER_SIZE;
736 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
737 &bp->rx_buffers_dma, GFP_KERNEL);
738 if (!bp->rx_buffers)
739 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000740 netdev_dbg(bp->dev,
741 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
742 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100743
744 return 0;
745
746out_err:
747 macb_free_consistent(bp);
748 return -ENOMEM;
749}
750
751static void macb_init_rings(struct macb *bp)
752{
753 int i;
754 dma_addr_t addr;
755
756 addr = bp->rx_buffers_dma;
757 for (i = 0; i < RX_RING_SIZE; i++) {
758 bp->rx_ring[i].addr = addr;
759 bp->rx_ring[i].ctrl = 0;
760 addr += RX_BUFFER_SIZE;
761 }
762 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
763
764 for (i = 0; i < TX_RING_SIZE; i++) {
765 bp->tx_ring[i].addr = 0;
766 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
767 }
768 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
769
770 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
771}
772
773static void macb_reset_hw(struct macb *bp)
774{
775 /* Make sure we have the write buffer for ourselves */
776 wmb();
777
778 /*
779 * Disable RX and TX (XXX: Should we halt the transmission
780 * more gracefully?)
781 */
782 macb_writel(bp, NCR, 0);
783
784 /* Clear the stats registers (XXX: Update stats first?) */
785 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
786
787 /* Clear all status flags */
788 macb_writel(bp, TSR, ~0UL);
789 macb_writel(bp, RSR, ~0UL);
790
791 /* Disable all interrupts */
792 macb_writel(bp, IDR, ~0UL);
793 macb_readl(bp, ISR);
794}
795
Jamie Iles70c9f3d2011-03-09 16:22:54 +0000796static u32 gem_mdc_clk_div(struct macb *bp)
797{
798 u32 config;
799 unsigned long pclk_hz = clk_get_rate(bp->pclk);
800
801 if (pclk_hz <= 20000000)
802 config = GEM_BF(CLK, GEM_CLK_DIV8);
803 else if (pclk_hz <= 40000000)
804 config = GEM_BF(CLK, GEM_CLK_DIV16);
805 else if (pclk_hz <= 80000000)
806 config = GEM_BF(CLK, GEM_CLK_DIV32);
807 else if (pclk_hz <= 120000000)
808 config = GEM_BF(CLK, GEM_CLK_DIV48);
809 else if (pclk_hz <= 160000000)
810 config = GEM_BF(CLK, GEM_CLK_DIV64);
811 else
812 config = GEM_BF(CLK, GEM_CLK_DIV96);
813
814 return config;
815}
816
817static u32 macb_mdc_clk_div(struct macb *bp)
818{
819 u32 config;
820 unsigned long pclk_hz;
821
822 if (macb_is_gem(bp))
823 return gem_mdc_clk_div(bp);
824
825 pclk_hz = clk_get_rate(bp->pclk);
826 if (pclk_hz <= 20000000)
827 config = MACB_BF(CLK, MACB_CLK_DIV8);
828 else if (pclk_hz <= 40000000)
829 config = MACB_BF(CLK, MACB_CLK_DIV16);
830 else if (pclk_hz <= 80000000)
831 config = MACB_BF(CLK, MACB_CLK_DIV32);
832 else
833 config = MACB_BF(CLK, MACB_CLK_DIV64);
834
835 return config;
836}
837
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100838static void macb_init_hw(struct macb *bp)
839{
840 u32 config;
841
842 macb_reset_hw(bp);
843 __macb_set_hwaddr(bp);
844
Jamie Iles70c9f3d2011-03-09 16:22:54 +0000845 config = macb_mdc_clk_div(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100846 config |= MACB_BIT(PAE); /* PAuse Enable */
847 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -0700848 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100849 if (bp->dev->flags & IFF_PROMISC)
850 config |= MACB_BIT(CAF); /* Copy All Frames */
851 if (!(bp->dev->flags & IFF_BROADCAST))
852 config |= MACB_BIT(NBC); /* No BroadCast */
853 macb_writel(bp, NCFGR, config);
854
855 /* Initialize TX and RX buffers */
856 macb_writel(bp, RBQP, bp->rx_ring_dma);
857 macb_writel(bp, TBQP, bp->tx_ring_dma);
858
859 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +0200860 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100861
862 /* Enable interrupts */
863 macb_writel(bp, IER, (MACB_BIT(RCOMP)
864 | MACB_BIT(RXUBR)
865 | MACB_BIT(ISR_TUND)
866 | MACB_BIT(ISR_RLE)
867 | MACB_BIT(TXERR)
868 | MACB_BIT(TCOMP)
869 | MACB_BIT(ISR_ROVR)
870 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100871
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100872}
873
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200874/*
875 * The hash address register is 64 bits long and takes up two
876 * locations in the memory map. The least significant bits are stored
877 * in EMAC_HSL and the most significant bits in EMAC_HSH.
878 *
879 * The unicast hash enable and the multicast hash enable bits in the
880 * network configuration register enable the reception of hash matched
881 * frames. The destination address is reduced to a 6 bit index into
882 * the 64 bit hash register using the following hash function. The
883 * hash function is an exclusive or of every sixth bit of the
884 * destination address.
885 *
886 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
887 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
888 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
889 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
890 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
891 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
892 *
893 * da[0] represents the least significant bit of the first byte
894 * received, that is, the multicast/unicast indicator, and da[47]
895 * represents the most significant bit of the last byte received. If
896 * the hash index, hi[n], points to a bit that is set in the hash
897 * register then the frame will be matched according to whether the
898 * frame is multicast or unicast. A multicast match will be signalled
899 * if the multicast hash enable bit is set, da[0] is 1 and the hash
900 * index points to a bit set in the hash register. A unicast match
901 * will be signalled if the unicast hash enable bit is set, da[0] is 0
902 * and the hash index points to a bit set in the hash register. To
903 * receive all multicast frames, the hash register should be set with
904 * all ones and the multicast hash enable bit should be set in the
905 * network configuration register.
906 */
907
908static inline int hash_bit_value(int bitnr, __u8 *addr)
909{
910 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
911 return 1;
912 return 0;
913}
914
915/*
916 * Return the hash index value for the specified address.
917 */
918static int hash_get_index(__u8 *addr)
919{
920 int i, j, bitval;
921 int hash_index = 0;
922
923 for (j = 0; j < 6; j++) {
924 for (i = 0, bitval = 0; i < 8; i++)
925 bitval ^= hash_bit_value(i*6 + j, addr);
926
927 hash_index |= (bitval << j);
928 }
929
930 return hash_index;
931}
932
933/*
934 * Add multicast addresses to the internal multicast-hash table.
935 */
936static void macb_sethashtable(struct net_device *dev)
937{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000938 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200939 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000940 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200941 struct macb *bp = netdev_priv(dev);
942
943 mc_filter[0] = mc_filter[1] = 0;
944
Jiri Pirko22bedad32010-04-01 21:22:57 +0000945 netdev_for_each_mc_addr(ha, dev) {
946 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200947 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
948 }
949
Jamie Ilesf75ba502011-11-08 10:12:32 +0000950 macb_or_gem_writel(bp, HRB, mc_filter[0]);
951 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200952}
953
954/*
955 * Enable/Disable promiscuous and multicast modes.
956 */
957static void macb_set_rx_mode(struct net_device *dev)
958{
959 unsigned long cfg;
960 struct macb *bp = netdev_priv(dev);
961
962 cfg = macb_readl(bp, NCFGR);
963
964 if (dev->flags & IFF_PROMISC)
965 /* Enable promiscuous mode */
966 cfg |= MACB_BIT(CAF);
967 else if (dev->flags & (~IFF_PROMISC))
968 /* Disable promiscuous mode */
969 cfg &= ~MACB_BIT(CAF);
970
971 if (dev->flags & IFF_ALLMULTI) {
972 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000973 macb_or_gem_writel(bp, HRB, -1);
974 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200975 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000976 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200977 /* Enable specific multicasts */
978 macb_sethashtable(dev);
979 cfg |= MACB_BIT(NCFGR_MTI);
980 } else if (dev->flags & (~IFF_ALLMULTI)) {
981 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000982 macb_or_gem_writel(bp, HRB, 0);
983 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200984 cfg &= ~MACB_BIT(NCFGR_MTI);
985 }
986
987 macb_writel(bp, NCFGR, cfg);
988}
989
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100990static int macb_open(struct net_device *dev)
991{
992 struct macb *bp = netdev_priv(dev);
993 int err;
994
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000995 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100996
frederic RODO6c36a702007-07-12 19:07:24 +0200997 /* if the phy is not yet register, retry later*/
998 if (!bp->phy_dev)
999 return -EAGAIN;
1000
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001001 if (!is_valid_ether_addr(dev->dev_addr))
1002 return -EADDRNOTAVAIL;
1003
1004 err = macb_alloc_consistent(bp);
1005 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001006 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1007 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001008 return err;
1009 }
1010
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001011 napi_enable(&bp->napi);
1012
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001013 macb_init_rings(bp);
1014 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001015
frederic RODO6c36a702007-07-12 19:07:24 +02001016 /* schedule a link state check */
1017 phy_start(bp->phy_dev);
1018
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001019 netif_start_queue(dev);
1020
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001021 return 0;
1022}
1023
1024static int macb_close(struct net_device *dev)
1025{
1026 struct macb *bp = netdev_priv(dev);
1027 unsigned long flags;
1028
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001029 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001030 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001031
frederic RODO6c36a702007-07-12 19:07:24 +02001032 if (bp->phy_dev)
1033 phy_stop(bp->phy_dev);
1034
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001035 spin_lock_irqsave(&bp->lock, flags);
1036 macb_reset_hw(bp);
1037 netif_carrier_off(dev);
1038 spin_unlock_irqrestore(&bp->lock, flags);
1039
1040 macb_free_consistent(bp);
1041
1042 return 0;
1043}
1044
1045static struct net_device_stats *macb_get_stats(struct net_device *dev)
1046{
1047 struct macb *bp = netdev_priv(dev);
1048 struct net_device_stats *nstat = &bp->stats;
1049 struct macb_stats *hwstat = &bp->hw_stats;
1050
frederic RODO6c36a702007-07-12 19:07:24 +02001051 /* read stats from hardware */
1052 macb_update_stats(bp);
1053
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001054 /* Convert HW stats into netdevice stats */
1055 nstat->rx_errors = (hwstat->rx_fcs_errors +
1056 hwstat->rx_align_errors +
1057 hwstat->rx_resource_errors +
1058 hwstat->rx_overruns +
1059 hwstat->rx_oversize_pkts +
1060 hwstat->rx_jabbers +
1061 hwstat->rx_undersize_pkts +
1062 hwstat->sqe_test_errors +
1063 hwstat->rx_length_mismatch);
1064 nstat->tx_errors = (hwstat->tx_late_cols +
1065 hwstat->tx_excessive_cols +
1066 hwstat->tx_underruns +
1067 hwstat->tx_carrier_errors);
1068 nstat->collisions = (hwstat->tx_single_cols +
1069 hwstat->tx_multiple_cols +
1070 hwstat->tx_excessive_cols);
1071 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1072 hwstat->rx_jabbers +
1073 hwstat->rx_undersize_pkts +
1074 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001075 nstat->rx_over_errors = hwstat->rx_resource_errors +
1076 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001077 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1078 nstat->rx_frame_errors = hwstat->rx_align_errors;
1079 nstat->rx_fifo_errors = hwstat->rx_overruns;
1080 /* XXX: What does "missed" mean? */
1081 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1082 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1083 nstat->tx_fifo_errors = hwstat->tx_underruns;
1084 /* Don't know about heartbeat or window errors... */
1085
1086 return nstat;
1087}
1088
1089static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1090{
1091 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001092 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001093
frederic RODO6c36a702007-07-12 19:07:24 +02001094 if (!phydev)
1095 return -ENODEV;
1096
1097 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001098}
1099
1100static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1101{
1102 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001103 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001104
frederic RODO6c36a702007-07-12 19:07:24 +02001105 if (!phydev)
1106 return -ENODEV;
1107
1108 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001109}
1110
frederic RODO6c36a702007-07-12 19:07:24 +02001111static void macb_get_drvinfo(struct net_device *dev,
1112 struct ethtool_drvinfo *info)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001113{
1114 struct macb *bp = netdev_priv(dev);
1115
1116 strcpy(info->driver, bp->pdev->dev.driver->name);
1117 strcpy(info->version, "$Revision: 1.14 $");
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08001118 strcpy(info->bus_info, dev_name(&bp->pdev->dev));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001119}
1120
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001121static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001122 .get_settings = macb_get_settings,
1123 .set_settings = macb_set_settings,
1124 .get_drvinfo = macb_get_drvinfo,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001125 .get_link = ethtool_op_get_link,
1126};
1127
1128static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1129{
1130 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001131 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001132
1133 if (!netif_running(dev))
1134 return -EINVAL;
1135
frederic RODO6c36a702007-07-12 19:07:24 +02001136 if (!phydev)
1137 return -ENODEV;
1138
Richard Cochran28b04112010-07-17 08:48:55 +00001139 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001140}
1141
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001142static const struct net_device_ops macb_netdev_ops = {
1143 .ndo_open = macb_open,
1144 .ndo_stop = macb_close,
1145 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001146 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001147 .ndo_get_stats = macb_get_stats,
1148 .ndo_do_ioctl = macb_ioctl,
1149 .ndo_validate_addr = eth_validate_addr,
1150 .ndo_change_mtu = eth_change_mtu,
1151 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001152#ifdef CONFIG_NET_POLL_CONTROLLER
1153 .ndo_poll_controller = macb_poll_controller,
1154#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001155};
1156
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001157static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001158{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001159 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001160 struct resource *regs;
1161 struct net_device *dev;
1162 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001163 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001164 u32 config;
1165 int err = -ENXIO;
1166
1167 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1168 if (!regs) {
1169 dev_err(&pdev->dev, "no mmio resource defined\n");
1170 goto err_out;
1171 }
1172
1173 err = -ENOMEM;
1174 dev = alloc_etherdev(sizeof(*bp));
1175 if (!dev) {
1176 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1177 goto err_out;
1178 }
1179
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001180 SET_NETDEV_DEV(dev, &pdev->dev);
1181
1182 /* TODO: Actually, we have some interesting features... */
1183 dev->features |= 0;
1184
1185 bp = netdev_priv(dev);
1186 bp->pdev = pdev;
1187 bp->dev = dev;
1188
1189 spin_lock_init(&bp->lock);
1190
Jamie Iles461845d2011-03-08 20:19:23 +00001191 bp->pclk = clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001192 if (IS_ERR(bp->pclk)) {
1193 dev_err(&pdev->dev, "failed to get macb_clk\n");
1194 goto err_out_free_dev;
1195 }
1196 clk_enable(bp->pclk);
Jamie Iles461845d2011-03-08 20:19:23 +00001197
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001198 bp->hclk = clk_get(&pdev->dev, "hclk");
1199 if (IS_ERR(bp->hclk)) {
1200 dev_err(&pdev->dev, "failed to get hclk\n");
1201 goto err_out_put_pclk;
1202 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001203 clk_enable(bp->hclk);
1204
Joe Perches28f65c112011-06-09 09:13:32 -07001205 bp->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001206 if (!bp->regs) {
1207 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1208 err = -ENOMEM;
1209 goto err_out_disable_clocks;
1210 }
1211
1212 dev->irq = platform_get_irq(pdev, 0);
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00001213 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001215 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1216 dev->irq, err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001217 goto err_out_iounmap;
1218 }
1219
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001220 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001221 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001222 dev->ethtool_ops = &macb_ethtool_ops;
1223
1224 dev->base_addr = regs->start;
1225
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001226 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001227 config = macb_mdc_clk_div(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001228 macb_writel(bp, NCFGR, config);
1229
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001230 macb_get_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001231 pdata = pdev->dev.platform_data;
frederic RODO6c36a702007-07-12 19:07:24 +02001232
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001233 if (pdata && pdata->is_rmii)
Andrew Victor0cc86742007-02-07 16:40:44 +01001234#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001235 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1236 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01001237#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001238 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001239#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001240 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001241#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001242 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01001243#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001244 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001245#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001246
1247 bp->tx_pending = DEF_TX_RING_PENDING;
1248
1249 err = register_netdev(dev);
1250 if (err) {
1251 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1252 goto err_out_free_irq;
1253 }
1254
frederic RODO6c36a702007-07-12 19:07:24 +02001255 if (macb_mii_init(bp) != 0) {
1256 goto err_out_unregister_netdev;
1257 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001258
frederic RODO6c36a702007-07-12 19:07:24 +02001259 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001260
Jamie Ilesf75ba502011-11-08 10:12:32 +00001261 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1262 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1263 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001264
frederic RODO6c36a702007-07-12 19:07:24 +02001265 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001266 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1267 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02001268
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001269 return 0;
1270
frederic RODO6c36a702007-07-12 19:07:24 +02001271err_out_unregister_netdev:
1272 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001273err_out_free_irq:
1274 free_irq(dev->irq, dev);
1275err_out_iounmap:
1276 iounmap(bp->regs);
1277err_out_disable_clocks:
1278 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001279 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001280 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001281err_out_put_pclk:
1282 clk_put(bp->pclk);
1283err_out_free_dev:
1284 free_netdev(dev);
1285err_out:
1286 platform_set_drvdata(pdev, NULL);
1287 return err;
1288}
1289
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001290static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001291{
1292 struct net_device *dev;
1293 struct macb *bp;
1294
1295 dev = platform_get_drvdata(pdev);
1296
1297 if (dev) {
1298 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09001299 if (bp->phy_dev)
1300 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001301 mdiobus_unregister(bp->mii_bus);
1302 kfree(bp->mii_bus->irq);
1303 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001304 unregister_netdev(dev);
1305 free_irq(dev->irq, dev);
1306 iounmap(bp->regs);
1307 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001308 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001309 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001310 clk_put(bp->pclk);
1311 free_netdev(dev);
1312 platform_set_drvdata(pdev, NULL);
1313 }
1314
1315 return 0;
1316}
1317
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001318#ifdef CONFIG_PM
1319static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1320{
1321 struct net_device *netdev = platform_get_drvdata(pdev);
1322 struct macb *bp = netdev_priv(netdev);
1323
1324 netif_device_detach(netdev);
1325
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001326 clk_disable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001327 clk_disable(bp->pclk);
1328
1329 return 0;
1330}
1331
1332static int macb_resume(struct platform_device *pdev)
1333{
1334 struct net_device *netdev = platform_get_drvdata(pdev);
1335 struct macb *bp = netdev_priv(netdev);
1336
1337 clk_enable(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001338 clk_enable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001339
1340 netif_device_attach(netdev);
1341
1342 return 0;
1343}
1344#else
1345#define macb_suspend NULL
1346#define macb_resume NULL
1347#endif
1348
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001349static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001350 .remove = __exit_p(macb_remove),
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001351 .suspend = macb_suspend,
1352 .resume = macb_resume,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001353 .driver = {
1354 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07001355 .owner = THIS_MODULE,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001356 },
1357};
1358
1359static int __init macb_init(void)
1360{
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001361 return platform_driver_probe(&macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001362}
1363
1364static void __exit macb_exit(void)
1365{
1366 platform_driver_unregister(&macb_driver);
1367}
1368
1369module_init(macb_init);
1370module_exit(macb_exit);
1371
1372MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00001373MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001374MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07001375MODULE_ALIAS("platform:macb");