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/vmalloc.h>
41 #include <asm/pgtable.h>
43 #include <asm/uaccess.h>
45 #ifdef CONFIG_PPC_CPM_NEW_BINDING
46 #include <asm/of_platform.h>
51 /*************************************************/
53 #ifndef CONFIG_PPC_CPM_NEW_BINDING
54 static char version
[] __devinitdata
=
55 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")" "\n";
58 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
59 MODULE_DESCRIPTION("Freescale Ethernet Driver");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(DRV_MODULE_VERSION
);
63 static int fs_enet_debug
= -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
64 module_param(fs_enet_debug
, int, 0);
65 MODULE_PARM_DESC(fs_enet_debug
,
66 "Freescale bitmapped debugging message enable value");
68 #ifdef CONFIG_NET_POLL_CONTROLLER
69 static void fs_enet_netpoll(struct net_device
*dev
);
72 static void fs_set_multicast_list(struct net_device
*dev
)
74 struct fs_enet_private
*fep
= netdev_priv(dev
);
76 (*fep
->ops
->set_multicast_list
)(dev
);
79 static void skb_align(struct sk_buff
*skb
, int align
)
81 int off
= ((unsigned long)skb
->data
) & (align
- 1);
84 skb_reserve(skb
, align
- off
);
87 /* NAPI receive function */
88 static int fs_enet_rx_napi(struct napi_struct
*napi
, int budget
)
90 struct fs_enet_private
*fep
= container_of(napi
, struct fs_enet_private
, napi
);
91 struct net_device
*dev
= fep
->ndev
;
92 const struct fs_platform_info
*fpi
= fep
->fpi
;
94 struct sk_buff
*skb
, *skbn
, *skbt
;
100 * First, grab all of the stats for the incoming packet.
101 * These get messed up if we get called due to a busy condition.
105 /* clear RX status bits for napi*/
106 (*fep
->ops
->napi_clear_rx_event
)(dev
);
108 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
109 curidx
= bdp
- fep
->rx_bd_base
;
112 * Since we have allocated space to hold a complete frame,
113 * the last indicator should be set.
115 if ((sc
& BD_ENET_RX_LAST
) == 0)
116 printk(KERN_WARNING DRV_MODULE_NAME
117 ": %s rcv is not +last\n",
123 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_CL
|
124 BD_ENET_RX_NO
| BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
125 fep
->stats
.rx_errors
++;
126 /* Frame too long or too short. */
127 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
))
128 fep
->stats
.rx_length_errors
++;
129 /* Frame alignment */
130 if (sc
& (BD_ENET_RX_NO
| BD_ENET_RX_CL
))
131 fep
->stats
.rx_frame_errors
++;
133 if (sc
& BD_ENET_RX_CR
)
134 fep
->stats
.rx_crc_errors
++;
136 if (sc
& BD_ENET_RX_OV
)
137 fep
->stats
.rx_crc_errors
++;
139 skb
= fep
->rx_skbuff
[curidx
];
141 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
142 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
148 skb
= fep
->rx_skbuff
[curidx
];
150 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
151 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
155 * Process the incoming frame.
157 fep
->stats
.rx_packets
++;
158 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
159 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
161 if (pkt_len
<= fpi
->rx_copybreak
) {
162 /* +2 to make IP header L1 cache aligned */
163 skbn
= dev_alloc_skb(pkt_len
+ 2);
165 skb_reserve(skbn
, 2); /* align IP header */
166 skb_copy_from_linear_data(skb
,
167 skbn
->data
, pkt_len
);
174 skbn
= dev_alloc_skb(ENET_RX_FRSIZE
);
177 skb_align(skbn
, ENET_RX_ALIGN
);
181 skb_put(skb
, pkt_len
); /* Make room */
182 skb
->protocol
= eth_type_trans(skb
, dev
);
184 netif_receive_skb(skb
);
186 printk(KERN_WARNING DRV_MODULE_NAME
187 ": %s Memory squeeze, dropping packet.\n",
189 fep
->stats
.rx_dropped
++;
194 fep
->rx_skbuff
[curidx
] = skbn
;
195 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
196 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
199 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
202 * Update BD pointer to next entry.
204 if ((sc
& BD_ENET_RX_WRAP
) == 0)
207 bdp
= fep
->rx_bd_base
;
209 (*fep
->ops
->rx_bd_done
)(dev
);
211 if (received
>= budget
)
217 if (received
< budget
) {
219 netif_rx_complete(dev
, napi
);
220 (*fep
->ops
->napi_enable_rx
)(dev
);
225 /* non NAPI receive function */
226 static int fs_enet_rx_non_napi(struct net_device
*dev
)
228 struct fs_enet_private
*fep
= netdev_priv(dev
);
229 const struct fs_platform_info
*fpi
= fep
->fpi
;
231 struct sk_buff
*skb
, *skbn
, *skbt
;
236 * First, grab all of the stats for the incoming packet.
237 * These get messed up if we get called due to a busy condition.
241 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
243 curidx
= bdp
- fep
->rx_bd_base
;
246 * Since we have allocated space to hold a complete frame,
247 * the last indicator should be set.
249 if ((sc
& BD_ENET_RX_LAST
) == 0)
250 printk(KERN_WARNING DRV_MODULE_NAME
251 ": %s rcv is not +last\n",
257 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_CL
|
258 BD_ENET_RX_NO
| BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
259 fep
->stats
.rx_errors
++;
260 /* Frame too long or too short. */
261 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
))
262 fep
->stats
.rx_length_errors
++;
263 /* Frame alignment */
264 if (sc
& (BD_ENET_RX_NO
| BD_ENET_RX_CL
))
265 fep
->stats
.rx_frame_errors
++;
267 if (sc
& BD_ENET_RX_CR
)
268 fep
->stats
.rx_crc_errors
++;
270 if (sc
& BD_ENET_RX_OV
)
271 fep
->stats
.rx_crc_errors
++;
273 skb
= fep
->rx_skbuff
[curidx
];
275 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
276 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
283 skb
= fep
->rx_skbuff
[curidx
];
285 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
286 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
290 * Process the incoming frame.
292 fep
->stats
.rx_packets
++;
293 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
294 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
296 if (pkt_len
<= fpi
->rx_copybreak
) {
297 /* +2 to make IP header L1 cache aligned */
298 skbn
= dev_alloc_skb(pkt_len
+ 2);
300 skb_reserve(skbn
, 2); /* align IP header */
301 skb_copy_from_linear_data(skb
,
302 skbn
->data
, pkt_len
);
309 skbn
= dev_alloc_skb(ENET_RX_FRSIZE
);
312 skb_align(skbn
, ENET_RX_ALIGN
);
316 skb_put(skb
, pkt_len
); /* Make room */
317 skb
->protocol
= eth_type_trans(skb
, dev
);
321 printk(KERN_WARNING DRV_MODULE_NAME
322 ": %s Memory squeeze, dropping packet.\n",
324 fep
->stats
.rx_dropped
++;
329 fep
->rx_skbuff
[curidx
] = skbn
;
330 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
331 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
334 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
337 * Update BD pointer to next entry.
339 if ((sc
& BD_ENET_RX_WRAP
) == 0)
342 bdp
= fep
->rx_bd_base
;
344 (*fep
->ops
->rx_bd_done
)(dev
);
352 static void fs_enet_tx(struct net_device
*dev
)
354 struct fs_enet_private
*fep
= netdev_priv(dev
);
357 int dirtyidx
, do_wake
, do_restart
;
360 spin_lock(&fep
->tx_lock
);
363 do_wake
= do_restart
= 0;
364 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_TX_READY
) == 0) {
365 dirtyidx
= bdp
- fep
->tx_bd_base
;
367 if (fep
->tx_free
== fep
->tx_ring
)
370 skb
= fep
->tx_skbuff
[dirtyidx
];
375 if (sc
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
376 BD_ENET_TX_RL
| BD_ENET_TX_UN
| BD_ENET_TX_CSL
)) {
378 if (sc
& BD_ENET_TX_HB
) /* No heartbeat */
379 fep
->stats
.tx_heartbeat_errors
++;
380 if (sc
& BD_ENET_TX_LC
) /* Late collision */
381 fep
->stats
.tx_window_errors
++;
382 if (sc
& BD_ENET_TX_RL
) /* Retrans limit */
383 fep
->stats
.tx_aborted_errors
++;
384 if (sc
& BD_ENET_TX_UN
) /* Underrun */
385 fep
->stats
.tx_fifo_errors
++;
386 if (sc
& BD_ENET_TX_CSL
) /* Carrier lost */
387 fep
->stats
.tx_carrier_errors
++;
389 if (sc
& (BD_ENET_TX_LC
| BD_ENET_TX_RL
| BD_ENET_TX_UN
)) {
390 fep
->stats
.tx_errors
++;
394 fep
->stats
.tx_packets
++;
396 if (sc
& BD_ENET_TX_READY
)
397 printk(KERN_WARNING DRV_MODULE_NAME
398 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
402 * Deferred means some collisions occurred during transmit,
403 * but we eventually sent the packet OK.
405 if (sc
& BD_ENET_TX_DEF
)
406 fep
->stats
.collisions
++;
409 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
410 skb
->len
, DMA_TO_DEVICE
);
413 * Free the sk buffer associated with this last transmit.
415 dev_kfree_skb_irq(skb
);
416 fep
->tx_skbuff
[dirtyidx
] = NULL
;
419 * Update pointer to next buffer descriptor to be transmitted.
421 if ((sc
& BD_ENET_TX_WRAP
) == 0)
424 bdp
= fep
->tx_bd_base
;
427 * Since we have freed up a buffer, the ring is no longer
437 (*fep
->ops
->tx_restart
)(dev
);
439 spin_unlock(&fep
->tx_lock
);
442 netif_wake_queue(dev
);
446 * The interrupt handler.
447 * This is called from the MPC core interrupt.
450 fs_enet_interrupt(int irq
, void *dev_id
)
452 struct net_device
*dev
= dev_id
;
453 struct fs_enet_private
*fep
;
454 const struct fs_platform_info
*fpi
;
460 fep
= netdev_priv(dev
);
464 while ((int_events
= (*fep
->ops
->get_int_events
)(dev
)) != 0) {
467 int_clr_events
= int_events
;
469 int_clr_events
&= ~fep
->ev_napi_rx
;
471 (*fep
->ops
->clear_int_events
)(dev
, int_clr_events
);
473 if (int_events
& fep
->ev_err
)
474 (*fep
->ops
->ev_error
)(dev
, int_events
);
476 if (int_events
& fep
->ev_rx
) {
478 fs_enet_rx_non_napi(dev
);
480 napi_ok
= napi_schedule_prep(&fep
->napi
);
482 (*fep
->ops
->napi_disable_rx
)(dev
);
483 (*fep
->ops
->clear_int_events
)(dev
, fep
->ev_napi_rx
);
485 /* NOTE: it is possible for FCCs in NAPI mode */
486 /* to submit a spurious interrupt while in poll */
488 __netif_rx_schedule(dev
, &fep
->napi
);
492 if (int_events
& fep
->ev_tx
)
497 return IRQ_RETVAL(handled
);
500 void fs_init_bds(struct net_device
*dev
)
502 struct fs_enet_private
*fep
= netdev_priv(dev
);
509 fep
->dirty_tx
= fep
->cur_tx
= fep
->tx_bd_base
;
510 fep
->tx_free
= fep
->tx_ring
;
511 fep
->cur_rx
= fep
->rx_bd_base
;
514 * Initialize the receive buffer descriptors.
516 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
517 skb
= dev_alloc_skb(ENET_RX_FRSIZE
);
519 printk(KERN_WARNING DRV_MODULE_NAME
520 ": %s Memory squeeze, unable to allocate skb\n",
524 skb_align(skb
, ENET_RX_ALIGN
);
525 fep
->rx_skbuff
[i
] = skb
;
527 dma_map_single(fep
->dev
, skb
->data
,
528 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
530 CBDW_DATLEN(bdp
, 0); /* zero */
531 CBDW_SC(bdp
, BD_ENET_RX_EMPTY
|
532 ((i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
));
535 * if we failed, fillup remainder
537 for (; i
< fep
->rx_ring
; i
++, bdp
++) {
538 fep
->rx_skbuff
[i
] = NULL
;
539 CBDW_SC(bdp
, (i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
);
543 * ...and the same for transmit.
545 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
546 fep
->tx_skbuff
[i
] = NULL
;
547 CBDW_BUFADDR(bdp
, 0);
549 CBDW_SC(bdp
, (i
< fep
->tx_ring
- 1) ? 0 : BD_SC_WRAP
);
553 void fs_cleanup_bds(struct net_device
*dev
)
555 struct fs_enet_private
*fep
= netdev_priv(dev
);
561 * Reset SKB transmit buffers.
563 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
564 if ((skb
= fep
->tx_skbuff
[i
]) == NULL
)
568 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
569 skb
->len
, DMA_TO_DEVICE
);
571 fep
->tx_skbuff
[i
] = NULL
;
576 * Reset SKB receive buffers
578 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
579 if ((skb
= fep
->rx_skbuff
[i
]) == NULL
)
583 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
584 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
587 fep
->rx_skbuff
[i
] = NULL
;
593 /**********************************************************************************/
595 static int fs_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
597 struct fs_enet_private
*fep
= netdev_priv(dev
);
603 spin_lock_irqsave(&fep
->tx_lock
, flags
);
606 * Fill in a Tx ring entry
610 if (!fep
->tx_free
|| (CBDR_SC(bdp
) & BD_ENET_TX_READY
)) {
611 netif_stop_queue(dev
);
612 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
615 * Ooops. All transmit buffers are full. Bail out.
616 * This should not happen, since the tx queue should be stopped.
618 printk(KERN_WARNING DRV_MODULE_NAME
619 ": %s tx queue full!.\n", dev
->name
);
620 return NETDEV_TX_BUSY
;
623 curidx
= bdp
- fep
->tx_bd_base
;
625 * Clear all of the status flags.
627 CBDC_SC(bdp
, BD_ENET_TX_STATS
);
632 fep
->tx_skbuff
[curidx
] = skb
;
634 fep
->stats
.tx_bytes
+= skb
->len
;
637 * Push the data cache so the CPM does not get stale memory data.
639 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
,
640 skb
->data
, skb
->len
, DMA_TO_DEVICE
));
641 CBDW_DATLEN(bdp
, skb
->len
);
643 dev
->trans_start
= jiffies
;
646 * If this was the last BD in the ring, start at the beginning again.
648 if ((CBDR_SC(bdp
) & BD_ENET_TX_WRAP
) == 0)
651 fep
->cur_tx
= fep
->tx_bd_base
;
654 netif_stop_queue(dev
);
656 /* Trigger transmission start */
657 sc
= BD_ENET_TX_READY
| BD_ENET_TX_INTR
|
658 BD_ENET_TX_LAST
| BD_ENET_TX_TC
;
660 /* note that while FEC does not have this bit
661 * it marks it as available for software use
662 * yay for hw reuse :) */
664 sc
|= BD_ENET_TX_PAD
;
667 (*fep
->ops
->tx_kickstart
)(dev
);
669 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
674 static int fs_request_irq(struct net_device
*dev
, int irq
, const char *name
,
677 struct fs_enet_private
*fep
= netdev_priv(dev
);
679 (*fep
->ops
->pre_request_irq
)(dev
, irq
);
680 return request_irq(irq
, irqf
, IRQF_SHARED
, name
, dev
);
683 static void fs_free_irq(struct net_device
*dev
, int irq
)
685 struct fs_enet_private
*fep
= netdev_priv(dev
);
688 (*fep
->ops
->post_free_irq
)(dev
, irq
);
691 static void fs_timeout(struct net_device
*dev
)
693 struct fs_enet_private
*fep
= netdev_priv(dev
);
697 fep
->stats
.tx_errors
++;
699 spin_lock_irqsave(&fep
->lock
, flags
);
701 if (dev
->flags
& IFF_UP
) {
702 phy_stop(fep
->phydev
);
703 (*fep
->ops
->stop
)(dev
);
704 (*fep
->ops
->restart
)(dev
);
705 phy_start(fep
->phydev
);
708 phy_start(fep
->phydev
);
709 wake
= fep
->tx_free
&& !(CBDR_SC(fep
->cur_tx
) & BD_ENET_TX_READY
);
710 spin_unlock_irqrestore(&fep
->lock
, flags
);
713 netif_wake_queue(dev
);
716 /*-----------------------------------------------------------------------------
717 * generic link-change handler - should be sufficient for most cases
718 *-----------------------------------------------------------------------------*/
719 static void generic_adjust_link(struct net_device
*dev
)
721 struct fs_enet_private
*fep
= netdev_priv(dev
);
722 struct phy_device
*phydev
= fep
->phydev
;
726 /* adjust to duplex mode */
727 if (phydev
->duplex
!= fep
->oldduplex
) {
729 fep
->oldduplex
= phydev
->duplex
;
732 if (phydev
->speed
!= fep
->oldspeed
) {
734 fep
->oldspeed
= phydev
->speed
;
741 netif_carrier_on(dev
);
742 netif_start_queue(dev
);
746 fep
->ops
->restart(dev
);
747 } else if (fep
->oldlink
) {
752 netif_carrier_off(dev
);
753 netif_stop_queue(dev
);
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
;
785 phydev
= phy_connect(dev
, fep
->fpi
->bus_id
, &fs_adjust_link
, 0,
786 PHY_INTERFACE_MODE_MII
);
788 printk("No phy bus ID specified in BSP code\n");
791 if (IS_ERR(phydev
)) {
792 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
793 return PTR_ERR(phydev
);
796 fep
->phydev
= phydev
;
801 static int fs_enet_open(struct net_device
*dev
)
803 struct fs_enet_private
*fep
= netdev_priv(dev
);
807 if (fep
->fpi
->use_napi
)
808 napi_enable(&fep
->napi
);
810 /* Install our interrupt handler. */
811 r
= fs_request_irq(dev
, fep
->interrupt
, "fs_enet-mac", fs_enet_interrupt
);
813 printk(KERN_ERR DRV_MODULE_NAME
814 ": %s Could not allocate FS_ENET IRQ!", dev
->name
);
815 if (fep
->fpi
->use_napi
)
816 napi_disable(&fep
->napi
);
820 err
= fs_init_phy(dev
);
822 if (fep
->fpi
->use_napi
)
823 napi_disable(&fep
->napi
);
826 phy_start(fep
->phydev
);
831 static int fs_enet_close(struct net_device
*dev
)
833 struct fs_enet_private
*fep
= netdev_priv(dev
);
836 netif_stop_queue(dev
);
837 netif_carrier_off(dev
);
838 napi_disable(&fep
->napi
);
839 phy_stop(fep
->phydev
);
841 spin_lock_irqsave(&fep
->lock
, flags
);
842 spin_lock(&fep
->tx_lock
);
843 (*fep
->ops
->stop
)(dev
);
844 spin_unlock(&fep
->tx_lock
);
845 spin_unlock_irqrestore(&fep
->lock
, flags
);
847 /* release any irqs */
848 phy_disconnect(fep
->phydev
);
850 fs_free_irq(dev
, fep
->interrupt
);
855 static struct net_device_stats
*fs_enet_get_stats(struct net_device
*dev
)
857 struct fs_enet_private
*fep
= netdev_priv(dev
);
861 /*************************************************************************/
863 static void fs_get_drvinfo(struct net_device
*dev
,
864 struct ethtool_drvinfo
*info
)
866 strcpy(info
->driver
, DRV_MODULE_NAME
);
867 strcpy(info
->version
, DRV_MODULE_VERSION
);
870 static int fs_get_regs_len(struct net_device
*dev
)
872 struct fs_enet_private
*fep
= netdev_priv(dev
);
874 return (*fep
->ops
->get_regs_len
)(dev
);
877 static void fs_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
880 struct fs_enet_private
*fep
= netdev_priv(dev
);
886 spin_lock_irqsave(&fep
->lock
, flags
);
887 r
= (*fep
->ops
->get_regs
)(dev
, p
, &len
);
888 spin_unlock_irqrestore(&fep
->lock
, flags
);
894 static int fs_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
896 struct fs_enet_private
*fep
= netdev_priv(dev
);
897 return phy_ethtool_gset(fep
->phydev
, cmd
);
900 static int fs_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
902 struct fs_enet_private
*fep
= netdev_priv(dev
);
903 phy_ethtool_sset(fep
->phydev
, cmd
);
907 static int fs_nway_reset(struct net_device
*dev
)
912 static u32
fs_get_msglevel(struct net_device
*dev
)
914 struct fs_enet_private
*fep
= netdev_priv(dev
);
915 return fep
->msg_enable
;
918 static void fs_set_msglevel(struct net_device
*dev
, u32 value
)
920 struct fs_enet_private
*fep
= netdev_priv(dev
);
921 fep
->msg_enable
= value
;
924 static const struct ethtool_ops fs_ethtool_ops
= {
925 .get_drvinfo
= fs_get_drvinfo
,
926 .get_regs_len
= fs_get_regs_len
,
927 .get_settings
= fs_get_settings
,
928 .set_settings
= fs_set_settings
,
929 .nway_reset
= fs_nway_reset
,
930 .get_link
= ethtool_op_get_link
,
931 .get_msglevel
= fs_get_msglevel
,
932 .set_msglevel
= fs_set_msglevel
,
933 .set_tx_csum
= ethtool_op_set_tx_csum
, /* local! */
934 .set_sg
= ethtool_op_set_sg
,
935 .get_regs
= fs_get_regs
,
938 static int fs_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
940 struct fs_enet_private
*fep
= netdev_priv(dev
);
941 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*)&rq
->ifr_data
;
945 if (!netif_running(dev
))
948 spin_lock_irqsave(&fep
->lock
, flags
);
949 rc
= phy_mii_ioctl(fep
->phydev
, mii
, cmd
);
950 spin_unlock_irqrestore(&fep
->lock
, flags
);
954 extern int fs_mii_connect(struct net_device
*dev
);
955 extern void fs_mii_disconnect(struct net_device
*dev
);
957 #ifndef CONFIG_PPC_CPM_NEW_BINDING
958 static struct net_device
*fs_init_instance(struct device
*dev
,
959 struct fs_platform_info
*fpi
)
961 struct net_device
*ndev
= NULL
;
962 struct fs_enet_private
*fep
= NULL
;
963 int privsize
, i
, r
, err
= 0, registered
= 0;
965 fpi
->fs_no
= fs_get_id(fpi
);
967 if ((unsigned int)fpi
->fs_no
>= FS_MAX_INDEX
)
968 return ERR_PTR(-EINVAL
);
970 privsize
= sizeof(*fep
) + (sizeof(struct sk_buff
**) *
971 (fpi
->rx_ring
+ fpi
->tx_ring
));
973 ndev
= alloc_etherdev(privsize
);
979 fep
= netdev_priv(ndev
);
982 dev_set_drvdata(dev
, ndev
);
984 if (fpi
->init_ioports
)
985 fpi
->init_ioports((struct fs_platform_info
*)fpi
);
987 #ifdef CONFIG_FS_ENET_HAS_FEC
988 if (fs_get_fec_index(fpi
->fs_no
) >= 0)
989 fep
->ops
= &fs_fec_ops
;
992 #ifdef CONFIG_FS_ENET_HAS_SCC
993 if (fs_get_scc_index(fpi
->fs_no
) >=0)
994 fep
->ops
= &fs_scc_ops
;
997 #ifdef CONFIG_FS_ENET_HAS_FCC
998 if (fs_get_fcc_index(fpi
->fs_no
) >= 0)
999 fep
->ops
= &fs_fcc_ops
;
1002 if (fep
->ops
== NULL
) {
1003 printk(KERN_ERR DRV_MODULE_NAME
1004 ": %s No matching ops found (%d).\n",
1005 ndev
->name
, fpi
->fs_no
);
1010 r
= (*fep
->ops
->setup_data
)(ndev
);
1012 printk(KERN_ERR DRV_MODULE_NAME
1013 ": %s setup_data failed\n",
1019 /* point rx_skbuff, tx_skbuff */
1020 fep
->rx_skbuff
= (struct sk_buff
**)&fep
[1];
1021 fep
->tx_skbuff
= fep
->rx_skbuff
+ fpi
->rx_ring
;
1024 spin_lock_init(&fep
->lock
);
1025 spin_lock_init(&fep
->tx_lock
);
1028 * Set the Ethernet address.
1030 for (i
= 0; i
< 6; i
++)
1031 ndev
->dev_addr
[i
] = fpi
->macaddr
[i
];
1033 r
= (*fep
->ops
->allocate_bd
)(ndev
);
1035 if (fep
->ring_base
== NULL
) {
1036 printk(KERN_ERR DRV_MODULE_NAME
1037 ": %s buffer descriptor alloc failed (%d).\n", ndev
->name
, r
);
1043 * Set receive and transmit descriptor base.
1045 fep
->rx_bd_base
= fep
->ring_base
;
1046 fep
->tx_bd_base
= fep
->rx_bd_base
+ fpi
->rx_ring
;
1048 /* initialize ring size variables */
1049 fep
->tx_ring
= fpi
->tx_ring
;
1050 fep
->rx_ring
= fpi
->rx_ring
;
1053 * The FEC Ethernet specific entries in the device structure.
1055 ndev
->open
= fs_enet_open
;
1056 ndev
->hard_start_xmit
= fs_enet_start_xmit
;
1057 ndev
->tx_timeout
= fs_timeout
;
1058 ndev
->watchdog_timeo
= 2 * HZ
;
1059 ndev
->stop
= fs_enet_close
;
1060 ndev
->get_stats
= fs_enet_get_stats
;
1061 ndev
->set_multicast_list
= fs_set_multicast_list
;
1063 #ifdef CONFIG_NET_POLL_CONTROLLER
1064 ndev
->poll_controller
= fs_enet_netpoll
;
1067 netif_napi_add(ndev
, &fep
->napi
,
1068 fs_enet_rx_napi
, fpi
->napi_weight
);
1070 ndev
->ethtool_ops
= &fs_ethtool_ops
;
1071 ndev
->do_ioctl
= fs_ioctl
;
1073 init_timer(&fep
->phy_timer_list
);
1075 netif_carrier_off(ndev
);
1077 err
= register_netdev(ndev
);
1079 printk(KERN_ERR DRV_MODULE_NAME
1080 ": %s register_netdev failed.\n", ndev
->name
);
1091 unregister_netdev(ndev
);
1094 (*fep
->ops
->free_bd
)(ndev
);
1095 (*fep
->ops
->cleanup_data
)(ndev
);
1101 dev_set_drvdata(dev
, NULL
);
1103 return ERR_PTR(err
);
1106 static int fs_cleanup_instance(struct net_device
*ndev
)
1108 struct fs_enet_private
*fep
;
1109 const struct fs_platform_info
*fpi
;
1115 fep
= netdev_priv(ndev
);
1121 unregister_netdev(ndev
);
1123 dma_free_coherent(fep
->dev
, (fpi
->tx_ring
+ fpi
->rx_ring
) * sizeof(cbd_t
),
1124 (void __force
*)fep
->ring_base
, fep
->ring_mem_addr
);
1127 (*fep
->ops
->cleanup_data
)(ndev
);
1131 dev_set_drvdata(dev
, NULL
);
1141 /**************************************************************************************/
1143 /* handy pointer to the immap */
1144 void __iomem
*fs_enet_immap
= NULL
;
1146 static int setup_immap(void)
1149 fs_enet_immap
= ioremap(IMAP_ADDR
, 0x4000);
1150 WARN_ON(!fs_enet_immap
);
1151 #elif defined(CONFIG_CPM2)
1152 fs_enet_immap
= cpm2_immr
;
1158 static void cleanup_immap(void)
1160 #if defined(CONFIG_CPM1)
1161 iounmap(fs_enet_immap
);
1165 /**************************************************************************************/
1167 #ifdef CONFIG_PPC_CPM_NEW_BINDING
1168 static int __devinit
find_phy(struct device_node
*np
,
1169 struct fs_platform_info
*fpi
)
1171 struct device_node
*phynode
, *mdionode
;
1172 struct resource res
;
1175 const u32
*data
= of_get_property(np
, "phy-handle", &len
);
1176 if (!data
|| len
!= 4)
1179 phynode
= of_find_node_by_phandle(*data
);
1183 mdionode
= of_get_parent(phynode
);
1187 ret
= of_address_to_resource(mdionode
, 0, &res
);
1191 data
= of_get_property(phynode
, "reg", &len
);
1192 if (!data
|| len
!= 4)
1195 snprintf(fpi
->bus_id
, 16, PHY_ID_FMT
, res
.start
, *data
);
1198 of_node_put(mdionode
);
1200 of_node_put(phynode
);
1204 #ifdef CONFIG_FS_ENET_HAS_FEC
1205 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
1207 #define IS_FEC(match) 0
1210 static int __devinit
fs_enet_probe(struct of_device
*ofdev
,
1211 const struct of_device_id
*match
)
1213 struct net_device
*ndev
;
1214 struct fs_enet_private
*fep
;
1215 struct fs_platform_info
*fpi
;
1218 int privsize
, len
, ret
= -ENODEV
;
1220 fpi
= kzalloc(sizeof(*fpi
), GFP_KERNEL
);
1224 if (!IS_FEC(match
)) {
1225 data
= of_get_property(ofdev
->node
, "fsl,cpm-command", &len
);
1226 if (!data
|| len
!= 4)
1229 fpi
->cp_command
= *data
;
1234 fpi
->rx_copybreak
= 240;
1236 fpi
->napi_weight
= 17;
1238 ret
= find_phy(ofdev
->node
, fpi
);
1242 privsize
= sizeof(*fep
) +
1243 sizeof(struct sk_buff
**) *
1244 (fpi
->rx_ring
+ fpi
->tx_ring
);
1246 ndev
= alloc_etherdev(privsize
);
1252 dev_set_drvdata(&ofdev
->dev
, ndev
);
1254 fep
= netdev_priv(ndev
);
1255 fep
->dev
= &ofdev
->dev
;
1258 fep
->ops
= match
->data
;
1260 ret
= fep
->ops
->setup_data(ndev
);
1264 fep
->rx_skbuff
= (struct sk_buff
**)&fep
[1];
1265 fep
->tx_skbuff
= fep
->rx_skbuff
+ fpi
->rx_ring
;
1267 spin_lock_init(&fep
->lock
);
1268 spin_lock_init(&fep
->tx_lock
);
1270 mac_addr
= of_get_mac_address(ofdev
->node
);
1272 memcpy(ndev
->dev_addr
, mac_addr
, 6);
1274 ret
= fep
->ops
->allocate_bd(ndev
);
1276 goto out_cleanup_data
;
1278 fep
->rx_bd_base
= fep
->ring_base
;
1279 fep
->tx_bd_base
= fep
->rx_bd_base
+ fpi
->rx_ring
;
1281 fep
->tx_ring
= fpi
->tx_ring
;
1282 fep
->rx_ring
= fpi
->rx_ring
;
1284 ndev
->open
= fs_enet_open
;
1285 ndev
->hard_start_xmit
= fs_enet_start_xmit
;
1286 ndev
->tx_timeout
= fs_timeout
;
1287 ndev
->watchdog_timeo
= 2 * HZ
;
1288 ndev
->stop
= fs_enet_close
;
1289 ndev
->get_stats
= fs_enet_get_stats
;
1290 ndev
->set_multicast_list
= fs_set_multicast_list
;
1293 netif_napi_add(ndev
, &fep
->napi
, fs_enet_rx_napi
,
1296 ndev
->ethtool_ops
= &fs_ethtool_ops
;
1297 ndev
->do_ioctl
= fs_ioctl
;
1299 init_timer(&fep
->phy_timer_list
);
1301 netif_carrier_off(ndev
);
1303 ret
= register_netdev(ndev
);
1307 printk(KERN_INFO
"%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
1309 ndev
->dev_addr
[0], ndev
->dev_addr
[1], ndev
->dev_addr
[2],
1310 ndev
->dev_addr
[3], ndev
->dev_addr
[4], ndev
->dev_addr
[5]);
1315 fep
->ops
->free_bd(ndev
);
1317 fep
->ops
->cleanup_data(ndev
);
1320 dev_set_drvdata(&ofdev
->dev
, NULL
);
1326 static int fs_enet_remove(struct of_device
*ofdev
)
1328 struct net_device
*ndev
= dev_get_drvdata(&ofdev
->dev
);
1329 struct fs_enet_private
*fep
= netdev_priv(ndev
);
1331 unregister_netdev(ndev
);
1333 fep
->ops
->free_bd(ndev
);
1334 fep
->ops
->cleanup_data(ndev
);
1335 dev_set_drvdata(fep
->dev
, NULL
);
1341 static struct of_device_id fs_enet_match
[] = {
1342 #ifdef CONFIG_FS_ENET_HAS_SCC
1344 .compatible
= "fsl,cpm1-scc-enet",
1345 .data
= (void *)&fs_scc_ops
,
1348 #ifdef CONFIG_FS_ENET_HAS_FCC
1350 .compatible
= "fsl,cpm2-fcc-enet",
1351 .data
= (void *)&fs_fcc_ops
,
1354 #ifdef CONFIG_FS_ENET_HAS_FEC
1356 .compatible
= "fsl,pq1-fec-enet",
1357 .data
= (void *)&fs_fec_ops
,
1363 static struct of_platform_driver fs_enet_driver
= {
1365 .match_table
= fs_enet_match
,
1366 .probe
= fs_enet_probe
,
1367 .remove
= fs_enet_remove
,
1370 static int __init
fs_init(void)
1372 int r
= setup_immap();
1376 r
= of_register_platform_driver(&fs_enet_driver
);
1387 static void __exit
fs_cleanup(void)
1389 of_unregister_platform_driver(&fs_enet_driver
);
1393 static int __devinit
fs_enet_probe(struct device
*dev
)
1395 struct net_device
*ndev
;
1397 /* no fixup - no device */
1398 if (dev
->platform_data
== NULL
) {
1399 printk(KERN_INFO
"fs_enet: "
1400 "probe called with no platform data; "
1401 "remove unused devices\n");
1405 ndev
= fs_init_instance(dev
, dev
->platform_data
);
1407 return PTR_ERR(ndev
);
1411 static int fs_enet_remove(struct device
*dev
)
1413 return fs_cleanup_instance(dev_get_drvdata(dev
));
1416 static struct device_driver fs_enet_fec_driver
= {
1417 .name
= "fsl-cpm-fec",
1418 .bus
= &platform_bus_type
,
1419 .probe
= fs_enet_probe
,
1420 .remove
= fs_enet_remove
,
1422 /* .suspend = fs_enet_suspend, TODO */
1423 /* .resume = fs_enet_resume, TODO */
1427 static struct device_driver fs_enet_scc_driver
= {
1428 .name
= "fsl-cpm-scc",
1429 .bus
= &platform_bus_type
,
1430 .probe
= fs_enet_probe
,
1431 .remove
= fs_enet_remove
,
1433 /* .suspend = fs_enet_suspend, TODO */
1434 /* .resume = fs_enet_resume, TODO */
1438 static struct device_driver fs_enet_fcc_driver
= {
1439 .name
= "fsl-cpm-fcc",
1440 .bus
= &platform_bus_type
,
1441 .probe
= fs_enet_probe
,
1442 .remove
= fs_enet_remove
,
1444 /* .suspend = fs_enet_suspend, TODO */
1445 /* .resume = fs_enet_resume, TODO */
1449 static int __init
fs_init(void)
1460 #ifdef CONFIG_FS_ENET_HAS_FCC
1461 /* let's insert mii stuff */
1462 r
= fs_enet_mdio_bb_init();
1465 printk(KERN_ERR DRV_MODULE_NAME
1466 "BB PHY init failed.\n");
1469 r
= driver_register(&fs_enet_fcc_driver
);
1474 #ifdef CONFIG_FS_ENET_HAS_FEC
1475 r
= fs_enet_mdio_fec_init();
1477 printk(KERN_ERR DRV_MODULE_NAME
1478 "FEC PHY init failed.\n");
1482 r
= driver_register(&fs_enet_fec_driver
);
1487 #ifdef CONFIG_FS_ENET_HAS_SCC
1488 r
= driver_register(&fs_enet_scc_driver
);
1499 static void __exit
fs_cleanup(void)
1501 driver_unregister(&fs_enet_fec_driver
);
1502 driver_unregister(&fs_enet_fcc_driver
);
1503 driver_unregister(&fs_enet_scc_driver
);
1508 #ifdef CONFIG_NET_POLL_CONTROLLER
1509 static void fs_enet_netpoll(struct net_device
*dev
)
1511 disable_irq(dev
->irq
);
1512 fs_enet_interrupt(dev
->irq
, dev
, NULL
);
1513 enable_irq(dev
->irq
);
1517 /**************************************************************************************/
1519 module_init(fs_init
);
1520 module_exit(fs_cleanup
);