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 <asm/of_platform.h>
51 /*************************************************/
53 #ifndef CONFIG_PPC_CPM_NEW_BINDING
54 static char version[] __devinitdata =
55 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
58 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
59 MODULE_DESCRIPTION("Freescale Ethernet Driver");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(DRV_MODULE_VERSION);
63 static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
64 module_param(fs_enet_debug, int, 0);
65 MODULE_PARM_DESC(fs_enet_debug,
66 "Freescale bitmapped debugging message enable value");
68 #ifdef CONFIG_NET_POLL_CONTROLLER
69 static void fs_enet_netpoll(struct net_device *dev);
72 static void fs_set_multicast_list(struct net_device *dev)
74 struct fs_enet_private *fep = netdev_priv(dev);
76 (*fep->ops->set_multicast_list)(dev);
79 static void skb_align(struct sk_buff *skb, int align)
81 int off = ((unsigned long)skb->data) & (align - 1);
84 skb_reserve(skb, align - off);
87 /* NAPI receive function */
88 static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
90 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
91 struct net_device *dev = to_net_dev(fep->dev);
92 const struct fs_platform_info *fpi = fep->fpi;
94 struct sk_buff *skb, *skbn, *skbt;
99 if (!netif_running(dev))
103 * First, grab all of the stats for the incoming packet.
104 * These get messed up if we get called due to a busy condition.
108 /* clear RX status bits for napi*/
109 (*fep->ops->napi_clear_rx_event)(dev);
111 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
112 curidx = bdp - fep->rx_bd_base;
115 * Since we have allocated space to hold a complete frame,
116 * the last indicator should be set.
118 if ((sc & BD_ENET_RX_LAST) == 0)
119 printk(KERN_WARNING DRV_MODULE_NAME
120 ": %s rcv is not +last\n",
126 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
127 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
128 fep->stats.rx_errors++;
129 /* Frame too long or too short. */
130 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
131 fep->stats.rx_length_errors++;
132 /* Frame alignment */
133 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
134 fep->stats.rx_frame_errors++;
136 if (sc & BD_ENET_RX_CR)
137 fep->stats.rx_crc_errors++;
139 if (sc & BD_ENET_RX_OV)
140 fep->stats.rx_crc_errors++;
142 skb = fep->rx_skbuff[curidx];
144 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
145 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
151 skb = fep->rx_skbuff[curidx];
153 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
154 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
158 * Process the incoming frame.
160 fep->stats.rx_packets++;
161 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
162 fep->stats.rx_bytes += pkt_len + 4;
164 if (pkt_len <= fpi->rx_copybreak) {
165 /* +2 to make IP header L1 cache aligned */
166 skbn = dev_alloc_skb(pkt_len + 2);
168 skb_reserve(skbn, 2); /* align IP header */
169 skb_copy_from_linear_data(skb,
170 skbn->data, pkt_len);
177 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
180 skb_align(skbn, ENET_RX_ALIGN);
184 skb_put(skb, pkt_len); /* Make room */
185 skb->protocol = eth_type_trans(skb, dev);
187 netif_receive_skb(skb);
189 printk(KERN_WARNING DRV_MODULE_NAME
190 ": %s Memory squeeze, dropping packet.\n",
192 fep->stats.rx_dropped++;
197 fep->rx_skbuff[curidx] = skbn;
198 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
199 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
202 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
205 * Update BD pointer to next entry.
207 if ((sc & BD_ENET_RX_WRAP) == 0)
210 bdp = fep->rx_bd_base;
212 (*fep->ops->rx_bd_done)(dev);
214 if (received >= budget)
220 if (received >= budget) {
222 netif_rx_complete(dev, napi);
223 (*fep->ops->napi_enable_rx)(dev);
228 /* non NAPI receive function */
229 static int fs_enet_rx_non_napi(struct net_device *dev)
231 struct fs_enet_private *fep = netdev_priv(dev);
232 const struct fs_platform_info *fpi = fep->fpi;
234 struct sk_buff *skb, *skbn, *skbt;
239 * First, grab all of the stats for the incoming packet.
240 * These get messed up if we get called due to a busy condition.
244 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
246 curidx = bdp - fep->rx_bd_base;
249 * Since we have allocated space to hold a complete frame,
250 * the last indicator should be set.
252 if ((sc & BD_ENET_RX_LAST) == 0)
253 printk(KERN_WARNING DRV_MODULE_NAME
254 ": %s rcv is not +last\n",
260 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
261 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
262 fep->stats.rx_errors++;
263 /* Frame too long or too short. */
264 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
265 fep->stats.rx_length_errors++;
266 /* Frame alignment */
267 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
268 fep->stats.rx_frame_errors++;
270 if (sc & BD_ENET_RX_CR)
271 fep->stats.rx_crc_errors++;
273 if (sc & BD_ENET_RX_OV)
274 fep->stats.rx_crc_errors++;
276 skb = fep->rx_skbuff[curidx];
278 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
279 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
286 skb = fep->rx_skbuff[curidx];
288 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
289 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
293 * Process the incoming frame.
295 fep->stats.rx_packets++;
296 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
297 fep->stats.rx_bytes += pkt_len + 4;
299 if (pkt_len <= fpi->rx_copybreak) {
300 /* +2 to make IP header L1 cache aligned */
301 skbn = dev_alloc_skb(pkt_len + 2);
303 skb_reserve(skbn, 2); /* align IP header */
304 skb_copy_from_linear_data(skb,
305 skbn->data, pkt_len);
312 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
315 skb_align(skbn, ENET_RX_ALIGN);
319 skb_put(skb, pkt_len); /* Make room */
320 skb->protocol = eth_type_trans(skb, dev);
324 printk(KERN_WARNING DRV_MODULE_NAME
325 ": %s Memory squeeze, dropping packet.\n",
327 fep->stats.rx_dropped++;
332 fep->rx_skbuff[curidx] = skbn;
333 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
334 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
337 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
340 * Update BD pointer to next entry.
342 if ((sc & BD_ENET_RX_WRAP) == 0)
345 bdp = fep->rx_bd_base;
347 (*fep->ops->rx_bd_done)(dev);
355 static void fs_enet_tx(struct net_device *dev)
357 struct fs_enet_private *fep = netdev_priv(dev);
360 int dirtyidx, do_wake, do_restart;
363 spin_lock(&fep->tx_lock);
366 do_wake = do_restart = 0;
367 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
368 dirtyidx = bdp - fep->tx_bd_base;
370 if (fep->tx_free == fep->tx_ring)
373 skb = fep->tx_skbuff[dirtyidx];
378 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
379 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
381 if (sc & BD_ENET_TX_HB) /* No heartbeat */
382 fep->stats.tx_heartbeat_errors++;
383 if (sc & BD_ENET_TX_LC) /* Late collision */
384 fep->stats.tx_window_errors++;
385 if (sc & BD_ENET_TX_RL) /* Retrans limit */
386 fep->stats.tx_aborted_errors++;
387 if (sc & BD_ENET_TX_UN) /* Underrun */
388 fep->stats.tx_fifo_errors++;
389 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
390 fep->stats.tx_carrier_errors++;
392 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
393 fep->stats.tx_errors++;
397 fep->stats.tx_packets++;
399 if (sc & BD_ENET_TX_READY)
400 printk(KERN_WARNING DRV_MODULE_NAME
401 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
405 * Deferred means some collisions occurred during transmit,
406 * but we eventually sent the packet OK.
408 if (sc & BD_ENET_TX_DEF)
409 fep->stats.collisions++;
412 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
413 skb->len, DMA_TO_DEVICE);
416 * Free the sk buffer associated with this last transmit.
418 dev_kfree_skb_irq(skb);
419 fep->tx_skbuff[dirtyidx] = NULL;
422 * Update pointer to next buffer descriptor to be transmitted.
424 if ((sc & BD_ENET_TX_WRAP) == 0)
427 bdp = fep->tx_bd_base;
430 * Since we have freed up a buffer, the ring is no longer
440 (*fep->ops->tx_restart)(dev);
442 spin_unlock(&fep->tx_lock);
445 netif_wake_queue(dev);
449 * The interrupt handler.
450 * This is called from the MPC core interrupt.
453 fs_enet_interrupt(int irq, void *dev_id)
455 struct net_device *dev = dev_id;
456 struct fs_enet_private *fep;
457 const struct fs_platform_info *fpi;
463 fep = netdev_priv(dev);
467 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
470 int_clr_events = int_events;
472 int_clr_events &= ~fep->ev_napi_rx;
474 (*fep->ops->clear_int_events)(dev, int_clr_events);
476 if (int_events & fep->ev_err)
477 (*fep->ops->ev_error)(dev, int_events);
479 if (int_events & fep->ev_rx) {
481 fs_enet_rx_non_napi(dev);
483 napi_ok = napi_schedule_prep(&fep->napi);
485 (*fep->ops->napi_disable_rx)(dev);
486 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
488 /* NOTE: it is possible for FCCs in NAPI mode */
489 /* to submit a spurious interrupt while in poll */
491 __netif_rx_schedule(dev, &fep->napi);
495 if (int_events & fep->ev_tx)
500 return IRQ_RETVAL(handled);
503 void fs_init_bds(struct net_device *dev)
505 struct fs_enet_private *fep = netdev_priv(dev);
512 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
513 fep->tx_free = fep->tx_ring;
514 fep->cur_rx = fep->rx_bd_base;
517 * Initialize the receive buffer descriptors.
519 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
520 skb = dev_alloc_skb(ENET_RX_FRSIZE);
522 printk(KERN_WARNING DRV_MODULE_NAME
523 ": %s Memory squeeze, unable to allocate skb\n",
527 skb_align(skb, ENET_RX_ALIGN);
528 fep->rx_skbuff[i] = skb;
530 dma_map_single(fep->dev, skb->data,
531 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
533 CBDW_DATLEN(bdp, 0); /* zero */
534 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
535 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
538 * if we failed, fillup remainder
540 for (; i < fep->rx_ring; i++, bdp++) {
541 fep->rx_skbuff[i] = NULL;
542 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
546 * ...and the same for transmit.
548 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
549 fep->tx_skbuff[i] = NULL;
550 CBDW_BUFADDR(bdp, 0);
552 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
556 void fs_cleanup_bds(struct net_device *dev)
558 struct fs_enet_private *fep = netdev_priv(dev);
564 * Reset SKB transmit buffers.
566 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
567 if ((skb = fep->tx_skbuff[i]) == NULL)
571 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
572 skb->len, DMA_TO_DEVICE);
574 fep->tx_skbuff[i] = NULL;
579 * Reset SKB receive buffers
581 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
582 if ((skb = fep->rx_skbuff[i]) == NULL)
586 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
587 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
590 fep->rx_skbuff[i] = NULL;
596 /**********************************************************************************/
598 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
600 struct fs_enet_private *fep = netdev_priv(dev);
606 spin_lock_irqsave(&fep->tx_lock, flags);
609 * Fill in a Tx ring entry
613 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
614 netif_stop_queue(dev);
615 spin_unlock_irqrestore(&fep->tx_lock, flags);
618 * Ooops. All transmit buffers are full. Bail out.
619 * This should not happen, since the tx queue should be stopped.
621 printk(KERN_WARNING DRV_MODULE_NAME
622 ": %s tx queue full!.\n", dev->name);
623 return NETDEV_TX_BUSY;
626 curidx = bdp - fep->tx_bd_base;
628 * Clear all of the status flags.
630 CBDC_SC(bdp, BD_ENET_TX_STATS);
635 fep->tx_skbuff[curidx] = skb;
637 fep->stats.tx_bytes += skb->len;
640 * Push the data cache so the CPM does not get stale memory data.
642 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
643 skb->data, skb->len, DMA_TO_DEVICE));
644 CBDW_DATLEN(bdp, skb->len);
646 dev->trans_start = jiffies;
649 * If this was the last BD in the ring, start at the beginning again.
651 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
654 fep->cur_tx = fep->tx_bd_base;
657 netif_stop_queue(dev);
659 /* Trigger transmission start */
660 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
661 BD_ENET_TX_LAST | BD_ENET_TX_TC;
663 /* note that while FEC does not have this bit
664 * it marks it as available for software use
665 * yay for hw reuse :) */
667 sc |= BD_ENET_TX_PAD;
670 (*fep->ops->tx_kickstart)(dev);
672 spin_unlock_irqrestore(&fep->tx_lock, flags);
677 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
680 struct fs_enet_private *fep = netdev_priv(dev);
682 (*fep->ops->pre_request_irq)(dev, irq);
683 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
686 static void fs_free_irq(struct net_device *dev, int irq)
688 struct fs_enet_private *fep = netdev_priv(dev);
691 (*fep->ops->post_free_irq)(dev, irq);
694 static void fs_timeout(struct net_device *dev)
696 struct fs_enet_private *fep = netdev_priv(dev);
700 fep->stats.tx_errors++;
702 spin_lock_irqsave(&fep->lock, flags);
704 if (dev->flags & IFF_UP) {
705 phy_stop(fep->phydev);
706 (*fep->ops->stop)(dev);
707 (*fep->ops->restart)(dev);
708 phy_start(fep->phydev);
711 phy_start(fep->phydev);
712 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
713 spin_unlock_irqrestore(&fep->lock, flags);
716 netif_wake_queue(dev);
719 /*-----------------------------------------------------------------------------
720 * generic link-change handler - should be sufficient for most cases
721 *-----------------------------------------------------------------------------*/
722 static void generic_adjust_link(struct net_device *dev)
724 struct fs_enet_private *fep = netdev_priv(dev);
725 struct phy_device *phydev = fep->phydev;
729 /* adjust to duplex mode */
730 if (phydev->duplex != fep->oldduplex) {
732 fep->oldduplex = phydev->duplex;
735 if (phydev->speed != fep->oldspeed) {
737 fep->oldspeed = phydev->speed;
744 netif_carrier_on(dev);
745 netif_start_queue(dev);
749 fep->ops->restart(dev);
750 } else if (fep->oldlink) {
755 netif_carrier_off(dev);
756 netif_stop_queue(dev);
759 if (new_state && netif_msg_link(fep))
760 phy_print_status(phydev);
764 static void fs_adjust_link(struct net_device *dev)
766 struct fs_enet_private *fep = netdev_priv(dev);
769 spin_lock_irqsave(&fep->lock, flags);
771 if(fep->ops->adjust_link)
772 fep->ops->adjust_link(dev);
774 generic_adjust_link(dev);
776 spin_unlock_irqrestore(&fep->lock, flags);
779 static int fs_init_phy(struct net_device *dev)
781 struct fs_enet_private *fep = netdev_priv(dev);
782 struct phy_device *phydev;
788 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
789 PHY_INTERFACE_MODE_MII);
791 printk("No phy bus ID specified in BSP code\n");
794 if (IS_ERR(phydev)) {
795 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
796 return PTR_ERR(phydev);
799 fep->phydev = phydev;
804 static int fs_enet_open(struct net_device *dev)
806 struct fs_enet_private *fep = netdev_priv(dev);
810 napi_enable(&fep->napi);
812 /* Install our interrupt handler. */
813 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
815 printk(KERN_ERR DRV_MODULE_NAME
816 ": %s Could not allocate FS_ENET IRQ!", dev->name);
817 napi_disable(&fep->napi);
821 err = fs_init_phy(dev);
823 napi_disable(&fep->napi);
826 phy_start(fep->phydev);
831 static int fs_enet_close(struct net_device *dev)
833 struct fs_enet_private *fep = netdev_priv(dev);
836 netif_stop_queue(dev);
837 netif_carrier_off(dev);
838 napi_disable(&fep->napi);
839 phy_stop(fep->phydev);
841 spin_lock_irqsave(&fep->lock, flags);
842 spin_lock(&fep->tx_lock);
843 (*fep->ops->stop)(dev);
844 spin_unlock(&fep->tx_lock);
845 spin_unlock_irqrestore(&fep->lock, flags);
847 /* release any irqs */
848 phy_disconnect(fep->phydev);
850 fs_free_irq(dev, fep->interrupt);
855 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
857 struct fs_enet_private *fep = netdev_priv(dev);
861 /*************************************************************************/
863 static void fs_get_drvinfo(struct net_device *dev,
864 struct ethtool_drvinfo *info)
866 strcpy(info->driver, DRV_MODULE_NAME);
867 strcpy(info->version, DRV_MODULE_VERSION);
870 static int fs_get_regs_len(struct net_device *dev)
872 struct fs_enet_private *fep = netdev_priv(dev);
874 return (*fep->ops->get_regs_len)(dev);
877 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
880 struct fs_enet_private *fep = netdev_priv(dev);
886 spin_lock_irqsave(&fep->lock, flags);
887 r = (*fep->ops->get_regs)(dev, p, &len);
888 spin_unlock_irqrestore(&fep->lock, flags);
894 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
896 struct fs_enet_private *fep = netdev_priv(dev);
897 return phy_ethtool_gset(fep->phydev, cmd);
900 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
902 struct fs_enet_private *fep = netdev_priv(dev);
903 phy_ethtool_sset(fep->phydev, cmd);
907 static int fs_nway_reset(struct net_device *dev)
912 static u32 fs_get_msglevel(struct net_device *dev)
914 struct fs_enet_private *fep = netdev_priv(dev);
915 return fep->msg_enable;
918 static void fs_set_msglevel(struct net_device *dev, u32 value)
920 struct fs_enet_private *fep = netdev_priv(dev);
921 fep->msg_enable = value;
924 static const struct ethtool_ops fs_ethtool_ops = {
925 .get_drvinfo = fs_get_drvinfo,
926 .get_regs_len = fs_get_regs_len,
927 .get_settings = fs_get_settings,
928 .set_settings = fs_set_settings,
929 .nway_reset = fs_nway_reset,
930 .get_link = ethtool_op_get_link,
931 .get_msglevel = fs_get_msglevel,
932 .set_msglevel = fs_set_msglevel,
933 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
934 .set_sg = ethtool_op_set_sg,
935 .get_regs = fs_get_regs,
938 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
940 struct fs_enet_private *fep = netdev_priv(dev);
941 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
945 if (!netif_running(dev))
948 spin_lock_irqsave(&fep->lock, flags);
949 rc = phy_mii_ioctl(fep->phydev, mii, cmd);
950 spin_unlock_irqrestore(&fep->lock, flags);
954 extern int fs_mii_connect(struct net_device *dev);
955 extern void fs_mii_disconnect(struct net_device *dev);
957 #ifndef CONFIG_PPC_CPM_NEW_BINDING
958 static struct net_device *fs_init_instance(struct device *dev,
959 struct fs_platform_info *fpi)
961 struct net_device *ndev = NULL;
962 struct fs_enet_private *fep = NULL;
963 int privsize, i, r, err = 0, registered = 0;
965 fpi->fs_no = fs_get_id(fpi);
967 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
968 return ERR_PTR(-EINVAL);
970 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
971 (fpi->rx_ring + fpi->tx_ring));
973 ndev = alloc_etherdev(privsize);
979 fep = netdev_priv(ndev);
982 dev_set_drvdata(dev, ndev);
984 if (fpi->init_ioports)
985 fpi->init_ioports((struct fs_platform_info *)fpi);
987 #ifdef CONFIG_FS_ENET_HAS_FEC
988 if (fs_get_fec_index(fpi->fs_no) >= 0)
989 fep->ops = &fs_fec_ops;
992 #ifdef CONFIG_FS_ENET_HAS_SCC
993 if (fs_get_scc_index(fpi->fs_no) >=0)
994 fep->ops = &fs_scc_ops;
997 #ifdef CONFIG_FS_ENET_HAS_FCC
998 if (fs_get_fcc_index(fpi->fs_no) >= 0)
999 fep->ops = &fs_fcc_ops;
1002 if (fep->ops == NULL) {
1003 printk(KERN_ERR DRV_MODULE_NAME
1004 ": %s No matching ops found (%d).\n",
1005 ndev->name, fpi->fs_no);
1010 r = (*fep->ops->setup_data)(ndev);
1012 printk(KERN_ERR DRV_MODULE_NAME
1013 ": %s setup_data failed\n",
1019 /* point rx_skbuff, tx_skbuff */
1020 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1021 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1024 spin_lock_init(&fep->lock);
1025 spin_lock_init(&fep->tx_lock);
1028 * Set the Ethernet address.
1030 for (i = 0; i < 6; i++)
1031 ndev->dev_addr[i] = fpi->macaddr[i];
1033 r = (*fep->ops->allocate_bd)(ndev);
1035 if (fep->ring_base == NULL) {
1036 printk(KERN_ERR DRV_MODULE_NAME
1037 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1043 * Set receive and transmit descriptor base.
1045 fep->rx_bd_base = fep->ring_base;
1046 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1048 /* initialize ring size variables */
1049 fep->tx_ring = fpi->tx_ring;
1050 fep->rx_ring = fpi->rx_ring;
1053 * The FEC Ethernet specific entries in the device structure.
1055 ndev->open = fs_enet_open;
1056 ndev->hard_start_xmit = fs_enet_start_xmit;
1057 ndev->tx_timeout = fs_timeout;
1058 ndev->watchdog_timeo = 2 * HZ;
1059 ndev->stop = fs_enet_close;
1060 ndev->get_stats = fs_enet_get_stats;
1061 ndev->set_multicast_list = fs_set_multicast_list;
1063 #ifdef CONFIG_NET_POLL_CONTROLLER
1064 ndev->poll_controller = fs_enet_netpoll;
1067 netif_napi_add(ndev, &fep->napi,
1068 fs_enet_rx_napi, fpi->napi_weight);
1070 ndev->ethtool_ops = &fs_ethtool_ops;
1071 ndev->do_ioctl = fs_ioctl;
1073 init_timer(&fep->phy_timer_list);
1075 netif_carrier_off(ndev);
1077 err = register_netdev(ndev);
1079 printk(KERN_ERR DRV_MODULE_NAME
1080 ": %s register_netdev failed.\n", ndev->name);
1091 unregister_netdev(ndev);
1094 (*fep->ops->free_bd)(ndev);
1095 (*fep->ops->cleanup_data)(ndev);
1101 dev_set_drvdata(dev, NULL);
1103 return ERR_PTR(err);
1106 static int fs_cleanup_instance(struct net_device *ndev)
1108 struct fs_enet_private *fep;
1109 const struct fs_platform_info *fpi;
1115 fep = netdev_priv(ndev);
1121 unregister_netdev(ndev);
1123 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1124 (void __force *)fep->ring_base, fep->ring_mem_addr);
1127 (*fep->ops->cleanup_data)(ndev);
1131 dev_set_drvdata(dev, NULL);
1141 /**************************************************************************************/
1143 /* handy pointer to the immap */
1144 void __iomem *fs_enet_immap = NULL;
1146 static int setup_immap(void)
1149 fs_enet_immap = ioremap(IMAP_ADDR, 0x4000);
1150 WARN_ON(!fs_enet_immap);
1151 #elif defined(CONFIG_CPM2)
1152 fs_enet_immap = cpm2_immr;
1158 static void cleanup_immap(void)
1160 #if defined(CONFIG_CPM1)
1161 iounmap(fs_enet_immap);
1165 /**************************************************************************************/
1167 #ifdef CONFIG_PPC_CPM_NEW_BINDING
1168 static int __devinit find_phy(struct device_node *np,
1169 struct fs_platform_info *fpi)
1171 struct device_node *phynode, *mdionode;
1172 struct resource res;
1175 const u32 *data = of_get_property(np, "phy-handle", &len);
1176 if (!data || len != 4)
1179 phynode = of_find_node_by_phandle(*data);
1183 mdionode = of_get_parent(phynode);
1187 ret = of_address_to_resource(mdionode, 0, &res);
1191 data = of_get_property(phynode, "reg", &len);
1192 if (!data || len != 4)
1195 snprintf(fpi->bus_id, 16, PHY_ID_FMT, res.start, *data);
1198 of_node_put(mdionode);
1200 of_node_put(phynode);
1204 #ifdef CONFIG_FS_ENET_HAS_FEC
1205 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
1207 #define IS_FEC(match) 0
1210 static int __devinit fs_enet_probe(struct of_device *ofdev,
1211 const struct of_device_id *match)
1213 struct net_device *ndev;
1214 struct fs_enet_private *fep;
1215 struct fs_platform_info *fpi;
1218 int privsize, len, ret = -ENODEV;
1220 fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
1224 if (!IS_FEC(match)) {
1225 data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
1226 if (!data || len != 4)
1229 fpi->cp_command = *data;
1234 fpi->rx_copybreak = 240;
1236 fpi->napi_weight = 17;
1238 ret = find_phy(ofdev->node, fpi);
1242 privsize = sizeof(*fep) +
1243 sizeof(struct sk_buff **) *
1244 (fpi->rx_ring + fpi->tx_ring);
1246 ndev = alloc_etherdev(privsize);
1252 SET_MODULE_OWNER(ndev);
1253 dev_set_drvdata(&ofdev->dev, ndev);
1255 fep = netdev_priv(ndev);
1256 fep->dev = &ofdev->dev;
1258 fep->ops = match->data;
1260 ret = fep->ops->setup_data(ndev);
1264 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1265 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1267 spin_lock_init(&fep->lock);
1268 spin_lock_init(&fep->tx_lock);
1270 mac_addr = of_get_mac_address(ofdev->node);
1272 memcpy(ndev->dev_addr, mac_addr, 6);
1274 ret = fep->ops->allocate_bd(ndev);
1276 goto out_cleanup_data;
1278 fep->rx_bd_base = fep->ring_base;
1279 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1281 fep->tx_ring = fpi->tx_ring;
1282 fep->rx_ring = fpi->rx_ring;
1284 ndev->open = fs_enet_open;
1285 ndev->hard_start_xmit = fs_enet_start_xmit;
1286 ndev->tx_timeout = fs_timeout;
1287 ndev->watchdog_timeo = 2 * HZ;
1288 ndev->stop = fs_enet_close;
1289 ndev->get_stats = fs_enet_get_stats;
1290 ndev->set_multicast_list = fs_set_multicast_list;
1291 if (fpi->use_napi) {
1292 ndev->poll = fs_enet_rx_napi;
1293 ndev->weight = fpi->napi_weight;
1295 ndev->ethtool_ops = &fs_ethtool_ops;
1296 ndev->do_ioctl = fs_ioctl;
1298 init_timer(&fep->phy_timer_list);
1300 netif_carrier_off(ndev);
1302 ret = register_netdev(ndev);
1306 printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
1308 ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
1309 ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);
1314 fep->ops->free_bd(ndev);
1316 fep->ops->cleanup_data(ndev);
1319 dev_set_drvdata(&ofdev->dev, NULL);
1325 static int fs_enet_remove(struct of_device *ofdev)
1327 struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
1328 struct fs_enet_private *fep = netdev_priv(ndev);
1330 unregister_netdev(ndev);
1332 fep->ops->free_bd(ndev);
1333 fep->ops->cleanup_data(ndev);
1334 dev_set_drvdata(fep->dev, NULL);
1340 static struct of_device_id fs_enet_match[] = {
1341 #ifdef CONFIG_FS_ENET_HAS_SCC
1343 .compatible = "fsl,cpm1-scc-enet",
1344 .data = (void *)&fs_scc_ops,
1347 #ifdef CONFIG_FS_ENET_HAS_FCC
1349 .compatible = "fsl,cpm2-fcc-enet",
1350 .data = (void *)&fs_fcc_ops,
1353 #ifdef CONFIG_FS_ENET_HAS_FEC
1355 .compatible = "fsl,pq1-fec-enet",
1356 .data = (void *)&fs_fec_ops,
1362 static struct of_platform_driver fs_enet_driver = {
1364 .match_table = fs_enet_match,
1365 .probe = fs_enet_probe,
1366 .remove = fs_enet_remove,
1369 static int __init fs_init(void)
1371 int r = setup_immap();
1375 r = of_register_platform_driver(&fs_enet_driver);
1386 static void __exit fs_cleanup(void)
1388 of_unregister_platform_driver(&fs_enet_driver);
1392 static int __devinit fs_enet_probe(struct device *dev)
1394 struct net_device *ndev;
1396 /* no fixup - no device */
1397 if (dev->platform_data == NULL) {
1398 printk(KERN_INFO "fs_enet: "
1399 "probe called with no platform data; "
1400 "remove unused devices\n");
1404 ndev = fs_init_instance(dev, dev->platform_data);
1406 return PTR_ERR(ndev);
1410 static int fs_enet_remove(struct device *dev)
1412 return fs_cleanup_instance(dev_get_drvdata(dev));
1415 static struct device_driver fs_enet_fec_driver = {
1416 .name = "fsl-cpm-fec",
1417 .bus = &platform_bus_type,
1418 .probe = fs_enet_probe,
1419 .remove = fs_enet_remove,
1421 /* .suspend = fs_enet_suspend, TODO */
1422 /* .resume = fs_enet_resume, TODO */
1426 static struct device_driver fs_enet_scc_driver = {
1427 .name = "fsl-cpm-scc",
1428 .bus = &platform_bus_type,
1429 .probe = fs_enet_probe,
1430 .remove = fs_enet_remove,
1432 /* .suspend = fs_enet_suspend, TODO */
1433 /* .resume = fs_enet_resume, TODO */
1437 static struct device_driver fs_enet_fcc_driver = {
1438 .name = "fsl-cpm-fcc",
1439 .bus = &platform_bus_type,
1440 .probe = fs_enet_probe,
1441 .remove = fs_enet_remove,
1443 /* .suspend = fs_enet_suspend, TODO */
1444 /* .resume = fs_enet_resume, TODO */
1448 static int __init fs_init(void)
1459 #ifdef CONFIG_FS_ENET_HAS_FCC
1460 /* let's insert mii stuff */
1461 r = fs_enet_mdio_bb_init();
1464 printk(KERN_ERR DRV_MODULE_NAME
1465 "BB PHY init failed.\n");
1468 r = driver_register(&fs_enet_fcc_driver);
1473 #ifdef CONFIG_FS_ENET_HAS_FEC
1474 r = fs_enet_mdio_fec_init();
1476 printk(KERN_ERR DRV_MODULE_NAME
1477 "FEC PHY init failed.\n");
1481 r = driver_register(&fs_enet_fec_driver);
1486 #ifdef CONFIG_FS_ENET_HAS_SCC
1487 r = driver_register(&fs_enet_scc_driver);
1498 static void __exit fs_cleanup(void)
1500 driver_unregister(&fs_enet_fec_driver);
1501 driver_unregister(&fs_enet_fcc_driver);
1502 driver_unregister(&fs_enet_scc_driver);
1507 #ifdef CONFIG_NET_POLL_CONTROLLER
1508 static void fs_enet_netpoll(struct net_device *dev)
1510 disable_irq(dev->irq);
1511 fs_enet_interrupt(dev->irq, dev, NULL);
1512 enable_irq(dev->irq);
1516 /**************************************************************************************/
1518 module_init(fs_init);
1519 module_exit(fs_cleanup);