ptp.c 9.1 KB

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