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/pgtable.h>
45 #include <asm/uaccess.h>
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("Freescale Ethernet Driver");
56 MODULE_LICENSE("GPL");
57 MODULE_VERSION(DRV_MODULE_VERSION);
59 int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
60 module_param(fs_enet_debug, int, 0);
61 MODULE_PARM_DESC(fs_enet_debug,
62 "Freescale bitmapped debugging message enable value");
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 /* NAPI receive function */
73 static int fs_enet_rx_napi(struct net_device *dev, int *budget)
75 struct fs_enet_private *fep = netdev_priv(dev);
76 const struct fs_platform_info *fpi = fep->fpi;
78 struct sk_buff *skb, *skbn, *skbt;
82 int rx_work_limit = 0; /* pacify gcc */
84 rx_work_limit = min(dev->quota, *budget);
86 if (!netif_running(dev))
90 * First, grab all of the stats for the incoming packet.
91 * These get messed up if we get called due to a busy condition.
95 /* clear RX status bits for napi*/
96 (*fep->ops->napi_clear_rx_event)(dev);
98 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
100 curidx = bdp - fep->rx_bd_base;
103 * Since we have allocated space to hold a complete frame,
104 * the last indicator should be set.
106 if ((sc & BD_ENET_RX_LAST) == 0)
107 printk(KERN_WARNING DRV_MODULE_NAME
108 ": %s rcv is not +last\n",
114 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
115 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
116 fep->stats.rx_errors++;
117 /* Frame too long or too short. */
118 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
119 fep->stats.rx_length_errors++;
120 /* Frame alignment */
121 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
122 fep->stats.rx_frame_errors++;
124 if (sc & BD_ENET_RX_CR)
125 fep->stats.rx_crc_errors++;
127 if (sc & BD_ENET_RX_OV)
128 fep->stats.rx_crc_errors++;
130 skb = fep->rx_skbuff[curidx];
132 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
133 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
140 /* napi, got packet but no quota */
141 if (--rx_work_limit < 0)
144 skb = fep->rx_skbuff[curidx];
146 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
147 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
151 * Process the incoming frame.
153 fep->stats.rx_packets++;
154 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
155 fep->stats.rx_bytes += pkt_len + 4;
157 if (pkt_len <= fpi->rx_copybreak) {
158 /* +2 to make IP header L1 cache aligned */
159 skbn = dev_alloc_skb(pkt_len + 2);
161 skb_reserve(skbn, 2); /* align IP header */
162 skb_copy_from_linear_data(skb,
163 skbn->data, pkt_len);
170 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
173 skb_put(skb, pkt_len); /* Make room */
174 skb->protocol = eth_type_trans(skb, dev);
176 netif_receive_skb(skb);
178 printk(KERN_WARNING DRV_MODULE_NAME
179 ": %s Memory squeeze, dropping packet.\n",
181 fep->stats.rx_dropped++;
186 fep->rx_skbuff[curidx] = skbn;
187 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
188 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
191 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
194 * Update BD pointer to next entry.
196 if ((sc & BD_ENET_RX_WRAP) == 0)
199 bdp = fep->rx_bd_base;
201 (*fep->ops->rx_bd_done)(dev);
206 dev->quota -= received;
209 if (rx_work_limit < 0)
210 return 1; /* not done */
213 netif_rx_complete(dev);
215 (*fep->ops->napi_enable_rx)(dev);
220 /* non NAPI receive function */
221 static int fs_enet_rx_non_napi(struct net_device *dev)
223 struct fs_enet_private *fep = netdev_priv(dev);
224 const struct fs_platform_info *fpi = fep->fpi;
226 struct sk_buff *skb, *skbn, *skbt;
231 * First, grab all of the stats for the incoming packet.
232 * These get messed up if we get called due to a busy condition.
236 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
238 curidx = bdp - fep->rx_bd_base;
241 * Since we have allocated space to hold a complete frame,
242 * the last indicator should be set.
244 if ((sc & BD_ENET_RX_LAST) == 0)
245 printk(KERN_WARNING DRV_MODULE_NAME
246 ": %s rcv is not +last\n",
252 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
253 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
254 fep->stats.rx_errors++;
255 /* Frame too long or too short. */
256 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
257 fep->stats.rx_length_errors++;
258 /* Frame alignment */
259 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
260 fep->stats.rx_frame_errors++;
262 if (sc & BD_ENET_RX_CR)
263 fep->stats.rx_crc_errors++;
265 if (sc & BD_ENET_RX_OV)
266 fep->stats.rx_crc_errors++;
268 skb = fep->rx_skbuff[curidx];
270 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
271 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
278 skb = fep->rx_skbuff[curidx];
280 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
281 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
285 * Process the incoming frame.
287 fep->stats.rx_packets++;
288 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
289 fep->stats.rx_bytes += pkt_len + 4;
291 if (pkt_len <= fpi->rx_copybreak) {
292 /* +2 to make IP header L1 cache aligned */
293 skbn = dev_alloc_skb(pkt_len + 2);
295 skb_reserve(skbn, 2); /* align IP header */
296 skb_copy_from_linear_data(skb,
297 skbn->data, pkt_len);
304 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
307 skb_put(skb, pkt_len); /* Make room */
308 skb->protocol = eth_type_trans(skb, dev);
312 printk(KERN_WARNING DRV_MODULE_NAME
313 ": %s Memory squeeze, dropping packet.\n",
315 fep->stats.rx_dropped++;
320 fep->rx_skbuff[curidx] = skbn;
321 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
322 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
325 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
328 * Update BD pointer to next entry.
330 if ((sc & BD_ENET_RX_WRAP) == 0)
333 bdp = fep->rx_bd_base;
335 (*fep->ops->rx_bd_done)(dev);
343 static void fs_enet_tx(struct net_device *dev)
345 struct fs_enet_private *fep = netdev_priv(dev);
348 int dirtyidx, do_wake, do_restart;
351 spin_lock(&fep->lock);
354 do_wake = do_restart = 0;
355 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
357 dirtyidx = bdp - fep->tx_bd_base;
359 if (fep->tx_free == fep->tx_ring)
362 skb = fep->tx_skbuff[dirtyidx];
367 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
368 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
370 if (sc & BD_ENET_TX_HB) /* No heartbeat */
371 fep->stats.tx_heartbeat_errors++;
372 if (sc & BD_ENET_TX_LC) /* Late collision */
373 fep->stats.tx_window_errors++;
374 if (sc & BD_ENET_TX_RL) /* Retrans limit */
375 fep->stats.tx_aborted_errors++;
376 if (sc & BD_ENET_TX_UN) /* Underrun */
377 fep->stats.tx_fifo_errors++;
378 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
379 fep->stats.tx_carrier_errors++;
381 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
382 fep->stats.tx_errors++;
386 fep->stats.tx_packets++;
388 if (sc & BD_ENET_TX_READY)
389 printk(KERN_WARNING DRV_MODULE_NAME
390 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
394 * Deferred means some collisions occurred during transmit,
395 * but we eventually sent the packet OK.
397 if (sc & BD_ENET_TX_DEF)
398 fep->stats.collisions++;
401 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
402 skb->len, DMA_TO_DEVICE);
405 * Free the sk buffer associated with this last transmit.
407 dev_kfree_skb_irq(skb);
408 fep->tx_skbuff[dirtyidx] = NULL;
411 * Update pointer to next buffer descriptor to be transmitted.
413 if ((sc & BD_ENET_TX_WRAP) == 0)
416 bdp = fep->tx_bd_base;
419 * Since we have freed up a buffer, the ring is no longer
429 (*fep->ops->tx_restart)(dev);
431 spin_unlock(&fep->lock);
434 netif_wake_queue(dev);
438 * The interrupt handler.
439 * This is called from the MPC core interrupt.
442 fs_enet_interrupt(int irq, void *dev_id)
444 struct net_device *dev = dev_id;
445 struct fs_enet_private *fep;
446 const struct fs_platform_info *fpi;
452 fep = netdev_priv(dev);
456 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 = netif_rx_schedule_prep(dev);
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);
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 fep->rx_skbuff[i] = skb;
519 dma_map_single(fep->dev, skb->data,
520 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
522 CBDW_DATLEN(bdp, 0); /* zero */
523 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
524 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
527 * if we failed, fillup remainder
529 for (; i < fep->rx_ring; i++, bdp++) {
530 fep->rx_skbuff[i] = NULL;
531 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
535 * ...and the same for transmit.
537 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
538 fep->tx_skbuff[i] = NULL;
539 CBDW_BUFADDR(bdp, 0);
541 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
545 void fs_cleanup_bds(struct net_device *dev)
547 struct fs_enet_private *fep = netdev_priv(dev);
553 * Reset SKB transmit buffers.
555 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
556 if ((skb = fep->tx_skbuff[i]) == NULL)
560 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
561 skb->len, DMA_TO_DEVICE);
563 fep->tx_skbuff[i] = NULL;
568 * Reset SKB receive buffers
570 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
571 if ((skb = fep->rx_skbuff[i]) == NULL)
575 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
576 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
579 fep->rx_skbuff[i] = NULL;
585 /**********************************************************************************/
587 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
589 struct fs_enet_private *fep = netdev_priv(dev);
595 spin_lock_irqsave(&fep->tx_lock, flags);
598 * Fill in a Tx ring entry
602 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
603 netif_stop_queue(dev);
604 spin_unlock_irqrestore(&fep->tx_lock, flags);
607 * Ooops. All transmit buffers are full. Bail out.
608 * This should not happen, since the tx queue should be stopped.
610 printk(KERN_WARNING DRV_MODULE_NAME
611 ": %s tx queue full!.\n", dev->name);
612 return NETDEV_TX_BUSY;
615 curidx = bdp - fep->tx_bd_base;
617 * Clear all of the status flags.
619 CBDC_SC(bdp, BD_ENET_TX_STATS);
624 fep->tx_skbuff[curidx] = skb;
626 fep->stats.tx_bytes += skb->len;
629 * Push the data cache so the CPM does not get stale memory data.
631 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
632 skb->data, skb->len, DMA_TO_DEVICE));
633 CBDW_DATLEN(bdp, skb->len);
635 dev->trans_start = jiffies;
638 * If this was the last BD in the ring, start at the beginning again.
640 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
643 fep->cur_tx = fep->tx_bd_base;
646 netif_stop_queue(dev);
648 /* Trigger transmission start */
649 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
650 BD_ENET_TX_LAST | BD_ENET_TX_TC;
652 /* note that while FEC does not have this bit
653 * it marks it as available for software use
654 * yay for hw reuse :) */
656 sc |= BD_ENET_TX_PAD;
659 (*fep->ops->tx_kickstart)(dev);
661 spin_unlock_irqrestore(&fep->tx_lock, flags);
666 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
669 struct fs_enet_private *fep = netdev_priv(dev);
671 (*fep->ops->pre_request_irq)(dev, irq);
672 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
675 static void fs_free_irq(struct net_device *dev, int irq)
677 struct fs_enet_private *fep = netdev_priv(dev);
680 (*fep->ops->post_free_irq)(dev, irq);
683 static void fs_timeout(struct net_device *dev)
685 struct fs_enet_private *fep = netdev_priv(dev);
689 fep->stats.tx_errors++;
691 spin_lock_irqsave(&fep->lock, flags);
693 if (dev->flags & IFF_UP) {
694 phy_stop(fep->phydev);
695 (*fep->ops->stop)(dev);
696 (*fep->ops->restart)(dev);
697 phy_start(fep->phydev);
700 phy_start(fep->phydev);
701 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
702 spin_unlock_irqrestore(&fep->lock, flags);
705 netif_wake_queue(dev);
708 /*-----------------------------------------------------------------------------
709 * generic link-change handler - should be sufficient for most cases
710 *-----------------------------------------------------------------------------*/
711 static void generic_adjust_link(struct net_device *dev)
713 struct fs_enet_private *fep = netdev_priv(dev);
714 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;
734 netif_carrier_on(dev);
735 netif_start_queue(dev);
739 fep->ops->restart(dev);
741 } else if (fep->oldlink) {
746 netif_carrier_off(dev);
747 netif_stop_queue(dev);
750 if (new_state && netif_msg_link(fep))
751 phy_print_status(phydev);
755 static void fs_adjust_link(struct net_device *dev)
757 struct fs_enet_private *fep = netdev_priv(dev);
760 spin_lock_irqsave(&fep->lock, flags);
762 if(fep->ops->adjust_link)
763 fep->ops->adjust_link(dev);
765 generic_adjust_link(dev);
767 spin_unlock_irqrestore(&fep->lock, flags);
770 static int fs_init_phy(struct net_device *dev)
772 struct fs_enet_private *fep = netdev_priv(dev);
773 struct phy_device *phydev;
779 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
780 PHY_INTERFACE_MODE_MII);
782 printk("No phy bus ID specified in BSP code\n");
785 if (IS_ERR(phydev)) {
786 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
787 return PTR_ERR(phydev);
790 fep->phydev = phydev;
796 static int fs_enet_open(struct net_device *dev)
798 struct fs_enet_private *fep = netdev_priv(dev);
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);
810 err = fs_init_phy(dev);
814 phy_start(fep->phydev);
819 static int fs_enet_close(struct net_device *dev)
821 struct fs_enet_private *fep = netdev_priv(dev);
824 netif_stop_queue(dev);
825 netif_carrier_off(dev);
826 phy_stop(fep->phydev);
828 spin_lock_irqsave(&fep->lock, flags);
829 (*fep->ops->stop)(dev);
830 spin_unlock_irqrestore(&fep->lock, flags);
832 /* release any irqs */
833 phy_disconnect(fep->phydev);
835 fs_free_irq(dev, fep->interrupt);
840 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
842 struct fs_enet_private *fep = netdev_priv(dev);
846 /*************************************************************************/
848 static void fs_get_drvinfo(struct net_device *dev,
849 struct ethtool_drvinfo *info)
851 strcpy(info->driver, DRV_MODULE_NAME);
852 strcpy(info->version, DRV_MODULE_VERSION);
855 static int fs_get_regs_len(struct net_device *dev)
857 struct fs_enet_private *fep = netdev_priv(dev);
859 return (*fep->ops->get_regs_len)(dev);
862 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
865 struct fs_enet_private *fep = netdev_priv(dev);
871 spin_lock_irqsave(&fep->lock, flags);
872 r = (*fep->ops->get_regs)(dev, p, &len);
873 spin_unlock_irqrestore(&fep->lock, flags);
879 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
881 struct fs_enet_private *fep = netdev_priv(dev);
882 return phy_ethtool_gset(fep->phydev, cmd);
885 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
887 struct fs_enet_private *fep = netdev_priv(dev);
888 phy_ethtool_sset(fep->phydev, cmd);
892 static int fs_nway_reset(struct net_device *dev)
897 static u32 fs_get_msglevel(struct net_device *dev)
899 struct fs_enet_private *fep = netdev_priv(dev);
900 return fep->msg_enable;
903 static void fs_set_msglevel(struct net_device *dev, u32 value)
905 struct fs_enet_private *fep = netdev_priv(dev);
906 fep->msg_enable = value;
909 static const struct ethtool_ops fs_ethtool_ops = {
910 .get_drvinfo = fs_get_drvinfo,
911 .get_regs_len = fs_get_regs_len,
912 .get_settings = fs_get_settings,
913 .set_settings = fs_set_settings,
914 .nway_reset = fs_nway_reset,
915 .get_link = ethtool_op_get_link,
916 .get_msglevel = fs_get_msglevel,
917 .set_msglevel = fs_set_msglevel,
918 .get_tx_csum = ethtool_op_get_tx_csum,
919 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
920 .get_sg = ethtool_op_get_sg,
921 .set_sg = ethtool_op_set_sg,
922 .get_regs = fs_get_regs,
925 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
927 struct fs_enet_private *fep = netdev_priv(dev);
928 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
932 if (!netif_running(dev))
935 spin_lock_irqsave(&fep->lock, flags);
936 rc = phy_mii_ioctl(fep->phydev, mii, cmd);
937 spin_unlock_irqrestore(&fep->lock, flags);
941 extern int fs_mii_connect(struct net_device *dev);
942 extern void fs_mii_disconnect(struct net_device *dev);
944 static struct net_device *fs_init_instance(struct device *dev,
945 struct fs_platform_info *fpi)
947 struct net_device *ndev = NULL;
948 struct fs_enet_private *fep = NULL;
949 int privsize, i, r, err = 0, registered = 0;
951 fpi->fs_no = fs_get_id(fpi);
953 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
954 return ERR_PTR(-EINVAL);
956 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
957 (fpi->rx_ring + fpi->tx_ring));
959 ndev = alloc_etherdev(privsize);
964 SET_MODULE_OWNER(ndev);
966 fep = netdev_priv(ndev);
967 memset(fep, 0, privsize); /* clear everything */
970 dev_set_drvdata(dev, ndev);
972 if (fpi->init_ioports)
973 fpi->init_ioports((struct fs_platform_info *)fpi);
975 #ifdef CONFIG_FS_ENET_HAS_FEC
976 if (fs_get_fec_index(fpi->fs_no) >= 0)
977 fep->ops = &fs_fec_ops;
980 #ifdef CONFIG_FS_ENET_HAS_SCC
981 if (fs_get_scc_index(fpi->fs_no) >=0 )
982 fep->ops = &fs_scc_ops;
985 #ifdef CONFIG_FS_ENET_HAS_FCC
986 if (fs_get_fcc_index(fpi->fs_no) >= 0)
987 fep->ops = &fs_fcc_ops;
990 if (fep->ops == NULL) {
991 printk(KERN_ERR DRV_MODULE_NAME
992 ": %s No matching ops found (%d).\n",
993 ndev->name, fpi->fs_no);
998 r = (*fep->ops->setup_data)(ndev);
1000 printk(KERN_ERR DRV_MODULE_NAME
1001 ": %s setup_data failed\n",
1007 /* point rx_skbuff, tx_skbuff */
1008 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1009 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1012 spin_lock_init(&fep->lock);
1013 spin_lock_init(&fep->tx_lock);
1016 * Set the Ethernet address.
1018 for (i = 0; i < 6; i++)
1019 ndev->dev_addr[i] = fpi->macaddr[i];
1021 r = (*fep->ops->allocate_bd)(ndev);
1023 if (fep->ring_base == NULL) {
1024 printk(KERN_ERR DRV_MODULE_NAME
1025 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1031 * Set receive and transmit descriptor base.
1033 fep->rx_bd_base = fep->ring_base;
1034 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1036 /* initialize ring size variables */
1037 fep->tx_ring = fpi->tx_ring;
1038 fep->rx_ring = fpi->rx_ring;
1041 * The FEC Ethernet specific entries in the device structure.
1043 ndev->open = fs_enet_open;
1044 ndev->hard_start_xmit = fs_enet_start_xmit;
1045 ndev->tx_timeout = fs_timeout;
1046 ndev->watchdog_timeo = 2 * HZ;
1047 ndev->stop = fs_enet_close;
1048 ndev->get_stats = fs_enet_get_stats;
1049 ndev->set_multicast_list = fs_set_multicast_list;
1050 if (fpi->use_napi) {
1051 ndev->poll = fs_enet_rx_napi;
1052 ndev->weight = fpi->napi_weight;
1054 ndev->ethtool_ops = &fs_ethtool_ops;
1055 ndev->do_ioctl = fs_ioctl;
1057 init_timer(&fep->phy_timer_list);
1059 netif_carrier_off(ndev);
1061 err = register_netdev(ndev);
1063 printk(KERN_ERR DRV_MODULE_NAME
1064 ": %s register_netdev failed.\n", ndev->name);
1076 unregister_netdev(ndev);
1079 (*fep->ops->free_bd)(ndev);
1080 (*fep->ops->cleanup_data)(ndev);
1086 dev_set_drvdata(dev, NULL);
1088 return ERR_PTR(err);
1091 static int fs_cleanup_instance(struct net_device *ndev)
1093 struct fs_enet_private *fep;
1094 const struct fs_platform_info *fpi;
1100 fep = netdev_priv(ndev);
1106 unregister_netdev(ndev);
1108 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1109 fep->ring_base, fep->ring_mem_addr);
1112 (*fep->ops->cleanup_data)(ndev);
1116 dev_set_drvdata(dev, NULL);
1125 /**************************************************************************************/
1127 /* handy pointer to the immap */
1128 void *fs_enet_immap = NULL;
1130 static int setup_immap(void)
1132 phys_addr_t paddr = 0;
1133 unsigned long size = 0;
1137 size = 0x10000; /* map 64K */
1141 paddr = CPM_MAP_ADDR;
1142 size = 0x40000; /* map 256 K */
1144 fs_enet_immap = ioremap(paddr, size);
1145 if (fs_enet_immap == NULL)
1146 return -EBADF; /* XXX ahem; maybe just BUG_ON? */
1151 static void cleanup_immap(void)
1153 if (fs_enet_immap != NULL) {
1154 iounmap(fs_enet_immap);
1155 fs_enet_immap = NULL;
1159 /**************************************************************************************/
1161 static int __devinit fs_enet_probe(struct device *dev)
1163 struct net_device *ndev;
1165 /* no fixup - no device */
1166 if (dev->platform_data == NULL) {
1167 printk(KERN_INFO "fs_enet: "
1168 "probe called with no platform data; "
1169 "remove unused devices\n");
1173 ndev = fs_init_instance(dev, dev->platform_data);
1175 return PTR_ERR(ndev);
1179 static int fs_enet_remove(struct device *dev)
1181 return fs_cleanup_instance(dev_get_drvdata(dev));
1184 static struct device_driver fs_enet_fec_driver = {
1185 .name = "fsl-cpm-fec",
1186 .bus = &platform_bus_type,
1187 .probe = fs_enet_probe,
1188 .remove = fs_enet_remove,
1190 /* .suspend = fs_enet_suspend, TODO */
1191 /* .resume = fs_enet_resume, TODO */
1195 static struct device_driver fs_enet_scc_driver = {
1196 .name = "fsl-cpm-scc",
1197 .bus = &platform_bus_type,
1198 .probe = fs_enet_probe,
1199 .remove = fs_enet_remove,
1201 /* .suspend = fs_enet_suspend, TODO */
1202 /* .resume = fs_enet_resume, TODO */
1206 static struct device_driver fs_enet_fcc_driver = {
1207 .name = "fsl-cpm-fcc",
1208 .bus = &platform_bus_type,
1209 .probe = fs_enet_probe,
1210 .remove = fs_enet_remove,
1212 /* .suspend = fs_enet_suspend, TODO */
1213 /* .resume = fs_enet_resume, TODO */
1217 static int __init fs_init(void)
1228 #ifdef CONFIG_FS_ENET_HAS_FCC
1229 /* let's insert mii stuff */
1230 r = fs_enet_mdio_bb_init();
1233 printk(KERN_ERR DRV_MODULE_NAME
1234 "BB PHY init failed.\n");
1237 r = driver_register(&fs_enet_fcc_driver);
1242 #ifdef CONFIG_FS_ENET_HAS_FEC
1243 r = fs_enet_mdio_fec_init();
1245 printk(KERN_ERR DRV_MODULE_NAME
1246 "FEC PHY init failed.\n");
1250 r = driver_register(&fs_enet_fec_driver);
1255 #ifdef CONFIG_FS_ENET_HAS_SCC
1256 r = driver_register(&fs_enet_scc_driver);
1268 static void __exit fs_cleanup(void)
1270 driver_unregister(&fs_enet_fec_driver);
1271 driver_unregister(&fs_enet_fcc_driver);
1272 driver_unregister(&fs_enet_scc_driver);
1276 /**************************************************************************************/
1278 module_init(fs_init);
1279 module_exit(fs_cleanup);