PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / net / ethernet / aeroflex / greth.c
blobc5d75e7aeeb6e7140af25eb7b3b16797dae4f62e
1 /*
2 * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
4 * 2005-2010 (c) Aeroflex Gaisler AB
6 * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
7 * available in the GRLIB VHDL IP core library.
9 * Full documentation of both cores can be found here:
10 * http://www.gaisler.com/products/grlib/grip.pdf
12 * The Gigabit version supports scatter/gather DMA, any alignment of
13 * buffers and checksum offloading.
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * Contributors: Kristoffer Glembo
21 * Daniel Hellstrom
22 * Marko Isomaki
25 #include <linux/dma-mapping.h>
26 #include <linux/module.h>
27 #include <linux/uaccess.h>
28 #include <linux/interrupt.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/ethtool.h>
32 #include <linux/skbuff.h>
33 #include <linux/io.h>
34 #include <linux/crc32.h>
35 #include <linux/mii.h>
36 #include <linux/of_device.h>
37 #include <linux/of_platform.h>
38 #include <linux/slab.h>
39 #include <asm/cacheflush.h>
40 #include <asm/byteorder.h>
42 #ifdef CONFIG_SPARC
43 #include <asm/idprom.h>
44 #endif
46 #include "greth.h"
48 #define GRETH_DEF_MSG_ENABLE \
49 (NETIF_MSG_DRV | \
50 NETIF_MSG_PROBE | \
51 NETIF_MSG_LINK | \
52 NETIF_MSG_IFDOWN | \
53 NETIF_MSG_IFUP | \
54 NETIF_MSG_RX_ERR | \
55 NETIF_MSG_TX_ERR)
57 static int greth_debug = -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */
58 module_param(greth_debug, int, 0);
59 MODULE_PARM_DESC(greth_debug, "GRETH bitmapped debugging message enable value");
61 /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
62 static int macaddr[6];
63 module_param_array(macaddr, int, NULL, 0);
64 MODULE_PARM_DESC(macaddr, "GRETH Ethernet MAC address");
66 static int greth_edcl = 1;
67 module_param(greth_edcl, int, 0);
68 MODULE_PARM_DESC(greth_edcl, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
70 static int greth_open(struct net_device *dev);
71 static netdev_tx_t greth_start_xmit(struct sk_buff *skb,
72 struct net_device *dev);
73 static netdev_tx_t greth_start_xmit_gbit(struct sk_buff *skb,
74 struct net_device *dev);
75 static int greth_rx(struct net_device *dev, int limit);
76 static int greth_rx_gbit(struct net_device *dev, int limit);
77 static void greth_clean_tx(struct net_device *dev);
78 static void greth_clean_tx_gbit(struct net_device *dev);
79 static irqreturn_t greth_interrupt(int irq, void *dev_id);
80 static int greth_close(struct net_device *dev);
81 static int greth_set_mac_add(struct net_device *dev, void *p);
82 static void greth_set_multicast_list(struct net_device *dev);
84 #define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
85 #define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
86 #define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
87 #define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
89 #define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
90 #define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
91 #define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
93 static void greth_print_rx_packet(void *addr, int len)
95 print_hex_dump(KERN_DEBUG, "RX: ", DUMP_PREFIX_OFFSET, 16, 1,
96 addr, len, true);
99 static void greth_print_tx_packet(struct sk_buff *skb)
101 int i;
102 int length;
104 if (skb_shinfo(skb)->nr_frags == 0)
105 length = skb->len;
106 else
107 length = skb_headlen(skb);
109 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
110 skb->data, length, true);
112 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
114 print_hex_dump(KERN_DEBUG, "TX: ", DUMP_PREFIX_OFFSET, 16, 1,
115 skb_frag_address(&skb_shinfo(skb)->frags[i]),
116 skb_shinfo(skb)->frags[i].size, 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 skb_frag_size(frag),
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);
429 greth->tx_bufs_length[greth->tx_next] = skb->len & GRETH_BD_LEN;
431 /* Wrap around descriptor ring */
432 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
433 status |= GRETH_BD_WR;
436 greth->tx_next = NEXT_TX(greth->tx_next);
437 greth->tx_free--;
439 /* Write descriptor control word and enable transmission */
440 greth_write_bd(&bdp->stat, status);
441 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
442 greth_enable_tx(greth);
443 spin_unlock_irqrestore(&greth->devlock, flags);
445 out:
446 dev_kfree_skb(skb);
447 return err;
451 static netdev_tx_t
452 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
454 struct greth_private *greth = netdev_priv(dev);
455 struct greth_bd *bdp;
456 u32 status = 0, dma_addr, ctrl;
457 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
458 unsigned long flags;
460 nr_frags = skb_shinfo(skb)->nr_frags;
462 /* Clean TX Ring */
463 greth_clean_tx_gbit(dev);
465 if (greth->tx_free < nr_frags + 1) {
466 spin_lock_irqsave(&greth->devlock, flags);/*save from poll/irq*/
467 ctrl = GRETH_REGLOAD(greth->regs->control);
468 /* Enable TX IRQ only if not already in poll() routine */
469 if (ctrl & GRETH_RXI)
470 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_TXI);
471 netif_stop_queue(dev);
472 spin_unlock_irqrestore(&greth->devlock, flags);
473 err = NETDEV_TX_BUSY;
474 goto out;
477 if (netif_msg_pktdata(greth))
478 greth_print_tx_packet(skb);
480 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
481 dev->stats.tx_errors++;
482 goto out;
485 /* Save skb pointer. */
486 greth->tx_skbuff[greth->tx_next] = skb;
488 /* Linear buf */
489 if (nr_frags != 0)
490 status = GRETH_TXBD_MORE;
492 if (skb->ip_summed == CHECKSUM_PARTIAL)
493 status |= GRETH_TXBD_CSALL;
494 status |= skb_headlen(skb) & GRETH_BD_LEN;
495 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
496 status |= GRETH_BD_WR;
499 bdp = greth->tx_bd_base + greth->tx_next;
500 greth_write_bd(&bdp->stat, status);
501 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
503 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
504 goto map_error;
506 greth_write_bd(&bdp->addr, dma_addr);
508 curr_tx = NEXT_TX(greth->tx_next);
510 /* Frags */
511 for (i = 0; i < nr_frags; i++) {
512 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
513 greth->tx_skbuff[curr_tx] = NULL;
514 bdp = greth->tx_bd_base + curr_tx;
516 status = GRETH_BD_EN;
517 if (skb->ip_summed == CHECKSUM_PARTIAL)
518 status |= GRETH_TXBD_CSALL;
519 status |= skb_frag_size(frag) & GRETH_BD_LEN;
521 /* Wrap around descriptor ring */
522 if (curr_tx == GRETH_TXBD_NUM_MASK)
523 status |= GRETH_BD_WR;
525 /* More fragments left */
526 if (i < nr_frags - 1)
527 status |= GRETH_TXBD_MORE;
528 else
529 status |= GRETH_BD_IE; /* enable IRQ on last fragment */
531 greth_write_bd(&bdp->stat, status);
533 dma_addr = skb_frag_dma_map(greth->dev, frag, 0, skb_frag_size(frag),
534 DMA_TO_DEVICE);
536 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
537 goto frag_map_error;
539 greth_write_bd(&bdp->addr, dma_addr);
541 curr_tx = NEXT_TX(curr_tx);
544 wmb();
546 /* Enable the descriptor chain by enabling the first descriptor */
547 bdp = greth->tx_bd_base + greth->tx_next;
548 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
549 greth->tx_next = curr_tx;
550 greth->tx_free -= nr_frags + 1;
552 wmb();
554 spin_lock_irqsave(&greth->devlock, flags); /*save from poll/irq*/
555 greth_enable_tx(greth);
556 spin_unlock_irqrestore(&greth->devlock, flags);
558 return NETDEV_TX_OK;
560 frag_map_error:
561 /* Unmap SKB mappings that succeeded and disable descriptor */
562 for (i = 0; greth->tx_next + i != curr_tx; i++) {
563 bdp = greth->tx_bd_base + greth->tx_next + i;
564 dma_unmap_single(greth->dev,
565 greth_read_bd(&bdp->addr),
566 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
567 DMA_TO_DEVICE);
568 greth_write_bd(&bdp->stat, 0);
570 map_error:
571 if (net_ratelimit())
572 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
573 dev_kfree_skb(skb);
574 out:
575 return err;
578 static irqreturn_t greth_interrupt(int irq, void *dev_id)
580 struct net_device *dev = dev_id;
581 struct greth_private *greth;
582 u32 status, ctrl;
583 irqreturn_t retval = IRQ_NONE;
585 greth = netdev_priv(dev);
587 spin_lock(&greth->devlock);
589 /* Get the interrupt events that caused us to be here. */
590 status = GRETH_REGLOAD(greth->regs->status);
592 /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
593 * set regardless of whether IRQ is enabled or not. Especially
594 * important when shared IRQ.
596 ctrl = GRETH_REGLOAD(greth->regs->control);
598 /* Handle rx and tx interrupts through poll */
599 if (((status & (GRETH_INT_RE | GRETH_INT_RX)) && (ctrl & GRETH_RXI)) ||
600 ((status & (GRETH_INT_TE | GRETH_INT_TX)) && (ctrl & GRETH_TXI))) {
601 retval = IRQ_HANDLED;
603 /* Disable interrupts and schedule poll() */
604 greth_disable_irqs(greth);
605 napi_schedule(&greth->napi);
608 mmiowb();
609 spin_unlock(&greth->devlock);
611 return retval;
614 static void greth_clean_tx(struct net_device *dev)
616 struct greth_private *greth;
617 struct greth_bd *bdp;
618 u32 stat;
620 greth = netdev_priv(dev);
622 while (1) {
623 bdp = greth->tx_bd_base + greth->tx_last;
624 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
625 mb();
626 stat = greth_read_bd(&bdp->stat);
628 if (unlikely(stat & GRETH_BD_EN))
629 break;
631 if (greth->tx_free == GRETH_TXBD_NUM)
632 break;
634 /* Check status for errors */
635 if (unlikely(stat & GRETH_TXBD_STATUS)) {
636 dev->stats.tx_errors++;
637 if (stat & GRETH_TXBD_ERR_AL)
638 dev->stats.tx_aborted_errors++;
639 if (stat & GRETH_TXBD_ERR_UE)
640 dev->stats.tx_fifo_errors++;
642 dev->stats.tx_packets++;
643 dev->stats.tx_bytes += greth->tx_bufs_length[greth->tx_last];
644 greth->tx_last = NEXT_TX(greth->tx_last);
645 greth->tx_free++;
648 if (greth->tx_free > 0) {
649 netif_wake_queue(dev);
654 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
656 /* Check status for errors */
657 if (unlikely(stat & GRETH_TXBD_STATUS)) {
658 dev->stats.tx_errors++;
659 if (stat & GRETH_TXBD_ERR_AL)
660 dev->stats.tx_aborted_errors++;
661 if (stat & GRETH_TXBD_ERR_UE)
662 dev->stats.tx_fifo_errors++;
663 if (stat & GRETH_TXBD_ERR_LC)
664 dev->stats.tx_aborted_errors++;
666 dev->stats.tx_packets++;
669 static void greth_clean_tx_gbit(struct net_device *dev)
671 struct greth_private *greth;
672 struct greth_bd *bdp, *bdp_last_frag;
673 struct sk_buff *skb;
674 u32 stat;
675 int nr_frags, i;
677 greth = netdev_priv(dev);
679 while (greth->tx_free < GRETH_TXBD_NUM) {
681 skb = greth->tx_skbuff[greth->tx_last];
683 nr_frags = skb_shinfo(skb)->nr_frags;
685 /* We only clean fully completed SKBs */
686 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
688 GRETH_REGSAVE(greth->regs->status, GRETH_INT_TE | GRETH_INT_TX);
689 mb();
690 stat = greth_read_bd(&bdp_last_frag->stat);
692 if (stat & GRETH_BD_EN)
693 break;
695 greth->tx_skbuff[greth->tx_last] = NULL;
697 greth_update_tx_stats(dev, stat);
698 dev->stats.tx_bytes += skb->len;
700 bdp = greth->tx_bd_base + greth->tx_last;
702 greth->tx_last = NEXT_TX(greth->tx_last);
704 dma_unmap_single(greth->dev,
705 greth_read_bd(&bdp->addr),
706 skb_headlen(skb),
707 DMA_TO_DEVICE);
709 for (i = 0; i < nr_frags; i++) {
710 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
711 bdp = greth->tx_bd_base + greth->tx_last;
713 dma_unmap_page(greth->dev,
714 greth_read_bd(&bdp->addr),
715 skb_frag_size(frag),
716 DMA_TO_DEVICE);
718 greth->tx_last = NEXT_TX(greth->tx_last);
720 greth->tx_free += nr_frags+1;
721 dev_kfree_skb(skb);
724 if (netif_queue_stopped(dev) && (greth->tx_free > (MAX_SKB_FRAGS+1)))
725 netif_wake_queue(dev);
728 static int greth_rx(struct net_device *dev, int limit)
730 struct greth_private *greth;
731 struct greth_bd *bdp;
732 struct sk_buff *skb;
733 int pkt_len;
734 int bad, count;
735 u32 status, dma_addr;
736 unsigned long flags;
738 greth = netdev_priv(dev);
740 for (count = 0; count < limit; ++count) {
742 bdp = greth->rx_bd_base + greth->rx_cur;
743 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
744 mb();
745 status = greth_read_bd(&bdp->stat);
747 if (unlikely(status & GRETH_BD_EN)) {
748 break;
751 dma_addr = greth_read_bd(&bdp->addr);
752 bad = 0;
754 /* Check status for errors. */
755 if (unlikely(status & GRETH_RXBD_STATUS)) {
756 if (status & GRETH_RXBD_ERR_FT) {
757 dev->stats.rx_length_errors++;
758 bad = 1;
760 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
761 dev->stats.rx_frame_errors++;
762 bad = 1;
764 if (status & GRETH_RXBD_ERR_CRC) {
765 dev->stats.rx_crc_errors++;
766 bad = 1;
769 if (unlikely(bad)) {
770 dev->stats.rx_errors++;
772 } else {
774 pkt_len = status & GRETH_BD_LEN;
776 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
778 if (unlikely(skb == NULL)) {
780 if (net_ratelimit())
781 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
783 dev->stats.rx_dropped++;
785 } else {
786 skb_reserve(skb, NET_IP_ALIGN);
788 dma_sync_single_for_cpu(greth->dev,
789 dma_addr,
790 pkt_len,
791 DMA_FROM_DEVICE);
793 if (netif_msg_pktdata(greth))
794 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
796 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
798 skb->protocol = eth_type_trans(skb, dev);
799 dev->stats.rx_bytes += pkt_len;
800 dev->stats.rx_packets++;
801 netif_receive_skb(skb);
805 status = GRETH_BD_EN | GRETH_BD_IE;
806 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
807 status |= GRETH_BD_WR;
810 wmb();
811 greth_write_bd(&bdp->stat, status);
813 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
815 spin_lock_irqsave(&greth->devlock, flags); /* save from XMIT */
816 greth_enable_rx(greth);
817 spin_unlock_irqrestore(&greth->devlock, flags);
819 greth->rx_cur = NEXT_RX(greth->rx_cur);
822 return count;
825 static inline int hw_checksummed(u32 status)
828 if (status & GRETH_RXBD_IP_FRAG)
829 return 0;
831 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
832 return 0;
834 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
835 return 0;
837 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
838 return 0;
840 return 1;
843 static int greth_rx_gbit(struct net_device *dev, int limit)
845 struct greth_private *greth;
846 struct greth_bd *bdp;
847 struct sk_buff *skb, *newskb;
848 int pkt_len;
849 int bad, count = 0;
850 u32 status, dma_addr;
851 unsigned long flags;
853 greth = netdev_priv(dev);
855 for (count = 0; count < limit; ++count) {
857 bdp = greth->rx_bd_base + greth->rx_cur;
858 skb = greth->rx_skbuff[greth->rx_cur];
859 GRETH_REGSAVE(greth->regs->status, GRETH_INT_RE | GRETH_INT_RX);
860 mb();
861 status = greth_read_bd(&bdp->stat);
862 bad = 0;
864 if (status & GRETH_BD_EN)
865 break;
867 /* Check status for errors. */
868 if (unlikely(status & GRETH_RXBD_STATUS)) {
870 if (status & GRETH_RXBD_ERR_FT) {
871 dev->stats.rx_length_errors++;
872 bad = 1;
873 } else if (status &
874 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
875 dev->stats.rx_frame_errors++;
876 bad = 1;
877 } else if (status & GRETH_RXBD_ERR_CRC) {
878 dev->stats.rx_crc_errors++;
879 bad = 1;
883 /* Allocate new skb to replace current, not needed if the
884 * current skb can be reused */
885 if (!bad && (newskb=netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN))) {
886 skb_reserve(newskb, NET_IP_ALIGN);
888 dma_addr = dma_map_single(greth->dev,
889 newskb->data,
890 MAX_FRAME_SIZE + NET_IP_ALIGN,
891 DMA_FROM_DEVICE);
893 if (!dma_mapping_error(greth->dev, dma_addr)) {
894 /* Process the incoming frame. */
895 pkt_len = status & GRETH_BD_LEN;
897 dma_unmap_single(greth->dev,
898 greth_read_bd(&bdp->addr),
899 MAX_FRAME_SIZE + NET_IP_ALIGN,
900 DMA_FROM_DEVICE);
902 if (netif_msg_pktdata(greth))
903 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
905 skb_put(skb, pkt_len);
907 if (dev->features & NETIF_F_RXCSUM && hw_checksummed(status))
908 skb->ip_summed = CHECKSUM_UNNECESSARY;
909 else
910 skb_checksum_none_assert(skb);
912 skb->protocol = eth_type_trans(skb, dev);
913 dev->stats.rx_packets++;
914 dev->stats.rx_bytes += pkt_len;
915 netif_receive_skb(skb);
917 greth->rx_skbuff[greth->rx_cur] = newskb;
918 greth_write_bd(&bdp->addr, dma_addr);
919 } else {
920 if (net_ratelimit())
921 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
922 dev_kfree_skb(newskb);
923 /* reusing current skb, so it is a drop */
924 dev->stats.rx_dropped++;
926 } else if (bad) {
927 /* Bad Frame transfer, the skb is reused */
928 dev->stats.rx_dropped++;
929 } else {
930 /* Failed Allocating a new skb. This is rather stupid
931 * but the current "filled" skb is reused, as if
932 * transfer failure. One could argue that RX descriptor
933 * table handling should be divided into cleaning and
934 * filling as the TX part of the driver
936 if (net_ratelimit())
937 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
938 /* reusing current skb, so it is a drop */
939 dev->stats.rx_dropped++;
942 status = GRETH_BD_EN | GRETH_BD_IE;
943 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
944 status |= GRETH_BD_WR;
947 wmb();
948 greth_write_bd(&bdp->stat, status);
949 spin_lock_irqsave(&greth->devlock, flags);
950 greth_enable_rx(greth);
951 spin_unlock_irqrestore(&greth->devlock, flags);
952 greth->rx_cur = NEXT_RX(greth->rx_cur);
955 return count;
959 static int greth_poll(struct napi_struct *napi, int budget)
961 struct greth_private *greth;
962 int work_done = 0;
963 unsigned long flags;
964 u32 mask, ctrl;
965 greth = container_of(napi, struct greth_private, napi);
967 restart_txrx_poll:
968 if (netif_queue_stopped(greth->netdev)) {
969 if (greth->gbit_mac)
970 greth_clean_tx_gbit(greth->netdev);
971 else
972 greth_clean_tx(greth->netdev);
975 if (greth->gbit_mac) {
976 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
977 } else {
978 work_done += greth_rx(greth->netdev, budget - work_done);
981 if (work_done < budget) {
983 spin_lock_irqsave(&greth->devlock, flags);
985 ctrl = GRETH_REGLOAD(greth->regs->control);
986 if (netif_queue_stopped(greth->netdev)) {
987 GRETH_REGSAVE(greth->regs->control,
988 ctrl | GRETH_TXI | GRETH_RXI);
989 mask = GRETH_INT_RX | GRETH_INT_RE |
990 GRETH_INT_TX | GRETH_INT_TE;
991 } else {
992 GRETH_REGSAVE(greth->regs->control, ctrl | GRETH_RXI);
993 mask = GRETH_INT_RX | GRETH_INT_RE;
996 if (GRETH_REGLOAD(greth->regs->status) & mask) {
997 GRETH_REGSAVE(greth->regs->control, ctrl);
998 spin_unlock_irqrestore(&greth->devlock, flags);
999 goto restart_txrx_poll;
1000 } else {
1001 __napi_complete(napi);
1002 spin_unlock_irqrestore(&greth->devlock, flags);
1006 return work_done;
1009 static int greth_set_mac_add(struct net_device *dev, void *p)
1011 struct sockaddr *addr = p;
1012 struct greth_private *greth;
1013 struct greth_regs *regs;
1015 greth = netdev_priv(dev);
1016 regs = greth->regs;
1018 if (!is_valid_ether_addr(addr->sa_data))
1019 return -EADDRNOTAVAIL;
1021 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1022 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1023 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1024 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1026 return 0;
1029 static u32 greth_hash_get_index(__u8 *addr)
1031 return (ether_crc(6, addr)) & 0x3F;
1034 static void greth_set_hash_filter(struct net_device *dev)
1036 struct netdev_hw_addr *ha;
1037 struct greth_private *greth = netdev_priv(dev);
1038 struct greth_regs *regs = greth->regs;
1039 u32 mc_filter[2];
1040 unsigned int bitnr;
1042 mc_filter[0] = mc_filter[1] = 0;
1044 netdev_for_each_mc_addr(ha, dev) {
1045 bitnr = greth_hash_get_index(ha->addr);
1046 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1049 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1050 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1053 static void greth_set_multicast_list(struct net_device *dev)
1055 int cfg;
1056 struct greth_private *greth = netdev_priv(dev);
1057 struct greth_regs *regs = greth->regs;
1059 cfg = GRETH_REGLOAD(regs->control);
1060 if (dev->flags & IFF_PROMISC)
1061 cfg |= GRETH_CTRL_PR;
1062 else
1063 cfg &= ~GRETH_CTRL_PR;
1065 if (greth->multicast) {
1066 if (dev->flags & IFF_ALLMULTI) {
1067 GRETH_REGSAVE(regs->hash_msb, -1);
1068 GRETH_REGSAVE(regs->hash_lsb, -1);
1069 cfg |= GRETH_CTRL_MCEN;
1070 GRETH_REGSAVE(regs->control, cfg);
1071 return;
1074 if (netdev_mc_empty(dev)) {
1075 cfg &= ~GRETH_CTRL_MCEN;
1076 GRETH_REGSAVE(regs->control, cfg);
1077 return;
1080 /* Setup multicast filter */
1081 greth_set_hash_filter(dev);
1082 cfg |= GRETH_CTRL_MCEN;
1084 GRETH_REGSAVE(regs->control, cfg);
1087 static u32 greth_get_msglevel(struct net_device *dev)
1089 struct greth_private *greth = netdev_priv(dev);
1090 return greth->msg_enable;
1093 static void greth_set_msglevel(struct net_device *dev, u32 value)
1095 struct greth_private *greth = netdev_priv(dev);
1096 greth->msg_enable = value;
1098 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1100 struct greth_private *greth = netdev_priv(dev);
1101 struct phy_device *phy = greth->phy;
1103 if (!phy)
1104 return -ENODEV;
1106 return phy_ethtool_gset(phy, cmd);
1109 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1111 struct greth_private *greth = netdev_priv(dev);
1112 struct phy_device *phy = greth->phy;
1114 if (!phy)
1115 return -ENODEV;
1117 return phy_ethtool_sset(phy, cmd);
1120 static int greth_get_regs_len(struct net_device *dev)
1122 return sizeof(struct greth_regs);
1125 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1127 struct greth_private *greth = netdev_priv(dev);
1129 strlcpy(info->driver, dev_driver_string(greth->dev),
1130 sizeof(info->driver));
1131 strlcpy(info->version, "revision: 1.0", sizeof(info->version));
1132 strlcpy(info->bus_info, greth->dev->bus->name, sizeof(info->bus_info));
1133 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
1134 info->eedump_len = 0;
1135 info->regdump_len = sizeof(struct greth_regs);
1138 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1140 int i;
1141 struct greth_private *greth = netdev_priv(dev);
1142 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1143 u32 *buff = p;
1145 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1146 buff[i] = greth_read_bd(&greth_regs[i]);
1149 static const struct ethtool_ops greth_ethtool_ops = {
1150 .get_msglevel = greth_get_msglevel,
1151 .set_msglevel = greth_set_msglevel,
1152 .get_settings = greth_get_settings,
1153 .set_settings = greth_set_settings,
1154 .get_drvinfo = greth_get_drvinfo,
1155 .get_regs_len = greth_get_regs_len,
1156 .get_regs = greth_get_regs,
1157 .get_link = ethtool_op_get_link,
1160 static struct net_device_ops greth_netdev_ops = {
1161 .ndo_open = greth_open,
1162 .ndo_stop = greth_close,
1163 .ndo_start_xmit = greth_start_xmit,
1164 .ndo_set_mac_address = greth_set_mac_add,
1165 .ndo_validate_addr = eth_validate_addr,
1168 static inline int wait_for_mdio(struct greth_private *greth)
1170 unsigned long timeout = jiffies + 4*HZ/100;
1171 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1172 if (time_after(jiffies, timeout))
1173 return 0;
1175 return 1;
1178 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1180 struct greth_private *greth = bus->priv;
1181 int data;
1183 if (!wait_for_mdio(greth))
1184 return -EBUSY;
1186 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1188 if (!wait_for_mdio(greth))
1189 return -EBUSY;
1191 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1192 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1193 return data;
1195 } else {
1196 return -1;
1200 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1202 struct greth_private *greth = bus->priv;
1204 if (!wait_for_mdio(greth))
1205 return -EBUSY;
1207 GRETH_REGSAVE(greth->regs->mdio,
1208 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1210 if (!wait_for_mdio(greth))
1211 return -EBUSY;
1213 return 0;
1216 static int greth_mdio_reset(struct mii_bus *bus)
1218 return 0;
1221 static void greth_link_change(struct net_device *dev)
1223 struct greth_private *greth = netdev_priv(dev);
1224 struct phy_device *phydev = greth->phy;
1225 unsigned long flags;
1226 int status_change = 0;
1227 u32 ctrl;
1229 spin_lock_irqsave(&greth->devlock, flags);
1231 if (phydev->link) {
1233 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1234 ctrl = GRETH_REGLOAD(greth->regs->control) &
1235 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB);
1237 if (phydev->duplex)
1238 ctrl |= GRETH_CTRL_FD;
1240 if (phydev->speed == SPEED_100)
1241 ctrl |= GRETH_CTRL_SP;
1242 else if (phydev->speed == SPEED_1000)
1243 ctrl |= GRETH_CTRL_GB;
1245 GRETH_REGSAVE(greth->regs->control, ctrl);
1246 greth->speed = phydev->speed;
1247 greth->duplex = phydev->duplex;
1248 status_change = 1;
1252 if (phydev->link != greth->link) {
1253 if (!phydev->link) {
1254 greth->speed = 0;
1255 greth->duplex = -1;
1257 greth->link = phydev->link;
1259 status_change = 1;
1262 spin_unlock_irqrestore(&greth->devlock, flags);
1264 if (status_change) {
1265 if (phydev->link)
1266 pr_debug("%s: link up (%d/%s)\n",
1267 dev->name, phydev->speed,
1268 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1269 else
1270 pr_debug("%s: link down\n", dev->name);
1274 static int greth_mdio_probe(struct net_device *dev)
1276 struct greth_private *greth = netdev_priv(dev);
1277 struct phy_device *phy = NULL;
1278 int ret;
1280 /* Find the first PHY */
1281 phy = phy_find_first(greth->mdio);
1283 if (!phy) {
1284 if (netif_msg_probe(greth))
1285 dev_err(&dev->dev, "no PHY found\n");
1286 return -ENXIO;
1289 ret = phy_connect_direct(dev, phy, &greth_link_change,
1290 greth->gbit_mac ? PHY_INTERFACE_MODE_GMII : 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 phy_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 greth_of_probe(struct platform_device *ofdev)
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 = greth->regs;
1416 greth->irq = ofdev->archdata.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, "resetting 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 = dma_zalloc_coherent(greth->dev, 1024,
1467 &greth->tx_bd_base_phys,
1468 GFP_KERNEL);
1469 if (!greth->tx_bd_base) {
1470 err = -ENOMEM;
1471 goto error3;
1474 /* Allocate RX descriptor ring in coherent memory */
1475 greth->rx_bd_base = dma_zalloc_coherent(greth->dev, 1024,
1476 &greth->rx_bd_base_phys,
1477 GFP_KERNEL);
1478 if (!greth->rx_bd_base) {
1479 err = -ENOMEM;
1480 goto error4;
1483 /* Get MAC address from: module param, OF property or ID prom */
1484 for (i = 0; i < 6; i++) {
1485 if (macaddr[i] != 0)
1486 break;
1488 if (i == 6) {
1489 const unsigned char *addr;
1490 int len;
1491 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1492 &len);
1493 if (addr != NULL && len == 6) {
1494 for (i = 0; i < 6; i++)
1495 macaddr[i] = (unsigned int) addr[i];
1496 } else {
1497 #ifdef CONFIG_SPARC
1498 for (i = 0; i < 6; i++)
1499 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1500 #endif
1504 for (i = 0; i < 6; i++)
1505 dev->dev_addr[i] = macaddr[i];
1507 macaddr[5]++;
1509 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1510 if (netif_msg_probe(greth))
1511 dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1512 err = -EINVAL;
1513 goto error5;
1516 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1517 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1518 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1520 /* Clear all pending interrupts except PHY irq */
1521 GRETH_REGSAVE(regs->status, 0xFF);
1523 if (greth->gbit_mac) {
1524 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
1525 NETIF_F_RXCSUM;
1526 dev->features = dev->hw_features | NETIF_F_HIGHDMA;
1527 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1530 if (greth->multicast) {
1531 greth_netdev_ops.ndo_set_rx_mode = greth_set_multicast_list;
1532 dev->flags |= IFF_MULTICAST;
1533 } else {
1534 dev->flags &= ~IFF_MULTICAST;
1537 dev->netdev_ops = &greth_netdev_ops;
1538 dev->ethtool_ops = &greth_ethtool_ops;
1540 err = register_netdev(dev);
1541 if (err) {
1542 if (netif_msg_probe(greth))
1543 dev_err(greth->dev, "netdevice registration failed.\n");
1544 goto error5;
1547 /* setup NAPI */
1548 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1550 return 0;
1552 error5:
1553 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1554 error4:
1555 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1556 error3:
1557 mdiobus_unregister(greth->mdio);
1558 error2:
1559 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1560 error1:
1561 free_netdev(dev);
1562 return err;
1565 static int greth_of_remove(struct platform_device *of_dev)
1567 struct net_device *ndev = platform_get_drvdata(of_dev);
1568 struct greth_private *greth = netdev_priv(ndev);
1570 /* Free descriptor areas */
1571 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1573 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1575 if (greth->phy)
1576 phy_stop(greth->phy);
1577 mdiobus_unregister(greth->mdio);
1579 unregister_netdev(ndev);
1580 free_netdev(ndev);
1582 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1584 return 0;
1587 static struct of_device_id greth_of_match[] = {
1589 .name = "GAISLER_ETHMAC",
1592 .name = "01_01d",
1597 MODULE_DEVICE_TABLE(of, greth_of_match);
1599 static struct platform_driver greth_of_driver = {
1600 .driver = {
1601 .name = "grlib-greth",
1602 .owner = THIS_MODULE,
1603 .of_match_table = greth_of_match,
1605 .probe = greth_of_probe,
1606 .remove = greth_of_remove,
1609 module_platform_driver(greth_of_driver);
1611 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1612 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1613 MODULE_LICENSE("GPL");