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
->cycles
= sys_cycles
;
128 system
->cs_id
= CSID_X86_ART
;
134 * e1000e_phc_getcrosststamp - Reads the current system/device cross timestamp
135 * @ptp: ptp clock structure
136 * @xtstamp: structure containing timestamp
138 * Read device and system (ART) clock simultaneously and return the scaled
139 * clock values in ns.
141 static int e1000e_phc_getcrosststamp(struct ptp_clock_info
*ptp
,
142 struct system_device_crosststamp
*xtstamp
)
144 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
147 return get_device_system_crosststamp(e1000e_phc_get_syncdevicetime
,
148 adapter
, NULL
, xtstamp
);
150 #endif/*CONFIG_E1000E_HWTS*/
153 * e1000e_phc_gettimex - Reads the current time from the hardware clock and
155 * @ptp: ptp clock structure
156 * @ts: timespec structure to hold the current PHC time
157 * @sts: structure to hold the current system time
159 * Read the timecounter and return the correct value in ns after converting
160 * it into a struct timespec.
162 static int e1000e_phc_gettimex(struct ptp_clock_info
*ptp
,
163 struct timespec64
*ts
,
164 struct ptp_system_timestamp
*sts
)
166 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
171 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
173 /* NOTE: Non-monotonic SYSTIM readings may be returned */
174 cycles
= e1000e_read_systim(adapter
, sts
);
175 ns
= timecounter_cyc2time(&adapter
->tc
, cycles
);
177 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
179 *ts
= ns_to_timespec64(ns
);
185 * e1000e_phc_settime - Set the current time on the hardware clock
186 * @ptp: ptp clock structure
187 * @ts: timespec containing the new time for the cycle counter
189 * Reset the timecounter to use a new base value instead of the kernel
192 static int e1000e_phc_settime(struct ptp_clock_info
*ptp
,
193 const struct timespec64
*ts
)
195 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
200 ns
= timespec64_to_ns(ts
);
202 /* reset the timecounter */
203 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
204 timecounter_init(&adapter
->tc
, &adapter
->cc
, ns
);
205 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
211 * e1000e_phc_enable - enable or disable an ancillary feature
212 * @ptp: ptp clock structure
213 * @request: Desired resource to enable or disable
214 * @on: Caller passes one to enable or zero to disable
216 * Enable (or disable) ancillary features of the PHC subsystem.
217 * Currently, no ancillary features are supported.
219 static int e1000e_phc_enable(struct ptp_clock_info __always_unused
*ptp
,
220 struct ptp_clock_request __always_unused
*request
,
221 int __always_unused on
)
226 static void e1000e_systim_overflow_work(struct work_struct
*work
)
228 struct e1000_adapter
*adapter
= container_of(work
, struct e1000_adapter
,
229 systim_overflow_work
.work
);
230 struct e1000_hw
*hw
= &adapter
->hw
;
231 struct timespec64 ts
;
234 /* Update the timecounter */
235 ns
= timecounter_read(&adapter
->tc
);
237 ts
= ns_to_timespec64(ns
);
238 e_dbg("SYSTIM overflow check at %lld.%09lu\n",
239 (long long) ts
.tv_sec
, ts
.tv_nsec
);
241 schedule_delayed_work(&adapter
->systim_overflow_work
,
242 E1000_SYSTIM_OVERFLOW_PERIOD
);
245 static const struct ptp_clock_info e1000e_ptp_clock_info
= {
246 .owner
= THIS_MODULE
,
252 .adjfine
= e1000e_phc_adjfine
,
253 .adjtime
= e1000e_phc_adjtime
,
254 .gettimex64
= e1000e_phc_gettimex
,
255 .settime64
= e1000e_phc_settime
,
256 .enable
= e1000e_phc_enable
,
260 * e1000e_ptp_init - initialize PTP for devices which support it
261 * @adapter: board private structure
263 * This function performs the required steps for enabling PTP support.
264 * If PTP support has already been loaded it simply calls the cyclecounter
265 * init routine and exits.
267 void e1000e_ptp_init(struct e1000_adapter
*adapter
)
269 struct e1000_hw
*hw
= &adapter
->hw
;
271 adapter
->ptp_clock
= NULL
;
273 if (!(adapter
->flags
& FLAG_HAS_HW_TIMESTAMP
))
276 adapter
->ptp_clock_info
= e1000e_ptp_clock_info
;
278 snprintf(adapter
->ptp_clock_info
.name
,
279 sizeof(adapter
->ptp_clock_info
.name
), "%pm",
280 adapter
->netdev
->perm_addr
);
282 switch (hw
->mac
.type
) {
284 adapter
->ptp_clock_info
.max_adj
= MAX_PPB_96MHZ
;
287 if (er32(TSYNCRXCTL
) & E1000_TSYNCRXCTL_SYSCFI
)
288 adapter
->ptp_clock_info
.max_adj
= MAX_PPB_96MHZ
;
290 adapter
->ptp_clock_info
.max_adj
= MAX_PPB_25MHZ
;
293 adapter
->ptp_clock_info
.max_adj
= MAX_PPB_24MHZ
;
302 if (er32(TSYNCRXCTL
) & E1000_TSYNCRXCTL_SYSCFI
)
303 adapter
->ptp_clock_info
.max_adj
= MAX_PPB_24MHZ
;
305 adapter
->ptp_clock_info
.max_adj
= MAX_PPB_38400KHZ
;
309 adapter
->ptp_clock_info
.max_adj
= MAX_PPB_25MHZ
;
315 #ifdef CONFIG_E1000E_HWTS
316 /* CPU must have ART and GBe must be from Sunrise Point or greater */
317 if (hw
->mac
.type
>= e1000_pch_spt
&& boot_cpu_has(X86_FEATURE_ART
))
318 adapter
->ptp_clock_info
.getcrosststamp
=
319 e1000e_phc_getcrosststamp
;
320 #endif/*CONFIG_E1000E_HWTS*/
322 INIT_DELAYED_WORK(&adapter
->systim_overflow_work
,
323 e1000e_systim_overflow_work
);
325 schedule_delayed_work(&adapter
->systim_overflow_work
,
326 E1000_SYSTIM_OVERFLOW_PERIOD
);
328 adapter
->ptp_clock
= ptp_clock_register(&adapter
->ptp_clock_info
,
329 &adapter
->pdev
->dev
);
330 if (IS_ERR(adapter
->ptp_clock
)) {
331 adapter
->ptp_clock
= NULL
;
332 e_err("ptp_clock_register failed\n");
333 } else if (adapter
->ptp_clock
) {
334 e_info("registered PHC clock\n");
339 * e1000e_ptp_remove - disable PTP device and stop the overflow check
340 * @adapter: board private structure
342 * Stop the PTP support, and cancel the delayed work.
344 void e1000e_ptp_remove(struct e1000_adapter
*adapter
)
346 if (!(adapter
->flags
& FLAG_HAS_HW_TIMESTAMP
))
349 cancel_delayed_work_sync(&adapter
->systim_overflow_work
);
351 if (adapter
->ptp_clock
) {
352 ptp_clock_unregister(adapter
->ptp_clock
);
353 adapter
->ptp_clock
= NULL
;
354 e_info("removed PHC\n");