1*40b0425fSVladimir Oltean // SPDX-License-Identifier: GPL-2.0-or-later 2*40b0425fSVladimir Oltean /* 3*40b0425fSVladimir Oltean * Copyright 2023 NXP 4*40b0425fSVladimir Oltean * 5*40b0425fSVladimir Oltean * Mock-up PTP Hardware Clock driver for virtual network devices 6*40b0425fSVladimir Oltean * 7*40b0425fSVladimir Oltean * Create a PTP clock which offers PTP time manipulation operations 8*40b0425fSVladimir Oltean * using a timecounter/cyclecounter on top of CLOCK_MONOTONIC_RAW. 9*40b0425fSVladimir Oltean */ 10*40b0425fSVladimir Oltean 11*40b0425fSVladimir Oltean #include <linux/ptp_clock_kernel.h> 12*40b0425fSVladimir Oltean #include <linux/ptp_mock.h> 13*40b0425fSVladimir Oltean #include <linux/timecounter.h> 14*40b0425fSVladimir Oltean 15*40b0425fSVladimir Oltean /* Clamp scaled_ppm between -2,097,152,000 and 2,097,152,000, 16*40b0425fSVladimir Oltean * and thus "adj" between -68,719,476 and 68,719,476 17*40b0425fSVladimir Oltean */ 18*40b0425fSVladimir Oltean #define MOCK_PHC_MAX_ADJ_PPB 32000000 19*40b0425fSVladimir Oltean /* Timestamps from ktime_get_raw() have 1 ns resolution, so the scale factor 20*40b0425fSVladimir Oltean * (MULT >> SHIFT) needs to be 1. Pick SHIFT as 31 bits, which translates 21*40b0425fSVladimir Oltean * MULT(freq 0) into 0x80000000. 22*40b0425fSVladimir Oltean */ 23*40b0425fSVladimir Oltean #define MOCK_PHC_CC_SHIFT 31 24*40b0425fSVladimir Oltean #define MOCK_PHC_CC_MULT (1 << MOCK_PHC_CC_SHIFT) 25*40b0425fSVladimir Oltean #define MOCK_PHC_FADJ_SHIFT 9 26*40b0425fSVladimir Oltean #define MOCK_PHC_FADJ_DENOMINATOR 15625ULL 27*40b0425fSVladimir Oltean 28*40b0425fSVladimir Oltean /* The largest cycle_delta that timecounter_read_delta() can handle without a 29*40b0425fSVladimir Oltean * 64-bit overflow during the multiplication with cc->mult, given the max "adj" 30*40b0425fSVladimir Oltean * we permit, is ~8.3 seconds. Make sure readouts are more frequent than that. 31*40b0425fSVladimir Oltean */ 32*40b0425fSVladimir Oltean #define MOCK_PHC_REFRESH_INTERVAL (HZ * 5) 33*40b0425fSVladimir Oltean 34*40b0425fSVladimir Oltean #define info_to_phc(d) container_of((d), struct mock_phc, info) 35*40b0425fSVladimir Oltean 36*40b0425fSVladimir Oltean struct mock_phc { 37*40b0425fSVladimir Oltean struct ptp_clock_info info; 38*40b0425fSVladimir Oltean struct ptp_clock *clock; 39*40b0425fSVladimir Oltean struct timecounter tc; 40*40b0425fSVladimir Oltean struct cyclecounter cc; 41*40b0425fSVladimir Oltean spinlock_t lock; 42*40b0425fSVladimir Oltean }; 43*40b0425fSVladimir Oltean 44*40b0425fSVladimir Oltean static u64 mock_phc_cc_read(const struct cyclecounter *cc) 45*40b0425fSVladimir Oltean { 46*40b0425fSVladimir Oltean return ktime_get_raw_ns(); 47*40b0425fSVladimir Oltean } 48*40b0425fSVladimir Oltean 49*40b0425fSVladimir Oltean static int mock_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm) 50*40b0425fSVladimir Oltean { 51*40b0425fSVladimir Oltean struct mock_phc *phc = info_to_phc(info); 52*40b0425fSVladimir Oltean s64 adj; 53*40b0425fSVladimir Oltean 54*40b0425fSVladimir Oltean adj = (s64)scaled_ppm << MOCK_PHC_FADJ_SHIFT; 55*40b0425fSVladimir Oltean adj = div_s64(adj, MOCK_PHC_FADJ_DENOMINATOR); 56*40b0425fSVladimir Oltean 57*40b0425fSVladimir Oltean spin_lock(&phc->lock); 58*40b0425fSVladimir Oltean timecounter_read(&phc->tc); 59*40b0425fSVladimir Oltean phc->cc.mult = MOCK_PHC_CC_MULT + adj; 60*40b0425fSVladimir Oltean spin_unlock(&phc->lock); 61*40b0425fSVladimir Oltean 62*40b0425fSVladimir Oltean return 0; 63*40b0425fSVladimir Oltean } 64*40b0425fSVladimir Oltean 65*40b0425fSVladimir Oltean static int mock_phc_adjtime(struct ptp_clock_info *info, s64 delta) 66*40b0425fSVladimir Oltean { 67*40b0425fSVladimir Oltean struct mock_phc *phc = info_to_phc(info); 68*40b0425fSVladimir Oltean 69*40b0425fSVladimir Oltean spin_lock(&phc->lock); 70*40b0425fSVladimir Oltean timecounter_adjtime(&phc->tc, delta); 71*40b0425fSVladimir Oltean spin_unlock(&phc->lock); 72*40b0425fSVladimir Oltean 73*40b0425fSVladimir Oltean return 0; 74*40b0425fSVladimir Oltean } 75*40b0425fSVladimir Oltean 76*40b0425fSVladimir Oltean static int mock_phc_settime64(struct ptp_clock_info *info, 77*40b0425fSVladimir Oltean const struct timespec64 *ts) 78*40b0425fSVladimir Oltean { 79*40b0425fSVladimir Oltean struct mock_phc *phc = info_to_phc(info); 80*40b0425fSVladimir Oltean u64 ns = timespec64_to_ns(ts); 81*40b0425fSVladimir Oltean 82*40b0425fSVladimir Oltean spin_lock(&phc->lock); 83*40b0425fSVladimir Oltean timecounter_init(&phc->tc, &phc->cc, ns); 84*40b0425fSVladimir Oltean spin_unlock(&phc->lock); 85*40b0425fSVladimir Oltean 86*40b0425fSVladimir Oltean return 0; 87*40b0425fSVladimir Oltean } 88*40b0425fSVladimir Oltean 89*40b0425fSVladimir Oltean static int mock_phc_gettime64(struct ptp_clock_info *info, struct timespec64 *ts) 90*40b0425fSVladimir Oltean { 91*40b0425fSVladimir Oltean struct mock_phc *phc = info_to_phc(info); 92*40b0425fSVladimir Oltean u64 ns; 93*40b0425fSVladimir Oltean 94*40b0425fSVladimir Oltean spin_lock(&phc->lock); 95*40b0425fSVladimir Oltean ns = timecounter_read(&phc->tc); 96*40b0425fSVladimir Oltean spin_unlock(&phc->lock); 97*40b0425fSVladimir Oltean 98*40b0425fSVladimir Oltean *ts = ns_to_timespec64(ns); 99*40b0425fSVladimir Oltean 100*40b0425fSVladimir Oltean return 0; 101*40b0425fSVladimir Oltean } 102*40b0425fSVladimir Oltean 103*40b0425fSVladimir Oltean static long mock_phc_refresh(struct ptp_clock_info *info) 104*40b0425fSVladimir Oltean { 105*40b0425fSVladimir Oltean struct timespec64 ts; 106*40b0425fSVladimir Oltean 107*40b0425fSVladimir Oltean mock_phc_gettime64(info, &ts); 108*40b0425fSVladimir Oltean 109*40b0425fSVladimir Oltean return MOCK_PHC_REFRESH_INTERVAL; 110*40b0425fSVladimir Oltean } 111*40b0425fSVladimir Oltean 112*40b0425fSVladimir Oltean int mock_phc_index(struct mock_phc *phc) 113*40b0425fSVladimir Oltean { 114*40b0425fSVladimir Oltean return ptp_clock_index(phc->clock); 115*40b0425fSVladimir Oltean } 116*40b0425fSVladimir Oltean EXPORT_SYMBOL_GPL(mock_phc_index); 117*40b0425fSVladimir Oltean 118*40b0425fSVladimir Oltean struct mock_phc *mock_phc_create(struct device *dev) 119*40b0425fSVladimir Oltean { 120*40b0425fSVladimir Oltean struct mock_phc *phc; 121*40b0425fSVladimir Oltean int err; 122*40b0425fSVladimir Oltean 123*40b0425fSVladimir Oltean phc = kzalloc(sizeof(*phc), GFP_KERNEL); 124*40b0425fSVladimir Oltean if (!phc) { 125*40b0425fSVladimir Oltean err = -ENOMEM; 126*40b0425fSVladimir Oltean goto out; 127*40b0425fSVladimir Oltean } 128*40b0425fSVladimir Oltean 129*40b0425fSVladimir Oltean phc->info = (struct ptp_clock_info) { 130*40b0425fSVladimir Oltean .owner = THIS_MODULE, 131*40b0425fSVladimir Oltean .name = "Mock-up PTP clock", 132*40b0425fSVladimir Oltean .max_adj = MOCK_PHC_MAX_ADJ_PPB, 133*40b0425fSVladimir Oltean .adjfine = mock_phc_adjfine, 134*40b0425fSVladimir Oltean .adjtime = mock_phc_adjtime, 135*40b0425fSVladimir Oltean .gettime64 = mock_phc_gettime64, 136*40b0425fSVladimir Oltean .settime64 = mock_phc_settime64, 137*40b0425fSVladimir Oltean .do_aux_work = mock_phc_refresh, 138*40b0425fSVladimir Oltean }; 139*40b0425fSVladimir Oltean 140*40b0425fSVladimir Oltean phc->cc = (struct cyclecounter) { 141*40b0425fSVladimir Oltean .read = mock_phc_cc_read, 142*40b0425fSVladimir Oltean .mask = CYCLECOUNTER_MASK(64), 143*40b0425fSVladimir Oltean .mult = MOCK_PHC_CC_MULT, 144*40b0425fSVladimir Oltean .shift = MOCK_PHC_CC_SHIFT, 145*40b0425fSVladimir Oltean }; 146*40b0425fSVladimir Oltean 147*40b0425fSVladimir Oltean spin_lock_init(&phc->lock); 148*40b0425fSVladimir Oltean timecounter_init(&phc->tc, &phc->cc, 0); 149*40b0425fSVladimir Oltean 150*40b0425fSVladimir Oltean phc->clock = ptp_clock_register(&phc->info, dev); 151*40b0425fSVladimir Oltean if (IS_ERR(phc->clock)) { 152*40b0425fSVladimir Oltean err = PTR_ERR(phc->clock); 153*40b0425fSVladimir Oltean goto out_free_phc; 154*40b0425fSVladimir Oltean } 155*40b0425fSVladimir Oltean 156*40b0425fSVladimir Oltean ptp_schedule_worker(phc->clock, MOCK_PHC_REFRESH_INTERVAL); 157*40b0425fSVladimir Oltean 158*40b0425fSVladimir Oltean return phc; 159*40b0425fSVladimir Oltean 160*40b0425fSVladimir Oltean out_free_phc: 161*40b0425fSVladimir Oltean kfree(phc); 162*40b0425fSVladimir Oltean out: 163*40b0425fSVladimir Oltean return ERR_PTR(err); 164*40b0425fSVladimir Oltean } 165*40b0425fSVladimir Oltean EXPORT_SYMBOL_GPL(mock_phc_create); 166*40b0425fSVladimir Oltean 167*40b0425fSVladimir Oltean void mock_phc_destroy(struct mock_phc *phc) 168*40b0425fSVladimir Oltean { 169*40b0425fSVladimir Oltean ptp_clock_unregister(phc->clock); 170*40b0425fSVladimir Oltean kfree(phc); 171*40b0425fSVladimir Oltean } 172*40b0425fSVladimir Oltean EXPORT_SYMBOL_GPL(mock_phc_destroy); 173*40b0425fSVladimir Oltean 174*40b0425fSVladimir Oltean MODULE_DESCRIPTION("Mock-up PTP Hardware Clock driver"); 175*40b0425fSVladimir Oltean MODULE_LICENSE("GPL"); 176