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/pci.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/spinlock.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/bitops.h>
38 #include <linux/platform_device.h>
39 #include <linux/phy.h>
41 #include <linux/vmalloc.h>
42 #include <asm/pgtable.h>
44 #include <asm/pgtable.h>
46 #include <asm/uaccess.h>
50 /*************************************************/
52 static char version[] __devinitdata =
53 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
55 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
56 MODULE_DESCRIPTION("Freescale Ethernet Driver");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(DRV_MODULE_VERSION);
60 int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
61 module_param(fs_enet_debug, int, 0);
62 MODULE_PARM_DESC(fs_enet_debug,
63 "Freescale bitmapped debugging message enable value");
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 net_device *dev, int *budget)
76 struct fs_enet_private *fep = netdev_priv(dev);
77 const struct fs_platform_info *fpi = fep->fpi;
79 struct sk_buff *skb, *skbn, *skbt;
83 int rx_work_limit = 0; /* pacify gcc */
85 rx_work_limit = min(dev->quota, *budget);
87 if (!netif_running(dev))
91 * First, grab all of the stats for the incoming packet.
92 * These get messed up if we get called due to a busy condition.
96 /* clear RX status bits for napi*/
97 (*fep->ops->napi_clear_rx_event)(dev);
99 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
101 curidx = bdp - fep->rx_bd_base;
104 * Since we have allocated space to hold a complete frame,
105 * the last indicator should be set.
107 if ((sc & BD_ENET_RX_LAST) == 0)
108 printk(KERN_WARNING DRV_MODULE_NAME
109 ": %s rcv is not +last\n",
115 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
116 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
117 fep->stats.rx_errors++;
118 /* Frame too long or too short. */
119 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
120 fep->stats.rx_length_errors++;
121 /* Frame alignment */
122 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
123 fep->stats.rx_frame_errors++;
125 if (sc & BD_ENET_RX_CR)
126 fep->stats.rx_crc_errors++;
128 if (sc & BD_ENET_RX_OV)
129 fep->stats.rx_crc_errors++;
131 skb = fep->rx_skbuff[curidx];
133 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
134 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
141 /* napi, got packet but no quota */
142 if (--rx_work_limit < 0)
145 skb = fep->rx_skbuff[curidx];
147 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
148 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
152 * Process the incoming frame.
154 fep->stats.rx_packets++;
155 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
156 fep->stats.rx_bytes += pkt_len + 4;
158 if (pkt_len <= fpi->rx_copybreak) {
159 /* +2 to make IP header L1 cache aligned */
160 skbn = dev_alloc_skb(pkt_len + 2);
162 skb_reserve(skbn, 2); /* align IP header */
163 skb_copy_from_linear_data(skb,
164 skbn->data, pkt_len);
171 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
174 skb_put(skb, pkt_len); /* Make room */
175 skb->protocol = eth_type_trans(skb, dev);
177 netif_receive_skb(skb);
179 printk(KERN_WARNING DRV_MODULE_NAME
180 ": %s Memory squeeze, dropping packet.\n",
182 fep->stats.rx_dropped++;
187 fep->rx_skbuff[curidx] = skbn;
188 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
189 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
192 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
195 * Update BD pointer to next entry.
197 if ((sc & BD_ENET_RX_WRAP) == 0)
200 bdp = fep->rx_bd_base;
202 (*fep->ops->rx_bd_done)(dev);
207 dev->quota -= received;
210 if (rx_work_limit < 0)
211 return 1; /* not done */
214 netif_rx_complete(dev);
216 (*fep->ops->napi_enable_rx)(dev);
221 /* non NAPI receive function */
222 static int fs_enet_rx_non_napi(struct net_device *dev)
224 struct fs_enet_private *fep = netdev_priv(dev);
225 const struct fs_platform_info *fpi = fep->fpi;
227 struct sk_buff *skb, *skbn, *skbt;
232 * First, grab all of the stats for the incoming packet.
233 * These get messed up if we get called due to a busy condition.
237 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
239 curidx = bdp - fep->rx_bd_base;
242 * Since we have allocated space to hold a complete frame,
243 * the last indicator should be set.
245 if ((sc & BD_ENET_RX_LAST) == 0)
246 printk(KERN_WARNING DRV_MODULE_NAME
247 ": %s rcv is not +last\n",
253 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
254 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
255 fep->stats.rx_errors++;
256 /* Frame too long or too short. */
257 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
258 fep->stats.rx_length_errors++;
259 /* Frame alignment */
260 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
261 fep->stats.rx_frame_errors++;
263 if (sc & BD_ENET_RX_CR)
264 fep->stats.rx_crc_errors++;
266 if (sc & BD_ENET_RX_OV)
267 fep->stats.rx_crc_errors++;
269 skb = fep->rx_skbuff[curidx];
271 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
272 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
279 skb = fep->rx_skbuff[curidx];
281 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
282 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
286 * Process the incoming frame.
288 fep->stats.rx_packets++;
289 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
290 fep->stats.rx_bytes += pkt_len + 4;
292 if (pkt_len <= fpi->rx_copybreak) {
293 /* +2 to make IP header L1 cache aligned */
294 skbn = dev_alloc_skb(pkt_len + 2);
296 skb_reserve(skbn, 2); /* align IP header */
297 skb_copy_from_linear_data(skb,
298 skbn->data, pkt_len);
305 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
308 skb_put(skb, pkt_len); /* Make room */
309 skb->protocol = eth_type_trans(skb, dev);
313 printk(KERN_WARNING DRV_MODULE_NAME
314 ": %s Memory squeeze, dropping packet.\n",
316 fep->stats.rx_dropped++;
321 fep->rx_skbuff[curidx] = skbn;
322 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
323 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
326 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
329 * Update BD pointer to next entry.
331 if ((sc & BD_ENET_RX_WRAP) == 0)
334 bdp = fep->rx_bd_base;
336 (*fep->ops->rx_bd_done)(dev);
344 static void fs_enet_tx(struct net_device *dev)
346 struct fs_enet_private *fep = netdev_priv(dev);
349 int dirtyidx, do_wake, do_restart;
352 spin_lock(&fep->lock);
355 do_wake = do_restart = 0;
356 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
358 dirtyidx = bdp - fep->tx_bd_base;
360 if (fep->tx_free == fep->tx_ring)
363 skb = fep->tx_skbuff[dirtyidx];
368 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
369 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
371 if (sc & BD_ENET_TX_HB) /* No heartbeat */
372 fep->stats.tx_heartbeat_errors++;
373 if (sc & BD_ENET_TX_LC) /* Late collision */
374 fep->stats.tx_window_errors++;
375 if (sc & BD_ENET_TX_RL) /* Retrans limit */
376 fep->stats.tx_aborted_errors++;
377 if (sc & BD_ENET_TX_UN) /* Underrun */
378 fep->stats.tx_fifo_errors++;
379 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
380 fep->stats.tx_carrier_errors++;
382 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
383 fep->stats.tx_errors++;
387 fep->stats.tx_packets++;
389 if (sc & BD_ENET_TX_READY)
390 printk(KERN_WARNING DRV_MODULE_NAME
391 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
395 * Deferred means some collisions occurred during transmit,
396 * but we eventually sent the packet OK.
398 if (sc & BD_ENET_TX_DEF)
399 fep->stats.collisions++;
402 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
403 skb->len, DMA_TO_DEVICE);
406 * Free the sk buffer associated with this last transmit.
408 dev_kfree_skb_irq(skb);
409 fep->tx_skbuff[dirtyidx] = NULL;
412 * Update pointer to next buffer descriptor to be transmitted.
414 if ((sc & BD_ENET_TX_WRAP) == 0)
417 bdp = fep->tx_bd_base;
420 * Since we have freed up a buffer, the ring is no longer
430 (*fep->ops->tx_restart)(dev);
432 spin_unlock(&fep->lock);
435 netif_wake_queue(dev);
439 * The interrupt handler.
440 * This is called from the MPC core interrupt.
443 fs_enet_interrupt(int irq, void *dev_id)
445 struct net_device *dev = dev_id;
446 struct fs_enet_private *fep;
447 const struct fs_platform_info *fpi;
453 fep = netdev_priv(dev);
457 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
461 int_clr_events = int_events;
463 int_clr_events &= ~fep->ev_napi_rx;
465 (*fep->ops->clear_int_events)(dev, int_clr_events);
467 if (int_events & fep->ev_err)
468 (*fep->ops->ev_error)(dev, int_events);
470 if (int_events & fep->ev_rx) {
472 fs_enet_rx_non_napi(dev);
474 napi_ok = netif_rx_schedule_prep(dev);
476 (*fep->ops->napi_disable_rx)(dev);
477 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
479 /* NOTE: it is possible for FCCs in NAPI mode */
480 /* to submit a spurious interrupt while in poll */
482 __netif_rx_schedule(dev);
486 if (int_events & fep->ev_tx)
491 return IRQ_RETVAL(handled);
494 void fs_init_bds(struct net_device *dev)
496 struct fs_enet_private *fep = netdev_priv(dev);
503 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
504 fep->tx_free = fep->tx_ring;
505 fep->cur_rx = fep->rx_bd_base;
508 * Initialize the receive buffer descriptors.
510 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
511 skb = dev_alloc_skb(ENET_RX_FRSIZE);
513 printk(KERN_WARNING DRV_MODULE_NAME
514 ": %s Memory squeeze, unable to allocate skb\n",
518 fep->rx_skbuff[i] = skb;
520 dma_map_single(fep->dev, skb->data,
521 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
523 CBDW_DATLEN(bdp, 0); /* zero */
524 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
525 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
528 * if we failed, fillup remainder
530 for (; i < fep->rx_ring; i++, bdp++) {
531 fep->rx_skbuff[i] = NULL;
532 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
536 * ...and the same for transmit.
538 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
539 fep->tx_skbuff[i] = NULL;
540 CBDW_BUFADDR(bdp, 0);
542 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
546 void fs_cleanup_bds(struct net_device *dev)
548 struct fs_enet_private *fep = netdev_priv(dev);
554 * Reset SKB transmit buffers.
556 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
557 if ((skb = fep->tx_skbuff[i]) == NULL)
561 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
562 skb->len, DMA_TO_DEVICE);
564 fep->tx_skbuff[i] = NULL;
569 * Reset SKB receive buffers
571 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
572 if ((skb = fep->rx_skbuff[i]) == NULL)
576 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
577 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
580 fep->rx_skbuff[i] = NULL;
586 /**********************************************************************************/
588 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
590 struct fs_enet_private *fep = netdev_priv(dev);
596 spin_lock_irqsave(&fep->tx_lock, flags);
599 * Fill in a Tx ring entry
603 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
604 netif_stop_queue(dev);
605 spin_unlock_irqrestore(&fep->tx_lock, flags);
608 * Ooops. All transmit buffers are full. Bail out.
609 * This should not happen, since the tx queue should be stopped.
611 printk(KERN_WARNING DRV_MODULE_NAME
612 ": %s tx queue full!.\n", dev->name);
613 return NETDEV_TX_BUSY;
616 curidx = bdp - fep->tx_bd_base;
618 * Clear all of the status flags.
620 CBDC_SC(bdp, BD_ENET_TX_STATS);
625 fep->tx_skbuff[curidx] = skb;
627 fep->stats.tx_bytes += skb->len;
630 * Push the data cache so the CPM does not get stale memory data.
632 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
633 skb->data, skb->len, DMA_TO_DEVICE));
634 CBDW_DATLEN(bdp, skb->len);
636 dev->trans_start = jiffies;
639 * If this was the last BD in the ring, start at the beginning again.
641 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
644 fep->cur_tx = fep->tx_bd_base;
647 netif_stop_queue(dev);
649 /* Trigger transmission start */
650 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
651 BD_ENET_TX_LAST | BD_ENET_TX_TC;
653 /* note that while FEC does not have this bit
654 * it marks it as available for software use
655 * yay for hw reuse :) */
657 sc |= BD_ENET_TX_PAD;
660 (*fep->ops->tx_kickstart)(dev);
662 spin_unlock_irqrestore(&fep->tx_lock, flags);
667 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
670 struct fs_enet_private *fep = netdev_priv(dev);
672 (*fep->ops->pre_request_irq)(dev, irq);
673 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
676 static void fs_free_irq(struct net_device *dev, int irq)
678 struct fs_enet_private *fep = netdev_priv(dev);
681 (*fep->ops->post_free_irq)(dev, irq);
684 static void fs_timeout(struct net_device *dev)
686 struct fs_enet_private *fep = netdev_priv(dev);
690 fep->stats.tx_errors++;
692 spin_lock_irqsave(&fep->lock, flags);
694 if (dev->flags & IFF_UP) {
695 phy_stop(fep->phydev);
696 (*fep->ops->stop)(dev);
697 (*fep->ops->restart)(dev);
698 phy_start(fep->phydev);
701 phy_start(fep->phydev);
702 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
703 spin_unlock_irqrestore(&fep->lock, flags);
706 netif_wake_queue(dev);
709 /*-----------------------------------------------------------------------------
710 * generic link-change handler - should be sufficient for most cases
711 *-----------------------------------------------------------------------------*/
712 static void generic_adjust_link(struct net_device *dev)
714 struct fs_enet_private *fep = netdev_priv(dev);
715 struct phy_device *phydev = fep->phydev;
720 /* adjust to duplex mode */
721 if (phydev->duplex != fep->oldduplex){
723 fep->oldduplex = phydev->duplex;
726 if (phydev->speed != fep->oldspeed) {
728 fep->oldspeed = phydev->speed;
735 netif_carrier_on(dev);
736 netif_start_queue(dev);
740 fep->ops->restart(dev);
742 } else if (fep->oldlink) {
747 netif_carrier_off(dev);
748 netif_stop_queue(dev);
751 if (new_state && netif_msg_link(fep))
752 phy_print_status(phydev);
756 static void fs_adjust_link(struct net_device *dev)
758 struct fs_enet_private *fep = netdev_priv(dev);
761 spin_lock_irqsave(&fep->lock, flags);
763 if(fep->ops->adjust_link)
764 fep->ops->adjust_link(dev);
766 generic_adjust_link(dev);
768 spin_unlock_irqrestore(&fep->lock, flags);
771 static int fs_init_phy(struct net_device *dev)
773 struct fs_enet_private *fep = netdev_priv(dev);
774 struct phy_device *phydev;
780 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
781 PHY_INTERFACE_MODE_MII);
783 printk("No phy bus ID specified in BSP code\n");
786 if (IS_ERR(phydev)) {
787 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
788 return PTR_ERR(phydev);
791 fep->phydev = phydev;
797 static int fs_enet_open(struct net_device *dev)
799 struct fs_enet_private *fep = netdev_priv(dev);
803 /* Install our interrupt handler. */
804 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
806 printk(KERN_ERR DRV_MODULE_NAME
807 ": %s Could not allocate FS_ENET IRQ!", dev->name);
811 err = fs_init_phy(dev);
815 phy_start(fep->phydev);
820 static int fs_enet_close(struct net_device *dev)
822 struct fs_enet_private *fep = netdev_priv(dev);
825 netif_stop_queue(dev);
826 netif_carrier_off(dev);
827 phy_stop(fep->phydev);
829 spin_lock_irqsave(&fep->lock, flags);
830 (*fep->ops->stop)(dev);
831 spin_unlock_irqrestore(&fep->lock, flags);
833 /* release any irqs */
834 phy_disconnect(fep->phydev);
836 fs_free_irq(dev, fep->interrupt);
841 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
843 struct fs_enet_private *fep = netdev_priv(dev);
847 /*************************************************************************/
849 static void fs_get_drvinfo(struct net_device *dev,
850 struct ethtool_drvinfo *info)
852 strcpy(info->driver, DRV_MODULE_NAME);
853 strcpy(info->version, DRV_MODULE_VERSION);
856 static int fs_get_regs_len(struct net_device *dev)
858 struct fs_enet_private *fep = netdev_priv(dev);
860 return (*fep->ops->get_regs_len)(dev);
863 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
866 struct fs_enet_private *fep = netdev_priv(dev);
872 spin_lock_irqsave(&fep->lock, flags);
873 r = (*fep->ops->get_regs)(dev, p, &len);
874 spin_unlock_irqrestore(&fep->lock, flags);
880 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
882 struct fs_enet_private *fep = netdev_priv(dev);
883 return phy_ethtool_gset(fep->phydev, cmd);
886 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
888 struct fs_enet_private *fep = netdev_priv(dev);
889 phy_ethtool_sset(fep->phydev, cmd);
893 static int fs_nway_reset(struct net_device *dev)
898 static u32 fs_get_msglevel(struct net_device *dev)
900 struct fs_enet_private *fep = netdev_priv(dev);
901 return fep->msg_enable;
904 static void fs_set_msglevel(struct net_device *dev, u32 value)
906 struct fs_enet_private *fep = netdev_priv(dev);
907 fep->msg_enable = value;
910 static const struct ethtool_ops fs_ethtool_ops = {
911 .get_drvinfo = fs_get_drvinfo,
912 .get_regs_len = fs_get_regs_len,
913 .get_settings = fs_get_settings,
914 .set_settings = fs_set_settings,
915 .nway_reset = fs_nway_reset,
916 .get_link = ethtool_op_get_link,
917 .get_msglevel = fs_get_msglevel,
918 .set_msglevel = fs_set_msglevel,
919 .get_tx_csum = ethtool_op_get_tx_csum,
920 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
921 .get_sg = ethtool_op_get_sg,
922 .set_sg = ethtool_op_set_sg,
923 .get_regs = fs_get_regs,
926 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
928 struct fs_enet_private *fep = netdev_priv(dev);
929 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
933 if (!netif_running(dev))
936 spin_lock_irqsave(&fep->lock, flags);
937 rc = phy_mii_ioctl(fep->phydev, mii, cmd);
938 spin_unlock_irqrestore(&fep->lock, flags);
942 extern int fs_mii_connect(struct net_device *dev);
943 extern void fs_mii_disconnect(struct net_device *dev);
945 static struct net_device *fs_init_instance(struct device *dev,
946 struct fs_platform_info *fpi)
948 struct net_device *ndev = NULL;
949 struct fs_enet_private *fep = NULL;
950 int privsize, i, r, err = 0, registered = 0;
952 fpi->fs_no = fs_get_id(fpi);
954 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
955 return ERR_PTR(-EINVAL);
957 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
958 (fpi->rx_ring + fpi->tx_ring));
960 ndev = alloc_etherdev(privsize);
965 SET_MODULE_OWNER(ndev);
967 fep = netdev_priv(ndev);
968 memset(fep, 0, privsize); /* clear everything */
971 dev_set_drvdata(dev, ndev);
973 if (fpi->init_ioports)
974 fpi->init_ioports((struct fs_platform_info *)fpi);
976 #ifdef CONFIG_FS_ENET_HAS_FEC
977 if (fs_get_fec_index(fpi->fs_no) >= 0)
978 fep->ops = &fs_fec_ops;
981 #ifdef CONFIG_FS_ENET_HAS_SCC
982 if (fs_get_scc_index(fpi->fs_no) >=0 )
983 fep->ops = &fs_scc_ops;
986 #ifdef CONFIG_FS_ENET_HAS_FCC
987 if (fs_get_fcc_index(fpi->fs_no) >= 0)
988 fep->ops = &fs_fcc_ops;
991 if (fep->ops == NULL) {
992 printk(KERN_ERR DRV_MODULE_NAME
993 ": %s No matching ops found (%d).\n",
994 ndev->name, fpi->fs_no);
999 r = (*fep->ops->setup_data)(ndev);
1001 printk(KERN_ERR DRV_MODULE_NAME
1002 ": %s setup_data failed\n",
1008 /* point rx_skbuff, tx_skbuff */
1009 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1010 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1013 spin_lock_init(&fep->lock);
1014 spin_lock_init(&fep->tx_lock);
1017 * Set the Ethernet address.
1019 for (i = 0; i < 6; i++)
1020 ndev->dev_addr[i] = fpi->macaddr[i];
1022 r = (*fep->ops->allocate_bd)(ndev);
1024 if (fep->ring_base == NULL) {
1025 printk(KERN_ERR DRV_MODULE_NAME
1026 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1032 * Set receive and transmit descriptor base.
1034 fep->rx_bd_base = fep->ring_base;
1035 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1037 /* initialize ring size variables */
1038 fep->tx_ring = fpi->tx_ring;
1039 fep->rx_ring = fpi->rx_ring;
1042 * The FEC Ethernet specific entries in the device structure.
1044 ndev->open = fs_enet_open;
1045 ndev->hard_start_xmit = fs_enet_start_xmit;
1046 ndev->tx_timeout = fs_timeout;
1047 ndev->watchdog_timeo = 2 * HZ;
1048 ndev->stop = fs_enet_close;
1049 ndev->get_stats = fs_enet_get_stats;
1050 ndev->set_multicast_list = fs_set_multicast_list;
1051 if (fpi->use_napi) {
1052 ndev->poll = fs_enet_rx_napi;
1053 ndev->weight = fpi->napi_weight;
1055 ndev->ethtool_ops = &fs_ethtool_ops;
1056 ndev->do_ioctl = fs_ioctl;
1058 init_timer(&fep->phy_timer_list);
1060 netif_carrier_off(ndev);
1062 err = register_netdev(ndev);
1064 printk(KERN_ERR DRV_MODULE_NAME
1065 ": %s register_netdev failed.\n", ndev->name);
1077 unregister_netdev(ndev);
1080 (*fep->ops->free_bd)(ndev);
1081 (*fep->ops->cleanup_data)(ndev);
1087 dev_set_drvdata(dev, NULL);
1089 return ERR_PTR(err);
1092 static int fs_cleanup_instance(struct net_device *ndev)
1094 struct fs_enet_private *fep;
1095 const struct fs_platform_info *fpi;
1101 fep = netdev_priv(ndev);
1107 unregister_netdev(ndev);
1109 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1110 fep->ring_base, fep->ring_mem_addr);
1113 (*fep->ops->cleanup_data)(ndev);
1117 dev_set_drvdata(dev, NULL);
1126 /**************************************************************************************/
1128 /* handy pointer to the immap */
1129 void *fs_enet_immap = NULL;
1131 static int setup_immap(void)
1133 phys_addr_t paddr = 0;
1134 unsigned long size = 0;
1138 size = 0x10000; /* map 64K */
1142 paddr = CPM_MAP_ADDR;
1143 size = 0x40000; /* map 256 K */
1145 fs_enet_immap = ioremap(paddr, size);
1146 if (fs_enet_immap == NULL)
1147 return -EBADF; /* XXX ahem; maybe just BUG_ON? */
1152 static void cleanup_immap(void)
1154 if (fs_enet_immap != NULL) {
1155 iounmap(fs_enet_immap);
1156 fs_enet_immap = NULL;
1160 /**************************************************************************************/
1162 static int __devinit fs_enet_probe(struct device *dev)
1164 struct net_device *ndev;
1166 /* no fixup - no device */
1167 if (dev->platform_data == NULL) {
1168 printk(KERN_INFO "fs_enet: "
1169 "probe called with no platform data; "
1170 "remove unused devices\n");
1174 ndev = fs_init_instance(dev, dev->platform_data);
1176 return PTR_ERR(ndev);
1180 static int fs_enet_remove(struct device *dev)
1182 return fs_cleanup_instance(dev_get_drvdata(dev));
1185 static struct device_driver fs_enet_fec_driver = {
1186 .name = "fsl-cpm-fec",
1187 .bus = &platform_bus_type,
1188 .probe = fs_enet_probe,
1189 .remove = fs_enet_remove,
1191 /* .suspend = fs_enet_suspend, TODO */
1192 /* .resume = fs_enet_resume, TODO */
1196 static struct device_driver fs_enet_scc_driver = {
1197 .name = "fsl-cpm-scc",
1198 .bus = &platform_bus_type,
1199 .probe = fs_enet_probe,
1200 .remove = fs_enet_remove,
1202 /* .suspend = fs_enet_suspend, TODO */
1203 /* .resume = fs_enet_resume, TODO */
1207 static struct device_driver fs_enet_fcc_driver = {
1208 .name = "fsl-cpm-fcc",
1209 .bus = &platform_bus_type,
1210 .probe = fs_enet_probe,
1211 .remove = fs_enet_remove,
1213 /* .suspend = fs_enet_suspend, TODO */
1214 /* .resume = fs_enet_resume, TODO */
1218 static int __init fs_init(void)
1229 #ifdef CONFIG_FS_ENET_HAS_FCC
1230 /* let's insert mii stuff */
1231 r = fs_enet_mdio_bb_init();
1234 printk(KERN_ERR DRV_MODULE_NAME
1235 "BB PHY init failed.\n");
1238 r = driver_register(&fs_enet_fcc_driver);
1243 #ifdef CONFIG_FS_ENET_HAS_FEC
1244 r = fs_enet_mdio_fec_init();
1246 printk(KERN_ERR DRV_MODULE_NAME
1247 "FEC PHY init failed.\n");
1251 r = driver_register(&fs_enet_fec_driver);
1256 #ifdef CONFIG_FS_ENET_HAS_SCC
1257 r = driver_register(&fs_enet_scc_driver);
1269 static void __exit fs_cleanup(void)
1271 driver_unregister(&fs_enet_fec_driver);
1272 driver_unregister(&fs_enet_fcc_driver);
1273 driver_unregister(&fs_enet_scc_driver);
1277 /**************************************************************************************/
1279 module_init(fs_init);
1280 module_exit(fs_cleanup);