ptp_clock_kernel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * PTP 1588 clock support
  4. *
  5. * Copyright (C) 2010 OMICRON electronics GmbH
  6. */
  7. #ifndef _PTP_CLOCK_KERNEL_H_
  8. #define _PTP_CLOCK_KERNEL_H_
  9. #include <linux/device.h>
  10. #include <linux/pps_kernel.h>
  11. #include <linux/ptp_clock.h>
  12. #include <linux/timecounter.h>
  13. #include <linux/skbuff.h>
  14. #define PTP_CLOCK_NAME_LEN 32
  15. /**
  16. * struct ptp_clock_request - request PTP clock event
  17. *
  18. * @type: The type of the request.
  19. * EXTTS: Configure external trigger timestamping
  20. * PEROUT: Configure periodic output signal (e.g. PPS)
  21. * PPS: trigger internal PPS event for input
  22. * into kernel PPS subsystem
  23. * @extts: describes configuration for external trigger timestamping.
  24. * This is only valid when event == PTP_CLK_REQ_EXTTS.
  25. * @perout: describes configuration for periodic output.
  26. * This is only valid when event == PTP_CLK_REQ_PEROUT.
  27. */
  28. struct ptp_clock_request {
  29. enum {
  30. PTP_CLK_REQ_EXTTS,
  31. PTP_CLK_REQ_PEROUT,
  32. PTP_CLK_REQ_PPS,
  33. } type;
  34. union {
  35. struct ptp_extts_request extts;
  36. struct ptp_perout_request perout;
  37. };
  38. };
  39. struct system_device_crosststamp;
  40. /**
  41. * struct ptp_system_timestamp - system time corresponding to a PHC timestamp
  42. */
  43. struct ptp_system_timestamp {
  44. struct timespec64 pre_ts;
  45. struct timespec64 post_ts;
  46. };
  47. /**
  48. * struct ptp_clock_info - describes a PTP hardware clock
  49. *
  50. * @owner: The clock driver should set to THIS_MODULE.
  51. * @name: A short "friendly name" to identify the clock and to
  52. * help distinguish PHY based devices from MAC based ones.
  53. * The string is not meant to be a unique id.
  54. * @max_adj: The maximum possible frequency adjustment, in parts per billon.
  55. * @n_alarm: The number of programmable alarms.
  56. * @n_ext_ts: The number of external time stamp channels.
  57. * @n_per_out: The number of programmable periodic signals.
  58. * @n_pins: The number of programmable pins.
  59. * @pps: Indicates whether the clock supports a PPS callback.
  60. * @pin_config: Array of length 'n_pins'. If the number of
  61. * programmable pins is nonzero, then drivers must
  62. * allocate and initialize this array.
  63. *
  64. * clock operations
  65. *
  66. * @adjfine: Adjusts the frequency of the hardware clock.
  67. * parameter scaled_ppm: Desired frequency offset from
  68. * nominal frequency in parts per million, but with a
  69. * 16 bit binary fractional field.
  70. *
  71. * @adjfreq: Adjusts the frequency of the hardware clock.
  72. * This method is deprecated. New drivers should implement
  73. * the @adjfine method instead.
  74. * parameter delta: Desired frequency offset from nominal frequency
  75. * in parts per billion
  76. *
  77. * @adjphase: Adjusts the phase offset of the hardware clock.
  78. * parameter delta: Desired change in nanoseconds.
  79. *
  80. * @adjtime: Shifts the time of the hardware clock.
  81. * parameter delta: Desired change in nanoseconds.
  82. *
  83. * @gettime64: Reads the current time from the hardware clock.
  84. * This method is deprecated. New drivers should implement
  85. * the @gettimex64 method instead.
  86. * parameter ts: Holds the result.
  87. *
  88. * @gettimex64: Reads the current time from the hardware clock and optionally
  89. * also the system clock.
  90. * parameter ts: Holds the PHC timestamp.
  91. * parameter sts: If not NULL, it holds a pair of timestamps from
  92. * the system clock. The first reading is made right before
  93. * reading the lowest bits of the PHC timestamp and the second
  94. * reading immediately follows that.
  95. *
  96. * @getcrosststamp: Reads the current time from the hardware clock and
  97. * system clock simultaneously.
  98. * parameter cts: Contains timestamp (device,system) pair,
  99. * where system time is realtime and monotonic.
  100. *
  101. * @settime64: Set the current time on the hardware clock.
  102. * parameter ts: Time value to set.
  103. *
  104. * @getcycles64: Reads the current free running cycle counter from the hardware
  105. * clock.
  106. * If @getcycles64 and @getcyclesx64 are not supported, then
  107. * @gettime64 or @gettimex64 will be used as default
  108. * implementation.
  109. * parameter ts: Holds the result.
  110. *
  111. * @getcyclesx64: Reads the current free running cycle counter from the
  112. * hardware clock and optionally also the system clock.
  113. * If @getcycles64 and @getcyclesx64 are not supported, then
  114. * @gettimex64 will be used as default implementation if
  115. * available.
  116. * parameter ts: Holds the PHC timestamp.
  117. * parameter sts: If not NULL, it holds a pair of timestamps
  118. * from the system clock. The first reading is made right before
  119. * reading the lowest bits of the PHC timestamp and the second
  120. * reading immediately follows that.
  121. *
  122. * @getcrosscycles: Reads the current free running cycle counter from the
  123. * hardware clock and system clock simultaneously.
  124. * If @getcycles64 and @getcyclesx64 are not supported, then
  125. * @getcrosststamp will be used as default implementation if
  126. * available.
  127. * parameter cts: Contains timestamp (device,system) pair,
  128. * where system time is realtime and monotonic.
  129. *
  130. * @enable: Request driver to enable or disable an ancillary feature.
  131. * parameter request: Desired resource to enable or disable.
  132. * parameter on: Caller passes one to enable or zero to disable.
  133. *
  134. * @verify: Confirm that a pin can perform a given function. The PTP
  135. * Hardware Clock subsystem maintains the 'pin_config'
  136. * array on behalf of the drivers, but the PHC subsystem
  137. * assumes that every pin can perform every function. This
  138. * hook gives drivers a way of telling the core about
  139. * limitations on specific pins. This function must return
  140. * zero if the function can be assigned to this pin, and
  141. * nonzero otherwise.
  142. * parameter pin: index of the pin in question.
  143. * parameter func: the desired function to use.
  144. * parameter chan: the function channel index to use.
  145. *
  146. * @do_aux_work: Request driver to perform auxiliary (periodic) operations
  147. * Driver should return delay of the next auxiliary work
  148. * scheduling time (>=0) or negative value in case further
  149. * scheduling is not required.
  150. *
  151. * Drivers should embed their ptp_clock_info within a private
  152. * structure, obtaining a reference to it using container_of().
  153. *
  154. * The callbacks must all return zero on success, non-zero otherwise.
  155. */
  156. struct ptp_clock_info {
  157. struct module *owner;
  158. char name[PTP_CLOCK_NAME_LEN];
  159. s32 max_adj;
  160. int n_alarm;
  161. int n_ext_ts;
  162. int n_per_out;
  163. int n_pins;
  164. int pps;
  165. struct ptp_pin_desc *pin_config;
  166. int (*adjfine)(struct ptp_clock_info *ptp, long scaled_ppm);
  167. int (*adjfreq)(struct ptp_clock_info *ptp, s32 delta);
  168. int (*adjphase)(struct ptp_clock_info *ptp, s32 phase);
  169. int (*adjtime)(struct ptp_clock_info *ptp, s64 delta);
  170. int (*gettime64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
  171. int (*gettimex64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
  172. struct ptp_system_timestamp *sts);
  173. int (*getcrosststamp)(struct ptp_clock_info *ptp,
  174. struct system_device_crosststamp *cts);
  175. int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts);
  176. int (*getcycles64)(struct ptp_clock_info *ptp, struct timespec64 *ts);
  177. int (*getcyclesx64)(struct ptp_clock_info *ptp, struct timespec64 *ts,
  178. struct ptp_system_timestamp *sts);
  179. int (*getcrosscycles)(struct ptp_clock_info *ptp,
  180. struct system_device_crosststamp *cts);
  181. int (*enable)(struct ptp_clock_info *ptp,
  182. struct ptp_clock_request *request, int on);
  183. int (*verify)(struct ptp_clock_info *ptp, unsigned int pin,
  184. enum ptp_pin_function func, unsigned int chan);
  185. long (*do_aux_work)(struct ptp_clock_info *ptp);
  186. };
  187. struct ptp_clock;
  188. enum ptp_clock_events {
  189. PTP_CLOCK_ALARM,
  190. PTP_CLOCK_EXTTS,
  191. PTP_CLOCK_PPS,
  192. PTP_CLOCK_PPSUSR,
  193. };
  194. /**
  195. * struct ptp_clock_event - decribes a PTP hardware clock event
  196. *
  197. * @type: One of the ptp_clock_events enumeration values.
  198. * @index: Identifies the source of the event.
  199. * @timestamp: When the event occurred (%PTP_CLOCK_EXTTS only).
  200. * @pps_times: When the event occurred (%PTP_CLOCK_PPSUSR only).
  201. */
  202. struct ptp_clock_event {
  203. int type;
  204. int index;
  205. union {
  206. u64 timestamp;
  207. struct pps_event_time pps_times;
  208. };
  209. };
  210. /**
  211. * scaled_ppm_to_ppb() - convert scaled ppm to ppb
  212. *
  213. * @ppm: Parts per million, but with a 16 bit binary fractional field
  214. */
  215. static inline long scaled_ppm_to_ppb(long ppm)
  216. {
  217. /*
  218. * The 'freq' field in the 'struct timex' is in parts per
  219. * million, but with a 16 bit binary fractional field.
  220. *
  221. * We want to calculate
  222. *
  223. * ppb = scaled_ppm * 1000 / 2^16
  224. *
  225. * which simplifies to
  226. *
  227. * ppb = scaled_ppm * 125 / 2^13
  228. */
  229. s64 ppb = 1 + ppm;
  230. ppb *= 125;
  231. ppb >>= 13;
  232. return (long)ppb;
  233. }
  234. #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
  235. /**
  236. * ptp_clock_register() - register a PTP hardware clock driver
  237. *
  238. * @info: Structure describing the new clock.
  239. * @parent: Pointer to the parent device of the new clock.
  240. *
  241. * Returns a valid pointer on success or PTR_ERR on failure. If PHC
  242. * support is missing at the configuration level, this function
  243. * returns NULL, and drivers are expected to gracefully handle that
  244. * case separately.
  245. */
  246. extern struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
  247. struct device *parent);
  248. /**
  249. * ptp_clock_unregister() - unregister a PTP hardware clock driver
  250. *
  251. * @ptp: The clock to remove from service.
  252. */
  253. extern int ptp_clock_unregister(struct ptp_clock *ptp);
  254. /**
  255. * ptp_clock_event() - notify the PTP layer about an event
  256. *
  257. * @ptp: The clock obtained from ptp_clock_register().
  258. * @event: Message structure describing the event.
  259. */
  260. extern void ptp_clock_event(struct ptp_clock *ptp,
  261. struct ptp_clock_event *event);
  262. /**
  263. * ptp_clock_index() - obtain the device index of a PTP clock
  264. *
  265. * @ptp: The clock obtained from ptp_clock_register().
  266. */
  267. extern int ptp_clock_index(struct ptp_clock *ptp);
  268. /**
  269. * ptp_find_pin() - obtain the pin index of a given auxiliary function
  270. *
  271. * The caller must hold ptp_clock::pincfg_mux. Drivers do not have
  272. * access to that mutex as ptp_clock is an opaque type. However, the
  273. * core code acquires the mutex before invoking the driver's
  274. * ptp_clock_info::enable() callback, and so drivers may call this
  275. * function from that context.
  276. *
  277. * @ptp: The clock obtained from ptp_clock_register().
  278. * @func: One of the ptp_pin_function enumerated values.
  279. * @chan: The particular functional channel to find.
  280. * Return: Pin index in the range of zero to ptp_clock_caps.n_pins - 1,
  281. * or -1 if the auxiliary function cannot be found.
  282. */
  283. int ptp_find_pin(struct ptp_clock *ptp,
  284. enum ptp_pin_function func, unsigned int chan);
  285. /**
  286. * ptp_find_pin_unlocked() - wrapper for ptp_find_pin()
  287. *
  288. * This function acquires the ptp_clock::pincfg_mux mutex before
  289. * invoking ptp_find_pin(). Instead of using this function, drivers
  290. * should most likely call ptp_find_pin() directly from their
  291. * ptp_clock_info::enable() method.
  292. *
  293. */
  294. int ptp_find_pin_unlocked(struct ptp_clock *ptp,
  295. enum ptp_pin_function func, unsigned int chan);
  296. /**
  297. * ptp_schedule_worker() - schedule ptp auxiliary work
  298. *
  299. * @ptp: The clock obtained from ptp_clock_register().
  300. * @delay: number of jiffies to wait before queuing
  301. * See kthread_queue_delayed_work() for more info.
  302. */
  303. int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay);
  304. /**
  305. * ptp_cancel_worker_sync() - cancel ptp auxiliary clock
  306. *
  307. * @ptp: The clock obtained from ptp_clock_register().
  308. */
  309. void ptp_cancel_worker_sync(struct ptp_clock *ptp);
  310. #else
  311. static inline struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
  312. struct device *parent)
  313. { return NULL; }
  314. static inline int ptp_clock_unregister(struct ptp_clock *ptp)
  315. { return 0; }
  316. static inline void ptp_clock_event(struct ptp_clock *ptp,
  317. struct ptp_clock_event *event)
  318. { }
  319. static inline int ptp_clock_index(struct ptp_clock *ptp)
  320. { return -1; }
  321. static inline int ptp_find_pin(struct ptp_clock *ptp,
  322. enum ptp_pin_function func, unsigned int chan)
  323. { return -1; }
  324. static inline int ptp_find_pin_unlocked(struct ptp_clock *ptp,
  325. enum ptp_pin_function func,
  326. unsigned int chan)
  327. { return -1; }
  328. static inline int ptp_schedule_worker(struct ptp_clock *ptp,
  329. unsigned long delay)
  330. { return -EOPNOTSUPP; }
  331. static inline void ptp_cancel_worker_sync(struct ptp_clock *ptp)
  332. { }
  333. #endif
  334. #if IS_BUILTIN(CONFIG_PTP_1588_CLOCK)
  335. /*
  336. * These are called by the network core, and don't work if PTP is in
  337. * a loadable module.
  338. */
  339. /**
  340. * ptp_get_vclocks_index() - get all vclocks index on pclock, and
  341. * caller is responsible to free memory
  342. * of vclock_index
  343. *
  344. * @pclock_index: phc index of ptp pclock.
  345. * @vclock_index: pointer to pointer of vclock index.
  346. *
  347. * return number of vclocks.
  348. */
  349. int ptp_get_vclocks_index(int pclock_index, int **vclock_index);
  350. /**
  351. * ptp_convert_timestamp() - convert timestamp to a ptp vclock time
  352. *
  353. * @hwtstamp: timestamp
  354. * @vclock_index: phc index of ptp vclock.
  355. *
  356. * Returns converted timestamp, or 0 on error.
  357. */
  358. ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, int vclock_index);
  359. #else
  360. static inline int ptp_get_vclocks_index(int pclock_index, int **vclock_index)
  361. { return 0; }
  362. static inline ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp,
  363. int vclock_index)
  364. { return 0; }
  365. #endif
  366. static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts)
  367. {
  368. if (sts)
  369. ktime_get_real_ts64(&sts->pre_ts);
  370. }
  371. static inline void ptp_read_system_postts(struct ptp_system_timestamp *sts)
  372. {
  373. if (sts)
  374. ktime_get_real_ts64(&sts->post_ts);
  375. }
  376. #endif