ptp.c 9.9 KB

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