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_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
, ctrl
;
399 greth_clean_tx(greth
->netdev
);
401 if (unlikely(greth
->tx_free
<= 0)) {
402 spin_lock_irqsave(&greth
->devlock
, flags
);/*save from poll/irq*/
403 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
404 /* Enable TX IRQ only if not already in poll() routine */
405 if (ctrl
& GRETH_RXI
)
406 GRETH_REGSAVE(greth
->regs
->control
, ctrl
| GRETH_TXI
);
407 netif_stop_queue(dev
);
408 spin_unlock_irqrestore(&greth
->devlock
, flags
);
409 return NETDEV_TX_BUSY
;
412 if (netif_msg_pktdata(greth
))
413 greth_print_tx_packet(skb
);
416 if (unlikely(skb
->len
> MAX_FRAME_SIZE
)) {
417 dev
->stats
.tx_errors
++;
421 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
422 dma_addr
= greth_read_bd(&bdp
->addr
);
424 memcpy((unsigned char *) phys_to_virt(dma_addr
), skb
->data
, skb
->len
);
426 dma_sync_single_for_device(greth
->dev
, dma_addr
, skb
->len
, DMA_TO_DEVICE
);
428 status
= GRETH_BD_EN
| GRETH_BD_IE
| (skb
->len
& GRETH_BD_LEN
);
429 greth
->tx_bufs_length
[greth
->tx_next
] = skb
->len
& GRETH_BD_LEN
;
431 /* Wrap around descriptor ring */
432 if (greth
->tx_next
== GRETH_TXBD_NUM_MASK
) {
433 status
|= GRETH_BD_WR
;
436 greth
->tx_next
= NEXT_TX(greth
->tx_next
);
439 /* Write descriptor control word and enable transmission */
440 greth_write_bd(&bdp
->stat
, status
);
441 spin_lock_irqsave(&greth
->devlock
, flags
); /*save from poll/irq*/
442 greth_enable_tx(greth
);
443 spin_unlock_irqrestore(&greth
->devlock
, flags
);
452 greth_start_xmit_gbit(struct sk_buff
*skb
, struct net_device
*dev
)
454 struct greth_private
*greth
= netdev_priv(dev
);
455 struct greth_bd
*bdp
;
456 u32 status
= 0, dma_addr
, ctrl
;
457 int curr_tx
, nr_frags
, i
, err
= NETDEV_TX_OK
;
460 nr_frags
= skb_shinfo(skb
)->nr_frags
;
463 greth_clean_tx_gbit(dev
);
465 if (greth
->tx_free
< nr_frags
+ 1) {
466 spin_lock_irqsave(&greth
->devlock
, flags
);/*save from poll/irq*/
467 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
468 /* Enable TX IRQ only if not already in poll() routine */
469 if (ctrl
& GRETH_RXI
)
470 GRETH_REGSAVE(greth
->regs
->control
, ctrl
| GRETH_TXI
);
471 netif_stop_queue(dev
);
472 spin_unlock_irqrestore(&greth
->devlock
, flags
);
473 err
= NETDEV_TX_BUSY
;
477 if (netif_msg_pktdata(greth
))
478 greth_print_tx_packet(skb
);
480 if (unlikely(skb
->len
> MAX_FRAME_SIZE
)) {
481 dev
->stats
.tx_errors
++;
485 /* Save skb pointer. */
486 greth
->tx_skbuff
[greth
->tx_next
] = skb
;
490 status
= GRETH_TXBD_MORE
;
492 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
493 status
|= GRETH_TXBD_CSALL
;
494 status
|= skb_headlen(skb
) & GRETH_BD_LEN
;
495 if (greth
->tx_next
== GRETH_TXBD_NUM_MASK
)
496 status
|= GRETH_BD_WR
;
499 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
500 greth_write_bd(&bdp
->stat
, status
);
501 dma_addr
= dma_map_single(greth
->dev
, skb
->data
, skb_headlen(skb
), DMA_TO_DEVICE
);
503 if (unlikely(dma_mapping_error(greth
->dev
, dma_addr
)))
506 greth_write_bd(&bdp
->addr
, dma_addr
);
508 curr_tx
= NEXT_TX(greth
->tx_next
);
511 for (i
= 0; i
< nr_frags
; i
++) {
512 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
513 greth
->tx_skbuff
[curr_tx
] = NULL
;
514 bdp
= greth
->tx_bd_base
+ curr_tx
;
516 status
= GRETH_BD_EN
;
517 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
518 status
|= GRETH_TXBD_CSALL
;
519 status
|= skb_frag_size(frag
) & GRETH_BD_LEN
;
521 /* Wrap around descriptor ring */
522 if (curr_tx
== GRETH_TXBD_NUM_MASK
)
523 status
|= GRETH_BD_WR
;
525 /* More fragments left */
526 if (i
< nr_frags
- 1)
527 status
|= GRETH_TXBD_MORE
;
529 status
|= GRETH_BD_IE
; /* enable IRQ on last fragment */
531 greth_write_bd(&bdp
->stat
, status
);
533 dma_addr
= skb_frag_dma_map(greth
->dev
, frag
, 0, skb_frag_size(frag
),
536 if (unlikely(dma_mapping_error(greth
->dev
, dma_addr
)))
539 greth_write_bd(&bdp
->addr
, dma_addr
);
541 curr_tx
= NEXT_TX(curr_tx
);
546 /* Enable the descriptor chain by enabling the first descriptor */
547 bdp
= greth
->tx_bd_base
+ greth
->tx_next
;
548 greth_write_bd(&bdp
->stat
, greth_read_bd(&bdp
->stat
) | GRETH_BD_EN
);
549 greth
->tx_next
= curr_tx
;
550 greth
->tx_free
-= nr_frags
+ 1;
554 spin_lock_irqsave(&greth
->devlock
, flags
); /*save from poll/irq*/
555 greth_enable_tx(greth
);
556 spin_unlock_irqrestore(&greth
->devlock
, flags
);
561 /* Unmap SKB mappings that succeeded and disable descriptor */
562 for (i
= 0; greth
->tx_next
+ i
!= curr_tx
; i
++) {
563 bdp
= greth
->tx_bd_base
+ greth
->tx_next
+ i
;
564 dma_unmap_single(greth
->dev
,
565 greth_read_bd(&bdp
->addr
),
566 greth_read_bd(&bdp
->stat
) & GRETH_BD_LEN
,
568 greth_write_bd(&bdp
->stat
, 0);
572 dev_warn(greth
->dev
, "Could not create TX DMA mapping\n");
578 static irqreturn_t
greth_interrupt(int irq
, void *dev_id
)
580 struct net_device
*dev
= dev_id
;
581 struct greth_private
*greth
;
583 irqreturn_t retval
= IRQ_NONE
;
585 greth
= netdev_priv(dev
);
587 spin_lock(&greth
->devlock
);
589 /* Get the interrupt events that caused us to be here. */
590 status
= GRETH_REGLOAD(greth
->regs
->status
);
592 /* Must see if interrupts are enabled also, INT_TX|INT_RX flags may be
593 * set regardless of whether IRQ is enabled or not. Especially
594 * important when shared IRQ.
596 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
598 /* Handle rx and tx interrupts through poll */
599 if (((status
& (GRETH_INT_RE
| GRETH_INT_RX
)) && (ctrl
& GRETH_RXI
)) ||
600 ((status
& (GRETH_INT_TE
| GRETH_INT_TX
)) && (ctrl
& GRETH_TXI
))) {
601 retval
= IRQ_HANDLED
;
603 /* Disable interrupts and schedule poll() */
604 greth_disable_irqs(greth
);
605 napi_schedule(&greth
->napi
);
609 spin_unlock(&greth
->devlock
);
614 static void greth_clean_tx(struct net_device
*dev
)
616 struct greth_private
*greth
;
617 struct greth_bd
*bdp
;
620 greth
= netdev_priv(dev
);
623 bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
624 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_TE
| GRETH_INT_TX
);
626 stat
= greth_read_bd(&bdp
->stat
);
628 if (unlikely(stat
& GRETH_BD_EN
))
631 if (greth
->tx_free
== GRETH_TXBD_NUM
)
634 /* Check status for errors */
635 if (unlikely(stat
& GRETH_TXBD_STATUS
)) {
636 dev
->stats
.tx_errors
++;
637 if (stat
& GRETH_TXBD_ERR_AL
)
638 dev
->stats
.tx_aborted_errors
++;
639 if (stat
& GRETH_TXBD_ERR_UE
)
640 dev
->stats
.tx_fifo_errors
++;
642 dev
->stats
.tx_packets
++;
643 dev
->stats
.tx_bytes
+= greth
->tx_bufs_length
[greth
->tx_last
];
644 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
648 if (greth
->tx_free
> 0) {
649 netif_wake_queue(dev
);
654 static inline void greth_update_tx_stats(struct net_device
*dev
, u32 stat
)
656 /* Check status for errors */
657 if (unlikely(stat
& GRETH_TXBD_STATUS
)) {
658 dev
->stats
.tx_errors
++;
659 if (stat
& GRETH_TXBD_ERR_AL
)
660 dev
->stats
.tx_aborted_errors
++;
661 if (stat
& GRETH_TXBD_ERR_UE
)
662 dev
->stats
.tx_fifo_errors
++;
663 if (stat
& GRETH_TXBD_ERR_LC
)
664 dev
->stats
.tx_aborted_errors
++;
666 dev
->stats
.tx_packets
++;
669 static void greth_clean_tx_gbit(struct net_device
*dev
)
671 struct greth_private
*greth
;
672 struct greth_bd
*bdp
, *bdp_last_frag
;
677 greth
= netdev_priv(dev
);
679 while (greth
->tx_free
< GRETH_TXBD_NUM
) {
681 skb
= greth
->tx_skbuff
[greth
->tx_last
];
683 nr_frags
= skb_shinfo(skb
)->nr_frags
;
685 /* We only clean fully completed SKBs */
686 bdp_last_frag
= greth
->tx_bd_base
+ SKIP_TX(greth
->tx_last
, nr_frags
);
688 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_TE
| GRETH_INT_TX
);
690 stat
= greth_read_bd(&bdp_last_frag
->stat
);
692 if (stat
& GRETH_BD_EN
)
695 greth
->tx_skbuff
[greth
->tx_last
] = NULL
;
697 greth_update_tx_stats(dev
, stat
);
698 dev
->stats
.tx_bytes
+= skb
->len
;
700 bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
702 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
704 dma_unmap_single(greth
->dev
,
705 greth_read_bd(&bdp
->addr
),
709 for (i
= 0; i
< nr_frags
; i
++) {
710 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
711 bdp
= greth
->tx_bd_base
+ greth
->tx_last
;
713 dma_unmap_page(greth
->dev
,
714 greth_read_bd(&bdp
->addr
),
718 greth
->tx_last
= NEXT_TX(greth
->tx_last
);
720 greth
->tx_free
+= nr_frags
+1;
724 if (netif_queue_stopped(dev
) && (greth
->tx_free
> (MAX_SKB_FRAGS
+1)))
725 netif_wake_queue(dev
);
728 static int greth_rx(struct net_device
*dev
, int limit
)
730 struct greth_private
*greth
;
731 struct greth_bd
*bdp
;
735 u32 status
, dma_addr
;
738 greth
= netdev_priv(dev
);
740 for (count
= 0; count
< limit
; ++count
) {
742 bdp
= greth
->rx_bd_base
+ greth
->rx_cur
;
743 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_RE
| GRETH_INT_RX
);
745 status
= greth_read_bd(&bdp
->stat
);
747 if (unlikely(status
& GRETH_BD_EN
)) {
751 dma_addr
= greth_read_bd(&bdp
->addr
);
754 /* Check status for errors. */
755 if (unlikely(status
& GRETH_RXBD_STATUS
)) {
756 if (status
& GRETH_RXBD_ERR_FT
) {
757 dev
->stats
.rx_length_errors
++;
760 if (status
& (GRETH_RXBD_ERR_AE
| GRETH_RXBD_ERR_OE
)) {
761 dev
->stats
.rx_frame_errors
++;
764 if (status
& GRETH_RXBD_ERR_CRC
) {
765 dev
->stats
.rx_crc_errors
++;
770 dev
->stats
.rx_errors
++;
774 pkt_len
= status
& GRETH_BD_LEN
;
776 skb
= netdev_alloc_skb(dev
, pkt_len
+ NET_IP_ALIGN
);
778 if (unlikely(skb
== NULL
)) {
781 dev_warn(&dev
->dev
, "low on memory - " "packet dropped\n");
783 dev
->stats
.rx_dropped
++;
786 skb_reserve(skb
, NET_IP_ALIGN
);
788 dma_sync_single_for_cpu(greth
->dev
,
793 if (netif_msg_pktdata(greth
))
794 greth_print_rx_packet(phys_to_virt(dma_addr
), pkt_len
);
796 memcpy(skb_put(skb
, pkt_len
), phys_to_virt(dma_addr
), pkt_len
);
798 skb
->protocol
= eth_type_trans(skb
, dev
);
799 dev
->stats
.rx_bytes
+= pkt_len
;
800 dev
->stats
.rx_packets
++;
801 netif_receive_skb(skb
);
805 status
= GRETH_BD_EN
| GRETH_BD_IE
;
806 if (greth
->rx_cur
== GRETH_RXBD_NUM_MASK
) {
807 status
|= GRETH_BD_WR
;
811 greth_write_bd(&bdp
->stat
, status
);
813 dma_sync_single_for_device(greth
->dev
, dma_addr
, MAX_FRAME_SIZE
, DMA_FROM_DEVICE
);
815 spin_lock_irqsave(&greth
->devlock
, flags
); /* save from XMIT */
816 greth_enable_rx(greth
);
817 spin_unlock_irqrestore(&greth
->devlock
, flags
);
819 greth
->rx_cur
= NEXT_RX(greth
->rx_cur
);
825 static inline int hw_checksummed(u32 status
)
828 if (status
& GRETH_RXBD_IP_FRAG
)
831 if (status
& GRETH_RXBD_IP
&& status
& GRETH_RXBD_IP_CSERR
)
834 if (status
& GRETH_RXBD_UDP
&& status
& GRETH_RXBD_UDP_CSERR
)
837 if (status
& GRETH_RXBD_TCP
&& status
& GRETH_RXBD_TCP_CSERR
)
843 static int greth_rx_gbit(struct net_device
*dev
, int limit
)
845 struct greth_private
*greth
;
846 struct greth_bd
*bdp
;
847 struct sk_buff
*skb
, *newskb
;
850 u32 status
, dma_addr
;
853 greth
= netdev_priv(dev
);
855 for (count
= 0; count
< limit
; ++count
) {
857 bdp
= greth
->rx_bd_base
+ greth
->rx_cur
;
858 skb
= greth
->rx_skbuff
[greth
->rx_cur
];
859 GRETH_REGSAVE(greth
->regs
->status
, GRETH_INT_RE
| GRETH_INT_RX
);
861 status
= greth_read_bd(&bdp
->stat
);
864 if (status
& GRETH_BD_EN
)
867 /* Check status for errors. */
868 if (unlikely(status
& GRETH_RXBD_STATUS
)) {
870 if (status
& GRETH_RXBD_ERR_FT
) {
871 dev
->stats
.rx_length_errors
++;
874 (GRETH_RXBD_ERR_AE
| GRETH_RXBD_ERR_OE
| GRETH_RXBD_ERR_LE
)) {
875 dev
->stats
.rx_frame_errors
++;
877 } else if (status
& GRETH_RXBD_ERR_CRC
) {
878 dev
->stats
.rx_crc_errors
++;
883 /* Allocate new skb to replace current, not needed if the
884 * current skb can be reused */
885 if (!bad
&& (newskb
=netdev_alloc_skb(dev
, MAX_FRAME_SIZE
+ NET_IP_ALIGN
))) {
886 skb_reserve(newskb
, NET_IP_ALIGN
);
888 dma_addr
= dma_map_single(greth
->dev
,
890 MAX_FRAME_SIZE
+ NET_IP_ALIGN
,
893 if (!dma_mapping_error(greth
->dev
, dma_addr
)) {
894 /* Process the incoming frame. */
895 pkt_len
= status
& GRETH_BD_LEN
;
897 dma_unmap_single(greth
->dev
,
898 greth_read_bd(&bdp
->addr
),
899 MAX_FRAME_SIZE
+ NET_IP_ALIGN
,
902 if (netif_msg_pktdata(greth
))
903 greth_print_rx_packet(phys_to_virt(greth_read_bd(&bdp
->addr
)), pkt_len
);
905 skb_put(skb
, pkt_len
);
907 if (dev
->features
& NETIF_F_RXCSUM
&& hw_checksummed(status
))
908 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
910 skb_checksum_none_assert(skb
);
912 skb
->protocol
= eth_type_trans(skb
, dev
);
913 dev
->stats
.rx_packets
++;
914 dev
->stats
.rx_bytes
+= pkt_len
;
915 netif_receive_skb(skb
);
917 greth
->rx_skbuff
[greth
->rx_cur
] = newskb
;
918 greth_write_bd(&bdp
->addr
, dma_addr
);
921 dev_warn(greth
->dev
, "Could not create DMA mapping, dropping packet\n");
922 dev_kfree_skb(newskb
);
923 /* reusing current skb, so it is a drop */
924 dev
->stats
.rx_dropped
++;
927 /* Bad Frame transfer, the skb is reused */
928 dev
->stats
.rx_dropped
++;
930 /* Failed Allocating a new skb. This is rather stupid
931 * but the current "filled" skb is reused, as if
932 * transfer failure. One could argue that RX descriptor
933 * table handling should be divided into cleaning and
934 * filling as the TX part of the driver
937 dev_warn(greth
->dev
, "Could not allocate SKB, dropping packet\n");
938 /* reusing current skb, so it is a drop */
939 dev
->stats
.rx_dropped
++;
942 status
= GRETH_BD_EN
| GRETH_BD_IE
;
943 if (greth
->rx_cur
== GRETH_RXBD_NUM_MASK
) {
944 status
|= GRETH_BD_WR
;
948 greth_write_bd(&bdp
->stat
, status
);
949 spin_lock_irqsave(&greth
->devlock
, flags
);
950 greth_enable_rx(greth
);
951 spin_unlock_irqrestore(&greth
->devlock
, flags
);
952 greth
->rx_cur
= NEXT_RX(greth
->rx_cur
);
959 static int greth_poll(struct napi_struct
*napi
, int budget
)
961 struct greth_private
*greth
;
965 greth
= container_of(napi
, struct greth_private
, napi
);
968 if (netif_queue_stopped(greth
->netdev
)) {
970 greth_clean_tx_gbit(greth
->netdev
);
972 greth_clean_tx(greth
->netdev
);
975 if (greth
->gbit_mac
) {
976 work_done
+= greth_rx_gbit(greth
->netdev
, budget
- work_done
);
978 work_done
+= greth_rx(greth
->netdev
, budget
- work_done
);
981 if (work_done
< budget
) {
983 spin_lock_irqsave(&greth
->devlock
, flags
);
985 ctrl
= GRETH_REGLOAD(greth
->regs
->control
);
986 if (netif_queue_stopped(greth
->netdev
)) {
987 GRETH_REGSAVE(greth
->regs
->control
,
988 ctrl
| GRETH_TXI
| GRETH_RXI
);
989 mask
= GRETH_INT_RX
| GRETH_INT_RE
|
990 GRETH_INT_TX
| GRETH_INT_TE
;
992 GRETH_REGSAVE(greth
->regs
->control
, ctrl
| GRETH_RXI
);
993 mask
= GRETH_INT_RX
| GRETH_INT_RE
;
996 if (GRETH_REGLOAD(greth
->regs
->status
) & mask
) {
997 GRETH_REGSAVE(greth
->regs
->control
, ctrl
);
998 spin_unlock_irqrestore(&greth
->devlock
, flags
);
999 goto restart_txrx_poll
;
1001 __napi_complete(napi
);
1002 spin_unlock_irqrestore(&greth
->devlock
, flags
);
1009 static int greth_set_mac_add(struct net_device
*dev
, void *p
)
1011 struct sockaddr
*addr
= p
;
1012 struct greth_private
*greth
;
1013 struct greth_regs
*regs
;
1015 greth
= netdev_priv(dev
);
1018 if (!is_valid_ether_addr(addr
->sa_data
))
1019 return -EADDRNOTAVAIL
;
1021 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
1022 GRETH_REGSAVE(regs
->esa_msb
, dev
->dev_addr
[0] << 8 | dev
->dev_addr
[1]);
1023 GRETH_REGSAVE(regs
->esa_lsb
, dev
->dev_addr
[2] << 24 | dev
->dev_addr
[3] << 16 |
1024 dev
->dev_addr
[4] << 8 | dev
->dev_addr
[5]);
1029 static u32
greth_hash_get_index(__u8
*addr
)
1031 return (ether_crc(6, addr
)) & 0x3F;
1034 static void greth_set_hash_filter(struct net_device
*dev
)
1036 struct netdev_hw_addr
*ha
;
1037 struct greth_private
*greth
= netdev_priv(dev
);
1038 struct greth_regs
*regs
= greth
->regs
;
1042 mc_filter
[0] = mc_filter
[1] = 0;
1044 netdev_for_each_mc_addr(ha
, dev
) {
1045 bitnr
= greth_hash_get_index(ha
->addr
);
1046 mc_filter
[bitnr
>> 5] |= 1 << (bitnr
& 31);
1049 GRETH_REGSAVE(regs
->hash_msb
, mc_filter
[1]);
1050 GRETH_REGSAVE(regs
->hash_lsb
, mc_filter
[0]);
1053 static void greth_set_multicast_list(struct net_device
*dev
)
1056 struct greth_private
*greth
= netdev_priv(dev
);
1057 struct greth_regs
*regs
= greth
->regs
;
1059 cfg
= GRETH_REGLOAD(regs
->control
);
1060 if (dev
->flags
& IFF_PROMISC
)
1061 cfg
|= GRETH_CTRL_PR
;
1063 cfg
&= ~GRETH_CTRL_PR
;
1065 if (greth
->multicast
) {
1066 if (dev
->flags
& IFF_ALLMULTI
) {
1067 GRETH_REGSAVE(regs
->hash_msb
, -1);
1068 GRETH_REGSAVE(regs
->hash_lsb
, -1);
1069 cfg
|= GRETH_CTRL_MCEN
;
1070 GRETH_REGSAVE(regs
->control
, cfg
);
1074 if (netdev_mc_empty(dev
)) {
1075 cfg
&= ~GRETH_CTRL_MCEN
;
1076 GRETH_REGSAVE(regs
->control
, cfg
);
1080 /* Setup multicast filter */
1081 greth_set_hash_filter(dev
);
1082 cfg
|= GRETH_CTRL_MCEN
;
1084 GRETH_REGSAVE(regs
->control
, cfg
);
1087 static u32
greth_get_msglevel(struct net_device
*dev
)
1089 struct greth_private
*greth
= netdev_priv(dev
);
1090 return greth
->msg_enable
;
1093 static void greth_set_msglevel(struct net_device
*dev
, u32 value
)
1095 struct greth_private
*greth
= netdev_priv(dev
);
1096 greth
->msg_enable
= value
;
1098 static int greth_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1100 struct greth_private
*greth
= netdev_priv(dev
);
1101 struct phy_device
*phy
= greth
->phy
;
1106 return phy_ethtool_gset(phy
, cmd
);
1109 static int greth_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1111 struct greth_private
*greth
= netdev_priv(dev
);
1112 struct phy_device
*phy
= greth
->phy
;
1117 return phy_ethtool_sset(phy
, cmd
);
1120 static int greth_get_regs_len(struct net_device
*dev
)
1122 return sizeof(struct greth_regs
);
1125 static void greth_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
1127 struct greth_private
*greth
= netdev_priv(dev
);
1129 strlcpy(info
->driver
, dev_driver_string(greth
->dev
),
1130 sizeof(info
->driver
));
1131 strlcpy(info
->version
, "revision: 1.0", sizeof(info
->version
));
1132 strlcpy(info
->bus_info
, greth
->dev
->bus
->name
, sizeof(info
->bus_info
));
1133 strlcpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
1134 info
->eedump_len
= 0;
1135 info
->regdump_len
= sizeof(struct greth_regs
);
1138 static void greth_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
, void *p
)
1141 struct greth_private
*greth
= netdev_priv(dev
);
1142 u32 __iomem
*greth_regs
= (u32 __iomem
*) greth
->regs
;
1145 for (i
= 0; i
< sizeof(struct greth_regs
) / sizeof(u32
); i
++)
1146 buff
[i
] = greth_read_bd(&greth_regs
[i
]);
1149 static const struct ethtool_ops greth_ethtool_ops
= {
1150 .get_msglevel
= greth_get_msglevel
,
1151 .set_msglevel
= greth_set_msglevel
,
1152 .get_settings
= greth_get_settings
,
1153 .set_settings
= greth_set_settings
,
1154 .get_drvinfo
= greth_get_drvinfo
,
1155 .get_regs_len
= greth_get_regs_len
,
1156 .get_regs
= greth_get_regs
,
1157 .get_link
= ethtool_op_get_link
,
1160 static struct net_device_ops greth_netdev_ops
= {
1161 .ndo_open
= greth_open
,
1162 .ndo_stop
= greth_close
,
1163 .ndo_start_xmit
= greth_start_xmit
,
1164 .ndo_set_mac_address
= greth_set_mac_add
,
1165 .ndo_validate_addr
= eth_validate_addr
,
1168 static inline int wait_for_mdio(struct greth_private
*greth
)
1170 unsigned long timeout
= jiffies
+ 4*HZ
/100;
1171 while (GRETH_REGLOAD(greth
->regs
->mdio
) & GRETH_MII_BUSY
) {
1172 if (time_after(jiffies
, timeout
))
1178 static int greth_mdio_read(struct mii_bus
*bus
, int phy
, int reg
)
1180 struct greth_private
*greth
= bus
->priv
;
1183 if (!wait_for_mdio(greth
))
1186 GRETH_REGSAVE(greth
->regs
->mdio
, ((phy
& 0x1F) << 11) | ((reg
& 0x1F) << 6) | 2);
1188 if (!wait_for_mdio(greth
))
1191 if (!(GRETH_REGLOAD(greth
->regs
->mdio
) & GRETH_MII_NVALID
)) {
1192 data
= (GRETH_REGLOAD(greth
->regs
->mdio
) >> 16) & 0xFFFF;
1200 static int greth_mdio_write(struct mii_bus
*bus
, int phy
, int reg
, u16 val
)
1202 struct greth_private
*greth
= bus
->priv
;
1204 if (!wait_for_mdio(greth
))
1207 GRETH_REGSAVE(greth
->regs
->mdio
,
1208 ((val
& 0xFFFF) << 16) | ((phy
& 0x1F) << 11) | ((reg
& 0x1F) << 6) | 1);
1210 if (!wait_for_mdio(greth
))
1216 static int greth_mdio_reset(struct mii_bus
*bus
)
1221 static void greth_link_change(struct net_device
*dev
)
1223 struct greth_private
*greth
= netdev_priv(dev
);
1224 struct phy_device
*phydev
= greth
->phy
;
1225 unsigned long flags
;
1226 int status_change
= 0;
1229 spin_lock_irqsave(&greth
->devlock
, flags
);
1233 if ((greth
->speed
!= phydev
->speed
) || (greth
->duplex
!= phydev
->duplex
)) {
1234 ctrl
= GRETH_REGLOAD(greth
->regs
->control
) &
1235 ~(GRETH_CTRL_FD
| GRETH_CTRL_SP
| GRETH_CTRL_GB
);
1238 ctrl
|= GRETH_CTRL_FD
;
1240 if (phydev
->speed
== SPEED_100
)
1241 ctrl
|= GRETH_CTRL_SP
;
1242 else if (phydev
->speed
== SPEED_1000
)
1243 ctrl
|= GRETH_CTRL_GB
;
1245 GRETH_REGSAVE(greth
->regs
->control
, ctrl
);
1246 greth
->speed
= phydev
->speed
;
1247 greth
->duplex
= phydev
->duplex
;
1252 if (phydev
->link
!= greth
->link
) {
1253 if (!phydev
->link
) {
1257 greth
->link
= phydev
->link
;
1262 spin_unlock_irqrestore(&greth
->devlock
, flags
);
1264 if (status_change
) {
1266 pr_debug("%s: link up (%d/%s)\n",
1267 dev
->name
, phydev
->speed
,
1268 DUPLEX_FULL
== phydev
->duplex
? "Full" : "Half");
1270 pr_debug("%s: link down\n", dev
->name
);
1274 static int greth_mdio_probe(struct net_device
*dev
)
1276 struct greth_private
*greth
= netdev_priv(dev
);
1277 struct phy_device
*phy
= NULL
;
1280 /* Find the first PHY */
1281 phy
= phy_find_first(greth
->mdio
);
1284 if (netif_msg_probe(greth
))
1285 dev_err(&dev
->dev
, "no PHY found\n");
1289 ret
= phy_connect_direct(dev
, phy
, &greth_link_change
,
1290 greth
->gbit_mac
? PHY_INTERFACE_MODE_GMII
: PHY_INTERFACE_MODE_MII
);
1292 if (netif_msg_ifup(greth
))
1293 dev_err(&dev
->dev
, "could not attach to PHY\n");
1297 if (greth
->gbit_mac
)
1298 phy
->supported
&= PHY_GBIT_FEATURES
;
1300 phy
->supported
&= PHY_BASIC_FEATURES
;
1302 phy
->advertising
= phy
->supported
;
1312 static inline int phy_aneg_done(struct phy_device
*phydev
)
1316 retval
= phy_read(phydev
, MII_BMSR
);
1318 return (retval
< 0) ? retval
: (retval
& BMSR_ANEGCOMPLETE
);
1321 static int greth_mdio_init(struct greth_private
*greth
)
1324 unsigned long timeout
;
1326 greth
->mdio
= mdiobus_alloc();
1331 greth
->mdio
->name
= "greth-mdio";
1332 snprintf(greth
->mdio
->id
, MII_BUS_ID_SIZE
, "%s-%d", greth
->mdio
->name
, greth
->irq
);
1333 greth
->mdio
->read
= greth_mdio_read
;
1334 greth
->mdio
->write
= greth_mdio_write
;
1335 greth
->mdio
->reset
= greth_mdio_reset
;
1336 greth
->mdio
->priv
= greth
;
1338 greth
->mdio
->irq
= greth
->mdio_irqs
;
1340 for (phy
= 0; phy
< PHY_MAX_ADDR
; phy
++)
1341 greth
->mdio
->irq
[phy
] = PHY_POLL
;
1343 ret
= mdiobus_register(greth
->mdio
);
1348 ret
= greth_mdio_probe(greth
->netdev
);
1350 if (netif_msg_probe(greth
))
1351 dev_err(&greth
->netdev
->dev
, "failed to probe MDIO bus\n");
1355 phy_start(greth
->phy
);
1357 /* If Ethernet debug link is used make autoneg happen right away */
1358 if (greth
->edcl
&& greth_edcl
== 1) {
1359 phy_start_aneg(greth
->phy
);
1360 timeout
= jiffies
+ 6*HZ
;
1361 while (!phy_aneg_done(greth
->phy
) && time_before(jiffies
, timeout
)) {
1363 phy_read_status(greth
->phy
);
1364 greth_link_change(greth
->netdev
);
1370 mdiobus_unregister(greth
->mdio
);
1372 mdiobus_free(greth
->mdio
);
1376 /* Initialize the GRETH MAC */
1377 static int greth_of_probe(struct platform_device
*ofdev
)
1379 struct net_device
*dev
;
1380 struct greth_private
*greth
;
1381 struct greth_regs
*regs
;
1386 unsigned long timeout
;
1388 dev
= alloc_etherdev(sizeof(struct greth_private
));
1393 greth
= netdev_priv(dev
);
1394 greth
->netdev
= dev
;
1395 greth
->dev
= &ofdev
->dev
;
1397 if (greth_debug
> 0)
1398 greth
->msg_enable
= greth_debug
;
1400 greth
->msg_enable
= GRETH_DEF_MSG_ENABLE
;
1402 spin_lock_init(&greth
->devlock
);
1404 greth
->regs
= of_ioremap(&ofdev
->resource
[0], 0,
1405 resource_size(&ofdev
->resource
[0]),
1406 "grlib-greth regs");
1408 if (greth
->regs
== NULL
) {
1409 if (netif_msg_probe(greth
))
1410 dev_err(greth
->dev
, "ioremap failure.\n");
1416 greth
->irq
= ofdev
->archdata
.irqs
[0];
1418 dev_set_drvdata(greth
->dev
, dev
);
1419 SET_NETDEV_DEV(dev
, greth
->dev
);
1421 if (netif_msg_probe(greth
))
1422 dev_dbg(greth
->dev
, "resetting controller.\n");
1424 /* Reset the controller. */
1425 GRETH_REGSAVE(regs
->control
, GRETH_RESET
);
1427 /* Wait for MAC to reset itself */
1428 timeout
= jiffies
+ HZ
/100;
1429 while (GRETH_REGLOAD(regs
->control
) & GRETH_RESET
) {
1430 if (time_after(jiffies
, timeout
)) {
1432 if (netif_msg_probe(greth
))
1433 dev_err(greth
->dev
, "timeout when waiting for reset.\n");
1438 /* Get default PHY address */
1439 greth
->phyaddr
= (GRETH_REGLOAD(regs
->mdio
) >> 11) & 0x1F;
1441 /* Check if we have GBIT capable MAC */
1442 tmp
= GRETH_REGLOAD(regs
->control
);
1443 greth
->gbit_mac
= (tmp
>> 27) & 1;
1445 /* Check for multicast capability */
1446 greth
->multicast
= (tmp
>> 25) & 1;
1448 greth
->edcl
= (tmp
>> 31) & 1;
1450 /* If we have EDCL we disable the EDCL speed-duplex FSM so
1451 * it doesn't interfere with the software */
1452 if (greth
->edcl
!= 0)
1453 GRETH_REGORIN(regs
->control
, GRETH_CTRL_DISDUPLEX
);
1455 /* Check if MAC can handle MDIO interrupts */
1456 greth
->mdio_int_en
= (tmp
>> 26) & 1;
1458 err
= greth_mdio_init(greth
);
1460 if (netif_msg_probe(greth
))
1461 dev_err(greth
->dev
, "failed to register MDIO bus\n");
1465 /* Allocate TX descriptor ring in coherent memory */
1466 greth
->tx_bd_base
= dma_zalloc_coherent(greth
->dev
, 1024,
1467 &greth
->tx_bd_base_phys
,
1469 if (!greth
->tx_bd_base
) {
1474 /* Allocate RX descriptor ring in coherent memory */
1475 greth
->rx_bd_base
= dma_zalloc_coherent(greth
->dev
, 1024,
1476 &greth
->rx_bd_base_phys
,
1478 if (!greth
->rx_bd_base
) {
1483 /* Get MAC address from: module param, OF property or ID prom */
1484 for (i
= 0; i
< 6; i
++) {
1485 if (macaddr
[i
] != 0)
1489 const unsigned char *addr
;
1491 addr
= of_get_property(ofdev
->dev
.of_node
, "local-mac-address",
1493 if (addr
!= NULL
&& len
== 6) {
1494 for (i
= 0; i
< 6; i
++)
1495 macaddr
[i
] = (unsigned int) addr
[i
];
1498 for (i
= 0; i
< 6; i
++)
1499 macaddr
[i
] = (unsigned int) idprom
->id_ethaddr
[i
];
1504 for (i
= 0; i
< 6; i
++)
1505 dev
->dev_addr
[i
] = macaddr
[i
];
1509 if (!is_valid_ether_addr(&dev
->dev_addr
[0])) {
1510 if (netif_msg_probe(greth
))
1511 dev_err(greth
->dev
, "no valid ethernet address, aborting.\n");
1516 GRETH_REGSAVE(regs
->esa_msb
, dev
->dev_addr
[0] << 8 | dev
->dev_addr
[1]);
1517 GRETH_REGSAVE(regs
->esa_lsb
, dev
->dev_addr
[2] << 24 | dev
->dev_addr
[3] << 16 |
1518 dev
->dev_addr
[4] << 8 | dev
->dev_addr
[5]);
1520 /* Clear all pending interrupts except PHY irq */
1521 GRETH_REGSAVE(regs
->status
, 0xFF);
1523 if (greth
->gbit_mac
) {
1524 dev
->hw_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
|
1526 dev
->features
= dev
->hw_features
| NETIF_F_HIGHDMA
;
1527 greth_netdev_ops
.ndo_start_xmit
= greth_start_xmit_gbit
;
1530 if (greth
->multicast
) {
1531 greth_netdev_ops
.ndo_set_rx_mode
= greth_set_multicast_list
;
1532 dev
->flags
|= IFF_MULTICAST
;
1534 dev
->flags
&= ~IFF_MULTICAST
;
1537 dev
->netdev_ops
= &greth_netdev_ops
;
1538 dev
->ethtool_ops
= &greth_ethtool_ops
;
1540 err
= register_netdev(dev
);
1542 if (netif_msg_probe(greth
))
1543 dev_err(greth
->dev
, "netdevice registration failed.\n");
1548 netif_napi_add(dev
, &greth
->napi
, greth_poll
, 64);
1553 dma_free_coherent(greth
->dev
, 1024, greth
->rx_bd_base
, greth
->rx_bd_base_phys
);
1555 dma_free_coherent(greth
->dev
, 1024, greth
->tx_bd_base
, greth
->tx_bd_base_phys
);
1557 mdiobus_unregister(greth
->mdio
);
1559 of_iounmap(&ofdev
->resource
[0], greth
->regs
, resource_size(&ofdev
->resource
[0]));
1565 static int greth_of_remove(struct platform_device
*of_dev
)
1567 struct net_device
*ndev
= platform_get_drvdata(of_dev
);
1568 struct greth_private
*greth
= netdev_priv(ndev
);
1570 /* Free descriptor areas */
1571 dma_free_coherent(&of_dev
->dev
, 1024, greth
->rx_bd_base
, greth
->rx_bd_base_phys
);
1573 dma_free_coherent(&of_dev
->dev
, 1024, greth
->tx_bd_base
, greth
->tx_bd_base_phys
);
1576 phy_stop(greth
->phy
);
1577 mdiobus_unregister(greth
->mdio
);
1579 unregister_netdev(ndev
);
1582 of_iounmap(&of_dev
->resource
[0], greth
->regs
, resource_size(&of_dev
->resource
[0]));
1587 static struct of_device_id greth_of_match
[] = {
1589 .name
= "GAISLER_ETHMAC",
1597 MODULE_DEVICE_TABLE(of
, greth_of_match
);
1599 static struct platform_driver greth_of_driver
= {
1601 .name
= "grlib-greth",
1602 .owner
= THIS_MODULE
,
1603 .of_match_table
= greth_of_match
,
1605 .probe
= greth_of_probe
,
1606 .remove
= greth_of_remove
,
1609 module_platform_driver(greth_of_driver
);
1611 MODULE_AUTHOR("Aeroflex Gaisler AB.");
1612 MODULE_DESCRIPTION("Aeroflex Gaisler Ethernet MAC driver");
1613 MODULE_LICENSE("GPL");