Loading...
1// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/* Microsemi Ocelot PTP clock driver
3 *
4 * Copyright (c) 2017 Microsemi Corporation
5 * Copyright 2020 NXP
6 */
7#include <linux/time64.h>
8
9#include <linux/dsa/ocelot.h>
10#include <linux/ptp_classify.h>
11#include <soc/mscc/ocelot_ptp.h>
12#include <soc/mscc/ocelot_sys.h>
13#include <soc/mscc/ocelot_vcap.h>
14#include <soc/mscc/ocelot.h>
15#include "ocelot.h"
16
17#define OCELOT_PTP_TX_TSTAMP_TIMEOUT (5 * HZ)
18
19int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
20{
21 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
22 unsigned long flags;
23 time64_t s;
24 u32 val;
25 s64 ns;
26
27 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
28
29 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
30 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
31 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
32 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
33
34 s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
35 s <<= 32;
36 s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
37 ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
38
39 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
40
41 /* Deal with negative values */
42 if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
43 s--;
44 ns &= 0xf;
45 ns += 999999984;
46 }
47
48 set_normalized_timespec64(ts, s, ns);
49 return 0;
50}
51EXPORT_SYMBOL(ocelot_ptp_gettime64);
52
53int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
54 const struct timespec64 *ts)
55{
56 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
57 unsigned long flags;
58 u32 val;
59
60 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
61
62 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
63 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
64 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
65
66 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
67
68 ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
69 TOD_ACC_PIN);
70 ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
71 TOD_ACC_PIN);
72 ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
73
74 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
75 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
76 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
77
78 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
79
80 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
81
82 if (ocelot->ops->tas_clock_adjust)
83 ocelot->ops->tas_clock_adjust(ocelot);
84
85 return 0;
86}
87EXPORT_SYMBOL(ocelot_ptp_settime64);
88
89int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
90{
91 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
92 struct ocelot *ocelot = container_of(ptp, struct ocelot,
93 ptp_info);
94 unsigned long flags;
95 u32 val;
96
97 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
98
99 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
100 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
101 PTP_PIN_CFG_DOM);
102 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
103
104 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
105
106 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
107 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
108 ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
109
110 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
111 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
112 PTP_PIN_CFG_DOM);
113 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
114
115 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
116
117 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
118
119 if (ocelot->ops->tas_clock_adjust)
120 ocelot->ops->tas_clock_adjust(ocelot);
121 } else {
122 /* Fall back using ocelot_ptp_settime64 which is not exact. */
123 struct timespec64 ts;
124 u64 now;
125
126 ocelot_ptp_gettime64(ptp, &ts);
127
128 now = ktime_to_ns(timespec64_to_ktime(ts));
129 ts = ns_to_timespec64(now + delta);
130
131 ocelot_ptp_settime64(ptp, &ts);
132 }
133
134 return 0;
135}
136EXPORT_SYMBOL(ocelot_ptp_adjtime);
137
138int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
139{
140 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
141 u32 unit = 0, direction = 0;
142 unsigned long flags;
143 u64 adj = 0;
144
145 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
146
147 if (!scaled_ppm)
148 goto disable_adj;
149
150 if (scaled_ppm < 0) {
151 direction = PTP_CFG_CLK_ADJ_CFG_DIR;
152 scaled_ppm = -scaled_ppm;
153 }
154
155 adj = PSEC_PER_SEC << 16;
156 do_div(adj, scaled_ppm);
157 do_div(adj, 1000);
158
159 /* If the adjustment value is too large, use ns instead */
160 if (adj >= (1L << 30)) {
161 unit = PTP_CFG_CLK_ADJ_FREQ_NS;
162 do_div(adj, 1000);
163 }
164
165 /* Still too big */
166 if (adj >= (1L << 30))
167 goto disable_adj;
168
169 ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
170 ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
171 PTP_CLK_CFG_ADJ_CFG);
172
173 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
174 return 0;
175
176disable_adj:
177 ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
178
179 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
180 return 0;
181}
182EXPORT_SYMBOL(ocelot_ptp_adjfine);
183
184int ocelot_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
185 enum ptp_pin_function func, unsigned int chan)
186{
187 switch (func) {
188 case PTP_PF_NONE:
189 case PTP_PF_PEROUT:
190 break;
191 case PTP_PF_EXTTS:
192 case PTP_PF_PHYSYNC:
193 return -1;
194 }
195 return 0;
196}
197EXPORT_SYMBOL(ocelot_ptp_verify);
198
199int ocelot_ptp_enable(struct ptp_clock_info *ptp,
200 struct ptp_clock_request *rq, int on)
201{
202 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
203 struct timespec64 ts_phase, ts_period;
204 enum ocelot_ptp_pins ptp_pin;
205 unsigned long flags;
206 bool pps = false;
207 int pin = -1;
208 s64 wf_high;
209 s64 wf_low;
210 u32 val;
211
212 switch (rq->type) {
213 case PTP_CLK_REQ_PEROUT:
214 /* Reject requests with unsupported flags */
215 if (rq->perout.flags & ~(PTP_PEROUT_DUTY_CYCLE |
216 PTP_PEROUT_PHASE))
217 return -EOPNOTSUPP;
218
219 pin = ptp_find_pin(ocelot->ptp_clock, PTP_PF_PEROUT,
220 rq->perout.index);
221 if (pin == 0)
222 ptp_pin = PTP_PIN_0;
223 else if (pin == 1)
224 ptp_pin = PTP_PIN_1;
225 else if (pin == 2)
226 ptp_pin = PTP_PIN_2;
227 else if (pin == 3)
228 ptp_pin = PTP_PIN_3;
229 else
230 return -EBUSY;
231
232 ts_period.tv_sec = rq->perout.period.sec;
233 ts_period.tv_nsec = rq->perout.period.nsec;
234
235 if (ts_period.tv_sec == 1 && ts_period.tv_nsec == 0)
236 pps = true;
237
238 /* Handle turning off */
239 if (!on) {
240 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
241 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
242 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
243 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
244 break;
245 }
246
247 if (rq->perout.flags & PTP_PEROUT_PHASE) {
248 ts_phase.tv_sec = rq->perout.phase.sec;
249 ts_phase.tv_nsec = rq->perout.phase.nsec;
250 } else {
251 /* Compatibility */
252 ts_phase.tv_sec = rq->perout.start.sec;
253 ts_phase.tv_nsec = rq->perout.start.nsec;
254 }
255 if (ts_phase.tv_sec || (ts_phase.tv_nsec && !pps)) {
256 dev_warn(ocelot->dev,
257 "Absolute start time not supported!\n");
258 dev_warn(ocelot->dev,
259 "Accept nsec for PPS phase adjustment, otherwise start time should be 0 0.\n");
260 return -EINVAL;
261 }
262
263 /* Calculate waveform high and low times */
264 if (rq->perout.flags & PTP_PEROUT_DUTY_CYCLE) {
265 struct timespec64 ts_on;
266
267 ts_on.tv_sec = rq->perout.on.sec;
268 ts_on.tv_nsec = rq->perout.on.nsec;
269
270 wf_high = timespec64_to_ns(&ts_on);
271 } else {
272 if (pps) {
273 wf_high = 1000;
274 } else {
275 wf_high = timespec64_to_ns(&ts_period);
276 wf_high = div_s64(wf_high, 2);
277 }
278 }
279
280 wf_low = timespec64_to_ns(&ts_period);
281 wf_low -= wf_high;
282
283 /* Handle PPS request */
284 if (pps) {
285 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
286 ocelot_write_rix(ocelot, ts_phase.tv_nsec,
287 PTP_PIN_WF_LOW_PERIOD, ptp_pin);
288 ocelot_write_rix(ocelot, wf_high,
289 PTP_PIN_WF_HIGH_PERIOD, ptp_pin);
290 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_CLOCK);
291 val |= PTP_PIN_CFG_SYNC;
292 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
293 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
294 break;
295 }
296
297 /* Handle periodic clock */
298 if (wf_high > 0x3fffffff || wf_high <= 0x6)
299 return -EINVAL;
300 if (wf_low > 0x3fffffff || wf_low <= 0x6)
301 return -EINVAL;
302
303 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
304 ocelot_write_rix(ocelot, wf_low, PTP_PIN_WF_LOW_PERIOD,
305 ptp_pin);
306 ocelot_write_rix(ocelot, wf_high, PTP_PIN_WF_HIGH_PERIOD,
307 ptp_pin);
308 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_CLOCK);
309 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
310 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
311 break;
312 default:
313 return -EOPNOTSUPP;
314 }
315 return 0;
316}
317EXPORT_SYMBOL(ocelot_ptp_enable);
318
319static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
320{
321 trap->key_type = OCELOT_VCAP_KEY_ETYPE;
322 *(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588);
323 *(__be16 *)trap->key.etype.etype.mask = htons(0xffff);
324}
325
326static void
327ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
328{
329 trap->key_type = OCELOT_VCAP_KEY_IPV4;
330 trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
331 trap->key.ipv4.proto.mask[0] = 0xff;
332 trap->key.ipv4.dport.value = PTP_EV_PORT;
333 trap->key.ipv4.dport.mask = 0xffff;
334}
335
336static void
337ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
338{
339 trap->key_type = OCELOT_VCAP_KEY_IPV6;
340 trap->key.ipv6.proto.value[0] = IPPROTO_UDP;
341 trap->key.ipv6.proto.mask[0] = 0xff;
342 trap->key.ipv6.dport.value = PTP_EV_PORT;
343 trap->key.ipv6.dport.mask = 0xffff;
344}
345
346static void
347ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
348{
349 trap->key_type = OCELOT_VCAP_KEY_IPV4;
350 trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
351 trap->key.ipv4.proto.mask[0] = 0xff;
352 trap->key.ipv4.dport.value = PTP_GEN_PORT;
353 trap->key.ipv4.dport.mask = 0xffff;
354}
355
356static void
357ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
358{
359 trap->key_type = OCELOT_VCAP_KEY_IPV6;
360 trap->key.ipv6.proto.value[0] = IPPROTO_UDP;
361 trap->key.ipv6.proto.mask[0] = 0xff;
362 trap->key.ipv6.dport.value = PTP_GEN_PORT;
363 trap->key.ipv6.dport.mask = 0xffff;
364}
365
366static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
367{
368 unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
369
370 return ocelot_trap_add(ocelot, port, l2_cookie, true,
371 ocelot_populate_l2_ptp_trap_key);
372}
373
374static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
375{
376 unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
377
378 return ocelot_trap_del(ocelot, port, l2_cookie);
379}
380
381static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
382{
383 unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
384 unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
385 int err;
386
387 err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, true,
388 ocelot_populate_ipv4_ptp_event_trap_key);
389 if (err)
390 return err;
391
392 err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, false,
393 ocelot_populate_ipv4_ptp_general_trap_key);
394 if (err)
395 ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
396
397 return err;
398}
399
400static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
401{
402 unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
403 unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
404 int err;
405
406 err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
407 err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
408 return err;
409}
410
411static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
412{
413 unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
414 unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
415 int err;
416
417 err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, true,
418 ocelot_populate_ipv6_ptp_event_trap_key);
419 if (err)
420 return err;
421
422 err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, false,
423 ocelot_populate_ipv6_ptp_general_trap_key);
424 if (err)
425 ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
426
427 return err;
428}
429
430static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
431{
432 unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
433 unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
434 int err;
435
436 err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
437 err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
438 return err;
439}
440
441static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
442 bool l2, bool l4)
443{
444 struct ocelot_port *ocelot_port = ocelot->ports[port];
445 int err;
446
447 ocelot_port->trap_proto &= ~(OCELOT_PROTO_PTP_L2 |
448 OCELOT_PROTO_PTP_L4);
449
450 if (l2)
451 err = ocelot_l2_ptp_trap_add(ocelot, port);
452 else
453 err = ocelot_l2_ptp_trap_del(ocelot, port);
454 if (err)
455 return err;
456
457 if (l4) {
458 err = ocelot_ipv4_ptp_trap_add(ocelot, port);
459 if (err)
460 goto err_ipv4;
461
462 err = ocelot_ipv6_ptp_trap_add(ocelot, port);
463 if (err)
464 goto err_ipv6;
465 } else {
466 err = ocelot_ipv4_ptp_trap_del(ocelot, port);
467
468 err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
469 }
470 if (err)
471 return err;
472
473 if (l2)
474 ocelot_port->trap_proto |= OCELOT_PROTO_PTP_L2;
475 if (l4)
476 ocelot_port->trap_proto |= OCELOT_PROTO_PTP_L4;
477
478 return 0;
479
480err_ipv6:
481 ocelot_ipv4_ptp_trap_del(ocelot, port);
482err_ipv4:
483 if (l2)
484 ocelot_l2_ptp_trap_del(ocelot, port);
485 return err;
486}
487
488static int ocelot_traps_to_ptp_rx_filter(unsigned int proto)
489{
490 if ((proto & OCELOT_PROTO_PTP_L2) && (proto & OCELOT_PROTO_PTP_L4))
491 return HWTSTAMP_FILTER_PTP_V2_EVENT;
492 else if (proto & OCELOT_PROTO_PTP_L2)
493 return HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
494 else if (proto & OCELOT_PROTO_PTP_L4)
495 return HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
496
497 return HWTSTAMP_FILTER_NONE;
498}
499
500static int ocelot_ptp_tx_type_to_cmd(int tx_type, int *ptp_cmd)
501{
502 switch (tx_type) {
503 case HWTSTAMP_TX_ON:
504 *ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
505 break;
506 case HWTSTAMP_TX_ONESTEP_SYNC:
507 /* IFH_REW_OP_ONE_STEP_PTP updates the correctionField,
508 * what we need to update is the originTimestamp.
509 */
510 *ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
511 break;
512 case HWTSTAMP_TX_OFF:
513 *ptp_cmd = 0;
514 break;
515 default:
516 return -ERANGE;
517 }
518
519 return 0;
520}
521
522int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
523{
524 struct ocelot_port *ocelot_port = ocelot->ports[port];
525 struct hwtstamp_config cfg = {};
526
527 switch (ocelot_port->ptp_cmd) {
528 case IFH_REW_OP_TWO_STEP_PTP:
529 cfg.tx_type = HWTSTAMP_TX_ON;
530 break;
531 case IFH_REW_OP_ORIGIN_PTP:
532 cfg.tx_type = HWTSTAMP_TX_ONESTEP_SYNC;
533 break;
534 default:
535 cfg.tx_type = HWTSTAMP_TX_OFF;
536 break;
537 }
538
539 cfg.rx_filter = ocelot_traps_to_ptp_rx_filter(ocelot_port->trap_proto);
540
541 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
542}
543EXPORT_SYMBOL(ocelot_hwstamp_get);
544
545int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
546{
547 struct ocelot_port *ocelot_port = ocelot->ports[port];
548 int ptp_cmd, old_ptp_cmd = ocelot_port->ptp_cmd;
549 bool l2 = false, l4 = false;
550 struct hwtstamp_config cfg;
551 bool old_l2, old_l4;
552 int err;
553
554 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
555 return -EFAULT;
556
557 /* Tx type sanity check */
558 err = ocelot_ptp_tx_type_to_cmd(cfg.tx_type, &ptp_cmd);
559 if (err)
560 return err;
561
562 switch (cfg.rx_filter) {
563 case HWTSTAMP_FILTER_NONE:
564 break;
565 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
566 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
567 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
568 l4 = true;
569 break;
570 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
571 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
572 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
573 l2 = true;
574 break;
575 case HWTSTAMP_FILTER_PTP_V2_EVENT:
576 case HWTSTAMP_FILTER_PTP_V2_SYNC:
577 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
578 l2 = true;
579 l4 = true;
580 break;
581 default:
582 return -ERANGE;
583 }
584
585 old_l2 = ocelot_port->trap_proto & OCELOT_PROTO_PTP_L2;
586 old_l4 = ocelot_port->trap_proto & OCELOT_PROTO_PTP_L4;
587
588 err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
589 if (err)
590 return err;
591
592 ocelot_port->ptp_cmd = ptp_cmd;
593
594 cfg.rx_filter = ocelot_traps_to_ptp_rx_filter(ocelot_port->trap_proto);
595
596 if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg))) {
597 err = -EFAULT;
598 goto out_restore_ptp_traps;
599 }
600
601 return 0;
602out_restore_ptp_traps:
603 ocelot_setup_ptp_traps(ocelot, port, old_l2, old_l4);
604 ocelot_port->ptp_cmd = old_ptp_cmd;
605 return err;
606}
607EXPORT_SYMBOL(ocelot_hwstamp_set);
608
609int ocelot_get_ts_info(struct ocelot *ocelot, int port,
610 struct kernel_ethtool_ts_info *info)
611{
612 if (ocelot->ptp_clock) {
613 info->phc_index = ptp_clock_index(ocelot->ptp_clock);
614 } else {
615 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
616 return 0;
617 }
618 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
619 SOF_TIMESTAMPING_TX_HARDWARE |
620 SOF_TIMESTAMPING_RX_HARDWARE |
621 SOF_TIMESTAMPING_RAW_HARDWARE;
622 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
623 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
624 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
625 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
626 BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
627 BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
628
629 return 0;
630}
631EXPORT_SYMBOL(ocelot_get_ts_info);
632
633static struct sk_buff *ocelot_port_dequeue_ptp_tx_skb(struct ocelot *ocelot,
634 int port, u8 ts_id,
635 u32 seqid)
636{
637 struct ocelot_port *ocelot_port = ocelot->ports[port];
638 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
639 struct ptp_header *hdr;
640
641 spin_lock(&ocelot->ts_id_lock);
642
643 skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
644 if (OCELOT_SKB_CB(skb)->ts_id != ts_id)
645 continue;
646
647 /* Check that the timestamp ID is for the expected PTP
648 * sequenceId. We don't have to test ptp_parse_header() against
649 * NULL, because we've pre-validated the packet's ptp_class.
650 */
651 hdr = ptp_parse_header(skb, OCELOT_SKB_CB(skb)->ptp_class);
652 if (seqid != ntohs(hdr->sequence_id))
653 continue;
654
655 __skb_unlink(skb, &ocelot_port->tx_skbs);
656 ocelot->ptp_skbs_in_flight--;
657 skb_match = skb;
658 break;
659 }
660
661 spin_unlock(&ocelot->ts_id_lock);
662
663 return skb_match;
664}
665
666static int ocelot_port_queue_ptp_tx_skb(struct ocelot *ocelot, int port,
667 struct sk_buff *clone)
668{
669 struct ocelot_port *ocelot_port = ocelot->ports[port];
670 DECLARE_BITMAP(ts_id_in_flight, OCELOT_MAX_PTP_ID);
671 struct sk_buff *skb, *skb_tmp;
672 unsigned long n;
673
674 spin_lock(&ocelot->ts_id_lock);
675
676 /* To get a better chance of acquiring a timestamp ID, first flush the
677 * stale packets still waiting in the TX timestamping queue. They are
678 * probably lost.
679 */
680 skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
681 if (time_before(OCELOT_SKB_CB(skb)->ptp_tx_time +
682 OCELOT_PTP_TX_TSTAMP_TIMEOUT, jiffies)) {
683 dev_warn_ratelimited(ocelot->dev,
684 "port %d invalidating stale timestamp ID %u which seems lost\n",
685 port, OCELOT_SKB_CB(skb)->ts_id);
686 __skb_unlink(skb, &ocelot_port->tx_skbs);
687 kfree_skb(skb);
688 ocelot->ptp_skbs_in_flight--;
689 } else {
690 __set_bit(OCELOT_SKB_CB(skb)->ts_id, ts_id_in_flight);
691 }
692 }
693
694 if (ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
695 spin_unlock(&ocelot->ts_id_lock);
696 return -EBUSY;
697 }
698
699 n = find_first_zero_bit(ts_id_in_flight, OCELOT_MAX_PTP_ID);
700 if (n == OCELOT_MAX_PTP_ID) {
701 spin_unlock(&ocelot->ts_id_lock);
702 return -EBUSY;
703 }
704
705 /* Found an available timestamp ID, use it */
706 OCELOT_SKB_CB(clone)->ts_id = n;
707 OCELOT_SKB_CB(clone)->ptp_tx_time = jiffies;
708 ocelot->ptp_skbs_in_flight++;
709 __skb_queue_tail(&ocelot_port->tx_skbs, clone);
710
711 spin_unlock(&ocelot->ts_id_lock);
712
713 dev_dbg_ratelimited(ocelot->dev, "port %d timestamp id %lu\n", port, n);
714
715 return 0;
716}
717
718static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
719 unsigned int ptp_class)
720{
721 struct ptp_header *hdr;
722 u8 msgtype, twostep;
723
724 hdr = ptp_parse_header(skb, ptp_class);
725 if (!hdr)
726 return false;
727
728 msgtype = ptp_get_msgtype(hdr, ptp_class);
729 twostep = hdr->flag_field[0] & 0x2;
730
731 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
732 return true;
733
734 return false;
735}
736
737int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
738 struct sk_buff *skb,
739 struct sk_buff **clone)
740{
741 struct ocelot_port *ocelot_port = ocelot->ports[port];
742 u8 ptp_cmd = ocelot_port->ptp_cmd;
743 unsigned int ptp_class;
744 int err;
745
746 /* Don't do anything if PTP timestamping not enabled */
747 if (!ptp_cmd)
748 return 0;
749
750 ptp_class = ptp_classify_raw(skb);
751 if (ptp_class == PTP_CLASS_NONE)
752 return -EINVAL;
753
754 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
755 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
756 if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
757 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
758 return 0;
759 }
760
761 /* Fall back to two-step timestamping */
762 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
763 }
764
765 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
766 *clone = skb_clone_sk(skb);
767 if (!(*clone))
768 return -ENOMEM;
769
770 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
771 err = ocelot_port_queue_ptp_tx_skb(ocelot, port, *clone);
772 if (err) {
773 kfree_skb(*clone);
774 return err;
775 }
776
777 skb_shinfo(*clone)->tx_flags |= SKBTX_IN_PROGRESS;
778 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
779 OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
780 }
781
782 return 0;
783}
784EXPORT_SYMBOL(ocelot_port_txtstamp_request);
785
786static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
787 struct timespec64 *ts)
788{
789 unsigned long flags;
790 u32 val;
791
792 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
793
794 /* Read current PTP time to get seconds */
795 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
796
797 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
798 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
799 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
800 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
801
802 /* Read packet HW timestamp from FIFO */
803 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
804 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
805
806 /* Sec has incremented since the ts was registered */
807 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
808 ts->tv_sec--;
809
810 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
811}
812
813void ocelot_get_txtstamp(struct ocelot *ocelot)
814{
815 int budget = OCELOT_PTP_QUEUE_SZ;
816
817 while (budget--) {
818 struct skb_shared_hwtstamps shhwtstamps;
819 u32 val, id, seqid, txport;
820 struct sk_buff *skb_match;
821 struct timespec64 ts;
822
823 val = ocelot_read(ocelot, SYS_PTP_STATUS);
824
825 /* Check if a timestamp can be retrieved */
826 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
827 break;
828
829 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
830
831 /* Retrieve the ts ID and Tx port */
832 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
833 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
834 seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
835
836 /* Retrieve its associated skb */
837 skb_match = ocelot_port_dequeue_ptp_tx_skb(ocelot, txport, id,
838 seqid);
839 if (!skb_match) {
840 dev_warn_ratelimited(ocelot->dev,
841 "port %d received TX timestamp (seqid %d, ts id %u) for packet previously declared stale\n",
842 txport, seqid, id);
843 goto next_ts;
844 }
845
846 /* Get the h/w timestamp */
847 ocelot_get_hwtimestamp(ocelot, &ts);
848
849 /* Set the timestamp into the skb */
850 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
851 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
852 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
853
854next_ts:
855 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
856 }
857}
858EXPORT_SYMBOL(ocelot_get_txtstamp);
859
860int ocelot_init_timestamp(struct ocelot *ocelot,
861 const struct ptp_clock_info *info)
862{
863 struct ptp_clock *ptp_clock;
864 int i;
865
866 ocelot->ptp_info = *info;
867
868 for (i = 0; i < OCELOT_PTP_PINS_NUM; i++) {
869 struct ptp_pin_desc *p = &ocelot->ptp_pins[i];
870
871 snprintf(p->name, sizeof(p->name), "switch_1588_dat%d", i);
872 p->index = i;
873 p->func = PTP_PF_NONE;
874 }
875
876 ocelot->ptp_info.pin_config = &ocelot->ptp_pins[0];
877
878 ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
879 if (IS_ERR(ptp_clock))
880 return PTR_ERR(ptp_clock);
881 /* Check if PHC support is missing at the configuration level */
882 if (!ptp_clock)
883 return 0;
884
885 ocelot->ptp_clock = ptp_clock;
886
887 ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
888 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
889 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
890
891 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
892
893 return 0;
894}
895EXPORT_SYMBOL(ocelot_init_timestamp);
896
897int ocelot_deinit_timestamp(struct ocelot *ocelot)
898{
899 if (ocelot->ptp_clock)
900 ptp_clock_unregister(ocelot->ptp_clock);
901 return 0;
902}
903EXPORT_SYMBOL(ocelot_deinit_timestamp);
1// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/* Microsemi Ocelot PTP clock driver
3 *
4 * Copyright (c) 2017 Microsemi Corporation
5 * Copyright 2020 NXP
6 */
7#include <linux/time64.h>
8
9#include <linux/dsa/ocelot.h>
10#include <linux/ptp_classify.h>
11#include <soc/mscc/ocelot_ptp.h>
12#include <soc/mscc/ocelot_sys.h>
13#include <soc/mscc/ocelot_vcap.h>
14#include <soc/mscc/ocelot.h>
15#include "ocelot.h"
16
17int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
18{
19 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
20 unsigned long flags;
21 time64_t s;
22 u32 val;
23 s64 ns;
24
25 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
26
27 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
28 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
29 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
30 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
31
32 s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
33 s <<= 32;
34 s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
35 ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
36
37 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
38
39 /* Deal with negative values */
40 if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
41 s--;
42 ns &= 0xf;
43 ns += 999999984;
44 }
45
46 set_normalized_timespec64(ts, s, ns);
47 return 0;
48}
49EXPORT_SYMBOL(ocelot_ptp_gettime64);
50
51int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
52 const struct timespec64 *ts)
53{
54 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
55 unsigned long flags;
56 u32 val;
57
58 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
59
60 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
61 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
62 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
63
64 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
65
66 ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
67 TOD_ACC_PIN);
68 ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
69 TOD_ACC_PIN);
70 ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
71
72 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
73 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
74 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
75
76 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
77
78 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
79
80 if (ocelot->ops->tas_clock_adjust)
81 ocelot->ops->tas_clock_adjust(ocelot);
82
83 return 0;
84}
85EXPORT_SYMBOL(ocelot_ptp_settime64);
86
87int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
88{
89 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
90 struct ocelot *ocelot = container_of(ptp, struct ocelot,
91 ptp_info);
92 unsigned long flags;
93 u32 val;
94
95 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
96
97 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
98 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
99 PTP_PIN_CFG_DOM);
100 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
101
102 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
103
104 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
105 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
106 ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
107
108 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
109 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
110 PTP_PIN_CFG_DOM);
111 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
112
113 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
114
115 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
116
117 if (ocelot->ops->tas_clock_adjust)
118 ocelot->ops->tas_clock_adjust(ocelot);
119 } else {
120 /* Fall back using ocelot_ptp_settime64 which is not exact. */
121 struct timespec64 ts;
122 u64 now;
123
124 ocelot_ptp_gettime64(ptp, &ts);
125
126 now = ktime_to_ns(timespec64_to_ktime(ts));
127 ts = ns_to_timespec64(now + delta);
128
129 ocelot_ptp_settime64(ptp, &ts);
130 }
131
132 return 0;
133}
134EXPORT_SYMBOL(ocelot_ptp_adjtime);
135
136int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
137{
138 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
139 u32 unit = 0, direction = 0;
140 unsigned long flags;
141 u64 adj = 0;
142
143 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
144
145 if (!scaled_ppm)
146 goto disable_adj;
147
148 if (scaled_ppm < 0) {
149 direction = PTP_CFG_CLK_ADJ_CFG_DIR;
150 scaled_ppm = -scaled_ppm;
151 }
152
153 adj = PSEC_PER_SEC << 16;
154 do_div(adj, scaled_ppm);
155 do_div(adj, 1000);
156
157 /* If the adjustment value is too large, use ns instead */
158 if (adj >= (1L << 30)) {
159 unit = PTP_CFG_CLK_ADJ_FREQ_NS;
160 do_div(adj, 1000);
161 }
162
163 /* Still too big */
164 if (adj >= (1L << 30))
165 goto disable_adj;
166
167 ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
168 ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
169 PTP_CLK_CFG_ADJ_CFG);
170
171 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
172 return 0;
173
174disable_adj:
175 ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
176
177 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
178 return 0;
179}
180EXPORT_SYMBOL(ocelot_ptp_adjfine);
181
182int ocelot_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
183 enum ptp_pin_function func, unsigned int chan)
184{
185 switch (func) {
186 case PTP_PF_NONE:
187 case PTP_PF_PEROUT:
188 break;
189 case PTP_PF_EXTTS:
190 case PTP_PF_PHYSYNC:
191 return -1;
192 }
193 return 0;
194}
195EXPORT_SYMBOL(ocelot_ptp_verify);
196
197int ocelot_ptp_enable(struct ptp_clock_info *ptp,
198 struct ptp_clock_request *rq, int on)
199{
200 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
201 struct timespec64 ts_phase, ts_period;
202 enum ocelot_ptp_pins ptp_pin;
203 unsigned long flags;
204 bool pps = false;
205 int pin = -1;
206 s64 wf_high;
207 s64 wf_low;
208 u32 val;
209
210 switch (rq->type) {
211 case PTP_CLK_REQ_PEROUT:
212 /* Reject requests with unsupported flags */
213 if (rq->perout.flags & ~(PTP_PEROUT_DUTY_CYCLE |
214 PTP_PEROUT_PHASE))
215 return -EOPNOTSUPP;
216
217 pin = ptp_find_pin(ocelot->ptp_clock, PTP_PF_PEROUT,
218 rq->perout.index);
219 if (pin == 0)
220 ptp_pin = PTP_PIN_0;
221 else if (pin == 1)
222 ptp_pin = PTP_PIN_1;
223 else if (pin == 2)
224 ptp_pin = PTP_PIN_2;
225 else if (pin == 3)
226 ptp_pin = PTP_PIN_3;
227 else
228 return -EBUSY;
229
230 ts_period.tv_sec = rq->perout.period.sec;
231 ts_period.tv_nsec = rq->perout.period.nsec;
232
233 if (ts_period.tv_sec == 1 && ts_period.tv_nsec == 0)
234 pps = true;
235
236 /* Handle turning off */
237 if (!on) {
238 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
239 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
240 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
241 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
242 break;
243 }
244
245 if (rq->perout.flags & PTP_PEROUT_PHASE) {
246 ts_phase.tv_sec = rq->perout.phase.sec;
247 ts_phase.tv_nsec = rq->perout.phase.nsec;
248 } else {
249 /* Compatibility */
250 ts_phase.tv_sec = rq->perout.start.sec;
251 ts_phase.tv_nsec = rq->perout.start.nsec;
252 }
253 if (ts_phase.tv_sec || (ts_phase.tv_nsec && !pps)) {
254 dev_warn(ocelot->dev,
255 "Absolute start time not supported!\n");
256 dev_warn(ocelot->dev,
257 "Accept nsec for PPS phase adjustment, otherwise start time should be 0 0.\n");
258 return -EINVAL;
259 }
260
261 /* Calculate waveform high and low times */
262 if (rq->perout.flags & PTP_PEROUT_DUTY_CYCLE) {
263 struct timespec64 ts_on;
264
265 ts_on.tv_sec = rq->perout.on.sec;
266 ts_on.tv_nsec = rq->perout.on.nsec;
267
268 wf_high = timespec64_to_ns(&ts_on);
269 } else {
270 if (pps) {
271 wf_high = 1000;
272 } else {
273 wf_high = timespec64_to_ns(&ts_period);
274 wf_high = div_s64(wf_high, 2);
275 }
276 }
277
278 wf_low = timespec64_to_ns(&ts_period);
279 wf_low -= wf_high;
280
281 /* Handle PPS request */
282 if (pps) {
283 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
284 ocelot_write_rix(ocelot, ts_phase.tv_nsec,
285 PTP_PIN_WF_LOW_PERIOD, ptp_pin);
286 ocelot_write_rix(ocelot, wf_high,
287 PTP_PIN_WF_HIGH_PERIOD, ptp_pin);
288 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_CLOCK);
289 val |= PTP_PIN_CFG_SYNC;
290 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
291 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
292 break;
293 }
294
295 /* Handle periodic clock */
296 if (wf_high > 0x3fffffff || wf_high <= 0x6)
297 return -EINVAL;
298 if (wf_low > 0x3fffffff || wf_low <= 0x6)
299 return -EINVAL;
300
301 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
302 ocelot_write_rix(ocelot, wf_low, PTP_PIN_WF_LOW_PERIOD,
303 ptp_pin);
304 ocelot_write_rix(ocelot, wf_high, PTP_PIN_WF_HIGH_PERIOD,
305 ptp_pin);
306 val = PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_CLOCK);
307 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, ptp_pin);
308 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
309 break;
310 default:
311 return -EOPNOTSUPP;
312 }
313 return 0;
314}
315EXPORT_SYMBOL(ocelot_ptp_enable);
316
317static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
318{
319 trap->key_type = OCELOT_VCAP_KEY_ETYPE;
320 *(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588);
321 *(__be16 *)trap->key.etype.etype.mask = htons(0xffff);
322}
323
324static void
325ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
326{
327 trap->key_type = OCELOT_VCAP_KEY_IPV4;
328 trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
329 trap->key.ipv4.proto.mask[0] = 0xff;
330 trap->key.ipv4.dport.value = PTP_EV_PORT;
331 trap->key.ipv4.dport.mask = 0xffff;
332}
333
334static void
335ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
336{
337 trap->key_type = OCELOT_VCAP_KEY_IPV6;
338 trap->key.ipv6.proto.value[0] = IPPROTO_UDP;
339 trap->key.ipv6.proto.mask[0] = 0xff;
340 trap->key.ipv6.dport.value = PTP_EV_PORT;
341 trap->key.ipv6.dport.mask = 0xffff;
342}
343
344static void
345ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
346{
347 trap->key_type = OCELOT_VCAP_KEY_IPV4;
348 trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
349 trap->key.ipv4.proto.mask[0] = 0xff;
350 trap->key.ipv4.dport.value = PTP_GEN_PORT;
351 trap->key.ipv4.dport.mask = 0xffff;
352}
353
354static void
355ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
356{
357 trap->key_type = OCELOT_VCAP_KEY_IPV6;
358 trap->key.ipv6.proto.value[0] = IPPROTO_UDP;
359 trap->key.ipv6.proto.mask[0] = 0xff;
360 trap->key.ipv6.dport.value = PTP_GEN_PORT;
361 trap->key.ipv6.dport.mask = 0xffff;
362}
363
364static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
365{
366 unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
367
368 return ocelot_trap_add(ocelot, port, l2_cookie, true,
369 ocelot_populate_l2_ptp_trap_key);
370}
371
372static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
373{
374 unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
375
376 return ocelot_trap_del(ocelot, port, l2_cookie);
377}
378
379static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
380{
381 unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
382 unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
383 int err;
384
385 err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, true,
386 ocelot_populate_ipv4_ptp_event_trap_key);
387 if (err)
388 return err;
389
390 err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, false,
391 ocelot_populate_ipv4_ptp_general_trap_key);
392 if (err)
393 ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
394
395 return err;
396}
397
398static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
399{
400 unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
401 unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
402 int err;
403
404 err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
405 err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
406 return err;
407}
408
409static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
410{
411 unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
412 unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
413 int err;
414
415 err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, true,
416 ocelot_populate_ipv6_ptp_event_trap_key);
417 if (err)
418 return err;
419
420 err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, false,
421 ocelot_populate_ipv6_ptp_general_trap_key);
422 if (err)
423 ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
424
425 return err;
426}
427
428static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
429{
430 unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
431 unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
432 int err;
433
434 err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
435 err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
436 return err;
437}
438
439static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
440 bool l2, bool l4)
441{
442 struct ocelot_port *ocelot_port = ocelot->ports[port];
443 int err;
444
445 ocelot_port->trap_proto &= ~(OCELOT_PROTO_PTP_L2 |
446 OCELOT_PROTO_PTP_L4);
447
448 if (l2)
449 err = ocelot_l2_ptp_trap_add(ocelot, port);
450 else
451 err = ocelot_l2_ptp_trap_del(ocelot, port);
452 if (err)
453 return err;
454
455 if (l4) {
456 err = ocelot_ipv4_ptp_trap_add(ocelot, port);
457 if (err)
458 goto err_ipv4;
459
460 err = ocelot_ipv6_ptp_trap_add(ocelot, port);
461 if (err)
462 goto err_ipv6;
463 } else {
464 err = ocelot_ipv4_ptp_trap_del(ocelot, port);
465
466 err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
467 }
468 if (err)
469 return err;
470
471 if (l2)
472 ocelot_port->trap_proto |= OCELOT_PROTO_PTP_L2;
473 if (l4)
474 ocelot_port->trap_proto |= OCELOT_PROTO_PTP_L4;
475
476 return 0;
477
478err_ipv6:
479 ocelot_ipv4_ptp_trap_del(ocelot, port);
480err_ipv4:
481 if (l2)
482 ocelot_l2_ptp_trap_del(ocelot, port);
483 return err;
484}
485
486static int ocelot_traps_to_ptp_rx_filter(unsigned int proto)
487{
488 if ((proto & OCELOT_PROTO_PTP_L2) && (proto & OCELOT_PROTO_PTP_L4))
489 return HWTSTAMP_FILTER_PTP_V2_EVENT;
490 else if (proto & OCELOT_PROTO_PTP_L2)
491 return HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
492 else if (proto & OCELOT_PROTO_PTP_L4)
493 return HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
494
495 return HWTSTAMP_FILTER_NONE;
496}
497
498int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
499{
500 struct ocelot_port *ocelot_port = ocelot->ports[port];
501 struct hwtstamp_config cfg = {};
502
503 switch (ocelot_port->ptp_cmd) {
504 case IFH_REW_OP_TWO_STEP_PTP:
505 cfg.tx_type = HWTSTAMP_TX_ON;
506 break;
507 case IFH_REW_OP_ORIGIN_PTP:
508 cfg.tx_type = HWTSTAMP_TX_ONESTEP_SYNC;
509 break;
510 default:
511 cfg.tx_type = HWTSTAMP_TX_OFF;
512 break;
513 }
514
515 cfg.rx_filter = ocelot_traps_to_ptp_rx_filter(ocelot_port->trap_proto);
516
517 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
518}
519EXPORT_SYMBOL(ocelot_hwstamp_get);
520
521int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
522{
523 struct ocelot_port *ocelot_port = ocelot->ports[port];
524 bool l2 = false, l4 = false;
525 struct hwtstamp_config cfg;
526 int err;
527
528 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
529 return -EFAULT;
530
531 /* Tx type sanity check */
532 switch (cfg.tx_type) {
533 case HWTSTAMP_TX_ON:
534 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
535 break;
536 case HWTSTAMP_TX_ONESTEP_SYNC:
537 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
538 * need to update the origin time.
539 */
540 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
541 break;
542 case HWTSTAMP_TX_OFF:
543 ocelot_port->ptp_cmd = 0;
544 break;
545 default:
546 return -ERANGE;
547 }
548
549 switch (cfg.rx_filter) {
550 case HWTSTAMP_FILTER_NONE:
551 break;
552 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
553 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
554 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
555 l4 = true;
556 break;
557 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
558 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
559 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
560 l2 = true;
561 break;
562 case HWTSTAMP_FILTER_PTP_V2_EVENT:
563 case HWTSTAMP_FILTER_PTP_V2_SYNC:
564 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
565 l2 = true;
566 l4 = true;
567 break;
568 default:
569 return -ERANGE;
570 }
571
572 err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
573 if (err)
574 return err;
575
576 cfg.rx_filter = ocelot_traps_to_ptp_rx_filter(ocelot_port->trap_proto);
577
578 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
579}
580EXPORT_SYMBOL(ocelot_hwstamp_set);
581
582int ocelot_get_ts_info(struct ocelot *ocelot, int port,
583 struct ethtool_ts_info *info)
584{
585 info->phc_index = ocelot->ptp_clock ?
586 ptp_clock_index(ocelot->ptp_clock) : -1;
587 if (info->phc_index == -1) {
588 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
589 SOF_TIMESTAMPING_RX_SOFTWARE |
590 SOF_TIMESTAMPING_SOFTWARE;
591 return 0;
592 }
593 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
594 SOF_TIMESTAMPING_RX_SOFTWARE |
595 SOF_TIMESTAMPING_SOFTWARE |
596 SOF_TIMESTAMPING_TX_HARDWARE |
597 SOF_TIMESTAMPING_RX_HARDWARE |
598 SOF_TIMESTAMPING_RAW_HARDWARE;
599 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
600 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
601 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
602 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
603 BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
604 BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
605
606 return 0;
607}
608EXPORT_SYMBOL(ocelot_get_ts_info);
609
610static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
611 struct sk_buff *clone)
612{
613 struct ocelot_port *ocelot_port = ocelot->ports[port];
614 unsigned long flags;
615
616 spin_lock_irqsave(&ocelot->ts_id_lock, flags);
617
618 if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
619 ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
620 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
621 return -EBUSY;
622 }
623
624 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
625 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
626 OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
627
628 ocelot_port->ts_id++;
629 if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
630 ocelot_port->ts_id = 0;
631
632 ocelot_port->ptp_skbs_in_flight++;
633 ocelot->ptp_skbs_in_flight++;
634
635 skb_queue_tail(&ocelot_port->tx_skbs, clone);
636
637 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
638
639 return 0;
640}
641
642static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
643 unsigned int ptp_class)
644{
645 struct ptp_header *hdr;
646 u8 msgtype, twostep;
647
648 hdr = ptp_parse_header(skb, ptp_class);
649 if (!hdr)
650 return false;
651
652 msgtype = ptp_get_msgtype(hdr, ptp_class);
653 twostep = hdr->flag_field[0] & 0x2;
654
655 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
656 return true;
657
658 return false;
659}
660
661int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
662 struct sk_buff *skb,
663 struct sk_buff **clone)
664{
665 struct ocelot_port *ocelot_port = ocelot->ports[port];
666 u8 ptp_cmd = ocelot_port->ptp_cmd;
667 unsigned int ptp_class;
668 int err;
669
670 /* Don't do anything if PTP timestamping not enabled */
671 if (!ptp_cmd)
672 return 0;
673
674 ptp_class = ptp_classify_raw(skb);
675 if (ptp_class == PTP_CLASS_NONE)
676 return -EINVAL;
677
678 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
679 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
680 if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
681 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
682 return 0;
683 }
684
685 /* Fall back to two-step timestamping */
686 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
687 }
688
689 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
690 *clone = skb_clone_sk(skb);
691 if (!(*clone))
692 return -ENOMEM;
693
694 err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
695 if (err)
696 return err;
697
698 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
699 OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
700 }
701
702 return 0;
703}
704EXPORT_SYMBOL(ocelot_port_txtstamp_request);
705
706static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
707 struct timespec64 *ts)
708{
709 unsigned long flags;
710 u32 val;
711
712 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
713
714 /* Read current PTP time to get seconds */
715 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
716
717 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
718 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
719 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
720 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
721
722 /* Read packet HW timestamp from FIFO */
723 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
724 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
725
726 /* Sec has incremented since the ts was registered */
727 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
728 ts->tv_sec--;
729
730 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
731}
732
733static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
734{
735 struct ptp_header *hdr;
736
737 hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
738 if (WARN_ON(!hdr))
739 return false;
740
741 return seqid == ntohs(hdr->sequence_id);
742}
743
744void ocelot_get_txtstamp(struct ocelot *ocelot)
745{
746 int budget = OCELOT_PTP_QUEUE_SZ;
747
748 while (budget--) {
749 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
750 struct skb_shared_hwtstamps shhwtstamps;
751 u32 val, id, seqid, txport;
752 struct ocelot_port *port;
753 struct timespec64 ts;
754 unsigned long flags;
755
756 val = ocelot_read(ocelot, SYS_PTP_STATUS);
757
758 /* Check if a timestamp can be retrieved */
759 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
760 break;
761
762 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
763
764 /* Retrieve the ts ID and Tx port */
765 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
766 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
767 seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
768
769 port = ocelot->ports[txport];
770
771 spin_lock(&ocelot->ts_id_lock);
772 port->ptp_skbs_in_flight--;
773 ocelot->ptp_skbs_in_flight--;
774 spin_unlock(&ocelot->ts_id_lock);
775
776 /* Retrieve its associated skb */
777try_again:
778 spin_lock_irqsave(&port->tx_skbs.lock, flags);
779
780 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
781 if (OCELOT_SKB_CB(skb)->ts_id != id)
782 continue;
783 __skb_unlink(skb, &port->tx_skbs);
784 skb_match = skb;
785 break;
786 }
787
788 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
789
790 if (WARN_ON(!skb_match))
791 continue;
792
793 if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
794 dev_err_ratelimited(ocelot->dev,
795 "port %d received stale TX timestamp for seqid %d, discarding\n",
796 txport, seqid);
797 dev_kfree_skb_any(skb);
798 goto try_again;
799 }
800
801 /* Get the h/w timestamp */
802 ocelot_get_hwtimestamp(ocelot, &ts);
803
804 /* Set the timestamp into the skb */
805 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
806 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
807 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
808
809 /* Next ts */
810 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
811 }
812}
813EXPORT_SYMBOL(ocelot_get_txtstamp);
814
815int ocelot_init_timestamp(struct ocelot *ocelot,
816 const struct ptp_clock_info *info)
817{
818 struct ptp_clock *ptp_clock;
819 int i;
820
821 ocelot->ptp_info = *info;
822
823 for (i = 0; i < OCELOT_PTP_PINS_NUM; i++) {
824 struct ptp_pin_desc *p = &ocelot->ptp_pins[i];
825
826 snprintf(p->name, sizeof(p->name), "switch_1588_dat%d", i);
827 p->index = i;
828 p->func = PTP_PF_NONE;
829 }
830
831 ocelot->ptp_info.pin_config = &ocelot->ptp_pins[0];
832
833 ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
834 if (IS_ERR(ptp_clock))
835 return PTR_ERR(ptp_clock);
836 /* Check if PHC support is missing at the configuration level */
837 if (!ptp_clock)
838 return 0;
839
840 ocelot->ptp_clock = ptp_clock;
841
842 ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
843 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
844 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
845
846 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
847
848 return 0;
849}
850EXPORT_SYMBOL(ocelot_init_timestamp);
851
852int ocelot_deinit_timestamp(struct ocelot *ocelot)
853{
854 if (ocelot->ptp_clock)
855 ptp_clock_unregister(ocelot->ptp_clock);
856 return 0;
857}
858EXPORT_SYMBOL(ocelot_deinit_timestamp);