1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/ptp_classify.h>
5 #include "lan966x_main.h"
7 #include "vcap_api_client.h"
9 #define LAN966X_MAX_PTP_ID 512
11 /* Represents 1ppm adjustment in 2^59 format with 6.037735849ns as reference
12 * The value is calculated as following: (1/1000000)/((2^-59)/6.037735849)
14 #define LAN966X_1PPM_FORMAT 3480517749723LL
16 /* Represents 1ppb adjustment in 2^29 format with 6.037735849ns as reference
17 * The value is calculated as following: (1/1000000000)/((2^59)/6.037735849)
19 #define LAN966X_1PPB_FORMAT 3480517749LL
21 #define TOD_ACC_PIN 0x7
23 /* This represents the base rule ID for the PTP rules that are added in the
24 * VCAP to trap frames to CPU. This number needs to be bigger than the maximum
25 * number of entries that can exist in the VCAP.
27 #define LAN966X_VCAP_PTP_RULE_ID 1000000
28 #define LAN966X_VCAP_L2_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 0)
29 #define LAN966X_VCAP_IPV4_EV_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 1)
30 #define LAN966X_VCAP_IPV4_GEN_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 2)
31 #define LAN966X_VCAP_IPV6_EV_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 3)
32 #define LAN966X_VCAP_IPV6_GEN_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 4)
35 PTP_PIN_ACTION_IDLE = 0,
43 static u64 lan966x_ptp_get_nominal_value(void)
45 /* This is the default value that for each system clock, the time of day
46 * is increased. It has the format 5.59 nanosecond.
48 return 0x304d4873ecade305;
51 static int lan966x_ptp_add_trap(struct lan966x_port *port,
52 int (*add_ptp_key)(struct vcap_rule *vrule,
53 struct lan966x_port*),
57 struct lan966x *lan966x = port->lan966x;
58 struct vcap_rule *vrule;
61 vrule = vcap_get_rule(lan966x->vcap_ctrl, rule_id);
65 /* Just modify the ingress port mask and exit */
66 vcap_rule_get_key_u32(vrule, VCAP_KF_IF_IGR_PORT_MASK,
68 mask &= ~BIT(port->chip_port);
69 vcap_rule_mod_key_u32(vrule, VCAP_KF_IF_IGR_PORT_MASK,
72 err = vcap_mod_rule(vrule);
76 vrule = vcap_alloc_rule(lan966x->vcap_ctrl, port->dev,
77 LAN966X_VCAP_CID_IS2_L0,
78 VCAP_USER_PTP, 0, rule_id);
80 return PTR_ERR(vrule);
82 err = add_ptp_key(vrule, port);
86 err = vcap_rule_add_action_bit(vrule, VCAP_AF_CPU_COPY_ENA, VCAP_BIT_1);
87 err |= vcap_rule_add_action_u32(vrule, VCAP_AF_MASK_MODE, LAN966X_PMM_REPLACE);
88 err |= vcap_val_rule(vrule, proto);
92 err = vcap_add_rule(vrule);
95 /* Free the local copy of the rule */
96 vcap_free_rule(vrule);
100 static int lan966x_ptp_del_trap(struct lan966x_port *port,
103 struct lan966x *lan966x = port->lan966x;
104 struct vcap_rule *vrule;
108 vrule = vcap_get_rule(lan966x->vcap_ctrl, rule_id);
112 vcap_rule_get_key_u32(vrule, VCAP_KF_IF_IGR_PORT_MASK, &value, &mask);
113 mask |= BIT(port->chip_port);
115 /* No other port requires this trap, so it is safe to remove it */
116 if (mask == GENMASK(lan966x->num_phys_ports, 0)) {
117 err = vcap_del_rule(lan966x->vcap_ctrl, port->dev, rule_id);
121 vcap_rule_mod_key_u32(vrule, VCAP_KF_IF_IGR_PORT_MASK, value, mask);
122 err = vcap_mod_rule(vrule);
125 vcap_free_rule(vrule);
129 static int lan966x_ptp_add_l2_key(struct vcap_rule *vrule,
130 struct lan966x_port *port)
132 return vcap_rule_add_key_u32(vrule, VCAP_KF_ETYPE, ETH_P_1588, ~0);
135 static int lan966x_ptp_add_ip_event_key(struct vcap_rule *vrule,
136 struct lan966x_port *port)
138 return vcap_rule_add_key_u32(vrule, VCAP_KF_L4_DPORT, PTP_EV_PORT, ~0) ||
139 vcap_rule_add_key_bit(vrule, VCAP_KF_TCP_IS, VCAP_BIT_0);
142 static int lan966x_ptp_add_ip_general_key(struct vcap_rule *vrule,
143 struct lan966x_port *port)
145 return vcap_rule_add_key_u32(vrule, VCAP_KF_L4_DPORT, PTP_GEN_PORT, ~0) ||
146 vcap_rule_add_key_bit(vrule, VCAP_KF_TCP_IS, VCAP_BIT_0);
149 static int lan966x_ptp_add_l2_rule(struct lan966x_port *port)
151 return lan966x_ptp_add_trap(port, lan966x_ptp_add_l2_key,
152 LAN966X_VCAP_L2_PTP_TRAP, ETH_P_ALL);
155 static int lan966x_ptp_add_ipv4_rules(struct lan966x_port *port)
159 err = lan966x_ptp_add_trap(port, lan966x_ptp_add_ip_event_key,
160 LAN966X_VCAP_IPV4_EV_PTP_TRAP, ETH_P_IP);
164 err = lan966x_ptp_add_trap(port, lan966x_ptp_add_ip_general_key,
165 LAN966X_VCAP_IPV4_GEN_PTP_TRAP, ETH_P_IP);
167 lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV4_EV_PTP_TRAP);
172 static int lan966x_ptp_add_ipv6_rules(struct lan966x_port *port)
176 err = lan966x_ptp_add_trap(port, lan966x_ptp_add_ip_event_key,
177 LAN966X_VCAP_IPV6_EV_PTP_TRAP, ETH_P_IPV6);
181 err = lan966x_ptp_add_trap(port, lan966x_ptp_add_ip_general_key,
182 LAN966X_VCAP_IPV6_GEN_PTP_TRAP, ETH_P_IPV6);
184 lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV6_EV_PTP_TRAP);
189 static int lan966x_ptp_del_l2_rule(struct lan966x_port *port)
191 return lan966x_ptp_del_trap(port, LAN966X_VCAP_L2_PTP_TRAP);
194 static int lan966x_ptp_del_ipv4_rules(struct lan966x_port *port)
198 err = lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV4_EV_PTP_TRAP);
199 err |= lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV4_GEN_PTP_TRAP);
204 static int lan966x_ptp_del_ipv6_rules(struct lan966x_port *port)
208 err = lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV6_EV_PTP_TRAP);
209 err |= lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV6_GEN_PTP_TRAP);
214 static int lan966x_ptp_add_traps(struct lan966x_port *port)
218 err = lan966x_ptp_add_l2_rule(port);
222 err = lan966x_ptp_add_ipv4_rules(port);
226 err = lan966x_ptp_add_ipv6_rules(port);
233 lan966x_ptp_del_ipv4_rules(port);
235 lan966x_ptp_del_l2_rule(port);
240 int lan966x_ptp_del_traps(struct lan966x_port *port)
244 err = lan966x_ptp_del_l2_rule(port);
245 err |= lan966x_ptp_del_ipv4_rules(port);
246 err |= lan966x_ptp_del_ipv6_rules(port);
251 int lan966x_ptp_setup_traps(struct lan966x_port *port,
252 struct kernel_hwtstamp_config *cfg)
254 if (cfg->rx_filter == HWTSTAMP_FILTER_NONE)
255 return lan966x_ptp_del_traps(port);
257 return lan966x_ptp_add_traps(port);
260 int lan966x_ptp_hwtstamp_set(struct lan966x_port *port,
261 struct kernel_hwtstamp_config *cfg,
262 struct netlink_ext_ack *extack)
264 struct lan966x *lan966x = port->lan966x;
265 struct lan966x_phc *phc;
267 switch (cfg->tx_type) {
269 port->ptp_tx_cmd = IFH_REW_OP_TWO_STEP_PTP;
271 case HWTSTAMP_TX_ONESTEP_SYNC:
272 port->ptp_tx_cmd = IFH_REW_OP_ONE_STEP_PTP;
274 case HWTSTAMP_TX_OFF:
275 port->ptp_tx_cmd = IFH_REW_OP_NOOP;
281 switch (cfg->rx_filter) {
282 case HWTSTAMP_FILTER_NONE:
283 port->ptp_rx_cmd = false;
285 case HWTSTAMP_FILTER_ALL:
286 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
287 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
288 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
289 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
290 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
291 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
292 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
293 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
294 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
295 case HWTSTAMP_FILTER_PTP_V2_EVENT:
296 case HWTSTAMP_FILTER_PTP_V2_SYNC:
297 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
298 case HWTSTAMP_FILTER_NTP_ALL:
299 port->ptp_rx_cmd = true;
300 cfg->rx_filter = HWTSTAMP_FILTER_ALL;
306 /* Commit back the result & save it */
307 mutex_lock(&lan966x->ptp_lock);
308 phc = &lan966x->phc[LAN966X_PHC_PORT];
309 phc->hwtstamp_config = *cfg;
310 mutex_unlock(&lan966x->ptp_lock);
315 void lan966x_ptp_hwtstamp_get(struct lan966x_port *port,
316 struct kernel_hwtstamp_config *cfg)
318 struct lan966x *lan966x = port->lan966x;
319 struct lan966x_phc *phc;
321 phc = &lan966x->phc[LAN966X_PHC_PORT];
322 *cfg = phc->hwtstamp_config;
325 static int lan966x_ptp_classify(struct lan966x_port *port, struct sk_buff *skb)
327 struct ptp_header *header;
331 if (port->ptp_tx_cmd == IFH_REW_OP_NOOP)
332 return IFH_REW_OP_NOOP;
334 type = ptp_classify_raw(skb);
335 if (type == PTP_CLASS_NONE)
336 return IFH_REW_OP_NOOP;
338 header = ptp_parse_header(skb, type);
340 return IFH_REW_OP_NOOP;
342 if (port->ptp_tx_cmd == IFH_REW_OP_TWO_STEP_PTP)
343 return IFH_REW_OP_TWO_STEP_PTP;
345 /* If it is sync and run 1 step then set the correct operation,
346 * otherwise run as 2 step
348 msgtype = ptp_get_msgtype(header, type);
349 if ((msgtype & 0xf) == 0)
350 return IFH_REW_OP_ONE_STEP_PTP;
352 return IFH_REW_OP_TWO_STEP_PTP;
355 static void lan966x_ptp_txtstamp_old_release(struct lan966x_port *port)
357 struct sk_buff *skb, *skb_tmp;
360 spin_lock_irqsave(&port->tx_skbs.lock, flags);
361 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
362 if time_after(LAN966X_SKB_CB(skb)->jiffies + LAN966X_PTP_TIMEOUT,
366 __skb_unlink(skb, &port->tx_skbs);
367 dev_kfree_skb_any(skb);
369 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
372 int lan966x_ptp_txtstamp_request(struct lan966x_port *port,
375 struct lan966x *lan966x = port->lan966x;
379 rew_op = lan966x_ptp_classify(port, skb);
380 LAN966X_SKB_CB(skb)->rew_op = rew_op;
382 if (rew_op != IFH_REW_OP_TWO_STEP_PTP)
385 lan966x_ptp_txtstamp_old_release(port);
387 spin_lock_irqsave(&lan966x->ptp_ts_id_lock, flags);
388 if (lan966x->ptp_skbs == LAN966X_MAX_PTP_ID) {
389 spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
393 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
395 skb_queue_tail(&port->tx_skbs, skb);
396 LAN966X_SKB_CB(skb)->ts_id = port->ts_id;
397 LAN966X_SKB_CB(skb)->jiffies = jiffies;
401 if (port->ts_id == LAN966X_MAX_PTP_ID)
404 spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
409 void lan966x_ptp_txtstamp_release(struct lan966x_port *port,
412 struct lan966x *lan966x = port->lan966x;
415 spin_lock_irqsave(&lan966x->ptp_ts_id_lock, flags);
418 skb_unlink(skb, &port->tx_skbs);
419 spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
422 static void lan966x_get_hwtimestamp(struct lan966x *lan966x,
423 struct timespec64 *ts,
426 /* Read current PTP time to get seconds */
430 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
432 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
433 PTP_PIN_CFG_PIN_DOM_SET(LAN966X_PHC_PORT) |
434 PTP_PIN_CFG_PIN_SYNC_SET(0),
435 PTP_PIN_CFG_PIN_ACTION |
436 PTP_PIN_CFG_PIN_DOM |
437 PTP_PIN_CFG_PIN_SYNC,
438 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
440 ts->tv_sec = lan_rd(lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
441 curr_nsec = lan_rd(lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
445 /* Sec has incremented since the ts was registered */
446 if (curr_nsec < nsec)
449 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
452 irqreturn_t lan966x_ptp_irq_handler(int irq, void *args)
454 int budget = LAN966X_MAX_PTP_ID;
455 struct lan966x *lan966x = args;
458 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
459 struct skb_shared_hwtstamps shhwtstamps;
460 struct lan966x_port *port;
461 struct timespec64 ts;
466 val = lan_rd(lan966x, PTP_TWOSTEP_CTRL);
468 /* Check if a timestamp can be retrieved */
469 if (!(val & PTP_TWOSTEP_CTRL_VLD))
472 WARN_ON(val & PTP_TWOSTEP_CTRL_OVFL);
474 if (!(val & PTP_TWOSTEP_CTRL_STAMP_TX))
477 /* Retrieve the ts Tx port */
478 txport = PTP_TWOSTEP_CTRL_STAMP_PORT_GET(val);
480 /* Retrieve its associated skb */
481 port = lan966x->ports[txport];
483 /* Retrieve the delay */
484 delay = lan_rd(lan966x, PTP_TWOSTEP_STAMP);
485 delay = PTP_TWOSTEP_STAMP_STAMP_NSEC_GET(delay);
487 /* Get next timestamp from fifo, which needs to be the
488 * rx timestamp which represents the id of the frame
490 lan_rmw(PTP_TWOSTEP_CTRL_NXT_SET(1),
491 PTP_TWOSTEP_CTRL_NXT,
492 lan966x, PTP_TWOSTEP_CTRL);
494 val = lan_rd(lan966x, PTP_TWOSTEP_CTRL);
496 /* Check if a timestamp can be retried */
497 if (!(val & PTP_TWOSTEP_CTRL_VLD))
500 /* Read RX timestamping to get the ID */
501 id = lan_rd(lan966x, PTP_TWOSTEP_STAMP);
503 spin_lock_irqsave(&port->tx_skbs.lock, flags);
504 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
505 if (LAN966X_SKB_CB(skb)->ts_id != id)
508 __skb_unlink(skb, &port->tx_skbs);
512 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
515 lan_rmw(PTP_TWOSTEP_CTRL_NXT_SET(1),
516 PTP_TWOSTEP_CTRL_NXT,
517 lan966x, PTP_TWOSTEP_CTRL);
519 if (WARN_ON(!skb_match))
522 spin_lock_irqsave(&lan966x->ptp_ts_id_lock, flags);
524 spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
526 /* Get the h/w timestamp */
527 lan966x_get_hwtimestamp(lan966x, &ts, delay);
529 /* Set the timestamp into the skb */
530 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
531 skb_tstamp_tx(skb_match, &shhwtstamps);
533 dev_kfree_skb_any(skb_match);
539 irqreturn_t lan966x_ptp_ext_irq_handler(int irq, void *args)
541 struct lan966x *lan966x = args;
542 struct lan966x_phc *phc;
549 if (!(lan_rd(lan966x, PTP_PIN_INTR)))
552 /* Go through all domains and see which pin generated the interrupt */
553 for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
554 struct ptp_clock_event ptp_event = {0};
556 phc = &lan966x->phc[i];
557 pin = ptp_find_pin_unlocked(phc->clock, PTP_PF_EXTTS, 0);
561 if (!(lan_rd(lan966x, PTP_PIN_INTR) & BIT(pin)))
564 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
566 /* Enable to get the new interrupt.
567 * By writing 1 it clears the bit
569 lan_wr(BIT(pin), lan966x, PTP_PIN_INTR);
571 /* Get current time */
572 s = lan_rd(lan966x, PTP_TOD_SEC_MSB(pin));
574 s |= lan_rd(lan966x, PTP_TOD_SEC_LSB(pin));
575 ns = lan_rd(lan966x, PTP_TOD_NSEC(pin));
576 ns &= PTP_TOD_NSEC_TOD_NSEC;
578 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
580 if ((ns & 0xFFFFFFF0) == 0x3FFFFFF0) {
585 time = ktime_set(s, ns);
587 ptp_event.index = pin;
588 ptp_event.timestamp = time;
589 ptp_event.type = PTP_CLOCK_EXTTS;
590 ptp_clock_event(phc->clock, &ptp_event);
596 static int lan966x_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
598 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
599 struct lan966x *lan966x = phc->lan966x;
608 if (scaled_ppm < 0) {
610 scaled_ppm = -scaled_ppm;
613 tod_inc = lan966x_ptp_get_nominal_value();
615 /* The multiplication is split in 2 separate additions because of
616 * overflow issues. If scaled_ppm with 16bit fractional part was bigger
617 * than 20ppm then we got overflow.
619 ref = LAN966X_1PPM_FORMAT * (scaled_ppm >> 16);
620 ref += (LAN966X_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
621 tod_inc = neg_adj ? tod_inc - ref : tod_inc + ref;
623 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
625 lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(1 << BIT(phc->index)),
626 PTP_DOM_CFG_CLKCFG_DIS,
627 lan966x, PTP_DOM_CFG);
629 lan_wr((u32)tod_inc & 0xFFFFFFFF, lan966x,
630 PTP_CLK_PER_CFG(phc->index, 0));
631 lan_wr((u32)(tod_inc >> 32), lan966x,
632 PTP_CLK_PER_CFG(phc->index, 1));
634 lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0),
635 PTP_DOM_CFG_CLKCFG_DIS,
636 lan966x, PTP_DOM_CFG);
638 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
643 static int lan966x_ptp_settime64(struct ptp_clock_info *ptp,
644 const struct timespec64 *ts)
646 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
647 struct lan966x *lan966x = phc->lan966x;
650 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
652 /* Must be in IDLE mode before the time can be loaded */
653 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
654 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
655 PTP_PIN_CFG_PIN_SYNC_SET(0),
656 PTP_PIN_CFG_PIN_ACTION |
657 PTP_PIN_CFG_PIN_DOM |
658 PTP_PIN_CFG_PIN_SYNC,
659 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
662 lan_wr(PTP_TOD_SEC_MSB_TOD_SEC_MSB_SET(upper_32_bits(ts->tv_sec)),
663 lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN));
664 lan_wr(lower_32_bits(ts->tv_sec),
665 lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
666 lan_wr(ts->tv_nsec, lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
668 /* Apply new values */
669 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_LOAD) |
670 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
671 PTP_PIN_CFG_PIN_SYNC_SET(0),
672 PTP_PIN_CFG_PIN_ACTION |
673 PTP_PIN_CFG_PIN_DOM |
674 PTP_PIN_CFG_PIN_SYNC,
675 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
677 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
682 int lan966x_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
684 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
685 struct lan966x *lan966x = phc->lan966x;
690 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
692 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
693 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
694 PTP_PIN_CFG_PIN_SYNC_SET(0),
695 PTP_PIN_CFG_PIN_ACTION |
696 PTP_PIN_CFG_PIN_DOM |
697 PTP_PIN_CFG_PIN_SYNC,
698 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
700 s = lan_rd(lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN));
702 s |= lan_rd(lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
703 ns = lan_rd(lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
704 ns &= PTP_TOD_NSEC_TOD_NSEC;
706 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
708 /* Deal with negative values */
709 if ((ns & 0xFFFFFFF0) == 0x3FFFFFF0) {
715 set_normalized_timespec64(ts, s, ns);
719 static int lan966x_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
721 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
722 struct lan966x *lan966x = phc->lan966x;
724 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
727 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
729 /* Must be in IDLE mode before the time can be loaded */
730 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
731 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
732 PTP_PIN_CFG_PIN_SYNC_SET(0),
733 PTP_PIN_CFG_PIN_ACTION |
734 PTP_PIN_CFG_PIN_DOM |
735 PTP_PIN_CFG_PIN_SYNC,
736 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
738 lan_wr(PTP_TOD_NSEC_TOD_NSEC_SET(delta),
739 lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
741 /* Adjust time with the value of PTP_TOD_NSEC */
742 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_DELTA) |
743 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
744 PTP_PIN_CFG_PIN_SYNC_SET(0),
745 PTP_PIN_CFG_PIN_ACTION |
746 PTP_PIN_CFG_PIN_DOM |
747 PTP_PIN_CFG_PIN_SYNC,
748 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
750 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
752 /* Fall back using lan966x_ptp_settime64 which is not exact */
753 struct timespec64 ts;
756 lan966x_ptp_gettime64(ptp, &ts);
758 now = ktime_to_ns(timespec64_to_ktime(ts));
759 ts = ns_to_timespec64(now + delta);
761 lan966x_ptp_settime64(ptp, &ts);
767 static int lan966x_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
768 enum ptp_pin_function func, unsigned int chan)
770 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
771 struct lan966x *lan966x = phc->lan966x;
772 struct ptp_clock_info *info;
775 /* Currently support only 1 channel */
788 /* The PTP pins are shared by all the PHC. So it is required to see if
789 * the pin is connected to another PHC. The pin is connected to another
790 * PHC if that pin already has a function on that PHC.
792 for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
793 info = &lan966x->phc[i].info;
795 /* Ignore the check with ourself */
799 if (info->pin_config[pin].func == PTP_PF_PEROUT ||
800 info->pin_config[pin].func == PTP_PF_EXTTS)
807 static int lan966x_ptp_perout(struct ptp_clock_info *ptp,
808 struct ptp_clock_request *rq, int on)
810 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
811 struct lan966x *lan966x = phc->lan966x;
812 struct timespec64 ts_phase, ts_period;
818 if (rq->perout.flags & ~(PTP_PEROUT_DUTY_CYCLE |
822 pin = ptp_find_pin(phc->clock, PTP_PF_PEROUT, rq->perout.index);
823 if (pin == -1 || pin >= LAN966X_PHC_PINS_NUM)
827 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
828 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
829 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
830 PTP_PIN_CFG_PIN_SYNC_SET(0),
831 PTP_PIN_CFG_PIN_ACTION |
832 PTP_PIN_CFG_PIN_DOM |
833 PTP_PIN_CFG_PIN_SYNC,
834 lan966x, PTP_PIN_CFG(pin));
835 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
839 if (rq->perout.period.sec == 1 &&
840 rq->perout.period.nsec == 0)
843 if (rq->perout.flags & PTP_PEROUT_PHASE) {
844 ts_phase.tv_sec = rq->perout.phase.sec;
845 ts_phase.tv_nsec = rq->perout.phase.nsec;
847 ts_phase.tv_sec = rq->perout.start.sec;
848 ts_phase.tv_nsec = rq->perout.start.nsec;
851 if (ts_phase.tv_sec || (ts_phase.tv_nsec && !pps)) {
852 dev_warn(lan966x->dev,
853 "Absolute time not supported!\n");
857 if (rq->perout.flags & PTP_PEROUT_DUTY_CYCLE) {
858 struct timespec64 ts_on;
860 ts_on.tv_sec = rq->perout.on.sec;
861 ts_on.tv_nsec = rq->perout.on.nsec;
863 wf_high = timespec64_to_ns(&ts_on);
869 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
870 lan_wr(PTP_WF_LOW_PERIOD_PIN_WFL(ts_phase.tv_nsec),
871 lan966x, PTP_WF_LOW_PERIOD(pin));
872 lan_wr(PTP_WF_HIGH_PERIOD_PIN_WFH(wf_high),
873 lan966x, PTP_WF_HIGH_PERIOD(pin));
874 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_CLOCK) |
875 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
876 PTP_PIN_CFG_PIN_SYNC_SET(3),
877 PTP_PIN_CFG_PIN_ACTION |
878 PTP_PIN_CFG_PIN_DOM |
879 PTP_PIN_CFG_PIN_SYNC,
880 lan966x, PTP_PIN_CFG(pin));
881 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
885 ts_period.tv_sec = rq->perout.period.sec;
886 ts_period.tv_nsec = rq->perout.period.nsec;
888 wf_low = timespec64_to_ns(&ts_period);
891 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
892 lan_wr(PTP_WF_LOW_PERIOD_PIN_WFL(wf_low),
893 lan966x, PTP_WF_LOW_PERIOD(pin));
894 lan_wr(PTP_WF_HIGH_PERIOD_PIN_WFH(wf_high),
895 lan966x, PTP_WF_HIGH_PERIOD(pin));
896 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_CLOCK) |
897 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
898 PTP_PIN_CFG_PIN_SYNC_SET(0),
899 PTP_PIN_CFG_PIN_ACTION |
900 PTP_PIN_CFG_PIN_DOM |
901 PTP_PIN_CFG_PIN_SYNC,
902 lan966x, PTP_PIN_CFG(pin));
903 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
908 static int lan966x_ptp_extts(struct ptp_clock_info *ptp,
909 struct ptp_clock_request *rq, int on)
911 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
912 struct lan966x *lan966x = phc->lan966x;
917 if (lan966x->ptp_ext_irq <= 0)
920 /* Reject requests with unsupported flags */
921 if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
926 pin = ptp_find_pin(phc->clock, PTP_PF_EXTTS, rq->extts.index);
927 if (pin == -1 || pin >= LAN966X_PHC_PINS_NUM)
930 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
931 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
932 PTP_PIN_CFG_PIN_SYNC_SET(on ? 3 : 0) |
933 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
934 PTP_PIN_CFG_PIN_SELECT_SET(pin),
935 PTP_PIN_CFG_PIN_ACTION |
936 PTP_PIN_CFG_PIN_SYNC |
937 PTP_PIN_CFG_PIN_DOM |
938 PTP_PIN_CFG_PIN_SELECT,
939 lan966x, PTP_PIN_CFG(pin));
941 val = lan_rd(lan966x, PTP_PIN_INTR_ENA);
946 lan_wr(val, lan966x, PTP_PIN_INTR_ENA);
948 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
953 static int lan966x_ptp_enable(struct ptp_clock_info *ptp,
954 struct ptp_clock_request *rq, int on)
957 case PTP_CLK_REQ_PEROUT:
958 return lan966x_ptp_perout(ptp, rq, on);
959 case PTP_CLK_REQ_EXTTS:
960 return lan966x_ptp_extts(ptp, rq, on);
968 static struct ptp_clock_info lan966x_ptp_clock_info = {
969 .owner = THIS_MODULE,
970 .name = "lan966x ptp",
972 .gettime64 = lan966x_ptp_gettime64,
973 .settime64 = lan966x_ptp_settime64,
974 .adjtime = lan966x_ptp_adjtime,
975 .adjfine = lan966x_ptp_adjfine,
976 .verify = lan966x_ptp_verify,
977 .enable = lan966x_ptp_enable,
978 .n_per_out = LAN966X_PHC_PINS_NUM,
979 .n_ext_ts = LAN966X_PHC_PINS_NUM,
980 .n_pins = LAN966X_PHC_PINS_NUM,
983 static int lan966x_ptp_phc_init(struct lan966x *lan966x,
985 struct ptp_clock_info *clock_info)
987 struct lan966x_phc *phc = &lan966x->phc[index];
988 struct ptp_pin_desc *p;
991 for (i = 0; i < LAN966X_PHC_PINS_NUM; i++) {
994 snprintf(p->name, sizeof(p->name), "pin%d", i);
996 p->func = PTP_PF_NONE;
999 phc->info = *clock_info;
1000 phc->info.pin_config = &phc->pins[0];
1001 phc->clock = ptp_clock_register(&phc->info, lan966x->dev);
1002 if (IS_ERR(phc->clock))
1003 return PTR_ERR(phc->clock);
1006 phc->lan966x = lan966x;
1011 int lan966x_ptp_init(struct lan966x *lan966x)
1013 u64 tod_adj = lan966x_ptp_get_nominal_value();
1014 struct lan966x_port *port;
1020 for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
1021 err = lan966x_ptp_phc_init(lan966x, i, &lan966x_ptp_clock_info);
1026 spin_lock_init(&lan966x->ptp_clock_lock);
1027 spin_lock_init(&lan966x->ptp_ts_id_lock);
1028 mutex_init(&lan966x->ptp_lock);
1030 /* Disable master counters */
1031 lan_wr(PTP_DOM_CFG_ENA_SET(0), lan966x, PTP_DOM_CFG);
1033 /* Configure the nominal TOD increment per clock cycle */
1034 lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0x7),
1035 PTP_DOM_CFG_CLKCFG_DIS,
1036 lan966x, PTP_DOM_CFG);
1038 for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
1039 lan_wr((u32)tod_adj & 0xFFFFFFFF, lan966x,
1040 PTP_CLK_PER_CFG(i, 0));
1041 lan_wr((u32)(tod_adj >> 32), lan966x,
1042 PTP_CLK_PER_CFG(i, 1));
1045 lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0),
1046 PTP_DOM_CFG_CLKCFG_DIS,
1047 lan966x, PTP_DOM_CFG);
1049 /* Enable master counters */
1050 lan_wr(PTP_DOM_CFG_ENA_SET(0x7), lan966x, PTP_DOM_CFG);
1052 for (i = 0; i < lan966x->num_phys_ports; i++) {
1053 port = lan966x->ports[i];
1057 skb_queue_head_init(&port->tx_skbs);
1063 void lan966x_ptp_deinit(struct lan966x *lan966x)
1065 struct lan966x_port *port;
1071 for (i = 0; i < lan966x->num_phys_ports; i++) {
1072 port = lan966x->ports[i];
1076 skb_queue_purge(&port->tx_skbs);
1079 for (i = 0; i < LAN966X_PHC_COUNT; ++i)
1080 ptp_clock_unregister(lan966x->phc[i].clock);
1083 void lan966x_ptp_rxtstamp(struct lan966x *lan966x, struct sk_buff *skb,
1084 u64 src_port, u64 timestamp)
1086 struct skb_shared_hwtstamps *shhwtstamps;
1087 struct lan966x_phc *phc;
1088 struct timespec64 ts;
1091 if (!lan966x->ptp ||
1092 !lan966x->ports[src_port]->ptp_rx_cmd)
1095 phc = &lan966x->phc[LAN966X_PHC_PORT];
1096 lan966x_ptp_gettime64(&phc->info, &ts);
1098 /* Drop the sub-ns precision */
1099 timestamp = timestamp >> 2;
1100 if (ts.tv_nsec < timestamp)
1102 ts.tv_nsec = timestamp;
1103 full_ts_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1105 shhwtstamps = skb_hwtstamps(skb);
1106 shhwtstamps->hwtstamp = full_ts_in_ns;
1109 u32 lan966x_ptp_get_period_ps(void)
1111 /* This represents the system clock period in picoseconds */