xref: /linux/drivers/net/ethernet/renesas/ravb_ptp.c (revision 3e44c471a2dab210f7e9b1e5f7d4d54d52df59eb)
1 /* PTP 1588 clock using the Renesas Ethernet AVB
2  *
3  * Copyright (C) 2013-2015 Renesas Electronics Corporation
4  * Copyright (C) 2015 Renesas Solutions Corp.
5  * Copyright (C) 2015 Cogent Embedded, Inc. <source@cogentembedded.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  */
12 
13 #include "ravb.h"
14 
15 static int ravb_ptp_tcr_request(struct ravb_private *priv, u32 request)
16 {
17 	struct net_device *ndev = priv->ndev;
18 	int error;
19 
20 	error = ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
21 	if (error)
22 		return error;
23 
24 	ravb_write(ndev, ravb_read(ndev, GCCR) | request, GCCR);
25 	return ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
26 }
27 
28 /* Caller must hold the lock */
29 static int ravb_ptp_time_read(struct ravb_private *priv, struct timespec64 *ts)
30 {
31 	struct net_device *ndev = priv->ndev;
32 	int error;
33 
34 	error = ravb_ptp_tcr_request(priv, GCCR_TCR_CAPTURE);
35 	if (error)
36 		return error;
37 
38 	ts->tv_nsec = ravb_read(ndev, GCT0);
39 	ts->tv_sec  = ravb_read(ndev, GCT1) |
40 		((s64)ravb_read(ndev, GCT2) << 32);
41 
42 	return 0;
43 }
44 
45 /* Caller must hold the lock */
46 static int ravb_ptp_time_write(struct ravb_private *priv,
47 				const struct timespec64 *ts)
48 {
49 	struct net_device *ndev = priv->ndev;
50 	int error;
51 	u32 gccr;
52 
53 	error = ravb_ptp_tcr_request(priv, GCCR_TCR_RESET);
54 	if (error)
55 		return error;
56 
57 	gccr = ravb_read(ndev, GCCR);
58 	if (gccr & GCCR_LTO)
59 		return -EBUSY;
60 	ravb_write(ndev, ts->tv_nsec, GTO0);
61 	ravb_write(ndev, ts->tv_sec,  GTO1);
62 	ravb_write(ndev, (ts->tv_sec >> 32) & 0xffff, GTO2);
63 	ravb_write(ndev, gccr | GCCR_LTO, GCCR);
64 
65 	return 0;
66 }
67 
68 /* Caller must hold the lock */
69 static int ravb_ptp_update_compare(struct ravb_private *priv, u32 ns)
70 {
71 	struct net_device *ndev = priv->ndev;
72 	/* When the comparison value (GPTC.PTCV) is in range of
73 	 * [x-1 to x+1] (x is the configured increment value in
74 	 * GTI.TIV), it may happen that a comparison match is
75 	 * not detected when the timer wraps around.
76 	 */
77 	u32 gti_ns_plus_1 = (priv->ptp.current_addend >> 20) + 1;
78 	u32 gccr;
79 
80 	if (ns < gti_ns_plus_1)
81 		ns = gti_ns_plus_1;
82 	else if (ns > 0 - gti_ns_plus_1)
83 		ns = 0 - gti_ns_plus_1;
84 
85 	gccr = ravb_read(ndev, GCCR);
86 	if (gccr & GCCR_LPTC)
87 		return -EBUSY;
88 	ravb_write(ndev, ns, GPTC);
89 	ravb_write(ndev, gccr | GCCR_LPTC, GCCR);
90 
91 	return 0;
92 }
93 
94 /* PTP clock operations */
95 static int ravb_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
96 {
97 	struct ravb_private *priv = container_of(ptp, struct ravb_private,
98 						 ptp.info);
99 	struct net_device *ndev = priv->ndev;
100 	unsigned long flags;
101 	u32 diff, addend;
102 	bool neg_adj = false;
103 	u32 gccr;
104 
105 	if (ppb < 0) {
106 		neg_adj = true;
107 		ppb = -ppb;
108 	}
109 	addend = priv->ptp.default_addend;
110 	diff = div_u64((u64)addend * ppb, NSEC_PER_SEC);
111 
112 	addend = neg_adj ? addend - diff : addend + diff;
113 
114 	spin_lock_irqsave(&priv->lock, flags);
115 
116 	priv->ptp.current_addend = addend;
117 
118 	gccr = ravb_read(ndev, GCCR);
119 	if (gccr & GCCR_LTI)
120 		return -EBUSY;
121 	ravb_write(ndev, addend & GTI_TIV, GTI);
122 	ravb_write(ndev, gccr | GCCR_LTI, GCCR);
123 
124 	spin_unlock_irqrestore(&priv->lock, flags);
125 
126 	return 0;
127 }
128 
129 static int ravb_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
130 {
131 	struct ravb_private *priv = container_of(ptp, struct ravb_private,
132 						 ptp.info);
133 	struct timespec64 ts;
134 	unsigned long flags;
135 	int error;
136 
137 	spin_lock_irqsave(&priv->lock, flags);
138 	error = ravb_ptp_time_read(priv, &ts);
139 	if (!error) {
140 		u64 now = ktime_to_ns(timespec64_to_ktime(ts));
141 
142 		ts = ns_to_timespec64(now + delta);
143 		error = ravb_ptp_time_write(priv, &ts);
144 	}
145 	spin_unlock_irqrestore(&priv->lock, flags);
146 
147 	return error;
148 }
149 
150 static int ravb_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
151 {
152 	struct ravb_private *priv = container_of(ptp, struct ravb_private,
153 						 ptp.info);
154 	unsigned long flags;
155 	int error;
156 
157 	spin_lock_irqsave(&priv->lock, flags);
158 	error = ravb_ptp_time_read(priv, ts);
159 	spin_unlock_irqrestore(&priv->lock, flags);
160 
161 	return error;
162 }
163 
164 static int ravb_ptp_settime64(struct ptp_clock_info *ptp,
165 			      const struct timespec64 *ts)
166 {
167 	struct ravb_private *priv = container_of(ptp, struct ravb_private,
168 						 ptp.info);
169 	unsigned long flags;
170 	int error;
171 
172 	spin_lock_irqsave(&priv->lock, flags);
173 	error = ravb_ptp_time_write(priv, ts);
174 	spin_unlock_irqrestore(&priv->lock, flags);
175 
176 	return error;
177 }
178 
179 static int ravb_ptp_extts(struct ptp_clock_info *ptp,
180 			  struct ptp_extts_request *req, int on)
181 {
182 	struct ravb_private *priv = container_of(ptp, struct ravb_private,
183 						 ptp.info);
184 	struct net_device *ndev = priv->ndev;
185 	unsigned long flags;
186 	u32 gic;
187 
188 	if (req->index)
189 		return -EINVAL;
190 
191 	if (priv->ptp.extts[req->index] == on)
192 		return 0;
193 	priv->ptp.extts[req->index] = on;
194 
195 	spin_lock_irqsave(&priv->lock, flags);
196 	gic = ravb_read(ndev, GIC);
197 	if (on)
198 		gic |= GIC_PTCE;
199 	else
200 		gic &= ~GIC_PTCE;
201 	ravb_write(ndev, gic, GIC);
202 	mmiowb();
203 	spin_unlock_irqrestore(&priv->lock, flags);
204 
205 	return 0;
206 }
207 
208 static int ravb_ptp_perout(struct ptp_clock_info *ptp,
209 			   struct ptp_perout_request *req, int on)
210 {
211 	struct ravb_private *priv = container_of(ptp, struct ravb_private,
212 						 ptp.info);
213 	struct net_device *ndev = priv->ndev;
214 	struct ravb_ptp_perout *perout;
215 	unsigned long flags;
216 	int error = 0;
217 	u32 gic;
218 
219 	if (req->index)
220 		return -EINVAL;
221 
222 	if (on) {
223 		u64 start_ns;
224 		u64 period_ns;
225 
226 		start_ns = req->start.sec * NSEC_PER_SEC + req->start.nsec;
227 		period_ns = req->period.sec * NSEC_PER_SEC + req->period.nsec;
228 
229 		if (start_ns > U32_MAX) {
230 			netdev_warn(ndev,
231 				    "ptp: start value (nsec) is over limit. Maximum size of start is only 32 bits\n");
232 			return -ERANGE;
233 		}
234 
235 		if (period_ns > U32_MAX) {
236 			netdev_warn(ndev,
237 				    "ptp: period value (nsec) is over limit. Maximum size of period is only 32 bits\n");
238 			return -ERANGE;
239 		}
240 
241 		spin_lock_irqsave(&priv->lock, flags);
242 
243 		perout = &priv->ptp.perout[req->index];
244 		perout->target = (u32)start_ns;
245 		perout->period = (u32)period_ns;
246 		error = ravb_ptp_update_compare(priv, (u32)start_ns);
247 		if (!error) {
248 			/* Unmask interrupt */
249 			gic = ravb_read(ndev, GIC);
250 			gic |= GIC_PTME;
251 			ravb_write(ndev, gic, GIC);
252 		}
253 	} else	{
254 		spin_lock_irqsave(&priv->lock, flags);
255 
256 		perout = &priv->ptp.perout[req->index];
257 		perout->period = 0;
258 
259 		/* Mask interrupt */
260 		gic = ravb_read(ndev, GIC);
261 		gic &= ~GIC_PTME;
262 		ravb_write(ndev, gic, GIC);
263 	}
264 	mmiowb();
265 	spin_unlock_irqrestore(&priv->lock, flags);
266 
267 	return error;
268 }
269 
270 static int ravb_ptp_enable(struct ptp_clock_info *ptp,
271 			   struct ptp_clock_request *req, int on)
272 {
273 	switch (req->type) {
274 	case PTP_CLK_REQ_EXTTS:
275 		return ravb_ptp_extts(ptp, &req->extts, on);
276 	case PTP_CLK_REQ_PEROUT:
277 		return ravb_ptp_perout(ptp, &req->perout, on);
278 	default:
279 		return -EOPNOTSUPP;
280 	}
281 }
282 
283 static const struct ptp_clock_info ravb_ptp_info = {
284 	.owner		= THIS_MODULE,
285 	.name		= "ravb clock",
286 	.max_adj	= 50000000,
287 	.n_ext_ts	= N_EXT_TS,
288 	.n_per_out	= N_PER_OUT,
289 	.adjfreq	= ravb_ptp_adjfreq,
290 	.adjtime	= ravb_ptp_adjtime,
291 	.gettime64	= ravb_ptp_gettime64,
292 	.settime64	= ravb_ptp_settime64,
293 	.enable		= ravb_ptp_enable,
294 };
295 
296 /* Caller must hold the lock */
297 irqreturn_t ravb_ptp_interrupt(struct net_device *ndev)
298 {
299 	struct ravb_private *priv = netdev_priv(ndev);
300 	u32 gis = ravb_read(ndev, GIS);
301 
302 	gis &= ravb_read(ndev, GIC);
303 	if (gis & GIS_PTCF) {
304 		struct ptp_clock_event event;
305 
306 		event.type = PTP_CLOCK_EXTTS;
307 		event.index = 0;
308 		event.timestamp = ravb_read(ndev, GCPT);
309 		ptp_clock_event(priv->ptp.clock, &event);
310 	}
311 	if (gis & GIS_PTMF) {
312 		struct ravb_ptp_perout *perout = priv->ptp.perout;
313 
314 		if (perout->period) {
315 			perout->target += perout->period;
316 			ravb_ptp_update_compare(priv, perout->target);
317 		}
318 	}
319 
320 	if (gis) {
321 		ravb_write(ndev, ~gis, GIS);
322 		return IRQ_HANDLED;
323 	}
324 
325 	return IRQ_NONE;
326 }
327 
328 void ravb_ptp_init(struct net_device *ndev, struct platform_device *pdev)
329 {
330 	struct ravb_private *priv = netdev_priv(ndev);
331 	unsigned long flags;
332 	u32 gccr;
333 
334 	priv->ptp.info = ravb_ptp_info;
335 
336 	priv->ptp.default_addend = ravb_read(ndev, GTI);
337 	priv->ptp.current_addend = priv->ptp.default_addend;
338 
339 	spin_lock_irqsave(&priv->lock, flags);
340 	ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
341 	gccr = ravb_read(ndev, GCCR) & ~GCCR_TCSS;
342 	ravb_write(ndev, gccr | GCCR_TCSS_ADJGPTP, GCCR);
343 	mmiowb();
344 	spin_unlock_irqrestore(&priv->lock, flags);
345 
346 	priv->ptp.clock = ptp_clock_register(&priv->ptp.info, &pdev->dev);
347 }
348 
349 void ravb_ptp_stop(struct net_device *ndev)
350 {
351 	struct ravb_private *priv = netdev_priv(ndev);
352 
353 	ravb_write(ndev, 0, GIC);
354 	ravb_write(ndev, 0, GIS);
355 
356 	ptp_clock_unregister(priv->ptp.clock);
357 }
358