1d339b133SRichard Cochran /*
2d339b133SRichard Cochran  * PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580
3d339b133SRichard Cochran  *
4d339b133SRichard Cochran  * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
5d339b133SRichard Cochran  *
6d339b133SRichard Cochran  * This program is free software; you can redistribute it and/or modify
7d339b133SRichard Cochran  * it under the terms of the GNU General Public License as published by
8d339b133SRichard Cochran  * the Free Software Foundation; either version 2 of the License, or
9d339b133SRichard Cochran  * (at your option) any later version.
10d339b133SRichard Cochran  *
11d339b133SRichard Cochran  * This program is distributed in the hope that it will be useful,
12d339b133SRichard Cochran  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13d339b133SRichard Cochran  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14d339b133SRichard Cochran  * GNU General Public License for more details.
15d339b133SRichard Cochran  *
16d339b133SRichard Cochran  * You should have received a copy of the GNU General Public License along
17d339b133SRichard Cochran  * with this program; if not, write to the Free Software Foundation, Inc.,
18d339b133SRichard Cochran  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19d339b133SRichard Cochran  */
20d339b133SRichard Cochran #include <linux/module.h>
21d339b133SRichard Cochran #include <linux/device.h>
22d339b133SRichard Cochran #include <linux/pci.h>
23d339b133SRichard Cochran 
24d339b133SRichard Cochran #include "igb.h"
25d339b133SRichard Cochran 
26d339b133SRichard Cochran #define INCVALUE_MASK		0x7fffffff
27d339b133SRichard Cochran #define ISGN			0x80000000
28d339b133SRichard Cochran 
29d339b133SRichard Cochran /*
307ebae817SRichard Cochran  * The 82580 timesync updates the system timer every 8ns by 8ns,
317ebae817SRichard Cochran  * and this update value cannot be reprogrammed.
327ebae817SRichard Cochran  *
33d339b133SRichard Cochran  * Neither the 82576 nor the 82580 offer registers wide enough to hold
34d339b133SRichard Cochran  * nanoseconds time values for very long. For the 82580, SYSTIM always
35d339b133SRichard Cochran  * counts nanoseconds, but the upper 24 bits are not availible. The
36d339b133SRichard Cochran  * frequency is adjusted by changing the 32 bit fractional nanoseconds
37d339b133SRichard Cochran  * register, TIMINCA.
38d339b133SRichard Cochran  *
39d339b133SRichard Cochran  * For the 82576, the SYSTIM register time unit is affect by the
40d339b133SRichard Cochran  * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
41d339b133SRichard Cochran  * field are needed to provide the nominal 16 nanosecond period,
42d339b133SRichard Cochran  * leaving 19 bits for fractional nanoseconds.
43d339b133SRichard Cochran  *
447ebae817SRichard Cochran  * We scale the NIC clock cycle by a large factor so that relatively
457ebae817SRichard Cochran  * small clock corrections can be added or subtracted at each clock
467ebae817SRichard Cochran  * tick. The drawbacks of a large factor are a) that the clock
477ebae817SRichard Cochran  * register overflows more quickly (not such a big deal) and b) that
487ebae817SRichard Cochran  * the increment per tick has to fit into 24 bits.  As a result we
497ebae817SRichard Cochran  * need to use a shift of 19 so we can fit a value of 16 into the
507ebae817SRichard Cochran  * TIMINCA register.
517ebae817SRichard Cochran  *
52d339b133SRichard Cochran  *
53d339b133SRichard Cochran  *             SYSTIMH            SYSTIML
54d339b133SRichard Cochran  *        +--------------+   +---+---+------+
55d339b133SRichard Cochran  *  82576 |      32      |   | 8 | 5 |  19  |
56d339b133SRichard Cochran  *        +--------------+   +---+---+------+
57d339b133SRichard Cochran  *         \________ 45 bits _______/  fract
58d339b133SRichard Cochran  *
59d339b133SRichard Cochran  *        +----------+---+   +--------------+
60d339b133SRichard Cochran  *  82580 |    24    | 8 |   |      32      |
61d339b133SRichard Cochran  *        +----------+---+   +--------------+
62d339b133SRichard Cochran  *          reserved  \______ 40 bits _____/
63d339b133SRichard Cochran  *
64d339b133SRichard Cochran  *
65d339b133SRichard Cochran  * The 45 bit 82576 SYSTIM overflows every
66d339b133SRichard Cochran  *   2^45 * 10^-9 / 3600 = 9.77 hours.
67d339b133SRichard Cochran  *
68d339b133SRichard Cochran  * The 40 bit 82580 SYSTIM overflows every
69d339b133SRichard Cochran  *   2^40 * 10^-9 /  60  = 18.3 minutes.
70d339b133SRichard Cochran  */
71d339b133SRichard Cochran 
72a79f4f88SMatthew Vick #define IGB_SYSTIM_OVERFLOW_PERIOD	(HZ * 60 * 9)
73d339b133SRichard Cochran #define INCPERIOD_82576			(1 << E1000_TIMINCA_16NS_SHIFT)
74d339b133SRichard Cochran #define INCVALUE_82576_MASK		((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
75d339b133SRichard Cochran #define INCVALUE_82576			(16 << IGB_82576_TSYNC_SHIFT)
76d339b133SRichard Cochran #define IGB_NBITS_82580			40
77d339b133SRichard Cochran 
78d339b133SRichard Cochran /*
79d339b133SRichard Cochran  * SYSTIM read access for the 82576
80d339b133SRichard Cochran  */
81d339b133SRichard Cochran 
82a79f4f88SMatthew Vick static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc)
83d339b133SRichard Cochran {
84d339b133SRichard Cochran 	struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
85d339b133SRichard Cochran 	struct e1000_hw *hw = &igb->hw;
86a79f4f88SMatthew Vick 	u64 val;
87a79f4f88SMatthew Vick 	u32 lo, hi;
88d339b133SRichard Cochran 
89d339b133SRichard Cochran 	lo = rd32(E1000_SYSTIML);
90d339b133SRichard Cochran 	hi = rd32(E1000_SYSTIMH);
91d339b133SRichard Cochran 
92d339b133SRichard Cochran 	val = ((u64) hi) << 32;
93d339b133SRichard Cochran 	val |= lo;
94d339b133SRichard Cochran 
95d339b133SRichard Cochran 	return val;
96d339b133SRichard Cochran }
97d339b133SRichard Cochran 
98d339b133SRichard Cochran /*
99d339b133SRichard Cochran  * SYSTIM read access for the 82580
100d339b133SRichard Cochran  */
101d339b133SRichard Cochran 
102a79f4f88SMatthew Vick static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc)
103d339b133SRichard Cochran {
104d339b133SRichard Cochran 	struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
105d339b133SRichard Cochran 	struct e1000_hw *hw = &igb->hw;
106a79f4f88SMatthew Vick 	u64 val;
107a79f4f88SMatthew Vick 	u32 lo, hi, jk;
108d339b133SRichard Cochran 
1097ebae817SRichard Cochran 	/*
1107ebae817SRichard Cochran 	 * The timestamp latches on lowest register read. For the 82580
1117ebae817SRichard Cochran 	 * the lowest register is SYSTIMR instead of SYSTIML.  However we only
1127ebae817SRichard Cochran 	 * need to provide nanosecond resolution, so we just ignore it.
1137ebae817SRichard Cochran 	 */
114d339b133SRichard Cochran 	jk = rd32(E1000_SYSTIMR);
115d339b133SRichard Cochran 	lo = rd32(E1000_SYSTIML);
116d339b133SRichard Cochran 	hi = rd32(E1000_SYSTIMH);
117d339b133SRichard Cochran 
118d339b133SRichard Cochran 	val = ((u64) hi) << 32;
119d339b133SRichard Cochran 	val |= lo;
120d339b133SRichard Cochran 
121d339b133SRichard Cochran 	return val;
122d339b133SRichard Cochran }
123d339b133SRichard Cochran 
124e57b8bdbSMatthew Vick /*
125e57b8bdbSMatthew Vick  * SYSTIM read access for I210/I211
126e57b8bdbSMatthew Vick  */
127e57b8bdbSMatthew Vick 
128e57b8bdbSMatthew Vick static void igb_ptp_read_i210(struct igb_adapter *adapter, struct timespec *ts)
129e57b8bdbSMatthew Vick {
130e57b8bdbSMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
131e57b8bdbSMatthew Vick 	u32 sec, nsec, jk;
132e57b8bdbSMatthew Vick 
133e57b8bdbSMatthew Vick 	/*
134e57b8bdbSMatthew Vick 	 * The timestamp latches on lowest register read. For I210/I211, the
135e57b8bdbSMatthew Vick 	 * lowest register is SYSTIMR. Since we only need to provide nanosecond
136e57b8bdbSMatthew Vick 	 * resolution, we can ignore it.
137e57b8bdbSMatthew Vick 	 */
138e57b8bdbSMatthew Vick 	jk = rd32(E1000_SYSTIMR);
139e57b8bdbSMatthew Vick 	nsec = rd32(E1000_SYSTIML);
140e57b8bdbSMatthew Vick 	sec = rd32(E1000_SYSTIMH);
141e57b8bdbSMatthew Vick 
142e57b8bdbSMatthew Vick 	ts->tv_sec = sec;
143e57b8bdbSMatthew Vick 	ts->tv_nsec = nsec;
144e57b8bdbSMatthew Vick }
145e57b8bdbSMatthew Vick 
146e57b8bdbSMatthew Vick static void igb_ptp_write_i210(struct igb_adapter *adapter,
147e57b8bdbSMatthew Vick 			       const struct timespec *ts)
148e57b8bdbSMatthew Vick {
149e57b8bdbSMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
150e57b8bdbSMatthew Vick 
151e57b8bdbSMatthew Vick 	/*
152e57b8bdbSMatthew Vick 	 * Writing the SYSTIMR register is not necessary as it only provides
153e57b8bdbSMatthew Vick 	 * sub-nanosecond resolution.
154e57b8bdbSMatthew Vick 	 */
155e57b8bdbSMatthew Vick 	wr32(E1000_SYSTIML, ts->tv_nsec);
156e57b8bdbSMatthew Vick 	wr32(E1000_SYSTIMH, ts->tv_sec);
157e57b8bdbSMatthew Vick }
158e57b8bdbSMatthew Vick 
159a79f4f88SMatthew Vick /**
160a79f4f88SMatthew Vick  * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
161a79f4f88SMatthew Vick  * @adapter: board private structure
162a79f4f88SMatthew Vick  * @hwtstamps: timestamp structure to update
163a79f4f88SMatthew Vick  * @systim: unsigned 64bit system time value.
164a79f4f88SMatthew Vick  *
165a79f4f88SMatthew Vick  * We need to convert the system time value stored in the RX/TXSTMP registers
166a79f4f88SMatthew Vick  * into a hwtstamp which can be used by the upper level timestamping functions.
167a79f4f88SMatthew Vick  *
168a79f4f88SMatthew Vick  * The 'tmreg_lock' spinlock is used to protect the consistency of the
169a79f4f88SMatthew Vick  * system time value. This is needed because reading the 64 bit time
170a79f4f88SMatthew Vick  * value involves reading two (or three) 32 bit registers. The first
171a79f4f88SMatthew Vick  * read latches the value. Ditto for writing.
172a79f4f88SMatthew Vick  *
173a79f4f88SMatthew Vick  * In addition, here have extended the system time with an overflow
174a79f4f88SMatthew Vick  * counter in software.
175a79f4f88SMatthew Vick  **/
176a79f4f88SMatthew Vick static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
177a79f4f88SMatthew Vick 				       struct skb_shared_hwtstamps *hwtstamps,
178a79f4f88SMatthew Vick 				       u64 systim)
179a79f4f88SMatthew Vick {
180a79f4f88SMatthew Vick 	unsigned long flags;
181a79f4f88SMatthew Vick 	u64 ns;
182a79f4f88SMatthew Vick 
183a79f4f88SMatthew Vick 	switch (adapter->hw.mac.type) {
184a79f4f88SMatthew Vick 	case e1000_82576:
185e57b8bdbSMatthew Vick 	case e1000_82580:
186e57b8bdbSMatthew Vick 	case e1000_i350:
187a79f4f88SMatthew Vick 		spin_lock_irqsave(&adapter->tmreg_lock, flags);
188a79f4f88SMatthew Vick 
189a79f4f88SMatthew Vick 		ns = timecounter_cyc2time(&adapter->tc, systim);
190a79f4f88SMatthew Vick 
191a79f4f88SMatthew Vick 		spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
192a79f4f88SMatthew Vick 
193a79f4f88SMatthew Vick 		memset(hwtstamps, 0, sizeof(*hwtstamps));
194a79f4f88SMatthew Vick 		hwtstamps->hwtstamp = ns_to_ktime(ns);
195e57b8bdbSMatthew Vick 		break;
196e57b8bdbSMatthew Vick 	case e1000_i210:
197e57b8bdbSMatthew Vick 	case e1000_i211:
198e57b8bdbSMatthew Vick 		memset(hwtstamps, 0, sizeof(*hwtstamps));
199e57b8bdbSMatthew Vick 		/* Upper 32 bits contain s, lower 32 bits contain ns. */
200e57b8bdbSMatthew Vick 		hwtstamps->hwtstamp = ktime_set(systim >> 32,
201e57b8bdbSMatthew Vick 						systim & 0xFFFFFFFF);
202e57b8bdbSMatthew Vick 		break;
203e57b8bdbSMatthew Vick 	default:
204e57b8bdbSMatthew Vick 		break;
205e57b8bdbSMatthew Vick 	}
206a79f4f88SMatthew Vick }
207a79f4f88SMatthew Vick 
208d339b133SRichard Cochran /*
209d339b133SRichard Cochran  * PTP clock operations
210d339b133SRichard Cochran  */
211d339b133SRichard Cochran 
212a79f4f88SMatthew Vick static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
213d339b133SRichard Cochran {
214a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
215a79f4f88SMatthew Vick 					       ptp_caps);
216a79f4f88SMatthew Vick 	struct e1000_hw *hw = &igb->hw;
217a79f4f88SMatthew Vick 	int neg_adj = 0;
218d339b133SRichard Cochran 	u64 rate;
219d339b133SRichard Cochran 	u32 incvalue;
220d339b133SRichard Cochran 
221d339b133SRichard Cochran 	if (ppb < 0) {
222d339b133SRichard Cochran 		neg_adj = 1;
223d339b133SRichard Cochran 		ppb = -ppb;
224d339b133SRichard Cochran 	}
225d339b133SRichard Cochran 	rate = ppb;
226d339b133SRichard Cochran 	rate <<= 14;
227d339b133SRichard Cochran 	rate = div_u64(rate, 1953125);
228d339b133SRichard Cochran 
229d339b133SRichard Cochran 	incvalue = 16 << IGB_82576_TSYNC_SHIFT;
230d339b133SRichard Cochran 
231d339b133SRichard Cochran 	if (neg_adj)
232d339b133SRichard Cochran 		incvalue -= rate;
233d339b133SRichard Cochran 	else
234d339b133SRichard Cochran 		incvalue += rate;
235d339b133SRichard Cochran 
236d339b133SRichard Cochran 	wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
237d339b133SRichard Cochran 
238d339b133SRichard Cochran 	return 0;
239d339b133SRichard Cochran }
240d339b133SRichard Cochran 
241a79f4f88SMatthew Vick static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb)
242d339b133SRichard Cochran {
243a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
244a79f4f88SMatthew Vick 					       ptp_caps);
245a79f4f88SMatthew Vick 	struct e1000_hw *hw = &igb->hw;
246a79f4f88SMatthew Vick 	int neg_adj = 0;
247d339b133SRichard Cochran 	u64 rate;
248d339b133SRichard Cochran 	u32 inca;
249d339b133SRichard Cochran 
250d339b133SRichard Cochran 	if (ppb < 0) {
251d339b133SRichard Cochran 		neg_adj = 1;
252d339b133SRichard Cochran 		ppb = -ppb;
253d339b133SRichard Cochran 	}
254d339b133SRichard Cochran 	rate = ppb;
255d339b133SRichard Cochran 	rate <<= 26;
256d339b133SRichard Cochran 	rate = div_u64(rate, 1953125);
257d339b133SRichard Cochran 
258d339b133SRichard Cochran 	inca = rate & INCVALUE_MASK;
259d339b133SRichard Cochran 	if (neg_adj)
260d339b133SRichard Cochran 		inca |= ISGN;
261d339b133SRichard Cochran 
262d339b133SRichard Cochran 	wr32(E1000_TIMINCA, inca);
263d339b133SRichard Cochran 
264d339b133SRichard Cochran 	return 0;
265d339b133SRichard Cochran }
266d339b133SRichard Cochran 
267e57b8bdbSMatthew Vick static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
268d339b133SRichard Cochran {
269a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
270a79f4f88SMatthew Vick 					       ptp_caps);
271d339b133SRichard Cochran 	unsigned long flags;
272a79f4f88SMatthew Vick 	s64 now;
273d339b133SRichard Cochran 
274d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
275d339b133SRichard Cochran 
276d339b133SRichard Cochran 	now = timecounter_read(&igb->tc);
277d339b133SRichard Cochran 	now += delta;
278d339b133SRichard Cochran 	timecounter_init(&igb->tc, &igb->cc, now);
279d339b133SRichard Cochran 
280d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
281d339b133SRichard Cochran 
282d339b133SRichard Cochran 	return 0;
283d339b133SRichard Cochran }
284d339b133SRichard Cochran 
285e57b8bdbSMatthew Vick static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
286e57b8bdbSMatthew Vick {
287e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
288e57b8bdbSMatthew Vick 					       ptp_caps);
289e57b8bdbSMatthew Vick 	unsigned long flags;
290e57b8bdbSMatthew Vick 	struct timespec now, then = ns_to_timespec(delta);
291e57b8bdbSMatthew Vick 
292e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
293e57b8bdbSMatthew Vick 
294e57b8bdbSMatthew Vick 	igb_ptp_read_i210(igb, &now);
295e57b8bdbSMatthew Vick 	now = timespec_add(now, then);
296e57b8bdbSMatthew Vick 	igb_ptp_write_i210(igb, (const struct timespec *)&now);
297e57b8bdbSMatthew Vick 
298e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
299e57b8bdbSMatthew Vick 
300e57b8bdbSMatthew Vick 	return 0;
301e57b8bdbSMatthew Vick }
302e57b8bdbSMatthew Vick 
303e57b8bdbSMatthew Vick static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
304e57b8bdbSMatthew Vick 				 struct timespec *ts)
305d339b133SRichard Cochran {
306a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
307a79f4f88SMatthew Vick 					       ptp_caps);
308a79f4f88SMatthew Vick 	unsigned long flags;
309d339b133SRichard Cochran 	u64 ns;
310d339b133SRichard Cochran 	u32 remainder;
311d339b133SRichard Cochran 
312d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
313d339b133SRichard Cochran 
314d339b133SRichard Cochran 	ns = timecounter_read(&igb->tc);
315d339b133SRichard Cochran 
316d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
317d339b133SRichard Cochran 
318d339b133SRichard Cochran 	ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
319d339b133SRichard Cochran 	ts->tv_nsec = remainder;
320d339b133SRichard Cochran 
321d339b133SRichard Cochran 	return 0;
322d339b133SRichard Cochran }
323d339b133SRichard Cochran 
324e57b8bdbSMatthew Vick static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
325e57b8bdbSMatthew Vick 				struct timespec *ts)
326e57b8bdbSMatthew Vick {
327e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
328e57b8bdbSMatthew Vick 					       ptp_caps);
329e57b8bdbSMatthew Vick 	unsigned long flags;
330e57b8bdbSMatthew Vick 
331e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
332e57b8bdbSMatthew Vick 
333e57b8bdbSMatthew Vick 	igb_ptp_read_i210(igb, ts);
334e57b8bdbSMatthew Vick 
335e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
336e57b8bdbSMatthew Vick 
337e57b8bdbSMatthew Vick 	return 0;
338e57b8bdbSMatthew Vick }
339e57b8bdbSMatthew Vick 
340e57b8bdbSMatthew Vick static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
341a79f4f88SMatthew Vick 				 const struct timespec *ts)
342d339b133SRichard Cochran {
343a79f4f88SMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
344a79f4f88SMatthew Vick 					       ptp_caps);
345d339b133SRichard Cochran 	unsigned long flags;
346a79f4f88SMatthew Vick 	u64 ns;
347d339b133SRichard Cochran 
348d339b133SRichard Cochran 	ns = ts->tv_sec * 1000000000ULL;
349d339b133SRichard Cochran 	ns += ts->tv_nsec;
350d339b133SRichard Cochran 
351d339b133SRichard Cochran 	spin_lock_irqsave(&igb->tmreg_lock, flags);
352d339b133SRichard Cochran 
353d339b133SRichard Cochran 	timecounter_init(&igb->tc, &igb->cc, ns);
354d339b133SRichard Cochran 
355d339b133SRichard Cochran 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
356d339b133SRichard Cochran 
357d339b133SRichard Cochran 	return 0;
358d339b133SRichard Cochran }
359d339b133SRichard Cochran 
360e57b8bdbSMatthew Vick static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
361e57b8bdbSMatthew Vick 				const struct timespec *ts)
362e57b8bdbSMatthew Vick {
363e57b8bdbSMatthew Vick 	struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
364e57b8bdbSMatthew Vick 					       ptp_caps);
365e57b8bdbSMatthew Vick 	unsigned long flags;
366e57b8bdbSMatthew Vick 
367e57b8bdbSMatthew Vick 	spin_lock_irqsave(&igb->tmreg_lock, flags);
368e57b8bdbSMatthew Vick 
369e57b8bdbSMatthew Vick 	igb_ptp_write_i210(igb, ts);
370e57b8bdbSMatthew Vick 
371e57b8bdbSMatthew Vick 	spin_unlock_irqrestore(&igb->tmreg_lock, flags);
372e57b8bdbSMatthew Vick 
373e57b8bdbSMatthew Vick 	return 0;
374e57b8bdbSMatthew Vick }
375e57b8bdbSMatthew Vick 
376a79f4f88SMatthew Vick static int igb_ptp_enable(struct ptp_clock_info *ptp,
377d339b133SRichard Cochran 			  struct ptp_clock_request *rq, int on)
378d339b133SRichard Cochran {
379d339b133SRichard Cochran 	return -EOPNOTSUPP;
380d339b133SRichard Cochran }
381d339b133SRichard Cochran 
3821f6e8178SMatthew Vick /**
3831f6e8178SMatthew Vick  * igb_ptp_tx_work
3841f6e8178SMatthew Vick  * @work: pointer to work struct
3851f6e8178SMatthew Vick  *
3861f6e8178SMatthew Vick  * This work function polls the TSYNCTXCTL valid bit to determine when a
3871f6e8178SMatthew Vick  * timestamp has been taken for the current stored skb.
3881f6e8178SMatthew Vick  */
3891f6e8178SMatthew Vick void igb_ptp_tx_work(struct work_struct *work)
3901f6e8178SMatthew Vick {
3911f6e8178SMatthew Vick 	struct igb_adapter *adapter = container_of(work, struct igb_adapter,
3921f6e8178SMatthew Vick 						   ptp_tx_work);
3931f6e8178SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
3941f6e8178SMatthew Vick 	u32 tsynctxctl;
3951f6e8178SMatthew Vick 
3961f6e8178SMatthew Vick 	if (!adapter->ptp_tx_skb)
3971f6e8178SMatthew Vick 		return;
3981f6e8178SMatthew Vick 
3991f6e8178SMatthew Vick 	tsynctxctl = rd32(E1000_TSYNCTXCTL);
4001f6e8178SMatthew Vick 	if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
4011f6e8178SMatthew Vick 		igb_ptp_tx_hwtstamp(adapter);
4021f6e8178SMatthew Vick 	else
4031f6e8178SMatthew Vick 		/* reschedule to check later */
4041f6e8178SMatthew Vick 		schedule_work(&adapter->ptp_tx_work);
4051f6e8178SMatthew Vick }
4061f6e8178SMatthew Vick 
407a79f4f88SMatthew Vick static void igb_ptp_overflow_check(struct work_struct *work)
408d339b133SRichard Cochran {
409d339b133SRichard Cochran 	struct igb_adapter *igb =
410a79f4f88SMatthew Vick 		container_of(work, struct igb_adapter, ptp_overflow_work.work);
411a79f4f88SMatthew Vick 	struct timespec ts;
412d339b133SRichard Cochran 
413e57b8bdbSMatthew Vick 	igb->ptp_caps.gettime(&igb->ptp_caps, &ts);
414d339b133SRichard Cochran 
415d339b133SRichard Cochran 	pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
416d339b133SRichard Cochran 
417a79f4f88SMatthew Vick 	schedule_delayed_work(&igb->ptp_overflow_work,
418a79f4f88SMatthew Vick 			      IGB_SYSTIM_OVERFLOW_PERIOD);
419a79f4f88SMatthew Vick }
420a79f4f88SMatthew Vick 
421a79f4f88SMatthew Vick /**
422a79f4f88SMatthew Vick  * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
4231f6e8178SMatthew Vick  * @adapter: Board private structure.
424a79f4f88SMatthew Vick  *
425a79f4f88SMatthew Vick  * If we were asked to do hardware stamping and such a time stamp is
426a79f4f88SMatthew Vick  * available, then it must have been for this skb here because we only
427a79f4f88SMatthew Vick  * allow only one such packet into the queue.
428a79f4f88SMatthew Vick  */
4291f6e8178SMatthew Vick void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
430a79f4f88SMatthew Vick {
431a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
432a79f4f88SMatthew Vick 	struct skb_shared_hwtstamps shhwtstamps;
433a79f4f88SMatthew Vick 	u64 regval;
434a79f4f88SMatthew Vick 
435a79f4f88SMatthew Vick 	regval = rd32(E1000_TXSTMPL);
436a79f4f88SMatthew Vick 	regval |= (u64)rd32(E1000_TXSTMPH) << 32;
437a79f4f88SMatthew Vick 
438a79f4f88SMatthew Vick 	igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
4391f6e8178SMatthew Vick 	skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
4401f6e8178SMatthew Vick 	dev_kfree_skb_any(adapter->ptp_tx_skb);
4411f6e8178SMatthew Vick 	adapter->ptp_tx_skb = NULL;
442a79f4f88SMatthew Vick }
443a79f4f88SMatthew Vick 
444b534550aSAlexander Duyck /**
445b534550aSAlexander Duyck  * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
446b534550aSAlexander Duyck  * @q_vector: Pointer to interrupt specific structure
447b534550aSAlexander Duyck  * @va: Pointer to address containing Rx buffer
448b534550aSAlexander Duyck  * @skb: Buffer containing timestamp and packet
449b534550aSAlexander Duyck  *
450b534550aSAlexander Duyck  * This function is meant to retrieve a timestamp from the first buffer of an
451b534550aSAlexander Duyck  * incoming frame.  The value is stored in little endian format starting on
452b534550aSAlexander Duyck  * byte 8.
453b534550aSAlexander Duyck  */
454b534550aSAlexander Duyck void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
455b534550aSAlexander Duyck 			 unsigned char *va,
456b534550aSAlexander Duyck 			 struct sk_buff *skb)
457b534550aSAlexander Duyck {
458ac61d515SAlexander Duyck 	__le64 *regval = (__le64 *)va;
459b534550aSAlexander Duyck 
460b534550aSAlexander Duyck 	/*
461b534550aSAlexander Duyck 	 * The timestamp is recorded in little endian format.
462b534550aSAlexander Duyck 	 * DWORD: 0        1        2        3
463b534550aSAlexander Duyck 	 * Field: Reserved Reserved SYSTIML  SYSTIMH
464b534550aSAlexander Duyck 	 */
465b534550aSAlexander Duyck 	igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
466b534550aSAlexander Duyck 				   le64_to_cpu(regval[1]));
467b534550aSAlexander Duyck }
468b534550aSAlexander Duyck 
469b534550aSAlexander Duyck /**
470b534550aSAlexander Duyck  * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
471b534550aSAlexander Duyck  * @q_vector: Pointer to interrupt specific structure
472b534550aSAlexander Duyck  * @skb: Buffer containing timestamp and packet
473b534550aSAlexander Duyck  *
474b534550aSAlexander Duyck  * This function is meant to retrieve a timestamp from the internal registers
475b534550aSAlexander Duyck  * of the adapter and store it in the skb.
476b534550aSAlexander Duyck  */
477b534550aSAlexander Duyck void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
478a79f4f88SMatthew Vick 			 struct sk_buff *skb)
479a79f4f88SMatthew Vick {
480a79f4f88SMatthew Vick 	struct igb_adapter *adapter = q_vector->adapter;
481a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
482a79f4f88SMatthew Vick 	u64 regval;
483a79f4f88SMatthew Vick 
484a79f4f88SMatthew Vick 	/*
485a79f4f88SMatthew Vick 	 * If this bit is set, then the RX registers contain the time stamp. No
486a79f4f88SMatthew Vick 	 * other packet will be time stamped until we read these registers, so
487a79f4f88SMatthew Vick 	 * read the registers to make them available again. Because only one
488a79f4f88SMatthew Vick 	 * packet can be time stamped at a time, we know that the register
489a79f4f88SMatthew Vick 	 * values must belong to this one here and therefore we don't need to
490a79f4f88SMatthew Vick 	 * compare any of the additional attributes stored for it.
491a79f4f88SMatthew Vick 	 *
492a79f4f88SMatthew Vick 	 * If nothing went wrong, then it should have a shared tx_flags that we
493a79f4f88SMatthew Vick 	 * can turn into a skb_shared_hwtstamps.
494a79f4f88SMatthew Vick 	 */
495a79f4f88SMatthew Vick 	if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
496a79f4f88SMatthew Vick 		return;
497a79f4f88SMatthew Vick 
498a79f4f88SMatthew Vick 	regval = rd32(E1000_RXSTMPL);
499a79f4f88SMatthew Vick 	regval |= (u64)rd32(E1000_RXSTMPH) << 32;
500a79f4f88SMatthew Vick 
501a79f4f88SMatthew Vick 	igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
502a79f4f88SMatthew Vick }
503a79f4f88SMatthew Vick 
504a79f4f88SMatthew Vick /**
505a79f4f88SMatthew Vick  * igb_ptp_hwtstamp_ioctl - control hardware time stamping
506a79f4f88SMatthew Vick  * @netdev:
507a79f4f88SMatthew Vick  * @ifreq:
508a79f4f88SMatthew Vick  * @cmd:
509a79f4f88SMatthew Vick  *
510a79f4f88SMatthew Vick  * Outgoing time stamping can be enabled and disabled. Play nice and
511a79f4f88SMatthew Vick  * disable it when requested, although it shouldn't case any overhead
512a79f4f88SMatthew Vick  * when no packet needs it. At most one packet in the queue may be
513a79f4f88SMatthew Vick  * marked for time stamping, otherwise it would be impossible to tell
514a79f4f88SMatthew Vick  * for sure to which packet the hardware time stamp belongs.
515a79f4f88SMatthew Vick  *
516a79f4f88SMatthew Vick  * Incoming time stamping has to be configured via the hardware
517a79f4f88SMatthew Vick  * filters. Not all combinations are supported, in particular event
518a79f4f88SMatthew Vick  * type has to be specified. Matching the kind of event packet is
519a79f4f88SMatthew Vick  * not supported, with the exception of "all V2 events regardless of
520a79f4f88SMatthew Vick  * level 2 or 4".
521a79f4f88SMatthew Vick  *
522a79f4f88SMatthew Vick  **/
523a79f4f88SMatthew Vick int igb_ptp_hwtstamp_ioctl(struct net_device *netdev,
524a79f4f88SMatthew Vick 			   struct ifreq *ifr, int cmd)
525a79f4f88SMatthew Vick {
526a79f4f88SMatthew Vick 	struct igb_adapter *adapter = netdev_priv(netdev);
527a79f4f88SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
528a79f4f88SMatthew Vick 	struct hwtstamp_config config;
529a79f4f88SMatthew Vick 	u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
530a79f4f88SMatthew Vick 	u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
531a79f4f88SMatthew Vick 	u32 tsync_rx_cfg = 0;
532a79f4f88SMatthew Vick 	bool is_l4 = false;
533a79f4f88SMatthew Vick 	bool is_l2 = false;
534a79f4f88SMatthew Vick 	u32 regval;
535a79f4f88SMatthew Vick 
536a79f4f88SMatthew Vick 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
537a79f4f88SMatthew Vick 		return -EFAULT;
538a79f4f88SMatthew Vick 
539a79f4f88SMatthew Vick 	/* reserved for future extensions */
540a79f4f88SMatthew Vick 	if (config.flags)
541a79f4f88SMatthew Vick 		return -EINVAL;
542a79f4f88SMatthew Vick 
543a79f4f88SMatthew Vick 	switch (config.tx_type) {
544a79f4f88SMatthew Vick 	case HWTSTAMP_TX_OFF:
545a79f4f88SMatthew Vick 		tsync_tx_ctl = 0;
546a79f4f88SMatthew Vick 	case HWTSTAMP_TX_ON:
547a79f4f88SMatthew Vick 		break;
548a79f4f88SMatthew Vick 	default:
549a79f4f88SMatthew Vick 		return -ERANGE;
550a79f4f88SMatthew Vick 	}
551a79f4f88SMatthew Vick 
552a79f4f88SMatthew Vick 	switch (config.rx_filter) {
553a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_NONE:
554a79f4f88SMatthew Vick 		tsync_rx_ctl = 0;
555a79f4f88SMatthew Vick 		break;
556a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
557a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
558a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
559a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_ALL:
560a79f4f88SMatthew Vick 		/*
561a79f4f88SMatthew Vick 		 * register TSYNCRXCFG must be set, therefore it is not
562a79f4f88SMatthew Vick 		 * possible to time stamp both Sync and Delay_Req messages
563a79f4f88SMatthew Vick 		 * => fall back to time stamping all packets
564a79f4f88SMatthew Vick 		 */
565a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
566a79f4f88SMatthew Vick 		config.rx_filter = HWTSTAMP_FILTER_ALL;
567a79f4f88SMatthew Vick 		break;
568a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
569a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
570a79f4f88SMatthew Vick 		tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
571a79f4f88SMatthew Vick 		is_l4 = true;
572a79f4f88SMatthew Vick 		break;
573a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
574a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
575a79f4f88SMatthew Vick 		tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
576a79f4f88SMatthew Vick 		is_l4 = true;
577a79f4f88SMatthew Vick 		break;
578a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
579a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
580a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
581a79f4f88SMatthew Vick 		tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_SYNC_MESSAGE;
582a79f4f88SMatthew Vick 		is_l2 = true;
583a79f4f88SMatthew Vick 		is_l4 = true;
584a79f4f88SMatthew Vick 		config.rx_filter = HWTSTAMP_FILTER_SOME;
585a79f4f88SMatthew Vick 		break;
586a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
587a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
588a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
589a79f4f88SMatthew Vick 		tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_DELAY_REQ_MESSAGE;
590a79f4f88SMatthew Vick 		is_l2 = true;
591a79f4f88SMatthew Vick 		is_l4 = true;
592a79f4f88SMatthew Vick 		config.rx_filter = HWTSTAMP_FILTER_SOME;
593a79f4f88SMatthew Vick 		break;
594a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
595a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
596a79f4f88SMatthew Vick 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
597a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
598a79f4f88SMatthew Vick 		config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
599a79f4f88SMatthew Vick 		is_l2 = true;
600a79f4f88SMatthew Vick 		is_l4 = true;
601a79f4f88SMatthew Vick 		break;
602a79f4f88SMatthew Vick 	default:
603a79f4f88SMatthew Vick 		return -ERANGE;
604a79f4f88SMatthew Vick 	}
605a79f4f88SMatthew Vick 
606a79f4f88SMatthew Vick 	if (hw->mac.type == e1000_82575) {
607a79f4f88SMatthew Vick 		if (tsync_rx_ctl | tsync_tx_ctl)
608a79f4f88SMatthew Vick 			return -EINVAL;
609a79f4f88SMatthew Vick 		return 0;
610a79f4f88SMatthew Vick 	}
611a79f4f88SMatthew Vick 
612a79f4f88SMatthew Vick 	/*
613a79f4f88SMatthew Vick 	 * Per-packet timestamping only works if all packets are
614a79f4f88SMatthew Vick 	 * timestamped, so enable timestamping in all packets as
615a79f4f88SMatthew Vick 	 * long as one rx filter was configured.
616a79f4f88SMatthew Vick 	 */
617a79f4f88SMatthew Vick 	if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
618a79f4f88SMatthew Vick 		tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
619a79f4f88SMatthew Vick 		tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
620e57b8bdbSMatthew Vick 
621e57b8bdbSMatthew Vick 		if ((hw->mac.type == e1000_i210) ||
622e57b8bdbSMatthew Vick 		    (hw->mac.type == e1000_i211)) {
623e57b8bdbSMatthew Vick 			regval = rd32(E1000_RXPBS);
624e57b8bdbSMatthew Vick 			regval |= E1000_RXPBS_CFG_TS_EN;
625e57b8bdbSMatthew Vick 			wr32(E1000_RXPBS, regval);
626e57b8bdbSMatthew Vick 		}
627a79f4f88SMatthew Vick 	}
628a79f4f88SMatthew Vick 
629a79f4f88SMatthew Vick 	/* enable/disable TX */
630a79f4f88SMatthew Vick 	regval = rd32(E1000_TSYNCTXCTL);
631a79f4f88SMatthew Vick 	regval &= ~E1000_TSYNCTXCTL_ENABLED;
632a79f4f88SMatthew Vick 	regval |= tsync_tx_ctl;
633a79f4f88SMatthew Vick 	wr32(E1000_TSYNCTXCTL, regval);
634a79f4f88SMatthew Vick 
635a79f4f88SMatthew Vick 	/* enable/disable RX */
636a79f4f88SMatthew Vick 	regval = rd32(E1000_TSYNCRXCTL);
637a79f4f88SMatthew Vick 	regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
638a79f4f88SMatthew Vick 	regval |= tsync_rx_ctl;
639a79f4f88SMatthew Vick 	wr32(E1000_TSYNCRXCTL, regval);
640a79f4f88SMatthew Vick 
641a79f4f88SMatthew Vick 	/* define which PTP packets are time stamped */
642a79f4f88SMatthew Vick 	wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
643a79f4f88SMatthew Vick 
644a79f4f88SMatthew Vick 	/* define ethertype filter for timestamped packets */
645a79f4f88SMatthew Vick 	if (is_l2)
646a79f4f88SMatthew Vick 		wr32(E1000_ETQF(3),
647a79f4f88SMatthew Vick 		     (E1000_ETQF_FILTER_ENABLE | /* enable filter */
648a79f4f88SMatthew Vick 		      E1000_ETQF_1588 | /* enable timestamping */
649a79f4f88SMatthew Vick 		      ETH_P_1588));     /* 1588 eth protocol type */
650a79f4f88SMatthew Vick 	else
651a79f4f88SMatthew Vick 		wr32(E1000_ETQF(3), 0);
652a79f4f88SMatthew Vick 
653a79f4f88SMatthew Vick #define PTP_PORT 319
654a79f4f88SMatthew Vick 	/* L4 Queue Filter[3]: filter by destination port and protocol */
655a79f4f88SMatthew Vick 	if (is_l4) {
656a79f4f88SMatthew Vick 		u32 ftqf = (IPPROTO_UDP /* UDP */
657a79f4f88SMatthew Vick 			| E1000_FTQF_VF_BP /* VF not compared */
658a79f4f88SMatthew Vick 			| E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
659a79f4f88SMatthew Vick 			| E1000_FTQF_MASK); /* mask all inputs */
660a79f4f88SMatthew Vick 		ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
661a79f4f88SMatthew Vick 
662a79f4f88SMatthew Vick 		wr32(E1000_IMIR(3), htons(PTP_PORT));
663a79f4f88SMatthew Vick 		wr32(E1000_IMIREXT(3),
664a79f4f88SMatthew Vick 		     (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
665a79f4f88SMatthew Vick 		if (hw->mac.type == e1000_82576) {
666a79f4f88SMatthew Vick 			/* enable source port check */
667a79f4f88SMatthew Vick 			wr32(E1000_SPQF(3), htons(PTP_PORT));
668a79f4f88SMatthew Vick 			ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
669a79f4f88SMatthew Vick 		}
670a79f4f88SMatthew Vick 		wr32(E1000_FTQF(3), ftqf);
671a79f4f88SMatthew Vick 	} else {
672a79f4f88SMatthew Vick 		wr32(E1000_FTQF(3), E1000_FTQF_MASK);
673a79f4f88SMatthew Vick 	}
674a79f4f88SMatthew Vick 	wrfl();
675a79f4f88SMatthew Vick 
676a79f4f88SMatthew Vick 	/* clear TX/RX time stamp registers, just to be sure */
677e57b8bdbSMatthew Vick 	regval = rd32(E1000_TXSTMPL);
678a79f4f88SMatthew Vick 	regval = rd32(E1000_TXSTMPH);
679e57b8bdbSMatthew Vick 	regval = rd32(E1000_RXSTMPL);
680a79f4f88SMatthew Vick 	regval = rd32(E1000_RXSTMPH);
681a79f4f88SMatthew Vick 
682a79f4f88SMatthew Vick 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
683a79f4f88SMatthew Vick 		-EFAULT : 0;
684d339b133SRichard Cochran }
685d339b133SRichard Cochran 
686d339b133SRichard Cochran void igb_ptp_init(struct igb_adapter *adapter)
687d339b133SRichard Cochran {
688d339b133SRichard Cochran 	struct e1000_hw *hw = &adapter->hw;
689201987e3SMatthew Vick 	struct net_device *netdev = adapter->netdev;
690d339b133SRichard Cochran 
691d339b133SRichard Cochran 	switch (hw->mac.type) {
692d339b133SRichard Cochran 	case e1000_82576:
693201987e3SMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
694a79f4f88SMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
695a79f4f88SMatthew Vick 		adapter->ptp_caps.max_adj = 1000000000;
696a79f4f88SMatthew Vick 		adapter->ptp_caps.n_ext_ts = 0;
697a79f4f88SMatthew Vick 		adapter->ptp_caps.pps = 0;
698a79f4f88SMatthew Vick 		adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
699e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
700e57b8bdbSMatthew Vick 		adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
701e57b8bdbSMatthew Vick 		adapter->ptp_caps.settime = igb_ptp_settime_82576;
702a79f4f88SMatthew Vick 		adapter->ptp_caps.enable = igb_ptp_enable;
703a79f4f88SMatthew Vick 		adapter->cc.read = igb_ptp_read_82576;
704d339b133SRichard Cochran 		adapter->cc.mask = CLOCKSOURCE_MASK(64);
705d339b133SRichard Cochran 		adapter->cc.mult = 1;
706d339b133SRichard Cochran 		adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
707d339b133SRichard Cochran 		/* Dial the nominal frequency. */
708d339b133SRichard Cochran 		wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
709d339b133SRichard Cochran 		break;
710e57b8bdbSMatthew Vick 	case e1000_82580:
711e57b8bdbSMatthew Vick 	case e1000_i350:
712e57b8bdbSMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
713e57b8bdbSMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
714e57b8bdbSMatthew Vick 		adapter->ptp_caps.max_adj = 62499999;
715e57b8bdbSMatthew Vick 		adapter->ptp_caps.n_ext_ts = 0;
716e57b8bdbSMatthew Vick 		adapter->ptp_caps.pps = 0;
717e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
718e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
719e57b8bdbSMatthew Vick 		adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
720e57b8bdbSMatthew Vick 		adapter->ptp_caps.settime = igb_ptp_settime_82576;
721e57b8bdbSMatthew Vick 		adapter->ptp_caps.enable = igb_ptp_enable;
722e57b8bdbSMatthew Vick 		adapter->cc.read = igb_ptp_read_82580;
723e57b8bdbSMatthew Vick 		adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580);
724e57b8bdbSMatthew Vick 		adapter->cc.mult = 1;
725e57b8bdbSMatthew Vick 		adapter->cc.shift = 0;
726e57b8bdbSMatthew Vick 		/* Enable the timer functions by clearing bit 31. */
727e57b8bdbSMatthew Vick 		wr32(E1000_TSAUXC, 0x0);
728e57b8bdbSMatthew Vick 		break;
729e57b8bdbSMatthew Vick 	case e1000_i210:
730e57b8bdbSMatthew Vick 	case e1000_i211:
731e57b8bdbSMatthew Vick 		snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
732e57b8bdbSMatthew Vick 		adapter->ptp_caps.owner = THIS_MODULE;
733e57b8bdbSMatthew Vick 		adapter->ptp_caps.max_adj = 62499999;
734e57b8bdbSMatthew Vick 		adapter->ptp_caps.n_ext_ts = 0;
735e57b8bdbSMatthew Vick 		adapter->ptp_caps.pps = 0;
736e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
737e57b8bdbSMatthew Vick 		adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
738e57b8bdbSMatthew Vick 		adapter->ptp_caps.gettime = igb_ptp_gettime_i210;
739e57b8bdbSMatthew Vick 		adapter->ptp_caps.settime = igb_ptp_settime_i210;
740e57b8bdbSMatthew Vick 		adapter->ptp_caps.enable = igb_ptp_enable;
741e57b8bdbSMatthew Vick 		/* Enable the timer functions by clearing bit 31. */
742e57b8bdbSMatthew Vick 		wr32(E1000_TSAUXC, 0x0);
743e57b8bdbSMatthew Vick 		break;
744d339b133SRichard Cochran 	default:
745d339b133SRichard Cochran 		adapter->ptp_clock = NULL;
746d339b133SRichard Cochran 		return;
747d339b133SRichard Cochran 	}
748d339b133SRichard Cochran 
749d339b133SRichard Cochran 	wrfl();
750d339b133SRichard Cochran 
751e57b8bdbSMatthew Vick 	spin_lock_init(&adapter->tmreg_lock);
752e57b8bdbSMatthew Vick 	INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
753e57b8bdbSMatthew Vick 
754e57b8bdbSMatthew Vick 	/* Initialize the clock and overflow work for devices that need it. */
755e57b8bdbSMatthew Vick 	if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
756e57b8bdbSMatthew Vick 		struct timespec ts = ktime_to_timespec(ktime_get_real());
757e57b8bdbSMatthew Vick 
758e57b8bdbSMatthew Vick 		igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
759e57b8bdbSMatthew Vick 	} else {
760d339b133SRichard Cochran 		timecounter_init(&adapter->tc, &adapter->cc,
761d339b133SRichard Cochran 				 ktime_to_ns(ktime_get_real()));
762d339b133SRichard Cochran 
763e57b8bdbSMatthew Vick 		INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
764e57b8bdbSMatthew Vick 				  igb_ptp_overflow_check);
7651f6e8178SMatthew Vick 
766a79f4f88SMatthew Vick 		schedule_delayed_work(&adapter->ptp_overflow_work,
767a79f4f88SMatthew Vick 				      IGB_SYSTIM_OVERFLOW_PERIOD);
768e57b8bdbSMatthew Vick 	}
769d339b133SRichard Cochran 
7701f6e8178SMatthew Vick 	/* Initialize the time sync interrupts for devices that support it. */
7711f6e8178SMatthew Vick 	if (hw->mac.type >= e1000_82580) {
7721f6e8178SMatthew Vick 		wr32(E1000_TSIM, E1000_TSIM_TXTS);
7731f6e8178SMatthew Vick 		wr32(E1000_IMS, E1000_IMS_TS);
7741f6e8178SMatthew Vick 	}
7751f6e8178SMatthew Vick 
7761ef76158SRichard Cochran 	adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
7771ef76158SRichard Cochran 						&adapter->pdev->dev);
778d339b133SRichard Cochran 	if (IS_ERR(adapter->ptp_clock)) {
779d339b133SRichard Cochran 		adapter->ptp_clock = NULL;
780d339b133SRichard Cochran 		dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
7811f6e8178SMatthew Vick 	} else {
782d339b133SRichard Cochran 		dev_info(&adapter->pdev->dev, "added PHC on %s\n",
783d339b133SRichard Cochran 			 adapter->netdev->name);
7841f6e8178SMatthew Vick 		adapter->flags |= IGB_FLAG_PTP;
7851f6e8178SMatthew Vick 	}
786d339b133SRichard Cochran }
787d339b133SRichard Cochran 
788a79f4f88SMatthew Vick /**
789a79f4f88SMatthew Vick  * igb_ptp_stop - Disable PTP device and stop the overflow check.
790a79f4f88SMatthew Vick  * @adapter: Board private structure.
791a79f4f88SMatthew Vick  *
792a79f4f88SMatthew Vick  * This function stops the PTP support and cancels the delayed work.
793a79f4f88SMatthew Vick  **/
794a79f4f88SMatthew Vick void igb_ptp_stop(struct igb_adapter *adapter)
795d339b133SRichard Cochran {
796d3eef8c8SCarolyn Wyborny 	switch (adapter->hw.mac.type) {
797d3eef8c8SCarolyn Wyborny 	case e1000_82576:
7981f6e8178SMatthew Vick 	case e1000_82580:
7991f6e8178SMatthew Vick 	case e1000_i350:
800a79f4f88SMatthew Vick 		cancel_delayed_work_sync(&adapter->ptp_overflow_work);
801d3eef8c8SCarolyn Wyborny 		break;
8021f6e8178SMatthew Vick 	case e1000_i210:
8031f6e8178SMatthew Vick 	case e1000_i211:
8041f6e8178SMatthew Vick 		/* No delayed work to cancel. */
8051f6e8178SMatthew Vick 		break;
806d3eef8c8SCarolyn Wyborny 	default:
807d3eef8c8SCarolyn Wyborny 		return;
808d3eef8c8SCarolyn Wyborny 	}
809d339b133SRichard Cochran 
8101f6e8178SMatthew Vick 	cancel_work_sync(&adapter->ptp_tx_work);
8111f6e8178SMatthew Vick 
812d339b133SRichard Cochran 	if (adapter->ptp_clock) {
813d339b133SRichard Cochran 		ptp_clock_unregister(adapter->ptp_clock);
814d339b133SRichard Cochran 		dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
815d339b133SRichard Cochran 			 adapter->netdev->name);
8161f6e8178SMatthew Vick 		adapter->flags &= ~IGB_FLAG_PTP;
817d339b133SRichard Cochran 	}
818d339b133SRichard Cochran }
8191f6e8178SMatthew Vick 
8201f6e8178SMatthew Vick /**
8211f6e8178SMatthew Vick  * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
8221f6e8178SMatthew Vick  * @adapter: Board private structure.
8231f6e8178SMatthew Vick  *
8241f6e8178SMatthew Vick  * This function handles the reset work required to re-enable the PTP device.
8251f6e8178SMatthew Vick  **/
8261f6e8178SMatthew Vick void igb_ptp_reset(struct igb_adapter *adapter)
8271f6e8178SMatthew Vick {
8281f6e8178SMatthew Vick 	struct e1000_hw *hw = &adapter->hw;
8291f6e8178SMatthew Vick 
8301f6e8178SMatthew Vick 	if (!(adapter->flags & IGB_FLAG_PTP))
8311f6e8178SMatthew Vick 		return;
8321f6e8178SMatthew Vick 
8331f6e8178SMatthew Vick 	switch (adapter->hw.mac.type) {
8341f6e8178SMatthew Vick 	case e1000_82576:
8351f6e8178SMatthew Vick 		/* Dial the nominal frequency. */
8361f6e8178SMatthew Vick 		wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
8371f6e8178SMatthew Vick 		break;
8381f6e8178SMatthew Vick 	case e1000_82580:
8391f6e8178SMatthew Vick 	case e1000_i350:
8401f6e8178SMatthew Vick 	case e1000_i210:
8411f6e8178SMatthew Vick 	case e1000_i211:
8421f6e8178SMatthew Vick 		/* Enable the timer functions and interrupts. */
8431f6e8178SMatthew Vick 		wr32(E1000_TSAUXC, 0x0);
8441f6e8178SMatthew Vick 		wr32(E1000_TSIM, E1000_TSIM_TXTS);
8451f6e8178SMatthew Vick 		wr32(E1000_IMS, E1000_IMS_TS);
8461f6e8178SMatthew Vick 		break;
8471f6e8178SMatthew Vick 	default:
8481f6e8178SMatthew Vick 		/* No work to do. */
8491f6e8178SMatthew Vick 		return;
8501f6e8178SMatthew Vick 	}
8511f6e8178SMatthew Vick 
852e57b8bdbSMatthew Vick 	/* Re-initialize the timer. */
853e57b8bdbSMatthew Vick 	if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
854e57b8bdbSMatthew Vick 		struct timespec ts = ktime_to_timespec(ktime_get_real());
855e57b8bdbSMatthew Vick 
856e57b8bdbSMatthew Vick 		igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
857e57b8bdbSMatthew Vick 	} else {
8581f6e8178SMatthew Vick 		timecounter_init(&adapter->tc, &adapter->cc,
8591f6e8178SMatthew Vick 				 ktime_to_ns(ktime_get_real()));
8601f6e8178SMatthew Vick 	}
861e57b8bdbSMatthew Vick }
862