ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / drivers / net / greth.c
blob672f096fe0905d8a59bf6efb495922e51b031c48
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/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 GRETH_REGSAVE(greth->regs->status, 0xFF);
361 napi_enable(&greth->napi);
363 greth_enable_irqs(greth);
364 greth_enable_tx(greth);
365 greth_enable_rx(greth);
366 return 0;
370 static int greth_close(struct net_device *dev)
372 struct greth_private *greth = netdev_priv(dev);
374 napi_disable(&greth->napi);
376 greth_disable_irqs(greth);
377 greth_disable_tx(greth);
378 greth_disable_rx(greth);
380 netif_stop_queue(dev);
382 free_irq(greth->irq, (void *) dev);
384 greth_clean_rings(greth);
386 return 0;
389 static netdev_tx_t
390 greth_start_xmit(struct sk_buff *skb, struct net_device *dev)
392 struct greth_private *greth = netdev_priv(dev);
393 struct greth_bd *bdp;
394 int err = NETDEV_TX_OK;
395 u32 status, dma_addr, ctrl;
396 unsigned long flags;
398 /* Clean TX Ring */
399 greth_clean_tx(greth->netdev);
401 if (unlikely(greth->tx_free <= 0)) {
402 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
403 ctrl = GRETH_REGLOAD(greth->regs->control);
404 /* Enable TX IRQ only if not already in poll() routine */
405 if (ctrl & GRETH_RXI)
406 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
407 netif_stop_queue(dev);
408 spin_unlock_irqrestore(&greth->devlock, flags);
409 return NETDEV_TX_BUSY;
412 if (netif_msg_pktdata(greth))
413 greth_print_tx_packet(skb);
416 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
417 dev->stats.tx_errors++;
418 goto out;
421 bdp = greth->tx_bd_base + greth->tx_next;
422 dma_addr = greth_read_bd(&bdp->addr);
424 memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
426 dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
428 status = GRETH_BD_EN | GRETH_BD_IE | (skb->len & GRETH_BD_LEN);
430 /* Wrap around descriptor ring */
431 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
432 status |= GRETH_BD_WR;
435 greth->tx_next = NEXT_TX(greth->tx_next);
436 greth->tx_free--;
438 /* Write descriptor control word and enable transmission */
439 greth_write_bd(&bdp->stat, status);
440 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
441 greth_enable_tx(greth);
442 spin_unlock_irqrestore(&greth->devlock, flags);
444 out:
445 dev_kfree_skb(skb);
446 return err;
450 static netdev_tx_t
451 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
453 struct greth_private *greth = netdev_priv(dev);
454 struct greth_bd *bdp;
455 u32 status = 0, dma_addr, ctrl;
456 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
457 unsigned long flags;
459 nr_frags = skb_shinfo(skb)->nr_frags;
461 /* Clean TX Ring */
462 greth_clean_tx_gbit(dev);
464 if (greth->tx_free < nr_frags + 1) {
465 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
466 ctrl = GRETH_REGLOAD(greth->regs->control);
467 /* Enable TX IRQ only if not already in poll() routine */
468 if (ctrl & GRETH_RXI)
469 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
470 netif_stop_queue(dev);
471 spin_unlock_irqrestore(&greth->devlock, flags);
472 err = NETDEV_TX_BUSY;
473 goto out;
476 if (netif_msg_pktdata(greth))
477 greth_print_tx_packet(skb);
479 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
480 dev->stats.tx_errors++;
481 goto out;
484 /* Save skb pointer. */
485 greth->tx_skbuff[greth->tx_next] = skb;
487 /* Linear buf */
488 if (nr_frags != 0)
489 status = GRETH_TXBD_MORE;
491 status |= GRETH_TXBD_CSALL;
492 status |= skb_headlen(skb) & GRETH_BD_LEN;
493 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
494 status |= GRETH_BD_WR;
497 bdp = greth->tx_bd_base + greth->tx_next;
498 greth_write_bd(&bdp->stat, status);
499 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
501 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
502 goto map_error;
504 greth_write_bd(&bdp->addr, dma_addr);
506 curr_tx = NEXT_TX(greth->tx_next);
508 /* Frags */
509 for (i = 0; i < nr_frags; i++) {
510 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
511 greth->tx_skbuff[curr_tx] = NULL;
512 bdp = greth->tx_bd_base + curr_tx;
514 status = GRETH_TXBD_CSALL | GRETH_BD_EN;
515 status |= frag->size & GRETH_BD_LEN;
517 /* Wrap around descriptor ring */
518 if (curr_tx == GRETH_TXBD_NUM_MASK)
519 status |= GRETH_BD_WR;
521 /* More fragments left */
522 if (i < nr_frags - 1)
523 status |= GRETH_TXBD_MORE;
524 else
525 status |= GRETH_BD_IE; /* enable IRQ on last fragment */
527 greth_write_bd(&bdp->stat, status);
529 dma_addr = dma_map_page(greth->dev,
530 frag->page,
531 frag->page_offset,
532 frag->size,
533 DMA_TO_DEVICE);
535 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
536 goto frag_map_error;
538 greth_write_bd(&bdp->addr, dma_addr);
540 curr_tx = NEXT_TX(curr_tx);
543 wmb();
545 /* Enable the descriptor chain by enabling the first descriptor */
546 bdp = greth->tx_bd_base + greth->tx_next;
547 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
548 greth->tx_next = curr_tx;
549 greth->tx_free -= nr_frags + 1;
551 wmb();
553 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
554 greth_enable_tx(greth);
555 spin_unlock_irqrestore(&greth->devlock, flags);
557 return NETDEV_TX_OK;
559 frag_map_error:
560 /* Unmap SKB mappings that succeeded and disable descriptor */
561 for (i = 0; greth->tx_next + i != curr_tx; i++) {
562 bdp = greth->tx_bd_base + greth->tx_next + i;
563 dma_unmap_single(greth->dev,
564 greth_read_bd(&bdp->addr),
565 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
566 DMA_TO_DEVICE);
567 greth_write_bd(&bdp->stat, 0);
569 map_error:
570 if (net_ratelimit())
571 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
572 dev_kfree_skb(skb);
573 out:
574 return err;
577 static irqreturn_t greth_interrupt(int irq, void *dev_id)
579 struct net_device *dev = dev_id;
580 struct greth_private *greth;
581 u32 status, ctrl;
582 irqreturn_t retval = IRQ_NONE;
584 greth = netdev_priv(dev);
586 spin_lock(&greth->devlock);
588 /* Get the interrupt events that caused us to be here. */
589 status = GRETH_REGLOAD(greth->regs->status);
591 /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
592 * set regardless of whether IRQ is enabled or not. Especially
593 * important when shared IRQ.
595 ctrl = GRETH_REGLOAD(greth->regs->control);
597 /* Handle rx and tx interrupts through poll */
598 if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
599 ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
600 retval = IRQ_HANDLED;
602 /* Disable interrupts and schedule poll() */
603 greth_disable_irqs(greth);
604 napi_schedule(&greth->napi);
607 mmiowb();
608 spin_unlock(&greth->devlock);
610 return retval;
613 static void greth_clean_tx(struct net_device *dev)
615 struct greth_private *greth;
616 struct greth_bd *bdp;
617 u32 stat;
619 greth = netdev_priv(dev);
621 while (1) {
622 bdp = greth->tx_bd_base + greth->tx_last;
623 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
624 mb();
625 stat = greth_read_bd(&bdp->stat);
627 if (unlikely(stat & GRETH_BD_EN))
628 break;
630 if (greth->tx_free == GRETH_TXBD_NUM)
631 break;
633 /* Check status for errors */
634 if (unlikely(stat & GRETH_TXBD_STATUS)) {
635 dev->stats.tx_errors++;
636 if (stat & GRETH_TXBD_ERR_AL)
637 dev->stats.tx_aborted_errors++;
638 if (stat & GRETH_TXBD_ERR_UE)
639 dev->stats.tx_fifo_errors++;
641 dev->stats.tx_packets++;
642 greth->tx_last = NEXT_TX(greth->tx_last);
643 greth->tx_free++;
646 if (greth->tx_free > 0) {
647 netif_wake_queue(dev);
652 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
654 /* Check status for errors */
655 if (unlikely(stat & GRETH_TXBD_STATUS)) {
656 dev->stats.tx_errors++;
657 if (stat & GRETH_TXBD_ERR_AL)
658 dev->stats.tx_aborted_errors++;
659 if (stat & GRETH_TXBD_ERR_UE)
660 dev->stats.tx_fifo_errors++;
661 if (stat & GRETH_TXBD_ERR_LC)
662 dev->stats.tx_aborted_errors++;
664 dev->stats.tx_packets++;
667 static void greth_clean_tx_gbit(struct net_device *dev)
669 struct greth_private *greth;
670 struct greth_bd *bdp, *bdp_last_frag;
671 struct sk_buff *skb;
672 u32 stat;
673 int nr_frags, i;
675 greth = netdev_priv(dev);
677 while (greth->tx_free < GRETH_TXBD_NUM) {
679 skb = greth->tx_skbuff[greth->tx_last];
681 nr_frags = skb_shinfo(skb)->nr_frags;
683 /* We only clean fully completed SKBs */
684 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
686 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
687 mb();
688 stat = greth_read_bd(&bdp_last_frag->stat);
690 if (stat & GRETH_BD_EN)
691 break;
693 greth->tx_skbuff[greth->tx_last] = NULL;
695 greth_update_tx_stats(dev, stat);
697 bdp = greth->tx_bd_base + greth->tx_last;
699 greth->tx_last = NEXT_TX(greth->tx_last);
701 dma_unmap_single(greth->dev,
702 greth_read_bd(&bdp->addr),
703 skb_headlen(skb),
704 DMA_TO_DEVICE);
706 for (i = 0; i < nr_frags; i++) {
707 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
708 bdp = greth->tx_bd_base + greth->tx_last;
710 dma_unmap_page(greth->dev,
711 greth_read_bd(&bdp->addr),
712 frag->size,
713 DMA_TO_DEVICE);
715 greth->tx_last = NEXT_TX(greth->tx_last);
717 greth->tx_free += nr_frags+1;
718 dev_kfree_skb(skb);
721 if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1)))
722 netif_wake_queue(dev);
725 static int greth_rx(struct net_device *dev, int limit)
727 struct greth_private *greth;
728 struct greth_bd *bdp;
729 struct sk_buff *skb;
730 int pkt_len;
731 int bad, count;
732 u32 status, dma_addr;
733 unsigned long flags;
735 greth = netdev_priv(dev);
737 for (count = 0; count < limit; ++count) {
739 bdp = greth->rx_bd_base + greth->rx_cur;
740 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
741 mb();
742 status = greth_read_bd(&bdp->stat);
744 if (unlikely(status & GRETH_BD_EN)) {
745 break;
748 dma_addr = greth_read_bd(&bdp->addr);
749 bad = 0;
751 /* Check status for errors. */
752 if (unlikely(status & GRETH_RXBD_STATUS)) {
753 if (status & GRETH_RXBD_ERR_FT) {
754 dev->stats.rx_length_errors++;
755 bad = 1;
757 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
758 dev->stats.rx_frame_errors++;
759 bad = 1;
761 if (status & GRETH_RXBD_ERR_CRC) {
762 dev->stats.rx_crc_errors++;
763 bad = 1;
766 if (unlikely(bad)) {
767 dev->stats.rx_errors++;
769 } else {
771 pkt_len = status & GRETH_BD_LEN;
773 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
775 if (unlikely(skb == NULL)) {
777 if (net_ratelimit())
778 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
780 dev->stats.rx_dropped++;
782 } else {
783 skb_reserve(skb, NET_IP_ALIGN);
784 skb->dev = dev;
786 dma_sync_single_for_cpu(greth->dev,
787 dma_addr,
788 pkt_len,
789 DMA_FROM_DEVICE);
791 if (netif_msg_pktdata(greth))
792 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
794 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
796 skb->protocol = eth_type_trans(skb, dev);
797 dev->stats.rx_packets++;
798 netif_receive_skb(skb);
802 status = GRETH_BD_EN | GRETH_BD_IE;
803 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
804 status |= GRETH_BD_WR;
807 wmb();
808 greth_write_bd(&bdp->stat, status);
810 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
812 spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
813 greth_enable_rx(greth);
814 spin_unlock_irqrestore(&greth->devlock, flags);
816 greth->rx_cur = NEXT_RX(greth->rx_cur);
819 return count;
822 static inline int hw_checksummed(u32 status)
825 if (status & GRETH_RXBD_IP_FRAG)
826 return 0;
828 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
829 return 0;
831 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
832 return 0;
834 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
835 return 0;
837 return 1;
840 static int greth_rx_gbit(struct net_device *dev, int limit)
842 struct greth_private *greth;
843 struct greth_bd *bdp;
844 struct sk_buff *skb, *newskb;
845 int pkt_len;
846 int bad, count = 0;
847 u32 status, dma_addr;
848 unsigned long flags;
850 greth = netdev_priv(dev);
852 for (count = 0; count < limit; ++count) {
854 bdp = greth->rx_bd_base + greth->rx_cur;
855 skb = greth->rx_skbuff[greth->rx_cur];
856 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
857 mb();
858 status = greth_read_bd(&bdp->stat);
859 bad = 0;
861 if (status & GRETH_BD_EN)
862 break;
864 /* Check status for errors. */
865 if (unlikely(status & GRETH_RXBD_STATUS)) {
867 if (status & GRETH_RXBD_ERR_FT) {
868 dev->stats.rx_length_errors++;
869 bad = 1;
870 } else if (status &
871 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
872 dev->stats.rx_frame_errors++;
873 bad = 1;
874 } else if (status & GRETH_RXBD_ERR_CRC) {
875 dev->stats.rx_crc_errors++;
876 bad = 1;
880 /* Allocate new skb to replace current, not needed if the
881 * current skb can be reused */
882 if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
883 skb_reserve(newskb, NET_IP_ALIGN);
885 dma_addr = dma_map_single(greth->dev,
886 newskb->data,
887 MAX_FRAME_SIZE + NET_IP_ALIGN,
888 DMA_FROM_DEVICE);
890 if (!dma_mapping_error(greth->dev, dma_addr)) {
891 /* Process the incoming frame. */
892 pkt_len = status & GRETH_BD_LEN;
894 dma_unmap_single(greth->dev,
895 greth_read_bd(&bdp->addr),
896 MAX_FRAME_SIZE + NET_IP_ALIGN,
897 DMA_FROM_DEVICE);
899 if (netif_msg_pktdata(greth))
900 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
902 skb_put(skb, pkt_len);
904 if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
905 skb->ip_summed = CHECKSUM_UNNECESSARY;
906 else
907 skb_checksum_none_assert(skb);
909 skb->protocol = eth_type_trans(skb, dev);
910 dev->stats.rx_packets++;
911 netif_receive_skb(skb);
913 greth->rx_skbuff[greth->rx_cur] = newskb;
914 greth_write_bd(&bdp->addr, dma_addr);
915 } else {
916 if (net_ratelimit())
917 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
918 dev_kfree_skb(newskb);
919 /* reusing current skb, so it is a drop */
920 dev->stats.rx_dropped++;
922 } else if (bad) {
923 /* Bad Frame transfer, the skb is reused */
924 dev->stats.rx_dropped++;
925 } else {
926 /* Failed Allocating a new skb. This is rather stupid
927 * but the current "filled" skb is reused, as if
928 * transfer failure. One could argue that RX descriptor
929 * table handling should be divided into cleaning and
930 * filling as the TX part of the driver
932 if (net_ratelimit())
933 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
934 /* reusing current skb, so it is a drop */
935 dev->stats.rx_dropped++;
938 status = GRETH_BD_EN | GRETH_BD_IE;
939 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
940 status |= GRETH_BD_WR;
943 wmb();
944 greth_write_bd(&bdp->stat, status);
945 spin_lock_irqsave(&greth->devlock, flags);
946 greth_enable_rx(greth);
947 spin_unlock_irqrestore(&greth->devlock, flags);
948 greth->rx_cur = NEXT_RX(greth->rx_cur);
951 return count;
955 static int greth_poll(struct napi_struct *napi, int budget)
957 struct greth_private *greth;
958 int work_done = 0;
959 unsigned long flags;
960 u32 mask, ctrl;
961 greth = container_of(napi, struct greth_private, napi);
963 restart_txrx_poll:
964 if (netif_queue_stopped(greth->netdev)) {
965 if (greth->gbit_mac)
966 greth_clean_tx_gbit(greth->netdev);
967 else
968 greth_clean_tx(greth->netdev);
971 if (greth->gbit_mac) {
972 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
973 } else {
974 work_done += greth_rx(greth->netdev, budget - work_done);
977 if (work_done < budget) {
979 spin_lock_irqsave(&greth->devlock, flags);
981 ctrl = GRETH_REGLOAD(greth->regs->control);
982 if (netif_queue_stopped(greth->netdev)) {
983 GRETH_REGSAVE(greth->regs->control,
984 ctrl | GRETH_TXI | GRETH_RXI);
985 mask = GRETH_INT_RX | GRETH_INT_RE |
986 GRETH_INT_TX | GRETH_INT_TE;
987 } else {
988 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
989 mask = GRETH_INT_RX | GRETH_INT_RE;
992 if (GRETH_REGLOAD(greth->regs->status) & mask) {
993 GRETH_REGSAVE(greth->regs->control, ctrl);
994 spin_unlock_irqrestore(&greth->devlock, flags);
995 goto restart_txrx_poll;
996 } else {
997 __napi_complete(napi);
998 spin_unlock_irqrestore(&greth->devlock, flags);
1002 return work_done;
1005 static int greth_set_mac_add(struct net_device *dev, void *p)
1007 struct sockaddr *addr = p;
1008 struct greth_private *greth;
1009 struct greth_regs *regs;
1011 greth = netdev_priv(dev);
1012 regs = (struct greth_regs *) greth->regs;
1014 if (!is_valid_ether_addr(addr->sa_data))
1015 return -EINVAL;
1017 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1018 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1019 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1020 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1022 return 0;
1025 static u32 greth_hash_get_index(__u8 *addr)
1027 return (ether_crc(6, addr)) & 0x3F;
1030 static void greth_set_hash_filter(struct net_device *dev)
1032 struct netdev_hw_addr *ha;
1033 struct greth_private *greth = netdev_priv(dev);
1034 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1035 u32 mc_filter[2];
1036 unsigned int bitnr;
1038 mc_filter[0] = mc_filter[1] = 0;
1040 netdev_for_each_mc_addr(ha, dev) {
1041 bitnr = greth_hash_get_index(ha->addr);
1042 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1045 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1046 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1049 static void greth_set_multicast_list(struct net_device *dev)
1051 int cfg;
1052 struct greth_private *greth = netdev_priv(dev);
1053 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1055 cfg = GRETH_REGLOAD(regs->control);
1056 if (dev->flags & IFF_PROMISC)
1057 cfg |= GRETH_CTRL_PR;
1058 else
1059 cfg &= ~GRETH_CTRL_PR;
1061 if (greth->multicast) {
1062 if (dev->flags & IFF_ALLMULTI) {
1063 GRETH_REGSAVE(regs->hash_msb, -1);
1064 GRETH_REGSAVE(regs->hash_lsb, -1);
1065 cfg |= GRETH_CTRL_MCEN;
1066 GRETH_REGSAVE(regs->control, cfg);
1067 return;
1070 if (netdev_mc_empty(dev)) {
1071 cfg &= ~GRETH_CTRL_MCEN;
1072 GRETH_REGSAVE(regs->control, cfg);
1073 return;
1076 /* Setup multicast filter */
1077 greth_set_hash_filter(dev);
1078 cfg |= GRETH_CTRL_MCEN;
1080 GRETH_REGSAVE(regs->control, cfg);
1083 static u32 greth_get_msglevel(struct net_device *dev)
1085 struct greth_private *greth = netdev_priv(dev);
1086 return greth->msg_enable;
1089 static void greth_set_msglevel(struct net_device *dev, u32 value)
1091 struct greth_private *greth = netdev_priv(dev);
1092 greth->msg_enable = value;
1094 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1096 struct greth_private *greth = netdev_priv(dev);
1097 struct phy_device *phy = greth->phy;
1099 if (!phy)
1100 return -ENODEV;
1102 return phy_ethtool_gset(phy, cmd);
1105 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1107 struct greth_private *greth = netdev_priv(dev);
1108 struct phy_device *phy = greth->phy;
1110 if (!phy)
1111 return -ENODEV;
1113 return phy_ethtool_sset(phy, cmd);
1116 static int greth_get_regs_len(struct net_device *dev)
1118 return sizeof(struct greth_regs);
1121 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1123 struct greth_private *greth = netdev_priv(dev);
1125 strncpy(info->driver, dev_driver_string(greth->dev), 32);
1126 strncpy(info->version, "revision: 1.0", 32);
1127 strncpy(info->bus_info, greth->dev->bus->name, 32);
1128 strncpy(info->fw_version, "N/A", 32);
1129 info->eedump_len = 0;
1130 info->regdump_len = sizeof(struct greth_regs);
1133 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1135 int i;
1136 struct greth_private *greth = netdev_priv(dev);
1137 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1138 u32 *buff = p;
1140 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1141 buff[i] = greth_read_bd(&greth_regs[i]);
1144 static const struct ethtool_ops greth_ethtool_ops = {
1145 .get_msglevel = greth_get_msglevel,
1146 .set_msglevel = greth_set_msglevel,
1147 .get_settings = greth_get_settings,
1148 .set_settings = greth_set_settings,
1149 .get_drvinfo = greth_get_drvinfo,
1150 .get_regs_len = greth_get_regs_len,
1151 .get_regs = greth_get_regs,
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;
1221 int status_change = 0;
1222 u32 ctrl;
1224 spin_lock_irqsave(&greth->devlock, flags);
1226 if (phydev->link) {
1228 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1229 ctrl = GRETH_REGLOAD(greth->regs->control) &
1230 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
1232 if (phydev->duplex)
1233 ctrl |= GRETH_CTRL_FD;
1235 if (phydev->speed == SPEED_100)
1236 ctrl |= GRETH_CTRL_SP;
1237 else if (phydev->speed == SPEED_1000)
1238 ctrl |= GRETH_CTRL_GB;
1240 GRETH_REGSAVE(greth->regs->control, ctrl);
1241 greth->speed = phydev->speed;
1242 greth->duplex = phydev->duplex;
1243 status_change = 1;
1247 if (phydev->link != greth->link) {
1248 if (!phydev->link) {
1249 greth->speed = 0;
1250 greth->duplex = -1;
1252 greth->link = phydev->link;
1254 status_change = 1;
1257 spin_unlock_irqrestore(&greth->devlock, flags);
1259 if (status_change) {
1260 if (phydev->link)
1261 pr_debug("%s: link up (%d/%s)\n",
1262 dev->name, phydev->speed,
1263 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1264 else
1265 pr_debug("%s: link down\n", dev->name);
1269 static int greth_mdio_probe(struct net_device *dev)
1271 struct greth_private *greth = netdev_priv(dev);
1272 struct phy_device *phy = NULL;
1273 int ret;
1275 /* Find the first PHY */
1276 phy = phy_find_first(greth->mdio);
1278 if (!phy) {
1279 if (netif_msg_probe(greth))
1280 dev_err(&dev->dev, "no PHY found\n");
1281 return -ENXIO;
1284 ret = phy_connect_direct(dev, phy, &greth_link_change,
1285 0, greth->gbit_mac ?
1286 PHY_INTERFACE_MODE_GMII :
1287 PHY_INTERFACE_MODE_MII);
1288 if (ret) {
1289 if (netif_msg_ifup(greth))
1290 dev_err(&dev->dev, "could not attach to PHY\n");
1291 return ret;
1294 if (greth->gbit_mac)
1295 phy->supported &= PHY_GBIT_FEATURES;
1296 else
1297 phy->supported &= PHY_BASIC_FEATURES;
1299 phy->advertising = phy->supported;
1301 greth->link = 0;
1302 greth->speed = 0;
1303 greth->duplex = -1;
1304 greth->phy = phy;
1306 return 0;
1309 static inline int phy_aneg_done(struct phy_device *phydev)
1311 int retval;
1313 retval = phy_read(phydev, MII_BMSR);
1315 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1318 static int greth_mdio_init(struct greth_private *greth)
1320 int ret, phy;
1321 unsigned long timeout;
1323 greth->mdio = mdiobus_alloc();
1324 if (!greth->mdio) {
1325 return -ENOMEM;
1328 greth->mdio->name = "greth-mdio";
1329 snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1330 greth->mdio->read = greth_mdio_read;
1331 greth->mdio->write = greth_mdio_write;
1332 greth->mdio->reset = greth_mdio_reset;
1333 greth->mdio->priv = greth;
1335 greth->mdio->irq = greth->mdio_irqs;
1337 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1338 greth->mdio->irq[phy] = PHY_POLL;
1340 ret = mdiobus_register(greth->mdio);
1341 if (ret) {
1342 goto error;
1345 ret = greth_mdio_probe(greth->netdev);
1346 if (ret) {
1347 if (netif_msg_probe(greth))
1348 dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1349 goto unreg_mdio;
1352 phy_start(greth->phy);
1354 /* If Ethernet debug link is used make autoneg happen right away */
1355 if (greth->edcl && greth_edcl == 1) {
1356 phy_start_aneg(greth->phy);
1357 timeout = jiffies + 6*HZ;
1358 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1360 genphy_read_status(greth->phy);
1361 greth_link_change(greth->netdev);
1364 return 0;
1366 unreg_mdio:
1367 mdiobus_unregister(greth->mdio);
1368 error:
1369 mdiobus_free(greth->mdio);
1370 return ret;
1373 /* Initialize the GRETH MAC */
1374 static int __devinit greth_of_probe(struct platform_device *ofdev)
1376 struct net_device *dev;
1377 struct greth_private *greth;
1378 struct greth_regs *regs;
1380 int i;
1381 int err;
1382 int tmp;
1383 unsigned long timeout;
1385 dev = alloc_etherdev(sizeof(struct greth_private));
1387 if (dev == NULL)
1388 return -ENOMEM;
1390 greth = netdev_priv(dev);
1391 greth->netdev = dev;
1392 greth->dev = &ofdev->dev;
1394 if (greth_debug > 0)
1395 greth->msg_enable = greth_debug;
1396 else
1397 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1399 spin_lock_init(&greth->devlock);
1401 greth->regs = of_ioremap(&ofdev->resource[0], 0,
1402 resource_size(&ofdev->resource[0]),
1403 "grlib-greth regs");
1405 if (greth->regs == NULL) {
1406 if (netif_msg_probe(greth))
1407 dev_err(greth->dev, "ioremap failure.\n");
1408 err = -EIO;
1409 goto error1;
1412 regs = (struct greth_regs *) greth->regs;
1413 greth->irq = ofdev->archdata.irqs[0];
1415 dev_set_drvdata(greth->dev, dev);
1416 SET_NETDEV_DEV(dev, greth->dev);
1418 if (netif_msg_probe(greth))
1419 dev_dbg(greth->dev, "reseting controller.\n");
1421 /* Reset the controller. */
1422 GRETH_REGSAVE(regs->control, GRETH_RESET);
1424 /* Wait for MAC to reset itself */
1425 timeout = jiffies + HZ/100;
1426 while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1427 if (time_after(jiffies, timeout)) {
1428 err = -EIO;
1429 if (netif_msg_probe(greth))
1430 dev_err(greth->dev, "timeout when waiting for reset.\n");
1431 goto error2;
1435 /* Get default PHY address */
1436 greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1438 /* Check if we have GBIT capable MAC */
1439 tmp = GRETH_REGLOAD(regs->control);
1440 greth->gbit_mac = (tmp >> 27) & 1;
1442 /* Check for multicast capability */
1443 greth->multicast = (tmp >> 25) & 1;
1445 greth->edcl = (tmp >> 31) & 1;
1447 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1448 * it doesn't interfere with the software */
1449 if (greth->edcl != 0)
1450 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1452 /* Check if MAC can handle MDIO interrupts */
1453 greth->mdio_int_en = (tmp >> 26) & 1;
1455 err = greth_mdio_init(greth);
1456 if (err) {
1457 if (netif_msg_probe(greth))
1458 dev_err(greth->dev, "failed to register MDIO bus\n");
1459 goto error2;
1462 /* Allocate TX descriptor ring in coherent memory */
1463 greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1464 1024,
1465 &greth->tx_bd_base_phys,
1466 GFP_KERNEL);
1468 if (!greth->tx_bd_base) {
1469 if (netif_msg_probe(greth))
1470 dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1471 err = -ENOMEM;
1472 goto error3;
1475 memset(greth->tx_bd_base, 0, 1024);
1477 /* Allocate RX descriptor ring in coherent memory */
1478 greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1479 1024,
1480 &greth->rx_bd_base_phys,
1481 GFP_KERNEL);
1483 if (!greth->rx_bd_base) {
1484 if (netif_msg_probe(greth))
1485 dev_err(greth->dev, "could not allocate descriptor memory.\n");
1486 err = -ENOMEM;
1487 goto error4;
1490 memset(greth->rx_bd_base, 0, 1024);
1492 /* Get MAC address from: module param, OF property or ID prom */
1493 for (i = 0; i < 6; i++) {
1494 if (macaddr[i] != 0)
1495 break;
1497 if (i == 6) {
1498 const unsigned char *addr;
1499 int len;
1500 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1501 &len);
1502 if (addr != NULL && len == 6) {
1503 for (i = 0; i < 6; i++)
1504 macaddr[i] = (unsigned int) addr[i];
1505 } else {
1506 #ifdef CONFIG_SPARC
1507 for (i = 0; i < 6; i++)
1508 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1509 #endif
1513 for (i = 0; i < 6; i++)
1514 dev->dev_addr[i] = macaddr[i];
1516 macaddr[5]++;
1518 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1519 if (netif_msg_probe(greth))
1520 dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1521 err = -EINVAL;
1522 goto error5;
1525 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1526 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1527 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1529 /* Clear all pending interrupts except PHY irq */
1530 GRETH_REGSAVE(regs->status, 0xFF);
1532 if (greth->gbit_mac) {
1533 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1534 NETIF_F_RXCSUM;
1535 dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1536 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1539 if (greth->multicast) {
1540 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1541 dev->flags |= IFF_MULTICAST;
1542 } else {
1543 dev->flags &= ~IFF_MULTICAST;
1546 dev->netdev_ops = &greth_netdev_ops;
1547 dev->ethtool_ops = &greth_ethtool_ops;
1549 err = register_netdev(dev);
1550 if (err) {
1551 if (netif_msg_probe(greth))
1552 dev_err(greth->dev, "netdevice registration failed.\n");
1553 goto error5;
1556 /* setup NAPI */
1557 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1559 return 0;
1561 error5:
1562 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1563 error4:
1564 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1565 error3:
1566 mdiobus_unregister(greth->mdio);
1567 error2:
1568 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1569 error1:
1570 free_netdev(dev);
1571 return err;
1574 static int __devexit greth_of_remove(struct platform_device *of_dev)
1576 struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1577 struct greth_private *greth = netdev_priv(ndev);
1579 /* Free descriptor areas */
1580 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1582 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1584 dev_set_drvdata(&of_dev->dev, NULL);
1586 if (greth->phy)
1587 phy_stop(greth->phy);
1588 mdiobus_unregister(greth->mdio);
1590 unregister_netdev(ndev);
1591 free_netdev(ndev);
1593 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1595 return 0;
1598 static struct of_device_id greth_of_match[] = {
1600 .name = "GAISLER_ETHMAC",
1603 .name = "01_01d",
1608 MODULE_DEVICE_TABLE(of, greth_of_match);
1610 static struct platform_driver greth_of_driver = {
1611 .driver = {
1612 .name = "grlib-greth",
1613 .owner = THIS_MODULE,
1614 .of_match_table = greth_of_match,
1616 .probe = greth_of_probe,
1617 .remove = __devexit_p(greth_of_remove),
1620 static int __init greth_init(void)
1622 return platform_driver_register(&greth_of_driver);
1625 static void __exit greth_cleanup(void)
1627 platform_driver_unregister(&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");