1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot PTP clock driver
4 * Copyright (c) 2017 Microsemi Corporation
7 #include <linux/time64.h>
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>
17 int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
19 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
25 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
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);
32 s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
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);
37 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
39 /* Deal with negative values */
40 if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
46 set_normalized_timespec64(ts, s, ns);
49 EXPORT_SYMBOL(ocelot_ptp_gettime64);
51 int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
52 const struct timespec64 *ts)
54 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
58 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
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);
64 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
66 ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
68 ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
70 ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
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);
76 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
78 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
80 if (ocelot->ops->tas_clock_adjust)
81 ocelot->ops->tas_clock_adjust(ocelot);
85 EXPORT_SYMBOL(ocelot_ptp_settime64);
87 int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
89 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
90 struct ocelot *ocelot = container_of(ptp, struct ocelot,
95 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
97 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
98 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
100 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
102 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
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);
108 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
109 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK |
111 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
113 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
115 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
117 if (ocelot->ops->tas_clock_adjust)
118 ocelot->ops->tas_clock_adjust(ocelot);
120 /* Fall back using ocelot_ptp_settime64 which is not exact. */
121 struct timespec64 ts;
124 ocelot_ptp_gettime64(ptp, &ts);
126 now = ktime_to_ns(timespec64_to_ktime(ts));
127 ts = ns_to_timespec64(now + delta);
129 ocelot_ptp_settime64(ptp, &ts);
134 EXPORT_SYMBOL(ocelot_ptp_adjtime);
136 int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
138 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
139 u32 unit = 0, direction = 0;
143 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
148 if (scaled_ppm < 0) {
149 direction = PTP_CFG_CLK_ADJ_CFG_DIR;
150 scaled_ppm = -scaled_ppm;
153 adj = PSEC_PER_SEC << 16;
154 do_div(adj, scaled_ppm);
157 /* If the adjustment value is too large, use ns instead */
158 if (adj >= (1L << 30)) {
159 unit = PTP_CFG_CLK_ADJ_FREQ_NS;
164 if (adj >= (1L << 30))
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);
171 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
175 ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
177 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
180 EXPORT_SYMBOL(ocelot_ptp_adjfine);
182 int ocelot_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
183 enum ptp_pin_function func, unsigned int chan)
195 EXPORT_SYMBOL(ocelot_ptp_verify);
197 int ocelot_ptp_enable(struct ptp_clock_info *ptp,
198 struct ptp_clock_request *rq, int on)
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;
211 case PTP_CLK_REQ_PEROUT:
212 /* Reject requests with unsupported flags */
213 if (rq->perout.flags & ~(PTP_PEROUT_DUTY_CYCLE |
217 pin = ptp_find_pin(ocelot->ptp_clock, PTP_PF_PEROUT,
230 ts_period.tv_sec = rq->perout.period.sec;
231 ts_period.tv_nsec = rq->perout.period.nsec;
233 if (ts_period.tv_sec == 1 && ts_period.tv_nsec == 0)
236 /* Handle turning off */
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);
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;
250 ts_phase.tv_sec = rq->perout.start.sec;
251 ts_phase.tv_nsec = rq->perout.start.nsec;
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");
261 /* Calculate waveform high and low times */
262 if (rq->perout.flags & PTP_PEROUT_DUTY_CYCLE) {
263 struct timespec64 ts_on;
265 ts_on.tv_sec = rq->perout.on.sec;
266 ts_on.tv_nsec = rq->perout.on.nsec;
268 wf_high = timespec64_to_ns(&ts_on);
273 wf_high = timespec64_to_ns(&ts_period);
274 wf_high = div_s64(wf_high, 2);
278 wf_low = timespec64_to_ns(&ts_period);
281 /* Handle PPS request */
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);
295 /* Handle periodic clock */
296 if (wf_high > 0x3fffffff || wf_high <= 0x6)
298 if (wf_low > 0x3fffffff || wf_low <= 0x6)
301 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
302 ocelot_write_rix(ocelot, wf_low, PTP_PIN_WF_LOW_PERIOD,
304 ocelot_write_rix(ocelot, wf_high, PTP_PIN_WF_HIGH_PERIOD,
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);
315 EXPORT_SYMBOL(ocelot_ptp_enable);
317 static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
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);
325 ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
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;
335 ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
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;
345 ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
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;
355 ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
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;
364 static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
366 unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
368 return ocelot_trap_add(ocelot, port, l2_cookie, true,
369 ocelot_populate_l2_ptp_trap_key);
372 static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
374 unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
376 return ocelot_trap_del(ocelot, port, l2_cookie);
379 static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
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);
385 err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, true,
386 ocelot_populate_ipv4_ptp_event_trap_key);
390 err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, false,
391 ocelot_populate_ipv4_ptp_general_trap_key);
393 ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
398 static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
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);
404 err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
405 err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
409 static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
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);
415 err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, true,
416 ocelot_populate_ipv6_ptp_event_trap_key);
420 err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, false,
421 ocelot_populate_ipv6_ptp_general_trap_key);
423 ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
428 static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
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);
434 err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
435 err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
439 static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
442 struct ocelot_port *ocelot_port = ocelot->ports[port];
445 ocelot_port->trap_proto &= ~(OCELOT_PROTO_PTP_L2 |
446 OCELOT_PROTO_PTP_L4);
449 err = ocelot_l2_ptp_trap_add(ocelot, port);
451 err = ocelot_l2_ptp_trap_del(ocelot, port);
456 err = ocelot_ipv4_ptp_trap_add(ocelot, port);
460 err = ocelot_ipv6_ptp_trap_add(ocelot, port);
464 err = ocelot_ipv4_ptp_trap_del(ocelot, port);
466 err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
472 ocelot_port->trap_proto |= OCELOT_PROTO_PTP_L2;
474 ocelot_port->trap_proto |= OCELOT_PROTO_PTP_L4;
479 ocelot_ipv4_ptp_trap_del(ocelot, port);
482 ocelot_l2_ptp_trap_del(ocelot, port);
486 static int ocelot_traps_to_ptp_rx_filter(unsigned int proto)
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;
495 return HWTSTAMP_FILTER_NONE;
498 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
500 struct ocelot_port *ocelot_port = ocelot->ports[port];
501 struct hwtstamp_config cfg = {};
503 switch (ocelot_port->ptp_cmd) {
504 case IFH_REW_OP_TWO_STEP_PTP:
505 cfg.tx_type = HWTSTAMP_TX_ON;
507 case IFH_REW_OP_ORIGIN_PTP:
508 cfg.tx_type = HWTSTAMP_TX_ONESTEP_SYNC;
511 cfg.tx_type = HWTSTAMP_TX_OFF;
515 cfg.rx_filter = ocelot_traps_to_ptp_rx_filter(ocelot_port->trap_proto);
517 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
519 EXPORT_SYMBOL(ocelot_hwstamp_get);
521 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
523 struct ocelot_port *ocelot_port = ocelot->ports[port];
524 bool l2 = false, l4 = false;
525 struct hwtstamp_config cfg;
528 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
531 /* Tx type sanity check */
532 switch (cfg.tx_type) {
534 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
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.
540 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
542 case HWTSTAMP_TX_OFF:
543 ocelot_port->ptp_cmd = 0;
549 switch (cfg.rx_filter) {
550 case HWTSTAMP_FILTER_NONE:
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:
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:
562 case HWTSTAMP_FILTER_PTP_V2_EVENT:
563 case HWTSTAMP_FILTER_PTP_V2_SYNC:
564 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
572 err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
576 cfg.rx_filter = ocelot_traps_to_ptp_rx_filter(ocelot_port->trap_proto);
578 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
580 EXPORT_SYMBOL(ocelot_hwstamp_set);
582 int ocelot_get_ts_info(struct ocelot *ocelot, int port,
583 struct ethtool_ts_info *info)
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;
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);
608 EXPORT_SYMBOL(ocelot_get_ts_info);
610 static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
611 struct sk_buff *clone)
613 struct ocelot_port *ocelot_port = ocelot->ports[port];
616 spin_lock_irqsave(&ocelot->ts_id_lock, flags);
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);
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;
628 ocelot_port->ts_id++;
629 if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
630 ocelot_port->ts_id = 0;
632 ocelot_port->ptp_skbs_in_flight++;
633 ocelot->ptp_skbs_in_flight++;
635 skb_queue_tail(&ocelot_port->tx_skbs, clone);
637 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
642 static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
643 unsigned int ptp_class)
645 struct ptp_header *hdr;
648 hdr = ptp_parse_header(skb, ptp_class);
652 msgtype = ptp_get_msgtype(hdr, ptp_class);
653 twostep = hdr->flag_field[0] & 0x2;
655 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
661 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
663 struct sk_buff **clone)
665 struct ocelot_port *ocelot_port = ocelot->ports[port];
666 u8 ptp_cmd = ocelot_port->ptp_cmd;
667 unsigned int ptp_class;
670 /* Don't do anything if PTP timestamping not enabled */
674 ptp_class = ptp_classify_raw(skb);
675 if (ptp_class == PTP_CLASS_NONE)
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;
685 /* Fall back to two-step timestamping */
686 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
689 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
690 *clone = skb_clone_sk(skb);
694 err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
698 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
699 OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
704 EXPORT_SYMBOL(ocelot_port_txtstamp_request);
706 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
707 struct timespec64 *ts)
712 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
714 /* Read current PTP time to get seconds */
715 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
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);
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);
726 /* Sec has incremented since the ts was registered */
727 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
730 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
733 static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
735 struct ptp_header *hdr;
737 hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
741 return seqid == ntohs(hdr->sequence_id);
744 void ocelot_get_txtstamp(struct ocelot *ocelot)
746 int budget = OCELOT_PTP_QUEUE_SZ;
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;
756 val = ocelot_read(ocelot, SYS_PTP_STATUS);
758 /* Check if a timestamp can be retrieved */
759 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
762 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
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);
769 port = ocelot->ports[txport];
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);
776 /* Retrieve its associated skb */
778 spin_lock_irqsave(&port->tx_skbs.lock, flags);
780 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
781 if (OCELOT_SKB_CB(skb)->ts_id != id)
783 __skb_unlink(skb, &port->tx_skbs);
788 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
790 if (WARN_ON(!skb_match))
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",
797 dev_kfree_skb_any(skb);
801 /* Get the h/w timestamp */
802 ocelot_get_hwtimestamp(ocelot, &ts);
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);
810 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
813 EXPORT_SYMBOL(ocelot_get_txtstamp);
815 int ocelot_init_timestamp(struct ocelot *ocelot,
816 const struct ptp_clock_info *info)
818 struct ptp_clock *ptp_clock;
821 ocelot->ptp_info = *info;
823 for (i = 0; i < OCELOT_PTP_PINS_NUM; i++) {
824 struct ptp_pin_desc *p = &ocelot->ptp_pins[i];
826 snprintf(p->name, sizeof(p->name), "switch_1588_dat%d", i);
828 p->func = PTP_PF_NONE;
831 ocelot->ptp_info.pin_config = &ocelot->ptp_pins[0];
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 */
840 ocelot->ptp_clock = ptp_clock;
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);
846 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
850 EXPORT_SYMBOL(ocelot_init_timestamp);
852 int ocelot_deinit_timestamp(struct ocelot *ocelot)
854 if (ocelot->ptp_clock)
855 ptp_clock_unregister(ocelot->ptp_clock);
858 EXPORT_SYMBOL(ocelot_deinit_timestamp);