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/module.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/string.h>
17 #include <linux/ptrace.h>
18 #include <linux/errno.h>
19 #include <linux/ioport.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
29 #include <linux/mii.h>
30 #include <linux/ethtool.h>
31 #include <linux/bitops.h>
32 #include <linux/dma-mapping.h>
34 #include <asm/8xx_immap.h>
35 #include <asm/pgtable.h>
36 #include <asm/mpc8xx.h>
38 #include <asm/uaccess.h>
39 #include <asm/commproc.h>
43 /*************************************************/
45 #define FEC_MAX_MULTICAST_ADDRS 64
47 /*************************************************/
49 static char version[] __devinitdata =
50 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
52 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
53 MODULE_DESCRIPTION("Motorola 8xx FEC ethernet driver");
54 MODULE_LICENSE("GPL");
56 int fec_8xx_debug = -1; /* -1 == use FEC_8XX_DEF_MSG_ENABLE as value */
57 module_param(fec_8xx_debug, int, 0);
58 MODULE_PARM_DESC(fec_8xx_debug,
59 "FEC 8xx bitmapped debugging message enable value");
62 /*************************************************/
65 * Delay to wait for FEC reset command to complete (in us)
67 #define FEC_RESET_DELAY 50
69 /*****************************************************************************************/
71 static void fec_whack_reset(fec_t * fecp)
76 * Whack a reset. We should wait for this.
78 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
80 (FR(fecp, ecntrl) & FEC_ECNTRL_RESET) != 0 && i < FEC_RESET_DELAY;
84 if (i == FEC_RESET_DELAY)
85 printk(KERN_WARNING "FEC Reset timeout!\n");
89 /****************************************************************************/
92 * Transmitter timeout.
94 #define TX_TIMEOUT (2*HZ)
96 /****************************************************************************/
99 * Returns the CRC needed when filling in the hash table for
100 * multicast group filtering
101 * pAddr must point to a MAC address (6 bytes)
103 static __u32 fec_mulicast_calc_crc(char *pAddr)
108 __u32 crc = 0xffffffff;
111 for (byte_count = 0; byte_count < 6; byte_count++) {
112 byte = pAddr[byte_count];
113 for (bit_count = 0; bit_count < 8; bit_count++) {
116 if (msb ^ (byte & 0x1)) {
126 * Set or clear the multicast filter for this adaptor.
127 * Skeleton taken from sunlance driver.
128 * The CPM Ethernet implementation allows Multicast as well as individual
129 * MAC address filtering. Some of the drivers check to make sure it is
130 * a group multicast address, and discard those that are not. I guess I
131 * will do the same for now, but just remove the test if you want
132 * individual filtering as well (do the upper net layers want or support
133 * this kind of feature?).
135 static void fec_set_multicast_list(struct net_device *dev)
137 struct fec_enet_private *fep = netdev_priv(dev);
138 fec_t *fecp = fep->fecp;
139 struct dev_mc_list *pmc;
148 if ((dev->flags & IFF_PROMISC) != 0) {
150 spin_lock_irqsave(&fep->lock, flags);
151 FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
152 spin_unlock_irqrestore(&fep->lock, flags);
157 printk(KERN_WARNING DRV_MODULE_NAME
158 ": %s: Promiscuous mode enabled.\n", dev->name);
163 if ((dev->flags & IFF_ALLMULTI) != 0 ||
164 dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
166 * Catch all multicast addresses, set the filter to all 1's.
175 * Now populate the hash table
177 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next) {
178 crc = fec_mulicast_calc_crc(pmc->dmi_addr);
179 temp = (crc & 0x3f) >> 1;
180 hash_index = ((temp & 0x01) << 4) |
181 ((temp & 0x02) << 2) |
183 ((temp & 0x08) >> 2) |
184 ((temp & 0x10) >> 4);
185 csrVal = (1 << hash_index);
193 spin_lock_irqsave(&fep->lock, flags);
194 FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
195 FW(fecp, hash_table_high, hthi);
196 FW(fecp, hash_table_low, htlo);
197 spin_unlock_irqrestore(&fep->lock, flags);
200 static int fec_set_mac_address(struct net_device *dev, void *addr)
202 struct sockaddr *mac = addr;
203 struct fec_enet_private *fep = netdev_priv(dev);
204 struct fec *fecp = fep->fecp;
206 __u32 addrhi, addrlo;
209 /* Get pointer to SCC area in parameter RAM. */
210 for (i = 0; i < 6; i++)
211 dev->dev_addr[i] = mac->sa_data[i];
214 * Set station address.
216 addrhi = ((__u32) dev->dev_addr[0] << 24) |
217 ((__u32) dev->dev_addr[1] << 16) |
218 ((__u32) dev->dev_addr[2] << 8) |
219 (__u32) dev->dev_addr[3];
220 addrlo = ((__u32) dev->dev_addr[4] << 24) |
221 ((__u32) dev->dev_addr[5] << 16);
223 spin_lock_irqsave(&fep->lock, flags);
224 FW(fecp, addr_low, addrhi);
225 FW(fecp, addr_high, addrlo);
226 spin_unlock_irqrestore(&fep->lock, flags);
232 * This function is called to start or restart the FEC during a link
233 * change. This only happens when switching between half and full
236 void fec_restart(struct net_device *dev, int duplex, int speed)
239 immap_t *immap = (immap_t *) IMAP_ADDR;
242 struct fec_enet_private *fep = netdev_priv(dev);
243 struct fec *fecp = fep->fecp;
244 const struct fec_platform_info *fpi = fep->fpi;
248 __u32 addrhi, addrlo;
250 fec_whack_reset(fep->fecp);
253 * Set station address.
255 addrhi = ((__u32) dev->dev_addr[0] << 24) |
256 ((__u32) dev->dev_addr[1] << 16) |
257 ((__u32) dev->dev_addr[2] << 8) |
258 (__u32) dev->dev_addr[3];
259 addrlo = ((__u32) dev->dev_addr[4] << 24) |
260 ((__u32) dev->dev_addr[5] << 16);
261 FW(fecp, addr_low, addrhi);
262 FW(fecp, addr_high, addrlo);
265 * Reset all multicast.
267 FW(fecp, hash_table_high, 0);
268 FW(fecp, hash_table_low, 0);
271 * Set maximum receive buffer size.
273 FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
274 FW(fecp, r_hash, PKT_MAXBUF_SIZE);
277 * Set receive and transmit descriptor base.
279 FW(fecp, r_des_start, iopa((__u32) (fep->rx_bd_base)));
280 FW(fecp, x_des_start, iopa((__u32) (fep->tx_bd_base)));
282 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
283 fep->tx_free = fep->tx_ring;
284 fep->cur_rx = fep->rx_bd_base;
287 * Reset SKB receive buffers
289 for (i = 0; i < fep->rx_ring; i++) {
290 if ((skb = fep->rx_skbuff[i]) == NULL)
292 fep->rx_skbuff[i] = NULL;
297 * Initialize the receive buffer descriptors.
299 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
300 skb = dev_alloc_skb(ENET_RX_FRSIZE);
302 printk(KERN_WARNING DRV_MODULE_NAME
303 ": %s Memory squeeze, unable to allocate skb\n",
305 fep->stats.rx_dropped++;
308 fep->rx_skbuff[i] = skb;
310 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
311 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
313 CBDW_DATLEN(bdp, 0); /* zero */
314 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
315 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
318 * if we failed, fillup remainder
320 for (; i < fep->rx_ring; i++, bdp++) {
321 fep->rx_skbuff[i] = NULL;
322 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
326 * Reset SKB transmit buffers.
328 for (i = 0; i < fep->tx_ring; i++) {
329 if ((skb = fep->tx_skbuff[i]) == NULL)
331 fep->tx_skbuff[i] = NULL;
336 * ...and the same for transmit.
338 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
339 fep->tx_skbuff[i] = NULL;
340 CBDW_BUFADDR(bdp, virt_to_bus(NULL));
342 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
346 * Enable big endian and don't care about SDMA FC.
348 FW(fecp, fun_code, 0x78000000);
353 FW(fecp, mii_speed, fep->fec_phy_speed);
356 * Clear any outstanding interrupt.
358 FW(fecp, ievent, 0xffc0);
359 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
362 * adjust to speed (only for DUET & RMII)
365 cptr = in_be32(&immap->im_cpm.cp_cptr);
366 switch (fpi->fec_no) {
369 * check if in RMII mode
371 if ((cptr & 0x100) == 0)
376 else if (speed == 100)
381 * check if in RMII mode
383 if ((cptr & 0x80) == 0)
388 else if (speed == 100)
394 out_be32(&immap->im_cpm.cp_cptr, cptr);
397 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
399 * adjust to duplex mode
402 FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
403 FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
405 FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
406 FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
410 * Enable interrupts we wish to service.
412 FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
413 FEC_ENET_RXF | FEC_ENET_RXB);
416 * And last, enable the transmit and receive processing.
418 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
419 FW(fecp, r_des_active, 0x01000000);
422 void fec_stop(struct net_device *dev)
424 struct fec_enet_private *fep = netdev_priv(dev);
425 fec_t *fecp = fep->fecp;
429 if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
430 return; /* already down */
432 FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
433 for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
434 i < FEC_RESET_DELAY; i++)
437 if (i == FEC_RESET_DELAY)
438 printk(KERN_WARNING DRV_MODULE_NAME
439 ": %s FEC timeout on graceful transmit stop\n",
442 * Disable FEC. Let only MII interrupts.
445 FW(fecp, ecntrl, ~FEC_ECNTRL_ETHER_EN);
448 * Reset SKB transmit buffers.
450 for (i = 0; i < fep->tx_ring; i++) {
451 if ((skb = fep->tx_skbuff[i]) == NULL)
453 fep->tx_skbuff[i] = NULL;
458 * Reset SKB receive buffers
460 for (i = 0; i < fep->rx_ring; i++) {
461 if ((skb = fep->rx_skbuff[i]) == NULL)
463 fep->rx_skbuff[i] = NULL;
468 /* common receive function */
469 static int fec_enet_rx_common(struct net_device *dev, int *budget)
471 struct fec_enet_private *fep = netdev_priv(dev);
472 fec_t *fecp = fep->fecp;
473 const struct fec_platform_info *fpi = fep->fpi;
475 struct sk_buff *skb, *skbn, *skbt;
482 rx_work_limit = min(dev->quota, *budget);
484 if (!netif_running(dev))
489 * First, grab all of the stats for the incoming packet.
490 * These get messed up if we get called due to a busy condition.
494 /* clear RX status bits for napi*/
496 FW(fecp, ievent, FEC_ENET_RXF | FEC_ENET_RXB);
498 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
500 curidx = bdp - fep->rx_bd_base;
503 * Since we have allocated space to hold a complete frame,
504 * the last indicator should be set.
506 if ((sc & BD_ENET_RX_LAST) == 0)
507 printk(KERN_WARNING DRV_MODULE_NAME
508 ": %s rcv is not +last\n",
514 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
515 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
516 fep->stats.rx_errors++;
517 /* Frame too long or too short. */
518 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
519 fep->stats.rx_length_errors++;
520 /* Frame alignment */
521 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
522 fep->stats.rx_frame_errors++;
524 if (sc & BD_ENET_RX_CR)
525 fep->stats.rx_crc_errors++;
527 if (sc & BD_ENET_RX_OV)
528 fep->stats.rx_crc_errors++;
530 skbn = fep->rx_skbuff[curidx];
531 BUG_ON(skbn == NULL);
535 /* napi, got packet but no quota */
536 if (fpi->use_napi && --rx_work_limit < 0)
539 skb = fep->rx_skbuff[curidx];
543 * Process the incoming frame.
545 fep->stats.rx_packets++;
546 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
547 fep->stats.rx_bytes += pkt_len + 4;
549 if (pkt_len <= fpi->rx_copybreak) {
550 /* +2 to make IP header L1 cache aligned */
551 skbn = dev_alloc_skb(pkt_len + 2);
553 skb_reserve(skbn, 2); /* align IP header */
554 skb_copy_from_linear_data(skb
563 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
566 skb_put(skb, pkt_len); /* Make room */
567 skb->protocol = eth_type_trans(skb, dev);
572 netif_receive_skb(skb);
574 printk(KERN_WARNING DRV_MODULE_NAME
575 ": %s Memory squeeze, dropping packet.\n",
577 fep->stats.rx_dropped++;
582 fep->rx_skbuff[curidx] = skbn;
583 CBDW_BUFADDR(bdp, dma_map_single(NULL, skbn->data,
584 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
587 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
590 * Update BD pointer to next entry.
592 if ((sc & BD_ENET_RX_WRAP) == 0)
595 bdp = fep->rx_bd_base;
598 * Doing this here will keep the FEC running while we process
599 * incoming frames. On a heavily loaded network, we should be
600 * able to keep up at the expense of system resources.
602 FW(fecp, r_des_active, 0x01000000);
608 dev->quota -= received;
611 if (rx_work_limit < 0)
612 return 1; /* not done */
615 netif_rx_complete(dev);
617 /* enable RX interrupt bits */
618 FS(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
624 static void fec_enet_tx(struct net_device *dev)
626 struct fec_enet_private *fep = netdev_priv(dev);
629 int dirtyidx, do_wake;
632 spin_lock(&fep->lock);
636 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
638 dirtyidx = bdp - fep->tx_bd_base;
640 if (fep->tx_free == fep->tx_ring)
643 skb = fep->tx_skbuff[dirtyidx];
648 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
649 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
650 fep->stats.tx_errors++;
651 if (sc & BD_ENET_TX_HB) /* No heartbeat */
652 fep->stats.tx_heartbeat_errors++;
653 if (sc & BD_ENET_TX_LC) /* Late collision */
654 fep->stats.tx_window_errors++;
655 if (sc & BD_ENET_TX_RL) /* Retrans limit */
656 fep->stats.tx_aborted_errors++;
657 if (sc & BD_ENET_TX_UN) /* Underrun */
658 fep->stats.tx_fifo_errors++;
659 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
660 fep->stats.tx_carrier_errors++;
662 fep->stats.tx_packets++;
664 if (sc & BD_ENET_TX_READY)
665 printk(KERN_WARNING DRV_MODULE_NAME
666 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
670 * Deferred means some collisions occurred during transmit,
671 * but we eventually sent the packet OK.
673 if (sc & BD_ENET_TX_DEF)
674 fep->stats.collisions++;
677 * Free the sk buffer associated with this last transmit.
679 dev_kfree_skb_irq(skb);
680 fep->tx_skbuff[dirtyidx] = NULL;
683 * Update pointer to next buffer descriptor to be transmitted.
685 if ((sc & BD_ENET_TX_WRAP) == 0)
688 bdp = fep->tx_bd_base;
691 * Since we have freed up a buffer, the ring is no longer
700 spin_unlock(&fep->lock);
702 if (do_wake && netif_queue_stopped(dev))
703 netif_wake_queue(dev);
707 * The interrupt handler.
708 * This is called from the MPC core interrupt.
711 fec_enet_interrupt(int irq, void *dev_id)
713 struct net_device *dev = dev_id;
714 struct fec_enet_private *fep;
715 const struct fec_platform_info *fpi;
718 __u32 int_events_napi;
720 if (unlikely(dev == NULL))
723 fep = netdev_priv(dev);
728 * Get the interrupt events that caused us to be here.
730 while ((int_events = FR(fecp, ievent) & FR(fecp, imask)) != 0) {
733 FW(fecp, ievent, int_events);
735 int_events_napi = int_events & ~(FEC_ENET_RXF | FEC_ENET_RXB);
736 FW(fecp, ievent, int_events_napi);
739 if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
740 FEC_ENET_BABT | FEC_ENET_EBERR)) != 0)
741 printk(KERN_WARNING DRV_MODULE_NAME
742 ": %s FEC ERROR(s) 0x%x\n",
743 dev->name, int_events);
745 if ((int_events & FEC_ENET_RXF) != 0) {
747 fec_enet_rx_common(dev, NULL);
749 if (netif_rx_schedule_prep(dev)) {
750 /* disable rx interrupts */
751 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
752 __netif_rx_schedule(dev);
754 printk(KERN_ERR DRV_MODULE_NAME
755 ": %s driver bug! interrupt while in poll!\n",
757 FC(fecp, imask, FEC_ENET_RXF | FEC_ENET_RXB);
762 if ((int_events & FEC_ENET_TXF) != 0)
769 /* This interrupt occurs when the PHY detects a link change. */
771 fec_mii_link_interrupt(int irq, void *dev_id)
773 struct net_device *dev = dev_id;
774 struct fec_enet_private *fep;
775 const struct fec_platform_info *fpi;
777 if (unlikely(dev == NULL))
780 fep = netdev_priv(dev);
787 * Acknowledge the interrupt if possible. If we have not
788 * found the PHY yet we can't process or acknowledge the
789 * interrupt now. Instead we ignore this interrupt for now,
790 * which we can do since it is edge triggered. It will be
791 * acknowledged later by fec_enet_open().
796 fec_mii_ack_int(dev);
797 fec_mii_link_status_change_check(dev, 0);
803 /**********************************************************************************/
805 static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
807 struct fec_enet_private *fep = netdev_priv(dev);
808 fec_t *fecp = fep->fecp;
813 spin_lock_irqsave(&fep->tx_lock, flags);
816 * Fill in a Tx ring entry
820 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
821 netif_stop_queue(dev);
822 spin_unlock_irqrestore(&fep->tx_lock, flags);
825 * Ooops. All transmit buffers are full. Bail out.
826 * This should not happen, since the tx queue should be stopped.
828 printk(KERN_WARNING DRV_MODULE_NAME
829 ": %s tx queue full!.\n", dev->name);
833 curidx = bdp - fep->tx_bd_base;
835 * Clear all of the status flags.
837 CBDC_SC(bdp, BD_ENET_TX_STATS);
842 fep->tx_skbuff[curidx] = skb;
844 fep->stats.tx_bytes += skb->len;
847 * Push the data cache so the CPM does not get stale memory data.
849 CBDW_BUFADDR(bdp, dma_map_single(NULL, skb->data,
850 skb->len, DMA_TO_DEVICE));
851 CBDW_DATLEN(bdp, skb->len);
853 dev->trans_start = jiffies;
856 * If this was the last BD in the ring, start at the beginning again.
858 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
861 fep->cur_tx = fep->tx_bd_base;
864 netif_stop_queue(dev);
867 * Trigger transmission start
869 CBDS_SC(bdp, BD_ENET_TX_READY | BD_ENET_TX_INTR |
870 BD_ENET_TX_LAST | BD_ENET_TX_TC);
871 FW(fecp, x_des_active, 0x01000000);
873 spin_unlock_irqrestore(&fep->tx_lock, flags);
878 static void fec_timeout(struct net_device *dev)
880 struct fec_enet_private *fep = netdev_priv(dev);
882 fep->stats.tx_errors++;
885 netif_wake_queue(dev);
887 /* check link status again */
888 fec_mii_link_status_change_check(dev, 0);
891 static int fec_enet_open(struct net_device *dev)
893 struct fec_enet_private *fep = netdev_priv(dev);
894 const struct fec_platform_info *fpi = fep->fpi;
897 /* Install our interrupt handler. */
898 if (request_irq(fpi->fec_irq, fec_enet_interrupt, 0, "fec", dev) != 0) {
899 printk(KERN_ERR DRV_MODULE_NAME
900 ": %s Could not allocate FEC IRQ!", dev->name);
904 /* Install our phy interrupt handler */
905 if (fpi->phy_irq != -1 &&
906 request_irq(fpi->phy_irq, fec_mii_link_interrupt, 0, "fec-phy",
908 printk(KERN_ERR DRV_MODULE_NAME
909 ": %s Could not allocate PHY IRQ!", dev->name);
910 free_irq(fpi->fec_irq, dev);
915 fec_mii_startup(dev);
916 netif_carrier_off(dev);
917 fec_mii_link_status_change_check(dev, 1);
919 spin_lock_irqsave(&fep->lock, flags);
920 fec_restart(dev, 1, 100); /* XXX this sucks */
921 spin_unlock_irqrestore(&fep->lock, flags);
923 netif_carrier_on(dev);
924 netif_start_queue(dev);
929 static int fec_enet_close(struct net_device *dev)
931 struct fec_enet_private *fep = netdev_priv(dev);
932 const struct fec_platform_info *fpi = fep->fpi;
935 netif_stop_queue(dev);
936 netif_carrier_off(dev);
939 fec_mii_shutdown(dev);
941 spin_lock_irqsave(&fep->lock, flags);
943 spin_unlock_irqrestore(&fep->lock, flags);
945 /* release any irqs */
946 if (fpi->phy_irq != -1)
947 free_irq(fpi->phy_irq, dev);
948 free_irq(fpi->fec_irq, dev);
953 static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
955 struct fec_enet_private *fep = netdev_priv(dev);
959 static int fec_enet_poll(struct net_device *dev, int *budget)
961 return fec_enet_rx_common(dev, budget);
964 /*************************************************************************/
966 static void fec_get_drvinfo(struct net_device *dev,
967 struct ethtool_drvinfo *info)
969 strcpy(info->driver, DRV_MODULE_NAME);
970 strcpy(info->version, DRV_MODULE_VERSION);
973 static int fec_get_regs_len(struct net_device *dev)
975 return sizeof(fec_t);
978 static void fec_get_regs(struct net_device *dev, struct ethtool_regs *regs,
981 struct fec_enet_private *fep = netdev_priv(dev);
984 if (regs->len < sizeof(fec_t))
988 spin_lock_irqsave(&fep->lock, flags);
989 memcpy_fromio(p, fep->fecp, sizeof(fec_t));
990 spin_unlock_irqrestore(&fep->lock, flags);
993 static int fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
995 struct fec_enet_private *fep = netdev_priv(dev);
999 spin_lock_irqsave(&fep->lock, flags);
1000 rc = mii_ethtool_gset(&fep->mii_if, cmd);
1001 spin_unlock_irqrestore(&fep->lock, flags);
1006 static int fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1008 struct fec_enet_private *fep = netdev_priv(dev);
1009 unsigned long flags;
1012 spin_lock_irqsave(&fep->lock, flags);
1013 rc = mii_ethtool_sset(&fep->mii_if, cmd);
1014 spin_unlock_irqrestore(&fep->lock, flags);
1019 static int fec_nway_reset(struct net_device *dev)
1021 struct fec_enet_private *fep = netdev_priv(dev);
1022 return mii_nway_restart(&fep->mii_if);
1025 static __u32 fec_get_msglevel(struct net_device *dev)
1027 struct fec_enet_private *fep = netdev_priv(dev);
1028 return fep->msg_enable;
1031 static void fec_set_msglevel(struct net_device *dev, __u32 value)
1033 struct fec_enet_private *fep = netdev_priv(dev);
1034 fep->msg_enable = value;
1037 static const struct ethtool_ops fec_ethtool_ops = {
1038 .get_drvinfo = fec_get_drvinfo,
1039 .get_regs_len = fec_get_regs_len,
1040 .get_settings = fec_get_settings,
1041 .set_settings = fec_set_settings,
1042 .nway_reset = fec_nway_reset,
1043 .get_link = ethtool_op_get_link,
1044 .get_msglevel = fec_get_msglevel,
1045 .set_msglevel = fec_set_msglevel,
1046 .get_tx_csum = ethtool_op_get_tx_csum,
1047 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
1048 .get_sg = ethtool_op_get_sg,
1049 .set_sg = ethtool_op_set_sg,
1050 .get_regs = fec_get_regs,
1053 static int fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1055 struct fec_enet_private *fep = netdev_priv(dev);
1056 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
1057 unsigned long flags;
1060 if (!netif_running(dev))
1063 spin_lock_irqsave(&fep->lock, flags);
1064 rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
1065 spin_unlock_irqrestore(&fep->lock, flags);
1069 int fec_8xx_init_one(const struct fec_platform_info *fpi,
1070 struct net_device **devp)
1072 immap_t *immap = (immap_t *) IMAP_ADDR;
1073 static int fec_8xx_version_printed = 0;
1074 struct net_device *dev = NULL;
1075 struct fec_enet_private *fep = NULL;
1084 switch (fpi->fec_no) {
1086 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec;
1090 fecp = &((immap_t *) IMAP_ADDR)->im_cpm.cp_fec2;
1097 if (fec_8xx_version_printed++ == 0)
1098 printk(KERN_INFO "%s", version);
1100 i = sizeof(*fep) + (sizeof(struct sk_buff **) *
1101 (fpi->rx_ring + fpi->tx_ring));
1103 dev = alloc_etherdev(i);
1108 SET_MODULE_OWNER(dev);
1110 fep = netdev_priv(dev);
1112 /* partial reset of FEC */
1113 fec_whack_reset(fecp);
1115 /* point rx_skbuff, tx_skbuff */
1116 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1117 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1123 spin_lock_init(&fep->lock);
1124 spin_lock_init(&fep->tx_lock);
1127 * Set the Ethernet address.
1129 for (i = 0; i < 6; i++)
1130 dev->dev_addr[i] = fpi->macaddr[i];
1132 fep->ring_base = dma_alloc_coherent(NULL,
1133 (fpi->tx_ring + fpi->rx_ring) *
1134 sizeof(cbd_t), &fep->ring_mem_addr,
1136 if (fep->ring_base == NULL) {
1137 printk(KERN_ERR DRV_MODULE_NAME
1138 ": %s dma alloc failed.\n", dev->name);
1144 * Set receive and transmit descriptor base.
1146 fep->rx_bd_base = fep->ring_base;
1147 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1149 /* initialize ring size variables */
1150 fep->tx_ring = fpi->tx_ring;
1151 fep->rx_ring = fpi->rx_ring;
1154 if (fpi->phy_irq != -1 &&
1155 (fpi->phy_irq >= SIU_IRQ0 && fpi->phy_irq < SIU_LEVEL7)) {
1157 siel = in_be32(&immap->im_siu_conf.sc_siel);
1158 if ((fpi->phy_irq & 1) == 0)
1159 siel |= (0x80000000 >> fpi->phy_irq);
1161 siel &= ~(0x80000000 >> (fpi->phy_irq & ~1));
1162 out_be32(&immap->im_siu_conf.sc_siel, siel);
1166 * The FEC Ethernet specific entries in the device structure.
1168 dev->open = fec_enet_open;
1169 dev->hard_start_xmit = fec_enet_start_xmit;
1170 dev->tx_timeout = fec_timeout;
1171 dev->watchdog_timeo = TX_TIMEOUT;
1172 dev->stop = fec_enet_close;
1173 dev->get_stats = fec_enet_get_stats;
1174 dev->set_multicast_list = fec_set_multicast_list;
1175 dev->set_mac_address = fec_set_mac_address;
1176 if (fpi->use_napi) {
1177 dev->poll = fec_enet_poll;
1178 dev->weight = fpi->napi_weight;
1180 dev->ethtool_ops = &fec_ethtool_ops;
1181 dev->do_ioctl = fec_ioctl;
1183 fep->fec_phy_speed =
1184 ((((fpi->sys_clk + 4999999) / 2500000) / 2) & 0x3F) << 1;
1186 init_timer(&fep->phy_timer_list);
1188 /* partial reset of FEC so that only MII works */
1189 FW(fecp, mii_speed, fep->fec_phy_speed);
1190 FW(fecp, ievent, 0xffc0);
1191 FW(fecp, ivec, (fpi->fec_irq / 2) << 29);
1193 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
1194 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
1196 netif_carrier_off(dev);
1198 err = register_netdev(dev);
1203 if (fpi->use_mdio) {
1204 fep->mii_if.dev = dev;
1205 fep->mii_if.mdio_read = fec_mii_read;
1206 fep->mii_if.mdio_write = fec_mii_write;
1207 fep->mii_if.phy_id_mask = 0x1f;
1208 fep->mii_if.reg_num_mask = 0x1f;
1209 fep->mii_if.phy_id = fec_mii_phy_id_detect(dev);
1219 fec_whack_reset(fecp);
1222 unregister_netdev(dev);
1226 dma_free_coherent(NULL,
1229 sizeof(cbd_t), fep->ring_base,
1230 fep->ring_mem_addr);
1237 int fec_8xx_cleanup_one(struct net_device *dev)
1239 struct fec_enet_private *fep = netdev_priv(dev);
1240 fec_t *fecp = fep->fecp;
1241 const struct fec_platform_info *fpi = fep->fpi;
1243 fec_whack_reset(fecp);
1245 unregister_netdev(dev);
1247 dma_free_coherent(NULL, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1248 fep->ring_base, fep->ring_mem_addr);
1255 /**************************************************************************************/
1256 /**************************************************************************************/
1257 /**************************************************************************************/
1259 static int __init fec_8xx_init(void)
1261 return fec_8xx_platform_init();
1264 static void __exit fec_8xx_cleanup(void)
1266 fec_8xx_platform_cleanup();
1269 /**************************************************************************************/
1270 /**************************************************************************************/
1271 /**************************************************************************************/
1273 module_init(fec_8xx_init);
1274 module_exit(fec_8xx_cleanup);