1 /* Intel PRO/1000 Linux driver
2 * Copyright(c) 1999 - 2015 Intel Corporation.
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.
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
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
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
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>
29 #ifdef CONFIG_E1000E_HWTS
30 #include <linux/clocksource.h>
31 #include <linux/ktime.h>
36 * e1000e_phc_adjfreq - adjust the frequency of the hardware clock
37 * @ptp: ptp clock structure
38 * @delta: Desired frequency change in parts per billion
40 * Adjust the frequency of the PHC cycle counter by the indicated delta from
43 static int e1000e_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
45 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
47 struct e1000_hw *hw = &adapter->hw;
51 u32 timinca, incvalue;
54 if ((delta > ptp->max_adj) || (delta <= -1000000000))
62 /* Get the System Time Register SYSTIM base frequency */
63 ret_val = e1000e_get_base_timinca(adapter, &timinca);
67 spin_lock_irqsave(&adapter->systim_lock, flags);
69 incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
71 adjustment = incvalue;
73 adjustment = div_u64(adjustment, 1000000000);
75 incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
77 timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
80 ew32(TIMINCA, timinca);
82 adapter->ptp_delta = delta;
84 spin_unlock_irqrestore(&adapter->systim_lock, flags);
90 * e1000e_phc_adjtime - Shift the time of the hardware clock
91 * @ptp: ptp clock structure
92 * @delta: Desired change in nanoseconds
94 * Adjust the timer by resetting the timecounter structure.
96 static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
98 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
102 spin_lock_irqsave(&adapter->systim_lock, flags);
103 timecounter_adjtime(&adapter->tc, delta);
104 spin_unlock_irqrestore(&adapter->systim_lock, flags);
109 #ifdef CONFIG_E1000E_HWTS
110 #define MAX_HW_WAIT_COUNT (3)
113 * e1000e_phc_get_syncdevicetime - Callback given to timekeeping code reads system/device registers
114 * @device: current device time
115 * @system: system counter value read synchronously with device time
116 * @ctx: context provided by timekeeping code
118 * Read device and system (ART) clock simultaneously and return the corrected
119 * clock values in ns.
121 static int e1000e_phc_get_syncdevicetime(ktime_t *device,
122 struct system_counterval_t *system,
125 struct e1000_adapter *adapter = (struct e1000_adapter *)ctx;
126 struct e1000_hw *hw = &adapter->hw;
133 tsync_ctrl = er32(TSYNCTXCTL);
134 tsync_ctrl |= E1000_TSYNCTXCTL_START_SYNC |
135 E1000_TSYNCTXCTL_MAX_ALLOWED_DLY_MASK;
136 ew32(TSYNCTXCTL, tsync_ctrl);
137 for (i = 0; i < MAX_HW_WAIT_COUNT; ++i) {
139 tsync_ctrl = er32(TSYNCTXCTL);
140 if (tsync_ctrl & E1000_TSYNCTXCTL_SYNC_COMP)
144 if (i == MAX_HW_WAIT_COUNT)
147 dev_cycles = er32(SYSSTMPH);
149 dev_cycles |= er32(SYSSTMPL);
150 spin_lock_irqsave(&adapter->systim_lock, flags);
151 *device = ns_to_ktime(timecounter_cyc2time(&adapter->tc, dev_cycles));
152 spin_unlock_irqrestore(&adapter->systim_lock, flags);
154 sys_cycles = er32(PLTSTMPH);
156 sys_cycles |= er32(PLTSTMPL);
157 *system = convert_art_to_tsc(sys_cycles);
163 * e1000e_phc_getsynctime - Reads the current system/device cross timestamp
164 * @ptp: ptp clock structure
165 * @cts: structure containing timestamp
167 * Read device and system (ART) clock simultaneously and return the scaled
168 * clock values in ns.
170 static int e1000e_phc_getcrosststamp(struct ptp_clock_info *ptp,
171 struct system_device_crosststamp *xtstamp)
173 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
176 return get_device_system_crosststamp(e1000e_phc_get_syncdevicetime,
177 adapter, NULL, xtstamp);
179 #endif/*CONFIG_E1000E_HWTS*/
182 * e1000e_phc_gettime - Reads the current time from the hardware clock
183 * @ptp: ptp clock structure
184 * @ts: timespec structure to hold the current time value
186 * Read the timecounter and return the correct value in ns after converting
187 * it into a struct timespec.
189 static int e1000e_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
191 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
196 spin_lock_irqsave(&adapter->systim_lock, flags);
198 /* Use timecounter_cyc2time() to allow non-monotonic SYSTIM readings */
199 cycles = adapter->cc.read(&adapter->cc);
200 ns = timecounter_cyc2time(&adapter->tc, cycles);
202 spin_unlock_irqrestore(&adapter->systim_lock, flags);
204 *ts = ns_to_timespec64(ns);
210 * e1000e_phc_settime - Set the current time on the hardware clock
211 * @ptp: ptp clock structure
212 * @ts: timespec containing the new time for the cycle counter
214 * Reset the timecounter to use a new base value instead of the kernel
217 static int e1000e_phc_settime(struct ptp_clock_info *ptp,
218 const struct timespec64 *ts)
220 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
225 ns = timespec64_to_ns(ts);
227 /* reset the timecounter */
228 spin_lock_irqsave(&adapter->systim_lock, flags);
229 timecounter_init(&adapter->tc, &adapter->cc, ns);
230 spin_unlock_irqrestore(&adapter->systim_lock, flags);
236 * e1000e_phc_enable - enable or disable an ancillary feature
237 * @ptp: ptp clock structure
238 * @request: Desired resource to enable or disable
239 * @on: Caller passes one to enable or zero to disable
241 * Enable (or disable) ancillary features of the PHC subsystem.
242 * Currently, no ancillary features are supported.
244 static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
245 struct ptp_clock_request __always_unused *request,
246 int __always_unused on)
251 static void e1000e_systim_overflow_work(struct work_struct *work)
253 struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
254 systim_overflow_work.work);
255 struct e1000_hw *hw = &adapter->hw;
256 struct timespec64 ts;
259 /* Update the timecounter */
260 ns = timecounter_read(&adapter->tc);
262 ts = ns_to_timespec64(ns);
263 e_dbg("SYSTIM overflow check at %lld.%09lu\n",
264 (long long) ts.tv_sec, ts.tv_nsec);
266 schedule_delayed_work(&adapter->systim_overflow_work,
267 E1000_SYSTIM_OVERFLOW_PERIOD);
270 static const struct ptp_clock_info e1000e_ptp_clock_info = {
271 .owner = THIS_MODULE,
277 .adjfreq = e1000e_phc_adjfreq,
278 .adjtime = e1000e_phc_adjtime,
279 .gettime64 = e1000e_phc_gettime,
280 .settime64 = e1000e_phc_settime,
281 .enable = e1000e_phc_enable,
285 * e1000e_ptp_init - initialize PTP for devices which support it
286 * @adapter: board private structure
288 * This function performs the required steps for enabling PTP support.
289 * If PTP support has already been loaded it simply calls the cyclecounter
290 * init routine and exits.
292 void e1000e_ptp_init(struct e1000_adapter *adapter)
294 struct e1000_hw *hw = &adapter->hw;
296 adapter->ptp_clock = NULL;
298 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
301 adapter->ptp_clock_info = e1000e_ptp_clock_info;
303 snprintf(adapter->ptp_clock_info.name,
304 sizeof(adapter->ptp_clock_info.name), "%pm",
305 adapter->netdev->perm_addr);
307 switch (hw->mac.type) {
311 if (((hw->mac.type != e1000_pch_lpt) &&
312 (hw->mac.type != e1000_pch_spt)) ||
313 (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
314 adapter->ptp_clock_info.max_adj = 24000000 - 1;
320 adapter->ptp_clock_info.max_adj = 600000000 - 1;
326 #ifdef CONFIG_E1000E_HWTS
327 /* CPU must have ART and GBe must be from Sunrise Point or greater */
328 if (hw->mac.type >= e1000_pch_spt && boot_cpu_has(X86_FEATURE_ART))
329 adapter->ptp_clock_info.getcrosststamp =
330 e1000e_phc_getcrosststamp;
331 #endif/*CONFIG_E1000E_HWTS*/
333 INIT_DELAYED_WORK(&adapter->systim_overflow_work,
334 e1000e_systim_overflow_work);
336 schedule_delayed_work(&adapter->systim_overflow_work,
337 E1000_SYSTIM_OVERFLOW_PERIOD);
339 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
340 &adapter->pdev->dev);
341 if (IS_ERR(adapter->ptp_clock)) {
342 adapter->ptp_clock = NULL;
343 e_err("ptp_clock_register failed\n");
344 } else if (adapter->ptp_clock) {
345 e_info("registered PHC clock\n");
350 * e1000e_ptp_remove - disable PTP device and stop the overflow check
351 * @adapter: board private structure
353 * Stop the PTP support, and cancel the delayed work.
355 void e1000e_ptp_remove(struct e1000_adapter *adapter)
357 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
360 cancel_delayed_work_sync(&adapter->systim_overflow_work);
362 if (adapter->ptp_clock) {
363 ptp_clock_unregister(adapter->ptp_clock);
364 adapter->ptp_clock = NULL;
365 e_info("removed PHC\n");