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
);
197 ns
= timecounter_read(&adapter
->tc
);
198 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
200 *ts
= ns_to_timespec64(ns
);
206 * e1000e_phc_settime - Set the current time on the hardware clock
207 * @ptp: ptp clock structure
208 * @ts: timespec containing the new time for the cycle counter
210 * Reset the timecounter to use a new base value instead of the kernel
213 static int e1000e_phc_settime(struct ptp_clock_info
*ptp
,
214 const struct timespec64
*ts
)
216 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
221 ns
= timespec64_to_ns(ts
);
223 /* reset the timecounter */
224 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
225 timecounter_init(&adapter
->tc
, &adapter
->cc
, ns
);
226 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
232 * e1000e_phc_enable - enable or disable an ancillary feature
233 * @ptp: ptp clock structure
234 * @request: Desired resource to enable or disable
235 * @on: Caller passes one to enable or zero to disable
237 * Enable (or disable) ancillary features of the PHC subsystem.
238 * Currently, no ancillary features are supported.
240 static int e1000e_phc_enable(struct ptp_clock_info __always_unused
*ptp
,
241 struct ptp_clock_request __always_unused
*request
,
242 int __always_unused on
)
247 static void e1000e_systim_overflow_work(struct work_struct
*work
)
249 struct e1000_adapter
*adapter
= container_of(work
, struct e1000_adapter
,
250 systim_overflow_work
.work
);
251 struct e1000_hw
*hw
= &adapter
->hw
;
252 struct timespec64 ts
;
254 adapter
->ptp_clock_info
.gettime64(&adapter
->ptp_clock_info
, &ts
);
256 e_dbg("SYSTIM overflow check at %lld.%09lu\n",
257 (long long) ts
.tv_sec
, ts
.tv_nsec
);
259 schedule_delayed_work(&adapter
->systim_overflow_work
,
260 E1000_SYSTIM_OVERFLOW_PERIOD
);
263 static const struct ptp_clock_info e1000e_ptp_clock_info
= {
264 .owner
= THIS_MODULE
,
270 .adjfreq
= e1000e_phc_adjfreq
,
271 .adjtime
= e1000e_phc_adjtime
,
272 .gettime64
= e1000e_phc_gettime
,
273 .settime64
= e1000e_phc_settime
,
274 .enable
= e1000e_phc_enable
,
278 * e1000e_ptp_init - initialize PTP for devices which support it
279 * @adapter: board private structure
281 * This function performs the required steps for enabling PTP support.
282 * If PTP support has already been loaded it simply calls the cyclecounter
283 * init routine and exits.
285 void e1000e_ptp_init(struct e1000_adapter
*adapter
)
287 struct e1000_hw
*hw
= &adapter
->hw
;
289 adapter
->ptp_clock
= NULL
;
291 if (!(adapter
->flags
& FLAG_HAS_HW_TIMESTAMP
))
294 adapter
->ptp_clock_info
= e1000e_ptp_clock_info
;
296 snprintf(adapter
->ptp_clock_info
.name
,
297 sizeof(adapter
->ptp_clock_info
.name
), "%pm",
298 adapter
->netdev
->perm_addr
);
300 switch (hw
->mac
.type
) {
305 if ((hw
->mac
.type
< e1000_pch_lpt
) ||
306 (er32(TSYNCRXCTL
) & E1000_TSYNCRXCTL_SYSCFI
)) {
307 adapter
->ptp_clock_info
.max_adj
= 24000000 - 1;
313 adapter
->ptp_clock_info
.max_adj
= 600000000 - 1;
319 #ifdef CONFIG_E1000E_HWTS
320 /* CPU must have ART and GBe must be from Sunrise Point or greater */
321 if (hw
->mac
.type
>= e1000_pch_spt
&& boot_cpu_has(X86_FEATURE_ART
))
322 adapter
->ptp_clock_info
.getcrosststamp
=
323 e1000e_phc_getcrosststamp
;
324 #endif/*CONFIG_E1000E_HWTS*/
326 INIT_DELAYED_WORK(&adapter
->systim_overflow_work
,
327 e1000e_systim_overflow_work
);
329 schedule_delayed_work(&adapter
->systim_overflow_work
,
330 E1000_SYSTIM_OVERFLOW_PERIOD
);
332 adapter
->ptp_clock
= ptp_clock_register(&adapter
->ptp_clock_info
,
333 &adapter
->pdev
->dev
);
334 if (IS_ERR(adapter
->ptp_clock
)) {
335 adapter
->ptp_clock
= NULL
;
336 e_err("ptp_clock_register failed\n");
337 } else if (adapter
->ptp_clock
) {
338 e_info("registered PHC clock\n");
343 * e1000e_ptp_remove - disable PTP device and stop the overflow check
344 * @adapter: board private structure
346 * Stop the PTP support, and cancel the delayed work.
348 void e1000e_ptp_remove(struct e1000_adapter
*adapter
)
350 if (!(adapter
->flags
& FLAG_HAS_HW_TIMESTAMP
))
353 cancel_delayed_work_sync(&adapter
->systim_overflow_work
);
355 if (adapter
->ptp_clock
) {
356 ptp_clock_unregister(adapter
->ptp_clock
);
357 adapter
->ptp_clock
= NULL
;
358 e_info("removed PHC\n");