cris: add arch/cris/include/asm/serial.h
[linux-2.6/next.git] / drivers / net / greth.c
blob16ce45c1193419fc7193f59de18049708ef3a2e2
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);
432 /* Wrap around descriptor ring */
433 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
434 status |= GRETH_BD_WR;
437 greth->tx_next = NEXT_TX(greth->tx_next);
438 greth->tx_free--;
440 /* Write descriptor control word and enable transmission */
441 greth_write_bd(&bdp->stat, status);
442 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
443 greth_enable_tx(greth);
444 spin_unlock_irqrestore(&greth->devlock, flags);
446 out:
447 dev_kfree_skb(skb);
448 return err;
452 static netdev_tx_t
453 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
455 struct greth_private *greth = netdev_priv(dev);
456 struct greth_bd *bdp;
457 u32 status = 0, dma_addr, ctrl;
458 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
459 unsigned long flags;
461 nr_frags = skb_shinfo(skb)->nr_frags;
463 /* Clean TX Ring */
464 greth_clean_tx_gbit(dev);
466 if (greth->tx_free < nr_frags + 1) {
467 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
468 ctrl = GRETH_REGLOAD(greth->regs->control);
469 /* Enable TX IRQ only if not already in poll() routine */
470 if (ctrl & GRETH_RXI)
471 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
472 netif_stop_queue(dev);
473 spin_unlock_irqrestore(&greth->devlock, flags);
474 err = NETDEV_TX_BUSY;
475 goto out;
478 if (netif_msg_pktdata(greth))
479 greth_print_tx_packet(skb);
481 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
482 dev->stats.tx_errors++;
483 goto out;
486 /* Save skb pointer. */
487 greth->tx_skbuff[greth->tx_next] = skb;
489 /* Linear buf */
490 if (nr_frags != 0)
491 status = GRETH_TXBD_MORE;
493 status |= GRETH_TXBD_CSALL;
494 status |= skb_headlen(skb) & GRETH_BD_LEN;
495 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
496 status |= GRETH_BD_WR;
499 bdp = greth->tx_bd_base + greth->tx_next;
500 greth_write_bd(&bdp->stat, status);
501 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
503 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
504 goto map_error;
506 greth_write_bd(&bdp->addr, dma_addr);
508 curr_tx = NEXT_TX(greth->tx_next);
510 /* Frags */
511 for (i = 0; i < nr_frags; i++) {
512 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
513 greth->tx_skbuff[curr_tx] = NULL;
514 bdp = greth->tx_bd_base + curr_tx;
516 status = GRETH_TXBD_CSALL | GRETH_BD_EN;
517 status |= frag->size & GRETH_BD_LEN;
519 /* Wrap around descriptor ring */
520 if (curr_tx == GRETH_TXBD_NUM_MASK)
521 status |= GRETH_BD_WR;
523 /* More fragments left */
524 if (i < nr_frags - 1)
525 status |= GRETH_TXBD_MORE;
526 else
527 status |= GRETH_BD_IE; /* enable IRQ on last fragment */
529 greth_write_bd(&bdp->stat, status);
531 dma_addr = dma_map_page(greth->dev,
532 frag->page,
533 frag->page_offset,
534 frag->size,
535 DMA_TO_DEVICE);
537 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
538 goto frag_map_error;
540 greth_write_bd(&bdp->addr, dma_addr);
542 curr_tx = NEXT_TX(curr_tx);
545 wmb();
547 /* Enable the descriptor chain by enabling the first descriptor */
548 bdp = greth->tx_bd_base + greth->tx_next;
549 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
550 greth->tx_next = curr_tx;
551 greth->tx_free -= nr_frags + 1;
553 wmb();
555 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
556 greth_enable_tx(greth);
557 spin_unlock_irqrestore(&greth->devlock, flags);
559 return NETDEV_TX_OK;
561 frag_map_error:
562 /* Unmap SKB mappings that succeeded and disable descriptor */
563 for (i = 0; greth->tx_next + i != curr_tx; i++) {
564 bdp = greth->tx_bd_base + greth->tx_next + i;
565 dma_unmap_single(greth->dev,
566 greth_read_bd(&bdp->addr),
567 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
568 DMA_TO_DEVICE);
569 greth_write_bd(&bdp->stat, 0);
571 map_error:
572 if (net_ratelimit())
573 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
574 dev_kfree_skb(skb);
575 out:
576 return err;
579 static irqreturn_t greth_interrupt(int irq, void *dev_id)
581 struct net_device *dev = dev_id;
582 struct greth_private *greth;
583 u32 status, ctrl;
584 irqreturn_t retval = IRQ_NONE;
586 greth = netdev_priv(dev);
588 spin_lock(&greth->devlock);
590 /* Get the interrupt events that caused us to be here. */
591 status = GRETH_REGLOAD(greth->regs->status);
593 /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
594 * set regardless of whether IRQ is enabled or not. Especially
595 * important when shared IRQ.
597 ctrl = GRETH_REGLOAD(greth->regs->control);
599 /* Handle rx and tx interrupts through poll */
600 if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
601 ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
602 retval = IRQ_HANDLED;
604 /* Disable interrupts and schedule poll() */
605 greth_disable_irqs(greth);
606 napi_schedule(&greth->napi);
609 mmiowb();
610 spin_unlock(&greth->devlock);
612 return retval;
615 static void greth_clean_tx(struct net_device *dev)
617 struct greth_private *greth;
618 struct greth_bd *bdp;
619 u32 stat;
621 greth = netdev_priv(dev);
623 while (1) {
624 bdp = greth->tx_bd_base + greth->tx_last;
625 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
626 mb();
627 stat = greth_read_bd(&bdp->stat);
629 if (unlikely(stat & GRETH_BD_EN))
630 break;
632 if (greth->tx_free == GRETH_TXBD_NUM)
633 break;
635 /* Check status for errors */
636 if (unlikely(stat & GRETH_TXBD_STATUS)) {
637 dev->stats.tx_errors++;
638 if (stat & GRETH_TXBD_ERR_AL)
639 dev->stats.tx_aborted_errors++;
640 if (stat & GRETH_TXBD_ERR_UE)
641 dev->stats.tx_fifo_errors++;
643 dev->stats.tx_packets++;
644 greth->tx_last = NEXT_TX(greth->tx_last);
645 greth->tx_free++;
648 if (greth->tx_free > 0) {
649 netif_wake_queue(dev);
654 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
656 /* Check status for errors */
657 if (unlikely(stat & GRETH_TXBD_STATUS)) {
658 dev->stats.tx_errors++;
659 if (stat & GRETH_TXBD_ERR_AL)
660 dev->stats.tx_aborted_errors++;
661 if (stat & GRETH_TXBD_ERR_UE)
662 dev->stats.tx_fifo_errors++;
663 if (stat & GRETH_TXBD_ERR_LC)
664 dev->stats.tx_aborted_errors++;
666 dev->stats.tx_packets++;
669 static void greth_clean_tx_gbit(struct net_device *dev)
671 struct greth_private *greth;
672 struct greth_bd *bdp, *bdp_last_frag;
673 struct sk_buff *skb;
674 u32 stat;
675 int nr_frags, i;
677 greth = netdev_priv(dev);
679 while (greth->tx_free < GRETH_TXBD_NUM) {
681 skb = greth->tx_skbuff[greth->tx_last];
683 nr_frags = skb_shinfo(skb)->nr_frags;
685 /* We only clean fully completed SKBs */
686 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
688 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
689 mb();
690 stat = greth_read_bd(&bdp_last_frag->stat);
692 if (stat & GRETH_BD_EN)
693 break;
695 greth->tx_skbuff[greth->tx_last] = NULL;
697 greth_update_tx_stats(dev, stat);
699 bdp = greth->tx_bd_base + greth->tx_last;
701 greth->tx_last = NEXT_TX(greth->tx_last);
703 dma_unmap_single(greth->dev,
704 greth_read_bd(&bdp->addr),
705 skb_headlen(skb),
706 DMA_TO_DEVICE);
708 for (i = 0; i < nr_frags; i++) {
709 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
710 bdp = greth->tx_bd_base + greth->tx_last;
712 dma_unmap_page(greth->dev,
713 greth_read_bd(&bdp->addr),
714 frag->size,
715 DMA_TO_DEVICE);
717 greth->tx_last = NEXT_TX(greth->tx_last);
719 greth->tx_free += nr_frags+1;
720 dev_kfree_skb(skb);
723 if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1)))
724 netif_wake_queue(dev);
727 static int greth_rx(struct net_device *dev, int limit)
729 struct greth_private *greth;
730 struct greth_bd *bdp;
731 struct sk_buff *skb;
732 int pkt_len;
733 int bad, count;
734 u32 status, dma_addr;
735 unsigned long flags;
737 greth = netdev_priv(dev);
739 for (count = 0; count < limit; ++count) {
741 bdp = greth->rx_bd_base + greth->rx_cur;
742 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
743 mb();
744 status = greth_read_bd(&bdp->stat);
746 if (unlikely(status & GRETH_BD_EN)) {
747 break;
750 dma_addr = greth_read_bd(&bdp->addr);
751 bad = 0;
753 /* Check status for errors. */
754 if (unlikely(status & GRETH_RXBD_STATUS)) {
755 if (status & GRETH_RXBD_ERR_FT) {
756 dev->stats.rx_length_errors++;
757 bad = 1;
759 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
760 dev->stats.rx_frame_errors++;
761 bad = 1;
763 if (status & GRETH_RXBD_ERR_CRC) {
764 dev->stats.rx_crc_errors++;
765 bad = 1;
768 if (unlikely(bad)) {
769 dev->stats.rx_errors++;
771 } else {
773 pkt_len = status & GRETH_BD_LEN;
775 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
777 if (unlikely(skb == NULL)) {
779 if (net_ratelimit())
780 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
782 dev->stats.rx_dropped++;
784 } else {
785 skb_reserve(skb, NET_IP_ALIGN);
786 skb->dev = dev;
788 dma_sync_single_for_cpu(greth->dev,
789 dma_addr,
790 pkt_len,
791 DMA_FROM_DEVICE);
793 if (netif_msg_pktdata(greth))
794 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
796 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
798 skb->protocol = eth_type_trans(skb, dev);
799 dev->stats.rx_packets++;
800 netif_receive_skb(skb);
804 status = GRETH_BD_EN | GRETH_BD_IE;
805 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
806 status |= GRETH_BD_WR;
809 wmb();
810 greth_write_bd(&bdp->stat, status);
812 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
814 spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
815 greth_enable_rx(greth);
816 spin_unlock_irqrestore(&greth->devlock, flags);
818 greth->rx_cur = NEXT_RX(greth->rx_cur);
821 return count;
824 static inline int hw_checksummed(u32 status)
827 if (status & GRETH_RXBD_IP_FRAG)
828 return 0;
830 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
831 return 0;
833 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
834 return 0;
836 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
837 return 0;
839 return 1;
842 static int greth_rx_gbit(struct net_device *dev, int limit)
844 struct greth_private *greth;
845 struct greth_bd *bdp;
846 struct sk_buff *skb, *newskb;
847 int pkt_len;
848 int bad, count = 0;
849 u32 status, dma_addr;
850 unsigned long flags;
852 greth = netdev_priv(dev);
854 for (count = 0; count < limit; ++count) {
856 bdp = greth->rx_bd_base + greth->rx_cur;
857 skb = greth->rx_skbuff[greth->rx_cur];
858 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
859 mb();
860 status = greth_read_bd(&bdp->stat);
861 bad = 0;
863 if (status & GRETH_BD_EN)
864 break;
866 /* Check status for errors. */
867 if (unlikely(status & GRETH_RXBD_STATUS)) {
869 if (status & GRETH_RXBD_ERR_FT) {
870 dev->stats.rx_length_errors++;
871 bad = 1;
872 } else if (status &
873 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
874 dev->stats.rx_frame_errors++;
875 bad = 1;
876 } else if (status & GRETH_RXBD_ERR_CRC) {
877 dev->stats.rx_crc_errors++;
878 bad = 1;
882 /* Allocate new skb to replace current, not needed if the
883 * current skb can be reused */
884 if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
885 skb_reserve(newskb, NET_IP_ALIGN);
887 dma_addr = dma_map_single(greth->dev,
888 newskb->data,
889 MAX_FRAME_SIZE + NET_IP_ALIGN,
890 DMA_FROM_DEVICE);
892 if (!dma_mapping_error(greth->dev, dma_addr)) {
893 /* Process the incoming frame. */
894 pkt_len = status & GRETH_BD_LEN;
896 dma_unmap_single(greth->dev,
897 greth_read_bd(&bdp->addr),
898 MAX_FRAME_SIZE + NET_IP_ALIGN,
899 DMA_FROM_DEVICE);
901 if (netif_msg_pktdata(greth))
902 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
904 skb_put(skb, pkt_len);
906 if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
907 skb->ip_summed = CHECKSUM_UNNECESSARY;
908 else
909 skb_checksum_none_assert(skb);
911 skb->protocol = eth_type_trans(skb, dev);
912 dev->stats.rx_packets++;
913 netif_receive_skb(skb);
915 greth->rx_skbuff[greth->rx_cur] = newskb;
916 greth_write_bd(&bdp->addr, dma_addr);
917 } else {
918 if (net_ratelimit())
919 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
920 dev_kfree_skb(newskb);
921 /* reusing current skb, so it is a drop */
922 dev->stats.rx_dropped++;
924 } else if (bad) {
925 /* Bad Frame transfer, the skb is reused */
926 dev->stats.rx_dropped++;
927 } else {
928 /* Failed Allocating a new skb. This is rather stupid
929 * but the current "filled" skb is reused, as if
930 * transfer failure. One could argue that RX descriptor
931 * table handling should be divided into cleaning and
932 * filling as the TX part of the driver
934 if (net_ratelimit())
935 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
936 /* reusing current skb, so it is a drop */
937 dev->stats.rx_dropped++;
940 status = GRETH_BD_EN | GRETH_BD_IE;
941 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
942 status |= GRETH_BD_WR;
945 wmb();
946 greth_write_bd(&bdp->stat, status);
947 spin_lock_irqsave(&greth->devlock, flags);
948 greth_enable_rx(greth);
949 spin_unlock_irqrestore(&greth->devlock, flags);
950 greth->rx_cur = NEXT_RX(greth->rx_cur);
953 return count;
957 static int greth_poll(struct napi_struct *napi, int budget)
959 struct greth_private *greth;
960 int work_done = 0;
961 unsigned long flags;
962 u32 mask, ctrl;
963 greth = container_of(napi, struct greth_private, napi);
965 restart_txrx_poll:
966 if (netif_queue_stopped(greth->netdev)) {
967 if (greth->gbit_mac)
968 greth_clean_tx_gbit(greth->netdev);
969 else
970 greth_clean_tx(greth->netdev);
973 if (greth->gbit_mac) {
974 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
975 } else {
976 work_done += greth_rx(greth->netdev, budget - work_done);
979 if (work_done < budget) {
981 spin_lock_irqsave(&greth->devlock, flags);
983 ctrl = GRETH_REGLOAD(greth->regs->control);
984 if (netif_queue_stopped(greth->netdev)) {
985 GRETH_REGSAVE(greth->regs->control,
986 ctrl | GRETH_TXI | GRETH_RXI);
987 mask = GRETH_INT_RX | GRETH_INT_RE |
988 GRETH_INT_TX | GRETH_INT_TE;
989 } else {
990 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
991 mask = GRETH_INT_RX | GRETH_INT_RE;
994 if (GRETH_REGLOAD(greth->regs->status) & mask) {
995 GRETH_REGSAVE(greth->regs->control, ctrl);
996 spin_unlock_irqrestore(&greth->devlock, flags);
997 goto restart_txrx_poll;
998 } else {
999 __napi_complete(napi);
1000 spin_unlock_irqrestore(&greth->devlock, flags);
1004 return work_done;
1007 static int greth_set_mac_add(struct net_device *dev, void *p)
1009 struct sockaddr *addr = p;
1010 struct greth_private *greth;
1011 struct greth_regs *regs;
1013 greth = netdev_priv(dev);
1014 regs = (struct greth_regs *) greth->regs;
1016 if (!is_valid_ether_addr(addr->sa_data))
1017 return -EINVAL;
1019 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1020 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1021 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1022 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1024 return 0;
1027 static u32 greth_hash_get_index(__u8 *addr)
1029 return (ether_crc(6, addr)) & 0x3F;
1032 static void greth_set_hash_filter(struct net_device *dev)
1034 struct netdev_hw_addr *ha;
1035 struct greth_private *greth = netdev_priv(dev);
1036 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1037 u32 mc_filter[2];
1038 unsigned int bitnr;
1040 mc_filter[0] = mc_filter[1] = 0;
1042 netdev_for_each_mc_addr(ha, dev) {
1043 bitnr = greth_hash_get_index(ha->addr);
1044 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1047 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1048 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1051 static void greth_set_multicast_list(struct net_device *dev)
1053 int cfg;
1054 struct greth_private *greth = netdev_priv(dev);
1055 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1057 cfg = GRETH_REGLOAD(regs->control);
1058 if (dev->flags & IFF_PROMISC)
1059 cfg |= GRETH_CTRL_PR;
1060 else
1061 cfg &= ~GRETH_CTRL_PR;
1063 if (greth->multicast) {
1064 if (dev->flags & IFF_ALLMULTI) {
1065 GRETH_REGSAVE(regs->hash_msb, -1);
1066 GRETH_REGSAVE(regs->hash_lsb, -1);
1067 cfg |= GRETH_CTRL_MCEN;
1068 GRETH_REGSAVE(regs->control, cfg);
1069 return;
1072 if (netdev_mc_empty(dev)) {
1073 cfg &= ~GRETH_CTRL_MCEN;
1074 GRETH_REGSAVE(regs->control, cfg);
1075 return;
1078 /* Setup multicast filter */
1079 greth_set_hash_filter(dev);
1080 cfg |= GRETH_CTRL_MCEN;
1082 GRETH_REGSAVE(regs->control, cfg);
1085 static u32 greth_get_msglevel(struct net_device *dev)
1087 struct greth_private *greth = netdev_priv(dev);
1088 return greth->msg_enable;
1091 static void greth_set_msglevel(struct net_device *dev, u32 value)
1093 struct greth_private *greth = netdev_priv(dev);
1094 greth->msg_enable = value;
1096 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1098 struct greth_private *greth = netdev_priv(dev);
1099 struct phy_device *phy = greth->phy;
1101 if (!phy)
1102 return -ENODEV;
1104 return phy_ethtool_gset(phy, cmd);
1107 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1109 struct greth_private *greth = netdev_priv(dev);
1110 struct phy_device *phy = greth->phy;
1112 if (!phy)
1113 return -ENODEV;
1115 return phy_ethtool_sset(phy, cmd);
1118 static int greth_get_regs_len(struct net_device *dev)
1120 return sizeof(struct greth_regs);
1123 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1125 struct greth_private *greth = netdev_priv(dev);
1127 strncpy(info->driver, dev_driver_string(greth->dev), 32);
1128 strncpy(info->version, "revision: 1.0", 32);
1129 strncpy(info->bus_info, greth->dev->bus->name, 32);
1130 strncpy(info->fw_version, "N/A", 32);
1131 info->eedump_len = 0;
1132 info->regdump_len = sizeof(struct greth_regs);
1135 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1137 int i;
1138 struct greth_private *greth = netdev_priv(dev);
1139 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1140 u32 *buff = p;
1142 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1143 buff[i] = greth_read_bd(&greth_regs[i]);
1146 static const struct ethtool_ops greth_ethtool_ops = {
1147 .get_msglevel = greth_get_msglevel,
1148 .set_msglevel = greth_set_msglevel,
1149 .get_settings = greth_get_settings,
1150 .set_settings = greth_set_settings,
1151 .get_drvinfo = greth_get_drvinfo,
1152 .get_regs_len = greth_get_regs_len,
1153 .get_regs = greth_get_regs,
1154 .get_link = ethtool_op_get_link,
1157 static struct net_device_ops greth_netdev_ops = {
1158 .ndo_open = greth_open,
1159 .ndo_stop = greth_close,
1160 .ndo_start_xmit = greth_start_xmit,
1161 .ndo_set_mac_address = greth_set_mac_add,
1162 .ndo_validate_addr = eth_validate_addr,
1165 static inline int wait_for_mdio(struct greth_private *greth)
1167 unsigned long timeout = jiffies + 4*HZ/100;
1168 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1169 if (time_after(jiffies, timeout))
1170 return 0;
1172 return 1;
1175 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1177 struct greth_private *greth = bus->priv;
1178 int data;
1180 if (!wait_for_mdio(greth))
1181 return -EBUSY;
1183 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1185 if (!wait_for_mdio(greth))
1186 return -EBUSY;
1188 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1189 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1190 return data;
1192 } else {
1193 return -1;
1197 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1199 struct greth_private *greth = bus->priv;
1201 if (!wait_for_mdio(greth))
1202 return -EBUSY;
1204 GRETH_REGSAVE(greth->regs->mdio,
1205 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1207 if (!wait_for_mdio(greth))
1208 return -EBUSY;
1210 return 0;
1213 static int greth_mdio_reset(struct mii_bus *bus)
1215 return 0;
1218 static void greth_link_change(struct net_device *dev)
1220 struct greth_private *greth = netdev_priv(dev);
1221 struct phy_device *phydev = greth->phy;
1222 unsigned long flags;
1223 int status_change = 0;
1224 u32 ctrl;
1226 spin_lock_irqsave(&greth->devlock, flags);
1228 if (phydev->link) {
1230 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1231 ctrl = GRETH_REGLOAD(greth->regs->control) &
1232 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
1234 if (phydev->duplex)
1235 ctrl |= GRETH_CTRL_FD;
1237 if (phydev->speed == SPEED_100)
1238 ctrl |= GRETH_CTRL_SP;
1239 else if (phydev->speed == SPEED_1000)
1240 ctrl |= GRETH_CTRL_GB;
1242 GRETH_REGSAVE(greth->regs->control, ctrl);
1243 greth->speed = phydev->speed;
1244 greth->duplex = phydev->duplex;
1245 status_change = 1;
1249 if (phydev->link != greth->link) {
1250 if (!phydev->link) {
1251 greth->speed = 0;
1252 greth->duplex = -1;
1254 greth->link = phydev->link;
1256 status_change = 1;
1259 spin_unlock_irqrestore(&greth->devlock, flags);
1261 if (status_change) {
1262 if (phydev->link)
1263 pr_debug("%s: link up (%d/%s)\n",
1264 dev->name, phydev->speed,
1265 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1266 else
1267 pr_debug("%s: link down\n", dev->name);
1271 static int greth_mdio_probe(struct net_device *dev)
1273 struct greth_private *greth = netdev_priv(dev);
1274 struct phy_device *phy = NULL;
1275 int ret;
1277 /* Find the first PHY */
1278 phy = phy_find_first(greth->mdio);
1280 if (!phy) {
1281 if (netif_msg_probe(greth))
1282 dev_err(&dev->dev, "no PHY found\n");
1283 return -ENXIO;
1286 ret = phy_connect_direct(dev, phy, &greth_link_change,
1287 0, greth->gbit_mac ?
1288 PHY_INTERFACE_MODE_GMII :
1289 PHY_INTERFACE_MODE_MII);
1290 if (ret) {
1291 if (netif_msg_ifup(greth))
1292 dev_err(&dev->dev, "could not attach to PHY\n");
1293 return ret;
1296 if (greth->gbit_mac)
1297 phy->supported &= PHY_GBIT_FEATURES;
1298 else
1299 phy->supported &= PHY_BASIC_FEATURES;
1301 phy->advertising = phy->supported;
1303 greth->link = 0;
1304 greth->speed = 0;
1305 greth->duplex = -1;
1306 greth->phy = phy;
1308 return 0;
1311 static inline int phy_aneg_done(struct phy_device *phydev)
1313 int retval;
1315 retval = phy_read(phydev, MII_BMSR);
1317 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1320 static int greth_mdio_init(struct greth_private *greth)
1322 int ret, phy;
1323 unsigned long timeout;
1325 greth->mdio = mdiobus_alloc();
1326 if (!greth->mdio) {
1327 return -ENOMEM;
1330 greth->mdio->name = "greth-mdio";
1331 snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1332 greth->mdio->read = greth_mdio_read;
1333 greth->mdio->write = greth_mdio_write;
1334 greth->mdio->reset = greth_mdio_reset;
1335 greth->mdio->priv = greth;
1337 greth->mdio->irq = greth->mdio_irqs;
1339 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1340 greth->mdio->irq[phy] = PHY_POLL;
1342 ret = mdiobus_register(greth->mdio);
1343 if (ret) {
1344 goto error;
1347 ret = greth_mdio_probe(greth->netdev);
1348 if (ret) {
1349 if (netif_msg_probe(greth))
1350 dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1351 goto unreg_mdio;
1354 phy_start(greth->phy);
1356 /* If Ethernet debug link is used make autoneg happen right away */
1357 if (greth->edcl && greth_edcl == 1) {
1358 phy_start_aneg(greth->phy);
1359 timeout = jiffies + 6*HZ;
1360 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1362 genphy_read_status(greth->phy);
1363 greth_link_change(greth->netdev);
1366 return 0;
1368 unreg_mdio:
1369 mdiobus_unregister(greth->mdio);
1370 error:
1371 mdiobus_free(greth->mdio);
1372 return ret;
1375 /* Initialize the GRETH MAC */
1376 static int __devinit greth_of_probe(struct platform_device *ofdev)
1378 struct net_device *dev;
1379 struct greth_private *greth;
1380 struct greth_regs *regs;
1382 int i;
1383 int err;
1384 int tmp;
1385 unsigned long timeout;
1387 dev = alloc_etherdev(sizeof(struct greth_private));
1389 if (dev == NULL)
1390 return -ENOMEM;
1392 greth = netdev_priv(dev);
1393 greth->netdev = dev;
1394 greth->dev = &ofdev->dev;
1396 if (greth_debug > 0)
1397 greth->msg_enable = greth_debug;
1398 else
1399 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1401 spin_lock_init(&greth->devlock);
1403 greth->regs = of_ioremap(&ofdev->resource[0], 0,
1404 resource_size(&ofdev->resource[0]),
1405 "grlib-greth regs");
1407 if (greth->regs == NULL) {
1408 if (netif_msg_probe(greth))
1409 dev_err(greth->dev, "ioremap failure.\n");
1410 err = -EIO;
1411 goto error1;
1414 regs = (struct greth_regs *) greth->regs;
1415 greth->irq = ofdev->archdata.irqs[0];
1417 dev_set_drvdata(greth->dev, dev);
1418 SET_NETDEV_DEV(dev, greth->dev);
1420 if (netif_msg_probe(greth))
1421 dev_dbg(greth->dev, "reseting controller.\n");
1423 /* Reset the controller. */
1424 GRETH_REGSAVE(regs->control, GRETH_RESET);
1426 /* Wait for MAC to reset itself */
1427 timeout = jiffies + HZ/100;
1428 while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1429 if (time_after(jiffies, timeout)) {
1430 err = -EIO;
1431 if (netif_msg_probe(greth))
1432 dev_err(greth->dev, "timeout when waiting for reset.\n");
1433 goto error2;
1437 /* Get default PHY address */
1438 greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1440 /* Check if we have GBIT capable MAC */
1441 tmp = GRETH_REGLOAD(regs->control);
1442 greth->gbit_mac = (tmp >> 27) & 1;
1444 /* Check for multicast capability */
1445 greth->multicast = (tmp >> 25) & 1;
1447 greth->edcl = (tmp >> 31) & 1;
1449 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1450 * it doesn't interfere with the software */
1451 if (greth->edcl != 0)
1452 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1454 /* Check if MAC can handle MDIO interrupts */
1455 greth->mdio_int_en = (tmp >> 26) & 1;
1457 err = greth_mdio_init(greth);
1458 if (err) {
1459 if (netif_msg_probe(greth))
1460 dev_err(greth->dev, "failed to register MDIO bus\n");
1461 goto error2;
1464 /* Allocate TX descriptor ring in coherent memory */
1465 greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1466 1024,
1467 &greth->tx_bd_base_phys,
1468 GFP_KERNEL);
1470 if (!greth->tx_bd_base) {
1471 if (netif_msg_probe(greth))
1472 dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1473 err = -ENOMEM;
1474 goto error3;
1477 memset(greth->tx_bd_base, 0, 1024);
1479 /* Allocate RX descriptor ring in coherent memory */
1480 greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1481 1024,
1482 &greth->rx_bd_base_phys,
1483 GFP_KERNEL);
1485 if (!greth->rx_bd_base) {
1486 if (netif_msg_probe(greth))
1487 dev_err(greth->dev, "could not allocate descriptor memory.\n");
1488 err = -ENOMEM;
1489 goto error4;
1492 memset(greth->rx_bd_base, 0, 1024);
1494 /* Get MAC address from: module param, OF property or ID prom */
1495 for (i = 0; i < 6; i++) {
1496 if (macaddr[i] != 0)
1497 break;
1499 if (i == 6) {
1500 const unsigned char *addr;
1501 int len;
1502 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1503 &len);
1504 if (addr != NULL && len == 6) {
1505 for (i = 0; i < 6; i++)
1506 macaddr[i] = (unsigned int) addr[i];
1507 } else {
1508 #ifdef CONFIG_SPARC
1509 for (i = 0; i < 6; i++)
1510 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1511 #endif
1515 for (i = 0; i < 6; i++)
1516 dev->dev_addr[i] = macaddr[i];
1518 macaddr[5]++;
1520 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1521 if (netif_msg_probe(greth))
1522 dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1523 err = -EINVAL;
1524 goto error5;
1527 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1528 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1529 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1531 /* Clear all pending interrupts except PHY irq */
1532 GRETH_REGSAVE(regs->status, 0xFF);
1534 if (greth->gbit_mac) {
1535 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1536 NETIF_F_RXCSUM;
1537 dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1538 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1541 if (greth->multicast) {
1542 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1543 dev->flags |= IFF_MULTICAST;
1544 } else {
1545 dev->flags &= ~IFF_MULTICAST;
1548 dev->netdev_ops = &greth_netdev_ops;
1549 dev->ethtool_ops = &greth_ethtool_ops;
1551 err = register_netdev(dev);
1552 if (err) {
1553 if (netif_msg_probe(greth))
1554 dev_err(greth->dev, "netdevice registration failed.\n");
1555 goto error5;
1558 /* setup NAPI */
1559 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1561 return 0;
1563 error5:
1564 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1565 error4:
1566 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1567 error3:
1568 mdiobus_unregister(greth->mdio);
1569 error2:
1570 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1571 error1:
1572 free_netdev(dev);
1573 return err;
1576 static int __devexit greth_of_remove(struct platform_device *of_dev)
1578 struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1579 struct greth_private *greth = netdev_priv(ndev);
1581 /* Free descriptor areas */
1582 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1584 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1586 dev_set_drvdata(&of_dev->dev, NULL);
1588 if (greth->phy)
1589 phy_stop(greth->phy);
1590 mdiobus_unregister(greth->mdio);
1592 unregister_netdev(ndev);
1593 free_netdev(ndev);
1595 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1597 return 0;
1600 static struct of_device_id greth_of_match[] = {
1602 .name = "GAISLER_ETHMAC",
1605 .name = "01_01d",
1610 MODULE_DEVICE_TABLE(of, greth_of_match);
1612 static struct platform_driver greth_of_driver = {
1613 .driver = {
1614 .name = "grlib-greth",
1615 .owner = THIS_MODULE,
1616 .of_match_table = greth_of_match,
1618 .probe = greth_of_probe,
1619 .remove = __devexit_p(greth_of_remove),
1622 static int __init greth_init(void)
1624 return platform_driver_register(&greth_of_driver);
1627 static void __exit greth_cleanup(void)
1629 platform_driver_unregister(&greth_of_driver);
1632 module_init(greth_init);
1633 module_exit(greth_cleanup);
1635 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1636 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1637 MODULE_LICENSE("GPL");