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 /* PTPSYNCTS has no interrupt or update mechanism, because the intended
18 * hardware use case is for the timestamp to be collected synchronously,
19 * immediately after the CAS_MASTER SJA1105 switch has triggered a CASSYNC
20 * pulse on the PTP_CLK pin. When used as a generic extts source, it needs
21 * polling and a comparison with the old value. The polling interval is just
22 * the Nyquist rate of a canonical PPS input (e.g. from a GPS module).
23 * Anything of higher frequency than 1 Hz will be lost, since there is no
26 #define SJA1105_EXTTS_INTERVAL (HZ / 2)
28 /* This range is actually +/- SJA1105_MAX_ADJ_PPB
29 * divided by 1000 (ppb -> ppm) and with a 16-bit
30 * "fractional" part (actually fixed point).
33 * Convert scaled_ppm from the +/- ((10^6) << 16) range
34 * into the +/- (1 << 31) range.
36 * This forgoes a "ppb" numeric representation (up to NSEC_PER_SEC)
37 * and defines the scaling factor between scaled_ppm and the actual
38 * frequency adjustments of the PHC.
40 * ptpclkrate = scaled_ppm * 2^31 / (10^6 * 2^16)
42 * ptpclkrate = scaled_ppm * 2^9 / 5^6
44 #define SJA1105_CC_MULT_NUM (1 << 9)
45 #define SJA1105_CC_MULT_DEM 15625
46 #define SJA1105_CC_MULT 0x80000000
48 enum sja1105_ptp_clk_mode
{
53 #define extts_to_data(d) \
54 container_of((d), struct sja1105_ptp_data, extts_work)
55 #define ptp_caps_to_data(d) \
56 container_of((d), struct sja1105_ptp_data, caps)
57 #define ptp_data_to_sja1105(d) \
58 container_of((d), struct sja1105_private, ptp_data)
60 /* Must be called only with priv->tagger_data.state bit
61 * SJA1105_HWTS_RX_EN cleared
63 static int sja1105_change_rxtstamping(struct sja1105_private
*priv
,
66 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
67 struct sja1105_general_params_entry
*general_params
;
68 struct sja1105_table
*table
;
70 table
= &priv
->static_config
.tables
[BLK_IDX_GENERAL_PARAMS
];
71 general_params
= table
->entries
;
72 general_params
->send_meta1
= on
;
73 general_params
->send_meta0
= on
;
75 /* Initialize the meta state machine to a known state */
76 if (priv
->tagger_data
.stampable_skb
) {
77 kfree_skb(priv
->tagger_data
.stampable_skb
);
78 priv
->tagger_data
.stampable_skb
= NULL
;
80 ptp_cancel_worker_sync(ptp_data
->clock
);
81 skb_queue_purge(&ptp_data
->skb_rxtstamp_queue
);
83 return sja1105_static_config_reload(priv
, SJA1105_RX_HWTSTAMPING
);
86 int sja1105_hwtstamp_set(struct dsa_switch
*ds
, int port
, struct ifreq
*ifr
)
88 struct sja1105_private
*priv
= ds
->priv
;
89 struct hwtstamp_config config
;
93 if (copy_from_user(&config
, ifr
->ifr_data
, sizeof(config
)))
96 switch (config
.tx_type
) {
98 priv
->ports
[port
].hwts_tx_en
= false;
101 priv
->ports
[port
].hwts_tx_en
= true;
107 switch (config
.rx_filter
) {
108 case HWTSTAMP_FILTER_NONE
:
116 if (rx_on
!= test_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
)) {
117 clear_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
);
119 rc
= sja1105_change_rxtstamping(priv
, rx_on
);
122 "Failed to change RX timestamping: %d\n", rc
);
126 set_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
);
129 if (copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)))
134 int sja1105_hwtstamp_get(struct dsa_switch
*ds
, int port
, struct ifreq
*ifr
)
136 struct sja1105_private
*priv
= ds
->priv
;
137 struct hwtstamp_config config
;
140 if (priv
->ports
[port
].hwts_tx_en
)
141 config
.tx_type
= HWTSTAMP_TX_ON
;
143 config
.tx_type
= HWTSTAMP_TX_OFF
;
144 if (test_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
))
145 config
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_L2_EVENT
;
147 config
.rx_filter
= HWTSTAMP_FILTER_NONE
;
149 return copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)) ?
153 int sja1105_get_ts_info(struct dsa_switch
*ds
, int port
,
154 struct ethtool_ts_info
*info
)
156 struct sja1105_private
*priv
= ds
->priv
;
157 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
159 /* Called during cleanup */
160 if (!ptp_data
->clock
)
163 info
->so_timestamping
= SOF_TIMESTAMPING_TX_HARDWARE
|
164 SOF_TIMESTAMPING_RX_HARDWARE
|
165 SOF_TIMESTAMPING_RAW_HARDWARE
;
166 info
->tx_types
= (1 << HWTSTAMP_TX_OFF
) |
167 (1 << HWTSTAMP_TX_ON
);
168 info
->rx_filters
= (1 << HWTSTAMP_FILTER_NONE
) |
169 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT
);
170 info
->phc_index
= ptp_clock_index(ptp_data
->clock
);
174 void sja1105et_ptp_cmd_packing(u8
*buf
, struct sja1105_ptp_cmd
*cmd
,
177 const int size
= SJA1105_SIZE_PTP_CMD
;
178 /* No need to keep this as part of the structure */
181 sja1105_packing(buf
, &valid
, 31, 31, size
, op
);
182 sja1105_packing(buf
, &cmd
->ptpstrtsch
, 30, 30, size
, op
);
183 sja1105_packing(buf
, &cmd
->ptpstopsch
, 29, 29, size
, op
);
184 sja1105_packing(buf
, &cmd
->startptpcp
, 28, 28, size
, op
);
185 sja1105_packing(buf
, &cmd
->stopptpcp
, 27, 27, size
, op
);
186 sja1105_packing(buf
, &cmd
->resptp
, 2, 2, size
, op
);
187 sja1105_packing(buf
, &cmd
->corrclk4ts
, 1, 1, size
, op
);
188 sja1105_packing(buf
, &cmd
->ptpclkadd
, 0, 0, size
, op
);
191 void sja1105pqrs_ptp_cmd_packing(u8
*buf
, struct sja1105_ptp_cmd
*cmd
,
194 const int size
= SJA1105_SIZE_PTP_CMD
;
195 /* No need to keep this as part of the structure */
198 sja1105_packing(buf
, &valid
, 31, 31, size
, op
);
199 sja1105_packing(buf
, &cmd
->ptpstrtsch
, 30, 30, size
, op
);
200 sja1105_packing(buf
, &cmd
->ptpstopsch
, 29, 29, size
, op
);
201 sja1105_packing(buf
, &cmd
->startptpcp
, 28, 28, size
, op
);
202 sja1105_packing(buf
, &cmd
->stopptpcp
, 27, 27, size
, op
);
203 sja1105_packing(buf
, &cmd
->resptp
, 3, 3, size
, op
);
204 sja1105_packing(buf
, &cmd
->corrclk4ts
, 2, 2, size
, op
);
205 sja1105_packing(buf
, &cmd
->ptpclkadd
, 0, 0, size
, op
);
208 int sja1105_ptp_commit(struct dsa_switch
*ds
, struct sja1105_ptp_cmd
*cmd
,
209 sja1105_spi_rw_mode_t rw
)
211 const struct sja1105_private
*priv
= ds
->priv
;
212 const struct sja1105_regs
*regs
= priv
->info
->regs
;
213 u8 buf
[SJA1105_SIZE_PTP_CMD
] = {0};
217 priv
->info
->ptp_cmd_packing(buf
, cmd
, PACK
);
219 rc
= sja1105_xfer_buf(priv
, rw
, regs
->ptp_control
, buf
,
220 SJA1105_SIZE_PTP_CMD
);
223 priv
->info
->ptp_cmd_packing(buf
, cmd
, UNPACK
);
228 /* The switch returns partial timestamps (24 bits for SJA1105 E/T, which wrap
229 * around in 0.135 seconds, and 32 bits for P/Q/R/S, wrapping around in 34.35
232 * This receives the RX or TX MAC timestamps, provided by hardware as
233 * the lower bits of the cycle counter, sampled at the time the timestamp was
236 * To reconstruct into a full 64-bit-wide timestamp, the cycle counter is
237 * read and the high-order bits are filled in.
239 * Must be called within one wraparound period of the partial timestamp since
240 * it was generated by the MAC.
242 static u64
sja1105_tstamp_reconstruct(struct dsa_switch
*ds
, u64 now
,
245 struct sja1105_private
*priv
= ds
->priv
;
246 u64 partial_tstamp_mask
= CYCLECOUNTER_MASK(priv
->info
->ptp_ts_bits
);
247 u64 ts_reconstructed
;
249 ts_reconstructed
= (now
& ~partial_tstamp_mask
) | ts_partial
;
251 /* Check lower bits of current cycle counter against the timestamp.
252 * If the current cycle counter is lower than the partial timestamp,
253 * then wraparound surely occurred and must be accounted for.
255 if ((now
& partial_tstamp_mask
) <= ts_partial
)
256 ts_reconstructed
-= (partial_tstamp_mask
+ 1);
258 return ts_reconstructed
;
261 /* Reads the SPI interface for an egress timestamp generated by the switch
262 * for frames sent using management routes.
264 * SJA1105 E/T layout of the 4-byte SPI payload:
268 * +-----+-----+-----+ ^
271 * 24-bit timestamp Update bit
274 * SJA1105 P/Q/R/S layout of the 8-byte SPI payload:
276 * 31 23 15 7 0 63 55 47 39 32
277 * | | | | | | | | | |
278 * ^ +-----+-----+-----+-----+
281 * Update bit 32-bit timestamp
283 * Notice that the update bit is in the same place.
284 * To have common code for E/T and P/Q/R/S for reading the timestamp,
285 * we need to juggle with the offset and the bit indices.
287 static int sja1105_ptpegr_ts_poll(struct dsa_switch
*ds
, int port
, u64
*ts
)
289 struct sja1105_private
*priv
= ds
->priv
;
290 const struct sja1105_regs
*regs
= priv
->info
->regs
;
291 int tstamp_bit_start
, tstamp_bit_end
;
298 rc
= sja1105_xfer_buf(priv
, SPI_READ
, regs
->ptpegr_ts
[port
],
299 packed_buf
, priv
->info
->ptpegr_ts_bytes
);
303 sja1105_unpack(packed_buf
, &update
, 0, 0,
304 priv
->info
->ptpegr_ts_bytes
);
308 usleep_range(10, 50);
314 /* Point the end bit to the second 32-bit word on P/Q/R/S,
317 tstamp_bit_end
= (priv
->info
->ptpegr_ts_bytes
- 4) * 8;
318 /* Shift the 24-bit timestamp on E/T to be collected from 31:8.
321 tstamp_bit_end
+= 32 - priv
->info
->ptp_ts_bits
;
322 tstamp_bit_start
= tstamp_bit_end
+ priv
->info
->ptp_ts_bits
- 1;
326 sja1105_unpack(packed_buf
, ts
, tstamp_bit_start
, tstamp_bit_end
,
327 priv
->info
->ptpegr_ts_bytes
);
332 /* Caller must hold ptp_data->lock */
333 static int sja1105_ptpclkval_read(struct sja1105_private
*priv
, u64
*ticks
,
334 struct ptp_system_timestamp
*ptp_sts
)
336 const struct sja1105_regs
*regs
= priv
->info
->regs
;
338 return sja1105_xfer_u64(priv
, SPI_READ
, regs
->ptpclkval
, ticks
,
342 /* Caller must hold ptp_data->lock */
343 static int sja1105_ptpclkval_write(struct sja1105_private
*priv
, u64 ticks
,
344 struct ptp_system_timestamp
*ptp_sts
)
346 const struct sja1105_regs
*regs
= priv
->info
->regs
;
348 return sja1105_xfer_u64(priv
, SPI_WRITE
, regs
->ptpclkval
, &ticks
,
352 static long sja1105_rxtstamp_work(struct ptp_clock_info
*ptp
)
354 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
355 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
356 struct dsa_switch
*ds
= priv
->ds
;
359 mutex_lock(&ptp_data
->lock
);
361 while ((skb
= skb_dequeue(&ptp_data
->skb_rxtstamp_queue
)) != NULL
) {
362 struct skb_shared_hwtstamps
*shwt
= skb_hwtstamps(skb
);
366 rc
= sja1105_ptpclkval_read(priv
, &ticks
, NULL
);
368 dev_err(ds
->dev
, "Failed to read PTP clock: %d\n", rc
);
373 *shwt
= (struct skb_shared_hwtstamps
) {0};
375 ts
= SJA1105_SKB_CB(skb
)->meta_tstamp
;
376 ts
= sja1105_tstamp_reconstruct(ds
, ticks
, ts
);
378 shwt
->hwtstamp
= ns_to_ktime(sja1105_ticks_to_ns(ts
));
382 mutex_unlock(&ptp_data
->lock
);
388 /* Called from dsa_skb_defer_rx_timestamp */
389 bool sja1105_port_rxtstamp(struct dsa_switch
*ds
, int port
,
390 struct sk_buff
*skb
, unsigned int type
)
392 struct sja1105_private
*priv
= ds
->priv
;
393 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
395 if (!test_bit(SJA1105_HWTS_RX_EN
, &priv
->tagger_data
.state
))
398 /* We need to read the full PTP clock to reconstruct the Rx
399 * timestamp. For that we need a sleepable context.
401 skb_queue_tail(&ptp_data
->skb_rxtstamp_queue
, skb
);
402 ptp_schedule_worker(ptp_data
->clock
, 0);
406 /* Called from dsa_skb_tx_timestamp. This callback is just to make DSA clone
407 * the skb and have it available in DSA_SKB_CB in the .port_deferred_xmit
408 * callback, where we will timestamp it synchronously.
410 bool sja1105_port_txtstamp(struct dsa_switch
*ds
, int port
,
411 struct sk_buff
*skb
, unsigned int type
)
413 struct sja1105_private
*priv
= ds
->priv
;
414 struct sja1105_port
*sp
= &priv
->ports
[port
];
422 static int sja1105_ptp_reset(struct dsa_switch
*ds
)
424 struct sja1105_private
*priv
= ds
->priv
;
425 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
426 struct sja1105_ptp_cmd cmd
= ptp_data
->cmd
;
429 mutex_lock(&ptp_data
->lock
);
433 dev_dbg(ds
->dev
, "Resetting PTP clock\n");
434 rc
= sja1105_ptp_commit(ds
, &cmd
, SPI_WRITE
);
436 sja1105_tas_clockstep(priv
->ds
);
438 mutex_unlock(&ptp_data
->lock
);
443 /* Caller must hold ptp_data->lock */
444 int __sja1105_ptp_gettimex(struct dsa_switch
*ds
, u64
*ns
,
445 struct ptp_system_timestamp
*ptp_sts
)
447 struct sja1105_private
*priv
= ds
->priv
;
451 rc
= sja1105_ptpclkval_read(priv
, &ticks
, ptp_sts
);
453 dev_err(ds
->dev
, "Failed to read PTP clock: %d\n", rc
);
457 *ns
= sja1105_ticks_to_ns(ticks
);
462 static int sja1105_ptp_gettimex(struct ptp_clock_info
*ptp
,
463 struct timespec64
*ts
,
464 struct ptp_system_timestamp
*ptp_sts
)
466 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
467 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
471 mutex_lock(&ptp_data
->lock
);
473 rc
= __sja1105_ptp_gettimex(priv
->ds
, &now
, ptp_sts
);
474 *ts
= ns_to_timespec64(now
);
476 mutex_unlock(&ptp_data
->lock
);
481 /* Caller must hold ptp_data->lock */
482 static int sja1105_ptp_mode_set(struct sja1105_private
*priv
,
483 enum sja1105_ptp_clk_mode mode
)
485 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
487 if (ptp_data
->cmd
.ptpclkadd
== mode
)
490 ptp_data
->cmd
.ptpclkadd
= mode
;
492 return sja1105_ptp_commit(priv
->ds
, &ptp_data
->cmd
, SPI_WRITE
);
495 /* Write to PTPCLKVAL while PTPCLKADD is 0 */
496 int __sja1105_ptp_settime(struct dsa_switch
*ds
, u64 ns
,
497 struct ptp_system_timestamp
*ptp_sts
)
499 struct sja1105_private
*priv
= ds
->priv
;
500 u64 ticks
= ns_to_sja1105_ticks(ns
);
503 rc
= sja1105_ptp_mode_set(priv
, PTP_SET_MODE
);
505 dev_err(priv
->ds
->dev
, "Failed to put PTPCLK in set mode\n");
509 rc
= sja1105_ptpclkval_write(priv
, ticks
, ptp_sts
);
511 sja1105_tas_clockstep(priv
->ds
);
516 static int sja1105_ptp_settime(struct ptp_clock_info
*ptp
,
517 const struct timespec64
*ts
)
519 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
520 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
521 u64 ns
= timespec64_to_ns(ts
);
524 mutex_lock(&ptp_data
->lock
);
526 rc
= __sja1105_ptp_settime(priv
->ds
, ns
, NULL
);
528 mutex_unlock(&ptp_data
->lock
);
533 static int sja1105_ptp_adjfine(struct ptp_clock_info
*ptp
, long scaled_ppm
)
535 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
536 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
537 const struct sja1105_regs
*regs
= priv
->info
->regs
;
542 clkrate
= (s64
)scaled_ppm
* SJA1105_CC_MULT_NUM
;
543 clkrate
= div_s64(clkrate
, SJA1105_CC_MULT_DEM
);
545 /* Take a +/- value and re-center it around 2^31. */
546 clkrate
= SJA1105_CC_MULT
+ clkrate
;
547 WARN_ON(abs(clkrate
) >= GENMASK_ULL(31, 0));
550 mutex_lock(&ptp_data
->lock
);
552 rc
= sja1105_xfer_u32(priv
, SPI_WRITE
, regs
->ptpclkrate
, &clkrate32
,
555 sja1105_tas_adjfreq(priv
->ds
);
557 mutex_unlock(&ptp_data
->lock
);
562 /* Write to PTPCLKVAL while PTPCLKADD is 1 */
563 int __sja1105_ptp_adjtime(struct dsa_switch
*ds
, s64 delta
)
565 struct sja1105_private
*priv
= ds
->priv
;
566 s64 ticks
= ns_to_sja1105_ticks(delta
);
569 rc
= sja1105_ptp_mode_set(priv
, PTP_ADD_MODE
);
571 dev_err(priv
->ds
->dev
, "Failed to put PTPCLK in add mode\n");
575 rc
= sja1105_ptpclkval_write(priv
, ticks
, NULL
);
577 sja1105_tas_clockstep(priv
->ds
);
582 static int sja1105_ptp_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
584 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
585 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
588 mutex_lock(&ptp_data
->lock
);
590 rc
= __sja1105_ptp_adjtime(priv
->ds
, delta
);
592 mutex_unlock(&ptp_data
->lock
);
597 static void sja1105_ptp_extts_work(struct work_struct
*work
)
599 struct delayed_work
*dw
= to_delayed_work(work
);
600 struct sja1105_ptp_data
*ptp_data
= extts_to_data(dw
);
601 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
602 const struct sja1105_regs
*regs
= priv
->info
->regs
;
603 struct ptp_clock_event event
;
607 mutex_lock(&ptp_data
->lock
);
609 rc
= sja1105_xfer_u64(priv
, SPI_READ
, regs
->ptpsyncts
, &ptpsyncts
,
612 dev_err_ratelimited(priv
->ds
->dev
,
613 "Failed to read PTPSYNCTS: %d\n", rc
);
615 if (ptpsyncts
&& ptp_data
->ptpsyncts
!= ptpsyncts
) {
617 event
.type
= PTP_CLOCK_EXTTS
;
618 event
.timestamp
= ns_to_ktime(sja1105_ticks_to_ns(ptpsyncts
));
619 ptp_clock_event(ptp_data
->clock
, &event
);
621 ptp_data
->ptpsyncts
= ptpsyncts
;
624 mutex_unlock(&ptp_data
->lock
);
626 schedule_delayed_work(&ptp_data
->extts_work
, SJA1105_EXTTS_INTERVAL
);
629 static int sja1105_change_ptp_clk_pin_func(struct sja1105_private
*priv
,
630 enum ptp_pin_function func
)
632 struct sja1105_avb_params_entry
*avb
;
633 enum ptp_pin_function old_func
;
635 avb
= priv
->static_config
.tables
[BLK_IDX_AVB_PARAMS
].entries
;
637 if (priv
->info
->device_id
== SJA1105E_DEVICE_ID
||
638 priv
->info
->device_id
== SJA1105T_DEVICE_ID
||
640 old_func
= PTP_PF_PEROUT
;
642 old_func
= PTP_PF_EXTTS
;
644 if (func
== old_func
)
647 avb
->cas_master
= (func
== PTP_PF_PEROUT
);
649 return sja1105_dynamic_config_write(priv
, BLK_IDX_AVB_PARAMS
, 0, avb
,
653 /* The PTP_CLK pin may be configured to toggle with a 50% duty cycle and a
657 * f = ----------------------
658 * (PTPPINDUR * 8 ns) * 2
660 static int sja1105_per_out_enable(struct sja1105_private
*priv
,
661 struct ptp_perout_request
*perout
,
664 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
665 const struct sja1105_regs
*regs
= priv
->info
->regs
;
666 struct sja1105_ptp_cmd cmd
= ptp_data
->cmd
;
669 /* We only support one channel */
670 if (perout
->index
!= 0)
673 /* Reject requests with unsupported flags */
677 mutex_lock(&ptp_data
->lock
);
679 rc
= sja1105_change_ptp_clk_pin_func(priv
, PTP_PF_PEROUT
);
684 struct timespec64 pin_duration_ts
= {
685 .tv_sec
= perout
->period
.sec
,
686 .tv_nsec
= perout
->period
.nsec
,
688 struct timespec64 pin_start_ts
= {
689 .tv_sec
= perout
->start
.sec
,
690 .tv_nsec
= perout
->start
.nsec
,
692 u64 pin_duration
= timespec64_to_ns(&pin_duration_ts
);
693 u64 pin_start
= timespec64_to_ns(&pin_start_ts
);
697 /* ptppindur: 32 bit register which holds the interval between
698 * 2 edges on PTP_CLK. So check for truncation which happens
699 * at periods larger than around 68.7 seconds.
701 pin_duration
= ns_to_sja1105_ticks(pin_duration
/ 2);
702 if (pin_duration
> U32_MAX
) {
706 pin_duration32
= pin_duration
;
708 /* ptppins: 64 bit register which needs to hold a PTP time
709 * larger than the current time, otherwise the startptpcp
710 * command won't do anything. So advance the current time
711 * by a number of periods in a way that won't alter the
714 rc
= __sja1105_ptp_gettimex(priv
->ds
, &now
, NULL
);
718 pin_start
= future_base_time(pin_start
, pin_duration
,
719 now
+ 1ull * NSEC_PER_SEC
);
720 pin_start
= ns_to_sja1105_ticks(pin_start
);
722 rc
= sja1105_xfer_u64(priv
, SPI_WRITE
, regs
->ptppinst
,
727 rc
= sja1105_xfer_u32(priv
, SPI_WRITE
, regs
->ptppindur
,
728 &pin_duration32
, NULL
);
734 cmd
.startptpcp
= true;
736 cmd
.stopptpcp
= true;
738 rc
= sja1105_ptp_commit(priv
->ds
, &cmd
, SPI_WRITE
);
741 mutex_unlock(&ptp_data
->lock
);
746 static int sja1105_extts_enable(struct sja1105_private
*priv
,
747 struct ptp_extts_request
*extts
,
752 /* We only support one channel */
753 if (extts
->index
!= 0)
756 /* Reject requests with unsupported flags */
760 rc
= sja1105_change_ptp_clk_pin_func(priv
, PTP_PF_EXTTS
);
765 schedule_delayed_work(&priv
->ptp_data
.extts_work
,
766 SJA1105_EXTTS_INTERVAL
);
768 cancel_delayed_work_sync(&priv
->ptp_data
.extts_work
);
773 static int sja1105_ptp_enable(struct ptp_clock_info
*ptp
,
774 struct ptp_clock_request
*req
, int on
)
776 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
777 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
778 int rc
= -EOPNOTSUPP
;
780 if (req
->type
== PTP_CLK_REQ_PEROUT
)
781 rc
= sja1105_per_out_enable(priv
, &req
->perout
, on
);
782 else if (req
->type
== PTP_CLK_REQ_EXTTS
)
783 rc
= sja1105_extts_enable(priv
, &req
->extts
, on
);
788 static int sja1105_ptp_verify_pin(struct ptp_clock_info
*ptp
, unsigned int pin
,
789 enum ptp_pin_function func
, unsigned int chan
)
791 struct sja1105_ptp_data
*ptp_data
= ptp_caps_to_data(ptp
);
792 struct sja1105_private
*priv
= ptp_data_to_sja1105(ptp_data
);
794 if (chan
!= 0 || pin
!= 0)
802 if (priv
->info
->device_id
== SJA1105E_DEVICE_ID
||
803 priv
->info
->device_id
== SJA1105T_DEVICE_ID
)
812 static struct ptp_pin_desc sja1105_ptp_pin
= {
818 int sja1105_ptp_clock_register(struct dsa_switch
*ds
)
820 struct sja1105_private
*priv
= ds
->priv
;
821 struct sja1105_tagger_data
*tagger_data
= &priv
->tagger_data
;
822 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
824 ptp_data
->caps
= (struct ptp_clock_info
) {
825 .owner
= THIS_MODULE
,
826 .name
= "SJA1105 PHC",
827 .adjfine
= sja1105_ptp_adjfine
,
828 .adjtime
= sja1105_ptp_adjtime
,
829 .gettimex64
= sja1105_ptp_gettimex
,
830 .settime64
= sja1105_ptp_settime
,
831 .enable
= sja1105_ptp_enable
,
832 .verify
= sja1105_ptp_verify_pin
,
833 .do_aux_work
= sja1105_rxtstamp_work
,
834 .max_adj
= SJA1105_MAX_ADJ_PPB
,
835 .pin_config
= &sja1105_ptp_pin
,
841 skb_queue_head_init(&ptp_data
->skb_rxtstamp_queue
);
842 spin_lock_init(&tagger_data
->meta_lock
);
844 ptp_data
->clock
= ptp_clock_register(&ptp_data
->caps
, ds
->dev
);
845 if (IS_ERR_OR_NULL(ptp_data
->clock
))
846 return PTR_ERR(ptp_data
->clock
);
848 ptp_data
->cmd
.corrclk4ts
= true;
849 ptp_data
->cmd
.ptpclkadd
= PTP_SET_MODE
;
851 INIT_DELAYED_WORK(&ptp_data
->extts_work
, sja1105_ptp_extts_work
);
853 return sja1105_ptp_reset(ds
);
856 void sja1105_ptp_clock_unregister(struct dsa_switch
*ds
)
858 struct sja1105_private
*priv
= ds
->priv
;
859 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
861 if (IS_ERR_OR_NULL(ptp_data
->clock
))
864 cancel_delayed_work_sync(&ptp_data
->extts_work
);
865 ptp_cancel_worker_sync(ptp_data
->clock
);
866 skb_queue_purge(&ptp_data
->skb_rxtstamp_queue
);
867 ptp_clock_unregister(ptp_data
->clock
);
868 ptp_data
->clock
= NULL
;
871 void sja1105_ptp_txtstamp_skb(struct dsa_switch
*ds
, int port
,
874 struct sja1105_private
*priv
= ds
->priv
;
875 struct sja1105_ptp_data
*ptp_data
= &priv
->ptp_data
;
876 struct skb_shared_hwtstamps shwt
= {0};
880 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
882 mutex_lock(&ptp_data
->lock
);
884 rc
= sja1105_ptpclkval_read(priv
, &ticks
, NULL
);
886 dev_err(ds
->dev
, "Failed to read PTP clock: %d\n", rc
);
891 rc
= sja1105_ptpegr_ts_poll(ds
, port
, &ts
);
893 dev_err(ds
->dev
, "timed out polling for tstamp\n");
898 ts
= sja1105_tstamp_reconstruct(ds
, ticks
, ts
);
900 shwt
.hwtstamp
= ns_to_ktime(sja1105_ticks_to_ns(ts
));
901 skb_complete_tx_timestamp(skb
, &shwt
);
904 mutex_unlock(&ptp_data
->lock
);