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>
30 * e1000e_phc_adjfreq - adjust the frequency of the hardware clock
31 * @ptp: ptp clock structure
32 * @delta: Desired frequency change in parts per billion
34 * Adjust the frequency of the PHC cycle counter by the indicated delta from
37 static int e1000e_phc_adjfreq(struct ptp_clock_info
*ptp
, s32 delta
)
39 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
41 struct e1000_hw
*hw
= &adapter
->hw
;
45 u32 timinca
, incvalue
;
48 if ((delta
> ptp
->max_adj
) || (delta
<= -1000000000))
56 /* Get the System Time Register SYSTIM base frequency */
57 ret_val
= e1000e_get_base_timinca(adapter
, &timinca
);
61 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
63 incvalue
= timinca
& E1000_TIMINCA_INCVALUE_MASK
;
65 adjustment
= incvalue
;
67 adjustment
= div_u64(adjustment
, 1000000000);
69 incvalue
= neg_adj
? (incvalue
- adjustment
) : (incvalue
+ adjustment
);
71 timinca
&= ~E1000_TIMINCA_INCVALUE_MASK
;
74 ew32(TIMINCA
, timinca
);
76 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
82 * e1000e_phc_adjtime - Shift the time of the hardware clock
83 * @ptp: ptp clock structure
84 * @delta: Desired change in nanoseconds
86 * Adjust the timer by resetting the timecounter structure.
88 static int e1000e_phc_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
90 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
94 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
95 timecounter_adjtime(&adapter
->tc
, delta
);
96 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
102 * e1000e_phc_gettime - Reads the current time from the hardware clock
103 * @ptp: ptp clock structure
104 * @ts: timespec structure to hold the current time value
106 * Read the timecounter and return the correct value in ns after converting
107 * it into a struct timespec.
109 static int e1000e_phc_gettime(struct ptp_clock_info
*ptp
, struct timespec64
*ts
)
111 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
116 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
117 ns
= timecounter_read(&adapter
->tc
);
118 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
120 *ts
= ns_to_timespec64(ns
);
126 * e1000e_phc_settime - Set the current time on the hardware clock
127 * @ptp: ptp clock structure
128 * @ts: timespec containing the new time for the cycle counter
130 * Reset the timecounter to use a new base value instead of the kernel
133 static int e1000e_phc_settime(struct ptp_clock_info
*ptp
,
134 const struct timespec64
*ts
)
136 struct e1000_adapter
*adapter
= container_of(ptp
, struct e1000_adapter
,
141 ns
= timespec64_to_ns(ts
);
143 /* reset the timecounter */
144 spin_lock_irqsave(&adapter
->systim_lock
, flags
);
145 timecounter_init(&adapter
->tc
, &adapter
->cc
, ns
);
146 spin_unlock_irqrestore(&adapter
->systim_lock
, flags
);
152 * e1000e_phc_enable - enable or disable an ancillary feature
153 * @ptp: ptp clock structure
154 * @request: Desired resource to enable or disable
155 * @on: Caller passes one to enable or zero to disable
157 * Enable (or disable) ancillary features of the PHC subsystem.
158 * Currently, no ancillary features are supported.
160 static int e1000e_phc_enable(struct ptp_clock_info __always_unused
*ptp
,
161 struct ptp_clock_request __always_unused
*request
,
162 int __always_unused on
)
167 static void e1000e_systim_overflow_work(struct work_struct
*work
)
169 struct e1000_adapter
*adapter
= container_of(work
, struct e1000_adapter
,
170 systim_overflow_work
.work
);
171 struct e1000_hw
*hw
= &adapter
->hw
;
172 struct timespec64 ts
;
174 adapter
->ptp_clock_info
.gettime64(&adapter
->ptp_clock_info
, &ts
);
176 e_dbg("SYSTIM overflow check at %lld.%09lu\n",
177 (long long) ts
.tv_sec
, ts
.tv_nsec
);
179 schedule_delayed_work(&adapter
->systim_overflow_work
,
180 E1000_SYSTIM_OVERFLOW_PERIOD
);
183 static const struct ptp_clock_info e1000e_ptp_clock_info
= {
184 .owner
= THIS_MODULE
,
190 .adjfreq
= e1000e_phc_adjfreq
,
191 .adjtime
= e1000e_phc_adjtime
,
192 .gettime64
= e1000e_phc_gettime
,
193 .settime64
= e1000e_phc_settime
,
194 .enable
= e1000e_phc_enable
,
198 * e1000e_ptp_init - initialize PTP for devices which support it
199 * @adapter: board private structure
201 * This function performs the required steps for enabling PTP support.
202 * If PTP support has already been loaded it simply calls the cyclecounter
203 * init routine and exits.
205 void e1000e_ptp_init(struct e1000_adapter
*adapter
)
207 struct e1000_hw
*hw
= &adapter
->hw
;
209 adapter
->ptp_clock
= NULL
;
211 if (!(adapter
->flags
& FLAG_HAS_HW_TIMESTAMP
))
214 adapter
->ptp_clock_info
= e1000e_ptp_clock_info
;
216 snprintf(adapter
->ptp_clock_info
.name
,
217 sizeof(adapter
->ptp_clock_info
.name
), "%pm",
218 adapter
->netdev
->perm_addr
);
220 switch (hw
->mac
.type
) {
224 if (((hw
->mac
.type
!= e1000_pch_lpt
) &&
225 (hw
->mac
.type
!= e1000_pch_spt
)) ||
226 (er32(TSYNCRXCTL
) & E1000_TSYNCRXCTL_SYSCFI
)) {
227 adapter
->ptp_clock_info
.max_adj
= 24000000 - 1;
233 adapter
->ptp_clock_info
.max_adj
= 600000000 - 1;
239 INIT_DELAYED_WORK(&adapter
->systim_overflow_work
,
240 e1000e_systim_overflow_work
);
242 schedule_delayed_work(&adapter
->systim_overflow_work
,
243 E1000_SYSTIM_OVERFLOW_PERIOD
);
245 adapter
->ptp_clock
= ptp_clock_register(&adapter
->ptp_clock_info
,
246 &adapter
->pdev
->dev
);
247 if (IS_ERR(adapter
->ptp_clock
)) {
248 adapter
->ptp_clock
= NULL
;
249 e_err("ptp_clock_register failed\n");
251 e_info("registered PHC clock\n");
256 * e1000e_ptp_remove - disable PTP device and stop the overflow check
257 * @adapter: board private structure
259 * Stop the PTP support, and cancel the delayed work.
261 void e1000e_ptp_remove(struct e1000_adapter
*adapter
)
263 if (!(adapter
->flags
& FLAG_HAS_HW_TIMESTAMP
))
266 cancel_delayed_work_sync(&adapter
->systim_overflow_work
);
268 if (adapter
->ptp_clock
) {
269 ptp_clock_unregister(adapter
->ptp_clock
);
270 adapter
->ptp_clock
= NULL
;
271 e_info("removed PHC\n");