1ae06c70bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0+
251dce24bSJeff Kirsher /* Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com> */
351dce24bSJeff Kirsher 
4d339b133SRichard Cochran #include <linux/module.h>
5d339b133SRichard Cochran #include <linux/device.h>
6d339b133SRichard Cochran #include <linux/pci.h>
7ba59814bSMatthew Vick #include <linux/ptp_classify.h>
8d339b133SRichard Cochran 
9d339b133SRichard Cochran #include "igb.h"
10d339b133SRichard Cochran 
11d339b133SRichard Cochran #define INCVALUE_MASK		0x7fffffff
12d339b133SRichard Cochran #define ISGN			0x80000000
13d339b133SRichard Cochran 
14b980ac18SJeff Kirsher /* The 82580 timesync updates the system timer every 8ns by 8ns,
157ebae817SRichard Cochran  * and this update value cannot be reprogrammed.
167ebae817SRichard Cochran  *
17d339b133SRichard Cochran  * Neither the 82576 nor the 82580 offer registers wide enough to hold
18d339b133SRichard Cochran  * nanoseconds time values for very long. For the 82580, SYSTIM always
19dbedd44eSJoe Perches  * counts nanoseconds, but the upper 24 bits are not available. The
20d339b133SRichard Cochran  * frequency is adjusted by changing the 32 bit fractional nanoseconds
21d339b133SRichard Cochran  * register, TIMINCA.
22d339b133SRichard Cochran  *
23d339b133SRichard Cochran  * For the 82576, the SYSTIM register time unit is affect by the
24d339b133SRichard Cochran  * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
25d339b133SRichard Cochran  * field are needed to provide the nominal 16 nanosecond period,
26d339b133SRichard Cochran  * leaving 19 bits for fractional nanoseconds.
27d339b133SRichard Cochran  *
287ebae817SRichard Cochran  * We scale the NIC clock cycle by a large factor so that relatively
297ebae817SRichard Cochran  * small clock corrections can be added or subtracted at each clock
307ebae817SRichard Cochran  * tick. The drawbacks of a large factor are a) that the clock
317ebae817SRichard Cochran  * register overflows more quickly (not such a big deal) and b) that
327ebae817SRichard Cochran  * the increment per tick has to fit into 24 bits.  As a result we
337ebae817SRichard Cochran  * need to use a shift of 19 so we can fit a value of 16 into the
347ebae817SRichard Cochran  * TIMINCA register.
357ebae817SRichard Cochran  *
36d339b133SRichard Cochran  *
37d339b133SRichard Cochran  *             SYSTIMH            SYSTIML
38d339b133SRichard Cochran  *        +--------------+   +---+---+------+
39d339b133SRichard Cochran  *  82576 |      32      |   | 8 | 5 |  19  |
40d339b133SRichard Cochran  *        +--------------+   +---+---+------+
41d339b133SRichard Cochran  *         \________ 45 bits _______/  fract
42d339b133SRichard Cochran  *
43d339b133SRichard Cochran  *        +----------+---+   +--------------+
44d339b133SRichard Cochran  *  82580 |    24    | 8 |   |      32      |
45d339b133SRichard Cochran  *        +----------+---+   +--------------+
46d339b133SRichard Cochran  *          reserved  \______ 40 bits _____/
47d339b133SRichard Cochran  *
48d339b133SRichard Cochran  *
49d339b133SRichard Cochran  * The 45 bit 82576 SYSTIM overflows every
50d339b133SRichard Cochran  *   2^45 * 10^-9 / 3600 = 9.77 hours.
51d339b133SRichard Cochran  *
52d339b133SRichard Cochran  * The 40 bit 82580 SYSTIM overflows every
53d339b133SRichard Cochran  *   2^40 * 10^-9 /  60  = 18.3 minutes.
54094bf4d0SMiroslav Lichvar  *
55094bf4d0SMiroslav Lichvar  * SYSTIM is converted to real time using a timecounter. As
564c9b658eSMiroslav Lichvar  * timecounter_cyc2time() allows old timestamps, the timecounter needs
574c9b658eSMiroslav Lichvar  * to be updated at least once per half of the SYSTIM interval.
584c9b658eSMiroslav Lichvar  * Scheduling of delayed work is not very accurate, and also the NIC
594c9b658eSMiroslav Lichvar  * clock can be adjusted to run up to 6% faster and the system clock
604c9b658eSMiroslav Lichvar  * up to 10% slower, so we aim for 6 minutes to be sure the actual
614c9b658eSMiroslav Lichvar  * interval in the NIC time is shorter than 9.16 minutes.
62d339b133SRichard Cochran  */
63d339b133SRichard Cochran 
644c9b658eSMiroslav Lichvar #define IGB_SYSTIM_OVERFLOW_PERIOD	(HZ * 60 * 6)
65428f1f71SMatthew Vick #define IGB_PTP_TX_TIMEOUT		(HZ * 15)
66a51d8c21SJacob Keller #define INCPERIOD_82576			BIT(E1000_TIMINCA_16NS_SHIFT)
67a51d8c21SJacob Keller #define INCVALUE_82576_MASK		GENMASK(E1000_TIMINCA_16NS_SHIFT - 1, 0)
68a51d8c21SJacob Keller #define INCVALUE_82576			(16u << IGB_82576_TSYNC_SHIFT)
69d339b133SRichard Cochran #define IGB_NBITS_82580			40
70d71980d4SAndrii Staikov #define IGB_82580_BASE_PERIOD		0x800000000
71d339b133SRichard Cochran 
72167f3f71SJeff Kirsher static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter);
738ab55abaSRuud Bos static void igb_ptp_sdp_init(struct igb_adapter *adapter);
74167f3f71SJeff Kirsher 
75b980ac18SJeff Kirsher /* SYSTIM read access for the 82576 */
igb_ptp_read_82576(const struct cyclecounter * cc)76a5a1d1c2SThomas Gleixner static u64 igb_ptp_read_82576(const struct cyclecounter *cc)
77d339b133SRichard Cochran {
78d339b133SRichard Cochran 	struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
79d339b133SRichard Cochran 	struct e1000_hw *hw = &igb->hw;
80a79f4f88SMatthew Vick 	u64 val;
81a79f4f88SMatthew Vick 	u32 lo, hi;
82d339b133SRichard Cochran 
83d339b133SRichard Cochran 	lo = rd32(E1000_SYSTIML);
84d339b133SRichard Cochran 	hi = rd32(E1000_SYSTIMH);
85d339b133SRichard Cochran 
86d339b133SRichard Cochran 	val = ((u64) hi) << 32;
87d339b133SRichard Cochran 	val |= lo;
88d339b133SRichard Cochran 
89d339b133SRichard Cochran 	return val;
90d339b133SRichard Cochran }
91d339b133SRichard Cochran 
92b980ac18SJeff Kirsher /* SYSTIM read access for the 82580 */
igb_ptp_read_82580(const struct cyclecounter * cc)93a5a1d1c2SThomas Gleixner static u64 igb_ptp_read_82580(const struct cyclecounter *cc)
94d339b133SRichard Cochran {
95d339b133SRichard Cochran 	struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
96d339b133SRichard Cochran 	struct e1000_hw *hw = &igb->hw;
97e5c3370fSAkeem G Abodunrin 	u32 lo, hi;
98a79f4f88SMatthew Vick 	u64 val;
99d339b133SRichard Cochran 
100b980ac18SJeff Kirsher 	/* The timestamp latches on lowest register read. For the 82580
1017ebae817SRichard Cochran 	 * the lowest register is SYSTIMR instead of SYSTIML.  However we only
1027ebae817SRichard Cochran 	 * need to provide nanosecond resolution, so we just ignore it.
1037ebae817SRichard Cochran 	 */
104e5c3370fSAkeem G Abodunrin 	rd32(E1000_SYSTIMR);
105d339b133SRichard Cochran 	lo = rd32(E1000_SYSTIML);
106d339b133SRichard Cochran 	hi = rd32(E1000_SYSTIMH);
107d339b133SRichard Cochran 
108d339b133SRichard Cochran 	val = ((u64) hi) << 32;
109d339b133SRichard Cochran 	val |= lo;
110d339b133SRichard Cochran 
111d339b133SRichard Cochran 	return val;
112d339b133SRichard Cochran }
113d339b133SRichard Cochran 
114b980ac18SJeff Kirsher /* SYSTIM read access for I210/I211 */
igb_ptp_read_i210(struct igb_adapter * adapter,struct timespec64 * ts)115d4c496feSRichard Cochran static void igb_ptp_read_i210(struct igb_adapter *adapter,
116d4c496feSRichard Cochran 			      struct timespec64 *ts)
117e57b8bdbSMatthew Vick {
118e57b8bdbSMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
119e5c3370fSAkeem G Abodunrin 	u32 sec, nsec;
120e57b8bdbSMatthew Vick 
121b980ac18SJeff Kirsher 	/* The timestamp latches on lowest register read. For I210/I211, the
122e57b8bdbSMatthew Vick 	 * lowest register is SYSTIMR. Since we only need to provide nanosecond
123e57b8bdbSMatthew Vick 	 * resolution, we can ignore it.
124e57b8bdbSMatthew Vick 	 */
125e5c3370fSAkeem G Abodunrin 	rd32(E1000_SYSTIMR);
126e57b8bdbSMatthew Vick 	nsec = rd32(E1000_SYSTIML);
127e57b8bdbSMatthew Vick 	sec = rd32(E1000_SYSTIMH);
128e57b8bdbSMatthew Vick 
129e57b8bdbSMatthew Vick 	ts->tv_sec = sec;
130e57b8bdbSMatthew Vick 	ts->tv_nsec = nsec;
131e57b8bdbSMatthew Vick }
132e57b8bdbSMatthew Vick 
igb_ptp_write_i210(struct igb_adapter * adapter,const struct timespec64 * ts)133e57b8bdbSMatthew Vick static void igb_ptp_write_i210(struct igb_adapter *adapter,
134d4c496feSRichard Cochran 			       const struct timespec64 *ts)
135e57b8bdbSMatthew Vick {
136e57b8bdbSMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
137e57b8bdbSMatthew Vick 
138b980ac18SJeff Kirsher 	/* Writing the SYSTIMR register is not necessary as it only provides
139e57b8bdbSMatthew Vick 	 * sub-nanosecond resolution.
140e57b8bdbSMatthew Vick 	 */
141e57b8bdbSMatthew Vick 	wr32(E1000_SYSTIML, ts->tv_nsec);
14240c9b079SArnd Bergmann 	wr32(E1000_SYSTIMH, (u32)ts->tv_sec);
143e57b8bdbSMatthew Vick }
144e57b8bdbSMatthew Vick 
145a79f4f88SMatthew Vick /**
146a79f4f88SMatthew Vick  * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
147a79f4f88SMatthew Vick  * @adapter: board private structure
148a79f4f88SMatthew Vick  * @hwtstamps: timestamp structure to update
149a79f4f88SMatthew Vick  * @systim: unsigned 64bit system time value.
150a79f4f88SMatthew Vick  *
151a79f4f88SMatthew Vick  * We need to convert the system time value stored in the RX/TXSTMP registers
152a79f4f88SMatthew Vick  * into a hwtstamp which can be used by the upper level timestamping functions.
153a79f4f88SMatthew Vick  *
154a79f4f88SMatthew Vick  * The 'tmreg_lock' spinlock is used to protect the consistency of the
155a79f4f88SMatthew Vick  * system time value. This is needed because reading the 64 bit time
156a79f4f88SMatthew Vick  * value involves reading two (or three) 32 bit registers. The first
157a79f4f88SMatthew Vick  * read latches the value. Ditto for writing.
158a79f4f88SMatthew Vick  *
159a79f4f88SMatthew Vick  * In addition, here have extended the system time with an overflow
160a79f4f88SMatthew Vick  * counter in software.
161a79f4f88SMatthew Vick  **/
igb_ptp_systim_to_hwtstamp(struct igb_adapter * adapter,struct skb_shared_hwtstamps * hwtstamps,u64 systim)162a79f4f88SMatthew Vick static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
163a79f4f88SMatthew Vick 				       struct skb_shared_hwtstamps *hwtstamps,
164a79f4f88SMatthew Vick 				       u64 systim)
165a79f4f88SMatthew Vick {
166a79f4f88SMatthew Vick 	unsigned long flags;
167a79f4f88SMatthew Vick 	u64 ns;
168a79f4f88SMatthew Vick 
1695d705de0STom Rix 	memset(hwtstamps, 0, sizeof(*hwtstamps));
1705d705de0STom Rix 
171a79f4f88SMatthew Vick 	switch (adapter->hw.mac.type) {
172a79f4f88SMatthew Vick 	case e1000_82576:
173e57b8bdbSMatthew Vick 	case e1000_82580:
174ceb5f13bSCarolyn Wyborny 	case e1000_i354:
175e57b8bdbSMatthew Vick 	case e1000_i350:
176a79f4f88SMatthew Vick 		spin_lock_irqsave(&adapter->tmreg_lock, flags);
177a79f4f88SMatthew Vick 		ns = timecounter_cyc2time(&adapter->tc, systim);
178a79f4f88SMatthew Vick 		spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
179a79f4f88SMatthew Vick 
180a79f4f88SMatthew Vick 		hwtstamps->hwtstamp = ns_to_ktime(ns);
181e57b8bdbSMatthew Vick 		break;
182e57b8bdbSMatthew Vick 	case e1000_i210:
183e57b8bdbSMatthew Vick 	case e1000_i211:
184e57b8bdbSMatthew Vick 		/* Upper 32 bits contain s, lower 32 bits contain ns. */
185e57b8bdbSMatthew Vick 		hwtstamps->hwtstamp = ktime_set(systim >> 32,
186e57b8bdbSMatthew Vick 						systim & 0xFFFFFFFF);
187e57b8bdbSMatthew Vick 		break;
188e57b8bdbSMatthew Vick 	default:
189e57b8bdbSMatthew Vick 		break;
190e57b8bdbSMatthew Vick 	}
191a79f4f88SMatthew Vick }
192a79f4f88SMatthew Vick 
193b980ac18SJeff Kirsher /* PTP clock operations */
igb_ptp_adjfine_82576(struct ptp_clock_info * ptp,long scaled_ppm)194d8fae250SJacob Keller static int igb_ptp_adjfine_82576(struct ptp_clock_info *ptp, long scaled_ppm)
195d339b133SRichard Cochran {
196a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
197a79f4f88SMatthew Vick 					       ptp_caps);
198a79f4f88SMatthew Vick 	struct e1000_hw *hw = &igb->hw;
1991060707eSJacob Keller 	u64 incvalue;
200d339b133SRichard Cochran 
2011060707eSJacob Keller 	incvalue = adjust_by_scaled_ppm(INCVALUE_82576, scaled_ppm);
202d339b133SRichard Cochran 
203d339b133SRichard Cochran 	wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
204d339b133SRichard Cochran 
205d339b133SRichard Cochran 	return 0;
206d339b133SRichard Cochran }
207d339b133SRichard Cochran 
igb_ptp_adjfine_82580(struct ptp_clock_info * ptp,long scaled_ppm)208c79e975eSRichard Cochran static int igb_ptp_adjfine_82580(struct ptp_clock_info *ptp, long scaled_ppm)
209d339b133SRichard Cochran {
210a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
211a79f4f88SMatthew Vick 					       ptp_caps);
212a79f4f88SMatthew Vick 	struct e1000_hw *hw = &igb->hw;
213d71980d4SAndrii Staikov 	bool neg_adj;
214d339b133SRichard Cochran 	u64 rate;
215d339b133SRichard Cochran 	u32 inca;
216d339b133SRichard Cochran 
217d71980d4SAndrii Staikov 	neg_adj = diff_by_scaled_ppm(IGB_82580_BASE_PERIOD, scaled_ppm, &rate);
218d339b133SRichard Cochran 
219d339b133SRichard Cochran 	inca = rate & INCVALUE_MASK;
220d339b133SRichard Cochran 	if (neg_adj)
221d339b133SRichard Cochran 		inca |= ISGN;
222d339b133SRichard Cochran 
223d339b133SRichard Cochran 	wr32(E1000_TIMINCA, inca);
224d339b133SRichard Cochran 
225d339b133SRichard Cochran 	return 0;
226d339b133SRichard Cochran }
227d339b133SRichard Cochran 
igb_ptp_adjtime_82576(struct ptp_clock_info * ptp,s64 delta)228e57b8bdbSMatthew Vick static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
229d339b133SRichard Cochran {
230a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
231a79f4f88SMatthew Vick 					       ptp_caps);
232d339b133SRichard Cochran 	unsigned long flags;
233d339b133SRichard Cochran 
234d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
2355ee698e3SRichard Cochran 	timecounter_adjtime(&igb->tc, delta);
236d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
237d339b133SRichard Cochran 
238d339b133SRichard Cochran 	return 0;
239d339b133SRichard Cochran }
240d339b133SRichard Cochran 
igb_ptp_adjtime_i210(struct ptp_clock_info * ptp,s64 delta)241e57b8bdbSMatthew Vick static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
242e57b8bdbSMatthew Vick {
243e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
244e57b8bdbSMatthew Vick 					       ptp_caps);
245e57b8bdbSMatthew Vick 	unsigned long flags;
246d4c496feSRichard Cochran 	struct timespec64 now, then = ns_to_timespec64(delta);
247e57b8bdbSMatthew Vick 
248e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
249e57b8bdbSMatthew Vick 
250e57b8bdbSMatthew Vick 	igb_ptp_read_i210(igb, &now);
251d4c496feSRichard Cochran 	now = timespec64_add(now, then);
252d4c496feSRichard Cochran 	igb_ptp_write_i210(igb, (const struct timespec64 *)&now);
253e57b8bdbSMatthew Vick 
254e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
255e57b8bdbSMatthew Vick 
256e57b8bdbSMatthew Vick 	return 0;
257e57b8bdbSMatthew Vick }
258e57b8bdbSMatthew Vick 
igb_ptp_gettimex_82576(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)259cff8ba28SMiroslav Lichvar static int igb_ptp_gettimex_82576(struct ptp_clock_info *ptp,
260cff8ba28SMiroslav Lichvar 				  struct timespec64 *ts,
261cff8ba28SMiroslav Lichvar 				  struct ptp_system_timestamp *sts)
262d339b133SRichard Cochran {
263a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
264a79f4f88SMatthew Vick 					       ptp_caps);
265cff8ba28SMiroslav Lichvar 	struct e1000_hw *hw = &igb->hw;
266a79f4f88SMatthew Vick 	unsigned long flags;
267cff8ba28SMiroslav Lichvar 	u32 lo, hi;
268d339b133SRichard Cochran 	u64 ns;
269d339b133SRichard Cochran 
270d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
271d339b133SRichard Cochran 
272cff8ba28SMiroslav Lichvar 	ptp_read_system_prets(sts);
273cff8ba28SMiroslav Lichvar 	lo = rd32(E1000_SYSTIML);
274cff8ba28SMiroslav Lichvar 	ptp_read_system_postts(sts);
275cff8ba28SMiroslav Lichvar 	hi = rd32(E1000_SYSTIMH);
276cff8ba28SMiroslav Lichvar 
277cff8ba28SMiroslav Lichvar 	ns = timecounter_cyc2time(&igb->tc, ((u64)hi << 32) | lo);
278d339b133SRichard Cochran 
279d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
280d339b133SRichard Cochran 
281350f66d5SRichard Cochran 	*ts = ns_to_timespec64(ns);
282d339b133SRichard Cochran 
283d339b133SRichard Cochran 	return 0;
284d339b133SRichard Cochran }
285d339b133SRichard Cochran 
igb_ptp_gettimex_82580(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)286cff8ba28SMiroslav Lichvar static int igb_ptp_gettimex_82580(struct ptp_clock_info *ptp,
287cff8ba28SMiroslav Lichvar 				  struct timespec64 *ts,
288cff8ba28SMiroslav Lichvar 				  struct ptp_system_timestamp *sts)
289e57b8bdbSMatthew Vick {
290e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
291e57b8bdbSMatthew Vick 					       ptp_caps);
292cff8ba28SMiroslav Lichvar 	struct e1000_hw *hw = &igb->hw;
293cff8ba28SMiroslav Lichvar 	unsigned long flags;
294cff8ba28SMiroslav Lichvar 	u32 lo, hi;
295cff8ba28SMiroslav Lichvar 	u64 ns;
296cff8ba28SMiroslav Lichvar 
297cff8ba28SMiroslav Lichvar 	spin_lock_irqsave(&igb->tmreg_lock, flags);
298cff8ba28SMiroslav Lichvar 
299cff8ba28SMiroslav Lichvar 	ptp_read_system_prets(sts);
300cff8ba28SMiroslav Lichvar 	rd32(E1000_SYSTIMR);
301cff8ba28SMiroslav Lichvar 	ptp_read_system_postts(sts);
302cff8ba28SMiroslav Lichvar 	lo = rd32(E1000_SYSTIML);
303cff8ba28SMiroslav Lichvar 	hi = rd32(E1000_SYSTIMH);
304cff8ba28SMiroslav Lichvar 
305cff8ba28SMiroslav Lichvar 	ns = timecounter_cyc2time(&igb->tc, ((u64)hi << 32) | lo);
306cff8ba28SMiroslav Lichvar 
307cff8ba28SMiroslav Lichvar 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
308cff8ba28SMiroslav Lichvar 
309cff8ba28SMiroslav Lichvar 	*ts = ns_to_timespec64(ns);
310cff8ba28SMiroslav Lichvar 
311cff8ba28SMiroslav Lichvar 	return 0;
312cff8ba28SMiroslav Lichvar }
313cff8ba28SMiroslav Lichvar 
igb_ptp_gettimex_i210(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)314cff8ba28SMiroslav Lichvar static int igb_ptp_gettimex_i210(struct ptp_clock_info *ptp,
315cff8ba28SMiroslav Lichvar 				 struct timespec64 *ts,
316cff8ba28SMiroslav Lichvar 				 struct ptp_system_timestamp *sts)
317cff8ba28SMiroslav Lichvar {
318cff8ba28SMiroslav Lichvar 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
319cff8ba28SMiroslav Lichvar 					       ptp_caps);
320cff8ba28SMiroslav Lichvar 	struct e1000_hw *hw = &igb->hw;
321e57b8bdbSMatthew Vick 	unsigned long flags;
322e57b8bdbSMatthew Vick 
323e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
324e57b8bdbSMatthew Vick 
325cff8ba28SMiroslav Lichvar 	ptp_read_system_prets(sts);
326cff8ba28SMiroslav Lichvar 	rd32(E1000_SYSTIMR);
327cff8ba28SMiroslav Lichvar 	ptp_read_system_postts(sts);
328cff8ba28SMiroslav Lichvar 	ts->tv_nsec = rd32(E1000_SYSTIML);
329cff8ba28SMiroslav Lichvar 	ts->tv_sec = rd32(E1000_SYSTIMH);
330e57b8bdbSMatthew Vick 
331e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
332e57b8bdbSMatthew Vick 
333e57b8bdbSMatthew Vick 	return 0;
334e57b8bdbSMatthew Vick }
335e57b8bdbSMatthew Vick 
igb_ptp_settime_82576(struct ptp_clock_info * ptp,const struct timespec64 * ts)336e57b8bdbSMatthew Vick static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
337d4c496feSRichard Cochran 				 const struct timespec64 *ts)
338d339b133SRichard Cochran {
339a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
340a79f4f88SMatthew Vick 					       ptp_caps);
341d339b133SRichard Cochran 	unsigned long flags;
342a79f4f88SMatthew Vick 	u64 ns;
343d339b133SRichard Cochran 
344350f66d5SRichard Cochran 	ns = timespec64_to_ns(ts);
345d339b133SRichard Cochran 
346d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
347d339b133SRichard Cochran 
348d339b133SRichard Cochran 	timecounter_init(&igb->tc, &igb->cc, ns);
349d339b133SRichard Cochran 
350d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
351d339b133SRichard Cochran 
352d339b133SRichard Cochran 	return 0;
353d339b133SRichard Cochran }
354d339b133SRichard Cochran 
igb_ptp_settime_i210(struct ptp_clock_info * ptp,const struct timespec64 * ts)355e57b8bdbSMatthew Vick static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
356d4c496feSRichard Cochran 				const struct timespec64 *ts)
357e57b8bdbSMatthew Vick {
358e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
359e57b8bdbSMatthew Vick 					       ptp_caps);
360e57b8bdbSMatthew Vick 	unsigned long flags;
361e57b8bdbSMatthew Vick 
362e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
363e57b8bdbSMatthew Vick 
364e57b8bdbSMatthew Vick 	igb_ptp_write_i210(igb, ts);
365e57b8bdbSMatthew Vick 
366e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
367e57b8bdbSMatthew Vick 
368e57b8bdbSMatthew Vick 	return 0;
369e57b8bdbSMatthew Vick }
370e57b8bdbSMatthew Vick 
igb_pin_direction(int pin,int input,u32 * ctrl,u32 * ctrl_ext)371720db4ffSRichard Cochran static void igb_pin_direction(int pin, int input, u32 *ctrl, u32 *ctrl_ext)
372720db4ffSRichard Cochran {
373720db4ffSRichard Cochran 	u32 *ptr = pin < 2 ? ctrl : ctrl_ext;
374b23c0cc5SAlexander Duyck 	static const u32 mask[IGB_N_SDP] = {
375720db4ffSRichard Cochran 		E1000_CTRL_SDP0_DIR,
376720db4ffSRichard Cochran 		E1000_CTRL_SDP1_DIR,
377720db4ffSRichard Cochran 		E1000_CTRL_EXT_SDP2_DIR,
378720db4ffSRichard Cochran 		E1000_CTRL_EXT_SDP3_DIR,
379720db4ffSRichard Cochran 	};
380720db4ffSRichard Cochran 
381720db4ffSRichard Cochran 	if (input)
382720db4ffSRichard Cochran 		*ptr &= ~mask[pin];
383720db4ffSRichard Cochran 	else
384720db4ffSRichard Cochran 		*ptr |= mask[pin];
385720db4ffSRichard Cochran }
386720db4ffSRichard Cochran 
igb_pin_extts(struct igb_adapter * igb,int chan,int pin)387720db4ffSRichard Cochran static void igb_pin_extts(struct igb_adapter *igb, int chan, int pin)
388720db4ffSRichard Cochran {
389b23c0cc5SAlexander Duyck 	static const u32 aux0_sel_sdp[IGB_N_SDP] = {
390720db4ffSRichard Cochran 		AUX0_SEL_SDP0, AUX0_SEL_SDP1, AUX0_SEL_SDP2, AUX0_SEL_SDP3,
391720db4ffSRichard Cochran 	};
392b23c0cc5SAlexander Duyck 	static const u32 aux1_sel_sdp[IGB_N_SDP] = {
393720db4ffSRichard Cochran 		AUX1_SEL_SDP0, AUX1_SEL_SDP1, AUX1_SEL_SDP2, AUX1_SEL_SDP3,
394720db4ffSRichard Cochran 	};
395b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_en[IGB_N_SDP] = {
396720db4ffSRichard Cochran 		TS_SDP0_EN, TS_SDP1_EN, TS_SDP2_EN, TS_SDP3_EN,
397720db4ffSRichard Cochran 	};
398b23c0cc5SAlexander Duyck 	struct e1000_hw *hw = &igb->hw;
399720db4ffSRichard Cochran 	u32 ctrl, ctrl_ext, tssdp = 0;
400720db4ffSRichard Cochran 
401720db4ffSRichard Cochran 	ctrl = rd32(E1000_CTRL);
402720db4ffSRichard Cochran 	ctrl_ext = rd32(E1000_CTRL_EXT);
403720db4ffSRichard Cochran 	tssdp = rd32(E1000_TSSDP);
404720db4ffSRichard Cochran 
405720db4ffSRichard Cochran 	igb_pin_direction(pin, 1, &ctrl, &ctrl_ext);
406720db4ffSRichard Cochran 
407720db4ffSRichard Cochran 	/* Make sure this pin is not enabled as an output. */
408720db4ffSRichard Cochran 	tssdp &= ~ts_sdp_en[pin];
409720db4ffSRichard Cochran 
410720db4ffSRichard Cochran 	if (chan == 1) {
411720db4ffSRichard Cochran 		tssdp &= ~AUX1_SEL_SDP3;
412720db4ffSRichard Cochran 		tssdp |= aux1_sel_sdp[pin] | AUX1_TS_SDP_EN;
413720db4ffSRichard Cochran 	} else {
414720db4ffSRichard Cochran 		tssdp &= ~AUX0_SEL_SDP3;
415720db4ffSRichard Cochran 		tssdp |= aux0_sel_sdp[pin] | AUX0_TS_SDP_EN;
416720db4ffSRichard Cochran 	}
417720db4ffSRichard Cochran 
418720db4ffSRichard Cochran 	wr32(E1000_TSSDP, tssdp);
419720db4ffSRichard Cochran 	wr32(E1000_CTRL, ctrl);
420720db4ffSRichard Cochran 	wr32(E1000_CTRL_EXT, ctrl_ext);
421720db4ffSRichard Cochran }
422720db4ffSRichard Cochran 
igb_pin_perout(struct igb_adapter * igb,int chan,int pin,int freq)42330c72916SRichard Cochran static void igb_pin_perout(struct igb_adapter *igb, int chan, int pin, int freq)
424720db4ffSRichard Cochran {
425b23c0cc5SAlexander Duyck 	static const u32 aux0_sel_sdp[IGB_N_SDP] = {
426720db4ffSRichard Cochran 		AUX0_SEL_SDP0, AUX0_SEL_SDP1, AUX0_SEL_SDP2, AUX0_SEL_SDP3,
427720db4ffSRichard Cochran 	};
428b23c0cc5SAlexander Duyck 	static const u32 aux1_sel_sdp[IGB_N_SDP] = {
429720db4ffSRichard Cochran 		AUX1_SEL_SDP0, AUX1_SEL_SDP1, AUX1_SEL_SDP2, AUX1_SEL_SDP3,
430720db4ffSRichard Cochran 	};
431b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_en[IGB_N_SDP] = {
432720db4ffSRichard Cochran 		TS_SDP0_EN, TS_SDP1_EN, TS_SDP2_EN, TS_SDP3_EN,
433720db4ffSRichard Cochran 	};
434b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_sel_tt0[IGB_N_SDP] = {
435720db4ffSRichard Cochran 		TS_SDP0_SEL_TT0, TS_SDP1_SEL_TT0,
436720db4ffSRichard Cochran 		TS_SDP2_SEL_TT0, TS_SDP3_SEL_TT0,
437720db4ffSRichard Cochran 	};
438b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_sel_tt1[IGB_N_SDP] = {
439720db4ffSRichard Cochran 		TS_SDP0_SEL_TT1, TS_SDP1_SEL_TT1,
440720db4ffSRichard Cochran 		TS_SDP2_SEL_TT1, TS_SDP3_SEL_TT1,
441720db4ffSRichard Cochran 	};
44230c72916SRichard Cochran 	static const u32 ts_sdp_sel_fc0[IGB_N_SDP] = {
44330c72916SRichard Cochran 		TS_SDP0_SEL_FC0, TS_SDP1_SEL_FC0,
44430c72916SRichard Cochran 		TS_SDP2_SEL_FC0, TS_SDP3_SEL_FC0,
44530c72916SRichard Cochran 	};
44630c72916SRichard Cochran 	static const u32 ts_sdp_sel_fc1[IGB_N_SDP] = {
44730c72916SRichard Cochran 		TS_SDP0_SEL_FC1, TS_SDP1_SEL_FC1,
44830c72916SRichard Cochran 		TS_SDP2_SEL_FC1, TS_SDP3_SEL_FC1,
44930c72916SRichard Cochran 	};
450b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_sel_clr[IGB_N_SDP] = {
451720db4ffSRichard Cochran 		TS_SDP0_SEL_FC1, TS_SDP1_SEL_FC1,
452720db4ffSRichard Cochran 		TS_SDP2_SEL_FC1, TS_SDP3_SEL_FC1,
453720db4ffSRichard Cochran 	};
454b23c0cc5SAlexander Duyck 	struct e1000_hw *hw = &igb->hw;
455720db4ffSRichard Cochran 	u32 ctrl, ctrl_ext, tssdp = 0;
456720db4ffSRichard Cochran 
457720db4ffSRichard Cochran 	ctrl = rd32(E1000_CTRL);
458720db4ffSRichard Cochran 	ctrl_ext = rd32(E1000_CTRL_EXT);
459720db4ffSRichard Cochran 	tssdp = rd32(E1000_TSSDP);
460720db4ffSRichard Cochran 
461720db4ffSRichard Cochran 	igb_pin_direction(pin, 0, &ctrl, &ctrl_ext);
462720db4ffSRichard Cochran 
463720db4ffSRichard Cochran 	/* Make sure this pin is not enabled as an input. */
464720db4ffSRichard Cochran 	if ((tssdp & AUX0_SEL_SDP3) == aux0_sel_sdp[pin])
465720db4ffSRichard Cochran 		tssdp &= ~AUX0_TS_SDP_EN;
466720db4ffSRichard Cochran 
467720db4ffSRichard Cochran 	if ((tssdp & AUX1_SEL_SDP3) == aux1_sel_sdp[pin])
468720db4ffSRichard Cochran 		tssdp &= ~AUX1_TS_SDP_EN;
469720db4ffSRichard Cochran 
470720db4ffSRichard Cochran 	tssdp &= ~ts_sdp_sel_clr[pin];
47130c72916SRichard Cochran 	if (freq) {
47230c72916SRichard Cochran 		if (chan == 1)
47330c72916SRichard Cochran 			tssdp |= ts_sdp_sel_fc1[pin];
47430c72916SRichard Cochran 		else
47530c72916SRichard Cochran 			tssdp |= ts_sdp_sel_fc0[pin];
47630c72916SRichard Cochran 	} else {
477720db4ffSRichard Cochran 		if (chan == 1)
478720db4ffSRichard Cochran 			tssdp |= ts_sdp_sel_tt1[pin];
479720db4ffSRichard Cochran 		else
480720db4ffSRichard Cochran 			tssdp |= ts_sdp_sel_tt0[pin];
48130c72916SRichard Cochran 	}
482720db4ffSRichard Cochran 	tssdp |= ts_sdp_en[pin];
483720db4ffSRichard Cochran 
484720db4ffSRichard Cochran 	wr32(E1000_TSSDP, tssdp);
485720db4ffSRichard Cochran 	wr32(E1000_CTRL, ctrl);
486720db4ffSRichard Cochran 	wr32(E1000_CTRL_EXT, ctrl_ext);
487720db4ffSRichard Cochran }
488720db4ffSRichard Cochran 
igb_ptp_feature_enable_82580(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)4891819fc75SRuud Bos static int igb_ptp_feature_enable_82580(struct ptp_clock_info *ptp,
4901819fc75SRuud Bos 					struct ptp_clock_request *rq, int on)
4911819fc75SRuud Bos {
4921819fc75SRuud Bos 	struct igb_adapter *igb =
4931819fc75SRuud Bos 		container_of(ptp, struct igb_adapter, ptp_caps);
4941819fc75SRuud Bos 	u32 tsauxc, tsim, tsauxc_mask, tsim_mask, trgttiml, trgttimh, systiml,
4951819fc75SRuud Bos 		systimh, level_mask, level, rem;
4961819fc75SRuud Bos 	struct e1000_hw *hw = &igb->hw;
4971819fc75SRuud Bos 	struct timespec64 ts, start;
4981819fc75SRuud Bos 	unsigned long flags;
4991819fc75SRuud Bos 	u64 systim, now;
5001819fc75SRuud Bos 	int pin = -1;
5011819fc75SRuud Bos 	s64 ns;
5021819fc75SRuud Bos 
5031819fc75SRuud Bos 	switch (rq->type) {
5041819fc75SRuud Bos 	case PTP_CLK_REQ_EXTTS:
50538970eacSRuud Bos 		/* Reject requests with unsupported flags */
50638970eacSRuud Bos 		if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
50738970eacSRuud Bos 					PTP_RISING_EDGE |
50838970eacSRuud Bos 					PTP_FALLING_EDGE |
50938970eacSRuud Bos 					PTP_STRICT_FLAGS))
5101819fc75SRuud Bos 			return -EOPNOTSUPP;
5111819fc75SRuud Bos 
51238970eacSRuud Bos 		if (on) {
51338970eacSRuud Bos 			pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS,
51438970eacSRuud Bos 					   rq->extts.index);
51538970eacSRuud Bos 			if (pin < 0)
51638970eacSRuud Bos 				return -EBUSY;
51738970eacSRuud Bos 		}
51838970eacSRuud Bos 		if (rq->extts.index == 1) {
51938970eacSRuud Bos 			tsauxc_mask = TSAUXC_EN_TS1;
52038970eacSRuud Bos 			tsim_mask = TSINTR_AUTT1;
52138970eacSRuud Bos 		} else {
52238970eacSRuud Bos 			tsauxc_mask = TSAUXC_EN_TS0;
52338970eacSRuud Bos 			tsim_mask = TSINTR_AUTT0;
52438970eacSRuud Bos 		}
52538970eacSRuud Bos 		spin_lock_irqsave(&igb->tmreg_lock, flags);
52638970eacSRuud Bos 		tsauxc = rd32(E1000_TSAUXC);
52738970eacSRuud Bos 		tsim = rd32(E1000_TSIM);
52838970eacSRuud Bos 		if (on) {
52938970eacSRuud Bos 			igb_pin_extts(igb, rq->extts.index, pin);
53038970eacSRuud Bos 			tsauxc |= tsauxc_mask;
53138970eacSRuud Bos 			tsim |= tsim_mask;
53238970eacSRuud Bos 		} else {
53338970eacSRuud Bos 			tsauxc &= ~tsauxc_mask;
53438970eacSRuud Bos 			tsim &= ~tsim_mask;
53538970eacSRuud Bos 		}
53638970eacSRuud Bos 		wr32(E1000_TSAUXC, tsauxc);
53738970eacSRuud Bos 		wr32(E1000_TSIM, tsim);
53838970eacSRuud Bos 		spin_unlock_irqrestore(&igb->tmreg_lock, flags);
53938970eacSRuud Bos 		return 0;
54038970eacSRuud Bos 
5411819fc75SRuud Bos 	case PTP_CLK_REQ_PEROUT:
5421819fc75SRuud Bos 		/* Reject requests with unsupported flags */
5431819fc75SRuud Bos 		if (rq->perout.flags)
5441819fc75SRuud Bos 			return -EOPNOTSUPP;
5451819fc75SRuud Bos 
5461819fc75SRuud Bos 		if (on) {
5471819fc75SRuud Bos 			pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT,
5481819fc75SRuud Bos 					   rq->perout.index);
5491819fc75SRuud Bos 			if (pin < 0)
5501819fc75SRuud Bos 				return -EBUSY;
5511819fc75SRuud Bos 		}
5521819fc75SRuud Bos 		ts.tv_sec = rq->perout.period.sec;
5531819fc75SRuud Bos 		ts.tv_nsec = rq->perout.period.nsec;
5541819fc75SRuud Bos 		ns = timespec64_to_ns(&ts);
5551819fc75SRuud Bos 		ns = ns >> 1;
5561819fc75SRuud Bos 		if (on && ns < 8LL)
5571819fc75SRuud Bos 			return -EINVAL;
5581819fc75SRuud Bos 		ts = ns_to_timespec64(ns);
5591819fc75SRuud Bos 		if (rq->perout.index == 1) {
5601819fc75SRuud Bos 			tsauxc_mask = TSAUXC_EN_TT1;
5611819fc75SRuud Bos 			tsim_mask = TSINTR_TT1;
5621819fc75SRuud Bos 			trgttiml = E1000_TRGTTIML1;
5631819fc75SRuud Bos 			trgttimh = E1000_TRGTTIMH1;
5641819fc75SRuud Bos 		} else {
5651819fc75SRuud Bos 			tsauxc_mask = TSAUXC_EN_TT0;
5661819fc75SRuud Bos 			tsim_mask = TSINTR_TT0;
5671819fc75SRuud Bos 			trgttiml = E1000_TRGTTIML0;
5681819fc75SRuud Bos 			trgttimh = E1000_TRGTTIMH0;
5691819fc75SRuud Bos 		}
5701819fc75SRuud Bos 		spin_lock_irqsave(&igb->tmreg_lock, flags);
5711819fc75SRuud Bos 		tsauxc = rd32(E1000_TSAUXC);
5721819fc75SRuud Bos 		tsim = rd32(E1000_TSIM);
5731819fc75SRuud Bos 		if (rq->perout.index == 1) {
5741819fc75SRuud Bos 			tsauxc &= ~(TSAUXC_EN_TT1 | TSAUXC_EN_CLK1 | TSAUXC_ST1);
5751819fc75SRuud Bos 			tsim &= ~TSINTR_TT1;
5761819fc75SRuud Bos 		} else {
5771819fc75SRuud Bos 			tsauxc &= ~(TSAUXC_EN_TT0 | TSAUXC_EN_CLK0 | TSAUXC_ST0);
5781819fc75SRuud Bos 			tsim &= ~TSINTR_TT0;
5791819fc75SRuud Bos 		}
5801819fc75SRuud Bos 		if (on) {
5811819fc75SRuud Bos 			int i = rq->perout.index;
5821819fc75SRuud Bos 
5831819fc75SRuud Bos 			/* read systim registers in sequence */
5841819fc75SRuud Bos 			rd32(E1000_SYSTIMR);
5851819fc75SRuud Bos 			systiml = rd32(E1000_SYSTIML);
5861819fc75SRuud Bos 			systimh = rd32(E1000_SYSTIMH);
5871819fc75SRuud Bos 			systim = (((u64)(systimh & 0xFF)) << 32) | ((u64)systiml);
5881819fc75SRuud Bos 			now = timecounter_cyc2time(&igb->tc, systim);
5891819fc75SRuud Bos 
5901819fc75SRuud Bos 			if (pin < 2) {
5911819fc75SRuud Bos 				level_mask = (i == 1) ? 0x80000 : 0x40000;
5921819fc75SRuud Bos 				level = (rd32(E1000_CTRL) & level_mask) ? 1 : 0;
5931819fc75SRuud Bos 			} else {
5941819fc75SRuud Bos 				level_mask = (i == 1) ? 0x80 : 0x40;
5951819fc75SRuud Bos 				level = (rd32(E1000_CTRL_EXT) & level_mask) ? 1 : 0;
5961819fc75SRuud Bos 			}
5971819fc75SRuud Bos 
5981819fc75SRuud Bos 			div_u64_rem(now, ns, &rem);
5991819fc75SRuud Bos 			systim = systim + (ns - rem);
6001819fc75SRuud Bos 
6011819fc75SRuud Bos 			/* synchronize pin level with rising/falling edges */
6021819fc75SRuud Bos 			div_u64_rem(now, ns << 1, &rem);
6031819fc75SRuud Bos 			if (rem < ns) {
6041819fc75SRuud Bos 				/* first half of period */
6051819fc75SRuud Bos 				if (level == 0) {
6061819fc75SRuud Bos 					/* output is already low, skip this period */
6071819fc75SRuud Bos 					systim += ns;
6081819fc75SRuud Bos 				}
6091819fc75SRuud Bos 			} else {
6101819fc75SRuud Bos 				/* second half of period */
6111819fc75SRuud Bos 				if (level == 1) {
6121819fc75SRuud Bos 					/* output is already high, skip this period */
6131819fc75SRuud Bos 					systim += ns;
6141819fc75SRuud Bos 				}
6151819fc75SRuud Bos 			}
6161819fc75SRuud Bos 
6171819fc75SRuud Bos 			start = ns_to_timespec64(systim + (ns - rem));
6181819fc75SRuud Bos 			igb_pin_perout(igb, i, pin, 0);
6191819fc75SRuud Bos 			igb->perout[i].start.tv_sec = start.tv_sec;
6201819fc75SRuud Bos 			igb->perout[i].start.tv_nsec = start.tv_nsec;
6211819fc75SRuud Bos 			igb->perout[i].period.tv_sec = ts.tv_sec;
6221819fc75SRuud Bos 			igb->perout[i].period.tv_nsec = ts.tv_nsec;
6231819fc75SRuud Bos 
6241819fc75SRuud Bos 			wr32(trgttiml, (u32)systim);
6251819fc75SRuud Bos 			wr32(trgttimh, ((u32)(systim >> 32)) & 0xFF);
6261819fc75SRuud Bos 			tsauxc |= tsauxc_mask;
6271819fc75SRuud Bos 			tsim |= tsim_mask;
6281819fc75SRuud Bos 		}
6291819fc75SRuud Bos 		wr32(E1000_TSAUXC, tsauxc);
6301819fc75SRuud Bos 		wr32(E1000_TSIM, tsim);
6311819fc75SRuud Bos 		spin_unlock_irqrestore(&igb->tmreg_lock, flags);
6321819fc75SRuud Bos 		return 0;
6331819fc75SRuud Bos 
6341819fc75SRuud Bos 	case PTP_CLK_REQ_PPS:
6351819fc75SRuud Bos 		return -EOPNOTSUPP;
6361819fc75SRuud Bos 	}
6371819fc75SRuud Bos 
6381819fc75SRuud Bos 	return -EOPNOTSUPP;
6391819fc75SRuud Bos }
6401819fc75SRuud Bos 
igb_ptp_feature_enable_i210(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)64100c65578SRichard Cochran static int igb_ptp_feature_enable_i210(struct ptp_clock_info *ptp,
64200c65578SRichard Cochran 				       struct ptp_clock_request *rq, int on)
64300c65578SRichard Cochran {
64400c65578SRichard Cochran 	struct igb_adapter *igb =
64500c65578SRichard Cochran 		container_of(ptp, struct igb_adapter, ptp_caps);
64600c65578SRichard Cochran 	struct e1000_hw *hw = &igb->hw;
64730c72916SRichard Cochran 	u32 tsauxc, tsim, tsauxc_mask, tsim_mask, trgttiml, trgttimh, freqout;
64800c65578SRichard Cochran 	unsigned long flags;
64940c9b079SArnd Bergmann 	struct timespec64 ts;
65030c72916SRichard Cochran 	int use_freq = 0, pin = -1;
651720db4ffSRichard Cochran 	s64 ns;
65200c65578SRichard Cochran 
65300c65578SRichard Cochran 	switch (rq->type) {
654720db4ffSRichard Cochran 	case PTP_CLK_REQ_EXTTS:
6556edd110bSJacob Keller 		/* Reject requests with unsupported flags */
6566edd110bSJacob Keller 		if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
6576edd110bSJacob Keller 					PTP_RISING_EDGE |
6586138e687SRichard Cochran 					PTP_FALLING_EDGE |
6596138e687SRichard Cochran 					PTP_STRICT_FLAGS))
6606edd110bSJacob Keller 			return -EOPNOTSUPP;
6616edd110bSJacob Keller 
6625a450eb3SRichard Cochran 		/* Reject requests failing to enable both edges. */
6635a450eb3SRichard Cochran 		if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
6645a450eb3SRichard Cochran 		    (rq->extts.flags & PTP_ENABLE_FEATURE) &&
6655a450eb3SRichard Cochran 		    (rq->extts.flags & PTP_EXTTS_EDGES) != PTP_EXTTS_EDGES)
6665a450eb3SRichard Cochran 			return -EOPNOTSUPP;
6675a450eb3SRichard Cochran 
668720db4ffSRichard Cochran 		if (on) {
669720db4ffSRichard Cochran 			pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS,
670720db4ffSRichard Cochran 					   rq->extts.index);
671720db4ffSRichard Cochran 			if (pin < 0)
672720db4ffSRichard Cochran 				return -EBUSY;
673720db4ffSRichard Cochran 		}
674720db4ffSRichard Cochran 		if (rq->extts.index == 1) {
675720db4ffSRichard Cochran 			tsauxc_mask = TSAUXC_EN_TS1;
676720db4ffSRichard Cochran 			tsim_mask = TSINTR_AUTT1;
677720db4ffSRichard Cochran 		} else {
678720db4ffSRichard Cochran 			tsauxc_mask = TSAUXC_EN_TS0;
679720db4ffSRichard Cochran 			tsim_mask = TSINTR_AUTT0;
680720db4ffSRichard Cochran 		}
681720db4ffSRichard Cochran 		spin_lock_irqsave(&igb->tmreg_lock, flags);
682720db4ffSRichard Cochran 		tsauxc = rd32(E1000_TSAUXC);
683720db4ffSRichard Cochran 		tsim = rd32(E1000_TSIM);
684720db4ffSRichard Cochran 		if (on) {
685720db4ffSRichard Cochran 			igb_pin_extts(igb, rq->extts.index, pin);
686720db4ffSRichard Cochran 			tsauxc |= tsauxc_mask;
687720db4ffSRichard Cochran 			tsim |= tsim_mask;
688720db4ffSRichard Cochran 		} else {
689720db4ffSRichard Cochran 			tsauxc &= ~tsauxc_mask;
690720db4ffSRichard Cochran 			tsim &= ~tsim_mask;
691720db4ffSRichard Cochran 		}
692720db4ffSRichard Cochran 		wr32(E1000_TSAUXC, tsauxc);
693720db4ffSRichard Cochran 		wr32(E1000_TSIM, tsim);
694720db4ffSRichard Cochran 		spin_unlock_irqrestore(&igb->tmreg_lock, flags);
695720db4ffSRichard Cochran 		return 0;
696720db4ffSRichard Cochran 
697720db4ffSRichard Cochran 	case PTP_CLK_REQ_PEROUT:
6987f9048f1SJacob Keller 		/* Reject requests with unsupported flags */
6997f9048f1SJacob Keller 		if (rq->perout.flags)
7007f9048f1SJacob Keller 			return -EOPNOTSUPP;
7017f9048f1SJacob Keller 
702720db4ffSRichard Cochran 		if (on) {
703720db4ffSRichard Cochran 			pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT,
704720db4ffSRichard Cochran 					   rq->perout.index);
705720db4ffSRichard Cochran 			if (pin < 0)
706720db4ffSRichard Cochran 				return -EBUSY;
707720db4ffSRichard Cochran 		}
708720db4ffSRichard Cochran 		ts.tv_sec = rq->perout.period.sec;
709720db4ffSRichard Cochran 		ts.tv_nsec = rq->perout.period.nsec;
71040c9b079SArnd Bergmann 		ns = timespec64_to_ns(&ts);
711720db4ffSRichard Cochran 		ns = ns >> 1;
712569f3b3dSRoland Hii 		if (on && ((ns <= 70000000LL) || (ns == 125000000LL) ||
713569f3b3dSRoland Hii 			   (ns == 250000000LL) || (ns == 500000000LL))) {
71430c72916SRichard Cochran 			if (ns < 8LL)
715720db4ffSRichard Cochran 				return -EINVAL;
71630c72916SRichard Cochran 			use_freq = 1;
717720db4ffSRichard Cochran 		}
71840c9b079SArnd Bergmann 		ts = ns_to_timespec64(ns);
719720db4ffSRichard Cochran 		if (rq->perout.index == 1) {
72030c72916SRichard Cochran 			if (use_freq) {
72130c72916SRichard Cochran 				tsauxc_mask = TSAUXC_EN_CLK1 | TSAUXC_ST1;
72230c72916SRichard Cochran 				tsim_mask = 0;
72330c72916SRichard Cochran 			} else {
724720db4ffSRichard Cochran 				tsauxc_mask = TSAUXC_EN_TT1;
725720db4ffSRichard Cochran 				tsim_mask = TSINTR_TT1;
72630c72916SRichard Cochran 			}
727720db4ffSRichard Cochran 			trgttiml = E1000_TRGTTIML1;
728720db4ffSRichard Cochran 			trgttimh = E1000_TRGTTIMH1;
72930c72916SRichard Cochran 			freqout = E1000_FREQOUT1;
73030c72916SRichard Cochran 		} else {
73130c72916SRichard Cochran 			if (use_freq) {
73230c72916SRichard Cochran 				tsauxc_mask = TSAUXC_EN_CLK0 | TSAUXC_ST0;
73330c72916SRichard Cochran 				tsim_mask = 0;
734720db4ffSRichard Cochran 			} else {
735720db4ffSRichard Cochran 				tsauxc_mask = TSAUXC_EN_TT0;
736720db4ffSRichard Cochran 				tsim_mask = TSINTR_TT0;
73730c72916SRichard Cochran 			}
738720db4ffSRichard Cochran 			trgttiml = E1000_TRGTTIML0;
739720db4ffSRichard Cochran 			trgttimh = E1000_TRGTTIMH0;
74030c72916SRichard Cochran 			freqout = E1000_FREQOUT0;
741720db4ffSRichard Cochran 		}
742720db4ffSRichard Cochran 		spin_lock_irqsave(&igb->tmreg_lock, flags);
743720db4ffSRichard Cochran 		tsauxc = rd32(E1000_TSAUXC);
744720db4ffSRichard Cochran 		tsim = rd32(E1000_TSIM);
74530c72916SRichard Cochran 		if (rq->perout.index == 1) {
74630c72916SRichard Cochran 			tsauxc &= ~(TSAUXC_EN_TT1 | TSAUXC_EN_CLK1 | TSAUXC_ST1);
74730c72916SRichard Cochran 			tsim &= ~TSINTR_TT1;
74830c72916SRichard Cochran 		} else {
74930c72916SRichard Cochran 			tsauxc &= ~(TSAUXC_EN_TT0 | TSAUXC_EN_CLK0 | TSAUXC_ST0);
75030c72916SRichard Cochran 			tsim &= ~TSINTR_TT0;
75130c72916SRichard Cochran 		}
752720db4ffSRichard Cochran 		if (on) {
753720db4ffSRichard Cochran 			int i = rq->perout.index;
75430c72916SRichard Cochran 			igb_pin_perout(igb, i, pin, use_freq);
755720db4ffSRichard Cochran 			igb->perout[i].start.tv_sec = rq->perout.start.sec;
756720db4ffSRichard Cochran 			igb->perout[i].start.tv_nsec = rq->perout.start.nsec;
757720db4ffSRichard Cochran 			igb->perout[i].period.tv_sec = ts.tv_sec;
758720db4ffSRichard Cochran 			igb->perout[i].period.tv_nsec = ts.tv_nsec;
75958c98be1SRichard Cochran 			wr32(trgttimh, rq->perout.start.sec);
76058c98be1SRichard Cochran 			wr32(trgttiml, rq->perout.start.nsec);
76130c72916SRichard Cochran 			if (use_freq)
76230c72916SRichard Cochran 				wr32(freqout, ns);
763720db4ffSRichard Cochran 			tsauxc |= tsauxc_mask;
764720db4ffSRichard Cochran 			tsim |= tsim_mask;
765720db4ffSRichard Cochran 		}
766720db4ffSRichard Cochran 		wr32(E1000_TSAUXC, tsauxc);
767720db4ffSRichard Cochran 		wr32(E1000_TSIM, tsim);
768720db4ffSRichard Cochran 		spin_unlock_irqrestore(&igb->tmreg_lock, flags);
769720db4ffSRichard Cochran 		return 0;
770720db4ffSRichard Cochran 
77100c65578SRichard Cochran 	case PTP_CLK_REQ_PPS:
77200c65578SRichard Cochran 		spin_lock_irqsave(&igb->tmreg_lock, flags);
77300c65578SRichard Cochran 		tsim = rd32(E1000_TSIM);
77400c65578SRichard Cochran 		if (on)
77500c65578SRichard Cochran 			tsim |= TSINTR_SYS_WRAP;
77600c65578SRichard Cochran 		else
77700c65578SRichard Cochran 			tsim &= ~TSINTR_SYS_WRAP;
778ac28b41aSJacob Keller 		igb->pps_sys_wrap_on = !!on;
77900c65578SRichard Cochran 		wr32(E1000_TSIM, tsim);
78000c65578SRichard Cochran 		spin_unlock_irqrestore(&igb->tmreg_lock, flags);
78100c65578SRichard Cochran 		return 0;
78200c65578SRichard Cochran 	}
78300c65578SRichard Cochran 
78400c65578SRichard Cochran 	return -EOPNOTSUPP;
78500c65578SRichard Cochran }
78600c65578SRichard Cochran 
igb_ptp_feature_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)787102be52fSJacob Keller static int igb_ptp_feature_enable(struct ptp_clock_info *ptp,
788d339b133SRichard Cochran 				  struct ptp_clock_request *rq, int on)
789d339b133SRichard Cochran {
790d339b133SRichard Cochran 	return -EOPNOTSUPP;
791d339b133SRichard Cochran }
792d339b133SRichard Cochran 
igb_ptp_verify_pin(struct ptp_clock_info * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)793720db4ffSRichard Cochran static int igb_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
794720db4ffSRichard Cochran 			      enum ptp_pin_function func, unsigned int chan)
795720db4ffSRichard Cochran {
796720db4ffSRichard Cochran 	switch (func) {
797720db4ffSRichard Cochran 	case PTP_PF_NONE:
798720db4ffSRichard Cochran 	case PTP_PF_EXTTS:
799720db4ffSRichard Cochran 	case PTP_PF_PEROUT:
800720db4ffSRichard Cochran 		break;
801720db4ffSRichard Cochran 	case PTP_PF_PHYSYNC:
802720db4ffSRichard Cochran 		return -1;
803720db4ffSRichard Cochran 	}
804720db4ffSRichard Cochran 	return 0;
805720db4ffSRichard Cochran }
806720db4ffSRichard Cochran 
8071f6e8178SMatthew Vick /**
8081f6e8178SMatthew Vick  * igb_ptp_tx_work
8091f6e8178SMatthew Vick  * @work: pointer to work struct
8101f6e8178SMatthew Vick  *
8111f6e8178SMatthew Vick  * This work function polls the TSYNCTXCTL valid bit to determine when a
8121f6e8178SMatthew Vick  * timestamp has been taken for the current stored skb.
813b980ac18SJeff Kirsher  **/
igb_ptp_tx_work(struct work_struct * work)814167f3f71SJeff Kirsher static void igb_ptp_tx_work(struct work_struct *work)
8151f6e8178SMatthew Vick {
8161f6e8178SMatthew Vick 	struct igb_adapter *adapter = container_of(work, struct igb_adapter,
8171f6e8178SMatthew Vick 						   ptp_tx_work);
8181f6e8178SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
8191f6e8178SMatthew Vick 	u32 tsynctxctl;
8201f6e8178SMatthew Vick 
8211f6e8178SMatthew Vick 	if (!adapter->ptp_tx_skb)
8221f6e8178SMatthew Vick 		return;
8231f6e8178SMatthew Vick 
824428f1f71SMatthew Vick 	if (time_is_before_jiffies(adapter->ptp_tx_start +
825428f1f71SMatthew Vick 				   IGB_PTP_TX_TIMEOUT)) {
826428f1f71SMatthew Vick 		dev_kfree_skb_any(adapter->ptp_tx_skb);
827428f1f71SMatthew Vick 		adapter->ptp_tx_skb = NULL;
828ed4420a3SJakub Kicinski 		clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
829428f1f71SMatthew Vick 		adapter->tx_hwtstamp_timeouts++;
8303a532852SDaniel Hua 		/* Clear the tx valid bit in TSYNCTXCTL register to enable
8313a532852SDaniel Hua 		 * interrupt
8323a532852SDaniel Hua 		 */
8333a532852SDaniel Hua 		rd32(E1000_TXSTMPH);
834c5ffe7e1SJakub Kicinski 		dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
835428f1f71SMatthew Vick 		return;
836428f1f71SMatthew Vick 	}
837428f1f71SMatthew Vick 
8381f6e8178SMatthew Vick 	tsynctxctl = rd32(E1000_TSYNCTXCTL);
8391f6e8178SMatthew Vick 	if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
8401f6e8178SMatthew Vick 		igb_ptp_tx_hwtstamp(adapter);
8411f6e8178SMatthew Vick 	else
8421f6e8178SMatthew Vick 		/* reschedule to check later */
8431f6e8178SMatthew Vick 		schedule_work(&adapter->ptp_tx_work);
8441f6e8178SMatthew Vick }
8451f6e8178SMatthew Vick 
igb_ptp_overflow_check(struct work_struct * work)846a79f4f88SMatthew Vick static void igb_ptp_overflow_check(struct work_struct *work)
847d339b133SRichard Cochran {
848d339b133SRichard Cochran 	struct igb_adapter *igb =
849a79f4f88SMatthew Vick 		container_of(work, struct igb_adapter, ptp_overflow_work.work);
850d4c496feSRichard Cochran 	struct timespec64 ts;
851cff8ba28SMiroslav Lichvar 	u64 ns;
852d339b133SRichard Cochran 
853cff8ba28SMiroslav Lichvar 	/* Update the timecounter */
854cff8ba28SMiroslav Lichvar 	ns = timecounter_read(&igb->tc);
855d339b133SRichard Cochran 
856cff8ba28SMiroslav Lichvar 	ts = ns_to_timespec64(ns);
85732eaf120SDavid S. Miller 	pr_debug("igb overflow check at %lld.%09lu\n",
85832eaf120SDavid S. Miller 		 (long long) ts.tv_sec, ts.tv_nsec);
859d339b133SRichard Cochran 
860a79f4f88SMatthew Vick 	schedule_delayed_work(&igb->ptp_overflow_work,
861a79f4f88SMatthew Vick 			      IGB_SYSTIM_OVERFLOW_PERIOD);
862a79f4f88SMatthew Vick }
863a79f4f88SMatthew Vick 
864a79f4f88SMatthew Vick /**
865fc580751SMatthew Vick  * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
866fc580751SMatthew Vick  * @adapter: private network adapter structure
867fc580751SMatthew Vick  *
868fc580751SMatthew Vick  * This watchdog task is scheduled to detect error case where hardware has
869fc580751SMatthew Vick  * dropped an Rx packet that was timestamped when the ring is full. The
870fc580751SMatthew Vick  * particular error is rare but leaves the device in a state unable to timestamp
871fc580751SMatthew Vick  * any future packets.
872b980ac18SJeff Kirsher  **/
igb_ptp_rx_hang(struct igb_adapter * adapter)873fc580751SMatthew Vick void igb_ptp_rx_hang(struct igb_adapter *adapter)
874fc580751SMatthew Vick {
875fc580751SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
876fc580751SMatthew Vick 	u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
877fc580751SMatthew Vick 	unsigned long rx_event;
878fc580751SMatthew Vick 
879462f1188SJacob Keller 	/* Other hardware uses per-packet timestamps */
880fc580751SMatthew Vick 	if (hw->mac.type != e1000_82576)
881fc580751SMatthew Vick 		return;
882fc580751SMatthew Vick 
883fc580751SMatthew Vick 	/* If we don't have a valid timestamp in the registers, just update the
884fc580751SMatthew Vick 	 * timeout counter and exit
885fc580751SMatthew Vick 	 */
886fc580751SMatthew Vick 	if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
887fc580751SMatthew Vick 		adapter->last_rx_ptp_check = jiffies;
888fc580751SMatthew Vick 		return;
889fc580751SMatthew Vick 	}
890fc580751SMatthew Vick 
891fc580751SMatthew Vick 	/* Determine the most recent watchdog or rx_timestamp event */
892fc580751SMatthew Vick 	rx_event = adapter->last_rx_ptp_check;
8935499a968SJakub Kicinski 	if (time_after(adapter->last_rx_timestamp, rx_event))
8945499a968SJakub Kicinski 		rx_event = adapter->last_rx_timestamp;
895fc580751SMatthew Vick 
896fc580751SMatthew Vick 	/* Only need to read the high RXSTMP register to clear the lock */
897fc580751SMatthew Vick 	if (time_is_before_jiffies(rx_event + 5 * HZ)) {
898fc580751SMatthew Vick 		rd32(E1000_RXSTMPH);
899fc580751SMatthew Vick 		adapter->last_rx_ptp_check = jiffies;
900fc580751SMatthew Vick 		adapter->rx_hwtstamp_cleared++;
901c5ffe7e1SJakub Kicinski 		dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang\n");
902fc580751SMatthew Vick 	}
903fc580751SMatthew Vick }
904fc580751SMatthew Vick 
905fc580751SMatthew Vick /**
906e5f36ad1SJacob Keller  * igb_ptp_tx_hang - detect error case where Tx timestamp never finishes
907e5f36ad1SJacob Keller  * @adapter: private network adapter structure
908e5f36ad1SJacob Keller  */
igb_ptp_tx_hang(struct igb_adapter * adapter)909e5f36ad1SJacob Keller void igb_ptp_tx_hang(struct igb_adapter *adapter)
910e5f36ad1SJacob Keller {
9113a532852SDaniel Hua 	struct e1000_hw *hw = &adapter->hw;
912e5f36ad1SJacob Keller 	bool timeout = time_is_before_jiffies(adapter->ptp_tx_start +
913e5f36ad1SJacob Keller 					      IGB_PTP_TX_TIMEOUT);
914e5f36ad1SJacob Keller 
915e5f36ad1SJacob Keller 	if (!adapter->ptp_tx_skb)
916e5f36ad1SJacob Keller 		return;
917e5f36ad1SJacob Keller 
918e5f36ad1SJacob Keller 	if (!test_bit(__IGB_PTP_TX_IN_PROGRESS, &adapter->state))
919e5f36ad1SJacob Keller 		return;
920e5f36ad1SJacob Keller 
921e5f36ad1SJacob Keller 	/* If we haven't received a timestamp within the timeout, it is
922e5f36ad1SJacob Keller 	 * reasonable to assume that it will never occur, so we can unlock the
923e5f36ad1SJacob Keller 	 * timestamp bit when this occurs.
924e5f36ad1SJacob Keller 	 */
925e5f36ad1SJacob Keller 	if (timeout) {
926e5f36ad1SJacob Keller 		cancel_work_sync(&adapter->ptp_tx_work);
927e5f36ad1SJacob Keller 		dev_kfree_skb_any(adapter->ptp_tx_skb);
928e5f36ad1SJacob Keller 		adapter->ptp_tx_skb = NULL;
929e5f36ad1SJacob Keller 		clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
930e5f36ad1SJacob Keller 		adapter->tx_hwtstamp_timeouts++;
9313a532852SDaniel Hua 		/* Clear the tx valid bit in TSYNCTXCTL register to enable
9323a532852SDaniel Hua 		 * interrupt
9333a532852SDaniel Hua 		 */
9343a532852SDaniel Hua 		rd32(E1000_TXSTMPH);
935e5f36ad1SJacob Keller 		dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
936e5f36ad1SJacob Keller 	}
937e5f36ad1SJacob Keller }
938e5f36ad1SJacob Keller 
939e5f36ad1SJacob Keller /**
940a79f4f88SMatthew Vick  * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
9411f6e8178SMatthew Vick  * @adapter: Board private structure.
942a79f4f88SMatthew Vick  *
943a79f4f88SMatthew Vick  * If we were asked to do hardware stamping and such a time stamp is
944a79f4f88SMatthew Vick  * available, then it must have been for this skb here because we only
945a79f4f88SMatthew Vick  * allow only one such packet into the queue.
946b980ac18SJeff Kirsher  **/
igb_ptp_tx_hwtstamp(struct igb_adapter * adapter)947167f3f71SJeff Kirsher static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
948a79f4f88SMatthew Vick {
9494ccdc013SJacob Keller 	struct sk_buff *skb = adapter->ptp_tx_skb;
950a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
951a79f4f88SMatthew Vick 	struct skb_shared_hwtstamps shhwtstamps;
952a79f4f88SMatthew Vick 	u64 regval;
9533f544d2aSNathan Sullivan 	int adjust = 0;
954a79f4f88SMatthew Vick 
955a79f4f88SMatthew Vick 	regval = rd32(E1000_TXSTMPL);
956a79f4f88SMatthew Vick 	regval |= (u64)rd32(E1000_TXSTMPH) << 32;
957a79f4f88SMatthew Vick 
958a79f4f88SMatthew Vick 	igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
9593f544d2aSNathan Sullivan 	/* adjust timestamp for the TX latency based on link speed */
960*c88bf996SOleksij Rempel 	if (hw->mac.type == e1000_i210 || hw->mac.type == e1000_i211) {
9613f544d2aSNathan Sullivan 		switch (adapter->link_speed) {
9623f544d2aSNathan Sullivan 		case SPEED_10:
9633f544d2aSNathan Sullivan 			adjust = IGB_I210_TX_LATENCY_10;
9643f544d2aSNathan Sullivan 			break;
9653f544d2aSNathan Sullivan 		case SPEED_100:
9663f544d2aSNathan Sullivan 			adjust = IGB_I210_TX_LATENCY_100;
9673f544d2aSNathan Sullivan 			break;
9683f544d2aSNathan Sullivan 		case SPEED_1000:
9693f544d2aSNathan Sullivan 			adjust = IGB_I210_TX_LATENCY_1000;
9703f544d2aSNathan Sullivan 			break;
9713f544d2aSNathan Sullivan 		}
9723f544d2aSNathan Sullivan 	}
9733f544d2aSNathan Sullivan 
9740066c8b6SKshitiz Gupta 	shhwtstamps.hwtstamp =
9750066c8b6SKshitiz Gupta 		ktime_add_ns(shhwtstamps.hwtstamp, adjust);
9763f544d2aSNathan Sullivan 
9774ccdc013SJacob Keller 	/* Clear the lock early before calling skb_tstamp_tx so that
9784ccdc013SJacob Keller 	 * applications are not woken up before the lock bit is clear. We use
9794ccdc013SJacob Keller 	 * a copy of the skb pointer to ensure other threads can't change it
9804ccdc013SJacob Keller 	 * while we're notifying the stack.
9814ccdc013SJacob Keller 	 */
9821f6e8178SMatthew Vick 	adapter->ptp_tx_skb = NULL;
983ed4420a3SJakub Kicinski 	clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
9844ccdc013SJacob Keller 
9854ccdc013SJacob Keller 	/* Notify the stack and free the skb after we've unlocked */
9864ccdc013SJacob Keller 	skb_tstamp_tx(skb, &shhwtstamps);
9874ccdc013SJacob Keller 	dev_kfree_skb_any(skb);
988a79f4f88SMatthew Vick }
989a79f4f88SMatthew Vick 
990b534550aSAlexander Duyck /**
991b534550aSAlexander Duyck  * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
992b534550aSAlexander Duyck  * @q_vector: Pointer to interrupt specific structure
993b534550aSAlexander Duyck  * @va: Pointer to address containing Rx buffer
99453792608SKurt Kanzenbach  * @timestamp: Pointer where timestamp will be stored
995b534550aSAlexander Duyck  *
996b534550aSAlexander Duyck  * This function is meant to retrieve a timestamp from the first buffer of an
997b534550aSAlexander Duyck  * incoming frame.  The value is stored in little endian format starting on
998f0a03a02SJesse Brandeburg  * byte 8
999f0a03a02SJesse Brandeburg  *
100053792608SKurt Kanzenbach  * Returns: The timestamp header length or 0 if not available
1001b980ac18SJeff Kirsher  **/
igb_ptp_rx_pktstamp(struct igb_q_vector * q_vector,void * va,ktime_t * timestamp)1002f0a03a02SJesse Brandeburg int igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector, void *va,
100353792608SKurt Kanzenbach 			ktime_t *timestamp)
1004b534550aSAlexander Duyck {
10050066c8b6SKshitiz Gupta 	struct igb_adapter *adapter = q_vector->adapter;
1006*c88bf996SOleksij Rempel 	struct e1000_hw *hw = &adapter->hw;
100753792608SKurt Kanzenbach 	struct skb_shared_hwtstamps ts;
1008f0a03a02SJesse Brandeburg 	__le64 *regval = (__le64 *)va;
10090066c8b6SKshitiz Gupta 	int adjust = 0;
1010b534550aSAlexander Duyck 
1011f0a03a02SJesse Brandeburg 	if (!(adapter->ptp_flags & IGB_PTP_ENABLED))
101253792608SKurt Kanzenbach 		return 0;
1013f0a03a02SJesse Brandeburg 
1014b980ac18SJeff Kirsher 	/* The timestamp is recorded in little endian format.
1015b534550aSAlexander Duyck 	 * DWORD: 0        1        2        3
1016b534550aSAlexander Duyck 	 * Field: Reserved Reserved SYSTIML  SYSTIMH
1017b534550aSAlexander Duyck 	 */
1018f0a03a02SJesse Brandeburg 
1019f0a03a02SJesse Brandeburg 	/* check reserved dwords are zero, be/le doesn't matter for zero */
1020f0a03a02SJesse Brandeburg 	if (regval[0])
102153792608SKurt Kanzenbach 		return 0;
1022f0a03a02SJesse Brandeburg 
102353792608SKurt Kanzenbach 	igb_ptp_systim_to_hwtstamp(adapter, &ts, le64_to_cpu(regval[1]));
10240066c8b6SKshitiz Gupta 
10250066c8b6SKshitiz Gupta 	/* adjust timestamp for the RX latency based on link speed */
1026*c88bf996SOleksij Rempel 	if (hw->mac.type == e1000_i210 || hw->mac.type == e1000_i211) {
10270066c8b6SKshitiz Gupta 		switch (adapter->link_speed) {
10280066c8b6SKshitiz Gupta 		case SPEED_10:
10290066c8b6SKshitiz Gupta 			adjust = IGB_I210_RX_LATENCY_10;
10300066c8b6SKshitiz Gupta 			break;
10310066c8b6SKshitiz Gupta 		case SPEED_100:
10320066c8b6SKshitiz Gupta 			adjust = IGB_I210_RX_LATENCY_100;
10330066c8b6SKshitiz Gupta 			break;
10340066c8b6SKshitiz Gupta 		case SPEED_1000:
10350066c8b6SKshitiz Gupta 			adjust = IGB_I210_RX_LATENCY_1000;
10360066c8b6SKshitiz Gupta 			break;
10370066c8b6SKshitiz Gupta 		}
10380066c8b6SKshitiz Gupta 	}
1039f0a03a02SJesse Brandeburg 
104053792608SKurt Kanzenbach 	*timestamp = ktime_sub_ns(ts.hwtstamp, adjust);
104153792608SKurt Kanzenbach 
104253792608SKurt Kanzenbach 	return IGB_TS_HDR_LEN;
1043b534550aSAlexander Duyck }
1044b534550aSAlexander Duyck 
1045b534550aSAlexander Duyck /**
1046b534550aSAlexander Duyck  * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
1047b534550aSAlexander Duyck  * @q_vector: Pointer to interrupt specific structure
1048b534550aSAlexander Duyck  * @skb: Buffer containing timestamp and packet
1049b534550aSAlexander Duyck  *
1050b534550aSAlexander Duyck  * This function is meant to retrieve a timestamp from the internal registers
1051b534550aSAlexander Duyck  * of the adapter and store it in the skb.
1052b980ac18SJeff Kirsher  **/
igb_ptp_rx_rgtstamp(struct igb_q_vector * q_vector,struct sk_buff * skb)1053f0a03a02SJesse Brandeburg void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector, struct sk_buff *skb)
1054a79f4f88SMatthew Vick {
1055a79f4f88SMatthew Vick 	struct igb_adapter *adapter = q_vector->adapter;
1056a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
10573f544d2aSNathan Sullivan 	int adjust = 0;
1058f0a03a02SJesse Brandeburg 	u64 regval;
1059f0a03a02SJesse Brandeburg 
1060f0a03a02SJesse Brandeburg 	if (!(adapter->ptp_flags & IGB_PTP_ENABLED))
1061f0a03a02SJesse Brandeburg 		return;
1062a79f4f88SMatthew Vick 
1063b980ac18SJeff Kirsher 	/* If this bit is set, then the RX registers contain the time stamp. No
1064a79f4f88SMatthew Vick 	 * other packet will be time stamped until we read these registers, so
1065a79f4f88SMatthew Vick 	 * read the registers to make them available again. Because only one
1066a79f4f88SMatthew Vick 	 * packet can be time stamped at a time, we know that the register
1067a79f4f88SMatthew Vick 	 * values must belong to this one here and therefore we don't need to
1068a79f4f88SMatthew Vick 	 * compare any of the additional attributes stored for it.
1069a79f4f88SMatthew Vick 	 *
1070a79f4f88SMatthew Vick 	 * If nothing went wrong, then it should have a shared tx_flags that we
1071a79f4f88SMatthew Vick 	 * can turn into a skb_shared_hwtstamps.
1072a79f4f88SMatthew Vick 	 */
1073a79f4f88SMatthew Vick 	if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
1074a79f4f88SMatthew Vick 		return;
1075a79f4f88SMatthew Vick 
1076a79f4f88SMatthew Vick 	regval = rd32(E1000_RXSTMPL);
1077a79f4f88SMatthew Vick 	regval |= (u64)rd32(E1000_RXSTMPH) << 32;
1078a79f4f88SMatthew Vick 
1079a79f4f88SMatthew Vick 	igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
10805499a968SJakub Kicinski 
10813f544d2aSNathan Sullivan 	/* adjust timestamp for the RX latency based on link speed */
10823f544d2aSNathan Sullivan 	if (adapter->hw.mac.type == e1000_i210) {
10833f544d2aSNathan Sullivan 		switch (adapter->link_speed) {
10843f544d2aSNathan Sullivan 		case SPEED_10:
10853f544d2aSNathan Sullivan 			adjust = IGB_I210_RX_LATENCY_10;
10863f544d2aSNathan Sullivan 			break;
10873f544d2aSNathan Sullivan 		case SPEED_100:
10883f544d2aSNathan Sullivan 			adjust = IGB_I210_RX_LATENCY_100;
10893f544d2aSNathan Sullivan 			break;
10903f544d2aSNathan Sullivan 		case SPEED_1000:
10913f544d2aSNathan Sullivan 			adjust = IGB_I210_RX_LATENCY_1000;
10923f544d2aSNathan Sullivan 			break;
10933f544d2aSNathan Sullivan 		}
10943f544d2aSNathan Sullivan 	}
10953f544d2aSNathan Sullivan 	skb_hwtstamps(skb)->hwtstamp =
10960066c8b6SKshitiz Gupta 		ktime_sub_ns(skb_hwtstamps(skb)->hwtstamp, adjust);
10973f544d2aSNathan Sullivan 
10985499a968SJakub Kicinski 	/* Update the last_rx_timestamp timer in order to enable watchdog check
10995499a968SJakub Kicinski 	 * for error case of latched timestamp on a dropped packet.
11005499a968SJakub Kicinski 	 */
11015499a968SJakub Kicinski 	adapter->last_rx_timestamp = jiffies;
1102a79f4f88SMatthew Vick }
1103a79f4f88SMatthew Vick 
1104a79f4f88SMatthew Vick /**
11056ab5f7b2SJacob Keller  * igb_ptp_get_ts_config - get hardware time stamping config
1106b50f7bcaSJesse Brandeburg  * @netdev: netdev struct
1107b50f7bcaSJesse Brandeburg  * @ifr: interface struct
11086ab5f7b2SJacob Keller  *
11096ab5f7b2SJacob Keller  * Get the hwtstamp_config settings to return to the user. Rather than attempt
11106ab5f7b2SJacob Keller  * to deconstruct the settings from the registers, just return a shadow copy
11116ab5f7b2SJacob Keller  * of the last known settings.
11126ab5f7b2SJacob Keller  **/
igb_ptp_get_ts_config(struct net_device * netdev,struct ifreq * ifr)11136ab5f7b2SJacob Keller int igb_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
11146ab5f7b2SJacob Keller {
11156ab5f7b2SJacob Keller 	struct igb_adapter *adapter = netdev_priv(netdev);
11166ab5f7b2SJacob Keller 	struct hwtstamp_config *config = &adapter->tstamp_config;
11176ab5f7b2SJacob Keller 
11186ab5f7b2SJacob Keller 	return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
11196ab5f7b2SJacob Keller 		-EFAULT : 0;
11206ab5f7b2SJacob Keller }
11219f62ecf4SJacob Keller 
11226ab5f7b2SJacob Keller /**
11239f62ecf4SJacob Keller  * igb_ptp_set_timestamp_mode - setup hardware for timestamping
11249f62ecf4SJacob Keller  * @adapter: networking device structure
11259f62ecf4SJacob Keller  * @config: hwtstamp configuration
1126a79f4f88SMatthew Vick  *
1127a79f4f88SMatthew Vick  * Outgoing time stamping can be enabled and disabled. Play nice and
1128a79f4f88SMatthew Vick  * disable it when requested, although it shouldn't case any overhead
1129a79f4f88SMatthew Vick  * when no packet needs it. At most one packet in the queue may be
1130a79f4f88SMatthew Vick  * marked for time stamping, otherwise it would be impossible to tell
1131a79f4f88SMatthew Vick  * for sure to which packet the hardware time stamp belongs.
1132a79f4f88SMatthew Vick  *
1133a79f4f88SMatthew Vick  * Incoming time stamping has to be configured via the hardware
1134a79f4f88SMatthew Vick  * filters. Not all combinations are supported, in particular event
1135a79f4f88SMatthew Vick  * type has to be specified. Matching the kind of event packet is
1136a79f4f88SMatthew Vick  * not supported, with the exception of "all V2 events regardless of
1137a79f4f88SMatthew Vick  * level 2 or 4".
11389f62ecf4SJacob Keller  */
igb_ptp_set_timestamp_mode(struct igb_adapter * adapter,struct hwtstamp_config * config)11399f62ecf4SJacob Keller static int igb_ptp_set_timestamp_mode(struct igb_adapter *adapter,
11409f62ecf4SJacob Keller 				      struct hwtstamp_config *config)
1141a79f4f88SMatthew Vick {
1142a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
1143a79f4f88SMatthew Vick 	u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
1144a79f4f88SMatthew Vick 	u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
1145a79f4f88SMatthew Vick 	u32 tsync_rx_cfg = 0;
1146a79f4f88SMatthew Vick 	bool is_l4 = false;
1147a79f4f88SMatthew Vick 	bool is_l2 = false;
1148a79f4f88SMatthew Vick 	u32 regval;
1149a79f4f88SMatthew Vick 
11506ab5f7b2SJacob Keller 	switch (config->tx_type) {
1151a79f4f88SMatthew Vick 	case HWTSTAMP_TX_OFF:
1152a79f4f88SMatthew Vick 		tsync_tx_ctl = 0;
115352c40698SGustavo A. R. Silva 		break;
1154a79f4f88SMatthew Vick 	case HWTSTAMP_TX_ON:
1155a79f4f88SMatthew Vick 		break;
1156a79f4f88SMatthew Vick 	default:
1157a79f4f88SMatthew Vick 		return -ERANGE;
1158a79f4f88SMatthew Vick 	}
1159a79f4f88SMatthew Vick 
11606ab5f7b2SJacob Keller 	switch (config->rx_filter) {
1161a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_NONE:
1162a79f4f88SMatthew Vick 		tsync_rx_ctl = 0;
1163a79f4f88SMatthew Vick 		break;
1164a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1165a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
1166a79f4f88SMatthew Vick 		tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
1167a79f4f88SMatthew Vick 		is_l4 = true;
1168a79f4f88SMatthew Vick 		break;
1169a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1170a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
1171a79f4f88SMatthew Vick 		tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
1172a79f4f88SMatthew Vick 		is_l4 = true;
1173a79f4f88SMatthew Vick 		break;
11743e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
11753e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
11763e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
11773e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1178a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1179a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
11803e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1181a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1182a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1183a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
11846ab5f7b2SJacob Keller 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1185a79f4f88SMatthew Vick 		is_l2 = true;
1186a79f4f88SMatthew Vick 		is_l4 = true;
1187a79f4f88SMatthew Vick 		break;
11883e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1189e3412575SMiroslav Lichvar 	case HWTSTAMP_FILTER_NTP_ALL:
11903e961a06SMatthew Vick 	case HWTSTAMP_FILTER_ALL:
11913e961a06SMatthew Vick 		/* 82576 cannot timestamp all packets, which it needs to do to
11923e961a06SMatthew Vick 		 * support both V1 Sync and Delay_Req messages
11933e961a06SMatthew Vick 		 */
11943e961a06SMatthew Vick 		if (hw->mac.type != e1000_82576) {
11953e961a06SMatthew Vick 			tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
11966ab5f7b2SJacob Keller 			config->rx_filter = HWTSTAMP_FILTER_ALL;
11973e961a06SMatthew Vick 			break;
11983e961a06SMatthew Vick 		}
11995463fce6SJeff Kirsher 		fallthrough;
1200a79f4f88SMatthew Vick 	default:
12016ab5f7b2SJacob Keller 		config->rx_filter = HWTSTAMP_FILTER_NONE;
1202a79f4f88SMatthew Vick 		return -ERANGE;
1203a79f4f88SMatthew Vick 	}
1204a79f4f88SMatthew Vick 
1205a79f4f88SMatthew Vick 	if (hw->mac.type == e1000_82575) {
1206a79f4f88SMatthew Vick 		if (tsync_rx_ctl | tsync_tx_ctl)
1207a79f4f88SMatthew Vick 			return -EINVAL;
1208a79f4f88SMatthew Vick 		return 0;
1209a79f4f88SMatthew Vick 	}
1210a79f4f88SMatthew Vick 
1211b980ac18SJeff Kirsher 	/* Per-packet timestamping only works if all packets are
1212a79f4f88SMatthew Vick 	 * timestamped, so enable timestamping in all packets as
1213b980ac18SJeff Kirsher 	 * long as one Rx filter was configured.
1214a79f4f88SMatthew Vick 	 */
1215a79f4f88SMatthew Vick 	if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
1216a79f4f88SMatthew Vick 		tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
1217a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
12186ab5f7b2SJacob Keller 		config->rx_filter = HWTSTAMP_FILTER_ALL;
12193e961a06SMatthew Vick 		is_l2 = true;
12203e961a06SMatthew Vick 		is_l4 = true;
1221e57b8bdbSMatthew Vick 
1222e57b8bdbSMatthew Vick 		if ((hw->mac.type == e1000_i210) ||
1223e57b8bdbSMatthew Vick 		    (hw->mac.type == e1000_i211)) {
1224e57b8bdbSMatthew Vick 			regval = rd32(E1000_RXPBS);
1225e57b8bdbSMatthew Vick 			regval |= E1000_RXPBS_CFG_TS_EN;
1226e57b8bdbSMatthew Vick 			wr32(E1000_RXPBS, regval);
1227e57b8bdbSMatthew Vick 		}
1228a79f4f88SMatthew Vick 	}
1229a79f4f88SMatthew Vick 
1230a79f4f88SMatthew Vick 	/* enable/disable TX */
1231a79f4f88SMatthew Vick 	regval = rd32(E1000_TSYNCTXCTL);
1232a79f4f88SMatthew Vick 	regval &= ~E1000_TSYNCTXCTL_ENABLED;
1233a79f4f88SMatthew Vick 	regval |= tsync_tx_ctl;
1234a79f4f88SMatthew Vick 	wr32(E1000_TSYNCTXCTL, regval);
1235a79f4f88SMatthew Vick 
1236a79f4f88SMatthew Vick 	/* enable/disable RX */
1237a79f4f88SMatthew Vick 	regval = rd32(E1000_TSYNCRXCTL);
1238a79f4f88SMatthew Vick 	regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
1239a79f4f88SMatthew Vick 	regval |= tsync_rx_ctl;
1240a79f4f88SMatthew Vick 	wr32(E1000_TSYNCRXCTL, regval);
1241a79f4f88SMatthew Vick 
1242a79f4f88SMatthew Vick 	/* define which PTP packets are time stamped */
1243a79f4f88SMatthew Vick 	wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
1244a79f4f88SMatthew Vick 
1245a79f4f88SMatthew Vick 	/* define ethertype filter for timestamped packets */
1246a79f4f88SMatthew Vick 	if (is_l2)
124764c75d41SGangfeng Huang 		wr32(E1000_ETQF(IGB_ETQF_FILTER_1588),
1248a79f4f88SMatthew Vick 		     (E1000_ETQF_FILTER_ENABLE | /* enable filter */
1249a79f4f88SMatthew Vick 		      E1000_ETQF_1588 | /* enable timestamping */
1250a79f4f88SMatthew Vick 		      ETH_P_1588));     /* 1588 eth protocol type */
1251a79f4f88SMatthew Vick 	else
125264c75d41SGangfeng Huang 		wr32(E1000_ETQF(IGB_ETQF_FILTER_1588), 0);
1253a79f4f88SMatthew Vick 
1254a79f4f88SMatthew Vick 	/* L4 Queue Filter[3]: filter by destination port and protocol */
1255a79f4f88SMatthew Vick 	if (is_l4) {
1256a79f4f88SMatthew Vick 		u32 ftqf = (IPPROTO_UDP /* UDP */
1257a79f4f88SMatthew Vick 			| E1000_FTQF_VF_BP /* VF not compared */
1258a79f4f88SMatthew Vick 			| E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
1259a79f4f88SMatthew Vick 			| E1000_FTQF_MASK); /* mask all inputs */
1260a79f4f88SMatthew Vick 		ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
1261a79f4f88SMatthew Vick 
12629fb8602eSJesse Brandeburg 		wr32(E1000_IMIR(3), (__force unsigned int)htons(PTP_EV_PORT));
1263a79f4f88SMatthew Vick 		wr32(E1000_IMIREXT(3),
1264a79f4f88SMatthew Vick 		     (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
1265a79f4f88SMatthew Vick 		if (hw->mac.type == e1000_82576) {
1266a79f4f88SMatthew Vick 			/* enable source port check */
12679fb8602eSJesse Brandeburg 			wr32(E1000_SPQF(3), (__force unsigned int)htons(PTP_EV_PORT));
1268a79f4f88SMatthew Vick 			ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
1269a79f4f88SMatthew Vick 		}
1270a79f4f88SMatthew Vick 		wr32(E1000_FTQF(3), ftqf);
1271a79f4f88SMatthew Vick 	} else {
1272a79f4f88SMatthew Vick 		wr32(E1000_FTQF(3), E1000_FTQF_MASK);
1273a79f4f88SMatthew Vick 	}
1274a79f4f88SMatthew Vick 	wrfl();
1275a79f4f88SMatthew Vick 
1276a79f4f88SMatthew Vick 	/* clear TX/RX time stamp registers, just to be sure */
1277e57b8bdbSMatthew Vick 	regval = rd32(E1000_TXSTMPL);
1278a79f4f88SMatthew Vick 	regval = rd32(E1000_TXSTMPH);
1279e57b8bdbSMatthew Vick 	regval = rd32(E1000_RXSTMPL);
1280a79f4f88SMatthew Vick 	regval = rd32(E1000_RXSTMPH);
1281a79f4f88SMatthew Vick 
12829f62ecf4SJacob Keller 	return 0;
12839f62ecf4SJacob Keller }
12849f62ecf4SJacob Keller 
12859f62ecf4SJacob Keller /**
12869f62ecf4SJacob Keller  * igb_ptp_set_ts_config - set hardware time stamping config
1287b50f7bcaSJesse Brandeburg  * @netdev: netdev struct
1288b50f7bcaSJesse Brandeburg  * @ifr: interface struct
12899f62ecf4SJacob Keller  *
12909f62ecf4SJacob Keller  **/
igb_ptp_set_ts_config(struct net_device * netdev,struct ifreq * ifr)12919f62ecf4SJacob Keller int igb_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
12929f62ecf4SJacob Keller {
12939f62ecf4SJacob Keller 	struct igb_adapter *adapter = netdev_priv(netdev);
12949f62ecf4SJacob Keller 	struct hwtstamp_config config;
12959f62ecf4SJacob Keller 	int err;
12969f62ecf4SJacob Keller 
12979f62ecf4SJacob Keller 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
12989f62ecf4SJacob Keller 		return -EFAULT;
12999f62ecf4SJacob Keller 
13009f62ecf4SJacob Keller 	err = igb_ptp_set_timestamp_mode(adapter, &config);
13019f62ecf4SJacob Keller 	if (err)
13029f62ecf4SJacob Keller 		return err;
13039f62ecf4SJacob Keller 
13049f62ecf4SJacob Keller 	/* save these settings for future reference */
13059f62ecf4SJacob Keller 	memcpy(&adapter->tstamp_config, &config,
13069f62ecf4SJacob Keller 	       sizeof(adapter->tstamp_config));
13079f62ecf4SJacob Keller 
13089f62ecf4SJacob Keller 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1309a79f4f88SMatthew Vick 		-EFAULT : 0;
1310d339b133SRichard Cochran }
1311d339b133SRichard Cochran 
13124f3ce71bSJacob Keller /**
13134f3ce71bSJacob Keller  * igb_ptp_init - Initialize PTP functionality
13144f3ce71bSJacob Keller  * @adapter: Board private structure
13154f3ce71bSJacob Keller  *
13164f3ce71bSJacob Keller  * This function is called at device probe to initialize the PTP
13174f3ce71bSJacob Keller  * functionality.
13184f3ce71bSJacob Keller  */
igb_ptp_init(struct igb_adapter * adapter)1319d339b133SRichard Cochran void igb_ptp_init(struct igb_adapter *adapter)
1320d339b133SRichard Cochran {
1321d339b133SRichard Cochran 	struct e1000_hw *hw = &adapter->hw;
1322201987e3SMatthew Vick 	struct net_device *netdev = adapter->netdev;
1323d339b133SRichard Cochran 
1324d339b133SRichard Cochran 	switch (hw->mac.type) {
1325d339b133SRichard Cochran 	case e1000_82576:
1326201987e3SMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
1327a79f4f88SMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
132875517d92SJiri Benc 		adapter->ptp_caps.max_adj = 999999881;
1329a79f4f88SMatthew Vick 		adapter->ptp_caps.n_ext_ts = 0;
1330a79f4f88SMatthew Vick 		adapter->ptp_caps.pps = 0;
1331d8fae250SJacob Keller 		adapter->ptp_caps.adjfine = igb_ptp_adjfine_82576;
1332e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
1333cff8ba28SMiroslav Lichvar 		adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_82576;
1334d4c496feSRichard Cochran 		adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
1335102be52fSJacob Keller 		adapter->ptp_caps.enable = igb_ptp_feature_enable;
1336a79f4f88SMatthew Vick 		adapter->cc.read = igb_ptp_read_82576;
1337b57c8940SRichard Cochran 		adapter->cc.mask = CYCLECOUNTER_MASK(64);
1338d339b133SRichard Cochran 		adapter->cc.mult = 1;
1339d339b133SRichard Cochran 		adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
134063737166SJacob Keller 		adapter->ptp_flags |= IGB_PTP_OVERFLOW_CHECK;
1341d339b133SRichard Cochran 		break;
1342e57b8bdbSMatthew Vick 	case e1000_82580:
1343ceb5f13bSCarolyn Wyborny 	case e1000_i354:
1344e57b8bdbSMatthew Vick 	case e1000_i350:
13451819fc75SRuud Bos 		igb_ptp_sdp_init(adapter);
1346e57b8bdbSMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
1347e57b8bdbSMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
1348e57b8bdbSMatthew Vick 		adapter->ptp_caps.max_adj = 62499999;
13491819fc75SRuud Bos 		adapter->ptp_caps.n_ext_ts = IGB_N_EXTTS;
13501819fc75SRuud Bos 		adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
13511819fc75SRuud Bos 		adapter->ptp_caps.n_pins = IGB_N_SDP;
1352e57b8bdbSMatthew Vick 		adapter->ptp_caps.pps = 0;
13531819fc75SRuud Bos 		adapter->ptp_caps.pin_config = adapter->sdp_config;
1354c79e975eSRichard Cochran 		adapter->ptp_caps.adjfine = igb_ptp_adjfine_82580;
1355e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
1356cff8ba28SMiroslav Lichvar 		adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_82580;
1357d4c496feSRichard Cochran 		adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
13581819fc75SRuud Bos 		adapter->ptp_caps.enable = igb_ptp_feature_enable_82580;
13591819fc75SRuud Bos 		adapter->ptp_caps.verify = igb_ptp_verify_pin;
1360e57b8bdbSMatthew Vick 		adapter->cc.read = igb_ptp_read_82580;
1361b57c8940SRichard Cochran 		adapter->cc.mask = CYCLECOUNTER_MASK(IGB_NBITS_82580);
1362e57b8bdbSMatthew Vick 		adapter->cc.mult = 1;
1363e57b8bdbSMatthew Vick 		adapter->cc.shift = 0;
136463737166SJacob Keller 		adapter->ptp_flags |= IGB_PTP_OVERFLOW_CHECK;
1365e57b8bdbSMatthew Vick 		break;
1366e57b8bdbSMatthew Vick 	case e1000_i210:
1367e57b8bdbSMatthew Vick 	case e1000_i211:
13688ab55abaSRuud Bos 		igb_ptp_sdp_init(adapter);
1369e57b8bdbSMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
1370e57b8bdbSMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
1371e57b8bdbSMatthew Vick 		adapter->ptp_caps.max_adj = 62499999;
1372720db4ffSRichard Cochran 		adapter->ptp_caps.n_ext_ts = IGB_N_EXTTS;
1373720db4ffSRichard Cochran 		adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
1374720db4ffSRichard Cochran 		adapter->ptp_caps.n_pins = IGB_N_SDP;
137500c65578SRichard Cochran 		adapter->ptp_caps.pps = 1;
1376720db4ffSRichard Cochran 		adapter->ptp_caps.pin_config = adapter->sdp_config;
1377c79e975eSRichard Cochran 		adapter->ptp_caps.adjfine = igb_ptp_adjfine_82580;
1378e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
1379cff8ba28SMiroslav Lichvar 		adapter->ptp_caps.gettimex64 = igb_ptp_gettimex_i210;
1380d4c496feSRichard Cochran 		adapter->ptp_caps.settime64 = igb_ptp_settime_i210;
138100c65578SRichard Cochran 		adapter->ptp_caps.enable = igb_ptp_feature_enable_i210;
1382720db4ffSRichard Cochran 		adapter->ptp_caps.verify = igb_ptp_verify_pin;
1383e57b8bdbSMatthew Vick 		break;
1384d339b133SRichard Cochran 	default:
1385d339b133SRichard Cochran 		adapter->ptp_clock = NULL;
1386d339b133SRichard Cochran 		return;
1387d339b133SRichard Cochran 	}
1388d339b133SRichard Cochran 
1389b888c510SAlessio Igor Bogani 	adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
1390b888c510SAlessio Igor Bogani 						&adapter->pdev->dev);
1391b888c510SAlessio Igor Bogani 	if (IS_ERR(adapter->ptp_clock)) {
1392b888c510SAlessio Igor Bogani 		adapter->ptp_clock = NULL;
1393b888c510SAlessio Igor Bogani 		dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
1394b888c510SAlessio Igor Bogani 	} else if (adapter->ptp_clock) {
1395b888c510SAlessio Igor Bogani 		dev_info(&adapter->pdev->dev, "added PHC on %s\n",
1396b888c510SAlessio Igor Bogani 			 adapter->netdev->name);
1397b888c510SAlessio Igor Bogani 		adapter->ptp_flags |= IGB_PTP_ENABLED;
1398b888c510SAlessio Igor Bogani 
1399e57b8bdbSMatthew Vick 		spin_lock_init(&adapter->tmreg_lock);
1400e57b8bdbSMatthew Vick 		INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
1401e57b8bdbSMatthew Vick 
14024f3ce71bSJacob Keller 		if (adapter->ptp_flags & IGB_PTP_OVERFLOW_CHECK)
1403e57b8bdbSMatthew Vick 			INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
1404e57b8bdbSMatthew Vick 					  igb_ptp_overflow_check);
14051f6e8178SMatthew Vick 
14069f62ecf4SJacob Keller 		adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
14079f62ecf4SJacob Keller 		adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
14089f62ecf4SJacob Keller 
14094f3ce71bSJacob Keller 		igb_ptp_reset(adapter);
14101f6e8178SMatthew Vick 	}
1411d339b133SRichard Cochran }
1412d339b133SRichard Cochran 
1413a79f4f88SMatthew Vick /**
14148ab55abaSRuud Bos  * igb_ptp_sdp_init - utility function which inits the SDP config structs
14158ab55abaSRuud Bos  * @adapter: Board private structure.
14168ab55abaSRuud Bos  **/
igb_ptp_sdp_init(struct igb_adapter * adapter)14178ab55abaSRuud Bos void igb_ptp_sdp_init(struct igb_adapter *adapter)
14188ab55abaSRuud Bos {
14198ab55abaSRuud Bos 	int i;
14208ab55abaSRuud Bos 
14218ab55abaSRuud Bos 	for (i = 0; i < IGB_N_SDP; i++) {
14228ab55abaSRuud Bos 		struct ptp_pin_desc *ppd = &adapter->sdp_config[i];
14238ab55abaSRuud Bos 
14248ab55abaSRuud Bos 		snprintf(ppd->name, sizeof(ppd->name), "SDP%d", i);
14258ab55abaSRuud Bos 		ppd->index = i;
14268ab55abaSRuud Bos 		ppd->func = PTP_PF_NONE;
14278ab55abaSRuud Bos 	}
14288ab55abaSRuud Bos }
14298ab55abaSRuud Bos 
14308ab55abaSRuud Bos /**
1431e3f2350dSJacob Keller  * igb_ptp_suspend - Disable PTP work items and prepare for suspend
1432e3f2350dSJacob Keller  * @adapter: Board private structure
1433a79f4f88SMatthew Vick  *
1434e3f2350dSJacob Keller  * This function stops the overflow check work and PTP Tx timestamp work, and
1435e3f2350dSJacob Keller  * will prepare the device for OS suspend.
1436e3f2350dSJacob Keller  */
igb_ptp_suspend(struct igb_adapter * adapter)1437e3f2350dSJacob Keller void igb_ptp_suspend(struct igb_adapter *adapter)
1438d339b133SRichard Cochran {
143963737166SJacob Keller 	if (!(adapter->ptp_flags & IGB_PTP_ENABLED))
1440d3eef8c8SCarolyn Wyborny 		return;
144163737166SJacob Keller 
144263737166SJacob Keller 	if (adapter->ptp_flags & IGB_PTP_OVERFLOW_CHECK)
144363737166SJacob Keller 		cancel_delayed_work_sync(&adapter->ptp_overflow_work);
1444d339b133SRichard Cochran 
14451f6e8178SMatthew Vick 	cancel_work_sync(&adapter->ptp_tx_work);
1446badc26ddSMatthew Vick 	if (adapter->ptp_tx_skb) {
1447badc26ddSMatthew Vick 		dev_kfree_skb_any(adapter->ptp_tx_skb);
1448badc26ddSMatthew Vick 		adapter->ptp_tx_skb = NULL;
1449ed4420a3SJakub Kicinski 		clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
1450badc26ddSMatthew Vick 	}
1451e3f2350dSJacob Keller }
1452e3f2350dSJacob Keller 
1453e3f2350dSJacob Keller /**
1454e3f2350dSJacob Keller  * igb_ptp_stop - Disable PTP device and stop the overflow check.
1455e3f2350dSJacob Keller  * @adapter: Board private structure.
1456e3f2350dSJacob Keller  *
1457e3f2350dSJacob Keller  * This function stops the PTP support and cancels the delayed work.
1458e3f2350dSJacob Keller  **/
igb_ptp_stop(struct igb_adapter * adapter)1459e3f2350dSJacob Keller void igb_ptp_stop(struct igb_adapter *adapter)
1460e3f2350dSJacob Keller {
1461e3f2350dSJacob Keller 	igb_ptp_suspend(adapter);
14621f6e8178SMatthew Vick 
1463d339b133SRichard Cochran 	if (adapter->ptp_clock) {
1464d339b133SRichard Cochran 		ptp_clock_unregister(adapter->ptp_clock);
1465d339b133SRichard Cochran 		dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
1466d339b133SRichard Cochran 			 adapter->netdev->name);
1467462f1188SJacob Keller 		adapter->ptp_flags &= ~IGB_PTP_ENABLED;
1468d339b133SRichard Cochran 	}
1469d339b133SRichard Cochran }
14701f6e8178SMatthew Vick 
14711f6e8178SMatthew Vick /**
14721f6e8178SMatthew Vick  * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
14731f6e8178SMatthew Vick  * @adapter: Board private structure.
14741f6e8178SMatthew Vick  *
14751f6e8178SMatthew Vick  * This function handles the reset work required to re-enable the PTP device.
14761f6e8178SMatthew Vick  **/
igb_ptp_reset(struct igb_adapter * adapter)14771f6e8178SMatthew Vick void igb_ptp_reset(struct igb_adapter *adapter)
14781f6e8178SMatthew Vick {
14791f6e8178SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
14808298c1ecSRichard Cochran 	unsigned long flags;
14811f6e8178SMatthew Vick 
14826ab5f7b2SJacob Keller 	/* reset the tstamp_config */
14839f62ecf4SJacob Keller 	igb_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
14846ab5f7b2SJacob Keller 
14858298c1ecSRichard Cochran 	spin_lock_irqsave(&adapter->tmreg_lock, flags);
14868298c1ecSRichard Cochran 
14871f6e8178SMatthew Vick 	switch (adapter->hw.mac.type) {
14881f6e8178SMatthew Vick 	case e1000_82576:
14891f6e8178SMatthew Vick 		/* Dial the nominal frequency. */
14901f6e8178SMatthew Vick 		wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
14911f6e8178SMatthew Vick 		break;
14921f6e8178SMatthew Vick 	case e1000_82580:
1493ceb5f13bSCarolyn Wyborny 	case e1000_i354:
14941f6e8178SMatthew Vick 	case e1000_i350:
14951f6e8178SMatthew Vick 	case e1000_i210:
14961f6e8178SMatthew Vick 	case e1000_i211:
14971f6e8178SMatthew Vick 		wr32(E1000_TSAUXC, 0x0);
1498720db4ffSRichard Cochran 		wr32(E1000_TSSDP, 0x0);
1499ac28b41aSJacob Keller 		wr32(E1000_TSIM,
1500ac28b41aSJacob Keller 		     TSYNC_INTERRUPTS |
1501ac28b41aSJacob Keller 		     (adapter->pps_sys_wrap_on ? TSINTR_SYS_WRAP : 0));
15021f6e8178SMatthew Vick 		wr32(E1000_IMS, E1000_IMS_TS);
15031f6e8178SMatthew Vick 		break;
15041f6e8178SMatthew Vick 	default:
15051f6e8178SMatthew Vick 		/* No work to do. */
15068298c1ecSRichard Cochran 		goto out;
15071f6e8178SMatthew Vick 	}
15081f6e8178SMatthew Vick 
1509e57b8bdbSMatthew Vick 	/* Re-initialize the timer. */
1510e57b8bdbSMatthew Vick 	if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
1511d4c496feSRichard Cochran 		struct timespec64 ts = ktime_to_timespec64(ktime_get_real());
1512e57b8bdbSMatthew Vick 
15138298c1ecSRichard Cochran 		igb_ptp_write_i210(adapter, &ts);
1514e57b8bdbSMatthew Vick 	} else {
15151f6e8178SMatthew Vick 		timecounter_init(&adapter->tc, &adapter->cc,
15161f6e8178SMatthew Vick 				 ktime_to_ns(ktime_get_real()));
15171f6e8178SMatthew Vick 	}
15188298c1ecSRichard Cochran out:
15198298c1ecSRichard Cochran 	spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
15204f3ce71bSJacob Keller 
15214f3ce71bSJacob Keller 	wrfl();
15224f3ce71bSJacob Keller 
15234f3ce71bSJacob Keller 	if (adapter->ptp_flags & IGB_PTP_OVERFLOW_CHECK)
15244f3ce71bSJacob Keller 		schedule_delayed_work(&adapter->ptp_overflow_work,
15254f3ce71bSJacob Keller 				      IGB_SYSTIM_OVERFLOW_PERIOD);
1526e57b8bdbSMatthew Vick }
1527