Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * PTP 1588 clock support
0004  *
0005  * Copyright (C) 2010 OMICRON electronics GmbH
0006  */
0007 
0008 #ifndef _PTP_CLOCK_KERNEL_H_
0009 #define _PTP_CLOCK_KERNEL_H_
0010 
0011 #include <linux/device.h>
0012 #include <linux/pps_kernel.h>
0013 #include <linux/ptp_clock.h>
0014 #include <linux/timecounter.h>
0015 #include <linux/skbuff.h>
0016 
0017 #define PTP_CLOCK_NAME_LEN  32
0018 /**
0019  * struct ptp_clock_request - request PTP clock event
0020  *
0021  * @type:   The type of the request.
0022  *      EXTTS:  Configure external trigger timestamping
0023  *      PEROUT: Configure periodic output signal (e.g. PPS)
0024  *      PPS:    trigger internal PPS event for input
0025  *              into kernel PPS subsystem
0026  * @extts:  describes configuration for external trigger timestamping.
0027  *          This is only valid when event == PTP_CLK_REQ_EXTTS.
0028  * @perout: describes configuration for periodic output.
0029  *      This is only valid when event == PTP_CLK_REQ_PEROUT.
0030  */
0031 
0032 struct ptp_clock_request {
0033     enum {
0034         PTP_CLK_REQ_EXTTS,
0035         PTP_CLK_REQ_PEROUT,
0036         PTP_CLK_REQ_PPS,
0037     } type;
0038     union {
0039         struct ptp_extts_request extts;
0040         struct ptp_perout_request perout;
0041     };
0042 };
0043 
0044 struct system_device_crosststamp;
0045 
0046 /**
0047  * struct ptp_system_timestamp - system time corresponding to a PHC timestamp
0048  */
0049 struct ptp_system_timestamp {
0050     struct timespec64 pre_ts;
0051     struct timespec64 post_ts;
0052 };
0053 
0054 /**
0055  * struct ptp_clock_info - describes a PTP hardware clock
0056  *
0057  * @owner:     The clock driver should set to THIS_MODULE.
0058  * @name:      A short "friendly name" to identify the clock and to
0059  *             help distinguish PHY based devices from MAC based ones.
0060  *             The string is not meant to be a unique id.
0061  * @max_adj:   The maximum possible frequency adjustment, in parts per billon.
0062  * @n_alarm:   The number of programmable alarms.
0063  * @n_ext_ts:  The number of external time stamp channels.
0064  * @n_per_out: The number of programmable periodic signals.
0065  * @n_pins:    The number of programmable pins.
0066  * @pps:       Indicates whether the clock supports a PPS callback.
0067  * @pin_config: Array of length 'n_pins'. If the number of
0068  *              programmable pins is nonzero, then drivers must
0069  *              allocate and initialize this array.
0070  *
0071  * clock operations
0072  *
0073  * @adjfine:  Adjusts the frequency of the hardware clock.
0074  *            parameter scaled_ppm: Desired frequency offset from
0075  *            nominal frequency in parts per million, but with a
0076  *            16 bit binary fractional field.
0077  *
0078  * @adjfreq:  Adjusts the frequency of the hardware clock.
0079  *            This method is deprecated.  New drivers should implement
0080  *            the @adjfine method instead.
0081  *            parameter delta: Desired frequency offset from nominal frequency
0082  *            in parts per billion
0083  *
0084  * @adjphase:  Adjusts the phase offset of the hardware clock.
0085  *             parameter delta: Desired change in nanoseconds.
0086  *
0087  * @adjtime:  Shifts the time of the hardware clock.
0088  *            parameter delta: Desired change in nanoseconds.
0089  *
0090  * @gettime64:  Reads the current time from the hardware clock.
0091  *              This method is deprecated.  New drivers should implement
0092  *              the @gettimex64 method instead.
0093  *              parameter ts: Holds the result.
0094  *
0095  * @gettimex64:  Reads the current time from the hardware clock and optionally
0096  *               also the system clock.
0097  *               parameter ts: Holds the PHC timestamp.
0098  *               parameter sts: If not NULL, it holds a pair of timestamps from
0099  *               the system clock. The first reading is made right before
0100  *               reading the lowest bits of the PHC timestamp and the second
0101  *               reading immediately follows that.
0102  *
0103  * @getcrosststamp:  Reads the current time from the hardware clock and
0104  *                   system clock simultaneously.
0105  *                   parameter cts: Contains timestamp (device,system) pair,
0106  *                   where system time is realtime and monotonic.
0107  *
0108  * @settime64:  Set the current time on the hardware clock.
0109  *              parameter ts: Time value to set.
0110  *
0111  * @getcycles64:  Reads the current free running cycle counter from the hardware
0112  *                clock.
0113  *                If @getcycles64 and @getcyclesx64 are not supported, then
0114  *                @gettime64 or @gettimex64 will be used as default
0115  *                implementation.
0116  *                parameter ts: Holds the result.
0117  *
0118  * @getcyclesx64:  Reads the current free running cycle counter from the
0119  *                 hardware clock and optionally also the system clock.
0120  *                 If @getcycles64 and @getcyclesx64 are not supported, then
0121  *                 @gettimex64 will be used as default implementation if
0122  *                 available.
0123  *                 parameter ts: Holds the PHC timestamp.
0124  *                 parameter sts: If not NULL, it holds a pair of timestamps
0125  *                 from the system clock. The first reading is made right before
0126  *                 reading the lowest bits of the PHC timestamp and the second
0127  *                 reading immediately follows that.
0128  *
0129  * @getcrosscycles:  Reads the current free running cycle counter from the
0130  *                   hardware clock and system clock simultaneously.
0131  *                   If @getcycles64 and @getcyclesx64 are not supported, then
0132  *                   @getcrosststamp will be used as default implementation if
0133  *                   available.
0134  *                   parameter cts: Contains timestamp (device,system) pair,
0135  *                   where system time is realtime and monotonic.
0136  *
0137  * @enable:   Request driver to enable or disable an ancillary feature.
0138  *            parameter request: Desired resource to enable or disable.
0139  *            parameter on: Caller passes one to enable or zero to disable.
0140  *
0141  * @verify:   Confirm that a pin can perform a given function. The PTP
0142  *            Hardware Clock subsystem maintains the 'pin_config'
0143  *            array on behalf of the drivers, but the PHC subsystem
0144  *            assumes that every pin can perform every function. This
0145  *            hook gives drivers a way of telling the core about
0146  *            limitations on specific pins. This function must return
0147  *            zero if the function can be assigned to this pin, and
0148  *            nonzero otherwise.
0149  *            parameter pin: index of the pin in question.
0150  *            parameter func: the desired function to use.
0151  *            parameter chan: the function channel index to use.
0152  *
0153  * @do_aux_work:  Request driver to perform auxiliary (periodic) operations
0154  *                Driver should return delay of the next auxiliary work
0155  *                scheduling time (>=0) or negative value in case further
0156  *                scheduling is not required.
0157  *
0158  * Drivers should embed their ptp_clock_info within a private
0159  * structure, obtaining a reference to it using container_of().
0160  *
0161  * The callbacks must all return zero on success, non-zero otherwise.
0162  */
0163 
0164 struct ptp_clock_info {
0165     struct module *owner;
0166     char name[PTP_CLOCK_NAME_LEN];
0167     s32 max_adj;
0168     int n_alarm;
0169     int n_ext_ts;
0170     int n_per_out;
0171     int n_pins;
0172     int pps;
0173     struct ptp_pin_desc *pin_config;
0174     int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm);
0175     int (*adjfreq)(struct ptp_clock_info *ptp, s32 delta);
0176     int (*adjphase)(struct ptp_clock_info *ptp, s32 phase);
0177     int (*adjtime)(struct ptp_clock_info *ptp, s64 delta);
0178     int (*gettime64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
0179     int (*gettimex64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
0180               struct ptp_system_timestamp *sts);
0181     int (*getcrosststamp)(struct ptp_clock_info *ptp,
0182                   struct system_device_crosststamp *cts);
0183     int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts);
0184     int (*getcycles64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
0185     int (*getcyclesx64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
0186                 struct ptp_system_timestamp *sts);
0187     int (*getcrosscycles)(struct ptp_clock_info *ptp,
0188                   struct system_device_crosststamp *cts);
0189     int (*enable)(struct ptp_clock_info *ptp,
0190               struct ptp_clock_request *request, int on);
0191     int (*verify)(struct ptp_clock_info *ptp, unsigned int pin,
0192               enum ptp_pin_function func, unsigned int chan);
0193     long (*do_aux_work)(struct ptp_clock_info *ptp);
0194 };
0195 
0196 struct ptp_clock;
0197 
0198 enum ptp_clock_events {
0199     PTP_CLOCK_ALARM,
0200     PTP_CLOCK_EXTTS,
0201     PTP_CLOCK_PPS,
0202     PTP_CLOCK_PPSUSR,
0203 };
0204 
0205 /**
0206  * struct ptp_clock_event - decribes a PTP hardware clock event
0207  *
0208  * @type:  One of the ptp_clock_events enumeration values.
0209  * @index: Identifies the source of the event.
0210  * @timestamp: When the event occurred (%PTP_CLOCK_EXTTS only).
0211  * @pps_times: When the event occurred (%PTP_CLOCK_PPSUSR only).
0212  */
0213 
0214 struct ptp_clock_event {
0215     int type;
0216     int index;
0217     union {
0218         u64 timestamp;
0219         struct pps_event_time pps_times;
0220     };
0221 };
0222 
0223 /**
0224  * scaled_ppm_to_ppb() - convert scaled ppm to ppb
0225  *
0226  * @ppm:    Parts per million, but with a 16 bit binary fractional field
0227  */
0228 static inline long scaled_ppm_to_ppb(long ppm)
0229 {
0230     /*
0231      * The 'freq' field in the 'struct timex' is in parts per
0232      * million, but with a 16 bit binary fractional field.
0233      *
0234      * We want to calculate
0235      *
0236      *    ppb = scaled_ppm * 1000 / 2^16
0237      *
0238      * which simplifies to
0239      *
0240      *    ppb = scaled_ppm * 125 / 2^13
0241      */
0242     s64 ppb = 1 + ppm;
0243 
0244     ppb *= 125;
0245     ppb >>= 13;
0246     return (long)ppb;
0247 }
0248 
0249 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
0250 
0251 /**
0252  * ptp_clock_register() - register a PTP hardware clock driver
0253  *
0254  * @info:   Structure describing the new clock.
0255  * @parent: Pointer to the parent device of the new clock.
0256  *
0257  * Returns a valid pointer on success or PTR_ERR on failure.  If PHC
0258  * support is missing at the configuration level, this function
0259  * returns NULL, and drivers are expected to gracefully handle that
0260  * case separately.
0261  */
0262 
0263 extern struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
0264                         struct device *parent);
0265 
0266 /**
0267  * ptp_clock_unregister() - unregister a PTP hardware clock driver
0268  *
0269  * @ptp:  The clock to remove from service.
0270  */
0271 
0272 extern int ptp_clock_unregister(struct ptp_clock *ptp);
0273 
0274 /**
0275  * ptp_clock_event() - notify the PTP layer about an event
0276  *
0277  * @ptp:    The clock obtained from ptp_clock_register().
0278  * @event:  Message structure describing the event.
0279  */
0280 
0281 extern void ptp_clock_event(struct ptp_clock *ptp,
0282                 struct ptp_clock_event *event);
0283 
0284 /**
0285  * ptp_clock_index() - obtain the device index of a PTP clock
0286  *
0287  * @ptp:    The clock obtained from ptp_clock_register().
0288  */
0289 
0290 extern int ptp_clock_index(struct ptp_clock *ptp);
0291 
0292 /**
0293  * ptp_find_pin() - obtain the pin index of a given auxiliary function
0294  *
0295  * The caller must hold ptp_clock::pincfg_mux.  Drivers do not have
0296  * access to that mutex as ptp_clock is an opaque type.  However, the
0297  * core code acquires the mutex before invoking the driver's
0298  * ptp_clock_info::enable() callback, and so drivers may call this
0299  * function from that context.
0300  *
0301  * @ptp:    The clock obtained from ptp_clock_register().
0302  * @func:   One of the ptp_pin_function enumerated values.
0303  * @chan:   The particular functional channel to find.
0304  * Return:  Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
0305  *          or -1 if the auxiliary function cannot be found.
0306  */
0307 
0308 int ptp_find_pin(struct ptp_clock *ptp,
0309          enum ptp_pin_function func, unsigned int chan);
0310 
0311 /**
0312  * ptp_find_pin_unlocked() - wrapper for ptp_find_pin()
0313  *
0314  * This function acquires the ptp_clock::pincfg_mux mutex before
0315  * invoking ptp_find_pin().  Instead of using this function, drivers
0316  * should most likely call ptp_find_pin() directly from their
0317  * ptp_clock_info::enable() method.
0318  *
0319  */
0320 
0321 int ptp_find_pin_unlocked(struct ptp_clock *ptp,
0322               enum ptp_pin_function func, unsigned int chan);
0323 
0324 /**
0325  * ptp_schedule_worker() - schedule ptp auxiliary work
0326  *
0327  * @ptp:    The clock obtained from ptp_clock_register().
0328  * @delay:  number of jiffies to wait before queuing
0329  *          See kthread_queue_delayed_work() for more info.
0330  */
0331 
0332 int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay);
0333 
0334 /**
0335  * ptp_cancel_worker_sync() - cancel ptp auxiliary clock
0336  *
0337  * @ptp:     The clock obtained from ptp_clock_register().
0338  */
0339 void ptp_cancel_worker_sync(struct ptp_clock *ptp);
0340 
0341 #else
0342 static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
0343                            struct device *parent)
0344 { return NULL; }
0345 static inline int ptp_clock_unregister(struct ptp_clock *ptp)
0346 { return 0; }
0347 static inline void ptp_clock_event(struct ptp_clock *ptp,
0348                    struct ptp_clock_event *event)
0349 { }
0350 static inline int ptp_clock_index(struct ptp_clock *ptp)
0351 { return -1; }
0352 static inline int ptp_find_pin(struct ptp_clock *ptp,
0353                    enum ptp_pin_function func, unsigned int chan)
0354 { return -1; }
0355 static inline int ptp_find_pin_unlocked(struct ptp_clock *ptp,
0356                     enum ptp_pin_function func,
0357                     unsigned int chan)
0358 { return -1; }
0359 static inline int ptp_schedule_worker(struct ptp_clock *ptp,
0360                       unsigned long delay)
0361 { return -EOPNOTSUPP; }
0362 static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp)
0363 { }
0364 #endif
0365 
0366 #if IS_BUILTIN(CONFIG_PTP_1588_CLOCK)
0367 /*
0368  * These are called by the network core, and don't work if PTP is in
0369  * a loadable module.
0370  */
0371 
0372 /**
0373  * ptp_get_vclocks_index() - get all vclocks index on pclock, and
0374  *                           caller is responsible to free memory
0375  *                           of vclock_index
0376  *
0377  * @pclock_index: phc index of ptp pclock.
0378  * @vclock_index: pointer to pointer of vclock index.
0379  *
0380  * return number of vclocks.
0381  */
0382 int ptp_get_vclocks_index(int pclock_index, int **vclock_index);
0383 
0384 /**
0385  * ptp_convert_timestamp() - convert timestamp to a ptp vclock time
0386  *
0387  * @hwtstamp:     timestamp
0388  * @vclock_index: phc index of ptp vclock.
0389  *
0390  * Returns converted timestamp, or 0 on error.
0391  */
0392 ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index);
0393 #else
0394 static inline int ptp_get_vclocks_index(int pclock_index, int **vclock_index)
0395 { return 0; }
0396 static inline ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp,
0397                         int vclock_index)
0398 { return 0; }
0399 
0400 #endif
0401 
0402 static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts)
0403 {
0404     if (sts)
0405         ktime_get_real_ts64(&sts->pre_ts);
0406 }
0407 
0408 static inline void ptp_read_system_postts(struct ptp_system_timestamp *sts)
0409 {
0410     if (sts)
0411         ktime_get_real_ts64(&sts->post_ts);
0412 }
0413 
0414 #endif