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 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
88 * e1000e_phc_adjtime - Shift the time of the hardware clock
89 * @ptp: ptp clock structure
90 * @delta: Desired change in nanoseconds
92 * Adjust the timer by resetting the timecounter structure.
94 static int e1000e_phc_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
96 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
100 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
101 timecounter_adjtime(&adapter
->tc
, delta
);
102 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
107 #ifdef CONFIG_E1000E_HWTS
108 #define MAX_HW_WAIT_COUNT (3)
111 * e1000e_phc_get_syncdevicetime - Callback given to timekeeping code reads system/device registers
112 * @device: current device time
113 * @system: system counter value read synchronously with device time
114 * @ctx: context provided by timekeeping code
116 * Read device and system (ART) clock simultaneously and return the corrected
117 * clock values in ns.
119 static int e1000e_phc_get_syncdevicetime(ktime_t
*device
,
120 struct system_counterval_t
*system
,
123 struct e1000_adapter
*adapter
= (struct e1000_adapter
*)ctx
;
124 struct e1000_hw
*hw
= &adapter
->hw
;
131 tsync_ctrl
= er32(TSYNCTXCTL
);
132 tsync_ctrl
|= E1000_TSYNCTXCTL_START_SYNC
|
133 E1000_TSYNCTXCTL_MAX_ALLOWED_DLY_MASK
;
134 ew32(TSYNCTXCTL
, tsync_ctrl
);
135 for (i
= 0; i
< MAX_HW_WAIT_COUNT
; ++i
) {
137 tsync_ctrl
= er32(TSYNCTXCTL
);
138 if (tsync_ctrl
& E1000_TSYNCTXCTL_SYNC_COMP
)
142 if (i
== MAX_HW_WAIT_COUNT
)
145 dev_cycles
= er32(SYSSTMPH
);
147 dev_cycles
|= er32(SYSSTMPL
);
148 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
149 *device
= ns_to_ktime(timecounter_cyc2time(&adapter
->tc
, dev_cycles
));
150 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
152 sys_cycles
= er32(PLTSTMPH
);
154 sys_cycles
|= er32(PLTSTMPL
);
155 *system
= convert_art_to_tsc(sys_cycles
);
161 * e1000e_phc_getsynctime - Reads the current system/device cross timestamp
162 * @ptp: ptp clock structure
163 * @cts: structure containing timestamp
165 * Read device and system (ART) clock simultaneously and return the scaled
166 * clock values in ns.
168 static int e1000e_phc_getcrosststamp(struct ptp_clock_info
*ptp
,
169 struct system_device_crosststamp
*xtstamp
)
171 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
174 return get_device_system_crosststamp(e1000e_phc_get_syncdevicetime
,
175 adapter
, NULL
, xtstamp
);
177 #endif/*CONFIG_E1000E_HWTS*/
180 * e1000e_phc_gettime - Reads the current time from the hardware clock
181 * @ptp: ptp clock structure
182 * @ts: timespec structure to hold the current time value
184 * Read the timecounter and return the correct value in ns after converting
185 * it into a struct timespec.
187 static int e1000e_phc_gettime(struct ptp_clock_info
*ptp
, struct timespec64
*ts
)
189 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
194 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
195 ns
= timecounter_read(&adapter
->tc
);
196 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
198 *ts
= ns_to_timespec64(ns
);
204 * e1000e_phc_settime - Set the current time on the hardware clock
205 * @ptp: ptp clock structure
206 * @ts: timespec containing the new time for the cycle counter
208 * Reset the timecounter to use a new base value instead of the kernel
211 static int e1000e_phc_settime(struct ptp_clock_info
*ptp
,
212 const struct timespec64
*ts
)
214 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
219 ns
= timespec64_to_ns(ts
);
221 /* reset the timecounter */
222 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
223 timecounter_init(&adapter
->tc
, &adapter
->cc
, ns
);
224 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
230 * e1000e_phc_enable - enable or disable an ancillary feature
231 * @ptp: ptp clock structure
232 * @request: Desired resource to enable or disable
233 * @on: Caller passes one to enable or zero to disable
235 * Enable (or disable) ancillary features of the PHC subsystem.
236 * Currently, no ancillary features are supported.
238 static int e1000e_phc_enable(struct ptp_clock_info __always_unused
*ptp
,
239 struct ptp_clock_request __always_unused
*request
,
240 int __always_unused on
)
245 static void e1000e_systim_overflow_work(struct work_struct
*work
)
247 struct e1000_adapter
*adapter
= container_of(work
, struct e1000_adapter
,
248 systim_overflow_work
.work
);
249 struct e1000_hw
*hw
= &adapter
->hw
;
250 struct timespec64 ts
;
252 adapter
->ptp_clock_info
.gettime64(&adapter
->ptp_clock_info
, &ts
);
254 e_dbg("SYSTIM overflow check at %lld.%09lu\n",
255 (long long) ts
.tv_sec
, ts
.tv_nsec
);
257 schedule_delayed_work(&adapter
->systim_overflow_work
,
258 E1000_SYSTIM_OVERFLOW_PERIOD
);
261 static const struct ptp_clock_info e1000e_ptp_clock_info
= {
262 .owner
= THIS_MODULE
,
268 .adjfreq
= e1000e_phc_adjfreq
,
269 .adjtime
= e1000e_phc_adjtime
,
270 .gettime64
= e1000e_phc_gettime
,
271 .settime64
= e1000e_phc_settime
,
272 .enable
= e1000e_phc_enable
,
276 * e1000e_ptp_init - initialize PTP for devices which support it
277 * @adapter: board private structure
279 * This function performs the required steps for enabling PTP support.
280 * If PTP support has already been loaded it simply calls the cyclecounter
281 * init routine and exits.
283 void e1000e_ptp_init(struct e1000_adapter
*adapter
)
285 struct e1000_hw
*hw
= &adapter
->hw
;
287 adapter
->ptp_clock
= NULL
;
289 if (!(adapter
->flags
& FLAG_HAS_HW_TIMESTAMP
))
292 adapter
->ptp_clock_info
= e1000e_ptp_clock_info
;
294 snprintf(adapter
->ptp_clock_info
.name
,
295 sizeof(adapter
->ptp_clock_info
.name
), "%pm",
296 adapter
->netdev
->perm_addr
);
298 switch (hw
->mac
.type
) {
302 if (((hw
->mac
.type
!= e1000_pch_lpt
) &&
303 (hw
->mac
.type
!= e1000_pch_spt
)) ||
304 (er32(TSYNCRXCTL
) & E1000_TSYNCRXCTL_SYSCFI
)) {
305 adapter
->ptp_clock_info
.max_adj
= 24000000 - 1;
311 adapter
->ptp_clock_info
.max_adj
= 600000000 - 1;
317 #ifdef CONFIG_E1000E_HWTS
318 /* CPU must have ART and GBe must be from Sunrise Point or greater */
319 if (hw
->mac
.type
>= e1000_pch_spt
&& boot_cpu_has(X86_FEATURE_ART
))
320 adapter
->ptp_clock_info
.getcrosststamp
=
321 e1000e_phc_getcrosststamp
;
322 #endif/*CONFIG_E1000E_HWTS*/
324 INIT_DELAYED_WORK(&adapter
->systim_overflow_work
,
325 e1000e_systim_overflow_work
);
327 schedule_delayed_work(&adapter
->systim_overflow_work
,
328 E1000_SYSTIM_OVERFLOW_PERIOD
);
330 adapter
->ptp_clock
= ptp_clock_register(&adapter
->ptp_clock_info
,
331 &adapter
->pdev
->dev
);
332 if (IS_ERR(adapter
->ptp_clock
)) {
333 adapter
->ptp_clock
= NULL
;
334 e_err("ptp_clock_register failed\n");
336 e_info("registered PHC clock\n");
341 * e1000e_ptp_remove - disable PTP device and stop the overflow check
342 * @adapter: board private structure
344 * Stop the PTP support, and cancel the delayed work.
346 void e1000e_ptp_remove(struct e1000_adapter
*adapter
)
348 if (!(adapter
->flags
& FLAG_HAS_HW_TIMESTAMP
))
351 cancel_delayed_work_sync(&adapter
->systim_overflow_work
);
353 if (adapter
->ptp_clock
) {
354 ptp_clock_unregister(adapter
->ptp_clock
);
355 adapter
->ptp_clock
= NULL
;
356 e_info("removed PHC\n");