ptp.c 9.8 KB

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