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 memcpy(skbn->data, skb->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 memcpy(skbn->data, skb->data, pkt_len);
303 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
306 skb_put(skb, pkt_len); /* Make room */
307 skb->protocol = eth_type_trans(skb, dev);
311 printk(KERN_WARNING DRV_MODULE_NAME
312 ": %s Memory squeeze, dropping packet.\n",
314 fep->stats.rx_dropped++;
319 fep->rx_skbuff[curidx] = skbn;
320 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
321 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
324 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
327 * Update BD pointer to next entry.
329 if ((sc & BD_ENET_RX_WRAP) == 0)
332 bdp = fep->rx_bd_base;
334 (*fep->ops->rx_bd_done)(dev);
342 static void fs_enet_tx(struct net_device *dev)
344 struct fs_enet_private *fep = netdev_priv(dev);
347 int dirtyidx, do_wake, do_restart;
350 spin_lock(&fep->lock);
353 do_wake = do_restart = 0;
354 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
356 dirtyidx = bdp - fep->tx_bd_base;
358 if (fep->tx_free == fep->tx_ring)
361 skb = fep->tx_skbuff[dirtyidx];
366 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
367 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
369 if (sc & BD_ENET_TX_HB) /* No heartbeat */
370 fep->stats.tx_heartbeat_errors++;
371 if (sc & BD_ENET_TX_LC) /* Late collision */
372 fep->stats.tx_window_errors++;
373 if (sc & BD_ENET_TX_RL) /* Retrans limit */
374 fep->stats.tx_aborted_errors++;
375 if (sc & BD_ENET_TX_UN) /* Underrun */
376 fep->stats.tx_fifo_errors++;
377 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
378 fep->stats.tx_carrier_errors++;
380 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
381 fep->stats.tx_errors++;
385 fep->stats.tx_packets++;
387 if (sc & BD_ENET_TX_READY)
388 printk(KERN_WARNING DRV_MODULE_NAME
389 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
393 * Deferred means some collisions occurred during transmit,
394 * but we eventually sent the packet OK.
396 if (sc & BD_ENET_TX_DEF)
397 fep->stats.collisions++;
400 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
401 skb->len, DMA_TO_DEVICE);
404 * Free the sk buffer associated with this last transmit.
406 dev_kfree_skb_irq(skb);
407 fep->tx_skbuff[dirtyidx] = NULL;
410 * Update pointer to next buffer descriptor to be transmitted.
412 if ((sc & BD_ENET_TX_WRAP) == 0)
415 bdp = fep->tx_bd_base;
418 * Since we have freed up a buffer, the ring is no longer
428 (*fep->ops->tx_restart)(dev);
430 spin_unlock(&fep->lock);
433 netif_wake_queue(dev);
437 * The interrupt handler.
438 * This is called from the MPC core interrupt.
441 fs_enet_interrupt(int irq, void *dev_id)
443 struct net_device *dev = dev_id;
444 struct fs_enet_private *fep;
445 const struct fs_platform_info *fpi;
451 fep = netdev_priv(dev);
455 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
459 int_clr_events = int_events;
461 int_clr_events &= ~fep->ev_napi_rx;
463 (*fep->ops->clear_int_events)(dev, int_clr_events);
465 if (int_events & fep->ev_err)
466 (*fep->ops->ev_error)(dev, int_events);
468 if (int_events & fep->ev_rx) {
470 fs_enet_rx_non_napi(dev);
472 napi_ok = netif_rx_schedule_prep(dev);
474 (*fep->ops->napi_disable_rx)(dev);
475 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
477 /* NOTE: it is possible for FCCs in NAPI mode */
478 /* to submit a spurious interrupt while in poll */
480 __netif_rx_schedule(dev);
484 if (int_events & fep->ev_tx)
489 return IRQ_RETVAL(handled);
492 void fs_init_bds(struct net_device *dev)
494 struct fs_enet_private *fep = netdev_priv(dev);
501 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
502 fep->tx_free = fep->tx_ring;
503 fep->cur_rx = fep->rx_bd_base;
506 * Initialize the receive buffer descriptors.
508 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
509 skb = dev_alloc_skb(ENET_RX_FRSIZE);
511 printk(KERN_WARNING DRV_MODULE_NAME
512 ": %s Memory squeeze, unable to allocate skb\n",
516 fep->rx_skbuff[i] = skb;
518 dma_map_single(fep->dev, skb->data,
519 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
521 CBDW_DATLEN(bdp, 0); /* zero */
522 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
523 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
526 * if we failed, fillup remainder
528 for (; i < fep->rx_ring; i++, bdp++) {
529 fep->rx_skbuff[i] = NULL;
530 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
534 * ...and the same for transmit.
536 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
537 fep->tx_skbuff[i] = NULL;
538 CBDW_BUFADDR(bdp, 0);
540 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
544 void fs_cleanup_bds(struct net_device *dev)
546 struct fs_enet_private *fep = netdev_priv(dev);
552 * Reset SKB transmit buffers.
554 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
555 if ((skb = fep->tx_skbuff[i]) == NULL)
559 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
560 skb->len, DMA_TO_DEVICE);
562 fep->tx_skbuff[i] = NULL;
567 * Reset SKB receive buffers
569 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
570 if ((skb = fep->rx_skbuff[i]) == NULL)
574 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
575 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
578 fep->rx_skbuff[i] = NULL;
584 /**********************************************************************************/
586 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
588 struct fs_enet_private *fep = netdev_priv(dev);
594 spin_lock_irqsave(&fep->tx_lock, flags);
597 * Fill in a Tx ring entry
601 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
602 netif_stop_queue(dev);
603 spin_unlock_irqrestore(&fep->tx_lock, flags);
606 * Ooops. All transmit buffers are full. Bail out.
607 * This should not happen, since the tx queue should be stopped.
609 printk(KERN_WARNING DRV_MODULE_NAME
610 ": %s tx queue full!.\n", dev->name);
611 return NETDEV_TX_BUSY;
614 curidx = bdp - fep->tx_bd_base;
616 * Clear all of the status flags.
618 CBDC_SC(bdp, BD_ENET_TX_STATS);
623 fep->tx_skbuff[curidx] = skb;
625 fep->stats.tx_bytes += skb->len;
628 * Push the data cache so the CPM does not get stale memory data.
630 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
631 skb->data, skb->len, DMA_TO_DEVICE));
632 CBDW_DATLEN(bdp, skb->len);
634 dev->trans_start = jiffies;
637 * If this was the last BD in the ring, start at the beginning again.
639 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
642 fep->cur_tx = fep->tx_bd_base;
645 netif_stop_queue(dev);
647 /* Trigger transmission start */
648 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
649 BD_ENET_TX_LAST | BD_ENET_TX_TC;
651 /* note that while FEC does not have this bit
652 * it marks it as available for software use
653 * yay for hw reuse :) */
655 sc |= BD_ENET_TX_PAD;
658 (*fep->ops->tx_kickstart)(dev);
660 spin_unlock_irqrestore(&fep->tx_lock, flags);
665 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
668 struct fs_enet_private *fep = netdev_priv(dev);
670 (*fep->ops->pre_request_irq)(dev, irq);
671 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
674 static void fs_free_irq(struct net_device *dev, int irq)
676 struct fs_enet_private *fep = netdev_priv(dev);
679 (*fep->ops->post_free_irq)(dev, irq);
682 static void fs_timeout(struct net_device *dev)
684 struct fs_enet_private *fep = netdev_priv(dev);
688 fep->stats.tx_errors++;
690 spin_lock_irqsave(&fep->lock, flags);
692 if (dev->flags & IFF_UP) {
693 phy_stop(fep->phydev);
694 (*fep->ops->stop)(dev);
695 (*fep->ops->restart)(dev);
696 phy_start(fep->phydev);
699 phy_start(fep->phydev);
700 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
701 spin_unlock_irqrestore(&fep->lock, flags);
704 netif_wake_queue(dev);
707 /*-----------------------------------------------------------------------------
708 * generic link-change handler - should be sufficient for most cases
709 *-----------------------------------------------------------------------------*/
710 static void generic_adjust_link(struct net_device *dev)
712 struct fs_enet_private *fep = netdev_priv(dev);
713 struct phy_device *phydev = fep->phydev;
718 /* adjust to duplex mode */
719 if (phydev->duplex != fep->oldduplex){
721 fep->oldduplex = phydev->duplex;
724 if (phydev->speed != fep->oldspeed) {
726 fep->oldspeed = phydev->speed;
733 netif_carrier_on(dev);
734 netif_start_queue(dev);
738 fep->ops->restart(dev);
740 } else if (fep->oldlink) {
745 netif_carrier_off(dev);
746 netif_stop_queue(dev);
749 if (new_state && netif_msg_link(fep))
750 phy_print_status(phydev);
754 static void fs_adjust_link(struct net_device *dev)
756 struct fs_enet_private *fep = netdev_priv(dev);
759 spin_lock_irqsave(&fep->lock, flags);
761 if(fep->ops->adjust_link)
762 fep->ops->adjust_link(dev);
764 generic_adjust_link(dev);
766 spin_unlock_irqrestore(&fep->lock, flags);
769 static int fs_init_phy(struct net_device *dev)
771 struct fs_enet_private *fep = netdev_priv(dev);
772 struct phy_device *phydev;
778 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
779 PHY_INTERFACE_MODE_MII);
781 printk("No phy bus ID specified in BSP code\n");
784 if (IS_ERR(phydev)) {
785 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
786 return PTR_ERR(phydev);
789 fep->phydev = phydev;
795 static int fs_enet_open(struct net_device *dev)
797 struct fs_enet_private *fep = netdev_priv(dev);
801 /* Install our interrupt handler. */
802 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
804 printk(KERN_ERR DRV_MODULE_NAME
805 ": %s Could not allocate FS_ENET IRQ!", dev->name);
809 err = fs_init_phy(dev);
813 phy_start(fep->phydev);
818 static int fs_enet_close(struct net_device *dev)
820 struct fs_enet_private *fep = netdev_priv(dev);
823 netif_stop_queue(dev);
824 netif_carrier_off(dev);
825 phy_stop(fep->phydev);
827 spin_lock_irqsave(&fep->lock, flags);
828 (*fep->ops->stop)(dev);
829 spin_unlock_irqrestore(&fep->lock, flags);
831 /* release any irqs */
832 phy_disconnect(fep->phydev);
834 fs_free_irq(dev, fep->interrupt);
839 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
841 struct fs_enet_private *fep = netdev_priv(dev);
845 /*************************************************************************/
847 static void fs_get_drvinfo(struct net_device *dev,
848 struct ethtool_drvinfo *info)
850 strcpy(info->driver, DRV_MODULE_NAME);
851 strcpy(info->version, DRV_MODULE_VERSION);
854 static int fs_get_regs_len(struct net_device *dev)
856 struct fs_enet_private *fep = netdev_priv(dev);
858 return (*fep->ops->get_regs_len)(dev);
861 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
864 struct fs_enet_private *fep = netdev_priv(dev);
870 spin_lock_irqsave(&fep->lock, flags);
871 r = (*fep->ops->get_regs)(dev, p, &len);
872 spin_unlock_irqrestore(&fep->lock, flags);
878 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
880 struct fs_enet_private *fep = netdev_priv(dev);
881 return phy_ethtool_gset(fep->phydev, cmd);
884 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
886 struct fs_enet_private *fep = netdev_priv(dev);
887 phy_ethtool_sset(fep->phydev, cmd);
891 static int fs_nway_reset(struct net_device *dev)
896 static u32 fs_get_msglevel(struct net_device *dev)
898 struct fs_enet_private *fep = netdev_priv(dev);
899 return fep->msg_enable;
902 static void fs_set_msglevel(struct net_device *dev, u32 value)
904 struct fs_enet_private *fep = netdev_priv(dev);
905 fep->msg_enable = value;
908 static const struct ethtool_ops fs_ethtool_ops = {
909 .get_drvinfo = fs_get_drvinfo,
910 .get_regs_len = fs_get_regs_len,
911 .get_settings = fs_get_settings,
912 .set_settings = fs_set_settings,
913 .nway_reset = fs_nway_reset,
914 .get_link = ethtool_op_get_link,
915 .get_msglevel = fs_get_msglevel,
916 .set_msglevel = fs_set_msglevel,
917 .get_tx_csum = ethtool_op_get_tx_csum,
918 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
919 .get_sg = ethtool_op_get_sg,
920 .set_sg = ethtool_op_set_sg,
921 .get_regs = fs_get_regs,
924 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
926 struct fs_enet_private *fep = netdev_priv(dev);
927 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
931 if (!netif_running(dev))
934 spin_lock_irqsave(&fep->lock, flags);
935 rc = phy_mii_ioctl(fep->phydev, mii, cmd);
936 spin_unlock_irqrestore(&fep->lock, flags);
940 extern int fs_mii_connect(struct net_device *dev);
941 extern void fs_mii_disconnect(struct net_device *dev);
943 static struct net_device *fs_init_instance(struct device *dev,
944 struct fs_platform_info *fpi)
946 struct net_device *ndev = NULL;
947 struct fs_enet_private *fep = NULL;
948 int privsize, i, r, err = 0, registered = 0;
950 fpi->fs_no = fs_get_id(fpi);
952 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
953 return ERR_PTR(-EINVAL);
955 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
956 (fpi->rx_ring + fpi->tx_ring));
958 ndev = alloc_etherdev(privsize);
963 SET_MODULE_OWNER(ndev);
965 fep = netdev_priv(ndev);
966 memset(fep, 0, privsize); /* clear everything */
969 dev_set_drvdata(dev, ndev);
971 if (fpi->init_ioports)
972 fpi->init_ioports((struct fs_platform_info *)fpi);
974 #ifdef CONFIG_FS_ENET_HAS_FEC
975 if (fs_get_fec_index(fpi->fs_no) >= 0)
976 fep->ops = &fs_fec_ops;
979 #ifdef CONFIG_FS_ENET_HAS_SCC
980 if (fs_get_scc_index(fpi->fs_no) >=0 )
981 fep->ops = &fs_scc_ops;
984 #ifdef CONFIG_FS_ENET_HAS_FCC
985 if (fs_get_fcc_index(fpi->fs_no) >= 0)
986 fep->ops = &fs_fcc_ops;
989 if (fep->ops == NULL) {
990 printk(KERN_ERR DRV_MODULE_NAME
991 ": %s No matching ops found (%d).\n",
992 ndev->name, fpi->fs_no);
997 r = (*fep->ops->setup_data)(ndev);
999 printk(KERN_ERR DRV_MODULE_NAME
1000 ": %s setup_data failed\n",
1006 /* point rx_skbuff, tx_skbuff */
1007 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1008 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1011 spin_lock_init(&fep->lock);
1012 spin_lock_init(&fep->tx_lock);
1015 * Set the Ethernet address.
1017 for (i = 0; i < 6; i++)
1018 ndev->dev_addr[i] = fpi->macaddr[i];
1020 r = (*fep->ops->allocate_bd)(ndev);
1022 if (fep->ring_base == NULL) {
1023 printk(KERN_ERR DRV_MODULE_NAME
1024 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1030 * Set receive and transmit descriptor base.
1032 fep->rx_bd_base = fep->ring_base;
1033 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1035 /* initialize ring size variables */
1036 fep->tx_ring = fpi->tx_ring;
1037 fep->rx_ring = fpi->rx_ring;
1040 * The FEC Ethernet specific entries in the device structure.
1042 ndev->open = fs_enet_open;
1043 ndev->hard_start_xmit = fs_enet_start_xmit;
1044 ndev->tx_timeout = fs_timeout;
1045 ndev->watchdog_timeo = 2 * HZ;
1046 ndev->stop = fs_enet_close;
1047 ndev->get_stats = fs_enet_get_stats;
1048 ndev->set_multicast_list = fs_set_multicast_list;
1049 if (fpi->use_napi) {
1050 ndev->poll = fs_enet_rx_napi;
1051 ndev->weight = fpi->napi_weight;
1053 ndev->ethtool_ops = &fs_ethtool_ops;
1054 ndev->do_ioctl = fs_ioctl;
1056 init_timer(&fep->phy_timer_list);
1058 netif_carrier_off(ndev);
1060 err = register_netdev(ndev);
1062 printk(KERN_ERR DRV_MODULE_NAME
1063 ": %s register_netdev failed.\n", ndev->name);
1075 unregister_netdev(ndev);
1078 (*fep->ops->free_bd)(ndev);
1079 (*fep->ops->cleanup_data)(ndev);
1085 dev_set_drvdata(dev, NULL);
1087 return ERR_PTR(err);
1090 static int fs_cleanup_instance(struct net_device *ndev)
1092 struct fs_enet_private *fep;
1093 const struct fs_platform_info *fpi;
1099 fep = netdev_priv(ndev);
1105 unregister_netdev(ndev);
1107 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1108 fep->ring_base, fep->ring_mem_addr);
1111 (*fep->ops->cleanup_data)(ndev);
1115 dev_set_drvdata(dev, NULL);
1124 /**************************************************************************************/
1126 /* handy pointer to the immap */
1127 void *fs_enet_immap = NULL;
1129 static int setup_immap(void)
1131 phys_addr_t paddr = 0;
1132 unsigned long size = 0;
1136 size = 0x10000; /* map 64K */
1140 paddr = CPM_MAP_ADDR;
1141 size = 0x40000; /* map 256 K */
1143 fs_enet_immap = ioremap(paddr, size);
1144 if (fs_enet_immap == NULL)
1145 return -EBADF; /* XXX ahem; maybe just BUG_ON? */
1150 static void cleanup_immap(void)
1152 if (fs_enet_immap != NULL) {
1153 iounmap(fs_enet_immap);
1154 fs_enet_immap = NULL;
1158 /**************************************************************************************/
1160 static int __devinit fs_enet_probe(struct device *dev)
1162 struct net_device *ndev;
1164 /* no fixup - no device */
1165 if (dev->platform_data == NULL) {
1166 printk(KERN_INFO "fs_enet: "
1167 "probe called with no platform data; "
1168 "remove unused devices\n");
1172 ndev = fs_init_instance(dev, dev->platform_data);
1174 return PTR_ERR(ndev);
1178 static int fs_enet_remove(struct device *dev)
1180 return fs_cleanup_instance(dev_get_drvdata(dev));
1183 static struct device_driver fs_enet_fec_driver = {
1184 .name = "fsl-cpm-fec",
1185 .bus = &platform_bus_type,
1186 .probe = fs_enet_probe,
1187 .remove = fs_enet_remove,
1189 /* .suspend = fs_enet_suspend, TODO */
1190 /* .resume = fs_enet_resume, TODO */
1194 static struct device_driver fs_enet_scc_driver = {
1195 .name = "fsl-cpm-scc",
1196 .bus = &platform_bus_type,
1197 .probe = fs_enet_probe,
1198 .remove = fs_enet_remove,
1200 /* .suspend = fs_enet_suspend, TODO */
1201 /* .resume = fs_enet_resume, TODO */
1205 static struct device_driver fs_enet_fcc_driver = {
1206 .name = "fsl-cpm-fcc",
1207 .bus = &platform_bus_type,
1208 .probe = fs_enet_probe,
1209 .remove = fs_enet_remove,
1211 /* .suspend = fs_enet_suspend, TODO */
1212 /* .resume = fs_enet_resume, TODO */
1216 static int __init fs_init(void)
1227 #ifdef CONFIG_FS_ENET_HAS_FCC
1228 /* let's insert mii stuff */
1229 r = fs_enet_mdio_bb_init();
1232 printk(KERN_ERR DRV_MODULE_NAME
1233 "BB PHY init failed.\n");
1236 r = driver_register(&fs_enet_fcc_driver);
1241 #ifdef CONFIG_FS_ENET_HAS_FEC
1242 r = fs_enet_mdio_fec_init();
1244 printk(KERN_ERR DRV_MODULE_NAME
1245 "FEC PHY init failed.\n");
1249 r = driver_register(&fs_enet_fec_driver);
1254 #ifdef CONFIG_FS_ENET_HAS_SCC
1255 r = driver_register(&fs_enet_scc_driver);
1267 static void __exit fs_cleanup(void)
1269 driver_unregister(&fs_enet_fec_driver);
1270 driver_unregister(&fs_enet_fcc_driver);
1271 driver_unregister(&fs_enet_scc_driver);
1275 /**************************************************************************************/
1277 module_init(fs_init);
1278 module_exit(fs_cleanup);