blob: d97d9ce986f84a2693bbc89192fd8e4054f9520a [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
2 * Atmel MACB Ethernet Controller driver
3 *
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
11#include <linux/clk.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
16#include <linux/slab.h>
17#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000018#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010019#include <linux/netdevice.h>
20#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010021#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000022#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020024#include <linux/phy.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010026#include "macb.h"
27
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010028#define RX_BUFFER_SIZE 128
29#define RX_RING_SIZE 512
30#define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
31
32/* Make the IP header word-aligned (the ethernet header is 14 bytes) */
33#define RX_OFFSET 2
34
35#define TX_RING_SIZE 128
36#define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
37#define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
38
39#define TX_RING_GAP(bp) \
40 (TX_RING_SIZE - (bp)->tx_pending)
41#define TX_BUFFS_AVAIL(bp) \
42 (((bp)->tx_tail <= (bp)->tx_head) ? \
43 (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
44 (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
45#define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
46
47#define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
48
49/* minimum number of free TX descriptors before waking up TX process */
50#define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
51
52#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
53 | MACB_BIT(ISR_ROVR))
54
55static void __macb_set_hwaddr(struct macb *bp)
56{
57 u32 bottom;
58 u16 top;
59
60 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
61 macb_writel(bp, SA1B, bottom);
62 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
63 macb_writel(bp, SA1T, top);
64}
65
66static void __init macb_get_hwaddr(struct macb *bp)
67{
68 u32 bottom;
69 u16 top;
70 u8 addr[6];
71
72 bottom = macb_readl(bp, SA1B);
73 top = macb_readl(bp, SA1T);
74
75 addr[0] = bottom & 0xff;
76 addr[1] = (bottom >> 8) & 0xff;
77 addr[2] = (bottom >> 16) & 0xff;
78 addr[3] = (bottom >> 24) & 0xff;
79 addr[4] = top & 0xff;
80 addr[5] = (top >> 8) & 0xff;
81
Sven Schnelled1d57412008-06-09 16:33:57 -070082 if (is_valid_ether_addr(addr)) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010083 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
Sven Schnelled1d57412008-06-09 16:33:57 -070084 } else {
85 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
86 random_ether_addr(bp->dev->dev_addr);
87 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010088}
89
frederic RODO6c36a702007-07-12 19:07:24 +020090static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010091{
frederic RODO6c36a702007-07-12 19:07:24 +020092 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010093 int value;
94
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010095 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
96 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +020097 | MACB_BF(PHYA, mii_id)
98 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010099 | MACB_BF(CODE, MACB_MAN_CODE)));
100
frederic RODO6c36a702007-07-12 19:07:24 +0200101 /* wait for end of transfer */
102 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
103 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100104
105 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100106
107 return value;
108}
109
frederic RODO6c36a702007-07-12 19:07:24 +0200110static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
111 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100112{
frederic RODO6c36a702007-07-12 19:07:24 +0200113 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100114
115 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
116 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200117 | MACB_BF(PHYA, mii_id)
118 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100119 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200120 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100121
frederic RODO6c36a702007-07-12 19:07:24 +0200122 /* wait for end of transfer */
123 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
124 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100125
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100126 return 0;
127}
128
frederic RODO6c36a702007-07-12 19:07:24 +0200129static int macb_mdio_reset(struct mii_bus *bus)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100130{
frederic RODO6c36a702007-07-12 19:07:24 +0200131 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100132}
133
frederic RODO6c36a702007-07-12 19:07:24 +0200134static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100135{
frederic RODO6c36a702007-07-12 19:07:24 +0200136 struct macb *bp = netdev_priv(dev);
137 struct phy_device *phydev = bp->phy_dev;
138 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100139
frederic RODO6c36a702007-07-12 19:07:24 +0200140 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100141
frederic RODO6c36a702007-07-12 19:07:24 +0200142 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100143
frederic RODO6c36a702007-07-12 19:07:24 +0200144 if (phydev->link) {
145 if ((bp->speed != phydev->speed) ||
146 (bp->duplex != phydev->duplex)) {
147 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100148
frederic RODO6c36a702007-07-12 19:07:24 +0200149 reg = macb_readl(bp, NCFGR);
150 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
151
152 if (phydev->duplex)
153 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900154 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200155 reg |= MACB_BIT(SPD);
156
157 macb_writel(bp, NCFGR, reg);
158
159 bp->speed = phydev->speed;
160 bp->duplex = phydev->duplex;
161 status_change = 1;
162 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100163 }
164
frederic RODO6c36a702007-07-12 19:07:24 +0200165 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700166 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200167 bp->speed = 0;
168 bp->duplex = -1;
169 }
170 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100171
frederic RODO6c36a702007-07-12 19:07:24 +0200172 status_change = 1;
173 }
174
175 spin_unlock_irqrestore(&bp->lock, flags);
176
177 if (status_change) {
178 if (phydev->link)
179 printk(KERN_INFO "%s: link up (%d/%s)\n",
180 dev->name, phydev->speed,
181 DUPLEX_FULL == phydev->duplex ? "Full":"Half");
182 else
183 printk(KERN_INFO "%s: link down\n", dev->name);
184 }
185}
186
187/* based on au1000_eth. c*/
188static int macb_mii_probe(struct net_device *dev)
189{
190 struct macb *bp = netdev_priv(dev);
Jiri Pirko7455a762010-02-08 05:12:08 +0000191 struct phy_device *phydev;
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000192 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000193 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200194
Jiri Pirko7455a762010-02-08 05:12:08 +0000195 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200196 if (!phydev) {
197 printk (KERN_ERR "%s: no PHY found\n", dev->name);
198 return -1;
199 }
200
201 pdata = bp->pdev->dev.platform_data;
202 /* TODO : add pin_irq */
203
204 /* attach the mac to the phy */
Jiri Pirko7455a762010-02-08 05:12:08 +0000205 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change, 0,
206 pdata && pdata->is_rmii ?
207 PHY_INTERFACE_MODE_RMII :
208 PHY_INTERFACE_MODE_MII);
209 if (ret) {
frederic RODO6c36a702007-07-12 19:07:24 +0200210 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
Jiri Pirko7455a762010-02-08 05:12:08 +0000211 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200212 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100213
frederic RODO6c36a702007-07-12 19:07:24 +0200214 /* mask with MAC supported features */
215 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100216
frederic RODO6c36a702007-07-12 19:07:24 +0200217 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100218
frederic RODO6c36a702007-07-12 19:07:24 +0200219 bp->link = 0;
220 bp->speed = 0;
221 bp->duplex = -1;
222 bp->phy_dev = phydev;
223
224 return 0;
225}
226
227static int macb_mii_init(struct macb *bp)
228{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000229 struct macb_platform_data *pdata;
frederic RODO6c36a702007-07-12 19:07:24 +0200230 int err = -ENXIO, i;
231
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200232 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200233 macb_writel(bp, NCR, MACB_BIT(MPE));
234
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700235 bp->mii_bus = mdiobus_alloc();
236 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200237 err = -ENOMEM;
238 goto err_out;
239 }
240
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700241 bp->mii_bus->name = "MACB_mii_bus";
242 bp->mii_bus->read = &macb_mdio_read;
243 bp->mii_bus->write = &macb_mdio_write;
244 bp->mii_bus->reset = &macb_mdio_reset;
245 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
246 bp->mii_bus->priv = bp;
247 bp->mii_bus->parent = &bp->dev->dev;
248 pdata = bp->pdev->dev.platform_data;
249
250 if (pdata)
251 bp->mii_bus->phy_mask = pdata->phy_mask;
252
253 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
254 if (!bp->mii_bus->irq) {
255 err = -ENOMEM;
256 goto err_out_free_mdiobus;
257 }
258
frederic RODO6c36a702007-07-12 19:07:24 +0200259 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700260 bp->mii_bus->irq[i] = PHY_POLL;
frederic RODO6c36a702007-07-12 19:07:24 +0200261
Jamie Iles91523942011-02-28 04:05:25 +0000262 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200263
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700264 if (mdiobus_register(bp->mii_bus))
frederic RODO6c36a702007-07-12 19:07:24 +0200265 goto err_out_free_mdio_irq;
266
267 if (macb_mii_probe(bp->dev) != 0) {
268 goto err_out_unregister_bus;
269 }
270
271 return 0;
272
273err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700274 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200275err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700276 kfree(bp->mii_bus->irq);
277err_out_free_mdiobus:
278 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200279err_out:
280 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100281}
282
283static void macb_update_stats(struct macb *bp)
284{
285 u32 __iomem *reg = bp->regs + MACB_PFR;
286 u32 *p = &bp->hw_stats.rx_pause_frames;
287 u32 *end = &bp->hw_stats.tx_pause_frames + 1;
288
289 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
290
291 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100292 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100293}
294
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100295static void macb_tx(struct macb *bp)
296{
297 unsigned int tail;
298 unsigned int head;
299 u32 status;
300
301 status = macb_readl(bp, TSR);
302 macb_writel(bp, TSR, status);
303
304 dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
305 (unsigned long)status);
306
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;
Erik Walingee33c582009-04-15 23:32:10 +0000309 printk(KERN_ERR "%s: TX %s, resetting buffers\n",
310 bp->dev->name, 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
370 dev_dbg(&bp->pdev->dev, "skb %u (data %p) TX complete\n",
371 tail, skb->data);
372 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
396 dev_dbg(&bp->pdev->dev, "macb_rx_frame frags %u - %u (len %u)\n",
397 first_frag, last_frag, len);
398
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;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438 dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
439 skb->len, skb->csum);
440 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
516 dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700517 (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)) {
frederic RODO6c36a702007-07-12 19:07:24 +0200566 dev_dbg(&bp->pdev->dev,
567 "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 */
583 bp->hw_stats.rx_overruns++;
584 }
585
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100586 if (status & MACB_BIT(HRESP)) {
587 /*
588 * TODO: Reset the hardware, and maybe move the printk
589 * to a lower-priority context as well (work queue?)
590 */
591 printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
592 dev->name);
593 }
594
595 status = macb_readl(bp, ISR);
596 }
597
598 spin_unlock(&bp->lock);
599
600 return IRQ_HANDLED;
601}
602
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700603#ifdef CONFIG_NET_POLL_CONTROLLER
604/*
605 * Polling receive - used by netconsole and other diagnostic tools
606 * to allow network i/o with interrupts disabled.
607 */
608static void macb_poll_controller(struct net_device *dev)
609{
610 unsigned long flags;
611
612 local_irq_save(flags);
613 macb_interrupt(dev->irq, dev);
614 local_irq_restore(flags);
615}
616#endif
617
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100618static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
619{
620 struct macb *bp = netdev_priv(dev);
621 dma_addr_t mapping;
622 unsigned int len, entry;
623 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -0700624 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100625
626#ifdef DEBUG
627 int i;
628 dev_dbg(&bp->pdev->dev,
629 "start_xmit: len %u head %p data %p tail %p end %p\n",
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700630 skb->len, skb->head, skb->data,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700631 skb_tail_pointer(skb), skb_end_pointer(skb));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100632 dev_dbg(&bp->pdev->dev,
633 "data:");
634 for (i = 0; i < 16; i++)
635 printk(" %02x", (unsigned int)skb->data[i]);
636 printk("\n");
637#endif
638
639 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -0700640 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100641
642 /* This is a hard error, log it. */
643 if (TX_BUFFS_AVAIL(bp) < 1) {
644 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -0700645 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100646 dev_err(&bp->pdev->dev,
647 "BUG! Tx Ring full when queue awake!\n");
648 dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
649 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +0000650 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100651 }
652
653 entry = bp->tx_head;
654 dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
655 mapping = dma_map_single(&bp->pdev->dev, skb->data,
656 len, DMA_TO_DEVICE);
657 bp->tx_skb[entry].skb = skb;
658 bp->tx_skb[entry].mapping = mapping;
659 dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
660 skb->data, (unsigned long)mapping);
661
662 ctrl = MACB_BF(TX_FRMLEN, len);
663 ctrl |= MACB_BIT(TX_LAST);
664 if (entry == (TX_RING_SIZE - 1))
665 ctrl |= MACB_BIT(TX_WRAP);
666
667 bp->tx_ring[entry].addr = mapping;
668 bp->tx_ring[entry].ctrl = ctrl;
669 wmb();
670
671 entry = NEXT_TX(entry);
672 bp->tx_head = entry;
673
Richard Cochrane0720922011-06-19 21:51:28 +0000674 skb_tx_timestamp(skb);
675
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100676 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
677
678 if (TX_BUFFS_AVAIL(bp) < 1)
679 netif_stop_queue(dev);
680
Dongdong Deng48719532009-08-23 19:49:07 -0700681 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100682
Patrick McHardy6ed10652009-06-23 06:03:08 +0000683 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100684}
685
686static void macb_free_consistent(struct macb *bp)
687{
688 if (bp->tx_skb) {
689 kfree(bp->tx_skb);
690 bp->tx_skb = NULL;
691 }
692 if (bp->rx_ring) {
693 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
694 bp->rx_ring, bp->rx_ring_dma);
695 bp->rx_ring = NULL;
696 }
697 if (bp->tx_ring) {
698 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
699 bp->tx_ring, bp->tx_ring_dma);
700 bp->tx_ring = NULL;
701 }
702 if (bp->rx_buffers) {
703 dma_free_coherent(&bp->pdev->dev,
704 RX_RING_SIZE * RX_BUFFER_SIZE,
705 bp->rx_buffers, bp->rx_buffers_dma);
706 bp->rx_buffers = NULL;
707 }
708}
709
710static int macb_alloc_consistent(struct macb *bp)
711{
712 int size;
713
714 size = TX_RING_SIZE * sizeof(struct ring_info);
715 bp->tx_skb = kmalloc(size, GFP_KERNEL);
716 if (!bp->tx_skb)
717 goto out_err;
718
719 size = RX_RING_BYTES;
720 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
721 &bp->rx_ring_dma, GFP_KERNEL);
722 if (!bp->rx_ring)
723 goto out_err;
724 dev_dbg(&bp->pdev->dev,
725 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
726 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
727
728 size = TX_RING_BYTES;
729 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
730 &bp->tx_ring_dma, GFP_KERNEL);
731 if (!bp->tx_ring)
732 goto out_err;
733 dev_dbg(&bp->pdev->dev,
734 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
735 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
736
737 size = RX_RING_SIZE * RX_BUFFER_SIZE;
738 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
739 &bp->rx_buffers_dma, GFP_KERNEL);
740 if (!bp->rx_buffers)
741 goto out_err;
742 dev_dbg(&bp->pdev->dev,
743 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
744 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
745
746 return 0;
747
748out_err:
749 macb_free_consistent(bp);
750 return -ENOMEM;
751}
752
753static void macb_init_rings(struct macb *bp)
754{
755 int i;
756 dma_addr_t addr;
757
758 addr = bp->rx_buffers_dma;
759 for (i = 0; i < RX_RING_SIZE; i++) {
760 bp->rx_ring[i].addr = addr;
761 bp->rx_ring[i].ctrl = 0;
762 addr += RX_BUFFER_SIZE;
763 }
764 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
765
766 for (i = 0; i < TX_RING_SIZE; i++) {
767 bp->tx_ring[i].addr = 0;
768 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
769 }
770 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
771
772 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
773}
774
775static void macb_reset_hw(struct macb *bp)
776{
777 /* Make sure we have the write buffer for ourselves */
778 wmb();
779
780 /*
781 * Disable RX and TX (XXX: Should we halt the transmission
782 * more gracefully?)
783 */
784 macb_writel(bp, NCR, 0);
785
786 /* Clear the stats registers (XXX: Update stats first?) */
787 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
788
789 /* Clear all status flags */
790 macb_writel(bp, TSR, ~0UL);
791 macb_writel(bp, RSR, ~0UL);
792
793 /* Disable all interrupts */
794 macb_writel(bp, IDR, ~0UL);
795 macb_readl(bp, ISR);
796}
797
798static void macb_init_hw(struct macb *bp)
799{
800 u32 config;
801
802 macb_reset_hw(bp);
803 __macb_set_hwaddr(bp);
804
805 config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
806 config |= MACB_BIT(PAE); /* PAuse Enable */
807 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -0700808 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100809 if (bp->dev->flags & IFF_PROMISC)
810 config |= MACB_BIT(CAF); /* Copy All Frames */
811 if (!(bp->dev->flags & IFF_BROADCAST))
812 config |= MACB_BIT(NBC); /* No BroadCast */
813 macb_writel(bp, NCFGR, config);
814
815 /* Initialize TX and RX buffers */
816 macb_writel(bp, RBQP, bp->rx_ring_dma);
817 macb_writel(bp, TBQP, bp->tx_ring_dma);
818
819 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +0200820 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100821
822 /* Enable interrupts */
823 macb_writel(bp, IER, (MACB_BIT(RCOMP)
824 | MACB_BIT(RXUBR)
825 | MACB_BIT(ISR_TUND)
826 | MACB_BIT(ISR_RLE)
827 | MACB_BIT(TXERR)
828 | MACB_BIT(TCOMP)
829 | MACB_BIT(ISR_ROVR)
830 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100831
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100832}
833
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200834/*
835 * The hash address register is 64 bits long and takes up two
836 * locations in the memory map. The least significant bits are stored
837 * in EMAC_HSL and the most significant bits in EMAC_HSH.
838 *
839 * The unicast hash enable and the multicast hash enable bits in the
840 * network configuration register enable the reception of hash matched
841 * frames. The destination address is reduced to a 6 bit index into
842 * the 64 bit hash register using the following hash function. The
843 * hash function is an exclusive or of every sixth bit of the
844 * destination address.
845 *
846 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
847 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
848 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
849 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
850 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
851 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
852 *
853 * da[0] represents the least significant bit of the first byte
854 * received, that is, the multicast/unicast indicator, and da[47]
855 * represents the most significant bit of the last byte received. If
856 * the hash index, hi[n], points to a bit that is set in the hash
857 * register then the frame will be matched according to whether the
858 * frame is multicast or unicast. A multicast match will be signalled
859 * if the multicast hash enable bit is set, da[0] is 1 and the hash
860 * index points to a bit set in the hash register. A unicast match
861 * will be signalled if the unicast hash enable bit is set, da[0] is 0
862 * and the hash index points to a bit set in the hash register. To
863 * receive all multicast frames, the hash register should be set with
864 * all ones and the multicast hash enable bit should be set in the
865 * network configuration register.
866 */
867
868static inline int hash_bit_value(int bitnr, __u8 *addr)
869{
870 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
871 return 1;
872 return 0;
873}
874
875/*
876 * Return the hash index value for the specified address.
877 */
878static int hash_get_index(__u8 *addr)
879{
880 int i, j, bitval;
881 int hash_index = 0;
882
883 for (j = 0; j < 6; j++) {
884 for (i = 0, bitval = 0; i < 8; i++)
885 bitval ^= hash_bit_value(i*6 + j, addr);
886
887 hash_index |= (bitval << j);
888 }
889
890 return hash_index;
891}
892
893/*
894 * Add multicast addresses to the internal multicast-hash table.
895 */
896static void macb_sethashtable(struct net_device *dev)
897{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000898 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200899 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000900 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200901 struct macb *bp = netdev_priv(dev);
902
903 mc_filter[0] = mc_filter[1] = 0;
904
Jiri Pirko22bedad32010-04-01 21:22:57 +0000905 netdev_for_each_mc_addr(ha, dev) {
906 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200907 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
908 }
909
910 macb_writel(bp, HRB, mc_filter[0]);
911 macb_writel(bp, HRT, mc_filter[1]);
912}
913
914/*
915 * Enable/Disable promiscuous and multicast modes.
916 */
917static void macb_set_rx_mode(struct net_device *dev)
918{
919 unsigned long cfg;
920 struct macb *bp = netdev_priv(dev);
921
922 cfg = macb_readl(bp, NCFGR);
923
924 if (dev->flags & IFF_PROMISC)
925 /* Enable promiscuous mode */
926 cfg |= MACB_BIT(CAF);
927 else if (dev->flags & (~IFF_PROMISC))
928 /* Disable promiscuous mode */
929 cfg &= ~MACB_BIT(CAF);
930
931 if (dev->flags & IFF_ALLMULTI) {
932 /* Enable all multicast mode */
933 macb_writel(bp, HRB, -1);
934 macb_writel(bp, HRT, -1);
935 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000936 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +0200937 /* Enable specific multicasts */
938 macb_sethashtable(dev);
939 cfg |= MACB_BIT(NCFGR_MTI);
940 } else if (dev->flags & (~IFF_ALLMULTI)) {
941 /* Disable all multicast mode */
942 macb_writel(bp, HRB, 0);
943 macb_writel(bp, HRT, 0);
944 cfg &= ~MACB_BIT(NCFGR_MTI);
945 }
946
947 macb_writel(bp, NCFGR, cfg);
948}
949
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100950static int macb_open(struct net_device *dev)
951{
952 struct macb *bp = netdev_priv(dev);
953 int err;
954
955 dev_dbg(&bp->pdev->dev, "open\n");
956
frederic RODO6c36a702007-07-12 19:07:24 +0200957 /* if the phy is not yet register, retry later*/
958 if (!bp->phy_dev)
959 return -EAGAIN;
960
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100961 if (!is_valid_ether_addr(dev->dev_addr))
962 return -EADDRNOTAVAIL;
963
964 err = macb_alloc_consistent(bp);
965 if (err) {
966 printk(KERN_ERR
967 "%s: Unable to allocate DMA memory (error %d)\n",
968 dev->name, err);
969 return err;
970 }
971
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700972 napi_enable(&bp->napi);
973
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100974 macb_init_rings(bp);
975 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100976
frederic RODO6c36a702007-07-12 19:07:24 +0200977 /* schedule a link state check */
978 phy_start(bp->phy_dev);
979
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100980 netif_start_queue(dev);
981
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100982 return 0;
983}
984
985static int macb_close(struct net_device *dev)
986{
987 struct macb *bp = netdev_priv(dev);
988 unsigned long flags;
989
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100990 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700991 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100992
frederic RODO6c36a702007-07-12 19:07:24 +0200993 if (bp->phy_dev)
994 phy_stop(bp->phy_dev);
995
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100996 spin_lock_irqsave(&bp->lock, flags);
997 macb_reset_hw(bp);
998 netif_carrier_off(dev);
999 spin_unlock_irqrestore(&bp->lock, flags);
1000
1001 macb_free_consistent(bp);
1002
1003 return 0;
1004}
1005
1006static struct net_device_stats *macb_get_stats(struct net_device *dev)
1007{
1008 struct macb *bp = netdev_priv(dev);
1009 struct net_device_stats *nstat = &bp->stats;
1010 struct macb_stats *hwstat = &bp->hw_stats;
1011
frederic RODO6c36a702007-07-12 19:07:24 +02001012 /* read stats from hardware */
1013 macb_update_stats(bp);
1014
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001015 /* Convert HW stats into netdevice stats */
1016 nstat->rx_errors = (hwstat->rx_fcs_errors +
1017 hwstat->rx_align_errors +
1018 hwstat->rx_resource_errors +
1019 hwstat->rx_overruns +
1020 hwstat->rx_oversize_pkts +
1021 hwstat->rx_jabbers +
1022 hwstat->rx_undersize_pkts +
1023 hwstat->sqe_test_errors +
1024 hwstat->rx_length_mismatch);
1025 nstat->tx_errors = (hwstat->tx_late_cols +
1026 hwstat->tx_excessive_cols +
1027 hwstat->tx_underruns +
1028 hwstat->tx_carrier_errors);
1029 nstat->collisions = (hwstat->tx_single_cols +
1030 hwstat->tx_multiple_cols +
1031 hwstat->tx_excessive_cols);
1032 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1033 hwstat->rx_jabbers +
1034 hwstat->rx_undersize_pkts +
1035 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001036 nstat->rx_over_errors = hwstat->rx_resource_errors +
1037 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001038 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1039 nstat->rx_frame_errors = hwstat->rx_align_errors;
1040 nstat->rx_fifo_errors = hwstat->rx_overruns;
1041 /* XXX: What does "missed" mean? */
1042 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1043 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1044 nstat->tx_fifo_errors = hwstat->tx_underruns;
1045 /* Don't know about heartbeat or window errors... */
1046
1047 return nstat;
1048}
1049
1050static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1051{
1052 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001053 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001054
frederic RODO6c36a702007-07-12 19:07:24 +02001055 if (!phydev)
1056 return -ENODEV;
1057
1058 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001059}
1060
1061static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1062{
1063 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001064 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001065
frederic RODO6c36a702007-07-12 19:07:24 +02001066 if (!phydev)
1067 return -ENODEV;
1068
1069 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001070}
1071
frederic RODO6c36a702007-07-12 19:07:24 +02001072static void macb_get_drvinfo(struct net_device *dev,
1073 struct ethtool_drvinfo *info)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001074{
1075 struct macb *bp = netdev_priv(dev);
1076
1077 strcpy(info->driver, bp->pdev->dev.driver->name);
1078 strcpy(info->version, "$Revision: 1.14 $");
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08001079 strcpy(info->bus_info, dev_name(&bp->pdev->dev));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001080}
1081
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001082static const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001083 .get_settings = macb_get_settings,
1084 .set_settings = macb_set_settings,
1085 .get_drvinfo = macb_get_drvinfo,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001086 .get_link = ethtool_op_get_link,
1087};
1088
1089static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int 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
1094 if (!netif_running(dev))
1095 return -EINVAL;
1096
frederic RODO6c36a702007-07-12 19:07:24 +02001097 if (!phydev)
1098 return -ENODEV;
1099
Richard Cochran28b04112010-07-17 08:48:55 +00001100 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001101}
1102
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001103static const struct net_device_ops macb_netdev_ops = {
1104 .ndo_open = macb_open,
1105 .ndo_stop = macb_close,
1106 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001107 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001108 .ndo_get_stats = macb_get_stats,
1109 .ndo_do_ioctl = macb_ioctl,
1110 .ndo_validate_addr = eth_validate_addr,
1111 .ndo_change_mtu = eth_change_mtu,
1112 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001113#ifdef CONFIG_NET_POLL_CONTROLLER
1114 .ndo_poll_controller = macb_poll_controller,
1115#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001116};
1117
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001118static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001119{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001120 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001121 struct resource *regs;
1122 struct net_device *dev;
1123 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001124 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001125 unsigned long pclk_hz;
1126 u32 config;
1127 int err = -ENXIO;
1128
1129 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1130 if (!regs) {
1131 dev_err(&pdev->dev, "no mmio resource defined\n");
1132 goto err_out;
1133 }
1134
1135 err = -ENOMEM;
1136 dev = alloc_etherdev(sizeof(*bp));
1137 if (!dev) {
1138 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1139 goto err_out;
1140 }
1141
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001142 SET_NETDEV_DEV(dev, &pdev->dev);
1143
1144 /* TODO: Actually, we have some interesting features... */
1145 dev->features |= 0;
1146
1147 bp = netdev_priv(dev);
1148 bp->pdev = pdev;
1149 bp->dev = dev;
1150
1151 spin_lock_init(&bp->lock);
1152
Jamie Iles461845d2011-03-08 20:19:23 +00001153 bp->pclk = clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001154 if (IS_ERR(bp->pclk)) {
1155 dev_err(&pdev->dev, "failed to get macb_clk\n");
1156 goto err_out_free_dev;
1157 }
1158 clk_enable(bp->pclk);
Jamie Iles461845d2011-03-08 20:19:23 +00001159
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001160 bp->hclk = clk_get(&pdev->dev, "hclk");
1161 if (IS_ERR(bp->hclk)) {
1162 dev_err(&pdev->dev, "failed to get hclk\n");
1163 goto err_out_put_pclk;
1164 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001165 clk_enable(bp->hclk);
1166
Joe Perches28f65c112011-06-09 09:13:32 -07001167 bp->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001168 if (!bp->regs) {
1169 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1170 err = -ENOMEM;
1171 goto err_out_disable_clocks;
1172 }
1173
1174 dev->irq = platform_get_irq(pdev, 0);
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00001175 err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001176 if (err) {
1177 printk(KERN_ERR
1178 "%s: Unable to request IRQ %d (error %d)\n",
1179 dev->name, dev->irq, err);
1180 goto err_out_iounmap;
1181 }
1182
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001183 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001184 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001185 dev->ethtool_ops = &macb_ethtool_ops;
1186
1187 dev->base_addr = regs->start;
1188
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001189 /* Set MII management clock divider */
1190 pclk_hz = clk_get_rate(bp->pclk);
1191 if (pclk_hz <= 20000000)
1192 config = MACB_BF(CLK, MACB_CLK_DIV8);
1193 else if (pclk_hz <= 40000000)
1194 config = MACB_BF(CLK, MACB_CLK_DIV16);
1195 else if (pclk_hz <= 80000000)
1196 config = MACB_BF(CLK, MACB_CLK_DIV32);
1197 else
1198 config = MACB_BF(CLK, MACB_CLK_DIV64);
1199 macb_writel(bp, NCFGR, config);
1200
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001201 macb_get_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001202 pdata = pdev->dev.platform_data;
frederic RODO6c36a702007-07-12 19:07:24 +02001203
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001204 if (pdata && pdata->is_rmii)
Andrew Victor0cc86742007-02-07 16:40:44 +01001205#if defined(CONFIG_ARCH_AT91)
1206 macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
1207#else
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001208 macb_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001209#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001210 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001211#if defined(CONFIG_ARCH_AT91)
1212 macb_writel(bp, USRIO, MACB_BIT(CLKEN));
1213#else
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214 macb_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001215#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001216
1217 bp->tx_pending = DEF_TX_RING_PENDING;
1218
1219 err = register_netdev(dev);
1220 if (err) {
1221 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1222 goto err_out_free_irq;
1223 }
1224
frederic RODO6c36a702007-07-12 19:07:24 +02001225 if (macb_mii_init(bp) != 0) {
1226 goto err_out_unregister_netdev;
1227 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001228
frederic RODO6c36a702007-07-12 19:07:24 +02001229 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001230
Johannes Berge1749612008-10-27 15:59:26 -07001231 printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d (%pM)\n",
1232 dev->name, dev->base_addr, dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001233
frederic RODO6c36a702007-07-12 19:07:24 +02001234 phydev = bp->phy_dev;
1235 printk(KERN_INFO "%s: attached PHY driver [%s] "
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08001236 "(mii_bus:phy_addr=%s, irq=%d)\n", dev->name,
1237 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02001238
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001239 return 0;
1240
frederic RODO6c36a702007-07-12 19:07:24 +02001241err_out_unregister_netdev:
1242 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001243err_out_free_irq:
1244 free_irq(dev->irq, dev);
1245err_out_iounmap:
1246 iounmap(bp->regs);
1247err_out_disable_clocks:
1248 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001249 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001250 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001251err_out_put_pclk:
1252 clk_put(bp->pclk);
1253err_out_free_dev:
1254 free_netdev(dev);
1255err_out:
1256 platform_set_drvdata(pdev, NULL);
1257 return err;
1258}
1259
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001260static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001261{
1262 struct net_device *dev;
1263 struct macb *bp;
1264
1265 dev = platform_get_drvdata(pdev);
1266
1267 if (dev) {
1268 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09001269 if (bp->phy_dev)
1270 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001271 mdiobus_unregister(bp->mii_bus);
1272 kfree(bp->mii_bus->irq);
1273 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001274 unregister_netdev(dev);
1275 free_irq(dev->irq, dev);
1276 iounmap(bp->regs);
1277 clk_disable(bp->hclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001278 clk_put(bp->hclk);
Andrew Victor0cc86742007-02-07 16:40:44 +01001279 clk_disable(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001280 clk_put(bp->pclk);
1281 free_netdev(dev);
1282 platform_set_drvdata(pdev, NULL);
1283 }
1284
1285 return 0;
1286}
1287
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001288#ifdef CONFIG_PM
1289static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1290{
1291 struct net_device *netdev = platform_get_drvdata(pdev);
1292 struct macb *bp = netdev_priv(netdev);
1293
1294 netif_device_detach(netdev);
1295
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001296 clk_disable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001297 clk_disable(bp->pclk);
1298
1299 return 0;
1300}
1301
1302static int macb_resume(struct platform_device *pdev)
1303{
1304 struct net_device *netdev = platform_get_drvdata(pdev);
1305 struct macb *bp = netdev_priv(netdev);
1306
1307 clk_enable(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001308 clk_enable(bp->hclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001309
1310 netif_device_attach(netdev);
1311
1312 return 0;
1313}
1314#else
1315#define macb_suspend NULL
1316#define macb_resume NULL
1317#endif
1318
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001319static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001320 .remove = __exit_p(macb_remove),
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01001321 .suspend = macb_suspend,
1322 .resume = macb_resume,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001323 .driver = {
1324 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07001325 .owner = THIS_MODULE,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001326 },
1327};
1328
1329static int __init macb_init(void)
1330{
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001331 return platform_driver_probe(&macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001332}
1333
1334static void __exit macb_exit(void)
1335{
1336 platform_driver_unregister(&macb_driver);
1337}
1338
1339module_init(macb_init);
1340module_exit(macb_exit);
1341
1342MODULE_LICENSE("GPL");
1343MODULE_DESCRIPTION("Atmel MACB Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001344MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07001345MODULE_ALIAS("platform:macb");