]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/macb.c
ipv6: icmp6_dst_gc return change
[net-next-2.6.git] / drivers / net / macb.c
CommitLineData
89e5785f
HS
1/*
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>
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
89e5785f 20#include <linux/dma-mapping.h>
89e5785f 21#include <linux/platform_device.h>
6c36a707 22#include <linux/phy.h>
89e5785f
HS
23
24#include <asm/arch/board.h>
6c36a707 25#include <asm/arch/cpu.h>
89e5785f
HS
26
27#include "macb.h"
28
89e5785f
HS
29#define RX_BUFFER_SIZE 128
30#define RX_RING_SIZE 512
31#define RX_RING_BYTES (sizeof(struct dma_desc) * RX_RING_SIZE)
32
33/* Make the IP header word-aligned (the ethernet header is 14 bytes) */
34#define RX_OFFSET 2
35
36#define TX_RING_SIZE 128
37#define DEF_TX_RING_PENDING (TX_RING_SIZE - 1)
38#define TX_RING_BYTES (sizeof(struct dma_desc) * TX_RING_SIZE)
39
40#define TX_RING_GAP(bp) \
41 (TX_RING_SIZE - (bp)->tx_pending)
42#define TX_BUFFS_AVAIL(bp) \
43 (((bp)->tx_tail <= (bp)->tx_head) ? \
44 (bp)->tx_tail + (bp)->tx_pending - (bp)->tx_head : \
45 (bp)->tx_tail - (bp)->tx_head - TX_RING_GAP(bp))
46#define NEXT_TX(n) (((n) + 1) & (TX_RING_SIZE - 1))
47
48#define NEXT_RX(n) (((n) + 1) & (RX_RING_SIZE - 1))
49
50/* minimum number of free TX descriptors before waking up TX process */
51#define MACB_TX_WAKEUP_THRESH (TX_RING_SIZE / 4)
52
53#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
54 | MACB_BIT(ISR_ROVR))
55
56static void __macb_set_hwaddr(struct macb *bp)
57{
58 u32 bottom;
59 u16 top;
60
61 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
62 macb_writel(bp, SA1B, bottom);
63 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
64 macb_writel(bp, SA1T, top);
65}
66
67static void __init macb_get_hwaddr(struct macb *bp)
68{
69 u32 bottom;
70 u16 top;
71 u8 addr[6];
72
73 bottom = macb_readl(bp, SA1B);
74 top = macb_readl(bp, SA1T);
75
76 addr[0] = bottom & 0xff;
77 addr[1] = (bottom >> 8) & 0xff;
78 addr[2] = (bottom >> 16) & 0xff;
79 addr[3] = (bottom >> 24) & 0xff;
80 addr[4] = top & 0xff;
81 addr[5] = (top >> 8) & 0xff;
82
d1d5741d 83 if (is_valid_ether_addr(addr)) {
89e5785f 84 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
d1d5741d
SS
85 } else {
86 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
87 random_ether_addr(bp->dev->dev_addr);
88 }
89e5785f
HS
89}
90
6c36a707 91static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
89e5785f 92{
6c36a707 93 struct macb *bp = bus->priv;
89e5785f
HS
94 int value;
95
89e5785f
HS
96 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
97 | MACB_BF(RW, MACB_MAN_READ)
6c36a707
R
98 | MACB_BF(PHYA, mii_id)
99 | MACB_BF(REGA, regnum)
89e5785f
HS
100 | MACB_BF(CODE, MACB_MAN_CODE)));
101
6c36a707
R
102 /* wait for end of transfer */
103 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
104 cpu_relax();
89e5785f
HS
105
106 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
89e5785f
HS
107
108 return value;
109}
110
6c36a707
R
111static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
112 u16 value)
89e5785f 113{
6c36a707 114 struct macb *bp = bus->priv;
89e5785f
HS
115
116 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
117 | MACB_BF(RW, MACB_MAN_WRITE)
6c36a707
R
118 | MACB_BF(PHYA, mii_id)
119 | MACB_BF(REGA, regnum)
89e5785f 120 | MACB_BF(CODE, MACB_MAN_CODE)
6c36a707 121 | MACB_BF(DATA, value)));
89e5785f 122
6c36a707
R
123 /* wait for end of transfer */
124 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
125 cpu_relax();
126
127 return 0;
128}
89e5785f 129
6c36a707
R
130static int macb_mdio_reset(struct mii_bus *bus)
131{
132 return 0;
89e5785f
HS
133}
134
6c36a707 135static void macb_handle_link_change(struct net_device *dev)
89e5785f 136{
6c36a707
R
137 struct macb *bp = netdev_priv(dev);
138 struct phy_device *phydev = bp->phy_dev;
139 unsigned long flags;
89e5785f 140
6c36a707 141 int status_change = 0;
89e5785f 142
6c36a707
R
143 spin_lock_irqsave(&bp->lock, flags);
144
145 if (phydev->link) {
146 if ((bp->speed != phydev->speed) ||
147 (bp->duplex != phydev->duplex)) {
148 u32 reg;
149
150 reg = macb_readl(bp, NCFGR);
151 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
152
153 if (phydev->duplex)
154 reg |= MACB_BIT(FD);
179956f4 155 if (phydev->speed == SPEED_100)
6c36a707
R
156 reg |= MACB_BIT(SPD);
157
158 macb_writel(bp, NCFGR, reg);
159
160 bp->speed = phydev->speed;
161 bp->duplex = phydev->duplex;
162 status_change = 1;
163 }
89e5785f
HS
164 }
165
6c36a707
R
166 if (phydev->link != bp->link) {
167 if (phydev->link)
263ba320 168 netif_tx_schedule_all(dev);
6c36a707
R
169 else {
170 bp->speed = 0;
171 bp->duplex = -1;
172 }
173 bp->link = phydev->link;
89e5785f 174
6c36a707
R
175 status_change = 1;
176 }
89e5785f 177
6c36a707
R
178 spin_unlock_irqrestore(&bp->lock, flags);
179
180 if (status_change) {
181 if (phydev->link)
182 printk(KERN_INFO "%s: link up (%d/%s)\n",
183 dev->name, phydev->speed,
184 DUPLEX_FULL == phydev->duplex ? "Full":"Half");
185 else
186 printk(KERN_INFO "%s: link down\n", dev->name);
187 }
89e5785f
HS
188}
189
6c36a707
R
190/* based on au1000_eth. c*/
191static int macb_mii_probe(struct net_device *dev)
89e5785f 192{
6c36a707
R
193 struct macb *bp = netdev_priv(dev);
194 struct phy_device *phydev = NULL;
195 struct eth_platform_data *pdata;
196 int phy_addr;
89e5785f 197
6c36a707
R
198 /* find the first phy */
199 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
200 if (bp->mii_bus.phy_map[phy_addr]) {
201 phydev = bp->mii_bus.phy_map[phy_addr];
202 break;
203 }
204 }
205
206 if (!phydev) {
207 printk (KERN_ERR "%s: no PHY found\n", dev->name);
208 return -1;
209 }
210
211 pdata = bp->pdev->dev.platform_data;
212 /* TODO : add pin_irq */
213
214 /* attach the mac to the phy */
215 if (pdata && pdata->is_rmii) {
216 phydev = phy_connect(dev, phydev->dev.bus_id,
217 &macb_handle_link_change, 0, PHY_INTERFACE_MODE_RMII);
218 } else {
219 phydev = phy_connect(dev, phydev->dev.bus_id,
220 &macb_handle_link_change, 0, PHY_INTERFACE_MODE_MII);
221 }
222
223 if (IS_ERR(phydev)) {
224 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
225 return PTR_ERR(phydev);
226 }
227
228 /* mask with MAC supported features */
229 phydev->supported &= PHY_BASIC_FEATURES;
230
231 phydev->advertising = phydev->supported;
232
233 bp->link = 0;
234 bp->speed = 0;
235 bp->duplex = -1;
236 bp->phy_dev = phydev;
237
238 return 0;
89e5785f
HS
239}
240
6c36a707 241static int macb_mii_init(struct macb *bp)
89e5785f 242{
6c36a707
R
243 struct eth_platform_data *pdata;
244 int err = -ENXIO, i;
89e5785f 245
6c36a707
R
246 /* Enable managment port */
247 macb_writel(bp, NCR, MACB_BIT(MPE));
89e5785f 248
72cfe922
AN
249 bp->mii_bus.name = "MACB_mii_bus";
250 bp->mii_bus.read = &macb_mdio_read;
251 bp->mii_bus.write = &macb_mdio_write;
252 bp->mii_bus.reset = &macb_mdio_reset;
9d9326d3 253 snprintf(bp->mii_bus.id, MII_BUS_ID_SIZE, "%x", bp->pdev->id);
72cfe922 254 bp->mii_bus.priv = bp;
6c36a707
R
255 bp->mii_bus.dev = &bp->dev->dev;
256 pdata = bp->pdev->dev.platform_data;
89e5785f 257
6c36a707
R
258 if (pdata)
259 bp->mii_bus.phy_mask = pdata->phy_mask;
89e5785f 260
6c36a707
R
261 bp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
262 if (!bp->mii_bus.irq) {
263 err = -ENOMEM;
264 goto err_out;
89e5785f
HS
265 }
266
6c36a707
R
267 for (i = 0; i < PHY_MAX_ADDR; i++)
268 bp->mii_bus.irq[i] = PHY_POLL;
89e5785f 269
6c36a707 270 platform_set_drvdata(bp->dev, &bp->mii_bus);
89e5785f 271
6c36a707
R
272 if (mdiobus_register(&bp->mii_bus))
273 goto err_out_free_mdio_irq;
89e5785f 274
6c36a707
R
275 if (macb_mii_probe(bp->dev) != 0) {
276 goto err_out_unregister_bus;
277 }
89e5785f 278
6c36a707 279 return 0;
89e5785f 280
6c36a707
R
281err_out_unregister_bus:
282 mdiobus_unregister(&bp->mii_bus);
283err_out_free_mdio_irq:
284 kfree(bp->mii_bus.irq);
285err_out:
286 return err;
89e5785f
HS
287}
288
289static void macb_update_stats(struct macb *bp)
290{
291 u32 __iomem *reg = bp->regs + MACB_PFR;
292 u32 *p = &bp->hw_stats.rx_pause_frames;
293 u32 *end = &bp->hw_stats.tx_pause_frames + 1;
294
295 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
296
297 for(; p < end; p++, reg++)
0f0d84e5 298 *p += __raw_readl(reg);
89e5785f
HS
299}
300
89e5785f
HS
301static void macb_tx(struct macb *bp)
302{
303 unsigned int tail;
304 unsigned int head;
305 u32 status;
306
307 status = macb_readl(bp, TSR);
308 macb_writel(bp, TSR, status);
309
310 dev_dbg(&bp->pdev->dev, "macb_tx status = %02lx\n",
311 (unsigned long)status);
312
313 if (status & MACB_BIT(UND)) {
bdcba151 314 int i;
89e5785f 315 printk(KERN_ERR "%s: TX underrun, resetting buffers\n",
bdcba151
GC
316 bp->dev->name);
317
318 head = bp->tx_head;
319
320 /*Mark all the buffer as used to avoid sending a lost buffer*/
321 for (i = 0; i < TX_RING_SIZE; i++)
322 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
323
324 /* free transmit buffer in upper layer*/
325 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
326 struct ring_info *rp = &bp->tx_skb[tail];
327 struct sk_buff *skb = rp->skb;
328
329 BUG_ON(skb == NULL);
330
331 rmb();
332
333 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
334 DMA_TO_DEVICE);
335 rp->skb = NULL;
336 dev_kfree_skb_irq(skb);
337 }
338
89e5785f
HS
339 bp->tx_head = bp->tx_tail = 0;
340 }
341
342 if (!(status & MACB_BIT(COMP)))
343 /*
344 * This may happen when a buffer becomes complete
345 * between reading the ISR and scanning the
346 * descriptors. Nothing to worry about.
347 */
348 return;
349
350 head = bp->tx_head;
351 for (tail = bp->tx_tail; tail != head; tail = NEXT_TX(tail)) {
352 struct ring_info *rp = &bp->tx_skb[tail];
353 struct sk_buff *skb = rp->skb;
354 u32 bufstat;
355
356 BUG_ON(skb == NULL);
357
358 rmb();
359 bufstat = bp->tx_ring[tail].ctrl;
360
361 if (!(bufstat & MACB_BIT(TX_USED)))
362 break;
363
364 dev_dbg(&bp->pdev->dev, "skb %u (data %p) TX complete\n",
365 tail, skb->data);
366 dma_unmap_single(&bp->pdev->dev, rp->mapping, skb->len,
367 DMA_TO_DEVICE);
368 bp->stats.tx_packets++;
369 bp->stats.tx_bytes += skb->len;
370 rp->skb = NULL;
371 dev_kfree_skb_irq(skb);
372 }
373
374 bp->tx_tail = tail;
375 if (netif_queue_stopped(bp->dev) &&
376 TX_BUFFS_AVAIL(bp) > MACB_TX_WAKEUP_THRESH)
377 netif_wake_queue(bp->dev);
378}
379
380static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
381 unsigned int last_frag)
382{
383 unsigned int len;
384 unsigned int frag;
385 unsigned int offset = 0;
386 struct sk_buff *skb;
387
388 len = MACB_BFEXT(RX_FRMLEN, bp->rx_ring[last_frag].ctrl);
389
390 dev_dbg(&bp->pdev->dev, "macb_rx_frame frags %u - %u (len %u)\n",
391 first_frag, last_frag, len);
392
393 skb = dev_alloc_skb(len + RX_OFFSET);
394 if (!skb) {
395 bp->stats.rx_dropped++;
396 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
397 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
398 if (frag == last_frag)
399 break;
400 }
401 wmb();
402 return 1;
403 }
404
405 skb_reserve(skb, RX_OFFSET);
89e5785f
HS
406 skb->ip_summed = CHECKSUM_NONE;
407 skb_put(skb, len);
408
409 for (frag = first_frag; ; frag = NEXT_RX(frag)) {
410 unsigned int frag_len = RX_BUFFER_SIZE;
411
412 if (offset + frag_len > len) {
413 BUG_ON(frag != last_frag);
414 frag_len = len - offset;
415 }
27d7ff46
ACM
416 skb_copy_to_linear_data_offset(skb, offset,
417 (bp->rx_buffers +
418 (RX_BUFFER_SIZE * frag)),
419 frag_len);
89e5785f
HS
420 offset += RX_BUFFER_SIZE;
421 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
422 wmb();
423
424 if (frag == last_frag)
425 break;
426 }
427
428 skb->protocol = eth_type_trans(skb, bp->dev);
429
430 bp->stats.rx_packets++;
431 bp->stats.rx_bytes += len;
432 bp->dev->last_rx = jiffies;
433 dev_dbg(&bp->pdev->dev, "received skb of length %u, csum: %08x\n",
434 skb->len, skb->csum);
435 netif_receive_skb(skb);
436
437 return 0;
438}
439
440/* Mark DMA descriptors from begin up to and not including end as unused */
441static void discard_partial_frame(struct macb *bp, unsigned int begin,
442 unsigned int end)
443{
444 unsigned int frag;
445
446 for (frag = begin; frag != end; frag = NEXT_RX(frag))
447 bp->rx_ring[frag].addr &= ~MACB_BIT(RX_USED);
448 wmb();
449
450 /*
451 * When this happens, the hardware stats registers for
452 * whatever caused this is updated, so we don't have to record
453 * anything.
454 */
455}
456
457static int macb_rx(struct macb *bp, int budget)
458{
459 int received = 0;
460 unsigned int tail = bp->rx_tail;
461 int first_frag = -1;
462
463 for (; budget > 0; tail = NEXT_RX(tail)) {
464 u32 addr, ctrl;
465
466 rmb();
467 addr = bp->rx_ring[tail].addr;
468 ctrl = bp->rx_ring[tail].ctrl;
469
470 if (!(addr & MACB_BIT(RX_USED)))
471 break;
472
473 if (ctrl & MACB_BIT(RX_SOF)) {
474 if (first_frag != -1)
475 discard_partial_frame(bp, first_frag, tail);
476 first_frag = tail;
477 }
478
479 if (ctrl & MACB_BIT(RX_EOF)) {
480 int dropped;
481 BUG_ON(first_frag == -1);
482
483 dropped = macb_rx_frame(bp, first_frag, tail);
484 first_frag = -1;
485 if (!dropped) {
486 received++;
487 budget--;
488 }
489 }
490 }
491
492 if (first_frag != -1)
493 bp->rx_tail = first_frag;
494 else
495 bp->rx_tail = tail;
496
497 return received;
498}
499
bea3348e 500static int macb_poll(struct napi_struct *napi, int budget)
89e5785f 501{
bea3348e
SH
502 struct macb *bp = container_of(napi, struct macb, napi);
503 struct net_device *dev = bp->dev;
504 int work_done;
89e5785f
HS
505 u32 status;
506
507 status = macb_readl(bp, RSR);
508 macb_writel(bp, RSR, status);
509
bea3348e 510 work_done = 0;
89e5785f
HS
511 if (!status) {
512 /*
513 * This may happen if an interrupt was pending before
514 * this function was called last time, and no packets
515 * have been received since.
516 */
bea3348e 517 netif_rx_complete(dev, napi);
89e5785f
HS
518 goto out;
519 }
520
521 dev_dbg(&bp->pdev->dev, "poll: status = %08lx, budget = %d\n",
bea3348e 522 (unsigned long)status, budget);
89e5785f
HS
523
524 if (!(status & MACB_BIT(REC))) {
525 dev_warn(&bp->pdev->dev,
526 "No RX buffers complete, status = %02lx\n",
527 (unsigned long)status);
bea3348e 528 netif_rx_complete(dev, napi);
89e5785f
HS
529 goto out;
530 }
531
bea3348e
SH
532 work_done = macb_rx(bp, budget);
533 if (work_done < budget)
534 netif_rx_complete(dev, napi);
89e5785f
HS
535
536 /*
537 * We've done what we can to clean the buffers. Make sure we
538 * get notified when new packets arrive.
539 */
540out:
541 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
542
543 /* TODO: Handle errors */
544
bea3348e 545 return work_done;
89e5785f
HS
546}
547
548static irqreturn_t macb_interrupt(int irq, void *dev_id)
549{
550 struct net_device *dev = dev_id;
551 struct macb *bp = netdev_priv(dev);
552 u32 status;
553
554 status = macb_readl(bp, ISR);
555
556 if (unlikely(!status))
557 return IRQ_NONE;
558
559 spin_lock(&bp->lock);
560
561 while (status) {
89e5785f
HS
562 /* close possible race with dev_close */
563 if (unlikely(!netif_running(dev))) {
564 macb_writel(bp, IDR, ~0UL);
565 break;
566 }
567
568 if (status & MACB_RX_INT_FLAGS) {
bea3348e 569 if (netif_rx_schedule_prep(dev, &bp->napi)) {
89e5785f
HS
570 /*
571 * There's no point taking any more interrupts
572 * until we have processed the buffers
573 */
574 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
6c36a707
R
575 dev_dbg(&bp->pdev->dev,
576 "scheduling RX softirq\n");
bea3348e 577 __netif_rx_schedule(dev, &bp->napi);
89e5785f
HS
578 }
579 }
580
581 if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND)))
582 macb_tx(bp);
583
584 /*
585 * Link change detection isn't possible with RMII, so we'll
586 * add that if/when we get our hands on a full-blown MII PHY.
587 */
588
589 if (status & MACB_BIT(HRESP)) {
590 /*
591 * TODO: Reset the hardware, and maybe move the printk
592 * to a lower-priority context as well (work queue?)
593 */
594 printk(KERN_ERR "%s: DMA bus error: HRESP not OK\n",
595 dev->name);
596 }
597
598 status = macb_readl(bp, ISR);
599 }
600
601 spin_unlock(&bp->lock);
602
603 return IRQ_HANDLED;
604}
605
606static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
607{
608 struct macb *bp = netdev_priv(dev);
609 dma_addr_t mapping;
610 unsigned int len, entry;
611 u32 ctrl;
612
613#ifdef DEBUG
614 int i;
615 dev_dbg(&bp->pdev->dev,
616 "start_xmit: len %u head %p data %p tail %p end %p\n",
27a884dc 617 skb->len, skb->head, skb->data,
4305b541 618 skb_tail_pointer(skb), skb_end_pointer(skb));
89e5785f
HS
619 dev_dbg(&bp->pdev->dev,
620 "data:");
621 for (i = 0; i < 16; i++)
622 printk(" %02x", (unsigned int)skb->data[i]);
623 printk("\n");
624#endif
625
626 len = skb->len;
627 spin_lock_irq(&bp->lock);
628
629 /* This is a hard error, log it. */
630 if (TX_BUFFS_AVAIL(bp) < 1) {
631 netif_stop_queue(dev);
632 spin_unlock_irq(&bp->lock);
633 dev_err(&bp->pdev->dev,
634 "BUG! Tx Ring full when queue awake!\n");
635 dev_dbg(&bp->pdev->dev, "tx_head = %u, tx_tail = %u\n",
636 bp->tx_head, bp->tx_tail);
637 return 1;
638 }
639
640 entry = bp->tx_head;
641 dev_dbg(&bp->pdev->dev, "Allocated ring entry %u\n", entry);
642 mapping = dma_map_single(&bp->pdev->dev, skb->data,
643 len, DMA_TO_DEVICE);
644 bp->tx_skb[entry].skb = skb;
645 bp->tx_skb[entry].mapping = mapping;
646 dev_dbg(&bp->pdev->dev, "Mapped skb data %p to DMA addr %08lx\n",
647 skb->data, (unsigned long)mapping);
648
649 ctrl = MACB_BF(TX_FRMLEN, len);
650 ctrl |= MACB_BIT(TX_LAST);
651 if (entry == (TX_RING_SIZE - 1))
652 ctrl |= MACB_BIT(TX_WRAP);
653
654 bp->tx_ring[entry].addr = mapping;
655 bp->tx_ring[entry].ctrl = ctrl;
656 wmb();
657
658 entry = NEXT_TX(entry);
659 bp->tx_head = entry;
660
661 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
662
663 if (TX_BUFFS_AVAIL(bp) < 1)
664 netif_stop_queue(dev);
665
666 spin_unlock_irq(&bp->lock);
667
668 dev->trans_start = jiffies;
669
670 return 0;
671}
672
673static void macb_free_consistent(struct macb *bp)
674{
675 if (bp->tx_skb) {
676 kfree(bp->tx_skb);
677 bp->tx_skb = NULL;
678 }
679 if (bp->rx_ring) {
680 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
681 bp->rx_ring, bp->rx_ring_dma);
682 bp->rx_ring = NULL;
683 }
684 if (bp->tx_ring) {
685 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
686 bp->tx_ring, bp->tx_ring_dma);
687 bp->tx_ring = NULL;
688 }
689 if (bp->rx_buffers) {
690 dma_free_coherent(&bp->pdev->dev,
691 RX_RING_SIZE * RX_BUFFER_SIZE,
692 bp->rx_buffers, bp->rx_buffers_dma);
693 bp->rx_buffers = NULL;
694 }
695}
696
697static int macb_alloc_consistent(struct macb *bp)
698{
699 int size;
700
701 size = TX_RING_SIZE * sizeof(struct ring_info);
702 bp->tx_skb = kmalloc(size, GFP_KERNEL);
703 if (!bp->tx_skb)
704 goto out_err;
705
706 size = RX_RING_BYTES;
707 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
708 &bp->rx_ring_dma, GFP_KERNEL);
709 if (!bp->rx_ring)
710 goto out_err;
711 dev_dbg(&bp->pdev->dev,
712 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
713 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
714
715 size = TX_RING_BYTES;
716 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
717 &bp->tx_ring_dma, GFP_KERNEL);
718 if (!bp->tx_ring)
719 goto out_err;
720 dev_dbg(&bp->pdev->dev,
721 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
722 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
723
724 size = RX_RING_SIZE * RX_BUFFER_SIZE;
725 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
726 &bp->rx_buffers_dma, GFP_KERNEL);
727 if (!bp->rx_buffers)
728 goto out_err;
729 dev_dbg(&bp->pdev->dev,
730 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
731 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
732
733 return 0;
734
735out_err:
736 macb_free_consistent(bp);
737 return -ENOMEM;
738}
739
740static void macb_init_rings(struct macb *bp)
741{
742 int i;
743 dma_addr_t addr;
744
745 addr = bp->rx_buffers_dma;
746 for (i = 0; i < RX_RING_SIZE; i++) {
747 bp->rx_ring[i].addr = addr;
748 bp->rx_ring[i].ctrl = 0;
749 addr += RX_BUFFER_SIZE;
750 }
751 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
752
753 for (i = 0; i < TX_RING_SIZE; i++) {
754 bp->tx_ring[i].addr = 0;
755 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
756 }
757 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
758
759 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
760}
761
762static void macb_reset_hw(struct macb *bp)
763{
764 /* Make sure we have the write buffer for ourselves */
765 wmb();
766
767 /*
768 * Disable RX and TX (XXX: Should we halt the transmission
769 * more gracefully?)
770 */
771 macb_writel(bp, NCR, 0);
772
773 /* Clear the stats registers (XXX: Update stats first?) */
774 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
775
776 /* Clear all status flags */
777 macb_writel(bp, TSR, ~0UL);
778 macb_writel(bp, RSR, ~0UL);
779
780 /* Disable all interrupts */
781 macb_writel(bp, IDR, ~0UL);
782 macb_readl(bp, ISR);
783}
784
785static void macb_init_hw(struct macb *bp)
786{
787 u32 config;
788
789 macb_reset_hw(bp);
790 __macb_set_hwaddr(bp);
791
792 config = macb_readl(bp, NCFGR) & MACB_BF(CLK, -1L);
793 config |= MACB_BIT(PAE); /* PAuse Enable */
794 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
795 if (bp->dev->flags & IFF_PROMISC)
796 config |= MACB_BIT(CAF); /* Copy All Frames */
797 if (!(bp->dev->flags & IFF_BROADCAST))
798 config |= MACB_BIT(NBC); /* No BroadCast */
799 macb_writel(bp, NCFGR, config);
800
801 /* Initialize TX and RX buffers */
802 macb_writel(bp, RBQP, bp->rx_ring_dma);
803 macb_writel(bp, TBQP, bp->tx_ring_dma);
804
805 /* Enable TX and RX */
6c36a707 806 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
89e5785f
HS
807
808 /* Enable interrupts */
809 macb_writel(bp, IER, (MACB_BIT(RCOMP)
810 | MACB_BIT(RXUBR)
811 | MACB_BIT(ISR_TUND)
812 | MACB_BIT(ISR_RLE)
813 | MACB_BIT(TXERR)
814 | MACB_BIT(TCOMP)
815 | MACB_BIT(ISR_ROVR)
816 | MACB_BIT(HRESP)));
89e5785f 817
89e5785f
HS
818}
819
446ebd01
PV
820/*
821 * The hash address register is 64 bits long and takes up two
822 * locations in the memory map. The least significant bits are stored
823 * in EMAC_HSL and the most significant bits in EMAC_HSH.
824 *
825 * The unicast hash enable and the multicast hash enable bits in the
826 * network configuration register enable the reception of hash matched
827 * frames. The destination address is reduced to a 6 bit index into
828 * the 64 bit hash register using the following hash function. The
829 * hash function is an exclusive or of every sixth bit of the
830 * destination address.
831 *
832 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
833 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
834 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
835 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
836 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
837 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
838 *
839 * da[0] represents the least significant bit of the first byte
840 * received, that is, the multicast/unicast indicator, and da[47]
841 * represents the most significant bit of the last byte received. If
842 * the hash index, hi[n], points to a bit that is set in the hash
843 * register then the frame will be matched according to whether the
844 * frame is multicast or unicast. A multicast match will be signalled
845 * if the multicast hash enable bit is set, da[0] is 1 and the hash
846 * index points to a bit set in the hash register. A unicast match
847 * will be signalled if the unicast hash enable bit is set, da[0] is 0
848 * and the hash index points to a bit set in the hash register. To
849 * receive all multicast frames, the hash register should be set with
850 * all ones and the multicast hash enable bit should be set in the
851 * network configuration register.
852 */
853
854static inline int hash_bit_value(int bitnr, __u8 *addr)
855{
856 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
857 return 1;
858 return 0;
859}
860
861/*
862 * Return the hash index value for the specified address.
863 */
864static int hash_get_index(__u8 *addr)
865{
866 int i, j, bitval;
867 int hash_index = 0;
868
869 for (j = 0; j < 6; j++) {
870 for (i = 0, bitval = 0; i < 8; i++)
871 bitval ^= hash_bit_value(i*6 + j, addr);
872
873 hash_index |= (bitval << j);
874 }
875
876 return hash_index;
877}
878
879/*
880 * Add multicast addresses to the internal multicast-hash table.
881 */
882static void macb_sethashtable(struct net_device *dev)
883{
884 struct dev_mc_list *curr;
885 unsigned long mc_filter[2];
886 unsigned int i, bitnr;
887 struct macb *bp = netdev_priv(dev);
888
889 mc_filter[0] = mc_filter[1] = 0;
890
891 curr = dev->mc_list;
892 for (i = 0; i < dev->mc_count; i++, curr = curr->next) {
893 if (!curr) break; /* unexpected end of list */
894
895 bitnr = hash_get_index(curr->dmi_addr);
896 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
897 }
898
899 macb_writel(bp, HRB, mc_filter[0]);
900 macb_writel(bp, HRT, mc_filter[1]);
901}
902
903/*
904 * Enable/Disable promiscuous and multicast modes.
905 */
906static void macb_set_rx_mode(struct net_device *dev)
907{
908 unsigned long cfg;
909 struct macb *bp = netdev_priv(dev);
910
911 cfg = macb_readl(bp, NCFGR);
912
913 if (dev->flags & IFF_PROMISC)
914 /* Enable promiscuous mode */
915 cfg |= MACB_BIT(CAF);
916 else if (dev->flags & (~IFF_PROMISC))
917 /* Disable promiscuous mode */
918 cfg &= ~MACB_BIT(CAF);
919
920 if (dev->flags & IFF_ALLMULTI) {
921 /* Enable all multicast mode */
922 macb_writel(bp, HRB, -1);
923 macb_writel(bp, HRT, -1);
924 cfg |= MACB_BIT(NCFGR_MTI);
925 } else if (dev->mc_count > 0) {
926 /* Enable specific multicasts */
927 macb_sethashtable(dev);
928 cfg |= MACB_BIT(NCFGR_MTI);
929 } else if (dev->flags & (~IFF_ALLMULTI)) {
930 /* Disable all multicast mode */
931 macb_writel(bp, HRB, 0);
932 macb_writel(bp, HRT, 0);
933 cfg &= ~MACB_BIT(NCFGR_MTI);
934 }
935
936 macb_writel(bp, NCFGR, cfg);
937}
938
89e5785f
HS
939static int macb_open(struct net_device *dev)
940{
941 struct macb *bp = netdev_priv(dev);
942 int err;
943
944 dev_dbg(&bp->pdev->dev, "open\n");
945
6c36a707
R
946 /* if the phy is not yet register, retry later*/
947 if (!bp->phy_dev)
948 return -EAGAIN;
949
89e5785f
HS
950 if (!is_valid_ether_addr(dev->dev_addr))
951 return -EADDRNOTAVAIL;
952
953 err = macb_alloc_consistent(bp);
954 if (err) {
955 printk(KERN_ERR
956 "%s: Unable to allocate DMA memory (error %d)\n",
957 dev->name, err);
958 return err;
959 }
960
bea3348e
SH
961 napi_enable(&bp->napi);
962
89e5785f
HS
963 macb_init_rings(bp);
964 macb_init_hw(bp);
89e5785f 965
6c36a707
R
966 /* schedule a link state check */
967 phy_start(bp->phy_dev);
89e5785f 968
6c36a707 969 netif_start_queue(dev);
89e5785f
HS
970
971 return 0;
972}
973
974static int macb_close(struct net_device *dev)
975{
976 struct macb *bp = netdev_priv(dev);
977 unsigned long flags;
978
89e5785f 979 netif_stop_queue(dev);
bea3348e 980 napi_disable(&bp->napi);
89e5785f 981
6c36a707
R
982 if (bp->phy_dev)
983 phy_stop(bp->phy_dev);
984
89e5785f
HS
985 spin_lock_irqsave(&bp->lock, flags);
986 macb_reset_hw(bp);
987 netif_carrier_off(dev);
988 spin_unlock_irqrestore(&bp->lock, flags);
989
990 macb_free_consistent(bp);
991
992 return 0;
993}
994
995static struct net_device_stats *macb_get_stats(struct net_device *dev)
996{
997 struct macb *bp = netdev_priv(dev);
998 struct net_device_stats *nstat = &bp->stats;
999 struct macb_stats *hwstat = &bp->hw_stats;
1000
6c36a707
R
1001 /* read stats from hardware */
1002 macb_update_stats(bp);
1003
89e5785f
HS
1004 /* Convert HW stats into netdevice stats */
1005 nstat->rx_errors = (hwstat->rx_fcs_errors +
1006 hwstat->rx_align_errors +
1007 hwstat->rx_resource_errors +
1008 hwstat->rx_overruns +
1009 hwstat->rx_oversize_pkts +
1010 hwstat->rx_jabbers +
1011 hwstat->rx_undersize_pkts +
1012 hwstat->sqe_test_errors +
1013 hwstat->rx_length_mismatch);
1014 nstat->tx_errors = (hwstat->tx_late_cols +
1015 hwstat->tx_excessive_cols +
1016 hwstat->tx_underruns +
1017 hwstat->tx_carrier_errors);
1018 nstat->collisions = (hwstat->tx_single_cols +
1019 hwstat->tx_multiple_cols +
1020 hwstat->tx_excessive_cols);
1021 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1022 hwstat->rx_jabbers +
1023 hwstat->rx_undersize_pkts +
1024 hwstat->rx_length_mismatch);
1025 nstat->rx_over_errors = hwstat->rx_resource_errors;
1026 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1027 nstat->rx_frame_errors = hwstat->rx_align_errors;
1028 nstat->rx_fifo_errors = hwstat->rx_overruns;
1029 /* XXX: What does "missed" mean? */
1030 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1031 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1032 nstat->tx_fifo_errors = hwstat->tx_underruns;
1033 /* Don't know about heartbeat or window errors... */
1034
1035 return nstat;
1036}
1037
1038static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1039{
1040 struct macb *bp = netdev_priv(dev);
6c36a707
R
1041 struct phy_device *phydev = bp->phy_dev;
1042
1043 if (!phydev)
1044 return -ENODEV;
89e5785f 1045
6c36a707 1046 return phy_ethtool_gset(phydev, cmd);
89e5785f
HS
1047}
1048
1049static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1050{
1051 struct macb *bp = netdev_priv(dev);
6c36a707 1052 struct phy_device *phydev = bp->phy_dev;
89e5785f 1053
6c36a707
R
1054 if (!phydev)
1055 return -ENODEV;
1056
1057 return phy_ethtool_sset(phydev, cmd);
89e5785f
HS
1058}
1059
6c36a707
R
1060static void macb_get_drvinfo(struct net_device *dev,
1061 struct ethtool_drvinfo *info)
89e5785f
HS
1062{
1063 struct macb *bp = netdev_priv(dev);
1064
1065 strcpy(info->driver, bp->pdev->dev.driver->name);
1066 strcpy(info->version, "$Revision: 1.14 $");
1067 strcpy(info->bus_info, bp->pdev->dev.bus_id);
1068}
1069
89e5785f
HS
1070static struct ethtool_ops macb_ethtool_ops = {
1071 .get_settings = macb_get_settings,
1072 .set_settings = macb_set_settings,
1073 .get_drvinfo = macb_get_drvinfo,
89e5785f
HS
1074 .get_link = ethtool_op_get_link,
1075};
1076
1077static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1078{
1079 struct macb *bp = netdev_priv(dev);
6c36a707 1080 struct phy_device *phydev = bp->phy_dev;
89e5785f
HS
1081
1082 if (!netif_running(dev))
1083 return -EINVAL;
1084
6c36a707
R
1085 if (!phydev)
1086 return -ENODEV;
89e5785f 1087
6c36a707 1088 return phy_mii_ioctl(phydev, if_mii(rq), cmd);
89e5785f
HS
1089}
1090
06c3fd6a 1091static int __init macb_probe(struct platform_device *pdev)
89e5785f
HS
1092{
1093 struct eth_platform_data *pdata;
1094 struct resource *regs;
1095 struct net_device *dev;
1096 struct macb *bp;
6c36a707 1097 struct phy_device *phydev;
89e5785f
HS
1098 unsigned long pclk_hz;
1099 u32 config;
1100 int err = -ENXIO;
0795af57 1101 DECLARE_MAC_BUF(mac);
89e5785f
HS
1102
1103 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1104 if (!regs) {
1105 dev_err(&pdev->dev, "no mmio resource defined\n");
1106 goto err_out;
1107 }
1108
1109 err = -ENOMEM;
1110 dev = alloc_etherdev(sizeof(*bp));
1111 if (!dev) {
1112 dev_err(&pdev->dev, "etherdev alloc failed, aborting.\n");
1113 goto err_out;
1114 }
1115
89e5785f
HS
1116 SET_NETDEV_DEV(dev, &pdev->dev);
1117
1118 /* TODO: Actually, we have some interesting features... */
1119 dev->features |= 0;
1120
1121 bp = netdev_priv(dev);
1122 bp->pdev = pdev;
1123 bp->dev = dev;
1124
1125 spin_lock_init(&bp->lock);
1126
0cc8674f
AV
1127#if defined(CONFIG_ARCH_AT91)
1128 bp->pclk = clk_get(&pdev->dev, "macb_clk");
1129 if (IS_ERR(bp->pclk)) {
1130 dev_err(&pdev->dev, "failed to get macb_clk\n");
1131 goto err_out_free_dev;
1132 }
1133 clk_enable(bp->pclk);
1134#else
89e5785f
HS
1135 bp->pclk = clk_get(&pdev->dev, "pclk");
1136 if (IS_ERR(bp->pclk)) {
1137 dev_err(&pdev->dev, "failed to get pclk\n");
1138 goto err_out_free_dev;
1139 }
1140 bp->hclk = clk_get(&pdev->dev, "hclk");
1141 if (IS_ERR(bp->hclk)) {
1142 dev_err(&pdev->dev, "failed to get hclk\n");
1143 goto err_out_put_pclk;
1144 }
1145
1146 clk_enable(bp->pclk);
1147 clk_enable(bp->hclk);
0cc8674f 1148#endif
89e5785f
HS
1149
1150 bp->regs = ioremap(regs->start, regs->end - regs->start + 1);
1151 if (!bp->regs) {
1152 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1153 err = -ENOMEM;
1154 goto err_out_disable_clocks;
1155 }
1156
1157 dev->irq = platform_get_irq(pdev, 0);
38515e90 1158 err = request_irq(dev->irq, macb_interrupt, IRQF_SAMPLE_RANDOM,
89e5785f
HS
1159 dev->name, dev);
1160 if (err) {
1161 printk(KERN_ERR
1162 "%s: Unable to request IRQ %d (error %d)\n",
1163 dev->name, dev->irq, err);
1164 goto err_out_iounmap;
1165 }
1166
1167 dev->open = macb_open;
1168 dev->stop = macb_close;
1169 dev->hard_start_xmit = macb_start_xmit;
1170 dev->get_stats = macb_get_stats;
446ebd01 1171 dev->set_multicast_list = macb_set_rx_mode;
89e5785f 1172 dev->do_ioctl = macb_ioctl;
bea3348e 1173 netif_napi_add(dev, &bp->napi, macb_poll, 64);
89e5785f
HS
1174 dev->ethtool_ops = &macb_ethtool_ops;
1175
1176 dev->base_addr = regs->start;
1177
89e5785f
HS
1178 /* Set MII management clock divider */
1179 pclk_hz = clk_get_rate(bp->pclk);
1180 if (pclk_hz <= 20000000)
1181 config = MACB_BF(CLK, MACB_CLK_DIV8);
1182 else if (pclk_hz <= 40000000)
1183 config = MACB_BF(CLK, MACB_CLK_DIV16);
1184 else if (pclk_hz <= 80000000)
1185 config = MACB_BF(CLK, MACB_CLK_DIV32);
1186 else
1187 config = MACB_BF(CLK, MACB_CLK_DIV64);
1188 macb_writel(bp, NCFGR, config);
1189
89e5785f 1190 macb_get_hwaddr(bp);
89e5785f 1191 pdata = pdev->dev.platform_data;
6c36a707 1192
89e5785f 1193 if (pdata && pdata->is_rmii)
0cc8674f
AV
1194#if defined(CONFIG_ARCH_AT91)
1195 macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
1196#else
89e5785f 1197 macb_writel(bp, USRIO, 0);
0cc8674f 1198#endif
89e5785f 1199 else
0cc8674f
AV
1200#if defined(CONFIG_ARCH_AT91)
1201 macb_writel(bp, USRIO, MACB_BIT(CLKEN));
1202#else
89e5785f 1203 macb_writel(bp, USRIO, MACB_BIT(MII));
0cc8674f 1204#endif
89e5785f
HS
1205
1206 bp->tx_pending = DEF_TX_RING_PENDING;
1207
1208 err = register_netdev(dev);
1209 if (err) {
1210 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1211 goto err_out_free_irq;
1212 }
1213
6c36a707
R
1214 if (macb_mii_init(bp) != 0) {
1215 goto err_out_unregister_netdev;
1216 }
89e5785f 1217
6c36a707 1218 platform_set_drvdata(pdev, dev);
89e5785f
HS
1219
1220 printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d "
0795af57 1221 "(%s)\n",
89e5785f 1222 dev->name, dev->base_addr, dev->irq,
0795af57 1223 print_mac(mac, dev->dev_addr));
89e5785f 1224
6c36a707
R
1225 phydev = bp->phy_dev;
1226 printk(KERN_INFO "%s: attached PHY driver [%s] "
1227 "(mii_bus:phy_addr=%s, irq=%d)\n",
1228 dev->name, phydev->drv->name, phydev->dev.bus_id, phydev->irq);
1229
89e5785f
HS
1230 return 0;
1231
6c36a707
R
1232err_out_unregister_netdev:
1233 unregister_netdev(dev);
89e5785f
HS
1234err_out_free_irq:
1235 free_irq(dev->irq, dev);
1236err_out_iounmap:
1237 iounmap(bp->regs);
1238err_out_disable_clocks:
0cc8674f 1239#ifndef CONFIG_ARCH_AT91
89e5785f 1240 clk_disable(bp->hclk);
89e5785f 1241 clk_put(bp->hclk);
0cc8674f
AV
1242#endif
1243 clk_disable(bp->pclk);
6c36a707 1244#ifndef CONFIG_ARCH_AT91
89e5785f 1245err_out_put_pclk:
6c36a707 1246#endif
89e5785f
HS
1247 clk_put(bp->pclk);
1248err_out_free_dev:
1249 free_netdev(dev);
1250err_out:
1251 platform_set_drvdata(pdev, NULL);
1252 return err;
1253}
1254
06c3fd6a 1255static int __exit macb_remove(struct platform_device *pdev)
89e5785f
HS
1256{
1257 struct net_device *dev;
1258 struct macb *bp;
1259
1260 dev = platform_get_drvdata(pdev);
1261
1262 if (dev) {
1263 bp = netdev_priv(dev);
84b7901f
AN
1264 if (bp->phy_dev)
1265 phy_disconnect(bp->phy_dev);
6c36a707
R
1266 mdiobus_unregister(&bp->mii_bus);
1267 kfree(bp->mii_bus.irq);
89e5785f
HS
1268 unregister_netdev(dev);
1269 free_irq(dev->irq, dev);
1270 iounmap(bp->regs);
0cc8674f 1271#ifndef CONFIG_ARCH_AT91
89e5785f 1272 clk_disable(bp->hclk);
89e5785f 1273 clk_put(bp->hclk);
0cc8674f
AV
1274#endif
1275 clk_disable(bp->pclk);
89e5785f
HS
1276 clk_put(bp->pclk);
1277 free_netdev(dev);
1278 platform_set_drvdata(pdev, NULL);
1279 }
1280
1281 return 0;
1282}
1283
c1f598fd
HS
1284#ifdef CONFIG_PM
1285static int macb_suspend(struct platform_device *pdev, pm_message_t state)
1286{
1287 struct net_device *netdev = platform_get_drvdata(pdev);
1288 struct macb *bp = netdev_priv(netdev);
1289
1290 netif_device_detach(netdev);
1291
1292#ifndef CONFIG_ARCH_AT91
1293 clk_disable(bp->hclk);
1294#endif
1295 clk_disable(bp->pclk);
1296
1297 return 0;
1298}
1299
1300static int macb_resume(struct platform_device *pdev)
1301{
1302 struct net_device *netdev = platform_get_drvdata(pdev);
1303 struct macb *bp = netdev_priv(netdev);
1304
1305 clk_enable(bp->pclk);
1306#ifndef CONFIG_ARCH_AT91
1307 clk_enable(bp->hclk);
1308#endif
1309
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
89e5785f 1319static struct platform_driver macb_driver = {
06c3fd6a 1320 .remove = __exit_p(macb_remove),
c1f598fd
HS
1321 .suspend = macb_suspend,
1322 .resume = macb_resume,
89e5785f
HS
1323 .driver = {
1324 .name = "macb",
72abb461 1325 .owner = THIS_MODULE,
89e5785f
HS
1326 },
1327};
1328
1329static int __init macb_init(void)
1330{
06c3fd6a 1331 return platform_driver_probe(&macb_driver, macb_probe);
89e5785f
HS
1332}
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");
1344MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
72abb461 1345MODULE_ALIAS("platform:macb");