blob: 3bd79a3ff8295f306ebdd22ffee782de858389a5 [file] [log] [blame]
David Ertmane78b80b2014-02-04 01:56:06 +00001/* Intel PRO/1000 Linux driver
2 * Copyright(c) 1999 - 2014 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 */
Bruce Alland89777b2013-01-19 01:09:58 +000021
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 **/
37static 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;
Todd Fujinaka6c2ed392014-01-18 06:09:33 +000043 unsigned long flags;
Bruce Alland89777b2013-01-19 01:09:58 +000044 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
Todd Fujinaka6c2ed392014-01-18 06:09:33 +000061 spin_lock_irqsave(&adapter->systim_lock, flags);
62
Bruce Alland89777b2013-01-19 01:09:58 +000063 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
Todd Fujinaka6c2ed392014-01-18 06:09:33 +000076 spin_unlock_irqrestore(&adapter->systim_lock, flags);
77
Bruce Alland89777b2013-01-19 01:09:58 +000078 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 **/
88static 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 s64 now;
94
95 spin_lock_irqsave(&adapter->systim_lock, flags);
96 now = timecounter_read(&adapter->tc);
97 now += delta;
98 timecounter_init(&adapter->tc, &adapter->cc, now);
99 spin_unlock_irqrestore(&adapter->systim_lock, flags);
100
101 return 0;
102}
103
104/**
105 * e1000e_phc_gettime - Reads the current time from the hardware clock
106 * @ptp: ptp clock structure
107 * @ts: timespec structure to hold the current time value
108 *
109 * Read the timecounter and return the correct value in ns after converting
110 * it into a struct timespec.
111 **/
112static int e1000e_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
113{
114 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
115 ptp_clock_info);
116 unsigned long flags;
117 u32 remainder;
118 u64 ns;
119
120 spin_lock_irqsave(&adapter->systim_lock, flags);
121 ns = timecounter_read(&adapter->tc);
122 spin_unlock_irqrestore(&adapter->systim_lock, flags);
123
124 ts->tv_sec = div_u64_rem(ns, NSEC_PER_SEC, &remainder);
125 ts->tv_nsec = remainder;
126
127 return 0;
128}
129
130/**
131 * e1000e_phc_settime - Set the current time on the hardware clock
132 * @ptp: ptp clock structure
133 * @ts: timespec containing the new time for the cycle counter
134 *
135 * Reset the timecounter to use a new base value instead of the kernel
136 * wall timer value.
137 **/
138static int e1000e_phc_settime(struct ptp_clock_info *ptp,
139 const struct timespec *ts)
140{
141 struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
142 ptp_clock_info);
143 unsigned long flags;
144 u64 ns;
145
Richard Cochran73e3dd62013-04-23 01:56:34 +0000146 ns = timespec_to_ns(ts);
Bruce Alland89777b2013-01-19 01:09:58 +0000147
148 /* reset the timecounter */
149 spin_lock_irqsave(&adapter->systim_lock, flags);
150 timecounter_init(&adapter->tc, &adapter->cc, ns);
151 spin_unlock_irqrestore(&adapter->systim_lock, flags);
152
153 return 0;
154}
155
156/**
157 * e1000e_phc_enable - enable or disable an ancillary feature
158 * @ptp: ptp clock structure
159 * @request: Desired resource to enable or disable
160 * @on: Caller passes one to enable or zero to disable
161 *
162 * Enable (or disable) ancillary features of the PHC subsystem.
163 * Currently, no ancillary features are supported.
164 **/
Bruce Allan8bb62862013-01-16 08:46:49 +0000165static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
166 struct ptp_clock_request __always_unused *request,
167 int __always_unused on)
Bruce Alland89777b2013-01-19 01:09:58 +0000168{
169 return -EOPNOTSUPP;
170}
171
172static void e1000e_systim_overflow_work(struct work_struct *work)
173{
174 struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
175 systim_overflow_work.work);
176 struct e1000_hw *hw = &adapter->hw;
177 struct timespec ts;
178
179 adapter->ptp_clock_info.gettime(&adapter->ptp_clock_info, &ts);
180
181 e_dbg("SYSTIM overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
182
183 schedule_delayed_work(&adapter->systim_overflow_work,
184 E1000_SYSTIM_OVERFLOW_PERIOD);
185}
186
187static const struct ptp_clock_info e1000e_ptp_clock_info = {
188 .owner = THIS_MODULE,
189 .n_alarm = 0,
190 .n_ext_ts = 0,
191 .n_per_out = 0,
192 .pps = 0,
193 .adjfreq = e1000e_phc_adjfreq,
194 .adjtime = e1000e_phc_adjtime,
195 .gettime = e1000e_phc_gettime,
196 .settime = e1000e_phc_settime,
197 .enable = e1000e_phc_enable,
198};
199
200/**
201 * e1000e_ptp_init - initialize PTP for devices which support it
202 * @adapter: board private structure
203 *
204 * This function performs the required steps for enabling PTP support.
205 * If PTP support has already been loaded it simply calls the cyclecounter
206 * init routine and exits.
207 **/
208void e1000e_ptp_init(struct e1000_adapter *adapter)
209{
210 struct e1000_hw *hw = &adapter->hw;
211
212 adapter->ptp_clock = NULL;
213
214 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
215 return;
216
217 adapter->ptp_clock_info = e1000e_ptp_clock_info;
218
219 snprintf(adapter->ptp_clock_info.name,
220 sizeof(adapter->ptp_clock_info.name), "%pm",
221 adapter->netdev->perm_addr);
222
223 switch (hw->mac.type) {
224 case e1000_pch2lan:
225 case e1000_pch_lpt:
226 if ((hw->mac.type != e1000_pch_lpt) ||
227 (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
228 adapter->ptp_clock_info.max_adj = 24000000 - 1;
229 break;
230 }
231 /* fall-through */
232 case e1000_82574:
233 case e1000_82583:
234 adapter->ptp_clock_info.max_adj = 600000000 - 1;
235 break;
236 default:
237 break;
238 }
239
240 INIT_DELAYED_WORK(&adapter->systim_overflow_work,
241 e1000e_systim_overflow_work);
242
243 schedule_delayed_work(&adapter->systim_overflow_work,
244 E1000_SYSTIM_OVERFLOW_PERIOD);
245
246 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
247 &adapter->pdev->dev);
248 if (IS_ERR(adapter->ptp_clock)) {
249 adapter->ptp_clock = NULL;
250 e_err("ptp_clock_register failed\n");
251 } else {
252 e_info("registered PHC clock\n");
253 }
254}
255
256/**
257 * e1000e_ptp_remove - disable PTP device and stop the overflow check
258 * @adapter: board private structure
259 *
260 * Stop the PTP support, and cancel the delayed work.
261 **/
262void e1000e_ptp_remove(struct e1000_adapter *adapter)
263{
264 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
265 return;
266
267 cancel_delayed_work_sync(&adapter->systim_overflow_work);
268
269 if (adapter->ptp_clock) {
270 ptp_clock_unregister(adapter->ptp_clock);
271 adapter->ptp_clock = NULL;
272 e_info("removed PHC\n");
273 }
274}