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
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>
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>
43 #include <asm/idprom.h>
48 #define GRETH_DEF_MSG_ENABLE \
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,
99 static void greth_print_tx_packet(struct sk_buff
*skb
)
104 if (skb_shinfo(skb
)->nr_frags
== 0)
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
)
123 GRETH_REGORIN(greth
->regs
->control
, GRETH_TXEN
);
126 static inline void greth_enable_tx_and_irq(struct greth_private
*greth
)
128 wmb(); /* BDs must been written to memory before enabling TX */
129 GRETH_REGORIN(greth
->regs
->control
, GRETH_TXEN
| GRETH_TXI
);
132 static inline void greth_disable_tx(struct greth_private
*greth
)
134 GRETH_REGANDIN(greth
->regs
->control
, ~GRETH_TXEN
);
137 static inline void greth_enable_rx(struct greth_private
*greth
)
140 GRETH_REGORIN(greth
->regs
->control
, GRETH_RXEN
);
143 static inline void greth_disable_rx(struct greth_private
*greth
)
145 GRETH_REGANDIN(greth
->regs
->control
, ~GRETH_RXEN
);
148 static inline void greth_enable_irqs(struct greth_private
*greth
)
150 GRETH_REGORIN(greth
->regs
->control
, GRETH_RXI
| GRETH_TXI
);
153 static inline void greth_disable_irqs(struct greth_private
*greth
)
155 GRETH_REGANDIN(greth
->regs
->control
, ~(GRETH_RXI
|GRETH_TXI
));
158 static inline void greth_write_bd(u32
*bd
, u32 val
)
160 __raw_writel(cpu_to_be32(val
), bd
);
163 static inline u32
greth_read_bd(u32
*bd
)
165 return be32_to_cpu(__raw_readl(bd
));
168 static void greth_clean_rings(struct greth_private
*greth
)
171 struct greth_bd
*rx_bdp
= greth
->rx_bd_base
;
172 struct greth_bd
*tx_bdp
= greth
->tx_bd_base
;
174 if (greth
->gbit_mac
) {
176 /* Free and unmap RX buffers */
177 for (i
= 0; i
< GRETH_RXBD_NUM
; i
++, rx_bdp
++) {
178 if (greth
->rx_skbuff
[i
] != NULL
) {
179 dev_kfree_skb(greth
->rx_skbuff
[i
]);
180 dma_unmap_single(greth
->dev
,
181 greth_read_bd(&rx_bdp
->addr
),
182 MAX_FRAME_SIZE
+NET_IP_ALIGN
,
188 while (greth
->tx_free
< GRETH_TXBD_NUM
) {
190 struct sk_buff
*skb
= greth
->tx_skbuff
[greth
->tx_last
];
191 int nr_frags
= skb_shinfo(skb
)->nr_frags
;
192 tx_bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
193 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
195 dma_unmap_single(greth
->dev
,
196 greth_read_bd(&tx_bdp
->addr
),
200 for (i
= 0; i
< nr_frags
; i
++) {
201 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
202 tx_bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
204 dma_unmap_page(greth
->dev
,
205 greth_read_bd(&tx_bdp
->addr
),
209 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
211 greth
->tx_free
+= nr_frags
+1;
216 } else { /* 10/100 Mbps MAC */
218 for (i
= 0; i
< GRETH_RXBD_NUM
; i
++, rx_bdp
++) {
219 kfree(greth
->rx_bufs
[i
]);
220 dma_unmap_single(greth
->dev
,
221 greth_read_bd(&rx_bdp
->addr
),
225 for (i
= 0; i
< GRETH_TXBD_NUM
; i
++, tx_bdp
++) {
226 kfree(greth
->tx_bufs
[i
]);
227 dma_unmap_single(greth
->dev
,
228 greth_read_bd(&tx_bdp
->addr
),
235 static int greth_init_rings(struct greth_private
*greth
)
238 struct greth_bd
*rx_bd
, *tx_bd
;
242 rx_bd
= greth
->rx_bd_base
;
243 tx_bd
= greth
->tx_bd_base
;
245 /* Initialize descriptor rings and buffers */
246 if (greth
->gbit_mac
) {
248 for (i
= 0; i
< GRETH_RXBD_NUM
; i
++) {
249 skb
= netdev_alloc_skb(greth
->netdev
, MAX_FRAME_SIZE
+NET_IP_ALIGN
);
251 if (netif_msg_ifup(greth
))
252 dev_err(greth
->dev
, "Error allocating DMA ring.\n");
255 skb_reserve(skb
, NET_IP_ALIGN
);
256 dma_addr
= dma_map_single(greth
->dev
,
258 MAX_FRAME_SIZE
+NET_IP_ALIGN
,
261 if (dma_mapping_error(greth
->dev
, dma_addr
)) {
262 if (netif_msg_ifup(greth
))
263 dev_err(greth
->dev
, "Could not create initial DMA mapping\n");
266 greth
->rx_skbuff
[i
] = skb
;
267 greth_write_bd(&rx_bd
[i
].addr
, dma_addr
);
268 greth_write_bd(&rx_bd
[i
].stat
, GRETH_BD_EN
| GRETH_BD_IE
);
273 /* 10/100 MAC uses a fixed set of buffers and copy to/from SKBs */
274 for (i
= 0; i
< GRETH_RXBD_NUM
; i
++) {
276 greth
->rx_bufs
[i
] = kmalloc(MAX_FRAME_SIZE
, GFP_KERNEL
);
278 if (greth
->rx_bufs
[i
] == NULL
) {
279 if (netif_msg_ifup(greth
))
280 dev_err(greth
->dev
, "Error allocating DMA ring.\n");
284 dma_addr
= dma_map_single(greth
->dev
,
289 if (dma_mapping_error(greth
->dev
, dma_addr
)) {
290 if (netif_msg_ifup(greth
))
291 dev_err(greth
->dev
, "Could not create initial DMA mapping\n");
294 greth_write_bd(&rx_bd
[i
].addr
, dma_addr
);
295 greth_write_bd(&rx_bd
[i
].stat
, GRETH_BD_EN
| GRETH_BD_IE
);
297 for (i
= 0; i
< GRETH_TXBD_NUM
; i
++) {
299 greth
->tx_bufs
[i
] = kmalloc(MAX_FRAME_SIZE
, GFP_KERNEL
);
301 if (greth
->tx_bufs
[i
] == NULL
) {
302 if (netif_msg_ifup(greth
))
303 dev_err(greth
->dev
, "Error allocating DMA ring.\n");
307 dma_addr
= dma_map_single(greth
->dev
,
312 if (dma_mapping_error(greth
->dev
, dma_addr
)) {
313 if (netif_msg_ifup(greth
))
314 dev_err(greth
->dev
, "Could not create initial DMA mapping\n");
317 greth_write_bd(&tx_bd
[i
].addr
, dma_addr
);
318 greth_write_bd(&tx_bd
[i
].stat
, 0);
321 greth_write_bd(&rx_bd
[GRETH_RXBD_NUM
- 1].stat
,
322 greth_read_bd(&rx_bd
[GRETH_RXBD_NUM
- 1].stat
) | GRETH_BD_WR
);
324 /* Initialize pointers. */
328 greth
->tx_free
= GRETH_TXBD_NUM
;
330 /* Initialize descriptor base address */
331 GRETH_REGSAVE(greth
->regs
->tx_desc_p
, greth
->tx_bd_base_phys
);
332 GRETH_REGSAVE(greth
->regs
->rx_desc_p
, greth
->rx_bd_base_phys
);
337 greth_clean_rings(greth
);
341 static int greth_open(struct net_device
*dev
)
343 struct greth_private
*greth
= netdev_priv(dev
);
346 err
= greth_init_rings(greth
);
348 if (netif_msg_ifup(greth
))
349 dev_err(&dev
->dev
, "Could not allocate memory for DMA rings\n");
353 err
= request_irq(greth
->irq
, greth_interrupt
, 0, "eth", (void *) dev
);
355 if (netif_msg_ifup(greth
))
356 dev_err(&dev
->dev
, "Could not allocate interrupt %d\n", dev
->irq
);
357 greth_clean_rings(greth
);
361 if (netif_msg_ifup(greth
))
362 dev_dbg(&dev
->dev
, " starting queue\n");
363 netif_start_queue(dev
);
365 GRETH_REGSAVE(greth
->regs
->status
, 0xFF);
367 napi_enable(&greth
->napi
);
369 greth_enable_irqs(greth
);
370 greth_enable_tx(greth
);
371 greth_enable_rx(greth
);
376 static int greth_close(struct net_device
*dev
)
378 struct greth_private
*greth
= netdev_priv(dev
);
380 napi_disable(&greth
->napi
);
382 greth_disable_irqs(greth
);
383 greth_disable_tx(greth
);
384 greth_disable_rx(greth
);
386 netif_stop_queue(dev
);
388 free_irq(greth
->irq
, (void *) dev
);
390 greth_clean_rings(greth
);
396 greth_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
398 struct greth_private
*greth
= netdev_priv(dev
);
399 struct greth_bd
*bdp
;
400 int err
= NETDEV_TX_OK
;
401 u32 status
, dma_addr
, ctrl
;
405 greth_clean_tx(greth
->netdev
);
407 if (unlikely(greth
->tx_free
<= 0)) {
408 spin_lock_irqsave(&greth
->devlock
, flags
);/*save from poll/irq*/
409 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
410 /* Enable TX IRQ only if not already in poll() routine */
411 if (ctrl
& GRETH_RXI
)
412 GRETH_REGSAVE(greth
->regs
->control
, ctrl
| GRETH_TXI
);
413 netif_stop_queue(dev
);
414 spin_unlock_irqrestore(&greth
->devlock
, flags
);
415 return NETDEV_TX_BUSY
;
418 if (netif_msg_pktdata(greth
))
419 greth_print_tx_packet(skb
);
422 if (unlikely(skb
->len
> MAX_FRAME_SIZE
)) {
423 dev
->stats
.tx_errors
++;
427 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
428 dma_addr
= greth_read_bd(&bdp
->addr
);
430 memcpy((unsigned char *) phys_to_virt(dma_addr
), skb
->data
, skb
->len
);
432 dma_sync_single_for_device(greth
->dev
, dma_addr
, skb
->len
, DMA_TO_DEVICE
);
434 status
= GRETH_BD_EN
| GRETH_BD_IE
| (skb
->len
& GRETH_BD_LEN
);
435 greth
->tx_bufs_length
[greth
->tx_next
] = skb
->len
& GRETH_BD_LEN
;
437 /* Wrap around descriptor ring */
438 if (greth
->tx_next
== GRETH_TXBD_NUM_MASK
) {
439 status
|= GRETH_BD_WR
;
442 greth
->tx_next
= NEXT_TX(greth
->tx_next
);
445 /* Write descriptor control word and enable transmission */
446 greth_write_bd(&bdp
->stat
, status
);
447 spin_lock_irqsave(&greth
->devlock
, flags
); /*save from poll/irq*/
448 greth_enable_tx(greth
);
449 spin_unlock_irqrestore(&greth
->devlock
, flags
);
456 static inline u16
greth_num_free_bds(u16 tx_last
, u16 tx_next
)
458 if (tx_next
< tx_last
)
459 return (tx_last
- tx_next
) - 1;
461 return GRETH_TXBD_NUM
- (tx_next
- tx_last
) - 1;
465 greth_start_xmit_gbit(struct sk_buff
*skb
, struct net_device
*dev
)
467 struct greth_private
*greth
= netdev_priv(dev
);
468 struct greth_bd
*bdp
;
469 u32 status
, dma_addr
;
470 int curr_tx
, nr_frags
, i
, err
= NETDEV_TX_OK
;
474 nr_frags
= skb_shinfo(skb
)->nr_frags
;
475 tx_last
= greth
->tx_last
;
476 rmb(); /* tx_last is updated by the poll task */
478 if (greth_num_free_bds(tx_last
, greth
->tx_next
) < nr_frags
+ 1) {
479 netif_stop_queue(dev
);
480 err
= NETDEV_TX_BUSY
;
484 if (netif_msg_pktdata(greth
))
485 greth_print_tx_packet(skb
);
487 if (unlikely(skb
->len
> MAX_FRAME_SIZE
)) {
488 dev
->stats
.tx_errors
++;
492 /* Save skb pointer. */
493 greth
->tx_skbuff
[greth
->tx_next
] = skb
;
497 status
= GRETH_TXBD_MORE
;
499 status
= GRETH_BD_IE
;
501 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
502 status
|= GRETH_TXBD_CSALL
;
503 status
|= skb_headlen(skb
) & GRETH_BD_LEN
;
504 if (greth
->tx_next
== GRETH_TXBD_NUM_MASK
)
505 status
|= GRETH_BD_WR
;
508 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
509 greth_write_bd(&bdp
->stat
, status
);
510 dma_addr
= dma_map_single(greth
->dev
, skb
->data
, skb_headlen(skb
), DMA_TO_DEVICE
);
512 if (unlikely(dma_mapping_error(greth
->dev
, dma_addr
)))
515 greth_write_bd(&bdp
->addr
, dma_addr
);
517 curr_tx
= NEXT_TX(greth
->tx_next
);
520 for (i
= 0; i
< nr_frags
; i
++) {
521 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
522 greth
->tx_skbuff
[curr_tx
] = NULL
;
523 bdp
= greth
->tx_bd_base
+ curr_tx
;
525 status
= GRETH_BD_EN
;
526 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
527 status
|= GRETH_TXBD_CSALL
;
528 status
|= skb_frag_size(frag
) & GRETH_BD_LEN
;
530 /* Wrap around descriptor ring */
531 if (curr_tx
== GRETH_TXBD_NUM_MASK
)
532 status
|= GRETH_BD_WR
;
534 /* More fragments left */
535 if (i
< nr_frags
- 1)
536 status
|= GRETH_TXBD_MORE
;
538 status
|= GRETH_BD_IE
; /* enable IRQ on last fragment */
540 greth_write_bd(&bdp
->stat
, status
);
542 dma_addr
= skb_frag_dma_map(greth
->dev
, frag
, 0, skb_frag_size(frag
),
545 if (unlikely(dma_mapping_error(greth
->dev
, dma_addr
)))
548 greth_write_bd(&bdp
->addr
, dma_addr
);
550 curr_tx
= NEXT_TX(curr_tx
);
555 /* Enable the descriptor chain by enabling the first descriptor */
556 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
557 greth_write_bd(&bdp
->stat
,
558 greth_read_bd(&bdp
->stat
) | GRETH_BD_EN
);
560 spin_lock_irqsave(&greth
->devlock
, flags
); /*save from poll/irq*/
561 greth
->tx_next
= curr_tx
;
562 greth_enable_tx_and_irq(greth
);
563 spin_unlock_irqrestore(&greth
->devlock
, flags
);
568 /* Unmap SKB mappings that succeeded and disable descriptor */
569 for (i
= 0; greth
->tx_next
+ i
!= curr_tx
; i
++) {
570 bdp
= greth
->tx_bd_base
+ greth
->tx_next
+ i
;
571 dma_unmap_single(greth
->dev
,
572 greth_read_bd(&bdp
->addr
),
573 greth_read_bd(&bdp
->stat
) & GRETH_BD_LEN
,
575 greth_write_bd(&bdp
->stat
, 0);
579 dev_warn(greth
->dev
, "Could not create TX DMA mapping\n");
585 static irqreturn_t
greth_interrupt(int irq
, void *dev_id
)
587 struct net_device
*dev
= dev_id
;
588 struct greth_private
*greth
;
590 irqreturn_t retval
= IRQ_NONE
;
592 greth
= netdev_priv(dev
);
594 spin_lock(&greth
->devlock
);
596 /* Get the interrupt events that caused us to be here. */
597 status
= GRETH_REGLOAD(greth
->regs
->status
);
599 /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
600 * set regardless of whether IRQ is enabled or not. Especially
601 * important when shared IRQ.
603 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
605 /* Handle rx and tx interrupts through poll */
606 if (((status
& (GRETH_INT_RE
| GRETH_INT_RX
)) && (ctrl
& GRETH_RXI
)) ||
607 ((status
& (GRETH_INT_TE
| GRETH_INT_TX
)) && (ctrl
& GRETH_TXI
))) {
608 retval
= IRQ_HANDLED
;
610 /* Disable interrupts and schedule poll() */
611 greth_disable_irqs(greth
);
612 napi_schedule(&greth
->napi
);
616 spin_unlock(&greth
->devlock
);
621 static void greth_clean_tx(struct net_device
*dev
)
623 struct greth_private
*greth
;
624 struct greth_bd
*bdp
;
627 greth
= netdev_priv(dev
);
630 bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
631 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_TE
| GRETH_INT_TX
);
633 stat
= greth_read_bd(&bdp
->stat
);
635 if (unlikely(stat
& GRETH_BD_EN
))
638 if (greth
->tx_free
== GRETH_TXBD_NUM
)
641 /* Check status for errors */
642 if (unlikely(stat
& GRETH_TXBD_STATUS
)) {
643 dev
->stats
.tx_errors
++;
644 if (stat
& GRETH_TXBD_ERR_AL
)
645 dev
->stats
.tx_aborted_errors
++;
646 if (stat
& GRETH_TXBD_ERR_UE
)
647 dev
->stats
.tx_fifo_errors
++;
649 dev
->stats
.tx_packets
++;
650 dev
->stats
.tx_bytes
+= greth
->tx_bufs_length
[greth
->tx_last
];
651 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
655 if (greth
->tx_free
> 0) {
656 netif_wake_queue(dev
);
660 static inline void greth_update_tx_stats(struct net_device
*dev
, u32 stat
)
662 /* Check status for errors */
663 if (unlikely(stat
& GRETH_TXBD_STATUS
)) {
664 dev
->stats
.tx_errors
++;
665 if (stat
& GRETH_TXBD_ERR_AL
)
666 dev
->stats
.tx_aborted_errors
++;
667 if (stat
& GRETH_TXBD_ERR_UE
)
668 dev
->stats
.tx_fifo_errors
++;
669 if (stat
& GRETH_TXBD_ERR_LC
)
670 dev
->stats
.tx_aborted_errors
++;
672 dev
->stats
.tx_packets
++;
675 static void greth_clean_tx_gbit(struct net_device
*dev
)
677 struct greth_private
*greth
;
678 struct greth_bd
*bdp
, *bdp_last_frag
;
679 struct sk_buff
*skb
= NULL
;
684 greth
= netdev_priv(dev
);
685 tx_last
= greth
->tx_last
;
687 while (tx_last
!= greth
->tx_next
) {
689 skb
= greth
->tx_skbuff
[tx_last
];
691 nr_frags
= skb_shinfo(skb
)->nr_frags
;
693 /* We only clean fully completed SKBs */
694 bdp_last_frag
= greth
->tx_bd_base
+ SKIP_TX(tx_last
, nr_frags
);
696 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_TE
| GRETH_INT_TX
);
698 stat
= greth_read_bd(&bdp_last_frag
->stat
);
700 if (stat
& GRETH_BD_EN
)
703 greth
->tx_skbuff
[tx_last
] = NULL
;
705 greth_update_tx_stats(dev
, stat
);
706 dev
->stats
.tx_bytes
+= skb
->len
;
708 bdp
= greth
->tx_bd_base
+ tx_last
;
710 tx_last
= NEXT_TX(tx_last
);
712 dma_unmap_single(greth
->dev
,
713 greth_read_bd(&bdp
->addr
),
717 for (i
= 0; i
< nr_frags
; i
++) {
718 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
719 bdp
= greth
->tx_bd_base
+ tx_last
;
721 dma_unmap_page(greth
->dev
,
722 greth_read_bd(&bdp
->addr
),
726 tx_last
= NEXT_TX(tx_last
);
730 if (skb
) { /* skb is set only if the above while loop was entered */
732 greth
->tx_last
= tx_last
;
734 if (netif_queue_stopped(dev
) &&
735 (greth_num_free_bds(tx_last
, greth
->tx_next
) >
737 netif_wake_queue(dev
);
741 static int greth_rx(struct net_device
*dev
, int limit
)
743 struct greth_private
*greth
;
744 struct greth_bd
*bdp
;
748 u32 status
, dma_addr
;
751 greth
= netdev_priv(dev
);
753 for (count
= 0; count
< limit
; ++count
) {
755 bdp
= greth
->rx_bd_base
+ greth
->rx_cur
;
756 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_RE
| GRETH_INT_RX
);
758 status
= greth_read_bd(&bdp
->stat
);
760 if (unlikely(status
& GRETH_BD_EN
)) {
764 dma_addr
= greth_read_bd(&bdp
->addr
);
767 /* Check status for errors. */
768 if (unlikely(status
& GRETH_RXBD_STATUS
)) {
769 if (status
& GRETH_RXBD_ERR_FT
) {
770 dev
->stats
.rx_length_errors
++;
773 if (status
& (GRETH_RXBD_ERR_AE
| GRETH_RXBD_ERR_OE
)) {
774 dev
->stats
.rx_frame_errors
++;
777 if (status
& GRETH_RXBD_ERR_CRC
) {
778 dev
->stats
.rx_crc_errors
++;
783 dev
->stats
.rx_errors
++;
787 pkt_len
= status
& GRETH_BD_LEN
;
789 skb
= netdev_alloc_skb(dev
, pkt_len
+ NET_IP_ALIGN
);
791 if (unlikely(skb
== NULL
)) {
794 dev_warn(&dev
->dev
, "low on memory - " "packet dropped\n");
796 dev
->stats
.rx_dropped
++;
799 skb_reserve(skb
, NET_IP_ALIGN
);
801 dma_sync_single_for_cpu(greth
->dev
,
806 if (netif_msg_pktdata(greth
))
807 greth_print_rx_packet(phys_to_virt(dma_addr
), pkt_len
);
809 memcpy(skb_put(skb
, pkt_len
), phys_to_virt(dma_addr
), pkt_len
);
811 skb
->protocol
= eth_type_trans(skb
, dev
);
812 dev
->stats
.rx_bytes
+= pkt_len
;
813 dev
->stats
.rx_packets
++;
814 netif_receive_skb(skb
);
818 status
= GRETH_BD_EN
| GRETH_BD_IE
;
819 if (greth
->rx_cur
== GRETH_RXBD_NUM_MASK
) {
820 status
|= GRETH_BD_WR
;
824 greth_write_bd(&bdp
->stat
, status
);
826 dma_sync_single_for_device(greth
->dev
, dma_addr
, MAX_FRAME_SIZE
, DMA_FROM_DEVICE
);
828 spin_lock_irqsave(&greth
->devlock
, flags
); /* save from XMIT */
829 greth_enable_rx(greth
);
830 spin_unlock_irqrestore(&greth
->devlock
, flags
);
832 greth
->rx_cur
= NEXT_RX(greth
->rx_cur
);
838 static inline int hw_checksummed(u32 status
)
841 if (status
& GRETH_RXBD_IP_FRAG
)
844 if (status
& GRETH_RXBD_IP
&& status
& GRETH_RXBD_IP_CSERR
)
847 if (status
& GRETH_RXBD_UDP
&& status
& GRETH_RXBD_UDP_CSERR
)
850 if (status
& GRETH_RXBD_TCP
&& status
& GRETH_RXBD_TCP_CSERR
)
856 static int greth_rx_gbit(struct net_device
*dev
, int limit
)
858 struct greth_private
*greth
;
859 struct greth_bd
*bdp
;
860 struct sk_buff
*skb
, *newskb
;
863 u32 status
, dma_addr
;
866 greth
= netdev_priv(dev
);
868 for (count
= 0; count
< limit
; ++count
) {
870 bdp
= greth
->rx_bd_base
+ greth
->rx_cur
;
871 skb
= greth
->rx_skbuff
[greth
->rx_cur
];
872 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_RE
| GRETH_INT_RX
);
874 status
= greth_read_bd(&bdp
->stat
);
877 if (status
& GRETH_BD_EN
)
880 /* Check status for errors. */
881 if (unlikely(status
& GRETH_RXBD_STATUS
)) {
883 if (status
& GRETH_RXBD_ERR_FT
) {
884 dev
->stats
.rx_length_errors
++;
887 (GRETH_RXBD_ERR_AE
| GRETH_RXBD_ERR_OE
| GRETH_RXBD_ERR_LE
)) {
888 dev
->stats
.rx_frame_errors
++;
890 } else if (status
& GRETH_RXBD_ERR_CRC
) {
891 dev
->stats
.rx_crc_errors
++;
896 /* Allocate new skb to replace current, not needed if the
897 * current skb can be reused */
898 if (!bad
&& (newskb
=netdev_alloc_skb(dev
, MAX_FRAME_SIZE
+ NET_IP_ALIGN
))) {
899 skb_reserve(newskb
, NET_IP_ALIGN
);
901 dma_addr
= dma_map_single(greth
->dev
,
903 MAX_FRAME_SIZE
+ NET_IP_ALIGN
,
906 if (!dma_mapping_error(greth
->dev
, dma_addr
)) {
907 /* Process the incoming frame. */
908 pkt_len
= status
& GRETH_BD_LEN
;
910 dma_unmap_single(greth
->dev
,
911 greth_read_bd(&bdp
->addr
),
912 MAX_FRAME_SIZE
+ NET_IP_ALIGN
,
915 if (netif_msg_pktdata(greth
))
916 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp
->addr
)), pkt_len
);
918 skb_put(skb
, pkt_len
);
920 if (dev
->features
& NETIF_F_RXCSUM
&& hw_checksummed(status
))
921 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
923 skb_checksum_none_assert(skb
);
925 skb
->protocol
= eth_type_trans(skb
, dev
);
926 dev
->stats
.rx_packets
++;
927 dev
->stats
.rx_bytes
+= pkt_len
;
928 netif_receive_skb(skb
);
930 greth
->rx_skbuff
[greth
->rx_cur
] = newskb
;
931 greth_write_bd(&bdp
->addr
, dma_addr
);
934 dev_warn(greth
->dev
, "Could not create DMA mapping, dropping packet\n");
935 dev_kfree_skb(newskb
);
936 /* reusing current skb, so it is a drop */
937 dev
->stats
.rx_dropped
++;
940 /* Bad Frame transfer, the skb is reused */
941 dev
->stats
.rx_dropped
++;
943 /* Failed Allocating a new skb. This is rather stupid
944 * but the current "filled" skb is reused, as if
945 * transfer failure. One could argue that RX descriptor
946 * table handling should be divided into cleaning and
947 * filling as the TX part of the driver
950 dev_warn(greth
->dev
, "Could not allocate SKB, dropping packet\n");
951 /* reusing current skb, so it is a drop */
952 dev
->stats
.rx_dropped
++;
955 status
= GRETH_BD_EN
| GRETH_BD_IE
;
956 if (greth
->rx_cur
== GRETH_RXBD_NUM_MASK
) {
957 status
|= GRETH_BD_WR
;
961 greth_write_bd(&bdp
->stat
, status
);
962 spin_lock_irqsave(&greth
->devlock
, flags
);
963 greth_enable_rx(greth
);
964 spin_unlock_irqrestore(&greth
->devlock
, flags
);
965 greth
->rx_cur
= NEXT_RX(greth
->rx_cur
);
972 static int greth_poll(struct napi_struct
*napi
, int budget
)
974 struct greth_private
*greth
;
978 greth
= container_of(napi
, struct greth_private
, napi
);
981 if (greth
->gbit_mac
) {
982 greth_clean_tx_gbit(greth
->netdev
);
983 work_done
+= greth_rx_gbit(greth
->netdev
, budget
- work_done
);
985 if (netif_queue_stopped(greth
->netdev
))
986 greth_clean_tx(greth
->netdev
);
987 work_done
+= greth_rx(greth
->netdev
, budget
- work_done
);
990 if (work_done
< budget
) {
992 spin_lock_irqsave(&greth
->devlock
, flags
);
994 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
995 if ((greth
->gbit_mac
&& (greth
->tx_last
!= greth
->tx_next
)) ||
996 (!greth
->gbit_mac
&& netif_queue_stopped(greth
->netdev
))) {
997 GRETH_REGSAVE(greth
->regs
->control
,
998 ctrl
| GRETH_TXI
| GRETH_RXI
);
999 mask
= GRETH_INT_RX
| GRETH_INT_RE
|
1000 GRETH_INT_TX
| GRETH_INT_TE
;
1002 GRETH_REGSAVE(greth
->regs
->control
, ctrl
| GRETH_RXI
);
1003 mask
= GRETH_INT_RX
| GRETH_INT_RE
;
1006 if (GRETH_REGLOAD(greth
->regs
->status
) & mask
) {
1007 GRETH_REGSAVE(greth
->regs
->control
, ctrl
);
1008 spin_unlock_irqrestore(&greth
->devlock
, flags
);
1009 goto restart_txrx_poll
;
1011 napi_complete_done(napi
, work_done
);
1012 spin_unlock_irqrestore(&greth
->devlock
, flags
);
1019 static int greth_set_mac_add(struct net_device
*dev
, void *p
)
1021 struct sockaddr
*addr
= p
;
1022 struct greth_private
*greth
;
1023 struct greth_regs
*regs
;
1025 greth
= netdev_priv(dev
);
1028 if (!is_valid_ether_addr(addr
->sa_data
))
1029 return -EADDRNOTAVAIL
;
1031 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
1032 GRETH_REGSAVE(regs
->esa_msb
, dev
->dev_addr
[0] << 8 | dev
->dev_addr
[1]);
1033 GRETH_REGSAVE(regs
->esa_lsb
, dev
->dev_addr
[2] << 24 | dev
->dev_addr
[3] << 16 |
1034 dev
->dev_addr
[4] << 8 | dev
->dev_addr
[5]);
1039 static u32
greth_hash_get_index(__u8
*addr
)
1041 return (ether_crc(6, addr
)) & 0x3F;
1044 static void greth_set_hash_filter(struct net_device
*dev
)
1046 struct netdev_hw_addr
*ha
;
1047 struct greth_private
*greth
= netdev_priv(dev
);
1048 struct greth_regs
*regs
= greth
->regs
;
1052 mc_filter
[0] = mc_filter
[1] = 0;
1054 netdev_for_each_mc_addr(ha
, dev
) {
1055 bitnr
= greth_hash_get_index(ha
->addr
);
1056 mc_filter
[bitnr
>> 5] |= 1 << (bitnr
& 31);
1059 GRETH_REGSAVE(regs
->hash_msb
, mc_filter
[1]);
1060 GRETH_REGSAVE(regs
->hash_lsb
, mc_filter
[0]);
1063 static void greth_set_multicast_list(struct net_device
*dev
)
1066 struct greth_private
*greth
= netdev_priv(dev
);
1067 struct greth_regs
*regs
= greth
->regs
;
1069 cfg
= GRETH_REGLOAD(regs
->control
);
1070 if (dev
->flags
& IFF_PROMISC
)
1071 cfg
|= GRETH_CTRL_PR
;
1073 cfg
&= ~GRETH_CTRL_PR
;
1075 if (greth
->multicast
) {
1076 if (dev
->flags
& IFF_ALLMULTI
) {
1077 GRETH_REGSAVE(regs
->hash_msb
, -1);
1078 GRETH_REGSAVE(regs
->hash_lsb
, -1);
1079 cfg
|= GRETH_CTRL_MCEN
;
1080 GRETH_REGSAVE(regs
->control
, cfg
);
1084 if (netdev_mc_empty(dev
)) {
1085 cfg
&= ~GRETH_CTRL_MCEN
;
1086 GRETH_REGSAVE(regs
->control
, cfg
);
1090 /* Setup multicast filter */
1091 greth_set_hash_filter(dev
);
1092 cfg
|= GRETH_CTRL_MCEN
;
1094 GRETH_REGSAVE(regs
->control
, cfg
);
1097 static u32
greth_get_msglevel(struct net_device
*dev
)
1099 struct greth_private
*greth
= netdev_priv(dev
);
1100 return greth
->msg_enable
;
1103 static void greth_set_msglevel(struct net_device
*dev
, u32 value
)
1105 struct greth_private
*greth
= netdev_priv(dev
);
1106 greth
->msg_enable
= value
;
1109 static int greth_get_regs_len(struct net_device
*dev
)
1111 return sizeof(struct greth_regs
);
1114 static void greth_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
1116 struct greth_private
*greth
= netdev_priv(dev
);
1118 strlcpy(info
->driver
, dev_driver_string(greth
->dev
),
1119 sizeof(info
->driver
));
1120 strlcpy(info
->version
, "revision: 1.0", sizeof(info
->version
));
1121 strlcpy(info
->bus_info
, greth
->dev
->bus
->name
, sizeof(info
->bus_info
));
1122 strlcpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
1125 static void greth_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *p
)
1128 struct greth_private
*greth
= netdev_priv(dev
);
1129 u32 __iomem
*greth_regs
= (u32 __iomem
*) greth
->regs
;
1132 for (i
= 0; i
< sizeof(struct greth_regs
) / sizeof(u32
); i
++)
1133 buff
[i
] = greth_read_bd(&greth_regs
[i
]);
1136 static const struct ethtool_ops greth_ethtool_ops
= {
1137 .get_msglevel
= greth_get_msglevel
,
1138 .set_msglevel
= greth_set_msglevel
,
1139 .get_drvinfo
= greth_get_drvinfo
,
1140 .get_regs_len
= greth_get_regs_len
,
1141 .get_regs
= greth_get_regs
,
1142 .get_link
= ethtool_op_get_link
,
1143 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
1144 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
1147 static struct net_device_ops greth_netdev_ops
= {
1148 .ndo_open
= greth_open
,
1149 .ndo_stop
= greth_close
,
1150 .ndo_start_xmit
= greth_start_xmit
,
1151 .ndo_set_mac_address
= greth_set_mac_add
,
1152 .ndo_validate_addr
= eth_validate_addr
,
1155 static inline int wait_for_mdio(struct greth_private
*greth
)
1157 unsigned long timeout
= jiffies
+ 4*HZ
/100;
1158 while (GRETH_REGLOAD(greth
->regs
->mdio
) & GRETH_MII_BUSY
) {
1159 if (time_after(jiffies
, timeout
))
1165 static int greth_mdio_read(struct mii_bus
*bus
, int phy
, int reg
)
1167 struct greth_private
*greth
= bus
->priv
;
1170 if (!wait_for_mdio(greth
))
1173 GRETH_REGSAVE(greth
->regs
->mdio
, ((phy
& 0x1F) << 11) | ((reg
& 0x1F) << 6) | 2);
1175 if (!wait_for_mdio(greth
))
1178 if (!(GRETH_REGLOAD(greth
->regs
->mdio
) & GRETH_MII_NVALID
)) {
1179 data
= (GRETH_REGLOAD(greth
->regs
->mdio
) >> 16) & 0xFFFF;
1187 static int greth_mdio_write(struct mii_bus
*bus
, int phy
, int reg
, u16 val
)
1189 struct greth_private
*greth
= bus
->priv
;
1191 if (!wait_for_mdio(greth
))
1194 GRETH_REGSAVE(greth
->regs
->mdio
,
1195 ((val
& 0xFFFF) << 16) | ((phy
& 0x1F) << 11) | ((reg
& 0x1F) << 6) | 1);
1197 if (!wait_for_mdio(greth
))
1203 static void greth_link_change(struct net_device
*dev
)
1205 struct greth_private
*greth
= netdev_priv(dev
);
1206 struct phy_device
*phydev
= dev
->phydev
;
1207 unsigned long flags
;
1208 int status_change
= 0;
1211 spin_lock_irqsave(&greth
->devlock
, flags
);
1215 if ((greth
->speed
!= phydev
->speed
) || (greth
->duplex
!= phydev
->duplex
)) {
1216 ctrl
= GRETH_REGLOAD(greth
->regs
->control
) &
1217 ~(GRETH_CTRL_FD
| GRETH_CTRL_SP
| GRETH_CTRL_GB
);
1220 ctrl
|= GRETH_CTRL_FD
;
1222 if (phydev
->speed
== SPEED_100
)
1223 ctrl
|= GRETH_CTRL_SP
;
1224 else if (phydev
->speed
== SPEED_1000
)
1225 ctrl
|= GRETH_CTRL_GB
;
1227 GRETH_REGSAVE(greth
->regs
->control
, ctrl
);
1228 greth
->speed
= phydev
->speed
;
1229 greth
->duplex
= phydev
->duplex
;
1234 if (phydev
->link
!= greth
->link
) {
1235 if (!phydev
->link
) {
1239 greth
->link
= phydev
->link
;
1244 spin_unlock_irqrestore(&greth
->devlock
, flags
);
1246 if (status_change
) {
1248 pr_debug("%s: link up (%d/%s)\n",
1249 dev
->name
, phydev
->speed
,
1250 DUPLEX_FULL
== phydev
->duplex
? "Full" : "Half");
1252 pr_debug("%s: link down\n", dev
->name
);
1256 static int greth_mdio_probe(struct net_device
*dev
)
1258 struct greth_private
*greth
= netdev_priv(dev
);
1259 struct phy_device
*phy
= NULL
;
1262 /* Find the first PHY */
1263 phy
= phy_find_first(greth
->mdio
);
1266 if (netif_msg_probe(greth
))
1267 dev_err(&dev
->dev
, "no PHY found\n");
1271 ret
= phy_connect_direct(dev
, phy
, &greth_link_change
,
1272 greth
->gbit_mac
? PHY_INTERFACE_MODE_GMII
: PHY_INTERFACE_MODE_MII
);
1274 if (netif_msg_ifup(greth
))
1275 dev_err(&dev
->dev
, "could not attach to PHY\n");
1279 if (greth
->gbit_mac
)
1280 phy
->supported
&= PHY_GBIT_FEATURES
;
1282 phy
->supported
&= PHY_BASIC_FEATURES
;
1284 phy
->advertising
= phy
->supported
;
1293 static int greth_mdio_init(struct greth_private
*greth
)
1296 unsigned long timeout
;
1297 struct net_device
*ndev
= greth
->netdev
;
1299 greth
->mdio
= mdiobus_alloc();
1304 greth
->mdio
->name
= "greth-mdio";
1305 snprintf(greth
->mdio
->id
, MII_BUS_ID_SIZE
, "%s-%d", greth
->mdio
->name
, greth
->irq
);
1306 greth
->mdio
->read
= greth_mdio_read
;
1307 greth
->mdio
->write
= greth_mdio_write
;
1308 greth
->mdio
->priv
= greth
;
1310 ret
= mdiobus_register(greth
->mdio
);
1315 ret
= greth_mdio_probe(greth
->netdev
);
1317 if (netif_msg_probe(greth
))
1318 dev_err(&greth
->netdev
->dev
, "failed to probe MDIO bus\n");
1322 phy_start(ndev
->phydev
);
1324 /* If Ethernet debug link is used make autoneg happen right away */
1325 if (greth
->edcl
&& greth_edcl
== 1) {
1326 phy_start_aneg(ndev
->phydev
);
1327 timeout
= jiffies
+ 6*HZ
;
1328 while (!phy_aneg_done(ndev
->phydev
) &&
1329 time_before(jiffies
, timeout
)) {
1331 phy_read_status(ndev
->phydev
);
1332 greth_link_change(greth
->netdev
);
1338 mdiobus_unregister(greth
->mdio
);
1340 mdiobus_free(greth
->mdio
);
1344 /* Initialize the GRETH MAC */
1345 static int greth_of_probe(struct platform_device
*ofdev
)
1347 struct net_device
*dev
;
1348 struct greth_private
*greth
;
1349 struct greth_regs
*regs
;
1354 unsigned long timeout
;
1356 dev
= alloc_etherdev(sizeof(struct greth_private
));
1361 greth
= netdev_priv(dev
);
1362 greth
->netdev
= dev
;
1363 greth
->dev
= &ofdev
->dev
;
1365 if (greth_debug
> 0)
1366 greth
->msg_enable
= greth_debug
;
1368 greth
->msg_enable
= GRETH_DEF_MSG_ENABLE
;
1370 spin_lock_init(&greth
->devlock
);
1372 greth
->regs
= of_ioremap(&ofdev
->resource
[0], 0,
1373 resource_size(&ofdev
->resource
[0]),
1374 "grlib-greth regs");
1376 if (greth
->regs
== NULL
) {
1377 if (netif_msg_probe(greth
))
1378 dev_err(greth
->dev
, "ioremap failure.\n");
1384 greth
->irq
= ofdev
->archdata
.irqs
[0];
1386 dev_set_drvdata(greth
->dev
, dev
);
1387 SET_NETDEV_DEV(dev
, greth
->dev
);
1389 if (netif_msg_probe(greth
))
1390 dev_dbg(greth
->dev
, "resetting controller.\n");
1392 /* Reset the controller. */
1393 GRETH_REGSAVE(regs
->control
, GRETH_RESET
);
1395 /* Wait for MAC to reset itself */
1396 timeout
= jiffies
+ HZ
/100;
1397 while (GRETH_REGLOAD(regs
->control
) & GRETH_RESET
) {
1398 if (time_after(jiffies
, timeout
)) {
1400 if (netif_msg_probe(greth
))
1401 dev_err(greth
->dev
, "timeout when waiting for reset.\n");
1406 /* Get default PHY address */
1407 greth
->phyaddr
= (GRETH_REGLOAD(regs
->mdio
) >> 11) & 0x1F;
1409 /* Check if we have GBIT capable MAC */
1410 tmp
= GRETH_REGLOAD(regs
->control
);
1411 greth
->gbit_mac
= (tmp
>> 27) & 1;
1413 /* Check for multicast capability */
1414 greth
->multicast
= (tmp
>> 25) & 1;
1416 greth
->edcl
= (tmp
>> 31) & 1;
1418 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1419 * it doesn't interfere with the software */
1420 if (greth
->edcl
!= 0)
1421 GRETH_REGORIN(regs
->control
, GRETH_CTRL_DISDUPLEX
);
1423 /* Check if MAC can handle MDIO interrupts */
1424 greth
->mdio_int_en
= (tmp
>> 26) & 1;
1426 err
= greth_mdio_init(greth
);
1428 if (netif_msg_probe(greth
))
1429 dev_err(greth
->dev
, "failed to register MDIO bus\n");
1433 /* Allocate TX descriptor ring in coherent memory */
1434 greth
->tx_bd_base
= dma_zalloc_coherent(greth
->dev
, 1024,
1435 &greth
->tx_bd_base_phys
,
1437 if (!greth
->tx_bd_base
) {
1442 /* Allocate RX descriptor ring in coherent memory */
1443 greth
->rx_bd_base
= dma_zalloc_coherent(greth
->dev
, 1024,
1444 &greth
->rx_bd_base_phys
,
1446 if (!greth
->rx_bd_base
) {
1451 /* Get MAC address from: module param, OF property or ID prom */
1452 for (i
= 0; i
< 6; i
++) {
1453 if (macaddr
[i
] != 0)
1457 const unsigned char *addr
;
1459 addr
= of_get_property(ofdev
->dev
.of_node
, "local-mac-address",
1461 if (addr
!= NULL
&& len
== 6) {
1462 for (i
= 0; i
< 6; i
++)
1463 macaddr
[i
] = (unsigned int) addr
[i
];
1466 for (i
= 0; i
< 6; i
++)
1467 macaddr
[i
] = (unsigned int) idprom
->id_ethaddr
[i
];
1472 for (i
= 0; i
< 6; i
++)
1473 dev
->dev_addr
[i
] = macaddr
[i
];
1477 if (!is_valid_ether_addr(&dev
->dev_addr
[0])) {
1478 if (netif_msg_probe(greth
))
1479 dev_err(greth
->dev
, "no valid ethernet address, aborting.\n");
1484 GRETH_REGSAVE(regs
->esa_msb
, dev
->dev_addr
[0] << 8 | dev
->dev_addr
[1]);
1485 GRETH_REGSAVE(regs
->esa_lsb
, dev
->dev_addr
[2] << 24 | dev
->dev_addr
[3] << 16 |
1486 dev
->dev_addr
[4] << 8 | dev
->dev_addr
[5]);
1488 /* Clear all pending interrupts except PHY irq */
1489 GRETH_REGSAVE(regs
->status
, 0xFF);
1491 if (greth
->gbit_mac
) {
1492 dev
->hw_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
|
1494 dev
->features
= dev
->hw_features
| NETIF_F_HIGHDMA
;
1495 greth_netdev_ops
.ndo_start_xmit
= greth_start_xmit_gbit
;
1498 if (greth
->multicast
) {
1499 greth_netdev_ops
.ndo_set_rx_mode
= greth_set_multicast_list
;
1500 dev
->flags
|= IFF_MULTICAST
;
1502 dev
->flags
&= ~IFF_MULTICAST
;
1505 dev
->netdev_ops
= &greth_netdev_ops
;
1506 dev
->ethtool_ops
= &greth_ethtool_ops
;
1508 err
= register_netdev(dev
);
1510 if (netif_msg_probe(greth
))
1511 dev_err(greth
->dev
, "netdevice registration failed.\n");
1516 netif_napi_add(dev
, &greth
->napi
, greth_poll
, 64);
1521 dma_free_coherent(greth
->dev
, 1024, greth
->rx_bd_base
, greth
->rx_bd_base_phys
);
1523 dma_free_coherent(greth
->dev
, 1024, greth
->tx_bd_base
, greth
->tx_bd_base_phys
);
1525 mdiobus_unregister(greth
->mdio
);
1527 of_iounmap(&ofdev
->resource
[0], greth
->regs
, resource_size(&ofdev
->resource
[0]));
1533 static int greth_of_remove(struct platform_device
*of_dev
)
1535 struct net_device
*ndev
= platform_get_drvdata(of_dev
);
1536 struct greth_private
*greth
= netdev_priv(ndev
);
1538 /* Free descriptor areas */
1539 dma_free_coherent(&of_dev
->dev
, 1024, greth
->rx_bd_base
, greth
->rx_bd_base_phys
);
1541 dma_free_coherent(&of_dev
->dev
, 1024, greth
->tx_bd_base
, greth
->tx_bd_base_phys
);
1544 phy_stop(ndev
->phydev
);
1545 mdiobus_unregister(greth
->mdio
);
1547 unregister_netdev(ndev
);
1550 of_iounmap(&of_dev
->resource
[0], greth
->regs
, resource_size(&of_dev
->resource
[0]));
1555 static const struct of_device_id greth_of_match
[] = {
1557 .name
= "GAISLER_ETHMAC",
1565 MODULE_DEVICE_TABLE(of
, greth_of_match
);
1567 static struct platform_driver greth_of_driver
= {
1569 .name
= "grlib-greth",
1570 .of_match_table
= greth_of_match
,
1572 .probe
= greth_of_probe
,
1573 .remove
= greth_of_remove
,
1576 module_platform_driver(greth_of_driver
);
1578 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1579 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1580 MODULE_LICENSE("GPL");