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