1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
4 #include <linux/spi/spi.h>
7 /* The adjfine API clamps ppb between [-32,768,000, 32,768,000], and
8 * therefore scaled_ppm between [-2,147,483,648, 2,147,483,647].
9 * Set the maximum supported ppb to a round value smaller than the maximum.
11 * Percentually speaking, this is a +/- 0.032x adjustment of the
12 * free-running counter (0.968x to 1.032x).
14 #define SJA1105_MAX_ADJ_PPB 32000000
15 #define SJA1105_SIZE_PTP_CMD 4
17 /* This range is actually +/- SJA1105_MAX_ADJ_PPB
18 * divided by 1000 (ppb -> ppm) and with a 16-bit
19 * "fractional" part (actually fixed point).
22 * Convert scaled_ppm from the +/- ((10^6) << 16) range
23 * into the +/- (1 << 31) range.
25 * This forgoes a "ppb" numeric representation (up to NSEC_PER_SEC)
26 * and defines the scaling factor between scaled_ppm and the actual
27 * frequency adjustments of the PHC.
29 * ptpclkrate = scaled_ppm * 2^31 / (10^6 * 2^16)
31 * ptpclkrate = scaled_ppm * 2^9 / 5^6
33 #define SJA1105_CC_MULT_NUM (1 << 9)
34 #define SJA1105_CC_MULT_DEM 15625
35 #define SJA1105_CC_MULT 0x80000000
37 enum sja1105_ptp_clk_mode
{
42 #define ptp_caps_to_data(d) \
43 container_of((d), struct sja1105_ptp_data, caps)
44 #define ptp_data_to_sja1105(d) \
45 container_of((d), struct sja1105_private, ptp_data)
47 static int sja1105_init_avb_params(struct sja1105_private
*priv
,
50 struct sja1105_avb_params_entry
*avb
;
51 struct sja1105_table
*table
;
53 table
= &priv
->static_config
.tables
[BLK_IDX_AVB_PARAMS
];
55 /* Discard previous AVB Parameters Table */
56 if (table
->entry_count
) {
57 kfree(table
->entries
);
58 table
->entry_count
= 0;
61 /* Configure the reception of meta frames only if requested */
65 table
->entries
= kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT
,
66 table
->ops
->unpacked_entry_size
, GFP_KERNEL
);
70 table
->entry_count
= SJA1105_MAX_AVB_PARAMS_COUNT
;
74 avb
->destmeta
= SJA1105_META_DMAC
;
75 avb
->srcmeta
= SJA1105_META_SMAC
;
80 /* Must be called only with priv->tagger_data.state bit
81 * SJA1105_HWTS_RX_EN cleared
83 static int sja1105_change_rxtstamping(struct sja1105_private
*priv
,
86 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
87 struct sja1105_general_params_entry
*general_params
;
88 struct sja1105_table
*table
;
91 table
= &priv
->static_config
.tables
[BLK_IDX_GENERAL_PARAMS
];
92 general_params
= table
->entries
;
93 general_params
->send_meta1
= on
;
94 general_params
->send_meta0
= on
;
96 rc
= sja1105_init_avb_params(priv
, on
);
100 /* Initialize the meta state machine to a known state */
101 if (priv
->tagger_data
.stampable_skb
) {
102 kfree_skb(priv
->tagger_data
.stampable_skb
);
103 priv
->tagger_data
.stampable_skb
= NULL
;
105 ptp_cancel_worker_sync(ptp_data
->clock
);
106 skb_queue_purge(&ptp_data
->skb_rxtstamp_queue
);
108 return sja1105_static_config_reload(priv
, SJA1105_RX_HWTSTAMPING
);
111 int sja1105_hwtstamp_set(struct dsa_switch
*ds
, int port
, struct ifreq
*ifr
)
113 struct sja1105_private
*priv
= ds
->priv
;
114 struct hwtstamp_config config
;
118 if (copy_from_user(&config
, ifr
->ifr_data
, sizeof(config
)))
121 switch (config
.tx_type
) {
122 case HWTSTAMP_TX_OFF
:
123 priv
->ports
[port
].hwts_tx_en
= false;
126 priv
->ports
[port
].hwts_tx_en
= true;
132 switch (config
.rx_filter
) {
133 case HWTSTAMP_FILTER_NONE
:
141 if (rx_on
!= test_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
)) {
142 clear_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
);
144 rc
= sja1105_change_rxtstamping(priv
, rx_on
);
147 "Failed to change RX timestamping: %d\n", rc
);
151 set_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
);
154 if (copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)))
159 int sja1105_hwtstamp_get(struct dsa_switch
*ds
, int port
, struct ifreq
*ifr
)
161 struct sja1105_private
*priv
= ds
->priv
;
162 struct hwtstamp_config config
;
165 if (priv
->ports
[port
].hwts_tx_en
)
166 config
.tx_type
= HWTSTAMP_TX_ON
;
168 config
.tx_type
= HWTSTAMP_TX_OFF
;
169 if (test_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
))
170 config
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_L2_EVENT
;
172 config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
174 return copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)) ?
178 int sja1105_get_ts_info(struct dsa_switch
*ds
, int port
,
179 struct ethtool_ts_info
*info
)
181 struct sja1105_private
*priv
= ds
->priv
;
182 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
184 /* Called during cleanup */
185 if (!ptp_data
->clock
)
188 info
->so_timestamping
= SOF_TIMESTAMPING_TX_HARDWARE
|
189 SOF_TIMESTAMPING_RX_HARDWARE
|
190 SOF_TIMESTAMPING_RAW_HARDWARE
;
191 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) |
192 (1 << HWTSTAMP_TX_ON
);
193 info
->rx_filters
= (1 << HWTSTAMP_FILTER_NONE
) |
194 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT
);
195 info
->phc_index
= ptp_clock_index(ptp_data
->clock
);
199 void sja1105et_ptp_cmd_packing(u8
*buf
, struct sja1105_ptp_cmd
*cmd
,
202 const int size
= SJA1105_SIZE_PTP_CMD
;
203 /* No need to keep this as part of the structure */
206 sja1105_packing(buf
, &valid
, 31, 31, size
, op
);
207 sja1105_packing(buf
, &cmd
->ptpstrtsch
, 30, 30, size
, op
);
208 sja1105_packing(buf
, &cmd
->ptpstopsch
, 29, 29, size
, op
);
209 sja1105_packing(buf
, &cmd
->resptp
, 2, 2, size
, op
);
210 sja1105_packing(buf
, &cmd
->corrclk4ts
, 1, 1, size
, op
);
211 sja1105_packing(buf
, &cmd
->ptpclkadd
, 0, 0, size
, op
);
214 void sja1105pqrs_ptp_cmd_packing(u8
*buf
, struct sja1105_ptp_cmd
*cmd
,
217 const int size
= SJA1105_SIZE_PTP_CMD
;
218 /* No need to keep this as part of the structure */
221 sja1105_packing(buf
, &valid
, 31, 31, size
, op
);
222 sja1105_packing(buf
, &cmd
->ptpstrtsch
, 30, 30, size
, op
);
223 sja1105_packing(buf
, &cmd
->ptpstopsch
, 29, 29, size
, op
);
224 sja1105_packing(buf
, &cmd
->resptp
, 3, 3, size
, op
);
225 sja1105_packing(buf
, &cmd
->corrclk4ts
, 2, 2, size
, op
);
226 sja1105_packing(buf
, &cmd
->ptpclkadd
, 0, 0, size
, op
);
229 int sja1105_ptp_commit(struct dsa_switch
*ds
, struct sja1105_ptp_cmd
*cmd
,
230 sja1105_spi_rw_mode_t rw
)
232 const struct sja1105_private
*priv
= ds
->priv
;
233 const struct sja1105_regs
*regs
= priv
->info
->regs
;
234 u8 buf
[SJA1105_SIZE_PTP_CMD
] = {0};
238 priv
->info
->ptp_cmd_packing(buf
, cmd
, PACK
);
240 rc
= sja1105_xfer_buf(priv
, rw
, regs
->ptp_control
, buf
,
241 SJA1105_SIZE_PTP_CMD
);
244 priv
->info
->ptp_cmd_packing(buf
, cmd
, UNPACK
);
249 /* The switch returns partial timestamps (24 bits for SJA1105 E/T, which wrap
250 * around in 0.135 seconds, and 32 bits for P/Q/R/S, wrapping around in 34.35
253 * This receives the RX or TX MAC timestamps, provided by hardware as
254 * the lower bits of the cycle counter, sampled at the time the timestamp was
257 * To reconstruct into a full 64-bit-wide timestamp, the cycle counter is
258 * read and the high-order bits are filled in.
260 * Must be called within one wraparound period of the partial timestamp since
261 * it was generated by the MAC.
263 static u64
sja1105_tstamp_reconstruct(struct dsa_switch
*ds
, u64 now
,
266 struct sja1105_private
*priv
= ds
->priv
;
267 u64 partial_tstamp_mask
= CYCLECOUNTER_MASK(priv
->info
->ptp_ts_bits
);
268 u64 ts_reconstructed
;
270 ts_reconstructed
= (now
& ~partial_tstamp_mask
) | ts_partial
;
272 /* Check lower bits of current cycle counter against the timestamp.
273 * If the current cycle counter is lower than the partial timestamp,
274 * then wraparound surely occurred and must be accounted for.
276 if ((now
& partial_tstamp_mask
) <= ts_partial
)
277 ts_reconstructed
-= (partial_tstamp_mask
+ 1);
279 return ts_reconstructed
;
282 /* Reads the SPI interface for an egress timestamp generated by the switch
283 * for frames sent using management routes.
285 * SJA1105 E/T layout of the 4-byte SPI payload:
289 * +-----+-----+-----+ ^
292 * 24-bit timestamp Update bit
295 * SJA1105 P/Q/R/S layout of the 8-byte SPI payload:
297 * 31 23 15 7 0 63 55 47 39 32
298 * | | | | | | | | | |
299 * ^ +-----+-----+-----+-----+
302 * Update bit 32-bit timestamp
304 * Notice that the update bit is in the same place.
305 * To have common code for E/T and P/Q/R/S for reading the timestamp,
306 * we need to juggle with the offset and the bit indices.
308 static int sja1105_ptpegr_ts_poll(struct dsa_switch
*ds
, int port
, u64
*ts
)
310 struct sja1105_private
*priv
= ds
->priv
;
311 const struct sja1105_regs
*regs
= priv
->info
->regs
;
312 int tstamp_bit_start
, tstamp_bit_end
;
319 rc
= sja1105_xfer_buf(priv
, SPI_READ
, regs
->ptpegr_ts
[port
],
320 packed_buf
, priv
->info
->ptpegr_ts_bytes
);
324 sja1105_unpack(packed_buf
, &update
, 0, 0,
325 priv
->info
->ptpegr_ts_bytes
);
329 usleep_range(10, 50);
335 /* Point the end bit to the second 32-bit word on P/Q/R/S,
338 tstamp_bit_end
= (priv
->info
->ptpegr_ts_bytes
- 4) * 8;
339 /* Shift the 24-bit timestamp on E/T to be collected from 31:8.
342 tstamp_bit_end
+= 32 - priv
->info
->ptp_ts_bits
;
343 tstamp_bit_start
= tstamp_bit_end
+ priv
->info
->ptp_ts_bits
- 1;
347 sja1105_unpack(packed_buf
, ts
, tstamp_bit_start
, tstamp_bit_end
,
348 priv
->info
->ptpegr_ts_bytes
);
353 /* Caller must hold ptp_data->lock */
354 static int sja1105_ptpclkval_read(struct sja1105_private
*priv
, u64
*ticks
,
355 struct ptp_system_timestamp
*ptp_sts
)
357 const struct sja1105_regs
*regs
= priv
->info
->regs
;
359 return sja1105_xfer_u64(priv
, SPI_READ
, regs
->ptpclkval
, ticks
,
363 /* Caller must hold ptp_data->lock */
364 static int sja1105_ptpclkval_write(struct sja1105_private
*priv
, u64 ticks
,
365 struct ptp_system_timestamp
*ptp_sts
)
367 const struct sja1105_regs
*regs
= priv
->info
->regs
;
369 return sja1105_xfer_u64(priv
, SPI_WRITE
, regs
->ptpclkval
, &ticks
,
373 static long sja1105_rxtstamp_work(struct ptp_clock_info
*ptp
)
375 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
376 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
377 struct dsa_switch
*ds
= priv
->ds
;
380 mutex_lock(&ptp_data
->lock
);
382 while ((skb
= skb_dequeue(&ptp_data
->skb_rxtstamp_queue
)) != NULL
) {
383 struct skb_shared_hwtstamps
*shwt
= skb_hwtstamps(skb
);
387 rc
= sja1105_ptpclkval_read(priv
, &ticks
, NULL
);
389 dev_err(ds
->dev
, "Failed to read PTP clock: %d\n", rc
);
394 *shwt
= (struct skb_shared_hwtstamps
) {0};
396 ts
= SJA1105_SKB_CB(skb
)->meta_tstamp
;
397 ts
= sja1105_tstamp_reconstruct(ds
, ticks
, ts
);
399 shwt
->hwtstamp
= ns_to_ktime(sja1105_ticks_to_ns(ts
));
403 mutex_unlock(&ptp_data
->lock
);
409 /* Called from dsa_skb_defer_rx_timestamp */
410 bool sja1105_port_rxtstamp(struct dsa_switch
*ds
, int port
,
411 struct sk_buff
*skb
, unsigned int type
)
413 struct sja1105_private
*priv
= ds
->priv
;
414 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
416 if (!test_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
))
419 /* We need to read the full PTP clock to reconstruct the Rx
420 * timestamp. For that we need a sleepable context.
422 skb_queue_tail(&ptp_data
->skb_rxtstamp_queue
, skb
);
423 ptp_schedule_worker(ptp_data
->clock
, 0);
427 /* Called from dsa_skb_tx_timestamp. This callback is just to make DSA clone
428 * the skb and have it available in DSA_SKB_CB in the .port_deferred_xmit
429 * callback, where we will timestamp it synchronously.
431 bool sja1105_port_txtstamp(struct dsa_switch
*ds
, int port
,
432 struct sk_buff
*skb
, unsigned int type
)
434 struct sja1105_private
*priv
= ds
->priv
;
435 struct sja1105_port
*sp
= &priv
->ports
[port
];
443 static int sja1105_ptp_reset(struct dsa_switch
*ds
)
445 struct sja1105_private
*priv
= ds
->priv
;
446 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
447 struct sja1105_ptp_cmd cmd
= ptp_data
->cmd
;
450 mutex_lock(&ptp_data
->lock
);
454 dev_dbg(ds
->dev
, "Resetting PTP clock\n");
455 rc
= sja1105_ptp_commit(ds
, &cmd
, SPI_WRITE
);
457 sja1105_tas_clockstep(priv
->ds
);
459 mutex_unlock(&ptp_data
->lock
);
464 /* Caller must hold ptp_data->lock */
465 int __sja1105_ptp_gettimex(struct dsa_switch
*ds
, u64
*ns
,
466 struct ptp_system_timestamp
*ptp_sts
)
468 struct sja1105_private
*priv
= ds
->priv
;
472 rc
= sja1105_ptpclkval_read(priv
, &ticks
, ptp_sts
);
474 dev_err(ds
->dev
, "Failed to read PTP clock: %d\n", rc
);
478 *ns
= sja1105_ticks_to_ns(ticks
);
483 static int sja1105_ptp_gettimex(struct ptp_clock_info
*ptp
,
484 struct timespec64
*ts
,
485 struct ptp_system_timestamp
*ptp_sts
)
487 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
488 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
492 mutex_lock(&ptp_data
->lock
);
494 rc
= __sja1105_ptp_gettimex(priv
->ds
, &now
, ptp_sts
);
495 *ts
= ns_to_timespec64(now
);
497 mutex_unlock(&ptp_data
->lock
);
502 /* Caller must hold ptp_data->lock */
503 static int sja1105_ptp_mode_set(struct sja1105_private
*priv
,
504 enum sja1105_ptp_clk_mode mode
)
506 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
508 if (ptp_data
->cmd
.ptpclkadd
== mode
)
511 ptp_data
->cmd
.ptpclkadd
= mode
;
513 return sja1105_ptp_commit(priv
->ds
, &ptp_data
->cmd
, SPI_WRITE
);
516 /* Write to PTPCLKVAL while PTPCLKADD is 0 */
517 int __sja1105_ptp_settime(struct dsa_switch
*ds
, u64 ns
,
518 struct ptp_system_timestamp
*ptp_sts
)
520 struct sja1105_private
*priv
= ds
->priv
;
521 u64 ticks
= ns_to_sja1105_ticks(ns
);
524 rc
= sja1105_ptp_mode_set(priv
, PTP_SET_MODE
);
526 dev_err(priv
->ds
->dev
, "Failed to put PTPCLK in set mode\n");
530 rc
= sja1105_ptpclkval_write(priv
, ticks
, ptp_sts
);
532 sja1105_tas_clockstep(priv
->ds
);
537 static int sja1105_ptp_settime(struct ptp_clock_info
*ptp
,
538 const struct timespec64
*ts
)
540 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
541 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
542 u64 ns
= timespec64_to_ns(ts
);
545 mutex_lock(&ptp_data
->lock
);
547 rc
= __sja1105_ptp_settime(priv
->ds
, ns
, NULL
);
549 mutex_unlock(&ptp_data
->lock
);
554 static int sja1105_ptp_adjfine(struct ptp_clock_info
*ptp
, long scaled_ppm
)
556 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
557 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
558 const struct sja1105_regs
*regs
= priv
->info
->regs
;
563 clkrate
= (s64
)scaled_ppm
* SJA1105_CC_MULT_NUM
;
564 clkrate
= div_s64(clkrate
, SJA1105_CC_MULT_DEM
);
566 /* Take a +/- value and re-center it around 2^31. */
567 clkrate
= SJA1105_CC_MULT
+ clkrate
;
568 WARN_ON(abs(clkrate
) >= GENMASK_ULL(31, 0));
571 mutex_lock(&ptp_data
->lock
);
573 rc
= sja1105_xfer_u32(priv
, SPI_WRITE
, regs
->ptpclkrate
, &clkrate32
,
576 sja1105_tas_adjfreq(priv
->ds
);
578 mutex_unlock(&ptp_data
->lock
);
583 /* Write to PTPCLKVAL while PTPCLKADD is 1 */
584 int __sja1105_ptp_adjtime(struct dsa_switch
*ds
, s64 delta
)
586 struct sja1105_private
*priv
= ds
->priv
;
587 s64 ticks
= ns_to_sja1105_ticks(delta
);
590 rc
= sja1105_ptp_mode_set(priv
, PTP_ADD_MODE
);
592 dev_err(priv
->ds
->dev
, "Failed to put PTPCLK in add mode\n");
596 rc
= sja1105_ptpclkval_write(priv
, ticks
, NULL
);
598 sja1105_tas_clockstep(priv
->ds
);
603 static int sja1105_ptp_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
605 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
606 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
609 mutex_lock(&ptp_data
->lock
);
611 rc
= __sja1105_ptp_adjtime(priv
->ds
, delta
);
613 mutex_unlock(&ptp_data
->lock
);
618 int sja1105_ptp_clock_register(struct dsa_switch
*ds
)
620 struct sja1105_private
*priv
= ds
->priv
;
621 struct sja1105_tagger_data
*tagger_data
= &priv
->tagger_data
;
622 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
624 ptp_data
->caps
= (struct ptp_clock_info
) {
625 .owner
= THIS_MODULE
,
626 .name
= "SJA1105 PHC",
627 .adjfine
= sja1105_ptp_adjfine
,
628 .adjtime
= sja1105_ptp_adjtime
,
629 .gettimex64
= sja1105_ptp_gettimex
,
630 .settime64
= sja1105_ptp_settime
,
631 .do_aux_work
= sja1105_rxtstamp_work
,
632 .max_adj
= SJA1105_MAX_ADJ_PPB
,
635 skb_queue_head_init(&ptp_data
->skb_rxtstamp_queue
);
636 spin_lock_init(&tagger_data
->meta_lock
);
638 ptp_data
->clock
= ptp_clock_register(&ptp_data
->caps
, ds
->dev
);
639 if (IS_ERR_OR_NULL(ptp_data
->clock
))
640 return PTR_ERR(ptp_data
->clock
);
642 ptp_data
->cmd
.corrclk4ts
= true;
643 ptp_data
->cmd
.ptpclkadd
= PTP_SET_MODE
;
645 return sja1105_ptp_reset(ds
);
648 void sja1105_ptp_clock_unregister(struct dsa_switch
*ds
)
650 struct sja1105_private
*priv
= ds
->priv
;
651 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
653 if (IS_ERR_OR_NULL(ptp_data
->clock
))
656 ptp_cancel_worker_sync(ptp_data
->clock
);
657 skb_queue_purge(&ptp_data
->skb_rxtstamp_queue
);
658 ptp_clock_unregister(ptp_data
->clock
);
659 ptp_data
->clock
= NULL
;
662 void sja1105_ptp_txtstamp_skb(struct dsa_switch
*ds
, int port
,
665 struct sja1105_private
*priv
= ds
->priv
;
666 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
667 struct skb_shared_hwtstamps shwt
= {0};
671 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
673 mutex_lock(&ptp_data
->lock
);
675 rc
= sja1105_ptpclkval_read(priv
, &ticks
, NULL
);
677 dev_err(ds
->dev
, "Failed to read PTP clock: %d\n", rc
);
682 rc
= sja1105_ptpegr_ts_poll(ds
, port
, &ts
);
684 dev_err(ds
->dev
, "timed out polling for tstamp\n");
689 ts
= sja1105_tstamp_reconstruct(ds
, ticks
, ts
);
691 shwt
.hwtstamp
= ns_to_ktime(sja1105_ticks_to_ns(ts
));
692 skb_complete_tx_timestamp(skb
, &shwt
);
695 mutex_unlock(&ptp_data
->lock
);