ptp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. /* PTP 1588 Hardware Clock (PHC)
  4. * Derived from PTP Hardware Clock driver for Intel 82576 and 82580 (igb)
  5. * Copyright (C) 2011 Richard Cochran <[email protected]>
  6. */
  7. #include "e1000.h"
  8. #ifdef CONFIG_E1000E_HWTS
  9. #include <linux/clocksource.h>
  10. #include <linux/ktime.h>
  11. #include <asm/tsc.h>
  12. #endif
  13. /**
  14. * e1000e_phc_adjfine - adjust the frequency of the hardware clock
  15. * @ptp: ptp clock structure
  16. * @delta: Desired frequency chance in scaled parts per million
  17. *
  18. * Adjust the frequency of the PHC cycle counter by the indicated delta from
  19. * the base frequency.
  20. *
  21. * Scaled parts per million is ppm but with a 16 bit binary fractional field.
  22. **/
  23. static int e1000e_phc_adjfine(struct ptp_clock_info *ptp, long delta)
  24. {
  25. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  26. ptp_clock_info);
  27. struct e1000_hw *hw = &adapter->hw;
  28. bool neg_adj = false;
  29. unsigned long flags;
  30. u64 adjustment;
  31. u32 timinca, incvalue;
  32. s32 ret_val;
  33. if (delta < 0) {
  34. neg_adj = true;
  35. delta = -delta;
  36. }
  37. /* Get the System Time Register SYSTIM base frequency */
  38. ret_val = e1000e_get_base_timinca(adapter, &timinca);
  39. if (ret_val)
  40. return ret_val;
  41. spin_lock_irqsave(&adapter->systim_lock, flags);
  42. incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
  43. adjustment = mul_u64_u64_div_u64(incvalue, (u64)delta,
  44. 1000000ULL << 16);
  45. incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
  46. timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
  47. timinca |= incvalue;
  48. ew32(TIMINCA, timinca);
  49. adapter->ptp_delta = delta;
  50. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  51. return 0;
  52. }
  53. /**
  54. * e1000e_phc_adjtime - Shift the time of the hardware clock
  55. * @ptp: ptp clock structure
  56. * @delta: Desired change in nanoseconds
  57. *
  58. * Adjust the timer by resetting the timecounter structure.
  59. **/
  60. static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  61. {
  62. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  63. ptp_clock_info);
  64. unsigned long flags;
  65. spin_lock_irqsave(&adapter->systim_lock, flags);
  66. timecounter_adjtime(&adapter->tc, delta);
  67. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  68. return 0;
  69. }
  70. #ifdef CONFIG_E1000E_HWTS
  71. #define MAX_HW_WAIT_COUNT (3)
  72. /**
  73. * e1000e_phc_get_syncdevicetime - Callback given to timekeeping code reads system/device registers
  74. * @device: current device time
  75. * @system: system counter value read synchronously with device time
  76. * @ctx: context provided by timekeeping code
  77. *
  78. * Read device and system (ART) clock simultaneously and return the corrected
  79. * clock values in ns.
  80. **/
  81. static int e1000e_phc_get_syncdevicetime(ktime_t *device,
  82. struct system_counterval_t *system,
  83. void *ctx)
  84. {
  85. struct e1000_adapter *adapter = (struct e1000_adapter *)ctx;
  86. struct e1000_hw *hw = &adapter->hw;
  87. unsigned long flags;
  88. int i;
  89. u32 tsync_ctrl;
  90. u64 dev_cycles;
  91. u64 sys_cycles;
  92. tsync_ctrl = er32(TSYNCTXCTL);
  93. tsync_ctrl |= E1000_TSYNCTXCTL_START_SYNC |
  94. E1000_TSYNCTXCTL_MAX_ALLOWED_DLY_MASK;
  95. ew32(TSYNCTXCTL, tsync_ctrl);
  96. for (i = 0; i < MAX_HW_WAIT_COUNT; ++i) {
  97. udelay(1);
  98. tsync_ctrl = er32(TSYNCTXCTL);
  99. if (tsync_ctrl & E1000_TSYNCTXCTL_SYNC_COMP)
  100. break;
  101. }
  102. if (i == MAX_HW_WAIT_COUNT)
  103. return -ETIMEDOUT;
  104. dev_cycles = er32(SYSSTMPH);
  105. dev_cycles <<= 32;
  106. dev_cycles |= er32(SYSSTMPL);
  107. spin_lock_irqsave(&adapter->systim_lock, flags);
  108. *device = ns_to_ktime(timecounter_cyc2time(&adapter->tc, dev_cycles));
  109. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  110. sys_cycles = er32(PLTSTMPH);
  111. sys_cycles <<= 32;
  112. sys_cycles |= er32(PLTSTMPL);
  113. *system = convert_art_to_tsc(sys_cycles);
  114. return 0;
  115. }
  116. /**
  117. * e1000e_phc_getcrosststamp - Reads the current system/device cross timestamp
  118. * @ptp: ptp clock structure
  119. * @xtstamp: structure containing timestamp
  120. *
  121. * Read device and system (ART) clock simultaneously and return the scaled
  122. * clock values in ns.
  123. **/
  124. static int e1000e_phc_getcrosststamp(struct ptp_clock_info *ptp,
  125. struct system_device_crosststamp *xtstamp)
  126. {
  127. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  128. ptp_clock_info);
  129. return get_device_system_crosststamp(e1000e_phc_get_syncdevicetime,
  130. adapter, NULL, xtstamp);
  131. }
  132. #endif/*CONFIG_E1000E_HWTS*/
  133. /**
  134. * e1000e_phc_gettimex - Reads the current time from the hardware clock and
  135. * system clock
  136. * @ptp: ptp clock structure
  137. * @ts: timespec structure to hold the current PHC time
  138. * @sts: structure to hold the current system time
  139. *
  140. * Read the timecounter and return the correct value in ns after converting
  141. * it into a struct timespec.
  142. **/
  143. static int e1000e_phc_gettimex(struct ptp_clock_info *ptp,
  144. struct timespec64 *ts,
  145. struct ptp_system_timestamp *sts)
  146. {
  147. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  148. ptp_clock_info);
  149. unsigned long flags;
  150. u64 cycles, ns;
  151. spin_lock_irqsave(&adapter->systim_lock, flags);
  152. /* NOTE: Non-monotonic SYSTIM readings may be returned */
  153. cycles = e1000e_read_systim(adapter, sts);
  154. ns = timecounter_cyc2time(&adapter->tc, cycles);
  155. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  156. *ts = ns_to_timespec64(ns);
  157. return 0;
  158. }
  159. /**
  160. * e1000e_phc_settime - Set the current time on the hardware clock
  161. * @ptp: ptp clock structure
  162. * @ts: timespec containing the new time for the cycle counter
  163. *
  164. * Reset the timecounter to use a new base value instead of the kernel
  165. * wall timer value.
  166. **/
  167. static int e1000e_phc_settime(struct ptp_clock_info *ptp,
  168. const struct timespec64 *ts)
  169. {
  170. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  171. ptp_clock_info);
  172. unsigned long flags;
  173. u64 ns;
  174. ns = timespec64_to_ns(ts);
  175. /* reset the timecounter */
  176. spin_lock_irqsave(&adapter->systim_lock, flags);
  177. timecounter_init(&adapter->tc, &adapter->cc, ns);
  178. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  179. return 0;
  180. }
  181. /**
  182. * e1000e_phc_enable - enable or disable an ancillary feature
  183. * @ptp: ptp clock structure
  184. * @request: Desired resource to enable or disable
  185. * @on: Caller passes one to enable or zero to disable
  186. *
  187. * Enable (or disable) ancillary features of the PHC subsystem.
  188. * Currently, no ancillary features are supported.
  189. **/
  190. static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
  191. struct ptp_clock_request __always_unused *request,
  192. int __always_unused on)
  193. {
  194. return -EOPNOTSUPP;
  195. }
  196. static void e1000e_systim_overflow_work(struct work_struct *work)
  197. {
  198. struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
  199. systim_overflow_work.work);
  200. struct e1000_hw *hw = &adapter->hw;
  201. struct timespec64 ts;
  202. u64 ns;
  203. /* Update the timecounter */
  204. ns = timecounter_read(&adapter->tc);
  205. ts = ns_to_timespec64(ns);
  206. e_dbg("SYSTIM overflow check at %lld.%09lu\n",
  207. (long long) ts.tv_sec, ts.tv_nsec);
  208. schedule_delayed_work(&adapter->systim_overflow_work,
  209. E1000_SYSTIM_OVERFLOW_PERIOD);
  210. }
  211. static const struct ptp_clock_info e1000e_ptp_clock_info = {
  212. .owner = THIS_MODULE,
  213. .n_alarm = 0,
  214. .n_ext_ts = 0,
  215. .n_per_out = 0,
  216. .n_pins = 0,
  217. .pps = 0,
  218. .adjfine = e1000e_phc_adjfine,
  219. .adjtime = e1000e_phc_adjtime,
  220. .gettimex64 = e1000e_phc_gettimex,
  221. .settime64 = e1000e_phc_settime,
  222. .enable = e1000e_phc_enable,
  223. };
  224. /**
  225. * e1000e_ptp_init - initialize PTP for devices which support it
  226. * @adapter: board private structure
  227. *
  228. * This function performs the required steps for enabling PTP support.
  229. * If PTP support has already been loaded it simply calls the cyclecounter
  230. * init routine and exits.
  231. **/
  232. void e1000e_ptp_init(struct e1000_adapter *adapter)
  233. {
  234. struct e1000_hw *hw = &adapter->hw;
  235. adapter->ptp_clock = NULL;
  236. if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
  237. return;
  238. adapter->ptp_clock_info = e1000e_ptp_clock_info;
  239. snprintf(adapter->ptp_clock_info.name,
  240. sizeof(adapter->ptp_clock_info.name), "%pm",
  241. adapter->netdev->perm_addr);
  242. switch (hw->mac.type) {
  243. case e1000_pch2lan:
  244. case e1000_pch_lpt:
  245. case e1000_pch_spt:
  246. case e1000_pch_cnp:
  247. case e1000_pch_tgp:
  248. case e1000_pch_adp:
  249. case e1000_pch_mtp:
  250. case e1000_pch_lnp:
  251. if ((hw->mac.type < e1000_pch_lpt) ||
  252. (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
  253. adapter->ptp_clock_info.max_adj = 24000000 - 1;
  254. break;
  255. }
  256. fallthrough;
  257. case e1000_82574:
  258. case e1000_82583:
  259. adapter->ptp_clock_info.max_adj = 600000000 - 1;
  260. break;
  261. default:
  262. break;
  263. }
  264. #ifdef CONFIG_E1000E_HWTS
  265. /* CPU must have ART and GBe must be from Sunrise Point or greater */
  266. if (hw->mac.type >= e1000_pch_spt && boot_cpu_has(X86_FEATURE_ART))
  267. adapter->ptp_clock_info.getcrosststamp =
  268. e1000e_phc_getcrosststamp;
  269. #endif/*CONFIG_E1000E_HWTS*/
  270. INIT_DELAYED_WORK(&adapter->systim_overflow_work,
  271. e1000e_systim_overflow_work);
  272. schedule_delayed_work(&adapter->systim_overflow_work,
  273. E1000_SYSTIM_OVERFLOW_PERIOD);
  274. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
  275. &adapter->pdev->dev);
  276. if (IS_ERR(adapter->ptp_clock)) {
  277. adapter->ptp_clock = NULL;
  278. e_err("ptp_clock_register failed\n");
  279. } else if (adapter->ptp_clock) {
  280. e_info("registered PHC clock\n");
  281. }
  282. }
  283. /**
  284. * e1000e_ptp_remove - disable PTP device and stop the overflow check
  285. * @adapter: board private structure
  286. *
  287. * Stop the PTP support, and cancel the delayed work.
  288. **/
  289. void e1000e_ptp_remove(struct e1000_adapter *adapter)
  290. {
  291. if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
  292. return;
  293. cancel_delayed_work_sync(&adapter->systim_overflow_work);
  294. if (adapter->ptp_clock) {
  295. ptp_clock_unregister(adapter->ptp_clock);
  296. adapter->ptp_clock = NULL;
  297. e_info("removed PHC\n");
  298. }
  299. }