ptp.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. s64 now;
  79. spin_lock_irqsave(&adapter->systim_lock, flags);
  80. now = timecounter_read(&adapter->tc);
  81. now += delta;
  82. timecounter_init(&adapter->tc, &adapter->cc, now);
  83. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  84. return 0;
  85. }
  86. /**
  87. * e1000e_phc_gettime - Reads the current time from the hardware clock
  88. * @ptp: ptp clock structure
  89. * @ts: timespec structure to hold the current time value
  90. *
  91. * Read the timecounter and return the correct value in ns after converting
  92. * it into a struct timespec.
  93. **/
  94. static int e1000e_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  95. {
  96. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  97. ptp_clock_info);
  98. unsigned long flags;
  99. u32 remainder;
  100. u64 ns;
  101. spin_lock_irqsave(&adapter->systim_lock, flags);
  102. ns = timecounter_read(&adapter->tc);
  103. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  104. ts->tv_sec = div_u64_rem(ns, NSEC_PER_SEC, &remainder);
  105. ts->tv_nsec = remainder;
  106. return 0;
  107. }
  108. /**
  109. * e1000e_phc_settime - Set the current time on the hardware clock
  110. * @ptp: ptp clock structure
  111. * @ts: timespec containing the new time for the cycle counter
  112. *
  113. * Reset the timecounter to use a new base value instead of the kernel
  114. * wall timer value.
  115. **/
  116. static int e1000e_phc_settime(struct ptp_clock_info *ptp,
  117. const struct timespec *ts)
  118. {
  119. struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter,
  120. ptp_clock_info);
  121. unsigned long flags;
  122. u64 ns;
  123. ns = timespec_to_ns(ts);
  124. /* reset the timecounter */
  125. spin_lock_irqsave(&adapter->systim_lock, flags);
  126. timecounter_init(&adapter->tc, &adapter->cc, ns);
  127. spin_unlock_irqrestore(&adapter->systim_lock, flags);
  128. return 0;
  129. }
  130. /**
  131. * e1000e_phc_enable - enable or disable an ancillary feature
  132. * @ptp: ptp clock structure
  133. * @request: Desired resource to enable or disable
  134. * @on: Caller passes one to enable or zero to disable
  135. *
  136. * Enable (or disable) ancillary features of the PHC subsystem.
  137. * Currently, no ancillary features are supported.
  138. **/
  139. static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp,
  140. struct ptp_clock_request __always_unused *request,
  141. int __always_unused on)
  142. {
  143. return -EOPNOTSUPP;
  144. }
  145. static void e1000e_systim_overflow_work(struct work_struct *work)
  146. {
  147. struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
  148. systim_overflow_work.work);
  149. struct e1000_hw *hw = &adapter->hw;
  150. struct timespec ts;
  151. adapter->ptp_clock_info.gettime(&adapter->ptp_clock_info, &ts);
  152. e_dbg("SYSTIM overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  153. schedule_delayed_work(&adapter->systim_overflow_work,
  154. E1000_SYSTIM_OVERFLOW_PERIOD);
  155. }
  156. static const struct ptp_clock_info e1000e_ptp_clock_info = {
  157. .owner = THIS_MODULE,
  158. .n_alarm = 0,
  159. .n_ext_ts = 0,
  160. .n_per_out = 0,
  161. .n_pins = 0,
  162. .pps = 0,
  163. .adjfreq = e1000e_phc_adjfreq,
  164. .adjtime = e1000e_phc_adjtime,
  165. .gettime = e1000e_phc_gettime,
  166. .settime = e1000e_phc_settime,
  167. .enable = e1000e_phc_enable,
  168. };
  169. /**
  170. * e1000e_ptp_init - initialize PTP for devices which support it
  171. * @adapter: board private structure
  172. *
  173. * This function performs the required steps for enabling PTP support.
  174. * If PTP support has already been loaded it simply calls the cyclecounter
  175. * init routine and exits.
  176. **/
  177. void e1000e_ptp_init(struct e1000_adapter *adapter)
  178. {
  179. struct e1000_hw *hw = &adapter->hw;
  180. adapter->ptp_clock = NULL;
  181. if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
  182. return;
  183. adapter->ptp_clock_info = e1000e_ptp_clock_info;
  184. snprintf(adapter->ptp_clock_info.name,
  185. sizeof(adapter->ptp_clock_info.name), "%pm",
  186. adapter->netdev->perm_addr);
  187. switch (hw->mac.type) {
  188. case e1000_pch2lan:
  189. case e1000_pch_lpt:
  190. if ((hw->mac.type != e1000_pch_lpt) ||
  191. (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) {
  192. adapter->ptp_clock_info.max_adj = 24000000 - 1;
  193. break;
  194. }
  195. /* fall-through */
  196. case e1000_82574:
  197. case e1000_82583:
  198. adapter->ptp_clock_info.max_adj = 600000000 - 1;
  199. break;
  200. default:
  201. break;
  202. }
  203. INIT_DELAYED_WORK(&adapter->systim_overflow_work,
  204. e1000e_systim_overflow_work);
  205. schedule_delayed_work(&adapter->systim_overflow_work,
  206. E1000_SYSTIM_OVERFLOW_PERIOD);
  207. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
  208. &adapter->pdev->dev);
  209. if (IS_ERR(adapter->ptp_clock)) {
  210. adapter->ptp_clock = NULL;
  211. e_err("ptp_clock_register failed\n");
  212. } else {
  213. e_info("registered PHC clock\n");
  214. }
  215. }
  216. /**
  217. * e1000e_ptp_remove - disable PTP device and stop the overflow check
  218. * @adapter: board private structure
  219. *
  220. * Stop the PTP support, and cancel the delayed work.
  221. **/
  222. void e1000e_ptp_remove(struct e1000_adapter *adapter)
  223. {
  224. if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
  225. return;
  226. cancel_delayed_work_sync(&adapter->systim_overflow_work);
  227. if (adapter->ptp_clock) {
  228. ptp_clock_unregister(adapter->ptp_clock);
  229. adapter->ptp_clock = NULL;
  230. e_info("removed PHC\n");
  231. }
  232. }