1b980ac18SJeff Kirsher /* PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580
2d339b133SRichard Cochran  *
3d339b133SRichard Cochran  * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
4d339b133SRichard Cochran  *
5d339b133SRichard Cochran  * This program is free software; you can redistribute it and/or modify
6d339b133SRichard Cochran  * it under the terms of the GNU General Public License as published by
7d339b133SRichard Cochran  * the Free Software Foundation; either version 2 of the License, or
8d339b133SRichard Cochran  * (at your option) any later version.
9d339b133SRichard Cochran  *
10d339b133SRichard Cochran  * This program is distributed in the hope that it will be useful,
11d339b133SRichard Cochran  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d339b133SRichard Cochran  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13d339b133SRichard Cochran  * GNU General Public License for more details.
14d339b133SRichard Cochran  *
1574cfb2e1SCarolyn Wyborny  * You should have received a copy of the GNU General Public License along with
1674cfb2e1SCarolyn Wyborny  * this program; if not, see <http://www.gnu.org/licenses/>.
17d339b133SRichard Cochran  */
18d339b133SRichard Cochran #include <linux/module.h>
19d339b133SRichard Cochran #include <linux/device.h>
20d339b133SRichard Cochran #include <linux/pci.h>
21ba59814bSMatthew Vick #include <linux/ptp_classify.h>
22d339b133SRichard Cochran 
23d339b133SRichard Cochran #include "igb.h"
24d339b133SRichard Cochran 
25d339b133SRichard Cochran #define INCVALUE_MASK		0x7fffffff
26d339b133SRichard Cochran #define ISGN			0x80000000
27d339b133SRichard Cochran 
28b980ac18SJeff Kirsher /* The 82580 timesync updates the system timer every 8ns by 8ns,
297ebae817SRichard Cochran  * and this update value cannot be reprogrammed.
307ebae817SRichard Cochran  *
31d339b133SRichard Cochran  * Neither the 82576 nor the 82580 offer registers wide enough to hold
32d339b133SRichard Cochran  * nanoseconds time values for very long. For the 82580, SYSTIM always
33dbedd44eSJoe Perches  * counts nanoseconds, but the upper 24 bits are not available. The
34d339b133SRichard Cochran  * frequency is adjusted by changing the 32 bit fractional nanoseconds
35d339b133SRichard Cochran  * register, TIMINCA.
36d339b133SRichard Cochran  *
37d339b133SRichard Cochran  * For the 82576, the SYSTIM register time unit is affect by the
38d339b133SRichard Cochran  * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
39d339b133SRichard Cochran  * field are needed to provide the nominal 16 nanosecond period,
40d339b133SRichard Cochran  * leaving 19 bits for fractional nanoseconds.
41d339b133SRichard Cochran  *
427ebae817SRichard Cochran  * We scale the NIC clock cycle by a large factor so that relatively
437ebae817SRichard Cochran  * small clock corrections can be added or subtracted at each clock
447ebae817SRichard Cochran  * tick. The drawbacks of a large factor are a) that the clock
457ebae817SRichard Cochran  * register overflows more quickly (not such a big deal) and b) that
467ebae817SRichard Cochran  * the increment per tick has to fit into 24 bits.  As a result we
477ebae817SRichard Cochran  * need to use a shift of 19 so we can fit a value of 16 into the
487ebae817SRichard Cochran  * TIMINCA register.
497ebae817SRichard Cochran  *
50d339b133SRichard Cochran  *
51d339b133SRichard Cochran  *             SYSTIMH            SYSTIML
52d339b133SRichard Cochran  *        +--------------+   +---+---+------+
53d339b133SRichard Cochran  *  82576 |      32      |   | 8 | 5 |  19  |
54d339b133SRichard Cochran  *        +--------------+   +---+---+------+
55d339b133SRichard Cochran  *         \________ 45 bits _______/  fract
56d339b133SRichard Cochran  *
57d339b133SRichard Cochran  *        +----------+---+   +--------------+
58d339b133SRichard Cochran  *  82580 |    24    | 8 |   |      32      |
59d339b133SRichard Cochran  *        +----------+---+   +--------------+
60d339b133SRichard Cochran  *          reserved  \______ 40 bits _____/
61d339b133SRichard Cochran  *
62d339b133SRichard Cochran  *
63d339b133SRichard Cochran  * The 45 bit 82576 SYSTIM overflows every
64d339b133SRichard Cochran  *   2^45 * 10^-9 / 3600 = 9.77 hours.
65d339b133SRichard Cochran  *
66d339b133SRichard Cochran  * The 40 bit 82580 SYSTIM overflows every
67d339b133SRichard Cochran  *   2^40 * 10^-9 /  60  = 18.3 minutes.
68d339b133SRichard Cochran  */
69d339b133SRichard Cochran 
70a79f4f88SMatthew Vick #define IGB_SYSTIM_OVERFLOW_PERIOD	(HZ * 60 * 9)
71428f1f71SMatthew Vick #define IGB_PTP_TX_TIMEOUT		(HZ * 15)
72d339b133SRichard Cochran #define INCPERIOD_82576			(1 << E1000_TIMINCA_16NS_SHIFT)
73d339b133SRichard Cochran #define INCVALUE_82576_MASK		((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
74d339b133SRichard Cochran #define INCVALUE_82576			(16 << IGB_82576_TSYNC_SHIFT)
75d339b133SRichard Cochran #define IGB_NBITS_82580			40
76d339b133SRichard Cochran 
77167f3f71SJeff Kirsher static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter);
78167f3f71SJeff Kirsher 
79b980ac18SJeff Kirsher /* SYSTIM read access for the 82576 */
80a79f4f88SMatthew Vick static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc)
81d339b133SRichard Cochran {
82d339b133SRichard Cochran 	struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
83d339b133SRichard Cochran 	struct e1000_hw *hw = &igb->hw;
84a79f4f88SMatthew Vick 	u64 val;
85a79f4f88SMatthew Vick 	u32 lo, hi;
86d339b133SRichard Cochran 
87d339b133SRichard Cochran 	lo = rd32(E1000_SYSTIML);
88d339b133SRichard Cochran 	hi = rd32(E1000_SYSTIMH);
89d339b133SRichard Cochran 
90d339b133SRichard Cochran 	val = ((u64) hi) << 32;
91d339b133SRichard Cochran 	val |= lo;
92d339b133SRichard Cochran 
93d339b133SRichard Cochran 	return val;
94d339b133SRichard Cochran }
95d339b133SRichard Cochran 
96b980ac18SJeff Kirsher /* SYSTIM read access for the 82580 */
97a79f4f88SMatthew Vick static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc)
98d339b133SRichard Cochran {
99d339b133SRichard Cochran 	struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
100d339b133SRichard Cochran 	struct e1000_hw *hw = &igb->hw;
101e5c3370fSAkeem G Abodunrin 	u32 lo, hi;
102a79f4f88SMatthew Vick 	u64 val;
103d339b133SRichard Cochran 
104b980ac18SJeff Kirsher 	/* The timestamp latches on lowest register read. For the 82580
1057ebae817SRichard Cochran 	 * the lowest register is SYSTIMR instead of SYSTIML.  However we only
1067ebae817SRichard Cochran 	 * need to provide nanosecond resolution, so we just ignore it.
1077ebae817SRichard Cochran 	 */
108e5c3370fSAkeem G Abodunrin 	rd32(E1000_SYSTIMR);
109d339b133SRichard Cochran 	lo = rd32(E1000_SYSTIML);
110d339b133SRichard Cochran 	hi = rd32(E1000_SYSTIMH);
111d339b133SRichard Cochran 
112d339b133SRichard Cochran 	val = ((u64) hi) << 32;
113d339b133SRichard Cochran 	val |= lo;
114d339b133SRichard Cochran 
115d339b133SRichard Cochran 	return val;
116d339b133SRichard Cochran }
117d339b133SRichard Cochran 
118b980ac18SJeff Kirsher /* SYSTIM read access for I210/I211 */
119d4c496feSRichard Cochran static void igb_ptp_read_i210(struct igb_adapter *adapter,
120d4c496feSRichard Cochran 			      struct timespec64 *ts)
121e57b8bdbSMatthew Vick {
122e57b8bdbSMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
123e5c3370fSAkeem G Abodunrin 	u32 sec, nsec;
124e57b8bdbSMatthew Vick 
125b980ac18SJeff Kirsher 	/* The timestamp latches on lowest register read. For I210/I211, the
126e57b8bdbSMatthew Vick 	 * lowest register is SYSTIMR. Since we only need to provide nanosecond
127e57b8bdbSMatthew Vick 	 * resolution, we can ignore it.
128e57b8bdbSMatthew Vick 	 */
129e5c3370fSAkeem G Abodunrin 	rd32(E1000_SYSTIMR);
130e57b8bdbSMatthew Vick 	nsec = rd32(E1000_SYSTIML);
131e57b8bdbSMatthew Vick 	sec = rd32(E1000_SYSTIMH);
132e57b8bdbSMatthew Vick 
133e57b8bdbSMatthew Vick 	ts->tv_sec = sec;
134e57b8bdbSMatthew Vick 	ts->tv_nsec = nsec;
135e57b8bdbSMatthew Vick }
136e57b8bdbSMatthew Vick 
137e57b8bdbSMatthew Vick static void igb_ptp_write_i210(struct igb_adapter *adapter,
138d4c496feSRichard Cochran 			       const struct timespec64 *ts)
139e57b8bdbSMatthew Vick {
140e57b8bdbSMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
141e57b8bdbSMatthew Vick 
142b980ac18SJeff Kirsher 	/* Writing the SYSTIMR register is not necessary as it only provides
143e57b8bdbSMatthew Vick 	 * sub-nanosecond resolution.
144e57b8bdbSMatthew Vick 	 */
145e57b8bdbSMatthew Vick 	wr32(E1000_SYSTIML, ts->tv_nsec);
146e57b8bdbSMatthew Vick 	wr32(E1000_SYSTIMH, ts->tv_sec);
147e57b8bdbSMatthew Vick }
148e57b8bdbSMatthew Vick 
149a79f4f88SMatthew Vick /**
150a79f4f88SMatthew Vick  * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
151a79f4f88SMatthew Vick  * @adapter: board private structure
152a79f4f88SMatthew Vick  * @hwtstamps: timestamp structure to update
153a79f4f88SMatthew Vick  * @systim: unsigned 64bit system time value.
154a79f4f88SMatthew Vick  *
155a79f4f88SMatthew Vick  * We need to convert the system time value stored in the RX/TXSTMP registers
156a79f4f88SMatthew Vick  * into a hwtstamp which can be used by the upper level timestamping functions.
157a79f4f88SMatthew Vick  *
158a79f4f88SMatthew Vick  * The 'tmreg_lock' spinlock is used to protect the consistency of the
159a79f4f88SMatthew Vick  * system time value. This is needed because reading the 64 bit time
160a79f4f88SMatthew Vick  * value involves reading two (or three) 32 bit registers. The first
161a79f4f88SMatthew Vick  * read latches the value. Ditto for writing.
162a79f4f88SMatthew Vick  *
163a79f4f88SMatthew Vick  * In addition, here have extended the system time with an overflow
164a79f4f88SMatthew Vick  * counter in software.
165a79f4f88SMatthew Vick  **/
166a79f4f88SMatthew Vick static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
167a79f4f88SMatthew Vick 				       struct skb_shared_hwtstamps *hwtstamps,
168a79f4f88SMatthew Vick 				       u64 systim)
169a79f4f88SMatthew Vick {
170a79f4f88SMatthew Vick 	unsigned long flags;
171a79f4f88SMatthew Vick 	u64 ns;
172a79f4f88SMatthew Vick 
173a79f4f88SMatthew Vick 	switch (adapter->hw.mac.type) {
174a79f4f88SMatthew Vick 	case e1000_82576:
175e57b8bdbSMatthew Vick 	case e1000_82580:
176ceb5f13bSCarolyn Wyborny 	case e1000_i354:
177e57b8bdbSMatthew Vick 	case e1000_i350:
178a79f4f88SMatthew Vick 		spin_lock_irqsave(&adapter->tmreg_lock, flags);
179a79f4f88SMatthew Vick 
180a79f4f88SMatthew Vick 		ns = timecounter_cyc2time(&adapter->tc, systim);
181a79f4f88SMatthew Vick 
182a79f4f88SMatthew Vick 		spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
183a79f4f88SMatthew Vick 
184a79f4f88SMatthew Vick 		memset(hwtstamps, 0, sizeof(*hwtstamps));
185a79f4f88SMatthew Vick 		hwtstamps->hwtstamp = ns_to_ktime(ns);
186e57b8bdbSMatthew Vick 		break;
187e57b8bdbSMatthew Vick 	case e1000_i210:
188e57b8bdbSMatthew Vick 	case e1000_i211:
189e57b8bdbSMatthew Vick 		memset(hwtstamps, 0, sizeof(*hwtstamps));
190e57b8bdbSMatthew Vick 		/* Upper 32 bits contain s, lower 32 bits contain ns. */
191e57b8bdbSMatthew Vick 		hwtstamps->hwtstamp = ktime_set(systim >> 32,
192e57b8bdbSMatthew Vick 						systim & 0xFFFFFFFF);
193e57b8bdbSMatthew Vick 		break;
194e57b8bdbSMatthew Vick 	default:
195e57b8bdbSMatthew Vick 		break;
196e57b8bdbSMatthew Vick 	}
197a79f4f88SMatthew Vick }
198a79f4f88SMatthew Vick 
199b980ac18SJeff Kirsher /* PTP clock operations */
200a79f4f88SMatthew Vick static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
201d339b133SRichard Cochran {
202a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
203a79f4f88SMatthew Vick 					       ptp_caps);
204a79f4f88SMatthew Vick 	struct e1000_hw *hw = &igb->hw;
205a79f4f88SMatthew Vick 	int neg_adj = 0;
206d339b133SRichard Cochran 	u64 rate;
207d339b133SRichard Cochran 	u32 incvalue;
208d339b133SRichard Cochran 
209d339b133SRichard Cochran 	if (ppb < 0) {
210d339b133SRichard Cochran 		neg_adj = 1;
211d339b133SRichard Cochran 		ppb = -ppb;
212d339b133SRichard Cochran 	}
213d339b133SRichard Cochran 	rate = ppb;
214d339b133SRichard Cochran 	rate <<= 14;
215d339b133SRichard Cochran 	rate = div_u64(rate, 1953125);
216d339b133SRichard Cochran 
217d339b133SRichard Cochran 	incvalue = 16 << IGB_82576_TSYNC_SHIFT;
218d339b133SRichard Cochran 
219d339b133SRichard Cochran 	if (neg_adj)
220d339b133SRichard Cochran 		incvalue -= rate;
221d339b133SRichard Cochran 	else
222d339b133SRichard Cochran 		incvalue += rate;
223d339b133SRichard Cochran 
224d339b133SRichard Cochran 	wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
225d339b133SRichard Cochran 
226d339b133SRichard Cochran 	return 0;
227d339b133SRichard Cochran }
228d339b133SRichard Cochran 
229a79f4f88SMatthew Vick static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb)
230d339b133SRichard Cochran {
231a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
232a79f4f88SMatthew Vick 					       ptp_caps);
233a79f4f88SMatthew Vick 	struct e1000_hw *hw = &igb->hw;
234a79f4f88SMatthew Vick 	int neg_adj = 0;
235d339b133SRichard Cochran 	u64 rate;
236d339b133SRichard Cochran 	u32 inca;
237d339b133SRichard Cochran 
238d339b133SRichard Cochran 	if (ppb < 0) {
239d339b133SRichard Cochran 		neg_adj = 1;
240d339b133SRichard Cochran 		ppb = -ppb;
241d339b133SRichard Cochran 	}
242d339b133SRichard Cochran 	rate = ppb;
243d339b133SRichard Cochran 	rate <<= 26;
244d339b133SRichard Cochran 	rate = div_u64(rate, 1953125);
245d339b133SRichard Cochran 
246d339b133SRichard Cochran 	inca = rate & INCVALUE_MASK;
247d339b133SRichard Cochran 	if (neg_adj)
248d339b133SRichard Cochran 		inca |= ISGN;
249d339b133SRichard Cochran 
250d339b133SRichard Cochran 	wr32(E1000_TIMINCA, inca);
251d339b133SRichard Cochran 
252d339b133SRichard Cochran 	return 0;
253d339b133SRichard Cochran }
254d339b133SRichard Cochran 
255e57b8bdbSMatthew Vick static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
256d339b133SRichard Cochran {
257a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
258a79f4f88SMatthew Vick 					       ptp_caps);
259d339b133SRichard Cochran 	unsigned long flags;
260d339b133SRichard Cochran 
261d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
2625ee698e3SRichard Cochran 	timecounter_adjtime(&igb->tc, delta);
263d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
264d339b133SRichard Cochran 
265d339b133SRichard Cochran 	return 0;
266d339b133SRichard Cochran }
267d339b133SRichard Cochran 
268e57b8bdbSMatthew Vick static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
269e57b8bdbSMatthew Vick {
270e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
271e57b8bdbSMatthew Vick 					       ptp_caps);
272e57b8bdbSMatthew Vick 	unsigned long flags;
273d4c496feSRichard Cochran 	struct timespec64 now, then = ns_to_timespec64(delta);
274e57b8bdbSMatthew Vick 
275e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
276e57b8bdbSMatthew Vick 
277e57b8bdbSMatthew Vick 	igb_ptp_read_i210(igb, &now);
278d4c496feSRichard Cochran 	now = timespec64_add(now, then);
279d4c496feSRichard Cochran 	igb_ptp_write_i210(igb, (const struct timespec64 *)&now);
280e57b8bdbSMatthew Vick 
281e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
282e57b8bdbSMatthew Vick 
283e57b8bdbSMatthew Vick 	return 0;
284e57b8bdbSMatthew Vick }
285e57b8bdbSMatthew Vick 
286e57b8bdbSMatthew Vick static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
287d4c496feSRichard Cochran 				 struct timespec64 *ts)
288d339b133SRichard Cochran {
289a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
290a79f4f88SMatthew Vick 					       ptp_caps);
291a79f4f88SMatthew Vick 	unsigned long flags;
292d339b133SRichard Cochran 	u64 ns;
293d339b133SRichard Cochran 
294d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
295d339b133SRichard Cochran 
296d339b133SRichard Cochran 	ns = timecounter_read(&igb->tc);
297d339b133SRichard Cochran 
298d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
299d339b133SRichard Cochran 
300350f66d5SRichard Cochran 	*ts = ns_to_timespec64(ns);
301d339b133SRichard Cochran 
302d339b133SRichard Cochran 	return 0;
303d339b133SRichard Cochran }
304d339b133SRichard Cochran 
305e57b8bdbSMatthew Vick static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
306d4c496feSRichard Cochran 				struct timespec64 *ts)
307e57b8bdbSMatthew Vick {
308e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
309e57b8bdbSMatthew Vick 					       ptp_caps);
310e57b8bdbSMatthew Vick 	unsigned long flags;
311e57b8bdbSMatthew Vick 
312e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
313e57b8bdbSMatthew Vick 
314e57b8bdbSMatthew Vick 	igb_ptp_read_i210(igb, ts);
315e57b8bdbSMatthew Vick 
316e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
317e57b8bdbSMatthew Vick 
318e57b8bdbSMatthew Vick 	return 0;
319e57b8bdbSMatthew Vick }
320e57b8bdbSMatthew Vick 
321e57b8bdbSMatthew Vick static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
322d4c496feSRichard Cochran 				 const struct timespec64 *ts)
323d339b133SRichard Cochran {
324a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
325a79f4f88SMatthew Vick 					       ptp_caps);
326d339b133SRichard Cochran 	unsigned long flags;
327a79f4f88SMatthew Vick 	u64 ns;
328d339b133SRichard Cochran 
329350f66d5SRichard Cochran 	ns = timespec64_to_ns(ts);
330d339b133SRichard Cochran 
331d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
332d339b133SRichard Cochran 
333d339b133SRichard Cochran 	timecounter_init(&igb->tc, &igb->cc, ns);
334d339b133SRichard Cochran 
335d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
336d339b133SRichard Cochran 
337d339b133SRichard Cochran 	return 0;
338d339b133SRichard Cochran }
339d339b133SRichard Cochran 
340e57b8bdbSMatthew Vick static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
341d4c496feSRichard Cochran 				const struct timespec64 *ts)
342e57b8bdbSMatthew Vick {
343e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
344e57b8bdbSMatthew Vick 					       ptp_caps);
345e57b8bdbSMatthew Vick 	unsigned long flags;
346e57b8bdbSMatthew Vick 
347e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
348e57b8bdbSMatthew Vick 
349e57b8bdbSMatthew Vick 	igb_ptp_write_i210(igb, ts);
350e57b8bdbSMatthew Vick 
351e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
352e57b8bdbSMatthew Vick 
353e57b8bdbSMatthew Vick 	return 0;
354e57b8bdbSMatthew Vick }
355e57b8bdbSMatthew Vick 
356720db4ffSRichard Cochran static void igb_pin_direction(int pin, int input, u32 *ctrl, u32 *ctrl_ext)
357720db4ffSRichard Cochran {
358720db4ffSRichard Cochran 	u32 *ptr = pin < 2 ? ctrl : ctrl_ext;
359b23c0cc5SAlexander Duyck 	static const u32 mask[IGB_N_SDP] = {
360720db4ffSRichard Cochran 		E1000_CTRL_SDP0_DIR,
361720db4ffSRichard Cochran 		E1000_CTRL_SDP1_DIR,
362720db4ffSRichard Cochran 		E1000_CTRL_EXT_SDP2_DIR,
363720db4ffSRichard Cochran 		E1000_CTRL_EXT_SDP3_DIR,
364720db4ffSRichard Cochran 	};
365720db4ffSRichard Cochran 
366720db4ffSRichard Cochran 	if (input)
367720db4ffSRichard Cochran 		*ptr &= ~mask[pin];
368720db4ffSRichard Cochran 	else
369720db4ffSRichard Cochran 		*ptr |= mask[pin];
370720db4ffSRichard Cochran }
371720db4ffSRichard Cochran 
372720db4ffSRichard Cochran static void igb_pin_extts(struct igb_adapter *igb, int chan, int pin)
373720db4ffSRichard Cochran {
374b23c0cc5SAlexander Duyck 	static const u32 aux0_sel_sdp[IGB_N_SDP] = {
375720db4ffSRichard Cochran 		AUX0_SEL_SDP0, AUX0_SEL_SDP1, AUX0_SEL_SDP2, AUX0_SEL_SDP3,
376720db4ffSRichard Cochran 	};
377b23c0cc5SAlexander Duyck 	static const u32 aux1_sel_sdp[IGB_N_SDP] = {
378720db4ffSRichard Cochran 		AUX1_SEL_SDP0, AUX1_SEL_SDP1, AUX1_SEL_SDP2, AUX1_SEL_SDP3,
379720db4ffSRichard Cochran 	};
380b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_en[IGB_N_SDP] = {
381720db4ffSRichard Cochran 		TS_SDP0_EN, TS_SDP1_EN, TS_SDP2_EN, TS_SDP3_EN,
382720db4ffSRichard Cochran 	};
383b23c0cc5SAlexander Duyck 	struct e1000_hw *hw = &igb->hw;
384720db4ffSRichard Cochran 	u32 ctrl, ctrl_ext, tssdp = 0;
385720db4ffSRichard Cochran 
386720db4ffSRichard Cochran 	ctrl = rd32(E1000_CTRL);
387720db4ffSRichard Cochran 	ctrl_ext = rd32(E1000_CTRL_EXT);
388720db4ffSRichard Cochran 	tssdp = rd32(E1000_TSSDP);
389720db4ffSRichard Cochran 
390720db4ffSRichard Cochran 	igb_pin_direction(pin, 1, &ctrl, &ctrl_ext);
391720db4ffSRichard Cochran 
392720db4ffSRichard Cochran 	/* Make sure this pin is not enabled as an output. */
393720db4ffSRichard Cochran 	tssdp &= ~ts_sdp_en[pin];
394720db4ffSRichard Cochran 
395720db4ffSRichard Cochran 	if (chan == 1) {
396720db4ffSRichard Cochran 		tssdp &= ~AUX1_SEL_SDP3;
397720db4ffSRichard Cochran 		tssdp |= aux1_sel_sdp[pin] | AUX1_TS_SDP_EN;
398720db4ffSRichard Cochran 	} else {
399720db4ffSRichard Cochran 		tssdp &= ~AUX0_SEL_SDP3;
400720db4ffSRichard Cochran 		tssdp |= aux0_sel_sdp[pin] | AUX0_TS_SDP_EN;
401720db4ffSRichard Cochran 	}
402720db4ffSRichard Cochran 
403720db4ffSRichard Cochran 	wr32(E1000_TSSDP, tssdp);
404720db4ffSRichard Cochran 	wr32(E1000_CTRL, ctrl);
405720db4ffSRichard Cochran 	wr32(E1000_CTRL_EXT, ctrl_ext);
406720db4ffSRichard Cochran }
407720db4ffSRichard Cochran 
408720db4ffSRichard Cochran static void igb_pin_perout(struct igb_adapter *igb, int chan, int pin)
409720db4ffSRichard Cochran {
410b23c0cc5SAlexander Duyck 	static const u32 aux0_sel_sdp[IGB_N_SDP] = {
411720db4ffSRichard Cochran 		AUX0_SEL_SDP0, AUX0_SEL_SDP1, AUX0_SEL_SDP2, AUX0_SEL_SDP3,
412720db4ffSRichard Cochran 	};
413b23c0cc5SAlexander Duyck 	static const u32 aux1_sel_sdp[IGB_N_SDP] = {
414720db4ffSRichard Cochran 		AUX1_SEL_SDP0, AUX1_SEL_SDP1, AUX1_SEL_SDP2, AUX1_SEL_SDP3,
415720db4ffSRichard Cochran 	};
416b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_en[IGB_N_SDP] = {
417720db4ffSRichard Cochran 		TS_SDP0_EN, TS_SDP1_EN, TS_SDP2_EN, TS_SDP3_EN,
418720db4ffSRichard Cochran 	};
419b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_sel_tt0[IGB_N_SDP] = {
420720db4ffSRichard Cochran 		TS_SDP0_SEL_TT0, TS_SDP1_SEL_TT0,
421720db4ffSRichard Cochran 		TS_SDP2_SEL_TT0, TS_SDP3_SEL_TT0,
422720db4ffSRichard Cochran 	};
423b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_sel_tt1[IGB_N_SDP] = {
424720db4ffSRichard Cochran 		TS_SDP0_SEL_TT1, TS_SDP1_SEL_TT1,
425720db4ffSRichard Cochran 		TS_SDP2_SEL_TT1, TS_SDP3_SEL_TT1,
426720db4ffSRichard Cochran 	};
427b23c0cc5SAlexander Duyck 	static const u32 ts_sdp_sel_clr[IGB_N_SDP] = {
428720db4ffSRichard Cochran 		TS_SDP0_SEL_FC1, TS_SDP1_SEL_FC1,
429720db4ffSRichard Cochran 		TS_SDP2_SEL_FC1, TS_SDP3_SEL_FC1,
430720db4ffSRichard Cochran 	};
431b23c0cc5SAlexander Duyck 	struct e1000_hw *hw = &igb->hw;
432720db4ffSRichard Cochran 	u32 ctrl, ctrl_ext, tssdp = 0;
433720db4ffSRichard Cochran 
434720db4ffSRichard Cochran 	ctrl = rd32(E1000_CTRL);
435720db4ffSRichard Cochran 	ctrl_ext = rd32(E1000_CTRL_EXT);
436720db4ffSRichard Cochran 	tssdp = rd32(E1000_TSSDP);
437720db4ffSRichard Cochran 
438720db4ffSRichard Cochran 	igb_pin_direction(pin, 0, &ctrl, &ctrl_ext);
439720db4ffSRichard Cochran 
440720db4ffSRichard Cochran 	/* Make sure this pin is not enabled as an input. */
441720db4ffSRichard Cochran 	if ((tssdp & AUX0_SEL_SDP3) == aux0_sel_sdp[pin])
442720db4ffSRichard Cochran 		tssdp &= ~AUX0_TS_SDP_EN;
443720db4ffSRichard Cochran 
444720db4ffSRichard Cochran 	if ((tssdp & AUX1_SEL_SDP3) == aux1_sel_sdp[pin])
445720db4ffSRichard Cochran 		tssdp &= ~AUX1_TS_SDP_EN;
446720db4ffSRichard Cochran 
447720db4ffSRichard Cochran 	tssdp &= ~ts_sdp_sel_clr[pin];
448720db4ffSRichard Cochran 	if (chan == 1)
449720db4ffSRichard Cochran 		tssdp |= ts_sdp_sel_tt1[pin];
450720db4ffSRichard Cochran 	else
451720db4ffSRichard Cochran 		tssdp |= ts_sdp_sel_tt0[pin];
452720db4ffSRichard Cochran 
453720db4ffSRichard Cochran 	tssdp |= ts_sdp_en[pin];
454720db4ffSRichard Cochran 
455720db4ffSRichard Cochran 	wr32(E1000_TSSDP, tssdp);
456720db4ffSRichard Cochran 	wr32(E1000_CTRL, ctrl);
457720db4ffSRichard Cochran 	wr32(E1000_CTRL_EXT, ctrl_ext);
458720db4ffSRichard Cochran }
459720db4ffSRichard Cochran 
46000c65578SRichard Cochran static int igb_ptp_feature_enable_i210(struct ptp_clock_info *ptp,
46100c65578SRichard Cochran 				       struct ptp_clock_request *rq, int on)
46200c65578SRichard Cochran {
46300c65578SRichard Cochran 	struct igb_adapter *igb =
46400c65578SRichard Cochran 		container_of(ptp, struct igb_adapter, ptp_caps);
46500c65578SRichard Cochran 	struct e1000_hw *hw = &igb->hw;
466720db4ffSRichard Cochran 	u32 tsauxc, tsim, tsauxc_mask, tsim_mask, trgttiml, trgttimh;
46700c65578SRichard Cochran 	unsigned long flags;
468720db4ffSRichard Cochran 	struct timespec ts;
469e357f0aaSAlexander Duyck 	int pin = -1;
470720db4ffSRichard Cochran 	s64 ns;
47100c65578SRichard Cochran 
47200c65578SRichard Cochran 	switch (rq->type) {
473720db4ffSRichard Cochran 	case PTP_CLK_REQ_EXTTS:
474720db4ffSRichard Cochran 		if (on) {
475720db4ffSRichard Cochran 			pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS,
476720db4ffSRichard Cochran 					   rq->extts.index);
477720db4ffSRichard Cochran 			if (pin < 0)
478720db4ffSRichard Cochran 				return -EBUSY;
479720db4ffSRichard Cochran 		}
480720db4ffSRichard Cochran 		if (rq->extts.index == 1) {
481720db4ffSRichard Cochran 			tsauxc_mask = TSAUXC_EN_TS1;
482720db4ffSRichard Cochran 			tsim_mask = TSINTR_AUTT1;
483720db4ffSRichard Cochran 		} else {
484720db4ffSRichard Cochran 			tsauxc_mask = TSAUXC_EN_TS0;
485720db4ffSRichard Cochran 			tsim_mask = TSINTR_AUTT0;
486720db4ffSRichard Cochran 		}
487720db4ffSRichard Cochran 		spin_lock_irqsave(&igb->tmreg_lock, flags);
488720db4ffSRichard Cochran 		tsauxc = rd32(E1000_TSAUXC);
489720db4ffSRichard Cochran 		tsim = rd32(E1000_TSIM);
490720db4ffSRichard Cochran 		if (on) {
491720db4ffSRichard Cochran 			igb_pin_extts(igb, rq->extts.index, pin);
492720db4ffSRichard Cochran 			tsauxc |= tsauxc_mask;
493720db4ffSRichard Cochran 			tsim |= tsim_mask;
494720db4ffSRichard Cochran 		} else {
495720db4ffSRichard Cochran 			tsauxc &= ~tsauxc_mask;
496720db4ffSRichard Cochran 			tsim &= ~tsim_mask;
497720db4ffSRichard Cochran 		}
498720db4ffSRichard Cochran 		wr32(E1000_TSAUXC, tsauxc);
499720db4ffSRichard Cochran 		wr32(E1000_TSIM, tsim);
500720db4ffSRichard Cochran 		spin_unlock_irqrestore(&igb->tmreg_lock, flags);
501720db4ffSRichard Cochran 		return 0;
502720db4ffSRichard Cochran 
503720db4ffSRichard Cochran 	case PTP_CLK_REQ_PEROUT:
504720db4ffSRichard Cochran 		if (on) {
505720db4ffSRichard Cochran 			pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT,
506720db4ffSRichard Cochran 					   rq->perout.index);
507720db4ffSRichard Cochran 			if (pin < 0)
508720db4ffSRichard Cochran 				return -EBUSY;
509720db4ffSRichard Cochran 		}
510720db4ffSRichard Cochran 		ts.tv_sec = rq->perout.period.sec;
511720db4ffSRichard Cochran 		ts.tv_nsec = rq->perout.period.nsec;
512720db4ffSRichard Cochran 		ns = timespec_to_ns(&ts);
513720db4ffSRichard Cochran 		ns = ns >> 1;
514720db4ffSRichard Cochran 		if (on && ns < 500000LL) {
515720db4ffSRichard Cochran 			/* 2k interrupts per second is an awful lot. */
516720db4ffSRichard Cochran 			return -EINVAL;
517720db4ffSRichard Cochran 		}
518720db4ffSRichard Cochran 		ts = ns_to_timespec(ns);
519720db4ffSRichard Cochran 		if (rq->perout.index == 1) {
520720db4ffSRichard Cochran 			tsauxc_mask = TSAUXC_EN_TT1;
521720db4ffSRichard Cochran 			tsim_mask = TSINTR_TT1;
522720db4ffSRichard Cochran 			trgttiml = E1000_TRGTTIML1;
523720db4ffSRichard Cochran 			trgttimh = E1000_TRGTTIMH1;
524720db4ffSRichard Cochran 		} else {
525720db4ffSRichard Cochran 			tsauxc_mask = TSAUXC_EN_TT0;
526720db4ffSRichard Cochran 			tsim_mask = TSINTR_TT0;
527720db4ffSRichard Cochran 			trgttiml = E1000_TRGTTIML0;
528720db4ffSRichard Cochran 			trgttimh = E1000_TRGTTIMH0;
529720db4ffSRichard Cochran 		}
530720db4ffSRichard Cochran 		spin_lock_irqsave(&igb->tmreg_lock, flags);
531720db4ffSRichard Cochran 		tsauxc = rd32(E1000_TSAUXC);
532720db4ffSRichard Cochran 		tsim = rd32(E1000_TSIM);
533720db4ffSRichard Cochran 		if (on) {
534720db4ffSRichard Cochran 			int i = rq->perout.index;
535720db4ffSRichard Cochran 
536720db4ffSRichard Cochran 			igb_pin_perout(igb, i, pin);
537720db4ffSRichard Cochran 			igb->perout[i].start.tv_sec = rq->perout.start.sec;
538720db4ffSRichard Cochran 			igb->perout[i].start.tv_nsec = rq->perout.start.nsec;
539720db4ffSRichard Cochran 			igb->perout[i].period.tv_sec = ts.tv_sec;
540720db4ffSRichard Cochran 			igb->perout[i].period.tv_nsec = ts.tv_nsec;
541720db4ffSRichard Cochran 			wr32(trgttiml, rq->perout.start.sec);
542720db4ffSRichard Cochran 			wr32(trgttimh, rq->perout.start.nsec);
543720db4ffSRichard Cochran 			tsauxc |= tsauxc_mask;
544720db4ffSRichard Cochran 			tsim |= tsim_mask;
545720db4ffSRichard Cochran 		} else {
546720db4ffSRichard Cochran 			tsauxc &= ~tsauxc_mask;
547720db4ffSRichard Cochran 			tsim &= ~tsim_mask;
548720db4ffSRichard Cochran 		}
549720db4ffSRichard Cochran 		wr32(E1000_TSAUXC, tsauxc);
550720db4ffSRichard Cochran 		wr32(E1000_TSIM, tsim);
551720db4ffSRichard Cochran 		spin_unlock_irqrestore(&igb->tmreg_lock, flags);
552720db4ffSRichard Cochran 		return 0;
553720db4ffSRichard Cochran 
55400c65578SRichard Cochran 	case PTP_CLK_REQ_PPS:
55500c65578SRichard Cochran 		spin_lock_irqsave(&igb->tmreg_lock, flags);
55600c65578SRichard Cochran 		tsim = rd32(E1000_TSIM);
55700c65578SRichard Cochran 		if (on)
55800c65578SRichard Cochran 			tsim |= TSINTR_SYS_WRAP;
55900c65578SRichard Cochran 		else
56000c65578SRichard Cochran 			tsim &= ~TSINTR_SYS_WRAP;
56100c65578SRichard Cochran 		wr32(E1000_TSIM, tsim);
56200c65578SRichard Cochran 		spin_unlock_irqrestore(&igb->tmreg_lock, flags);
56300c65578SRichard Cochran 		return 0;
56400c65578SRichard Cochran 	}
56500c65578SRichard Cochran 
56600c65578SRichard Cochran 	return -EOPNOTSUPP;
56700c65578SRichard Cochran }
56800c65578SRichard Cochran 
569102be52fSJacob Keller static int igb_ptp_feature_enable(struct ptp_clock_info *ptp,
570d339b133SRichard Cochran 				  struct ptp_clock_request *rq, int on)
571d339b133SRichard Cochran {
572d339b133SRichard Cochran 	return -EOPNOTSUPP;
573d339b133SRichard Cochran }
574d339b133SRichard Cochran 
575720db4ffSRichard Cochran static int igb_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
576720db4ffSRichard Cochran 			      enum ptp_pin_function func, unsigned int chan)
577720db4ffSRichard Cochran {
578720db4ffSRichard Cochran 	switch (func) {
579720db4ffSRichard Cochran 	case PTP_PF_NONE:
580720db4ffSRichard Cochran 	case PTP_PF_EXTTS:
581720db4ffSRichard Cochran 	case PTP_PF_PEROUT:
582720db4ffSRichard Cochran 		break;
583720db4ffSRichard Cochran 	case PTP_PF_PHYSYNC:
584720db4ffSRichard Cochran 		return -1;
585720db4ffSRichard Cochran 	}
586720db4ffSRichard Cochran 	return 0;
587720db4ffSRichard Cochran }
588720db4ffSRichard Cochran 
5891f6e8178SMatthew Vick /**
5901f6e8178SMatthew Vick  * igb_ptp_tx_work
5911f6e8178SMatthew Vick  * @work: pointer to work struct
5921f6e8178SMatthew Vick  *
5931f6e8178SMatthew Vick  * This work function polls the TSYNCTXCTL valid bit to determine when a
5941f6e8178SMatthew Vick  * timestamp has been taken for the current stored skb.
595b980ac18SJeff Kirsher  **/
596167f3f71SJeff Kirsher static void igb_ptp_tx_work(struct work_struct *work)
5971f6e8178SMatthew Vick {
5981f6e8178SMatthew Vick 	struct igb_adapter *adapter = container_of(work, struct igb_adapter,
5991f6e8178SMatthew Vick 						   ptp_tx_work);
6001f6e8178SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
6011f6e8178SMatthew Vick 	u32 tsynctxctl;
6021f6e8178SMatthew Vick 
6031f6e8178SMatthew Vick 	if (!adapter->ptp_tx_skb)
6041f6e8178SMatthew Vick 		return;
6051f6e8178SMatthew Vick 
606428f1f71SMatthew Vick 	if (time_is_before_jiffies(adapter->ptp_tx_start +
607428f1f71SMatthew Vick 				   IGB_PTP_TX_TIMEOUT)) {
608428f1f71SMatthew Vick 		dev_kfree_skb_any(adapter->ptp_tx_skb);
609428f1f71SMatthew Vick 		adapter->ptp_tx_skb = NULL;
610ed4420a3SJakub Kicinski 		clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
611428f1f71SMatthew Vick 		adapter->tx_hwtstamp_timeouts++;
612c5ffe7e1SJakub Kicinski 		dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
613428f1f71SMatthew Vick 		return;
614428f1f71SMatthew Vick 	}
615428f1f71SMatthew Vick 
6161f6e8178SMatthew Vick 	tsynctxctl = rd32(E1000_TSYNCTXCTL);
6171f6e8178SMatthew Vick 	if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
6181f6e8178SMatthew Vick 		igb_ptp_tx_hwtstamp(adapter);
6191f6e8178SMatthew Vick 	else
6201f6e8178SMatthew Vick 		/* reschedule to check later */
6211f6e8178SMatthew Vick 		schedule_work(&adapter->ptp_tx_work);
6221f6e8178SMatthew Vick }
6231f6e8178SMatthew Vick 
624a79f4f88SMatthew Vick static void igb_ptp_overflow_check(struct work_struct *work)
625d339b133SRichard Cochran {
626d339b133SRichard Cochran 	struct igb_adapter *igb =
627a79f4f88SMatthew Vick 		container_of(work, struct igb_adapter, ptp_overflow_work.work);
628d4c496feSRichard Cochran 	struct timespec64 ts;
629d339b133SRichard Cochran 
630d4c496feSRichard Cochran 	igb->ptp_caps.gettime64(&igb->ptp_caps, &ts);
631d339b133SRichard Cochran 
63232eaf120SDavid S. Miller 	pr_debug("igb overflow check at %lld.%09lu\n",
63332eaf120SDavid S. Miller 		 (long long) ts.tv_sec, ts.tv_nsec);
634d339b133SRichard Cochran 
635a79f4f88SMatthew Vick 	schedule_delayed_work(&igb->ptp_overflow_work,
636a79f4f88SMatthew Vick 			      IGB_SYSTIM_OVERFLOW_PERIOD);
637a79f4f88SMatthew Vick }
638a79f4f88SMatthew Vick 
639a79f4f88SMatthew Vick /**
640fc580751SMatthew Vick  * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
641fc580751SMatthew Vick  * @adapter: private network adapter structure
642fc580751SMatthew Vick  *
643fc580751SMatthew Vick  * This watchdog task is scheduled to detect error case where hardware has
644fc580751SMatthew Vick  * dropped an Rx packet that was timestamped when the ring is full. The
645fc580751SMatthew Vick  * particular error is rare but leaves the device in a state unable to timestamp
646fc580751SMatthew Vick  * any future packets.
647b980ac18SJeff Kirsher  **/
648fc580751SMatthew Vick void igb_ptp_rx_hang(struct igb_adapter *adapter)
649fc580751SMatthew Vick {
650fc580751SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
651fc580751SMatthew Vick 	u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
652fc580751SMatthew Vick 	unsigned long rx_event;
653fc580751SMatthew Vick 
654fc580751SMatthew Vick 	if (hw->mac.type != e1000_82576)
655fc580751SMatthew Vick 		return;
656fc580751SMatthew Vick 
657fc580751SMatthew Vick 	/* If we don't have a valid timestamp in the registers, just update the
658fc580751SMatthew Vick 	 * timeout counter and exit
659fc580751SMatthew Vick 	 */
660fc580751SMatthew Vick 	if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
661fc580751SMatthew Vick 		adapter->last_rx_ptp_check = jiffies;
662fc580751SMatthew Vick 		return;
663fc580751SMatthew Vick 	}
664fc580751SMatthew Vick 
665fc580751SMatthew Vick 	/* Determine the most recent watchdog or rx_timestamp event */
666fc580751SMatthew Vick 	rx_event = adapter->last_rx_ptp_check;
6675499a968SJakub Kicinski 	if (time_after(adapter->last_rx_timestamp, rx_event))
6685499a968SJakub Kicinski 		rx_event = adapter->last_rx_timestamp;
669fc580751SMatthew Vick 
670fc580751SMatthew Vick 	/* Only need to read the high RXSTMP register to clear the lock */
671fc580751SMatthew Vick 	if (time_is_before_jiffies(rx_event + 5 * HZ)) {
672fc580751SMatthew Vick 		rd32(E1000_RXSTMPH);
673fc580751SMatthew Vick 		adapter->last_rx_ptp_check = jiffies;
674fc580751SMatthew Vick 		adapter->rx_hwtstamp_cleared++;
675c5ffe7e1SJakub Kicinski 		dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang\n");
676fc580751SMatthew Vick 	}
677fc580751SMatthew Vick }
678fc580751SMatthew Vick 
679fc580751SMatthew Vick /**
680a79f4f88SMatthew Vick  * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
6811f6e8178SMatthew Vick  * @adapter: Board private structure.
682a79f4f88SMatthew Vick  *
683a79f4f88SMatthew Vick  * If we were asked to do hardware stamping and such a time stamp is
684a79f4f88SMatthew Vick  * available, then it must have been for this skb here because we only
685a79f4f88SMatthew Vick  * allow only one such packet into the queue.
686b980ac18SJeff Kirsher  **/
687167f3f71SJeff Kirsher static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
688a79f4f88SMatthew Vick {
689a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
690a79f4f88SMatthew Vick 	struct skb_shared_hwtstamps shhwtstamps;
691a79f4f88SMatthew Vick 	u64 regval;
692a79f4f88SMatthew Vick 
693a79f4f88SMatthew Vick 	regval = rd32(E1000_TXSTMPL);
694a79f4f88SMatthew Vick 	regval |= (u64)rd32(E1000_TXSTMPH) << 32;
695a79f4f88SMatthew Vick 
696a79f4f88SMatthew Vick 	igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
6971f6e8178SMatthew Vick 	skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
6981f6e8178SMatthew Vick 	dev_kfree_skb_any(adapter->ptp_tx_skb);
6991f6e8178SMatthew Vick 	adapter->ptp_tx_skb = NULL;
700ed4420a3SJakub Kicinski 	clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
701a79f4f88SMatthew Vick }
702a79f4f88SMatthew Vick 
703b534550aSAlexander Duyck /**
704b534550aSAlexander Duyck  * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
705b534550aSAlexander Duyck  * @q_vector: Pointer to interrupt specific structure
706b534550aSAlexander Duyck  * @va: Pointer to address containing Rx buffer
707b534550aSAlexander Duyck  * @skb: Buffer containing timestamp and packet
708b534550aSAlexander Duyck  *
709b534550aSAlexander Duyck  * This function is meant to retrieve a timestamp from the first buffer of an
710b534550aSAlexander Duyck  * incoming frame.  The value is stored in little endian format starting on
711b534550aSAlexander Duyck  * byte 8.
712b980ac18SJeff Kirsher  **/
713b534550aSAlexander Duyck void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
714b534550aSAlexander Duyck 			 unsigned char *va,
715b534550aSAlexander Duyck 			 struct sk_buff *skb)
716b534550aSAlexander Duyck {
717ac61d515SAlexander Duyck 	__le64 *regval = (__le64 *)va;
718b534550aSAlexander Duyck 
719b980ac18SJeff Kirsher 	/* The timestamp is recorded in little endian format.
720b534550aSAlexander Duyck 	 * DWORD: 0        1        2        3
721b534550aSAlexander Duyck 	 * Field: Reserved Reserved SYSTIML  SYSTIMH
722b534550aSAlexander Duyck 	 */
723b534550aSAlexander Duyck 	igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
724b534550aSAlexander Duyck 				   le64_to_cpu(regval[1]));
725b534550aSAlexander Duyck }
726b534550aSAlexander Duyck 
727b534550aSAlexander Duyck /**
728b534550aSAlexander Duyck  * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
729b534550aSAlexander Duyck  * @q_vector: Pointer to interrupt specific structure
730b534550aSAlexander Duyck  * @skb: Buffer containing timestamp and packet
731b534550aSAlexander Duyck  *
732b534550aSAlexander Duyck  * This function is meant to retrieve a timestamp from the internal registers
733b534550aSAlexander Duyck  * of the adapter and store it in the skb.
734b980ac18SJeff Kirsher  **/
735b534550aSAlexander Duyck void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
736a79f4f88SMatthew Vick 			 struct sk_buff *skb)
737a79f4f88SMatthew Vick {
738a79f4f88SMatthew Vick 	struct igb_adapter *adapter = q_vector->adapter;
739a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
740a79f4f88SMatthew Vick 	u64 regval;
741a79f4f88SMatthew Vick 
742b980ac18SJeff Kirsher 	/* If this bit is set, then the RX registers contain the time stamp. No
743a79f4f88SMatthew Vick 	 * other packet will be time stamped until we read these registers, so
744a79f4f88SMatthew Vick 	 * read the registers to make them available again. Because only one
745a79f4f88SMatthew Vick 	 * packet can be time stamped at a time, we know that the register
746a79f4f88SMatthew Vick 	 * values must belong to this one here and therefore we don't need to
747a79f4f88SMatthew Vick 	 * compare any of the additional attributes stored for it.
748a79f4f88SMatthew Vick 	 *
749a79f4f88SMatthew Vick 	 * If nothing went wrong, then it should have a shared tx_flags that we
750a79f4f88SMatthew Vick 	 * can turn into a skb_shared_hwtstamps.
751a79f4f88SMatthew Vick 	 */
752a79f4f88SMatthew Vick 	if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
753a79f4f88SMatthew Vick 		return;
754a79f4f88SMatthew Vick 
755a79f4f88SMatthew Vick 	regval = rd32(E1000_RXSTMPL);
756a79f4f88SMatthew Vick 	regval |= (u64)rd32(E1000_RXSTMPH) << 32;
757a79f4f88SMatthew Vick 
758a79f4f88SMatthew Vick 	igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
7595499a968SJakub Kicinski 
7605499a968SJakub Kicinski 	/* Update the last_rx_timestamp timer in order to enable watchdog check
7615499a968SJakub Kicinski 	 * for error case of latched timestamp on a dropped packet.
7625499a968SJakub Kicinski 	 */
7635499a968SJakub Kicinski 	adapter->last_rx_timestamp = jiffies;
764a79f4f88SMatthew Vick }
765a79f4f88SMatthew Vick 
766a79f4f88SMatthew Vick /**
7676ab5f7b2SJacob Keller  * igb_ptp_get_ts_config - get hardware time stamping config
768a79f4f88SMatthew Vick  * @netdev:
769a79f4f88SMatthew Vick  * @ifreq:
7706ab5f7b2SJacob Keller  *
7716ab5f7b2SJacob Keller  * Get the hwtstamp_config settings to return to the user. Rather than attempt
7726ab5f7b2SJacob Keller  * to deconstruct the settings from the registers, just return a shadow copy
7736ab5f7b2SJacob Keller  * of the last known settings.
7746ab5f7b2SJacob Keller  **/
7756ab5f7b2SJacob Keller int igb_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
7766ab5f7b2SJacob Keller {
7776ab5f7b2SJacob Keller 	struct igb_adapter *adapter = netdev_priv(netdev);
7786ab5f7b2SJacob Keller 	struct hwtstamp_config *config = &adapter->tstamp_config;
7796ab5f7b2SJacob Keller 
7806ab5f7b2SJacob Keller 	return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
7816ab5f7b2SJacob Keller 		-EFAULT : 0;
7826ab5f7b2SJacob Keller }
7839f62ecf4SJacob Keller 
7846ab5f7b2SJacob Keller /**
7859f62ecf4SJacob Keller  * igb_ptp_set_timestamp_mode - setup hardware for timestamping
7869f62ecf4SJacob Keller  * @adapter: networking device structure
7879f62ecf4SJacob Keller  * @config: hwtstamp configuration
788a79f4f88SMatthew Vick  *
789a79f4f88SMatthew Vick  * Outgoing time stamping can be enabled and disabled. Play nice and
790a79f4f88SMatthew Vick  * disable it when requested, although it shouldn't case any overhead
791a79f4f88SMatthew Vick  * when no packet needs it. At most one packet in the queue may be
792a79f4f88SMatthew Vick  * marked for time stamping, otherwise it would be impossible to tell
793a79f4f88SMatthew Vick  * for sure to which packet the hardware time stamp belongs.
794a79f4f88SMatthew Vick  *
795a79f4f88SMatthew Vick  * Incoming time stamping has to be configured via the hardware
796a79f4f88SMatthew Vick  * filters. Not all combinations are supported, in particular event
797a79f4f88SMatthew Vick  * type has to be specified. Matching the kind of event packet is
798a79f4f88SMatthew Vick  * not supported, with the exception of "all V2 events regardless of
799a79f4f88SMatthew Vick  * level 2 or 4".
8009f62ecf4SJacob Keller  */
8019f62ecf4SJacob Keller static int igb_ptp_set_timestamp_mode(struct igb_adapter *adapter,
8029f62ecf4SJacob Keller 				      struct hwtstamp_config *config)
803a79f4f88SMatthew Vick {
804a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
805a79f4f88SMatthew Vick 	u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
806a79f4f88SMatthew Vick 	u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
807a79f4f88SMatthew Vick 	u32 tsync_rx_cfg = 0;
808a79f4f88SMatthew Vick 	bool is_l4 = false;
809a79f4f88SMatthew Vick 	bool is_l2 = false;
810a79f4f88SMatthew Vick 	u32 regval;
811a79f4f88SMatthew Vick 
812a79f4f88SMatthew Vick 	/* reserved for future extensions */
8136ab5f7b2SJacob Keller 	if (config->flags)
814a79f4f88SMatthew Vick 		return -EINVAL;
815a79f4f88SMatthew Vick 
8166ab5f7b2SJacob Keller 	switch (config->tx_type) {
817a79f4f88SMatthew Vick 	case HWTSTAMP_TX_OFF:
818a79f4f88SMatthew Vick 		tsync_tx_ctl = 0;
819a79f4f88SMatthew Vick 	case HWTSTAMP_TX_ON:
820a79f4f88SMatthew Vick 		break;
821a79f4f88SMatthew Vick 	default:
822a79f4f88SMatthew Vick 		return -ERANGE;
823a79f4f88SMatthew Vick 	}
824a79f4f88SMatthew Vick 
8256ab5f7b2SJacob Keller 	switch (config->rx_filter) {
826a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_NONE:
827a79f4f88SMatthew Vick 		tsync_rx_ctl = 0;
828a79f4f88SMatthew Vick 		break;
829a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
830a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
831a79f4f88SMatthew Vick 		tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
832a79f4f88SMatthew Vick 		is_l4 = true;
833a79f4f88SMatthew Vick 		break;
834a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
835a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
836a79f4f88SMatthew Vick 		tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
837a79f4f88SMatthew Vick 		is_l4 = true;
838a79f4f88SMatthew Vick 		break;
8393e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
8403e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
8413e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
8423e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
843a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
844a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
8453e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
846a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
847a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
848a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
8496ab5f7b2SJacob Keller 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
850a79f4f88SMatthew Vick 		is_l2 = true;
851a79f4f88SMatthew Vick 		is_l4 = true;
852a79f4f88SMatthew Vick 		break;
8533e961a06SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
8543e961a06SMatthew Vick 	case HWTSTAMP_FILTER_ALL:
8553e961a06SMatthew Vick 		/* 82576 cannot timestamp all packets, which it needs to do to
8563e961a06SMatthew Vick 		 * support both V1 Sync and Delay_Req messages
8573e961a06SMatthew Vick 		 */
8583e961a06SMatthew Vick 		if (hw->mac.type != e1000_82576) {
8593e961a06SMatthew Vick 			tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
8606ab5f7b2SJacob Keller 			config->rx_filter = HWTSTAMP_FILTER_ALL;
8613e961a06SMatthew Vick 			break;
8623e961a06SMatthew Vick 		}
8633e961a06SMatthew Vick 		/* fall through */
864a79f4f88SMatthew Vick 	default:
8656ab5f7b2SJacob Keller 		config->rx_filter = HWTSTAMP_FILTER_NONE;
866a79f4f88SMatthew Vick 		return -ERANGE;
867a79f4f88SMatthew Vick 	}
868a79f4f88SMatthew Vick 
869a79f4f88SMatthew Vick 	if (hw->mac.type == e1000_82575) {
870a79f4f88SMatthew Vick 		if (tsync_rx_ctl | tsync_tx_ctl)
871a79f4f88SMatthew Vick 			return -EINVAL;
872a79f4f88SMatthew Vick 		return 0;
873a79f4f88SMatthew Vick 	}
874a79f4f88SMatthew Vick 
875b980ac18SJeff Kirsher 	/* Per-packet timestamping only works if all packets are
876a79f4f88SMatthew Vick 	 * timestamped, so enable timestamping in all packets as
877b980ac18SJeff Kirsher 	 * long as one Rx filter was configured.
878a79f4f88SMatthew Vick 	 */
879a79f4f88SMatthew Vick 	if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
880a79f4f88SMatthew Vick 		tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
881a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
8826ab5f7b2SJacob Keller 		config->rx_filter = HWTSTAMP_FILTER_ALL;
8833e961a06SMatthew Vick 		is_l2 = true;
8843e961a06SMatthew Vick 		is_l4 = true;
885e57b8bdbSMatthew Vick 
886e57b8bdbSMatthew Vick 		if ((hw->mac.type == e1000_i210) ||
887e57b8bdbSMatthew Vick 		    (hw->mac.type == e1000_i211)) {
888e57b8bdbSMatthew Vick 			regval = rd32(E1000_RXPBS);
889e57b8bdbSMatthew Vick 			regval |= E1000_RXPBS_CFG_TS_EN;
890e57b8bdbSMatthew Vick 			wr32(E1000_RXPBS, regval);
891e57b8bdbSMatthew Vick 		}
892a79f4f88SMatthew Vick 	}
893a79f4f88SMatthew Vick 
894a79f4f88SMatthew Vick 	/* enable/disable TX */
895a79f4f88SMatthew Vick 	regval = rd32(E1000_TSYNCTXCTL);
896a79f4f88SMatthew Vick 	regval &= ~E1000_TSYNCTXCTL_ENABLED;
897a79f4f88SMatthew Vick 	regval |= tsync_tx_ctl;
898a79f4f88SMatthew Vick 	wr32(E1000_TSYNCTXCTL, regval);
899a79f4f88SMatthew Vick 
900a79f4f88SMatthew Vick 	/* enable/disable RX */
901a79f4f88SMatthew Vick 	regval = rd32(E1000_TSYNCRXCTL);
902a79f4f88SMatthew Vick 	regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
903a79f4f88SMatthew Vick 	regval |= tsync_rx_ctl;
904a79f4f88SMatthew Vick 	wr32(E1000_TSYNCRXCTL, regval);
905a79f4f88SMatthew Vick 
906a79f4f88SMatthew Vick 	/* define which PTP packets are time stamped */
907a79f4f88SMatthew Vick 	wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
908a79f4f88SMatthew Vick 
909a79f4f88SMatthew Vick 	/* define ethertype filter for timestamped packets */
910a79f4f88SMatthew Vick 	if (is_l2)
911a79f4f88SMatthew Vick 		wr32(E1000_ETQF(3),
912a79f4f88SMatthew Vick 		     (E1000_ETQF_FILTER_ENABLE | /* enable filter */
913a79f4f88SMatthew Vick 		      E1000_ETQF_1588 | /* enable timestamping */
914a79f4f88SMatthew Vick 		      ETH_P_1588));     /* 1588 eth protocol type */
915a79f4f88SMatthew Vick 	else
916a79f4f88SMatthew Vick 		wr32(E1000_ETQF(3), 0);
917a79f4f88SMatthew Vick 
918a79f4f88SMatthew Vick 	/* L4 Queue Filter[3]: filter by destination port and protocol */
919a79f4f88SMatthew Vick 	if (is_l4) {
920a79f4f88SMatthew Vick 		u32 ftqf = (IPPROTO_UDP /* UDP */
921a79f4f88SMatthew Vick 			| E1000_FTQF_VF_BP /* VF not compared */
922a79f4f88SMatthew Vick 			| E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
923a79f4f88SMatthew Vick 			| E1000_FTQF_MASK); /* mask all inputs */
924a79f4f88SMatthew Vick 		ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
925a79f4f88SMatthew Vick 
926ba59814bSMatthew Vick 		wr32(E1000_IMIR(3), htons(PTP_EV_PORT));
927a79f4f88SMatthew Vick 		wr32(E1000_IMIREXT(3),
928a79f4f88SMatthew Vick 		     (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
929a79f4f88SMatthew Vick 		if (hw->mac.type == e1000_82576) {
930a79f4f88SMatthew Vick 			/* enable source port check */
931ba59814bSMatthew Vick 			wr32(E1000_SPQF(3), htons(PTP_EV_PORT));
932a79f4f88SMatthew Vick 			ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
933a79f4f88SMatthew Vick 		}
934a79f4f88SMatthew Vick 		wr32(E1000_FTQF(3), ftqf);
935a79f4f88SMatthew Vick 	} else {
936a79f4f88SMatthew Vick 		wr32(E1000_FTQF(3), E1000_FTQF_MASK);
937a79f4f88SMatthew Vick 	}
938a79f4f88SMatthew Vick 	wrfl();
939a79f4f88SMatthew Vick 
940a79f4f88SMatthew Vick 	/* clear TX/RX time stamp registers, just to be sure */
941e57b8bdbSMatthew Vick 	regval = rd32(E1000_TXSTMPL);
942a79f4f88SMatthew Vick 	regval = rd32(E1000_TXSTMPH);
943e57b8bdbSMatthew Vick 	regval = rd32(E1000_RXSTMPL);
944a79f4f88SMatthew Vick 	regval = rd32(E1000_RXSTMPH);
945a79f4f88SMatthew Vick 
9469f62ecf4SJacob Keller 	return 0;
9479f62ecf4SJacob Keller }
9489f62ecf4SJacob Keller 
9499f62ecf4SJacob Keller /**
9509f62ecf4SJacob Keller  * igb_ptp_set_ts_config - set hardware time stamping config
9519f62ecf4SJacob Keller  * @netdev:
9529f62ecf4SJacob Keller  * @ifreq:
9539f62ecf4SJacob Keller  *
9549f62ecf4SJacob Keller  **/
9559f62ecf4SJacob Keller int igb_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
9569f62ecf4SJacob Keller {
9579f62ecf4SJacob Keller 	struct igb_adapter *adapter = netdev_priv(netdev);
9589f62ecf4SJacob Keller 	struct hwtstamp_config config;
9599f62ecf4SJacob Keller 	int err;
9609f62ecf4SJacob Keller 
9619f62ecf4SJacob Keller 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
9629f62ecf4SJacob Keller 		return -EFAULT;
9639f62ecf4SJacob Keller 
9649f62ecf4SJacob Keller 	err = igb_ptp_set_timestamp_mode(adapter, &config);
9659f62ecf4SJacob Keller 	if (err)
9669f62ecf4SJacob Keller 		return err;
9679f62ecf4SJacob Keller 
9689f62ecf4SJacob Keller 	/* save these settings for future reference */
9699f62ecf4SJacob Keller 	memcpy(&adapter->tstamp_config, &config,
9709f62ecf4SJacob Keller 	       sizeof(adapter->tstamp_config));
9719f62ecf4SJacob Keller 
9729f62ecf4SJacob Keller 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
973a79f4f88SMatthew Vick 		-EFAULT : 0;
974d339b133SRichard Cochran }
975d339b133SRichard Cochran 
976d339b133SRichard Cochran void igb_ptp_init(struct igb_adapter *adapter)
977d339b133SRichard Cochran {
978d339b133SRichard Cochran 	struct e1000_hw *hw = &adapter->hw;
979201987e3SMatthew Vick 	struct net_device *netdev = adapter->netdev;
980720db4ffSRichard Cochran 	int i;
981d339b133SRichard Cochran 
982d339b133SRichard Cochran 	switch (hw->mac.type) {
983d339b133SRichard Cochran 	case e1000_82576:
984201987e3SMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
985a79f4f88SMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
98675517d92SJiri Benc 		adapter->ptp_caps.max_adj = 999999881;
987a79f4f88SMatthew Vick 		adapter->ptp_caps.n_ext_ts = 0;
988a79f4f88SMatthew Vick 		adapter->ptp_caps.pps = 0;
989a79f4f88SMatthew Vick 		adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
990e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
991d4c496feSRichard Cochran 		adapter->ptp_caps.gettime64 = igb_ptp_gettime_82576;
992d4c496feSRichard Cochran 		adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
993102be52fSJacob Keller 		adapter->ptp_caps.enable = igb_ptp_feature_enable;
994a79f4f88SMatthew Vick 		adapter->cc.read = igb_ptp_read_82576;
995b57c8940SRichard Cochran 		adapter->cc.mask = CYCLECOUNTER_MASK(64);
996d339b133SRichard Cochran 		adapter->cc.mult = 1;
997d339b133SRichard Cochran 		adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
998d339b133SRichard Cochran 		/* Dial the nominal frequency. */
999d339b133SRichard Cochran 		wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
1000d339b133SRichard Cochran 		break;
1001e57b8bdbSMatthew Vick 	case e1000_82580:
1002ceb5f13bSCarolyn Wyborny 	case e1000_i354:
1003e57b8bdbSMatthew Vick 	case e1000_i350:
1004e57b8bdbSMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
1005e57b8bdbSMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
1006e57b8bdbSMatthew Vick 		adapter->ptp_caps.max_adj = 62499999;
1007e57b8bdbSMatthew Vick 		adapter->ptp_caps.n_ext_ts = 0;
1008e57b8bdbSMatthew Vick 		adapter->ptp_caps.pps = 0;
1009e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
1010e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
1011d4c496feSRichard Cochran 		adapter->ptp_caps.gettime64 = igb_ptp_gettime_82576;
1012d4c496feSRichard Cochran 		adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
1013102be52fSJacob Keller 		adapter->ptp_caps.enable = igb_ptp_feature_enable;
1014e57b8bdbSMatthew Vick 		adapter->cc.read = igb_ptp_read_82580;
1015b57c8940SRichard Cochran 		adapter->cc.mask = CYCLECOUNTER_MASK(IGB_NBITS_82580);
1016e57b8bdbSMatthew Vick 		adapter->cc.mult = 1;
1017e57b8bdbSMatthew Vick 		adapter->cc.shift = 0;
1018e57b8bdbSMatthew Vick 		/* Enable the timer functions by clearing bit 31. */
1019e57b8bdbSMatthew Vick 		wr32(E1000_TSAUXC, 0x0);
1020e57b8bdbSMatthew Vick 		break;
1021e57b8bdbSMatthew Vick 	case e1000_i210:
1022e57b8bdbSMatthew Vick 	case e1000_i211:
1023720db4ffSRichard Cochran 		for (i = 0; i < IGB_N_SDP; i++) {
1024720db4ffSRichard Cochran 			struct ptp_pin_desc *ppd = &adapter->sdp_config[i];
1025720db4ffSRichard Cochran 
1026720db4ffSRichard Cochran 			snprintf(ppd->name, sizeof(ppd->name), "SDP%d", i);
1027720db4ffSRichard Cochran 			ppd->index = i;
1028720db4ffSRichard Cochran 			ppd->func = PTP_PF_NONE;
1029720db4ffSRichard Cochran 		}
1030e57b8bdbSMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
1031e57b8bdbSMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
1032e57b8bdbSMatthew Vick 		adapter->ptp_caps.max_adj = 62499999;
1033720db4ffSRichard Cochran 		adapter->ptp_caps.n_ext_ts = IGB_N_EXTTS;
1034720db4ffSRichard Cochran 		adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
1035720db4ffSRichard Cochran 		adapter->ptp_caps.n_pins = IGB_N_SDP;
103600c65578SRichard Cochran 		adapter->ptp_caps.pps = 1;
1037720db4ffSRichard Cochran 		adapter->ptp_caps.pin_config = adapter->sdp_config;
1038e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
1039e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
1040d4c496feSRichard Cochran 		adapter->ptp_caps.gettime64 = igb_ptp_gettime_i210;
1041d4c496feSRichard Cochran 		adapter->ptp_caps.settime64 = igb_ptp_settime_i210;
104200c65578SRichard Cochran 		adapter->ptp_caps.enable = igb_ptp_feature_enable_i210;
1043720db4ffSRichard Cochran 		adapter->ptp_caps.verify = igb_ptp_verify_pin;
1044e57b8bdbSMatthew Vick 		/* Enable the timer functions by clearing bit 31. */
1045e57b8bdbSMatthew Vick 		wr32(E1000_TSAUXC, 0x0);
1046e57b8bdbSMatthew Vick 		break;
1047d339b133SRichard Cochran 	default:
1048d339b133SRichard Cochran 		adapter->ptp_clock = NULL;
1049d339b133SRichard Cochran 		return;
1050d339b133SRichard Cochran 	}
1051d339b133SRichard Cochran 
1052d339b133SRichard Cochran 	wrfl();
1053d339b133SRichard Cochran 
1054e57b8bdbSMatthew Vick 	spin_lock_init(&adapter->tmreg_lock);
1055e57b8bdbSMatthew Vick 	INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
1056e57b8bdbSMatthew Vick 
1057e57b8bdbSMatthew Vick 	/* Initialize the clock and overflow work for devices that need it. */
1058e57b8bdbSMatthew Vick 	if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
1059d4c496feSRichard Cochran 		struct timespec64 ts = ktime_to_timespec64(ktime_get_real());
1060e57b8bdbSMatthew Vick 
1061e57b8bdbSMatthew Vick 		igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
1062e57b8bdbSMatthew Vick 	} else {
1063d339b133SRichard Cochran 		timecounter_init(&adapter->tc, &adapter->cc,
1064d339b133SRichard Cochran 				 ktime_to_ns(ktime_get_real()));
1065d339b133SRichard Cochran 
1066e57b8bdbSMatthew Vick 		INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
1067e57b8bdbSMatthew Vick 				  igb_ptp_overflow_check);
10681f6e8178SMatthew Vick 
1069a79f4f88SMatthew Vick 		schedule_delayed_work(&adapter->ptp_overflow_work,
1070a79f4f88SMatthew Vick 				      IGB_SYSTIM_OVERFLOW_PERIOD);
1071e57b8bdbSMatthew Vick 	}
1072d339b133SRichard Cochran 
10731f6e8178SMatthew Vick 	/* Initialize the time sync interrupts for devices that support it. */
10741f6e8178SMatthew Vick 	if (hw->mac.type >= e1000_82580) {
10750c375ac1SCarolyn Wyborny 		wr32(E1000_TSIM, TSYNC_INTERRUPTS);
10761f6e8178SMatthew Vick 		wr32(E1000_IMS, E1000_IMS_TS);
10771f6e8178SMatthew Vick 	}
10781f6e8178SMatthew Vick 
10799f62ecf4SJacob Keller 	adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
10809f62ecf4SJacob Keller 	adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
10819f62ecf4SJacob Keller 
10821ef76158SRichard Cochran 	adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
10831ef76158SRichard Cochran 						&adapter->pdev->dev);
1084d339b133SRichard Cochran 	if (IS_ERR(adapter->ptp_clock)) {
1085d339b133SRichard Cochran 		adapter->ptp_clock = NULL;
1086d339b133SRichard Cochran 		dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
10871f6e8178SMatthew Vick 	} else {
1088d339b133SRichard Cochran 		dev_info(&adapter->pdev->dev, "added PHC on %s\n",
1089d339b133SRichard Cochran 			 adapter->netdev->name);
10901f6e8178SMatthew Vick 		adapter->flags |= IGB_FLAG_PTP;
10911f6e8178SMatthew Vick 	}
1092d339b133SRichard Cochran }
1093d339b133SRichard Cochran 
1094a79f4f88SMatthew Vick /**
1095a79f4f88SMatthew Vick  * igb_ptp_stop - Disable PTP device and stop the overflow check.
1096a79f4f88SMatthew Vick  * @adapter: Board private structure.
1097a79f4f88SMatthew Vick  *
1098a79f4f88SMatthew Vick  * This function stops the PTP support and cancels the delayed work.
1099a79f4f88SMatthew Vick  **/
1100a79f4f88SMatthew Vick void igb_ptp_stop(struct igb_adapter *adapter)
1101d339b133SRichard Cochran {
1102d3eef8c8SCarolyn Wyborny 	switch (adapter->hw.mac.type) {
1103d3eef8c8SCarolyn Wyborny 	case e1000_82576:
11041f6e8178SMatthew Vick 	case e1000_82580:
1105ceb5f13bSCarolyn Wyborny 	case e1000_i354:
11061f6e8178SMatthew Vick 	case e1000_i350:
1107a79f4f88SMatthew Vick 		cancel_delayed_work_sync(&adapter->ptp_overflow_work);
1108d3eef8c8SCarolyn Wyborny 		break;
11091f6e8178SMatthew Vick 	case e1000_i210:
11101f6e8178SMatthew Vick 	case e1000_i211:
11111f6e8178SMatthew Vick 		/* No delayed work to cancel. */
11121f6e8178SMatthew Vick 		break;
1113d3eef8c8SCarolyn Wyborny 	default:
1114d3eef8c8SCarolyn Wyborny 		return;
1115d3eef8c8SCarolyn Wyborny 	}
1116d339b133SRichard Cochran 
11171f6e8178SMatthew Vick 	cancel_work_sync(&adapter->ptp_tx_work);
1118badc26ddSMatthew Vick 	if (adapter->ptp_tx_skb) {
1119badc26ddSMatthew Vick 		dev_kfree_skb_any(adapter->ptp_tx_skb);
1120badc26ddSMatthew Vick 		adapter->ptp_tx_skb = NULL;
1121ed4420a3SJakub Kicinski 		clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
1122badc26ddSMatthew Vick 	}
11231f6e8178SMatthew Vick 
1124d339b133SRichard Cochran 	if (adapter->ptp_clock) {
1125d339b133SRichard Cochran 		ptp_clock_unregister(adapter->ptp_clock);
1126d339b133SRichard Cochran 		dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
1127d339b133SRichard Cochran 			 adapter->netdev->name);
11281f6e8178SMatthew Vick 		adapter->flags &= ~IGB_FLAG_PTP;
1129d339b133SRichard Cochran 	}
1130d339b133SRichard Cochran }
11311f6e8178SMatthew Vick 
11321f6e8178SMatthew Vick /**
11331f6e8178SMatthew Vick  * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
11341f6e8178SMatthew Vick  * @adapter: Board private structure.
11351f6e8178SMatthew Vick  *
11361f6e8178SMatthew Vick  * This function handles the reset work required to re-enable the PTP device.
11371f6e8178SMatthew Vick  **/
11381f6e8178SMatthew Vick void igb_ptp_reset(struct igb_adapter *adapter)
11391f6e8178SMatthew Vick {
11401f6e8178SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
11418298c1ecSRichard Cochran 	unsigned long flags;
11421f6e8178SMatthew Vick 
11431f6e8178SMatthew Vick 	if (!(adapter->flags & IGB_FLAG_PTP))
11441f6e8178SMatthew Vick 		return;
11451f6e8178SMatthew Vick 
11466ab5f7b2SJacob Keller 	/* reset the tstamp_config */
11479f62ecf4SJacob Keller 	igb_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
11486ab5f7b2SJacob Keller 
11498298c1ecSRichard Cochran 	spin_lock_irqsave(&adapter->tmreg_lock, flags);
11508298c1ecSRichard Cochran 
11511f6e8178SMatthew Vick 	switch (adapter->hw.mac.type) {
11521f6e8178SMatthew Vick 	case e1000_82576:
11531f6e8178SMatthew Vick 		/* Dial the nominal frequency. */
11541f6e8178SMatthew Vick 		wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
11551f6e8178SMatthew Vick 		break;
11561f6e8178SMatthew Vick 	case e1000_82580:
1157ceb5f13bSCarolyn Wyborny 	case e1000_i354:
11581f6e8178SMatthew Vick 	case e1000_i350:
11591f6e8178SMatthew Vick 	case e1000_i210:
11601f6e8178SMatthew Vick 	case e1000_i211:
11611f6e8178SMatthew Vick 		wr32(E1000_TSAUXC, 0x0);
1162720db4ffSRichard Cochran 		wr32(E1000_TSSDP, 0x0);
11630c375ac1SCarolyn Wyborny 		wr32(E1000_TSIM, TSYNC_INTERRUPTS);
11641f6e8178SMatthew Vick 		wr32(E1000_IMS, E1000_IMS_TS);
11651f6e8178SMatthew Vick 		break;
11661f6e8178SMatthew Vick 	default:
11671f6e8178SMatthew Vick 		/* No work to do. */
11688298c1ecSRichard Cochran 		goto out;
11691f6e8178SMatthew Vick 	}
11701f6e8178SMatthew Vick 
1171e57b8bdbSMatthew Vick 	/* Re-initialize the timer. */
1172e57b8bdbSMatthew Vick 	if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
1173d4c496feSRichard Cochran 		struct timespec64 ts = ktime_to_timespec64(ktime_get_real());
1174e57b8bdbSMatthew Vick 
11758298c1ecSRichard Cochran 		igb_ptp_write_i210(adapter, &ts);
1176e57b8bdbSMatthew Vick 	} else {
11771f6e8178SMatthew Vick 		timecounter_init(&adapter->tc, &adapter->cc,
11781f6e8178SMatthew Vick 				 ktime_to_ns(ktime_get_real()));
11791f6e8178SMatthew Vick 	}
11808298c1ecSRichard Cochran out:
11818298c1ecSRichard Cochran 	spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
1182e57b8bdbSMatthew Vick }
1183