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>
39 #include <linux/of_platform.h>
41 #include <linux/vmalloc.h>
42 #include <asm/pgtable.h>
44 #include <asm/uaccess.h>
48 /*************************************************/
50 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
51 MODULE_DESCRIPTION("Freescale Ethernet Driver");
52 MODULE_LICENSE("GPL");
53 MODULE_VERSION(DRV_MODULE_VERSION);
55 static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
56 module_param(fs_enet_debug, int, 0);
57 MODULE_PARM_DESC(fs_enet_debug,
58 "Freescale bitmapped debugging message enable value");
60 #ifdef CONFIG_NET_POLL_CONTROLLER
61 static void fs_enet_netpoll(struct net_device *dev);
64 static void fs_set_multicast_list(struct net_device *dev)
66 struct fs_enet_private *fep = netdev_priv(dev);
68 (*fep->ops->set_multicast_list)(dev);
71 static void skb_align(struct sk_buff *skb, int align)
73 int off = ((unsigned long)skb->data) & (align - 1);
76 skb_reserve(skb, align - off);
79 /* NAPI receive function */
80 static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
82 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
83 struct net_device *dev = fep->ndev;
84 const struct fs_platform_info *fpi = fep->fpi;
86 struct sk_buff *skb, *skbn, *skbt;
92 * First, grab all of the stats for the incoming packet.
93 * These get messed up if we get called due to a busy condition.
97 /* clear RX status bits for napi*/
98 (*fep->ops->napi_clear_rx_event)(dev);
100 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),
140 skb = fep->rx_skbuff[curidx];
142 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
143 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
147 * Process the incoming frame.
149 fep->stats.rx_packets++;
150 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
151 fep->stats.rx_bytes += pkt_len + 4;
153 if (pkt_len <= fpi->rx_copybreak) {
154 /* +2 to make IP header L1 cache aligned */
155 skbn = dev_alloc_skb(pkt_len + 2);
157 skb_reserve(skbn, 2); /* align IP header */
158 skb_copy_from_linear_data(skb,
159 skbn->data, pkt_len);
166 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
169 skb_align(skbn, ENET_RX_ALIGN);
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);
203 if (received >= budget)
209 if (received < budget) {
211 netif_rx_complete(dev, napi);
212 (*fep->ops->napi_enable_rx)(dev);
217 /* non NAPI receive function */
218 static int fs_enet_rx_non_napi(struct net_device *dev)
220 struct fs_enet_private *fep = netdev_priv(dev);
221 const struct fs_platform_info *fpi = fep->fpi;
223 struct sk_buff *skb, *skbn, *skbt;
228 * First, grab all of the stats for the incoming packet.
229 * These get messed up if we get called due to a busy condition.
233 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
235 curidx = bdp - fep->rx_bd_base;
238 * Since we have allocated space to hold a complete frame,
239 * the last indicator should be set.
241 if ((sc & BD_ENET_RX_LAST) == 0)
242 printk(KERN_WARNING DRV_MODULE_NAME
243 ": %s rcv is not +last\n",
249 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
250 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
251 fep->stats.rx_errors++;
252 /* Frame too long or too short. */
253 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
254 fep->stats.rx_length_errors++;
255 /* Frame alignment */
256 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
257 fep->stats.rx_frame_errors++;
259 if (sc & BD_ENET_RX_CR)
260 fep->stats.rx_crc_errors++;
262 if (sc & BD_ENET_RX_OV)
263 fep->stats.rx_crc_errors++;
265 skb = fep->rx_skbuff[curidx];
267 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
268 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
275 skb = fep->rx_skbuff[curidx];
277 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
278 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
282 * Process the incoming frame.
284 fep->stats.rx_packets++;
285 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
286 fep->stats.rx_bytes += pkt_len + 4;
288 if (pkt_len <= fpi->rx_copybreak) {
289 /* +2 to make IP header L1 cache aligned */
290 skbn = dev_alloc_skb(pkt_len + 2);
292 skb_reserve(skbn, 2); /* align IP header */
293 skb_copy_from_linear_data(skb,
294 skbn->data, pkt_len);
301 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
304 skb_align(skbn, ENET_RX_ALIGN);
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->tx_lock);
355 do_wake = do_restart = 0;
356 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
357 dirtyidx = bdp - fep->tx_bd_base;
359 if (fep->tx_free == fep->tx_ring)
362 skb = fep->tx_skbuff[dirtyidx];
367 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
368 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
370 if (sc & BD_ENET_TX_HB) /* No heartbeat */
371 fep->stats.tx_heartbeat_errors++;
372 if (sc & BD_ENET_TX_LC) /* Late collision */
373 fep->stats.tx_window_errors++;
374 if (sc & BD_ENET_TX_RL) /* Retrans limit */
375 fep->stats.tx_aborted_errors++;
376 if (sc & BD_ENET_TX_UN) /* Underrun */
377 fep->stats.tx_fifo_errors++;
378 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
379 fep->stats.tx_carrier_errors++;
381 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
382 fep->stats.tx_errors++;
386 fep->stats.tx_packets++;
388 if (sc & BD_ENET_TX_READY)
389 printk(KERN_WARNING DRV_MODULE_NAME
390 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
394 * Deferred means some collisions occurred during transmit,
395 * but we eventually sent the packet OK.
397 if (sc & BD_ENET_TX_DEF)
398 fep->stats.collisions++;
401 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
402 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->tx_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)
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) {
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 = napi_schedule_prep(&fep->napi);
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, &fep->napi);
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 skb_align(skb, ENET_RX_ALIGN);
517 fep->rx_skbuff[i] = skb;
519 dma_map_single(fep->dev, skb->data,
520 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
522 CBDW_DATLEN(bdp, 0); /* zero */
523 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
524 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
527 * if we failed, fillup remainder
529 for (; i < fep->rx_ring; i++, bdp++) {
530 fep->rx_skbuff[i] = NULL;
531 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
535 * ...and the same for transmit.
537 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
538 fep->tx_skbuff[i] = NULL;
539 CBDW_BUFADDR(bdp, 0);
541 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
545 void fs_cleanup_bds(struct net_device *dev)
547 struct fs_enet_private *fep = netdev_priv(dev);
553 * Reset SKB transmit buffers.
555 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
556 if ((skb = fep->tx_skbuff[i]) == NULL)
560 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
561 skb->len, DMA_TO_DEVICE);
563 fep->tx_skbuff[i] = NULL;
568 * Reset SKB receive buffers
570 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
571 if ((skb = fep->rx_skbuff[i]) == NULL)
575 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
576 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
579 fep->rx_skbuff[i] = NULL;
585 /**********************************************************************************/
587 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
589 struct fs_enet_private *fep = netdev_priv(dev);
595 spin_lock_irqsave(&fep->tx_lock, flags);
598 * Fill in a Tx ring entry
602 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
603 netif_stop_queue(dev);
604 spin_unlock_irqrestore(&fep->tx_lock, flags);
607 * Ooops. All transmit buffers are full. Bail out.
608 * This should not happen, since the tx queue should be stopped.
610 printk(KERN_WARNING DRV_MODULE_NAME
611 ": %s tx queue full!.\n", dev->name);
612 return NETDEV_TX_BUSY;
615 curidx = bdp - fep->tx_bd_base;
617 * Clear all of the status flags.
619 CBDC_SC(bdp, BD_ENET_TX_STATS);
624 fep->tx_skbuff[curidx] = skb;
626 fep->stats.tx_bytes += skb->len;
629 * Push the data cache so the CPM does not get stale memory data.
631 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
632 skb->data, skb->len, DMA_TO_DEVICE));
633 CBDW_DATLEN(bdp, skb->len);
635 dev->trans_start = jiffies;
638 * If this was the last BD in the ring, start at the beginning again.
640 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
643 fep->cur_tx = fep->tx_bd_base;
646 netif_stop_queue(dev);
648 /* Trigger transmission start */
649 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
650 BD_ENET_TX_LAST | BD_ENET_TX_TC;
652 /* note that while FEC does not have this bit
653 * it marks it as available for software use
654 * yay for hw reuse :) */
656 sc |= BD_ENET_TX_PAD;
659 (*fep->ops->tx_kickstart)(dev);
661 spin_unlock_irqrestore(&fep->tx_lock, flags);
666 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
669 struct fs_enet_private *fep = netdev_priv(dev);
671 (*fep->ops->pre_request_irq)(dev, irq);
672 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
675 static void fs_free_irq(struct net_device *dev, int irq)
677 struct fs_enet_private *fep = netdev_priv(dev);
680 (*fep->ops->post_free_irq)(dev, irq);
683 static void fs_timeout(struct net_device *dev)
685 struct fs_enet_private *fep = netdev_priv(dev);
689 fep->stats.tx_errors++;
691 spin_lock_irqsave(&fep->lock, flags);
693 if (dev->flags & IFF_UP) {
694 phy_stop(fep->phydev);
695 (*fep->ops->stop)(dev);
696 (*fep->ops->restart)(dev);
697 phy_start(fep->phydev);
700 phy_start(fep->phydev);
701 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
702 spin_unlock_irqrestore(&fep->lock, flags);
705 netif_wake_queue(dev);
708 /*-----------------------------------------------------------------------------
709 * generic link-change handler - should be sufficient for most cases
710 *-----------------------------------------------------------------------------*/
711 static void generic_adjust_link(struct net_device *dev)
713 struct fs_enet_private *fep = netdev_priv(dev);
714 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);
739 } else if (fep->oldlink) {
744 netif_carrier_off(dev);
745 netif_stop_queue(dev);
748 if (new_state && netif_msg_link(fep))
749 phy_print_status(phydev);
753 static void fs_adjust_link(struct net_device *dev)
755 struct fs_enet_private *fep = netdev_priv(dev);
758 spin_lock_irqsave(&fep->lock, flags);
760 if(fep->ops->adjust_link)
761 fep->ops->adjust_link(dev);
763 generic_adjust_link(dev);
765 spin_unlock_irqrestore(&fep->lock, flags);
768 static int fs_init_phy(struct net_device *dev)
770 struct fs_enet_private *fep = netdev_priv(dev);
771 struct phy_device *phydev;
777 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
778 PHY_INTERFACE_MODE_MII);
780 printk("No phy bus ID specified in BSP code\n");
783 if (IS_ERR(phydev)) {
784 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
785 return PTR_ERR(phydev);
788 fep->phydev = phydev;
793 static int fs_enet_open(struct net_device *dev)
795 struct fs_enet_private *fep = netdev_priv(dev);
799 if (fep->fpi->use_napi)
800 napi_enable(&fep->napi);
802 /* Install our interrupt handler. */
803 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
805 printk(KERN_ERR DRV_MODULE_NAME
806 ": %s Could not allocate FS_ENET IRQ!", dev->name);
807 if (fep->fpi->use_napi)
808 napi_disable(&fep->napi);
812 err = fs_init_phy(dev);
814 if (fep->fpi->use_napi)
815 napi_disable(&fep->napi);
818 phy_start(fep->phydev);
823 static int fs_enet_close(struct net_device *dev)
825 struct fs_enet_private *fep = netdev_priv(dev);
828 netif_stop_queue(dev);
829 netif_carrier_off(dev);
830 if (fep->fpi->use_napi)
831 napi_disable(&fep->napi);
832 phy_stop(fep->phydev);
834 spin_lock_irqsave(&fep->lock, flags);
835 spin_lock(&fep->tx_lock);
836 (*fep->ops->stop)(dev);
837 spin_unlock(&fep->tx_lock);
838 spin_unlock_irqrestore(&fep->lock, flags);
840 /* release any irqs */
841 phy_disconnect(fep->phydev);
843 fs_free_irq(dev, fep->interrupt);
848 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
850 struct fs_enet_private *fep = netdev_priv(dev);
854 /*************************************************************************/
856 static void fs_get_drvinfo(struct net_device *dev,
857 struct ethtool_drvinfo *info)
859 strcpy(info->driver, DRV_MODULE_NAME);
860 strcpy(info->version, DRV_MODULE_VERSION);
863 static int fs_get_regs_len(struct net_device *dev)
865 struct fs_enet_private *fep = netdev_priv(dev);
867 return (*fep->ops->get_regs_len)(dev);
870 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
873 struct fs_enet_private *fep = netdev_priv(dev);
879 spin_lock_irqsave(&fep->lock, flags);
880 r = (*fep->ops->get_regs)(dev, p, &len);
881 spin_unlock_irqrestore(&fep->lock, flags);
887 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
889 struct fs_enet_private *fep = netdev_priv(dev);
894 return phy_ethtool_gset(fep->phydev, cmd);
897 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
899 struct fs_enet_private *fep = netdev_priv(dev);
904 return 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;
943 if (!netif_running(dev))
946 return phy_mii_ioctl(fep->phydev, mii, cmd);
949 extern int fs_mii_connect(struct net_device *dev);
950 extern void fs_mii_disconnect(struct net_device *dev);
952 /**************************************************************************************/
954 /* handy pointer to the immap */
955 void __iomem *fs_enet_immap = NULL;
957 static int setup_immap(void)
960 fs_enet_immap = ioremap(IMAP_ADDR, 0x4000);
961 WARN_ON(!fs_enet_immap);
962 #elif defined(CONFIG_CPM2)
963 fs_enet_immap = cpm2_immr;
969 static void cleanup_immap(void)
971 #if defined(CONFIG_CPM1)
972 iounmap(fs_enet_immap);
976 /**************************************************************************************/
978 static int __devinit find_phy(struct device_node *np,
979 struct fs_platform_info *fpi)
981 struct device_node *phynode, *mdionode;
986 data = of_get_property(np, "fixed-link", NULL);
988 snprintf(fpi->bus_id, 16, "%x:%02x", 0, *data);
992 data = of_get_property(np, "phy-handle", &len);
993 if (!data || len != 4)
996 phynode = of_find_node_by_phandle(*data);
1000 mdionode = of_get_parent(phynode);
1004 ret = of_address_to_resource(mdionode, 0, &res);
1008 data = of_get_property(phynode, "reg", &len);
1009 if (!data || len != 4)
1012 snprintf(fpi->bus_id, 16, "%x:%02x", res.start, *data);
1015 of_node_put(mdionode);
1017 of_node_put(phynode);
1021 #ifdef CONFIG_FS_ENET_HAS_FEC
1022 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
1024 #define IS_FEC(match) 0
1027 static int __devinit fs_enet_probe(struct of_device *ofdev,
1028 const struct of_device_id *match)
1030 struct net_device *ndev;
1031 struct fs_enet_private *fep;
1032 struct fs_platform_info *fpi;
1035 int privsize, len, ret = -ENODEV;
1037 fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
1041 if (!IS_FEC(match)) {
1042 data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
1043 if (!data || len != 4)
1046 fpi->cp_command = *data;
1051 fpi->rx_copybreak = 240;
1053 fpi->napi_weight = 17;
1055 ret = find_phy(ofdev->node, fpi);
1059 privsize = sizeof(*fep) +
1060 sizeof(struct sk_buff **) *
1061 (fpi->rx_ring + fpi->tx_ring);
1063 ndev = alloc_etherdev(privsize);
1069 dev_set_drvdata(&ofdev->dev, ndev);
1071 fep = netdev_priv(ndev);
1072 fep->dev = &ofdev->dev;
1075 fep->ops = match->data;
1077 ret = fep->ops->setup_data(ndev);
1081 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1082 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1084 spin_lock_init(&fep->lock);
1085 spin_lock_init(&fep->tx_lock);
1087 mac_addr = of_get_mac_address(ofdev->node);
1089 memcpy(ndev->dev_addr, mac_addr, 6);
1091 ret = fep->ops->allocate_bd(ndev);
1093 goto out_cleanup_data;
1095 fep->rx_bd_base = fep->ring_base;
1096 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1098 fep->tx_ring = fpi->tx_ring;
1099 fep->rx_ring = fpi->rx_ring;
1101 ndev->open = fs_enet_open;
1102 ndev->hard_start_xmit = fs_enet_start_xmit;
1103 ndev->tx_timeout = fs_timeout;
1104 ndev->watchdog_timeo = 2 * HZ;
1105 ndev->stop = fs_enet_close;
1106 ndev->get_stats = fs_enet_get_stats;
1107 ndev->set_multicast_list = fs_set_multicast_list;
1110 netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
1113 ndev->ethtool_ops = &fs_ethtool_ops;
1114 ndev->do_ioctl = fs_ioctl;
1116 init_timer(&fep->phy_timer_list);
1118 netif_carrier_off(ndev);
1120 ret = register_netdev(ndev);
1124 printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
1126 ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
1127 ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);
1132 fep->ops->free_bd(ndev);
1134 fep->ops->cleanup_data(ndev);
1137 dev_set_drvdata(&ofdev->dev, NULL);
1143 static int fs_enet_remove(struct of_device *ofdev)
1145 struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
1146 struct fs_enet_private *fep = netdev_priv(ndev);
1148 unregister_netdev(ndev);
1150 fep->ops->free_bd(ndev);
1151 fep->ops->cleanup_data(ndev);
1152 dev_set_drvdata(fep->dev, NULL);
1158 static struct of_device_id fs_enet_match[] = {
1159 #ifdef CONFIG_FS_ENET_HAS_SCC
1161 .compatible = "fsl,cpm1-scc-enet",
1162 .data = (void *)&fs_scc_ops,
1165 #ifdef CONFIG_FS_ENET_HAS_FCC
1167 .compatible = "fsl,cpm2-fcc-enet",
1168 .data = (void *)&fs_fcc_ops,
1171 #ifdef CONFIG_FS_ENET_HAS_FEC
1173 .compatible = "fsl,pq1-fec-enet",
1174 .data = (void *)&fs_fec_ops,
1180 static struct of_platform_driver fs_enet_driver = {
1182 .match_table = fs_enet_match,
1183 .probe = fs_enet_probe,
1184 .remove = fs_enet_remove,
1187 static int __init fs_init(void)
1189 int r = setup_immap();
1193 r = of_register_platform_driver(&fs_enet_driver);
1204 static void __exit fs_cleanup(void)
1206 of_unregister_platform_driver(&fs_enet_driver);
1210 #ifdef CONFIG_NET_POLL_CONTROLLER
1211 static void fs_enet_netpoll(struct net_device *dev)
1213 disable_irq(dev->irq);
1214 fs_enet_interrupt(dev->irq, dev, NULL);
1215 enable_irq(dev->irq);
1219 /**************************************************************************************/
1221 module_init(fs_init);
1222 module_exit(fs_cleanup);