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/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/bitops.h>
36 #include <linux/platform_device.h>
37 #include <linux/phy.h>
39 #include <linux/of_mdio.h>
40 #include <linux/of_platform.h>
41 #include <linux/of_gpio.h>
42 #include <linux/of_net.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
= netdev_alloc_skb(dev
, 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
= netdev_alloc_skb(dev
, 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
);
179 fep
->stats
.rx_dropped
++;
184 fep
->rx_skbuff
[curidx
] = skbn
;
185 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
186 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
189 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
192 * Update BD pointer to next entry.
194 if ((sc
& BD_ENET_RX_WRAP
) == 0)
197 bdp
= fep
->rx_bd_base
;
199 (*fep
->ops
->rx_bd_done
)(dev
);
201 if (received
>= budget
)
207 if (received
< budget
) {
210 (*fep
->ops
->napi_enable_rx
)(dev
);
215 /* non NAPI receive function */
216 static int fs_enet_rx_non_napi(struct net_device
*dev
)
218 struct fs_enet_private
*fep
= netdev_priv(dev
);
219 const struct fs_platform_info
*fpi
= fep
->fpi
;
221 struct sk_buff
*skb
, *skbn
, *skbt
;
226 * First, grab all of the stats for the incoming packet.
227 * These get messed up if we get called due to a busy condition.
231 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
233 curidx
= bdp
- fep
->rx_bd_base
;
236 * Since we have allocated space to hold a complete frame,
237 * the last indicator should be set.
239 if ((sc
& BD_ENET_RX_LAST
) == 0)
240 dev_warn(fep
->dev
, "rcv is not +last\n");
245 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_CL
|
246 BD_ENET_RX_NO
| BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
247 fep
->stats
.rx_errors
++;
248 /* Frame too long or too short. */
249 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
))
250 fep
->stats
.rx_length_errors
++;
251 /* Frame alignment */
252 if (sc
& (BD_ENET_RX_NO
| BD_ENET_RX_CL
))
253 fep
->stats
.rx_frame_errors
++;
255 if (sc
& BD_ENET_RX_CR
)
256 fep
->stats
.rx_crc_errors
++;
258 if (sc
& BD_ENET_RX_OV
)
259 fep
->stats
.rx_crc_errors
++;
261 skb
= fep
->rx_skbuff
[curidx
];
263 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
264 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
271 skb
= fep
->rx_skbuff
[curidx
];
273 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
274 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
278 * Process the incoming frame.
280 fep
->stats
.rx_packets
++;
281 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
282 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
284 if (pkt_len
<= fpi
->rx_copybreak
) {
285 /* +2 to make IP header L1 cache aligned */
286 skbn
= netdev_alloc_skb(dev
, pkt_len
+ 2);
288 skb_reserve(skbn
, 2); /* align IP header */
289 skb_copy_from_linear_data(skb
,
290 skbn
->data
, pkt_len
);
297 skbn
= netdev_alloc_skb(dev
, ENET_RX_FRSIZE
);
300 skb_align(skbn
, ENET_RX_ALIGN
);
304 skb_put(skb
, pkt_len
); /* Make room */
305 skb
->protocol
= eth_type_trans(skb
, dev
);
309 fep
->stats
.rx_dropped
++;
314 fep
->rx_skbuff
[curidx
] = skbn
;
315 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
316 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
319 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
322 * Update BD pointer to next entry.
324 if ((sc
& BD_ENET_RX_WRAP
) == 0)
327 bdp
= fep
->rx_bd_base
;
329 (*fep
->ops
->rx_bd_done
)(dev
);
337 static void fs_enet_tx(struct net_device
*dev
)
339 struct fs_enet_private
*fep
= netdev_priv(dev
);
342 int dirtyidx
, do_wake
, do_restart
;
345 spin_lock(&fep
->tx_lock
);
348 do_wake
= do_restart
= 0;
349 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_TX_READY
) == 0) {
350 dirtyidx
= bdp
- fep
->tx_bd_base
;
352 if (fep
->tx_free
== fep
->tx_ring
)
355 skb
= fep
->tx_skbuff
[dirtyidx
];
360 if (sc
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
361 BD_ENET_TX_RL
| BD_ENET_TX_UN
| BD_ENET_TX_CSL
)) {
363 if (sc
& BD_ENET_TX_HB
) /* No heartbeat */
364 fep
->stats
.tx_heartbeat_errors
++;
365 if (sc
& BD_ENET_TX_LC
) /* Late collision */
366 fep
->stats
.tx_window_errors
++;
367 if (sc
& BD_ENET_TX_RL
) /* Retrans limit */
368 fep
->stats
.tx_aborted_errors
++;
369 if (sc
& BD_ENET_TX_UN
) /* Underrun */
370 fep
->stats
.tx_fifo_errors
++;
371 if (sc
& BD_ENET_TX_CSL
) /* Carrier lost */
372 fep
->stats
.tx_carrier_errors
++;
374 if (sc
& (BD_ENET_TX_LC
| BD_ENET_TX_RL
| BD_ENET_TX_UN
)) {
375 fep
->stats
.tx_errors
++;
379 fep
->stats
.tx_packets
++;
381 if (sc
& BD_ENET_TX_READY
) {
383 "HEY! Enet xmit interrupt and TX_READY.\n");
387 * Deferred means some collisions occurred during transmit,
388 * but we eventually sent the packet OK.
390 if (sc
& BD_ENET_TX_DEF
)
391 fep
->stats
.collisions
++;
394 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
395 skb
->len
, DMA_TO_DEVICE
);
398 * Free the sk buffer associated with this last transmit.
400 dev_kfree_skb_irq(skb
);
401 fep
->tx_skbuff
[dirtyidx
] = NULL
;
404 * Update pointer to next buffer descriptor to be transmitted.
406 if ((sc
& BD_ENET_TX_WRAP
) == 0)
409 bdp
= fep
->tx_bd_base
;
412 * Since we have freed up a buffer, the ring is no longer
422 (*fep
->ops
->tx_restart
)(dev
);
424 spin_unlock(&fep
->tx_lock
);
427 netif_wake_queue(dev
);
431 * The interrupt handler.
432 * This is called from the MPC core interrupt.
435 fs_enet_interrupt(int irq
, void *dev_id
)
437 struct net_device
*dev
= dev_id
;
438 struct fs_enet_private
*fep
;
439 const struct fs_platform_info
*fpi
;
445 fep
= netdev_priv(dev
);
449 while ((int_events
= (*fep
->ops
->get_int_events
)(dev
)) != 0) {
452 int_clr_events
= int_events
;
454 int_clr_events
&= ~fep
->ev_napi_rx
;
456 (*fep
->ops
->clear_int_events
)(dev
, int_clr_events
);
458 if (int_events
& fep
->ev_err
)
459 (*fep
->ops
->ev_error
)(dev
, int_events
);
461 if (int_events
& fep
->ev_rx
) {
463 fs_enet_rx_non_napi(dev
);
465 napi_ok
= napi_schedule_prep(&fep
->napi
);
467 (*fep
->ops
->napi_disable_rx
)(dev
);
468 (*fep
->ops
->clear_int_events
)(dev
, fep
->ev_napi_rx
);
470 /* NOTE: it is possible for FCCs in NAPI mode */
471 /* to submit a spurious interrupt while in poll */
473 __napi_schedule(&fep
->napi
);
477 if (int_events
& fep
->ev_tx
)
482 return IRQ_RETVAL(handled
);
485 void fs_init_bds(struct net_device
*dev
)
487 struct fs_enet_private
*fep
= netdev_priv(dev
);
494 fep
->dirty_tx
= fep
->cur_tx
= fep
->tx_bd_base
;
495 fep
->tx_free
= fep
->tx_ring
;
496 fep
->cur_rx
= fep
->rx_bd_base
;
499 * Initialize the receive buffer descriptors.
501 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
502 skb
= netdev_alloc_skb(dev
, ENET_RX_FRSIZE
);
506 skb_align(skb
, ENET_RX_ALIGN
);
507 fep
->rx_skbuff
[i
] = skb
;
509 dma_map_single(fep
->dev
, skb
->data
,
510 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
512 CBDW_DATLEN(bdp
, 0); /* zero */
513 CBDW_SC(bdp
, BD_ENET_RX_EMPTY
|
514 ((i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
));
517 * if we failed, fillup remainder
519 for (; i
< fep
->rx_ring
; i
++, bdp
++) {
520 fep
->rx_skbuff
[i
] = NULL
;
521 CBDW_SC(bdp
, (i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
);
525 * ...and the same for transmit.
527 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
528 fep
->tx_skbuff
[i
] = NULL
;
529 CBDW_BUFADDR(bdp
, 0);
531 CBDW_SC(bdp
, (i
< fep
->tx_ring
- 1) ? 0 : BD_SC_WRAP
);
535 void fs_cleanup_bds(struct net_device
*dev
)
537 struct fs_enet_private
*fep
= netdev_priv(dev
);
543 * Reset SKB transmit buffers.
545 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
546 if ((skb
= fep
->tx_skbuff
[i
]) == NULL
)
550 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
551 skb
->len
, DMA_TO_DEVICE
);
553 fep
->tx_skbuff
[i
] = NULL
;
558 * Reset SKB receive buffers
560 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
561 if ((skb
= fep
->rx_skbuff
[i
]) == NULL
)
565 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
566 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
569 fep
->rx_skbuff
[i
] = NULL
;
575 /**********************************************************************************/
577 #ifdef CONFIG_FS_ENET_MPC5121_FEC
579 * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
581 static struct sk_buff
*tx_skb_align_workaround(struct net_device
*dev
,
584 struct sk_buff
*new_skb
;
587 new_skb
= netdev_alloc_skb(dev
, skb
->len
+ 4);
591 /* Make sure new skb is properly aligned */
592 skb_align(new_skb
, 4);
594 /* Copy data to new skb ... */
595 skb_copy_from_linear_data(skb
, new_skb
->data
, skb
->len
);
596 skb_put(new_skb
, skb
->len
);
598 /* ... and free an old one */
599 dev_kfree_skb_any(skb
);
605 static int fs_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
607 struct fs_enet_private
*fep
= netdev_priv(dev
);
613 #ifdef CONFIG_FS_ENET_MPC5121_FEC
614 if (((unsigned long)skb
->data
) & 0x3) {
615 skb
= tx_skb_align_workaround(dev
, skb
);
618 * We have lost packet due to memory allocation error
619 * in tx_skb_align_workaround(). Hopefully original
620 * skb is still valid, so try transmit it later.
622 return NETDEV_TX_BUSY
;
626 spin_lock_irqsave(&fep
->tx_lock
, flags
);
629 * Fill in a Tx ring entry
633 if (!fep
->tx_free
|| (CBDR_SC(bdp
) & BD_ENET_TX_READY
)) {
634 netif_stop_queue(dev
);
635 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
638 * Ooops. All transmit buffers are full. Bail out.
639 * This should not happen, since the tx queue should be stopped.
641 dev_warn(fep
->dev
, "tx queue full!.\n");
642 return NETDEV_TX_BUSY
;
645 curidx
= bdp
- fep
->tx_bd_base
;
647 * Clear all of the status flags.
649 CBDC_SC(bdp
, BD_ENET_TX_STATS
);
654 fep
->tx_skbuff
[curidx
] = skb
;
656 fep
->stats
.tx_bytes
+= skb
->len
;
659 * Push the data cache so the CPM does not get stale memory data.
661 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
,
662 skb
->data
, skb
->len
, DMA_TO_DEVICE
));
663 CBDW_DATLEN(bdp
, skb
->len
);
666 * If this was the last BD in the ring, start at the beginning again.
668 if ((CBDR_SC(bdp
) & BD_ENET_TX_WRAP
) == 0)
671 fep
->cur_tx
= fep
->tx_bd_base
;
674 netif_stop_queue(dev
);
676 /* Trigger transmission start */
677 sc
= BD_ENET_TX_READY
| BD_ENET_TX_INTR
|
678 BD_ENET_TX_LAST
| BD_ENET_TX_TC
;
680 /* note that while FEC does not have this bit
681 * it marks it as available for software use
682 * yay for hw reuse :) */
684 sc
|= BD_ENET_TX_PAD
;
687 skb_tx_timestamp(skb
);
689 (*fep
->ops
->tx_kickstart
)(dev
);
691 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
696 static void fs_timeout(struct net_device
*dev
)
698 struct fs_enet_private
*fep
= netdev_priv(dev
);
702 fep
->stats
.tx_errors
++;
704 spin_lock_irqsave(&fep
->lock
, flags
);
706 if (dev
->flags
& IFF_UP
) {
707 phy_stop(fep
->phydev
);
708 (*fep
->ops
->stop
)(dev
);
709 (*fep
->ops
->restart
)(dev
);
710 phy_start(fep
->phydev
);
713 phy_start(fep
->phydev
);
714 wake
= fep
->tx_free
&& !(CBDR_SC(fep
->cur_tx
) & BD_ENET_TX_READY
);
715 spin_unlock_irqrestore(&fep
->lock
, flags
);
718 netif_wake_queue(dev
);
721 /*-----------------------------------------------------------------------------
722 * generic link-change handler - should be sufficient for most cases
723 *-----------------------------------------------------------------------------*/
724 static void generic_adjust_link(struct net_device
*dev
)
726 struct fs_enet_private
*fep
= netdev_priv(dev
);
727 struct phy_device
*phydev
= fep
->phydev
;
731 /* adjust to duplex mode */
732 if (phydev
->duplex
!= fep
->oldduplex
) {
734 fep
->oldduplex
= phydev
->duplex
;
737 if (phydev
->speed
!= fep
->oldspeed
) {
739 fep
->oldspeed
= phydev
->speed
;
748 fep
->ops
->restart(dev
);
749 } else if (fep
->oldlink
) {
756 if (new_state
&& netif_msg_link(fep
))
757 phy_print_status(phydev
);
761 static void fs_adjust_link(struct net_device
*dev
)
763 struct fs_enet_private
*fep
= netdev_priv(dev
);
766 spin_lock_irqsave(&fep
->lock
, flags
);
768 if(fep
->ops
->adjust_link
)
769 fep
->ops
->adjust_link(dev
);
771 generic_adjust_link(dev
);
773 spin_unlock_irqrestore(&fep
->lock
, flags
);
776 static int fs_init_phy(struct net_device
*dev
)
778 struct fs_enet_private
*fep
= netdev_priv(dev
);
779 struct phy_device
*phydev
;
780 phy_interface_t iface
;
786 iface
= fep
->fpi
->use_rmii
?
787 PHY_INTERFACE_MODE_RMII
: PHY_INTERFACE_MODE_MII
;
789 phydev
= of_phy_connect(dev
, fep
->fpi
->phy_node
, &fs_adjust_link
, 0,
792 phydev
= of_phy_connect_fixed_link(dev
, &fs_adjust_link
,
796 dev_err(&dev
->dev
, "Could not attach to PHY\n");
800 fep
->phydev
= phydev
;
805 static int fs_enet_open(struct net_device
*dev
)
807 struct fs_enet_private
*fep
= netdev_priv(dev
);
811 /* to initialize the fep->cur_rx,... */
812 /* not doing this, will cause a crash in fs_enet_rx_napi */
813 fs_init_bds(fep
->ndev
);
815 if (fep
->fpi
->use_napi
)
816 napi_enable(&fep
->napi
);
818 /* Install our interrupt handler. */
819 r
= request_irq(fep
->interrupt
, fs_enet_interrupt
, IRQF_SHARED
,
822 dev_err(fep
->dev
, "Could not allocate FS_ENET IRQ!");
823 if (fep
->fpi
->use_napi
)
824 napi_disable(&fep
->napi
);
828 err
= fs_init_phy(dev
);
830 free_irq(fep
->interrupt
, dev
);
831 if (fep
->fpi
->use_napi
)
832 napi_disable(&fep
->napi
);
835 phy_start(fep
->phydev
);
837 netif_start_queue(dev
);
842 static int fs_enet_close(struct net_device
*dev
)
844 struct fs_enet_private
*fep
= netdev_priv(dev
);
847 netif_stop_queue(dev
);
848 netif_carrier_off(dev
);
849 if (fep
->fpi
->use_napi
)
850 napi_disable(&fep
->napi
);
851 phy_stop(fep
->phydev
);
853 spin_lock_irqsave(&fep
->lock
, flags
);
854 spin_lock(&fep
->tx_lock
);
855 (*fep
->ops
->stop
)(dev
);
856 spin_unlock(&fep
->tx_lock
);
857 spin_unlock_irqrestore(&fep
->lock
, flags
);
859 /* release any irqs */
860 phy_disconnect(fep
->phydev
);
862 free_irq(fep
->interrupt
, dev
);
867 static struct net_device_stats
*fs_enet_get_stats(struct net_device
*dev
)
869 struct fs_enet_private
*fep
= netdev_priv(dev
);
873 /*************************************************************************/
875 static void fs_get_drvinfo(struct net_device
*dev
,
876 struct ethtool_drvinfo
*info
)
878 strlcpy(info
->driver
, DRV_MODULE_NAME
, sizeof(info
->driver
));
879 strlcpy(info
->version
, DRV_MODULE_VERSION
, sizeof(info
->version
));
882 static int fs_get_regs_len(struct net_device
*dev
)
884 struct fs_enet_private
*fep
= netdev_priv(dev
);
886 return (*fep
->ops
->get_regs_len
)(dev
);
889 static void fs_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
892 struct fs_enet_private
*fep
= netdev_priv(dev
);
898 spin_lock_irqsave(&fep
->lock
, flags
);
899 r
= (*fep
->ops
->get_regs
)(dev
, p
, &len
);
900 spin_unlock_irqrestore(&fep
->lock
, flags
);
906 static int fs_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
908 struct fs_enet_private
*fep
= netdev_priv(dev
);
913 return phy_ethtool_gset(fep
->phydev
, cmd
);
916 static int fs_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
918 struct fs_enet_private
*fep
= netdev_priv(dev
);
923 return phy_ethtool_sset(fep
->phydev
, cmd
);
926 static int fs_nway_reset(struct net_device
*dev
)
931 static u32
fs_get_msglevel(struct net_device
*dev
)
933 struct fs_enet_private
*fep
= netdev_priv(dev
);
934 return fep
->msg_enable
;
937 static void fs_set_msglevel(struct net_device
*dev
, u32 value
)
939 struct fs_enet_private
*fep
= netdev_priv(dev
);
940 fep
->msg_enable
= value
;
943 static const struct ethtool_ops fs_ethtool_ops
= {
944 .get_drvinfo
= fs_get_drvinfo
,
945 .get_regs_len
= fs_get_regs_len
,
946 .get_settings
= fs_get_settings
,
947 .set_settings
= fs_set_settings
,
948 .nway_reset
= fs_nway_reset
,
949 .get_link
= ethtool_op_get_link
,
950 .get_msglevel
= fs_get_msglevel
,
951 .set_msglevel
= fs_set_msglevel
,
952 .get_regs
= fs_get_regs
,
953 .get_ts_info
= ethtool_op_get_ts_info
,
956 static int fs_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
958 struct fs_enet_private
*fep
= netdev_priv(dev
);
960 if (!netif_running(dev
))
963 return phy_mii_ioctl(fep
->phydev
, rq
, cmd
);
966 extern int fs_mii_connect(struct net_device
*dev
);
967 extern void fs_mii_disconnect(struct net_device
*dev
);
969 /**************************************************************************************/
971 #ifdef CONFIG_FS_ENET_HAS_FEC
972 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
974 #define IS_FEC(match) 0
977 static const struct net_device_ops fs_enet_netdev_ops
= {
978 .ndo_open
= fs_enet_open
,
979 .ndo_stop
= fs_enet_close
,
980 .ndo_get_stats
= fs_enet_get_stats
,
981 .ndo_start_xmit
= fs_enet_start_xmit
,
982 .ndo_tx_timeout
= fs_timeout
,
983 .ndo_set_rx_mode
= fs_set_multicast_list
,
984 .ndo_do_ioctl
= fs_ioctl
,
985 .ndo_validate_addr
= eth_validate_addr
,
986 .ndo_set_mac_address
= eth_mac_addr
,
987 .ndo_change_mtu
= eth_change_mtu
,
988 #ifdef CONFIG_NET_POLL_CONTROLLER
989 .ndo_poll_controller
= fs_enet_netpoll
,
993 static struct of_device_id fs_enet_match
[];
994 static int fs_enet_probe(struct platform_device
*ofdev
)
996 const struct of_device_id
*match
;
997 struct net_device
*ndev
;
998 struct fs_enet_private
*fep
;
999 struct fs_platform_info
*fpi
;
1004 const char *phy_connection_type
;
1005 int privsize
, len
, ret
= -ENODEV
;
1007 match
= of_match_device(fs_enet_match
, &ofdev
->dev
);
1011 fpi
= kzalloc(sizeof(*fpi
), GFP_KERNEL
);
1015 if (!IS_FEC(match
)) {
1016 data
= of_get_property(ofdev
->dev
.of_node
, "fsl,cpm-command", &len
);
1017 if (!data
|| len
!= 4)
1020 fpi
->cp_command
= *data
;
1025 fpi
->rx_copybreak
= 240;
1027 fpi
->napi_weight
= 17;
1028 fpi
->phy_node
= of_parse_phandle(ofdev
->dev
.of_node
, "phy-handle", 0);
1029 if ((!fpi
->phy_node
) && (!of_get_property(ofdev
->dev
.of_node
, "fixed-link",
1033 if (of_device_is_compatible(ofdev
->dev
.of_node
, "fsl,mpc5125-fec")) {
1034 phy_connection_type
= of_get_property(ofdev
->dev
.of_node
,
1035 "phy-connection-type", NULL
);
1036 if (phy_connection_type
&& !strcmp("rmii", phy_connection_type
))
1040 /* make clock lookup non-fatal (the driver is shared among platforms),
1041 * but require enable to succeed when a clock was specified/found,
1042 * keep a reference to the clock upon successful acquisition
1044 clk
= devm_clk_get(&ofdev
->dev
, "per");
1046 err
= clk_prepare_enable(clk
);
1054 privsize
= sizeof(*fep
) +
1055 sizeof(struct sk_buff
**) *
1056 (fpi
->rx_ring
+ fpi
->tx_ring
);
1058 ndev
= alloc_etherdev(privsize
);
1064 SET_NETDEV_DEV(ndev
, &ofdev
->dev
);
1065 platform_set_drvdata(ofdev
, ndev
);
1067 fep
= netdev_priv(ndev
);
1068 fep
->dev
= &ofdev
->dev
;
1071 fep
->ops
= match
->data
;
1073 ret
= fep
->ops
->setup_data(ndev
);
1077 fep
->rx_skbuff
= (struct sk_buff
**)&fep
[1];
1078 fep
->tx_skbuff
= fep
->rx_skbuff
+ fpi
->rx_ring
;
1080 spin_lock_init(&fep
->lock
);
1081 spin_lock_init(&fep
->tx_lock
);
1083 mac_addr
= of_get_mac_address(ofdev
->dev
.of_node
);
1085 memcpy(ndev
->dev_addr
, mac_addr
, ETH_ALEN
);
1087 ret
= fep
->ops
->allocate_bd(ndev
);
1089 goto out_cleanup_data
;
1091 fep
->rx_bd_base
= fep
->ring_base
;
1092 fep
->tx_bd_base
= fep
->rx_bd_base
+ fpi
->rx_ring
;
1094 fep
->tx_ring
= fpi
->tx_ring
;
1095 fep
->rx_ring
= fpi
->rx_ring
;
1097 ndev
->netdev_ops
= &fs_enet_netdev_ops
;
1098 ndev
->watchdog_timeo
= 2 * HZ
;
1100 netif_napi_add(ndev
, &fep
->napi
, fs_enet_rx_napi
,
1103 ndev
->ethtool_ops
= &fs_ethtool_ops
;
1105 init_timer(&fep
->phy_timer_list
);
1107 netif_carrier_off(ndev
);
1109 ret
= register_netdev(ndev
);
1113 pr_info("%s: fs_enet: %pM\n", ndev
->name
, ndev
->dev_addr
);
1118 fep
->ops
->free_bd(ndev
);
1120 fep
->ops
->cleanup_data(ndev
);
1124 of_node_put(fpi
->phy_node
);
1126 clk_disable_unprepare(fpi
->clk_per
);
1132 static int fs_enet_remove(struct platform_device
*ofdev
)
1134 struct net_device
*ndev
= platform_get_drvdata(ofdev
);
1135 struct fs_enet_private
*fep
= netdev_priv(ndev
);
1137 unregister_netdev(ndev
);
1139 fep
->ops
->free_bd(ndev
);
1140 fep
->ops
->cleanup_data(ndev
);
1141 dev_set_drvdata(fep
->dev
, NULL
);
1142 of_node_put(fep
->fpi
->phy_node
);
1143 if (fep
->fpi
->clk_per
)
1144 clk_disable_unprepare(fep
->fpi
->clk_per
);
1149 static struct of_device_id fs_enet_match
[] = {
1150 #ifdef CONFIG_FS_ENET_HAS_SCC
1152 .compatible
= "fsl,cpm1-scc-enet",
1153 .data
= (void *)&fs_scc_ops
,
1156 .compatible
= "fsl,cpm2-scc-enet",
1157 .data
= (void *)&fs_scc_ops
,
1160 #ifdef CONFIG_FS_ENET_HAS_FCC
1162 .compatible
= "fsl,cpm2-fcc-enet",
1163 .data
= (void *)&fs_fcc_ops
,
1166 #ifdef CONFIG_FS_ENET_HAS_FEC
1167 #ifdef CONFIG_FS_ENET_MPC5121_FEC
1169 .compatible
= "fsl,mpc5121-fec",
1170 .data
= (void *)&fs_fec_ops
,
1173 .compatible
= "fsl,mpc5125-fec",
1174 .data
= (void *)&fs_fec_ops
,
1178 .compatible
= "fsl,pq1-fec-enet",
1179 .data
= (void *)&fs_fec_ops
,
1185 MODULE_DEVICE_TABLE(of
, fs_enet_match
);
1187 static struct platform_driver fs_enet_driver
= {
1189 .owner
= THIS_MODULE
,
1191 .of_match_table
= fs_enet_match
,
1193 .probe
= fs_enet_probe
,
1194 .remove
= fs_enet_remove
,
1197 #ifdef CONFIG_NET_POLL_CONTROLLER
1198 static void fs_enet_netpoll(struct net_device
*dev
)
1200 disable_irq(dev
->irq
);
1201 fs_enet_interrupt(dev
->irq
, dev
);
1202 enable_irq(dev
->irq
);
1206 module_platform_driver(fs_enet_driver
);