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/pgtable.h>
45 #include <asm/uaccess.h>
49 /*************************************************/
51 static char version[] __devinitdata =
52 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n";
54 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
55 MODULE_DESCRIPTION("Freescale Ethernet Driver");
56 MODULE_LICENSE("GPL");
57 MODULE_VERSION(DRV_MODULE_VERSION);
59 int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
60 module_param(fs_enet_debug, int, 0);
61 MODULE_PARM_DESC(fs_enet_debug,
62 "Freescale bitmapped debugging message enable value");
65 static void fs_set_multicast_list(struct net_device *dev)
67 struct fs_enet_private *fep = netdev_priv(dev);
69 (*fep->ops->set_multicast_list)(dev);
72 /* NAPI receive function */
73 static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
75 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
76 struct net_device *dev = to_net_dev(fep->dev);
77 const struct fs_platform_info *fpi = fep->fpi;
79 struct sk_buff *skb, *skbn, *skbt;
84 if (!netif_running(dev))
88 * First, grab all of the stats for the incoming packet.
89 * These get messed up if we get called due to a busy condition.
93 /* clear RX status bits for napi*/
94 (*fep->ops->napi_clear_rx_event)(dev);
96 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
97 curidx = bdp - fep->rx_bd_base;
100 * Since we have allocated space to hold a complete frame,
101 * the last indicator should be set.
103 if ((sc & BD_ENET_RX_LAST) == 0)
104 printk(KERN_WARNING DRV_MODULE_NAME
105 ": %s rcv is not +last\n",
111 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
112 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
113 fep->stats.rx_errors++;
114 /* Frame too long or too short. */
115 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
116 fep->stats.rx_length_errors++;
117 /* Frame alignment */
118 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
119 fep->stats.rx_frame_errors++;
121 if (sc & BD_ENET_RX_CR)
122 fep->stats.rx_crc_errors++;
124 if (sc & BD_ENET_RX_OV)
125 fep->stats.rx_crc_errors++;
127 skb = fep->rx_skbuff[curidx];
129 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
130 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
136 skb = fep->rx_skbuff[curidx];
138 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
139 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
143 * Process the incoming frame.
145 fep->stats.rx_packets++;
146 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
147 fep->stats.rx_bytes += pkt_len + 4;
149 if (pkt_len <= fpi->rx_copybreak) {
150 /* +2 to make IP header L1 cache aligned */
151 skbn = dev_alloc_skb(pkt_len + 2);
153 skb_reserve(skbn, 2); /* align IP header */
154 skb_copy_from_linear_data(skb,
155 skbn->data, pkt_len);
162 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
165 skb_put(skb, pkt_len); /* Make room */
166 skb->protocol = eth_type_trans(skb, dev);
168 netif_receive_skb(skb);
170 printk(KERN_WARNING DRV_MODULE_NAME
171 ": %s Memory squeeze, dropping packet.\n",
173 fep->stats.rx_dropped++;
178 fep->rx_skbuff[curidx] = skbn;
179 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
180 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
183 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
186 * Update BD pointer to next entry.
188 if ((sc & BD_ENET_RX_WRAP) == 0)
191 bdp = fep->rx_bd_base;
193 (*fep->ops->rx_bd_done)(dev);
195 if (received >= budget)
201 if (received >= budget) {
203 netif_rx_complete(dev, napi);
204 (*fep->ops->napi_enable_rx)(dev);
209 /* non NAPI receive function */
210 static int fs_enet_rx_non_napi(struct net_device *dev)
212 struct fs_enet_private *fep = netdev_priv(dev);
213 const struct fs_platform_info *fpi = fep->fpi;
215 struct sk_buff *skb, *skbn, *skbt;
220 * First, grab all of the stats for the incoming packet.
221 * These get messed up if we get called due to a busy condition.
225 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
227 curidx = bdp - fep->rx_bd_base;
230 * Since we have allocated space to hold a complete frame,
231 * the last indicator should be set.
233 if ((sc & BD_ENET_RX_LAST) == 0)
234 printk(KERN_WARNING DRV_MODULE_NAME
235 ": %s rcv is not +last\n",
241 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
242 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
243 fep->stats.rx_errors++;
244 /* Frame too long or too short. */
245 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
246 fep->stats.rx_length_errors++;
247 /* Frame alignment */
248 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
249 fep->stats.rx_frame_errors++;
251 if (sc & BD_ENET_RX_CR)
252 fep->stats.rx_crc_errors++;
254 if (sc & BD_ENET_RX_OV)
255 fep->stats.rx_crc_errors++;
257 skb = fep->rx_skbuff[curidx];
259 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
260 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
267 skb = fep->rx_skbuff[curidx];
269 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
270 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
274 * Process the incoming frame.
276 fep->stats.rx_packets++;
277 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
278 fep->stats.rx_bytes += pkt_len + 4;
280 if (pkt_len <= fpi->rx_copybreak) {
281 /* +2 to make IP header L1 cache aligned */
282 skbn = dev_alloc_skb(pkt_len + 2);
284 skb_reserve(skbn, 2); /* align IP header */
285 skb_copy_from_linear_data(skb,
286 skbn->data, pkt_len);
293 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
296 skb_put(skb, pkt_len); /* Make room */
297 skb->protocol = eth_type_trans(skb, dev);
301 printk(KERN_WARNING DRV_MODULE_NAME
302 ": %s Memory squeeze, dropping packet.\n",
304 fep->stats.rx_dropped++;
309 fep->rx_skbuff[curidx] = skbn;
310 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
311 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
314 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
317 * Update BD pointer to next entry.
319 if ((sc & BD_ENET_RX_WRAP) == 0)
322 bdp = fep->rx_bd_base;
324 (*fep->ops->rx_bd_done)(dev);
332 static void fs_enet_tx(struct net_device *dev)
334 struct fs_enet_private *fep = netdev_priv(dev);
337 int dirtyidx, do_wake, do_restart;
340 spin_lock(&fep->lock);
343 do_wake = do_restart = 0;
344 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
346 dirtyidx = bdp - fep->tx_bd_base;
348 if (fep->tx_free == fep->tx_ring)
351 skb = fep->tx_skbuff[dirtyidx];
356 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
357 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
359 if (sc & BD_ENET_TX_HB) /* No heartbeat */
360 fep->stats.tx_heartbeat_errors++;
361 if (sc & BD_ENET_TX_LC) /* Late collision */
362 fep->stats.tx_window_errors++;
363 if (sc & BD_ENET_TX_RL) /* Retrans limit */
364 fep->stats.tx_aborted_errors++;
365 if (sc & BD_ENET_TX_UN) /* Underrun */
366 fep->stats.tx_fifo_errors++;
367 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
368 fep->stats.tx_carrier_errors++;
370 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
371 fep->stats.tx_errors++;
375 fep->stats.tx_packets++;
377 if (sc & BD_ENET_TX_READY)
378 printk(KERN_WARNING DRV_MODULE_NAME
379 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
383 * Deferred means some collisions occurred during transmit,
384 * but we eventually sent the packet OK.
386 if (sc & BD_ENET_TX_DEF)
387 fep->stats.collisions++;
390 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
391 skb->len, DMA_TO_DEVICE);
394 * Free the sk buffer associated with this last transmit.
396 dev_kfree_skb_irq(skb);
397 fep->tx_skbuff[dirtyidx] = NULL;
400 * Update pointer to next buffer descriptor to be transmitted.
402 if ((sc & BD_ENET_TX_WRAP) == 0)
405 bdp = fep->tx_bd_base;
408 * Since we have freed up a buffer, the ring is no longer
418 (*fep->ops->tx_restart)(dev);
420 spin_unlock(&fep->lock);
423 netif_wake_queue(dev);
427 * The interrupt handler.
428 * This is called from the MPC core interrupt.
431 fs_enet_interrupt(int irq, void *dev_id)
433 struct net_device *dev = dev_id;
434 struct fs_enet_private *fep;
435 const struct fs_platform_info *fpi;
441 fep = netdev_priv(dev);
445 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
449 int_clr_events = int_events;
451 int_clr_events &= ~fep->ev_napi_rx;
453 (*fep->ops->clear_int_events)(dev, int_clr_events);
455 if (int_events & fep->ev_err)
456 (*fep->ops->ev_error)(dev, int_events);
458 if (int_events & fep->ev_rx) {
460 fs_enet_rx_non_napi(dev);
462 napi_ok = napi_schedule_prep(&fep->napi);
464 (*fep->ops->napi_disable_rx)(dev);
465 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
467 /* NOTE: it is possible for FCCs in NAPI mode */
468 /* to submit a spurious interrupt while in poll */
470 __netif_rx_schedule(dev, &fep->napi);
474 if (int_events & fep->ev_tx)
479 return IRQ_RETVAL(handled);
482 void fs_init_bds(struct net_device *dev)
484 struct fs_enet_private *fep = netdev_priv(dev);
491 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
492 fep->tx_free = fep->tx_ring;
493 fep->cur_rx = fep->rx_bd_base;
496 * Initialize the receive buffer descriptors.
498 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
499 skb = dev_alloc_skb(ENET_RX_FRSIZE);
501 printk(KERN_WARNING DRV_MODULE_NAME
502 ": %s Memory squeeze, unable to allocate skb\n",
506 fep->rx_skbuff[i] = skb;
508 dma_map_single(fep->dev, skb->data,
509 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
511 CBDW_DATLEN(bdp, 0); /* zero */
512 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
513 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
516 * if we failed, fillup remainder
518 for (; i < fep->rx_ring; i++, bdp++) {
519 fep->rx_skbuff[i] = NULL;
520 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
524 * ...and the same for transmit.
526 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
527 fep->tx_skbuff[i] = NULL;
528 CBDW_BUFADDR(bdp, 0);
530 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
534 void fs_cleanup_bds(struct net_device *dev)
536 struct fs_enet_private *fep = netdev_priv(dev);
542 * Reset SKB transmit buffers.
544 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
545 if ((skb = fep->tx_skbuff[i]) == NULL)
549 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
550 skb->len, DMA_TO_DEVICE);
552 fep->tx_skbuff[i] = NULL;
557 * Reset SKB receive buffers
559 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
560 if ((skb = fep->rx_skbuff[i]) == NULL)
564 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
565 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
568 fep->rx_skbuff[i] = NULL;
574 /**********************************************************************************/
576 static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
578 struct fs_enet_private *fep = netdev_priv(dev);
584 spin_lock_irqsave(&fep->tx_lock, flags);
587 * Fill in a Tx ring entry
591 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
592 netif_stop_queue(dev);
593 spin_unlock_irqrestore(&fep->tx_lock, flags);
596 * Ooops. All transmit buffers are full. Bail out.
597 * This should not happen, since the tx queue should be stopped.
599 printk(KERN_WARNING DRV_MODULE_NAME
600 ": %s tx queue full!.\n", dev->name);
601 return NETDEV_TX_BUSY;
604 curidx = bdp - fep->tx_bd_base;
606 * Clear all of the status flags.
608 CBDC_SC(bdp, BD_ENET_TX_STATS);
613 fep->tx_skbuff[curidx] = skb;
615 fep->stats.tx_bytes += skb->len;
618 * Push the data cache so the CPM does not get stale memory data.
620 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
621 skb->data, skb->len, DMA_TO_DEVICE));
622 CBDW_DATLEN(bdp, skb->len);
624 dev->trans_start = jiffies;
627 * If this was the last BD in the ring, start at the beginning again.
629 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
632 fep->cur_tx = fep->tx_bd_base;
635 netif_stop_queue(dev);
637 /* Trigger transmission start */
638 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
639 BD_ENET_TX_LAST | BD_ENET_TX_TC;
641 /* note that while FEC does not have this bit
642 * it marks it as available for software use
643 * yay for hw reuse :) */
645 sc |= BD_ENET_TX_PAD;
648 (*fep->ops->tx_kickstart)(dev);
650 spin_unlock_irqrestore(&fep->tx_lock, flags);
655 static int fs_request_irq(struct net_device *dev, int irq, const char *name,
658 struct fs_enet_private *fep = netdev_priv(dev);
660 (*fep->ops->pre_request_irq)(dev, irq);
661 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
664 static void fs_free_irq(struct net_device *dev, int irq)
666 struct fs_enet_private *fep = netdev_priv(dev);
669 (*fep->ops->post_free_irq)(dev, irq);
672 static void fs_timeout(struct net_device *dev)
674 struct fs_enet_private *fep = netdev_priv(dev);
678 fep->stats.tx_errors++;
680 spin_lock_irqsave(&fep->lock, flags);
682 if (dev->flags & IFF_UP) {
683 phy_stop(fep->phydev);
684 (*fep->ops->stop)(dev);
685 (*fep->ops->restart)(dev);
686 phy_start(fep->phydev);
689 phy_start(fep->phydev);
690 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
691 spin_unlock_irqrestore(&fep->lock, flags);
694 netif_wake_queue(dev);
697 /*-----------------------------------------------------------------------------
698 * generic link-change handler - should be sufficient for most cases
699 *-----------------------------------------------------------------------------*/
700 static void generic_adjust_link(struct net_device *dev)
702 struct fs_enet_private *fep = netdev_priv(dev);
703 struct phy_device *phydev = fep->phydev;
708 /* adjust to duplex mode */
709 if (phydev->duplex != fep->oldduplex){
711 fep->oldduplex = phydev->duplex;
714 if (phydev->speed != fep->oldspeed) {
716 fep->oldspeed = phydev->speed;
723 netif_carrier_on(dev);
724 netif_start_queue(dev);
728 fep->ops->restart(dev);
730 } else if (fep->oldlink) {
735 netif_carrier_off(dev);
736 netif_stop_queue(dev);
739 if (new_state && netif_msg_link(fep))
740 phy_print_status(phydev);
744 static void fs_adjust_link(struct net_device *dev)
746 struct fs_enet_private *fep = netdev_priv(dev);
749 spin_lock_irqsave(&fep->lock, flags);
751 if(fep->ops->adjust_link)
752 fep->ops->adjust_link(dev);
754 generic_adjust_link(dev);
756 spin_unlock_irqrestore(&fep->lock, flags);
759 static int fs_init_phy(struct net_device *dev)
761 struct fs_enet_private *fep = netdev_priv(dev);
762 struct phy_device *phydev;
768 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
769 PHY_INTERFACE_MODE_MII);
771 printk("No phy bus ID specified in BSP code\n");
774 if (IS_ERR(phydev)) {
775 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
776 return PTR_ERR(phydev);
779 fep->phydev = phydev;
785 static int fs_enet_open(struct net_device *dev)
787 struct fs_enet_private *fep = netdev_priv(dev);
791 napi_enable(&fep->napi);
793 /* Install our interrupt handler. */
794 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
796 printk(KERN_ERR DRV_MODULE_NAME
797 ": %s Could not allocate FS_ENET IRQ!", dev->name);
798 napi_disable(&fep->napi);
802 err = fs_init_phy(dev);
804 napi_disable(&fep->napi);
807 phy_start(fep->phydev);
812 static int fs_enet_close(struct net_device *dev)
814 struct fs_enet_private *fep = netdev_priv(dev);
817 netif_stop_queue(dev);
818 netif_carrier_off(dev);
819 napi_disable(&fep->napi);
820 phy_stop(fep->phydev);
822 spin_lock_irqsave(&fep->lock, flags);
823 (*fep->ops->stop)(dev);
824 spin_unlock_irqrestore(&fep->lock, flags);
826 /* release any irqs */
827 phy_disconnect(fep->phydev);
829 fs_free_irq(dev, fep->interrupt);
834 static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
836 struct fs_enet_private *fep = netdev_priv(dev);
840 /*************************************************************************/
842 static void fs_get_drvinfo(struct net_device *dev,
843 struct ethtool_drvinfo *info)
845 strcpy(info->driver, DRV_MODULE_NAME);
846 strcpy(info->version, DRV_MODULE_VERSION);
849 static int fs_get_regs_len(struct net_device *dev)
851 struct fs_enet_private *fep = netdev_priv(dev);
853 return (*fep->ops->get_regs_len)(dev);
856 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
859 struct fs_enet_private *fep = netdev_priv(dev);
865 spin_lock_irqsave(&fep->lock, flags);
866 r = (*fep->ops->get_regs)(dev, p, &len);
867 spin_unlock_irqrestore(&fep->lock, flags);
873 static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
875 struct fs_enet_private *fep = netdev_priv(dev);
876 return phy_ethtool_gset(fep->phydev, cmd);
879 static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
881 struct fs_enet_private *fep = netdev_priv(dev);
882 phy_ethtool_sset(fep->phydev, cmd);
886 static int fs_nway_reset(struct net_device *dev)
891 static u32 fs_get_msglevel(struct net_device *dev)
893 struct fs_enet_private *fep = netdev_priv(dev);
894 return fep->msg_enable;
897 static void fs_set_msglevel(struct net_device *dev, u32 value)
899 struct fs_enet_private *fep = netdev_priv(dev);
900 fep->msg_enable = value;
903 static const struct ethtool_ops fs_ethtool_ops = {
904 .get_drvinfo = fs_get_drvinfo,
905 .get_regs_len = fs_get_regs_len,
906 .get_settings = fs_get_settings,
907 .set_settings = fs_set_settings,
908 .nway_reset = fs_nway_reset,
909 .get_link = ethtool_op_get_link,
910 .get_msglevel = fs_get_msglevel,
911 .set_msglevel = fs_set_msglevel,
912 .get_tx_csum = ethtool_op_get_tx_csum,
913 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
914 .get_sg = ethtool_op_get_sg,
915 .set_sg = ethtool_op_set_sg,
916 .get_regs = fs_get_regs,
919 static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
921 struct fs_enet_private *fep = netdev_priv(dev);
922 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
926 if (!netif_running(dev))
929 spin_lock_irqsave(&fep->lock, flags);
930 rc = phy_mii_ioctl(fep->phydev, mii, cmd);
931 spin_unlock_irqrestore(&fep->lock, flags);
935 extern int fs_mii_connect(struct net_device *dev);
936 extern void fs_mii_disconnect(struct net_device *dev);
938 static struct net_device *fs_init_instance(struct device *dev,
939 struct fs_platform_info *fpi)
941 struct net_device *ndev = NULL;
942 struct fs_enet_private *fep = NULL;
943 int privsize, i, r, err = 0, registered = 0;
945 fpi->fs_no = fs_get_id(fpi);
947 if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX)
948 return ERR_PTR(-EINVAL);
950 privsize = sizeof(*fep) + (sizeof(struct sk_buff **) *
951 (fpi->rx_ring + fpi->tx_ring));
953 ndev = alloc_etherdev(privsize);
958 SET_MODULE_OWNER(ndev);
960 fep = netdev_priv(ndev);
961 memset(fep, 0, privsize); /* clear everything */
964 dev_set_drvdata(dev, ndev);
966 if (fpi->init_ioports)
967 fpi->init_ioports((struct fs_platform_info *)fpi);
969 #ifdef CONFIG_FS_ENET_HAS_FEC
970 if (fs_get_fec_index(fpi->fs_no) >= 0)
971 fep->ops = &fs_fec_ops;
974 #ifdef CONFIG_FS_ENET_HAS_SCC
975 if (fs_get_scc_index(fpi->fs_no) >=0 )
976 fep->ops = &fs_scc_ops;
979 #ifdef CONFIG_FS_ENET_HAS_FCC
980 if (fs_get_fcc_index(fpi->fs_no) >= 0)
981 fep->ops = &fs_fcc_ops;
984 if (fep->ops == NULL) {
985 printk(KERN_ERR DRV_MODULE_NAME
986 ": %s No matching ops found (%d).\n",
987 ndev->name, fpi->fs_no);
992 r = (*fep->ops->setup_data)(ndev);
994 printk(KERN_ERR DRV_MODULE_NAME
995 ": %s setup_data failed\n",
1001 /* point rx_skbuff, tx_skbuff */
1002 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1003 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1006 spin_lock_init(&fep->lock);
1007 spin_lock_init(&fep->tx_lock);
1010 * Set the Ethernet address.
1012 for (i = 0; i < 6; i++)
1013 ndev->dev_addr[i] = fpi->macaddr[i];
1015 r = (*fep->ops->allocate_bd)(ndev);
1017 if (fep->ring_base == NULL) {
1018 printk(KERN_ERR DRV_MODULE_NAME
1019 ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r);
1025 * Set receive and transmit descriptor base.
1027 fep->rx_bd_base = fep->ring_base;
1028 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1030 /* initialize ring size variables */
1031 fep->tx_ring = fpi->tx_ring;
1032 fep->rx_ring = fpi->rx_ring;
1035 * The FEC Ethernet specific entries in the device structure.
1037 ndev->open = fs_enet_open;
1038 ndev->hard_start_xmit = fs_enet_start_xmit;
1039 ndev->tx_timeout = fs_timeout;
1040 ndev->watchdog_timeo = 2 * HZ;
1041 ndev->stop = fs_enet_close;
1042 ndev->get_stats = fs_enet_get_stats;
1043 ndev->set_multicast_list = fs_set_multicast_list;
1044 netif_napi_add(ndev, &fep->napi,
1045 fs_enet_rx_napi, fpi->napi_weight);
1047 ndev->ethtool_ops = &fs_ethtool_ops;
1048 ndev->do_ioctl = fs_ioctl;
1050 init_timer(&fep->phy_timer_list);
1052 netif_carrier_off(ndev);
1054 err = register_netdev(ndev);
1056 printk(KERN_ERR DRV_MODULE_NAME
1057 ": %s register_netdev failed.\n", ndev->name);
1069 unregister_netdev(ndev);
1072 (*fep->ops->free_bd)(ndev);
1073 (*fep->ops->cleanup_data)(ndev);
1079 dev_set_drvdata(dev, NULL);
1081 return ERR_PTR(err);
1084 static int fs_cleanup_instance(struct net_device *ndev)
1086 struct fs_enet_private *fep;
1087 const struct fs_platform_info *fpi;
1093 fep = netdev_priv(ndev);
1099 unregister_netdev(ndev);
1101 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1102 fep->ring_base, fep->ring_mem_addr);
1105 (*fep->ops->cleanup_data)(ndev);
1109 dev_set_drvdata(dev, NULL);
1118 /**************************************************************************************/
1120 /* handy pointer to the immap */
1121 void *fs_enet_immap = NULL;
1123 static int setup_immap(void)
1125 phys_addr_t paddr = 0;
1126 unsigned long size = 0;
1130 size = 0x10000; /* map 64K */
1134 paddr = CPM_MAP_ADDR;
1135 size = 0x40000; /* map 256 K */
1137 fs_enet_immap = ioremap(paddr, size);
1138 if (fs_enet_immap == NULL)
1139 return -EBADF; /* XXX ahem; maybe just BUG_ON? */
1144 static void cleanup_immap(void)
1146 if (fs_enet_immap != NULL) {
1147 iounmap(fs_enet_immap);
1148 fs_enet_immap = NULL;
1152 /**************************************************************************************/
1154 static int __devinit fs_enet_probe(struct device *dev)
1156 struct net_device *ndev;
1158 /* no fixup - no device */
1159 if (dev->platform_data == NULL) {
1160 printk(KERN_INFO "fs_enet: "
1161 "probe called with no platform data; "
1162 "remove unused devices\n");
1166 ndev = fs_init_instance(dev, dev->platform_data);
1168 return PTR_ERR(ndev);
1172 static int fs_enet_remove(struct device *dev)
1174 return fs_cleanup_instance(dev_get_drvdata(dev));
1177 static struct device_driver fs_enet_fec_driver = {
1178 .name = "fsl-cpm-fec",
1179 .bus = &platform_bus_type,
1180 .probe = fs_enet_probe,
1181 .remove = fs_enet_remove,
1183 /* .suspend = fs_enet_suspend, TODO */
1184 /* .resume = fs_enet_resume, TODO */
1188 static struct device_driver fs_enet_scc_driver = {
1189 .name = "fsl-cpm-scc",
1190 .bus = &platform_bus_type,
1191 .probe = fs_enet_probe,
1192 .remove = fs_enet_remove,
1194 /* .suspend = fs_enet_suspend, TODO */
1195 /* .resume = fs_enet_resume, TODO */
1199 static struct device_driver fs_enet_fcc_driver = {
1200 .name = "fsl-cpm-fcc",
1201 .bus = &platform_bus_type,
1202 .probe = fs_enet_probe,
1203 .remove = fs_enet_remove,
1205 /* .suspend = fs_enet_suspend, TODO */
1206 /* .resume = fs_enet_resume, TODO */
1210 static int __init fs_init(void)
1221 #ifdef CONFIG_FS_ENET_HAS_FCC
1222 /* let's insert mii stuff */
1223 r = fs_enet_mdio_bb_init();
1226 printk(KERN_ERR DRV_MODULE_NAME
1227 "BB PHY init failed.\n");
1230 r = driver_register(&fs_enet_fcc_driver);
1235 #ifdef CONFIG_FS_ENET_HAS_FEC
1236 r = fs_enet_mdio_fec_init();
1238 printk(KERN_ERR DRV_MODULE_NAME
1239 "FEC PHY init failed.\n");
1243 r = driver_register(&fs_enet_fec_driver);
1248 #ifdef CONFIG_FS_ENET_HAS_SCC
1249 r = driver_register(&fs_enet_scc_driver);
1261 static void __exit fs_cleanup(void)
1263 driver_unregister(&fs_enet_fec_driver);
1264 driver_unregister(&fs_enet_fcc_driver);
1265 driver_unregister(&fs_enet_scc_driver);
1269 /**************************************************************************************/
1271 module_init(fs_init);
1272 module_exit(fs_cleanup);