2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/ptrace.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/bitops.h>
37 #include <linux/platform_device.h>
38 #include <linux/phy.h>
40 #include <linux/vmalloc.h>
41 #include <asm/pgtable.h>
43 #include <asm/uaccess.h>
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("Freescale Ethernet Driver");
54 MODULE_LICENSE("GPL");
55 MODULE_VERSION(DRV_MODULE_VERSION);
57 int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
58 module_param(fs_enet_debug, int, 0);
59 MODULE_PARM_DESC(fs_enet_debug,
60 "Freescale bitmapped debugging message enable value");
62 #ifdef CONFIG_NET_POLL_CONTROLLER
63 static void fs_enet_netpoll(struct net_device *dev);
66 static void fs_set_multicast_list(struct net_device *dev)
68 struct fs_enet_private *fep = netdev_priv(dev);
70 (*fep->ops->set_multicast_list)(dev);
73 /* NAPI receive function */
74 static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
76 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
77 struct net_device *dev = to_net_dev(fep->dev);
78 const struct fs_platform_info *fpi = fep->fpi;
80 struct sk_buff *skb, *skbn, *skbt;
85 if (!netif_running(dev))
89 * First, grab all of the stats for the incoming packet.
90 * These get messed up if we get called due to a busy condition.
94 /* clear RX status bits for napi*/
95 (*fep->ops->napi_clear_rx_event)(dev);
97 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
98 curidx = bdp - fep->rx_bd_base;
101 * Since we have allocated space to hold a complete frame,
102 * the last indicator should be set.
104 if ((sc & BD_ENET_RX_LAST) == 0)
105 printk(KERN_WARNING DRV_MODULE_NAME
106 ": %s rcv is not +last\n",
112 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
113 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
114 fep->stats.rx_errors++;
115 /* Frame too long or too short. */
116 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
117 fep->stats.rx_length_errors++;
118 /* Frame alignment */
119 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
120 fep->stats.rx_frame_errors++;
122 if (sc & BD_ENET_RX_CR)
123 fep->stats.rx_crc_errors++;
125 if (sc & BD_ENET_RX_OV)
126 fep->stats.rx_crc_errors++;
128 skb = fep->rx_skbuff[curidx];
130 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
131 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
137 skb = fep->rx_skbuff[curidx];
139 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
140 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
144 * Process the incoming frame.
146 fep->stats.rx_packets++;
147 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
148 fep->stats.rx_bytes += pkt_len + 4;
150 if (pkt_len <= fpi->rx_copybreak) {
151 /* +2 to make IP header L1 cache aligned */
152 skbn = dev_alloc_skb(pkt_len + 2);
154 skb_reserve(skbn, 2); /* align IP header */
155 skb_copy_from_linear_data(skb,
156 skbn->data, pkt_len);
163 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
166 skb_put(skb, pkt_len); /* Make room */
167 skb->protocol = eth_type_trans(skb, dev);
169 netif_receive_skb(skb);
171 printk(KERN_WARNING DRV_MODULE_NAME
172 ": %s Memory squeeze, dropping packet.\n",
174 fep->stats.rx_dropped++;
179 fep->rx_skbuff[curidx] = skbn;
180 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
181 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
184 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
187 * Update BD pointer to next entry.
189 if ((sc & BD_ENET_RX_WRAP) == 0)
192 bdp = fep->rx_bd_base;
194 (*fep->ops->rx_bd_done)(dev);
196 if (received >= budget)
202 if (received >= budget) {
204 netif_rx_complete(dev, napi);
205 (*fep->ops->napi_enable_rx)(dev);
210 /* non NAPI receive function */
211 static int fs_enet_rx_non_napi(struct net_device *dev)
213 struct fs_enet_private *fep = netdev_priv(dev);
214 const struct fs_platform_info *fpi = fep->fpi;
216 struct sk_buff *skb, *skbn, *skbt;
221 * First, grab all of the stats for the incoming packet.
222 * These get messed up if we get called due to a busy condition.
226 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
228 curidx = bdp - fep->rx_bd_base;
231 * Since we have allocated space to hold a complete frame,
232 * the last indicator should be set.
234 if ((sc & BD_ENET_RX_LAST) == 0)
235 printk(KERN_WARNING DRV_MODULE_NAME
236 ": %s rcv is not +last\n",
242 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
243 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
244 fep->stats.rx_errors++;
245 /* Frame too long or too short. */
246 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
247 fep->stats.rx_length_errors++;
248 /* Frame alignment */
249 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
250 fep->stats.rx_frame_errors++;
252 if (sc & BD_ENET_RX_CR)
253 fep->stats.rx_crc_errors++;
255 if (sc & BD_ENET_RX_OV)
256 fep->stats.rx_crc_errors++;
258 skb = fep->rx_skbuff[curidx];
260 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
261 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
268 skb = fep->rx_skbuff[curidx];
270 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
271 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
275 * Process the incoming frame.
277 fep->stats.rx_packets++;
278 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
279 fep->stats.rx_bytes += pkt_len + 4;
281 if (pkt_len <= fpi->rx_copybreak) {
282 /* +2 to make IP header L1 cache aligned */
283 skbn = dev_alloc_skb(pkt_len + 2);
285 skb_reserve(skbn, 2); /* align IP header */
286 skb_copy_from_linear_data(skb,
287 skbn->data, pkt_len);
294 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
297 skb_put(skb, pkt_len); /* Make room */
298 skb->protocol = eth_type_trans(skb, dev);
302 printk(KERN_WARNING DRV_MODULE_NAME
303 ": %s Memory squeeze, dropping packet.\n",
305 fep->stats.rx_dropped++;
310 fep->rx_skbuff[curidx] = skbn;
311 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
312 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
315 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
318 * Update BD pointer to next entry.
320 if ((sc & BD_ENET_RX_WRAP) == 0)
323 bdp = fep->rx_bd_base;
325 (*fep->ops->rx_bd_done)(dev);
333 static void fs_enet_tx(struct net_device *dev)
335 struct fs_enet_private *fep = netdev_priv(dev);
338 int dirtyidx, do_wake, do_restart;
341 spin_lock(&fep->tx_lock);
344 do_wake = do_restart = 0;
345 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
346 dirtyidx = bdp - fep->tx_bd_base;
348 if (fep->tx_free == fep->tx_ring)
351 skb = fep->tx_skbuff[dirtyidx];
356 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
357 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
359 if (sc & BD_ENET_TX_HB) /* No heartbeat */
360 fep->stats.tx_heartbeat_errors++;
361 if (sc & BD_ENET_TX_LC) /* Late collision */
362 fep->stats.tx_window_errors++;
363 if (sc & BD_ENET_TX_RL) /* Retrans limit */
364 fep->stats.tx_aborted_errors++;
365 if (sc & BD_ENET_TX_UN) /* Underrun */
366 fep->stats.tx_fifo_errors++;
367 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
368 fep->stats.tx_carrier_errors++;
370 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
371 fep->stats.tx_errors++;
375 fep->stats.tx_packets++;
377 if (sc & BD_ENET_TX_READY)
378 printk(KERN_WARNING DRV_MODULE_NAME
379 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
383 * Deferred means some collisions occurred during transmit,
384 * but we eventually sent the packet OK.
386 if (sc & BD_ENET_TX_DEF)
387 fep->stats.collisions++;
390 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
391 skb->len, DMA_TO_DEVICE);
394 * Free the sk buffer associated with this last transmit.
396 dev_kfree_skb_irq(skb);
397 fep->tx_skbuff[dirtyidx] = NULL;
400 * Update pointer to next buffer descriptor to be transmitted.
402 if ((sc & BD_ENET_TX_WRAP) == 0)
405 bdp = fep->tx_bd_base;
408 * Since we have freed up a buffer, the ring is no longer
418 (*fep->ops->tx_restart)(dev);
420 spin_unlock(&fep->tx_lock);
423 netif_wake_queue(dev);
427 * The interrupt handler.
428 * This is called from the MPC core interrupt.
431 fs_enet_interrupt(int irq, void *dev_id)
433 struct net_device *dev = dev_id;
434 struct fs_enet_private *fep;
435 const struct fs_platform_info *fpi;
441 fep = netdev_priv(dev);
445 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
448 int_clr_events = int_events;
450 int_clr_events &= ~fep->ev_napi_rx;
452 (*fep->ops->clear_int_events)(dev, int_clr_events);
454 if (int_events & fep->ev_err)
455 (*fep->ops->ev_error)(dev, int_events);
457 if (int_events & fep->ev_rx) {
459 fs_enet_rx_non_napi(dev);
461 napi_ok = napi_schedule_prep(&fep->napi);
463 (*fep->ops->napi_disable_rx)(dev);
464 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
466 /* NOTE: it is possible for FCCs in NAPI mode */
467 /* to submit a spurious interrupt while in poll */
469 __netif_rx_schedule(dev, &fep->napi);
473 if (int_events & fep->ev_tx)
478 return IRQ_RETVAL(handled);
481 void fs_init_bds(struct net_device *dev)
483 struct fs_enet_private *fep = netdev_priv(dev);
490 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
491 fep->tx_free = fep->tx_ring;
492 fep->cur_rx = fep->rx_bd_base;
495 * Initialize the receive buffer descriptors.
497 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
498 skb = dev_alloc_skb(ENET_RX_FRSIZE);
500 printk(KERN_WARNING DRV_MODULE_NAME
501 ": %s Memory squeeze, unable to allocate skb\n",
505 fep->rx_skbuff[i] = skb;
507 dma_map_single(fep->dev, skb->data,
508 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
510 CBDW_DATLEN(bdp, 0); /* zero */
511 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
512 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
515 * if we failed, fillup remainder
517 for (; i < fep->rx_ring; i++, bdp++) {
518 fep->rx_skbuff[i] = NULL;
519 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
523 * ...and the same for transmit.
525 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
526 fep->tx_skbuff[i] = NULL;
527 CBDW_BUFADDR(bdp, 0);
529 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
533 void fs_cleanup_bds(struct net_device *dev)
535 struct fs_enet_private *fep = netdev_priv(dev);
541 * Reset SKB transmit buffers.
543 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
544 if ((skb = fep->tx_skbuff[i]) == NULL)
548 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
549 skb->len, DMA_TO_DEVICE);
551 fep->tx_skbuff[i] = NULL;
556 * Reset SKB receive buffers
558 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
559 if ((skb = fep->rx_skbuff[i]) == NULL)
563 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
564 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
567 fep->rx_skbuff[i] = NULL;
573 /**********************************************************************************/
575 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
577 struct fs_enet_private *fep = netdev_priv(dev);
583 spin_lock_irqsave(&fep->tx_lock, flags);
586 * Fill in a Tx ring entry
590 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
591 netif_stop_queue(dev);
592 spin_unlock_irqrestore(&fep->tx_lock, flags);
595 * Ooops. All transmit buffers are full. Bail out.
596 * This should not happen, since the tx queue should be stopped.
598 printk(KERN_WARNING DRV_MODULE_NAME
599 ": %s tx queue full!.\n", dev->name);
600 return NETDEV_TX_BUSY;
603 curidx = bdp - fep->tx_bd_base;
605 * Clear all of the status flags.
607 CBDC_SC(bdp, BD_ENET_TX_STATS);
612 fep->tx_skbuff[curidx] = skb;
614 fep->stats.tx_bytes += skb->len;
617 * Push the data cache so the CPM does not get stale memory data.
619 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
620 skb->data, skb->len, DMA_TO_DEVICE));
621 CBDW_DATLEN(bdp, skb->len);
623 dev->trans_start = jiffies;
626 * If this was the last BD in the ring, start at the beginning again.
628 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
631 fep->cur_tx = fep->tx_bd_base;
634 netif_stop_queue(dev);
636 /* Trigger transmission start */
637 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
638 BD_ENET_TX_LAST | BD_ENET_TX_TC;
640 /* note that while FEC does not have this bit
641 * it marks it as available for software use
642 * yay for hw reuse :) */
644 sc |= BD_ENET_TX_PAD;
647 (*fep->ops->tx_kickstart)(dev);
649 spin_unlock_irqrestore(&fep->tx_lock, flags);
654 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
657 struct fs_enet_private *fep = netdev_priv(dev);
659 (*fep->ops->pre_request_irq)(dev, irq);
660 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
663 static void fs_free_irq(struct net_device *dev, int irq)
665 struct fs_enet_private *fep = netdev_priv(dev);
668 (*fep->ops->post_free_irq)(dev, irq);
671 static void fs_timeout(struct net_device *dev)
673 struct fs_enet_private *fep = netdev_priv(dev);
677 fep->stats.tx_errors++;
679 spin_lock_irqsave(&fep->lock, flags);
681 if (dev->flags & IFF_UP) {
682 phy_stop(fep->phydev);
683 (*fep->ops->stop)(dev);
684 (*fep->ops->restart)(dev);
685 phy_start(fep->phydev);
688 phy_start(fep->phydev);
689 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
690 spin_unlock_irqrestore(&fep->lock, flags);
693 netif_wake_queue(dev);
696 /*-----------------------------------------------------------------------------
697 * generic link-change handler - should be sufficient for most cases
698 *-----------------------------------------------------------------------------*/
699 static void generic_adjust_link(struct net_device *dev)
701 struct fs_enet_private *fep = netdev_priv(dev);
702 struct phy_device *phydev = fep->phydev;
706 /* adjust to duplex mode */
707 if (phydev->duplex != fep->oldduplex) {
709 fep->oldduplex = phydev->duplex;
712 if (phydev->speed != fep->oldspeed) {
714 fep->oldspeed = phydev->speed;
721 netif_carrier_on(dev);
722 netif_start_queue(dev);
726 fep->ops->restart(dev);
727 } else if (fep->oldlink) {
732 netif_carrier_off(dev);
733 netif_stop_queue(dev);
736 if (new_state && netif_msg_link(fep))
737 phy_print_status(phydev);
741 static void fs_adjust_link(struct net_device *dev)
743 struct fs_enet_private *fep = netdev_priv(dev);
746 spin_lock_irqsave(&fep->lock, flags);
748 if(fep->ops->adjust_link)
749 fep->ops->adjust_link(dev);
751 generic_adjust_link(dev);
753 spin_unlock_irqrestore(&fep->lock, flags);
756 static int fs_init_phy(struct net_device *dev)
758 struct fs_enet_private *fep = netdev_priv(dev);
759 struct phy_device *phydev;
765 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
766 PHY_INTERFACE_MODE_MII);
768 printk("No phy bus ID specified in BSP code\n");
771 if (IS_ERR(phydev)) {
772 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
773 return PTR_ERR(phydev);
776 fep->phydev = phydev;
781 static int fs_enet_open(struct net_device *dev)
783 struct fs_enet_private *fep = netdev_priv(dev);
787 napi_enable(&fep->napi);
789 /* Install our interrupt handler. */
790 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
792 printk(KERN_ERR DRV_MODULE_NAME
793 ": %s Could not allocate FS_ENET IRQ!", dev->name);
794 napi_disable(&fep->napi);
798 err = fs_init_phy(dev);
800 napi_disable(&fep->napi);
803 phy_start(fep->phydev);
808 static int fs_enet_close(struct net_device *dev)
810 struct fs_enet_private *fep = netdev_priv(dev);
813 netif_stop_queue(dev);
814 netif_carrier_off(dev);
815 napi_disable(&fep->napi);
816 phy_stop(fep->phydev);
818 spin_lock_irqsave(&fep->lock, flags);
819 spin_lock(&fep->tx_lock);
820 (*fep->ops->stop)(dev);
821 spin_unlock(&fep->tx_lock);
822 spin_unlock_irqrestore(&fep->lock, flags);
824 /* release any irqs */
825 phy_disconnect(fep->phydev);
827 fs_free_irq(dev, fep->interrupt);
832 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
834 struct fs_enet_private *fep = netdev_priv(dev);
838 /*************************************************************************/
840 static void fs_get_drvinfo(struct net_device *dev,
841 struct ethtool_drvinfo *info)
843 strcpy(info->driver, DRV_MODULE_NAME);
844 strcpy(info->version, DRV_MODULE_VERSION);
847 static int fs_get_regs_len(struct net_device *dev)
849 struct fs_enet_private *fep = netdev_priv(dev);
851 return (*fep->ops->get_regs_len)(dev);
854 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
857 struct fs_enet_private *fep = netdev_priv(dev);
863 spin_lock_irqsave(&fep->lock, flags);
864 r = (*fep->ops->get_regs)(dev, p, &len);
865 spin_unlock_irqrestore(&fep->lock, flags);
871 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
873 struct fs_enet_private *fep = netdev_priv(dev);
874 return phy_ethtool_gset(fep->phydev, cmd);
877 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
879 struct fs_enet_private *fep = netdev_priv(dev);
880 phy_ethtool_sset(fep->phydev, cmd);
884 static int fs_nway_reset(struct net_device *dev)
889 static u32 fs_get_msglevel(struct net_device *dev)
891 struct fs_enet_private *fep = netdev_priv(dev);
892 return fep->msg_enable;
895 static void fs_set_msglevel(struct net_device *dev, u32 value)
897 struct fs_enet_private *fep = netdev_priv(dev);
898 fep->msg_enable = value;
901 static const struct ethtool_ops fs_ethtool_ops = {
902 .get_drvinfo = fs_get_drvinfo,
903 .get_regs_len = fs_get_regs_len,
904 .get_settings = fs_get_settings,
905 .set_settings = fs_set_settings,
906 .nway_reset = fs_nway_reset,
907 .get_link = ethtool_op_get_link,
908 .get_msglevel = fs_get_msglevel,
909 .set_msglevel = fs_set_msglevel,
910 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
911 .set_sg = ethtool_op_set_sg,
912 .get_regs = fs_get_regs,
915 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
917 struct fs_enet_private *fep = netdev_priv(dev);
918 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
922 if (!netif_running(dev))
925 spin_lock_irqsave(&fep->lock, flags);
926 rc = phy_mii_ioctl(fep->phydev, mii, cmd);
927 spin_unlock_irqrestore(&fep->lock, flags);
931 extern int fs_mii_connect(struct net_device *dev);
932 extern void fs_mii_disconnect(struct net_device *dev);
934 static struct net_device *fs_init_instance(struct device *dev,
935 struct fs_platform_info *fpi)
937 struct net_device *ndev = NULL;
938 struct fs_enet_private *fep = NULL;
939 int privsize, i, r, err = 0, registered = 0;
941 fpi->fs_no = fs_get_id(fpi);
943 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
944 return ERR_PTR(-EINVAL);
946 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
947 (fpi->rx_ring + fpi->tx_ring));
949 ndev = alloc_etherdev(privsize);
955 fep = netdev_priv(ndev);
958 dev_set_drvdata(dev, ndev);
960 if (fpi->init_ioports)
961 fpi->init_ioports((struct fs_platform_info *)fpi);
963 #ifdef CONFIG_FS_ENET_HAS_FEC
964 if (fs_get_fec_index(fpi->fs_no) >= 0)
965 fep->ops = &fs_fec_ops;
968 #ifdef CONFIG_FS_ENET_HAS_SCC
969 if (fs_get_scc_index(fpi->fs_no) >=0)
970 fep->ops = &fs_scc_ops;
973 #ifdef CONFIG_FS_ENET_HAS_FCC
974 if (fs_get_fcc_index(fpi->fs_no) >= 0)
975 fep->ops = &fs_fcc_ops;
978 if (fep->ops == NULL) {
979 printk(KERN_ERR DRV_MODULE_NAME
980 ": %s No matching ops found (%d).\n",
981 ndev->name, fpi->fs_no);
986 r = (*fep->ops->setup_data)(ndev);
988 printk(KERN_ERR DRV_MODULE_NAME
989 ": %s setup_data failed\n",
995 /* point rx_skbuff, tx_skbuff */
996 fep->rx_skbuff = (struct sk_buff **)&fep[1];
997 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1000 spin_lock_init(&fep->lock);
1001 spin_lock_init(&fep->tx_lock);
1004 * Set the Ethernet address.
1006 for (i = 0; i < 6; i++)
1007 ndev->dev_addr[i] = fpi->macaddr[i];
1009 r = (*fep->ops->allocate_bd)(ndev);
1011 if (fep->ring_base == NULL) {
1012 printk(KERN_ERR DRV_MODULE_NAME
1013 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1019 * Set receive and transmit descriptor base.
1021 fep->rx_bd_base = fep->ring_base;
1022 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1024 /* initialize ring size variables */
1025 fep->tx_ring = fpi->tx_ring;
1026 fep->rx_ring = fpi->rx_ring;
1029 * The FEC Ethernet specific entries in the device structure.
1031 ndev->open = fs_enet_open;
1032 ndev->hard_start_xmit = fs_enet_start_xmit;
1033 ndev->tx_timeout = fs_timeout;
1034 ndev->watchdog_timeo = 2 * HZ;
1035 ndev->stop = fs_enet_close;
1036 ndev->get_stats = fs_enet_get_stats;
1037 ndev->set_multicast_list = fs_set_multicast_list;
1039 #ifdef CONFIG_NET_POLL_CONTROLLER
1040 ndev->poll_controller = fs_enet_netpoll;
1043 netif_napi_add(ndev, &fep->napi,
1044 fs_enet_rx_napi, fpi->napi_weight);
1046 ndev->ethtool_ops = &fs_ethtool_ops;
1047 ndev->do_ioctl = fs_ioctl;
1049 init_timer(&fep->phy_timer_list);
1051 netif_carrier_off(ndev);
1053 err = register_netdev(ndev);
1055 printk(KERN_ERR DRV_MODULE_NAME
1056 ": %s register_netdev failed.\n", ndev->name);
1067 unregister_netdev(ndev);
1070 (*fep->ops->free_bd)(ndev);
1071 (*fep->ops->cleanup_data)(ndev);
1077 dev_set_drvdata(dev, NULL);
1079 return ERR_PTR(err);
1082 static int fs_cleanup_instance(struct net_device *ndev)
1084 struct fs_enet_private *fep;
1085 const struct fs_platform_info *fpi;
1091 fep = netdev_priv(ndev);
1097 unregister_netdev(ndev);
1099 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1100 fep->ring_base, fep->ring_mem_addr);
1103 (*fep->ops->cleanup_data)(ndev);
1107 dev_set_drvdata(dev, NULL);
1116 /**************************************************************************************/
1118 /* handy pointer to the immap */
1119 void *fs_enet_immap = NULL;
1121 static int setup_immap(void)
1123 phys_addr_t paddr = 0;
1124 unsigned long size = 0;
1128 size = 0x10000; /* map 64K */
1132 paddr = CPM_MAP_ADDR;
1133 size = 0x40000; /* map 256 K */
1135 fs_enet_immap = ioremap(paddr, size);
1136 if (fs_enet_immap == NULL)
1137 return -EBADF; /* XXX ahem; maybe just BUG_ON? */
1142 static void cleanup_immap(void)
1144 if (fs_enet_immap != NULL) {
1145 iounmap(fs_enet_immap);
1146 fs_enet_immap = NULL;
1150 /**************************************************************************************/
1152 static int __devinit fs_enet_probe(struct device *dev)
1154 struct net_device *ndev;
1156 /* no fixup - no device */
1157 if (dev->platform_data == NULL) {
1158 printk(KERN_INFO "fs_enet: "
1159 "probe called with no platform data; "
1160 "remove unused devices\n");
1164 ndev = fs_init_instance(dev, dev->platform_data);
1166 return PTR_ERR(ndev);
1170 static int fs_enet_remove(struct device *dev)
1172 return fs_cleanup_instance(dev_get_drvdata(dev));
1175 static struct device_driver fs_enet_fec_driver = {
1176 .name = "fsl-cpm-fec",
1177 .bus = &platform_bus_type,
1178 .probe = fs_enet_probe,
1179 .remove = fs_enet_remove,
1181 /* .suspend = fs_enet_suspend, TODO */
1182 /* .resume = fs_enet_resume, TODO */
1186 static struct device_driver fs_enet_scc_driver = {
1187 .name = "fsl-cpm-scc",
1188 .bus = &platform_bus_type,
1189 .probe = fs_enet_probe,
1190 .remove = fs_enet_remove,
1192 /* .suspend = fs_enet_suspend, TODO */
1193 /* .resume = fs_enet_resume, TODO */
1197 static struct device_driver fs_enet_fcc_driver = {
1198 .name = "fsl-cpm-fcc",
1199 .bus = &platform_bus_type,
1200 .probe = fs_enet_probe,
1201 .remove = fs_enet_remove,
1203 /* .suspend = fs_enet_suspend, TODO */
1204 /* .resume = fs_enet_resume, TODO */
1208 static int __init fs_init(void)
1219 #ifdef CONFIG_FS_ENET_HAS_FCC
1220 /* let's insert mii stuff */
1221 r = fs_enet_mdio_bb_init();
1224 printk(KERN_ERR DRV_MODULE_NAME
1225 "BB PHY init failed.\n");
1228 r = driver_register(&fs_enet_fcc_driver);
1233 #ifdef CONFIG_FS_ENET_HAS_FEC
1234 r = fs_enet_mdio_fec_init();
1236 printk(KERN_ERR DRV_MODULE_NAME
1237 "FEC PHY init failed.\n");
1241 r = driver_register(&fs_enet_fec_driver);
1246 #ifdef CONFIG_FS_ENET_HAS_SCC
1247 r = driver_register(&fs_enet_scc_driver);
1258 static void __exit fs_cleanup(void)
1260 driver_unregister(&fs_enet_fec_driver);
1261 driver_unregister(&fs_enet_fcc_driver);
1262 driver_unregister(&fs_enet_scc_driver);
1266 #ifdef CONFIG_NET_POLL_CONTROLLER
1267 static void fs_enet_netpoll(struct net_device *dev)
1269 disable_irq(dev->irq);
1270 fs_enet_interrupt(dev->irq, dev, NULL);
1271 enable_irq(dev->irq);
1275 /**************************************************************************************/
1277 module_init(fs_init);
1278 module_exit(fs_cleanup);