2 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
8 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
10 * Released under the GPL
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/ptrace.h>
20 #include <linux/errno.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/pci.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mii.h>
32 #include <linux/ethtool.h>
33 #include <linux/bitops.h>
35 #include <asm/8xx_immap.h>
36 #include <asm/pgtable.h>
37 #include <asm/mpc8xx.h>
39 #include <asm/uaccess.h>
40 #include <asm/commproc.h>
41 #include <asm/dma-mapping.h>
45 /*************************************************/
47 #define FEC_MAX_MULTICAST_ADDRS 64
49 /*************************************************/
51 static char version
[] __devinitdata
=
52 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")" "\n";
54 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
55 MODULE_DESCRIPTION("Motorola 8xx FEC ethernet driver");
56 MODULE_LICENSE("GPL");
58 int fec_8xx_debug
= -1; /* -1 == use FEC_8XX_DEF_MSG_ENABLE as value */
59 module_param(fec_8xx_debug
, int, 0);
60 MODULE_PARM_DESC(fec_8xx_debug
,
61 "FEC 8xx bitmapped debugging message enable value");
64 /*************************************************/
67 * Delay to wait for FEC reset command to complete (in us)
69 #define FEC_RESET_DELAY 50
71 /*****************************************************************************************/
73 static void fec_whack_reset(fec_t
* fecp
)
78 * Whack a reset. We should wait for this.
80 FW(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_RESET
);
82 (FR(fecp
, ecntrl
) & FEC_ECNTRL_RESET
) != 0 && i
< FEC_RESET_DELAY
;
86 if (i
== FEC_RESET_DELAY
)
87 printk(KERN_WARNING
"FEC Reset timeout!\n");
91 /****************************************************************************/
94 * Transmitter timeout.
96 #define TX_TIMEOUT (2*HZ)
98 /****************************************************************************/
101 * Returns the CRC needed when filling in the hash table for
102 * multicast group filtering
103 * pAddr must point to a MAC address (6 bytes)
105 static __u32
fec_mulicast_calc_crc(char *pAddr
)
110 __u32 crc
= 0xffffffff;
113 for (byte_count
= 0; byte_count
< 6; byte_count
++) {
114 byte
= pAddr
[byte_count
];
115 for (bit_count
= 0; bit_count
< 8; bit_count
++) {
118 if (msb
^ (byte
& 0x1)) {
128 * Set or clear the multicast filter for this adaptor.
129 * Skeleton taken from sunlance driver.
130 * The CPM Ethernet implementation allows Multicast as well as individual
131 * MAC address filtering. Some of the drivers check to make sure it is
132 * a group multicast address, and discard those that are not. I guess I
133 * will do the same for now, but just remove the test if you want
134 * individual filtering as well (do the upper net layers want or support
135 * this kind of feature?).
137 static void fec_set_multicast_list(struct net_device
*dev
)
139 struct fec_enet_private
*fep
= netdev_priv(dev
);
140 fec_t
*fecp
= fep
->fecp
;
141 struct dev_mc_list
*pmc
;
150 if ((dev
->flags
& IFF_PROMISC
) != 0) {
152 spin_lock_irqsave(&fep
->lock
, flags
);
153 FS(fecp
, r_cntrl
, FEC_RCNTRL_PROM
);
154 spin_unlock_irqrestore(&fep
->lock
, flags
);
159 printk(KERN_WARNING DRV_MODULE_NAME
160 ": %s: Promiscuous mode enabled.\n", dev
->name
);
165 if ((dev
->flags
& IFF_ALLMULTI
) != 0 ||
166 dev
->mc_count
> FEC_MAX_MULTICAST_ADDRS
) {
168 * Catch all multicast addresses, set the filter to all 1's.
177 * Now populate the hash table
179 for (pmc
= dev
->mc_list
; pmc
!= NULL
; pmc
= pmc
->next
) {
180 crc
= fec_mulicast_calc_crc(pmc
->dmi_addr
);
181 temp
= (crc
& 0x3f) >> 1;
182 hash_index
= ((temp
& 0x01) << 4) |
183 ((temp
& 0x02) << 2) |
185 ((temp
& 0x08) >> 2) |
186 ((temp
& 0x10) >> 4);
187 csrVal
= (1 << hash_index
);
195 spin_lock_irqsave(&fep
->lock
, flags
);
196 FC(fecp
, r_cntrl
, FEC_RCNTRL_PROM
);
197 FW(fecp
, hash_table_high
, hthi
);
198 FW(fecp
, hash_table_low
, htlo
);
199 spin_unlock_irqrestore(&fep
->lock
, flags
);
202 static int fec_set_mac_address(struct net_device
*dev
, void *addr
)
204 struct sockaddr
*mac
= addr
;
205 struct fec_enet_private
*fep
= netdev_priv(dev
);
206 struct fec
*fecp
= fep
->fecp
;
208 __u32 addrhi
, addrlo
;
211 /* Get pointer to SCC area in parameter RAM. */
212 for (i
= 0; i
< 6; i
++)
213 dev
->dev_addr
[i
] = mac
->sa_data
[i
];
216 * Set station address.
218 addrhi
= ((__u32
) dev
->dev_addr
[0] << 24) |
219 ((__u32
) dev
->dev_addr
[1] << 16) |
220 ((__u32
) dev
->dev_addr
[2] << 8) |
221 (__u32
) dev
->dev_addr
[3];
222 addrlo
= ((__u32
) dev
->dev_addr
[4] << 24) |
223 ((__u32
) dev
->dev_addr
[5] << 16);
225 spin_lock_irqsave(&fep
->lock
, flags
);
226 FW(fecp
, addr_low
, addrhi
);
227 FW(fecp
, addr_high
, addrlo
);
228 spin_unlock_irqrestore(&fep
->lock
, flags
);
234 * This function is called to start or restart the FEC during a link
235 * change. This only happens when switching between half and full
238 void fec_restart(struct net_device
*dev
, int duplex
, int speed
)
241 immap_t
*immap
= (immap_t
*) IMAP_ADDR
;
244 struct fec_enet_private
*fep
= netdev_priv(dev
);
245 struct fec
*fecp
= fep
->fecp
;
246 const struct fec_platform_info
*fpi
= fep
->fpi
;
250 __u32 addrhi
, addrlo
;
252 fec_whack_reset(fep
->fecp
);
255 * Set station address.
257 addrhi
= ((__u32
) dev
->dev_addr
[0] << 24) |
258 ((__u32
) dev
->dev_addr
[1] << 16) |
259 ((__u32
) dev
->dev_addr
[2] << 8) |
260 (__u32
) dev
->dev_addr
[3];
261 addrlo
= ((__u32
) dev
->dev_addr
[4] << 24) |
262 ((__u32
) dev
->dev_addr
[5] << 16);
263 FW(fecp
, addr_low
, addrhi
);
264 FW(fecp
, addr_high
, addrlo
);
267 * Reset all multicast.
269 FW(fecp
, hash_table_high
, 0);
270 FW(fecp
, hash_table_low
, 0);
273 * Set maximum receive buffer size.
275 FW(fecp
, r_buff_size
, PKT_MAXBLR_SIZE
);
276 FW(fecp
, r_hash
, PKT_MAXBUF_SIZE
);
279 * Set receive and transmit descriptor base.
281 FW(fecp
, r_des_start
, iopa((__u32
) (fep
->rx_bd_base
)));
282 FW(fecp
, x_des_start
, iopa((__u32
) (fep
->tx_bd_base
)));
284 fep
->dirty_tx
= fep
->cur_tx
= fep
->tx_bd_base
;
285 fep
->tx_free
= fep
->tx_ring
;
286 fep
->cur_rx
= fep
->rx_bd_base
;
289 * Reset SKB receive buffers
291 for (i
= 0; i
< fep
->rx_ring
; i
++) {
292 if ((skb
= fep
->rx_skbuff
[i
]) == NULL
)
294 fep
->rx_skbuff
[i
] = NULL
;
299 * Initialize the receive buffer descriptors.
301 for (i
= 0, bdp
= fep
->rx_bd_base
; i
< fep
->rx_ring
; i
++, bdp
++) {
302 skb
= dev_alloc_skb(ENET_RX_FRSIZE
);
304 printk(KERN_WARNING DRV_MODULE_NAME
305 ": %s Memory squeeze, unable to allocate skb\n",
307 fep
->stats
.rx_dropped
++;
310 fep
->rx_skbuff
[i
] = skb
;
312 CBDW_BUFADDR(bdp
, dma_map_single(NULL
, skb
->data
,
313 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
315 CBDW_DATLEN(bdp
, 0); /* zero */
316 CBDW_SC(bdp
, BD_ENET_RX_EMPTY
|
317 ((i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
));
320 * if we failed, fillup remainder
322 for (; i
< fep
->rx_ring
; i
++, bdp
++) {
323 fep
->rx_skbuff
[i
] = NULL
;
324 CBDW_SC(bdp
, (i
< fep
->rx_ring
- 1) ? 0 : BD_SC_WRAP
);
328 * Reset SKB transmit buffers.
330 for (i
= 0; i
< fep
->tx_ring
; i
++) {
331 if ((skb
= fep
->tx_skbuff
[i
]) == NULL
)
333 fep
->tx_skbuff
[i
] = NULL
;
338 * ...and the same for transmit.
340 for (i
= 0, bdp
= fep
->tx_bd_base
; i
< fep
->tx_ring
; i
++, bdp
++) {
341 fep
->tx_skbuff
[i
] = NULL
;
342 CBDW_BUFADDR(bdp
, virt_to_bus(NULL
));
344 CBDW_SC(bdp
, (i
< fep
->tx_ring
- 1) ? 0 : BD_SC_WRAP
);
348 * Enable big endian and don't care about SDMA FC.
350 FW(fecp
, fun_code
, 0x78000000);
355 FW(fecp
, mii_speed
, fep
->fec_phy_speed
);
358 * Clear any outstanding interrupt.
360 FW(fecp
, ievent
, 0xffc0);
361 FW(fecp
, ivec
, (fpi
->fec_irq
/ 2) << 29);
364 * adjust to speed (only for DUET & RMII)
367 cptr
= in_be32(&immap
->im_cpm
.cp_cptr
);
368 switch (fpi
->fec_no
) {
371 * check if in RMII mode
373 if ((cptr
& 0x100) == 0)
378 else if (speed
== 100)
383 * check if in RMII mode
385 if ((cptr
& 0x80) == 0)
390 else if (speed
== 100)
396 out_be32(&immap
->im_cpm
.cp_cptr
, cptr
);
399 FW(fecp
, r_cntrl
, FEC_RCNTRL_MII_MODE
); /* MII enable */
401 * adjust to duplex mode
404 FC(fecp
, r_cntrl
, FEC_RCNTRL_DRT
);
405 FS(fecp
, x_cntrl
, FEC_TCNTRL_FDEN
); /* FD enable */
407 FS(fecp
, r_cntrl
, FEC_RCNTRL_DRT
);
408 FC(fecp
, x_cntrl
, FEC_TCNTRL_FDEN
); /* FD disable */
412 * Enable interrupts we wish to service.
414 FW(fecp
, imask
, FEC_ENET_TXF
| FEC_ENET_TXB
|
415 FEC_ENET_RXF
| FEC_ENET_RXB
);
418 * And last, enable the transmit and receive processing.
420 FW(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_ETHER_EN
);
421 FW(fecp
, r_des_active
, 0x01000000);
424 void fec_stop(struct net_device
*dev
)
426 struct fec_enet_private
*fep
= netdev_priv(dev
);
427 fec_t
*fecp
= fep
->fecp
;
431 if ((FR(fecp
, ecntrl
) & FEC_ECNTRL_ETHER_EN
) == 0)
432 return; /* already down */
434 FW(fecp
, x_cntrl
, 0x01); /* Graceful transmit stop */
435 for (i
= 0; ((FR(fecp
, ievent
) & 0x10000000) == 0) &&
436 i
< FEC_RESET_DELAY
; i
++)
439 if (i
== FEC_RESET_DELAY
)
440 printk(KERN_WARNING DRV_MODULE_NAME
441 ": %s FEC timeout on graceful transmit stop\n",
444 * Disable FEC. Let only MII interrupts.
447 FW(fecp
, ecntrl
, ~FEC_ECNTRL_ETHER_EN
);
450 * Reset SKB transmit buffers.
452 for (i
= 0; i
< fep
->tx_ring
; i
++) {
453 if ((skb
= fep
->tx_skbuff
[i
]) == NULL
)
455 fep
->tx_skbuff
[i
] = NULL
;
460 * Reset SKB receive buffers
462 for (i
= 0; i
< fep
->rx_ring
; i
++) {
463 if ((skb
= fep
->rx_skbuff
[i
]) == NULL
)
465 fep
->rx_skbuff
[i
] = NULL
;
470 /* common receive function */
471 static int fec_enet_rx_common(struct net_device
*dev
, int *budget
)
473 struct fec_enet_private
*fep
= netdev_priv(dev
);
474 fec_t
*fecp
= fep
->fecp
;
475 const struct fec_platform_info
*fpi
= fep
->fpi
;
477 struct sk_buff
*skb
, *skbn
, *skbt
;
484 rx_work_limit
= min(dev
->quota
, *budget
);
486 if (!netif_running(dev
))
491 * First, grab all of the stats for the incoming packet.
492 * These get messed up if we get called due to a busy condition.
496 /* clear RX status bits for napi*/
498 FW(fecp
, ievent
, FEC_ENET_RXF
| FEC_ENET_RXB
);
500 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_RX_EMPTY
) == 0) {
502 curidx
= bdp
- fep
->rx_bd_base
;
505 * Since we have allocated space to hold a complete frame,
506 * the last indicator should be set.
508 if ((sc
& BD_ENET_RX_LAST
) == 0)
509 printk(KERN_WARNING DRV_MODULE_NAME
510 ": %s rcv is not +last\n",
516 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
| BD_ENET_RX_CL
|
517 BD_ENET_RX_NO
| BD_ENET_RX_CR
| BD_ENET_RX_OV
)) {
518 fep
->stats
.rx_errors
++;
519 /* Frame too long or too short. */
520 if (sc
& (BD_ENET_RX_LG
| BD_ENET_RX_SH
))
521 fep
->stats
.rx_length_errors
++;
522 /* Frame alignment */
523 if (sc
& (BD_ENET_RX_NO
| BD_ENET_RX_CL
))
524 fep
->stats
.rx_frame_errors
++;
526 if (sc
& BD_ENET_RX_CR
)
527 fep
->stats
.rx_crc_errors
++;
529 if (sc
& BD_ENET_RX_OV
)
530 fep
->stats
.rx_crc_errors
++;
532 skbn
= fep
->rx_skbuff
[curidx
];
533 BUG_ON(skbn
== NULL
);
537 /* napi, got packet but no quota */
538 if (fpi
->use_napi
&& --rx_work_limit
< 0)
541 skb
= fep
->rx_skbuff
[curidx
];
545 * Process the incoming frame.
547 fep
->stats
.rx_packets
++;
548 pkt_len
= CBDR_DATLEN(bdp
) - 4; /* remove CRC */
549 fep
->stats
.rx_bytes
+= pkt_len
+ 4;
551 if (pkt_len
<= fpi
->rx_copybreak
) {
552 /* +2 to make IP header L1 cache aligned */
553 skbn
= dev_alloc_skb(pkt_len
+ 2);
555 skb_reserve(skbn
, 2); /* align IP header */
556 memcpy(skbn
->data
, skb
->data
, pkt_len
);
563 skbn
= dev_alloc_skb(ENET_RX_FRSIZE
);
567 skb_put(skb
, pkt_len
); /* Make room */
568 skb
->protocol
= eth_type_trans(skb
, dev
);
573 netif_receive_skb(skb
);
575 printk(KERN_WARNING DRV_MODULE_NAME
576 ": %s Memory squeeze, dropping packet.\n",
578 fep
->stats
.rx_dropped
++;
583 fep
->rx_skbuff
[curidx
] = skbn
;
584 CBDW_BUFADDR(bdp
, dma_map_single(NULL
, skbn
->data
,
585 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE
),
588 CBDW_SC(bdp
, (sc
& ~BD_ENET_RX_STATS
) | BD_ENET_RX_EMPTY
);
591 * Update BD pointer to next entry.
593 if ((sc
& BD_ENET_RX_WRAP
) == 0)
596 bdp
= fep
->rx_bd_base
;
599 * Doing this here will keep the FEC running while we process
600 * incoming frames. On a heavily loaded network, we should be
601 * able to keep up at the expense of system resources.
603 FW(fecp
, r_des_active
, 0x01000000);
609 dev
->quota
-= received
;
612 if (rx_work_limit
< 0)
613 return 1; /* not done */
616 netif_rx_complete(dev
);
618 /* enable RX interrupt bits */
619 FS(fecp
, imask
, FEC_ENET_RXF
| FEC_ENET_RXB
);
625 static void fec_enet_tx(struct net_device
*dev
)
627 struct fec_enet_private
*fep
= netdev_priv(dev
);
630 int dirtyidx
, do_wake
;
633 spin_lock(&fep
->lock
);
637 while (((sc
= CBDR_SC(bdp
)) & BD_ENET_TX_READY
) == 0) {
639 dirtyidx
= bdp
- fep
->tx_bd_base
;
641 if (fep
->tx_free
== fep
->tx_ring
)
644 skb
= fep
->tx_skbuff
[dirtyidx
];
649 if (sc
& (BD_ENET_TX_HB
| BD_ENET_TX_LC
|
650 BD_ENET_TX_RL
| BD_ENET_TX_UN
| BD_ENET_TX_CSL
)) {
651 fep
->stats
.tx_errors
++;
652 if (sc
& BD_ENET_TX_HB
) /* No heartbeat */
653 fep
->stats
.tx_heartbeat_errors
++;
654 if (sc
& BD_ENET_TX_LC
) /* Late collision */
655 fep
->stats
.tx_window_errors
++;
656 if (sc
& BD_ENET_TX_RL
) /* Retrans limit */
657 fep
->stats
.tx_aborted_errors
++;
658 if (sc
& BD_ENET_TX_UN
) /* Underrun */
659 fep
->stats
.tx_fifo_errors
++;
660 if (sc
& BD_ENET_TX_CSL
) /* Carrier lost */
661 fep
->stats
.tx_carrier_errors
++;
663 fep
->stats
.tx_packets
++;
665 if (sc
& BD_ENET_TX_READY
)
666 printk(KERN_WARNING DRV_MODULE_NAME
667 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
671 * Deferred means some collisions occurred during transmit,
672 * but we eventually sent the packet OK.
674 if (sc
& BD_ENET_TX_DEF
)
675 fep
->stats
.collisions
++;
678 * Free the sk buffer associated with this last transmit.
680 dev_kfree_skb_irq(skb
);
681 fep
->tx_skbuff
[dirtyidx
] = NULL
;
684 * Update pointer to next buffer descriptor to be transmitted.
686 if ((sc
& BD_ENET_TX_WRAP
) == 0)
689 bdp
= fep
->tx_bd_base
;
692 * Since we have freed up a buffer, the ring is no longer
701 spin_unlock(&fep
->lock
);
703 if (do_wake
&& netif_queue_stopped(dev
))
704 netif_wake_queue(dev
);
708 * The interrupt handler.
709 * This is called from the MPC core interrupt.
712 fec_enet_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
714 struct net_device
*dev
= dev_id
;
715 struct fec_enet_private
*fep
;
716 const struct fec_platform_info
*fpi
;
719 __u32 int_events_napi
;
721 if (unlikely(dev
== NULL
))
724 fep
= netdev_priv(dev
);
729 * Get the interrupt events that caused us to be here.
731 while ((int_events
= FR(fecp
, ievent
) & FR(fecp
, imask
)) != 0) {
734 FW(fecp
, ievent
, int_events
);
736 int_events_napi
= int_events
& ~(FEC_ENET_RXF
| FEC_ENET_RXB
);
737 FW(fecp
, ievent
, int_events_napi
);
740 if ((int_events
& (FEC_ENET_HBERR
| FEC_ENET_BABR
|
741 FEC_ENET_BABT
| FEC_ENET_EBERR
)) != 0)
742 printk(KERN_WARNING DRV_MODULE_NAME
743 ": %s FEC ERROR(s) 0x%x\n",
744 dev
->name
, int_events
);
746 if ((int_events
& FEC_ENET_RXF
) != 0) {
748 fec_enet_rx_common(dev
, NULL
);
750 if (netif_rx_schedule_prep(dev
)) {
751 /* disable rx interrupts */
752 FC(fecp
, imask
, FEC_ENET_RXF
| FEC_ENET_RXB
);
753 __netif_rx_schedule(dev
);
755 printk(KERN_ERR DRV_MODULE_NAME
756 ": %s driver bug! interrupt while in poll!\n",
758 FC(fecp
, imask
, FEC_ENET_RXF
| FEC_ENET_RXB
);
763 if ((int_events
& FEC_ENET_TXF
) != 0)
770 /* This interrupt occurs when the PHY detects a link change. */
772 fec_mii_link_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
774 struct net_device
*dev
= dev_id
;
775 struct fec_enet_private
*fep
;
776 const struct fec_platform_info
*fpi
;
778 if (unlikely(dev
== NULL
))
781 fep
= netdev_priv(dev
);
788 * Acknowledge the interrupt if possible. If we have not
789 * found the PHY yet we can't process or acknowledge the
790 * interrupt now. Instead we ignore this interrupt for now,
791 * which we can do since it is edge triggered. It will be
792 * acknowledged later by fec_enet_open().
797 fec_mii_ack_int(dev
);
798 fec_mii_link_status_change_check(dev
, 0);
804 /**********************************************************************************/
806 static int fec_enet_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
808 struct fec_enet_private
*fep
= netdev_priv(dev
);
809 fec_t
*fecp
= fep
->fecp
;
814 spin_lock_irqsave(&fep
->tx_lock
, flags
);
817 * Fill in a Tx ring entry
821 if (!fep
->tx_free
|| (CBDR_SC(bdp
) & BD_ENET_TX_READY
)) {
822 netif_stop_queue(dev
);
823 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
826 * Ooops. All transmit buffers are full. Bail out.
827 * This should not happen, since the tx queue should be stopped.
829 printk(KERN_WARNING DRV_MODULE_NAME
830 ": %s tx queue full!.\n", dev
->name
);
834 curidx
= bdp
- fep
->tx_bd_base
;
836 * Clear all of the status flags.
838 CBDC_SC(bdp
, BD_ENET_TX_STATS
);
843 fep
->tx_skbuff
[curidx
] = skb
;
845 fep
->stats
.tx_bytes
+= skb
->len
;
848 * Push the data cache so the CPM does not get stale memory data.
850 CBDW_BUFADDR(bdp
, dma_map_single(NULL
, skb
->data
,
851 skb
->len
, DMA_TO_DEVICE
));
852 CBDW_DATLEN(bdp
, skb
->len
);
854 dev
->trans_start
= jiffies
;
857 * If this was the last BD in the ring, start at the beginning again.
859 if ((CBDR_SC(bdp
) & BD_ENET_TX_WRAP
) == 0)
862 fep
->cur_tx
= fep
->tx_bd_base
;
865 netif_stop_queue(dev
);
868 * Trigger transmission start
870 CBDS_SC(bdp
, BD_ENET_TX_READY
| BD_ENET_TX_INTR
|
871 BD_ENET_TX_LAST
| BD_ENET_TX_TC
);
872 FW(fecp
, x_des_active
, 0x01000000);
874 spin_unlock_irqrestore(&fep
->tx_lock
, flags
);
879 static void fec_timeout(struct net_device
*dev
)
881 struct fec_enet_private
*fep
= netdev_priv(dev
);
883 fep
->stats
.tx_errors
++;
886 netif_wake_queue(dev
);
888 /* check link status again */
889 fec_mii_link_status_change_check(dev
, 0);
892 static int fec_enet_open(struct net_device
*dev
)
894 struct fec_enet_private
*fep
= netdev_priv(dev
);
895 const struct fec_platform_info
*fpi
= fep
->fpi
;
898 /* Install our interrupt handler. */
899 if (request_irq(fpi
->fec_irq
, fec_enet_interrupt
, 0, "fec", dev
) != 0) {
900 printk(KERN_ERR DRV_MODULE_NAME
901 ": %s Could not allocate FEC IRQ!", dev
->name
);
905 /* Install our phy interrupt handler */
906 if (fpi
->phy_irq
!= -1 &&
907 request_irq(fpi
->phy_irq
, fec_mii_link_interrupt
, 0, "fec-phy",
909 printk(KERN_ERR DRV_MODULE_NAME
910 ": %s Could not allocate PHY IRQ!", dev
->name
);
911 free_irq(fpi
->fec_irq
, dev
);
916 fec_mii_startup(dev
);
917 netif_carrier_off(dev
);
918 fec_mii_link_status_change_check(dev
, 1);
920 spin_lock_irqsave(&fep
->lock
, flags
);
921 fec_restart(dev
, 1, 100); /* XXX this sucks */
922 spin_unlock_irqrestore(&fep
->lock
, flags
);
924 netif_carrier_on(dev
);
925 netif_start_queue(dev
);
930 static int fec_enet_close(struct net_device
*dev
)
932 struct fec_enet_private
*fep
= netdev_priv(dev
);
933 const struct fec_platform_info
*fpi
= fep
->fpi
;
936 netif_stop_queue(dev
);
937 netif_carrier_off(dev
);
940 fec_mii_shutdown(dev
);
942 spin_lock_irqsave(&fep
->lock
, flags
);
944 spin_unlock_irqrestore(&fep
->lock
, flags
);
946 /* release any irqs */
947 if (fpi
->phy_irq
!= -1)
948 free_irq(fpi
->phy_irq
, dev
);
949 free_irq(fpi
->fec_irq
, dev
);
954 static struct net_device_stats
*fec_enet_get_stats(struct net_device
*dev
)
956 struct fec_enet_private
*fep
= netdev_priv(dev
);
960 static int fec_enet_poll(struct net_device
*dev
, int *budget
)
962 return fec_enet_rx_common(dev
, budget
);
965 /*************************************************************************/
967 static void fec_get_drvinfo(struct net_device
*dev
,
968 struct ethtool_drvinfo
*info
)
970 strcpy(info
->driver
, DRV_MODULE_NAME
);
971 strcpy(info
->version
, DRV_MODULE_VERSION
);
974 static int fec_get_regs_len(struct net_device
*dev
)
976 return sizeof(fec_t
);
979 static void fec_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
982 struct fec_enet_private
*fep
= netdev_priv(dev
);
985 if (regs
->len
< sizeof(fec_t
))
989 spin_lock_irqsave(&fep
->lock
, flags
);
990 memcpy_fromio(p
, fep
->fecp
, sizeof(fec_t
));
991 spin_unlock_irqrestore(&fep
->lock
, flags
);
994 static int fec_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
996 struct fec_enet_private
*fep
= netdev_priv(dev
);
1000 spin_lock_irqsave(&fep
->lock
, flags
);
1001 rc
= mii_ethtool_gset(&fep
->mii_if
, cmd
);
1002 spin_unlock_irqrestore(&fep
->lock
, flags
);
1007 static int fec_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1009 struct fec_enet_private
*fep
= netdev_priv(dev
);
1010 unsigned long flags
;
1013 spin_lock_irqsave(&fep
->lock
, flags
);
1014 rc
= mii_ethtool_sset(&fep
->mii_if
, cmd
);
1015 spin_unlock_irqrestore(&fep
->lock
, flags
);
1020 static int fec_nway_reset(struct net_device
*dev
)
1022 struct fec_enet_private
*fep
= netdev_priv(dev
);
1023 return mii_nway_restart(&fep
->mii_if
);
1026 static __u32
fec_get_msglevel(struct net_device
*dev
)
1028 struct fec_enet_private
*fep
= netdev_priv(dev
);
1029 return fep
->msg_enable
;
1032 static void fec_set_msglevel(struct net_device
*dev
, __u32 value
)
1034 struct fec_enet_private
*fep
= netdev_priv(dev
);
1035 fep
->msg_enable
= value
;
1038 static struct ethtool_ops fec_ethtool_ops
= {
1039 .get_drvinfo
= fec_get_drvinfo
,
1040 .get_regs_len
= fec_get_regs_len
,
1041 .get_settings
= fec_get_settings
,
1042 .set_settings
= fec_set_settings
,
1043 .nway_reset
= fec_nway_reset
,
1044 .get_link
= ethtool_op_get_link
,
1045 .get_msglevel
= fec_get_msglevel
,
1046 .set_msglevel
= fec_set_msglevel
,
1047 .get_tx_csum
= ethtool_op_get_tx_csum
,
1048 .set_tx_csum
= ethtool_op_set_tx_csum
, /* local! */
1049 .get_sg
= ethtool_op_get_sg
,
1050 .set_sg
= ethtool_op_set_sg
,
1051 .get_regs
= fec_get_regs
,
1054 static int fec_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
1056 struct fec_enet_private
*fep
= netdev_priv(dev
);
1057 struct mii_ioctl_data
*mii
= (struct mii_ioctl_data
*)&rq
->ifr_data
;
1058 unsigned long flags
;
1061 if (!netif_running(dev
))
1064 spin_lock_irqsave(&fep
->lock
, flags
);
1065 rc
= generic_mii_ioctl(&fep
->mii_if
, mii
, cmd
, NULL
);
1066 spin_unlock_irqrestore(&fep
->lock
, flags
);
1070 int fec_8xx_init_one(const struct fec_platform_info
*fpi
,
1071 struct net_device
**devp
)
1073 immap_t
*immap
= (immap_t
*) IMAP_ADDR
;
1074 static int fec_8xx_version_printed
= 0;
1075 struct net_device
*dev
= NULL
;
1076 struct fec_enet_private
*fep
= NULL
;
1085 switch (fpi
->fec_no
) {
1087 fecp
= &((immap_t
*) IMAP_ADDR
)->im_cpm
.cp_fec
;
1091 fecp
= &((immap_t
*) IMAP_ADDR
)->im_cpm
.cp_fec2
;
1098 if (fec_8xx_version_printed
++ == 0)
1099 printk(KERN_INFO
"%s", version
);
1101 i
= sizeof(*fep
) + (sizeof(struct sk_buff
**) *
1102 (fpi
->rx_ring
+ fpi
->tx_ring
));
1104 dev
= alloc_etherdev(i
);
1109 SET_MODULE_OWNER(dev
);
1111 fep
= netdev_priv(dev
);
1113 /* partial reset of FEC */
1114 fec_whack_reset(fecp
);
1116 /* point rx_skbuff, tx_skbuff */
1117 fep
->rx_skbuff
= (struct sk_buff
**)&fep
[1];
1118 fep
->tx_skbuff
= fep
->rx_skbuff
+ fpi
->rx_ring
;
1124 spin_lock_init(&fep
->lock
);
1125 spin_lock_init(&fep
->tx_lock
);
1128 * Set the Ethernet address.
1130 for (i
= 0; i
< 6; i
++)
1131 dev
->dev_addr
[i
] = fpi
->macaddr
[i
];
1133 fep
->ring_base
= dma_alloc_coherent(NULL
,
1134 (fpi
->tx_ring
+ fpi
->rx_ring
) *
1135 sizeof(cbd_t
), &fep
->ring_mem_addr
,
1137 if (fep
->ring_base
== NULL
) {
1138 printk(KERN_ERR DRV_MODULE_NAME
1139 ": %s dma alloc failed.\n", dev
->name
);
1145 * Set receive and transmit descriptor base.
1147 fep
->rx_bd_base
= fep
->ring_base
;
1148 fep
->tx_bd_base
= fep
->rx_bd_base
+ fpi
->rx_ring
;
1150 /* initialize ring size variables */
1151 fep
->tx_ring
= fpi
->tx_ring
;
1152 fep
->rx_ring
= fpi
->rx_ring
;
1155 if (fpi
->phy_irq
!= -1 &&
1156 (fpi
->phy_irq
>= SIU_IRQ0
&& fpi
->phy_irq
< SIU_LEVEL7
)) {
1158 siel
= in_be32(&immap
->im_siu_conf
.sc_siel
);
1159 if ((fpi
->phy_irq
& 1) == 0)
1160 siel
|= (0x80000000 >> fpi
->phy_irq
);
1162 siel
&= ~(0x80000000 >> (fpi
->phy_irq
& ~1));
1163 out_be32(&immap
->im_siu_conf
.sc_siel
, siel
);
1167 * The FEC Ethernet specific entries in the device structure.
1169 dev
->open
= fec_enet_open
;
1170 dev
->hard_start_xmit
= fec_enet_start_xmit
;
1171 dev
->tx_timeout
= fec_timeout
;
1172 dev
->watchdog_timeo
= TX_TIMEOUT
;
1173 dev
->stop
= fec_enet_close
;
1174 dev
->get_stats
= fec_enet_get_stats
;
1175 dev
->set_multicast_list
= fec_set_multicast_list
;
1176 dev
->set_mac_address
= fec_set_mac_address
;
1177 if (fpi
->use_napi
) {
1178 dev
->poll
= fec_enet_poll
;
1179 dev
->weight
= fpi
->napi_weight
;
1181 dev
->ethtool_ops
= &fec_ethtool_ops
;
1182 dev
->do_ioctl
= fec_ioctl
;
1184 fep
->fec_phy_speed
=
1185 ((((fpi
->sys_clk
+ 4999999) / 2500000) / 2) & 0x3F) << 1;
1187 init_timer(&fep
->phy_timer_list
);
1189 /* partial reset of FEC so that only MII works */
1190 FW(fecp
, mii_speed
, fep
->fec_phy_speed
);
1191 FW(fecp
, ievent
, 0xffc0);
1192 FW(fecp
, ivec
, (fpi
->fec_irq
/ 2) << 29);
1194 FW(fecp
, r_cntrl
, FEC_RCNTRL_MII_MODE
); /* MII enable */
1195 FW(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_ETHER_EN
);
1197 netif_carrier_off(dev
);
1199 err
= register_netdev(dev
);
1204 if (fpi
->use_mdio
) {
1205 fep
->mii_if
.dev
= dev
;
1206 fep
->mii_if
.mdio_read
= fec_mii_read
;
1207 fep
->mii_if
.mdio_write
= fec_mii_write
;
1208 fep
->mii_if
.phy_id_mask
= 0x1f;
1209 fep
->mii_if
.reg_num_mask
= 0x1f;
1210 fep
->mii_if
.phy_id
= fec_mii_phy_id_detect(dev
);
1220 fec_whack_reset(fecp
);
1223 unregister_netdev(dev
);
1227 dma_free_coherent(NULL
,
1230 sizeof(cbd_t
), fep
->ring_base
,
1231 fep
->ring_mem_addr
);
1238 int fec_8xx_cleanup_one(struct net_device
*dev
)
1240 struct fec_enet_private
*fep
= netdev_priv(dev
);
1241 fec_t
*fecp
= fep
->fecp
;
1242 const struct fec_platform_info
*fpi
= fep
->fpi
;
1244 fec_whack_reset(fecp
);
1246 unregister_netdev(dev
);
1248 dma_free_coherent(NULL
, (fpi
->tx_ring
+ fpi
->rx_ring
) * sizeof(cbd_t
),
1249 fep
->ring_base
, fep
->ring_mem_addr
);
1256 /**************************************************************************************/
1257 /**************************************************************************************/
1258 /**************************************************************************************/
1260 static int __init
fec_8xx_init(void)
1262 return fec_8xx_platform_init();
1265 static void __exit
fec_8xx_cleanup(void)
1267 fec_8xx_platform_cleanup();
1270 /**************************************************************************************/
1271 /**************************************************************************************/
1272 /**************************************************************************************/
1274 module_init(fec_8xx_init
);
1275 module_exit(fec_8xx_cleanup
);