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,
445 err = ocelot_l2_ptp_trap_add(ocelot, port);
447 err = ocelot_l2_ptp_trap_del(ocelot, port);
452 err = ocelot_ipv4_ptp_trap_add(ocelot, port);
456 err = ocelot_ipv6_ptp_trap_add(ocelot, port);
460 err = ocelot_ipv4_ptp_trap_del(ocelot, port);
462 err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
470 ocelot_ipv4_ptp_trap_del(ocelot, port);
473 ocelot_l2_ptp_trap_del(ocelot, port);
477 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
479 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
480 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
482 EXPORT_SYMBOL(ocelot_hwstamp_get);
484 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
486 struct ocelot_port *ocelot_port = ocelot->ports[port];
487 bool l2 = false, l4 = false;
488 struct hwtstamp_config cfg;
491 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
494 /* Tx type sanity check */
495 switch (cfg.tx_type) {
497 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
499 case HWTSTAMP_TX_ONESTEP_SYNC:
500 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
501 * need to update the origin time.
503 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
505 case HWTSTAMP_TX_OFF:
506 ocelot_port->ptp_cmd = 0;
512 mutex_lock(&ocelot->ptp_lock);
514 switch (cfg.rx_filter) {
515 case HWTSTAMP_FILTER_NONE:
517 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
518 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
519 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
522 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
523 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
524 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
527 case HWTSTAMP_FILTER_PTP_V2_EVENT:
528 case HWTSTAMP_FILTER_PTP_V2_SYNC:
529 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
534 mutex_unlock(&ocelot->ptp_lock);
538 err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
540 mutex_unlock(&ocelot->ptp_lock);
545 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
547 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
549 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
551 cfg.rx_filter = HWTSTAMP_FILTER_NONE;
553 /* Commit back the result & save it */
554 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
555 mutex_unlock(&ocelot->ptp_lock);
557 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
559 EXPORT_SYMBOL(ocelot_hwstamp_set);
561 int ocelot_get_ts_info(struct ocelot *ocelot, int port,
562 struct ethtool_ts_info *info)
564 info->phc_index = ocelot->ptp_clock ?
565 ptp_clock_index(ocelot->ptp_clock) : -1;
566 if (info->phc_index == -1) {
567 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
568 SOF_TIMESTAMPING_RX_SOFTWARE |
569 SOF_TIMESTAMPING_SOFTWARE;
572 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
573 SOF_TIMESTAMPING_RX_SOFTWARE |
574 SOF_TIMESTAMPING_SOFTWARE |
575 SOF_TIMESTAMPING_TX_HARDWARE |
576 SOF_TIMESTAMPING_RX_HARDWARE |
577 SOF_TIMESTAMPING_RAW_HARDWARE;
578 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
579 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
580 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
581 BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
582 BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
583 BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
587 EXPORT_SYMBOL(ocelot_get_ts_info);
589 static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
590 struct sk_buff *clone)
592 struct ocelot_port *ocelot_port = ocelot->ports[port];
595 spin_lock_irqsave(&ocelot->ts_id_lock, flags);
597 if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
598 ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
599 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
603 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
604 /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
605 OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
607 ocelot_port->ts_id++;
608 if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
609 ocelot_port->ts_id = 0;
611 ocelot_port->ptp_skbs_in_flight++;
612 ocelot->ptp_skbs_in_flight++;
614 skb_queue_tail(&ocelot_port->tx_skbs, clone);
616 spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
621 static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
622 unsigned int ptp_class)
624 struct ptp_header *hdr;
627 hdr = ptp_parse_header(skb, ptp_class);
631 msgtype = ptp_get_msgtype(hdr, ptp_class);
632 twostep = hdr->flag_field[0] & 0x2;
634 if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
640 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
642 struct sk_buff **clone)
644 struct ocelot_port *ocelot_port = ocelot->ports[port];
645 u8 ptp_cmd = ocelot_port->ptp_cmd;
646 unsigned int ptp_class;
649 /* Don't do anything if PTP timestamping not enabled */
653 ptp_class = ptp_classify_raw(skb);
654 if (ptp_class == PTP_CLASS_NONE)
657 /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
658 if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
659 if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
660 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
664 /* Fall back to two-step timestamping */
665 ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
668 if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
669 *clone = skb_clone_sk(skb);
673 err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
677 OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
678 OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
683 EXPORT_SYMBOL(ocelot_port_txtstamp_request);
685 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
686 struct timespec64 *ts)
691 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
693 /* Read current PTP time to get seconds */
694 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
696 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
697 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
698 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
699 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
701 /* Read packet HW timestamp from FIFO */
702 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
703 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
705 /* Sec has incremented since the ts was registered */
706 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
709 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
712 static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
714 struct ptp_header *hdr;
716 hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
720 return seqid == ntohs(hdr->sequence_id);
723 void ocelot_get_txtstamp(struct ocelot *ocelot)
725 int budget = OCELOT_PTP_QUEUE_SZ;
728 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
729 struct skb_shared_hwtstamps shhwtstamps;
730 u32 val, id, seqid, txport;
731 struct ocelot_port *port;
732 struct timespec64 ts;
735 val = ocelot_read(ocelot, SYS_PTP_STATUS);
737 /* Check if a timestamp can be retrieved */
738 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
741 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
743 /* Retrieve the ts ID and Tx port */
744 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
745 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
746 seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
748 port = ocelot->ports[txport];
750 spin_lock(&ocelot->ts_id_lock);
751 port->ptp_skbs_in_flight--;
752 ocelot->ptp_skbs_in_flight--;
753 spin_unlock(&ocelot->ts_id_lock);
755 /* Retrieve its associated skb */
757 spin_lock_irqsave(&port->tx_skbs.lock, flags);
759 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
760 if (OCELOT_SKB_CB(skb)->ts_id != id)
762 __skb_unlink(skb, &port->tx_skbs);
767 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
769 if (WARN_ON(!skb_match))
772 if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
773 dev_err_ratelimited(ocelot->dev,
774 "port %d received stale TX timestamp for seqid %d, discarding\n",
776 dev_kfree_skb_any(skb);
780 /* Get the h/w timestamp */
781 ocelot_get_hwtimestamp(ocelot, &ts);
783 /* Set the timestamp into the skb */
784 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
785 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
786 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
789 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
792 EXPORT_SYMBOL(ocelot_get_txtstamp);
794 int ocelot_init_timestamp(struct ocelot *ocelot,
795 const struct ptp_clock_info *info)
797 struct ptp_clock *ptp_clock;
800 ocelot->ptp_info = *info;
802 for (i = 0; i < OCELOT_PTP_PINS_NUM; i++) {
803 struct ptp_pin_desc *p = &ocelot->ptp_pins[i];
805 snprintf(p->name, sizeof(p->name), "switch_1588_dat%d", i);
807 p->func = PTP_PF_NONE;
810 ocelot->ptp_info.pin_config = &ocelot->ptp_pins[0];
812 ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
813 if (IS_ERR(ptp_clock))
814 return PTR_ERR(ptp_clock);
815 /* Check if PHC support is missing at the configuration level */
819 ocelot->ptp_clock = ptp_clock;
821 ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
822 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
823 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
825 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
827 /* There is no device reconfiguration, PTP Rx stamping is always
830 ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
834 EXPORT_SYMBOL(ocelot_init_timestamp);
836 int ocelot_deinit_timestamp(struct ocelot *ocelot)
838 if (ocelot->ptp_clock)
839 ptp_clock_unregister(ocelot->ptp_clock);
842 EXPORT_SYMBOL(ocelot_deinit_timestamp);