ptp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*******************************************************************************
  2. Intel PRO/1000 Linux driver
  3. Copyright(c) 1999 - 2013 Intel Corporation.
  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. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  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. u64 adjustment;
  41. u32 timinca, incvalue;
  42. s32 ret_val;
  43. if ((delta > ptp->max_adj) || (delta <= -1000000000))
  44. return -EINVAL;
  45. if (delta < 0) {
  46. neg_adj = true;
  47. delta = -delta;
  48. }
  49. /* Get the System Time Register SYSTIM base frequency */
  50. ret_val = e1000e_get_base_timinca(adapter, &timinca);
  51. if (ret_val)
  52. return ret_val;
  53. incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK;
  54. adjustment = incvalue;
  55. adjustment *= delta;
  56. adjustment = div_u64(adjustment, 1000000000);
  57. incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment);
  58. timinca &= ~E1000_TIMINCA_INCVALUE_MASK;
  59. timinca |= incvalue;
  60. ew32(TIMINCA, timinca);
  61. return 0;
  62. }
  63. /**
  64. * e1000e_phc_adjtime - Shift the time of the hardware clock
  65. * @ptp: ptp clock structure
  66. * @delta: Desired change in nanoseconds
  67. *
  68. * Adjust the timer by resetting the timecounter structure.
  69. **/
  70. static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  71. {
  72. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  73. ptp_clock_info);
  74. unsigned long flags;
  75. s64 now;
  76. spin_lock_irqsave(&adapter->systim_lock, flags);
  77. now = timecounter_read(&adapter->tc);
  78. now += delta;
  79. timecounter_init(&adapter->tc, &adapter->cc, now);
  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 timespec *ts)
  92. {
  93. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  94. ptp_clock_info);
  95. unsigned long flags;
  96. u32 remainder;
  97. u64 ns;
  98. spin_lock_irqsave(&adapter->systim_lock, flags);
  99. ns = timecounter_read(&adapter->tc);
  100. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  101. ts->tv_sec = div_u64_rem(ns, NSEC_PER_SEC, &remainder);
  102. ts->tv_nsec = remainder;
  103. return 0;
  104. }
  105. /**
  106. * e1000e_phc_settime - Set the current time on the hardware clock
  107. * @ptp: ptp clock structure
  108. * @ts: timespec containing the new time for the cycle counter
  109. *
  110. * Reset the timecounter to use a new base value instead of the kernel
  111. * wall timer value.
  112. **/
  113. static int e1000e_phc_settime(struct ptp_clock_info *ptp,
  114. const struct timespec *ts)
  115. {
  116. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  117. ptp_clock_info);
  118. unsigned long flags;
  119. u64 ns;
  120. ns = timespec_to_ns(ts);
  121. /* reset the timecounter */
  122. spin_lock_irqsave(&adapter->systim_lock, flags);
  123. timecounter_init(&adapter->tc, &adapter->cc, ns);
  124. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  125. return 0;
  126. }
  127. /**
  128. * e1000e_phc_enable - enable or disable an ancillary feature
  129. * @ptp: ptp clock structure
  130. * @request: Desired resource to enable or disable
  131. * @on: Caller passes one to enable or zero to disable
  132. *
  133. * Enable (or disable) ancillary features of the PHC subsystem.
  134. * Currently, no ancillary features are supported.
  135. **/
  136. static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
  137. struct ptp_clock_request __always_unused *request,
  138. int __always_unused on)
  139. {
  140. return -EOPNOTSUPP;
  141. }
  142. static void e1000e_systim_overflow_work(struct work_struct *work)
  143. {
  144. struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
  145. systim_overflow_work.work);
  146. struct e1000_hw *hw = &adapter->hw;
  147. struct timespec ts;
  148. adapter->ptp_clock_info.gettime(&adapter->ptp_clock_info, &ts);
  149. e_dbg("SYSTIM overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  150. schedule_delayed_work(&adapter->systim_overflow_work,
  151. E1000_SYSTIM_OVERFLOW_PERIOD);
  152. }
  153. static const struct ptp_clock_info e1000e_ptp_clock_info = {
  154. .owner = THIS_MODULE,
  155. .n_alarm = 0,
  156. .n_ext_ts = 0,
  157. .n_per_out = 0,
  158. .pps = 0,
  159. .adjfreq = e1000e_phc_adjfreq,
  160. .adjtime = e1000e_phc_adjtime,
  161. .gettime = e1000e_phc_gettime,
  162. .settime = 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. if ((hw->mac.type != e1000_pch_lpt) ||
  187. (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
  188. adapter->ptp_clock_info.max_adj = 24000000 - 1;
  189. break;
  190. }
  191. /* fall-through */
  192. case e1000_82574:
  193. case e1000_82583:
  194. adapter->ptp_clock_info.max_adj = 600000000 - 1;
  195. break;
  196. default:
  197. break;
  198. }
  199. INIT_DELAYED_WORK(&adapter->systim_overflow_work,
  200. e1000e_systim_overflow_work);
  201. schedule_delayed_work(&adapter->systim_overflow_work,
  202. E1000_SYSTIM_OVERFLOW_PERIOD);
  203. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
  204. &adapter->pdev->dev);
  205. if (IS_ERR(adapter->ptp_clock)) {
  206. adapter->ptp_clock = NULL;
  207. e_err("ptp_clock_register failed\n");
  208. } else {
  209. e_info("registered PHC clock\n");
  210. }
  211. }
  212. /**
  213. * e1000e_ptp_remove - disable PTP device and stop the overflow check
  214. * @adapter: board private structure
  215. *
  216. * Stop the PTP support, and cancel the delayed work.
  217. **/
  218. void e1000e_ptp_remove(struct e1000_adapter *adapter)
  219. {
  220. if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
  221. return;
  222. cancel_delayed_work_sync(&adapter->systim_overflow_work);
  223. if (adapter->ptp_clock) {
  224. ptp_clock_unregister(adapter->ptp_clock);
  225. adapter->ptp_clock = NULL;
  226. e_info("removed PHC\n");
  227. }
  228. }