2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/ptrace.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/bitops.h>
37 #include <linux/platform_device.h>
38 #include <linux/phy.h>
40 #include <linux/vmalloc.h>
41 #include <asm/pgtable.h>
43 #include <asm/uaccess.h>
45 #ifdef CONFIG_PPC_CPM_NEW_BINDING
46 #include <linux/of_gpio.h>
47 #include <asm/of_platform.h>
52 /*************************************************/
54 #ifndef CONFIG_PPC_CPM_NEW_BINDING
55 static char version[] __devinitdata =
56 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
59 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
60 MODULE_DESCRIPTION("Freescale Ethernet Driver");
61 MODULE_LICENSE("GPL");
62 MODULE_VERSION(DRV_MODULE_VERSION);
64 static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
65 module_param(fs_enet_debug, int, 0);
66 MODULE_PARM_DESC(fs_enet_debug,
67 "Freescale bitmapped debugging message enable value");
69 #ifdef CONFIG_NET_POLL_CONTROLLER
70 static void fs_enet_netpoll(struct net_device *dev);
73 static void fs_set_multicast_list(struct net_device *dev)
75 struct fs_enet_private *fep = netdev_priv(dev);
77 (*fep->ops->set_multicast_list)(dev);
80 static void skb_align(struct sk_buff *skb, int align)
82 int off = ((unsigned long)skb->data) & (align - 1);
85 skb_reserve(skb, align - off);
88 /* NAPI receive function */
89 static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
91 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
92 struct net_device *dev = fep->ndev;
93 const struct fs_platform_info *fpi = fep->fpi;
95 struct sk_buff *skb, *skbn, *skbt;
101 * First, grab all of the stats for the incoming packet.
102 * These get messed up if we get called due to a busy condition.
106 /* clear RX status bits for napi*/
107 (*fep->ops->napi_clear_rx_event)(dev);
109 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
110 curidx = bdp - fep->rx_bd_base;
113 * Since we have allocated space to hold a complete frame,
114 * the last indicator should be set.
116 if ((sc & BD_ENET_RX_LAST) == 0)
117 printk(KERN_WARNING DRV_MODULE_NAME
118 ": %s rcv is not +last\n",
124 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
125 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
126 fep->stats.rx_errors++;
127 /* Frame too long or too short. */
128 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
129 fep->stats.rx_length_errors++;
130 /* Frame alignment */
131 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
132 fep->stats.rx_frame_errors++;
134 if (sc & BD_ENET_RX_CR)
135 fep->stats.rx_crc_errors++;
137 if (sc & BD_ENET_RX_OV)
138 fep->stats.rx_crc_errors++;
140 skb = fep->rx_skbuff[curidx];
142 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
143 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
149 skb = fep->rx_skbuff[curidx];
151 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
152 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
156 * Process the incoming frame.
158 fep->stats.rx_packets++;
159 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
160 fep->stats.rx_bytes += pkt_len + 4;
162 if (pkt_len <= fpi->rx_copybreak) {
163 /* +2 to make IP header L1 cache aligned */
164 skbn = dev_alloc_skb(pkt_len + 2);
166 skb_reserve(skbn, 2); /* align IP header */
167 skb_copy_from_linear_data(skb,
168 skbn->data, pkt_len);
175 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
178 skb_align(skbn, ENET_RX_ALIGN);
182 skb_put(skb, pkt_len); /* Make room */
183 skb->protocol = eth_type_trans(skb, dev);
185 netif_receive_skb(skb);
187 printk(KERN_WARNING DRV_MODULE_NAME
188 ": %s Memory squeeze, dropping packet.\n",
190 fep->stats.rx_dropped++;
195 fep->rx_skbuff[curidx] = skbn;
196 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
197 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
200 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
203 * Update BD pointer to next entry.
205 if ((sc & BD_ENET_RX_WRAP) == 0)
208 bdp = fep->rx_bd_base;
210 (*fep->ops->rx_bd_done)(dev);
212 if (received >= budget)
218 if (received < budget) {
220 netif_rx_complete(dev, napi);
221 (*fep->ops->napi_enable_rx)(dev);
226 /* non NAPI receive function */
227 static int fs_enet_rx_non_napi(struct net_device *dev)
229 struct fs_enet_private *fep = netdev_priv(dev);
230 const struct fs_platform_info *fpi = fep->fpi;
232 struct sk_buff *skb, *skbn, *skbt;
237 * First, grab all of the stats for the incoming packet.
238 * These get messed up if we get called due to a busy condition.
242 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
244 curidx = bdp - fep->rx_bd_base;
247 * Since we have allocated space to hold a complete frame,
248 * the last indicator should be set.
250 if ((sc & BD_ENET_RX_LAST) == 0)
251 printk(KERN_WARNING DRV_MODULE_NAME
252 ": %s rcv is not +last\n",
258 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
259 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
260 fep->stats.rx_errors++;
261 /* Frame too long or too short. */
262 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
263 fep->stats.rx_length_errors++;
264 /* Frame alignment */
265 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
266 fep->stats.rx_frame_errors++;
268 if (sc & BD_ENET_RX_CR)
269 fep->stats.rx_crc_errors++;
271 if (sc & BD_ENET_RX_OV)
272 fep->stats.rx_crc_errors++;
274 skb = fep->rx_skbuff[curidx];
276 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
277 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
284 skb = fep->rx_skbuff[curidx];
286 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
287 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
291 * Process the incoming frame.
293 fep->stats.rx_packets++;
294 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
295 fep->stats.rx_bytes += pkt_len + 4;
297 if (pkt_len <= fpi->rx_copybreak) {
298 /* +2 to make IP header L1 cache aligned */
299 skbn = dev_alloc_skb(pkt_len + 2);
301 skb_reserve(skbn, 2); /* align IP header */
302 skb_copy_from_linear_data(skb,
303 skbn->data, pkt_len);
310 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
313 skb_align(skbn, ENET_RX_ALIGN);
317 skb_put(skb, pkt_len); /* Make room */
318 skb->protocol = eth_type_trans(skb, dev);
322 printk(KERN_WARNING DRV_MODULE_NAME
323 ": %s Memory squeeze, dropping packet.\n",
325 fep->stats.rx_dropped++;
330 fep->rx_skbuff[curidx] = skbn;
331 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
332 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
335 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
338 * Update BD pointer to next entry.
340 if ((sc & BD_ENET_RX_WRAP) == 0)
343 bdp = fep->rx_bd_base;
345 (*fep->ops->rx_bd_done)(dev);
353 static void fs_enet_tx(struct net_device *dev)
355 struct fs_enet_private *fep = netdev_priv(dev);
358 int dirtyidx, do_wake, do_restart;
361 spin_lock(&fep->tx_lock);
364 do_wake = do_restart = 0;
365 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
366 dirtyidx = bdp - fep->tx_bd_base;
368 if (fep->tx_free == fep->tx_ring)
371 skb = fep->tx_skbuff[dirtyidx];
376 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
377 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
379 if (sc & BD_ENET_TX_HB) /* No heartbeat */
380 fep->stats.tx_heartbeat_errors++;
381 if (sc & BD_ENET_TX_LC) /* Late collision */
382 fep->stats.tx_window_errors++;
383 if (sc & BD_ENET_TX_RL) /* Retrans limit */
384 fep->stats.tx_aborted_errors++;
385 if (sc & BD_ENET_TX_UN) /* Underrun */
386 fep->stats.tx_fifo_errors++;
387 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
388 fep->stats.tx_carrier_errors++;
390 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
391 fep->stats.tx_errors++;
395 fep->stats.tx_packets++;
397 if (sc & BD_ENET_TX_READY)
398 printk(KERN_WARNING DRV_MODULE_NAME
399 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
403 * Deferred means some collisions occurred during transmit,
404 * but we eventually sent the packet OK.
406 if (sc & BD_ENET_TX_DEF)
407 fep->stats.collisions++;
410 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
411 skb->len, DMA_TO_DEVICE);
414 * Free the sk buffer associated with this last transmit.
416 dev_kfree_skb_irq(skb);
417 fep->tx_skbuff[dirtyidx] = NULL;
420 * Update pointer to next buffer descriptor to be transmitted.
422 if ((sc & BD_ENET_TX_WRAP) == 0)
425 bdp = fep->tx_bd_base;
428 * Since we have freed up a buffer, the ring is no longer
438 (*fep->ops->tx_restart)(dev);
440 spin_unlock(&fep->tx_lock);
443 netif_wake_queue(dev);
447 * The interrupt handler.
448 * This is called from the MPC core interrupt.
451 fs_enet_interrupt(int irq, void *dev_id)
453 struct net_device *dev = dev_id;
454 struct fs_enet_private *fep;
455 const struct fs_platform_info *fpi;
461 fep = netdev_priv(dev);
465 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
468 int_clr_events = int_events;
470 int_clr_events &= ~fep->ev_napi_rx;
472 (*fep->ops->clear_int_events)(dev, int_clr_events);
474 if (int_events & fep->ev_err)
475 (*fep->ops->ev_error)(dev, int_events);
477 if (int_events & fep->ev_rx) {
479 fs_enet_rx_non_napi(dev);
481 napi_ok = napi_schedule_prep(&fep->napi);
483 (*fep->ops->napi_disable_rx)(dev);
484 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
486 /* NOTE: it is possible for FCCs in NAPI mode */
487 /* to submit a spurious interrupt while in poll */
489 __netif_rx_schedule(dev, &fep->napi);
493 if (int_events & fep->ev_tx)
498 return IRQ_RETVAL(handled);
501 void fs_init_bds(struct net_device *dev)
503 struct fs_enet_private *fep = netdev_priv(dev);
510 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
511 fep->tx_free = fep->tx_ring;
512 fep->cur_rx = fep->rx_bd_base;
515 * Initialize the receive buffer descriptors.
517 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
518 skb = dev_alloc_skb(ENET_RX_FRSIZE);
520 printk(KERN_WARNING DRV_MODULE_NAME
521 ": %s Memory squeeze, unable to allocate skb\n",
525 skb_align(skb, ENET_RX_ALIGN);
526 fep->rx_skbuff[i] = skb;
528 dma_map_single(fep->dev, skb->data,
529 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
531 CBDW_DATLEN(bdp, 0); /* zero */
532 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
533 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
536 * if we failed, fillup remainder
538 for (; i < fep->rx_ring; i++, bdp++) {
539 fep->rx_skbuff[i] = NULL;
540 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
544 * ...and the same for transmit.
546 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
547 fep->tx_skbuff[i] = NULL;
548 CBDW_BUFADDR(bdp, 0);
550 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
554 void fs_cleanup_bds(struct net_device *dev)
556 struct fs_enet_private *fep = netdev_priv(dev);
562 * Reset SKB transmit buffers.
564 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
565 if ((skb = fep->tx_skbuff[i]) == NULL)
569 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
570 skb->len, DMA_TO_DEVICE);
572 fep->tx_skbuff[i] = NULL;
577 * Reset SKB receive buffers
579 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
580 if ((skb = fep->rx_skbuff[i]) == NULL)
584 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
585 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
588 fep->rx_skbuff[i] = NULL;
594 /**********************************************************************************/
596 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
598 struct fs_enet_private *fep = netdev_priv(dev);
604 spin_lock_irqsave(&fep->tx_lock, flags);
607 * Fill in a Tx ring entry
611 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
612 netif_stop_queue(dev);
613 spin_unlock_irqrestore(&fep->tx_lock, flags);
616 * Ooops. All transmit buffers are full. Bail out.
617 * This should not happen, since the tx queue should be stopped.
619 printk(KERN_WARNING DRV_MODULE_NAME
620 ": %s tx queue full!.\n", dev->name);
621 return NETDEV_TX_BUSY;
624 curidx = bdp - fep->tx_bd_base;
626 * Clear all of the status flags.
628 CBDC_SC(bdp, BD_ENET_TX_STATS);
633 fep->tx_skbuff[curidx] = skb;
635 fep->stats.tx_bytes += skb->len;
638 * Push the data cache so the CPM does not get stale memory data.
640 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
641 skb->data, skb->len, DMA_TO_DEVICE));
642 CBDW_DATLEN(bdp, skb->len);
644 dev->trans_start = jiffies;
647 * If this was the last BD in the ring, start at the beginning again.
649 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
652 fep->cur_tx = fep->tx_bd_base;
655 netif_stop_queue(dev);
657 /* Trigger transmission start */
658 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
659 BD_ENET_TX_LAST | BD_ENET_TX_TC;
661 /* note that while FEC does not have this bit
662 * it marks it as available for software use
663 * yay for hw reuse :) */
665 sc |= BD_ENET_TX_PAD;
668 (*fep->ops->tx_kickstart)(dev);
670 spin_unlock_irqrestore(&fep->tx_lock, flags);
675 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
678 struct fs_enet_private *fep = netdev_priv(dev);
680 (*fep->ops->pre_request_irq)(dev, irq);
681 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
684 static void fs_free_irq(struct net_device *dev, int irq)
686 struct fs_enet_private *fep = netdev_priv(dev);
689 (*fep->ops->post_free_irq)(dev, irq);
692 static void fs_timeout(struct net_device *dev)
694 struct fs_enet_private *fep = netdev_priv(dev);
698 fep->stats.tx_errors++;
700 spin_lock_irqsave(&fep->lock, flags);
702 if (dev->flags & IFF_UP) {
703 phy_stop(fep->phydev);
704 (*fep->ops->stop)(dev);
705 (*fep->ops->restart)(dev);
706 phy_start(fep->phydev);
709 phy_start(fep->phydev);
710 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
711 spin_unlock_irqrestore(&fep->lock, flags);
714 netif_wake_queue(dev);
717 /*-----------------------------------------------------------------------------
718 * generic link-change handler - should be sufficient for most cases
719 *-----------------------------------------------------------------------------*/
720 static void generic_adjust_link(struct net_device *dev)
722 struct fs_enet_private *fep = netdev_priv(dev);
723 struct phy_device *phydev = fep->phydev;
727 /* adjust to duplex mode */
728 if (phydev->duplex != fep->oldduplex) {
730 fep->oldduplex = phydev->duplex;
733 if (phydev->speed != fep->oldspeed) {
735 fep->oldspeed = phydev->speed;
742 netif_carrier_on(dev);
743 netif_start_queue(dev);
747 fep->ops->restart(dev);
748 } else if (fep->oldlink) {
753 netif_carrier_off(dev);
754 netif_stop_queue(dev);
757 if (new_state && netif_msg_link(fep))
758 phy_print_status(phydev);
762 static void fs_adjust_link(struct net_device *dev)
764 struct fs_enet_private *fep = netdev_priv(dev);
767 spin_lock_irqsave(&fep->lock, flags);
769 if(fep->ops->adjust_link)
770 fep->ops->adjust_link(dev);
772 generic_adjust_link(dev);
774 spin_unlock_irqrestore(&fep->lock, flags);
777 static int fs_init_phy(struct net_device *dev)
779 struct fs_enet_private *fep = netdev_priv(dev);
780 struct phy_device *phydev;
786 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
787 PHY_INTERFACE_MODE_MII);
789 printk("No phy bus ID specified in BSP code\n");
792 if (IS_ERR(phydev)) {
793 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
794 return PTR_ERR(phydev);
797 fep->phydev = phydev;
802 static int fs_enet_open(struct net_device *dev)
804 struct fs_enet_private *fep = netdev_priv(dev);
808 if (fep->fpi->use_napi)
809 napi_enable(&fep->napi);
811 /* Install our interrupt handler. */
812 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
814 printk(KERN_ERR DRV_MODULE_NAME
815 ": %s Could not allocate FS_ENET IRQ!", dev->name);
816 if (fep->fpi->use_napi)
817 napi_disable(&fep->napi);
821 err = fs_init_phy(dev);
823 if (fep->fpi->use_napi)
824 napi_disable(&fep->napi);
827 phy_start(fep->phydev);
832 static int fs_enet_close(struct net_device *dev)
834 struct fs_enet_private *fep = netdev_priv(dev);
837 netif_stop_queue(dev);
838 netif_carrier_off(dev);
839 if (fep->fpi->use_napi)
840 napi_disable(&fep->napi);
841 phy_stop(fep->phydev);
843 spin_lock_irqsave(&fep->lock, flags);
844 spin_lock(&fep->tx_lock);
845 (*fep->ops->stop)(dev);
846 spin_unlock(&fep->tx_lock);
847 spin_unlock_irqrestore(&fep->lock, flags);
849 /* release any irqs */
850 phy_disconnect(fep->phydev);
852 fs_free_irq(dev, fep->interrupt);
857 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
859 struct fs_enet_private *fep = netdev_priv(dev);
863 /*************************************************************************/
865 static void fs_get_drvinfo(struct net_device *dev,
866 struct ethtool_drvinfo *info)
868 strcpy(info->driver, DRV_MODULE_NAME);
869 strcpy(info->version, DRV_MODULE_VERSION);
872 static int fs_get_regs_len(struct net_device *dev)
874 struct fs_enet_private *fep = netdev_priv(dev);
876 return (*fep->ops->get_regs_len)(dev);
879 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
882 struct fs_enet_private *fep = netdev_priv(dev);
888 spin_lock_irqsave(&fep->lock, flags);
889 r = (*fep->ops->get_regs)(dev, p, &len);
890 spin_unlock_irqrestore(&fep->lock, flags);
896 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
898 struct fs_enet_private *fep = netdev_priv(dev);
903 return phy_ethtool_gset(fep->phydev, cmd);
906 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
908 struct fs_enet_private *fep = netdev_priv(dev);
913 return phy_ethtool_sset(fep->phydev, cmd);
916 static int fs_nway_reset(struct net_device *dev)
921 static u32 fs_get_msglevel(struct net_device *dev)
923 struct fs_enet_private *fep = netdev_priv(dev);
924 return fep->msg_enable;
927 static void fs_set_msglevel(struct net_device *dev, u32 value)
929 struct fs_enet_private *fep = netdev_priv(dev);
930 fep->msg_enable = value;
933 static const struct ethtool_ops fs_ethtool_ops = {
934 .get_drvinfo = fs_get_drvinfo,
935 .get_regs_len = fs_get_regs_len,
936 .get_settings = fs_get_settings,
937 .set_settings = fs_set_settings,
938 .nway_reset = fs_nway_reset,
939 .get_link = ethtool_op_get_link,
940 .get_msglevel = fs_get_msglevel,
941 .set_msglevel = fs_set_msglevel,
942 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
943 .set_sg = ethtool_op_set_sg,
944 .get_regs = fs_get_regs,
947 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
949 struct fs_enet_private *fep = netdev_priv(dev);
950 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
952 if (!netif_running(dev))
955 return phy_mii_ioctl(fep->phydev, mii, cmd);
958 extern int fs_mii_connect(struct net_device *dev);
959 extern void fs_mii_disconnect(struct net_device *dev);
961 #ifndef CONFIG_PPC_CPM_NEW_BINDING
962 static struct net_device *fs_init_instance(struct device *dev,
963 struct fs_platform_info *fpi)
965 struct net_device *ndev = NULL;
966 struct fs_enet_private *fep = NULL;
967 int privsize, i, r, err = 0, registered = 0;
969 fpi->fs_no = fs_get_id(fpi);
971 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
972 return ERR_PTR(-EINVAL);
974 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
975 (fpi->rx_ring + fpi->tx_ring));
977 ndev = alloc_etherdev(privsize);
983 fep = netdev_priv(ndev);
986 dev_set_drvdata(dev, ndev);
988 if (fpi->init_ioports)
989 fpi->init_ioports((struct fs_platform_info *)fpi);
991 #ifdef CONFIG_FS_ENET_HAS_FEC
992 if (fs_get_fec_index(fpi->fs_no) >= 0)
993 fep->ops = &fs_fec_ops;
996 #ifdef CONFIG_FS_ENET_HAS_SCC
997 if (fs_get_scc_index(fpi->fs_no) >=0)
998 fep->ops = &fs_scc_ops;
1001 #ifdef CONFIG_FS_ENET_HAS_FCC
1002 if (fs_get_fcc_index(fpi->fs_no) >= 0)
1003 fep->ops = &fs_fcc_ops;
1006 if (fep->ops == NULL) {
1007 printk(KERN_ERR DRV_MODULE_NAME
1008 ": %s No matching ops found (%d).\n",
1009 ndev->name, fpi->fs_no);
1014 r = (*fep->ops->setup_data)(ndev);
1016 printk(KERN_ERR DRV_MODULE_NAME
1017 ": %s setup_data failed\n",
1023 /* point rx_skbuff, tx_skbuff */
1024 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1025 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1028 spin_lock_init(&fep->lock);
1029 spin_lock_init(&fep->tx_lock);
1032 * Set the Ethernet address.
1034 for (i = 0; i < 6; i++)
1035 ndev->dev_addr[i] = fpi->macaddr[i];
1037 r = (*fep->ops->allocate_bd)(ndev);
1039 if (fep->ring_base == NULL) {
1040 printk(KERN_ERR DRV_MODULE_NAME
1041 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1047 * Set receive and transmit descriptor base.
1049 fep->rx_bd_base = fep->ring_base;
1050 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1052 /* initialize ring size variables */
1053 fep->tx_ring = fpi->tx_ring;
1054 fep->rx_ring = fpi->rx_ring;
1057 * The FEC Ethernet specific entries in the device structure.
1059 ndev->open = fs_enet_open;
1060 ndev->hard_start_xmit = fs_enet_start_xmit;
1061 ndev->tx_timeout = fs_timeout;
1062 ndev->watchdog_timeo = 2 * HZ;
1063 ndev->stop = fs_enet_close;
1064 ndev->get_stats = fs_enet_get_stats;
1065 ndev->set_multicast_list = fs_set_multicast_list;
1067 #ifdef CONFIG_NET_POLL_CONTROLLER
1068 ndev->poll_controller = fs_enet_netpoll;
1071 netif_napi_add(ndev, &fep->napi,
1072 fs_enet_rx_napi, fpi->napi_weight);
1074 ndev->ethtool_ops = &fs_ethtool_ops;
1075 ndev->do_ioctl = fs_ioctl;
1077 init_timer(&fep->phy_timer_list);
1079 netif_carrier_off(ndev);
1081 err = register_netdev(ndev);
1083 printk(KERN_ERR DRV_MODULE_NAME
1084 ": %s register_netdev failed.\n", ndev->name);
1095 unregister_netdev(ndev);
1097 if (fep && fep->ops) {
1098 (*fep->ops->free_bd)(ndev);
1099 (*fep->ops->cleanup_data)(ndev);
1105 dev_set_drvdata(dev, NULL);
1107 return ERR_PTR(err);
1110 static int fs_cleanup_instance(struct net_device *ndev)
1112 struct fs_enet_private *fep;
1113 const struct fs_platform_info *fpi;
1119 fep = netdev_priv(ndev);
1125 unregister_netdev(ndev);
1127 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1128 (void __force *)fep->ring_base, fep->ring_mem_addr);
1131 (*fep->ops->cleanup_data)(ndev);
1135 dev_set_drvdata(dev, NULL);
1145 /**************************************************************************************/
1147 /* handy pointer to the immap */
1148 void __iomem *fs_enet_immap = NULL;
1150 static int setup_immap(void)
1153 fs_enet_immap = ioremap(IMAP_ADDR, 0x4000);
1154 WARN_ON(!fs_enet_immap);
1155 #elif defined(CONFIG_CPM2)
1156 fs_enet_immap = cpm2_immr;
1162 static void cleanup_immap(void)
1164 #if defined(CONFIG_CPM1)
1165 iounmap(fs_enet_immap);
1169 /**************************************************************************************/
1171 #ifdef CONFIG_PPC_CPM_NEW_BINDING
1172 static int __devinit find_phy(struct device_node *np,
1173 struct fs_platform_info *fpi)
1175 struct device_node *phynode, *mdionode;
1176 int ret = 0, len, bus_id;
1179 data = of_get_property(np, "fixed-link", NULL);
1181 snprintf(fpi->bus_id, 16, "%x:%02x", 0, *data);
1185 data = of_get_property(np, "phy-handle", &len);
1186 if (!data || len != 4)
1189 phynode = of_find_node_by_phandle(*data);
1193 data = of_get_property(phynode, "reg", &len);
1194 if (!data || len != 4) {
1199 mdionode = of_get_parent(phynode);
1205 bus_id = of_get_gpio(mdionode, 0);
1207 struct resource res;
1208 ret = of_address_to_resource(mdionode, 0, &res);
1214 snprintf(fpi->bus_id, 16, "%x:%02x", bus_id, *data);
1217 of_node_put(mdionode);
1219 of_node_put(phynode);
1223 #ifdef CONFIG_FS_ENET_HAS_FEC
1224 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
1226 #define IS_FEC(match) 0
1229 static int __devinit fs_enet_probe(struct of_device *ofdev,
1230 const struct of_device_id *match)
1232 struct net_device *ndev;
1233 struct fs_enet_private *fep;
1234 struct fs_platform_info *fpi;
1237 int privsize, len, ret = -ENODEV;
1239 fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
1243 if (!IS_FEC(match)) {
1244 data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
1245 if (!data || len != 4)
1248 fpi->cp_command = *data;
1253 fpi->rx_copybreak = 240;
1255 fpi->napi_weight = 17;
1257 ret = find_phy(ofdev->node, fpi);
1261 privsize = sizeof(*fep) +
1262 sizeof(struct sk_buff **) *
1263 (fpi->rx_ring + fpi->tx_ring);
1265 ndev = alloc_etherdev(privsize);
1271 dev_set_drvdata(&ofdev->dev, ndev);
1273 fep = netdev_priv(ndev);
1274 fep->dev = &ofdev->dev;
1277 fep->ops = match->data;
1279 ret = fep->ops->setup_data(ndev);
1283 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1284 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1286 spin_lock_init(&fep->lock);
1287 spin_lock_init(&fep->tx_lock);
1289 mac_addr = of_get_mac_address(ofdev->node);
1291 memcpy(ndev->dev_addr, mac_addr, 6);
1293 ret = fep->ops->allocate_bd(ndev);
1295 goto out_cleanup_data;
1297 fep->rx_bd_base = fep->ring_base;
1298 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1300 fep->tx_ring = fpi->tx_ring;
1301 fep->rx_ring = fpi->rx_ring;
1303 ndev->open = fs_enet_open;
1304 ndev->hard_start_xmit = fs_enet_start_xmit;
1305 ndev->tx_timeout = fs_timeout;
1306 ndev->watchdog_timeo = 2 * HZ;
1307 ndev->stop = fs_enet_close;
1308 ndev->get_stats = fs_enet_get_stats;
1309 ndev->set_multicast_list = fs_set_multicast_list;
1312 netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
1315 ndev->ethtool_ops = &fs_ethtool_ops;
1316 ndev->do_ioctl = fs_ioctl;
1318 init_timer(&fep->phy_timer_list);
1320 netif_carrier_off(ndev);
1322 ret = register_netdev(ndev);
1326 printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
1328 ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
1329 ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);
1334 fep->ops->free_bd(ndev);
1336 fep->ops->cleanup_data(ndev);
1339 dev_set_drvdata(&ofdev->dev, NULL);
1345 static int fs_enet_remove(struct of_device *ofdev)
1347 struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
1348 struct fs_enet_private *fep = netdev_priv(ndev);
1350 unregister_netdev(ndev);
1352 fep->ops->free_bd(ndev);
1353 fep->ops->cleanup_data(ndev);
1354 dev_set_drvdata(fep->dev, NULL);
1360 static struct of_device_id fs_enet_match[] = {
1361 #ifdef CONFIG_FS_ENET_HAS_SCC
1363 .compatible = "fsl,cpm1-scc-enet",
1364 .data = (void *)&fs_scc_ops,
1367 #ifdef CONFIG_FS_ENET_HAS_FCC
1369 .compatible = "fsl,cpm2-fcc-enet",
1370 .data = (void *)&fs_fcc_ops,
1373 #ifdef CONFIG_FS_ENET_HAS_FEC
1375 .compatible = "fsl,pq1-fec-enet",
1376 .data = (void *)&fs_fec_ops,
1382 static struct of_platform_driver fs_enet_driver = {
1384 .match_table = fs_enet_match,
1385 .probe = fs_enet_probe,
1386 .remove = fs_enet_remove,
1389 static int __init fs_init(void)
1391 int r = setup_immap();
1395 r = of_register_platform_driver(&fs_enet_driver);
1406 static void __exit fs_cleanup(void)
1408 of_unregister_platform_driver(&fs_enet_driver);
1412 static int __devinit fs_enet_probe(struct device *dev)
1414 struct net_device *ndev;
1416 /* no fixup - no device */
1417 if (dev->platform_data == NULL) {
1418 printk(KERN_INFO "fs_enet: "
1419 "probe called with no platform data; "
1420 "remove unused devices\n");
1424 ndev = fs_init_instance(dev, dev->platform_data);
1426 return PTR_ERR(ndev);
1430 static int fs_enet_remove(struct device *dev)
1432 return fs_cleanup_instance(dev_get_drvdata(dev));
1435 static struct device_driver fs_enet_fec_driver = {
1436 .name = "fsl-cpm-fec",
1437 .bus = &platform_bus_type,
1438 .probe = fs_enet_probe,
1439 .remove = fs_enet_remove,
1441 /* .suspend = fs_enet_suspend, TODO */
1442 /* .resume = fs_enet_resume, TODO */
1446 static struct device_driver fs_enet_scc_driver = {
1447 .name = "fsl-cpm-scc",
1448 .bus = &platform_bus_type,
1449 .probe = fs_enet_probe,
1450 .remove = fs_enet_remove,
1452 /* .suspend = fs_enet_suspend, TODO */
1453 /* .resume = fs_enet_resume, TODO */
1457 static struct device_driver fs_enet_fcc_driver = {
1458 .name = "fsl-cpm-fcc",
1459 .bus = &platform_bus_type,
1460 .probe = fs_enet_probe,
1461 .remove = fs_enet_remove,
1463 /* .suspend = fs_enet_suspend, TODO */
1464 /* .resume = fs_enet_resume, TODO */
1468 static int __init fs_init(void)
1479 #ifdef CONFIG_FS_ENET_HAS_FCC
1480 /* let's insert mii stuff */
1481 r = fs_enet_mdio_bb_init();
1484 printk(KERN_ERR DRV_MODULE_NAME
1485 "BB PHY init failed.\n");
1488 r = driver_register(&fs_enet_fcc_driver);
1493 #ifdef CONFIG_FS_ENET_HAS_FEC
1494 r = fs_enet_mdio_fec_init();
1496 printk(KERN_ERR DRV_MODULE_NAME
1497 "FEC PHY init failed.\n");
1501 r = driver_register(&fs_enet_fec_driver);
1506 #ifdef CONFIG_FS_ENET_HAS_SCC
1507 r = driver_register(&fs_enet_scc_driver);
1518 static void __exit fs_cleanup(void)
1520 driver_unregister(&fs_enet_fec_driver);
1521 driver_unregister(&fs_enet_fcc_driver);
1522 driver_unregister(&fs_enet_scc_driver);
1527 #ifdef CONFIG_NET_POLL_CONTROLLER
1528 static void fs_enet_netpoll(struct net_device *dev)
1530 disable_irq(dev->irq);
1531 fs_enet_interrupt(dev->irq, dev, NULL);
1532 enable_irq(dev->irq);
1536 /**************************************************************************************/
1538 module_init(fs_init);
1539 module_exit(fs_cleanup);