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/sched.h>
22 #include <linux/string.h>
23 #include <linux/ptrace.h>
24 #include <linux/errno.h>
25 #include <linux/ioport.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/pci.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/spinlock.h>
35 #include <linux/mii.h>
36 #include <linux/ethtool.h>
37 #include <linux/bitops.h>
39 #include <linux/platform_device.h>
40 #include <linux/phy.h>
42 #include <linux/vmalloc.h>
43 #include <asm/pgtable.h>
45 #include <asm/pgtable.h>
47 #include <asm/uaccess.h>
51 /*************************************************/
53 static char version
[] __devinitdata
=
54 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")" "\n";
56 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
57 MODULE_DESCRIPTION("Freescale Ethernet Driver");
58 MODULE_LICENSE("GPL");
59 MODULE_VERSION(DRV_MODULE_VERSION
);
61 int fs_enet_debug
= -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
62 module_param(fs_enet_debug
, int, 0);
63 MODULE_PARM_DESC(fs_enet_debug
,
64 "Freescale bitmapped debugging message enable value");
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 /* NAPI receive function */
75 static int fs_enet_rx_napi(struct net_device
*dev
, int *budget
)
77 struct fs_enet_private
*fep
= netdev_priv(dev
);
78 const struct fs_platform_info
*fpi
= fep
->fpi
;
80 struct sk_buff
*skb
, *skbn
, *skbt
;
84 int rx_work_limit
= 0; /* pacify gcc */
86 rx_work_limit
= min(dev
->quota
, *budget
);
88 if (!netif_running(dev
))
92 * First, grab all of the stats for the incoming packet.
93 * These get messed up if we get called due to a busy condition.
97 /* clear RX status bits for napi*/
98 (*fep
->ops
->napi_clear_rx_event
)(dev
);
100 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
102 curidx
= bdp
- fep
->rx_bd_base
;
105 * Since we have allocated space to hold a complete frame,
106 * the last indicator should be set.
108 if ((sc
& BD_ENET_RX_LAST
) == 0)
109 printk(KERN_WARNING DRV_MODULE_NAME
110 ": %s 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
),
142 /* napi, got packet but no quota */
143 if (--rx_work_limit
< 0)
146 skb
= fep
->rx_skbuff
[curidx
];
148 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
149 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
153 * Process the incoming frame.
155 fep
->stats
.rx_packets
++;
156 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
157 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
159 if (pkt_len
<= fpi
->rx_copybreak
) {
160 /* +2 to make IP header L1 cache aligned */
161 skbn
= dev_alloc_skb(pkt_len
+ 2);
163 skb_reserve(skbn
, 2); /* align IP header */
164 memcpy(skbn
->data
, skb
->data
, pkt_len
);
171 skbn
= dev_alloc_skb(ENET_RX_FRSIZE
);
175 skb_put(skb
, pkt_len
); /* Make room */
176 skb
->protocol
= eth_type_trans(skb
, dev
);
178 netif_receive_skb(skb
);
180 printk(KERN_WARNING DRV_MODULE_NAME
181 ": %s Memory squeeze, dropping packet.\n",
183 fep
->stats
.rx_dropped
++;
188 fep
->rx_skbuff
[curidx
] = skbn
;
189 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
190 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
193 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
196 * Update BD pointer to next entry.
198 if ((sc
& BD_ENET_RX_WRAP
) == 0)
201 bdp
= fep
->rx_bd_base
;
203 (*fep
->ops
->rx_bd_done
)(dev
);
208 dev
->quota
-= received
;
211 if (rx_work_limit
< 0)
212 return 1; /* not done */
215 netif_rx_complete(dev
);
217 (*fep
->ops
->napi_enable_rx
)(dev
);
222 /* non NAPI receive function */
223 static int fs_enet_rx_non_napi(struct net_device
*dev
)
225 struct fs_enet_private
*fep
= netdev_priv(dev
);
226 const struct fs_platform_info
*fpi
= fep
->fpi
;
228 struct sk_buff
*skb
, *skbn
, *skbt
;
233 * First, grab all of the stats for the incoming packet.
234 * These get messed up if we get called due to a busy condition.
238 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
240 curidx
= bdp
- fep
->rx_bd_base
;
243 * Since we have allocated space to hold a complete frame,
244 * the last indicator should be set.
246 if ((sc
& BD_ENET_RX_LAST
) == 0)
247 printk(KERN_WARNING DRV_MODULE_NAME
248 ": %s rcv is not +last\n",
254 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_CL
|
255 BD_ENET_RX_NO
| BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
256 fep
->stats
.rx_errors
++;
257 /* Frame too long or too short. */
258 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
))
259 fep
->stats
.rx_length_errors
++;
260 /* Frame alignment */
261 if (sc
& (BD_ENET_RX_NO
| BD_ENET_RX_CL
))
262 fep
->stats
.rx_frame_errors
++;
264 if (sc
& BD_ENET_RX_CR
)
265 fep
->stats
.rx_crc_errors
++;
267 if (sc
& BD_ENET_RX_OV
)
268 fep
->stats
.rx_crc_errors
++;
270 skb
= fep
->rx_skbuff
[curidx
];
272 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
273 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
280 skb
= fep
->rx_skbuff
[curidx
];
282 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
283 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
287 * Process the incoming frame.
289 fep
->stats
.rx_packets
++;
290 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
291 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
293 if (pkt_len
<= fpi
->rx_copybreak
) {
294 /* +2 to make IP header L1 cache aligned */
295 skbn
= dev_alloc_skb(pkt_len
+ 2);
297 skb_reserve(skbn
, 2); /* align IP header */
298 memcpy(skbn
->data
, skb
->data
, pkt_len
);
305 skbn
= dev_alloc_skb(ENET_RX_FRSIZE
);
309 skb_put(skb
, pkt_len
); /* Make room */
310 skb
->protocol
= eth_type_trans(skb
, dev
);
314 printk(KERN_WARNING DRV_MODULE_NAME
315 ": %s Memory squeeze, dropping packet.\n",
317 fep
->stats
.rx_dropped
++;
322 fep
->rx_skbuff
[curidx
] = skbn
;
323 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
324 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
327 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
330 * Update BD pointer to next entry.
332 if ((sc
& BD_ENET_RX_WRAP
) == 0)
335 bdp
= fep
->rx_bd_base
;
337 (*fep
->ops
->rx_bd_done
)(dev
);
345 static void fs_enet_tx(struct net_device
*dev
)
347 struct fs_enet_private
*fep
= netdev_priv(dev
);
350 int dirtyidx
, do_wake
, do_restart
;
353 spin_lock(&fep
->lock
);
356 do_wake
= do_restart
= 0;
357 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_TX_READY
) == 0) {
359 dirtyidx
= bdp
- fep
->tx_bd_base
;
361 if (fep
->tx_free
== fep
->tx_ring
)
364 skb
= fep
->tx_skbuff
[dirtyidx
];
369 if (sc
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
370 BD_ENET_TX_RL
| BD_ENET_TX_UN
| BD_ENET_TX_CSL
)) {
372 if (sc
& BD_ENET_TX_HB
) /* No heartbeat */
373 fep
->stats
.tx_heartbeat_errors
++;
374 if (sc
& BD_ENET_TX_LC
) /* Late collision */
375 fep
->stats
.tx_window_errors
++;
376 if (sc
& BD_ENET_TX_RL
) /* Retrans limit */
377 fep
->stats
.tx_aborted_errors
++;
378 if (sc
& BD_ENET_TX_UN
) /* Underrun */
379 fep
->stats
.tx_fifo_errors
++;
380 if (sc
& BD_ENET_TX_CSL
) /* Carrier lost */
381 fep
->stats
.tx_carrier_errors
++;
383 if (sc
& (BD_ENET_TX_LC
| BD_ENET_TX_RL
| BD_ENET_TX_UN
)) {
384 fep
->stats
.tx_errors
++;
388 fep
->stats
.tx_packets
++;
390 if (sc
& BD_ENET_TX_READY
)
391 printk(KERN_WARNING DRV_MODULE_NAME
392 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
396 * Deferred means some collisions occurred during transmit,
397 * but we eventually sent the packet OK.
399 if (sc
& BD_ENET_TX_DEF
)
400 fep
->stats
.collisions
++;
403 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
404 skb
->len
, DMA_TO_DEVICE
);
407 * Free the sk buffer associated with this last transmit.
409 dev_kfree_skb_irq(skb
);
410 fep
->tx_skbuff
[dirtyidx
] = NULL
;
413 * Update pointer to next buffer descriptor to be transmitted.
415 if ((sc
& BD_ENET_TX_WRAP
) == 0)
418 bdp
= fep
->tx_bd_base
;
421 * Since we have freed up a buffer, the ring is no longer
431 (*fep
->ops
->tx_restart
)(dev
);
433 spin_unlock(&fep
->lock
);
436 netif_wake_queue(dev
);
440 * The interrupt handler.
441 * This is called from the MPC core interrupt.
444 fs_enet_interrupt(int irq
, void *dev_id
)
446 struct net_device
*dev
= dev_id
;
447 struct fs_enet_private
*fep
;
448 const struct fs_platform_info
*fpi
;
454 fep
= netdev_priv(dev
);
458 while ((int_events
= (*fep
->ops
->get_int_events
)(dev
)) != 0) {
462 int_clr_events
= int_events
;
464 int_clr_events
&= ~fep
->ev_napi_rx
;
466 (*fep
->ops
->clear_int_events
)(dev
, int_clr_events
);
468 if (int_events
& fep
->ev_err
)
469 (*fep
->ops
->ev_error
)(dev
, int_events
);
471 if (int_events
& fep
->ev_rx
) {
473 fs_enet_rx_non_napi(dev
);
475 napi_ok
= netif_rx_schedule_prep(dev
);
477 (*fep
->ops
->napi_disable_rx
)(dev
);
478 (*fep
->ops
->clear_int_events
)(dev
, fep
->ev_napi_rx
);
480 /* NOTE: it is possible for FCCs in NAPI mode */
481 /* to submit a spurious interrupt while in poll */
483 __netif_rx_schedule(dev
);
487 if (int_events
& fep
->ev_tx
)
492 return IRQ_RETVAL(handled
);
495 void fs_init_bds(struct net_device
*dev
)
497 struct fs_enet_private
*fep
= netdev_priv(dev
);
504 fep
->dirty_tx
= fep
->cur_tx
= fep
->tx_bd_base
;
505 fep
->tx_free
= fep
->tx_ring
;
506 fep
->cur_rx
= fep
->rx_bd_base
;
509 * Initialize the receive buffer descriptors.
511 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
512 skb
= dev_alloc_skb(ENET_RX_FRSIZE
);
514 printk(KERN_WARNING DRV_MODULE_NAME
515 ": %s Memory squeeze, unable to allocate skb\n",
519 fep
->rx_skbuff
[i
] = skb
;
522 dma_map_single(fep
->dev
, skb
->data
,
523 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
525 CBDW_DATLEN(bdp
, 0); /* zero */
526 CBDW_SC(bdp
, BD_ENET_RX_EMPTY
|
527 ((i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
));
530 * if we failed, fillup remainder
532 for (; i
< fep
->rx_ring
; i
++, bdp
++) {
533 fep
->rx_skbuff
[i
] = NULL
;
534 CBDW_SC(bdp
, (i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
);
538 * ...and the same for transmit.
540 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
541 fep
->tx_skbuff
[i
] = NULL
;
542 CBDW_BUFADDR(bdp
, 0);
544 CBDW_SC(bdp
, (i
< fep
->tx_ring
- 1) ? 0 : BD_SC_WRAP
);
548 void fs_cleanup_bds(struct net_device
*dev
)
550 struct fs_enet_private
*fep
= netdev_priv(dev
);
556 * Reset SKB transmit buffers.
558 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
559 if ((skb
= fep
->tx_skbuff
[i
]) == NULL
)
563 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
564 skb
->len
, DMA_TO_DEVICE
);
566 fep
->tx_skbuff
[i
] = NULL
;
571 * Reset SKB receive buffers
573 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
574 if ((skb
= fep
->rx_skbuff
[i
]) == NULL
)
578 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
579 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
582 fep
->rx_skbuff
[i
] = NULL
;
588 /**********************************************************************************/
590 static int fs_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
592 struct fs_enet_private
*fep
= netdev_priv(dev
);
598 spin_lock_irqsave(&fep
->tx_lock
, flags
);
601 * Fill in a Tx ring entry
605 if (!fep
->tx_free
|| (CBDR_SC(bdp
) & BD_ENET_TX_READY
)) {
606 netif_stop_queue(dev
);
607 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
610 * Ooops. All transmit buffers are full. Bail out.
611 * This should not happen, since the tx queue should be stopped.
613 printk(KERN_WARNING DRV_MODULE_NAME
614 ": %s tx queue full!.\n", dev
->name
);
615 return NETDEV_TX_BUSY
;
618 curidx
= bdp
- fep
->tx_bd_base
;
620 * Clear all of the status flags.
622 CBDC_SC(bdp
, BD_ENET_TX_STATS
);
627 fep
->tx_skbuff
[curidx
] = skb
;
629 fep
->stats
.tx_bytes
+= skb
->len
;
632 * Push the data cache so the CPM does not get stale memory data.
634 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
,
635 skb
->data
, skb
->len
, DMA_TO_DEVICE
));
636 CBDW_DATLEN(bdp
, skb
->len
);
638 dev
->trans_start
= jiffies
;
641 * If this was the last BD in the ring, start at the beginning again.
643 if ((CBDR_SC(bdp
) & BD_ENET_TX_WRAP
) == 0)
646 fep
->cur_tx
= fep
->tx_bd_base
;
649 netif_stop_queue(dev
);
651 /* Trigger transmission start */
652 sc
= BD_ENET_TX_READY
| BD_ENET_TX_INTR
|
653 BD_ENET_TX_LAST
| BD_ENET_TX_TC
;
655 /* note that while FEC does not have this bit
656 * it marks it as available for software use
657 * yay for hw reuse :) */
659 sc
|= BD_ENET_TX_PAD
;
662 (*fep
->ops
->tx_kickstart
)(dev
);
664 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
669 static int fs_request_irq(struct net_device
*dev
, int irq
, const char *name
,
672 struct fs_enet_private
*fep
= netdev_priv(dev
);
674 (*fep
->ops
->pre_request_irq
)(dev
, irq
);
675 return request_irq(irq
, irqf
, IRQF_SHARED
, name
, dev
);
678 static void fs_free_irq(struct net_device
*dev
, int irq
)
680 struct fs_enet_private
*fep
= netdev_priv(dev
);
683 (*fep
->ops
->post_free_irq
)(dev
, irq
);
686 static void fs_timeout(struct net_device
*dev
)
688 struct fs_enet_private
*fep
= netdev_priv(dev
);
692 fep
->stats
.tx_errors
++;
694 spin_lock_irqsave(&fep
->lock
, flags
);
696 if (dev
->flags
& IFF_UP
) {
697 phy_stop(fep
->phydev
);
698 (*fep
->ops
->stop
)(dev
);
699 (*fep
->ops
->restart
)(dev
);
700 phy_start(fep
->phydev
);
703 phy_start(fep
->phydev
);
704 wake
= fep
->tx_free
&& !(CBDR_SC(fep
->cur_tx
) & BD_ENET_TX_READY
);
705 spin_unlock_irqrestore(&fep
->lock
, flags
);
708 netif_wake_queue(dev
);
711 /*-----------------------------------------------------------------------------
712 * generic link-change handler - should be sufficient for most cases
713 *-----------------------------------------------------------------------------*/
714 static void generic_adjust_link(struct net_device
*dev
)
716 struct fs_enet_private
*fep
= netdev_priv(dev
);
717 struct phy_device
*phydev
= fep
->phydev
;
722 /* adjust to duplex mode */
723 if (phydev
->duplex
!= fep
->oldduplex
){
725 fep
->oldduplex
= phydev
->duplex
;
728 if (phydev
->speed
!= fep
->oldspeed
) {
730 fep
->oldspeed
= phydev
->speed
;
737 netif_carrier_on(dev
);
738 netif_start_queue(dev
);
742 fep
->ops
->restart(dev
);
744 } else if (fep
->oldlink
) {
749 netif_carrier_off(dev
);
750 netif_stop_queue(dev
);
753 if (new_state
&& netif_msg_link(fep
))
754 phy_print_status(phydev
);
758 static void fs_adjust_link(struct net_device
*dev
)
760 struct fs_enet_private
*fep
= netdev_priv(dev
);
763 spin_lock_irqsave(&fep
->lock
, flags
);
765 if(fep
->ops
->adjust_link
)
766 fep
->ops
->adjust_link(dev
);
768 generic_adjust_link(dev
);
770 spin_unlock_irqrestore(&fep
->lock
, flags
);
773 static int fs_init_phy(struct net_device
*dev
)
775 struct fs_enet_private
*fep
= netdev_priv(dev
);
776 struct phy_device
*phydev
;
782 phydev
= phy_connect(dev
, fep
->fpi
->bus_id
, &fs_adjust_link
, 0,
783 PHY_INTERFACE_MODE_MII
);
785 printk("No phy bus ID specified in BSP code\n");
788 if (IS_ERR(phydev
)) {
789 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
790 return PTR_ERR(phydev
);
793 fep
->phydev
= phydev
;
799 static int fs_enet_open(struct net_device
*dev
)
801 struct fs_enet_private
*fep
= netdev_priv(dev
);
805 /* Install our interrupt handler. */
806 r
= fs_request_irq(dev
, fep
->interrupt
, "fs_enet-mac", fs_enet_interrupt
);
808 printk(KERN_ERR DRV_MODULE_NAME
809 ": %s Could not allocate FS_ENET IRQ!", dev
->name
);
813 err
= fs_init_phy(dev
);
817 phy_start(fep
->phydev
);
822 static int fs_enet_close(struct net_device
*dev
)
824 struct fs_enet_private
*fep
= netdev_priv(dev
);
827 netif_stop_queue(dev
);
828 netif_carrier_off(dev
);
829 phy_stop(fep
->phydev
);
831 spin_lock_irqsave(&fep
->lock
, flags
);
832 (*fep
->ops
->stop
)(dev
);
833 spin_unlock_irqrestore(&fep
->lock
, flags
);
835 /* release any irqs */
836 phy_disconnect(fep
->phydev
);
838 fs_free_irq(dev
, fep
->interrupt
);
843 static struct net_device_stats
*fs_enet_get_stats(struct net_device
*dev
)
845 struct fs_enet_private
*fep
= netdev_priv(dev
);
849 /*************************************************************************/
851 static void fs_get_drvinfo(struct net_device
*dev
,
852 struct ethtool_drvinfo
*info
)
854 strcpy(info
->driver
, DRV_MODULE_NAME
);
855 strcpy(info
->version
, DRV_MODULE_VERSION
);
858 static int fs_get_regs_len(struct net_device
*dev
)
860 struct fs_enet_private
*fep
= netdev_priv(dev
);
862 return (*fep
->ops
->get_regs_len
)(dev
);
865 static void fs_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
868 struct fs_enet_private
*fep
= netdev_priv(dev
);
874 spin_lock_irqsave(&fep
->lock
, flags
);
875 r
= (*fep
->ops
->get_regs
)(dev
, p
, &len
);
876 spin_unlock_irqrestore(&fep
->lock
, flags
);
882 static int fs_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
884 struct fs_enet_private
*fep
= netdev_priv(dev
);
885 return phy_ethtool_gset(fep
->phydev
, cmd
);
888 static int fs_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
890 struct fs_enet_private
*fep
= netdev_priv(dev
);
891 phy_ethtool_sset(fep
->phydev
, cmd
);
895 static int fs_nway_reset(struct net_device
*dev
)
900 static u32
fs_get_msglevel(struct net_device
*dev
)
902 struct fs_enet_private
*fep
= netdev_priv(dev
);
903 return fep
->msg_enable
;
906 static void fs_set_msglevel(struct net_device
*dev
, u32 value
)
908 struct fs_enet_private
*fep
= netdev_priv(dev
);
909 fep
->msg_enable
= value
;
912 static const struct ethtool_ops fs_ethtool_ops
= {
913 .get_drvinfo
= fs_get_drvinfo
,
914 .get_regs_len
= fs_get_regs_len
,
915 .get_settings
= fs_get_settings
,
916 .set_settings
= fs_set_settings
,
917 .nway_reset
= fs_nway_reset
,
918 .get_link
= ethtool_op_get_link
,
919 .get_msglevel
= fs_get_msglevel
,
920 .set_msglevel
= fs_set_msglevel
,
921 .get_tx_csum
= ethtool_op_get_tx_csum
,
922 .set_tx_csum
= ethtool_op_set_tx_csum
, /* local! */
923 .get_sg
= ethtool_op_get_sg
,
924 .set_sg
= ethtool_op_set_sg
,
925 .get_regs
= fs_get_regs
,
928 static int fs_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
930 struct fs_enet_private
*fep
= netdev_priv(dev
);
931 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*)&rq
->ifr_data
;
935 if (!netif_running(dev
))
938 spin_lock_irqsave(&fep
->lock
, flags
);
939 rc
= phy_mii_ioctl(fep
->phydev
, mii
, cmd
);
940 spin_unlock_irqrestore(&fep
->lock
, flags
);
944 extern int fs_mii_connect(struct net_device
*dev
);
945 extern void fs_mii_disconnect(struct net_device
*dev
);
947 static struct net_device
*fs_init_instance(struct device
*dev
,
948 struct fs_platform_info
*fpi
)
950 struct net_device
*ndev
= NULL
;
951 struct fs_enet_private
*fep
= NULL
;
952 int privsize
, i
, r
, err
= 0, registered
= 0;
954 fpi
->fs_no
= fs_get_id(fpi
);
956 if ((unsigned int)fpi
->fs_no
>= FS_MAX_INDEX
)
957 return ERR_PTR(-EINVAL
);
959 privsize
= sizeof(*fep
) + (sizeof(struct sk_buff
**) *
960 (fpi
->rx_ring
+ fpi
->tx_ring
));
962 ndev
= alloc_etherdev(privsize
);
967 SET_MODULE_OWNER(ndev
);
969 fep
= netdev_priv(ndev
);
970 memset(fep
, 0, privsize
); /* clear everything */
973 dev_set_drvdata(dev
, ndev
);
975 if (fpi
->init_ioports
)
976 fpi
->init_ioports((struct fs_platform_info
*)fpi
);
978 #ifdef CONFIG_FS_ENET_HAS_FEC
979 if (fs_get_fec_index(fpi
->fs_no
) >= 0)
980 fep
->ops
= &fs_fec_ops
;
983 #ifdef CONFIG_FS_ENET_HAS_SCC
984 if (fs_get_scc_index(fpi
->fs_no
) >=0 )
985 fep
->ops
= &fs_scc_ops
;
988 #ifdef CONFIG_FS_ENET_HAS_FCC
989 if (fs_get_fcc_index(fpi
->fs_no
) >= 0)
990 fep
->ops
= &fs_fcc_ops
;
993 if (fep
->ops
== NULL
) {
994 printk(KERN_ERR DRV_MODULE_NAME
995 ": %s No matching ops found (%d).\n",
996 ndev
->name
, fpi
->fs_no
);
1001 r
= (*fep
->ops
->setup_data
)(ndev
);
1003 printk(KERN_ERR DRV_MODULE_NAME
1004 ": %s setup_data failed\n",
1010 /* point rx_skbuff, tx_skbuff */
1011 fep
->rx_skbuff
= (struct sk_buff
**)&fep
[1];
1012 fep
->tx_skbuff
= fep
->rx_skbuff
+ fpi
->rx_ring
;
1015 spin_lock_init(&fep
->lock
);
1016 spin_lock_init(&fep
->tx_lock
);
1019 * Set the Ethernet address.
1021 for (i
= 0; i
< 6; i
++)
1022 ndev
->dev_addr
[i
] = fpi
->macaddr
[i
];
1024 r
= (*fep
->ops
->allocate_bd
)(ndev
);
1026 if (fep
->ring_base
== NULL
) {
1027 printk(KERN_ERR DRV_MODULE_NAME
1028 ": %s buffer descriptor alloc failed (%d).\n", ndev
->name
, r
);
1034 * Set receive and transmit descriptor base.
1036 fep
->rx_bd_base
= fep
->ring_base
;
1037 fep
->tx_bd_base
= fep
->rx_bd_base
+ fpi
->rx_ring
;
1039 /* initialize ring size variables */
1040 fep
->tx_ring
= fpi
->tx_ring
;
1041 fep
->rx_ring
= fpi
->rx_ring
;
1044 * The FEC Ethernet specific entries in the device structure.
1046 ndev
->open
= fs_enet_open
;
1047 ndev
->hard_start_xmit
= fs_enet_start_xmit
;
1048 ndev
->tx_timeout
= fs_timeout
;
1049 ndev
->watchdog_timeo
= 2 * HZ
;
1050 ndev
->stop
= fs_enet_close
;
1051 ndev
->get_stats
= fs_enet_get_stats
;
1052 ndev
->set_multicast_list
= fs_set_multicast_list
;
1053 if (fpi
->use_napi
) {
1054 ndev
->poll
= fs_enet_rx_napi
;
1055 ndev
->weight
= fpi
->napi_weight
;
1057 ndev
->ethtool_ops
= &fs_ethtool_ops
;
1058 ndev
->do_ioctl
= fs_ioctl
;
1060 init_timer(&fep
->phy_timer_list
);
1062 netif_carrier_off(ndev
);
1064 err
= register_netdev(ndev
);
1066 printk(KERN_ERR DRV_MODULE_NAME
1067 ": %s register_netdev failed.\n", ndev
->name
);
1079 unregister_netdev(ndev
);
1082 (*fep
->ops
->free_bd
)(ndev
);
1083 (*fep
->ops
->cleanup_data
)(ndev
);
1089 dev_set_drvdata(dev
, NULL
);
1091 return ERR_PTR(err
);
1094 static int fs_cleanup_instance(struct net_device
*ndev
)
1096 struct fs_enet_private
*fep
;
1097 const struct fs_platform_info
*fpi
;
1103 fep
= netdev_priv(ndev
);
1109 unregister_netdev(ndev
);
1111 dma_free_coherent(fep
->dev
, (fpi
->tx_ring
+ fpi
->rx_ring
) * sizeof(cbd_t
),
1112 fep
->ring_base
, fep
->ring_mem_addr
);
1115 (*fep
->ops
->cleanup_data
)(ndev
);
1119 dev_set_drvdata(dev
, NULL
);
1128 /**************************************************************************************/
1130 /* handy pointer to the immap */
1131 void *fs_enet_immap
= NULL
;
1133 static int setup_immap(void)
1135 phys_addr_t paddr
= 0;
1136 unsigned long size
= 0;
1140 size
= 0x10000; /* map 64K */
1144 paddr
= CPM_MAP_ADDR
;
1145 size
= 0x40000; /* map 256 K */
1147 fs_enet_immap
= ioremap(paddr
, size
);
1148 if (fs_enet_immap
== NULL
)
1149 return -EBADF
; /* XXX ahem; maybe just BUG_ON? */
1154 static void cleanup_immap(void)
1156 if (fs_enet_immap
!= NULL
) {
1157 iounmap(fs_enet_immap
);
1158 fs_enet_immap
= NULL
;
1162 /**************************************************************************************/
1164 static int __devinit
fs_enet_probe(struct device
*dev
)
1166 struct net_device
*ndev
;
1168 /* no fixup - no device */
1169 if (dev
->platform_data
== NULL
) {
1170 printk(KERN_INFO
"fs_enet: "
1171 "probe called with no platform data; "
1172 "remove unused devices\n");
1176 ndev
= fs_init_instance(dev
, dev
->platform_data
);
1178 return PTR_ERR(ndev
);
1182 static int fs_enet_remove(struct device
*dev
)
1184 return fs_cleanup_instance(dev_get_drvdata(dev
));
1187 static struct device_driver fs_enet_fec_driver
= {
1188 .name
= "fsl-cpm-fec",
1189 .bus
= &platform_bus_type
,
1190 .probe
= fs_enet_probe
,
1191 .remove
= fs_enet_remove
,
1193 /* .suspend = fs_enet_suspend, TODO */
1194 /* .resume = fs_enet_resume, TODO */
1198 static struct device_driver fs_enet_scc_driver
= {
1199 .name
= "fsl-cpm-scc",
1200 .bus
= &platform_bus_type
,
1201 .probe
= fs_enet_probe
,
1202 .remove
= fs_enet_remove
,
1204 /* .suspend = fs_enet_suspend, TODO */
1205 /* .resume = fs_enet_resume, TODO */
1209 static struct device_driver fs_enet_fcc_driver
= {
1210 .name
= "fsl-cpm-fcc",
1211 .bus
= &platform_bus_type
,
1212 .probe
= fs_enet_probe
,
1213 .remove
= fs_enet_remove
,
1215 /* .suspend = fs_enet_suspend, TODO */
1216 /* .resume = fs_enet_resume, TODO */
1220 static int __init
fs_init(void)
1231 #ifdef CONFIG_FS_ENET_HAS_FCC
1232 /* let's insert mii stuff */
1233 r
= fs_enet_mdio_bb_init();
1236 printk(KERN_ERR DRV_MODULE_NAME
1237 "BB PHY init failed.\n");
1240 r
= driver_register(&fs_enet_fcc_driver
);
1245 #ifdef CONFIG_FS_ENET_HAS_FEC
1246 r
= fs_enet_mdio_fec_init();
1248 printk(KERN_ERR DRV_MODULE_NAME
1249 "FEC PHY init failed.\n");
1253 r
= driver_register(&fs_enet_fec_driver
);
1258 #ifdef CONFIG_FS_ENET_HAS_SCC
1259 r
= driver_register(&fs_enet_scc_driver
);
1271 static void __exit
fs_cleanup(void)
1273 driver_unregister(&fs_enet_fec_driver
);
1274 driver_unregister(&fs_enet_fcc_driver
);
1275 driver_unregister(&fs_enet_scc_driver
);
1279 /**************************************************************************************/
1281 module_init(fs_init
);
1282 module_exit(fs_cleanup
);