GNU Linux-libre 4.4.299-gnu1
[releases.git] / drivers / net / ethernet / intel / e1000e / ptp.c
1 /* Intel PRO/1000 Linux driver
2  * Copyright(c) 1999 - 2015 Intel Corporation.
3  *
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.
7  *
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
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
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
20  */
21
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>
25  */
26
27 #include "e1000.h"
28
29 /**
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
33  *
34  * Adjust the frequency of the PHC cycle counter by the indicated delta from
35  * the base frequency.
36  **/
37 static int e1000e_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
38 {
39         struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
40                                                      ptp_clock_info);
41         struct e1000_hw *hw = &adapter->hw;
42         bool neg_adj = false;
43         unsigned long flags;
44         u64 adjustment;
45         u32 timinca, incvalue;
46         s32 ret_val;
47
48         if ((delta > ptp->max_adj) || (delta <= -1000000000))
49                 return -EINVAL;
50
51         if (delta < 0) {
52                 neg_adj = true;
53                 delta = -delta;
54         }
55
56         /* Get the System Time Register SYSTIM base frequency */
57         ret_val = e1000e_get_base_timinca(adapter, &timinca);
58         if (ret_val)
59                 return ret_val;
60
61         spin_lock_irqsave(&adapter->systim_lock, flags);
62
63         incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
64
65         adjustment = incvalue;
66         adjustment *= delta;
67         adjustment = div_u64(adjustment, 1000000000);
68
69         incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
70
71         timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
72         timinca |= incvalue;
73
74         ew32(TIMINCA, timinca);
75
76         spin_unlock_irqrestore(&adapter->systim_lock, flags);
77
78         return 0;
79 }
80
81 /**
82  * e1000e_phc_adjtime - Shift the time of the hardware clock
83  * @ptp: ptp clock structure
84  * @delta: Desired change in nanoseconds
85  *
86  * Adjust the timer by resetting the timecounter structure.
87  **/
88 static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
89 {
90         struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
91                                                      ptp_clock_info);
92         unsigned long flags;
93
94         spin_lock_irqsave(&adapter->systim_lock, flags);
95         timecounter_adjtime(&adapter->tc, delta);
96         spin_unlock_irqrestore(&adapter->systim_lock, flags);
97
98         return 0;
99 }
100
101 /**
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
105  *
106  * Read the timecounter and return the correct value in ns after converting
107  * it into a struct timespec.
108  **/
109 static int e1000e_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
110 {
111         struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
112                                                      ptp_clock_info);
113         unsigned long flags;
114         u64 cycles, ns;
115
116         spin_lock_irqsave(&adapter->systim_lock, flags);
117
118         /* Use timecounter_cyc2time() to allow non-monotonic SYSTIM readings */
119         cycles = adapter->cc.read(&adapter->cc);
120         ns = timecounter_cyc2time(&adapter->tc, cycles);
121
122         spin_unlock_irqrestore(&adapter->systim_lock, flags);
123
124         *ts = ns_to_timespec64(ns);
125
126         return 0;
127 }
128
129 /**
130  * e1000e_phc_settime - Set the current time on the hardware clock
131  * @ptp: ptp clock structure
132  * @ts: timespec containing the new time for the cycle counter
133  *
134  * Reset the timecounter to use a new base value instead of the kernel
135  * wall timer value.
136  **/
137 static int e1000e_phc_settime(struct ptp_clock_info *ptp,
138                               const struct timespec64 *ts)
139 {
140         struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
141                                                      ptp_clock_info);
142         unsigned long flags;
143         u64 ns;
144
145         ns = timespec64_to_ns(ts);
146
147         /* reset the timecounter */
148         spin_lock_irqsave(&adapter->systim_lock, flags);
149         timecounter_init(&adapter->tc, &adapter->cc, ns);
150         spin_unlock_irqrestore(&adapter->systim_lock, flags);
151
152         return 0;
153 }
154
155 /**
156  * e1000e_phc_enable - enable or disable an ancillary feature
157  * @ptp: ptp clock structure
158  * @request: Desired resource to enable or disable
159  * @on: Caller passes one to enable or zero to disable
160  *
161  * Enable (or disable) ancillary features of the PHC subsystem.
162  * Currently, no ancillary features are supported.
163  **/
164 static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
165                              struct ptp_clock_request __always_unused *request,
166                              int __always_unused on)
167 {
168         return -EOPNOTSUPP;
169 }
170
171 static void e1000e_systim_overflow_work(struct work_struct *work)
172 {
173         struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
174                                                      systim_overflow_work.work);
175         struct e1000_hw *hw = &adapter->hw;
176         struct timespec64 ts;
177         u64 ns;
178
179         /* Update the timecounter */
180         ns = timecounter_read(&adapter->tc);
181
182         ts = ns_to_timespec64(ns);
183         e_dbg("SYSTIM overflow check at %lld.%09lu\n",
184               (long long) 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         .n_pins         = 0,
196         .pps            = 0,
197         .adjfreq        = e1000e_phc_adjfreq,
198         .adjtime        = e1000e_phc_adjtime,
199         .gettime64      = e1000e_phc_gettime,
200         .settime64      = e1000e_phc_settime,
201         .enable         = e1000e_phc_enable,
202 };
203
204 /**
205  * e1000e_ptp_init - initialize PTP for devices which support it
206  * @adapter: board private structure
207  *
208  * This function performs the required steps for enabling PTP support.
209  * If PTP support has already been loaded it simply calls the cyclecounter
210  * init routine and exits.
211  **/
212 void e1000e_ptp_init(struct e1000_adapter *adapter)
213 {
214         struct e1000_hw *hw = &adapter->hw;
215
216         adapter->ptp_clock = NULL;
217
218         if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
219                 return;
220
221         adapter->ptp_clock_info = e1000e_ptp_clock_info;
222
223         snprintf(adapter->ptp_clock_info.name,
224                  sizeof(adapter->ptp_clock_info.name), "%pm",
225                  adapter->netdev->perm_addr);
226
227         switch (hw->mac.type) {
228         case e1000_pch2lan:
229         case e1000_pch_lpt:
230         case e1000_pch_spt:
231                 if (((hw->mac.type != e1000_pch_lpt) &&
232                      (hw->mac.type != e1000_pch_spt)) ||
233                     (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
234                         adapter->ptp_clock_info.max_adj = 24000000 - 1;
235                         break;
236                 }
237                 /* fall-through */
238         case e1000_82574:
239         case e1000_82583:
240                 adapter->ptp_clock_info.max_adj = 600000000 - 1;
241                 break;
242         default:
243                 break;
244         }
245
246         INIT_DELAYED_WORK(&adapter->systim_overflow_work,
247                           e1000e_systim_overflow_work);
248
249         schedule_delayed_work(&adapter->systim_overflow_work,
250                               E1000_SYSTIM_OVERFLOW_PERIOD);
251
252         adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
253                                                 &adapter->pdev->dev);
254         if (IS_ERR(adapter->ptp_clock)) {
255                 adapter->ptp_clock = NULL;
256                 e_err("ptp_clock_register failed\n");
257         } else {
258                 e_info("registered PHC clock\n");
259         }
260 }
261
262 /**
263  * e1000e_ptp_remove - disable PTP device and stop the overflow check
264  * @adapter: board private structure
265  *
266  * Stop the PTP support, and cancel the delayed work.
267  **/
268 void e1000e_ptp_remove(struct e1000_adapter *adapter)
269 {
270         if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
271                 return;
272
273         cancel_delayed_work_sync(&adapter->systim_overflow_work);
274
275         if (adapter->ptp_clock) {
276                 ptp_clock_unregister(adapter->ptp_clock);
277                 adapter->ptp_clock = NULL;
278                 e_info("removed PHC\n");
279         }
280 }