1 // SPDX-License-Identifier: GPL-2.0+
4 #include <linux/module.h>
5 #include <linux/device.h>
7 #include <linux/ptp_classify.h>
11 #define INCVALUE_MASK 0x7fffffff
12 #define ISGN 0x80000000
14 /* The 82580 timesync updates the system timer every 8ns by 8ns,
15 * and this update value cannot be reprogrammed.
17 * Neither the 82576 nor the 82580 offer registers wide enough to hold
18 * nanoseconds time values for very long. For the 82580, SYSTIM always
19 * counts nanoseconds, but the upper 24 bits are not available. The
20 * frequency is adjusted by changing the 32 bit fractional nanoseconds
23 * For the 82576, the SYSTIM register time unit is affect by the
24 * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
25 * field are needed to provide the nominal 16 nanosecond period,
26 * leaving 19 bits for fractional nanoseconds.
28 * We scale the NIC clock cycle by a large factor so that relatively
29 * small clock corrections can be added or subtracted at each clock
30 * tick. The drawbacks of a large factor are a) that the clock
31 * register overflows more quickly (not such a big deal) and b) that
32 * the increment per tick has to fit into 24 bits. As a result we
33 * need to use a shift of 19 so we can fit a value of 16 into the
38 * +--------------+ +---+---+------+
39 * 82576 | 32 | | 8 | 5 | 19 |
40 * +--------------+ +---+---+------+
41 * \________ 45 bits _______/ fract
43 * +----------+---+ +--------------+
44 * 82580 | 24 | 8 | | 32 |
45 * +----------+---+ +--------------+
46 * reserved \______ 40 bits _____/
49 * The 45 bit 82576 SYSTIM overflows every
50 * 2^45 * 10^-9 / 3600 = 9.77 hours.
52 * The 40 bit 82580 SYSTIM overflows every
53 * 2^40 * 10^-9 / 60 = 18.3 minutes.
55 * SYSTIM is converted to real time using a timecounter. As
56 * timecounter_cyc2time() allows old timestamps, the timecounter needs
57 * to be updated at least once per half of the SYSTIM interval.
58 * Scheduling of delayed work is not very accurate, and also the NIC
59 * clock can be adjusted to run up to 6% faster and the system clock
60 * up to 10% slower, so we aim for 6 minutes to be sure the actual
61 * interval in the NIC time is shorter than 9.16 minutes.
64 #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 6)
65 #define IGB_PTP_TX_TIMEOUT (HZ * 15)
66 #define INCPERIOD_82576 BIT(E1000_TIMINCA_16NS_SHIFT)
67 #define INCVALUE_82576_MASK GENMASK(E1000_TIMINCA_16NS_SHIFT - 1, 0)
68 #define INCVALUE_82576 (16u << IGB_82576_TSYNC_SHIFT)
69 #define IGB_NBITS_82580 40
71 static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter);
72 static void igb_ptp_sdp_init(struct igb_adapter *adapter);
74 /* SYSTIM read access for the 82576 */
75 static u64 igb_ptp_read_82576(const struct cyclecounter *cc)
77 struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
78 struct e1000_hw *hw = &igb->hw;
82 lo = rd32(E1000_SYSTIML);
83 hi = rd32(E1000_SYSTIMH);
85 val = ((u64) hi) << 32;
91 /* SYSTIM read access for the 82580 */
92 static u64 igb_ptp_read_82580(const struct cyclecounter *cc)
94 struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
95 struct e1000_hw *hw = &igb->hw;
99 /* The timestamp latches on lowest register read. For the 82580
100 * the lowest register is SYSTIMR instead of SYSTIML. However we only
101 * need to provide nanosecond resolution, so we just ignore it.
104 lo = rd32(E1000_SYSTIML);
105 hi = rd32(E1000_SYSTIMH);
107 val = ((u64) hi) << 32;
113 /* SYSTIM read access for I210/I211 */
114 static void igb_ptp_read_i210(struct igb_adapter *adapter,
115 struct timespec64 *ts)
117 struct e1000_hw *hw = &adapter->hw;
120 /* The timestamp latches on lowest register read. For I210/I211, the
121 * lowest register is SYSTIMR. Since we only need to provide nanosecond
122 * resolution, we can ignore it.
125 nsec = rd32(E1000_SYSTIML);
126 sec = rd32(E1000_SYSTIMH);
132 static void igb_ptp_write_i210(struct igb_adapter *adapter,
133 const struct timespec64 *ts)
135 struct e1000_hw *hw = &adapter->hw;
137 /* Writing the SYSTIMR register is not necessary as it only provides
138 * sub-nanosecond resolution.
140 wr32(E1000_SYSTIML, ts->tv_nsec);
141 wr32(E1000_SYSTIMH, (u32)ts->tv_sec);
145 * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
146 * @adapter: board private structure
147 * @hwtstamps: timestamp structure to update
148 * @systim: unsigned 64bit system time value.
150 * We need to convert the system time value stored in the RX/TXSTMP registers
151 * into a hwtstamp which can be used by the upper level timestamping functions.
153 * The 'tmreg_lock' spinlock is used to protect the consistency of the
154 * system time value. This is needed because reading the 64 bit time
155 * value involves reading two (or three) 32 bit registers. The first
156 * read latches the value. Ditto for writing.
158 * In addition, here have extended the system time with an overflow
159 * counter in software.
161 static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
162 struct skb_shared_hwtstamps *hwtstamps,
168 memset(hwtstamps, 0, sizeof(*hwtstamps));
170 switch (adapter->hw.mac.type) {
175 spin_lock_irqsave(&adapter->tmreg_lock, flags);
176 ns = timecounter_cyc2time(&adapter->tc, systim);
177 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
179 hwtstamps->hwtstamp = ns_to_ktime(ns);
183 /* Upper 32 bits contain s, lower 32 bits contain ns. */
184 hwtstamps->hwtstamp = ktime_set(systim >> 32,
185 systim & 0xFFFFFFFF);
192 /* PTP clock operations */
193 static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
195 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
197 struct e1000_hw *hw = &igb->hw;
208 rate = div_u64(rate, 1953125);
210 incvalue = 16 << IGB_82576_TSYNC_SHIFT;
217 wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
222 static int igb_ptp_adjfine_82580(struct ptp_clock_info *ptp, long scaled_ppm)
224 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
226 struct e1000_hw *hw = &igb->hw;
231 if (scaled_ppm < 0) {
233 scaled_ppm = -scaled_ppm;
237 rate = div_u64(rate, 15625);
239 inca = rate & INCVALUE_MASK;
243 wr32(E1000_TIMINCA, inca);
248 static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
250 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
254 spin_lock_irqsave(&igb->tmreg_lock, flags);
255 timecounter_adjtime(&igb->tc, delta);
256 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
261 static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
263 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
266 struct timespec64 now, then = ns_to_timespec64(delta);
268 spin_lock_irqsave(&igb->tmreg_lock, flags);
270 igb_ptp_read_i210(igb, &now);
271 now = timespec64_add(now, then);
272 igb_ptp_write_i210(igb, (const struct timespec64 *)&now);
274 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
279 static int igb_ptp_gettimex_82576(struct ptp_clock_info *ptp,
280 struct timespec64 *ts,
281 struct ptp_system_timestamp *sts)
283 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
285 struct e1000_hw *hw = &igb->hw;
290 spin_lock_irqsave(&igb->tmreg_lock, flags);
292 ptp_read_system_prets(sts);
293 lo = rd32(E1000_SYSTIML);
294 ptp_read_system_postts(sts);
295 hi = rd32(E1000_SYSTIMH);
297 ns = timecounter_cyc2time(&igb->tc, ((u64)hi << 32) | lo);
299 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
301 *ts = ns_to_timespec64(ns);
306 static int igb_ptp_gettimex_82580(struct ptp_clock_info *ptp,
307 struct timespec64 *ts,
308 struct ptp_system_timestamp *sts)
310 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
312 struct e1000_hw *hw = &igb->hw;
317 spin_lock_irqsave(&igb->tmreg_lock, flags);
319 ptp_read_system_prets(sts);
321 ptp_read_system_postts(sts);
322 lo = rd32(E1000_SYSTIML);
323 hi = rd32(E1000_SYSTIMH);
325 ns = timecounter_cyc2time(&igb->tc, ((u64)hi << 32) | lo);
327 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
329 *ts = ns_to_timespec64(ns);
334 static int igb_ptp_gettimex_i210(struct ptp_clock_info *ptp,
335 struct timespec64 *ts,
336 struct ptp_system_timestamp *sts)
338 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
340 struct e1000_hw *hw = &igb->hw;
343 spin_lock_irqsave(&igb->tmreg_lock, flags);
345 ptp_read_system_prets(sts);
347 ptp_read_system_postts(sts);
348 ts->tv_nsec = rd32(E1000_SYSTIML);
349 ts->tv_sec = rd32(E1000_SYSTIMH);
351 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
356 static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
357 const struct timespec64 *ts)
359 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
364 ns = timespec64_to_ns(ts);
366 spin_lock_irqsave(&igb->tmreg_lock, flags);
368 timecounter_init(&igb->tc, &igb->cc, ns);
370 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
375 static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
376 const struct timespec64 *ts)
378 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
382 spin_lock_irqsave(&igb->tmreg_lock, flags);
384 igb_ptp_write_i210(igb, ts);
386 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
391 static void igb_pin_direction(int pin, int input, u32 *ctrl, u32 *ctrl_ext)
393 u32 *ptr = pin < 2 ? ctrl : ctrl_ext;
394 static const u32 mask[IGB_N_SDP] = {
397 E1000_CTRL_EXT_SDP2_DIR,
398 E1000_CTRL_EXT_SDP3_DIR,
407 static void igb_pin_extts(struct igb_adapter *igb, int chan, int pin)
409 static const u32 aux0_sel_sdp[IGB_N_SDP] = {
410 AUX0_SEL_SDP0, AUX0_SEL_SDP1, AUX0_SEL_SDP2, AUX0_SEL_SDP3,
412 static const u32 aux1_sel_sdp[IGB_N_SDP] = {
413 AUX1_SEL_SDP0, AUX1_SEL_SDP1, AUX1_SEL_SDP2, AUX1_SEL_SDP3,
415 static const u32 ts_sdp_en[IGB_N_SDP] = {
416 TS_SDP0_EN, TS_SDP1_EN, TS_SDP2_EN, TS_SDP3_EN,
418 struct e1000_hw *hw = &igb->hw;
419 u32 ctrl, ctrl_ext, tssdp = 0;
421 ctrl = rd32(E1000_CTRL);
422 ctrl_ext = rd32(E1000_CTRL_EXT);
423 tssdp = rd32(E1000_TSSDP);
425 igb_pin_direction(pin, 1, &ctrl, &ctrl_ext);
427 /* Make sure this pin is not enabled as an output. */
428 tssdp &= ~ts_sdp_en[pin];
431 tssdp &= ~AUX1_SEL_SDP3;
432 tssdp |= aux1_sel_sdp[pin] | AUX1_TS_SDP_EN;
434 tssdp &= ~AUX0_SEL_SDP3;
435 tssdp |= aux0_sel_sdp[pin] | AUX0_TS_SDP_EN;
438 wr32(E1000_TSSDP, tssdp);
439 wr32(E1000_CTRL, ctrl);
440 wr32(E1000_CTRL_EXT, ctrl_ext);
443 static void igb_pin_perout(struct igb_adapter *igb, int chan, int pin, int freq)
445 static const u32 aux0_sel_sdp[IGB_N_SDP] = {
446 AUX0_SEL_SDP0, AUX0_SEL_SDP1, AUX0_SEL_SDP2, AUX0_SEL_SDP3,
448 static const u32 aux1_sel_sdp[IGB_N_SDP] = {
449 AUX1_SEL_SDP0, AUX1_SEL_SDP1, AUX1_SEL_SDP2, AUX1_SEL_SDP3,
451 static const u32 ts_sdp_en[IGB_N_SDP] = {
452 TS_SDP0_EN, TS_SDP1_EN, TS_SDP2_EN, TS_SDP3_EN,
454 static const u32 ts_sdp_sel_tt0[IGB_N_SDP] = {
455 TS_SDP0_SEL_TT0, TS_SDP1_SEL_TT0,
456 TS_SDP2_SEL_TT0, TS_SDP3_SEL_TT0,
458 static const u32 ts_sdp_sel_tt1[IGB_N_SDP] = {
459 TS_SDP0_SEL_TT1, TS_SDP1_SEL_TT1,
460 TS_SDP2_SEL_TT1, TS_SDP3_SEL_TT1,
462 static const u32 ts_sdp_sel_fc0[IGB_N_SDP] = {
463 TS_SDP0_SEL_FC0, TS_SDP1_SEL_FC0,
464 TS_SDP2_SEL_FC0, TS_SDP3_SEL_FC0,
466 static const u32 ts_sdp_sel_fc1[IGB_N_SDP] = {
467 TS_SDP0_SEL_FC1, TS_SDP1_SEL_FC1,
468 TS_SDP2_SEL_FC1, TS_SDP3_SEL_FC1,
470 static const u32 ts_sdp_sel_clr[IGB_N_SDP] = {
471 TS_SDP0_SEL_FC1, TS_SDP1_SEL_FC1,
472 TS_SDP2_SEL_FC1, TS_SDP3_SEL_FC1,
474 struct e1000_hw *hw = &igb->hw;
475 u32 ctrl, ctrl_ext, tssdp = 0;
477 ctrl = rd32(E1000_CTRL);
478 ctrl_ext = rd32(E1000_CTRL_EXT);
479 tssdp = rd32(E1000_TSSDP);
481 igb_pin_direction(pin, 0, &ctrl, &ctrl_ext);
483 /* Make sure this pin is not enabled as an input. */
484 if ((tssdp & AUX0_SEL_SDP3) == aux0_sel_sdp[pin])
485 tssdp &= ~AUX0_TS_SDP_EN;
487 if ((tssdp & AUX1_SEL_SDP3) == aux1_sel_sdp[pin])
488 tssdp &= ~AUX1_TS_SDP_EN;
490 tssdp &= ~ts_sdp_sel_clr[pin];
493 tssdp |= ts_sdp_sel_fc1[pin];
495 tssdp |= ts_sdp_sel_fc0[pin];
498 tssdp |= ts_sdp_sel_tt1[pin];
500 tssdp |= ts_sdp_sel_tt0[pin];
502 tssdp |= ts_sdp_en[pin];
504 wr32(E1000_TSSDP, tssdp);
505 wr32(E1000_CTRL, ctrl);
506 wr32(E1000_CTRL_EXT, ctrl_ext);
509 static int igb_ptp_feature_enable_82580(struct ptp_clock_info *ptp,
510 struct ptp_clock_request *rq, int on)
512 struct igb_adapter *igb =
513 container_of(ptp, struct igb_adapter, ptp_caps);
514 u32 tsauxc, tsim, tsauxc_mask, tsim_mask, trgttiml, trgttimh, systiml,
515 systimh, level_mask, level, rem;
516 struct e1000_hw *hw = &igb->hw;
517 struct timespec64 ts, start;
524 case PTP_CLK_REQ_EXTTS:
525 /* Reject requests with unsupported flags */
526 if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
533 pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS,
538 if (rq->extts.index == 1) {
539 tsauxc_mask = TSAUXC_EN_TS1;
540 tsim_mask = TSINTR_AUTT1;
542 tsauxc_mask = TSAUXC_EN_TS0;
543 tsim_mask = TSINTR_AUTT0;
545 spin_lock_irqsave(&igb->tmreg_lock, flags);
546 tsauxc = rd32(E1000_TSAUXC);
547 tsim = rd32(E1000_TSIM);
549 igb_pin_extts(igb, rq->extts.index, pin);
550 tsauxc |= tsauxc_mask;
553 tsauxc &= ~tsauxc_mask;
556 wr32(E1000_TSAUXC, tsauxc);
557 wr32(E1000_TSIM, tsim);
558 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
561 case PTP_CLK_REQ_PEROUT:
562 /* Reject requests with unsupported flags */
563 if (rq->perout.flags)
567 pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT,
572 ts.tv_sec = rq->perout.period.sec;
573 ts.tv_nsec = rq->perout.period.nsec;
574 ns = timespec64_to_ns(&ts);
578 ts = ns_to_timespec64(ns);
579 if (rq->perout.index == 1) {
580 tsauxc_mask = TSAUXC_EN_TT1;
581 tsim_mask = TSINTR_TT1;
582 trgttiml = E1000_TRGTTIML1;
583 trgttimh = E1000_TRGTTIMH1;
585 tsauxc_mask = TSAUXC_EN_TT0;
586 tsim_mask = TSINTR_TT0;
587 trgttiml = E1000_TRGTTIML0;
588 trgttimh = E1000_TRGTTIMH0;
590 spin_lock_irqsave(&igb->tmreg_lock, flags);
591 tsauxc = rd32(E1000_TSAUXC);
592 tsim = rd32(E1000_TSIM);
593 if (rq->perout.index == 1) {
594 tsauxc &= ~(TSAUXC_EN_TT1 | TSAUXC_EN_CLK1 | TSAUXC_ST1);
597 tsauxc &= ~(TSAUXC_EN_TT0 | TSAUXC_EN_CLK0 | TSAUXC_ST0);
601 int i = rq->perout.index;
603 /* read systim registers in sequence */
605 systiml = rd32(E1000_SYSTIML);
606 systimh = rd32(E1000_SYSTIMH);
607 systim = (((u64)(systimh & 0xFF)) << 32) | ((u64)systiml);
608 now = timecounter_cyc2time(&igb->tc, systim);
611 level_mask = (i == 1) ? 0x80000 : 0x40000;
612 level = (rd32(E1000_CTRL) & level_mask) ? 1 : 0;
614 level_mask = (i == 1) ? 0x80 : 0x40;
615 level = (rd32(E1000_CTRL_EXT) & level_mask) ? 1 : 0;
618 div_u64_rem(now, ns, &rem);
619 systim = systim + (ns - rem);
621 /* synchronize pin level with rising/falling edges */
622 div_u64_rem(now, ns << 1, &rem);
624 /* first half of period */
626 /* output is already low, skip this period */
630 /* second half of period */
632 /* output is already high, skip this period */
637 start = ns_to_timespec64(systim + (ns - rem));
638 igb_pin_perout(igb, i, pin, 0);
639 igb->perout[i].start.tv_sec = start.tv_sec;
640 igb->perout[i].start.tv_nsec = start.tv_nsec;
641 igb->perout[i].period.tv_sec = ts.tv_sec;
642 igb->perout[i].period.tv_nsec = ts.tv_nsec;
644 wr32(trgttiml, (u32)systim);
645 wr32(trgttimh, ((u32)(systim >> 32)) & 0xFF);
646 tsauxc |= tsauxc_mask;
649 wr32(E1000_TSAUXC, tsauxc);
650 wr32(E1000_TSIM, tsim);
651 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
654 case PTP_CLK_REQ_PPS:
661 static int igb_ptp_feature_enable_i210(struct ptp_clock_info *ptp,
662 struct ptp_clock_request *rq, int on)
664 struct igb_adapter *igb =
665 container_of(ptp, struct igb_adapter, ptp_caps);
666 struct e1000_hw *hw = &igb->hw;
667 u32 tsauxc, tsim, tsauxc_mask, tsim_mask, trgttiml, trgttimh, freqout;
669 struct timespec64 ts;
670 int use_freq = 0, pin = -1;
674 case PTP_CLK_REQ_EXTTS:
675 /* Reject requests with unsupported flags */
676 if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
682 /* Reject requests failing to enable both edges. */
683 if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
684 (rq->extts.flags & PTP_ENABLE_FEATURE) &&
685 (rq->extts.flags & PTP_EXTTS_EDGES) != PTP_EXTTS_EDGES)
689 pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS,
694 if (rq->extts.index == 1) {
695 tsauxc_mask = TSAUXC_EN_TS1;
696 tsim_mask = TSINTR_AUTT1;
698 tsauxc_mask = TSAUXC_EN_TS0;
699 tsim_mask = TSINTR_AUTT0;
701 spin_lock_irqsave(&igb->tmreg_lock, flags);
702 tsauxc = rd32(E1000_TSAUXC);
703 tsim = rd32(E1000_TSIM);
705 igb_pin_extts(igb, rq->extts.index, pin);
706 tsauxc |= tsauxc_mask;
709 tsauxc &= ~tsauxc_mask;
712 wr32(E1000_TSAUXC, tsauxc);
713 wr32(E1000_TSIM, tsim);
714 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
717 case PTP_CLK_REQ_PEROUT:
718 /* Reject requests with unsupported flags */
719 if (rq->perout.flags)
723 pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT,
728 ts.tv_sec = rq->perout.period.sec;
729 ts.tv_nsec = rq->perout.period.nsec;
730 ns = timespec64_to_ns(&ts);
732 if (on && ((ns <= 70000000LL) || (ns == 125000000LL) ||
733 (ns == 250000000LL) || (ns == 500000000LL))) {
738 ts = ns_to_timespec64(ns);
739 if (rq->perout.index == 1) {
741 tsauxc_mask = TSAUXC_EN_CLK1 | TSAUXC_ST1;
744 tsauxc_mask = TSAUXC_EN_TT1;
745 tsim_mask = TSINTR_TT1;
747 trgttiml = E1000_TRGTTIML1;
748 trgttimh = E1000_TRGTTIMH1;
749 freqout = E1000_FREQOUT1;
752 tsauxc_mask = TSAUXC_EN_CLK0 | TSAUXC_ST0;
755 tsauxc_mask = TSAUXC_EN_TT0;
756 tsim_mask = TSINTR_TT0;
758 trgttiml = E1000_TRGTTIML0;
759 trgttimh = E1000_TRGTTIMH0;
760 freqout = E1000_FREQOUT0;
762 spin_lock_irqsave(&igb->tmreg_lock, flags);
763 tsauxc = rd32(E1000_TSAUXC);
764 tsim = rd32(E1000_TSIM);
765 if (rq->perout.index == 1) {
766 tsauxc &= ~(TSAUXC_EN_TT1 | TSAUXC_EN_CLK1 | TSAUXC_ST1);
769 tsauxc &= ~(TSAUXC_EN_TT0 | TSAUXC_EN_CLK0 | TSAUXC_ST0);
773 int i = rq->perout.index;
774 igb_pin_perout(igb, i, pin, use_freq);
775 igb->perout[i].start.tv_sec = rq->perout.start.sec;
776 igb->perout[i].start.tv_nsec = rq->perout.start.nsec;
777 igb->perout[i].period.tv_sec = ts.tv_sec;
778 igb->perout[i].period.tv_nsec = ts.tv_nsec;
779 wr32(trgttimh, rq->perout.start.sec);
780 wr32(trgttiml, rq->perout.start.nsec);
783 tsauxc |= tsauxc_mask;
786 wr32(E1000_TSAUXC, tsauxc);
787 wr32(E1000_TSIM, tsim);
788 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
791 case PTP_CLK_REQ_PPS:
792 spin_lock_irqsave(&igb->tmreg_lock, flags);
793 tsim = rd32(E1000_TSIM);
795 tsim |= TSINTR_SYS_WRAP;
797 tsim &= ~TSINTR_SYS_WRAP;
798 igb->pps_sys_wrap_on = !!on;
799 wr32(E1000_TSIM, tsim);
800 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
807 static int igb_ptp_feature_enable(struct ptp_clock_info *ptp,
808 struct ptp_clock_request *rq, int on)
813 static int igb_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
814 enum ptp_pin_function func, unsigned int chan)
829 * @work: pointer to work struct
831 * This work function polls the TSYNCTXCTL valid bit to determine when a
832 * timestamp has been taken for the current stored skb.
834 static void igb_ptp_tx_work(struct work_struct *work)
836 struct igb_adapter *adapter = container_of(work, struct igb_adapter,
838 struct e1000_hw *hw = &adapter->hw;
841 if (!adapter->ptp_tx_skb)
844 if (time_is_before_jiffies(adapter->ptp_tx_start +
845 IGB_PTP_TX_TIMEOUT)) {
846 dev_kfree_skb_any(adapter->ptp_tx_skb);
847 adapter->ptp_tx_skb = NULL;
848 clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
849 adapter->tx_hwtstamp_timeouts++;
850 /* Clear the tx valid bit in TSYNCTXCTL register to enable
854 dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
858 tsynctxctl = rd32(E1000_TSYNCTXCTL);
859 if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
860 igb_ptp_tx_hwtstamp(adapter);
862 /* reschedule to check later */
863 schedule_work(&adapter->ptp_tx_work);
866 static void igb_ptp_overflow_check(struct work_struct *work)
868 struct igb_adapter *igb =
869 container_of(work, struct igb_adapter, ptp_overflow_work.work);
870 struct timespec64 ts;
873 /* Update the timecounter */
874 ns = timecounter_read(&igb->tc);
876 ts = ns_to_timespec64(ns);
877 pr_debug("igb overflow check at %lld.%09lu\n",
878 (long long) ts.tv_sec, ts.tv_nsec);
880 schedule_delayed_work(&igb->ptp_overflow_work,
881 IGB_SYSTIM_OVERFLOW_PERIOD);
885 * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
886 * @adapter: private network adapter structure
888 * This watchdog task is scheduled to detect error case where hardware has
889 * dropped an Rx packet that was timestamped when the ring is full. The
890 * particular error is rare but leaves the device in a state unable to timestamp
891 * any future packets.
893 void igb_ptp_rx_hang(struct igb_adapter *adapter)
895 struct e1000_hw *hw = &adapter->hw;
896 u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
897 unsigned long rx_event;
899 /* Other hardware uses per-packet timestamps */
900 if (hw->mac.type != e1000_82576)
903 /* If we don't have a valid timestamp in the registers, just update the
904 * timeout counter and exit
906 if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
907 adapter->last_rx_ptp_check = jiffies;
911 /* Determine the most recent watchdog or rx_timestamp event */
912 rx_event = adapter->last_rx_ptp_check;
913 if (time_after(adapter->last_rx_timestamp, rx_event))
914 rx_event = adapter->last_rx_timestamp;
916 /* Only need to read the high RXSTMP register to clear the lock */
917 if (time_is_before_jiffies(rx_event + 5 * HZ)) {
919 adapter->last_rx_ptp_check = jiffies;
920 adapter->rx_hwtstamp_cleared++;
921 dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang\n");
926 * igb_ptp_tx_hang - detect error case where Tx timestamp never finishes
927 * @adapter: private network adapter structure
929 void igb_ptp_tx_hang(struct igb_adapter *adapter)
931 struct e1000_hw *hw = &adapter->hw;
932 bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
935 if (!adapter->ptp_tx_skb)
938 if (!test_bit(__IGB_PTP_TX_IN_PROGRESS, &adapter->state))
941 /* If we haven't received a timestamp within the timeout, it is
942 * reasonable to assume that it will never occur, so we can unlock the
943 * timestamp bit when this occurs.
946 cancel_work_sync(&adapter->ptp_tx_work);
947 dev_kfree_skb_any(adapter->ptp_tx_skb);
948 adapter->ptp_tx_skb = NULL;
949 clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
950 adapter->tx_hwtstamp_timeouts++;
951 /* Clear the tx valid bit in TSYNCTXCTL register to enable
955 dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
960 * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
961 * @adapter: Board private structure.
963 * If we were asked to do hardware stamping and such a time stamp is
964 * available, then it must have been for this skb here because we only
965 * allow only one such packet into the queue.
967 static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
969 struct sk_buff *skb = adapter->ptp_tx_skb;
970 struct e1000_hw *hw = &adapter->hw;
971 struct skb_shared_hwtstamps shhwtstamps;
975 regval = rd32(E1000_TXSTMPL);
976 regval |= (u64)rd32(E1000_TXSTMPH) << 32;
978 igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
979 /* adjust timestamp for the TX latency based on link speed */
980 if (adapter->hw.mac.type == e1000_i210) {
981 switch (adapter->link_speed) {
983 adjust = IGB_I210_TX_LATENCY_10;
986 adjust = IGB_I210_TX_LATENCY_100;
989 adjust = IGB_I210_TX_LATENCY_1000;
994 shhwtstamps.hwtstamp =
995 ktime_add_ns(shhwtstamps.hwtstamp, adjust);
997 /* Clear the lock early before calling skb_tstamp_tx so that
998 * applications are not woken up before the lock bit is clear. We use
999 * a copy of the skb pointer to ensure other threads can't change it
1000 * while we're notifying the stack.
1002 adapter->ptp_tx_skb = NULL;
1003 clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
1005 /* Notify the stack and free the skb after we've unlocked */
1006 skb_tstamp_tx(skb, &shhwtstamps);
1007 dev_kfree_skb_any(skb);
1011 * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
1012 * @q_vector: Pointer to interrupt specific structure
1013 * @va: Pointer to address containing Rx buffer
1014 * @timestamp: Pointer where timestamp will be stored
1016 * This function is meant to retrieve a timestamp from the first buffer of an
1017 * incoming frame. The value is stored in little endian format starting on
1020 * Returns: The timestamp header length or 0 if not available
1022 int igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector, void *va,
1025 struct igb_adapter *adapter = q_vector->adapter;
1026 struct skb_shared_hwtstamps ts;
1027 __le64 *regval = (__le64 *)va;
1030 if (!(adapter->ptp_flags & IGB_PTP_ENABLED))
1033 /* The timestamp is recorded in little endian format.
1035 * Field: Reserved Reserved SYSTIML SYSTIMH
1038 /* check reserved dwords are zero, be/le doesn't matter for zero */
1042 igb_ptp_systim_to_hwtstamp(adapter, &ts, le64_to_cpu(regval[1]));
1044 /* adjust timestamp for the RX latency based on link speed */
1045 if (adapter->hw.mac.type == e1000_i210) {
1046 switch (adapter->link_speed) {
1048 adjust = IGB_I210_RX_LATENCY_10;
1051 adjust = IGB_I210_RX_LATENCY_100;
1054 adjust = IGB_I210_RX_LATENCY_1000;
1059 *timestamp = ktime_sub_ns(ts.hwtstamp, adjust);
1061 return IGB_TS_HDR_LEN;
1065 * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
1066 * @q_vector: Pointer to interrupt specific structure
1067 * @skb: Buffer containing timestamp and packet
1069 * This function is meant to retrieve a timestamp from the internal registers
1070 * of the adapter and store it in the skb.
1072 void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector, struct sk_buff *skb)
1074 struct igb_adapter *adapter = q_vector->adapter;
1075 struct e1000_hw *hw = &adapter->hw;
1079 if (!(adapter->ptp_flags & IGB_PTP_ENABLED))
1082 /* If this bit is set, then the RX registers contain the time stamp. No
1083 * other packet will be time stamped until we read these registers, so
1084 * read the registers to make them available again. Because only one
1085 * packet can be time stamped at a time, we know that the register
1086 * values must belong to this one here and therefore we don't need to
1087 * compare any of the additional attributes stored for it.
1089 * If nothing went wrong, then it should have a shared tx_flags that we
1090 * can turn into a skb_shared_hwtstamps.
1092 if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
1095 regval = rd32(E1000_RXSTMPL);
1096 regval |= (u64)rd32(E1000_RXSTMPH) << 32;
1098 igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
1100 /* adjust timestamp for the RX latency based on link speed */
1101 if (adapter->hw.mac.type == e1000_i210) {
1102 switch (adapter->link_speed) {
1104 adjust = IGB_I210_RX_LATENCY_10;
1107 adjust = IGB_I210_RX_LATENCY_100;
1110 adjust = IGB_I210_RX_LATENCY_1000;
1114 skb_hwtstamps(skb)->hwtstamp =
1115 ktime_sub_ns(skb_hwtstamps(skb)->hwtstamp, adjust);
1117 /* Update the last_rx_timestamp timer in order to enable watchdog check
1118 * for error case of latched timestamp on a dropped packet.
1120 adapter->last_rx_timestamp = jiffies;
1124 * igb_ptp_get_ts_config - get hardware time stamping config
1125 * @netdev: netdev struct
1126 * @ifr: interface struct
1128 * Get the hwtstamp_config settings to return to the user. Rather than attempt
1129 * to deconstruct the settings from the registers, just return a shadow copy
1130 * of the last known settings.
1132 int igb_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
1134 struct igb_adapter *adapter = netdev_priv(netdev);
1135 struct hwtstamp_config *config = &adapter->tstamp_config;
1137 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
1142 * igb_ptp_set_timestamp_mode - setup hardware for timestamping
1143 * @adapter: networking device structure
1144 * @config: hwtstamp configuration
1146 * Outgoing time stamping can be enabled and disabled. Play nice and
1147 * disable it when requested, although it shouldn't case any overhead
1148 * when no packet needs it. At most one packet in the queue may be
1149 * marked for time stamping, otherwise it would be impossible to tell
1150 * for sure to which packet the hardware time stamp belongs.
1152 * Incoming time stamping has to be configured via the hardware
1153 * filters. Not all combinations are supported, in particular event
1154 * type has to be specified. Matching the kind of event packet is
1155 * not supported, with the exception of "all V2 events regardless of
1158 static int igb_ptp_set_timestamp_mode(struct igb_adapter *adapter,
1159 struct hwtstamp_config *config)
1161 struct e1000_hw *hw = &adapter->hw;
1162 u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
1163 u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
1164 u32 tsync_rx_cfg = 0;
1169 switch (config->tx_type) {
1170 case HWTSTAMP_TX_OFF:
1173 case HWTSTAMP_TX_ON:
1179 switch (config->rx_filter) {
1180 case HWTSTAMP_FILTER_NONE:
1183 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1184 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
1185 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
1188 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1189 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
1190 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
1193 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1194 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1195 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1196 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1197 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1198 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1199 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1200 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1201 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1202 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
1203 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1207 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1208 case HWTSTAMP_FILTER_NTP_ALL:
1209 case HWTSTAMP_FILTER_ALL:
1210 /* 82576 cannot timestamp all packets, which it needs to do to
1211 * support both V1 Sync and Delay_Req messages
1213 if (hw->mac.type != e1000_82576) {
1214 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
1215 config->rx_filter = HWTSTAMP_FILTER_ALL;
1220 config->rx_filter = HWTSTAMP_FILTER_NONE;
1224 if (hw->mac.type == e1000_82575) {
1225 if (tsync_rx_ctl | tsync_tx_ctl)
1230 /* Per-packet timestamping only works if all packets are
1231 * timestamped, so enable timestamping in all packets as
1232 * long as one Rx filter was configured.
1234 if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
1235 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
1236 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
1237 config->rx_filter = HWTSTAMP_FILTER_ALL;
1241 if ((hw->mac.type == e1000_i210) ||
1242 (hw->mac.type == e1000_i211)) {
1243 regval = rd32(E1000_RXPBS);
1244 regval |= E1000_RXPBS_CFG_TS_EN;
1245 wr32(E1000_RXPBS, regval);
1249 /* enable/disable TX */
1250 regval = rd32(E1000_TSYNCTXCTL);
1251 regval &= ~E1000_TSYNCTXCTL_ENABLED;
1252 regval |= tsync_tx_ctl;
1253 wr32(E1000_TSYNCTXCTL, regval);
1255 /* enable/disable RX */
1256 regval = rd32(E1000_TSYNCRXCTL);
1257 regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
1258 regval |= tsync_rx_ctl;
1259 wr32(E1000_TSYNCRXCTL, regval);
1261 /* define which PTP packets are time stamped */
1262 wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
1264 /* define ethertype filter for timestamped packets */
1266 wr32(E1000_ETQF(IGB_ETQF_FILTER_1588),
1267 (E1000_ETQF_FILTER_ENABLE | /* enable filter */
1268 E1000_ETQF_1588 | /* enable timestamping */
1269 ETH_P_1588)); /* 1588 eth protocol type */
1271 wr32(E1000_ETQF(IGB_ETQF_FILTER_1588), 0);
1273 /* L4 Queue Filter[3]: filter by destination port and protocol */
1275 u32 ftqf = (IPPROTO_UDP /* UDP */
1276 | E1000_FTQF_VF_BP /* VF not compared */
1277 | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
1278 | E1000_FTQF_MASK); /* mask all inputs */
1279 ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
1281 wr32(E1000_IMIR(3), (__force unsigned int)htons(PTP_EV_PORT));
1282 wr32(E1000_IMIREXT(3),
1283 (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
1284 if (hw->mac.type == e1000_82576) {
1285 /* enable source port check */
1286 wr32(E1000_SPQF(3), (__force unsigned int)htons(PTP_EV_PORT));
1287 ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
1289 wr32(E1000_FTQF(3), ftqf);
1291 wr32(E1000_FTQF(3), E1000_FTQF_MASK);
1295 /* clear TX/RX time stamp registers, just to be sure */
1296 regval = rd32(E1000_TXSTMPL);
1297 regval = rd32(E1000_TXSTMPH);
1298 regval = rd32(E1000_RXSTMPL);
1299 regval = rd32(E1000_RXSTMPH);
1305 * igb_ptp_set_ts_config - set hardware time stamping config
1306 * @netdev: netdev struct
1307 * @ifr: interface struct
1310 int igb_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
1312 struct igb_adapter *adapter = netdev_priv(netdev);
1313 struct hwtstamp_config config;
1316 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1319 err = igb_ptp_set_timestamp_mode(adapter, &config);
1323 /* save these settings for future reference */
1324 memcpy(&adapter->tstamp_config, &config,
1325 sizeof(adapter->tstamp_config));
1327 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1332 * igb_ptp_init - Initialize PTP functionality
1333 * @adapter: Board private structure
1335 * This function is called at device probe to initialize the PTP
1338 void igb_ptp_init(struct igb_adapter *adapter)
1340 struct e1000_hw *hw = &adapter->hw;
1341 struct net_device *netdev = adapter->netdev;
1343 switch (hw->mac.type) {
1345 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
1346 adapter->ptp_caps.owner = THIS_MODULE;
1347 adapter->ptp_caps.max_adj = 999999881;
1348 adapter->ptp_caps.n_ext_ts = 0;
1349 adapter->ptp_caps.pps = 0;
1350 adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
1351 adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
1352 adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_82576;
1353 adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
1354 adapter->ptp_caps.enable = igb_ptp_feature_enable;
1355 adapter->cc.read = igb_ptp_read_82576;
1356 adapter->cc.mask = CYCLECOUNTER_MASK(64);
1357 adapter->cc.mult = 1;
1358 adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
1359 adapter->ptp_flags |= IGB_PTP_OVERFLOW_CHECK;
1364 igb_ptp_sdp_init(adapter);
1365 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
1366 adapter->ptp_caps.owner = THIS_MODULE;
1367 adapter->ptp_caps.max_adj = 62499999;
1368 adapter->ptp_caps.n_ext_ts = IGB_N_EXTTS;
1369 adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
1370 adapter->ptp_caps.n_pins = IGB_N_SDP;
1371 adapter->ptp_caps.pps = 0;
1372 adapter->ptp_caps.pin_config = adapter->sdp_config;
1373 adapter->ptp_caps.adjfine = igb_ptp_adjfine_82580;
1374 adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
1375 adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_82580;
1376 adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
1377 adapter->ptp_caps.enable = igb_ptp_feature_enable_82580;
1378 adapter->ptp_caps.verify = igb_ptp_verify_pin;
1379 adapter->cc.read = igb_ptp_read_82580;
1380 adapter->cc.mask = CYCLECOUNTER_MASK(IGB_NBITS_82580);
1381 adapter->cc.mult = 1;
1382 adapter->cc.shift = 0;
1383 adapter->ptp_flags |= IGB_PTP_OVERFLOW_CHECK;
1387 igb_ptp_sdp_init(adapter);
1388 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
1389 adapter->ptp_caps.owner = THIS_MODULE;
1390 adapter->ptp_caps.max_adj = 62499999;
1391 adapter->ptp_caps.n_ext_ts = IGB_N_EXTTS;
1392 adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
1393 adapter->ptp_caps.n_pins = IGB_N_SDP;
1394 adapter->ptp_caps.pps = 1;
1395 adapter->ptp_caps.pin_config = adapter->sdp_config;
1396 adapter->ptp_caps.adjfine = igb_ptp_adjfine_82580;
1397 adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
1398 adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_i210;
1399 adapter->ptp_caps.settime64 = igb_ptp_settime_i210;
1400 adapter->ptp_caps.enable = igb_ptp_feature_enable_i210;
1401 adapter->ptp_caps.verify = igb_ptp_verify_pin;
1404 adapter->ptp_clock = NULL;
1408 spin_lock_init(&adapter->tmreg_lock);
1409 INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
1411 if (adapter->ptp_flags & IGB_PTP_OVERFLOW_CHECK)
1412 INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
1413 igb_ptp_overflow_check);
1415 adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
1416 adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
1418 igb_ptp_reset(adapter);
1420 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
1421 &adapter->pdev->dev);
1422 if (IS_ERR(adapter->ptp_clock)) {
1423 adapter->ptp_clock = NULL;
1424 dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
1425 } else if (adapter->ptp_clock) {
1426 dev_info(&adapter->pdev->dev, "added PHC on %s\n",
1427 adapter->netdev->name);
1428 adapter->ptp_flags |= IGB_PTP_ENABLED;
1433 * igb_ptp_sdp_init - utility function which inits the SDP config structs
1434 * @adapter: Board private structure.
1436 void igb_ptp_sdp_init(struct igb_adapter *adapter)
1440 for (i = 0; i < IGB_N_SDP; i++) {
1441 struct ptp_pin_desc *ppd = &adapter->sdp_config[i];
1443 snprintf(ppd->name, sizeof(ppd->name), "SDP%d", i);
1445 ppd->func = PTP_PF_NONE;
1450 * igb_ptp_suspend - Disable PTP work items and prepare for suspend
1451 * @adapter: Board private structure
1453 * This function stops the overflow check work and PTP Tx timestamp work, and
1454 * will prepare the device for OS suspend.
1456 void igb_ptp_suspend(struct igb_adapter *adapter)
1458 if (!(adapter->ptp_flags & IGB_PTP_ENABLED))
1461 if (adapter->ptp_flags & IGB_PTP_OVERFLOW_CHECK)
1462 cancel_delayed_work_sync(&adapter->ptp_overflow_work);
1464 cancel_work_sync(&adapter->ptp_tx_work);
1465 if (adapter->ptp_tx_skb) {
1466 dev_kfree_skb_any(adapter->ptp_tx_skb);
1467 adapter->ptp_tx_skb = NULL;
1468 clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
1473 * igb_ptp_stop - Disable PTP device and stop the overflow check.
1474 * @adapter: Board private structure.
1476 * This function stops the PTP support and cancels the delayed work.
1478 void igb_ptp_stop(struct igb_adapter *adapter)
1480 igb_ptp_suspend(adapter);
1482 if (adapter->ptp_clock) {
1483 ptp_clock_unregister(adapter->ptp_clock);
1484 dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
1485 adapter->netdev->name);
1486 adapter->ptp_flags &= ~IGB_PTP_ENABLED;
1491 * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
1492 * @adapter: Board private structure.
1494 * This function handles the reset work required to re-enable the PTP device.
1496 void igb_ptp_reset(struct igb_adapter *adapter)
1498 struct e1000_hw *hw = &adapter->hw;
1499 unsigned long flags;
1501 /* reset the tstamp_config */
1502 igb_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
1504 spin_lock_irqsave(&adapter->tmreg_lock, flags);
1506 switch (adapter->hw.mac.type) {
1508 /* Dial the nominal frequency. */
1509 wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
1516 wr32(E1000_TSAUXC, 0x0);
1517 wr32(E1000_TSSDP, 0x0);
1520 (adapter->pps_sys_wrap_on ? TSINTR_SYS_WRAP : 0));
1521 wr32(E1000_IMS, E1000_IMS_TS);
1524 /* No work to do. */
1528 /* Re-initialize the timer. */
1529 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
1530 struct timespec64 ts = ktime_to_timespec64(ktime_get_real());
1532 igb_ptp_write_i210(adapter, &ts);
1534 timecounter_init(&adapter->tc, &adapter->cc,
1535 ktime_to_ns(ktime_get_real()));
1538 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
1542 if (adapter->ptp_flags & IGB_PTP_OVERFLOW_CHECK)
1543 schedule_delayed_work(&adapter->ptp_overflow_work,
1544 IGB_SYSTIM_OVERFLOW_PERIOD);