1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include "e1000.h"
35
36
37
38
39
40
41
42
43
44static 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
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
85
86
87
88
89
90static 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
108
109
110
111
112
113
114static 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
134
135
136
137
138
139
140static 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 = timespec_to_ns(ts);
149
150
151 spin_lock_irqsave(&adapter->systim_lock, flags);
152 timecounter_init(&adapter->tc, &adapter->cc, ns);
153 spin_unlock_irqrestore(&adapter->systim_lock, flags);
154
155 return 0;
156}
157
158
159
160
161
162
163
164
165
166
167static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
168 struct ptp_clock_request __always_unused *request,
169 int __always_unused on)
170{
171 return -EOPNOTSUPP;
172}
173
174static void e1000e_systim_overflow_work(struct work_struct *work)
175{
176 struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
177 systim_overflow_work.work);
178 struct e1000_hw *hw = &adapter->hw;
179 struct timespec ts;
180
181 adapter->ptp_clock_info.gettime(&adapter->ptp_clock_info, &ts);
182
183 e_dbg("SYSTIM overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
184
185 schedule_delayed_work(&adapter->systim_overflow_work,
186 E1000_SYSTIM_OVERFLOW_PERIOD);
187}
188
189static const struct ptp_clock_info e1000e_ptp_clock_info = {
190 .owner = THIS_MODULE,
191 .n_alarm = 0,
192 .n_ext_ts = 0,
193 .n_per_out = 0,
194 .pps = 0,
195 .adjfreq = e1000e_phc_adjfreq,
196 .adjtime = e1000e_phc_adjtime,
197 .gettime = e1000e_phc_gettime,
198 .settime = e1000e_phc_settime,
199 .enable = e1000e_phc_enable,
200};
201
202
203
204
205
206
207
208
209
210void e1000e_ptp_init(struct e1000_adapter *adapter)
211{
212 struct e1000_hw *hw = &adapter->hw;
213
214 adapter->ptp_clock = NULL;
215
216 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
217 return;
218
219 adapter->ptp_clock_info = e1000e_ptp_clock_info;
220
221 snprintf(adapter->ptp_clock_info.name,
222 sizeof(adapter->ptp_clock_info.name), "%pm",
223 adapter->netdev->perm_addr);
224
225 switch (hw->mac.type) {
226 case e1000_pch2lan:
227 case e1000_pch_lpt:
228 if ((hw->mac.type != e1000_pch_lpt) ||
229 (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
230 adapter->ptp_clock_info.max_adj = 24000000 - 1;
231 break;
232 }
233
234 case e1000_82574:
235 case e1000_82583:
236 adapter->ptp_clock_info.max_adj = 600000000 - 1;
237 break;
238 default:
239 break;
240 }
241
242 INIT_DELAYED_WORK(&adapter->systim_overflow_work,
243 e1000e_systim_overflow_work);
244
245 schedule_delayed_work(&adapter->systim_overflow_work,
246 E1000_SYSTIM_OVERFLOW_PERIOD);
247
248 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
249 &adapter->pdev->dev);
250 if (IS_ERR(adapter->ptp_clock)) {
251 adapter->ptp_clock = NULL;
252 e_err("ptp_clock_register failed\n");
253 } else {
254 e_info("registered PHC clock\n");
255 }
256}
257
258
259
260
261
262
263
264void e1000e_ptp_remove(struct e1000_adapter *adapter)
265{
266 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
267 return;
268
269 cancel_delayed_work_sync(&adapter->systim_overflow_work);
270
271 if (adapter->ptp_clock) {
272 ptp_clock_unregister(adapter->ptp_clock);
273 adapter->ptp_clock = NULL;
274 e_info("removed PHC\n");
275 }
276}
277