Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
0003  *
0004  * Copyright (c) 2003 Intracom S.A.
0005  *  by Pantelis Antoniou <panto@intracom.gr>
0006  *
0007  * 2005 (c) MontaVista Software, Inc.
0008  * Vitaly Bordug <vbordug@ru.mvista.com>
0009  *
0010  * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
0011  * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
0012  *
0013  * This file is licensed under the terms of the GNU General Public License
0014  * version 2. This program is licensed "as is" without any warranty of any
0015  * kind, whether express or implied.
0016  */
0017 
0018 #include <linux/module.h>
0019 #include <linux/kernel.h>
0020 #include <linux/types.h>
0021 #include <linux/string.h>
0022 #include <linux/ptrace.h>
0023 #include <linux/errno.h>
0024 #include <linux/ioport.h>
0025 #include <linux/slab.h>
0026 #include <linux/interrupt.h>
0027 #include <linux/delay.h>
0028 #include <linux/netdevice.h>
0029 #include <linux/etherdevice.h>
0030 #include <linux/skbuff.h>
0031 #include <linux/spinlock.h>
0032 #include <linux/mii.h>
0033 #include <linux/ethtool.h>
0034 #include <linux/bitops.h>
0035 #include <linux/fs.h>
0036 #include <linux/platform_device.h>
0037 #include <linux/phy.h>
0038 #include <linux/of.h>
0039 #include <linux/of_mdio.h>
0040 #include <linux/of_platform.h>
0041 #include <linux/of_gpio.h>
0042 #include <linux/of_net.h>
0043 #include <linux/pgtable.h>
0044 
0045 #include <linux/vmalloc.h>
0046 #include <asm/irq.h>
0047 #include <linux/uaccess.h>
0048 
0049 #include "fs_enet.h"
0050 
0051 /*************************************************/
0052 
0053 MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
0054 MODULE_DESCRIPTION("Freescale Ethernet Driver");
0055 MODULE_LICENSE("GPL");
0056 
0057 static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
0058 module_param(fs_enet_debug, int, 0);
0059 MODULE_PARM_DESC(fs_enet_debug,
0060          "Freescale bitmapped debugging message enable value");
0061 
0062 #define RX_RING_SIZE    32
0063 #define TX_RING_SIZE    64
0064 
0065 #ifdef CONFIG_NET_POLL_CONTROLLER
0066 static void fs_enet_netpoll(struct net_device *dev);
0067 #endif
0068 
0069 static void fs_set_multicast_list(struct net_device *dev)
0070 {
0071     struct fs_enet_private *fep = netdev_priv(dev);
0072 
0073     (*fep->ops->set_multicast_list)(dev);
0074 }
0075 
0076 static void skb_align(struct sk_buff *skb, int align)
0077 {
0078     int off = ((unsigned long)skb->data) & (align - 1);
0079 
0080     if (off)
0081         skb_reserve(skb, align - off);
0082 }
0083 
0084 /* NAPI function */
0085 static int fs_enet_napi(struct napi_struct *napi, int budget)
0086 {
0087     struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
0088     struct net_device *dev = fep->ndev;
0089     const struct fs_platform_info *fpi = fep->fpi;
0090     cbd_t __iomem *bdp;
0091     struct sk_buff *skb, *skbn;
0092     int received = 0;
0093     u16 pkt_len, sc;
0094     int curidx;
0095     int dirtyidx, do_wake, do_restart;
0096     int tx_left = TX_RING_SIZE;
0097 
0098     spin_lock(&fep->tx_lock);
0099     bdp = fep->dirty_tx;
0100 
0101     /* clear status bits for napi*/
0102     (*fep->ops->napi_clear_event)(dev);
0103 
0104     do_wake = do_restart = 0;
0105     while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0 && tx_left) {
0106         dirtyidx = bdp - fep->tx_bd_base;
0107 
0108         if (fep->tx_free == fep->tx_ring)
0109             break;
0110 
0111         skb = fep->tx_skbuff[dirtyidx];
0112 
0113         /*
0114          * Check for errors.
0115          */
0116         if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
0117               BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
0118 
0119             if (sc & BD_ENET_TX_HB) /* No heartbeat */
0120                 dev->stats.tx_heartbeat_errors++;
0121             if (sc & BD_ENET_TX_LC) /* Late collision */
0122                 dev->stats.tx_window_errors++;
0123             if (sc & BD_ENET_TX_RL) /* Retrans limit */
0124                 dev->stats.tx_aborted_errors++;
0125             if (sc & BD_ENET_TX_UN) /* Underrun */
0126                 dev->stats.tx_fifo_errors++;
0127             if (sc & BD_ENET_TX_CSL)    /* Carrier lost */
0128                 dev->stats.tx_carrier_errors++;
0129 
0130             if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
0131                 dev->stats.tx_errors++;
0132                 do_restart = 1;
0133             }
0134         } else
0135             dev->stats.tx_packets++;
0136 
0137         if (sc & BD_ENET_TX_READY) {
0138             dev_warn(fep->dev,
0139                  "HEY! Enet xmit interrupt and TX_READY.\n");
0140         }
0141 
0142         /*
0143          * Deferred means some collisions occurred during transmit,
0144          * but we eventually sent the packet OK.
0145          */
0146         if (sc & BD_ENET_TX_DEF)
0147             dev->stats.collisions++;
0148 
0149         /* unmap */
0150         if (fep->mapped_as_page[dirtyidx])
0151             dma_unmap_page(fep->dev, CBDR_BUFADDR(bdp),
0152                        CBDR_DATLEN(bdp), DMA_TO_DEVICE);
0153         else
0154             dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
0155                      CBDR_DATLEN(bdp), DMA_TO_DEVICE);
0156 
0157         /*
0158          * Free the sk buffer associated with this last transmit.
0159          */
0160         if (skb) {
0161             dev_kfree_skb(skb);
0162             fep->tx_skbuff[dirtyidx] = NULL;
0163         }
0164 
0165         /*
0166          * Update pointer to next buffer descriptor to be transmitted.
0167          */
0168         if ((sc & BD_ENET_TX_WRAP) == 0)
0169             bdp++;
0170         else
0171             bdp = fep->tx_bd_base;
0172 
0173         /*
0174          * Since we have freed up a buffer, the ring is no longer
0175          * full.
0176          */
0177         if (++fep->tx_free == MAX_SKB_FRAGS)
0178             do_wake = 1;
0179         tx_left--;
0180     }
0181 
0182     fep->dirty_tx = bdp;
0183 
0184     if (do_restart)
0185         (*fep->ops->tx_restart)(dev);
0186 
0187     spin_unlock(&fep->tx_lock);
0188 
0189     if (do_wake)
0190         netif_wake_queue(dev);
0191 
0192     /*
0193      * First, grab all of the stats for the incoming packet.
0194      * These get messed up if we get called due to a busy condition.
0195      */
0196     bdp = fep->cur_rx;
0197 
0198     while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0 &&
0199            received < budget) {
0200         curidx = bdp - fep->rx_bd_base;
0201 
0202         /*
0203          * Since we have allocated space to hold a complete frame,
0204          * the last indicator should be set.
0205          */
0206         if ((sc & BD_ENET_RX_LAST) == 0)
0207             dev_warn(fep->dev, "rcv is not +last\n");
0208 
0209         /*
0210          * Check for errors.
0211          */
0212         if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
0213               BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
0214             dev->stats.rx_errors++;
0215             /* Frame too long or too short. */
0216             if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
0217                 dev->stats.rx_length_errors++;
0218             /* Frame alignment */
0219             if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
0220                 dev->stats.rx_frame_errors++;
0221             /* CRC Error */
0222             if (sc & BD_ENET_RX_CR)
0223                 dev->stats.rx_crc_errors++;
0224             /* FIFO overrun */
0225             if (sc & BD_ENET_RX_OV)
0226                 dev->stats.rx_crc_errors++;
0227 
0228             skbn = fep->rx_skbuff[curidx];
0229         } else {
0230             skb = fep->rx_skbuff[curidx];
0231 
0232             /*
0233              * Process the incoming frame.
0234              */
0235             dev->stats.rx_packets++;
0236             pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
0237             dev->stats.rx_bytes += pkt_len + 4;
0238 
0239             if (pkt_len <= fpi->rx_copybreak) {
0240                 /* +2 to make IP header L1 cache aligned */
0241                 skbn = netdev_alloc_skb(dev, pkt_len + 2);
0242                 if (skbn != NULL) {
0243                     skb_reserve(skbn, 2);   /* align IP header */
0244                     skb_copy_from_linear_data(skb,
0245                               skbn->data, pkt_len);
0246                     swap(skb, skbn);
0247                     dma_sync_single_for_cpu(fep->dev,
0248                         CBDR_BUFADDR(bdp),
0249                         L1_CACHE_ALIGN(pkt_len),
0250                         DMA_FROM_DEVICE);
0251                 }
0252             } else {
0253                 skbn = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
0254 
0255                 if (skbn) {
0256                     dma_addr_t dma;
0257 
0258                     skb_align(skbn, ENET_RX_ALIGN);
0259 
0260                     dma_unmap_single(fep->dev,
0261                         CBDR_BUFADDR(bdp),
0262                         L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
0263                         DMA_FROM_DEVICE);
0264 
0265                     dma = dma_map_single(fep->dev,
0266                         skbn->data,
0267                         L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
0268                         DMA_FROM_DEVICE);
0269                     CBDW_BUFADDR(bdp, dma);
0270                 }
0271             }
0272 
0273             if (skbn != NULL) {
0274                 skb_put(skb, pkt_len);  /* Make room */
0275                 skb->protocol = eth_type_trans(skb, dev);
0276                 received++;
0277                 netif_receive_skb(skb);
0278             } else {
0279                 dev->stats.rx_dropped++;
0280                 skbn = skb;
0281             }
0282         }
0283 
0284         fep->rx_skbuff[curidx] = skbn;
0285         CBDW_DATLEN(bdp, 0);
0286         CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
0287 
0288         /*
0289          * Update BD pointer to next entry.
0290          */
0291         if ((sc & BD_ENET_RX_WRAP) == 0)
0292             bdp++;
0293         else
0294             bdp = fep->rx_bd_base;
0295 
0296         (*fep->ops->rx_bd_done)(dev);
0297     }
0298 
0299     fep->cur_rx = bdp;
0300 
0301     if (received < budget && tx_left) {
0302         /* done */
0303         napi_complete_done(napi, received);
0304         (*fep->ops->napi_enable)(dev);
0305 
0306         return received;
0307     }
0308 
0309     return budget;
0310 }
0311 
0312 /*
0313  * The interrupt handler.
0314  * This is called from the MPC core interrupt.
0315  */
0316 static irqreturn_t
0317 fs_enet_interrupt(int irq, void *dev_id)
0318 {
0319     struct net_device *dev = dev_id;
0320     struct fs_enet_private *fep;
0321     const struct fs_platform_info *fpi;
0322     u32 int_events;
0323     u32 int_clr_events;
0324     int nr, napi_ok;
0325     int handled;
0326 
0327     fep = netdev_priv(dev);
0328     fpi = fep->fpi;
0329 
0330     nr = 0;
0331     while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
0332         nr++;
0333 
0334         int_clr_events = int_events;
0335         int_clr_events &= ~fep->ev_napi;
0336 
0337         (*fep->ops->clear_int_events)(dev, int_clr_events);
0338 
0339         if (int_events & fep->ev_err)
0340             (*fep->ops->ev_error)(dev, int_events);
0341 
0342         if (int_events & fep->ev) {
0343             napi_ok = napi_schedule_prep(&fep->napi);
0344 
0345             (*fep->ops->napi_disable)(dev);
0346             (*fep->ops->clear_int_events)(dev, fep->ev_napi);
0347 
0348             /* NOTE: it is possible for FCCs in NAPI mode    */
0349             /* to submit a spurious interrupt while in poll  */
0350             if (napi_ok)
0351                 __napi_schedule(&fep->napi);
0352         }
0353 
0354     }
0355 
0356     handled = nr > 0;
0357     return IRQ_RETVAL(handled);
0358 }
0359 
0360 void fs_init_bds(struct net_device *dev)
0361 {
0362     struct fs_enet_private *fep = netdev_priv(dev);
0363     cbd_t __iomem *bdp;
0364     struct sk_buff *skb;
0365     int i;
0366 
0367     fs_cleanup_bds(dev);
0368 
0369     fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
0370     fep->tx_free = fep->tx_ring;
0371     fep->cur_rx = fep->rx_bd_base;
0372 
0373     /*
0374      * Initialize the receive buffer descriptors.
0375      */
0376     for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
0377         skb = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
0378         if (skb == NULL)
0379             break;
0380 
0381         skb_align(skb, ENET_RX_ALIGN);
0382         fep->rx_skbuff[i] = skb;
0383         CBDW_BUFADDR(bdp,
0384             dma_map_single(fep->dev, skb->data,
0385                 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
0386                 DMA_FROM_DEVICE));
0387         CBDW_DATLEN(bdp, 0);    /* zero */
0388         CBDW_SC(bdp, BD_ENET_RX_EMPTY |
0389             ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
0390     }
0391     /*
0392      * if we failed, fillup remainder
0393      */
0394     for (; i < fep->rx_ring; i++, bdp++) {
0395         fep->rx_skbuff[i] = NULL;
0396         CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
0397     }
0398 
0399     /*
0400      * ...and the same for transmit.
0401      */
0402     for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
0403         fep->tx_skbuff[i] = NULL;
0404         CBDW_BUFADDR(bdp, 0);
0405         CBDW_DATLEN(bdp, 0);
0406         CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
0407     }
0408 }
0409 
0410 void fs_cleanup_bds(struct net_device *dev)
0411 {
0412     struct fs_enet_private *fep = netdev_priv(dev);
0413     struct sk_buff *skb;
0414     cbd_t __iomem *bdp;
0415     int i;
0416 
0417     /*
0418      * Reset SKB transmit buffers.
0419      */
0420     for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
0421         if ((skb = fep->tx_skbuff[i]) == NULL)
0422             continue;
0423 
0424         /* unmap */
0425         dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
0426                 skb->len, DMA_TO_DEVICE);
0427 
0428         fep->tx_skbuff[i] = NULL;
0429         dev_kfree_skb(skb);
0430     }
0431 
0432     /*
0433      * Reset SKB receive buffers
0434      */
0435     for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
0436         if ((skb = fep->rx_skbuff[i]) == NULL)
0437             continue;
0438 
0439         /* unmap */
0440         dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
0441             L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
0442             DMA_FROM_DEVICE);
0443 
0444         fep->rx_skbuff[i] = NULL;
0445 
0446         dev_kfree_skb(skb);
0447     }
0448 }
0449 
0450 /**********************************************************************************/
0451 
0452 #ifdef CONFIG_FS_ENET_MPC5121_FEC
0453 /*
0454  * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
0455  */
0456 static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
0457                            struct sk_buff *skb)
0458 {
0459     struct sk_buff *new_skb;
0460 
0461     if (skb_linearize(skb))
0462         return NULL;
0463 
0464     /* Alloc new skb */
0465     new_skb = netdev_alloc_skb(dev, skb->len + 4);
0466     if (!new_skb)
0467         return NULL;
0468 
0469     /* Make sure new skb is properly aligned */
0470     skb_align(new_skb, 4);
0471 
0472     /* Copy data to new skb ... */
0473     skb_copy_from_linear_data(skb, new_skb->data, skb->len);
0474     skb_put(new_skb, skb->len);
0475 
0476     /* ... and free an old one */
0477     dev_kfree_skb_any(skb);
0478 
0479     return new_skb;
0480 }
0481 #endif
0482 
0483 static netdev_tx_t
0484 fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
0485 {
0486     struct fs_enet_private *fep = netdev_priv(dev);
0487     cbd_t __iomem *bdp;
0488     int curidx;
0489     u16 sc;
0490     int nr_frags;
0491     skb_frag_t *frag;
0492     int len;
0493 #ifdef CONFIG_FS_ENET_MPC5121_FEC
0494     int is_aligned = 1;
0495     int i;
0496 
0497     if (!IS_ALIGNED((unsigned long)skb->data, 4)) {
0498         is_aligned = 0;
0499     } else {
0500         nr_frags = skb_shinfo(skb)->nr_frags;
0501         frag = skb_shinfo(skb)->frags;
0502         for (i = 0; i < nr_frags; i++, frag++) {
0503             if (!IS_ALIGNED(skb_frag_off(frag), 4)) {
0504                 is_aligned = 0;
0505                 break;
0506             }
0507         }
0508     }
0509 
0510     if (!is_aligned) {
0511         skb = tx_skb_align_workaround(dev, skb);
0512         if (!skb) {
0513             /*
0514              * We have lost packet due to memory allocation error
0515              * in tx_skb_align_workaround(). Hopefully original
0516              * skb is still valid, so try transmit it later.
0517              */
0518             return NETDEV_TX_BUSY;
0519         }
0520     }
0521 #endif
0522 
0523     spin_lock(&fep->tx_lock);
0524 
0525     /*
0526      * Fill in a Tx ring entry
0527      */
0528     bdp = fep->cur_tx;
0529 
0530     nr_frags = skb_shinfo(skb)->nr_frags;
0531     if (fep->tx_free <= nr_frags || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
0532         netif_stop_queue(dev);
0533         spin_unlock(&fep->tx_lock);
0534 
0535         /*
0536          * Ooops.  All transmit buffers are full.  Bail out.
0537          * This should not happen, since the tx queue should be stopped.
0538          */
0539         dev_warn(fep->dev, "tx queue full!.\n");
0540         return NETDEV_TX_BUSY;
0541     }
0542 
0543     curidx = bdp - fep->tx_bd_base;
0544 
0545     len = skb->len;
0546     dev->stats.tx_bytes += len;
0547     if (nr_frags)
0548         len -= skb->data_len;
0549     fep->tx_free -= nr_frags + 1;
0550     /*
0551      * Push the data cache so the CPM does not get stale memory data.
0552      */
0553     CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
0554                 skb->data, len, DMA_TO_DEVICE));
0555     CBDW_DATLEN(bdp, len);
0556 
0557     fep->mapped_as_page[curidx] = 0;
0558     frag = skb_shinfo(skb)->frags;
0559     while (nr_frags) {
0560         CBDC_SC(bdp,
0561             BD_ENET_TX_STATS | BD_ENET_TX_INTR | BD_ENET_TX_LAST |
0562             BD_ENET_TX_TC);
0563         CBDS_SC(bdp, BD_ENET_TX_READY);
0564 
0565         if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0) {
0566             bdp++;
0567             curidx++;
0568         } else {
0569             bdp = fep->tx_bd_base;
0570             curidx = 0;
0571         }
0572 
0573         len = skb_frag_size(frag);
0574         CBDW_BUFADDR(bdp, skb_frag_dma_map(fep->dev, frag, 0, len,
0575                            DMA_TO_DEVICE));
0576         CBDW_DATLEN(bdp, len);
0577 
0578         fep->tx_skbuff[curidx] = NULL;
0579         fep->mapped_as_page[curidx] = 1;
0580 
0581         frag++;
0582         nr_frags--;
0583     }
0584 
0585     /* Trigger transmission start */
0586     sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
0587          BD_ENET_TX_LAST | BD_ENET_TX_TC;
0588 
0589     /* note that while FEC does not have this bit
0590      * it marks it as available for software use
0591      * yay for hw reuse :) */
0592     if (skb->len <= 60)
0593         sc |= BD_ENET_TX_PAD;
0594     CBDC_SC(bdp, BD_ENET_TX_STATS);
0595     CBDS_SC(bdp, sc);
0596 
0597     /* Save skb pointer. */
0598     fep->tx_skbuff[curidx] = skb;
0599 
0600     /* If this was the last BD in the ring, start at the beginning again. */
0601     if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
0602         bdp++;
0603     else
0604         bdp = fep->tx_bd_base;
0605     fep->cur_tx = bdp;
0606 
0607     if (fep->tx_free < MAX_SKB_FRAGS)
0608         netif_stop_queue(dev);
0609 
0610     skb_tx_timestamp(skb);
0611 
0612     (*fep->ops->tx_kickstart)(dev);
0613 
0614     spin_unlock(&fep->tx_lock);
0615 
0616     return NETDEV_TX_OK;
0617 }
0618 
0619 static void fs_timeout_work(struct work_struct *work)
0620 {
0621     struct fs_enet_private *fep = container_of(work, struct fs_enet_private,
0622                            timeout_work);
0623     struct net_device *dev = fep->ndev;
0624     unsigned long flags;
0625     int wake = 0;
0626 
0627     dev->stats.tx_errors++;
0628 
0629     spin_lock_irqsave(&fep->lock, flags);
0630 
0631     if (dev->flags & IFF_UP) {
0632         phy_stop(dev->phydev);
0633         (*fep->ops->stop)(dev);
0634         (*fep->ops->restart)(dev);
0635     }
0636 
0637     phy_start(dev->phydev);
0638     wake = fep->tx_free >= MAX_SKB_FRAGS &&
0639            !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
0640     spin_unlock_irqrestore(&fep->lock, flags);
0641 
0642     if (wake)
0643         netif_wake_queue(dev);
0644 }
0645 
0646 static void fs_timeout(struct net_device *dev, unsigned int txqueue)
0647 {
0648     struct fs_enet_private *fep = netdev_priv(dev);
0649 
0650     schedule_work(&fep->timeout_work);
0651 }
0652 
0653 /*-----------------------------------------------------------------------------
0654  *  generic link-change handler - should be sufficient for most cases
0655  *-----------------------------------------------------------------------------*/
0656 static void generic_adjust_link(struct  net_device *dev)
0657 {
0658     struct fs_enet_private *fep = netdev_priv(dev);
0659     struct phy_device *phydev = dev->phydev;
0660     int new_state = 0;
0661 
0662     if (phydev->link) {
0663         /* adjust to duplex mode */
0664         if (phydev->duplex != fep->oldduplex) {
0665             new_state = 1;
0666             fep->oldduplex = phydev->duplex;
0667         }
0668 
0669         if (phydev->speed != fep->oldspeed) {
0670             new_state = 1;
0671             fep->oldspeed = phydev->speed;
0672         }
0673 
0674         if (!fep->oldlink) {
0675             new_state = 1;
0676             fep->oldlink = 1;
0677         }
0678 
0679         if (new_state)
0680             fep->ops->restart(dev);
0681     } else if (fep->oldlink) {
0682         new_state = 1;
0683         fep->oldlink = 0;
0684         fep->oldspeed = 0;
0685         fep->oldduplex = -1;
0686     }
0687 
0688     if (new_state && netif_msg_link(fep))
0689         phy_print_status(phydev);
0690 }
0691 
0692 
0693 static void fs_adjust_link(struct net_device *dev)
0694 {
0695     struct fs_enet_private *fep = netdev_priv(dev);
0696     unsigned long flags;
0697 
0698     spin_lock_irqsave(&fep->lock, flags);
0699 
0700     if(fep->ops->adjust_link)
0701         fep->ops->adjust_link(dev);
0702     else
0703         generic_adjust_link(dev);
0704 
0705     spin_unlock_irqrestore(&fep->lock, flags);
0706 }
0707 
0708 static int fs_init_phy(struct net_device *dev)
0709 {
0710     struct fs_enet_private *fep = netdev_priv(dev);
0711     struct phy_device *phydev;
0712     phy_interface_t iface;
0713 
0714     fep->oldlink = 0;
0715     fep->oldspeed = 0;
0716     fep->oldduplex = -1;
0717 
0718     iface = fep->fpi->use_rmii ?
0719         PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII;
0720 
0721     phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
0722                 iface);
0723     if (!phydev) {
0724         dev_err(&dev->dev, "Could not attach to PHY\n");
0725         return -ENODEV;
0726     }
0727 
0728     return 0;
0729 }
0730 
0731 static int fs_enet_open(struct net_device *dev)
0732 {
0733     struct fs_enet_private *fep = netdev_priv(dev);
0734     int r;
0735     int err;
0736 
0737     /* to initialize the fep->cur_rx,... */
0738     /* not doing this, will cause a crash in fs_enet_napi */
0739     fs_init_bds(fep->ndev);
0740 
0741     napi_enable(&fep->napi);
0742 
0743     /* Install our interrupt handler. */
0744     r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
0745             "fs_enet-mac", dev);
0746     if (r != 0) {
0747         dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
0748         napi_disable(&fep->napi);
0749         return -EINVAL;
0750     }
0751 
0752     err = fs_init_phy(dev);
0753     if (err) {
0754         free_irq(fep->interrupt, dev);
0755         napi_disable(&fep->napi);
0756         return err;
0757     }
0758     phy_start(dev->phydev);
0759 
0760     netif_start_queue(dev);
0761 
0762     return 0;
0763 }
0764 
0765 static int fs_enet_close(struct net_device *dev)
0766 {
0767     struct fs_enet_private *fep = netdev_priv(dev);
0768     unsigned long flags;
0769 
0770     netif_stop_queue(dev);
0771     netif_carrier_off(dev);
0772     napi_disable(&fep->napi);
0773     cancel_work_sync(&fep->timeout_work);
0774     phy_stop(dev->phydev);
0775 
0776     spin_lock_irqsave(&fep->lock, flags);
0777     spin_lock(&fep->tx_lock);
0778     (*fep->ops->stop)(dev);
0779     spin_unlock(&fep->tx_lock);
0780     spin_unlock_irqrestore(&fep->lock, flags);
0781 
0782     /* release any irqs */
0783     phy_disconnect(dev->phydev);
0784     free_irq(fep->interrupt, dev);
0785 
0786     return 0;
0787 }
0788 
0789 /*************************************************************************/
0790 
0791 static void fs_get_drvinfo(struct net_device *dev,
0792                 struct ethtool_drvinfo *info)
0793 {
0794     strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
0795 }
0796 
0797 static int fs_get_regs_len(struct net_device *dev)
0798 {
0799     struct fs_enet_private *fep = netdev_priv(dev);
0800 
0801     return (*fep->ops->get_regs_len)(dev);
0802 }
0803 
0804 static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
0805              void *p)
0806 {
0807     struct fs_enet_private *fep = netdev_priv(dev);
0808     unsigned long flags;
0809     int r, len;
0810 
0811     len = regs->len;
0812 
0813     spin_lock_irqsave(&fep->lock, flags);
0814     r = (*fep->ops->get_regs)(dev, p, &len);
0815     spin_unlock_irqrestore(&fep->lock, flags);
0816 
0817     if (r == 0)
0818         regs->version = 0;
0819 }
0820 
0821 static u32 fs_get_msglevel(struct net_device *dev)
0822 {
0823     struct fs_enet_private *fep = netdev_priv(dev);
0824     return fep->msg_enable;
0825 }
0826 
0827 static void fs_set_msglevel(struct net_device *dev, u32 value)
0828 {
0829     struct fs_enet_private *fep = netdev_priv(dev);
0830     fep->msg_enable = value;
0831 }
0832 
0833 static int fs_get_tunable(struct net_device *dev,
0834               const struct ethtool_tunable *tuna, void *data)
0835 {
0836     struct fs_enet_private *fep = netdev_priv(dev);
0837     struct fs_platform_info *fpi = fep->fpi;
0838     int ret = 0;
0839 
0840     switch (tuna->id) {
0841     case ETHTOOL_RX_COPYBREAK:
0842         *(u32 *)data = fpi->rx_copybreak;
0843         break;
0844     default:
0845         ret = -EINVAL;
0846         break;
0847     }
0848 
0849     return ret;
0850 }
0851 
0852 static int fs_set_tunable(struct net_device *dev,
0853               const struct ethtool_tunable *tuna, const void *data)
0854 {
0855     struct fs_enet_private *fep = netdev_priv(dev);
0856     struct fs_platform_info *fpi = fep->fpi;
0857     int ret = 0;
0858 
0859     switch (tuna->id) {
0860     case ETHTOOL_RX_COPYBREAK:
0861         fpi->rx_copybreak = *(u32 *)data;
0862         break;
0863     default:
0864         ret = -EINVAL;
0865         break;
0866     }
0867 
0868     return ret;
0869 }
0870 
0871 static const struct ethtool_ops fs_ethtool_ops = {
0872     .get_drvinfo = fs_get_drvinfo,
0873     .get_regs_len = fs_get_regs_len,
0874     .nway_reset = phy_ethtool_nway_reset,
0875     .get_link = ethtool_op_get_link,
0876     .get_msglevel = fs_get_msglevel,
0877     .set_msglevel = fs_set_msglevel,
0878     .get_regs = fs_get_regs,
0879     .get_ts_info = ethtool_op_get_ts_info,
0880     .get_link_ksettings = phy_ethtool_get_link_ksettings,
0881     .set_link_ksettings = phy_ethtool_set_link_ksettings,
0882     .get_tunable = fs_get_tunable,
0883     .set_tunable = fs_set_tunable,
0884 };
0885 
0886 extern int fs_mii_connect(struct net_device *dev);
0887 extern void fs_mii_disconnect(struct net_device *dev);
0888 
0889 /**************************************************************************************/
0890 
0891 #ifdef CONFIG_FS_ENET_HAS_FEC
0892 #define IS_FEC(match) ((match)->data == &fs_fec_ops)
0893 #else
0894 #define IS_FEC(match) 0
0895 #endif
0896 
0897 static const struct net_device_ops fs_enet_netdev_ops = {
0898     .ndo_open       = fs_enet_open,
0899     .ndo_stop       = fs_enet_close,
0900     .ndo_start_xmit     = fs_enet_start_xmit,
0901     .ndo_tx_timeout     = fs_timeout,
0902     .ndo_set_rx_mode    = fs_set_multicast_list,
0903     .ndo_eth_ioctl      = phy_do_ioctl_running,
0904     .ndo_validate_addr  = eth_validate_addr,
0905     .ndo_set_mac_address    = eth_mac_addr,
0906 #ifdef CONFIG_NET_POLL_CONTROLLER
0907     .ndo_poll_controller    = fs_enet_netpoll,
0908 #endif
0909 };
0910 
0911 static const struct of_device_id fs_enet_match[];
0912 static int fs_enet_probe(struct platform_device *ofdev)
0913 {
0914     const struct of_device_id *match;
0915     struct net_device *ndev;
0916     struct fs_enet_private *fep;
0917     struct fs_platform_info *fpi;
0918     const u32 *data;
0919     struct clk *clk;
0920     int err;
0921     const char *phy_connection_type;
0922     int privsize, len, ret = -ENODEV;
0923 
0924     match = of_match_device(fs_enet_match, &ofdev->dev);
0925     if (!match)
0926         return -EINVAL;
0927 
0928     fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
0929     if (!fpi)
0930         return -ENOMEM;
0931 
0932     if (!IS_FEC(match)) {
0933         data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
0934         if (!data || len != 4)
0935             goto out_free_fpi;
0936 
0937         fpi->cp_command = *data;
0938     }
0939 
0940     fpi->rx_ring = RX_RING_SIZE;
0941     fpi->tx_ring = TX_RING_SIZE;
0942     fpi->rx_copybreak = 240;
0943     fpi->napi_weight = 17;
0944     fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
0945     if (!fpi->phy_node && of_phy_is_fixed_link(ofdev->dev.of_node)) {
0946         err = of_phy_register_fixed_link(ofdev->dev.of_node);
0947         if (err)
0948             goto out_free_fpi;
0949 
0950         /* In the case of a fixed PHY, the DT node associated
0951          * to the PHY is the Ethernet MAC DT node.
0952          */
0953         fpi->phy_node = of_node_get(ofdev->dev.of_node);
0954     }
0955 
0956     if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) {
0957         phy_connection_type = of_get_property(ofdev->dev.of_node,
0958                         "phy-connection-type", NULL);
0959         if (phy_connection_type && !strcmp("rmii", phy_connection_type))
0960             fpi->use_rmii = 1;
0961     }
0962 
0963     /* make clock lookup non-fatal (the driver is shared among platforms),
0964      * but require enable to succeed when a clock was specified/found,
0965      * keep a reference to the clock upon successful acquisition
0966      */
0967     clk = devm_clk_get(&ofdev->dev, "per");
0968     if (!IS_ERR(clk)) {
0969         ret = clk_prepare_enable(clk);
0970         if (ret)
0971             goto out_deregister_fixed_link;
0972 
0973         fpi->clk_per = clk;
0974     }
0975 
0976     privsize = sizeof(*fep) +
0977                sizeof(struct sk_buff **) *
0978              (fpi->rx_ring + fpi->tx_ring) +
0979            sizeof(char) * fpi->tx_ring;
0980 
0981     ndev = alloc_etherdev(privsize);
0982     if (!ndev) {
0983         ret = -ENOMEM;
0984         goto out_put;
0985     }
0986 
0987     SET_NETDEV_DEV(ndev, &ofdev->dev);
0988     platform_set_drvdata(ofdev, ndev);
0989 
0990     fep = netdev_priv(ndev);
0991     fep->dev = &ofdev->dev;
0992     fep->ndev = ndev;
0993     fep->fpi = fpi;
0994     fep->ops = match->data;
0995 
0996     ret = fep->ops->setup_data(ndev);
0997     if (ret)
0998         goto out_free_dev;
0999 
1000     fep->rx_skbuff = (struct sk_buff **)&fep[1];
1001     fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1002     fep->mapped_as_page = (char *)(fep->rx_skbuff + fpi->rx_ring +
1003                        fpi->tx_ring);
1004 
1005     spin_lock_init(&fep->lock);
1006     spin_lock_init(&fep->tx_lock);
1007 
1008     of_get_ethdev_address(ofdev->dev.of_node, ndev);
1009 
1010     ret = fep->ops->allocate_bd(ndev);
1011     if (ret)
1012         goto out_cleanup_data;
1013 
1014     fep->rx_bd_base = fep->ring_base;
1015     fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1016 
1017     fep->tx_ring = fpi->tx_ring;
1018     fep->rx_ring = fpi->rx_ring;
1019 
1020     ndev->netdev_ops = &fs_enet_netdev_ops;
1021     ndev->watchdog_timeo = 2 * HZ;
1022     INIT_WORK(&fep->timeout_work, fs_timeout_work);
1023     netif_napi_add_weight(ndev, &fep->napi, fs_enet_napi,
1024                   fpi->napi_weight);
1025 
1026     ndev->ethtool_ops = &fs_ethtool_ops;
1027 
1028     netif_carrier_off(ndev);
1029 
1030     ndev->features |= NETIF_F_SG;
1031 
1032     ret = register_netdev(ndev);
1033     if (ret)
1034         goto out_free_bd;
1035 
1036     pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
1037 
1038     return 0;
1039 
1040 out_free_bd:
1041     fep->ops->free_bd(ndev);
1042 out_cleanup_data:
1043     fep->ops->cleanup_data(ndev);
1044 out_free_dev:
1045     free_netdev(ndev);
1046 out_put:
1047     clk_disable_unprepare(fpi->clk_per);
1048 out_deregister_fixed_link:
1049     of_node_put(fpi->phy_node);
1050     if (of_phy_is_fixed_link(ofdev->dev.of_node))
1051         of_phy_deregister_fixed_link(ofdev->dev.of_node);
1052 out_free_fpi:
1053     kfree(fpi);
1054     return ret;
1055 }
1056 
1057 static int fs_enet_remove(struct platform_device *ofdev)
1058 {
1059     struct net_device *ndev = platform_get_drvdata(ofdev);
1060     struct fs_enet_private *fep = netdev_priv(ndev);
1061 
1062     unregister_netdev(ndev);
1063 
1064     fep->ops->free_bd(ndev);
1065     fep->ops->cleanup_data(ndev);
1066     dev_set_drvdata(fep->dev, NULL);
1067     of_node_put(fep->fpi->phy_node);
1068     clk_disable_unprepare(fep->fpi->clk_per);
1069     if (of_phy_is_fixed_link(ofdev->dev.of_node))
1070         of_phy_deregister_fixed_link(ofdev->dev.of_node);
1071     free_netdev(ndev);
1072     return 0;
1073 }
1074 
1075 static const struct of_device_id fs_enet_match[] = {
1076 #ifdef CONFIG_FS_ENET_HAS_SCC
1077     {
1078         .compatible = "fsl,cpm1-scc-enet",
1079         .data = (void *)&fs_scc_ops,
1080     },
1081     {
1082         .compatible = "fsl,cpm2-scc-enet",
1083         .data = (void *)&fs_scc_ops,
1084     },
1085 #endif
1086 #ifdef CONFIG_FS_ENET_HAS_FCC
1087     {
1088         .compatible = "fsl,cpm2-fcc-enet",
1089         .data = (void *)&fs_fcc_ops,
1090     },
1091 #endif
1092 #ifdef CONFIG_FS_ENET_HAS_FEC
1093 #ifdef CONFIG_FS_ENET_MPC5121_FEC
1094     {
1095         .compatible = "fsl,mpc5121-fec",
1096         .data = (void *)&fs_fec_ops,
1097     },
1098     {
1099         .compatible = "fsl,mpc5125-fec",
1100         .data = (void *)&fs_fec_ops,
1101     },
1102 #else
1103     {
1104         .compatible = "fsl,pq1-fec-enet",
1105         .data = (void *)&fs_fec_ops,
1106     },
1107 #endif
1108 #endif
1109     {}
1110 };
1111 MODULE_DEVICE_TABLE(of, fs_enet_match);
1112 
1113 static struct platform_driver fs_enet_driver = {
1114     .driver = {
1115         .name = "fs_enet",
1116         .of_match_table = fs_enet_match,
1117     },
1118     .probe = fs_enet_probe,
1119     .remove = fs_enet_remove,
1120 };
1121 
1122 #ifdef CONFIG_NET_POLL_CONTROLLER
1123 static void fs_enet_netpoll(struct net_device *dev)
1124 {
1125        disable_irq(dev->irq);
1126        fs_enet_interrupt(dev->irq, dev);
1127        enable_irq(dev->irq);
1128 }
1129 #endif
1130 
1131 module_platform_driver(fs_enet_driver);