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
);
678 * If this was the last BD in the ring, start at the beginning again.
680 if ((CBDR_SC(bdp
) & BD_ENET_TX_WRAP
) == 0)
683 fep
->cur_tx
= fep
->tx_bd_base
;
686 netif_stop_queue(dev
);
688 /* Trigger transmission start */
689 sc
= BD_ENET_TX_READY
| BD_ENET_TX_INTR
|
690 BD_ENET_TX_LAST
| BD_ENET_TX_TC
;
692 /* note that while FEC does not have this bit
693 * it marks it as available for software use
694 * yay for hw reuse :) */
696 sc
|= BD_ENET_TX_PAD
;
699 (*fep
->ops
->tx_kickstart
)(dev
);
701 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
706 static void fs_timeout(struct net_device
*dev
)
708 struct fs_enet_private
*fep
= netdev_priv(dev
);
712 fep
->stats
.tx_errors
++;
714 spin_lock_irqsave(&fep
->lock
, flags
);
716 if (dev
->flags
& IFF_UP
) {
717 phy_stop(fep
->phydev
);
718 (*fep
->ops
->stop
)(dev
);
719 (*fep
->ops
->restart
)(dev
);
720 phy_start(fep
->phydev
);
723 phy_start(fep
->phydev
);
724 wake
= fep
->tx_free
&& !(CBDR_SC(fep
->cur_tx
) & BD_ENET_TX_READY
);
725 spin_unlock_irqrestore(&fep
->lock
, flags
);
728 netif_wake_queue(dev
);
731 /*-----------------------------------------------------------------------------
732 * generic link-change handler - should be sufficient for most cases
733 *-----------------------------------------------------------------------------*/
734 static void generic_adjust_link(struct net_device
*dev
)
736 struct fs_enet_private
*fep
= netdev_priv(dev
);
737 struct phy_device
*phydev
= fep
->phydev
;
741 /* adjust to duplex mode */
742 if (phydev
->duplex
!= fep
->oldduplex
) {
744 fep
->oldduplex
= phydev
->duplex
;
747 if (phydev
->speed
!= fep
->oldspeed
) {
749 fep
->oldspeed
= phydev
->speed
;
758 fep
->ops
->restart(dev
);
759 } else if (fep
->oldlink
) {
766 if (new_state
&& netif_msg_link(fep
))
767 phy_print_status(phydev
);
771 static void fs_adjust_link(struct net_device
*dev
)
773 struct fs_enet_private
*fep
= netdev_priv(dev
);
776 spin_lock_irqsave(&fep
->lock
, flags
);
778 if(fep
->ops
->adjust_link
)
779 fep
->ops
->adjust_link(dev
);
781 generic_adjust_link(dev
);
783 spin_unlock_irqrestore(&fep
->lock
, flags
);
786 static int fs_init_phy(struct net_device
*dev
)
788 struct fs_enet_private
*fep
= netdev_priv(dev
);
789 struct phy_device
*phydev
;
795 phydev
= of_phy_connect(dev
, fep
->fpi
->phy_node
, &fs_adjust_link
, 0,
796 PHY_INTERFACE_MODE_MII
);
798 phydev
= of_phy_connect_fixed_link(dev
, &fs_adjust_link
,
799 PHY_INTERFACE_MODE_MII
);
802 dev_err(&dev
->dev
, "Could not attach to PHY\n");
806 fep
->phydev
= phydev
;
811 static int fs_enet_open(struct net_device
*dev
)
813 struct fs_enet_private
*fep
= netdev_priv(dev
);
817 /* to initialize the fep->cur_rx,... */
818 /* not doing this, will cause a crash in fs_enet_rx_napi */
819 fs_init_bds(fep
->ndev
);
821 if (fep
->fpi
->use_napi
)
822 napi_enable(&fep
->napi
);
824 /* Install our interrupt handler. */
825 r
= request_irq(fep
->interrupt
, fs_enet_interrupt
, IRQF_SHARED
,
828 dev_err(fep
->dev
, "Could not allocate FS_ENET IRQ!");
829 if (fep
->fpi
->use_napi
)
830 napi_disable(&fep
->napi
);
834 err
= fs_init_phy(dev
);
836 free_irq(fep
->interrupt
, dev
);
837 if (fep
->fpi
->use_napi
)
838 napi_disable(&fep
->napi
);
841 phy_start(fep
->phydev
);
843 netif_start_queue(dev
);
848 static int fs_enet_close(struct net_device
*dev
)
850 struct fs_enet_private
*fep
= netdev_priv(dev
);
853 netif_stop_queue(dev
);
854 netif_carrier_off(dev
);
855 if (fep
->fpi
->use_napi
)
856 napi_disable(&fep
->napi
);
857 phy_stop(fep
->phydev
);
859 spin_lock_irqsave(&fep
->lock
, flags
);
860 spin_lock(&fep
->tx_lock
);
861 (*fep
->ops
->stop
)(dev
);
862 spin_unlock(&fep
->tx_lock
);
863 spin_unlock_irqrestore(&fep
->lock
, flags
);
865 /* release any irqs */
866 phy_disconnect(fep
->phydev
);
868 free_irq(fep
->interrupt
, dev
);
873 static struct net_device_stats
*fs_enet_get_stats(struct net_device
*dev
)
875 struct fs_enet_private
*fep
= netdev_priv(dev
);
879 /*************************************************************************/
881 static void fs_get_drvinfo(struct net_device
*dev
,
882 struct ethtool_drvinfo
*info
)
884 strcpy(info
->driver
, DRV_MODULE_NAME
);
885 strcpy(info
->version
, DRV_MODULE_VERSION
);
888 static int fs_get_regs_len(struct net_device
*dev
)
890 struct fs_enet_private
*fep
= netdev_priv(dev
);
892 return (*fep
->ops
->get_regs_len
)(dev
);
895 static void fs_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
898 struct fs_enet_private
*fep
= netdev_priv(dev
);
904 spin_lock_irqsave(&fep
->lock
, flags
);
905 r
= (*fep
->ops
->get_regs
)(dev
, p
, &len
);
906 spin_unlock_irqrestore(&fep
->lock
, flags
);
912 static int fs_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
914 struct fs_enet_private
*fep
= netdev_priv(dev
);
919 return phy_ethtool_gset(fep
->phydev
, cmd
);
922 static int fs_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
924 struct fs_enet_private
*fep
= netdev_priv(dev
);
929 return phy_ethtool_sset(fep
->phydev
, cmd
);
932 static int fs_nway_reset(struct net_device
*dev
)
937 static u32
fs_get_msglevel(struct net_device
*dev
)
939 struct fs_enet_private
*fep
= netdev_priv(dev
);
940 return fep
->msg_enable
;
943 static void fs_set_msglevel(struct net_device
*dev
, u32 value
)
945 struct fs_enet_private
*fep
= netdev_priv(dev
);
946 fep
->msg_enable
= value
;
949 static const struct ethtool_ops fs_ethtool_ops
= {
950 .get_drvinfo
= fs_get_drvinfo
,
951 .get_regs_len
= fs_get_regs_len
,
952 .get_settings
= fs_get_settings
,
953 .set_settings
= fs_set_settings
,
954 .nway_reset
= fs_nway_reset
,
955 .get_link
= ethtool_op_get_link
,
956 .get_msglevel
= fs_get_msglevel
,
957 .set_msglevel
= fs_set_msglevel
,
958 .set_tx_csum
= ethtool_op_set_tx_csum
, /* local! */
959 .set_sg
= ethtool_op_set_sg
,
960 .get_regs
= fs_get_regs
,
963 static int fs_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
965 struct fs_enet_private
*fep
= netdev_priv(dev
);
967 if (!netif_running(dev
))
970 return phy_mii_ioctl(fep
->phydev
, rq
, cmd
);
973 extern int fs_mii_connect(struct net_device
*dev
);
974 extern void fs_mii_disconnect(struct net_device
*dev
);
976 /**************************************************************************************/
978 #ifdef CONFIG_FS_ENET_HAS_FEC
979 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
981 #define IS_FEC(match) 0
984 static const struct net_device_ops fs_enet_netdev_ops
= {
985 .ndo_open
= fs_enet_open
,
986 .ndo_stop
= fs_enet_close
,
987 .ndo_get_stats
= fs_enet_get_stats
,
988 .ndo_start_xmit
= fs_enet_start_xmit
,
989 .ndo_tx_timeout
= fs_timeout
,
990 .ndo_set_multicast_list
= fs_set_multicast_list
,
991 .ndo_do_ioctl
= fs_ioctl
,
992 .ndo_validate_addr
= eth_validate_addr
,
993 .ndo_set_mac_address
= eth_mac_addr
,
994 .ndo_change_mtu
= eth_change_mtu
,
995 #ifdef CONFIG_NET_POLL_CONTROLLER
996 .ndo_poll_controller
= fs_enet_netpoll
,
1000 static int __devinit
fs_enet_probe(struct platform_device
*ofdev
,
1001 const struct of_device_id
*match
)
1003 struct net_device
*ndev
;
1004 struct fs_enet_private
*fep
;
1005 struct fs_platform_info
*fpi
;
1008 int privsize
, len
, ret
= -ENODEV
;
1010 fpi
= kzalloc(sizeof(*fpi
), GFP_KERNEL
);
1014 if (!IS_FEC(match
)) {
1015 data
= of_get_property(ofdev
->dev
.of_node
, "fsl,cpm-command", &len
);
1016 if (!data
|| len
!= 4)
1019 fpi
->cp_command
= *data
;
1024 fpi
->rx_copybreak
= 240;
1026 fpi
->napi_weight
= 17;
1027 fpi
->phy_node
= of_parse_phandle(ofdev
->dev
.of_node
, "phy-handle", 0);
1028 if ((!fpi
->phy_node
) && (!of_get_property(ofdev
->dev
.of_node
, "fixed-link",
1032 privsize
= sizeof(*fep
) +
1033 sizeof(struct sk_buff
**) *
1034 (fpi
->rx_ring
+ fpi
->tx_ring
);
1036 ndev
= alloc_etherdev(privsize
);
1042 SET_NETDEV_DEV(ndev
, &ofdev
->dev
);
1043 dev_set_drvdata(&ofdev
->dev
, ndev
);
1045 fep
= netdev_priv(ndev
);
1046 fep
->dev
= &ofdev
->dev
;
1049 fep
->ops
= match
->data
;
1051 ret
= fep
->ops
->setup_data(ndev
);
1055 fep
->rx_skbuff
= (struct sk_buff
**)&fep
[1];
1056 fep
->tx_skbuff
= fep
->rx_skbuff
+ fpi
->rx_ring
;
1058 spin_lock_init(&fep
->lock
);
1059 spin_lock_init(&fep
->tx_lock
);
1061 mac_addr
= of_get_mac_address(ofdev
->dev
.of_node
);
1063 memcpy(ndev
->dev_addr
, mac_addr
, 6);
1065 ret
= fep
->ops
->allocate_bd(ndev
);
1067 goto out_cleanup_data
;
1069 fep
->rx_bd_base
= fep
->ring_base
;
1070 fep
->tx_bd_base
= fep
->rx_bd_base
+ fpi
->rx_ring
;
1072 fep
->tx_ring
= fpi
->tx_ring
;
1073 fep
->rx_ring
= fpi
->rx_ring
;
1075 ndev
->netdev_ops
= &fs_enet_netdev_ops
;
1076 ndev
->watchdog_timeo
= 2 * HZ
;
1078 netif_napi_add(ndev
, &fep
->napi
, fs_enet_rx_napi
,
1081 ndev
->ethtool_ops
= &fs_ethtool_ops
;
1083 init_timer(&fep
->phy_timer_list
);
1085 netif_carrier_off(ndev
);
1087 ret
= register_netdev(ndev
);
1091 pr_info("%s: fs_enet: %pM\n", ndev
->name
, ndev
->dev_addr
);
1096 fep
->ops
->free_bd(ndev
);
1098 fep
->ops
->cleanup_data(ndev
);
1101 dev_set_drvdata(&ofdev
->dev
, NULL
);
1103 of_node_put(fpi
->phy_node
);
1109 static int fs_enet_remove(struct platform_device
*ofdev
)
1111 struct net_device
*ndev
= dev_get_drvdata(&ofdev
->dev
);
1112 struct fs_enet_private
*fep
= netdev_priv(ndev
);
1114 unregister_netdev(ndev
);
1116 fep
->ops
->free_bd(ndev
);
1117 fep
->ops
->cleanup_data(ndev
);
1118 dev_set_drvdata(fep
->dev
, NULL
);
1119 of_node_put(fep
->fpi
->phy_node
);
1124 static struct of_device_id fs_enet_match
[] = {
1125 #ifdef CONFIG_FS_ENET_HAS_SCC
1127 .compatible
= "fsl,cpm1-scc-enet",
1128 .data
= (void *)&fs_scc_ops
,
1131 .compatible
= "fsl,cpm2-scc-enet",
1132 .data
= (void *)&fs_scc_ops
,
1135 #ifdef CONFIG_FS_ENET_HAS_FCC
1137 .compatible
= "fsl,cpm2-fcc-enet",
1138 .data
= (void *)&fs_fcc_ops
,
1141 #ifdef CONFIG_FS_ENET_HAS_FEC
1142 #ifdef CONFIG_FS_ENET_MPC5121_FEC
1144 .compatible
= "fsl,mpc5121-fec",
1145 .data
= (void *)&fs_fec_ops
,
1149 .compatible
= "fsl,pq1-fec-enet",
1150 .data
= (void *)&fs_fec_ops
,
1156 MODULE_DEVICE_TABLE(of
, fs_enet_match
);
1158 static struct of_platform_driver fs_enet_driver
= {
1160 .owner
= THIS_MODULE
,
1162 .of_match_table
= fs_enet_match
,
1164 .probe
= fs_enet_probe
,
1165 .remove
= fs_enet_remove
,
1168 static int __init
fs_init(void)
1170 return of_register_platform_driver(&fs_enet_driver
);
1173 static void __exit
fs_cleanup(void)
1175 of_unregister_platform_driver(&fs_enet_driver
);
1178 #ifdef CONFIG_NET_POLL_CONTROLLER
1179 static void fs_enet_netpoll(struct net_device
*dev
)
1181 disable_irq(dev
->irq
);
1182 fs_enet_interrupt(dev
->irq
, dev
);
1183 enable_irq(dev
->irq
);
1187 /**************************************************************************************/
1189 module_init(fs_init
);
1190 module_exit(fs_cleanup
);