1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
4 * Copyright (C) 2020 Marvell.
8 #include <linux/module.h>
10 #include "otx2_common.h"
13 static bool is_tstmp_atomic_update_supported(struct otx2_ptp *ptp)
15 struct ptp_get_cap_rsp *rsp;
22 mutex_lock(&ptp->nic->mbox.lock);
23 req = otx2_mbox_alloc_msg_ptp_get_cap(&ptp->nic->mbox);
25 mutex_unlock(&ptp->nic->mbox.lock);
29 err = otx2_sync_mbox_msg(&ptp->nic->mbox);
31 mutex_unlock(&ptp->nic->mbox.lock);
34 rsp = (struct ptp_get_cap_rsp *)otx2_mbox_get_rsp(&ptp->nic->mbox.mbox, 0,
36 mutex_unlock(&ptp->nic->mbox.lock);
41 if (rsp->cap & PTP_CAP_HW_ATOMIC_UPDATE)
47 static int otx2_ptp_hw_adjtime(struct ptp_clock_info *ptp_info, s64 delta)
49 struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
51 struct otx2_nic *pfvf = ptp->nic;
58 mutex_lock(&pfvf->mbox.lock);
59 req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
61 mutex_unlock(&pfvf->mbox.lock);
64 req->op = PTP_OP_ADJTIME;
66 rc = otx2_sync_mbox_msg(&ptp->nic->mbox);
67 mutex_unlock(&pfvf->mbox.lock);
72 static u64 otx2_ptp_get_clock(struct otx2_ptp *ptp)
81 req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
85 req->op = PTP_OP_GET_CLOCK;
87 err = otx2_sync_mbox_msg(&ptp->nic->mbox);
91 rsp = (struct ptp_rsp *)otx2_mbox_get_rsp(&ptp->nic->mbox.mbox, 0,
99 static int otx2_ptp_hw_gettime(struct ptp_clock_info *ptp_info,
100 struct timespec64 *ts)
102 struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
106 tstamp = otx2_ptp_get_clock(ptp);
108 *ts = ns_to_timespec64(tstamp);
112 static int otx2_ptp_hw_settime(struct ptp_clock_info *ptp_info,
113 const struct timespec64 *ts)
115 struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
117 struct otx2_nic *pfvf = ptp->nic;
125 nsec = timespec64_to_ns(ts);
127 mutex_lock(&pfvf->mbox.lock);
128 req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
130 mutex_unlock(&pfvf->mbox.lock);
134 req->op = PTP_OP_SET_CLOCK;
136 rc = otx2_sync_mbox_msg(&ptp->nic->mbox);
137 mutex_unlock(&pfvf->mbox.lock);
142 static int otx2_ptp_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
144 struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
151 req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
155 req->op = PTP_OP_ADJFINE;
156 req->scaled_ppm = scaled_ppm;
158 return otx2_sync_mbox_msg(&ptp->nic->mbox);
161 static int ptp_set_thresh(struct otx2_ptp *ptp, u64 thresh)
168 req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
172 req->op = PTP_OP_SET_THRESH;
173 req->thresh = thresh;
175 return otx2_sync_mbox_msg(&ptp->nic->mbox);
178 static int ptp_pps_on(struct otx2_ptp *ptp, int on, u64 period)
185 req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
189 req->op = PTP_OP_PPS_ON;
191 req->period = period;
193 return otx2_sync_mbox_msg(&ptp->nic->mbox);
196 static u64 ptp_cc_read(const struct cyclecounter *cc)
198 struct otx2_ptp *ptp = container_of(cc, struct otx2_ptp, cycle_counter);
200 return otx2_ptp_get_clock(ptp);
203 static u64 ptp_tstmp_read(struct otx2_ptp *ptp)
212 req = otx2_mbox_alloc_msg_ptp_op(&ptp->nic->mbox);
216 req->op = PTP_OP_GET_TSTMP;
218 err = otx2_sync_mbox_msg(&ptp->nic->mbox);
222 rsp = (struct ptp_rsp *)otx2_mbox_get_rsp(&ptp->nic->mbox.mbox, 0,
230 static int otx2_ptp_tc_adjtime(struct ptp_clock_info *ptp_info, s64 delta)
232 struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
234 struct otx2_nic *pfvf = ptp->nic;
236 mutex_lock(&pfvf->mbox.lock);
237 timecounter_adjtime(&ptp->time_counter, delta);
238 mutex_unlock(&pfvf->mbox.lock);
243 static int otx2_ptp_tc_gettime(struct ptp_clock_info *ptp_info,
244 struct timespec64 *ts)
246 struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
250 mutex_lock(&ptp->nic->mbox.lock);
251 tstamp = timecounter_read(&ptp->time_counter);
252 mutex_unlock(&ptp->nic->mbox.lock);
253 *ts = ns_to_timespec64(tstamp);
258 static int otx2_ptp_tc_settime(struct ptp_clock_info *ptp_info,
259 const struct timespec64 *ts)
261 struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
265 nsec = timespec64_to_ns(ts);
267 mutex_lock(&ptp->nic->mbox.lock);
268 timecounter_init(&ptp->time_counter, &ptp->cycle_counter, nsec);
269 mutex_unlock(&ptp->nic->mbox.lock);
274 static int otx2_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
275 enum ptp_pin_function func, unsigned int chan)
288 static u64 otx2_ptp_hw_tstamp2time(const struct timecounter *time_counter, u64 tstamp)
290 /* On HW which supports atomic updates, timecounter is not initialized */
294 static void otx2_ptp_extts_check(struct work_struct *work)
296 struct otx2_ptp *ptp = container_of(work, struct otx2_ptp,
298 struct ptp_clock_event event;
299 u64 tstmp, new_thresh;
301 mutex_lock(&ptp->nic->mbox.lock);
302 tstmp = ptp_tstmp_read(ptp);
303 mutex_unlock(&ptp->nic->mbox.lock);
305 if (tstmp != ptp->last_extts) {
306 event.type = PTP_CLOCK_EXTTS;
308 event.timestamp = ptp->ptp_tstamp2nsec(&ptp->time_counter, tstmp);
309 ptp_clock_event(ptp->ptp_clock, &event);
310 new_thresh = tstmp % 500000000;
311 if (ptp->thresh != new_thresh) {
312 mutex_lock(&ptp->nic->mbox.lock);
313 ptp_set_thresh(ptp, new_thresh);
314 mutex_unlock(&ptp->nic->mbox.lock);
315 ptp->thresh = new_thresh;
317 ptp->last_extts = tstmp;
319 schedule_delayed_work(&ptp->extts_work, msecs_to_jiffies(200));
322 static void otx2_sync_tstamp(struct work_struct *work)
324 struct otx2_ptp *ptp = container_of(work, struct otx2_ptp,
325 synctstamp_work.work);
326 struct otx2_nic *pfvf = ptp->nic;
329 mutex_lock(&pfvf->mbox.lock);
330 tstamp = otx2_ptp_get_clock(ptp);
331 mutex_unlock(&pfvf->mbox.lock);
333 ptp->tstamp = ptp->ptp_tstamp2nsec(&ptp->time_counter, tstamp);
334 ptp->base_ns = tstamp % NSEC_PER_SEC;
336 schedule_delayed_work(&ptp->synctstamp_work, msecs_to_jiffies(250));
339 static int otx2_ptp_enable(struct ptp_clock_info *ptp_info,
340 struct ptp_clock_request *rq, int on)
342 struct otx2_ptp *ptp = container_of(ptp_info, struct otx2_ptp,
351 case PTP_CLK_REQ_EXTTS:
352 pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS,
357 schedule_delayed_work(&ptp->extts_work, msecs_to_jiffies(200));
359 cancel_delayed_work_sync(&ptp->extts_work);
362 case PTP_CLK_REQ_PEROUT:
363 if (rq->perout.flags)
366 if (rq->perout.index >= ptp_info->n_pins)
369 period = rq->perout.period.sec * NSEC_PER_SEC +
370 rq->perout.period.nsec;
371 ptp_pps_on(ptp, on, period);
373 ptp_pps_on(ptp, on, period);
382 int otx2_ptp_init(struct otx2_nic *pfvf)
384 struct otx2_ptp *ptp_ptr;
385 struct cyclecounter *cc;
389 if (is_otx2_lbkvf(pfvf->pdev)) {
394 mutex_lock(&pfvf->mbox.lock);
395 /* check if PTP block is available */
396 req = otx2_mbox_alloc_msg_ptp_op(&pfvf->mbox);
398 mutex_unlock(&pfvf->mbox.lock);
402 req->op = PTP_OP_GET_CLOCK;
404 err = otx2_sync_mbox_msg(&pfvf->mbox);
406 mutex_unlock(&pfvf->mbox.lock);
409 mutex_unlock(&pfvf->mbox.lock);
411 ptp_ptr = kzalloc(sizeof(*ptp_ptr), GFP_KERNEL);
419 snprintf(ptp_ptr->extts_config.name, sizeof(ptp_ptr->extts_config.name), "TSTAMP");
420 ptp_ptr->extts_config.index = 0;
421 ptp_ptr->extts_config.func = PTP_PF_NONE;
423 ptp_ptr->ptp_info = (struct ptp_clock_info) {
424 .owner = THIS_MODULE,
425 .name = "OcteonTX2 PTP",
426 .max_adj = 1000000000ull,
431 .pin_config = &ptp_ptr->extts_config,
432 .adjfine = otx2_ptp_adjfine,
433 .enable = otx2_ptp_enable,
434 .verify = otx2_ptp_verify_pin,
437 /* Check whether hardware supports atomic updates to timestamp */
438 if (is_tstmp_atomic_update_supported(ptp_ptr)) {
439 ptp_ptr->ptp_info.adjtime = otx2_ptp_hw_adjtime;
440 ptp_ptr->ptp_info.gettime64 = otx2_ptp_hw_gettime;
441 ptp_ptr->ptp_info.settime64 = otx2_ptp_hw_settime;
443 ptp_ptr->ptp_tstamp2nsec = otx2_ptp_hw_tstamp2time;
445 ptp_ptr->ptp_info.adjtime = otx2_ptp_tc_adjtime;
446 ptp_ptr->ptp_info.gettime64 = otx2_ptp_tc_gettime;
447 ptp_ptr->ptp_info.settime64 = otx2_ptp_tc_settime;
449 cc = &ptp_ptr->cycle_counter;
450 cc->read = ptp_cc_read;
451 cc->mask = CYCLECOUNTER_MASK(64);
454 ptp_ptr->ptp_tstamp2nsec = timecounter_cyc2time;
456 timecounter_init(&ptp_ptr->time_counter, &ptp_ptr->cycle_counter,
457 ktime_to_ns(ktime_get_real()));
460 INIT_DELAYED_WORK(&ptp_ptr->extts_work, otx2_ptp_extts_check);
462 ptp_ptr->ptp_clock = ptp_clock_register(&ptp_ptr->ptp_info, pfvf->dev);
463 if (IS_ERR_OR_NULL(ptp_ptr->ptp_clock)) {
464 err = ptp_ptr->ptp_clock ?
465 PTR_ERR(ptp_ptr->ptp_clock) : -ENODEV;
470 if (is_dev_otx2(pfvf->pdev)) {
471 ptp_ptr->convert_rx_ptp_tstmp = &otx2_ptp_convert_rx_timestamp;
472 ptp_ptr->convert_tx_ptp_tstmp = &otx2_ptp_convert_tx_timestamp;
474 ptp_ptr->convert_rx_ptp_tstmp = &cn10k_ptp_convert_timestamp;
475 ptp_ptr->convert_tx_ptp_tstmp = &cn10k_ptp_convert_timestamp;
478 INIT_DELAYED_WORK(&ptp_ptr->synctstamp_work, otx2_sync_tstamp);
485 EXPORT_SYMBOL_GPL(otx2_ptp_init);
487 void otx2_ptp_destroy(struct otx2_nic *pfvf)
489 struct otx2_ptp *ptp = pfvf->ptp;
494 cancel_delayed_work(&pfvf->ptp->synctstamp_work);
496 ptp_clock_unregister(ptp->ptp_clock);
500 EXPORT_SYMBOL_GPL(otx2_ptp_destroy);
502 int otx2_ptp_clock_index(struct otx2_nic *pfvf)
507 return ptp_clock_index(pfvf->ptp->ptp_clock);
509 EXPORT_SYMBOL_GPL(otx2_ptp_clock_index);
511 int otx2_ptp_tstamp2time(struct otx2_nic *pfvf, u64 tstamp, u64 *tsns)
516 *tsns = pfvf->ptp->ptp_tstamp2nsec(&pfvf->ptp->time_counter, tstamp);
520 EXPORT_SYMBOL_GPL(otx2_ptp_tstamp2time);
522 MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
523 MODULE_DESCRIPTION("Marvell RVU NIC PTP Driver");
524 MODULE_LICENSE("GPL v2");