Commit | Line | Data |
---|---|---|
d89777bf BA |
1 | /******************************************************************************* |
2 | ||
3 | Intel PRO/1000 Linux driver | |
bf67044b | 4 | Copyright(c) 1999 - 2013 Intel Corporation. |
d89777bf BA |
5 | |
6 | This program is free software; you can redistribute it and/or modify it | |
7 | under the terms and conditions of the GNU General Public License, | |
8 | version 2, as published by the Free Software Foundation. | |
9 | ||
10 | This program is distributed in the hope it will be useful, but WITHOUT | |
11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License along with | |
16 | this program; if not, write to the Free Software Foundation, Inc., | |
17 | 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | ||
19 | The full GNU General Public License is included in this distribution in | |
20 | the file called "COPYING". | |
21 | ||
22 | Contact Information: | |
23 | Linux NICS <linux.nics@intel.com> | |
24 | e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> | |
25 | Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 | |
26 | ||
27 | *******************************************************************************/ | |
28 | ||
29 | /* PTP 1588 Hardware Clock (PHC) | |
30 | * Derived from PTP Hardware Clock driver for Intel 82576 and 82580 (igb) | |
31 | * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com> | |
32 | */ | |
33 | ||
34 | #include "e1000.h" | |
35 | ||
36 | /** | |
37 | * e1000e_phc_adjfreq - adjust the frequency of the hardware clock | |
38 | * @ptp: ptp clock structure | |
39 | * @delta: Desired frequency change in parts per billion | |
40 | * | |
41 | * Adjust the frequency of the PHC cycle counter by the indicated delta from | |
42 | * the base frequency. | |
43 | **/ | |
44 | static int e1000e_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta) | |
45 | { | |
46 | struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter, | |
47 | ptp_clock_info); | |
48 | struct e1000_hw *hw = &adapter->hw; | |
49 | bool neg_adj = false; | |
50 | u64 adjustment; | |
51 | u32 timinca, incvalue; | |
52 | s32 ret_val; | |
53 | ||
54 | if ((delta > ptp->max_adj) || (delta <= -1000000000)) | |
55 | return -EINVAL; | |
56 | ||
57 | if (delta < 0) { | |
58 | neg_adj = true; | |
59 | delta = -delta; | |
60 | } | |
61 | ||
62 | /* Get the System Time Register SYSTIM base frequency */ | |
63 | ret_val = e1000e_get_base_timinca(adapter, &timinca); | |
64 | if (ret_val) | |
65 | return ret_val; | |
66 | ||
67 | incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK; | |
68 | ||
69 | adjustment = incvalue; | |
70 | adjustment *= delta; | |
71 | adjustment = div_u64(adjustment, 1000000000); | |
72 | ||
73 | incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment); | |
74 | ||
75 | timinca &= ~E1000_TIMINCA_INCVALUE_MASK; | |
76 | timinca |= incvalue; | |
77 | ||
78 | ew32(TIMINCA, timinca); | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | /** | |
84 | * e1000e_phc_adjtime - Shift the time of the hardware clock | |
85 | * @ptp: ptp clock structure | |
86 | * @delta: Desired change in nanoseconds | |
87 | * | |
88 | * Adjust the timer by resetting the timecounter structure. | |
89 | **/ | |
90 | static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta) | |
91 | { | |
92 | struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter, | |
93 | ptp_clock_info); | |
94 | unsigned long flags; | |
95 | s64 now; | |
96 | ||
97 | spin_lock_irqsave(&adapter->systim_lock, flags); | |
98 | now = timecounter_read(&adapter->tc); | |
99 | now += delta; | |
100 | timecounter_init(&adapter->tc, &adapter->cc, now); | |
101 | spin_unlock_irqrestore(&adapter->systim_lock, flags); | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | /** | |
107 | * e1000e_phc_gettime - Reads the current time from the hardware clock | |
108 | * @ptp: ptp clock structure | |
109 | * @ts: timespec structure to hold the current time value | |
110 | * | |
111 | * Read the timecounter and return the correct value in ns after converting | |
112 | * it into a struct timespec. | |
113 | **/ | |
114 | static int e1000e_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts) | |
115 | { | |
116 | struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter, | |
117 | ptp_clock_info); | |
118 | unsigned long flags; | |
119 | u32 remainder; | |
120 | u64 ns; | |
121 | ||
122 | spin_lock_irqsave(&adapter->systim_lock, flags); | |
123 | ns = timecounter_read(&adapter->tc); | |
124 | spin_unlock_irqrestore(&adapter->systim_lock, flags); | |
125 | ||
126 | ts->tv_sec = div_u64_rem(ns, NSEC_PER_SEC, &remainder); | |
127 | ts->tv_nsec = remainder; | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
132 | /** | |
133 | * e1000e_phc_settime - Set the current time on the hardware clock | |
134 | * @ptp: ptp clock structure | |
135 | * @ts: timespec containing the new time for the cycle counter | |
136 | * | |
137 | * Reset the timecounter to use a new base value instead of the kernel | |
138 | * wall timer value. | |
139 | **/ | |
140 | static int e1000e_phc_settime(struct ptp_clock_info *ptp, | |
141 | const struct timespec *ts) | |
142 | { | |
143 | struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter, | |
144 | ptp_clock_info); | |
145 | unsigned long flags; | |
146 | u64 ns; | |
147 | ||
148 | ns = ts->tv_sec * NSEC_PER_SEC; | |
149 | ns += ts->tv_nsec; | |
150 | ||
151 | /* reset the timecounter */ | |
152 | spin_lock_irqsave(&adapter->systim_lock, flags); | |
153 | timecounter_init(&adapter->tc, &adapter->cc, ns); | |
154 | spin_unlock_irqrestore(&adapter->systim_lock, flags); | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | /** | |
160 | * e1000e_phc_enable - enable or disable an ancillary feature | |
161 | * @ptp: ptp clock structure | |
162 | * @request: Desired resource to enable or disable | |
163 | * @on: Caller passes one to enable or zero to disable | |
164 | * | |
165 | * Enable (or disable) ancillary features of the PHC subsystem. | |
166 | * Currently, no ancillary features are supported. | |
167 | **/ | |
8bb62869 BA |
168 | static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp, |
169 | struct ptp_clock_request __always_unused *request, | |
170 | int __always_unused on) | |
d89777bf BA |
171 | { |
172 | return -EOPNOTSUPP; | |
173 | } | |
174 | ||
175 | static void e1000e_systim_overflow_work(struct work_struct *work) | |
176 | { | |
177 | struct e1000_adapter *adapter = container_of(work, struct e1000_adapter, | |
178 | systim_overflow_work.work); | |
179 | struct e1000_hw *hw = &adapter->hw; | |
180 | struct timespec ts; | |
181 | ||
182 | adapter->ptp_clock_info.gettime(&adapter->ptp_clock_info, &ts); | |
183 | ||
184 | e_dbg("SYSTIM overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec); | |
185 | ||
186 | schedule_delayed_work(&adapter->systim_overflow_work, | |
187 | E1000_SYSTIM_OVERFLOW_PERIOD); | |
188 | } | |
189 | ||
190 | static const struct ptp_clock_info e1000e_ptp_clock_info = { | |
191 | .owner = THIS_MODULE, | |
192 | .n_alarm = 0, | |
193 | .n_ext_ts = 0, | |
194 | .n_per_out = 0, | |
195 | .pps = 0, | |
196 | .adjfreq = e1000e_phc_adjfreq, | |
197 | .adjtime = e1000e_phc_adjtime, | |
198 | .gettime = e1000e_phc_gettime, | |
199 | .settime = e1000e_phc_settime, | |
200 | .enable = e1000e_phc_enable, | |
201 | }; | |
202 | ||
203 | /** | |
204 | * e1000e_ptp_init - initialize PTP for devices which support it | |
205 | * @adapter: board private structure | |
206 | * | |
207 | * This function performs the required steps for enabling PTP support. | |
208 | * If PTP support has already been loaded it simply calls the cyclecounter | |
209 | * init routine and exits. | |
210 | **/ | |
211 | void e1000e_ptp_init(struct e1000_adapter *adapter) | |
212 | { | |
213 | struct e1000_hw *hw = &adapter->hw; | |
214 | ||
215 | adapter->ptp_clock = NULL; | |
216 | ||
217 | if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP)) | |
218 | return; | |
219 | ||
220 | adapter->ptp_clock_info = e1000e_ptp_clock_info; | |
221 | ||
222 | snprintf(adapter->ptp_clock_info.name, | |
223 | sizeof(adapter->ptp_clock_info.name), "%pm", | |
224 | adapter->netdev->perm_addr); | |
225 | ||
226 | switch (hw->mac.type) { | |
227 | case e1000_pch2lan: | |
228 | case e1000_pch_lpt: | |
229 | if ((hw->mac.type != e1000_pch_lpt) || | |
230 | (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) { | |
231 | adapter->ptp_clock_info.max_adj = 24000000 - 1; | |
232 | break; | |
233 | } | |
234 | /* fall-through */ | |
235 | case e1000_82574: | |
236 | case e1000_82583: | |
237 | adapter->ptp_clock_info.max_adj = 600000000 - 1; | |
238 | break; | |
239 | default: | |
240 | break; | |
241 | } | |
242 | ||
243 | INIT_DELAYED_WORK(&adapter->systim_overflow_work, | |
244 | e1000e_systim_overflow_work); | |
245 | ||
246 | schedule_delayed_work(&adapter->systim_overflow_work, | |
247 | E1000_SYSTIM_OVERFLOW_PERIOD); | |
248 | ||
249 | adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info, | |
250 | &adapter->pdev->dev); | |
251 | if (IS_ERR(adapter->ptp_clock)) { | |
252 | adapter->ptp_clock = NULL; | |
253 | e_err("ptp_clock_register failed\n"); | |
254 | } else { | |
255 | e_info("registered PHC clock\n"); | |
256 | } | |
257 | } | |
258 | ||
259 | /** | |
260 | * e1000e_ptp_remove - disable PTP device and stop the overflow check | |
261 | * @adapter: board private structure | |
262 | * | |
263 | * Stop the PTP support, and cancel the delayed work. | |
264 | **/ | |
265 | void e1000e_ptp_remove(struct e1000_adapter *adapter) | |
266 | { | |
267 | if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP)) | |
268 | return; | |
269 | ||
270 | cancel_delayed_work_sync(&adapter->systim_overflow_work); | |
271 | ||
272 | if (adapter->ptp_clock) { | |
273 | ptp_clock_unregister(adapter->ptp_clock); | |
274 | adapter->ptp_clock = NULL; | |
275 | e_info("removed PHC\n"); | |
276 | } | |
277 | } |