1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Aeroflex Gaisler GRETH 10/100/1G Ethernet MAC.
5 * 2005-2010 (c) Aeroflex Gaisler AB
7 * This driver supports GRETH 10/100 and GRETH 10/100/1G Ethernet MACs
8 * available in the GRLIB VHDL IP core library.
10 * Full documentation of both cores can be found here:
11 * https://www.gaisler.com/products/grlib/grip.pdf
13 * The Gigabit version supports scatter/gather DMA, any alignment of
14 * buffers and checksum offloading.
16 * Contributors: Kristoffer Glembo
21 #include <linux/dma-mapping.h>
22 #include <linux/module.h>
23 #include <linux/uaccess.h>
24 #include <linux/interrupt.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/skbuff.h>
30 #include <linux/crc32.h>
31 #include <linux/mii.h>
32 #include <linux/of_device.h>
33 #include <linux/of_net.h>
34 #include <linux/of_platform.h>
35 #include <linux/slab.h>
36 #include <asm/cacheflush.h>
37 #include <asm/byteorder.h>
40 #include <asm/idprom.h>
45 #define GRETH_DEF_MSG_ENABLE \
54 static int greth_debug
= -1; /* -1 == use GRETH_DEF_MSG_ENABLE as value */
55 module_param(greth_debug
, int, 0);
56 MODULE_PARM_DESC(greth_debug
, "GRETH bitmapped debugging message enable value");
58 /* Accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
59 static int macaddr
[6];
60 module_param_array(macaddr
, int, NULL
, 0);
61 MODULE_PARM_DESC(macaddr
, "GRETH Ethernet MAC address");
63 static int greth_edcl
= 1;
64 module_param(greth_edcl
, int, 0);
65 MODULE_PARM_DESC(greth_edcl
, "GRETH EDCL usage indicator. Set to 1 if EDCL is used.");
67 static int greth_open(struct net_device
*dev
);
68 static netdev_tx_t
greth_start_xmit(struct sk_buff
*skb
,
69 struct net_device
*dev
);
70 static netdev_tx_t
greth_start_xmit_gbit(struct sk_buff
*skb
,
71 struct net_device
*dev
);
72 static int greth_rx(struct net_device
*dev
, int limit
);
73 static int greth_rx_gbit(struct net_device
*dev
, int limit
);
74 static void greth_clean_tx(struct net_device
*dev
);
75 static void greth_clean_tx_gbit(struct net_device
*dev
);
76 static irqreturn_t
greth_interrupt(int irq
, void *dev_id
);
77 static int greth_close(struct net_device
*dev
);
78 static int greth_set_mac_add(struct net_device
*dev
, void *p
);
79 static void greth_set_multicast_list(struct net_device
*dev
);
81 #define GRETH_REGLOAD(a) (be32_to_cpu(__raw_readl(&(a))))
82 #define GRETH_REGSAVE(a, v) (__raw_writel(cpu_to_be32(v), &(a)))
83 #define GRETH_REGORIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) | (v))))
84 #define GRETH_REGANDIN(a, v) (GRETH_REGSAVE(a, (GRETH_REGLOAD(a) & (v))))
86 #define NEXT_TX(N) (((N) + 1) & GRETH_TXBD_NUM_MASK)
87 #define SKIP_TX(N, C) (((N) + C) & GRETH_TXBD_NUM_MASK)
88 #define NEXT_RX(N) (((N) + 1) & GRETH_RXBD_NUM_MASK)
90 static void greth_print_rx_packet(void *addr
, int len
)
92 print_hex_dump(KERN_DEBUG
, "RX: ", DUMP_PREFIX_OFFSET
, 16, 1,
96 static void greth_print_tx_packet(struct sk_buff
*skb
)
101 if (skb_shinfo(skb
)->nr_frags
== 0)
104 length
= skb_headlen(skb
);
106 print_hex_dump(KERN_DEBUG
, "TX: ", DUMP_PREFIX_OFFSET
, 16, 1,
107 skb
->data
, length
, true);
109 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
111 print_hex_dump(KERN_DEBUG
, "TX: ", DUMP_PREFIX_OFFSET
, 16, 1,
112 skb_frag_address(&skb_shinfo(skb
)->frags
[i
]),
113 skb_frag_size(&skb_shinfo(skb
)->frags
[i
]), true);
117 static inline void greth_enable_tx(struct greth_private
*greth
)
120 GRETH_REGORIN(greth
->regs
->control
, GRETH_TXEN
);
123 static inline void greth_enable_tx_and_irq(struct greth_private
*greth
)
125 wmb(); /* BDs must been written to memory before enabling TX */
126 GRETH_REGORIN(greth
->regs
->control
, GRETH_TXEN
| GRETH_TXI
);
129 static inline void greth_disable_tx(struct greth_private
*greth
)
131 GRETH_REGANDIN(greth
->regs
->control
, ~GRETH_TXEN
);
134 static inline void greth_enable_rx(struct greth_private
*greth
)
137 GRETH_REGORIN(greth
->regs
->control
, GRETH_RXEN
);
140 static inline void greth_disable_rx(struct greth_private
*greth
)
142 GRETH_REGANDIN(greth
->regs
->control
, ~GRETH_RXEN
);
145 static inline void greth_enable_irqs(struct greth_private
*greth
)
147 GRETH_REGORIN(greth
->regs
->control
, GRETH_RXI
| GRETH_TXI
);
150 static inline void greth_disable_irqs(struct greth_private
*greth
)
152 GRETH_REGANDIN(greth
->regs
->control
, ~(GRETH_RXI
|GRETH_TXI
));
155 static inline void greth_write_bd(u32
*bd
, u32 val
)
157 __raw_writel(cpu_to_be32(val
), bd
);
160 static inline u32
greth_read_bd(u32
*bd
)
162 return be32_to_cpu(__raw_readl(bd
));
165 static void greth_clean_rings(struct greth_private
*greth
)
168 struct greth_bd
*rx_bdp
= greth
->rx_bd_base
;
169 struct greth_bd
*tx_bdp
= greth
->tx_bd_base
;
171 if (greth
->gbit_mac
) {
173 /* Free and unmap RX buffers */
174 for (i
= 0; i
< GRETH_RXBD_NUM
; i
++, rx_bdp
++) {
175 if (greth
->rx_skbuff
[i
] != NULL
) {
176 dev_kfree_skb(greth
->rx_skbuff
[i
]);
177 dma_unmap_single(greth
->dev
,
178 greth_read_bd(&rx_bdp
->addr
),
179 MAX_FRAME_SIZE
+NET_IP_ALIGN
,
185 while (greth
->tx_free
< GRETH_TXBD_NUM
) {
187 struct sk_buff
*skb
= greth
->tx_skbuff
[greth
->tx_last
];
188 int nr_frags
= skb_shinfo(skb
)->nr_frags
;
189 tx_bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
190 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
192 dma_unmap_single(greth
->dev
,
193 greth_read_bd(&tx_bdp
->addr
),
197 for (i
= 0; i
< nr_frags
; i
++) {
198 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
199 tx_bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
201 dma_unmap_page(greth
->dev
,
202 greth_read_bd(&tx_bdp
->addr
),
206 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
208 greth
->tx_free
+= nr_frags
+1;
213 } else { /* 10/100 Mbps MAC */
215 for (i
= 0; i
< GRETH_RXBD_NUM
; i
++, rx_bdp
++) {
216 kfree(greth
->rx_bufs
[i
]);
217 dma_unmap_single(greth
->dev
,
218 greth_read_bd(&rx_bdp
->addr
),
222 for (i
= 0; i
< GRETH_TXBD_NUM
; i
++, tx_bdp
++) {
223 kfree(greth
->tx_bufs
[i
]);
224 dma_unmap_single(greth
->dev
,
225 greth_read_bd(&tx_bdp
->addr
),
232 static int greth_init_rings(struct greth_private
*greth
)
235 struct greth_bd
*rx_bd
, *tx_bd
;
239 rx_bd
= greth
->rx_bd_base
;
240 tx_bd
= greth
->tx_bd_base
;
242 /* Initialize descriptor rings and buffers */
243 if (greth
->gbit_mac
) {
245 for (i
= 0; i
< GRETH_RXBD_NUM
; i
++) {
246 skb
= netdev_alloc_skb(greth
->netdev
, MAX_FRAME_SIZE
+NET_IP_ALIGN
);
248 if (netif_msg_ifup(greth
))
249 dev_err(greth
->dev
, "Error allocating DMA ring.\n");
252 skb_reserve(skb
, NET_IP_ALIGN
);
253 dma_addr
= dma_map_single(greth
->dev
,
255 MAX_FRAME_SIZE
+NET_IP_ALIGN
,
258 if (dma_mapping_error(greth
->dev
, dma_addr
)) {
259 if (netif_msg_ifup(greth
))
260 dev_err(greth
->dev
, "Could not create initial DMA mapping\n");
263 greth
->rx_skbuff
[i
] = skb
;
264 greth_write_bd(&rx_bd
[i
].addr
, dma_addr
);
265 greth_write_bd(&rx_bd
[i
].stat
, GRETH_BD_EN
| GRETH_BD_IE
);
270 /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
271 for (i
= 0; i
< GRETH_RXBD_NUM
; i
++) {
273 greth
->rx_bufs
[i
] = kmalloc(MAX_FRAME_SIZE
, GFP_KERNEL
);
275 if (greth
->rx_bufs
[i
] == NULL
) {
276 if (netif_msg_ifup(greth
))
277 dev_err(greth
->dev
, "Error allocating DMA ring.\n");
281 dma_addr
= dma_map_single(greth
->dev
,
286 if (dma_mapping_error(greth
->dev
, dma_addr
)) {
287 if (netif_msg_ifup(greth
))
288 dev_err(greth
->dev
, "Could not create initial DMA mapping\n");
291 greth_write_bd(&rx_bd
[i
].addr
, dma_addr
);
292 greth_write_bd(&rx_bd
[i
].stat
, GRETH_BD_EN
| GRETH_BD_IE
);
294 for (i
= 0; i
< GRETH_TXBD_NUM
; i
++) {
296 greth
->tx_bufs
[i
] = kmalloc(MAX_FRAME_SIZE
, GFP_KERNEL
);
298 if (greth
->tx_bufs
[i
] == NULL
) {
299 if (netif_msg_ifup(greth
))
300 dev_err(greth
->dev
, "Error allocating DMA ring.\n");
304 dma_addr
= dma_map_single(greth
->dev
,
309 if (dma_mapping_error(greth
->dev
, dma_addr
)) {
310 if (netif_msg_ifup(greth
))
311 dev_err(greth
->dev
, "Could not create initial DMA mapping\n");
314 greth_write_bd(&tx_bd
[i
].addr
, dma_addr
);
315 greth_write_bd(&tx_bd
[i
].stat
, 0);
318 greth_write_bd(&rx_bd
[GRETH_RXBD_NUM
- 1].stat
,
319 greth_read_bd(&rx_bd
[GRETH_RXBD_NUM
- 1].stat
) | GRETH_BD_WR
);
321 /* Initialize pointers. */
325 greth
->tx_free
= GRETH_TXBD_NUM
;
327 /* Initialize descriptor base address */
328 GRETH_REGSAVE(greth
->regs
->tx_desc_p
, greth
->tx_bd_base_phys
);
329 GRETH_REGSAVE(greth
->regs
->rx_desc_p
, greth
->rx_bd_base_phys
);
334 greth_clean_rings(greth
);
338 static int greth_open(struct net_device
*dev
)
340 struct greth_private
*greth
= netdev_priv(dev
);
343 err
= greth_init_rings(greth
);
345 if (netif_msg_ifup(greth
))
346 dev_err(&dev
->dev
, "Could not allocate memory for DMA rings\n");
350 err
= request_irq(greth
->irq
, greth_interrupt
, 0, "eth", (void *) dev
);
352 if (netif_msg_ifup(greth
))
353 dev_err(&dev
->dev
, "Could not allocate interrupt %d\n", dev
->irq
);
354 greth_clean_rings(greth
);
358 if (netif_msg_ifup(greth
))
359 dev_dbg(&dev
->dev
, " starting queue\n");
360 netif_start_queue(dev
);
362 GRETH_REGSAVE(greth
->regs
->status
, 0xFF);
364 napi_enable(&greth
->napi
);
366 greth_enable_irqs(greth
);
367 greth_enable_tx(greth
);
368 greth_enable_rx(greth
);
373 static int greth_close(struct net_device
*dev
)
375 struct greth_private
*greth
= netdev_priv(dev
);
377 napi_disable(&greth
->napi
);
379 greth_disable_irqs(greth
);
380 greth_disable_tx(greth
);
381 greth_disable_rx(greth
);
383 netif_stop_queue(dev
);
385 free_irq(greth
->irq
, (void *) dev
);
387 greth_clean_rings(greth
);
393 greth_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
395 struct greth_private
*greth
= netdev_priv(dev
);
396 struct greth_bd
*bdp
;
397 int err
= NETDEV_TX_OK
;
398 u32 status
, dma_addr
, ctrl
;
402 greth_clean_tx(greth
->netdev
);
404 if (unlikely(greth
->tx_free
<= 0)) {
405 spin_lock_irqsave(&greth
->devlock
, flags
);/*save from poll/irq*/
406 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
407 /* Enable TX IRQ only if not already in poll() routine */
408 if (ctrl
& GRETH_RXI
)
409 GRETH_REGSAVE(greth
->regs
->control
, ctrl
| GRETH_TXI
);
410 netif_stop_queue(dev
);
411 spin_unlock_irqrestore(&greth
->devlock
, flags
);
412 return NETDEV_TX_BUSY
;
415 if (netif_msg_pktdata(greth
))
416 greth_print_tx_packet(skb
);
419 if (unlikely(skb
->len
> MAX_FRAME_SIZE
)) {
420 dev
->stats
.tx_errors
++;
424 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
425 dma_addr
= greth_read_bd(&bdp
->addr
);
427 memcpy((unsigned char *) phys_to_virt(dma_addr
), skb
->data
, skb
->len
);
429 dma_sync_single_for_device(greth
->dev
, dma_addr
, skb
->len
, DMA_TO_DEVICE
);
431 status
= GRETH_BD_EN
| GRETH_BD_IE
| (skb
->len
& GRETH_BD_LEN
);
432 greth
->tx_bufs_length
[greth
->tx_next
] = skb
->len
& GRETH_BD_LEN
;
434 /* Wrap around descriptor ring */
435 if (greth
->tx_next
== GRETH_TXBD_NUM_MASK
) {
436 status
|= GRETH_BD_WR
;
439 greth
->tx_next
= NEXT_TX(greth
->tx_next
);
442 /* Write descriptor control word and enable transmission */
443 greth_write_bd(&bdp
->stat
, status
);
444 spin_lock_irqsave(&greth
->devlock
, flags
); /*save from poll/irq*/
445 greth_enable_tx(greth
);
446 spin_unlock_irqrestore(&greth
->devlock
, flags
);
453 static inline u16
greth_num_free_bds(u16 tx_last
, u16 tx_next
)
455 if (tx_next
< tx_last
)
456 return (tx_last
- tx_next
) - 1;
458 return GRETH_TXBD_NUM
- (tx_next
- tx_last
) - 1;
462 greth_start_xmit_gbit(struct sk_buff
*skb
, struct net_device
*dev
)
464 struct greth_private
*greth
= netdev_priv(dev
);
465 struct greth_bd
*bdp
;
466 u32 status
, dma_addr
;
467 int curr_tx
, nr_frags
, i
, err
= NETDEV_TX_OK
;
471 nr_frags
= skb_shinfo(skb
)->nr_frags
;
472 tx_last
= greth
->tx_last
;
473 rmb(); /* tx_last is updated by the poll task */
475 if (greth_num_free_bds(tx_last
, greth
->tx_next
) < nr_frags
+ 1) {
476 netif_stop_queue(dev
);
477 err
= NETDEV_TX_BUSY
;
481 if (netif_msg_pktdata(greth
))
482 greth_print_tx_packet(skb
);
484 if (unlikely(skb
->len
> MAX_FRAME_SIZE
)) {
485 dev
->stats
.tx_errors
++;
489 /* Save skb pointer. */
490 greth
->tx_skbuff
[greth
->tx_next
] = skb
;
494 status
= GRETH_TXBD_MORE
;
496 status
= GRETH_BD_IE
;
498 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
499 status
|= GRETH_TXBD_CSALL
;
500 status
|= skb_headlen(skb
) & GRETH_BD_LEN
;
501 if (greth
->tx_next
== GRETH_TXBD_NUM_MASK
)
502 status
|= GRETH_BD_WR
;
505 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
506 greth_write_bd(&bdp
->stat
, status
);
507 dma_addr
= dma_map_single(greth
->dev
, skb
->data
, skb_headlen(skb
), DMA_TO_DEVICE
);
509 if (unlikely(dma_mapping_error(greth
->dev
, dma_addr
)))
512 greth_write_bd(&bdp
->addr
, dma_addr
);
514 curr_tx
= NEXT_TX(greth
->tx_next
);
517 for (i
= 0; i
< nr_frags
; i
++) {
518 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
519 greth
->tx_skbuff
[curr_tx
] = NULL
;
520 bdp
= greth
->tx_bd_base
+ curr_tx
;
522 status
= GRETH_BD_EN
;
523 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
524 status
|= GRETH_TXBD_CSALL
;
525 status
|= skb_frag_size(frag
) & GRETH_BD_LEN
;
527 /* Wrap around descriptor ring */
528 if (curr_tx
== GRETH_TXBD_NUM_MASK
)
529 status
|= GRETH_BD_WR
;
531 /* More fragments left */
532 if (i
< nr_frags
- 1)
533 status
|= GRETH_TXBD_MORE
;
535 status
|= GRETH_BD_IE
; /* enable IRQ on last fragment */
537 greth_write_bd(&bdp
->stat
, status
);
539 dma_addr
= skb_frag_dma_map(greth
->dev
, frag
, 0, skb_frag_size(frag
),
542 if (unlikely(dma_mapping_error(greth
->dev
, dma_addr
)))
545 greth_write_bd(&bdp
->addr
, dma_addr
);
547 curr_tx
= NEXT_TX(curr_tx
);
552 /* Enable the descriptor chain by enabling the first descriptor */
553 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
554 greth_write_bd(&bdp
->stat
,
555 greth_read_bd(&bdp
->stat
) | GRETH_BD_EN
);
557 spin_lock_irqsave(&greth
->devlock
, flags
); /*save from poll/irq*/
558 greth
->tx_next
= curr_tx
;
559 greth_enable_tx_and_irq(greth
);
560 spin_unlock_irqrestore(&greth
->devlock
, flags
);
565 /* Unmap SKB mappings that succeeded and disable descriptor */
566 for (i
= 0; greth
->tx_next
+ i
!= curr_tx
; i
++) {
567 bdp
= greth
->tx_bd_base
+ greth
->tx_next
+ i
;
568 dma_unmap_single(greth
->dev
,
569 greth_read_bd(&bdp
->addr
),
570 greth_read_bd(&bdp
->stat
) & GRETH_BD_LEN
,
572 greth_write_bd(&bdp
->stat
, 0);
576 dev_warn(greth
->dev
, "Could not create TX DMA mapping\n");
582 static irqreturn_t
greth_interrupt(int irq
, void *dev_id
)
584 struct net_device
*dev
= dev_id
;
585 struct greth_private
*greth
;
587 irqreturn_t retval
= IRQ_NONE
;
589 greth
= netdev_priv(dev
);
591 spin_lock(&greth
->devlock
);
593 /* Get the interrupt events that caused us to be here. */
594 status
= GRETH_REGLOAD(greth
->regs
->status
);
596 /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
597 * set regardless of whether IRQ is enabled or not. Especially
598 * important when shared IRQ.
600 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
602 /* Handle rx and tx interrupts through poll */
603 if (((status
& (GRETH_INT_RE
| GRETH_INT_RX
)) && (ctrl
& GRETH_RXI
)) ||
604 ((status
& (GRETH_INT_TE
| GRETH_INT_TX
)) && (ctrl
& GRETH_TXI
))) {
605 retval
= IRQ_HANDLED
;
607 /* Disable interrupts and schedule poll() */
608 greth_disable_irqs(greth
);
609 napi_schedule(&greth
->napi
);
612 spin_unlock(&greth
->devlock
);
617 static void greth_clean_tx(struct net_device
*dev
)
619 struct greth_private
*greth
;
620 struct greth_bd
*bdp
;
623 greth
= netdev_priv(dev
);
626 bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
627 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_TE
| GRETH_INT_TX
);
629 stat
= greth_read_bd(&bdp
->stat
);
631 if (unlikely(stat
& GRETH_BD_EN
))
634 if (greth
->tx_free
== GRETH_TXBD_NUM
)
637 /* Check status for errors */
638 if (unlikely(stat
& GRETH_TXBD_STATUS
)) {
639 dev
->stats
.tx_errors
++;
640 if (stat
& GRETH_TXBD_ERR_AL
)
641 dev
->stats
.tx_aborted_errors
++;
642 if (stat
& GRETH_TXBD_ERR_UE
)
643 dev
->stats
.tx_fifo_errors
++;
645 dev
->stats
.tx_packets
++;
646 dev
->stats
.tx_bytes
+= greth
->tx_bufs_length
[greth
->tx_last
];
647 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
651 if (greth
->tx_free
> 0) {
652 netif_wake_queue(dev
);
656 static inline void greth_update_tx_stats(struct net_device
*dev
, u32 stat
)
658 /* Check status for errors */
659 if (unlikely(stat
& GRETH_TXBD_STATUS
)) {
660 dev
->stats
.tx_errors
++;
661 if (stat
& GRETH_TXBD_ERR_AL
)
662 dev
->stats
.tx_aborted_errors
++;
663 if (stat
& GRETH_TXBD_ERR_UE
)
664 dev
->stats
.tx_fifo_errors
++;
665 if (stat
& GRETH_TXBD_ERR_LC
)
666 dev
->stats
.tx_aborted_errors
++;
668 dev
->stats
.tx_packets
++;
671 static void greth_clean_tx_gbit(struct net_device
*dev
)
673 struct greth_private
*greth
;
674 struct greth_bd
*bdp
, *bdp_last_frag
;
675 struct sk_buff
*skb
= NULL
;
680 greth
= netdev_priv(dev
);
681 tx_last
= greth
->tx_last
;
683 while (tx_last
!= greth
->tx_next
) {
685 skb
= greth
->tx_skbuff
[tx_last
];
687 nr_frags
= skb_shinfo(skb
)->nr_frags
;
689 /* We only clean fully completed SKBs */
690 bdp_last_frag
= greth
->tx_bd_base
+ SKIP_TX(tx_last
, nr_frags
);
692 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_TE
| GRETH_INT_TX
);
694 stat
= greth_read_bd(&bdp_last_frag
->stat
);
696 if (stat
& GRETH_BD_EN
)
699 greth
->tx_skbuff
[tx_last
] = NULL
;
701 greth_update_tx_stats(dev
, stat
);
702 dev
->stats
.tx_bytes
+= skb
->len
;
704 bdp
= greth
->tx_bd_base
+ tx_last
;
706 tx_last
= NEXT_TX(tx_last
);
708 dma_unmap_single(greth
->dev
,
709 greth_read_bd(&bdp
->addr
),
713 for (i
= 0; i
< nr_frags
; i
++) {
714 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
715 bdp
= greth
->tx_bd_base
+ tx_last
;
717 dma_unmap_page(greth
->dev
,
718 greth_read_bd(&bdp
->addr
),
722 tx_last
= NEXT_TX(tx_last
);
726 if (skb
) { /* skb is set only if the above while loop was entered */
728 greth
->tx_last
= tx_last
;
730 if (netif_queue_stopped(dev
) &&
731 (greth_num_free_bds(tx_last
, greth
->tx_next
) >
733 netif_wake_queue(dev
);
737 static int greth_rx(struct net_device
*dev
, int limit
)
739 struct greth_private
*greth
;
740 struct greth_bd
*bdp
;
744 u32 status
, dma_addr
;
747 greth
= netdev_priv(dev
);
749 for (count
= 0; count
< limit
; ++count
) {
751 bdp
= greth
->rx_bd_base
+ greth
->rx_cur
;
752 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_RE
| GRETH_INT_RX
);
754 status
= greth_read_bd(&bdp
->stat
);
756 if (unlikely(status
& GRETH_BD_EN
)) {
760 dma_addr
= greth_read_bd(&bdp
->addr
);
763 /* Check status for errors. */
764 if (unlikely(status
& GRETH_RXBD_STATUS
)) {
765 if (status
& GRETH_RXBD_ERR_FT
) {
766 dev
->stats
.rx_length_errors
++;
769 if (status
& (GRETH_RXBD_ERR_AE
| GRETH_RXBD_ERR_OE
)) {
770 dev
->stats
.rx_frame_errors
++;
773 if (status
& GRETH_RXBD_ERR_CRC
) {
774 dev
->stats
.rx_crc_errors
++;
779 dev
->stats
.rx_errors
++;
783 pkt_len
= status
& GRETH_BD_LEN
;
785 skb
= netdev_alloc_skb(dev
, pkt_len
+ NET_IP_ALIGN
);
787 if (unlikely(skb
== NULL
)) {
790 dev_warn(&dev
->dev
, "low on memory - " "packet dropped\n");
792 dev
->stats
.rx_dropped
++;
795 skb_reserve(skb
, NET_IP_ALIGN
);
797 dma_sync_single_for_cpu(greth
->dev
,
802 if (netif_msg_pktdata(greth
))
803 greth_print_rx_packet(phys_to_virt(dma_addr
), pkt_len
);
805 skb_put_data(skb
, phys_to_virt(dma_addr
),
808 skb
->protocol
= eth_type_trans(skb
, dev
);
809 dev
->stats
.rx_bytes
+= pkt_len
;
810 dev
->stats
.rx_packets
++;
811 netif_receive_skb(skb
);
815 status
= GRETH_BD_EN
| GRETH_BD_IE
;
816 if (greth
->rx_cur
== GRETH_RXBD_NUM_MASK
) {
817 status
|= GRETH_BD_WR
;
821 greth_write_bd(&bdp
->stat
, status
);
823 dma_sync_single_for_device(greth
->dev
, dma_addr
, MAX_FRAME_SIZE
, DMA_FROM_DEVICE
);
825 spin_lock_irqsave(&greth
->devlock
, flags
); /* save from XMIT */
826 greth_enable_rx(greth
);
827 spin_unlock_irqrestore(&greth
->devlock
, flags
);
829 greth
->rx_cur
= NEXT_RX(greth
->rx_cur
);
835 static inline int hw_checksummed(u32 status
)
838 if (status
& GRETH_RXBD_IP_FRAG
)
841 if (status
& GRETH_RXBD_IP
&& status
& GRETH_RXBD_IP_CSERR
)
844 if (status
& GRETH_RXBD_UDP
&& status
& GRETH_RXBD_UDP_CSERR
)
847 if (status
& GRETH_RXBD_TCP
&& status
& GRETH_RXBD_TCP_CSERR
)
853 static int greth_rx_gbit(struct net_device
*dev
, int limit
)
855 struct greth_private
*greth
;
856 struct greth_bd
*bdp
;
857 struct sk_buff
*skb
, *newskb
;
860 u32 status
, dma_addr
;
863 greth
= netdev_priv(dev
);
865 for (count
= 0; count
< limit
; ++count
) {
867 bdp
= greth
->rx_bd_base
+ greth
->rx_cur
;
868 skb
= greth
->rx_skbuff
[greth
->rx_cur
];
869 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_RE
| GRETH_INT_RX
);
871 status
= greth_read_bd(&bdp
->stat
);
874 if (status
& GRETH_BD_EN
)
877 /* Check status for errors. */
878 if (unlikely(status
& GRETH_RXBD_STATUS
)) {
880 if (status
& GRETH_RXBD_ERR_FT
) {
881 dev
->stats
.rx_length_errors
++;
884 (GRETH_RXBD_ERR_AE
| GRETH_RXBD_ERR_OE
| GRETH_RXBD_ERR_LE
)) {
885 dev
->stats
.rx_frame_errors
++;
887 } else if (status
& GRETH_RXBD_ERR_CRC
) {
888 dev
->stats
.rx_crc_errors
++;
893 /* Allocate new skb to replace current, not needed if the
894 * current skb can be reused */
895 if (!bad
&& (newskb
=netdev_alloc_skb(dev
, MAX_FRAME_SIZE
+ NET_IP_ALIGN
))) {
896 skb_reserve(newskb
, NET_IP_ALIGN
);
898 dma_addr
= dma_map_single(greth
->dev
,
900 MAX_FRAME_SIZE
+ NET_IP_ALIGN
,
903 if (!dma_mapping_error(greth
->dev
, dma_addr
)) {
904 /* Process the incoming frame. */
905 pkt_len
= status
& GRETH_BD_LEN
;
907 dma_unmap_single(greth
->dev
,
908 greth_read_bd(&bdp
->addr
),
909 MAX_FRAME_SIZE
+ NET_IP_ALIGN
,
912 if (netif_msg_pktdata(greth
))
913 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp
->addr
)), pkt_len
);
915 skb_put(skb
, pkt_len
);
917 if (dev
->features
& NETIF_F_RXCSUM
&& hw_checksummed(status
))
918 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
920 skb_checksum_none_assert(skb
);
922 skb
->protocol
= eth_type_trans(skb
, dev
);
923 dev
->stats
.rx_packets
++;
924 dev
->stats
.rx_bytes
+= pkt_len
;
925 netif_receive_skb(skb
);
927 greth
->rx_skbuff
[greth
->rx_cur
] = newskb
;
928 greth_write_bd(&bdp
->addr
, dma_addr
);
931 dev_warn(greth
->dev
, "Could not create DMA mapping, dropping packet\n");
932 dev_kfree_skb(newskb
);
933 /* reusing current skb, so it is a drop */
934 dev
->stats
.rx_dropped
++;
937 /* Bad Frame transfer, the skb is reused */
938 dev
->stats
.rx_dropped
++;
940 /* Failed Allocating a new skb. This is rather stupid
941 * but the current "filled" skb is reused, as if
942 * transfer failure. One could argue that RX descriptor
943 * table handling should be divided into cleaning and
944 * filling as the TX part of the driver
947 dev_warn(greth
->dev
, "Could not allocate SKB, dropping packet\n");
948 /* reusing current skb, so it is a drop */
949 dev
->stats
.rx_dropped
++;
952 status
= GRETH_BD_EN
| GRETH_BD_IE
;
953 if (greth
->rx_cur
== GRETH_RXBD_NUM_MASK
) {
954 status
|= GRETH_BD_WR
;
958 greth_write_bd(&bdp
->stat
, status
);
959 spin_lock_irqsave(&greth
->devlock
, flags
);
960 greth_enable_rx(greth
);
961 spin_unlock_irqrestore(&greth
->devlock
, flags
);
962 greth
->rx_cur
= NEXT_RX(greth
->rx_cur
);
969 static int greth_poll(struct napi_struct
*napi
, int budget
)
971 struct greth_private
*greth
;
975 greth
= container_of(napi
, struct greth_private
, napi
);
978 if (greth
->gbit_mac
) {
979 greth_clean_tx_gbit(greth
->netdev
);
980 work_done
+= greth_rx_gbit(greth
->netdev
, budget
- work_done
);
982 if (netif_queue_stopped(greth
->netdev
))
983 greth_clean_tx(greth
->netdev
);
984 work_done
+= greth_rx(greth
->netdev
, budget
- work_done
);
987 if (work_done
< budget
) {
989 spin_lock_irqsave(&greth
->devlock
, flags
);
991 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
992 if ((greth
->gbit_mac
&& (greth
->tx_last
!= greth
->tx_next
)) ||
993 (!greth
->gbit_mac
&& netif_queue_stopped(greth
->netdev
))) {
994 GRETH_REGSAVE(greth
->regs
->control
,
995 ctrl
| GRETH_TXI
| GRETH_RXI
);
996 mask
= GRETH_INT_RX
| GRETH_INT_RE
|
997 GRETH_INT_TX
| GRETH_INT_TE
;
999 GRETH_REGSAVE(greth
->regs
->control
, ctrl
| GRETH_RXI
);
1000 mask
= GRETH_INT_RX
| GRETH_INT_RE
;
1003 if (GRETH_REGLOAD(greth
->regs
->status
) & mask
) {
1004 GRETH_REGSAVE(greth
->regs
->control
, ctrl
);
1005 spin_unlock_irqrestore(&greth
->devlock
, flags
);
1006 goto restart_txrx_poll
;
1008 napi_complete_done(napi
, work_done
);
1009 spin_unlock_irqrestore(&greth
->devlock
, flags
);
1016 static int greth_set_mac_add(struct net_device
*dev
, void *p
)
1018 struct sockaddr
*addr
= p
;
1019 struct greth_private
*greth
;
1020 struct greth_regs
*regs
;
1022 greth
= netdev_priv(dev
);
1025 if (!is_valid_ether_addr(addr
->sa_data
))
1026 return -EADDRNOTAVAIL
;
1028 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
1029 GRETH_REGSAVE(regs
->esa_msb
, dev
->dev_addr
[0] << 8 | dev
->dev_addr
[1]);
1030 GRETH_REGSAVE(regs
->esa_lsb
, dev
->dev_addr
[2] << 24 | dev
->dev_addr
[3] << 16 |
1031 dev
->dev_addr
[4] << 8 | dev
->dev_addr
[5]);
1036 static u32
greth_hash_get_index(__u8
*addr
)
1038 return (ether_crc(6, addr
)) & 0x3F;
1041 static void greth_set_hash_filter(struct net_device
*dev
)
1043 struct netdev_hw_addr
*ha
;
1044 struct greth_private
*greth
= netdev_priv(dev
);
1045 struct greth_regs
*regs
= greth
->regs
;
1049 mc_filter
[0] = mc_filter
[1] = 0;
1051 netdev_for_each_mc_addr(ha
, dev
) {
1052 bitnr
= greth_hash_get_index(ha
->addr
);
1053 mc_filter
[bitnr
>> 5] |= 1 << (bitnr
& 31);
1056 GRETH_REGSAVE(regs
->hash_msb
, mc_filter
[1]);
1057 GRETH_REGSAVE(regs
->hash_lsb
, mc_filter
[0]);
1060 static void greth_set_multicast_list(struct net_device
*dev
)
1063 struct greth_private
*greth
= netdev_priv(dev
);
1064 struct greth_regs
*regs
= greth
->regs
;
1066 cfg
= GRETH_REGLOAD(regs
->control
);
1067 if (dev
->flags
& IFF_PROMISC
)
1068 cfg
|= GRETH_CTRL_PR
;
1070 cfg
&= ~GRETH_CTRL_PR
;
1072 if (greth
->multicast
) {
1073 if (dev
->flags
& IFF_ALLMULTI
) {
1074 GRETH_REGSAVE(regs
->hash_msb
, -1);
1075 GRETH_REGSAVE(regs
->hash_lsb
, -1);
1076 cfg
|= GRETH_CTRL_MCEN
;
1077 GRETH_REGSAVE(regs
->control
, cfg
);
1081 if (netdev_mc_empty(dev
)) {
1082 cfg
&= ~GRETH_CTRL_MCEN
;
1083 GRETH_REGSAVE(regs
->control
, cfg
);
1087 /* Setup multicast filter */
1088 greth_set_hash_filter(dev
);
1089 cfg
|= GRETH_CTRL_MCEN
;
1091 GRETH_REGSAVE(regs
->control
, cfg
);
1094 static u32
greth_get_msglevel(struct net_device
*dev
)
1096 struct greth_private
*greth
= netdev_priv(dev
);
1097 return greth
->msg_enable
;
1100 static void greth_set_msglevel(struct net_device
*dev
, u32 value
)
1102 struct greth_private
*greth
= netdev_priv(dev
);
1103 greth
->msg_enable
= value
;
1106 static int greth_get_regs_len(struct net_device
*dev
)
1108 return sizeof(struct greth_regs
);
1111 static void greth_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
1113 struct greth_private
*greth
= netdev_priv(dev
);
1115 strlcpy(info
->driver
, dev_driver_string(greth
->dev
),
1116 sizeof(info
->driver
));
1117 strlcpy(info
->bus_info
, greth
->dev
->bus
->name
, sizeof(info
->bus_info
));
1120 static void greth_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *p
)
1123 struct greth_private
*greth
= netdev_priv(dev
);
1124 u32 __iomem
*greth_regs
= (u32 __iomem
*) greth
->regs
;
1127 for (i
= 0; i
< sizeof(struct greth_regs
) / sizeof(u32
); i
++)
1128 buff
[i
] = greth_read_bd(&greth_regs
[i
]);
1131 static const struct ethtool_ops greth_ethtool_ops
= {
1132 .get_msglevel
= greth_get_msglevel
,
1133 .set_msglevel
= greth_set_msglevel
,
1134 .get_drvinfo
= greth_get_drvinfo
,
1135 .get_regs_len
= greth_get_regs_len
,
1136 .get_regs
= greth_get_regs
,
1137 .get_link
= ethtool_op_get_link
,
1138 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
1139 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
1142 static struct net_device_ops greth_netdev_ops
= {
1143 .ndo_open
= greth_open
,
1144 .ndo_stop
= greth_close
,
1145 .ndo_start_xmit
= greth_start_xmit
,
1146 .ndo_set_mac_address
= greth_set_mac_add
,
1147 .ndo_validate_addr
= eth_validate_addr
,
1150 static inline int wait_for_mdio(struct greth_private
*greth
)
1152 unsigned long timeout
= jiffies
+ 4*HZ
/100;
1153 while (GRETH_REGLOAD(greth
->regs
->mdio
) & GRETH_MII_BUSY
) {
1154 if (time_after(jiffies
, timeout
))
1160 static int greth_mdio_read(struct mii_bus
*bus
, int phy
, int reg
)
1162 struct greth_private
*greth
= bus
->priv
;
1165 if (!wait_for_mdio(greth
))
1168 GRETH_REGSAVE(greth
->regs
->mdio
, ((phy
& 0x1F) << 11) | ((reg
& 0x1F) << 6) | 2);
1170 if (!wait_for_mdio(greth
))
1173 if (!(GRETH_REGLOAD(greth
->regs
->mdio
) & GRETH_MII_NVALID
)) {
1174 data
= (GRETH_REGLOAD(greth
->regs
->mdio
) >> 16) & 0xFFFF;
1182 static int greth_mdio_write(struct mii_bus
*bus
, int phy
, int reg
, u16 val
)
1184 struct greth_private
*greth
= bus
->priv
;
1186 if (!wait_for_mdio(greth
))
1189 GRETH_REGSAVE(greth
->regs
->mdio
,
1190 ((val
& 0xFFFF) << 16) | ((phy
& 0x1F) << 11) | ((reg
& 0x1F) << 6) | 1);
1192 if (!wait_for_mdio(greth
))
1198 static void greth_link_change(struct net_device
*dev
)
1200 struct greth_private
*greth
= netdev_priv(dev
);
1201 struct phy_device
*phydev
= dev
->phydev
;
1202 unsigned long flags
;
1203 int status_change
= 0;
1206 spin_lock_irqsave(&greth
->devlock
, flags
);
1210 if ((greth
->speed
!= phydev
->speed
) || (greth
->duplex
!= phydev
->duplex
)) {
1211 ctrl
= GRETH_REGLOAD(greth
->regs
->control
) &
1212 ~(GRETH_CTRL_FD
| GRETH_CTRL_SP
| GRETH_CTRL_GB
);
1215 ctrl
|= GRETH_CTRL_FD
;
1217 if (phydev
->speed
== SPEED_100
)
1218 ctrl
|= GRETH_CTRL_SP
;
1219 else if (phydev
->speed
== SPEED_1000
)
1220 ctrl
|= GRETH_CTRL_GB
;
1222 GRETH_REGSAVE(greth
->regs
->control
, ctrl
);
1223 greth
->speed
= phydev
->speed
;
1224 greth
->duplex
= phydev
->duplex
;
1229 if (phydev
->link
!= greth
->link
) {
1230 if (!phydev
->link
) {
1234 greth
->link
= phydev
->link
;
1239 spin_unlock_irqrestore(&greth
->devlock
, flags
);
1241 if (status_change
) {
1243 pr_debug("%s: link up (%d/%s)\n",
1244 dev
->name
, phydev
->speed
,
1245 DUPLEX_FULL
== phydev
->duplex
? "Full" : "Half");
1247 pr_debug("%s: link down\n", dev
->name
);
1251 static int greth_mdio_probe(struct net_device
*dev
)
1253 struct greth_private
*greth
= netdev_priv(dev
);
1254 struct phy_device
*phy
= NULL
;
1257 /* Find the first PHY */
1258 phy
= phy_find_first(greth
->mdio
);
1261 if (netif_msg_probe(greth
))
1262 dev_err(&dev
->dev
, "no PHY found\n");
1266 ret
= phy_connect_direct(dev
, phy
, &greth_link_change
,
1267 greth
->gbit_mac
? PHY_INTERFACE_MODE_GMII
: PHY_INTERFACE_MODE_MII
);
1269 if (netif_msg_ifup(greth
))
1270 dev_err(&dev
->dev
, "could not attach to PHY\n");
1274 if (greth
->gbit_mac
)
1275 phy_set_max_speed(phy
, SPEED_1000
);
1277 phy_set_max_speed(phy
, SPEED_100
);
1279 linkmode_copy(phy
->advertising
, phy
->supported
);
1288 static int greth_mdio_init(struct greth_private
*greth
)
1291 unsigned long timeout
;
1292 struct net_device
*ndev
= greth
->netdev
;
1294 greth
->mdio
= mdiobus_alloc();
1299 greth
->mdio
->name
= "greth-mdio";
1300 snprintf(greth
->mdio
->id
, MII_BUS_ID_SIZE
, "%s-%d", greth
->mdio
->name
, greth
->irq
);
1301 greth
->mdio
->read
= greth_mdio_read
;
1302 greth
->mdio
->write
= greth_mdio_write
;
1303 greth
->mdio
->priv
= greth
;
1305 ret
= mdiobus_register(greth
->mdio
);
1310 ret
= greth_mdio_probe(greth
->netdev
);
1312 if (netif_msg_probe(greth
))
1313 dev_err(&greth
->netdev
->dev
, "failed to probe MDIO bus\n");
1317 phy_start(ndev
->phydev
);
1319 /* If Ethernet debug link is used make autoneg happen right away */
1320 if (greth
->edcl
&& greth_edcl
== 1) {
1321 phy_start_aneg(ndev
->phydev
);
1322 timeout
= jiffies
+ 6*HZ
;
1323 while (!phy_aneg_done(ndev
->phydev
) &&
1324 time_before(jiffies
, timeout
)) {
1326 phy_read_status(ndev
->phydev
);
1327 greth_link_change(greth
->netdev
);
1333 mdiobus_unregister(greth
->mdio
);
1335 mdiobus_free(greth
->mdio
);
1339 /* Initialize the GRETH MAC */
1340 static int greth_of_probe(struct platform_device
*ofdev
)
1342 struct net_device
*dev
;
1343 struct greth_private
*greth
;
1344 struct greth_regs
*regs
;
1349 unsigned long timeout
;
1351 dev
= alloc_etherdev(sizeof(struct greth_private
));
1356 greth
= netdev_priv(dev
);
1357 greth
->netdev
= dev
;
1358 greth
->dev
= &ofdev
->dev
;
1360 if (greth_debug
> 0)
1361 greth
->msg_enable
= greth_debug
;
1363 greth
->msg_enable
= GRETH_DEF_MSG_ENABLE
;
1365 spin_lock_init(&greth
->devlock
);
1367 greth
->regs
= of_ioremap(&ofdev
->resource
[0], 0,
1368 resource_size(&ofdev
->resource
[0]),
1369 "grlib-greth regs");
1371 if (greth
->regs
== NULL
) {
1372 if (netif_msg_probe(greth
))
1373 dev_err(greth
->dev
, "ioremap failure.\n");
1379 greth
->irq
= ofdev
->archdata
.irqs
[0];
1381 dev_set_drvdata(greth
->dev
, dev
);
1382 SET_NETDEV_DEV(dev
, greth
->dev
);
1384 if (netif_msg_probe(greth
))
1385 dev_dbg(greth
->dev
, "resetting controller.\n");
1387 /* Reset the controller. */
1388 GRETH_REGSAVE(regs
->control
, GRETH_RESET
);
1390 /* Wait for MAC to reset itself */
1391 timeout
= jiffies
+ HZ
/100;
1392 while (GRETH_REGLOAD(regs
->control
) & GRETH_RESET
) {
1393 if (time_after(jiffies
, timeout
)) {
1395 if (netif_msg_probe(greth
))
1396 dev_err(greth
->dev
, "timeout when waiting for reset.\n");
1401 /* Get default PHY address */
1402 greth
->phyaddr
= (GRETH_REGLOAD(regs
->mdio
) >> 11) & 0x1F;
1404 /* Check if we have GBIT capable MAC */
1405 tmp
= GRETH_REGLOAD(regs
->control
);
1406 greth
->gbit_mac
= (tmp
>> 27) & 1;
1408 /* Check for multicast capability */
1409 greth
->multicast
= (tmp
>> 25) & 1;
1411 greth
->edcl
= (tmp
>> 31) & 1;
1413 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1414 * it doesn't interfere with the software */
1415 if (greth
->edcl
!= 0)
1416 GRETH_REGORIN(regs
->control
, GRETH_CTRL_DISDUPLEX
);
1418 /* Check if MAC can handle MDIO interrupts */
1419 greth
->mdio_int_en
= (tmp
>> 26) & 1;
1421 err
= greth_mdio_init(greth
);
1423 if (netif_msg_probe(greth
))
1424 dev_err(greth
->dev
, "failed to register MDIO bus\n");
1428 /* Allocate TX descriptor ring in coherent memory */
1429 greth
->tx_bd_base
= dma_alloc_coherent(greth
->dev
, 1024,
1430 &greth
->tx_bd_base_phys
,
1432 if (!greth
->tx_bd_base
) {
1437 /* Allocate RX descriptor ring in coherent memory */
1438 greth
->rx_bd_base
= dma_alloc_coherent(greth
->dev
, 1024,
1439 &greth
->rx_bd_base_phys
,
1441 if (!greth
->rx_bd_base
) {
1446 /* Get MAC address from: module param, OF property or ID prom */
1447 for (i
= 0; i
< 6; i
++) {
1448 if (macaddr
[i
] != 0)
1454 addr
= of_get_mac_address(ofdev
->dev
.of_node
);
1455 if (!IS_ERR(addr
)) {
1456 for (i
= 0; i
< 6; i
++)
1457 macaddr
[i
] = (unsigned int) addr
[i
];
1460 for (i
= 0; i
< 6; i
++)
1461 macaddr
[i
] = (unsigned int) idprom
->id_ethaddr
[i
];
1466 for (i
= 0; i
< 6; i
++)
1467 dev
->dev_addr
[i
] = macaddr
[i
];
1471 if (!is_valid_ether_addr(&dev
->dev_addr
[0])) {
1472 if (netif_msg_probe(greth
))
1473 dev_err(greth
->dev
, "no valid ethernet address, aborting.\n");
1478 GRETH_REGSAVE(regs
->esa_msb
, dev
->dev_addr
[0] << 8 | dev
->dev_addr
[1]);
1479 GRETH_REGSAVE(regs
->esa_lsb
, dev
->dev_addr
[2] << 24 | dev
->dev_addr
[3] << 16 |
1480 dev
->dev_addr
[4] << 8 | dev
->dev_addr
[5]);
1482 /* Clear all pending interrupts except PHY irq */
1483 GRETH_REGSAVE(regs
->status
, 0xFF);
1485 if (greth
->gbit_mac
) {
1486 dev
->hw_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
|
1488 dev
->features
= dev
->hw_features
| NETIF_F_HIGHDMA
;
1489 greth_netdev_ops
.ndo_start_xmit
= greth_start_xmit_gbit
;
1492 if (greth
->multicast
) {
1493 greth_netdev_ops
.ndo_set_rx_mode
= greth_set_multicast_list
;
1494 dev
->flags
|= IFF_MULTICAST
;
1496 dev
->flags
&= ~IFF_MULTICAST
;
1499 dev
->netdev_ops
= &greth_netdev_ops
;
1500 dev
->ethtool_ops
= &greth_ethtool_ops
;
1502 err
= register_netdev(dev
);
1504 if (netif_msg_probe(greth
))
1505 dev_err(greth
->dev
, "netdevice registration failed.\n");
1510 netif_napi_add(dev
, &greth
->napi
, greth_poll
, 64);
1515 dma_free_coherent(greth
->dev
, 1024, greth
->rx_bd_base
, greth
->rx_bd_base_phys
);
1517 dma_free_coherent(greth
->dev
, 1024, greth
->tx_bd_base
, greth
->tx_bd_base_phys
);
1519 mdiobus_unregister(greth
->mdio
);
1521 of_iounmap(&ofdev
->resource
[0], greth
->regs
, resource_size(&ofdev
->resource
[0]));
1527 static int greth_of_remove(struct platform_device
*of_dev
)
1529 struct net_device
*ndev
= platform_get_drvdata(of_dev
);
1530 struct greth_private
*greth
= netdev_priv(ndev
);
1532 /* Free descriptor areas */
1533 dma_free_coherent(&of_dev
->dev
, 1024, greth
->rx_bd_base
, greth
->rx_bd_base_phys
);
1535 dma_free_coherent(&of_dev
->dev
, 1024, greth
->tx_bd_base
, greth
->tx_bd_base_phys
);
1538 phy_stop(ndev
->phydev
);
1539 mdiobus_unregister(greth
->mdio
);
1541 unregister_netdev(ndev
);
1544 of_iounmap(&of_dev
->resource
[0], greth
->regs
, resource_size(&of_dev
->resource
[0]));
1549 static const struct of_device_id greth_of_match
[] = {
1551 .name
= "GAISLER_ETHMAC",
1559 MODULE_DEVICE_TABLE(of
, greth_of_match
);
1561 static struct platform_driver greth_of_driver
= {
1563 .name
= "grlib-greth",
1564 .of_match_table
= greth_of_match
,
1566 .probe
= greth_of_probe
,
1567 .remove
= greth_of_remove
,
1570 module_platform_driver(greth_of_driver
);
1572 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1573 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1574 MODULE_LICENSE("GPL");