xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/amd/xgbe/xgbe-ptp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * AMD 10Gb Ethernet driver
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is available to you under your choice of the following two
5*4882a593Smuzhiyun  * licenses:
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * License 1: GPLv2
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (c) 2014 Advanced Micro Devices, Inc.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This file is free software; you may copy, redistribute and/or modify
12*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
13*4882a593Smuzhiyun  * the Free Software Foundation, either version 2 of the License, or (at
14*4882a593Smuzhiyun  * your option) any later version.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * This file is distributed in the hope that it will be useful, but
17*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
18*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19*4882a593Smuzhiyun  * General Public License for more details.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
22*4882a593Smuzhiyun  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * This file incorporates work covered by the following copyright and
25*4882a593Smuzhiyun  * permission notice:
26*4882a593Smuzhiyun  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
27*4882a593Smuzhiyun  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28*4882a593Smuzhiyun  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
29*4882a593Smuzhiyun  *     and you.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  *     The Software IS NOT an item of Licensed Software or Licensed Product
32*4882a593Smuzhiyun  *     under any End User Software License Agreement or Agreement for Licensed
33*4882a593Smuzhiyun  *     Product with Synopsys or any supplement thereto.  Permission is hereby
34*4882a593Smuzhiyun  *     granted, free of charge, to any person obtaining a copy of this software
35*4882a593Smuzhiyun  *     annotated with this license and the Software, to deal in the Software
36*4882a593Smuzhiyun  *     without restriction, including without limitation the rights to use,
37*4882a593Smuzhiyun  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38*4882a593Smuzhiyun  *     of the Software, and to permit persons to whom the Software is furnished
39*4882a593Smuzhiyun  *     to do so, subject to the following conditions:
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  *     The above copyright notice and this permission notice shall be included
42*4882a593Smuzhiyun  *     in all copies or substantial portions of the Software.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45*4882a593Smuzhiyun  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46*4882a593Smuzhiyun  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47*4882a593Smuzhiyun  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48*4882a593Smuzhiyun  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49*4882a593Smuzhiyun  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50*4882a593Smuzhiyun  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51*4882a593Smuzhiyun  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52*4882a593Smuzhiyun  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53*4882a593Smuzhiyun  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54*4882a593Smuzhiyun  *     THE POSSIBILITY OF SUCH DAMAGE.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * License 2: Modified BSD
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Copyright (c) 2014 Advanced Micro Devices, Inc.
60*4882a593Smuzhiyun  * All rights reserved.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
63*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions are met:
64*4882a593Smuzhiyun  *     * Redistributions of source code must retain the above copyright
65*4882a593Smuzhiyun  *       notice, this list of conditions and the following disclaimer.
66*4882a593Smuzhiyun  *     * Redistributions in binary form must reproduce the above copyright
67*4882a593Smuzhiyun  *       notice, this list of conditions and the following disclaimer in the
68*4882a593Smuzhiyun  *       documentation and/or other materials provided with the distribution.
69*4882a593Smuzhiyun  *     * Neither the name of Advanced Micro Devices, Inc. nor the
70*4882a593Smuzhiyun  *       names of its contributors may be used to endorse or promote products
71*4882a593Smuzhiyun  *       derived from this software without specific prior written permission.
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74*4882a593Smuzhiyun  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76*4882a593Smuzhiyun  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77*4882a593Smuzhiyun  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78*4882a593Smuzhiyun  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79*4882a593Smuzhiyun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80*4882a593Smuzhiyun  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82*4882a593Smuzhiyun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * This file incorporates work covered by the following copyright and
85*4882a593Smuzhiyun  * permission notice:
86*4882a593Smuzhiyun  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
87*4882a593Smuzhiyun  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88*4882a593Smuzhiyun  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
89*4882a593Smuzhiyun  *     and you.
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  *     The Software IS NOT an item of Licensed Software or Licensed Product
92*4882a593Smuzhiyun  *     under any End User Software License Agreement or Agreement for Licensed
93*4882a593Smuzhiyun  *     Product with Synopsys or any supplement thereto.  Permission is hereby
94*4882a593Smuzhiyun  *     granted, free of charge, to any person obtaining a copy of this software
95*4882a593Smuzhiyun  *     annotated with this license and the Software, to deal in the Software
96*4882a593Smuzhiyun  *     without restriction, including without limitation the rights to use,
97*4882a593Smuzhiyun  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98*4882a593Smuzhiyun  *     of the Software, and to permit persons to whom the Software is furnished
99*4882a593Smuzhiyun  *     to do so, subject to the following conditions:
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  *     The above copyright notice and this permission notice shall be included
102*4882a593Smuzhiyun  *     in all copies or substantial portions of the Software.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105*4882a593Smuzhiyun  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106*4882a593Smuzhiyun  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107*4882a593Smuzhiyun  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108*4882a593Smuzhiyun  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109*4882a593Smuzhiyun  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110*4882a593Smuzhiyun  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111*4882a593Smuzhiyun  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112*4882a593Smuzhiyun  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113*4882a593Smuzhiyun  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114*4882a593Smuzhiyun  *     THE POSSIBILITY OF SUCH DAMAGE.
115*4882a593Smuzhiyun  */
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun #include <linux/clk.h>
118*4882a593Smuzhiyun #include <linux/clocksource.h>
119*4882a593Smuzhiyun #include <linux/ptp_clock_kernel.h>
120*4882a593Smuzhiyun #include <linux/net_tstamp.h>
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun #include "xgbe.h"
123*4882a593Smuzhiyun #include "xgbe-common.h"
124*4882a593Smuzhiyun 
xgbe_cc_read(const struct cyclecounter * cc)125*4882a593Smuzhiyun static u64 xgbe_cc_read(const struct cyclecounter *cc)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(cc,
128*4882a593Smuzhiyun 						   struct xgbe_prv_data,
129*4882a593Smuzhiyun 						   tstamp_cc);
130*4882a593Smuzhiyun 	u64 nsec;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	nsec = pdata->hw_if.get_tstamp_time(pdata);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	return nsec;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
xgbe_adjfreq(struct ptp_clock_info * info,s32 delta)137*4882a593Smuzhiyun static int xgbe_adjfreq(struct ptp_clock_info *info, s32 delta)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(info,
140*4882a593Smuzhiyun 						   struct xgbe_prv_data,
141*4882a593Smuzhiyun 						   ptp_clock_info);
142*4882a593Smuzhiyun 	unsigned long flags;
143*4882a593Smuzhiyun 	u64 adjust;
144*4882a593Smuzhiyun 	u32 addend, diff;
145*4882a593Smuzhiyun 	unsigned int neg_adjust = 0;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	if (delta < 0) {
148*4882a593Smuzhiyun 		neg_adjust = 1;
149*4882a593Smuzhiyun 		delta = -delta;
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	adjust = pdata->tstamp_addend;
153*4882a593Smuzhiyun 	adjust *= delta;
154*4882a593Smuzhiyun 	diff = div_u64(adjust, 1000000000UL);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	addend = (neg_adjust) ? pdata->tstamp_addend - diff :
157*4882a593Smuzhiyun 				pdata->tstamp_addend + diff;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	spin_lock_irqsave(&pdata->tstamp_lock, flags);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	pdata->hw_if.update_tstamp_addend(pdata, addend);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	return 0;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
xgbe_adjtime(struct ptp_clock_info * info,s64 delta)168*4882a593Smuzhiyun static int xgbe_adjtime(struct ptp_clock_info *info, s64 delta)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(info,
171*4882a593Smuzhiyun 						   struct xgbe_prv_data,
172*4882a593Smuzhiyun 						   ptp_clock_info);
173*4882a593Smuzhiyun 	unsigned long flags;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	spin_lock_irqsave(&pdata->tstamp_lock, flags);
176*4882a593Smuzhiyun 	timecounter_adjtime(&pdata->tstamp_tc, delta);
177*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	return 0;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
xgbe_gettime(struct ptp_clock_info * info,struct timespec64 * ts)182*4882a593Smuzhiyun static int xgbe_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(info,
185*4882a593Smuzhiyun 						   struct xgbe_prv_data,
186*4882a593Smuzhiyun 						   ptp_clock_info);
187*4882a593Smuzhiyun 	unsigned long flags;
188*4882a593Smuzhiyun 	u64 nsec;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	spin_lock_irqsave(&pdata->tstamp_lock, flags);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	nsec = timecounter_read(&pdata->tstamp_tc);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	*ts = ns_to_timespec64(nsec);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	return 0;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
xgbe_settime(struct ptp_clock_info * info,const struct timespec64 * ts)201*4882a593Smuzhiyun static int xgbe_settime(struct ptp_clock_info *info,
202*4882a593Smuzhiyun 			const struct timespec64 *ts)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(info,
205*4882a593Smuzhiyun 						   struct xgbe_prv_data,
206*4882a593Smuzhiyun 						   ptp_clock_info);
207*4882a593Smuzhiyun 	unsigned long flags;
208*4882a593Smuzhiyun 	u64 nsec;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	nsec = timespec64_to_ns(ts);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	spin_lock_irqsave(&pdata->tstamp_lock, flags);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc, nsec);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	return 0;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
xgbe_enable(struct ptp_clock_info * info,struct ptp_clock_request * request,int on)221*4882a593Smuzhiyun static int xgbe_enable(struct ptp_clock_info *info,
222*4882a593Smuzhiyun 		       struct ptp_clock_request *request, int on)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	return -EOPNOTSUPP;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
xgbe_ptp_register(struct xgbe_prv_data * pdata)227*4882a593Smuzhiyun void xgbe_ptp_register(struct xgbe_prv_data *pdata)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	struct ptp_clock_info *info = &pdata->ptp_clock_info;
230*4882a593Smuzhiyun 	struct ptp_clock *clock;
231*4882a593Smuzhiyun 	struct cyclecounter *cc = &pdata->tstamp_cc;
232*4882a593Smuzhiyun 	u64 dividend;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	snprintf(info->name, sizeof(info->name), "%s",
235*4882a593Smuzhiyun 		 netdev_name(pdata->netdev));
236*4882a593Smuzhiyun 	info->owner = THIS_MODULE;
237*4882a593Smuzhiyun 	info->max_adj = pdata->ptpclk_rate;
238*4882a593Smuzhiyun 	info->adjfreq = xgbe_adjfreq;
239*4882a593Smuzhiyun 	info->adjtime = xgbe_adjtime;
240*4882a593Smuzhiyun 	info->gettime64 = xgbe_gettime;
241*4882a593Smuzhiyun 	info->settime64 = xgbe_settime;
242*4882a593Smuzhiyun 	info->enable = xgbe_enable;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	clock = ptp_clock_register(info, pdata->dev);
245*4882a593Smuzhiyun 	if (IS_ERR(clock)) {
246*4882a593Smuzhiyun 		dev_err(pdata->dev, "ptp_clock_register failed\n");
247*4882a593Smuzhiyun 		return;
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	pdata->ptp_clock = clock;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/* Calculate the addend:
253*4882a593Smuzhiyun 	 *   addend = 2^32 / (PTP ref clock / 50Mhz)
254*4882a593Smuzhiyun 	 *          = (2^32 * 50Mhz) / PTP ref clock
255*4882a593Smuzhiyun 	 */
256*4882a593Smuzhiyun 	dividend = 50000000;
257*4882a593Smuzhiyun 	dividend <<= 32;
258*4882a593Smuzhiyun 	pdata->tstamp_addend = div_u64(dividend, pdata->ptpclk_rate);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/* Setup the timecounter */
261*4882a593Smuzhiyun 	cc->read = xgbe_cc_read;
262*4882a593Smuzhiyun 	cc->mask = CLOCKSOURCE_MASK(64);
263*4882a593Smuzhiyun 	cc->mult = 1;
264*4882a593Smuzhiyun 	cc->shift = 0;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
267*4882a593Smuzhiyun 			 ktime_to_ns(ktime_get_real()));
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	/* Disable all timestamping to start */
270*4882a593Smuzhiyun 	XGMAC_IOWRITE(pdata, MAC_TSCR, 0);
271*4882a593Smuzhiyun 	pdata->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
272*4882a593Smuzhiyun 	pdata->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
xgbe_ptp_unregister(struct xgbe_prv_data * pdata)275*4882a593Smuzhiyun void xgbe_ptp_unregister(struct xgbe_prv_data *pdata)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	if (pdata->ptp_clock)
278*4882a593Smuzhiyun 		ptp_clock_unregister(pdata->ptp_clock);
279*4882a593Smuzhiyun }
280