1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Vladimir Oltean <[email protected]>
3 */
4 #include <linux/spi/spi.h>
5 #include "sja1105.h"
6
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.
10 *
11 * Percentually speaking, this is a +/- 0.032x adjustment of the
12 * free-running counter (0.968x to 1.032x).
13 */
14 #define SJA1105_MAX_ADJ_PPB 32000000
15 #define SJA1105_SIZE_PTP_CMD 4
16
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 performed a CASSYNC
20 * one-shot toggle (no return to level) on the PTP_CLK pin. When used as a
21 * generic extts source, the PTPSYNCTS register needs polling and a comparison
22 * with the old value. The polling interval is configured as the Nyquist rate
23 * of a signal with 50% duty cycle and 1Hz frequency, which is sadly all that
24 * this hardware can do (but may be enough for some setups). Anything of higher
25 * frequency than 1 Hz will be lost, since there is no timestamp FIFO.
26 */
27 #define SJA1105_EXTTS_INTERVAL (HZ / 6)
28
29 /* This range is actually +/- SJA1105_MAX_ADJ_PPB
30 * divided by 1000 (ppb -> ppm) and with a 16-bit
31 * "fractional" part (actually fixed point).
32 * |
33 * v
34 * Convert scaled_ppm from the +/- ((10^6) << 16) range
35 * into the +/- (1 << 31) range.
36 *
37 * This forgoes a "ppb" numeric representation (up to NSEC_PER_SEC)
38 * and defines the scaling factor between scaled_ppm and the actual
39 * frequency adjustments of the PHC.
40 *
41 * ptpclkrate = scaled_ppm * 2^31 / (10^6 * 2^16)
42 * simplifies to
43 * ptpclkrate = scaled_ppm * 2^9 / 5^6
44 */
45 #define SJA1105_CC_MULT_NUM (1 << 9)
46 #define SJA1105_CC_MULT_DEM 15625
47 #define SJA1105_CC_MULT 0x80000000
48
49 enum sja1105_ptp_clk_mode {
50 PTP_ADD_MODE = 1,
51 PTP_SET_MODE = 0,
52 };
53
54 #define extts_to_data(t) \
55 container_of((t), struct sja1105_ptp_data, extts_timer)
56 #define ptp_caps_to_data(d) \
57 container_of((d), struct sja1105_ptp_data, caps)
58 #define ptp_data_to_sja1105(d) \
59 container_of((d), struct sja1105_private, ptp_data)
60
sja1105_hwtstamp_set(struct dsa_switch * ds,int port,struct ifreq * ifr)61 int sja1105_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr)
62 {
63 struct sja1105_private *priv = ds->priv;
64 unsigned long hwts_tx_en, hwts_rx_en;
65 struct hwtstamp_config config;
66
67 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
68 return -EFAULT;
69
70 hwts_tx_en = priv->hwts_tx_en;
71 hwts_rx_en = priv->hwts_rx_en;
72
73 switch (config.tx_type) {
74 case HWTSTAMP_TX_OFF:
75 hwts_tx_en &= ~BIT(port);
76 break;
77 case HWTSTAMP_TX_ON:
78 hwts_tx_en |= BIT(port);
79 break;
80 default:
81 return -ERANGE;
82 }
83
84 switch (config.rx_filter) {
85 case HWTSTAMP_FILTER_NONE:
86 hwts_rx_en &= ~BIT(port);
87 break;
88 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
89 hwts_rx_en |= BIT(port);
90 break;
91 default:
92 return -ERANGE;
93 }
94
95 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
96 return -EFAULT;
97
98 priv->hwts_tx_en = hwts_tx_en;
99 priv->hwts_rx_en = hwts_rx_en;
100
101 return 0;
102 }
103
sja1105_hwtstamp_get(struct dsa_switch * ds,int port,struct ifreq * ifr)104 int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr)
105 {
106 struct sja1105_private *priv = ds->priv;
107 struct hwtstamp_config config;
108
109 config.flags = 0;
110 if (priv->hwts_tx_en & BIT(port))
111 config.tx_type = HWTSTAMP_TX_ON;
112 else
113 config.tx_type = HWTSTAMP_TX_OFF;
114 if (priv->hwts_rx_en & BIT(port))
115 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
116 else
117 config.rx_filter = HWTSTAMP_FILTER_NONE;
118
119 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
120 -EFAULT : 0;
121 }
122
sja1105_get_ts_info(struct dsa_switch * ds,int port,struct kernel_ethtool_ts_info * info)123 int sja1105_get_ts_info(struct dsa_switch *ds, int port,
124 struct kernel_ethtool_ts_info *info)
125 {
126 struct sja1105_private *priv = ds->priv;
127 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
128
129 /* Called during cleanup */
130 if (!ptp_data->clock)
131 return -ENODEV;
132
133 info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE |
134 SOF_TIMESTAMPING_RX_HARDWARE |
135 SOF_TIMESTAMPING_RAW_HARDWARE;
136 info->tx_types = (1 << HWTSTAMP_TX_OFF) |
137 (1 << HWTSTAMP_TX_ON);
138 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
139 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT);
140 info->phc_index = ptp_clock_index(ptp_data->clock);
141 return 0;
142 }
143
sja1105et_ptp_cmd_packing(u8 * buf,struct sja1105_ptp_cmd * cmd,enum packing_op op)144 void sja1105et_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
145 enum packing_op op)
146 {
147 const int size = SJA1105_SIZE_PTP_CMD;
148 /* No need to keep this as part of the structure */
149 u64 valid = 1;
150
151 sja1105_packing(buf, &valid, 31, 31, size, op);
152 sja1105_packing(buf, &cmd->ptpstrtsch, 30, 30, size, op);
153 sja1105_packing(buf, &cmd->ptpstopsch, 29, 29, size, op);
154 sja1105_packing(buf, &cmd->startptpcp, 28, 28, size, op);
155 sja1105_packing(buf, &cmd->stopptpcp, 27, 27, size, op);
156 sja1105_packing(buf, &cmd->resptp, 2, 2, size, op);
157 sja1105_packing(buf, &cmd->corrclk4ts, 1, 1, size, op);
158 sja1105_packing(buf, &cmd->ptpclkadd, 0, 0, size, op);
159 }
160
sja1105pqrs_ptp_cmd_packing(u8 * buf,struct sja1105_ptp_cmd * cmd,enum packing_op op)161 void sja1105pqrs_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
162 enum packing_op op)
163 {
164 const int size = SJA1105_SIZE_PTP_CMD;
165 /* No need to keep this as part of the structure */
166 u64 valid = 1;
167
168 sja1105_packing(buf, &valid, 31, 31, size, op);
169 sja1105_packing(buf, &cmd->ptpstrtsch, 30, 30, size, op);
170 sja1105_packing(buf, &cmd->ptpstopsch, 29, 29, size, op);
171 sja1105_packing(buf, &cmd->startptpcp, 28, 28, size, op);
172 sja1105_packing(buf, &cmd->stopptpcp, 27, 27, size, op);
173 sja1105_packing(buf, &cmd->resptp, 3, 3, size, op);
174 sja1105_packing(buf, &cmd->corrclk4ts, 2, 2, size, op);
175 sja1105_packing(buf, &cmd->ptpclkadd, 0, 0, size, op);
176 }
177
sja1105_ptp_commit(struct dsa_switch * ds,struct sja1105_ptp_cmd * cmd,sja1105_spi_rw_mode_t rw)178 int sja1105_ptp_commit(struct dsa_switch *ds, struct sja1105_ptp_cmd *cmd,
179 sja1105_spi_rw_mode_t rw)
180 {
181 const struct sja1105_private *priv = ds->priv;
182 const struct sja1105_regs *regs = priv->info->regs;
183 u8 buf[SJA1105_SIZE_PTP_CMD] = {0};
184 int rc;
185
186 if (rw == SPI_WRITE)
187 priv->info->ptp_cmd_packing(buf, cmd, PACK);
188
189 rc = sja1105_xfer_buf(priv, rw, regs->ptp_control, buf,
190 SJA1105_SIZE_PTP_CMD);
191
192 if (rw == SPI_READ)
193 priv->info->ptp_cmd_packing(buf, cmd, UNPACK);
194
195 return rc;
196 }
197
198 /* The switch returns partial timestamps (24 bits for SJA1105 E/T, which wrap
199 * around in 0.135 seconds, and 32 bits for P/Q/R/S, wrapping around in 34.35
200 * seconds).
201 *
202 * This receives the RX or TX MAC timestamps, provided by hardware as
203 * the lower bits of the cycle counter, sampled at the time the timestamp was
204 * collected.
205 *
206 * To reconstruct into a full 64-bit-wide timestamp, the cycle counter is
207 * read and the high-order bits are filled in.
208 *
209 * Must be called within one wraparound period of the partial timestamp since
210 * it was generated by the MAC.
211 */
sja1105_tstamp_reconstruct(struct dsa_switch * ds,u64 now,u64 ts_partial)212 static u64 sja1105_tstamp_reconstruct(struct dsa_switch *ds, u64 now,
213 u64 ts_partial)
214 {
215 struct sja1105_private *priv = ds->priv;
216 u64 partial_tstamp_mask = CYCLECOUNTER_MASK(priv->info->ptp_ts_bits);
217 u64 ts_reconstructed;
218
219 ts_reconstructed = (now & ~partial_tstamp_mask) | ts_partial;
220
221 /* Check lower bits of current cycle counter against the timestamp.
222 * If the current cycle counter is lower than the partial timestamp,
223 * then wraparound surely occurred and must be accounted for.
224 */
225 if ((now & partial_tstamp_mask) <= ts_partial)
226 ts_reconstructed -= (partial_tstamp_mask + 1);
227
228 return ts_reconstructed;
229 }
230
231 /* Reads the SPI interface for an egress timestamp generated by the switch
232 * for frames sent using management routes.
233 *
234 * SJA1105 E/T layout of the 4-byte SPI payload:
235 *
236 * 31 23 15 7 0
237 * | | | | |
238 * +-----+-----+-----+ ^
239 * ^ |
240 * | |
241 * 24-bit timestamp Update bit
242 *
243 *
244 * SJA1105 P/Q/R/S layout of the 8-byte SPI payload:
245 *
246 * 31 23 15 7 0 63 55 47 39 32
247 * | | | | | | | | | |
248 * ^ +-----+-----+-----+-----+
249 * | ^
250 * | |
251 * Update bit 32-bit timestamp
252 *
253 * Notice that the update bit is in the same place.
254 * To have common code for E/T and P/Q/R/S for reading the timestamp,
255 * we need to juggle with the offset and the bit indices.
256 */
sja1105_ptpegr_ts_poll(struct dsa_switch * ds,int port,u64 * ts)257 static int sja1105_ptpegr_ts_poll(struct dsa_switch *ds, int port, u64 *ts)
258 {
259 struct sja1105_private *priv = ds->priv;
260 const struct sja1105_regs *regs = priv->info->regs;
261 int tstamp_bit_start, tstamp_bit_end;
262 int timeout = 10;
263 u8 packed_buf[8];
264 u64 update;
265 int rc;
266
267 do {
268 rc = sja1105_xfer_buf(priv, SPI_READ, regs->ptpegr_ts[port],
269 packed_buf, priv->info->ptpegr_ts_bytes);
270 if (rc < 0)
271 return rc;
272
273 sja1105_unpack(packed_buf, &update, 0, 0,
274 priv->info->ptpegr_ts_bytes);
275 if (update)
276 break;
277
278 usleep_range(10, 50);
279 } while (--timeout);
280
281 if (!timeout)
282 return -ETIMEDOUT;
283
284 /* Point the end bit to the second 32-bit word on P/Q/R/S,
285 * no-op on E/T.
286 */
287 tstamp_bit_end = (priv->info->ptpegr_ts_bytes - 4) * 8;
288 /* Shift the 24-bit timestamp on E/T to be collected from 31:8.
289 * No-op on P/Q/R/S.
290 */
291 tstamp_bit_end += 32 - priv->info->ptp_ts_bits;
292 tstamp_bit_start = tstamp_bit_end + priv->info->ptp_ts_bits - 1;
293
294 *ts = 0;
295
296 sja1105_unpack(packed_buf, ts, tstamp_bit_start, tstamp_bit_end,
297 priv->info->ptpegr_ts_bytes);
298
299 return 0;
300 }
301
302 /* Caller must hold ptp_data->lock */
sja1105_ptpclkval_read(struct sja1105_private * priv,u64 * ticks,struct ptp_system_timestamp * ptp_sts)303 static int sja1105_ptpclkval_read(struct sja1105_private *priv, u64 *ticks,
304 struct ptp_system_timestamp *ptp_sts)
305 {
306 const struct sja1105_regs *regs = priv->info->regs;
307
308 return sja1105_xfer_u64(priv, SPI_READ, regs->ptpclkval, ticks,
309 ptp_sts);
310 }
311
312 /* Caller must hold ptp_data->lock */
sja1105_ptpclkval_write(struct sja1105_private * priv,u64 ticks,struct ptp_system_timestamp * ptp_sts)313 static int sja1105_ptpclkval_write(struct sja1105_private *priv, u64 ticks,
314 struct ptp_system_timestamp *ptp_sts)
315 {
316 const struct sja1105_regs *regs = priv->info->regs;
317
318 return sja1105_xfer_u64(priv, SPI_WRITE, regs->ptpclkval, &ticks,
319 ptp_sts);
320 }
321
sja1105_extts_poll(struct sja1105_private * priv)322 static void sja1105_extts_poll(struct sja1105_private *priv)
323 {
324 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
325 const struct sja1105_regs *regs = priv->info->regs;
326 struct ptp_clock_event event;
327 u64 ptpsyncts = 0;
328 int rc;
329
330 rc = sja1105_xfer_u64(priv, SPI_READ, regs->ptpsyncts, &ptpsyncts,
331 NULL);
332 if (rc < 0)
333 dev_err_ratelimited(priv->ds->dev,
334 "Failed to read PTPSYNCTS: %d\n", rc);
335
336 if (ptpsyncts && ptp_data->ptpsyncts != ptpsyncts) {
337 event.index = 0;
338 event.type = PTP_CLOCK_EXTTS;
339 event.timestamp = ns_to_ktime(sja1105_ticks_to_ns(ptpsyncts));
340 ptp_clock_event(ptp_data->clock, &event);
341
342 ptp_data->ptpsyncts = ptpsyncts;
343 }
344 }
345
sja1105_rxtstamp_work(struct ptp_clock_info * ptp)346 static long sja1105_rxtstamp_work(struct ptp_clock_info *ptp)
347 {
348 struct sja1105_ptp_data *ptp_data = ptp_caps_to_data(ptp);
349 struct sja1105_private *priv = ptp_data_to_sja1105(ptp_data);
350 struct dsa_switch *ds = priv->ds;
351 struct sk_buff *skb;
352
353 mutex_lock(&ptp_data->lock);
354
355 while ((skb = skb_dequeue(&ptp_data->skb_rxtstamp_queue)) != NULL) {
356 struct skb_shared_hwtstamps *shwt = skb_hwtstamps(skb);
357 u64 ticks, ts;
358 int rc;
359
360 rc = sja1105_ptpclkval_read(priv, &ticks, NULL);
361 if (rc < 0) {
362 dev_err(ds->dev, "Failed to read PTP clock: %d\n", rc);
363 kfree_skb(skb);
364 continue;
365 }
366
367 *shwt = (struct skb_shared_hwtstamps) {0};
368
369 ts = SJA1105_SKB_CB(skb)->tstamp;
370 ts = sja1105_tstamp_reconstruct(ds, ticks, ts);
371
372 shwt->hwtstamp = ns_to_ktime(sja1105_ticks_to_ns(ts));
373 netif_rx(skb);
374 }
375
376 if (ptp_data->extts_enabled)
377 sja1105_extts_poll(priv);
378
379 mutex_unlock(&ptp_data->lock);
380
381 /* Don't restart */
382 return -1;
383 }
384
sja1105_rxtstamp(struct dsa_switch * ds,int port,struct sk_buff * skb)385 bool sja1105_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb)
386 {
387 struct sja1105_private *priv = ds->priv;
388 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
389
390 if (!(priv->hwts_rx_en & BIT(port)))
391 return false;
392
393 /* We need to read the full PTP clock to reconstruct the Rx
394 * timestamp. For that we need a sleepable context.
395 */
396 skb_queue_tail(&ptp_data->skb_rxtstamp_queue, skb);
397 ptp_schedule_worker(ptp_data->clock, 0);
398 return true;
399 }
400
sja1110_rxtstamp(struct dsa_switch * ds,int port,struct sk_buff * skb)401 bool sja1110_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb)
402 {
403 struct skb_shared_hwtstamps *shwt = skb_hwtstamps(skb);
404 u64 ts = SJA1105_SKB_CB(skb)->tstamp;
405
406 *shwt = (struct skb_shared_hwtstamps) {0};
407
408 shwt->hwtstamp = ns_to_ktime(sja1105_ticks_to_ns(ts));
409
410 /* Don't defer */
411 return false;
412 }
413
414 /* Called from dsa_skb_defer_rx_timestamp */
sja1105_port_rxtstamp(struct dsa_switch * ds,int port,struct sk_buff * skb,unsigned int type)415 bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
416 struct sk_buff *skb, unsigned int type)
417 {
418 struct sja1105_private *priv = ds->priv;
419
420 return priv->info->rxtstamp(ds, port, skb);
421 }
422
sja1110_process_meta_tstamp(struct dsa_switch * ds,int port,u8 ts_id,enum sja1110_meta_tstamp dir,u64 tstamp)423 void sja1110_process_meta_tstamp(struct dsa_switch *ds, int port, u8 ts_id,
424 enum sja1110_meta_tstamp dir, u64 tstamp)
425 {
426 struct sja1105_private *priv = ds->priv;
427 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
428 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
429 struct skb_shared_hwtstamps shwt = {0};
430
431 /* We don't care about RX timestamps on the CPU port */
432 if (dir == SJA1110_META_TSTAMP_RX)
433 return;
434
435 spin_lock(&ptp_data->skb_txtstamp_queue.lock);
436
437 skb_queue_walk_safe(&ptp_data->skb_txtstamp_queue, skb, skb_tmp) {
438 if (SJA1105_SKB_CB(skb)->ts_id != ts_id)
439 continue;
440
441 __skb_unlink(skb, &ptp_data->skb_txtstamp_queue);
442 skb_match = skb;
443
444 break;
445 }
446
447 spin_unlock(&ptp_data->skb_txtstamp_queue.lock);
448
449 if (WARN_ON(!skb_match))
450 return;
451
452 shwt.hwtstamp = ns_to_ktime(sja1105_ticks_to_ns(tstamp));
453 skb_complete_tx_timestamp(skb_match, &shwt);
454 }
455
456 /* In addition to cloning the skb which is done by the common
457 * sja1105_port_txtstamp, we need to generate a timestamp ID and save the
458 * packet to the TX timestamping queue.
459 */
sja1110_txtstamp(struct dsa_switch * ds,int port,struct sk_buff * skb)460 void sja1110_txtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb)
461 {
462 struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone;
463 struct sja1105_private *priv = ds->priv;
464 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
465 u8 ts_id;
466
467 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
468
469 spin_lock(&priv->ts_id_lock);
470
471 ts_id = priv->ts_id;
472 /* Deal automatically with 8-bit wraparound */
473 priv->ts_id++;
474
475 SJA1105_SKB_CB(clone)->ts_id = ts_id;
476
477 spin_unlock(&priv->ts_id_lock);
478
479 skb_queue_tail(&ptp_data->skb_txtstamp_queue, clone);
480 }
481
482 /* Called from dsa_skb_tx_timestamp. This callback is just to clone
483 * the skb and have it available in SJA1105_SKB_CB in the .port_deferred_xmit
484 * callback, where we will timestamp it synchronously.
485 */
sja1105_port_txtstamp(struct dsa_switch * ds,int port,struct sk_buff * skb)486 void sja1105_port_txtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb)
487 {
488 struct sja1105_private *priv = ds->priv;
489 struct sk_buff *clone;
490
491 if (!(priv->hwts_tx_en & BIT(port)))
492 return;
493
494 clone = skb_clone_sk(skb);
495 if (!clone)
496 return;
497
498 SJA1105_SKB_CB(skb)->clone = clone;
499
500 if (priv->info->txtstamp)
501 priv->info->txtstamp(ds, port, skb);
502 }
503
sja1105_ptp_reset(struct dsa_switch * ds)504 static int sja1105_ptp_reset(struct dsa_switch *ds)
505 {
506 struct sja1105_private *priv = ds->priv;
507 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
508 struct sja1105_ptp_cmd cmd = ptp_data->cmd;
509 int rc;
510
511 mutex_lock(&ptp_data->lock);
512
513 cmd.resptp = 1;
514
515 dev_dbg(ds->dev, "Resetting PTP clock\n");
516 rc = sja1105_ptp_commit(ds, &cmd, SPI_WRITE);
517
518 sja1105_tas_clockstep(priv->ds);
519
520 mutex_unlock(&ptp_data->lock);
521
522 return rc;
523 }
524
525 /* Caller must hold ptp_data->lock */
__sja1105_ptp_gettimex(struct dsa_switch * ds,u64 * ns,struct ptp_system_timestamp * ptp_sts)526 int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
527 struct ptp_system_timestamp *ptp_sts)
528 {
529 struct sja1105_private *priv = ds->priv;
530 u64 ticks;
531 int rc;
532
533 rc = sja1105_ptpclkval_read(priv, &ticks, ptp_sts);
534 if (rc < 0) {
535 dev_err(ds->dev, "Failed to read PTP clock: %d\n", rc);
536 return rc;
537 }
538
539 *ns = sja1105_ticks_to_ns(ticks);
540
541 return 0;
542 }
543
sja1105_ptp_gettimex(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * ptp_sts)544 static int sja1105_ptp_gettimex(struct ptp_clock_info *ptp,
545 struct timespec64 *ts,
546 struct ptp_system_timestamp *ptp_sts)
547 {
548 struct sja1105_ptp_data *ptp_data = ptp_caps_to_data(ptp);
549 struct sja1105_private *priv = ptp_data_to_sja1105(ptp_data);
550 u64 now = 0;
551 int rc;
552
553 mutex_lock(&ptp_data->lock);
554
555 rc = __sja1105_ptp_gettimex(priv->ds, &now, ptp_sts);
556 *ts = ns_to_timespec64(now);
557
558 mutex_unlock(&ptp_data->lock);
559
560 return rc;
561 }
562
563 /* Caller must hold ptp_data->lock */
sja1105_ptp_mode_set(struct sja1105_private * priv,enum sja1105_ptp_clk_mode mode)564 static int sja1105_ptp_mode_set(struct sja1105_private *priv,
565 enum sja1105_ptp_clk_mode mode)
566 {
567 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
568
569 if (ptp_data->cmd.ptpclkadd == mode)
570 return 0;
571
572 ptp_data->cmd.ptpclkadd = mode;
573
574 return sja1105_ptp_commit(priv->ds, &ptp_data->cmd, SPI_WRITE);
575 }
576
577 /* Write to PTPCLKVAL while PTPCLKADD is 0 */
__sja1105_ptp_settime(struct dsa_switch * ds,u64 ns,struct ptp_system_timestamp * ptp_sts)578 int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
579 struct ptp_system_timestamp *ptp_sts)
580 {
581 struct sja1105_private *priv = ds->priv;
582 u64 ticks = ns_to_sja1105_ticks(ns);
583 int rc;
584
585 rc = sja1105_ptp_mode_set(priv, PTP_SET_MODE);
586 if (rc < 0) {
587 dev_err(priv->ds->dev, "Failed to put PTPCLK in set mode\n");
588 return rc;
589 }
590
591 rc = sja1105_ptpclkval_write(priv, ticks, ptp_sts);
592
593 sja1105_tas_clockstep(priv->ds);
594
595 return rc;
596 }
597
sja1105_ptp_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)598 static int sja1105_ptp_settime(struct ptp_clock_info *ptp,
599 const struct timespec64 *ts)
600 {
601 struct sja1105_ptp_data *ptp_data = ptp_caps_to_data(ptp);
602 struct sja1105_private *priv = ptp_data_to_sja1105(ptp_data);
603 u64 ns = timespec64_to_ns(ts);
604 int rc;
605
606 mutex_lock(&ptp_data->lock);
607
608 rc = __sja1105_ptp_settime(priv->ds, ns, NULL);
609
610 mutex_unlock(&ptp_data->lock);
611
612 return rc;
613 }
614
sja1105_ptp_adjfine(struct ptp_clock_info * ptp,long scaled_ppm)615 static int sja1105_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
616 {
617 struct sja1105_ptp_data *ptp_data = ptp_caps_to_data(ptp);
618 struct sja1105_private *priv = ptp_data_to_sja1105(ptp_data);
619 const struct sja1105_regs *regs = priv->info->regs;
620 u32 clkrate32;
621 s64 clkrate;
622 int rc;
623
624 clkrate = (s64)scaled_ppm * SJA1105_CC_MULT_NUM;
625 clkrate = div_s64(clkrate, SJA1105_CC_MULT_DEM);
626
627 /* Take a +/- value and re-center it around 2^31. */
628 clkrate = SJA1105_CC_MULT + clkrate;
629 WARN_ON(abs(clkrate) >= GENMASK_ULL(31, 0));
630 clkrate32 = clkrate;
631
632 mutex_lock(&ptp_data->lock);
633
634 rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->ptpclkrate, &clkrate32,
635 NULL);
636
637 sja1105_tas_adjfreq(priv->ds);
638
639 mutex_unlock(&ptp_data->lock);
640
641 return rc;
642 }
643
644 /* Write to PTPCLKVAL while PTPCLKADD is 1 */
__sja1105_ptp_adjtime(struct dsa_switch * ds,s64 delta)645 int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta)
646 {
647 struct sja1105_private *priv = ds->priv;
648 s64 ticks = ns_to_sja1105_ticks(delta);
649 int rc;
650
651 rc = sja1105_ptp_mode_set(priv, PTP_ADD_MODE);
652 if (rc < 0) {
653 dev_err(priv->ds->dev, "Failed to put PTPCLK in add mode\n");
654 return rc;
655 }
656
657 rc = sja1105_ptpclkval_write(priv, ticks, NULL);
658
659 sja1105_tas_clockstep(priv->ds);
660
661 return rc;
662 }
663
sja1105_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)664 static int sja1105_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
665 {
666 struct sja1105_ptp_data *ptp_data = ptp_caps_to_data(ptp);
667 struct sja1105_private *priv = ptp_data_to_sja1105(ptp_data);
668 int rc;
669
670 mutex_lock(&ptp_data->lock);
671
672 rc = __sja1105_ptp_adjtime(priv->ds, delta);
673
674 mutex_unlock(&ptp_data->lock);
675
676 return rc;
677 }
678
sja1105_ptp_extts_setup_timer(struct sja1105_ptp_data * ptp_data)679 static void sja1105_ptp_extts_setup_timer(struct sja1105_ptp_data *ptp_data)
680 {
681 unsigned long expires = ((jiffies / SJA1105_EXTTS_INTERVAL) + 1) *
682 SJA1105_EXTTS_INTERVAL;
683
684 mod_timer(&ptp_data->extts_timer, expires);
685 }
686
sja1105_ptp_extts_timer(struct timer_list * t)687 static void sja1105_ptp_extts_timer(struct timer_list *t)
688 {
689 struct sja1105_ptp_data *ptp_data = extts_to_data(t);
690
691 ptp_schedule_worker(ptp_data->clock, 0);
692
693 sja1105_ptp_extts_setup_timer(ptp_data);
694 }
695
sja1105_change_ptp_clk_pin_func(struct sja1105_private * priv,enum ptp_pin_function func)696 static int sja1105_change_ptp_clk_pin_func(struct sja1105_private *priv,
697 enum ptp_pin_function func)
698 {
699 struct sja1105_avb_params_entry *avb;
700 enum ptp_pin_function old_func;
701
702 avb = priv->static_config.tables[BLK_IDX_AVB_PARAMS].entries;
703
704 if (priv->info->device_id == SJA1105E_DEVICE_ID ||
705 priv->info->device_id == SJA1105T_DEVICE_ID ||
706 avb->cas_master)
707 old_func = PTP_PF_PEROUT;
708 else
709 old_func = PTP_PF_EXTTS;
710
711 if (func == old_func)
712 return 0;
713
714 avb->cas_master = (func == PTP_PF_PEROUT);
715
716 return sja1105_dynamic_config_write(priv, BLK_IDX_AVB_PARAMS, 0, avb,
717 true);
718 }
719
720 /* The PTP_CLK pin may be configured to toggle with a 50% duty cycle and a
721 * frequency f:
722 *
723 * NSEC_PER_SEC
724 * f = ----------------------
725 * (PTPPINDUR * 8 ns) * 2
726 */
sja1105_per_out_enable(struct sja1105_private * priv,struct ptp_perout_request * perout,bool on)727 static int sja1105_per_out_enable(struct sja1105_private *priv,
728 struct ptp_perout_request *perout,
729 bool on)
730 {
731 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
732 const struct sja1105_regs *regs = priv->info->regs;
733 struct sja1105_ptp_cmd cmd = ptp_data->cmd;
734 int rc;
735
736 /* We only support one channel */
737 if (perout->index != 0)
738 return -EOPNOTSUPP;
739
740 /* Reject requests with unsupported flags */
741 if (perout->flags)
742 return -EOPNOTSUPP;
743
744 mutex_lock(&ptp_data->lock);
745
746 rc = sja1105_change_ptp_clk_pin_func(priv, PTP_PF_PEROUT);
747 if (rc)
748 goto out;
749
750 if (on) {
751 struct timespec64 pin_duration_ts = {
752 .tv_sec = perout->period.sec,
753 .tv_nsec = perout->period.nsec,
754 };
755 struct timespec64 pin_start_ts = {
756 .tv_sec = perout->start.sec,
757 .tv_nsec = perout->start.nsec,
758 };
759 u64 pin_duration = timespec64_to_ns(&pin_duration_ts);
760 u64 pin_start = timespec64_to_ns(&pin_start_ts);
761 u32 pin_duration32;
762 u64 now;
763
764 /* ptppindur: 32 bit register which holds the interval between
765 * 2 edges on PTP_CLK. So check for truncation which happens
766 * at periods larger than around 68.7 seconds.
767 */
768 pin_duration = ns_to_sja1105_ticks(pin_duration / 2);
769 if (pin_duration > U32_MAX) {
770 rc = -ERANGE;
771 goto out;
772 }
773 pin_duration32 = pin_duration;
774
775 /* ptppins: 64 bit register which needs to hold a PTP time
776 * larger than the current time, otherwise the startptpcp
777 * command won't do anything. So advance the current time
778 * by a number of periods in a way that won't alter the
779 * phase offset.
780 */
781 rc = __sja1105_ptp_gettimex(priv->ds, &now, NULL);
782 if (rc < 0)
783 goto out;
784
785 pin_start = future_base_time(pin_start, pin_duration,
786 now + 1ull * NSEC_PER_SEC);
787 pin_start = ns_to_sja1105_ticks(pin_start);
788
789 rc = sja1105_xfer_u64(priv, SPI_WRITE, regs->ptppinst,
790 &pin_start, NULL);
791 if (rc < 0)
792 goto out;
793
794 rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->ptppindur,
795 &pin_duration32, NULL);
796 if (rc < 0)
797 goto out;
798 }
799
800 if (on)
801 cmd.startptpcp = true;
802 else
803 cmd.stopptpcp = true;
804
805 rc = sja1105_ptp_commit(priv->ds, &cmd, SPI_WRITE);
806
807 out:
808 mutex_unlock(&ptp_data->lock);
809
810 return rc;
811 }
812
sja1105_extts_enable(struct sja1105_private * priv,struct ptp_extts_request * extts,bool on)813 static int sja1105_extts_enable(struct sja1105_private *priv,
814 struct ptp_extts_request *extts,
815 bool on)
816 {
817 int rc;
818
819 /* We only support one channel */
820 if (extts->index != 0)
821 return -EOPNOTSUPP;
822
823 /* Reject requests with unsupported flags */
824 if (extts->flags & ~(PTP_ENABLE_FEATURE |
825 PTP_RISING_EDGE |
826 PTP_FALLING_EDGE |
827 PTP_STRICT_FLAGS))
828 return -EOPNOTSUPP;
829
830 /* We can only enable time stamping on both edges, sadly. */
831 if ((extts->flags & PTP_STRICT_FLAGS) &&
832 (extts->flags & PTP_ENABLE_FEATURE) &&
833 (extts->flags & PTP_EXTTS_EDGES) != PTP_EXTTS_EDGES)
834 return -EOPNOTSUPP;
835
836 rc = sja1105_change_ptp_clk_pin_func(priv, PTP_PF_EXTTS);
837 if (rc)
838 return rc;
839
840 priv->ptp_data.extts_enabled = on;
841
842 if (on)
843 sja1105_ptp_extts_setup_timer(&priv->ptp_data);
844 else
845 del_timer_sync(&priv->ptp_data.extts_timer);
846
847 return 0;
848 }
849
sja1105_ptp_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * req,int on)850 static int sja1105_ptp_enable(struct ptp_clock_info *ptp,
851 struct ptp_clock_request *req, int on)
852 {
853 struct sja1105_ptp_data *ptp_data = ptp_caps_to_data(ptp);
854 struct sja1105_private *priv = ptp_data_to_sja1105(ptp_data);
855 int rc = -EOPNOTSUPP;
856
857 if (req->type == PTP_CLK_REQ_PEROUT)
858 rc = sja1105_per_out_enable(priv, &req->perout, on);
859 else if (req->type == PTP_CLK_REQ_EXTTS)
860 rc = sja1105_extts_enable(priv, &req->extts, on);
861
862 return rc;
863 }
864
sja1105_ptp_verify_pin(struct ptp_clock_info * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)865 static int sja1105_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
866 enum ptp_pin_function func, unsigned int chan)
867 {
868 struct sja1105_ptp_data *ptp_data = ptp_caps_to_data(ptp);
869 struct sja1105_private *priv = ptp_data_to_sja1105(ptp_data);
870
871 if (chan != 0 || pin != 0)
872 return -1;
873
874 switch (func) {
875 case PTP_PF_NONE:
876 case PTP_PF_PEROUT:
877 break;
878 case PTP_PF_EXTTS:
879 if (priv->info->device_id == SJA1105E_DEVICE_ID ||
880 priv->info->device_id == SJA1105T_DEVICE_ID)
881 return -1;
882 break;
883 default:
884 return -1;
885 }
886 return 0;
887 }
888
889 static struct ptp_pin_desc sja1105_ptp_pin = {
890 .name = "ptp_clk",
891 .index = 0,
892 .func = PTP_PF_NONE,
893 };
894
sja1105_ptp_clock_register(struct dsa_switch * ds)895 int sja1105_ptp_clock_register(struct dsa_switch *ds)
896 {
897 struct sja1105_private *priv = ds->priv;
898 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
899
900 ptp_data->caps = (struct ptp_clock_info) {
901 .owner = THIS_MODULE,
902 .name = "SJA1105 PHC",
903 .adjfine = sja1105_ptp_adjfine,
904 .adjtime = sja1105_ptp_adjtime,
905 .gettimex64 = sja1105_ptp_gettimex,
906 .settime64 = sja1105_ptp_settime,
907 .enable = sja1105_ptp_enable,
908 .verify = sja1105_ptp_verify_pin,
909 .do_aux_work = sja1105_rxtstamp_work,
910 .max_adj = SJA1105_MAX_ADJ_PPB,
911 .pin_config = &sja1105_ptp_pin,
912 .n_pins = 1,
913 .n_ext_ts = 1,
914 .n_per_out = 1,
915 };
916
917 /* Only used on SJA1105 */
918 skb_queue_head_init(&ptp_data->skb_rxtstamp_queue);
919 /* Only used on SJA1110 */
920 skb_queue_head_init(&ptp_data->skb_txtstamp_queue);
921
922 ptp_data->clock = ptp_clock_register(&ptp_data->caps, ds->dev);
923 if (IS_ERR_OR_NULL(ptp_data->clock))
924 return PTR_ERR(ptp_data->clock);
925
926 ptp_data->cmd.corrclk4ts = true;
927 ptp_data->cmd.ptpclkadd = PTP_SET_MODE;
928
929 timer_setup(&ptp_data->extts_timer, sja1105_ptp_extts_timer, 0);
930
931 return sja1105_ptp_reset(ds);
932 }
933
sja1105_ptp_clock_unregister(struct dsa_switch * ds)934 void sja1105_ptp_clock_unregister(struct dsa_switch *ds)
935 {
936 struct sja1105_private *priv = ds->priv;
937 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
938
939 if (IS_ERR_OR_NULL(ptp_data->clock))
940 return;
941
942 del_timer_sync(&ptp_data->extts_timer);
943 ptp_cancel_worker_sync(ptp_data->clock);
944 skb_queue_purge(&ptp_data->skb_txtstamp_queue);
945 skb_queue_purge(&ptp_data->skb_rxtstamp_queue);
946 ptp_clock_unregister(ptp_data->clock);
947 ptp_data->clock = NULL;
948 }
949
sja1105_ptp_txtstamp_skb(struct dsa_switch * ds,int port,struct sk_buff * skb)950 void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int port,
951 struct sk_buff *skb)
952 {
953 struct sja1105_private *priv = ds->priv;
954 struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
955 struct skb_shared_hwtstamps shwt = {0};
956 u64 ticks, ts;
957 int rc;
958
959 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
960
961 mutex_lock(&ptp_data->lock);
962
963 rc = sja1105_ptpegr_ts_poll(ds, port, &ts);
964 if (rc < 0) {
965 dev_err(ds->dev, "timed out polling for tstamp\n");
966 kfree_skb(skb);
967 goto out;
968 }
969
970 rc = sja1105_ptpclkval_read(priv, &ticks, NULL);
971 if (rc < 0) {
972 dev_err(ds->dev, "Failed to read PTP clock: %d\n", rc);
973 kfree_skb(skb);
974 goto out;
975 }
976
977 ts = sja1105_tstamp_reconstruct(ds, ticks, ts);
978
979 shwt.hwtstamp = ns_to_ktime(sja1105_ticks_to_ns(ts));
980 skb_complete_tx_timestamp(skb, &shwt);
981
982 out:
983 mutex_unlock(&ptp_data->lock);
984 }
985