blob: f3d5c65d99cf20ae57f14fc9b7dd2007d81bba85 [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");
Sven Schnelled1d57412008-06-09 16:33:57 -070090 random_ether_addr(bp->dev->dev_addr);
91 }
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));
155
156 if (phydev->duplex)
157 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900158 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200159 reg |= MACB_BIT(SPD);
160
161 macb_writel(bp, NCFGR, reg);
162
163 bp->speed = phydev->speed;
164 bp->duplex = phydev->duplex;
165 status_change = 1;
166 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100167 }
168
frederic RODO6c36a702007-07-12 19:07:24 +0200169 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700170 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200171 bp->speed = 0;
172 bp->duplex = -1;
173 }
174 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100175
frederic RODO6c36a702007-07-12 19:07:24 +0200176 status_change = 1;
177 }
178
179 spin_unlock_irqrestore(&bp->lock, flags);
180
181 if (status_change) {
182 if (phydev->link)
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000183 netdev_info(dev, "link up (%d/%s)\n",
184 phydev->speed,
185 phydev->duplex == DUPLEX_FULL ?
186 "Full" : "Half");
frederic RODO6c36a702007-07-12 19:07:24 +0200187 else
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000188 netdev_info(dev, "link down\n");
frederic RODO6c36a702007-07-12 19:07:24 +0200189 }
190}
191
192/* based on au1000_eth. c*/
193static int macb_mii_probe(struct net_device *dev)
194{
195 struct macb *bp = netdev_priv(dev);
Jiri Pirko7455a762010-02-08 05:12:08 +0000196 struct phy_device *phydev;
Jiri Pirko7455a762010-02-08 05:12:08 +0000197 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200198
Jiri Pirko7455a762010-02-08 05:12:08 +0000199 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200200 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000201 netdev_err(dev, "no PHY found\n");
frederic RODO6c36a702007-07-12 19:07:24 +0200202 return -1;
203 }
204
frederic RODO6c36a702007-07-12 19:07:24 +0200205 /* TODO : add pin_irq */
206
207 /* attach the mac to the phy */
Jiri Pirko7455a762010-02-08 05:12:08 +0000208 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100209 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000210 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000211 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000212 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200213 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100214
frederic RODO6c36a702007-07-12 19:07:24 +0200215 /* mask with MAC supported features */
216 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100217
frederic RODO6c36a702007-07-12 19:07:24 +0200218 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100219
frederic RODO6c36a702007-07-12 19:07:24 +0200220 bp->link = 0;
221 bp->speed = 0;
222 bp->duplex = -1;
223 bp->phy_dev = phydev;
224
225 return 0;
226}
227
228static int macb_mii_init(struct macb *bp)
229{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000230 struct macb_platform_data *pdata;
frederic RODO6c36a702007-07-12 19:07:24 +0200231 int err = -ENXIO, i;
232
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200233 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200234 macb_writel(bp, NCR, MACB_BIT(MPE));
235
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700236 bp->mii_bus = mdiobus_alloc();
237 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200238 err = -ENOMEM;
239 goto err_out;
240 }
241
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700242 bp->mii_bus->name = "MACB_mii_bus";
243 bp->mii_bus->read = &macb_mdio_read;
244 bp->mii_bus->write = &macb_mdio_write;
245 bp->mii_bus->reset = &macb_mdio_reset;
246 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
247 bp->mii_bus->priv = bp;
248 bp->mii_bus->parent = &bp->dev->dev;
249 pdata = bp->pdev->dev.platform_data;
250
251 if (pdata)
252 bp->mii_bus->phy_mask = pdata->phy_mask;
253
254 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
255 if (!bp->mii_bus->irq) {
256 err = -ENOMEM;
257 goto err_out_free_mdiobus;
258 }
259
frederic RODO6c36a702007-07-12 19:07:24 +0200260 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700261 bp->mii_bus->irq[i] = PHY_POLL;
frederic RODO6c36a702007-07-12 19:07:24 +0200262
Jamie Iles91523942011-02-28 04:05:25 +0000263 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200264
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700265 if (mdiobus_register(bp->mii_bus))
frederic RODO6c36a702007-07-12 19:07:24 +0200266 goto err_out_free_mdio_irq;
267
268 if (macb_mii_probe(bp->dev) != 0) {
269 goto err_out_unregister_bus;
270 }
271
272 return 0;
273
274err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700275 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200276err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700277 kfree(bp->mii_bus->irq);
278err_out_free_mdiobus:
279 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200280err_out:
281 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100282}
283
284static void macb_update_stats(struct macb *bp)
285{
286 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000287 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
288 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100289
290 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
291
292 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100293 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100294}
295
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100296static void macb_tx(struct macb *bp)
297{
298 unsigned int tail;
299 unsigned int head;
300 u32 status;
301
302 status = macb_readl(bp, TSR);
303 macb_writel(bp, TSR, status);
304
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000305 netdev_dbg(bp->dev, "macb_tx status = %02lx\n", (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100306
Erik Walingee33c582009-04-15 23:32:10 +0000307 if (status & (MACB_BIT(UND) | MACB_BIT(TSR_RLE))) {
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100308 int i;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000309 netdev_err(bp->dev, "TX %s, resetting buffers\n",
310 status & MACB_BIT(UND) ?
311 "underrun" : "retry limit exceeded");
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100312
Richard Röjfors39eddb42009-01-18 21:57:35 -0800313 /* Transfer ongoing, disable transmitter, to avoid confusion */
314 if (status & MACB_BIT(TGO))
315 macb_writel(bp, NCR, macb_readl(bp, NCR) & ~MACB_BIT(TE));
316
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100317 head = bp->tx_head;
318
319 /*Mark all the buffer as used to avoid sending a lost buffer*/
320 for (i = 0; i < TX_RING_SIZE; i++)
321 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
322
Tord Anderssond3e61452011-08-03 22:11:47 +0000323 /* Add wrap bit */
324 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
325
Gregory CLEMENTbdcba1512007-12-19 18:23:44 +0100326 /* free transmit buffer in upper layer*/
327 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
328 struct ring_info *rp = &bp->tx_skb[tail];
329 struct sk_buff *skb = rp->skb;
330
331 BUG_ON(skb == NULL);
332
333 rmb();
334
335 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
336 DMA_TO_DEVICE);
337 rp->skb = NULL;
338 dev_kfree_skb_irq(skb);
339 }
340
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100341 bp->tx_head = bp->tx_tail = 0;
Richard Röjfors39eddb42009-01-18 21:57:35 -0800342
343 /* Enable the transmitter again */
344 if (status & MACB_BIT(TGO))
345 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100346 }
347
348 if (!(status & MACB_BIT(COMP)))
349 /*
350 * This may happen when a buffer becomes complete
351 * between reading the ISR and scanning the
352 * descriptors. Nothing to worry about.
353 */
354 return;
355
356 head = bp->tx_head;
357 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
358 struct ring_info *rp = &bp->tx_skb[tail];
359 struct sk_buff *skb = rp->skb;
360 u32 bufstat;
361
362 BUG_ON(skb == NULL);
363
364 rmb();
365 bufstat = bp->tx_ring[tail].ctrl;
366
367 if (!(bufstat & MACB_BIT(TX_USED)))
368 break;
369
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000370 netdev_dbg(bp->dev, "skb %u (data %p) TX complete\n",
371 tail, skb->data);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100372 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
373 DMA_TO_DEVICE);
374 bp->stats.tx_packets++;
375 bp->stats.tx_bytes += skb->len;
376 rp->skb = NULL;
377 dev_kfree_skb_irq(skb);
378 }
379
380 bp->tx_tail = tail;
381 if (netif_queue_stopped(bp->dev) &&
382 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
383 netif_wake_queue(bp->dev);
384}
385
386static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
387 unsigned int last_frag)
388{
389 unsigned int len;
390 unsigned int frag;
391 unsigned int offset = 0;
392 struct sk_buff *skb;
393
394 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
395
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000396 netdev_dbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
397 first_frag, last_frag, len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100398
399 skb = dev_alloc_skb(len + RX_OFFSET);
400 if (!skb) {
401 bp->stats.rx_dropped++;
402 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
403 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
404 if (frag == last_frag)
405 break;
406 }
407 wmb();
408 return 1;
409 }
410
411 skb_reserve(skb, RX_OFFSET);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700412 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100413 skb_put(skb, len);
414
415 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
416 unsigned int frag_len = RX_BUFFER_SIZE;
417
418 if (offset + frag_len > len) {
419 BUG_ON(frag != last_frag);
420 frag_len = len - offset;
421 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300422 skb_copy_to_linear_data_offset(skb, offset,
423 (bp->rx_buffers +
424 (RX_BUFFER_SIZE * frag)),
425 frag_len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100426 offset += RX_BUFFER_SIZE;
427 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
428 wmb();
429
430 if (frag == last_frag)
431 break;
432 }
433
434 skb->protocol = eth_type_trans(skb, bp->dev);
435
436 bp->stats.rx_packets++;
437 bp->stats.rx_bytes += len;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000438 netdev_dbg(bp->dev, "received skb of length %u, csum: %08x\n",
439 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100440 netif_receive_skb(skb);
441
442 return 0;
443}
444
445/* Mark DMA descriptors from begin up to and not including end as unused */
446static void discard_partial_frame(struct macb *bp, unsigned int begin,
447 unsigned int end)
448{
449 unsigned int frag;
450
451 for (frag = begin; frag != end; frag = NEXT_RX(frag))
452 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
453 wmb();
454
455 /*
456 * When this happens, the hardware stats registers for
457 * whatever caused this is updated, so we don't have to record
458 * anything.
459 */
460}
461
462static int macb_rx(struct macb *bp, int budget)
463{
464 int received = 0;
465 unsigned int tail = bp->rx_tail;
466 int first_frag = -1;
467
468 for (; budget > 0; tail = NEXT_RX(tail)) {
469 u32 addr, ctrl;
470
471 rmb();
472 addr = bp->rx_ring[tail].addr;
473 ctrl = bp->rx_ring[tail].ctrl;
474
475 if (!(addr & MACB_BIT(RX_USED)))
476 break;
477
478 if (ctrl & MACB_BIT(RX_SOF)) {
479 if (first_frag != -1)
480 discard_partial_frame(bp, first_frag, tail);
481 first_frag = tail;
482 }
483
484 if (ctrl & MACB_BIT(RX_EOF)) {
485 int dropped;
486 BUG_ON(first_frag == -1);
487
488 dropped = macb_rx_frame(bp, first_frag, tail);
489 first_frag = -1;
490 if (!dropped) {
491 received++;
492 budget--;
493 }
494 }
495 }
496
497 if (first_frag != -1)
498 bp->rx_tail = first_frag;
499 else
500 bp->rx_tail = tail;
501
502 return received;
503}
504
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700505static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100506{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700507 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700508 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100509 u32 status;
510
511 status = macb_readl(bp, RSR);
512 macb_writel(bp, RSR, status);
513
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700514 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100515
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000516 netdev_dbg(bp->dev, "poll: status = %08lx, budget = %d\n",
517 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100518
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700519 work_done = macb_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000520 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800521 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100522
Joshua Hokeb3363692010-10-25 01:44:22 +0000523 /*
524 * We've done what we can to clean the buffers. Make sure we
525 * get notified when new packets arrive.
526 */
527 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
528 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100529
530 /* TODO: Handle errors */
531
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700532 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100533}
534
535static irqreturn_t macb_interrupt(int irq, void *dev_id)
536{
537 struct net_device *dev = dev_id;
538 struct macb *bp = netdev_priv(dev);
539 u32 status;
540
541 status = macb_readl(bp, ISR);
542
543 if (unlikely(!status))
544 return IRQ_NONE;
545
546 spin_lock(&bp->lock);
547
548 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100549 /* close possible race with dev_close */
550 if (unlikely(!netif_running(dev))) {
551 macb_writel(bp, IDR, ~0UL);
552 break;
553 }
554
555 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000556 /*
557 * There's no point taking any more interrupts
558 * until we have processed the buffers. The
559 * scheduling call may fail if the poll routine
560 * is already scheduled, so disable interrupts
561 * now.
562 */
563 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
564
Ben Hutchings288379f2009-01-19 16:43:59 -0800565 if (napi_schedule_prep(&bp->napi)) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000566 netdev_dbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800567 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100568 }
569 }
570
Erik Walingee33c582009-04-15 23:32:10 +0000571 if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
572 MACB_BIT(ISR_RLE)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100573 macb_tx(bp);
574
575 /*
576 * Link change detection isn't possible with RMII, so we'll
577 * add that if/when we get our hands on a full-blown MII PHY.
578 */
579
Alexander Steinb19f7f72011-04-13 05:03:24 +0000580 if (status & MACB_BIT(ISR_ROVR)) {
581 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000582 if (macb_is_gem(bp))
583 bp->hw_stats.gem.rx_overruns++;
584 else
585 bp->hw_stats.macb.rx_overruns++;
Alexander Steinb19f7f72011-04-13 05:03:24 +0000586 }
587
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100588 if (status & MACB_BIT(HRESP)) {
589 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000590 * TODO: Reset the hardware, and maybe move the
591 * netdev_err to a lower-priority context as well
592 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100593 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000594 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100595 }
596
597 status = macb_readl(bp, ISR);
598 }
599
600 spin_unlock(&bp->lock);
601
602 return IRQ_HANDLED;
603}
604
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700605#ifdef CONFIG_NET_POLL_CONTROLLER
606/*
607 * Polling receive - used by netconsole and other diagnostic tools
608 * to allow network i/o with interrupts disabled.
609 */
610static void macb_poll_controller(struct net_device *dev)
611{
612 unsigned long flags;
613
614 local_irq_save(flags);
615 macb_interrupt(dev->irq, dev);
616 local_irq_restore(flags);
617}
618#endif
619
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100620static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
621{
622 struct macb *bp = netdev_priv(dev);
623 dma_addr_t mapping;
624 unsigned int len, entry;
625 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -0700626 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100627
628#ifdef DEBUG
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000629 netdev_dbg(bp->dev,
630 "start_xmit: len %u head %p data %p tail %p end %p\n",
631 skb->len, skb->head, skb->data,
632 skb_tail_pointer(skb), skb_end_pointer(skb));
633 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
634 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100635#endif
636
637 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -0700638 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100639
640 /* This is a hard error, log it. */
641 if (TX_BUFFS_AVAIL(bp) < 1) {
642 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -0700643 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000644 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
645 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
646 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +0000647 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100648 }
649
650 entry = bp->tx_head;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000651 netdev_dbg(bp->dev, "Allocated ring entry %u\n", entry);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100652 mapping = dma_map_single(&bp->pdev->dev, skb->data,
653 len, DMA_TO_DEVICE);
654 bp->tx_skb[entry].skb = skb;
655 bp->tx_skb[entry].mapping = mapping;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000656 netdev_dbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
657 skb->data, (unsigned long)mapping);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100658
659 ctrl = MACB_BF(TX_FRMLEN, len);
660 ctrl |= MACB_BIT(TX_LAST);
661 if (entry == (TX_RING_SIZE - 1))
662 ctrl |= MACB_BIT(TX_WRAP);
663
664 bp->tx_ring[entry].addr = mapping;
665 bp->tx_ring[entry].ctrl = ctrl;
666 wmb();
667
668 entry = NEXT_TX(entry);
669 bp->tx_head = entry;
670
Richard Cochrane0720922011-06-19 21:51:28 +0000671 skb_tx_timestamp(skb);
672
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100673 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
674
675 if (TX_BUFFS_AVAIL(bp) < 1)
676 netif_stop_queue(dev);
677
Dongdong Deng48719532009-08-23 19:49:07 -0700678 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100679
Patrick McHardy6ed10652009-06-23 06:03:08 +0000680 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100681}
682
683static void macb_free_consistent(struct macb *bp)
684{
685 if (bp->tx_skb) {
686 kfree(bp->tx_skb);
687 bp->tx_skb = NULL;
688 }
689 if (bp->rx_ring) {
690 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
691 bp->rx_ring, bp->rx_ring_dma);
692 bp->rx_ring = NULL;
693 }
694 if (bp->tx_ring) {
695 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
696 bp->tx_ring, bp->tx_ring_dma);
697 bp->tx_ring = NULL;
698 }
699 if (bp->rx_buffers) {
700 dma_free_coherent(&bp->pdev->dev,
701 RX_RING_SIZE * RX_BUFFER_SIZE,
702 bp->rx_buffers, bp->rx_buffers_dma);
703 bp->rx_buffers = NULL;
704 }
705}
706
707static int macb_alloc_consistent(struct macb *bp)
708{
709 int size;
710
711 size = TX_RING_SIZE * sizeof(struct ring_info);
712 bp->tx_skb = kmalloc(size, GFP_KERNEL);
713 if (!bp->tx_skb)
714 goto out_err;
715
716 size = RX_RING_BYTES;
717 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
718 &bp->rx_ring_dma, GFP_KERNEL);
719 if (!bp->rx_ring)
720 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000721 netdev_dbg(bp->dev,
722 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
723 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100724
725 size = TX_RING_BYTES;
726 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
727 &bp->tx_ring_dma, GFP_KERNEL);
728 if (!bp->tx_ring)
729 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000730 netdev_dbg(bp->dev,
731 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
732 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100733
734 size = RX_RING_SIZE * RX_BUFFER_SIZE;
735 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
736 &bp->rx_buffers_dma, GFP_KERNEL);
737 if (!bp->rx_buffers)
738 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000739 netdev_dbg(bp->dev,
740 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
741 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100742
743 return 0;
744
745out_err:
746 macb_free_consistent(bp);
747 return -ENOMEM;
748}
749
750static void macb_init_rings(struct macb *bp)
751{
752 int i;
753 dma_addr_t addr;
754
755 addr = bp->rx_buffers_dma;
756 for (i = 0; i < RX_RING_SIZE; i++) {
757 bp->rx_ring[i].addr = addr;
758 bp->rx_ring[i].ctrl = 0;
759 addr += RX_BUFFER_SIZE;
760 }
761 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
762
763 for (i = 0; i < TX_RING_SIZE; i++) {
764 bp->tx_ring[i].addr = 0;
765 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
766 }
767 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
768
769 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
770}
771
772static void macb_reset_hw(struct macb *bp)
773{
774 /* Make sure we have the write buffer for ourselves */
775 wmb();
776
777 /*
778 * Disable RX and TX (XXX: Should we halt the transmission
779 * more gracefully?)
780 */
781 macb_writel(bp, NCR, 0);
782
783 /* Clear the stats registers (XXX: Update stats first?) */
784 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
785
786 /* Clear all status flags */
787 macb_writel(bp, TSR, ~0UL);
788 macb_writel(bp, RSR, ~0UL);
789
790 /* Disable all interrupts */
791 macb_writel(bp, IDR, ~0UL);
792 macb_readl(bp, ISR);
793}
794
Jamie Iles70c9f3d2011-03-09 16:22:54 +0000795static u32 gem_mdc_clk_div(struct macb *bp)
796{
797 u32 config;
798 unsigned long pclk_hz = clk_get_rate(bp->pclk);
799
800 if (pclk_hz <= 20000000)
801 config = GEM_BF(CLK, GEM_CLK_DIV8);
802 else if (pclk_hz <= 40000000)
803 config = GEM_BF(CLK, GEM_CLK_DIV16);
804 else if (pclk_hz <= 80000000)
805 config = GEM_BF(CLK, GEM_CLK_DIV32);
806 else if (pclk_hz <= 120000000)
807 config = GEM_BF(CLK, GEM_CLK_DIV48);
808 else if (pclk_hz <= 160000000)
809 config = GEM_BF(CLK, GEM_CLK_DIV64);
810 else
811 config = GEM_BF(CLK, GEM_CLK_DIV96);
812
813 return config;
814}
815
816static u32 macb_mdc_clk_div(struct macb *bp)
817{
818 u32 config;
819 unsigned long pclk_hz;
820
821 if (macb_is_gem(bp))
822 return gem_mdc_clk_div(bp);
823
824 pclk_hz = clk_get_rate(bp->pclk);
825 if (pclk_hz <= 20000000)
826 config = MACB_BF(CLK, MACB_CLK_DIV8);
827 else if (pclk_hz <= 40000000)
828 config = MACB_BF(CLK, MACB_CLK_DIV16);
829 else if (pclk_hz <= 80000000)
830 config = MACB_BF(CLK, MACB_CLK_DIV32);
831 else
832 config = MACB_BF(CLK, MACB_CLK_DIV64);
833
834 return config;
835}
836
Jamie Iles757a03c2011-03-09 16:29:59 +0000837/*
838 * Get the DMA bus width field of the network configuration register that we
839 * should program. We find the width from decoding the design configuration
840 * register to find the maximum supported data bus width.
841 */
842static u32 macb_dbw(struct macb *bp)
843{
844 if (!macb_is_gem(bp))
845 return 0;
846
847 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
848 case 4:
849 return GEM_BF(DBW, GEM_DBW128);
850 case 2:
851 return GEM_BF(DBW, GEM_DBW64);
852 case 1:
853 default:
854 return GEM_BF(DBW, GEM_DBW32);
855 }
856}
857
Jamie Iles0116da42011-03-14 17:38:30 +0000858/*
859 * Configure the receive DMA engine to use the correct receive buffer size.
860 * This is a configurable parameter for GEM.
861 */
862static void macb_configure_dma(struct macb *bp)
863{
864 u32 dmacfg;
865
866 if (macb_is_gem(bp)) {
867 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
868 dmacfg |= GEM_BF(RXBS, RX_BUFFER_SIZE / 64);
869 gem_writel(bp, DMACFG, dmacfg);
870 }
871}
872
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100873static void macb_init_hw(struct macb *bp)
874{
875 u32 config;
876
877 macb_reset_hw(bp);
878 __macb_set_hwaddr(bp);
879
Jamie Iles70c9f3d2011-03-09 16:22:54 +0000880 config = macb_mdc_clk_div(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100881 config |= MACB_BIT(PAE); /* PAuse Enable */
882 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -0700883 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100884 if (bp->dev->flags & IFF_PROMISC)
885 config |= MACB_BIT(CAF); /* Copy All Frames */
886 if (!(bp->dev->flags & IFF_BROADCAST))
887 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +0000888 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100889 macb_writel(bp, NCFGR, config);
890
Jamie Iles0116da42011-03-14 17:38:30 +0000891 macb_configure_dma(bp);
892
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100893 /* Initialize TX and RX buffers */
894 macb_writel(bp, RBQP, bp->rx_ring_dma);
895 macb_writel(bp, TBQP, bp->tx_ring_dma);
896
897 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +0200898 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100899
900 /* Enable interrupts */
901 macb_writel(bp, IER, (MACB_BIT(RCOMP)
902 | MACB_BIT(RXUBR)
903 | MACB_BIT(ISR_TUND)
904 | MACB_BIT(ISR_RLE)
905 | MACB_BIT(TXERR)
906 | MACB_BIT(TCOMP)
907 | MACB_BIT(ISR_ROVR)
908 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100909
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100910}
911
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200912/*
913 * The hash address register is 64 bits long and takes up two
914 * locations in the memory map. The least significant bits are stored
915 * in EMAC_HSL and the most significant bits in EMAC_HSH.
916 *
917 * The unicast hash enable and the multicast hash enable bits in the
918 * network configuration register enable the reception of hash matched
919 * frames. The destination address is reduced to a 6 bit index into
920 * the 64 bit hash register using the following hash function. The
921 * hash function is an exclusive or of every sixth bit of the
922 * destination address.
923 *
924 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
925 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
926 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
927 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
928 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
929 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
930 *
931 * da[0] represents the least significant bit of the first byte
932 * received, that is, the multicast/unicast indicator, and da[47]
933 * represents the most significant bit of the last byte received. If
934 * the hash index, hi[n], points to a bit that is set in the hash
935 * register then the frame will be matched according to whether the
936 * frame is multicast or unicast. A multicast match will be signalled
937 * if the multicast hash enable bit is set, da[0] is 1 and the hash
938 * index points to a bit set in the hash register. A unicast match
939 * will be signalled if the unicast hash enable bit is set, da[0] is 0
940 * and the hash index points to a bit set in the hash register. To
941 * receive all multicast frames, the hash register should be set with
942 * all ones and the multicast hash enable bit should be set in the
943 * network configuration register.
944 */
945
946static inline int hash_bit_value(int bitnr, __u8 *addr)
947{
948 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
949 return 1;
950 return 0;
951}
952
953/*
954 * Return the hash index value for the specified address.
955 */
956static int hash_get_index(__u8 *addr)
957{
958 int i, j, bitval;
959 int hash_index = 0;
960
961 for (j = 0; j < 6; j++) {
962 for (i = 0, bitval = 0; i < 8; i++)
963 bitval ^= hash_bit_value(i*6 + j, addr);
964
965 hash_index |= (bitval << j);
966 }
967
968 return hash_index;
969}
970
971/*
972 * Add multicast addresses to the internal multicast-hash table.
973 */
974static void macb_sethashtable(struct net_device *dev)
975{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000976 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200977 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000978 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200979 struct macb *bp = netdev_priv(dev);
980
981 mc_filter[0] = mc_filter[1] = 0;
982
Jiri Pirko22bedad32010-04-01 21:22:57 +0000983 netdev_for_each_mc_addr(ha, dev) {
984 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200985 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
986 }
987
Jamie Ilesf75ba502011-11-08 10:12:32 +0000988 macb_or_gem_writel(bp, HRB, mc_filter[0]);
989 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200990}
991
992/*
993 * Enable/Disable promiscuous and multicast modes.
994 */
995static void macb_set_rx_mode(struct net_device *dev)
996{
997 unsigned long cfg;
998 struct macb *bp = netdev_priv(dev);
999
1000 cfg = macb_readl(bp, NCFGR);
1001
1002 if (dev->flags & IFF_PROMISC)
1003 /* Enable promiscuous mode */
1004 cfg |= MACB_BIT(CAF);
1005 else if (dev->flags & (~IFF_PROMISC))
1006 /* Disable promiscuous mode */
1007 cfg &= ~MACB_BIT(CAF);
1008
1009 if (dev->flags & IFF_ALLMULTI) {
1010 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001011 macb_or_gem_writel(bp, HRB, -1);
1012 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001013 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001014 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001015 /* Enable specific multicasts */
1016 macb_sethashtable(dev);
1017 cfg |= MACB_BIT(NCFGR_MTI);
1018 } else if (dev->flags & (~IFF_ALLMULTI)) {
1019 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001020 macb_or_gem_writel(bp, HRB, 0);
1021 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001022 cfg &= ~MACB_BIT(NCFGR_MTI);
1023 }
1024
1025 macb_writel(bp, NCFGR, cfg);
1026}
1027
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001028static int macb_open(struct net_device *dev)
1029{
1030 struct macb *bp = netdev_priv(dev);
1031 int err;
1032
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001033 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001034
frederic RODO6c36a702007-07-12 19:07:24 +02001035 /* if the phy is not yet register, retry later*/
1036 if (!bp->phy_dev)
1037 return -EAGAIN;
1038
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001039 if (!is_valid_ether_addr(dev->dev_addr))
1040 return -EADDRNOTAVAIL;
1041
1042 err = macb_alloc_consistent(bp);
1043 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001044 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1045 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001046 return err;
1047 }
1048
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001049 napi_enable(&bp->napi);
1050
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001051 macb_init_rings(bp);
1052 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001053
frederic RODO6c36a702007-07-12 19:07:24 +02001054 /* schedule a link state check */
1055 phy_start(bp->phy_dev);
1056
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001057 netif_start_queue(dev);
1058
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001059 return 0;
1060}
1061
1062static int macb_close(struct net_device *dev)
1063{
1064 struct macb *bp = netdev_priv(dev);
1065 unsigned long flags;
1066
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001067 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001068 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001069
frederic RODO6c36a702007-07-12 19:07:24 +02001070 if (bp->phy_dev)
1071 phy_stop(bp->phy_dev);
1072
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001073 spin_lock_irqsave(&bp->lock, flags);
1074 macb_reset_hw(bp);
1075 netif_carrier_off(dev);
1076 spin_unlock_irqrestore(&bp->lock, flags);
1077
1078 macb_free_consistent(bp);
1079
1080 return 0;
1081}
1082
Jamie Ilesa494ed82011-03-09 16:26:35 +00001083static void gem_update_stats(struct macb *bp)
1084{
1085 u32 __iomem *reg = bp->regs + GEM_OTX;
1086 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1087 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1088
1089 for (; p < end; p++, reg++)
1090 *p += __raw_readl(reg);
1091}
1092
1093static struct net_device_stats *gem_get_stats(struct macb *bp)
1094{
1095 struct gem_stats *hwstat = &bp->hw_stats.gem;
1096 struct net_device_stats *nstat = &bp->stats;
1097
1098 gem_update_stats(bp);
1099
1100 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1101 hwstat->rx_alignment_errors +
1102 hwstat->rx_resource_errors +
1103 hwstat->rx_overruns +
1104 hwstat->rx_oversize_frames +
1105 hwstat->rx_jabbers +
1106 hwstat->rx_undersized_frames +
1107 hwstat->rx_length_field_frame_errors);
1108 nstat->tx_errors = (hwstat->tx_late_collisions +
1109 hwstat->tx_excessive_collisions +
1110 hwstat->tx_underrun +
1111 hwstat->tx_carrier_sense_errors);
1112 nstat->multicast = hwstat->rx_multicast_frames;
1113 nstat->collisions = (hwstat->tx_single_collision_frames +
1114 hwstat->tx_multiple_collision_frames +
1115 hwstat->tx_excessive_collisions);
1116 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1117 hwstat->rx_jabbers +
1118 hwstat->rx_undersized_frames +
1119 hwstat->rx_length_field_frame_errors);
1120 nstat->rx_over_errors = hwstat->rx_resource_errors;
1121 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1122 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1123 nstat->rx_fifo_errors = hwstat->rx_overruns;
1124 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1125 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1126 nstat->tx_fifo_errors = hwstat->tx_underrun;
1127
1128 return nstat;
1129}
1130
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001131static struct net_device_stats *macb_get_stats(struct net_device *dev)
1132{
1133 struct macb *bp = netdev_priv(dev);
1134 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001135 struct macb_stats *hwstat = &bp->hw_stats.macb;
1136
1137 if (macb_is_gem(bp))
1138 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001139
frederic RODO6c36a702007-07-12 19:07:24 +02001140 /* read stats from hardware */
1141 macb_update_stats(bp);
1142
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001143 /* Convert HW stats into netdevice stats */
1144 nstat->rx_errors = (hwstat->rx_fcs_errors +
1145 hwstat->rx_align_errors +
1146 hwstat->rx_resource_errors +
1147 hwstat->rx_overruns +
1148 hwstat->rx_oversize_pkts +
1149 hwstat->rx_jabbers +
1150 hwstat->rx_undersize_pkts +
1151 hwstat->sqe_test_errors +
1152 hwstat->rx_length_mismatch);
1153 nstat->tx_errors = (hwstat->tx_late_cols +
1154 hwstat->tx_excessive_cols +
1155 hwstat->tx_underruns +
1156 hwstat->tx_carrier_errors);
1157 nstat->collisions = (hwstat->tx_single_cols +
1158 hwstat->tx_multiple_cols +
1159 hwstat->tx_excessive_cols);
1160 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1161 hwstat->rx_jabbers +
1162 hwstat->rx_undersize_pkts +
1163 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001164 nstat->rx_over_errors = hwstat->rx_resource_errors +
1165 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001166 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1167 nstat->rx_frame_errors = hwstat->rx_align_errors;
1168 nstat->rx_fifo_errors = hwstat->rx_overruns;
1169 /* XXX: What does "missed" mean? */
1170 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1171 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1172 nstat->tx_fifo_errors = hwstat->tx_underruns;
1173 /* Don't know about heartbeat or window errors... */
1174
1175 return nstat;
1176}
1177
1178static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1179{
1180 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001181 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001182
frederic RODO6c36a702007-07-12 19:07:24 +02001183 if (!phydev)
1184 return -ENODEV;
1185
1186 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001187}
1188
1189static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1190{
1191 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001192 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001193
frederic RODO6c36a702007-07-12 19:07:24 +02001194 if (!phydev)
1195 return -ENODEV;
1196
1197 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001198}
1199
frederic RODO6c36a702007-07-12 19:07:24 +02001200static void macb_get_drvinfo(struct net_device *dev,
1201 struct ethtool_drvinfo *info)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001202{
1203 struct macb *bp = netdev_priv(dev);
1204
1205 strcpy(info->driver, bp->pdev->dev.driver->name);
1206 strcpy(info->version, "$Revision: 1.14 $");
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08001207 strcpy(info->bus_info, dev_name(&bp->pdev->dev));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001208}
1209
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001210static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001211 .get_settings = macb_get_settings,
1212 .set_settings = macb_set_settings,
1213 .get_drvinfo = macb_get_drvinfo,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214 .get_link = ethtool_op_get_link,
1215};
1216
1217static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1218{
1219 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001220 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001221
1222 if (!netif_running(dev))
1223 return -EINVAL;
1224
frederic RODO6c36a702007-07-12 19:07:24 +02001225 if (!phydev)
1226 return -ENODEV;
1227
Richard Cochran28b04112010-07-17 08:48:55 +00001228 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001229}
1230
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001231static const struct net_device_ops macb_netdev_ops = {
1232 .ndo_open = macb_open,
1233 .ndo_stop = macb_close,
1234 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001235 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001236 .ndo_get_stats = macb_get_stats,
1237 .ndo_do_ioctl = macb_ioctl,
1238 .ndo_validate_addr = eth_validate_addr,
1239 .ndo_change_mtu = eth_change_mtu,
1240 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001241#ifdef CONFIG_NET_POLL_CONTROLLER
1242 .ndo_poll_controller = macb_poll_controller,
1243#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001244};
1245
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001246#if defined(CONFIG_OF)
1247static const struct of_device_id macb_dt_ids[] = {
1248 { .compatible = "cdns,at32ap7000-macb" },
1249 { .compatible = "cdns,at91sam9260-macb" },
1250 { .compatible = "cdns,macb" },
1251 { .compatible = "cdns,pc302-gem" },
1252 { .compatible = "cdns,gem" },
1253 { /* sentinel */ }
1254};
1255
1256MODULE_DEVICE_TABLE(of, macb_dt_ids);
1257
1258static int __devinit macb_get_phy_mode_dt(struct platform_device *pdev)
1259{
1260 struct device_node *np = pdev->dev.of_node;
1261
1262 if (np)
1263 return of_get_phy_mode(np);
1264
1265 return -ENODEV;
1266}
1267
1268static int __devinit macb_get_hwaddr_dt(struct macb *bp)
1269{
1270 struct device_node *np = bp->pdev->dev.of_node;
1271 if (np) {
1272 const char *mac = of_get_mac_address(np);
1273 if (mac) {
1274 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
1275 return 0;
1276 }
1277 }
1278
1279 return -ENODEV;
1280}
1281#else
1282static int __devinit macb_get_phy_mode_dt(struct platform_device *pdev)
1283{
1284 return -ENODEV;
1285}
1286static int __devinit macb_get_hwaddr_dt(struct macb *bp)
1287{
1288 return -ENODEV;
1289}
1290#endif
1291
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001292static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001293{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001294 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001295 struct resource *regs;
1296 struct net_device *dev;
1297 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001298 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001299 u32 config;
1300 int err = -ENXIO;
1301
1302 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1303 if (!regs) {
1304 dev_err(&pdev->dev, "no mmio resource defined\n");
1305 goto err_out;
1306 }
1307
1308 err = -ENOMEM;
1309 dev = alloc_etherdev(sizeof(*bp));
1310 if (!dev) {
1311 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1312 goto err_out;
1313 }
1314
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001315 SET_NETDEV_DEV(dev, &pdev->dev);
1316
1317 /* TODO: Actually, we have some interesting features... */
1318 dev->features |= 0;
1319
1320 bp = netdev_priv(dev);
1321 bp->pdev = pdev;
1322 bp->dev = dev;
1323
1324 spin_lock_init(&bp->lock);
1325
Jamie Iles461845d2011-03-08 20:19:23 +00001326 bp->pclk = clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001327 if (IS_ERR(bp->pclk)) {
1328 dev_err(&pdev->dev, "failed to get macb_clk\n");
1329 goto err_out_free_dev;
1330 }
1331 clk_enable(bp->pclk);
Jamie Iles461845d2011-03-08 20:19:23 +00001332
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001333 bp->hclk = clk_get(&pdev->dev, "hclk");
1334 if (IS_ERR(bp->hclk)) {
1335 dev_err(&pdev->dev, "failed to get hclk\n");
1336 goto err_out_put_pclk;
1337 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001338 clk_enable(bp->hclk);
1339
Joe Perches28f65c112011-06-09 09:13:32 -07001340 bp->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001341 if (!bp->regs) {
1342 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1343 err = -ENOMEM;
1344 goto err_out_disable_clocks;
1345 }
1346
1347 dev->irq = platform_get_irq(pdev, 0);
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00001348 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001349 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001350 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1351 dev->irq, err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001352 goto err_out_iounmap;
1353 }
1354
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001355 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001356 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001357 dev->ethtool_ops = &macb_ethtool_ops;
1358
1359 dev->base_addr = regs->start;
1360
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001361 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001362 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00001363 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001364 macb_writel(bp, NCFGR, config);
1365
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001366 err = macb_get_hwaddr_dt(bp);
1367 if (err < 0)
1368 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02001369
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001370 err = macb_get_phy_mode_dt(pdev);
1371 if (err < 0) {
1372 pdata = pdev->dev.platform_data;
1373 if (pdata && pdata->is_rmii)
1374 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
1375 else
1376 bp->phy_interface = PHY_INTERFACE_MODE_MII;
1377 } else {
1378 bp->phy_interface = err;
1379 }
1380
1381 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01001382#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001383 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1384 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01001385#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001386 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001387#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001388 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001389#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001390 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01001391#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001392 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001393#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001394
1395 bp->tx_pending = DEF_TX_RING_PENDING;
1396
1397 err = register_netdev(dev);
1398 if (err) {
1399 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1400 goto err_out_free_irq;
1401 }
1402
frederic RODO6c36a702007-07-12 19:07:24 +02001403 if (macb_mii_init(bp) != 0) {
1404 goto err_out_unregister_netdev;
1405 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001406
frederic RODO6c36a702007-07-12 19:07:24 +02001407 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001408
Jamie Ilesf75ba502011-11-08 10:12:32 +00001409 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1410 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1411 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001412
frederic RODO6c36a702007-07-12 19:07:24 +02001413 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001414 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1415 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02001416
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001417 return 0;
1418
frederic RODO6c36a702007-07-12 19:07:24 +02001419err_out_unregister_netdev:
1420 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001421err_out_free_irq:
1422 free_irq(dev->irq, dev);
1423err_out_iounmap:
1424 iounmap(bp->regs);
1425err_out_disable_clocks:
1426 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001427 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001428 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001429err_out_put_pclk:
1430 clk_put(bp->pclk);
1431err_out_free_dev:
1432 free_netdev(dev);
1433err_out:
1434 platform_set_drvdata(pdev, NULL);
1435 return err;
1436}
1437
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001438static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001439{
1440 struct net_device *dev;
1441 struct macb *bp;
1442
1443 dev = platform_get_drvdata(pdev);
1444
1445 if (dev) {
1446 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09001447 if (bp->phy_dev)
1448 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001449 mdiobus_unregister(bp->mii_bus);
1450 kfree(bp->mii_bus->irq);
1451 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001452 unregister_netdev(dev);
1453 free_irq(dev->irq, dev);
1454 iounmap(bp->regs);
1455 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001456 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001457 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001458 clk_put(bp->pclk);
1459 free_netdev(dev);
1460 platform_set_drvdata(pdev, NULL);
1461 }
1462
1463 return 0;
1464}
1465
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001466#ifdef CONFIG_PM
1467static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1468{
1469 struct net_device *netdev = platform_get_drvdata(pdev);
1470 struct macb *bp = netdev_priv(netdev);
1471
1472 netif_device_detach(netdev);
1473
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001474 clk_disable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001475 clk_disable(bp->pclk);
1476
1477 return 0;
1478}
1479
1480static int macb_resume(struct platform_device *pdev)
1481{
1482 struct net_device *netdev = platform_get_drvdata(pdev);
1483 struct macb *bp = netdev_priv(netdev);
1484
1485 clk_enable(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001486 clk_enable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001487
1488 netif_device_attach(netdev);
1489
1490 return 0;
1491}
1492#else
1493#define macb_suspend NULL
1494#define macb_resume NULL
1495#endif
1496
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001497static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001498 .remove = __exit_p(macb_remove),
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001499 .suspend = macb_suspend,
1500 .resume = macb_resume,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001501 .driver = {
1502 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07001503 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001504 .of_match_table = of_match_ptr(macb_dt_ids),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001505 },
1506};
1507
1508static int __init macb_init(void)
1509{
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001510 return platform_driver_probe(&macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001511}
1512
1513static void __exit macb_exit(void)
1514{
1515 platform_driver_unregister(&macb_driver);
1516}
1517
1518module_init(macb_init);
1519module_exit(macb_exit);
1520
1521MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00001522MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001523MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07001524MODULE_ALIAS("platform:macb");