Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Fast Ethernet Controller (ENET) PTP driver for MX6x.
0004  *
0005  * Copyright (C) 2012 Freescale Semiconductor, Inc.
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/string.h>
0013 #include <linux/ptrace.h>
0014 #include <linux/errno.h>
0015 #include <linux/ioport.h>
0016 #include <linux/slab.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/pci.h>
0019 #include <linux/delay.h>
0020 #include <linux/netdevice.h>
0021 #include <linux/etherdevice.h>
0022 #include <linux/skbuff.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/workqueue.h>
0025 #include <linux/bitops.h>
0026 #include <linux/io.h>
0027 #include <linux/irq.h>
0028 #include <linux/clk.h>
0029 #include <linux/platform_device.h>
0030 #include <linux/phy.h>
0031 #include <linux/fec.h>
0032 #include <linux/of.h>
0033 #include <linux/of_device.h>
0034 #include <linux/of_gpio.h>
0035 #include <linux/of_net.h>
0036 
0037 #include "fec.h"
0038 
0039 /* FEC 1588 register bits */
0040 #define FEC_T_CTRL_SLAVE                0x00002000
0041 #define FEC_T_CTRL_CAPTURE              0x00000800
0042 #define FEC_T_CTRL_RESTART              0x00000200
0043 #define FEC_T_CTRL_PERIOD_RST           0x00000030
0044 #define FEC_T_CTRL_PERIOD_EN        0x00000010
0045 #define FEC_T_CTRL_ENABLE               0x00000001
0046 
0047 #define FEC_T_INC_MASK                  0x0000007f
0048 #define FEC_T_INC_OFFSET                0
0049 #define FEC_T_INC_CORR_MASK             0x00007f00
0050 #define FEC_T_INC_CORR_OFFSET           8
0051 
0052 #define FEC_T_CTRL_PINPER       0x00000080
0053 #define FEC_T_TF0_MASK          0x00000001
0054 #define FEC_T_TF0_OFFSET        0
0055 #define FEC_T_TF1_MASK          0x00000002
0056 #define FEC_T_TF1_OFFSET        1
0057 #define FEC_T_TF2_MASK          0x00000004
0058 #define FEC_T_TF2_OFFSET        2
0059 #define FEC_T_TF3_MASK          0x00000008
0060 #define FEC_T_TF3_OFFSET        3
0061 #define FEC_T_TDRE_MASK         0x00000001
0062 #define FEC_T_TDRE_OFFSET       0
0063 #define FEC_T_TMODE_MASK        0x0000003C
0064 #define FEC_T_TMODE_OFFSET      2
0065 #define FEC_T_TIE_MASK          0x00000040
0066 #define FEC_T_TIE_OFFSET        6
0067 #define FEC_T_TF_MASK           0x00000080
0068 #define FEC_T_TF_OFFSET         7
0069 
0070 #define FEC_ATIME_CTRL      0x400
0071 #define FEC_ATIME       0x404
0072 #define FEC_ATIME_EVT_OFFSET    0x408
0073 #define FEC_ATIME_EVT_PERIOD    0x40c
0074 #define FEC_ATIME_CORR      0x410
0075 #define FEC_ATIME_INC       0x414
0076 #define FEC_TS_TIMESTAMP    0x418
0077 
0078 #define FEC_TGSR        0x604
0079 #define FEC_TCSR(n)     (0x608 + n * 0x08)
0080 #define FEC_TCCR(n)     (0x60C + n * 0x08)
0081 #define MAX_TIMER_CHANNEL   3
0082 #define FEC_TMODE_TOGGLE    0x05
0083 #define FEC_HIGH_PULSE      0x0F
0084 
0085 #define FEC_CC_MULT (1 << 31)
0086 #define FEC_COUNTER_PERIOD  (1 << 31)
0087 #define PPS_OUPUT_RELOAD_PERIOD NSEC_PER_SEC
0088 #define FEC_CHANNLE_0       0
0089 #define DEFAULT_PPS_CHANNEL FEC_CHANNLE_0
0090 
0091 /**
0092  * fec_ptp_enable_pps
0093  * @fep: the fec_enet_private structure handle
0094  * @enable: enable the channel pps output
0095  *
0096  * This function enble the PPS ouput on the timer channel.
0097  */
0098 static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable)
0099 {
0100     unsigned long flags;
0101     u32 val, tempval;
0102     struct timespec64 ts;
0103     u64 ns;
0104 
0105     if (fep->pps_enable == enable)
0106         return 0;
0107 
0108     fep->pps_channel = DEFAULT_PPS_CHANNEL;
0109     fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
0110 
0111     spin_lock_irqsave(&fep->tmreg_lock, flags);
0112 
0113     if (enable) {
0114         /* clear capture or output compare interrupt status if have.
0115          */
0116         writel(FEC_T_TF_MASK, fep->hwp + FEC_TCSR(fep->pps_channel));
0117 
0118         /* It is recommended to double check the TMODE field in the
0119          * TCSR register to be cleared before the first compare counter
0120          * is written into TCCR register. Just add a double check.
0121          */
0122         val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
0123         do {
0124             val &= ~(FEC_T_TMODE_MASK);
0125             writel(val, fep->hwp + FEC_TCSR(fep->pps_channel));
0126             val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
0127         } while (val & FEC_T_TMODE_MASK);
0128 
0129         /* Dummy read counter to update the counter */
0130         timecounter_read(&fep->tc);
0131         /* We want to find the first compare event in the next
0132          * second point. So we need to know what the ptp time
0133          * is now and how many nanoseconds is ahead to get next second.
0134          * The remaining nanosecond ahead before the next second would be
0135          * NSEC_PER_SEC - ts.tv_nsec. Add the remaining nanoseconds
0136          * to current timer would be next second.
0137          */
0138         tempval = fep->cc.read(&fep->cc);
0139         /* Convert the ptp local counter to 1588 timestamp */
0140         ns = timecounter_cyc2time(&fep->tc, tempval);
0141         ts = ns_to_timespec64(ns);
0142 
0143         /* The tempval is  less than 3 seconds, and  so val is less than
0144          * 4 seconds. No overflow for 32bit calculation.
0145          */
0146         val = NSEC_PER_SEC - (u32)ts.tv_nsec + tempval;
0147 
0148         /* Need to consider the situation that the current time is
0149          * very close to the second point, which means NSEC_PER_SEC
0150          * - ts.tv_nsec is close to be zero(For example 20ns); Since the timer
0151          * is still running when we calculate the first compare event, it is
0152          * possible that the remaining nanoseonds run out before the compare
0153          * counter is calculated and written into TCCR register. To avoid
0154          * this possibility, we will set the compare event to be the next
0155          * of next second. The current setting is 31-bit timer and wrap
0156          * around over 2 seconds. So it is okay to set the next of next
0157          * seond for the timer.
0158          */
0159         val += NSEC_PER_SEC;
0160 
0161         /* We add (2 * NSEC_PER_SEC - (u32)ts.tv_nsec) to current
0162          * ptp counter, which maybe cause 32-bit wrap. Since the
0163          * (NSEC_PER_SEC - (u32)ts.tv_nsec) is less than 2 second.
0164          * We can ensure the wrap will not cause issue. If the offset
0165          * is bigger than fep->cc.mask would be a error.
0166          */
0167         val &= fep->cc.mask;
0168         writel(val, fep->hwp + FEC_TCCR(fep->pps_channel));
0169 
0170         /* Calculate the second the compare event timestamp */
0171         fep->next_counter = (val + fep->reload_period) & fep->cc.mask;
0172 
0173         /* * Enable compare event when overflow */
0174         val = readl(fep->hwp + FEC_ATIME_CTRL);
0175         val |= FEC_T_CTRL_PINPER;
0176         writel(val, fep->hwp + FEC_ATIME_CTRL);
0177 
0178         /* Compare channel setting. */
0179         val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
0180         val |= (1 << FEC_T_TF_OFFSET | 1 << FEC_T_TIE_OFFSET);
0181         val &= ~(1 << FEC_T_TDRE_OFFSET);
0182         val &= ~(FEC_T_TMODE_MASK);
0183         val |= (FEC_HIGH_PULSE << FEC_T_TMODE_OFFSET);
0184         writel(val, fep->hwp + FEC_TCSR(fep->pps_channel));
0185 
0186         /* Write the second compare event timestamp and calculate
0187          * the third timestamp. Refer the TCCR register detail in the spec.
0188          */
0189         writel(fep->next_counter, fep->hwp + FEC_TCCR(fep->pps_channel));
0190         fep->next_counter = (fep->next_counter + fep->reload_period) & fep->cc.mask;
0191     } else {
0192         writel(0, fep->hwp + FEC_TCSR(fep->pps_channel));
0193     }
0194 
0195     fep->pps_enable = enable;
0196     spin_unlock_irqrestore(&fep->tmreg_lock, flags);
0197 
0198     return 0;
0199 }
0200 
0201 /**
0202  * fec_ptp_read - read raw cycle counter (to be used by time counter)
0203  * @cc: the cyclecounter structure
0204  *
0205  * this function reads the cyclecounter registers and is called by the
0206  * cyclecounter structure used to construct a ns counter from the
0207  * arbitrary fixed point registers
0208  */
0209 static u64 fec_ptp_read(const struct cyclecounter *cc)
0210 {
0211     struct fec_enet_private *fep =
0212         container_of(cc, struct fec_enet_private, cc);
0213     u32 tempval;
0214 
0215     tempval = readl(fep->hwp + FEC_ATIME_CTRL);
0216     tempval |= FEC_T_CTRL_CAPTURE;
0217     writel(tempval, fep->hwp + FEC_ATIME_CTRL);
0218 
0219     if (fep->quirks & FEC_QUIRK_BUG_CAPTURE)
0220         udelay(1);
0221 
0222     return readl(fep->hwp + FEC_ATIME);
0223 }
0224 
0225 /**
0226  * fec_ptp_start_cyclecounter - create the cycle counter from hw
0227  * @ndev: network device
0228  *
0229  * this function initializes the timecounter and cyclecounter
0230  * structures for use in generated a ns counter from the arbitrary
0231  * fixed point cycles registers in the hardware.
0232  */
0233 void fec_ptp_start_cyclecounter(struct net_device *ndev)
0234 {
0235     struct fec_enet_private *fep = netdev_priv(ndev);
0236     unsigned long flags;
0237     int inc;
0238 
0239     inc = 1000000000 / fep->cycle_speed;
0240 
0241     /* grab the ptp lock */
0242     spin_lock_irqsave(&fep->tmreg_lock, flags);
0243 
0244     /* 1ns counter */
0245     writel(inc << FEC_T_INC_OFFSET, fep->hwp + FEC_ATIME_INC);
0246 
0247     /* use 31-bit timer counter */
0248     writel(FEC_COUNTER_PERIOD, fep->hwp + FEC_ATIME_EVT_PERIOD);
0249 
0250     writel(FEC_T_CTRL_ENABLE | FEC_T_CTRL_PERIOD_RST,
0251         fep->hwp + FEC_ATIME_CTRL);
0252 
0253     memset(&fep->cc, 0, sizeof(fep->cc));
0254     fep->cc.read = fec_ptp_read;
0255     fep->cc.mask = CLOCKSOURCE_MASK(31);
0256     fep->cc.shift = 31;
0257     fep->cc.mult = FEC_CC_MULT;
0258 
0259     /* reset the ns time counter */
0260     timecounter_init(&fep->tc, &fep->cc, 0);
0261 
0262     spin_unlock_irqrestore(&fep->tmreg_lock, flags);
0263 }
0264 
0265 /**
0266  * fec_ptp_adjfreq - adjust ptp cycle frequency
0267  * @ptp: the ptp clock structure
0268  * @ppb: parts per billion adjustment from base
0269  *
0270  * Adjust the frequency of the ptp cycle counter by the
0271  * indicated ppb from the base frequency.
0272  *
0273  * Because ENET hardware frequency adjust is complex,
0274  * using software method to do that.
0275  */
0276 static int fec_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
0277 {
0278     unsigned long flags;
0279     int neg_adj = 0;
0280     u32 i, tmp;
0281     u32 corr_inc, corr_period;
0282     u32 corr_ns;
0283     u64 lhs, rhs;
0284 
0285     struct fec_enet_private *fep =
0286         container_of(ptp, struct fec_enet_private, ptp_caps);
0287 
0288     if (ppb == 0)
0289         return 0;
0290 
0291     if (ppb < 0) {
0292         ppb = -ppb;
0293         neg_adj = 1;
0294     }
0295 
0296     /* In theory, corr_inc/corr_period = ppb/NSEC_PER_SEC;
0297      * Try to find the corr_inc  between 1 to fep->ptp_inc to
0298      * meet adjustment requirement.
0299      */
0300     lhs = NSEC_PER_SEC;
0301     rhs = (u64)ppb * (u64)fep->ptp_inc;
0302     for (i = 1; i <= fep->ptp_inc; i++) {
0303         if (lhs >= rhs) {
0304             corr_inc = i;
0305             corr_period = div_u64(lhs, rhs);
0306             break;
0307         }
0308         lhs += NSEC_PER_SEC;
0309     }
0310     /* Not found? Set it to high value - double speed
0311      * correct in every clock step.
0312      */
0313     if (i > fep->ptp_inc) {
0314         corr_inc = fep->ptp_inc;
0315         corr_period = 1;
0316     }
0317 
0318     if (neg_adj)
0319         corr_ns = fep->ptp_inc - corr_inc;
0320     else
0321         corr_ns = fep->ptp_inc + corr_inc;
0322 
0323     spin_lock_irqsave(&fep->tmreg_lock, flags);
0324 
0325     tmp = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_MASK;
0326     tmp |= corr_ns << FEC_T_INC_CORR_OFFSET;
0327     writel(tmp, fep->hwp + FEC_ATIME_INC);
0328     corr_period = corr_period > 1 ? corr_period - 1 : corr_period;
0329     writel(corr_period, fep->hwp + FEC_ATIME_CORR);
0330     /* dummy read to update the timer. */
0331     timecounter_read(&fep->tc);
0332 
0333     spin_unlock_irqrestore(&fep->tmreg_lock, flags);
0334 
0335     return 0;
0336 }
0337 
0338 /**
0339  * fec_ptp_adjtime
0340  * @ptp: the ptp clock structure
0341  * @delta: offset to adjust the cycle counter by
0342  *
0343  * adjust the timer by resetting the timecounter structure.
0344  */
0345 static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
0346 {
0347     struct fec_enet_private *fep =
0348         container_of(ptp, struct fec_enet_private, ptp_caps);
0349     unsigned long flags;
0350 
0351     spin_lock_irqsave(&fep->tmreg_lock, flags);
0352     timecounter_adjtime(&fep->tc, delta);
0353     spin_unlock_irqrestore(&fep->tmreg_lock, flags);
0354 
0355     return 0;
0356 }
0357 
0358 /**
0359  * fec_ptp_gettime
0360  * @ptp: the ptp clock structure
0361  * @ts: timespec structure to hold the current time value
0362  *
0363  * read the timecounter and return the correct value on ns,
0364  * after converting it into a struct timespec.
0365  */
0366 static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
0367 {
0368     struct fec_enet_private *adapter =
0369         container_of(ptp, struct fec_enet_private, ptp_caps);
0370     u64 ns;
0371     unsigned long flags;
0372 
0373     mutex_lock(&adapter->ptp_clk_mutex);
0374     /* Check the ptp clock */
0375     if (!adapter->ptp_clk_on) {
0376         mutex_unlock(&adapter->ptp_clk_mutex);
0377         return -EINVAL;
0378     }
0379     spin_lock_irqsave(&adapter->tmreg_lock, flags);
0380     ns = timecounter_read(&adapter->tc);
0381     spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
0382     mutex_unlock(&adapter->ptp_clk_mutex);
0383 
0384     *ts = ns_to_timespec64(ns);
0385 
0386     return 0;
0387 }
0388 
0389 /**
0390  * fec_ptp_settime
0391  * @ptp: the ptp clock structure
0392  * @ts: the timespec containing the new time for the cycle counter
0393  *
0394  * reset the timecounter to use a new base value instead of the kernel
0395  * wall timer value.
0396  */
0397 static int fec_ptp_settime(struct ptp_clock_info *ptp,
0398                const struct timespec64 *ts)
0399 {
0400     struct fec_enet_private *fep =
0401         container_of(ptp, struct fec_enet_private, ptp_caps);
0402 
0403     u64 ns;
0404     unsigned long flags;
0405     u32 counter;
0406 
0407     mutex_lock(&fep->ptp_clk_mutex);
0408     /* Check the ptp clock */
0409     if (!fep->ptp_clk_on) {
0410         mutex_unlock(&fep->ptp_clk_mutex);
0411         return -EINVAL;
0412     }
0413 
0414     ns = timespec64_to_ns(ts);
0415     /* Get the timer value based on timestamp.
0416      * Update the counter with the masked value.
0417      */
0418     counter = ns & fep->cc.mask;
0419 
0420     spin_lock_irqsave(&fep->tmreg_lock, flags);
0421     writel(counter, fep->hwp + FEC_ATIME);
0422     timecounter_init(&fep->tc, &fep->cc, ns);
0423     spin_unlock_irqrestore(&fep->tmreg_lock, flags);
0424     mutex_unlock(&fep->ptp_clk_mutex);
0425     return 0;
0426 }
0427 
0428 /**
0429  * fec_ptp_enable
0430  * @ptp: the ptp clock structure
0431  * @rq: the requested feature to change
0432  * @on: whether to enable or disable the feature
0433  *
0434  */
0435 static int fec_ptp_enable(struct ptp_clock_info *ptp,
0436               struct ptp_clock_request *rq, int on)
0437 {
0438     struct fec_enet_private *fep =
0439         container_of(ptp, struct fec_enet_private, ptp_caps);
0440     int ret = 0;
0441 
0442     if (rq->type == PTP_CLK_REQ_PPS) {
0443         ret = fec_ptp_enable_pps(fep, on);
0444 
0445         return ret;
0446     }
0447     return -EOPNOTSUPP;
0448 }
0449 
0450 /**
0451  * fec_ptp_disable_hwts - disable hardware time stamping
0452  * @ndev: pointer to net_device
0453  */
0454 void fec_ptp_disable_hwts(struct net_device *ndev)
0455 {
0456     struct fec_enet_private *fep = netdev_priv(ndev);
0457 
0458     fep->hwts_tx_en = 0;
0459     fep->hwts_rx_en = 0;
0460 }
0461 
0462 int fec_ptp_set(struct net_device *ndev, struct ifreq *ifr)
0463 {
0464     struct fec_enet_private *fep = netdev_priv(ndev);
0465 
0466     struct hwtstamp_config config;
0467 
0468     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
0469         return -EFAULT;
0470 
0471     switch (config.tx_type) {
0472     case HWTSTAMP_TX_OFF:
0473         fep->hwts_tx_en = 0;
0474         break;
0475     case HWTSTAMP_TX_ON:
0476         fep->hwts_tx_en = 1;
0477         break;
0478     default:
0479         return -ERANGE;
0480     }
0481 
0482     switch (config.rx_filter) {
0483     case HWTSTAMP_FILTER_NONE:
0484         fep->hwts_rx_en = 0;
0485         break;
0486 
0487     default:
0488         fep->hwts_rx_en = 1;
0489         config.rx_filter = HWTSTAMP_FILTER_ALL;
0490         break;
0491     }
0492 
0493     return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
0494         -EFAULT : 0;
0495 }
0496 
0497 int fec_ptp_get(struct net_device *ndev, struct ifreq *ifr)
0498 {
0499     struct fec_enet_private *fep = netdev_priv(ndev);
0500     struct hwtstamp_config config;
0501 
0502     config.flags = 0;
0503     config.tx_type = fep->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
0504     config.rx_filter = (fep->hwts_rx_en ?
0505                 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
0506 
0507     return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
0508         -EFAULT : 0;
0509 }
0510 
0511 /*
0512  * fec_time_keep - call timecounter_read every second to avoid timer overrun
0513  *                 because ENET just support 32bit counter, will timeout in 4s
0514  */
0515 static void fec_time_keep(struct work_struct *work)
0516 {
0517     struct delayed_work *dwork = to_delayed_work(work);
0518     struct fec_enet_private *fep = container_of(dwork, struct fec_enet_private, time_keep);
0519     unsigned long flags;
0520 
0521     mutex_lock(&fep->ptp_clk_mutex);
0522     if (fep->ptp_clk_on) {
0523         spin_lock_irqsave(&fep->tmreg_lock, flags);
0524         timecounter_read(&fep->tc);
0525         spin_unlock_irqrestore(&fep->tmreg_lock, flags);
0526     }
0527     mutex_unlock(&fep->ptp_clk_mutex);
0528 
0529     schedule_delayed_work(&fep->time_keep, HZ);
0530 }
0531 
0532 /* This function checks the pps event and reloads the timer compare counter. */
0533 static irqreturn_t fec_pps_interrupt(int irq, void *dev_id)
0534 {
0535     struct net_device *ndev = dev_id;
0536     struct fec_enet_private *fep = netdev_priv(ndev);
0537     u32 val;
0538     u8 channel = fep->pps_channel;
0539     struct ptp_clock_event event;
0540 
0541     val = readl(fep->hwp + FEC_TCSR(channel));
0542     if (val & FEC_T_TF_MASK) {
0543         /* Write the next next compare(not the next according the spec)
0544          * value to the register
0545          */
0546         writel(fep->next_counter, fep->hwp + FEC_TCCR(channel));
0547         do {
0548             writel(val, fep->hwp + FEC_TCSR(channel));
0549         } while (readl(fep->hwp + FEC_TCSR(channel)) & FEC_T_TF_MASK);
0550 
0551         /* Update the counter; */
0552         fep->next_counter = (fep->next_counter + fep->reload_period) &
0553                 fep->cc.mask;
0554 
0555         event.type = PTP_CLOCK_PPS;
0556         ptp_clock_event(fep->ptp_clock, &event);
0557         return IRQ_HANDLED;
0558     }
0559 
0560     return IRQ_NONE;
0561 }
0562 
0563 /**
0564  * fec_ptp_init
0565  * @pdev: The FEC network adapter
0566  * @irq_idx: the interrupt index
0567  *
0568  * This function performs the required steps for enabling ptp
0569  * support. If ptp support has already been loaded it simply calls the
0570  * cyclecounter init routine and exits.
0571  */
0572 
0573 void fec_ptp_init(struct platform_device *pdev, int irq_idx)
0574 {
0575     struct net_device *ndev = platform_get_drvdata(pdev);
0576     struct fec_enet_private *fep = netdev_priv(ndev);
0577     int irq;
0578     int ret;
0579 
0580     fep->ptp_caps.owner = THIS_MODULE;
0581     strlcpy(fep->ptp_caps.name, "fec ptp", sizeof(fep->ptp_caps.name));
0582 
0583     fep->ptp_caps.max_adj = 250000000;
0584     fep->ptp_caps.n_alarm = 0;
0585     fep->ptp_caps.n_ext_ts = 0;
0586     fep->ptp_caps.n_per_out = 0;
0587     fep->ptp_caps.n_pins = 0;
0588     fep->ptp_caps.pps = 1;
0589     fep->ptp_caps.adjfreq = fec_ptp_adjfreq;
0590     fep->ptp_caps.adjtime = fec_ptp_adjtime;
0591     fep->ptp_caps.gettime64 = fec_ptp_gettime;
0592     fep->ptp_caps.settime64 = fec_ptp_settime;
0593     fep->ptp_caps.enable = fec_ptp_enable;
0594 
0595     fep->cycle_speed = clk_get_rate(fep->clk_ptp);
0596     if (!fep->cycle_speed) {
0597         fep->cycle_speed = NSEC_PER_SEC;
0598         dev_err(&fep->pdev->dev, "clk_ptp clock rate is zero\n");
0599     }
0600     fep->ptp_inc = NSEC_PER_SEC / fep->cycle_speed;
0601 
0602     spin_lock_init(&fep->tmreg_lock);
0603 
0604     fec_ptp_start_cyclecounter(ndev);
0605 
0606     INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep);
0607 
0608     irq = platform_get_irq_byname_optional(pdev, "pps");
0609     if (irq < 0)
0610         irq = platform_get_irq_optional(pdev, irq_idx);
0611     /* Failure to get an irq is not fatal,
0612      * only the PTP_CLOCK_PPS clock events should stop
0613      */
0614     if (irq >= 0) {
0615         ret = devm_request_irq(&pdev->dev, irq, fec_pps_interrupt,
0616                        0, pdev->name, ndev);
0617         if (ret < 0)
0618             dev_warn(&pdev->dev, "request for pps irq failed(%d)\n",
0619                  ret);
0620     }
0621 
0622     fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
0623     if (IS_ERR(fep->ptp_clock)) {
0624         fep->ptp_clock = NULL;
0625         dev_err(&pdev->dev, "ptp_clock_register failed\n");
0626     }
0627 
0628     schedule_delayed_work(&fep->time_keep, HZ);
0629 }
0630 
0631 void fec_ptp_stop(struct platform_device *pdev)
0632 {
0633     struct net_device *ndev = platform_get_drvdata(pdev);
0634     struct fec_enet_private *fep = netdev_priv(ndev);
0635 
0636     cancel_delayed_work_sync(&fep->time_keep);
0637     if (fep->ptp_clock)
0638         ptp_clock_unregister(fep->ptp_clock);
0639 }