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>
39 #include <linux/of_platform.h>
40 #include <linux/of_gpio.h>
42 #include <linux/vmalloc.h>
43 #include <asm/pgtable.h>
45 #include <asm/uaccess.h>
49 /*************************************************/
51 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
52 MODULE_DESCRIPTION("Freescale Ethernet Driver");
53 MODULE_LICENSE("GPL");
54 MODULE_VERSION(DRV_MODULE_VERSION
);
56 static int fs_enet_debug
= -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
57 module_param(fs_enet_debug
, int, 0);
58 MODULE_PARM_DESC(fs_enet_debug
,
59 "Freescale bitmapped debugging message enable value");
61 #ifdef CONFIG_NET_POLL_CONTROLLER
62 static void fs_enet_netpoll(struct net_device
*dev
);
65 static void fs_set_multicast_list(struct net_device
*dev
)
67 struct fs_enet_private
*fep
= netdev_priv(dev
);
69 (*fep
->ops
->set_multicast_list
)(dev
);
72 static void skb_align(struct sk_buff
*skb
, int align
)
74 int off
= ((unsigned long)skb
->data
) & (align
- 1);
77 skb_reserve(skb
, align
- off
);
80 /* NAPI receive function */
81 static int fs_enet_rx_napi(struct napi_struct
*napi
, int budget
)
83 struct fs_enet_private
*fep
= container_of(napi
, struct fs_enet_private
, napi
);
84 struct net_device
*dev
= fep
->ndev
;
85 const struct fs_platform_info
*fpi
= fep
->fpi
;
87 struct sk_buff
*skb
, *skbn
, *skbt
;
93 * First, grab all of the stats for the incoming packet.
94 * These get messed up if we get called due to a busy condition.
98 /* clear RX status bits for napi*/
99 (*fep
->ops
->napi_clear_rx_event
)(dev
);
101 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
),
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
);
179 printk(KERN_WARNING DRV_MODULE_NAME
180 ": %s Memory squeeze, dropping packet.\n",
182 fep
->stats
.rx_dropped
++;
187 fep
->rx_skbuff
[curidx
] = skbn
;
188 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
, skbn
->data
,
189 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
192 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
195 * Update BD pointer to next entry.
197 if ((sc
& BD_ENET_RX_WRAP
) == 0)
200 bdp
= fep
->rx_bd_base
;
202 (*fep
->ops
->rx_bd_done
)(dev
);
204 if (received
>= budget
)
210 if (received
< budget
) {
212 netif_rx_complete(dev
, napi
);
213 (*fep
->ops
->napi_enable_rx
)(dev
);
218 /* non NAPI receive function */
219 static int fs_enet_rx_non_napi(struct net_device
*dev
)
221 struct fs_enet_private
*fep
= netdev_priv(dev
);
222 const struct fs_platform_info
*fpi
= fep
->fpi
;
224 struct sk_buff
*skb
, *skbn
, *skbt
;
229 * First, grab all of the stats for the incoming packet.
230 * These get messed up if we get called due to a busy condition.
234 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
236 curidx
= bdp
- fep
->rx_bd_base
;
239 * Since we have allocated space to hold a complete frame,
240 * the last indicator should be set.
242 if ((sc
& BD_ENET_RX_LAST
) == 0)
243 printk(KERN_WARNING DRV_MODULE_NAME
244 ": %s rcv is not +last\n",
250 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_CL
|
251 BD_ENET_RX_NO
| BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
252 fep
->stats
.rx_errors
++;
253 /* Frame too long or too short. */
254 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
))
255 fep
->stats
.rx_length_errors
++;
256 /* Frame alignment */
257 if (sc
& (BD_ENET_RX_NO
| BD_ENET_RX_CL
))
258 fep
->stats
.rx_frame_errors
++;
260 if (sc
& BD_ENET_RX_CR
)
261 fep
->stats
.rx_crc_errors
++;
263 if (sc
& BD_ENET_RX_OV
)
264 fep
->stats
.rx_crc_errors
++;
266 skb
= fep
->rx_skbuff
[curidx
];
268 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
269 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
276 skb
= fep
->rx_skbuff
[curidx
];
278 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
279 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
283 * Process the incoming frame.
285 fep
->stats
.rx_packets
++;
286 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
287 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
289 if (pkt_len
<= fpi
->rx_copybreak
) {
290 /* +2 to make IP header L1 cache aligned */
291 skbn
= dev_alloc_skb(pkt_len
+ 2);
293 skb_reserve(skbn
, 2); /* align IP header */
294 skb_copy_from_linear_data(skb
,
295 skbn
->data
, pkt_len
);
302 skbn
= dev_alloc_skb(ENET_RX_FRSIZE
);
305 skb_align(skbn
, ENET_RX_ALIGN
);
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
->tx_lock
);
356 do_wake
= do_restart
= 0;
357 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_TX_READY
) == 0) {
358 dirtyidx
= bdp
- fep
->tx_bd_base
;
360 if (fep
->tx_free
== fep
->tx_ring
)
363 skb
= fep
->tx_skbuff
[dirtyidx
];
368 if (sc
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
369 BD_ENET_TX_RL
| BD_ENET_TX_UN
| BD_ENET_TX_CSL
)) {
371 if (sc
& BD_ENET_TX_HB
) /* No heartbeat */
372 fep
->stats
.tx_heartbeat_errors
++;
373 if (sc
& BD_ENET_TX_LC
) /* Late collision */
374 fep
->stats
.tx_window_errors
++;
375 if (sc
& BD_ENET_TX_RL
) /* Retrans limit */
376 fep
->stats
.tx_aborted_errors
++;
377 if (sc
& BD_ENET_TX_UN
) /* Underrun */
378 fep
->stats
.tx_fifo_errors
++;
379 if (sc
& BD_ENET_TX_CSL
) /* Carrier lost */
380 fep
->stats
.tx_carrier_errors
++;
382 if (sc
& (BD_ENET_TX_LC
| BD_ENET_TX_RL
| BD_ENET_TX_UN
)) {
383 fep
->stats
.tx_errors
++;
387 fep
->stats
.tx_packets
++;
389 if (sc
& BD_ENET_TX_READY
)
390 printk(KERN_WARNING DRV_MODULE_NAME
391 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
395 * Deferred means some collisions occurred during transmit,
396 * but we eventually sent the packet OK.
398 if (sc
& BD_ENET_TX_DEF
)
399 fep
->stats
.collisions
++;
402 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
403 skb
->len
, DMA_TO_DEVICE
);
406 * Free the sk buffer associated with this last transmit.
408 dev_kfree_skb_irq(skb
);
409 fep
->tx_skbuff
[dirtyidx
] = NULL
;
412 * Update pointer to next buffer descriptor to be transmitted.
414 if ((sc
& BD_ENET_TX_WRAP
) == 0)
417 bdp
= fep
->tx_bd_base
;
420 * Since we have freed up a buffer, the ring is no longer
430 (*fep
->ops
->tx_restart
)(dev
);
432 spin_unlock(&fep
->tx_lock
);
435 netif_wake_queue(dev
);
439 * The interrupt handler.
440 * This is called from the MPC core interrupt.
443 fs_enet_interrupt(int irq
, void *dev_id
)
445 struct net_device
*dev
= dev_id
;
446 struct fs_enet_private
*fep
;
447 const struct fs_platform_info
*fpi
;
453 fep
= netdev_priv(dev
);
457 while ((int_events
= (*fep
->ops
->get_int_events
)(dev
)) != 0) {
460 int_clr_events
= int_events
;
462 int_clr_events
&= ~fep
->ev_napi_rx
;
464 (*fep
->ops
->clear_int_events
)(dev
, int_clr_events
);
466 if (int_events
& fep
->ev_err
)
467 (*fep
->ops
->ev_error
)(dev
, int_events
);
469 if (int_events
& fep
->ev_rx
) {
471 fs_enet_rx_non_napi(dev
);
473 napi_ok
= napi_schedule_prep(&fep
->napi
);
475 (*fep
->ops
->napi_disable_rx
)(dev
);
476 (*fep
->ops
->clear_int_events
)(dev
, fep
->ev_napi_rx
);
478 /* NOTE: it is possible for FCCs in NAPI mode */
479 /* to submit a spurious interrupt while in poll */
481 __netif_rx_schedule(dev
, &fep
->napi
);
485 if (int_events
& fep
->ev_tx
)
490 return IRQ_RETVAL(handled
);
493 void fs_init_bds(struct net_device
*dev
)
495 struct fs_enet_private
*fep
= netdev_priv(dev
);
502 fep
->dirty_tx
= fep
->cur_tx
= fep
->tx_bd_base
;
503 fep
->tx_free
= fep
->tx_ring
;
504 fep
->cur_rx
= fep
->rx_bd_base
;
507 * Initialize the receive buffer descriptors.
509 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
510 skb
= dev_alloc_skb(ENET_RX_FRSIZE
);
512 printk(KERN_WARNING DRV_MODULE_NAME
513 ": %s Memory squeeze, unable to allocate skb\n",
517 skb_align(skb
, ENET_RX_ALIGN
);
518 fep
->rx_skbuff
[i
] = skb
;
520 dma_map_single(fep
->dev
, skb
->data
,
521 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
523 CBDW_DATLEN(bdp
, 0); /* zero */
524 CBDW_SC(bdp
, BD_ENET_RX_EMPTY
|
525 ((i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
));
528 * if we failed, fillup remainder
530 for (; i
< fep
->rx_ring
; i
++, bdp
++) {
531 fep
->rx_skbuff
[i
] = NULL
;
532 CBDW_SC(bdp
, (i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
);
536 * ...and the same for transmit.
538 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
539 fep
->tx_skbuff
[i
] = NULL
;
540 CBDW_BUFADDR(bdp
, 0);
542 CBDW_SC(bdp
, (i
< fep
->tx_ring
- 1) ? 0 : BD_SC_WRAP
);
546 void fs_cleanup_bds(struct net_device
*dev
)
548 struct fs_enet_private
*fep
= netdev_priv(dev
);
554 * Reset SKB transmit buffers.
556 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
557 if ((skb
= fep
->tx_skbuff
[i
]) == NULL
)
561 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
562 skb
->len
, DMA_TO_DEVICE
);
564 fep
->tx_skbuff
[i
] = NULL
;
569 * Reset SKB receive buffers
571 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
572 if ((skb
= fep
->rx_skbuff
[i
]) == NULL
)
576 dma_unmap_single(fep
->dev
, CBDR_BUFADDR(bdp
),
577 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
580 fep
->rx_skbuff
[i
] = NULL
;
586 /**********************************************************************************/
588 static int fs_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
590 struct fs_enet_private
*fep
= netdev_priv(dev
);
596 spin_lock_irqsave(&fep
->tx_lock
, flags
);
599 * Fill in a Tx ring entry
603 if (!fep
->tx_free
|| (CBDR_SC(bdp
) & BD_ENET_TX_READY
)) {
604 netif_stop_queue(dev
);
605 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
608 * Ooops. All transmit buffers are full. Bail out.
609 * This should not happen, since the tx queue should be stopped.
611 printk(KERN_WARNING DRV_MODULE_NAME
612 ": %s tx queue full!.\n", dev
->name
);
613 return NETDEV_TX_BUSY
;
616 curidx
= bdp
- fep
->tx_bd_base
;
618 * Clear all of the status flags.
620 CBDC_SC(bdp
, BD_ENET_TX_STATS
);
625 fep
->tx_skbuff
[curidx
] = skb
;
627 fep
->stats
.tx_bytes
+= skb
->len
;
630 * Push the data cache so the CPM does not get stale memory data.
632 CBDW_BUFADDR(bdp
, dma_map_single(fep
->dev
,
633 skb
->data
, skb
->len
, DMA_TO_DEVICE
));
634 CBDW_DATLEN(bdp
, skb
->len
);
636 dev
->trans_start
= jiffies
;
639 * If this was the last BD in the ring, start at the beginning again.
641 if ((CBDR_SC(bdp
) & BD_ENET_TX_WRAP
) == 0)
644 fep
->cur_tx
= fep
->tx_bd_base
;
647 netif_stop_queue(dev
);
649 /* Trigger transmission start */
650 sc
= BD_ENET_TX_READY
| BD_ENET_TX_INTR
|
651 BD_ENET_TX_LAST
| BD_ENET_TX_TC
;
653 /* note that while FEC does not have this bit
654 * it marks it as available for software use
655 * yay for hw reuse :) */
657 sc
|= BD_ENET_TX_PAD
;
660 (*fep
->ops
->tx_kickstart
)(dev
);
662 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
667 static int fs_request_irq(struct net_device
*dev
, int irq
, const char *name
,
670 struct fs_enet_private
*fep
= netdev_priv(dev
);
672 (*fep
->ops
->pre_request_irq
)(dev
, irq
);
673 return request_irq(irq
, irqf
, IRQF_SHARED
, name
, dev
);
676 static void fs_free_irq(struct net_device
*dev
, int irq
)
678 struct fs_enet_private
*fep
= netdev_priv(dev
);
681 (*fep
->ops
->post_free_irq
)(dev
, irq
);
684 static void fs_timeout(struct net_device
*dev
)
686 struct fs_enet_private
*fep
= netdev_priv(dev
);
690 fep
->stats
.tx_errors
++;
692 spin_lock_irqsave(&fep
->lock
, flags
);
694 if (dev
->flags
& IFF_UP
) {
695 phy_stop(fep
->phydev
);
696 (*fep
->ops
->stop
)(dev
);
697 (*fep
->ops
->restart
)(dev
);
698 phy_start(fep
->phydev
);
701 phy_start(fep
->phydev
);
702 wake
= fep
->tx_free
&& !(CBDR_SC(fep
->cur_tx
) & BD_ENET_TX_READY
);
703 spin_unlock_irqrestore(&fep
->lock
, flags
);
706 netif_wake_queue(dev
);
709 /*-----------------------------------------------------------------------------
710 * generic link-change handler - should be sufficient for most cases
711 *-----------------------------------------------------------------------------*/
712 static void generic_adjust_link(struct net_device
*dev
)
714 struct fs_enet_private
*fep
= netdev_priv(dev
);
715 struct phy_device
*phydev
= fep
->phydev
;
719 /* adjust to duplex mode */
720 if (phydev
->duplex
!= fep
->oldduplex
) {
722 fep
->oldduplex
= phydev
->duplex
;
725 if (phydev
->speed
!= fep
->oldspeed
) {
727 fep
->oldspeed
= phydev
->speed
;
736 fep
->ops
->restart(dev
);
737 } else if (fep
->oldlink
) {
744 if (new_state
&& netif_msg_link(fep
))
745 phy_print_status(phydev
);
749 static void fs_adjust_link(struct net_device
*dev
)
751 struct fs_enet_private
*fep
= netdev_priv(dev
);
754 spin_lock_irqsave(&fep
->lock
, flags
);
756 if(fep
->ops
->adjust_link
)
757 fep
->ops
->adjust_link(dev
);
759 generic_adjust_link(dev
);
761 spin_unlock_irqrestore(&fep
->lock
, flags
);
764 static int fs_init_phy(struct net_device
*dev
)
766 struct fs_enet_private
*fep
= netdev_priv(dev
);
767 struct phy_device
*phydev
;
773 phydev
= phy_connect(dev
, fep
->fpi
->bus_id
, &fs_adjust_link
, 0,
774 PHY_INTERFACE_MODE_MII
);
776 printk("No phy bus ID specified in BSP code\n");
779 if (IS_ERR(phydev
)) {
780 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
781 return PTR_ERR(phydev
);
784 fep
->phydev
= phydev
;
789 static int fs_enet_open(struct net_device
*dev
)
791 struct fs_enet_private
*fep
= netdev_priv(dev
);
795 /* to initialize the fep->cur_rx,... */
796 /* not doing this, will cause a crash in fs_enet_rx_napi */
797 fs_init_bds(fep
->ndev
);
799 if (fep
->fpi
->use_napi
)
800 napi_enable(&fep
->napi
);
802 /* Install our interrupt handler. */
803 r
= fs_request_irq(dev
, fep
->interrupt
, "fs_enet-mac", fs_enet_interrupt
);
805 printk(KERN_ERR DRV_MODULE_NAME
806 ": %s Could not allocate FS_ENET IRQ!", dev
->name
);
807 if (fep
->fpi
->use_napi
)
808 napi_disable(&fep
->napi
);
812 err
= fs_init_phy(dev
);
814 if (fep
->fpi
->use_napi
)
815 napi_disable(&fep
->napi
);
818 phy_start(fep
->phydev
);
820 netif_start_queue(dev
);
825 static int fs_enet_close(struct net_device
*dev
)
827 struct fs_enet_private
*fep
= netdev_priv(dev
);
830 netif_stop_queue(dev
);
831 netif_carrier_off(dev
);
832 if (fep
->fpi
->use_napi
)
833 napi_disable(&fep
->napi
);
834 phy_stop(fep
->phydev
);
836 spin_lock_irqsave(&fep
->lock
, flags
);
837 spin_lock(&fep
->tx_lock
);
838 (*fep
->ops
->stop
)(dev
);
839 spin_unlock(&fep
->tx_lock
);
840 spin_unlock_irqrestore(&fep
->lock
, flags
);
842 /* release any irqs */
843 phy_disconnect(fep
->phydev
);
845 fs_free_irq(dev
, fep
->interrupt
);
850 static struct net_device_stats
*fs_enet_get_stats(struct net_device
*dev
)
852 struct fs_enet_private
*fep
= netdev_priv(dev
);
856 /*************************************************************************/
858 static void fs_get_drvinfo(struct net_device
*dev
,
859 struct ethtool_drvinfo
*info
)
861 strcpy(info
->driver
, DRV_MODULE_NAME
);
862 strcpy(info
->version
, DRV_MODULE_VERSION
);
865 static int fs_get_regs_len(struct net_device
*dev
)
867 struct fs_enet_private
*fep
= netdev_priv(dev
);
869 return (*fep
->ops
->get_regs_len
)(dev
);
872 static void fs_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
875 struct fs_enet_private
*fep
= netdev_priv(dev
);
881 spin_lock_irqsave(&fep
->lock
, flags
);
882 r
= (*fep
->ops
->get_regs
)(dev
, p
, &len
);
883 spin_unlock_irqrestore(&fep
->lock
, flags
);
889 static int fs_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
891 struct fs_enet_private
*fep
= netdev_priv(dev
);
896 return phy_ethtool_gset(fep
->phydev
, cmd
);
899 static int fs_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
901 struct fs_enet_private
*fep
= netdev_priv(dev
);
906 return phy_ethtool_sset(fep
->phydev
, cmd
);
909 static int fs_nway_reset(struct net_device
*dev
)
914 static u32
fs_get_msglevel(struct net_device
*dev
)
916 struct fs_enet_private
*fep
= netdev_priv(dev
);
917 return fep
->msg_enable
;
920 static void fs_set_msglevel(struct net_device
*dev
, u32 value
)
922 struct fs_enet_private
*fep
= netdev_priv(dev
);
923 fep
->msg_enable
= value
;
926 static const struct ethtool_ops fs_ethtool_ops
= {
927 .get_drvinfo
= fs_get_drvinfo
,
928 .get_regs_len
= fs_get_regs_len
,
929 .get_settings
= fs_get_settings
,
930 .set_settings
= fs_set_settings
,
931 .nway_reset
= fs_nway_reset
,
932 .get_link
= ethtool_op_get_link
,
933 .get_msglevel
= fs_get_msglevel
,
934 .set_msglevel
= fs_set_msglevel
,
935 .set_tx_csum
= ethtool_op_set_tx_csum
, /* local! */
936 .set_sg
= ethtool_op_set_sg
,
937 .get_regs
= fs_get_regs
,
940 static int fs_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
942 struct fs_enet_private
*fep
= netdev_priv(dev
);
943 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*)&rq
->ifr_data
;
945 if (!netif_running(dev
))
948 return phy_mii_ioctl(fep
->phydev
, mii
, cmd
);
951 extern int fs_mii_connect(struct net_device
*dev
);
952 extern void fs_mii_disconnect(struct net_device
*dev
);
954 /**************************************************************************************/
956 /* handy pointer to the immap */
957 void __iomem
*fs_enet_immap
= NULL
;
959 static int setup_immap(void)
962 fs_enet_immap
= ioremap(IMAP_ADDR
, 0x4000);
963 WARN_ON(!fs_enet_immap
);
964 #elif defined(CONFIG_CPM2)
965 fs_enet_immap
= cpm2_immr
;
971 static void cleanup_immap(void)
973 #if defined(CONFIG_CPM1)
974 iounmap(fs_enet_immap
);
978 /**************************************************************************************/
980 static int __devinit
find_phy(struct device_node
*np
,
981 struct fs_platform_info
*fpi
)
983 struct device_node
*phynode
, *mdionode
;
984 int ret
= 0, len
, bus_id
;
987 data
= of_get_property(np
, "fixed-link", NULL
);
989 snprintf(fpi
->bus_id
, 16, "%x:%02x", 0, *data
);
993 data
= of_get_property(np
, "phy-handle", &len
);
994 if (!data
|| len
!= 4)
997 phynode
= of_find_node_by_phandle(*data
);
1001 data
= of_get_property(phynode
, "reg", &len
);
1002 if (!data
|| len
!= 4) {
1007 mdionode
= of_get_parent(phynode
);
1013 bus_id
= of_get_gpio(mdionode
, 0);
1015 struct resource res
;
1016 ret
= of_address_to_resource(mdionode
, 0, &res
);
1022 snprintf(fpi
->bus_id
, 16, "%x:%02x", bus_id
, *data
);
1025 of_node_put(mdionode
);
1027 of_node_put(phynode
);
1031 #ifdef CONFIG_FS_ENET_HAS_FEC
1032 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
1034 #define IS_FEC(match) 0
1037 static int __devinit
fs_enet_probe(struct of_device
*ofdev
,
1038 const struct of_device_id
*match
)
1040 struct net_device
*ndev
;
1041 struct fs_enet_private
*fep
;
1042 struct fs_platform_info
*fpi
;
1045 int privsize
, len
, ret
= -ENODEV
;
1047 fpi
= kzalloc(sizeof(*fpi
), GFP_KERNEL
);
1051 if (!IS_FEC(match
)) {
1052 data
= of_get_property(ofdev
->node
, "fsl,cpm-command", &len
);
1053 if (!data
|| len
!= 4)
1056 fpi
->cp_command
= *data
;
1061 fpi
->rx_copybreak
= 240;
1063 fpi
->napi_weight
= 17;
1065 ret
= find_phy(ofdev
->node
, fpi
);
1069 privsize
= sizeof(*fep
) +
1070 sizeof(struct sk_buff
**) *
1071 (fpi
->rx_ring
+ fpi
->tx_ring
);
1073 ndev
= alloc_etherdev(privsize
);
1079 dev_set_drvdata(&ofdev
->dev
, ndev
);
1081 fep
= netdev_priv(ndev
);
1082 fep
->dev
= &ofdev
->dev
;
1085 fep
->ops
= match
->data
;
1087 ret
= fep
->ops
->setup_data(ndev
);
1091 fep
->rx_skbuff
= (struct sk_buff
**)&fep
[1];
1092 fep
->tx_skbuff
= fep
->rx_skbuff
+ fpi
->rx_ring
;
1094 spin_lock_init(&fep
->lock
);
1095 spin_lock_init(&fep
->tx_lock
);
1097 mac_addr
= of_get_mac_address(ofdev
->node
);
1099 memcpy(ndev
->dev_addr
, mac_addr
, 6);
1101 ret
= fep
->ops
->allocate_bd(ndev
);
1103 goto out_cleanup_data
;
1105 fep
->rx_bd_base
= fep
->ring_base
;
1106 fep
->tx_bd_base
= fep
->rx_bd_base
+ fpi
->rx_ring
;
1108 fep
->tx_ring
= fpi
->tx_ring
;
1109 fep
->rx_ring
= fpi
->rx_ring
;
1111 ndev
->open
= fs_enet_open
;
1112 ndev
->hard_start_xmit
= fs_enet_start_xmit
;
1113 ndev
->tx_timeout
= fs_timeout
;
1114 ndev
->watchdog_timeo
= 2 * HZ
;
1115 ndev
->stop
= fs_enet_close
;
1116 ndev
->get_stats
= fs_enet_get_stats
;
1117 ndev
->set_multicast_list
= fs_set_multicast_list
;
1120 netif_napi_add(ndev
, &fep
->napi
, fs_enet_rx_napi
,
1123 ndev
->ethtool_ops
= &fs_ethtool_ops
;
1124 ndev
->do_ioctl
= fs_ioctl
;
1126 init_timer(&fep
->phy_timer_list
);
1128 netif_carrier_off(ndev
);
1130 ret
= register_netdev(ndev
);
1134 printk(KERN_INFO
"%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
1136 ndev
->dev_addr
[0], ndev
->dev_addr
[1], ndev
->dev_addr
[2],
1137 ndev
->dev_addr
[3], ndev
->dev_addr
[4], ndev
->dev_addr
[5]);
1142 fep
->ops
->free_bd(ndev
);
1144 fep
->ops
->cleanup_data(ndev
);
1147 dev_set_drvdata(&ofdev
->dev
, NULL
);
1153 static int fs_enet_remove(struct of_device
*ofdev
)
1155 struct net_device
*ndev
= dev_get_drvdata(&ofdev
->dev
);
1156 struct fs_enet_private
*fep
= netdev_priv(ndev
);
1158 unregister_netdev(ndev
);
1160 fep
->ops
->free_bd(ndev
);
1161 fep
->ops
->cleanup_data(ndev
);
1162 dev_set_drvdata(fep
->dev
, NULL
);
1168 static struct of_device_id fs_enet_match
[] = {
1169 #ifdef CONFIG_FS_ENET_HAS_SCC
1171 .compatible
= "fsl,cpm1-scc-enet",
1172 .data
= (void *)&fs_scc_ops
,
1175 .compatible
= "fsl,cpm2-scc-enet",
1176 .data
= (void *)&fs_scc_ops
,
1179 #ifdef CONFIG_FS_ENET_HAS_FCC
1181 .compatible
= "fsl,cpm2-fcc-enet",
1182 .data
= (void *)&fs_fcc_ops
,
1185 #ifdef CONFIG_FS_ENET_HAS_FEC
1187 .compatible
= "fsl,pq1-fec-enet",
1188 .data
= (void *)&fs_fec_ops
,
1194 static struct of_platform_driver fs_enet_driver
= {
1196 .match_table
= fs_enet_match
,
1197 .probe
= fs_enet_probe
,
1198 .remove
= fs_enet_remove
,
1201 static int __init
fs_init(void)
1203 int r
= setup_immap();
1207 r
= of_register_platform_driver(&fs_enet_driver
);
1218 static void __exit
fs_cleanup(void)
1220 of_unregister_platform_driver(&fs_enet_driver
);
1224 #ifdef CONFIG_NET_POLL_CONTROLLER
1225 static void fs_enet_netpoll(struct net_device
*dev
)
1227 disable_irq(dev
->irq
);
1228 fs_enet_interrupt(dev
->irq
, dev
, NULL
);
1229 enable_irq(dev
->irq
);
1233 /**************************************************************************************/
1235 module_init(fs_init
);
1236 module_exit(fs_cleanup
);