1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2018 Intel Corporation. */
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>
11 #ifdef CONFIG_E1000E_HWTS
12 #include <linux/clocksource.h>
13 #include <linux/ktime.h>
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
22 * Adjust the frequency of the PHC cycle counter by the indicated delta from
25 * Scaled parts per million is ppm but with a 16 bit binary fractional field.
27 static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta)
29 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
31 struct e1000_hw *hw = &adapter->hw;
37 /* Get the System Time Register SYSTIM base frequency */
38 ret_val = e1000e_get_base_timinca(adapter, &timinca);
42 spin_lock_irqsave(&adapter->systim_lock, flags);
44 incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
45 incvalue = adjust_by_scaled_ppm(incvalue, delta);
47 timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
50 ew32(TIMINCA, timinca);
52 adapter->ptp_delta = delta;
54 spin_unlock_irqrestore(&adapter->systim_lock, flags);
60 * e1000e_phc_adjtime - Shift the time of the hardware clock
61 * @ptp: ptp clock structure
62 * @delta: Desired change in nanoseconds
64 * Adjust the timer by resetting the timecounter structure.
66 static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
68 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
72 spin_lock_irqsave(&adapter->systim_lock, flags);
73 timecounter_adjtime(&adapter->tc, delta);
74 spin_unlock_irqrestore(&adapter->systim_lock, flags);
79 #ifdef CONFIG_E1000E_HWTS
80 #define MAX_HW_WAIT_COUNT (3)
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
88 * Read device and system (ART) clock simultaneously and return the corrected
91 static int e1000e_phc_get_syncdevicetime(ktime_t *device,
92 struct system_counterval_t *system,
95 struct e1000_adapter *adapter = (struct e1000_adapter *)ctx;
96 struct e1000_hw *hw = &adapter->hw;
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) {
109 tsync_ctrl = er32(TSYNCTXCTL);
110 if (tsync_ctrl & E1000_TSYNCTXCTL_SYNC_COMP)
114 if (i == MAX_HW_WAIT_COUNT)
117 dev_cycles = er32(SYSSTMPH);
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);
124 sys_cycles = er32(PLTSTMPH);
126 sys_cycles |= er32(PLTSTMPL);
127 *system = convert_art_to_tsc(sys_cycles);
133 * e1000e_phc_getcrosststamp - Reads the current system/device cross timestamp
134 * @ptp: ptp clock structure
135 * @xtstamp: structure containing timestamp
137 * Read device and system (ART) clock simultaneously and return the scaled
138 * clock values in ns.
140 static int e1000e_phc_getcrosststamp(struct ptp_clock_info *ptp,
141 struct system_device_crosststamp *xtstamp)
143 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
146 return get_device_system_crosststamp(e1000e_phc_get_syncdevicetime,
147 adapter, NULL, xtstamp);
149 #endif/*CONFIG_E1000E_HWTS*/
152 * e1000e_phc_gettimex - Reads the current time from the hardware clock and
154 * @ptp: ptp clock structure
155 * @ts: timespec structure to hold the current PHC time
156 * @sts: structure to hold the current system time
158 * Read the timecounter and return the correct value in ns after converting
159 * it into a struct timespec.
161 static int e1000e_phc_gettimex(struct ptp_clock_info *ptp,
162 struct timespec64 *ts,
163 struct ptp_system_timestamp *sts)
165 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
170 spin_lock_irqsave(&adapter->systim_lock, flags);
172 /* NOTE: Non-monotonic SYSTIM readings may be returned */
173 cycles = e1000e_read_systim(adapter, sts);
174 ns = timecounter_cyc2time(&adapter->tc, cycles);
176 spin_unlock_irqrestore(&adapter->systim_lock, flags);
178 *ts = ns_to_timespec64(ns);
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
188 * Reset the timecounter to use a new base value instead of the kernel
191 static int e1000e_phc_settime(struct ptp_clock_info *ptp,
192 const struct timespec64 *ts)
194 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
199 ns = timespec64_to_ns(ts);
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);
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
215 * Enable (or disable) ancillary features of the PHC subsystem.
216 * Currently, no ancillary features are supported.
218 static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
219 struct ptp_clock_request __always_unused *request,
220 int __always_unused on)
225 static void e1000e_systim_overflow_work(struct work_struct *work)
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;
233 /* Update the timecounter */
234 ns = timecounter_read(&adapter->tc);
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);
240 schedule_delayed_work(&adapter->systim_overflow_work,
241 E1000_SYSTIM_OVERFLOW_PERIOD);
244 static const struct ptp_clock_info e1000e_ptp_clock_info = {
245 .owner = THIS_MODULE,
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,
259 * e1000e_ptp_init - initialize PTP for devices which support it
260 * @adapter: board private structure
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.
266 void e1000e_ptp_init(struct e1000_adapter *adapter)
268 struct e1000_hw *hw = &adapter->hw;
270 adapter->ptp_clock = NULL;
272 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
275 adapter->ptp_clock_info = e1000e_ptp_clock_info;
277 snprintf(adapter->ptp_clock_info.name,
278 sizeof(adapter->ptp_clock_info.name), "%pm",
279 adapter->netdev->perm_addr);
281 switch (hw->mac.type) {
283 adapter->ptp_clock_info.max_adj = MAX_PPB_96MHZ;
286 if (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)
287 adapter->ptp_clock_info.max_adj = MAX_PPB_96MHZ;
289 adapter->ptp_clock_info.max_adj = MAX_PPB_25MHZ;
292 adapter->ptp_clock_info.max_adj = MAX_PPB_24MHZ;
301 if (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)
302 adapter->ptp_clock_info.max_adj = MAX_PPB_24MHZ;
304 adapter->ptp_clock_info.max_adj = MAX_PPB_38400KHZ;
308 adapter->ptp_clock_info.max_adj = MAX_PPB_25MHZ;
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*/
321 INIT_DELAYED_WORK(&adapter->systim_overflow_work,
322 e1000e_systim_overflow_work);
324 schedule_delayed_work(&adapter->systim_overflow_work,
325 E1000_SYSTIM_OVERFLOW_PERIOD);
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");
338 * e1000e_ptp_remove - disable PTP device and stop the overflow check
339 * @adapter: board private structure
341 * Stop the PTP support, and cancel the delayed work.
343 void e1000e_ptp_remove(struct e1000_adapter *adapter)
345 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
348 cancel_delayed_work_sync(&adapter->systim_overflow_work);
350 if (adapter->ptp_clock) {
351 ptp_clock_unregister(adapter->ptp_clock);
352 adapter->ptp_clock = NULL;
353 e_info("removed PHC\n");