Merge branch 'irq-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / net / ethernet / renesas / ravb_ptp.c
blob7a8ce920c49e709b067321ae91306153f4f24390
1 /* PTP 1588 clock using the Renesas Ethernet AVB
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>
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.
13 #include "ravb.h"
15 static int ravb_ptp_tcr_request(struct ravb_private *priv, u32 request)
17 struct net_device *ndev = priv->ndev;
18 int error;
20 error = ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
21 if (error)
22 return error;
24 ravb_write(ndev, ravb_read(ndev, GCCR) | request, GCCR);
25 return ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
28 /* Caller must hold the lock */
29 static int ravb_ptp_time_read(struct ravb_private *priv, struct timespec64 *ts)
31 struct net_device *ndev = priv->ndev;
32 int error;
34 error = ravb_ptp_tcr_request(priv, GCCR_TCR_CAPTURE);
35 if (error)
36 return error;
38 ts->tv_nsec = ravb_read(ndev, GCT0);
39 ts->tv_sec = ravb_read(ndev, GCT1) |
40 ((s64)ravb_read(ndev, GCT2) << 32);
42 return 0;
45 /* Caller must hold the lock */
46 static int ravb_ptp_time_write(struct ravb_private *priv,
47 const struct timespec64 *ts)
49 struct net_device *ndev = priv->ndev;
50 int error;
51 u32 gccr;
53 error = ravb_ptp_tcr_request(priv, GCCR_TCR_RESET);
54 if (error)
55 return error;
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);
65 return 0;
68 /* Caller must hold the lock */
69 static int ravb_ptp_update_compare(struct ravb_private *priv, u32 ns)
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.
77 u32 gti_ns_plus_1 = (priv->ptp.current_addend >> 20) + 1;
78 u32 gccr;
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;
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);
91 return 0;
94 /* PTP clock operations */
95 static int ravb_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
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;
105 if (ppb < 0) {
106 neg_adj = true;
107 ppb = -ppb;
109 addend = priv->ptp.default_addend;
110 diff = div_u64((u64)addend * ppb, NSEC_PER_SEC);
112 addend = neg_adj ? addend - diff : addend + diff;
114 spin_lock_irqsave(&priv->lock, flags);
116 priv->ptp.current_addend = addend;
118 gccr = ravb_read(ndev, GCCR);
119 if (gccr & GCCR_LTI) {
120 spin_unlock_irqrestore(&priv->lock, flags);
121 return -EBUSY;
123 ravb_write(ndev, addend & GTI_TIV, GTI);
124 ravb_write(ndev, gccr | GCCR_LTI, GCCR);
126 spin_unlock_irqrestore(&priv->lock, flags);
128 return 0;
131 static int ravb_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
133 struct ravb_private *priv = container_of(ptp, struct ravb_private,
134 ptp.info);
135 struct timespec64 ts;
136 unsigned long flags;
137 int error;
139 spin_lock_irqsave(&priv->lock, flags);
140 error = ravb_ptp_time_read(priv, &ts);
141 if (!error) {
142 u64 now = ktime_to_ns(timespec64_to_ktime(ts));
144 ts = ns_to_timespec64(now + delta);
145 error = ravb_ptp_time_write(priv, &ts);
147 spin_unlock_irqrestore(&priv->lock, flags);
149 return error;
152 static int ravb_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
154 struct ravb_private *priv = container_of(ptp, struct ravb_private,
155 ptp.info);
156 unsigned long flags;
157 int error;
159 spin_lock_irqsave(&priv->lock, flags);
160 error = ravb_ptp_time_read(priv, ts);
161 spin_unlock_irqrestore(&priv->lock, flags);
163 return error;
166 static int ravb_ptp_settime64(struct ptp_clock_info *ptp,
167 const struct timespec64 *ts)
169 struct ravb_private *priv = container_of(ptp, struct ravb_private,
170 ptp.info);
171 unsigned long flags;
172 int error;
174 spin_lock_irqsave(&priv->lock, flags);
175 error = ravb_ptp_time_write(priv, ts);
176 spin_unlock_irqrestore(&priv->lock, flags);
178 return error;
181 static int ravb_ptp_extts(struct ptp_clock_info *ptp,
182 struct ptp_extts_request *req, int on)
184 struct ravb_private *priv = container_of(ptp, struct ravb_private,
185 ptp.info);
186 struct net_device *ndev = priv->ndev;
187 unsigned long flags;
188 u32 gic;
190 if (req->index)
191 return -EINVAL;
193 if (priv->ptp.extts[req->index] == on)
194 return 0;
195 priv->ptp.extts[req->index] = on;
197 spin_lock_irqsave(&priv->lock, flags);
198 gic = ravb_read(ndev, GIC);
199 if (on)
200 gic |= GIC_PTCE;
201 else
202 gic &= ~GIC_PTCE;
203 ravb_write(ndev, gic, GIC);
204 mmiowb();
205 spin_unlock_irqrestore(&priv->lock, flags);
207 return 0;
210 static int ravb_ptp_perout(struct ptp_clock_info *ptp,
211 struct ptp_perout_request *req, int on)
213 struct ravb_private *priv = container_of(ptp, struct ravb_private,
214 ptp.info);
215 struct net_device *ndev = priv->ndev;
216 struct ravb_ptp_perout *perout;
217 unsigned long flags;
218 int error = 0;
219 u32 gic;
221 if (req->index)
222 return -EINVAL;
224 if (on) {
225 u64 start_ns;
226 u64 period_ns;
228 start_ns = req->start.sec * NSEC_PER_SEC + req->start.nsec;
229 period_ns = req->period.sec * NSEC_PER_SEC + req->period.nsec;
231 if (start_ns > U32_MAX) {
232 netdev_warn(ndev,
233 "ptp: start value (nsec) is over limit. Maximum size of start is only 32 bits\n");
234 return -ERANGE;
237 if (period_ns > U32_MAX) {
238 netdev_warn(ndev,
239 "ptp: period value (nsec) is over limit. Maximum size of period is only 32 bits\n");
240 return -ERANGE;
243 spin_lock_irqsave(&priv->lock, flags);
245 perout = &priv->ptp.perout[req->index];
246 perout->target = (u32)start_ns;
247 perout->period = (u32)period_ns;
248 error = ravb_ptp_update_compare(priv, (u32)start_ns);
249 if (!error) {
250 /* Unmask interrupt */
251 gic = ravb_read(ndev, GIC);
252 gic |= GIC_PTME;
253 ravb_write(ndev, gic, GIC);
255 } else {
256 spin_lock_irqsave(&priv->lock, flags);
258 perout = &priv->ptp.perout[req->index];
259 perout->period = 0;
261 /* Mask interrupt */
262 gic = ravb_read(ndev, GIC);
263 gic &= ~GIC_PTME;
264 ravb_write(ndev, gic, GIC);
266 mmiowb();
267 spin_unlock_irqrestore(&priv->lock, flags);
269 return error;
272 static int ravb_ptp_enable(struct ptp_clock_info *ptp,
273 struct ptp_clock_request *req, int on)
275 switch (req->type) {
276 case PTP_CLK_REQ_EXTTS:
277 return ravb_ptp_extts(ptp, &req->extts, on);
278 case PTP_CLK_REQ_PEROUT:
279 return ravb_ptp_perout(ptp, &req->perout, on);
280 default:
281 return -EOPNOTSUPP;
285 static const struct ptp_clock_info ravb_ptp_info = {
286 .owner = THIS_MODULE,
287 .name = "ravb clock",
288 .max_adj = 50000000,
289 .n_ext_ts = N_EXT_TS,
290 .n_per_out = N_PER_OUT,
291 .adjfreq = ravb_ptp_adjfreq,
292 .adjtime = ravb_ptp_adjtime,
293 .gettime64 = ravb_ptp_gettime64,
294 .settime64 = ravb_ptp_settime64,
295 .enable = ravb_ptp_enable,
298 /* Caller must hold the lock */
299 irqreturn_t ravb_ptp_interrupt(struct net_device *ndev)
301 struct ravb_private *priv = netdev_priv(ndev);
302 u32 gis = ravb_read(ndev, GIS);
304 gis &= ravb_read(ndev, GIC);
305 if (gis & GIS_PTCF) {
306 struct ptp_clock_event event;
308 event.type = PTP_CLOCK_EXTTS;
309 event.index = 0;
310 event.timestamp = ravb_read(ndev, GCPT);
311 ptp_clock_event(priv->ptp.clock, &event);
313 if (gis & GIS_PTMF) {
314 struct ravb_ptp_perout *perout = priv->ptp.perout;
316 if (perout->period) {
317 perout->target += perout->period;
318 ravb_ptp_update_compare(priv, perout->target);
322 if (gis) {
323 ravb_write(ndev, ~gis, GIS);
324 return IRQ_HANDLED;
327 return IRQ_NONE;
330 void ravb_ptp_init(struct net_device *ndev, struct platform_device *pdev)
332 struct ravb_private *priv = netdev_priv(ndev);
333 unsigned long flags;
334 u32 gccr;
336 priv->ptp.info = ravb_ptp_info;
338 priv->ptp.default_addend = ravb_read(ndev, GTI);
339 priv->ptp.current_addend = priv->ptp.default_addend;
341 spin_lock_irqsave(&priv->lock, flags);
342 ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
343 gccr = ravb_read(ndev, GCCR) & ~GCCR_TCSS;
344 ravb_write(ndev, gccr | GCCR_TCSS_ADJGPTP, GCCR);
345 mmiowb();
346 spin_unlock_irqrestore(&priv->lock, flags);
348 priv->ptp.clock = ptp_clock_register(&priv->ptp.info, &pdev->dev);
351 void ravb_ptp_stop(struct net_device *ndev)
353 struct ravb_private *priv = netdev_priv(ndev);
355 ravb_write(ndev, 0, GIC);
356 ravb_write(ndev, 0, GIS);
358 ptp_clock_unregister(priv->ptp.clock);