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
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>
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>
42 #include <asm/idprom.h>
47 #define GRETH_DEF_MSG_ENABLE \
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,
98 static void greth_print_tx_packet(struct sk_buff *skb)
103 if (skb_shinfo(skb)->nr_frags == 0)
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,
120 static inline void greth_enable_tx(struct greth_private *greth)
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)
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)
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,
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),
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),
203 greth->tx_last = NEXT_TX(greth->tx_last);
205 greth->tx_free += nr_frags+1;
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),
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),
229 static int greth_init_rings(struct greth_private *greth)
232 struct greth_bd *rx_bd, *tx_bd;
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);
245 if (netif_msg_ifup(greth))
246 dev_err(greth->dev, "Error allocating DMA ring.\n");
249 skb_reserve(skb, NET_IP_ALIGN);
250 dma_addr = dma_map_single(greth->dev,
252 MAX_FRAME_SIZE+NET_IP_ALIGN,
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");
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);
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");
278 dma_addr = dma_map_single(greth->dev,
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");
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");
301 dma_addr = dma_map_single(greth->dev,
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");
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. */
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);
331 greth_clean_rings(greth);
335 static int greth_open(struct net_device *dev)
337 struct greth_private *greth = netdev_priv(dev);
340 err = greth_init_rings(greth);
342 if (netif_msg_ifup(greth))
343 dev_err(&dev->dev, "Could not allocate memory for DMA rings\n");
347 err = request_irq(greth->irq, greth_interrupt, 0, "eth", (void *) dev);
349 if (netif_msg_ifup(greth))
350 dev_err(&dev->dev, "Could not allocate interrupt %d\n", dev->irq);
351 greth_clean_rings(greth);
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);
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);
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;
397 bdp = greth->tx_bd_base + greth->tx_next;
399 if (unlikely(greth->tx_free <= 0)) {
400 netif_stop_queue(dev);
401 return NETDEV_TX_BUSY;
404 if (netif_msg_pktdata(greth))
405 greth_print_tx_packet(skb);
408 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
409 dev->stats.tx_errors++;
413 dma_addr = greth_read_bd(&bdp->addr);
415 memcpy((unsigned char *) phys_to_virt(dma_addr), skb->data, skb->len);
417 dma_sync_single_for_device(greth->dev, dma_addr, skb->len, DMA_TO_DEVICE);
419 status = GRETH_BD_EN | (skb->len & GRETH_BD_LEN);
421 /* Wrap around descriptor ring */
422 if (greth->tx_next == GRETH_TXBD_NUM_MASK) {
423 status |= GRETH_BD_WR;
426 greth->tx_next = NEXT_TX(greth->tx_next);
429 /* No more descriptors */
430 if (unlikely(greth->tx_free == 0)) {
432 /* Free transmitted descriptors */
435 /* If nothing was cleaned, stop queue & wait for irq */
436 if (unlikely(greth->tx_free == 0)) {
437 status |= GRETH_BD_IE;
438 netif_stop_queue(dev);
442 /* Write descriptor control word and enable transmission */
443 greth_write_bd(&bdp->stat, status);
444 greth_enable_tx(greth);
453 greth_start_xmit_gbit(struct sk_buff *skb, struct net_device *dev)
455 struct greth_private *greth = netdev_priv(dev);
456 struct greth_bd *bdp;
457 u32 status = 0, dma_addr;
458 int curr_tx, nr_frags, i, err = NETDEV_TX_OK;
460 nr_frags = skb_shinfo(skb)->nr_frags;
462 if (greth->tx_free < nr_frags + 1) {
463 netif_stop_queue(dev);
464 err = NETDEV_TX_BUSY;
468 if (netif_msg_pktdata(greth))
469 greth_print_tx_packet(skb);
471 if (unlikely(skb->len > MAX_FRAME_SIZE)) {
472 dev->stats.tx_errors++;
476 /* Save skb pointer. */
477 greth->tx_skbuff[greth->tx_next] = skb;
481 status = GRETH_TXBD_MORE;
483 status |= GRETH_TXBD_CSALL;
484 status |= skb_headlen(skb) & GRETH_BD_LEN;
485 if (greth->tx_next == GRETH_TXBD_NUM_MASK)
486 status |= GRETH_BD_WR;
489 bdp = greth->tx_bd_base + greth->tx_next;
490 greth_write_bd(&bdp->stat, status);
491 dma_addr = dma_map_single(greth->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
493 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
496 greth_write_bd(&bdp->addr, dma_addr);
498 curr_tx = NEXT_TX(greth->tx_next);
501 for (i = 0; i < nr_frags; i++) {
502 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
503 greth->tx_skbuff[curr_tx] = NULL;
504 bdp = greth->tx_bd_base + curr_tx;
506 status = GRETH_TXBD_CSALL;
507 status |= frag->size & GRETH_BD_LEN;
509 /* Wrap around descriptor ring */
510 if (curr_tx == GRETH_TXBD_NUM_MASK)
511 status |= GRETH_BD_WR;
513 /* More fragments left */
514 if (i < nr_frags - 1)
515 status |= GRETH_TXBD_MORE;
517 /* ... last fragment, check if out of descriptors */
518 else if (greth->tx_free - nr_frags - 1 < (MAX_SKB_FRAGS + 1)) {
520 /* Enable interrupts and stop queue */
521 status |= GRETH_BD_IE;
522 netif_stop_queue(dev);
525 greth_write_bd(&bdp->stat, status);
527 dma_addr = dma_map_page(greth->dev,
533 if (unlikely(dma_mapping_error(greth->dev, dma_addr)))
536 greth_write_bd(&bdp->addr, dma_addr);
538 curr_tx = NEXT_TX(curr_tx);
543 /* Enable the descriptors that we configured ... */
544 for (i = 0; i < nr_frags + 1; i++) {
545 bdp = greth->tx_bd_base + greth->tx_next;
546 greth_write_bd(&bdp->stat, greth_read_bd(&bdp->stat) | GRETH_BD_EN);
547 greth->tx_next = NEXT_TX(greth->tx_next);
551 greth_enable_tx(greth);
556 /* Unmap SKB mappings that succeeded */
557 for (i = 0; greth->tx_next + i != curr_tx; i++) {
558 bdp = greth->tx_bd_base + greth->tx_next + i;
559 dma_unmap_single(greth->dev,
560 greth_read_bd(&bdp->addr),
561 greth_read_bd(&bdp->stat) & GRETH_BD_LEN,
566 dev_warn(greth->dev, "Could not create TX DMA mapping\n");
573 static irqreturn_t greth_interrupt(int irq, void *dev_id)
575 struct net_device *dev = dev_id;
576 struct greth_private *greth;
578 irqreturn_t retval = IRQ_NONE;
580 greth = netdev_priv(dev);
582 spin_lock(&greth->devlock);
584 /* Get the interrupt events that caused us to be here. */
585 status = GRETH_REGLOAD(greth->regs->status);
587 /* Handle rx and tx interrupts through poll */
588 if (status & (GRETH_INT_RX | GRETH_INT_TX)) {
590 /* Clear interrupt status */
591 GRETH_REGORIN(greth->regs->status,
592 status & (GRETH_INT_RX | GRETH_INT_TX));
594 retval = IRQ_HANDLED;
596 /* Disable interrupts and schedule poll() */
597 greth_disable_irqs(greth);
598 napi_schedule(&greth->napi);
602 spin_unlock(&greth->devlock);
607 static void greth_clean_tx(struct net_device *dev)
609 struct greth_private *greth;
610 struct greth_bd *bdp;
613 greth = netdev_priv(dev);
616 bdp = greth->tx_bd_base + greth->tx_last;
617 stat = greth_read_bd(&bdp->stat);
619 if (unlikely(stat & GRETH_BD_EN))
622 if (greth->tx_free == GRETH_TXBD_NUM)
625 /* Check status for errors */
626 if (unlikely(stat & GRETH_TXBD_STATUS)) {
627 dev->stats.tx_errors++;
628 if (stat & GRETH_TXBD_ERR_AL)
629 dev->stats.tx_aborted_errors++;
630 if (stat & GRETH_TXBD_ERR_UE)
631 dev->stats.tx_fifo_errors++;
633 dev->stats.tx_packets++;
634 greth->tx_last = NEXT_TX(greth->tx_last);
638 if (greth->tx_free > 0) {
639 netif_wake_queue(dev);
644 static inline void greth_update_tx_stats(struct net_device *dev, u32 stat)
646 /* Check status for errors */
647 if (unlikely(stat & GRETH_TXBD_STATUS)) {
648 dev->stats.tx_errors++;
649 if (stat & GRETH_TXBD_ERR_AL)
650 dev->stats.tx_aborted_errors++;
651 if (stat & GRETH_TXBD_ERR_UE)
652 dev->stats.tx_fifo_errors++;
653 if (stat & GRETH_TXBD_ERR_LC)
654 dev->stats.tx_aborted_errors++;
656 dev->stats.tx_packets++;
659 static void greth_clean_tx_gbit(struct net_device *dev)
661 struct greth_private *greth;
662 struct greth_bd *bdp, *bdp_last_frag;
667 greth = netdev_priv(dev);
669 while (greth->tx_free < GRETH_TXBD_NUM) {
671 skb = greth->tx_skbuff[greth->tx_last];
673 nr_frags = skb_shinfo(skb)->nr_frags;
675 /* We only clean fully completed SKBs */
676 bdp_last_frag = greth->tx_bd_base + SKIP_TX(greth->tx_last, nr_frags);
677 stat = bdp_last_frag->stat;
679 if (stat & GRETH_BD_EN)
682 greth->tx_skbuff[greth->tx_last] = NULL;
684 greth_update_tx_stats(dev, stat);
686 bdp = greth->tx_bd_base + greth->tx_last;
688 greth->tx_last = NEXT_TX(greth->tx_last);
690 dma_unmap_single(greth->dev,
691 greth_read_bd(&bdp->addr),
695 for (i = 0; i < nr_frags; i++) {
696 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
697 bdp = greth->tx_bd_base + greth->tx_last;
699 dma_unmap_page(greth->dev,
700 greth_read_bd(&bdp->addr),
704 greth->tx_last = NEXT_TX(greth->tx_last);
706 greth->tx_free += nr_frags+1;
709 if (greth->tx_free > (MAX_SKB_FRAGS + 1)) {
710 netif_wake_queue(dev);
714 static int greth_pending_packets(struct greth_private *greth)
716 struct greth_bd *bdp;
718 bdp = greth->rx_bd_base + greth->rx_cur;
719 status = greth_read_bd(&bdp->stat);
720 if (status & GRETH_BD_EN)
726 static int greth_rx(struct net_device *dev, int limit)
728 struct greth_private *greth;
729 struct greth_bd *bdp;
733 u32 status, dma_addr;
735 greth = netdev_priv(dev);
737 for (count = 0; count < limit; ++count) {
739 bdp = greth->rx_bd_base + greth->rx_cur;
740 status = greth_read_bd(&bdp->stat);
741 dma_addr = greth_read_bd(&bdp->addr);
744 if (unlikely(status & GRETH_BD_EN)) {
748 /* Check status for errors. */
749 if (unlikely(status & GRETH_RXBD_STATUS)) {
750 if (status & GRETH_RXBD_ERR_FT) {
751 dev->stats.rx_length_errors++;
754 if (status & (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE)) {
755 dev->stats.rx_frame_errors++;
758 if (status & GRETH_RXBD_ERR_CRC) {
759 dev->stats.rx_crc_errors++;
764 dev->stats.rx_errors++;
768 pkt_len = status & GRETH_BD_LEN;
770 skb = netdev_alloc_skb(dev, pkt_len + NET_IP_ALIGN);
772 if (unlikely(skb == NULL)) {
775 dev_warn(&dev->dev, "low on memory - " "packet dropped\n");
777 dev->stats.rx_dropped++;
780 skb_reserve(skb, NET_IP_ALIGN);
783 dma_sync_single_for_cpu(greth->dev,
788 if (netif_msg_pktdata(greth))
789 greth_print_rx_packet(phys_to_virt(dma_addr), pkt_len);
791 memcpy(skb_put(skb, pkt_len), phys_to_virt(dma_addr), pkt_len);
793 skb->protocol = eth_type_trans(skb, dev);
794 dev->stats.rx_packets++;
795 netif_receive_skb(skb);
799 status = GRETH_BD_EN | GRETH_BD_IE;
800 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
801 status |= GRETH_BD_WR;
805 greth_write_bd(&bdp->stat, status);
807 dma_sync_single_for_device(greth->dev, dma_addr, MAX_FRAME_SIZE, DMA_FROM_DEVICE);
809 greth_enable_rx(greth);
811 greth->rx_cur = NEXT_RX(greth->rx_cur);
817 static inline int hw_checksummed(u32 status)
820 if (status & GRETH_RXBD_IP_FRAG)
823 if (status & GRETH_RXBD_IP && status & GRETH_RXBD_IP_CSERR)
826 if (status & GRETH_RXBD_UDP && status & GRETH_RXBD_UDP_CSERR)
829 if (status & GRETH_RXBD_TCP && status & GRETH_RXBD_TCP_CSERR)
835 static int greth_rx_gbit(struct net_device *dev, int limit)
837 struct greth_private *greth;
838 struct greth_bd *bdp;
839 struct sk_buff *skb, *newskb;
842 u32 status, dma_addr;
844 greth = netdev_priv(dev);
846 for (count = 0; count < limit; ++count) {
848 bdp = greth->rx_bd_base + greth->rx_cur;
849 skb = greth->rx_skbuff[greth->rx_cur];
850 status = greth_read_bd(&bdp->stat);
853 if (status & GRETH_BD_EN)
856 /* Check status for errors. */
857 if (unlikely(status & GRETH_RXBD_STATUS)) {
859 if (status & GRETH_RXBD_ERR_FT) {
860 dev->stats.rx_length_errors++;
863 (GRETH_RXBD_ERR_AE | GRETH_RXBD_ERR_OE | GRETH_RXBD_ERR_LE)) {
864 dev->stats.rx_frame_errors++;
866 } else if (status & GRETH_RXBD_ERR_CRC) {
867 dev->stats.rx_crc_errors++;
872 /* Allocate new skb to replace current */
873 newskb = netdev_alloc_skb(dev, MAX_FRAME_SIZE + NET_IP_ALIGN);
875 if (!bad && newskb) {
876 skb_reserve(newskb, NET_IP_ALIGN);
878 dma_addr = dma_map_single(greth->dev,
880 MAX_FRAME_SIZE + NET_IP_ALIGN,
883 if (!dma_mapping_error(greth->dev, dma_addr)) {
884 /* Process the incoming frame. */
885 pkt_len = status & GRETH_BD_LEN;
887 dma_unmap_single(greth->dev,
888 greth_read_bd(&bdp->addr),
889 MAX_FRAME_SIZE + NET_IP_ALIGN,
892 if (netif_msg_pktdata(greth))
893 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp->addr)), pkt_len);
895 skb_put(skb, pkt_len);
897 if (greth->flags & GRETH_FLAG_RX_CSUM && hw_checksummed(status))
898 skb->ip_summed = CHECKSUM_UNNECESSARY;
900 skb_checksum_none_assert(skb);
902 skb->protocol = eth_type_trans(skb, dev);
903 dev->stats.rx_packets++;
904 netif_receive_skb(skb);
906 greth->rx_skbuff[greth->rx_cur] = newskb;
907 greth_write_bd(&bdp->addr, dma_addr);
910 dev_warn(greth->dev, "Could not create DMA mapping, dropping packet\n");
911 dev_kfree_skb(newskb);
912 dev->stats.rx_dropped++;
916 dev_warn(greth->dev, "Could not allocate SKB, dropping packet\n");
917 dev->stats.rx_dropped++;
920 status = GRETH_BD_EN | GRETH_BD_IE;
921 if (greth->rx_cur == GRETH_RXBD_NUM_MASK) {
922 status |= GRETH_BD_WR;
926 greth_write_bd(&bdp->stat, status);
927 greth_enable_rx(greth);
928 greth->rx_cur = NEXT_RX(greth->rx_cur);
935 static int greth_poll(struct napi_struct *napi, int budget)
937 struct greth_private *greth;
939 greth = container_of(napi, struct greth_private, napi);
941 if (greth->gbit_mac) {
942 greth_clean_tx_gbit(greth->netdev);
944 greth_clean_tx(greth->netdev);
948 if (greth->gbit_mac) {
949 work_done += greth_rx_gbit(greth->netdev, budget - work_done);
951 work_done += greth_rx(greth->netdev, budget - work_done);
954 if (work_done < budget) {
958 if (greth_pending_packets(greth)) {
959 napi_reschedule(napi);
964 greth_enable_irqs(greth);
968 static int greth_set_mac_add(struct net_device *dev, void *p)
970 struct sockaddr *addr = p;
971 struct greth_private *greth;
972 struct greth_regs *regs;
974 greth = netdev_priv(dev);
975 regs = (struct greth_regs *) greth->regs;
977 if (!is_valid_ether_addr(addr->sa_data))
980 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
982 GRETH_REGSAVE(regs->esa_msb, addr->sa_data[0] << 8 | addr->sa_data[1]);
983 GRETH_REGSAVE(regs->esa_lsb,
984 addr->sa_data[2] << 24 | addr->
985 sa_data[3] << 16 | addr->sa_data[4] << 8 | addr->sa_data[5]);
989 static u32 greth_hash_get_index(__u8 *addr)
991 return (ether_crc(6, addr)) & 0x3F;
994 static void greth_set_hash_filter(struct net_device *dev)
996 struct netdev_hw_addr *ha;
997 struct greth_private *greth = netdev_priv(dev);
998 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1002 mc_filter[0] = mc_filter[1] = 0;
1004 netdev_for_each_mc_addr(ha, dev) {
1005 bitnr = greth_hash_get_index(ha->addr);
1006 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1009 GRETH_REGSAVE(regs->hash_msb, mc_filter[1]);
1010 GRETH_REGSAVE(regs->hash_lsb, mc_filter[0]);
1013 static void greth_set_multicast_list(struct net_device *dev)
1016 struct greth_private *greth = netdev_priv(dev);
1017 struct greth_regs *regs = (struct greth_regs *) greth->regs;
1019 cfg = GRETH_REGLOAD(regs->control);
1020 if (dev->flags & IFF_PROMISC)
1021 cfg |= GRETH_CTRL_PR;
1023 cfg &= ~GRETH_CTRL_PR;
1025 if (greth->multicast) {
1026 if (dev->flags & IFF_ALLMULTI) {
1027 GRETH_REGSAVE(regs->hash_msb, -1);
1028 GRETH_REGSAVE(regs->hash_lsb, -1);
1029 cfg |= GRETH_CTRL_MCEN;
1030 GRETH_REGSAVE(regs->control, cfg);
1034 if (netdev_mc_empty(dev)) {
1035 cfg &= ~GRETH_CTRL_MCEN;
1036 GRETH_REGSAVE(regs->control, cfg);
1040 /* Setup multicast filter */
1041 greth_set_hash_filter(dev);
1042 cfg |= GRETH_CTRL_MCEN;
1044 GRETH_REGSAVE(regs->control, cfg);
1047 static u32 greth_get_msglevel(struct net_device *dev)
1049 struct greth_private *greth = netdev_priv(dev);
1050 return greth->msg_enable;
1053 static void greth_set_msglevel(struct net_device *dev, u32 value)
1055 struct greth_private *greth = netdev_priv(dev);
1056 greth->msg_enable = value;
1058 static int greth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1060 struct greth_private *greth = netdev_priv(dev);
1061 struct phy_device *phy = greth->phy;
1066 return phy_ethtool_gset(phy, cmd);
1069 static int greth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1071 struct greth_private *greth = netdev_priv(dev);
1072 struct phy_device *phy = greth->phy;
1077 return phy_ethtool_sset(phy, cmd);
1080 static int greth_get_regs_len(struct net_device *dev)
1082 return sizeof(struct greth_regs);
1085 static void greth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1087 struct greth_private *greth = netdev_priv(dev);
1089 strncpy(info->driver, dev_driver_string(greth->dev), 32);
1090 strncpy(info->version, "revision: 1.0", 32);
1091 strncpy(info->bus_info, greth->dev->bus->name, 32);
1092 strncpy(info->fw_version, "N/A", 32);
1093 info->eedump_len = 0;
1094 info->regdump_len = sizeof(struct greth_regs);
1097 static void greth_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
1100 struct greth_private *greth = netdev_priv(dev);
1101 u32 __iomem *greth_regs = (u32 __iomem *) greth->regs;
1104 for (i = 0; i < sizeof(struct greth_regs) / sizeof(u32); i++)
1105 buff[i] = greth_read_bd(&greth_regs[i]);
1108 static u32 greth_get_rx_csum(struct net_device *dev)
1110 struct greth_private *greth = netdev_priv(dev);
1111 return (greth->flags & GRETH_FLAG_RX_CSUM) != 0;
1114 static int greth_set_rx_csum(struct net_device *dev, u32 data)
1116 struct greth_private *greth = netdev_priv(dev);
1118 spin_lock_bh(&greth->devlock);
1121 greth->flags |= GRETH_FLAG_RX_CSUM;
1123 greth->flags &= ~GRETH_FLAG_RX_CSUM;
1125 spin_unlock_bh(&greth->devlock);
1130 static u32 greth_get_tx_csum(struct net_device *dev)
1132 return (dev->features & NETIF_F_IP_CSUM) != 0;
1135 static int greth_set_tx_csum(struct net_device *dev, u32 data)
1137 netif_tx_lock_bh(dev);
1138 ethtool_op_set_tx_csum(dev, data);
1139 netif_tx_unlock_bh(dev);
1143 static const struct ethtool_ops greth_ethtool_ops = {
1144 .get_msglevel = greth_get_msglevel,
1145 .set_msglevel = greth_set_msglevel,
1146 .get_settings = greth_get_settings,
1147 .set_settings = greth_set_settings,
1148 .get_drvinfo = greth_get_drvinfo,
1149 .get_regs_len = greth_get_regs_len,
1150 .get_regs = greth_get_regs,
1151 .get_rx_csum = greth_get_rx_csum,
1152 .set_rx_csum = greth_set_rx_csum,
1153 .get_tx_csum = greth_get_tx_csum,
1154 .set_tx_csum = greth_set_tx_csum,
1155 .get_link = ethtool_op_get_link,
1158 static struct net_device_ops greth_netdev_ops = {
1159 .ndo_open = greth_open,
1160 .ndo_stop = greth_close,
1161 .ndo_start_xmit = greth_start_xmit,
1162 .ndo_set_mac_address = greth_set_mac_add,
1163 .ndo_validate_addr = eth_validate_addr,
1166 static inline int wait_for_mdio(struct greth_private *greth)
1168 unsigned long timeout = jiffies + 4*HZ/100;
1169 while (GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_BUSY) {
1170 if (time_after(jiffies, timeout))
1176 static int greth_mdio_read(struct mii_bus *bus, int phy, int reg)
1178 struct greth_private *greth = bus->priv;
1181 if (!wait_for_mdio(greth))
1184 GRETH_REGSAVE(greth->regs->mdio, ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 2);
1186 if (!wait_for_mdio(greth))
1189 if (!(GRETH_REGLOAD(greth->regs->mdio) & GRETH_MII_NVALID)) {
1190 data = (GRETH_REGLOAD(greth->regs->mdio) >> 16) & 0xFFFF;
1198 static int greth_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
1200 struct greth_private *greth = bus->priv;
1202 if (!wait_for_mdio(greth))
1205 GRETH_REGSAVE(greth->regs->mdio,
1206 ((val & 0xFFFF) << 16) | ((phy & 0x1F) << 11) | ((reg & 0x1F) << 6) | 1);
1208 if (!wait_for_mdio(greth))
1214 static int greth_mdio_reset(struct mii_bus *bus)
1219 static void greth_link_change(struct net_device *dev)
1221 struct greth_private *greth = netdev_priv(dev);
1222 struct phy_device *phydev = greth->phy;
1223 unsigned long flags;
1225 int status_change = 0;
1227 spin_lock_irqsave(&greth->devlock, flags);
1231 if ((greth->speed != phydev->speed) || (greth->duplex != phydev->duplex)) {
1233 GRETH_REGANDIN(greth->regs->control,
1234 ~(GRETH_CTRL_FD | GRETH_CTRL_SP | GRETH_CTRL_GB));
1237 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_FD);
1239 if (phydev->speed == SPEED_100) {
1241 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_SP);
1244 else if (phydev->speed == SPEED_1000)
1245 GRETH_REGORIN(greth->regs->control, GRETH_CTRL_GB);
1247 greth->speed = phydev->speed;
1248 greth->duplex = phydev->duplex;
1253 if (phydev->link != greth->link) {
1254 if (!phydev->link) {
1258 greth->link = phydev->link;
1263 spin_unlock_irqrestore(&greth->devlock, flags);
1265 if (status_change) {
1267 pr_debug("%s: link up (%d/%s)\n",
1268 dev->name, phydev->speed,
1269 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
1271 pr_debug("%s: link down\n", dev->name);
1275 static int greth_mdio_probe(struct net_device *dev)
1277 struct greth_private *greth = netdev_priv(dev);
1278 struct phy_device *phy = NULL;
1281 /* Find the first PHY */
1282 phy = phy_find_first(greth->mdio);
1285 if (netif_msg_probe(greth))
1286 dev_err(&dev->dev, "no PHY found\n");
1290 ret = phy_connect_direct(dev, phy, &greth_link_change,
1291 0, greth->gbit_mac ?
1292 PHY_INTERFACE_MODE_GMII :
1293 PHY_INTERFACE_MODE_MII);
1295 if (netif_msg_ifup(greth))
1296 dev_err(&dev->dev, "could not attach to PHY\n");
1300 if (greth->gbit_mac)
1301 phy->supported &= PHY_GBIT_FEATURES;
1303 phy->supported &= PHY_BASIC_FEATURES;
1305 phy->advertising = phy->supported;
1315 static inline int phy_aneg_done(struct phy_device *phydev)
1319 retval = phy_read(phydev, MII_BMSR);
1321 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1324 static int greth_mdio_init(struct greth_private *greth)
1327 unsigned long timeout;
1329 greth->mdio = mdiobus_alloc();
1334 greth->mdio->name = "greth-mdio";
1335 snprintf(greth->mdio->id, MII_BUS_ID_SIZE, "%s-%d", greth->mdio->name, greth->irq);
1336 greth->mdio->read = greth_mdio_read;
1337 greth->mdio->write = greth_mdio_write;
1338 greth->mdio->reset = greth_mdio_reset;
1339 greth->mdio->priv = greth;
1341 greth->mdio->irq = greth->mdio_irqs;
1343 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1344 greth->mdio->irq[phy] = PHY_POLL;
1346 ret = mdiobus_register(greth->mdio);
1351 ret = greth_mdio_probe(greth->netdev);
1353 if (netif_msg_probe(greth))
1354 dev_err(&greth->netdev->dev, "failed to probe MDIO bus\n");
1358 phy_start(greth->phy);
1360 /* If Ethernet debug link is used make autoneg happen right away */
1361 if (greth->edcl && greth_edcl == 1) {
1362 phy_start_aneg(greth->phy);
1363 timeout = jiffies + 6*HZ;
1364 while (!phy_aneg_done(greth->phy) && time_before(jiffies, timeout)) {
1366 genphy_read_status(greth->phy);
1367 greth_link_change(greth->netdev);
1373 mdiobus_unregister(greth->mdio);
1375 mdiobus_free(greth->mdio);
1379 /* Initialize the GRETH MAC */
1380 static int __devinit greth_of_probe(struct platform_device *ofdev, const struct of_device_id *match)
1382 struct net_device *dev;
1383 struct greth_private *greth;
1384 struct greth_regs *regs;
1389 unsigned long timeout;
1391 dev = alloc_etherdev(sizeof(struct greth_private));
1396 greth = netdev_priv(dev);
1397 greth->netdev = dev;
1398 greth->dev = &ofdev->dev;
1400 if (greth_debug > 0)
1401 greth->msg_enable = greth_debug;
1403 greth->msg_enable = GRETH_DEF_MSG_ENABLE;
1405 spin_lock_init(&greth->devlock);
1407 greth->regs = of_ioremap(&ofdev->resource[0], 0,
1408 resource_size(&ofdev->resource[0]),
1409 "grlib-greth regs");
1411 if (greth->regs == NULL) {
1412 if (netif_msg_probe(greth))
1413 dev_err(greth->dev, "ioremap failure.\n");
1418 regs = (struct greth_regs *) greth->regs;
1419 greth->irq = ofdev->archdata.irqs[0];
1421 dev_set_drvdata(greth->dev, dev);
1422 SET_NETDEV_DEV(dev, greth->dev);
1424 if (netif_msg_probe(greth))
1425 dev_dbg(greth->dev, "reseting controller.\n");
1427 /* Reset the controller. */
1428 GRETH_REGSAVE(regs->control, GRETH_RESET);
1430 /* Wait for MAC to reset itself */
1431 timeout = jiffies + HZ/100;
1432 while (GRETH_REGLOAD(regs->control) & GRETH_RESET) {
1433 if (time_after(jiffies, timeout)) {
1435 if (netif_msg_probe(greth))
1436 dev_err(greth->dev, "timeout when waiting for reset.\n");
1441 /* Get default PHY address */
1442 greth->phyaddr = (GRETH_REGLOAD(regs->mdio) >> 11) & 0x1F;
1444 /* Check if we have GBIT capable MAC */
1445 tmp = GRETH_REGLOAD(regs->control);
1446 greth->gbit_mac = (tmp >> 27) & 1;
1448 /* Check for multicast capability */
1449 greth->multicast = (tmp >> 25) & 1;
1451 greth->edcl = (tmp >> 31) & 1;
1453 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1454 * it doesn't interfere with the software */
1455 if (greth->edcl != 0)
1456 GRETH_REGORIN(regs->control, GRETH_CTRL_DISDUPLEX);
1458 /* Check if MAC can handle MDIO interrupts */
1459 greth->mdio_int_en = (tmp >> 26) & 1;
1461 err = greth_mdio_init(greth);
1463 if (netif_msg_probe(greth))
1464 dev_err(greth->dev, "failed to register MDIO bus\n");
1468 /* Allocate TX descriptor ring in coherent memory */
1469 greth->tx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1471 &greth->tx_bd_base_phys,
1474 if (!greth->tx_bd_base) {
1475 if (netif_msg_probe(greth))
1476 dev_err(&dev->dev, "could not allocate descriptor memory.\n");
1481 memset(greth->tx_bd_base, 0, 1024);
1483 /* Allocate RX descriptor ring in coherent memory */
1484 greth->rx_bd_base = (struct greth_bd *) dma_alloc_coherent(greth->dev,
1486 &greth->rx_bd_base_phys,
1489 if (!greth->rx_bd_base) {
1490 if (netif_msg_probe(greth))
1491 dev_err(greth->dev, "could not allocate descriptor memory.\n");
1496 memset(greth->rx_bd_base, 0, 1024);
1498 /* Get MAC address from: module param, OF property or ID prom */
1499 for (i = 0; i < 6; i++) {
1500 if (macaddr[i] != 0)
1504 const unsigned char *addr;
1506 addr = of_get_property(ofdev->dev.of_node, "local-mac-address",
1508 if (addr != NULL && len == 6) {
1509 for (i = 0; i < 6; i++)
1510 macaddr[i] = (unsigned int) addr[i];
1513 for (i = 0; i < 6; i++)
1514 macaddr[i] = (unsigned int) idprom->id_ethaddr[i];
1519 for (i = 0; i < 6; i++)
1520 dev->dev_addr[i] = macaddr[i];
1524 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
1525 if (netif_msg_probe(greth))
1526 dev_err(greth->dev, "no valid ethernet address, aborting.\n");
1531 GRETH_REGSAVE(regs->esa_msb, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
1532 GRETH_REGSAVE(regs->esa_lsb, dev->dev_addr[2] << 24 | dev->dev_addr[3] << 16 |
1533 dev->dev_addr[4] << 8 | dev->dev_addr[5]);
1535 /* Clear all pending interrupts except PHY irq */
1536 GRETH_REGSAVE(regs->status, 0xFF);
1538 if (greth->gbit_mac) {
1539 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_HIGHDMA;
1540 greth_netdev_ops.ndo_start_xmit = greth_start_xmit_gbit;
1541 greth->flags = GRETH_FLAG_RX_CSUM;
1544 if (greth->multicast) {
1545 greth_netdev_ops.ndo_set_multicast_list = greth_set_multicast_list;
1546 dev->flags |= IFF_MULTICAST;
1548 dev->flags &= ~IFF_MULTICAST;
1551 dev->netdev_ops = &greth_netdev_ops;
1552 dev->ethtool_ops = &greth_ethtool_ops;
1554 err = register_netdev(dev);
1556 if (netif_msg_probe(greth))
1557 dev_err(greth->dev, "netdevice registration failed.\n");
1562 netif_napi_add(dev, &greth->napi, greth_poll, 64);
1567 dma_free_coherent(greth->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1569 dma_free_coherent(greth->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1571 mdiobus_unregister(greth->mdio);
1573 of_iounmap(&ofdev->resource[0], greth->regs, resource_size(&ofdev->resource[0]));
1579 static int __devexit greth_of_remove(struct platform_device *of_dev)
1581 struct net_device *ndev = dev_get_drvdata(&of_dev->dev);
1582 struct greth_private *greth = netdev_priv(ndev);
1584 /* Free descriptor areas */
1585 dma_free_coherent(&of_dev->dev, 1024, greth->rx_bd_base, greth->rx_bd_base_phys);
1587 dma_free_coherent(&of_dev->dev, 1024, greth->tx_bd_base, greth->tx_bd_base_phys);
1589 dev_set_drvdata(&of_dev->dev, NULL);
1592 phy_stop(greth->phy);
1593 mdiobus_unregister(greth->mdio);
1595 unregister_netdev(ndev);
1598 of_iounmap(&of_dev->resource[0], greth->regs, resource_size(&of_dev->resource[0]));
1603 static struct of_device_id greth_of_match[] = {
1605 .name = "GAISLER_ETHMAC",
1613 MODULE_DEVICE_TABLE(of, greth_of_match);
1615 static struct of_platform_driver greth_of_driver = {
1617 .name = "grlib-greth",
1618 .owner = THIS_MODULE,
1619 .of_match_table = greth_of_match,
1621 .probe = greth_of_probe,
1622 .remove = __devexit_p(greth_of_remove),
1625 static int __init greth_init(void)
1627 return of_register_platform_driver(&greth_of_driver);
1630 static void __exit greth_cleanup(void)
1632 of_unregister_platform_driver(&greth_of_driver);
1635 module_init(greth_init);
1636 module_exit(greth_cleanup);
1638 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1639 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1640 MODULE_LICENSE("GPL");