thp: skip transhuge pages in ksm for now
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / greth.c
blob27d6960ce09ea7a8d7f12ef970f9de5c769ebd37
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_checksum_none_assert(skb);
898 skb->protocol = eth_type_trans(skb, dev);
899 dev->stats.rx_packets++;
900 netif_receive_skb(skb);
902 greth->rx_skbuff[greth->rx_cur] = newskb;
903 greth_write_bd(&bdp->addr, dma_addr);
904 } else {
905 if (net_ratelimit())
906 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
907 dev_kfree_skb(newskb);
908 dev->stats.rx_dropped++;
910 } else {
911 if (net_ratelimit())
912 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
913 dev->stats.rx_dropped++;
916 status = GRETH_BD_EN | GRETH_BD_IE;
917 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
918 status |= GRETH_BD_WR;
921 wmb();
922 greth_write_bd(&bdp->stat, status);
923 greth_enable_rx(greth);
924 greth->rx_cur = NEXT_RX(greth->rx_cur);
927 return count;
931 static int greth_poll(struct napi_struct *napi, int budget)
933 struct greth_private *greth;
934 int work_done = 0;
935 greth = container_of(napi, struct greth_private, napi);
937 if (greth->gbit_mac) {
938 greth_clean_tx_gbit(greth->netdev);
939 } else {
940 greth_clean_tx(greth->netdev);
943 restart_poll:
944 if (greth->gbit_mac) {
945 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
946 } else {
947 work_done += greth_rx(greth->netdev, budget - work_done);
950 if (work_done < budget) {
952 napi_complete(napi);
954 if (greth_pending_packets(greth)) {
955 napi_reschedule(napi);
956 goto restart_poll;
960 greth_enable_irqs(greth);
961 return work_done;
964 static int greth_set_mac_add(struct net_device *dev, void *p)
966 struct sockaddr *addr = p;
967 struct greth_private *greth;
968 struct greth_regs *regs;
970 greth = netdev_priv(dev);
971 regs = (struct greth_regs *) greth->regs;
973 if (!is_valid_ether_addr(addr->sa_data))
974 return -EINVAL;
976 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
978 GRETH_REGSAVE(regs->esa_msb, addr->sa_data[0] << 8 | addr->sa_data[1]);
979 GRETH_REGSAVE(regs->esa_lsb,
980 addr->sa_data[2] << 24 | addr->
981 sa_data[3] << 16 | addr->sa_data[4] << 8 | addr->sa_data[5]);
982 return 0;
985 static u32 greth_hash_get_index(__u8 *addr)
987 return (ether_crc(6, addr)) & 0x3F;
990 static void greth_set_hash_filter(struct net_device *dev)
992 struct netdev_hw_addr *ha;
993 struct greth_private *greth = netdev_priv(dev);
994 struct greth_regs *regs = (struct greth_regs *) greth->regs;
995 u32 mc_filter[2];
996 unsigned int bitnr;
998 mc_filter[0] = mc_filter[1] = 0;
1000 netdev_for_each_mc_addr(ha, dev) {
1001 bitnr = greth_hash_get_index(ha->addr);
1002 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1005 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1006 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1009 static void greth_set_multicast_list(struct net_device *dev)
1011 int cfg;
1012 struct greth_private *greth = netdev_priv(dev);
1013 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1015 cfg = GRETH_REGLOAD(regs->control);
1016 if (dev->flags & IFF_PROMISC)
1017 cfg |= GRETH_CTRL_PR;
1018 else
1019 cfg &= ~GRETH_CTRL_PR;
1021 if (greth->multicast) {
1022 if (dev->flags & IFF_ALLMULTI) {
1023 GRETH_REGSAVE(regs->hash_msb, -1);
1024 GRETH_REGSAVE(regs->hash_lsb, -1);
1025 cfg |= GRETH_CTRL_MCEN;
1026 GRETH_REGSAVE(regs->control, cfg);
1027 return;
1030 if (netdev_mc_empty(dev)) {
1031 cfg &= ~GRETH_CTRL_MCEN;
1032 GRETH_REGSAVE(regs->control, cfg);
1033 return;
1036 /* Setup multicast filter */
1037 greth_set_hash_filter(dev);
1038 cfg |= GRETH_CTRL_MCEN;
1040 GRETH_REGSAVE(regs->control, cfg);
1043 static u32 greth_get_msglevel(struct net_device *dev)
1045 struct greth_private *greth = netdev_priv(dev);
1046 return greth->msg_enable;
1049 static void greth_set_msglevel(struct net_device *dev, u32 value)
1051 struct greth_private *greth = netdev_priv(dev);
1052 greth->msg_enable = value;
1054 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1056 struct greth_private *greth = netdev_priv(dev);
1057 struct phy_device *phy = greth->phy;
1059 if (!phy)
1060 return -ENODEV;
1062 return phy_ethtool_gset(phy, cmd);
1065 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1067 struct greth_private *greth = netdev_priv(dev);
1068 struct phy_device *phy = greth->phy;
1070 if (!phy)
1071 return -ENODEV;
1073 return phy_ethtool_sset(phy, cmd);
1076 static int greth_get_regs_len(struct net_device *dev)
1078 return sizeof(struct greth_regs);
1081 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1083 struct greth_private *greth = netdev_priv(dev);
1085 strncpy(info->driver, dev_driver_string(greth->dev), 32);
1086 strncpy(info->version, "revision: 1.0", 32);
1087 strncpy(info->bus_info, greth->dev->bus->name, 32);
1088 strncpy(info->fw_version, "N/A", 32);
1089 info->eedump_len = 0;
1090 info->regdump_len = sizeof(struct greth_regs);
1093 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1095 int i;
1096 struct greth_private *greth = netdev_priv(dev);
1097 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1098 u32 *buff = p;
1100 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1101 buff[i] = greth_read_bd(&greth_regs[i]);
1104 static u32 greth_get_rx_csum(struct net_device *dev)
1106 struct greth_private *greth = netdev_priv(dev);
1107 return (greth->flags & GRETH_FLAG_RX_CSUM) != 0;
1110 static int greth_set_rx_csum(struct net_device *dev, u32 data)
1112 struct greth_private *greth = netdev_priv(dev);
1114 spin_lock_bh(&greth->devlock);
1116 if (data)
1117 greth->flags |= GRETH_FLAG_RX_CSUM;
1118 else
1119 greth->flags &= ~GRETH_FLAG_RX_CSUM;
1121 spin_unlock_bh(&greth->devlock);
1123 return 0;
1126 static u32 greth_get_tx_csum(struct net_device *dev)
1128 return (dev->features & NETIF_F_IP_CSUM) != 0;
1131 static int greth_set_tx_csum(struct net_device *dev, u32 data)
1133 netif_tx_lock_bh(dev);
1134 ethtool_op_set_tx_csum(dev, data);
1135 netif_tx_unlock_bh(dev);
1136 return 0;
1139 static const struct ethtool_ops greth_ethtool_ops = {
1140 .get_msglevel = greth_get_msglevel,
1141 .set_msglevel = greth_set_msglevel,
1142 .get_settings = greth_get_settings,
1143 .set_settings = greth_set_settings,
1144 .get_drvinfo = greth_get_drvinfo,
1145 .get_regs_len = greth_get_regs_len,
1146 .get_regs = greth_get_regs,
1147 .get_rx_csum = greth_get_rx_csum,
1148 .set_rx_csum = greth_set_rx_csum,
1149 .get_tx_csum = greth_get_tx_csum,
1150 .set_tx_csum = greth_set_tx_csum,
1151 .get_link = ethtool_op_get_link,
1154 static struct net_device_ops greth_netdev_ops = {
1155 .ndo_open = greth_open,
1156 .ndo_stop = greth_close,
1157 .ndo_start_xmit = greth_start_xmit,
1158 .ndo_set_mac_address = greth_set_mac_add,
1159 .ndo_validate_addr = eth_validate_addr,
1162 static inline int wait_for_mdio(struct greth_private *greth)
1164 unsigned long timeout = jiffies + 4*HZ/100;
1165 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1166 if (time_after(jiffies, timeout))
1167 return 0;
1169 return 1;
1172 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1174 struct greth_private *greth = bus->priv;
1175 int data;
1177 if (!wait_for_mdio(greth))
1178 return -EBUSY;
1180 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1182 if (!wait_for_mdio(greth))
1183 return -EBUSY;
1185 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1186 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1187 return data;
1189 } else {
1190 return -1;
1194 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1196 struct greth_private *greth = bus->priv;
1198 if (!wait_for_mdio(greth))
1199 return -EBUSY;
1201 GRETH_REGSAVE(greth->regs->mdio,
1202 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1204 if (!wait_for_mdio(greth))
1205 return -EBUSY;
1207 return 0;
1210 static int greth_mdio_reset(struct mii_bus *bus)
1212 return 0;
1215 static void greth_link_change(struct net_device *dev)
1217 struct greth_private *greth = netdev_priv(dev);
1218 struct phy_device *phydev = greth->phy;
1219 unsigned long flags;
1221 int status_change = 0;
1223 spin_lock_irqsave(&greth->devlock, flags);
1225 if (phydev->link) {
1227 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1229 GRETH_REGANDIN(greth->regs->control,
1230 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB));
1232 if (phydev->duplex)
1233 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_FD);
1235 if (phydev->speed == SPEED_100) {
1237 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_SP);
1240 else if (phydev->speed == SPEED_1000)
1241 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_GB);
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, const struct of_device_id *match)
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->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 err = register_netdev(dev);
1551 if (err) {
1552 if (netif_msg_probe(greth))
1553 dev_err(greth->dev, "netdevice registration failed.\n");
1554 goto error5;
1557 /* setup NAPI */
1558 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1560 return 0;
1562 error5:
1563 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1564 error4:
1565 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1566 error3:
1567 mdiobus_unregister(greth->mdio);
1568 error2:
1569 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1570 error1:
1571 free_netdev(dev);
1572 return err;
1575 static int __devexit greth_of_remove(struct platform_device *of_dev)
1577 struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1578 struct greth_private *greth = netdev_priv(ndev);
1580 /* Free descriptor areas */
1581 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1583 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1585 dev_set_drvdata(&of_dev->dev, NULL);
1587 if (greth->phy)
1588 phy_stop(greth->phy);
1589 mdiobus_unregister(greth->mdio);
1591 unregister_netdev(ndev);
1592 free_netdev(ndev);
1594 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1596 return 0;
1599 static struct of_device_id greth_of_match[] = {
1601 .name = "GAISLER_ETHMAC",
1606 MODULE_DEVICE_TABLE(of, greth_of_match);
1608 static struct of_platform_driver greth_of_driver = {
1609 .driver = {
1610 .name = "grlib-greth",
1611 .owner = THIS_MODULE,
1612 .of_match_table = greth_of_match,
1614 .probe = greth_of_probe,
1615 .remove = __devexit_p(greth_of_remove),
1618 static int __init greth_init(void)
1620 return of_register_platform_driver(&greth_of_driver);
1623 static void __exit greth_cleanup(void)
1625 of_unregister_platform_driver(&greth_of_driver);
1628 module_init(greth_init);
1629 module_exit(greth_cleanup);
1631 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1632 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1633 MODULE_LICENSE("GPL");