Merge branch 'for-linus' of git://git.infradead.org/users/sameo/mfd-2.6
[linux-btrfs-devel.git] / drivers / net / greth.c
blob52a39000c42c77f46ef702be9ce7992b6967c4f4
1 /*
2 * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
4 * 2005-2010 (c) Aeroflex Gaisler AB
6 * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
7 * available in the GRLIB VHDL IP core library.
9 * Full documentation of both cores can be found here:
10 * http://www.gaisler.com/products/grlib/grip.pdf
12 * The Gigabit version supports scatter/gather DMA, any alignment of
13 * buffers and checksum offloading.
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * Contributors: Kristoffer Glembo
21 * Daniel Hellstrom
22 * Marko Isomaki
25 #include <linux/dma-mapping.h>
26 #include <linux/module.h>
27 #include <linux/uaccess.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/ethtool.h>
33 #include <linux/skbuff.h>
34 #include <linux/io.h>
35 #include <linux/crc32.h>
36 #include <linux/mii.h>
37 #include <linux/of_device.h>
38 #include <linux/of_platform.h>
39 #include <linux/slab.h>
40 #include <asm/cacheflush.h>
41 #include <asm/byteorder.h>
43 #ifdef CONFIG_SPARC
44 #include <asm/idprom.h>
45 #endif
47 #include "greth.h"
49 #define GRETH_DEF_MSG_ENABLE \
50 (NETIF_MSG_DRV | \
51 NETIF_MSG_PROBE | \
52 NETIF_MSG_LINK | \
53 NETIF_MSG_IFDOWN | \
54 NETIF_MSG_IFUP | \
55 NETIF_MSG_RX_ERR | \
56 NETIF_MSG_TX_ERR)
58 static int greth_debug = -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */
59 module_param(greth_debug, int, 0);
60 MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
62 /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
63 static int macaddr[6];
64 module_param_array(macaddr, int, NULL, 0);
65 MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
67 static int greth_edcl = 1;
68 module_param(greth_edcl, int, 0);
69 MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
71 static int greth_open(struct net_device *dev);
72 static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
73 struct net_device *dev);
74 static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
75 struct net_device *dev);
76 static int greth_rx(struct net_device *dev, int limit);
77 static int greth_rx_gbit(struct net_device *dev, int limit);
78 static void greth_clean_tx(struct net_device *dev);
79 static void greth_clean_tx_gbit(struct net_device *dev);
80 static irqreturn_t greth_interrupt(int irq, void *dev_id);
81 static int greth_close(struct net_device *dev);
82 static int greth_set_mac_add(struct net_device *dev, void *p);
83 static void greth_set_multicast_list(struct net_device *dev);
85 #define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
86 #define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
87 #define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
88 #define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
90 #define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
91 #define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
92 #define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
94 static void greth_print_rx_packet(void *addr, int len)
96 print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
97 addr, len, true);
100 static void greth_print_tx_packet(struct sk_buff *skb)
102 int i;
103 int length;
105 if (skb_shinfo(skb)->nr_frags == 0)
106 length = skb->len;
107 else
108 length = skb_headlen(skb);
110 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
111 skb->data, length, true);
113 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
115 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
116 phys_to_virt(page_to_phys(skb_shinfo(skb)->frags[i].page)) +
117 skb_shinfo(skb)->frags[i].page_offset,
118 length, true);
122 static inline void greth_enable_tx(struct greth_private *greth)
124 wmb();
125 GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
128 static inline void greth_disable_tx(struct greth_private *greth)
130 GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
133 static inline void greth_enable_rx(struct greth_private *greth)
135 wmb();
136 GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
139 static inline void greth_disable_rx(struct greth_private *greth)
141 GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
144 static inline void greth_enable_irqs(struct greth_private *greth)
146 GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
149 static inline void greth_disable_irqs(struct greth_private *greth)
151 GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
154 static inline void greth_write_bd(u32 *bd, u32 val)
156 __raw_writel(cpu_to_be32(val), bd);
159 static inline u32 greth_read_bd(u32 *bd)
161 return be32_to_cpu(__raw_readl(bd));
164 static void greth_clean_rings(struct greth_private *greth)
166 int i;
167 struct greth_bd *rx_bdp = greth->rx_bd_base;
168 struct greth_bd *tx_bdp = greth->tx_bd_base;
170 if (greth->gbit_mac) {
172 /* Free and unmap RX buffers */
173 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
174 if (greth->rx_skbuff[i] != NULL) {
175 dev_kfree_skb(greth->rx_skbuff[i]);
176 dma_unmap_single(greth->dev,
177 greth_read_bd(&rx_bdp->addr),
178 MAX_FRAME_SIZE+NET_IP_ALIGN,
179 DMA_FROM_DEVICE);
183 /* TX buffers */
184 while (greth->tx_free < GRETH_TXBD_NUM) {
186 struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
187 int nr_frags = skb_shinfo(skb)->nr_frags;
188 tx_bdp = greth->tx_bd_base + greth->tx_last;
189 greth->tx_last = NEXT_TX(greth->tx_last);
191 dma_unmap_single(greth->dev,
192 greth_read_bd(&tx_bdp->addr),
193 skb_headlen(skb),
194 DMA_TO_DEVICE);
196 for (i = 0; i < nr_frags; i++) {
197 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
198 tx_bdp = greth->tx_bd_base + greth->tx_last;
200 dma_unmap_page(greth->dev,
201 greth_read_bd(&tx_bdp->addr),
202 frag->size,
203 DMA_TO_DEVICE);
205 greth->tx_last = NEXT_TX(greth->tx_last);
207 greth->tx_free += nr_frags+1;
208 dev_kfree_skb(skb);
212 } else { /* 10/100 Mbps MAC */
214 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
215 kfree(greth->rx_bufs[i]);
216 dma_unmap_single(greth->dev,
217 greth_read_bd(&rx_bdp->addr),
218 MAX_FRAME_SIZE,
219 DMA_FROM_DEVICE);
221 for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
222 kfree(greth->tx_bufs[i]);
223 dma_unmap_single(greth->dev,
224 greth_read_bd(&tx_bdp->addr),
225 MAX_FRAME_SIZE,
226 DMA_TO_DEVICE);
231 static int greth_init_rings(struct greth_private *greth)
233 struct sk_buff *skb;
234 struct greth_bd *rx_bd, *tx_bd;
235 u32 dma_addr;
236 int i;
238 rx_bd = greth->rx_bd_base;
239 tx_bd = greth->tx_bd_base;
241 /* Initialize descriptor rings and buffers */
242 if (greth->gbit_mac) {
244 for (i = 0; i < GRETH_RXBD_NUM; i++) {
245 skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
246 if (skb == NULL) {
247 if (netif_msg_ifup(greth))
248 dev_err(greth->dev, "Error allocating DMA ring.\n");
249 goto cleanup;
251 skb_reserve(skb, NET_IP_ALIGN);
252 dma_addr = dma_map_single(greth->dev,
253 skb->data,
254 MAX_FRAME_SIZE+NET_IP_ALIGN,
255 DMA_FROM_DEVICE);
257 if (dma_mapping_error(greth->dev, dma_addr)) {
258 if (netif_msg_ifup(greth))
259 dev_err(greth->dev, "Could not create initial DMA mapping\n");
260 goto cleanup;
262 greth->rx_skbuff[i] = skb;
263 greth_write_bd(&rx_bd[i].addr, dma_addr);
264 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
267 } else {
269 /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
270 for (i = 0; i < GRETH_RXBD_NUM; i++) {
272 greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
274 if (greth->rx_bufs[i] == NULL) {
275 if (netif_msg_ifup(greth))
276 dev_err(greth->dev, "Error allocating DMA ring.\n");
277 goto cleanup;
280 dma_addr = dma_map_single(greth->dev,
281 greth->rx_bufs[i],
282 MAX_FRAME_SIZE,
283 DMA_FROM_DEVICE);
285 if (dma_mapping_error(greth->dev, dma_addr)) {
286 if (netif_msg_ifup(greth))
287 dev_err(greth->dev, "Could not create initial DMA mapping\n");
288 goto cleanup;
290 greth_write_bd(&rx_bd[i].addr, dma_addr);
291 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
293 for (i = 0; i < GRETH_TXBD_NUM; i++) {
295 greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
297 if (greth->tx_bufs[i] == NULL) {
298 if (netif_msg_ifup(greth))
299 dev_err(greth->dev, "Error allocating DMA ring.\n");
300 goto cleanup;
303 dma_addr = dma_map_single(greth->dev,
304 greth->tx_bufs[i],
305 MAX_FRAME_SIZE,
306 DMA_TO_DEVICE);
308 if (dma_mapping_error(greth->dev, dma_addr)) {
309 if (netif_msg_ifup(greth))
310 dev_err(greth->dev, "Could not create initial DMA mapping\n");
311 goto cleanup;
313 greth_write_bd(&tx_bd[i].addr, dma_addr);
314 greth_write_bd(&tx_bd[i].stat, 0);
317 greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
318 greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
320 /* Initialize pointers. */
321 greth->rx_cur = 0;
322 greth->tx_next = 0;
323 greth->tx_last = 0;
324 greth->tx_free = GRETH_TXBD_NUM;
326 /* Initialize descriptor base address */
327 GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
328 GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
330 return 0;
332 cleanup:
333 greth_clean_rings(greth);
334 return -ENOMEM;
337 static int greth_open(struct net_device *dev)
339 struct greth_private *greth = netdev_priv(dev);
340 int err;
342 err = greth_init_rings(greth);
343 if (err) {
344 if (netif_msg_ifup(greth))
345 dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
346 return err;
349 err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
350 if (err) {
351 if (netif_msg_ifup(greth))
352 dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
353 greth_clean_rings(greth);
354 return err;
357 if (netif_msg_ifup(greth))
358 dev_dbg(&dev->dev, " starting queue\n");
359 netif_start_queue(dev);
361 GRETH_REGSAVE(greth->regs->status, 0xFF);
363 napi_enable(&greth->napi);
365 greth_enable_irqs(greth);
366 greth_enable_tx(greth);
367 greth_enable_rx(greth);
368 return 0;
372 static int greth_close(struct net_device *dev)
374 struct greth_private *greth = netdev_priv(dev);
376 napi_disable(&greth->napi);
378 greth_disable_irqs(greth);
379 greth_disable_tx(greth);
380 greth_disable_rx(greth);
382 netif_stop_queue(dev);
384 free_irq(greth->irq, (void *) dev);
386 greth_clean_rings(greth);
388 return 0;
391 static netdev_tx_t
392 greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
394 struct greth_private *greth = netdev_priv(dev);
395 struct greth_bd *bdp;
396 int err = NETDEV_TX_OK;
397 u32 status, dma_addr, ctrl;
398 unsigned long flags;
400 /* Clean TX Ring */
401 greth_clean_tx(greth->netdev);
403 if (unlikely(greth->tx_free <= 0)) {
404 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
405 ctrl = GRETH_REGLOAD(greth->regs->control);
406 /* Enable TX IRQ only if not already in poll() routine */
407 if (ctrl & GRETH_RXI)
408 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
409 netif_stop_queue(dev);
410 spin_unlock_irqrestore(&greth->devlock, flags);
411 return NETDEV_TX_BUSY;
414 if (netif_msg_pktdata(greth))
415 greth_print_tx_packet(skb);
418 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
419 dev->stats.tx_errors++;
420 goto out;
423 bdp = greth->tx_bd_base + greth->tx_next;
424 dma_addr = greth_read_bd(&bdp->addr);
426 memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
428 dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
430 status = GRETH_BD_EN | GRETH_BD_IE | (skb->len & GRETH_BD_LEN);
431 greth->tx_bufs_length[greth->tx_next] = skb->len & GRETH_BD_LEN;
433 /* Wrap around descriptor ring */
434 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
435 status |= GRETH_BD_WR;
438 greth->tx_next = NEXT_TX(greth->tx_next);
439 greth->tx_free--;
441 /* Write descriptor control word and enable transmission */
442 greth_write_bd(&bdp->stat, status);
443 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
444 greth_enable_tx(greth);
445 spin_unlock_irqrestore(&greth->devlock, flags);
447 out:
448 dev_kfree_skb(skb);
449 return err;
453 static netdev_tx_t
454 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
456 struct greth_private *greth = netdev_priv(dev);
457 struct greth_bd *bdp;
458 u32 status = 0, dma_addr, ctrl;
459 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
460 unsigned long flags;
462 nr_frags = skb_shinfo(skb)->nr_frags;
464 /* Clean TX Ring */
465 greth_clean_tx_gbit(dev);
467 if (greth->tx_free < nr_frags + 1) {
468 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
469 ctrl = GRETH_REGLOAD(greth->regs->control);
470 /* Enable TX IRQ only if not already in poll() routine */
471 if (ctrl & GRETH_RXI)
472 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
473 netif_stop_queue(dev);
474 spin_unlock_irqrestore(&greth->devlock, flags);
475 err = NETDEV_TX_BUSY;
476 goto out;
479 if (netif_msg_pktdata(greth))
480 greth_print_tx_packet(skb);
482 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
483 dev->stats.tx_errors++;
484 goto out;
487 /* Save skb pointer. */
488 greth->tx_skbuff[greth->tx_next] = skb;
490 /* Linear buf */
491 if (nr_frags != 0)
492 status = GRETH_TXBD_MORE;
494 if (skb->ip_summed == CHECKSUM_PARTIAL)
495 status |= GRETH_TXBD_CSALL;
496 status |= skb_headlen(skb) & GRETH_BD_LEN;
497 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
498 status |= GRETH_BD_WR;
501 bdp = greth->tx_bd_base + greth->tx_next;
502 greth_write_bd(&bdp->stat, status);
503 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
505 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
506 goto map_error;
508 greth_write_bd(&bdp->addr, dma_addr);
510 curr_tx = NEXT_TX(greth->tx_next);
512 /* Frags */
513 for (i = 0; i < nr_frags; i++) {
514 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
515 greth->tx_skbuff[curr_tx] = NULL;
516 bdp = greth->tx_bd_base + curr_tx;
518 status = GRETH_BD_EN;
519 if (skb->ip_summed == CHECKSUM_PARTIAL)
520 status |= GRETH_TXBD_CSALL;
521 status |= frag->size & GRETH_BD_LEN;
523 /* Wrap around descriptor ring */
524 if (curr_tx == GRETH_TXBD_NUM_MASK)
525 status |= GRETH_BD_WR;
527 /* More fragments left */
528 if (i < nr_frags - 1)
529 status |= GRETH_TXBD_MORE;
530 else
531 status |= GRETH_BD_IE; /* enable IRQ on last fragment */
533 greth_write_bd(&bdp->stat, status);
535 dma_addr = dma_map_page(greth->dev,
536 frag->page,
537 frag->page_offset,
538 frag->size,
539 DMA_TO_DEVICE);
541 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
542 goto frag_map_error;
544 greth_write_bd(&bdp->addr, dma_addr);
546 curr_tx = NEXT_TX(curr_tx);
549 wmb();
551 /* Enable the descriptor chain by enabling the first descriptor */
552 bdp = greth->tx_bd_base + greth->tx_next;
553 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
554 greth->tx_next = curr_tx;
555 greth->tx_free -= nr_frags + 1;
557 wmb();
559 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
560 greth_enable_tx(greth);
561 spin_unlock_irqrestore(&greth->devlock, flags);
563 return NETDEV_TX_OK;
565 frag_map_error:
566 /* Unmap SKB mappings that succeeded and disable descriptor */
567 for (i = 0; greth->tx_next + i != curr_tx; i++) {
568 bdp = greth->tx_bd_base + greth->tx_next + i;
569 dma_unmap_single(greth->dev,
570 greth_read_bd(&bdp->addr),
571 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
572 DMA_TO_DEVICE);
573 greth_write_bd(&bdp->stat, 0);
575 map_error:
576 if (net_ratelimit())
577 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
578 dev_kfree_skb(skb);
579 out:
580 return err;
583 static irqreturn_t greth_interrupt(int irq, void *dev_id)
585 struct net_device *dev = dev_id;
586 struct greth_private *greth;
587 u32 status, ctrl;
588 irqreturn_t retval = IRQ_NONE;
590 greth = netdev_priv(dev);
592 spin_lock(&greth->devlock);
594 /* Get the interrupt events that caused us to be here. */
595 status = GRETH_REGLOAD(greth->regs->status);
597 /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
598 * set regardless of whether IRQ is enabled or not. Especially
599 * important when shared IRQ.
601 ctrl = GRETH_REGLOAD(greth->regs->control);
603 /* Handle rx and tx interrupts through poll */
604 if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
605 ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
606 retval = IRQ_HANDLED;
608 /* Disable interrupts and schedule poll() */
609 greth_disable_irqs(greth);
610 napi_schedule(&greth->napi);
613 mmiowb();
614 spin_unlock(&greth->devlock);
616 return retval;
619 static void greth_clean_tx(struct net_device *dev)
621 struct greth_private *greth;
622 struct greth_bd *bdp;
623 u32 stat;
625 greth = netdev_priv(dev);
627 while (1) {
628 bdp = greth->tx_bd_base + greth->tx_last;
629 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
630 mb();
631 stat = greth_read_bd(&bdp->stat);
633 if (unlikely(stat & GRETH_BD_EN))
634 break;
636 if (greth->tx_free == GRETH_TXBD_NUM)
637 break;
639 /* Check status for errors */
640 if (unlikely(stat & GRETH_TXBD_STATUS)) {
641 dev->stats.tx_errors++;
642 if (stat & GRETH_TXBD_ERR_AL)
643 dev->stats.tx_aborted_errors++;
644 if (stat & GRETH_TXBD_ERR_UE)
645 dev->stats.tx_fifo_errors++;
647 dev->stats.tx_packets++;
648 dev->stats.tx_bytes += greth->tx_bufs_length[greth->tx_last];
649 greth->tx_last = NEXT_TX(greth->tx_last);
650 greth->tx_free++;
653 if (greth->tx_free > 0) {
654 netif_wake_queue(dev);
659 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
661 /* Check status for errors */
662 if (unlikely(stat & GRETH_TXBD_STATUS)) {
663 dev->stats.tx_errors++;
664 if (stat & GRETH_TXBD_ERR_AL)
665 dev->stats.tx_aborted_errors++;
666 if (stat & GRETH_TXBD_ERR_UE)
667 dev->stats.tx_fifo_errors++;
668 if (stat & GRETH_TXBD_ERR_LC)
669 dev->stats.tx_aborted_errors++;
671 dev->stats.tx_packets++;
674 static void greth_clean_tx_gbit(struct net_device *dev)
676 struct greth_private *greth;
677 struct greth_bd *bdp, *bdp_last_frag;
678 struct sk_buff *skb;
679 u32 stat;
680 int nr_frags, i;
682 greth = netdev_priv(dev);
684 while (greth->tx_free < GRETH_TXBD_NUM) {
686 skb = greth->tx_skbuff[greth->tx_last];
688 nr_frags = skb_shinfo(skb)->nr_frags;
690 /* We only clean fully completed SKBs */
691 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
693 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
694 mb();
695 stat = greth_read_bd(&bdp_last_frag->stat);
697 if (stat & GRETH_BD_EN)
698 break;
700 greth->tx_skbuff[greth->tx_last] = NULL;
702 greth_update_tx_stats(dev, stat);
703 dev->stats.tx_bytes += skb->len;
705 bdp = greth->tx_bd_base + greth->tx_last;
707 greth->tx_last = NEXT_TX(greth->tx_last);
709 dma_unmap_single(greth->dev,
710 greth_read_bd(&bdp->addr),
711 skb_headlen(skb),
712 DMA_TO_DEVICE);
714 for (i = 0; i < nr_frags; i++) {
715 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
716 bdp = greth->tx_bd_base + greth->tx_last;
718 dma_unmap_page(greth->dev,
719 greth_read_bd(&bdp->addr),
720 frag->size,
721 DMA_TO_DEVICE);
723 greth->tx_last = NEXT_TX(greth->tx_last);
725 greth->tx_free += nr_frags+1;
726 dev_kfree_skb(skb);
729 if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1)))
730 netif_wake_queue(dev);
733 static int greth_rx(struct net_device *dev, int limit)
735 struct greth_private *greth;
736 struct greth_bd *bdp;
737 struct sk_buff *skb;
738 int pkt_len;
739 int bad, count;
740 u32 status, dma_addr;
741 unsigned long flags;
743 greth = netdev_priv(dev);
745 for (count = 0; count < limit; ++count) {
747 bdp = greth->rx_bd_base + greth->rx_cur;
748 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
749 mb();
750 status = greth_read_bd(&bdp->stat);
752 if (unlikely(status & GRETH_BD_EN)) {
753 break;
756 dma_addr = greth_read_bd(&bdp->addr);
757 bad = 0;
759 /* Check status for errors. */
760 if (unlikely(status & GRETH_RXBD_STATUS)) {
761 if (status & GRETH_RXBD_ERR_FT) {
762 dev->stats.rx_length_errors++;
763 bad = 1;
765 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
766 dev->stats.rx_frame_errors++;
767 bad = 1;
769 if (status & GRETH_RXBD_ERR_CRC) {
770 dev->stats.rx_crc_errors++;
771 bad = 1;
774 if (unlikely(bad)) {
775 dev->stats.rx_errors++;
777 } else {
779 pkt_len = status & GRETH_BD_LEN;
781 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
783 if (unlikely(skb == NULL)) {
785 if (net_ratelimit())
786 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
788 dev->stats.rx_dropped++;
790 } else {
791 skb_reserve(skb, NET_IP_ALIGN);
792 skb->dev = dev;
794 dma_sync_single_for_cpu(greth->dev,
795 dma_addr,
796 pkt_len,
797 DMA_FROM_DEVICE);
799 if (netif_msg_pktdata(greth))
800 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
802 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
804 skb->protocol = eth_type_trans(skb, dev);
805 dev->stats.rx_bytes += pkt_len;
806 dev->stats.rx_packets++;
807 netif_receive_skb(skb);
811 status = GRETH_BD_EN | GRETH_BD_IE;
812 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
813 status |= GRETH_BD_WR;
816 wmb();
817 greth_write_bd(&bdp->stat, status);
819 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
821 spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
822 greth_enable_rx(greth);
823 spin_unlock_irqrestore(&greth->devlock, flags);
825 greth->rx_cur = NEXT_RX(greth->rx_cur);
828 return count;
831 static inline int hw_checksummed(u32 status)
834 if (status & GRETH_RXBD_IP_FRAG)
835 return 0;
837 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
838 return 0;
840 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
841 return 0;
843 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
844 return 0;
846 return 1;
849 static int greth_rx_gbit(struct net_device *dev, int limit)
851 struct greth_private *greth;
852 struct greth_bd *bdp;
853 struct sk_buff *skb, *newskb;
854 int pkt_len;
855 int bad, count = 0;
856 u32 status, dma_addr;
857 unsigned long flags;
859 greth = netdev_priv(dev);
861 for (count = 0; count < limit; ++count) {
863 bdp = greth->rx_bd_base + greth->rx_cur;
864 skb = greth->rx_skbuff[greth->rx_cur];
865 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
866 mb();
867 status = greth_read_bd(&bdp->stat);
868 bad = 0;
870 if (status & GRETH_BD_EN)
871 break;
873 /* Check status for errors. */
874 if (unlikely(status & GRETH_RXBD_STATUS)) {
876 if (status & GRETH_RXBD_ERR_FT) {
877 dev->stats.rx_length_errors++;
878 bad = 1;
879 } else if (status &
880 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
881 dev->stats.rx_frame_errors++;
882 bad = 1;
883 } else if (status & GRETH_RXBD_ERR_CRC) {
884 dev->stats.rx_crc_errors++;
885 bad = 1;
889 /* Allocate new skb to replace current, not needed if the
890 * current skb can be reused */
891 if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
892 skb_reserve(newskb, NET_IP_ALIGN);
894 dma_addr = dma_map_single(greth->dev,
895 newskb->data,
896 MAX_FRAME_SIZE + NET_IP_ALIGN,
897 DMA_FROM_DEVICE);
899 if (!dma_mapping_error(greth->dev, dma_addr)) {
900 /* Process the incoming frame. */
901 pkt_len = status & GRETH_BD_LEN;
903 dma_unmap_single(greth->dev,
904 greth_read_bd(&bdp->addr),
905 MAX_FRAME_SIZE + NET_IP_ALIGN,
906 DMA_FROM_DEVICE);
908 if (netif_msg_pktdata(greth))
909 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
911 skb_put(skb, pkt_len);
913 if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
914 skb->ip_summed = CHECKSUM_UNNECESSARY;
915 else
916 skb_checksum_none_assert(skb);
918 skb->protocol = eth_type_trans(skb, dev);
919 dev->stats.rx_packets++;
920 dev->stats.rx_bytes += pkt_len;
921 netif_receive_skb(skb);
923 greth->rx_skbuff[greth->rx_cur] = newskb;
924 greth_write_bd(&bdp->addr, dma_addr);
925 } else {
926 if (net_ratelimit())
927 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
928 dev_kfree_skb(newskb);
929 /* reusing current skb, so it is a drop */
930 dev->stats.rx_dropped++;
932 } else if (bad) {
933 /* Bad Frame transfer, the skb is reused */
934 dev->stats.rx_dropped++;
935 } else {
936 /* Failed Allocating a new skb. This is rather stupid
937 * but the current "filled" skb is reused, as if
938 * transfer failure. One could argue that RX descriptor
939 * table handling should be divided into cleaning and
940 * filling as the TX part of the driver
942 if (net_ratelimit())
943 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
944 /* reusing current skb, so it is a drop */
945 dev->stats.rx_dropped++;
948 status = GRETH_BD_EN | GRETH_BD_IE;
949 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
950 status |= GRETH_BD_WR;
953 wmb();
954 greth_write_bd(&bdp->stat, status);
955 spin_lock_irqsave(&greth->devlock, flags);
956 greth_enable_rx(greth);
957 spin_unlock_irqrestore(&greth->devlock, flags);
958 greth->rx_cur = NEXT_RX(greth->rx_cur);
961 return count;
965 static int greth_poll(struct napi_struct *napi, int budget)
967 struct greth_private *greth;
968 int work_done = 0;
969 unsigned long flags;
970 u32 mask, ctrl;
971 greth = container_of(napi, struct greth_private, napi);
973 restart_txrx_poll:
974 if (netif_queue_stopped(greth->netdev)) {
975 if (greth->gbit_mac)
976 greth_clean_tx_gbit(greth->netdev);
977 else
978 greth_clean_tx(greth->netdev);
981 if (greth->gbit_mac) {
982 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
983 } else {
984 work_done += greth_rx(greth->netdev, budget - work_done);
987 if (work_done < budget) {
989 spin_lock_irqsave(&greth->devlock, flags);
991 ctrl = GRETH_REGLOAD(greth->regs->control);
992 if (netif_queue_stopped(greth->netdev)) {
993 GRETH_REGSAVE(greth->regs->control,
994 ctrl | GRETH_TXI | GRETH_RXI);
995 mask = GRETH_INT_RX | GRETH_INT_RE |
996 GRETH_INT_TX | GRETH_INT_TE;
997 } else {
998 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
999 mask = GRETH_INT_RX | GRETH_INT_RE;
1002 if (GRETH_REGLOAD(greth->regs->status) & mask) {
1003 GRETH_REGSAVE(greth->regs->control, ctrl);
1004 spin_unlock_irqrestore(&greth->devlock, flags);
1005 goto restart_txrx_poll;
1006 } else {
1007 __napi_complete(napi);
1008 spin_unlock_irqrestore(&greth->devlock, flags);
1012 return work_done;
1015 static int greth_set_mac_add(struct net_device *dev, void *p)
1017 struct sockaddr *addr = p;
1018 struct greth_private *greth;
1019 struct greth_regs *regs;
1021 greth = netdev_priv(dev);
1022 regs = (struct greth_regs *) greth->regs;
1024 if (!is_valid_ether_addr(addr->sa_data))
1025 return -EINVAL;
1027 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1028 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1029 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1030 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1032 return 0;
1035 static u32 greth_hash_get_index(__u8 *addr)
1037 return (ether_crc(6, addr)) & 0x3F;
1040 static void greth_set_hash_filter(struct net_device *dev)
1042 struct netdev_hw_addr *ha;
1043 struct greth_private *greth = netdev_priv(dev);
1044 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1045 u32 mc_filter[2];
1046 unsigned int bitnr;
1048 mc_filter[0] = mc_filter[1] = 0;
1050 netdev_for_each_mc_addr(ha, dev) {
1051 bitnr = greth_hash_get_index(ha->addr);
1052 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1055 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1056 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1059 static void greth_set_multicast_list(struct net_device *dev)
1061 int cfg;
1062 struct greth_private *greth = netdev_priv(dev);
1063 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1065 cfg = GRETH_REGLOAD(regs->control);
1066 if (dev->flags & IFF_PROMISC)
1067 cfg |= GRETH_CTRL_PR;
1068 else
1069 cfg &= ~GRETH_CTRL_PR;
1071 if (greth->multicast) {
1072 if (dev->flags & IFF_ALLMULTI) {
1073 GRETH_REGSAVE(regs->hash_msb, -1);
1074 GRETH_REGSAVE(regs->hash_lsb, -1);
1075 cfg |= GRETH_CTRL_MCEN;
1076 GRETH_REGSAVE(regs->control, cfg);
1077 return;
1080 if (netdev_mc_empty(dev)) {
1081 cfg &= ~GRETH_CTRL_MCEN;
1082 GRETH_REGSAVE(regs->control, cfg);
1083 return;
1086 /* Setup multicast filter */
1087 greth_set_hash_filter(dev);
1088 cfg |= GRETH_CTRL_MCEN;
1090 GRETH_REGSAVE(regs->control, cfg);
1093 static u32 greth_get_msglevel(struct net_device *dev)
1095 struct greth_private *greth = netdev_priv(dev);
1096 return greth->msg_enable;
1099 static void greth_set_msglevel(struct net_device *dev, u32 value)
1101 struct greth_private *greth = netdev_priv(dev);
1102 greth->msg_enable = value;
1104 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1106 struct greth_private *greth = netdev_priv(dev);
1107 struct phy_device *phy = greth->phy;
1109 if (!phy)
1110 return -ENODEV;
1112 return phy_ethtool_gset(phy, cmd);
1115 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1117 struct greth_private *greth = netdev_priv(dev);
1118 struct phy_device *phy = greth->phy;
1120 if (!phy)
1121 return -ENODEV;
1123 return phy_ethtool_sset(phy, cmd);
1126 static int greth_get_regs_len(struct net_device *dev)
1128 return sizeof(struct greth_regs);
1131 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1133 struct greth_private *greth = netdev_priv(dev);
1135 strncpy(info->driver, dev_driver_string(greth->dev), 32);
1136 strncpy(info->version, "revision: 1.0", 32);
1137 strncpy(info->bus_info, greth->dev->bus->name, 32);
1138 strncpy(info->fw_version, "N/A", 32);
1139 info->eedump_len = 0;
1140 info->regdump_len = sizeof(struct greth_regs);
1143 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1145 int i;
1146 struct greth_private *greth = netdev_priv(dev);
1147 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1148 u32 *buff = p;
1150 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1151 buff[i] = greth_read_bd(&greth_regs[i]);
1154 static const struct ethtool_ops greth_ethtool_ops = {
1155 .get_msglevel = greth_get_msglevel,
1156 .set_msglevel = greth_set_msglevel,
1157 .get_settings = greth_get_settings,
1158 .set_settings = greth_set_settings,
1159 .get_drvinfo = greth_get_drvinfo,
1160 .get_regs_len = greth_get_regs_len,
1161 .get_regs = greth_get_regs,
1162 .get_link = ethtool_op_get_link,
1165 static struct net_device_ops greth_netdev_ops = {
1166 .ndo_open = greth_open,
1167 .ndo_stop = greth_close,
1168 .ndo_start_xmit = greth_start_xmit,
1169 .ndo_set_mac_address = greth_set_mac_add,
1170 .ndo_validate_addr = eth_validate_addr,
1173 static inline int wait_for_mdio(struct greth_private *greth)
1175 unsigned long timeout = jiffies + 4*HZ/100;
1176 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1177 if (time_after(jiffies, timeout))
1178 return 0;
1180 return 1;
1183 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1185 struct greth_private *greth = bus->priv;
1186 int data;
1188 if (!wait_for_mdio(greth))
1189 return -EBUSY;
1191 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1193 if (!wait_for_mdio(greth))
1194 return -EBUSY;
1196 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1197 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1198 return data;
1200 } else {
1201 return -1;
1205 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1207 struct greth_private *greth = bus->priv;
1209 if (!wait_for_mdio(greth))
1210 return -EBUSY;
1212 GRETH_REGSAVE(greth->regs->mdio,
1213 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1215 if (!wait_for_mdio(greth))
1216 return -EBUSY;
1218 return 0;
1221 static int greth_mdio_reset(struct mii_bus *bus)
1223 return 0;
1226 static void greth_link_change(struct net_device *dev)
1228 struct greth_private *greth = netdev_priv(dev);
1229 struct phy_device *phydev = greth->phy;
1230 unsigned long flags;
1231 int status_change = 0;
1232 u32 ctrl;
1234 spin_lock_irqsave(&greth->devlock, flags);
1236 if (phydev->link) {
1238 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1239 ctrl = GRETH_REGLOAD(greth->regs->control) &
1240 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
1242 if (phydev->duplex)
1243 ctrl |= GRETH_CTRL_FD;
1245 if (phydev->speed == SPEED_100)
1246 ctrl |= GRETH_CTRL_SP;
1247 else if (phydev->speed == SPEED_1000)
1248 ctrl |= GRETH_CTRL_GB;
1250 GRETH_REGSAVE(greth->regs->control, ctrl);
1251 greth->speed = phydev->speed;
1252 greth->duplex = phydev->duplex;
1253 status_change = 1;
1257 if (phydev->link != greth->link) {
1258 if (!phydev->link) {
1259 greth->speed = 0;
1260 greth->duplex = -1;
1262 greth->link = phydev->link;
1264 status_change = 1;
1267 spin_unlock_irqrestore(&greth->devlock, flags);
1269 if (status_change) {
1270 if (phydev->link)
1271 pr_debug("%s: link up (%d/%s)\n",
1272 dev->name, phydev->speed,
1273 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1274 else
1275 pr_debug("%s: link down\n", dev->name);
1279 static int greth_mdio_probe(struct net_device *dev)
1281 struct greth_private *greth = netdev_priv(dev);
1282 struct phy_device *phy = NULL;
1283 int ret;
1285 /* Find the first PHY */
1286 phy = phy_find_first(greth->mdio);
1288 if (!phy) {
1289 if (netif_msg_probe(greth))
1290 dev_err(&dev->dev, "no PHY found\n");
1291 return -ENXIO;
1294 ret = phy_connect_direct(dev, phy, &greth_link_change,
1295 0, greth->gbit_mac ?
1296 PHY_INTERFACE_MODE_GMII :
1297 PHY_INTERFACE_MODE_MII);
1298 if (ret) {
1299 if (netif_msg_ifup(greth))
1300 dev_err(&dev->dev, "could not attach to PHY\n");
1301 return ret;
1304 if (greth->gbit_mac)
1305 phy->supported &= PHY_GBIT_FEATURES;
1306 else
1307 phy->supported &= PHY_BASIC_FEATURES;
1309 phy->advertising = phy->supported;
1311 greth->link = 0;
1312 greth->speed = 0;
1313 greth->duplex = -1;
1314 greth->phy = phy;
1316 return 0;
1319 static inline int phy_aneg_done(struct phy_device *phydev)
1321 int retval;
1323 retval = phy_read(phydev, MII_BMSR);
1325 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1328 static int greth_mdio_init(struct greth_private *greth)
1330 int ret, phy;
1331 unsigned long timeout;
1333 greth->mdio = mdiobus_alloc();
1334 if (!greth->mdio) {
1335 return -ENOMEM;
1338 greth->mdio->name = "greth-mdio";
1339 snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1340 greth->mdio->read = greth_mdio_read;
1341 greth->mdio->write = greth_mdio_write;
1342 greth->mdio->reset = greth_mdio_reset;
1343 greth->mdio->priv = greth;
1345 greth->mdio->irq = greth->mdio_irqs;
1347 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1348 greth->mdio->irq[phy] = PHY_POLL;
1350 ret = mdiobus_register(greth->mdio);
1351 if (ret) {
1352 goto error;
1355 ret = greth_mdio_probe(greth->netdev);
1356 if (ret) {
1357 if (netif_msg_probe(greth))
1358 dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1359 goto unreg_mdio;
1362 phy_start(greth->phy);
1364 /* If Ethernet debug link is used make autoneg happen right away */
1365 if (greth->edcl && greth_edcl == 1) {
1366 phy_start_aneg(greth->phy);
1367 timeout = jiffies + 6*HZ;
1368 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1370 genphy_read_status(greth->phy);
1371 greth_link_change(greth->netdev);
1374 return 0;
1376 unreg_mdio:
1377 mdiobus_unregister(greth->mdio);
1378 error:
1379 mdiobus_free(greth->mdio);
1380 return ret;
1383 /* Initialize the GRETH MAC */
1384 static int __devinit greth_of_probe(struct platform_device *ofdev)
1386 struct net_device *dev;
1387 struct greth_private *greth;
1388 struct greth_regs *regs;
1390 int i;
1391 int err;
1392 int tmp;
1393 unsigned long timeout;
1395 dev = alloc_etherdev(sizeof(struct greth_private));
1397 if (dev == NULL)
1398 return -ENOMEM;
1400 greth = netdev_priv(dev);
1401 greth->netdev = dev;
1402 greth->dev = &ofdev->dev;
1404 if (greth_debug > 0)
1405 greth->msg_enable = greth_debug;
1406 else
1407 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1409 spin_lock_init(&greth->devlock);
1411 greth->regs = of_ioremap(&ofdev->resource[0], 0,
1412 resource_size(&ofdev->resource[0]),
1413 "grlib-greth regs");
1415 if (greth->regs == NULL) {
1416 if (netif_msg_probe(greth))
1417 dev_err(greth->dev, "ioremap failure.\n");
1418 err = -EIO;
1419 goto error1;
1422 regs = (struct greth_regs *) greth->regs;
1423 greth->irq = ofdev->archdata.irqs[0];
1425 dev_set_drvdata(greth->dev, dev);
1426 SET_NETDEV_DEV(dev, greth->dev);
1428 if (netif_msg_probe(greth))
1429 dev_dbg(greth->dev, "reseting controller.\n");
1431 /* Reset the controller. */
1432 GRETH_REGSAVE(regs->control, GRETH_RESET);
1434 /* Wait for MAC to reset itself */
1435 timeout = jiffies + HZ/100;
1436 while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1437 if (time_after(jiffies, timeout)) {
1438 err = -EIO;
1439 if (netif_msg_probe(greth))
1440 dev_err(greth->dev, "timeout when waiting for reset.\n");
1441 goto error2;
1445 /* Get default PHY address */
1446 greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1448 /* Check if we have GBIT capable MAC */
1449 tmp = GRETH_REGLOAD(regs->control);
1450 greth->gbit_mac = (tmp >> 27) & 1;
1452 /* Check for multicast capability */
1453 greth->multicast = (tmp >> 25) & 1;
1455 greth->edcl = (tmp >> 31) & 1;
1457 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1458 * it doesn't interfere with the software */
1459 if (greth->edcl != 0)
1460 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1462 /* Check if MAC can handle MDIO interrupts */
1463 greth->mdio_int_en = (tmp >> 26) & 1;
1465 err = greth_mdio_init(greth);
1466 if (err) {
1467 if (netif_msg_probe(greth))
1468 dev_err(greth->dev, "failed to register MDIO bus\n");
1469 goto error2;
1472 /* Allocate TX descriptor ring in coherent memory */
1473 greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1474 1024,
1475 &greth->tx_bd_base_phys,
1476 GFP_KERNEL);
1478 if (!greth->tx_bd_base) {
1479 if (netif_msg_probe(greth))
1480 dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1481 err = -ENOMEM;
1482 goto error3;
1485 memset(greth->tx_bd_base, 0, 1024);
1487 /* Allocate RX descriptor ring in coherent memory */
1488 greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1489 1024,
1490 &greth->rx_bd_base_phys,
1491 GFP_KERNEL);
1493 if (!greth->rx_bd_base) {
1494 if (netif_msg_probe(greth))
1495 dev_err(greth->dev, "could not allocate descriptor memory.\n");
1496 err = -ENOMEM;
1497 goto error4;
1500 memset(greth->rx_bd_base, 0, 1024);
1502 /* Get MAC address from: module param, OF property or ID prom */
1503 for (i = 0; i < 6; i++) {
1504 if (macaddr[i] != 0)
1505 break;
1507 if (i == 6) {
1508 const unsigned char *addr;
1509 int len;
1510 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1511 &len);
1512 if (addr != NULL && len == 6) {
1513 for (i = 0; i < 6; i++)
1514 macaddr[i] = (unsigned int) addr[i];
1515 } else {
1516 #ifdef CONFIG_SPARC
1517 for (i = 0; i < 6; i++)
1518 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1519 #endif
1523 for (i = 0; i < 6; i++)
1524 dev->dev_addr[i] = macaddr[i];
1526 macaddr[5]++;
1528 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1529 if (netif_msg_probe(greth))
1530 dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1531 err = -EINVAL;
1532 goto error5;
1535 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1536 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1537 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1539 /* Clear all pending interrupts except PHY irq */
1540 GRETH_REGSAVE(regs->status, 0xFF);
1542 if (greth->gbit_mac) {
1543 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1544 NETIF_F_RXCSUM;
1545 dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1546 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1549 if (greth->multicast) {
1550 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1551 dev->flags |= IFF_MULTICAST;
1552 } else {
1553 dev->flags &= ~IFF_MULTICAST;
1556 dev->netdev_ops = &greth_netdev_ops;
1557 dev->ethtool_ops = &greth_ethtool_ops;
1559 err = register_netdev(dev);
1560 if (err) {
1561 if (netif_msg_probe(greth))
1562 dev_err(greth->dev, "netdevice registration failed.\n");
1563 goto error5;
1566 /* setup NAPI */
1567 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1569 return 0;
1571 error5:
1572 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1573 error4:
1574 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1575 error3:
1576 mdiobus_unregister(greth->mdio);
1577 error2:
1578 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1579 error1:
1580 free_netdev(dev);
1581 return err;
1584 static int __devexit greth_of_remove(struct platform_device *of_dev)
1586 struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1587 struct greth_private *greth = netdev_priv(ndev);
1589 /* Free descriptor areas */
1590 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1592 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1594 dev_set_drvdata(&of_dev->dev, NULL);
1596 if (greth->phy)
1597 phy_stop(greth->phy);
1598 mdiobus_unregister(greth->mdio);
1600 unregister_netdev(ndev);
1601 free_netdev(ndev);
1603 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1605 return 0;
1608 static struct of_device_id greth_of_match[] = {
1610 .name = "GAISLER_ETHMAC",
1613 .name = "01_01d",
1618 MODULE_DEVICE_TABLE(of, greth_of_match);
1620 static struct platform_driver greth_of_driver = {
1621 .driver = {
1622 .name = "grlib-greth",
1623 .owner = THIS_MODULE,
1624 .of_match_table = greth_of_match,
1626 .probe = greth_of_probe,
1627 .remove = __devexit_p(greth_of_remove),
1630 static int __init greth_init(void)
1632 return platform_driver_register(&greth_of_driver);
1635 static void __exit greth_cleanup(void)
1637 platform_driver_unregister(&greth_of_driver);
1640 module_init(greth_init);
1641 module_exit(greth_cleanup);
1643 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1644 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1645 MODULE_LICENSE("GPL");