Lines Matching +full:double +full:- +full:channel
1 // SPDX-License-Identifier: GPL-2.0
96 * @enable: enable the channel pps output
98 * This function enble the PPS ouput on the timer channel.
107 if (fep->pps_enable == enable) in fec_ptp_enable_pps()
110 fep->pps_channel = DEFAULT_PPS_CHANNEL; in fec_ptp_enable_pps()
111 fep->reload_period = PPS_OUPUT_RELOAD_PERIOD; in fec_ptp_enable_pps()
113 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_enable_pps()
118 writel(FEC_T_TF_MASK, fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_enable_pps()
120 /* It is recommended to double check the TMODE field in the in fec_ptp_enable_pps()
122 * is written into TCCR register. Just add a double check. in fec_ptp_enable_pps()
124 val = readl(fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_enable_pps()
127 writel(val, fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_enable_pps()
128 val = readl(fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_enable_pps()
132 timecounter_read(&fep->tc); in fec_ptp_enable_pps()
137 * NSEC_PER_SEC - ts.tv_nsec. Add the remaining nanoseconds in fec_ptp_enable_pps()
140 tempval = fep->cc.read(&fep->cc); in fec_ptp_enable_pps()
142 ns = timecounter_cyc2time(&fep->tc, tempval); in fec_ptp_enable_pps()
148 val = NSEC_PER_SEC - (u32)ts.tv_nsec + tempval; in fec_ptp_enable_pps()
152 * - ts.tv_nsec is close to be zero(For example 20ns); Since the timer in fec_ptp_enable_pps()
157 * of next second. The current setting is 31-bit timer and wrap in fec_ptp_enable_pps()
163 /* We add (2 * NSEC_PER_SEC - (u32)ts.tv_nsec) to current in fec_ptp_enable_pps()
164 * ptp counter, which maybe cause 32-bit wrap. Since the in fec_ptp_enable_pps()
165 * (NSEC_PER_SEC - (u32)ts.tv_nsec) is less than 2 second. in fec_ptp_enable_pps()
167 * is bigger than fep->cc.mask would be a error. in fec_ptp_enable_pps()
169 val &= fep->cc.mask; in fec_ptp_enable_pps()
170 writel(val, fep->hwp + FEC_TCCR(fep->pps_channel)); in fec_ptp_enable_pps()
173 fep->next_counter = (val + fep->reload_period) & fep->cc.mask; in fec_ptp_enable_pps()
176 val = readl(fep->hwp + FEC_ATIME_CTRL); in fec_ptp_enable_pps()
178 writel(val, fep->hwp + FEC_ATIME_CTRL); in fec_ptp_enable_pps()
180 /* Compare channel setting. */ in fec_ptp_enable_pps()
181 val = readl(fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_enable_pps()
186 writel(val, fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_enable_pps()
191 writel(fep->next_counter, fep->hwp + FEC_TCCR(fep->pps_channel)); in fec_ptp_enable_pps()
192 fep->next_counter = (fep->next_counter + fep->reload_period) & fep->cc.mask; in fec_ptp_enable_pps()
194 writel(0, fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_enable_pps()
197 fep->pps_enable = enable; in fec_ptp_enable_pps()
198 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_enable_pps()
209 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_pps_perout()
212 timecounter_read(&fep->tc); in fec_ptp_pps_perout()
215 temp_val = readl(fep->hwp + FEC_ATIME_CTRL); in fec_ptp_pps_perout()
217 writel(temp_val, fep->hwp + FEC_ATIME_CTRL); in fec_ptp_pps_perout()
218 if (fep->quirks & FEC_QUIRK_BUG_CAPTURE) in fec_ptp_pps_perout()
221 ptp_hc = readl(fep->hwp + FEC_ATIME); in fec_ptp_pps_perout()
224 curr_time = timecounter_cyc2time(&fep->tc, ptp_hc); in fec_ptp_pps_perout()
230 if (fep->perout_stime < curr_time + 100 * NSEC_PER_MSEC) { in fec_ptp_pps_perout()
231 dev_err(&fep->pdev->dev, "Current time is too close to the start time!\n"); in fec_ptp_pps_perout()
232 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_pps_perout()
233 return -1; in fec_ptp_pps_perout()
236 compare_val = fep->perout_stime - curr_time + ptp_hc; in fec_ptp_pps_perout()
237 compare_val &= fep->cc.mask; in fec_ptp_pps_perout()
239 writel(compare_val, fep->hwp + FEC_TCCR(fep->pps_channel)); in fec_ptp_pps_perout()
240 fep->next_counter = (compare_val + fep->reload_period) & fep->cc.mask; in fec_ptp_pps_perout()
243 temp_val = readl(fep->hwp + FEC_ATIME_CTRL); in fec_ptp_pps_perout()
245 writel(temp_val, fep->hwp + FEC_ATIME_CTRL); in fec_ptp_pps_perout()
247 /* Compare channel setting. */ in fec_ptp_pps_perout()
248 temp_val = readl(fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_pps_perout()
253 writel(temp_val, fep->hwp + FEC_TCSR(fep->pps_channel)); in fec_ptp_pps_perout()
258 writel(fep->next_counter, fep->hwp + FEC_TCCR(fep->pps_channel)); in fec_ptp_pps_perout()
259 fep->next_counter = (fep->next_counter + fep->reload_period) & fep->cc.mask; in fec_ptp_pps_perout()
260 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_pps_perout()
276 * fec_ptp_read - read raw cycle counter (to be used by time counter)
289 tempval = readl(fep->hwp + FEC_ATIME_CTRL); in fec_ptp_read()
291 writel(tempval, fep->hwp + FEC_ATIME_CTRL); in fec_ptp_read()
293 if (fep->quirks & FEC_QUIRK_BUG_CAPTURE) in fec_ptp_read()
296 return readl(fep->hwp + FEC_ATIME); in fec_ptp_read()
300 * fec_ptp_start_cyclecounter - create the cycle counter from hw
313 inc = 1000000000 / fep->cycle_speed; in fec_ptp_start_cyclecounter()
316 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_start_cyclecounter()
319 writel(inc << FEC_T_INC_OFFSET, fep->hwp + FEC_ATIME_INC); in fec_ptp_start_cyclecounter()
321 /* use 31-bit timer counter */ in fec_ptp_start_cyclecounter()
322 writel(FEC_COUNTER_PERIOD, fep->hwp + FEC_ATIME_EVT_PERIOD); in fec_ptp_start_cyclecounter()
325 fep->hwp + FEC_ATIME_CTRL); in fec_ptp_start_cyclecounter()
327 memset(&fep->cc, 0, sizeof(fep->cc)); in fec_ptp_start_cyclecounter()
328 fep->cc.read = fec_ptp_read; in fec_ptp_start_cyclecounter()
329 fep->cc.mask = CLOCKSOURCE_MASK(31); in fec_ptp_start_cyclecounter()
330 fep->cc.shift = 31; in fec_ptp_start_cyclecounter()
331 fep->cc.mult = FEC_CC_MULT; in fec_ptp_start_cyclecounter()
334 timecounter_init(&fep->tc, &fep->cc, 0); in fec_ptp_start_cyclecounter()
336 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_start_cyclecounter()
340 * fec_ptp_adjfine - adjust ptp cycle frequency
347 * Scaled parts per million is ppm with a 16-bit binary fractional field.
369 ppb = -ppb; in fec_ptp_adjfine()
374 * Try to find the corr_inc between 1 to fep->ptp_inc to in fec_ptp_adjfine()
378 rhs = (u64)ppb * (u64)fep->ptp_inc; in fec_ptp_adjfine()
379 for (i = 1; i <= fep->ptp_inc; i++) { in fec_ptp_adjfine()
387 /* Not found? Set it to high value - double speed in fec_ptp_adjfine()
390 if (i > fep->ptp_inc) { in fec_ptp_adjfine()
391 corr_inc = fep->ptp_inc; in fec_ptp_adjfine()
396 corr_ns = fep->ptp_inc - corr_inc; in fec_ptp_adjfine()
398 corr_ns = fep->ptp_inc + corr_inc; in fec_ptp_adjfine()
400 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_adjfine()
402 tmp = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_MASK; in fec_ptp_adjfine()
404 writel(tmp, fep->hwp + FEC_ATIME_INC); in fec_ptp_adjfine()
405 corr_period = corr_period > 1 ? corr_period - 1 : corr_period; in fec_ptp_adjfine()
406 writel(corr_period, fep->hwp + FEC_ATIME_CORR); in fec_ptp_adjfine()
408 timecounter_read(&fep->tc); in fec_ptp_adjfine()
410 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_adjfine()
428 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_adjtime()
429 timecounter_adjtime(&fep->tc, delta); in fec_ptp_adjtime()
430 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_adjtime()
450 mutex_lock(&fep->ptp_clk_mutex); in fec_ptp_gettime()
452 if (!fep->ptp_clk_on) { in fec_ptp_gettime()
453 mutex_unlock(&fep->ptp_clk_mutex); in fec_ptp_gettime()
454 return -EINVAL; in fec_ptp_gettime()
456 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_gettime()
457 ns = timecounter_read(&fep->tc); in fec_ptp_gettime()
458 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_gettime()
459 mutex_unlock(&fep->ptp_clk_mutex); in fec_ptp_gettime()
484 mutex_lock(&fep->ptp_clk_mutex); in fec_ptp_settime()
486 if (!fep->ptp_clk_on) { in fec_ptp_settime()
487 mutex_unlock(&fep->ptp_clk_mutex); in fec_ptp_settime()
488 return -EINVAL; in fec_ptp_settime()
495 counter = ns & fep->cc.mask; in fec_ptp_settime()
497 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_settime()
498 writel(counter, fep->hwp + FEC_ATIME); in fec_ptp_settime()
499 timecounter_init(&fep->tc, &fep->cc, ns); in fec_ptp_settime()
500 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_settime()
501 mutex_unlock(&fep->ptp_clk_mutex); in fec_ptp_settime()
505 static int fec_ptp_pps_disable(struct fec_enet_private *fep, uint channel) in fec_ptp_pps_disable() argument
509 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_pps_disable()
510 writel(0, fep->hwp + FEC_TCSR(channel)); in fec_ptp_pps_disable()
511 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_pps_disable()
534 if (rq->type == PTP_CLK_REQ_PPS) { in fec_ptp_enable()
538 } else if (rq->type == PTP_CLK_REQ_PEROUT) { in fec_ptp_enable()
540 if (rq->perout.flags) in fec_ptp_enable()
541 return -EOPNOTSUPP; in fec_ptp_enable()
543 if (rq->perout.index != DEFAULT_PPS_CHANNEL) in fec_ptp_enable()
544 return -EOPNOTSUPP; in fec_ptp_enable()
546 fep->pps_channel = DEFAULT_PPS_CHANNEL; in fec_ptp_enable()
547 period.tv_sec = rq->perout.period.sec; in fec_ptp_enable()
548 period.tv_nsec = rq->perout.period.nsec; in fec_ptp_enable()
555 dev_err(&fep->pdev->dev, "The period must equal to or less than 4s!\n"); in fec_ptp_enable()
556 return -EOPNOTSUPP; in fec_ptp_enable()
559 fep->reload_period = div_u64(period_ns, 2); in fec_ptp_enable()
560 if (on && fep->reload_period) { in fec_ptp_enable()
562 start_time.tv_sec = rq->perout.start.sec; in fec_ptp_enable()
563 start_time.tv_nsec = rq->perout.start.nsec; in fec_ptp_enable()
564 fep->perout_stime = timespec64_to_ns(&start_time); in fec_ptp_enable()
566 mutex_lock(&fep->ptp_clk_mutex); in fec_ptp_enable()
567 if (!fep->ptp_clk_on) { in fec_ptp_enable()
568 dev_err(&fep->pdev->dev, "Error: PTP clock is closed!\n"); in fec_ptp_enable()
569 mutex_unlock(&fep->ptp_clk_mutex); in fec_ptp_enable()
570 return -EOPNOTSUPP; in fec_ptp_enable()
572 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_ptp_enable()
574 curr_time = timecounter_read(&fep->tc); in fec_ptp_enable()
575 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_ptp_enable()
576 mutex_unlock(&fep->ptp_clk_mutex); in fec_ptp_enable()
579 delta = fep->perout_stime - curr_time; in fec_ptp_enable()
581 if (fep->perout_stime <= curr_time) { in fec_ptp_enable()
582 dev_err(&fep->pdev->dev, "Start time must larger than current time!\n"); in fec_ptp_enable()
583 return -EINVAL; in fec_ptp_enable()
586 /* Because the timer counter of FEC only has 31-bits, correspondingly, in fec_ptp_enable()
593 timeout = ns_to_ktime(delta - NSEC_PER_SEC); in fec_ptp_enable()
594 hrtimer_start(&fep->perout_timer, timeout, HRTIMER_MODE_REL); in fec_ptp_enable()
599 fec_ptp_pps_disable(fep, fep->pps_channel); in fec_ptp_enable()
604 return -EOPNOTSUPP; in fec_ptp_enable()
613 switch (config->tx_type) { in fec_ptp_set()
615 fep->hwts_tx_en = 0; in fec_ptp_set()
618 fep->hwts_tx_en = 1; in fec_ptp_set()
621 return -ERANGE; in fec_ptp_set()
624 switch (config->rx_filter) { in fec_ptp_set()
626 fep->hwts_rx_en = 0; in fec_ptp_set()
630 fep->hwts_rx_en = 1; in fec_ptp_set()
631 config->rx_filter = HWTSTAMP_FILTER_ALL; in fec_ptp_set()
642 config->flags = 0; in fec_ptp_get()
643 config->tx_type = fep->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; in fec_ptp_get()
644 config->rx_filter = (fep->hwts_rx_en ? in fec_ptp_get()
649 * fec_time_keep - call timecounter_read every second to avoid timer overrun
658 mutex_lock(&fep->ptp_clk_mutex); in fec_time_keep()
659 if (fep->ptp_clk_on) { in fec_time_keep()
660 spin_lock_irqsave(&fep->tmreg_lock, flags); in fec_time_keep()
661 timecounter_read(&fep->tc); in fec_time_keep()
662 spin_unlock_irqrestore(&fep->tmreg_lock, flags); in fec_time_keep()
664 mutex_unlock(&fep->ptp_clk_mutex); in fec_time_keep()
666 schedule_delayed_work(&fep->time_keep, HZ); in fec_time_keep()
675 u8 channel = fep->pps_channel; in fec_pps_interrupt() local
678 val = readl(fep->hwp + FEC_TCSR(channel)); in fec_pps_interrupt()
683 writel(fep->next_counter, fep->hwp + FEC_TCCR(channel)); in fec_pps_interrupt()
685 writel(val, fep->hwp + FEC_TCSR(channel)); in fec_pps_interrupt()
686 } while (readl(fep->hwp + FEC_TCSR(channel)) & FEC_T_TF_MASK); in fec_pps_interrupt()
689 fep->next_counter = (fep->next_counter + fep->reload_period) & in fec_pps_interrupt()
690 fep->cc.mask; in fec_pps_interrupt()
693 ptp_clock_event(fep->ptp_clock, &event); in fec_pps_interrupt()
717 fep->ptp_caps.owner = THIS_MODULE; in fec_ptp_init()
718 strscpy(fep->ptp_caps.name, "fec ptp", sizeof(fep->ptp_caps.name)); in fec_ptp_init()
720 fep->ptp_caps.max_adj = 250000000; in fec_ptp_init()
721 fep->ptp_caps.n_alarm = 0; in fec_ptp_init()
722 fep->ptp_caps.n_ext_ts = 0; in fec_ptp_init()
723 fep->ptp_caps.n_per_out = 1; in fec_ptp_init()
724 fep->ptp_caps.n_pins = 0; in fec_ptp_init()
725 fep->ptp_caps.pps = 1; in fec_ptp_init()
726 fep->ptp_caps.adjfine = fec_ptp_adjfine; in fec_ptp_init()
727 fep->ptp_caps.adjtime = fec_ptp_adjtime; in fec_ptp_init()
728 fep->ptp_caps.gettime64 = fec_ptp_gettime; in fec_ptp_init()
729 fep->ptp_caps.settime64 = fec_ptp_settime; in fec_ptp_init()
730 fep->ptp_caps.enable = fec_ptp_enable; in fec_ptp_init()
732 fep->cycle_speed = clk_get_rate(fep->clk_ptp); in fec_ptp_init()
733 if (!fep->cycle_speed) { in fec_ptp_init()
734 fep->cycle_speed = NSEC_PER_SEC; in fec_ptp_init()
735 dev_err(&fep->pdev->dev, "clk_ptp clock rate is zero\n"); in fec_ptp_init()
737 fep->ptp_inc = NSEC_PER_SEC / fep->cycle_speed; in fec_ptp_init()
739 spin_lock_init(&fep->tmreg_lock); in fec_ptp_init()
743 INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep); in fec_ptp_init()
745 hrtimer_init(&fep->perout_timer, CLOCK_REALTIME, HRTIMER_MODE_REL); in fec_ptp_init()
746 fep->perout_timer.function = fec_ptp_pps_perout_handler; in fec_ptp_init()
755 ret = devm_request_irq(&pdev->dev, irq, fec_pps_interrupt, in fec_ptp_init()
756 0, pdev->name, ndev); in fec_ptp_init()
758 dev_warn(&pdev->dev, "request for pps irq failed(%d)\n", in fec_ptp_init()
762 fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev); in fec_ptp_init()
763 if (IS_ERR(fep->ptp_clock)) { in fec_ptp_init()
764 fep->ptp_clock = NULL; in fec_ptp_init()
765 dev_err(&pdev->dev, "ptp_clock_register failed\n"); in fec_ptp_init()
768 schedule_delayed_work(&fep->time_keep, HZ); in fec_ptp_init()
776 cancel_delayed_work_sync(&fep->time_keep); in fec_ptp_stop()
777 hrtimer_cancel(&fep->perout_timer); in fec_ptp_stop()
778 if (fep->ptp_clock) in fec_ptp_stop()
779 ptp_clock_unregister(fep->ptp_clock); in fec_ptp_stop()