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/config.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/sched.h>
23 #include <linux/string.h>
24 #include <linux/ptrace.h>
25 #include <linux/errno.h>
26 #include <linux/ioport.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/pci.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/spinlock.h>
36 #include <linux/mii.h>
37 #include <linux/ethtool.h>
38 #include <linux/bitops.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 MODULE_PARM(fs_enet_debug, "i");
61 MODULE_PARM_DESC(fs_enet_debug,
62 "Freescale bitmapped debugging message enable value");
64 int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as 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, skb->data,
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, skb->data,
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);
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, skb->data,
272 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
279 skb = fep->rx_skbuff[curidx];
281 dma_unmap_single(fep->dev, skb->data,
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 memcpy(skbn->data, skb->data, pkt_len);
304 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, skb->data, 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, struct pt_regs *regs)
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;
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);
553 * Reset SKB transmit buffers.
555 for (i = 0; i < fep->tx_ring; i++) {
556 if ((skb = fep->tx_skbuff[i]) == NULL)
560 dma_unmap_single(fep->dev, skb->data, skb->len, DMA_TO_DEVICE);
562 fep->tx_skbuff[i] = NULL;
567 * Reset SKB receive buffers
569 for (i = 0; i < fep->rx_ring; i++) {
570 if ((skb = fep->rx_skbuff[i]) == NULL)
574 dma_unmap_single(fep->dev, skb->data,
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,
666 irqreturn_t (*irqf)(int irq, void *dev_id, struct pt_regs *regs))
668 struct fs_enet_private *fep = netdev_priv(dev);
670 (*fep->ops->pre_request_irq)(dev, irq);
671 return request_irq(irq, irqf, SA_SHIRQ, 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 /**********************************************************************************/
684 /* This interrupt occurs when the PHY detects a link change. */
686 fs_mii_link_interrupt(int irq, void *dev_id, struct pt_regs *regs)
688 struct net_device *dev = dev_id;
689 struct fs_enet_private *fep;
690 const struct fs_platform_info *fpi;
692 fep = netdev_priv(dev);
696 * Acknowledge the interrupt if possible. If we have not
697 * found the PHY yet we can't process or acknowledge the
698 * interrupt now. Instead we ignore this interrupt for now,
699 * which we can do since it is edge triggered. It will be
700 * acknowledged later by fs_enet_open().
706 fs_mii_link_status_change_check(dev, 0);
711 static void fs_timeout(struct net_device *dev)
713 struct fs_enet_private *fep = netdev_priv(dev);
717 fep->stats.tx_errors++;
719 spin_lock_irqsave(&fep->lock, flags);
721 if (dev->flags & IFF_UP) {
722 (*fep->ops->stop)(dev);
723 (*fep->ops->restart)(dev);
726 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
727 spin_unlock_irqrestore(&fep->lock, flags);
730 netif_wake_queue(dev);
733 static int fs_enet_open(struct net_device *dev)
735 struct fs_enet_private *fep = netdev_priv(dev);
736 const struct fs_platform_info *fpi = fep->fpi;
739 /* Install our interrupt handler. */
740 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
742 printk(KERN_ERR DRV_MODULE_NAME
743 ": %s Could not allocate FEC IRQ!", dev->name);
747 /* Install our phy interrupt handler */
748 if (fpi->phy_irq != -1) {
750 r = fs_request_irq(dev, fpi->phy_irq, "fs_enet-phy", fs_mii_link_interrupt);
752 printk(KERN_ERR DRV_MODULE_NAME
753 ": %s Could not allocate PHY IRQ!", dev->name);
754 fs_free_irq(dev, fep->interrupt);
760 netif_carrier_off(dev);
761 fs_mii_link_status_change_check(dev, 1);
766 static int fs_enet_close(struct net_device *dev)
768 struct fs_enet_private *fep = netdev_priv(dev);
769 const struct fs_platform_info *fpi = fep->fpi;
772 netif_stop_queue(dev);
773 netif_carrier_off(dev);
774 fs_mii_shutdown(dev);
776 spin_lock_irqsave(&fep->lock, flags);
777 (*fep->ops->stop)(dev);
778 spin_unlock_irqrestore(&fep->lock, flags);
780 /* release any irqs */
781 if (fpi->phy_irq != -1)
782 fs_free_irq(dev, fpi->phy_irq);
783 fs_free_irq(dev, fep->interrupt);
788 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
790 struct fs_enet_private *fep = netdev_priv(dev);
794 /*************************************************************************/
796 static void fs_get_drvinfo(struct net_device *dev,
797 struct ethtool_drvinfo *info)
799 strcpy(info->driver, DRV_MODULE_NAME);
800 strcpy(info->version, DRV_MODULE_VERSION);
803 static int fs_get_regs_len(struct net_device *dev)
805 struct fs_enet_private *fep = netdev_priv(dev);
807 return (*fep->ops->get_regs_len)(dev);
810 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
813 struct fs_enet_private *fep = netdev_priv(dev);
819 spin_lock_irqsave(&fep->lock, flags);
820 r = (*fep->ops->get_regs)(dev, p, &len);
821 spin_unlock_irqrestore(&fep->lock, flags);
827 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
829 struct fs_enet_private *fep = netdev_priv(dev);
833 spin_lock_irqsave(&fep->lock, flags);
834 rc = mii_ethtool_gset(&fep->mii_if, cmd);
835 spin_unlock_irqrestore(&fep->lock, flags);
840 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
842 struct fs_enet_private *fep = netdev_priv(dev);
846 spin_lock_irqsave(&fep->lock, flags);
847 rc = mii_ethtool_sset(&fep->mii_if, cmd);
848 spin_unlock_irqrestore(&fep->lock, flags);
853 static int fs_nway_reset(struct net_device *dev)
855 struct fs_enet_private *fep = netdev_priv(dev);
856 return mii_nway_restart(&fep->mii_if);
859 static u32 fs_get_msglevel(struct net_device *dev)
861 struct fs_enet_private *fep = netdev_priv(dev);
862 return fep->msg_enable;
865 static void fs_set_msglevel(struct net_device *dev, u32 value)
867 struct fs_enet_private *fep = netdev_priv(dev);
868 fep->msg_enable = value;
871 static struct ethtool_ops fs_ethtool_ops = {
872 .get_drvinfo = fs_get_drvinfo,
873 .get_regs_len = fs_get_regs_len,
874 .get_settings = fs_get_settings,
875 .set_settings = fs_set_settings,
876 .nway_reset = fs_nway_reset,
877 .get_link = ethtool_op_get_link,
878 .get_msglevel = fs_get_msglevel,
879 .set_msglevel = fs_set_msglevel,
880 .get_tx_csum = ethtool_op_get_tx_csum,
881 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
882 .get_sg = ethtool_op_get_sg,
883 .set_sg = ethtool_op_set_sg,
884 .get_regs = fs_get_regs,
887 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
889 struct fs_enet_private *fep = netdev_priv(dev);
890 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
894 if (!netif_running(dev))
897 spin_lock_irqsave(&fep->lock, flags);
898 rc = generic_mii_ioctl(&fep->mii_if, mii, cmd, NULL);
899 spin_unlock_irqrestore(&fep->lock, flags);
903 extern int fs_mii_connect(struct net_device *dev);
904 extern void fs_mii_disconnect(struct net_device *dev);
906 static struct net_device *fs_init_instance(struct device *dev,
907 const struct fs_platform_info *fpi)
909 struct net_device *ndev = NULL;
910 struct fs_enet_private *fep = NULL;
911 int privsize, i, r, err = 0, registered = 0;
914 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
915 return ERR_PTR(-EINVAL);
917 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
918 (fpi->rx_ring + fpi->tx_ring));
920 ndev = alloc_etherdev(privsize);
925 SET_MODULE_OWNER(ndev);
927 fep = netdev_priv(ndev);
928 memset(fep, 0, privsize); /* clear everything */
931 dev_set_drvdata(dev, ndev);
933 if (fpi->init_ioports)
936 #ifdef CONFIG_FS_ENET_HAS_FEC
937 if (fs_get_fec_index(fpi->fs_no) >= 0)
938 fep->ops = &fs_fec_ops;
941 #ifdef CONFIG_FS_ENET_HAS_SCC
942 if (fs_get_scc_index(fpi->fs_no) >=0 )
943 fep->ops = &fs_scc_ops;
946 #ifdef CONFIG_FS_ENET_HAS_FCC
947 if (fs_get_fcc_index(fpi->fs_no) >= 0)
948 fep->ops = &fs_fcc_ops;
951 if (fep->ops == NULL) {
952 printk(KERN_ERR DRV_MODULE_NAME
953 ": %s No matching ops found (%d).\n",
954 ndev->name, fpi->fs_no);
959 r = (*fep->ops->setup_data)(ndev);
961 printk(KERN_ERR DRV_MODULE_NAME
962 ": %s setup_data failed\n",
968 /* point rx_skbuff, tx_skbuff */
969 fep->rx_skbuff = (struct sk_buff **)&fep[1];
970 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
973 spin_lock_init(&fep->lock);
974 spin_lock_init(&fep->tx_lock);
977 * Set the Ethernet address.
979 for (i = 0; i < 6; i++)
980 ndev->dev_addr[i] = fpi->macaddr[i];
982 r = (*fep->ops->allocate_bd)(ndev);
984 if (fep->ring_base == NULL) {
985 printk(KERN_ERR DRV_MODULE_NAME
986 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
992 * Set receive and transmit descriptor base.
994 fep->rx_bd_base = fep->ring_base;
995 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
997 /* initialize ring size variables */
998 fep->tx_ring = fpi->tx_ring;
999 fep->rx_ring = fpi->rx_ring;
1002 * The FEC Ethernet specific entries in the device structure.
1004 ndev->open = fs_enet_open;
1005 ndev->hard_start_xmit = fs_enet_start_xmit;
1006 ndev->tx_timeout = fs_timeout;
1007 ndev->watchdog_timeo = 2 * HZ;
1008 ndev->stop = fs_enet_close;
1009 ndev->get_stats = fs_enet_get_stats;
1010 ndev->set_multicast_list = fs_set_multicast_list;
1011 if (fpi->use_napi) {
1012 ndev->poll = fs_enet_rx_napi;
1013 ndev->weight = fpi->napi_weight;
1015 ndev->ethtool_ops = &fs_ethtool_ops;
1016 ndev->do_ioctl = fs_ioctl;
1018 init_timer(&fep->phy_timer_list);
1020 netif_carrier_off(ndev);
1022 err = register_netdev(ndev);
1024 printk(KERN_ERR DRV_MODULE_NAME
1025 ": %s register_netdev failed.\n", ndev->name);
1030 err = fs_mii_connect(ndev);
1032 printk(KERN_ERR DRV_MODULE_NAME
1033 ": %s fs_mii_connect failed.\n", ndev->name);
1043 unregister_netdev(ndev);
1046 (*fep->ops->free_bd)(ndev);
1047 (*fep->ops->cleanup_data)(ndev);
1053 dev_set_drvdata(dev, NULL);
1055 return ERR_PTR(err);
1058 static int fs_cleanup_instance(struct net_device *ndev)
1060 struct fs_enet_private *fep;
1061 const struct fs_platform_info *fpi;
1067 fep = netdev_priv(ndev);
1073 fs_mii_disconnect(ndev);
1075 unregister_netdev(ndev);
1077 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1078 fep->ring_base, fep->ring_mem_addr);
1081 (*fep->ops->cleanup_data)(ndev);
1085 dev_set_drvdata(dev, NULL);
1094 /**************************************************************************************/
1096 /* handy pointer to the immap */
1097 void *fs_enet_immap = NULL;
1099 static int setup_immap(void)
1101 phys_addr_t paddr = 0;
1102 unsigned long size = 0;
1106 size = 0x10000; /* map 64K */
1110 paddr = CPM_MAP_ADDR;
1111 size = 0x40000; /* map 256 K */
1113 fs_enet_immap = ioremap(paddr, size);
1114 if (fs_enet_immap == NULL)
1115 return -EBADF; /* XXX ahem; maybe just BUG_ON? */
1120 static void cleanup_immap(void)
1122 if (fs_enet_immap != NULL) {
1123 iounmap(fs_enet_immap);
1124 fs_enet_immap = NULL;
1128 /**************************************************************************************/
1130 static int __devinit fs_enet_probe(struct device *dev)
1132 struct net_device *ndev;
1134 /* no fixup - no device */
1135 if (dev->platform_data == NULL) {
1136 printk(KERN_INFO "fs_enet: "
1137 "probe called with no platform data; "
1138 "remove unused devices\n");
1142 ndev = fs_init_instance(dev, dev->platform_data);
1144 return PTR_ERR(ndev);
1148 static int fs_enet_remove(struct device *dev)
1150 return fs_cleanup_instance(dev_get_drvdata(dev));
1153 static struct device_driver fs_enet_fec_driver = {
1154 .name = "fsl-cpm-fec",
1155 .bus = &platform_bus_type,
1156 .probe = fs_enet_probe,
1157 .remove = fs_enet_remove,
1159 /* .suspend = fs_enet_suspend, TODO */
1160 /* .resume = fs_enet_resume, TODO */
1164 static struct device_driver fs_enet_scc_driver = {
1165 .name = "fsl-cpm-scc",
1166 .bus = &platform_bus_type,
1167 .probe = fs_enet_probe,
1168 .remove = fs_enet_remove,
1170 /* .suspend = fs_enet_suspend, TODO */
1171 /* .resume = fs_enet_resume, TODO */
1175 static struct device_driver fs_enet_fcc_driver = {
1176 .name = "fsl-cpm-fcc",
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 int __init fs_init(void)
1196 r = driver_register(&fs_enet_fec_driver);
1200 r = driver_register(&fs_enet_fcc_driver);
1204 r = driver_register(&fs_enet_scc_driver);
1215 static void __exit fs_cleanup(void)
1217 driver_unregister(&fs_enet_fec_driver);
1218 driver_unregister(&fs_enet_fcc_driver);
1219 driver_unregister(&fs_enet_scc_driver);
1223 /**************************************************************************************/
1225 module_init(fs_init);
1226 module_exit(fs_cleanup);