2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/ptrace.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/bitops.h>
37 #include <linux/platform_device.h>
38 #include <linux/phy.h>
40 #include <linux/of_mdio.h>
41 #include <linux/of_platform.h>
42 #include <linux/of_gpio.h>
44 #include <linux/vmalloc.h>
45 #include <asm/pgtable.h>
47 #include <asm/uaccess.h>
51 /*************************************************/
53 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
54 MODULE_DESCRIPTION("Freescale Ethernet Driver");
55 MODULE_LICENSE("GPL");
56 MODULE_VERSION(DRV_MODULE_VERSION
);
58 static int fs_enet_debug
= -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
59 module_param(fs_enet_debug
, int, 0);
60 MODULE_PARM_DESC(fs_enet_debug
,
61 "Freescale bitmapped debugging message enable value");
63 #ifdef CONFIG_NET_POLL_CONTROLLER
64 static void fs_enet_netpoll(struct net_device
*dev
);
67 static void fs_set_multicast_list(struct net_device
*dev
)
69 struct fs_enet_private
*fep
= netdev_priv(dev
);
71 (*fep
->ops
->set_multicast_list
)(dev
);
74 static void skb_align(struct sk_buff
*skb
, int align
)
76 int off
= ((unsigned long)skb
->data
) & (align
- 1);
79 skb_reserve(skb
, align
- off
);
82 /* NAPI receive function */
83 static int fs_enet_rx_napi(struct napi_struct
*napi
, int budget
)
85 struct fs_enet_private
*fep
= container_of(napi
, struct fs_enet_private
, napi
);
86 struct net_device
*dev
= fep
->ndev
;
87 const struct fs_platform_info
*fpi
= fep
->fpi
;
89 struct sk_buff
*skb
, *skbn
, *skbt
;
95 * First, grab all of the stats for the incoming packet.
96 * These get messed up if we get called due to a busy condition.
100 /* clear RX status bits for napi*/
101 (*fep
->ops
->napi_clear_rx_event
)(dev
);
103 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
104 curidx
= bdp
- fep
->rx_bd_base
;
107 * Since we have allocated space to hold a complete frame,
108 * the last indicator should be set.
110 if ((sc
& BD_ENET_RX_LAST
) == 0)
111 dev_warn(fep
->dev
, "rcv is not +last\n");
116 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_CL
|
117 BD_ENET_RX_NO
| BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
118 fep
->stats
.rx_errors
++;
119 /* Frame too long or too short. */
120 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
))
121 fep
->stats
.rx_length_errors
++;
122 /* Frame alignment */
123 if (sc
& (BD_ENET_RX_NO
| BD_ENET_RX_CL
))
124 fep
->stats
.rx_frame_errors
++;
126 if (sc
& BD_ENET_RX_CR
)
127 fep
->stats
.rx_crc_errors
++;
129 if (sc
& BD_ENET_RX_OV
)
130 fep
->stats
.rx_crc_errors
++;
132 skb
= fep
->rx_skbuff
[curidx
];
134 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
135 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
141 skb
= fep
->rx_skbuff
[curidx
];
143 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
144 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
148 * Process the incoming frame.
150 fep
->stats
.rx_packets
++;
151 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
152 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
154 if (pkt_len
<= fpi
->rx_copybreak
) {
155 /* +2 to make IP header L1 cache aligned */
156 skbn
= dev_alloc_skb(pkt_len
+ 2);
158 skb_reserve(skbn
, 2); /* align IP header */
159 skb_copy_from_linear_data(skb
,
160 skbn
->data
, pkt_len
);
167 skbn
= dev_alloc_skb(ENET_RX_FRSIZE
);
170 skb_align(skbn
, ENET_RX_ALIGN
);
174 skb_put(skb
, pkt_len
); /* Make room */
175 skb
->protocol
= eth_type_trans(skb
, dev
);
177 netif_receive_skb(skb
);
180 "Memory squeeze, dropping packet.\n");
181 fep
->stats
.rx_dropped
++;
186 fep
->rx_skbuff
[curidx
] = skbn
;
187 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
188 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
191 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
194 * Update BD pointer to next entry.
196 if ((sc
& BD_ENET_RX_WRAP
) == 0)
199 bdp
= fep
->rx_bd_base
;
201 (*fep
->ops
->rx_bd_done
)(dev
);
203 if (received
>= budget
)
209 if (received
< budget
) {
212 (*fep
->ops
->napi_enable_rx
)(dev
);
217 /* non NAPI receive function */
218 static int fs_enet_rx_non_napi(struct net_device
*dev
)
220 struct fs_enet_private
*fep
= netdev_priv(dev
);
221 const struct fs_platform_info
*fpi
= fep
->fpi
;
223 struct sk_buff
*skb
, *skbn
, *skbt
;
228 * First, grab all of the stats for the incoming packet.
229 * These get messed up if we get called due to a busy condition.
233 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
235 curidx
= bdp
- fep
->rx_bd_base
;
238 * Since we have allocated space to hold a complete frame,
239 * the last indicator should be set.
241 if ((sc
& BD_ENET_RX_LAST
) == 0)
242 dev_warn(fep
->dev
, "rcv is not +last\n");
247 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_CL
|
248 BD_ENET_RX_NO
| BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
249 fep
->stats
.rx_errors
++;
250 /* Frame too long or too short. */
251 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
))
252 fep
->stats
.rx_length_errors
++;
253 /* Frame alignment */
254 if (sc
& (BD_ENET_RX_NO
| BD_ENET_RX_CL
))
255 fep
->stats
.rx_frame_errors
++;
257 if (sc
& BD_ENET_RX_CR
)
258 fep
->stats
.rx_crc_errors
++;
260 if (sc
& BD_ENET_RX_OV
)
261 fep
->stats
.rx_crc_errors
++;
263 skb
= fep
->rx_skbuff
[curidx
];
265 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
266 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
273 skb
= fep
->rx_skbuff
[curidx
];
275 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
276 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
280 * Process the incoming frame.
282 fep
->stats
.rx_packets
++;
283 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
284 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
286 if (pkt_len
<= fpi
->rx_copybreak
) {
287 /* +2 to make IP header L1 cache aligned */
288 skbn
= dev_alloc_skb(pkt_len
+ 2);
290 skb_reserve(skbn
, 2); /* align IP header */
291 skb_copy_from_linear_data(skb
,
292 skbn
->data
, pkt_len
);
299 skbn
= dev_alloc_skb(ENET_RX_FRSIZE
);
302 skb_align(skbn
, ENET_RX_ALIGN
);
306 skb_put(skb
, pkt_len
); /* Make room */
307 skb
->protocol
= eth_type_trans(skb
, dev
);
312 "Memory squeeze, dropping packet.\n");
313 fep
->stats
.rx_dropped
++;
318 fep
->rx_skbuff
[curidx
] = skbn
;
319 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
320 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
323 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
326 * Update BD pointer to next entry.
328 if ((sc
& BD_ENET_RX_WRAP
) == 0)
331 bdp
= fep
->rx_bd_base
;
333 (*fep
->ops
->rx_bd_done
)(dev
);
341 static void fs_enet_tx(struct net_device
*dev
)
343 struct fs_enet_private
*fep
= netdev_priv(dev
);
346 int dirtyidx
, do_wake
, do_restart
;
349 spin_lock(&fep
->tx_lock
);
352 do_wake
= do_restart
= 0;
353 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_TX_READY
) == 0) {
354 dirtyidx
= bdp
- fep
->tx_bd_base
;
356 if (fep
->tx_free
== fep
->tx_ring
)
359 skb
= fep
->tx_skbuff
[dirtyidx
];
364 if (sc
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
365 BD_ENET_TX_RL
| BD_ENET_TX_UN
| BD_ENET_TX_CSL
)) {
367 if (sc
& BD_ENET_TX_HB
) /* No heartbeat */
368 fep
->stats
.tx_heartbeat_errors
++;
369 if (sc
& BD_ENET_TX_LC
) /* Late collision */
370 fep
->stats
.tx_window_errors
++;
371 if (sc
& BD_ENET_TX_RL
) /* Retrans limit */
372 fep
->stats
.tx_aborted_errors
++;
373 if (sc
& BD_ENET_TX_UN
) /* Underrun */
374 fep
->stats
.tx_fifo_errors
++;
375 if (sc
& BD_ENET_TX_CSL
) /* Carrier lost */
376 fep
->stats
.tx_carrier_errors
++;
378 if (sc
& (BD_ENET_TX_LC
| BD_ENET_TX_RL
| BD_ENET_TX_UN
)) {
379 fep
->stats
.tx_errors
++;
383 fep
->stats
.tx_packets
++;
385 if (sc
& BD_ENET_TX_READY
) {
387 "HEY! Enet xmit interrupt and TX_READY.\n");
391 * Deferred means some collisions occurred during transmit,
392 * but we eventually sent the packet OK.
394 if (sc
& BD_ENET_TX_DEF
)
395 fep
->stats
.collisions
++;
398 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
399 skb
->len
, DMA_TO_DEVICE
);
402 * Free the sk buffer associated with this last transmit.
404 dev_kfree_skb_irq(skb
);
405 fep
->tx_skbuff
[dirtyidx
] = NULL
;
408 * Update pointer to next buffer descriptor to be transmitted.
410 if ((sc
& BD_ENET_TX_WRAP
) == 0)
413 bdp
= fep
->tx_bd_base
;
416 * Since we have freed up a buffer, the ring is no longer
426 (*fep
->ops
->tx_restart
)(dev
);
428 spin_unlock(&fep
->tx_lock
);
431 netif_wake_queue(dev
);
435 * The interrupt handler.
436 * This is called from the MPC core interrupt.
439 fs_enet_interrupt(int irq
, void *dev_id
)
441 struct net_device
*dev
= dev_id
;
442 struct fs_enet_private
*fep
;
443 const struct fs_platform_info
*fpi
;
449 fep
= netdev_priv(dev
);
453 while ((int_events
= (*fep
->ops
->get_int_events
)(dev
)) != 0) {
456 int_clr_events
= int_events
;
458 int_clr_events
&= ~fep
->ev_napi_rx
;
460 (*fep
->ops
->clear_int_events
)(dev
, int_clr_events
);
462 if (int_events
& fep
->ev_err
)
463 (*fep
->ops
->ev_error
)(dev
, int_events
);
465 if (int_events
& fep
->ev_rx
) {
467 fs_enet_rx_non_napi(dev
);
469 napi_ok
= napi_schedule_prep(&fep
->napi
);
471 (*fep
->ops
->napi_disable_rx
)(dev
);
472 (*fep
->ops
->clear_int_events
)(dev
, fep
->ev_napi_rx
);
474 /* NOTE: it is possible for FCCs in NAPI mode */
475 /* to submit a spurious interrupt while in poll */
477 __napi_schedule(&fep
->napi
);
481 if (int_events
& fep
->ev_tx
)
486 return IRQ_RETVAL(handled
);
489 void fs_init_bds(struct net_device
*dev
)
491 struct fs_enet_private
*fep
= netdev_priv(dev
);
498 fep
->dirty_tx
= fep
->cur_tx
= fep
->tx_bd_base
;
499 fep
->tx_free
= fep
->tx_ring
;
500 fep
->cur_rx
= fep
->rx_bd_base
;
503 * Initialize the receive buffer descriptors.
505 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
506 skb
= dev_alloc_skb(ENET_RX_FRSIZE
);
509 "Memory squeeze, unable to allocate skb\n");
512 skb_align(skb
, ENET_RX_ALIGN
);
513 fep
->rx_skbuff
[i
] = skb
;
515 dma_map_single(fep
->dev
, skb
->data
,
516 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
518 CBDW_DATLEN(bdp
, 0); /* zero */
519 CBDW_SC(bdp
, BD_ENET_RX_EMPTY
|
520 ((i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
));
523 * if we failed, fillup remainder
525 for (; i
< fep
->rx_ring
; i
++, bdp
++) {
526 fep
->rx_skbuff
[i
] = NULL
;
527 CBDW_SC(bdp
, (i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
);
531 * ...and the same for transmit.
533 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
534 fep
->tx_skbuff
[i
] = NULL
;
535 CBDW_BUFADDR(bdp
, 0);
537 CBDW_SC(bdp
, (i
< fep
->tx_ring
- 1) ? 0 : BD_SC_WRAP
);
541 void fs_cleanup_bds(struct net_device
*dev
)
543 struct fs_enet_private
*fep
= netdev_priv(dev
);
549 * Reset SKB transmit buffers.
551 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
552 if ((skb
= fep
->tx_skbuff
[i
]) == NULL
)
556 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
557 skb
->len
, DMA_TO_DEVICE
);
559 fep
->tx_skbuff
[i
] = NULL
;
564 * Reset SKB receive buffers
566 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
567 if ((skb
= fep
->rx_skbuff
[i
]) == NULL
)
571 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
572 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
575 fep
->rx_skbuff
[i
] = NULL
;
581 /**********************************************************************************/
583 #ifdef CONFIG_FS_ENET_MPC5121_FEC
585 * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
587 static struct sk_buff
*tx_skb_align_workaround(struct net_device
*dev
,
590 struct sk_buff
*new_skb
;
591 struct fs_enet_private
*fep
= netdev_priv(dev
);
594 new_skb
= dev_alloc_skb(skb
->len
+ 4);
596 if (net_ratelimit()) {
598 "Memory squeeze, dropping tx packet.\n");
603 /* Make sure new skb is properly aligned */
604 skb_align(new_skb
, 4);
606 /* Copy data to new skb ... */
607 skb_copy_from_linear_data(skb
, new_skb
->data
, skb
->len
);
608 skb_put(new_skb
, skb
->len
);
610 /* ... and free an old one */
611 dev_kfree_skb_any(skb
);
617 static int fs_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
619 struct fs_enet_private
*fep
= netdev_priv(dev
);
625 #ifdef CONFIG_FS_ENET_MPC5121_FEC
626 if (((unsigned long)skb
->data
) & 0x3) {
627 skb
= tx_skb_align_workaround(dev
, skb
);
630 * We have lost packet due to memory allocation error
631 * in tx_skb_align_workaround(). Hopefully original
632 * skb is still valid, so try transmit it later.
634 return NETDEV_TX_BUSY
;
638 spin_lock_irqsave(&fep
->tx_lock
, flags
);
641 * Fill in a Tx ring entry
645 if (!fep
->tx_free
|| (CBDR_SC(bdp
) & BD_ENET_TX_READY
)) {
646 netif_stop_queue(dev
);
647 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
650 * Ooops. All transmit buffers are full. Bail out.
651 * This should not happen, since the tx queue should be stopped.
653 dev_warn(fep
->dev
, "tx queue full!.\n");
654 return NETDEV_TX_BUSY
;
657 curidx
= bdp
- fep
->tx_bd_base
;
659 * Clear all of the status flags.
661 CBDC_SC(bdp
, BD_ENET_TX_STATS
);
666 fep
->tx_skbuff
[curidx
] = skb
;
668 fep
->stats
.tx_bytes
+= skb
->len
;
671 * Push the data cache so the CPM does not get stale memory data.
673 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
,
674 skb
->data
, skb
->len
, DMA_TO_DEVICE
));
675 CBDW_DATLEN(bdp
, skb
->len
);
677 dev
->trans_start
= jiffies
;
680 * If this was the last BD in the ring, start at the beginning again.
682 if ((CBDR_SC(bdp
) & BD_ENET_TX_WRAP
) == 0)
685 fep
->cur_tx
= fep
->tx_bd_base
;
688 netif_stop_queue(dev
);
690 /* Trigger transmission start */
691 sc
= BD_ENET_TX_READY
| BD_ENET_TX_INTR
|
692 BD_ENET_TX_LAST
| BD_ENET_TX_TC
;
694 /* note that while FEC does not have this bit
695 * it marks it as available for software use
696 * yay for hw reuse :) */
698 sc
|= BD_ENET_TX_PAD
;
701 (*fep
->ops
->tx_kickstart
)(dev
);
703 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
708 static void fs_timeout(struct net_device
*dev
)
710 struct fs_enet_private
*fep
= netdev_priv(dev
);
714 fep
->stats
.tx_errors
++;
716 spin_lock_irqsave(&fep
->lock
, flags
);
718 if (dev
->flags
& IFF_UP
) {
719 phy_stop(fep
->phydev
);
720 (*fep
->ops
->stop
)(dev
);
721 (*fep
->ops
->restart
)(dev
);
722 phy_start(fep
->phydev
);
725 phy_start(fep
->phydev
);
726 wake
= fep
->tx_free
&& !(CBDR_SC(fep
->cur_tx
) & BD_ENET_TX_READY
);
727 spin_unlock_irqrestore(&fep
->lock
, flags
);
730 netif_wake_queue(dev
);
733 /*-----------------------------------------------------------------------------
734 * generic link-change handler - should be sufficient for most cases
735 *-----------------------------------------------------------------------------*/
736 static void generic_adjust_link(struct net_device
*dev
)
738 struct fs_enet_private
*fep
= netdev_priv(dev
);
739 struct phy_device
*phydev
= fep
->phydev
;
743 /* adjust to duplex mode */
744 if (phydev
->duplex
!= fep
->oldduplex
) {
746 fep
->oldduplex
= phydev
->duplex
;
749 if (phydev
->speed
!= fep
->oldspeed
) {
751 fep
->oldspeed
= phydev
->speed
;
760 fep
->ops
->restart(dev
);
761 } else if (fep
->oldlink
) {
768 if (new_state
&& netif_msg_link(fep
))
769 phy_print_status(phydev
);
773 static void fs_adjust_link(struct net_device
*dev
)
775 struct fs_enet_private
*fep
= netdev_priv(dev
);
778 spin_lock_irqsave(&fep
->lock
, flags
);
780 if(fep
->ops
->adjust_link
)
781 fep
->ops
->adjust_link(dev
);
783 generic_adjust_link(dev
);
785 spin_unlock_irqrestore(&fep
->lock
, flags
);
788 static int fs_init_phy(struct net_device
*dev
)
790 struct fs_enet_private
*fep
= netdev_priv(dev
);
791 struct phy_device
*phydev
;
797 phydev
= of_phy_connect(dev
, fep
->fpi
->phy_node
, &fs_adjust_link
, 0,
798 PHY_INTERFACE_MODE_MII
);
800 phydev
= of_phy_connect_fixed_link(dev
, &fs_adjust_link
,
801 PHY_INTERFACE_MODE_MII
);
804 dev_err(&dev
->dev
, "Could not attach to PHY\n");
808 fep
->phydev
= phydev
;
813 static int fs_enet_open(struct net_device
*dev
)
815 struct fs_enet_private
*fep
= netdev_priv(dev
);
819 /* to initialize the fep->cur_rx,... */
820 /* not doing this, will cause a crash in fs_enet_rx_napi */
821 fs_init_bds(fep
->ndev
);
823 if (fep
->fpi
->use_napi
)
824 napi_enable(&fep
->napi
);
826 /* Install our interrupt handler. */
827 r
= request_irq(fep
->interrupt
, fs_enet_interrupt
, IRQF_SHARED
,
830 dev_err(fep
->dev
, "Could not allocate FS_ENET IRQ!");
831 if (fep
->fpi
->use_napi
)
832 napi_disable(&fep
->napi
);
836 err
= fs_init_phy(dev
);
838 free_irq(fep
->interrupt
, dev
);
839 if (fep
->fpi
->use_napi
)
840 napi_disable(&fep
->napi
);
843 phy_start(fep
->phydev
);
845 netif_start_queue(dev
);
850 static int fs_enet_close(struct net_device
*dev
)
852 struct fs_enet_private
*fep
= netdev_priv(dev
);
855 netif_stop_queue(dev
);
856 netif_carrier_off(dev
);
857 if (fep
->fpi
->use_napi
)
858 napi_disable(&fep
->napi
);
859 phy_stop(fep
->phydev
);
861 spin_lock_irqsave(&fep
->lock
, flags
);
862 spin_lock(&fep
->tx_lock
);
863 (*fep
->ops
->stop
)(dev
);
864 spin_unlock(&fep
->tx_lock
);
865 spin_unlock_irqrestore(&fep
->lock
, flags
);
867 /* release any irqs */
868 phy_disconnect(fep
->phydev
);
870 free_irq(fep
->interrupt
, dev
);
875 static struct net_device_stats
*fs_enet_get_stats(struct net_device
*dev
)
877 struct fs_enet_private
*fep
= netdev_priv(dev
);
881 /*************************************************************************/
883 static void fs_get_drvinfo(struct net_device
*dev
,
884 struct ethtool_drvinfo
*info
)
886 strcpy(info
->driver
, DRV_MODULE_NAME
);
887 strcpy(info
->version
, DRV_MODULE_VERSION
);
890 static int fs_get_regs_len(struct net_device
*dev
)
892 struct fs_enet_private
*fep
= netdev_priv(dev
);
894 return (*fep
->ops
->get_regs_len
)(dev
);
897 static void fs_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
900 struct fs_enet_private
*fep
= netdev_priv(dev
);
906 spin_lock_irqsave(&fep
->lock
, flags
);
907 r
= (*fep
->ops
->get_regs
)(dev
, p
, &len
);
908 spin_unlock_irqrestore(&fep
->lock
, flags
);
914 static int fs_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
916 struct fs_enet_private
*fep
= netdev_priv(dev
);
921 return phy_ethtool_gset(fep
->phydev
, cmd
);
924 static int fs_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
926 struct fs_enet_private
*fep
= netdev_priv(dev
);
931 return phy_ethtool_sset(fep
->phydev
, cmd
);
934 static int fs_nway_reset(struct net_device
*dev
)
939 static u32
fs_get_msglevel(struct net_device
*dev
)
941 struct fs_enet_private
*fep
= netdev_priv(dev
);
942 return fep
->msg_enable
;
945 static void fs_set_msglevel(struct net_device
*dev
, u32 value
)
947 struct fs_enet_private
*fep
= netdev_priv(dev
);
948 fep
->msg_enable
= value
;
951 static const struct ethtool_ops fs_ethtool_ops
= {
952 .get_drvinfo
= fs_get_drvinfo
,
953 .get_regs_len
= fs_get_regs_len
,
954 .get_settings
= fs_get_settings
,
955 .set_settings
= fs_set_settings
,
956 .nway_reset
= fs_nway_reset
,
957 .get_link
= ethtool_op_get_link
,
958 .get_msglevel
= fs_get_msglevel
,
959 .set_msglevel
= fs_set_msglevel
,
960 .set_tx_csum
= ethtool_op_set_tx_csum
, /* local! */
961 .set_sg
= ethtool_op_set_sg
,
962 .get_regs
= fs_get_regs
,
965 static int fs_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
967 struct fs_enet_private
*fep
= netdev_priv(dev
);
968 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*)&rq
->ifr_data
;
970 if (!netif_running(dev
))
973 return phy_mii_ioctl(fep
->phydev
, mii
, cmd
);
976 extern int fs_mii_connect(struct net_device
*dev
);
977 extern void fs_mii_disconnect(struct net_device
*dev
);
979 /**************************************************************************************/
981 #ifdef CONFIG_FS_ENET_HAS_FEC
982 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
984 #define IS_FEC(match) 0
987 static const struct net_device_ops fs_enet_netdev_ops
= {
988 .ndo_open
= fs_enet_open
,
989 .ndo_stop
= fs_enet_close
,
990 .ndo_get_stats
= fs_enet_get_stats
,
991 .ndo_start_xmit
= fs_enet_start_xmit
,
992 .ndo_tx_timeout
= fs_timeout
,
993 .ndo_set_multicast_list
= fs_set_multicast_list
,
994 .ndo_do_ioctl
= fs_ioctl
,
995 .ndo_validate_addr
= eth_validate_addr
,
996 .ndo_set_mac_address
= eth_mac_addr
,
997 .ndo_change_mtu
= eth_change_mtu
,
998 #ifdef CONFIG_NET_POLL_CONTROLLER
999 .ndo_poll_controller
= fs_enet_netpoll
,
1003 static int __devinit
fs_enet_probe(struct of_device
*ofdev
,
1004 const struct of_device_id
*match
)
1006 struct net_device
*ndev
;
1007 struct fs_enet_private
*fep
;
1008 struct fs_platform_info
*fpi
;
1011 int privsize
, len
, ret
= -ENODEV
;
1013 fpi
= kzalloc(sizeof(*fpi
), GFP_KERNEL
);
1017 if (!IS_FEC(match
)) {
1018 data
= of_get_property(ofdev
->node
, "fsl,cpm-command", &len
);
1019 if (!data
|| len
!= 4)
1022 fpi
->cp_command
= *data
;
1027 fpi
->rx_copybreak
= 240;
1029 fpi
->napi_weight
= 17;
1030 fpi
->phy_node
= of_parse_phandle(ofdev
->node
, "phy-handle", 0);
1031 if ((!fpi
->phy_node
) && (!of_get_property(ofdev
->node
, "fixed-link",
1035 privsize
= sizeof(*fep
) +
1036 sizeof(struct sk_buff
**) *
1037 (fpi
->rx_ring
+ fpi
->tx_ring
);
1039 ndev
= alloc_etherdev(privsize
);
1045 SET_NETDEV_DEV(ndev
, &ofdev
->dev
);
1046 dev_set_drvdata(&ofdev
->dev
, ndev
);
1048 fep
= netdev_priv(ndev
);
1049 fep
->dev
= &ofdev
->dev
;
1052 fep
->ops
= match
->data
;
1054 ret
= fep
->ops
->setup_data(ndev
);
1058 fep
->rx_skbuff
= (struct sk_buff
**)&fep
[1];
1059 fep
->tx_skbuff
= fep
->rx_skbuff
+ fpi
->rx_ring
;
1061 spin_lock_init(&fep
->lock
);
1062 spin_lock_init(&fep
->tx_lock
);
1064 mac_addr
= of_get_mac_address(ofdev
->node
);
1066 memcpy(ndev
->dev_addr
, mac_addr
, 6);
1068 ret
= fep
->ops
->allocate_bd(ndev
);
1070 goto out_cleanup_data
;
1072 fep
->rx_bd_base
= fep
->ring_base
;
1073 fep
->tx_bd_base
= fep
->rx_bd_base
+ fpi
->rx_ring
;
1075 fep
->tx_ring
= fpi
->tx_ring
;
1076 fep
->rx_ring
= fpi
->rx_ring
;
1078 ndev
->netdev_ops
= &fs_enet_netdev_ops
;
1079 ndev
->watchdog_timeo
= 2 * HZ
;
1081 netif_napi_add(ndev
, &fep
->napi
, fs_enet_rx_napi
,
1084 ndev
->ethtool_ops
= &fs_ethtool_ops
;
1086 init_timer(&fep
->phy_timer_list
);
1088 netif_carrier_off(ndev
);
1090 ret
= register_netdev(ndev
);
1094 pr_info("%s: fs_enet: %pM\n", ndev
->name
, ndev
->dev_addr
);
1099 fep
->ops
->free_bd(ndev
);
1101 fep
->ops
->cleanup_data(ndev
);
1104 dev_set_drvdata(&ofdev
->dev
, NULL
);
1105 of_node_put(fpi
->phy_node
);
1111 static int fs_enet_remove(struct of_device
*ofdev
)
1113 struct net_device
*ndev
= dev_get_drvdata(&ofdev
->dev
);
1114 struct fs_enet_private
*fep
= netdev_priv(ndev
);
1116 unregister_netdev(ndev
);
1118 fep
->ops
->free_bd(ndev
);
1119 fep
->ops
->cleanup_data(ndev
);
1120 dev_set_drvdata(fep
->dev
, NULL
);
1121 of_node_put(fep
->fpi
->phy_node
);
1126 static struct of_device_id fs_enet_match
[] = {
1127 #ifdef CONFIG_FS_ENET_HAS_SCC
1129 .compatible
= "fsl,cpm1-scc-enet",
1130 .data
= (void *)&fs_scc_ops
,
1133 .compatible
= "fsl,cpm2-scc-enet",
1134 .data
= (void *)&fs_scc_ops
,
1137 #ifdef CONFIG_FS_ENET_HAS_FCC
1139 .compatible
= "fsl,cpm2-fcc-enet",
1140 .data
= (void *)&fs_fcc_ops
,
1143 #ifdef CONFIG_FS_ENET_HAS_FEC
1144 #ifdef CONFIG_FS_ENET_MPC5121_FEC
1146 .compatible
= "fsl,mpc5121-fec",
1147 .data
= (void *)&fs_fec_ops
,
1151 .compatible
= "fsl,pq1-fec-enet",
1152 .data
= (void *)&fs_fec_ops
,
1158 MODULE_DEVICE_TABLE(of
, fs_enet_match
);
1160 static struct of_platform_driver fs_enet_driver
= {
1162 .match_table
= fs_enet_match
,
1163 .probe
= fs_enet_probe
,
1164 .remove
= fs_enet_remove
,
1167 static int __init
fs_init(void)
1169 return of_register_platform_driver(&fs_enet_driver
);
1172 static void __exit
fs_cleanup(void)
1174 of_unregister_platform_driver(&fs_enet_driver
);
1177 #ifdef CONFIG_NET_POLL_CONTROLLER
1178 static void fs_enet_netpoll(struct net_device
*dev
)
1180 disable_irq(dev
->irq
);
1181 fs_enet_interrupt(dev
->irq
, dev
);
1182 enable_irq(dev
->irq
);
1186 /**************************************************************************************/
1188 module_init(fs_init
);
1189 module_exit(fs_cleanup
);