USB: usb_debug: use the generic kfifo-based write implementation
[linux-2.6.git] / drivers / net / greth.c
blob3a90430de918bc1b1a1de8bd05e0d0f5f11bda6e
1 /*
2 * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
4 * 2005-2009 (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/module.h>
26 #include <linux/uaccess.h>
27 #include <linux/init.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/ethtool.h>
31 #include <linux/skbuff.h>
32 #include <linux/io.h>
33 #include <linux/crc32.h>
34 #include <linux/mii.h>
35 #include <linux/of_device.h>
36 #include <linux/of_platform.h>
37 #include <linux/slab.h>
38 #include <asm/cacheflush.h>
39 #include <asm/byteorder.h>
41 #ifdef CONFIG_SPARC
42 #include <asm/idprom.h>
43 #endif
45 #include "greth.h"
47 #define GRETH_DEF_MSG_ENABLE \
48 (NETIF_MSG_DRV | \
49 NETIF_MSG_PROBE | \
50 NETIF_MSG_LINK | \
51 NETIF_MSG_IFDOWN | \
52 NETIF_MSG_IFUP | \
53 NETIF_MSG_RX_ERR | \
54 NETIF_MSG_TX_ERR)
56 static int greth_debug = -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */
57 module_param(greth_debug, int, 0);
58 MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
60 /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
61 static int macaddr[6];
62 module_param_array(macaddr, int, NULL, 0);
63 MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
65 static int greth_edcl = 1;
66 module_param(greth_edcl, int, 0);
67 MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
69 static int greth_open(struct net_device *dev);
70 static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
71 struct net_device *dev);
72 static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
73 struct net_device *dev);
74 static int greth_rx(struct net_device *dev, int limit);
75 static int greth_rx_gbit(struct net_device *dev, int limit);
76 static void greth_clean_tx(struct net_device *dev);
77 static void greth_clean_tx_gbit(struct net_device *dev);
78 static irqreturn_t greth_interrupt(int irq, void *dev_id);
79 static int greth_close(struct net_device *dev);
80 static int greth_set_mac_add(struct net_device *dev, void *p);
81 static void greth_set_multicast_list(struct net_device *dev);
83 #define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
84 #define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
85 #define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
86 #define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
88 #define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
89 #define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
90 #define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
92 static void greth_print_rx_packet(void *addr, int len)
94 print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
95 addr, len, true);
98 static void greth_print_tx_packet(struct sk_buff *skb)
100 int i;
101 int length;
103 if (skb_shinfo(skb)->nr_frags == 0)
104 length = skb->len;
105 else
106 length = skb_headlen(skb);
108 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
109 skb->data, length, true);
111 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
113 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
114 phys_to_virt(page_to_phys(skb_shinfo(skb)->frags[i].page)) +
115 skb_shinfo(skb)->frags[i].page_offset,
116 length, true);
120 static inline void greth_enable_tx(struct greth_private *greth)
122 wmb();
123 GRETH_REGORIN(greth->regs->control, GRETH_TXEN);
126 static inline void greth_disable_tx(struct greth_private *greth)
128 GRETH_REGANDIN(greth->regs->control, ~GRETH_TXEN);
131 static inline void greth_enable_rx(struct greth_private *greth)
133 wmb();
134 GRETH_REGORIN(greth->regs->control, GRETH_RXEN);
137 static inline void greth_disable_rx(struct greth_private *greth)
139 GRETH_REGANDIN(greth->regs->control, ~GRETH_RXEN);
142 static inline void greth_enable_irqs(struct greth_private *greth)
144 GRETH_REGORIN(greth->regs->control, GRETH_RXI | GRETH_TXI);
147 static inline void greth_disable_irqs(struct greth_private *greth)
149 GRETH_REGANDIN(greth->regs->control, ~(GRETH_RXI|GRETH_TXI));
152 static inline void greth_write_bd(u32 *bd, u32 val)
154 __raw_writel(cpu_to_be32(val), bd);
157 static inline u32 greth_read_bd(u32 *bd)
159 return be32_to_cpu(__raw_readl(bd));
162 static void greth_clean_rings(struct greth_private *greth)
164 int i;
165 struct greth_bd *rx_bdp = greth->rx_bd_base;
166 struct greth_bd *tx_bdp = greth->tx_bd_base;
168 if (greth->gbit_mac) {
170 /* Free and unmap RX buffers */
171 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
172 if (greth->rx_skbuff[i] != NULL) {
173 dev_kfree_skb(greth->rx_skbuff[i]);
174 dma_unmap_single(greth->dev,
175 greth_read_bd(&rx_bdp->addr),
176 MAX_FRAME_SIZE+NET_IP_ALIGN,
177 DMA_FROM_DEVICE);
181 /* TX buffers */
182 while (greth->tx_free < GRETH_TXBD_NUM) {
184 struct sk_buff *skb = greth->tx_skbuff[greth->tx_last];
185 int nr_frags = skb_shinfo(skb)->nr_frags;
186 tx_bdp = greth->tx_bd_base + greth->tx_last;
187 greth->tx_last = NEXT_TX(greth->tx_last);
189 dma_unmap_single(greth->dev,
190 greth_read_bd(&tx_bdp->addr),
191 skb_headlen(skb),
192 DMA_TO_DEVICE);
194 for (i = 0; i < nr_frags; i++) {
195 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
196 tx_bdp = greth->tx_bd_base + greth->tx_last;
198 dma_unmap_page(greth->dev,
199 greth_read_bd(&tx_bdp->addr),
200 frag->size,
201 DMA_TO_DEVICE);
203 greth->tx_last = NEXT_TX(greth->tx_last);
205 greth->tx_free += nr_frags+1;
206 dev_kfree_skb(skb);
210 } else { /* 10/100 Mbps MAC */
212 for (i = 0; i < GRETH_RXBD_NUM; i++, rx_bdp++) {
213 kfree(greth->rx_bufs[i]);
214 dma_unmap_single(greth->dev,
215 greth_read_bd(&rx_bdp->addr),
216 MAX_FRAME_SIZE,
217 DMA_FROM_DEVICE);
219 for (i = 0; i < GRETH_TXBD_NUM; i++, tx_bdp++) {
220 kfree(greth->tx_bufs[i]);
221 dma_unmap_single(greth->dev,
222 greth_read_bd(&tx_bdp->addr),
223 MAX_FRAME_SIZE,
224 DMA_TO_DEVICE);
229 static int greth_init_rings(struct greth_private *greth)
231 struct sk_buff *skb;
232 struct greth_bd *rx_bd, *tx_bd;
233 u32 dma_addr;
234 int i;
236 rx_bd = greth->rx_bd_base;
237 tx_bd = greth->tx_bd_base;
239 /* Initialize descriptor rings and buffers */
240 if (greth->gbit_mac) {
242 for (i = 0; i < GRETH_RXBD_NUM; i++) {
243 skb = netdev_alloc_skb(greth->netdev, MAX_FRAME_SIZE+NET_IP_ALIGN);
244 if (skb == NULL) {
245 if (netif_msg_ifup(greth))
246 dev_err(greth->dev, "Error allocating DMA ring.\n");
247 goto cleanup;
249 skb_reserve(skb, NET_IP_ALIGN);
250 dma_addr = dma_map_single(greth->dev,
251 skb->data,
252 MAX_FRAME_SIZE+NET_IP_ALIGN,
253 DMA_FROM_DEVICE);
255 if (dma_mapping_error(greth->dev, dma_addr)) {
256 if (netif_msg_ifup(greth))
257 dev_err(greth->dev, "Could not create initial DMA mapping\n");
258 goto cleanup;
260 greth->rx_skbuff[i] = skb;
261 greth_write_bd(&rx_bd[i].addr, dma_addr);
262 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
265 } else {
267 /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
268 for (i = 0; i < GRETH_RXBD_NUM; i++) {
270 greth->rx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
272 if (greth->rx_bufs[i] == NULL) {
273 if (netif_msg_ifup(greth))
274 dev_err(greth->dev, "Error allocating DMA ring.\n");
275 goto cleanup;
278 dma_addr = dma_map_single(greth->dev,
279 greth->rx_bufs[i],
280 MAX_FRAME_SIZE,
281 DMA_FROM_DEVICE);
283 if (dma_mapping_error(greth->dev, dma_addr)) {
284 if (netif_msg_ifup(greth))
285 dev_err(greth->dev, "Could not create initial DMA mapping\n");
286 goto cleanup;
288 greth_write_bd(&rx_bd[i].addr, dma_addr);
289 greth_write_bd(&rx_bd[i].stat, GRETH_BD_EN | GRETH_BD_IE);
291 for (i = 0; i < GRETH_TXBD_NUM; i++) {
293 greth->tx_bufs[i] = kmalloc(MAX_FRAME_SIZE, GFP_KERNEL);
295 if (greth->tx_bufs[i] == NULL) {
296 if (netif_msg_ifup(greth))
297 dev_err(greth->dev, "Error allocating DMA ring.\n");
298 goto cleanup;
301 dma_addr = dma_map_single(greth->dev,
302 greth->tx_bufs[i],
303 MAX_FRAME_SIZE,
304 DMA_TO_DEVICE);
306 if (dma_mapping_error(greth->dev, dma_addr)) {
307 if (netif_msg_ifup(greth))
308 dev_err(greth->dev, "Could not create initial DMA mapping\n");
309 goto cleanup;
311 greth_write_bd(&tx_bd[i].addr, dma_addr);
312 greth_write_bd(&tx_bd[i].stat, 0);
315 greth_write_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat,
316 greth_read_bd(&rx_bd[GRETH_RXBD_NUM - 1].stat) | GRETH_BD_WR);
318 /* Initialize pointers. */
319 greth->rx_cur = 0;
320 greth->tx_next = 0;
321 greth->tx_last = 0;
322 greth->tx_free = GRETH_TXBD_NUM;
324 /* Initialize descriptor base address */
325 GRETH_REGSAVE(greth->regs->tx_desc_p, greth->tx_bd_base_phys);
326 GRETH_REGSAVE(greth->regs->rx_desc_p, greth->rx_bd_base_phys);
328 return 0;
330 cleanup:
331 greth_clean_rings(greth);
332 return -ENOMEM;
335 static int greth_open(struct net_device *dev)
337 struct greth_private *greth = netdev_priv(dev);
338 int err;
340 err = greth_init_rings(greth);
341 if (err) {
342 if (netif_msg_ifup(greth))
343 dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
344 return err;
347 err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
348 if (err) {
349 if (netif_msg_ifup(greth))
350 dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
351 greth_clean_rings(greth);
352 return err;
355 if (netif_msg_ifup(greth))
356 dev_dbg(&dev->dev, " starting queue\n");
357 netif_start_queue(dev);
359 napi_enable(&greth->napi);
361 greth_enable_irqs(greth);
362 greth_enable_tx(greth);
363 greth_enable_rx(greth);
364 return 0;
368 static int greth_close(struct net_device *dev)
370 struct greth_private *greth = netdev_priv(dev);
372 napi_disable(&greth->napi);
374 greth_disable_tx(greth);
376 netif_stop_queue(dev);
378 free_irq(greth->irq, (void *) dev);
380 greth_clean_rings(greth);
382 return 0;
385 static netdev_tx_t
386 greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
388 struct greth_private *greth = netdev_priv(dev);
389 struct greth_bd *bdp;
390 int err = NETDEV_TX_OK;
391 u32 status, dma_addr;
393 bdp = greth->tx_bd_base + greth->tx_next;
395 if (unlikely(greth->tx_free <= 0)) {
396 netif_stop_queue(dev);
397 return NETDEV_TX_BUSY;
400 if (netif_msg_pktdata(greth))
401 greth_print_tx_packet(skb);
404 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
405 dev->stats.tx_errors++;
406 goto out;
409 dma_addr = greth_read_bd(&bdp->addr);
411 memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
413 dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
415 status = GRETH_BD_EN | (skb->len & GRETH_BD_LEN);
417 /* Wrap around descriptor ring */
418 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
419 status |= GRETH_BD_WR;
422 greth->tx_next = NEXT_TX(greth->tx_next);
423 greth->tx_free--;
425 /* No more descriptors */
426 if (unlikely(greth->tx_free == 0)) {
428 /* Free transmitted descriptors */
429 greth_clean_tx(dev);
431 /* If nothing was cleaned, stop queue & wait for irq */
432 if (unlikely(greth->tx_free == 0)) {
433 status |= GRETH_BD_IE;
434 netif_stop_queue(dev);
438 /* Write descriptor control word and enable transmission */
439 greth_write_bd(&bdp->stat, status);
440 greth_enable_tx(greth);
442 out:
443 dev_kfree_skb(skb);
444 return err;
448 static netdev_tx_t
449 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
451 struct greth_private *greth = netdev_priv(dev);
452 struct greth_bd *bdp;
453 u32 status = 0, dma_addr;
454 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
456 nr_frags = skb_shinfo(skb)->nr_frags;
458 if (greth->tx_free < nr_frags + 1) {
459 netif_stop_queue(dev);
460 err = NETDEV_TX_BUSY;
461 goto out;
464 if (netif_msg_pktdata(greth))
465 greth_print_tx_packet(skb);
467 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
468 dev->stats.tx_errors++;
469 goto out;
472 /* Save skb pointer. */
473 greth->tx_skbuff[greth->tx_next] = skb;
475 /* Linear buf */
476 if (nr_frags != 0)
477 status = GRETH_TXBD_MORE;
479 status |= GRETH_TXBD_CSALL;
480 status |= skb_headlen(skb) & GRETH_BD_LEN;
481 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
482 status |= GRETH_BD_WR;
485 bdp = greth->tx_bd_base + greth->tx_next;
486 greth_write_bd(&bdp->stat, status);
487 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
489 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
490 goto map_error;
492 greth_write_bd(&bdp->addr, dma_addr);
494 curr_tx = NEXT_TX(greth->tx_next);
496 /* Frags */
497 for (i = 0; i < nr_frags; i++) {
498 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
499 greth->tx_skbuff[curr_tx] = NULL;
500 bdp = greth->tx_bd_base + curr_tx;
502 status = GRETH_TXBD_CSALL;
503 status |= frag->size & GRETH_BD_LEN;
505 /* Wrap around descriptor ring */
506 if (curr_tx == GRETH_TXBD_NUM_MASK)
507 status |= GRETH_BD_WR;
509 /* More fragments left */
510 if (i < nr_frags - 1)
511 status |= GRETH_TXBD_MORE;
513 /* ... last fragment, check if out of descriptors */
514 else if (greth->tx_free - nr_frags - 1 < (MAX_SKB_FRAGS + 1)) {
516 /* Enable interrupts and stop queue */
517 status |= GRETH_BD_IE;
518 netif_stop_queue(dev);
521 greth_write_bd(&bdp->stat, status);
523 dma_addr = dma_map_page(greth->dev,
524 frag->page,
525 frag->page_offset,
526 frag->size,
527 DMA_TO_DEVICE);
529 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
530 goto frag_map_error;
532 greth_write_bd(&bdp->addr, dma_addr);
534 curr_tx = NEXT_TX(curr_tx);
537 wmb();
539 /* Enable the descriptors that we configured ... */
540 for (i = 0; i < nr_frags + 1; i++) {
541 bdp = greth->tx_bd_base + greth->tx_next;
542 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
543 greth->tx_next = NEXT_TX(greth->tx_next);
544 greth->tx_free--;
547 greth_enable_tx(greth);
549 return NETDEV_TX_OK;
551 frag_map_error:
552 /* Unmap SKB mappings that succeeded */
553 for (i = 0; greth->tx_next + i != curr_tx; i++) {
554 bdp = greth->tx_bd_base + greth->tx_next + i;
555 dma_unmap_single(greth->dev,
556 greth_read_bd(&bdp->addr),
557 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
558 DMA_TO_DEVICE);
560 map_error:
561 if (net_ratelimit())
562 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
563 dev_kfree_skb(skb);
564 out:
565 return err;
569 static irqreturn_t greth_interrupt(int irq, void *dev_id)
571 struct net_device *dev = dev_id;
572 struct greth_private *greth;
573 u32 status;
574 irqreturn_t retval = IRQ_NONE;
576 greth = netdev_priv(dev);
578 spin_lock(&greth->devlock);
580 /* Get the interrupt events that caused us to be here. */
581 status = GRETH_REGLOAD(greth->regs->status);
583 /* Handle rx and tx interrupts through poll */
584 if (status & (GRETH_INT_RX | GRETH_INT_TX)) {
586 /* Clear interrupt status */
587 GRETH_REGORIN(greth->regs->status,
588 status & (GRETH_INT_RX | GRETH_INT_TX));
590 retval = IRQ_HANDLED;
592 /* Disable interrupts and schedule poll() */
593 greth_disable_irqs(greth);
594 napi_schedule(&greth->napi);
597 mmiowb();
598 spin_unlock(&greth->devlock);
600 return retval;
603 static void greth_clean_tx(struct net_device *dev)
605 struct greth_private *greth;
606 struct greth_bd *bdp;
607 u32 stat;
609 greth = netdev_priv(dev);
611 while (1) {
612 bdp = greth->tx_bd_base + greth->tx_last;
613 stat = greth_read_bd(&bdp->stat);
615 if (unlikely(stat & GRETH_BD_EN))
616 break;
618 if (greth->tx_free == GRETH_TXBD_NUM)
619 break;
621 /* Check status for errors */
622 if (unlikely(stat & GRETH_TXBD_STATUS)) {
623 dev->stats.tx_errors++;
624 if (stat & GRETH_TXBD_ERR_AL)
625 dev->stats.tx_aborted_errors++;
626 if (stat & GRETH_TXBD_ERR_UE)
627 dev->stats.tx_fifo_errors++;
629 dev->stats.tx_packets++;
630 greth->tx_last = NEXT_TX(greth->tx_last);
631 greth->tx_free++;
634 if (greth->tx_free > 0) {
635 netif_wake_queue(dev);
640 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
642 /* Check status for errors */
643 if (unlikely(stat & GRETH_TXBD_STATUS)) {
644 dev->stats.tx_errors++;
645 if (stat & GRETH_TXBD_ERR_AL)
646 dev->stats.tx_aborted_errors++;
647 if (stat & GRETH_TXBD_ERR_UE)
648 dev->stats.tx_fifo_errors++;
649 if (stat & GRETH_TXBD_ERR_LC)
650 dev->stats.tx_aborted_errors++;
652 dev->stats.tx_packets++;
655 static void greth_clean_tx_gbit(struct net_device *dev)
657 struct greth_private *greth;
658 struct greth_bd *bdp, *bdp_last_frag;
659 struct sk_buff *skb;
660 u32 stat;
661 int nr_frags, i;
663 greth = netdev_priv(dev);
665 while (greth->tx_free < GRETH_TXBD_NUM) {
667 skb = greth->tx_skbuff[greth->tx_last];
669 nr_frags = skb_shinfo(skb)->nr_frags;
671 /* We only clean fully completed SKBs */
672 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
673 stat = bdp_last_frag->stat;
675 if (stat & GRETH_BD_EN)
676 break;
678 greth->tx_skbuff[greth->tx_last] = NULL;
680 greth_update_tx_stats(dev, stat);
682 bdp = greth->tx_bd_base + greth->tx_last;
684 greth->tx_last = NEXT_TX(greth->tx_last);
686 dma_unmap_single(greth->dev,
687 greth_read_bd(&bdp->addr),
688 skb_headlen(skb),
689 DMA_TO_DEVICE);
691 for (i = 0; i < nr_frags; i++) {
692 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
693 bdp = greth->tx_bd_base + greth->tx_last;
695 dma_unmap_page(greth->dev,
696 greth_read_bd(&bdp->addr),
697 frag->size,
698 DMA_TO_DEVICE);
700 greth->tx_last = NEXT_TX(greth->tx_last);
702 greth->tx_free += nr_frags+1;
703 dev_kfree_skb(skb);
705 if (greth->tx_free > (MAX_SKB_FRAGS + 1)) {
706 netif_wake_queue(dev);
710 static int greth_pending_packets(struct greth_private *greth)
712 struct greth_bd *bdp;
713 u32 status;
714 bdp = greth->rx_bd_base + greth->rx_cur;
715 status = greth_read_bd(&bdp->stat);
716 if (status & GRETH_BD_EN)
717 return 0;
718 else
719 return 1;
722 static int greth_rx(struct net_device *dev, int limit)
724 struct greth_private *greth;
725 struct greth_bd *bdp;
726 struct sk_buff *skb;
727 int pkt_len;
728 int bad, count;
729 u32 status, dma_addr;
731 greth = netdev_priv(dev);
733 for (count = 0; count < limit; ++count) {
735 bdp = greth->rx_bd_base + greth->rx_cur;
736 status = greth_read_bd(&bdp->stat);
737 dma_addr = greth_read_bd(&bdp->addr);
738 bad = 0;
740 if (unlikely(status & GRETH_BD_EN)) {
741 break;
744 /* Check status for errors. */
745 if (unlikely(status & GRETH_RXBD_STATUS)) {
746 if (status & GRETH_RXBD_ERR_FT) {
747 dev->stats.rx_length_errors++;
748 bad = 1;
750 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
751 dev->stats.rx_frame_errors++;
752 bad = 1;
754 if (status & GRETH_RXBD_ERR_CRC) {
755 dev->stats.rx_crc_errors++;
756 bad = 1;
759 if (unlikely(bad)) {
760 dev->stats.rx_errors++;
762 } else {
764 pkt_len = status & GRETH_BD_LEN;
766 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
768 if (unlikely(skb == NULL)) {
770 if (net_ratelimit())
771 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
773 dev->stats.rx_dropped++;
775 } else {
776 skb_reserve(skb, NET_IP_ALIGN);
777 skb->dev = dev;
779 dma_sync_single_for_cpu(greth->dev,
780 dma_addr,
781 pkt_len,
782 DMA_FROM_DEVICE);
784 if (netif_msg_pktdata(greth))
785 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
787 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
789 skb->protocol = eth_type_trans(skb, dev);
790 dev->stats.rx_packets++;
791 netif_receive_skb(skb);
795 status = GRETH_BD_EN | GRETH_BD_IE;
796 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
797 status |= GRETH_BD_WR;
800 wmb();
801 greth_write_bd(&bdp->stat, status);
803 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
805 greth_enable_rx(greth);
807 greth->rx_cur = NEXT_RX(greth->rx_cur);
810 return count;
813 static inline int hw_checksummed(u32 status)
816 if (status & GRETH_RXBD_IP_FRAG)
817 return 0;
819 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
820 return 0;
822 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
823 return 0;
825 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
826 return 0;
828 return 1;
831 static int greth_rx_gbit(struct net_device *dev, int limit)
833 struct greth_private *greth;
834 struct greth_bd *bdp;
835 struct sk_buff *skb, *newskb;
836 int pkt_len;
837 int bad, count = 0;
838 u32 status, dma_addr;
840 greth = netdev_priv(dev);
842 for (count = 0; count < limit; ++count) {
844 bdp = greth->rx_bd_base + greth->rx_cur;
845 skb = greth->rx_skbuff[greth->rx_cur];
846 status = greth_read_bd(&bdp->stat);
847 bad = 0;
849 if (status & GRETH_BD_EN)
850 break;
852 /* Check status for errors. */
853 if (unlikely(status & GRETH_RXBD_STATUS)) {
855 if (status & GRETH_RXBD_ERR_FT) {
856 dev->stats.rx_length_errors++;
857 bad = 1;
858 } else if (status &
859 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
860 dev->stats.rx_frame_errors++;
861 bad = 1;
862 } else if (status & GRETH_RXBD_ERR_CRC) {
863 dev->stats.rx_crc_errors++;
864 bad = 1;
868 /* Allocate new skb to replace current */
869 newskb = netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN);
871 if (!bad && newskb) {
872 skb_reserve(newskb, NET_IP_ALIGN);
874 dma_addr = dma_map_single(greth->dev,
875 newskb->data,
876 MAX_FRAME_SIZE + NET_IP_ALIGN,
877 DMA_FROM_DEVICE);
879 if (!dma_mapping_error(greth->dev, dma_addr)) {
880 /* Process the incoming frame. */
881 pkt_len = status & GRETH_BD_LEN;
883 dma_unmap_single(greth->dev,
884 greth_read_bd(&bdp->addr),
885 MAX_FRAME_SIZE + NET_IP_ALIGN,
886 DMA_FROM_DEVICE);
888 if (netif_msg_pktdata(greth))
889 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
891 skb_put(skb, pkt_len);
893 if (greth->flags & GRETH_FLAG_RX_CSUM && hw_checksummed(status))
894 skb->ip_summed = CHECKSUM_UNNECESSARY;
895 else
896 skb->ip_summed = CHECKSUM_NONE;
898 skb->dev = dev;
899 skb->protocol = eth_type_trans(skb, dev);
900 dev->stats.rx_packets++;
901 netif_receive_skb(skb);
903 greth->rx_skbuff[greth->rx_cur] = newskb;
904 greth_write_bd(&bdp->addr, dma_addr);
905 } else {
906 if (net_ratelimit())
907 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
908 dev_kfree_skb(newskb);
909 dev->stats.rx_dropped++;
911 } else {
912 if (net_ratelimit())
913 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
914 dev->stats.rx_dropped++;
917 status = GRETH_BD_EN | GRETH_BD_IE;
918 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
919 status |= GRETH_BD_WR;
922 wmb();
923 greth_write_bd(&bdp->stat, status);
924 greth_enable_rx(greth);
925 greth->rx_cur = NEXT_RX(greth->rx_cur);
928 return count;
932 static int greth_poll(struct napi_struct *napi, int budget)
934 struct greth_private *greth;
935 int work_done = 0;
936 greth = container_of(napi, struct greth_private, napi);
938 if (greth->gbit_mac) {
939 greth_clean_tx_gbit(greth->netdev);
940 } else {
941 greth_clean_tx(greth->netdev);
944 restart_poll:
945 if (greth->gbit_mac) {
946 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
947 } else {
948 work_done += greth_rx(greth->netdev, budget - work_done);
951 if (work_done < budget) {
953 napi_complete(napi);
955 if (greth_pending_packets(greth)) {
956 napi_reschedule(napi);
957 goto restart_poll;
961 greth_enable_irqs(greth);
962 return work_done;
965 static int greth_set_mac_add(struct net_device *dev, void *p)
967 struct sockaddr *addr = p;
968 struct greth_private *greth;
969 struct greth_regs *regs;
971 greth = netdev_priv(dev);
972 regs = (struct greth_regs *) greth->regs;
974 if (!is_valid_ether_addr(addr->sa_data))
975 return -EINVAL;
977 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
979 GRETH_REGSAVE(regs->esa_msb, addr->sa_data[0] << 8 | addr->sa_data[1]);
980 GRETH_REGSAVE(regs->esa_lsb,
981 addr->sa_data[2] << 24 | addr->
982 sa_data[3] << 16 | addr->sa_data[4] << 8 | addr->sa_data[5]);
983 return 0;
986 static u32 greth_hash_get_index(__u8 *addr)
988 return (ether_crc(6, addr)) & 0x3F;
991 static void greth_set_hash_filter(struct net_device *dev)
993 struct dev_mc_list *curr;
994 struct greth_private *greth = netdev_priv(dev);
995 struct greth_regs *regs = (struct greth_regs *) greth->regs;
996 u32 mc_filter[2];
997 unsigned int bitnr;
999 mc_filter[0] = mc_filter[1] = 0;
1001 netdev_for_each_mc_addr(curr, dev) {
1002 bitnr = greth_hash_get_index(curr->dmi_addr);
1003 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1006 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1007 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1010 static void greth_set_multicast_list(struct net_device *dev)
1012 int cfg;
1013 struct greth_private *greth = netdev_priv(dev);
1014 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1016 cfg = GRETH_REGLOAD(regs->control);
1017 if (dev->flags & IFF_PROMISC)
1018 cfg |= GRETH_CTRL_PR;
1019 else
1020 cfg &= ~GRETH_CTRL_PR;
1022 if (greth->multicast) {
1023 if (dev->flags & IFF_ALLMULTI) {
1024 GRETH_REGSAVE(regs->hash_msb, -1);
1025 GRETH_REGSAVE(regs->hash_lsb, -1);
1026 cfg |= GRETH_CTRL_MCEN;
1027 GRETH_REGSAVE(regs->control, cfg);
1028 return;
1031 if (netdev_mc_empty(dev)) {
1032 cfg &= ~GRETH_CTRL_MCEN;
1033 GRETH_REGSAVE(regs->control, cfg);
1034 return;
1037 /* Setup multicast filter */
1038 greth_set_hash_filter(dev);
1039 cfg |= GRETH_CTRL_MCEN;
1041 GRETH_REGSAVE(regs->control, cfg);
1044 static u32 greth_get_msglevel(struct net_device *dev)
1046 struct greth_private *greth = netdev_priv(dev);
1047 return greth->msg_enable;
1050 static void greth_set_msglevel(struct net_device *dev, u32 value)
1052 struct greth_private *greth = netdev_priv(dev);
1053 greth->msg_enable = value;
1055 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1057 struct greth_private *greth = netdev_priv(dev);
1058 struct phy_device *phy = greth->phy;
1060 if (!phy)
1061 return -ENODEV;
1063 return phy_ethtool_gset(phy, cmd);
1066 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1068 struct greth_private *greth = netdev_priv(dev);
1069 struct phy_device *phy = greth->phy;
1071 if (!phy)
1072 return -ENODEV;
1074 return phy_ethtool_sset(phy, cmd);
1077 static int greth_get_regs_len(struct net_device *dev)
1079 return sizeof(struct greth_regs);
1082 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1084 struct greth_private *greth = netdev_priv(dev);
1086 strncpy(info->driver, dev_driver_string(greth->dev), 32);
1087 strncpy(info->version, "revision: 1.0", 32);
1088 strncpy(info->bus_info, greth->dev->bus->name, 32);
1089 strncpy(info->fw_version, "N/A", 32);
1090 info->eedump_len = 0;
1091 info->regdump_len = sizeof(struct greth_regs);
1094 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1096 int i;
1097 struct greth_private *greth = netdev_priv(dev);
1098 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1099 u32 *buff = p;
1101 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1102 buff[i] = greth_read_bd(&greth_regs[i]);
1105 static u32 greth_get_rx_csum(struct net_device *dev)
1107 struct greth_private *greth = netdev_priv(dev);
1108 return (greth->flags & GRETH_FLAG_RX_CSUM) != 0;
1111 static int greth_set_rx_csum(struct net_device *dev, u32 data)
1113 struct greth_private *greth = netdev_priv(dev);
1115 spin_lock_bh(&greth->devlock);
1117 if (data)
1118 greth->flags |= GRETH_FLAG_RX_CSUM;
1119 else
1120 greth->flags &= ~GRETH_FLAG_RX_CSUM;
1122 spin_unlock_bh(&greth->devlock);
1124 return 0;
1127 static u32 greth_get_tx_csum(struct net_device *dev)
1129 return (dev->features & NETIF_F_IP_CSUM) != 0;
1132 static int greth_set_tx_csum(struct net_device *dev, u32 data)
1134 netif_tx_lock_bh(dev);
1135 ethtool_op_set_tx_csum(dev, data);
1136 netif_tx_unlock_bh(dev);
1137 return 0;
1140 static const struct ethtool_ops greth_ethtool_ops = {
1141 .get_msglevel = greth_get_msglevel,
1142 .set_msglevel = greth_set_msglevel,
1143 .get_settings = greth_get_settings,
1144 .set_settings = greth_set_settings,
1145 .get_drvinfo = greth_get_drvinfo,
1146 .get_regs_len = greth_get_regs_len,
1147 .get_regs = greth_get_regs,
1148 .get_rx_csum = greth_get_rx_csum,
1149 .set_rx_csum = greth_set_rx_csum,
1150 .get_tx_csum = greth_get_tx_csum,
1151 .set_tx_csum = greth_set_tx_csum,
1152 .get_link = ethtool_op_get_link,
1155 static struct net_device_ops greth_netdev_ops = {
1156 .ndo_open = greth_open,
1157 .ndo_stop = greth_close,
1158 .ndo_start_xmit = greth_start_xmit,
1159 .ndo_set_mac_address = greth_set_mac_add,
1160 .ndo_validate_addr = eth_validate_addr,
1163 static inline int wait_for_mdio(struct greth_private *greth)
1165 unsigned long timeout = jiffies + 4*HZ/100;
1166 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1167 if (time_after(jiffies, timeout))
1168 return 0;
1170 return 1;
1173 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1175 struct greth_private *greth = bus->priv;
1176 int data;
1178 if (!wait_for_mdio(greth))
1179 return -EBUSY;
1181 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1183 if (!wait_for_mdio(greth))
1184 return -EBUSY;
1186 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1187 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1188 return data;
1190 } else {
1191 return -1;
1195 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1197 struct greth_private *greth = bus->priv;
1199 if (!wait_for_mdio(greth))
1200 return -EBUSY;
1202 GRETH_REGSAVE(greth->regs->mdio,
1203 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1205 if (!wait_for_mdio(greth))
1206 return -EBUSY;
1208 return 0;
1211 static int greth_mdio_reset(struct mii_bus *bus)
1213 return 0;
1216 static void greth_link_change(struct net_device *dev)
1218 struct greth_private *greth = netdev_priv(dev);
1219 struct phy_device *phydev = greth->phy;
1220 unsigned long flags;
1222 int status_change = 0;
1224 spin_lock_irqsave(&greth->devlock, flags);
1226 if (phydev->link) {
1228 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1230 GRETH_REGANDIN(greth->regs->control,
1231 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB));
1233 if (phydev->duplex)
1234 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_FD);
1236 if (phydev->speed == SPEED_100) {
1238 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_SP);
1241 else if (phydev->speed == SPEED_1000)
1242 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_GB);
1244 greth->speed = phydev->speed;
1245 greth->duplex = phydev->duplex;
1246 status_change = 1;
1250 if (phydev->link != greth->link) {
1251 if (!phydev->link) {
1252 greth->speed = 0;
1253 greth->duplex = -1;
1255 greth->link = phydev->link;
1257 status_change = 1;
1260 spin_unlock_irqrestore(&greth->devlock, flags);
1262 if (status_change) {
1263 if (phydev->link)
1264 pr_debug("%s: link up (%d/%s)\n",
1265 dev->name, phydev->speed,
1266 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1267 else
1268 pr_debug("%s: link down\n", dev->name);
1272 static int greth_mdio_probe(struct net_device *dev)
1274 struct greth_private *greth = netdev_priv(dev);
1275 struct phy_device *phy = NULL;
1276 int ret;
1278 /* Find the first PHY */
1279 phy = phy_find_first(greth->mdio);
1281 if (!phy) {
1282 if (netif_msg_probe(greth))
1283 dev_err(&dev->dev, "no PHY found\n");
1284 return -ENXIO;
1287 ret = phy_connect_direct(dev, phy, &greth_link_change,
1288 0, greth->gbit_mac ?
1289 PHY_INTERFACE_MODE_GMII :
1290 PHY_INTERFACE_MODE_MII);
1291 if (ret) {
1292 if (netif_msg_ifup(greth))
1293 dev_err(&dev->dev, "could not attach to PHY\n");
1294 return ret;
1297 if (greth->gbit_mac)
1298 phy->supported &= PHY_GBIT_FEATURES;
1299 else
1300 phy->supported &= PHY_BASIC_FEATURES;
1302 phy->advertising = phy->supported;
1304 greth->link = 0;
1305 greth->speed = 0;
1306 greth->duplex = -1;
1307 greth->phy = phy;
1309 return 0;
1312 static inline int phy_aneg_done(struct phy_device *phydev)
1314 int retval;
1316 retval = phy_read(phydev, MII_BMSR);
1318 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1321 static int greth_mdio_init(struct greth_private *greth)
1323 int ret, phy;
1324 unsigned long timeout;
1326 greth->mdio = mdiobus_alloc();
1327 if (!greth->mdio) {
1328 return -ENOMEM;
1331 greth->mdio->name = "greth-mdio";
1332 snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1333 greth->mdio->read = greth_mdio_read;
1334 greth->mdio->write = greth_mdio_write;
1335 greth->mdio->reset = greth_mdio_reset;
1336 greth->mdio->priv = greth;
1338 greth->mdio->irq = greth->mdio_irqs;
1340 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1341 greth->mdio->irq[phy] = PHY_POLL;
1343 ret = mdiobus_register(greth->mdio);
1344 if (ret) {
1345 goto error;
1348 ret = greth_mdio_probe(greth->netdev);
1349 if (ret) {
1350 if (netif_msg_probe(greth))
1351 dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1352 goto unreg_mdio;
1355 phy_start(greth->phy);
1357 /* If Ethernet debug link is used make autoneg happen right away */
1358 if (greth->edcl && greth_edcl == 1) {
1359 phy_start_aneg(greth->phy);
1360 timeout = jiffies + 6*HZ;
1361 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1363 genphy_read_status(greth->phy);
1364 greth_link_change(greth->netdev);
1367 return 0;
1369 unreg_mdio:
1370 mdiobus_unregister(greth->mdio);
1371 error:
1372 mdiobus_free(greth->mdio);
1373 return ret;
1376 /* Initialize the GRETH MAC */
1377 static int __devinit greth_of_probe(struct of_device *ofdev, const struct of_device_id *match)
1379 struct net_device *dev;
1380 struct greth_private *greth;
1381 struct greth_regs *regs;
1383 int i;
1384 int err;
1385 int tmp;
1386 unsigned long timeout;
1388 dev = alloc_etherdev(sizeof(struct greth_private));
1390 if (dev == NULL)
1391 return -ENOMEM;
1393 greth = netdev_priv(dev);
1394 greth->netdev = dev;
1395 greth->dev = &ofdev->dev;
1397 if (greth_debug > 0)
1398 greth->msg_enable = greth_debug;
1399 else
1400 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1402 spin_lock_init(&greth->devlock);
1404 greth->regs = of_ioremap(&ofdev->resource[0], 0,
1405 resource_size(&ofdev->resource[0]),
1406 "grlib-greth regs");
1408 if (greth->regs == NULL) {
1409 if (netif_msg_probe(greth))
1410 dev_err(greth->dev, "ioremap failure.\n");
1411 err = -EIO;
1412 goto error1;
1415 regs = (struct greth_regs *) greth->regs;
1416 greth->irq = ofdev->irqs[0];
1418 dev_set_drvdata(greth->dev, dev);
1419 SET_NETDEV_DEV(dev, greth->dev);
1421 if (netif_msg_probe(greth))
1422 dev_dbg(greth->dev, "reseting controller.\n");
1424 /* Reset the controller. */
1425 GRETH_REGSAVE(regs->control, GRETH_RESET);
1427 /* Wait for MAC to reset itself */
1428 timeout = jiffies + HZ/100;
1429 while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1430 if (time_after(jiffies, timeout)) {
1431 err = -EIO;
1432 if (netif_msg_probe(greth))
1433 dev_err(greth->dev, "timeout when waiting for reset.\n");
1434 goto error2;
1438 /* Get default PHY address */
1439 greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1441 /* Check if we have GBIT capable MAC */
1442 tmp = GRETH_REGLOAD(regs->control);
1443 greth->gbit_mac = (tmp >> 27) & 1;
1445 /* Check for multicast capability */
1446 greth->multicast = (tmp >> 25) & 1;
1448 greth->edcl = (tmp >> 31) & 1;
1450 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1451 * it doesn't interfere with the software */
1452 if (greth->edcl != 0)
1453 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1455 /* Check if MAC can handle MDIO interrupts */
1456 greth->mdio_int_en = (tmp >> 26) & 1;
1458 err = greth_mdio_init(greth);
1459 if (err) {
1460 if (netif_msg_probe(greth))
1461 dev_err(greth->dev, "failed to register MDIO bus\n");
1462 goto error2;
1465 /* Allocate TX descriptor ring in coherent memory */
1466 greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1467 1024,
1468 &greth->tx_bd_base_phys,
1469 GFP_KERNEL);
1471 if (!greth->tx_bd_base) {
1472 if (netif_msg_probe(greth))
1473 dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1474 err = -ENOMEM;
1475 goto error3;
1478 memset(greth->tx_bd_base, 0, 1024);
1480 /* Allocate RX descriptor ring in coherent memory */
1481 greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1482 1024,
1483 &greth->rx_bd_base_phys,
1484 GFP_KERNEL);
1486 if (!greth->rx_bd_base) {
1487 if (netif_msg_probe(greth))
1488 dev_err(greth->dev, "could not allocate descriptor memory.\n");
1489 err = -ENOMEM;
1490 goto error4;
1493 memset(greth->rx_bd_base, 0, 1024);
1495 /* Get MAC address from: module param, OF property or ID prom */
1496 for (i = 0; i < 6; i++) {
1497 if (macaddr[i] != 0)
1498 break;
1500 if (i == 6) {
1501 const unsigned char *addr;
1502 int len;
1503 addr = of_get_property(ofdev->node, "local-mac-address", &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->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_HIGHDMA;
1536 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1537 greth->flags = GRETH_FLAG_RX_CSUM;
1540 if (greth->multicast) {
1541 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1542 dev->flags |= IFF_MULTICAST;
1543 } else {
1544 dev->flags &= ~IFF_MULTICAST;
1547 dev->netdev_ops = &greth_netdev_ops;
1548 dev->ethtool_ops = &greth_ethtool_ops;
1550 if (register_netdev(dev)) {
1551 if (netif_msg_probe(greth))
1552 dev_err(greth->dev, "netdevice registration failed.\n");
1553 err = -ENOMEM;
1554 goto error5;
1557 /* setup NAPI */
1558 memset(&greth->napi, 0, sizeof(greth->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 of_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",
1607 MODULE_DEVICE_TABLE(of, greth_of_match);
1609 static struct of_platform_driver greth_of_driver = {
1610 .name = "grlib-greth",
1611 .match_table = greth_of_match,
1612 .probe = greth_of_probe,
1613 .remove = __devexit_p(greth_of_remove),
1614 .driver = {
1615 .owner = THIS_MODULE,
1616 .name = "grlib-greth",
1620 static int __init greth_init(void)
1622 return of_register_platform_driver(&greth_of_driver);
1625 static void __exit greth_cleanup(void)
1627 of_unregister_platform_driver(&greth_of_driver);
1630 module_init(greth_init);
1631 module_exit(greth_cleanup);
1633 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1634 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1635 MODULE_LICENSE("GPL");