1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/ptp_classify.h>
5 #include "lan966x_main.h"
7 #define LAN966X_MAX_PTP_ID 512
9 /* Represents 1ppm adjustment in 2^59 format with 6.037735849ns as reference
10 * The value is calculated as following: (1/1000000)/((2^-59)/6.037735849)
12 #define LAN966X_1PPM_FORMAT 3480517749723LL
14 /* Represents 1ppb adjustment in 2^29 format with 6.037735849ns as reference
15 * The value is calculated as following: (1/1000000000)/((2^59)/6.037735849)
17 #define LAN966X_1PPB_FORMAT 3480517749LL
19 #define TOD_ACC_PIN 0x5
22 PTP_PIN_ACTION_IDLE = 0,
30 static u64 lan966x_ptp_get_nominal_value(void)
38 int lan966x_ptp_hwtstamp_set(struct lan966x_port *port, struct ifreq *ifr)
40 struct lan966x *lan966x = port->lan966x;
41 struct hwtstamp_config cfg;
42 struct lan966x_phc *phc;
44 /* For now don't allow to run ptp on ports that are part of a bridge,
45 * because in case of transparent clock the HW will still forward the
46 * frames, so there would be duplicate frames
48 if (lan966x->bridge_mask & BIT(port->chip_port))
51 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
54 switch (cfg.tx_type) {
56 port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
58 case HWTSTAMP_TX_ONESTEP_SYNC:
59 port->ptp_cmd = IFH_REW_OP_ONE_STEP_PTP;
62 port->ptp_cmd = IFH_REW_OP_NOOP;
68 switch (cfg.rx_filter) {
69 case HWTSTAMP_FILTER_NONE:
71 case HWTSTAMP_FILTER_ALL:
72 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
73 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
74 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
75 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
76 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
77 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
78 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
79 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
80 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
81 case HWTSTAMP_FILTER_PTP_V2_EVENT:
82 case HWTSTAMP_FILTER_PTP_V2_SYNC:
83 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
84 case HWTSTAMP_FILTER_NTP_ALL:
85 cfg.rx_filter = HWTSTAMP_FILTER_ALL;
91 /* Commit back the result & save it */
92 mutex_lock(&lan966x->ptp_lock);
93 phc = &lan966x->phc[LAN966X_PHC_PORT];
94 memcpy(&phc->hwtstamp_config, &cfg, sizeof(cfg));
95 mutex_unlock(&lan966x->ptp_lock);
97 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
100 int lan966x_ptp_hwtstamp_get(struct lan966x_port *port, struct ifreq *ifr)
102 struct lan966x *lan966x = port->lan966x;
103 struct lan966x_phc *phc;
105 phc = &lan966x->phc[LAN966X_PHC_PORT];
106 return copy_to_user(ifr->ifr_data, &phc->hwtstamp_config,
107 sizeof(phc->hwtstamp_config)) ? -EFAULT : 0;
110 static int lan966x_ptp_classify(struct lan966x_port *port, struct sk_buff *skb)
112 struct ptp_header *header;
116 if (port->ptp_cmd == IFH_REW_OP_NOOP)
117 return IFH_REW_OP_NOOP;
119 type = ptp_classify_raw(skb);
120 if (type == PTP_CLASS_NONE)
121 return IFH_REW_OP_NOOP;
123 header = ptp_parse_header(skb, type);
125 return IFH_REW_OP_NOOP;
127 if (port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
128 return IFH_REW_OP_TWO_STEP_PTP;
130 /* If it is sync and run 1 step then set the correct operation,
131 * otherwise run as 2 step
133 msgtype = ptp_get_msgtype(header, type);
134 if ((msgtype & 0xf) == 0)
135 return IFH_REW_OP_ONE_STEP_PTP;
137 return IFH_REW_OP_TWO_STEP_PTP;
140 static void lan966x_ptp_txtstamp_old_release(struct lan966x_port *port)
142 struct sk_buff *skb, *skb_tmp;
145 spin_lock_irqsave(&port->tx_skbs.lock, flags);
146 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
147 if time_after(LAN966X_SKB_CB(skb)->jiffies + LAN966X_PTP_TIMEOUT,
151 __skb_unlink(skb, &port->tx_skbs);
152 dev_kfree_skb_any(skb);
154 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
157 int lan966x_ptp_txtstamp_request(struct lan966x_port *port,
160 struct lan966x *lan966x = port->lan966x;
164 rew_op = lan966x_ptp_classify(port, skb);
165 LAN966X_SKB_CB(skb)->rew_op = rew_op;
167 if (rew_op != IFH_REW_OP_TWO_STEP_PTP)
170 lan966x_ptp_txtstamp_old_release(port);
172 spin_lock_irqsave(&lan966x->ptp_ts_id_lock, flags);
173 if (lan966x->ptp_skbs == LAN966X_MAX_PTP_ID) {
174 spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
178 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
180 skb_queue_tail(&port->tx_skbs, skb);
181 LAN966X_SKB_CB(skb)->ts_id = port->ts_id;
182 LAN966X_SKB_CB(skb)->jiffies = jiffies;
186 if (port->ts_id == LAN966X_MAX_PTP_ID)
189 spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
194 void lan966x_ptp_txtstamp_release(struct lan966x_port *port,
197 struct lan966x *lan966x = port->lan966x;
200 spin_lock_irqsave(&lan966x->ptp_ts_id_lock, flags);
203 skb_unlink(skb, &port->tx_skbs);
204 spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
207 static void lan966x_get_hwtimestamp(struct lan966x *lan966x,
208 struct timespec64 *ts,
211 /* Read current PTP time to get seconds */
215 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
217 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
218 PTP_PIN_CFG_PIN_DOM_SET(LAN966X_PHC_PORT) |
219 PTP_PIN_CFG_PIN_SYNC_SET(0),
220 PTP_PIN_CFG_PIN_ACTION |
221 PTP_PIN_CFG_PIN_DOM |
222 PTP_PIN_CFG_PIN_SYNC,
223 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
225 ts->tv_sec = lan_rd(lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
226 curr_nsec = lan_rd(lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
230 /* Sec has incremented since the ts was registered */
231 if (curr_nsec < nsec)
234 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
237 irqreturn_t lan966x_ptp_irq_handler(int irq, void *args)
239 int budget = LAN966X_MAX_PTP_ID;
240 struct lan966x *lan966x = args;
243 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
244 struct skb_shared_hwtstamps shhwtstamps;
245 struct lan966x_port *port;
246 struct timespec64 ts;
251 val = lan_rd(lan966x, PTP_TWOSTEP_CTRL);
253 /* Check if a timestamp can be retrieved */
254 if (!(val & PTP_TWOSTEP_CTRL_VLD))
257 WARN_ON(val & PTP_TWOSTEP_CTRL_OVFL);
259 if (!(val & PTP_TWOSTEP_CTRL_STAMP_TX))
262 /* Retrieve the ts Tx port */
263 txport = PTP_TWOSTEP_CTRL_STAMP_PORT_GET(val);
265 /* Retrieve its associated skb */
266 port = lan966x->ports[txport];
268 /* Retrieve the delay */
269 delay = lan_rd(lan966x, PTP_TWOSTEP_STAMP);
270 delay = PTP_TWOSTEP_STAMP_STAMP_NSEC_GET(delay);
272 /* Get next timestamp from fifo, which needs to be the
273 * rx timestamp which represents the id of the frame
275 lan_rmw(PTP_TWOSTEP_CTRL_NXT_SET(1),
276 PTP_TWOSTEP_CTRL_NXT,
277 lan966x, PTP_TWOSTEP_CTRL);
279 val = lan_rd(lan966x, PTP_TWOSTEP_CTRL);
281 /* Check if a timestamp can be retried */
282 if (!(val & PTP_TWOSTEP_CTRL_VLD))
285 /* Read RX timestamping to get the ID */
286 id = lan_rd(lan966x, PTP_TWOSTEP_STAMP);
288 spin_lock_irqsave(&port->tx_skbs.lock, flags);
289 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
290 if (LAN966X_SKB_CB(skb)->ts_id != id)
293 __skb_unlink(skb, &port->tx_skbs);
297 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
300 lan_rmw(PTP_TWOSTEP_CTRL_NXT_SET(1),
301 PTP_TWOSTEP_CTRL_NXT,
302 lan966x, PTP_TWOSTEP_CTRL);
304 if (WARN_ON(!skb_match))
307 spin_lock(&lan966x->ptp_ts_id_lock);
309 spin_unlock(&lan966x->ptp_ts_id_lock);
311 /* Get the h/w timestamp */
312 lan966x_get_hwtimestamp(lan966x, &ts, delay);
314 /* Set the timestamp into the skb */
315 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
316 skb_tstamp_tx(skb_match, &shhwtstamps);
318 dev_kfree_skb_any(skb_match);
324 static int lan966x_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
326 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
327 struct lan966x *lan966x = phc->lan966x;
336 if (scaled_ppm < 0) {
338 scaled_ppm = -scaled_ppm;
341 tod_inc = lan966x_ptp_get_nominal_value();
343 /* The multiplication is split in 2 separate additions because of
344 * overflow issues. If scaled_ppm with 16bit fractional part was bigger
345 * than 20ppm then we got overflow.
347 ref = LAN966X_1PPM_FORMAT * (scaled_ppm >> 16);
348 ref += (LAN966X_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
349 tod_inc = neg_adj ? tod_inc - ref : tod_inc + ref;
351 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
353 lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(1 << BIT(phc->index)),
354 PTP_DOM_CFG_CLKCFG_DIS,
355 lan966x, PTP_DOM_CFG);
357 lan_wr((u32)tod_inc & 0xFFFFFFFF, lan966x,
358 PTP_CLK_PER_CFG(phc->index, 0));
359 lan_wr((u32)(tod_inc >> 32), lan966x,
360 PTP_CLK_PER_CFG(phc->index, 1));
362 lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0),
363 PTP_DOM_CFG_CLKCFG_DIS,
364 lan966x, PTP_DOM_CFG);
366 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
371 static int lan966x_ptp_settime64(struct ptp_clock_info *ptp,
372 const struct timespec64 *ts)
374 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
375 struct lan966x *lan966x = phc->lan966x;
378 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
380 /* Must be in IDLE mode before the time can be loaded */
381 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
382 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
383 PTP_PIN_CFG_PIN_SYNC_SET(0),
384 PTP_PIN_CFG_PIN_ACTION |
385 PTP_PIN_CFG_PIN_DOM |
386 PTP_PIN_CFG_PIN_SYNC,
387 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
390 lan_wr(PTP_TOD_SEC_MSB_TOD_SEC_MSB_SET(upper_32_bits(ts->tv_sec)),
391 lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN));
392 lan_wr(lower_32_bits(ts->tv_sec),
393 lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
394 lan_wr(ts->tv_nsec, lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
396 /* Apply new values */
397 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_LOAD) |
398 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
399 PTP_PIN_CFG_PIN_SYNC_SET(0),
400 PTP_PIN_CFG_PIN_ACTION |
401 PTP_PIN_CFG_PIN_DOM |
402 PTP_PIN_CFG_PIN_SYNC,
403 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
405 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
410 static int lan966x_ptp_gettime64(struct ptp_clock_info *ptp,
411 struct timespec64 *ts)
413 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
414 struct lan966x *lan966x = phc->lan966x;
419 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
421 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
422 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
423 PTP_PIN_CFG_PIN_SYNC_SET(0),
424 PTP_PIN_CFG_PIN_ACTION |
425 PTP_PIN_CFG_PIN_DOM |
426 PTP_PIN_CFG_PIN_SYNC,
427 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
429 s = lan_rd(lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN));
431 s |= lan_rd(lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
432 ns = lan_rd(lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
433 ns &= PTP_TOD_NSEC_TOD_NSEC;
435 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
437 /* Deal with negative values */
438 if ((ns & 0xFFFFFFF0) == 0x3FFFFFF0) {
444 set_normalized_timespec64(ts, s, ns);
448 static int lan966x_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
450 struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
451 struct lan966x *lan966x = phc->lan966x;
453 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
456 spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
458 /* Must be in IDLE mode before the time can be loaded */
459 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
460 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
461 PTP_PIN_CFG_PIN_SYNC_SET(0),
462 PTP_PIN_CFG_PIN_ACTION |
463 PTP_PIN_CFG_PIN_DOM |
464 PTP_PIN_CFG_PIN_SYNC,
465 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
467 lan_wr(PTP_TOD_NSEC_TOD_NSEC_SET(delta),
468 lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
470 /* Adjust time with the value of PTP_TOD_NSEC */
471 lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_DELTA) |
472 PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
473 PTP_PIN_CFG_PIN_SYNC_SET(0),
474 PTP_PIN_CFG_PIN_ACTION |
475 PTP_PIN_CFG_PIN_DOM |
476 PTP_PIN_CFG_PIN_SYNC,
477 lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
479 spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
481 /* Fall back using lan966x_ptp_settime64 which is not exact */
482 struct timespec64 ts;
485 lan966x_ptp_gettime64(ptp, &ts);
487 now = ktime_to_ns(timespec64_to_ktime(ts));
488 ts = ns_to_timespec64(now + delta);
490 lan966x_ptp_settime64(ptp, &ts);
496 static struct ptp_clock_info lan966x_ptp_clock_info = {
497 .owner = THIS_MODULE,
498 .name = "lan966x ptp",
500 .gettime64 = lan966x_ptp_gettime64,
501 .settime64 = lan966x_ptp_settime64,
502 .adjtime = lan966x_ptp_adjtime,
503 .adjfine = lan966x_ptp_adjfine,
506 static int lan966x_ptp_phc_init(struct lan966x *lan966x,
508 struct ptp_clock_info *clock_info)
510 struct lan966x_phc *phc = &lan966x->phc[index];
512 phc->info = *clock_info;
513 phc->clock = ptp_clock_register(&phc->info, lan966x->dev);
514 if (IS_ERR(phc->clock))
515 return PTR_ERR(phc->clock);
518 phc->lan966x = lan966x;
520 /* PTP Rx stamping is always enabled. */
521 phc->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
526 int lan966x_ptp_init(struct lan966x *lan966x)
528 u64 tod_adj = lan966x_ptp_get_nominal_value();
529 struct lan966x_port *port;
535 for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
536 err = lan966x_ptp_phc_init(lan966x, i, &lan966x_ptp_clock_info);
541 spin_lock_init(&lan966x->ptp_clock_lock);
542 spin_lock_init(&lan966x->ptp_ts_id_lock);
543 mutex_init(&lan966x->ptp_lock);
545 /* Disable master counters */
546 lan_wr(PTP_DOM_CFG_ENA_SET(0), lan966x, PTP_DOM_CFG);
548 /* Configure the nominal TOD increment per clock cycle */
549 lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0x7),
550 PTP_DOM_CFG_CLKCFG_DIS,
551 lan966x, PTP_DOM_CFG);
553 for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
554 lan_wr((u32)tod_adj & 0xFFFFFFFF, lan966x,
555 PTP_CLK_PER_CFG(i, 0));
556 lan_wr((u32)(tod_adj >> 32), lan966x,
557 PTP_CLK_PER_CFG(i, 1));
560 lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0),
561 PTP_DOM_CFG_CLKCFG_DIS,
562 lan966x, PTP_DOM_CFG);
564 /* Enable master counters */
565 lan_wr(PTP_DOM_CFG_ENA_SET(0x7), lan966x, PTP_DOM_CFG);
567 for (i = 0; i < lan966x->num_phys_ports; i++) {
568 port = lan966x->ports[i];
572 skb_queue_head_init(&port->tx_skbs);
578 void lan966x_ptp_deinit(struct lan966x *lan966x)
580 struct lan966x_port *port;
583 for (i = 0; i < lan966x->num_phys_ports; i++) {
584 port = lan966x->ports[i];
588 skb_queue_purge(&port->tx_skbs);
591 for (i = 0; i < LAN966X_PHC_COUNT; ++i)
592 ptp_clock_unregister(lan966x->phc[i].clock);
595 void lan966x_ptp_rxtstamp(struct lan966x *lan966x, struct sk_buff *skb,
598 struct skb_shared_hwtstamps *shhwtstamps;
599 struct lan966x_phc *phc;
600 struct timespec64 ts;
606 phc = &lan966x->phc[LAN966X_PHC_PORT];
607 lan966x_ptp_gettime64(&phc->info, &ts);
609 /* Drop the sub-ns precision */
610 timestamp = timestamp >> 2;
611 if (ts.tv_nsec < timestamp)
613 ts.tv_nsec = timestamp;
614 full_ts_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
616 shhwtstamps = skb_hwtstamps(skb);
617 shhwtstamps->hwtstamp = full_ts_in_ns;