ptp.c 9.9 KB

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