• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
3 
4 /* PTP 1588 Hardware Clock (PHC)
5  * Derived from PTP Hardware Clock driver for Intel 82576 and 82580 (igb)
6  * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
7  */
8 
9 #include "e1000.h"
10 
11 #ifdef CONFIG_E1000E_HWTS
12 #include <linux/clocksource.h>
13 #include <linux/ktime.h>
14 #include <asm/tsc.h>
15 #endif
16 
17 /**
18  * e1000e_phc_adjfreq - adjust the frequency of the hardware clock
19  * @ptp: ptp clock structure
20  * @delta: Desired frequency change in parts per billion
21  *
22  * Adjust the frequency of the PHC cycle counter by the indicated delta from
23  * the base frequency.
24  **/
e1000e_phc_adjfreq(struct ptp_clock_info * ptp,s32 delta)25 static int e1000e_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
26 {
27 	struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
28 						     ptp_clock_info);
29 	struct e1000_hw *hw = &adapter->hw;
30 	bool neg_adj = false;
31 	unsigned long flags;
32 	u64 adjustment;
33 	u32 timinca, incvalue;
34 	s32 ret_val;
35 
36 	if ((delta > ptp->max_adj) || (delta <= -1000000000))
37 		return -EINVAL;
38 
39 	if (delta < 0) {
40 		neg_adj = true;
41 		delta = -delta;
42 	}
43 
44 	/* Get the System Time Register SYSTIM base frequency */
45 	ret_val = e1000e_get_base_timinca(adapter, &timinca);
46 	if (ret_val)
47 		return ret_val;
48 
49 	spin_lock_irqsave(&adapter->systim_lock, flags);
50 
51 	incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
52 
53 	adjustment = incvalue;
54 	adjustment *= delta;
55 	adjustment = div_u64(adjustment, 1000000000);
56 
57 	incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
58 
59 	timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
60 	timinca |= incvalue;
61 
62 	ew32(TIMINCA, timinca);
63 
64 	adapter->ptp_delta = delta;
65 
66 	spin_unlock_irqrestore(&adapter->systim_lock, flags);
67 
68 	return 0;
69 }
70 
71 /**
72  * e1000e_phc_adjtime - Shift the time of the hardware clock
73  * @ptp: ptp clock structure
74  * @delta: Desired change in nanoseconds
75  *
76  * Adjust the timer by resetting the timecounter structure.
77  **/
e1000e_phc_adjtime(struct ptp_clock_info * ptp,s64 delta)78 static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
79 {
80 	struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
81 						     ptp_clock_info);
82 	unsigned long flags;
83 
84 	spin_lock_irqsave(&adapter->systim_lock, flags);
85 	timecounter_adjtime(&adapter->tc, delta);
86 	spin_unlock_irqrestore(&adapter->systim_lock, flags);
87 
88 	return 0;
89 }
90 
91 #ifdef CONFIG_E1000E_HWTS
92 #define MAX_HW_WAIT_COUNT (3)
93 
94 /**
95  * e1000e_phc_get_syncdevicetime - Callback given to timekeeping code reads system/device registers
96  * @device: current device time
97  * @system: system counter value read synchronously with device time
98  * @ctx: context provided by timekeeping code
99  *
100  * Read device and system (ART) clock simultaneously and return the corrected
101  * clock values in ns.
102  **/
e1000e_phc_get_syncdevicetime(ktime_t * device,struct system_counterval_t * system,void * ctx)103 static int e1000e_phc_get_syncdevicetime(ktime_t *device,
104 					 struct system_counterval_t *system,
105 					 void *ctx)
106 {
107 	struct e1000_adapter *adapter = (struct e1000_adapter *)ctx;
108 	struct e1000_hw *hw = &adapter->hw;
109 	unsigned long flags;
110 	int i;
111 	u32 tsync_ctrl;
112 	u64 dev_cycles;
113 	u64 sys_cycles;
114 
115 	tsync_ctrl = er32(TSYNCTXCTL);
116 	tsync_ctrl |= E1000_TSYNCTXCTL_START_SYNC |
117 		E1000_TSYNCTXCTL_MAX_ALLOWED_DLY_MASK;
118 	ew32(TSYNCTXCTL, tsync_ctrl);
119 	for (i = 0; i < MAX_HW_WAIT_COUNT; ++i) {
120 		udelay(1);
121 		tsync_ctrl = er32(TSYNCTXCTL);
122 		if (tsync_ctrl & E1000_TSYNCTXCTL_SYNC_COMP)
123 			break;
124 	}
125 
126 	if (i == MAX_HW_WAIT_COUNT)
127 		return -ETIMEDOUT;
128 
129 	dev_cycles = er32(SYSSTMPH);
130 	dev_cycles <<= 32;
131 	dev_cycles |= er32(SYSSTMPL);
132 	spin_lock_irqsave(&adapter->systim_lock, flags);
133 	*device = ns_to_ktime(timecounter_cyc2time(&adapter->tc, dev_cycles));
134 	spin_unlock_irqrestore(&adapter->systim_lock, flags);
135 
136 	sys_cycles = er32(PLTSTMPH);
137 	sys_cycles <<= 32;
138 	sys_cycles |= er32(PLTSTMPL);
139 	*system = convert_art_to_tsc(sys_cycles);
140 
141 	return 0;
142 }
143 
144 /**
145  * e1000e_phc_getsynctime - Reads the current system/device cross timestamp
146  * @ptp: ptp clock structure
147  * @cts: structure containing timestamp
148  *
149  * Read device and system (ART) clock simultaneously and return the scaled
150  * clock values in ns.
151  **/
e1000e_phc_getcrosststamp(struct ptp_clock_info * ptp,struct system_device_crosststamp * xtstamp)152 static int e1000e_phc_getcrosststamp(struct ptp_clock_info *ptp,
153 				     struct system_device_crosststamp *xtstamp)
154 {
155 	struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
156 						     ptp_clock_info);
157 
158 	return get_device_system_crosststamp(e1000e_phc_get_syncdevicetime,
159 						adapter, NULL, xtstamp);
160 }
161 #endif/*CONFIG_E1000E_HWTS*/
162 
163 /**
164  * e1000e_phc_gettime - Reads the current time from the hardware clock
165  * @ptp: ptp clock structure
166  * @ts: timespec structure to hold the current time value
167  *
168  * Read the timecounter and return the correct value in ns after converting
169  * it into a struct timespec.
170  **/
e1000e_phc_gettime(struct ptp_clock_info * ptp,struct timespec64 * ts)171 static int e1000e_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
172 {
173 	struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
174 						     ptp_clock_info);
175 	unsigned long flags;
176 	u64 cycles, ns;
177 
178 	spin_lock_irqsave(&adapter->systim_lock, flags);
179 
180 	/* Use timecounter_cyc2time() to allow non-monotonic SYSTIM readings */
181 	cycles = adapter->cc.read(&adapter->cc);
182 	ns = timecounter_cyc2time(&adapter->tc, cycles);
183 
184 	spin_unlock_irqrestore(&adapter->systim_lock, flags);
185 
186 	*ts = ns_to_timespec64(ns);
187 
188 	return 0;
189 }
190 
191 /**
192  * e1000e_phc_settime - Set the current time on the hardware clock
193  * @ptp: ptp clock structure
194  * @ts: timespec containing the new time for the cycle counter
195  *
196  * Reset the timecounter to use a new base value instead of the kernel
197  * wall timer value.
198  **/
e1000e_phc_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)199 static int e1000e_phc_settime(struct ptp_clock_info *ptp,
200 			      const struct timespec64 *ts)
201 {
202 	struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
203 						     ptp_clock_info);
204 	unsigned long flags;
205 	u64 ns;
206 
207 	ns = timespec64_to_ns(ts);
208 
209 	/* reset the timecounter */
210 	spin_lock_irqsave(&adapter->systim_lock, flags);
211 	timecounter_init(&adapter->tc, &adapter->cc, ns);
212 	spin_unlock_irqrestore(&adapter->systim_lock, flags);
213 
214 	return 0;
215 }
216 
217 /**
218  * e1000e_phc_enable - enable or disable an ancillary feature
219  * @ptp: ptp clock structure
220  * @request: Desired resource to enable or disable
221  * @on: Caller passes one to enable or zero to disable
222  *
223  * Enable (or disable) ancillary features of the PHC subsystem.
224  * Currently, no ancillary features are supported.
225  **/
e1000e_phc_enable(struct ptp_clock_info __always_unused * ptp,struct ptp_clock_request __always_unused * request,int __always_unused on)226 static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
227 			     struct ptp_clock_request __always_unused *request,
228 			     int __always_unused on)
229 {
230 	return -EOPNOTSUPP;
231 }
232 
e1000e_systim_overflow_work(struct work_struct * work)233 static void e1000e_systim_overflow_work(struct work_struct *work)
234 {
235 	struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
236 						     systim_overflow_work.work);
237 	struct e1000_hw *hw = &adapter->hw;
238 	struct timespec64 ts;
239 	u64 ns;
240 
241 	/* Update the timecounter */
242 	ns = timecounter_read(&adapter->tc);
243 
244 	ts = ns_to_timespec64(ns);
245 	e_dbg("SYSTIM overflow check at %lld.%09lu\n",
246 	      (long long) ts.tv_sec, ts.tv_nsec);
247 
248 	schedule_delayed_work(&adapter->systim_overflow_work,
249 			      E1000_SYSTIM_OVERFLOW_PERIOD);
250 }
251 
252 static const struct ptp_clock_info e1000e_ptp_clock_info = {
253 	.owner		= THIS_MODULE,
254 	.n_alarm	= 0,
255 	.n_ext_ts	= 0,
256 	.n_per_out	= 0,
257 	.n_pins		= 0,
258 	.pps		= 0,
259 	.adjfreq	= e1000e_phc_adjfreq,
260 	.adjtime	= e1000e_phc_adjtime,
261 	.gettime64	= e1000e_phc_gettime,
262 	.settime64	= e1000e_phc_settime,
263 	.enable		= e1000e_phc_enable,
264 };
265 
266 /**
267  * e1000e_ptp_init - initialize PTP for devices which support it
268  * @adapter: board private structure
269  *
270  * This function performs the required steps for enabling PTP support.
271  * If PTP support has already been loaded it simply calls the cyclecounter
272  * init routine and exits.
273  **/
e1000e_ptp_init(struct e1000_adapter * adapter)274 void e1000e_ptp_init(struct e1000_adapter *adapter)
275 {
276 	struct e1000_hw *hw = &adapter->hw;
277 
278 	adapter->ptp_clock = NULL;
279 
280 	if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
281 		return;
282 
283 	adapter->ptp_clock_info = e1000e_ptp_clock_info;
284 
285 	snprintf(adapter->ptp_clock_info.name,
286 		 sizeof(adapter->ptp_clock_info.name), "%pm",
287 		 adapter->netdev->perm_addr);
288 
289 	switch (hw->mac.type) {
290 	case e1000_pch2lan:
291 	case e1000_pch_lpt:
292 	case e1000_pch_spt:
293 	case e1000_pch_cnp:
294 		if ((hw->mac.type < e1000_pch_lpt) ||
295 		    (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
296 			adapter->ptp_clock_info.max_adj = 24000000 - 1;
297 			break;
298 		}
299 		/* fall-through */
300 	case e1000_82574:
301 	case e1000_82583:
302 		adapter->ptp_clock_info.max_adj = 600000000 - 1;
303 		break;
304 	default:
305 		break;
306 	}
307 
308 #ifdef CONFIG_E1000E_HWTS
309 	/* CPU must have ART and GBe must be from Sunrise Point or greater */
310 	if (hw->mac.type >= e1000_pch_spt && boot_cpu_has(X86_FEATURE_ART))
311 		adapter->ptp_clock_info.getcrosststamp =
312 			e1000e_phc_getcrosststamp;
313 #endif/*CONFIG_E1000E_HWTS*/
314 
315 	INIT_DELAYED_WORK(&adapter->systim_overflow_work,
316 			  e1000e_systim_overflow_work);
317 
318 	schedule_delayed_work(&adapter->systim_overflow_work,
319 			      E1000_SYSTIM_OVERFLOW_PERIOD);
320 
321 	adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
322 						&adapter->pdev->dev);
323 	if (IS_ERR(adapter->ptp_clock)) {
324 		adapter->ptp_clock = NULL;
325 		e_err("ptp_clock_register failed\n");
326 	} else if (adapter->ptp_clock) {
327 		e_info("registered PHC clock\n");
328 	}
329 }
330 
331 /**
332  * e1000e_ptp_remove - disable PTP device and stop the overflow check
333  * @adapter: board private structure
334  *
335  * Stop the PTP support, and cancel the delayed work.
336  **/
e1000e_ptp_remove(struct e1000_adapter * adapter)337 void e1000e_ptp_remove(struct e1000_adapter *adapter)
338 {
339 	if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
340 		return;
341 
342 	cancel_delayed_work_sync(&adapter->systim_overflow_work);
343 
344 	if (adapter->ptp_clock) {
345 		ptp_clock_unregister(adapter->ptp_clock);
346 		adapter->ptp_clock = NULL;
347 		e_info("removed PHC\n");
348 	}
349 }
350