ptp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Intel PRO/1000 Linux driver
  2. * Copyright(c) 1999 - 2014 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. /**
  27. * e1000e_phc_adjfreq - adjust the frequency of the hardware clock
  28. * @ptp: ptp clock structure
  29. * @delta: Desired frequency change in parts per billion
  30. *
  31. * Adjust the frequency of the PHC cycle counter by the indicated delta from
  32. * the base frequency.
  33. **/
  34. static int e1000e_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  35. {
  36. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  37. ptp_clock_info);
  38. struct e1000_hw *hw = &adapter->hw;
  39. bool neg_adj = false;
  40. unsigned long flags;
  41. u64 adjustment;
  42. u32 timinca, incvalue;
  43. s32 ret_val;
  44. if ((delta > ptp->max_adj) || (delta <= -1000000000))
  45. return -EINVAL;
  46. if (delta < 0) {
  47. neg_adj = true;
  48. delta = -delta;
  49. }
  50. /* Get the System Time Register SYSTIM base frequency */
  51. ret_val = e1000e_get_base_timinca(adapter, &timinca);
  52. if (ret_val)
  53. return ret_val;
  54. spin_lock_irqsave(&adapter->systim_lock, flags);
  55. incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
  56. adjustment = incvalue;
  57. adjustment *= delta;
  58. adjustment = div_u64(adjustment, 1000000000);
  59. incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
  60. timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
  61. timinca |= incvalue;
  62. ew32(TIMINCA, timinca);
  63. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  64. return 0;
  65. }
  66. /**
  67. * e1000e_phc_adjtime - Shift the time of the hardware clock
  68. * @ptp: ptp clock structure
  69. * @delta: Desired change in nanoseconds
  70. *
  71. * Adjust the timer by resetting the timecounter structure.
  72. **/
  73. static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  74. {
  75. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  76. ptp_clock_info);
  77. unsigned long flags;
  78. spin_lock_irqsave(&adapter->systim_lock, flags);
  79. timecounter_adjtime(&adapter->tc, delta);
  80. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  81. return 0;
  82. }
  83. /**
  84. * e1000e_phc_gettime - Reads the current time from the hardware clock
  85. * @ptp: ptp clock structure
  86. * @ts: timespec structure to hold the current time value
  87. *
  88. * Read the timecounter and return the correct value in ns after converting
  89. * it into a struct timespec.
  90. **/
  91. static int e1000e_phc_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  92. {
  93. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  94. ptp_clock_info);
  95. unsigned long flags;
  96. u64 ns;
  97. spin_lock_irqsave(&adapter->systim_lock, flags);
  98. ns = timecounter_read(&adapter->tc);
  99. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  100. *ts = ns_to_timespec64(ns);
  101. return 0;
  102. }
  103. /**
  104. * e1000e_phc_settime - Set the current time on the hardware clock
  105. * @ptp: ptp clock structure
  106. * @ts: timespec containing the new time for the cycle counter
  107. *
  108. * Reset the timecounter to use a new base value instead of the kernel
  109. * wall timer value.
  110. **/
  111. static int e1000e_phc_settime(struct ptp_clock_info *ptp,
  112. const struct timespec64 *ts)
  113. {
  114. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  115. ptp_clock_info);
  116. unsigned long flags;
  117. u64 ns;
  118. ns = timespec64_to_ns(ts);
  119. /* reset the timecounter */
  120. spin_lock_irqsave(&adapter->systim_lock, flags);
  121. timecounter_init(&adapter->tc, &adapter->cc, ns);
  122. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  123. return 0;
  124. }
  125. /**
  126. * e1000e_phc_enable - enable or disable an ancillary feature
  127. * @ptp: ptp clock structure
  128. * @request: Desired resource to enable or disable
  129. * @on: Caller passes one to enable or zero to disable
  130. *
  131. * Enable (or disable) ancillary features of the PHC subsystem.
  132. * Currently, no ancillary features are supported.
  133. **/
  134. static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
  135. struct ptp_clock_request __always_unused *request,
  136. int __always_unused on)
  137. {
  138. return -EOPNOTSUPP;
  139. }
  140. static void e1000e_systim_overflow_work(struct work_struct *work)
  141. {
  142. struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
  143. systim_overflow_work.work);
  144. struct e1000_hw *hw = &adapter->hw;
  145. struct timespec64 ts;
  146. adapter->ptp_clock_info.gettime64(&adapter->ptp_clock_info, &ts);
  147. e_dbg("SYSTIM overflow check at %lld.%09lu\n",
  148. (long long) ts.tv_sec, ts.tv_nsec);
  149. schedule_delayed_work(&adapter->systim_overflow_work,
  150. E1000_SYSTIM_OVERFLOW_PERIOD);
  151. }
  152. static const struct ptp_clock_info e1000e_ptp_clock_info = {
  153. .owner = THIS_MODULE,
  154. .n_alarm = 0,
  155. .n_ext_ts = 0,
  156. .n_per_out = 0,
  157. .n_pins = 0,
  158. .pps = 0,
  159. .adjfreq = e1000e_phc_adjfreq,
  160. .adjtime = e1000e_phc_adjtime,
  161. .gettime64 = e1000e_phc_gettime,
  162. .settime64 = e1000e_phc_settime,
  163. .enable = e1000e_phc_enable,
  164. };
  165. /**
  166. * e1000e_ptp_init - initialize PTP for devices which support it
  167. * @adapter: board private structure
  168. *
  169. * This function performs the required steps for enabling PTP support.
  170. * If PTP support has already been loaded it simply calls the cyclecounter
  171. * init routine and exits.
  172. **/
  173. void e1000e_ptp_init(struct e1000_adapter *adapter)
  174. {
  175. struct e1000_hw *hw = &adapter->hw;
  176. adapter->ptp_clock = NULL;
  177. if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
  178. return;
  179. adapter->ptp_clock_info = e1000e_ptp_clock_info;
  180. snprintf(adapter->ptp_clock_info.name,
  181. sizeof(adapter->ptp_clock_info.name), "%pm",
  182. adapter->netdev->perm_addr);
  183. switch (hw->mac.type) {
  184. case e1000_pch2lan:
  185. case e1000_pch_lpt:
  186. case e1000_pch_spt:
  187. if (((hw->mac.type != e1000_pch_lpt) &&
  188. (hw->mac.type != e1000_pch_spt)) ||
  189. (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
  190. adapter->ptp_clock_info.max_adj = 24000000 - 1;
  191. break;
  192. }
  193. /* fall-through */
  194. case e1000_82574:
  195. case e1000_82583:
  196. adapter->ptp_clock_info.max_adj = 600000000 - 1;
  197. break;
  198. default:
  199. break;
  200. }
  201. INIT_DELAYED_WORK(&adapter->systim_overflow_work,
  202. e1000e_systim_overflow_work);
  203. schedule_delayed_work(&adapter->systim_overflow_work,
  204. E1000_SYSTIM_OVERFLOW_PERIOD);
  205. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
  206. &adapter->pdev->dev);
  207. if (IS_ERR(adapter->ptp_clock)) {
  208. adapter->ptp_clock = NULL;
  209. e_err("ptp_clock_register failed\n");
  210. } else {
  211. e_info("registered PHC clock\n");
  212. }
  213. }
  214. /**
  215. * e1000e_ptp_remove - disable PTP device and stop the overflow check
  216. * @adapter: board private structure
  217. *
  218. * Stop the PTP support, and cancel the delayed work.
  219. **/
  220. void e1000e_ptp_remove(struct e1000_adapter *adapter)
  221. {
  222. if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
  223. return;
  224. cancel_delayed_work_sync(&adapter->systim_overflow_work);
  225. if (adapter->ptp_clock) {
  226. ptp_clock_unregister(adapter->ptp_clock);
  227. adapter->ptp_clock = NULL;
  228. e_info("removed PHC\n");
  229. }
  230. }