igb_ptp.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580
  3. *
  4. * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/pci.h>
  23. #include "igb.h"
  24. #define INCVALUE_MASK 0x7fffffff
  25. #define ISGN 0x80000000
  26. /*
  27. * The 82580 timesync updates the system timer every 8ns by 8ns,
  28. * and this update value cannot be reprogrammed.
  29. *
  30. * Neither the 82576 nor the 82580 offer registers wide enough to hold
  31. * nanoseconds time values for very long. For the 82580, SYSTIM always
  32. * counts nanoseconds, but the upper 24 bits are not availible. The
  33. * frequency is adjusted by changing the 32 bit fractional nanoseconds
  34. * register, TIMINCA.
  35. *
  36. * For the 82576, the SYSTIM register time unit is affect by the
  37. * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
  38. * field are needed to provide the nominal 16 nanosecond period,
  39. * leaving 19 bits for fractional nanoseconds.
  40. *
  41. * We scale the NIC clock cycle by a large factor so that relatively
  42. * small clock corrections can be added or subtracted at each clock
  43. * tick. The drawbacks of a large factor are a) that the clock
  44. * register overflows more quickly (not such a big deal) and b) that
  45. * the increment per tick has to fit into 24 bits. As a result we
  46. * need to use a shift of 19 so we can fit a value of 16 into the
  47. * TIMINCA register.
  48. *
  49. *
  50. * SYSTIMH SYSTIML
  51. * +--------------+ +---+---+------+
  52. * 82576 | 32 | | 8 | 5 | 19 |
  53. * +--------------+ +---+---+------+
  54. * \________ 45 bits _______/ fract
  55. *
  56. * +----------+---+ +--------------+
  57. * 82580 | 24 | 8 | | 32 |
  58. * +----------+---+ +--------------+
  59. * reserved \______ 40 bits _____/
  60. *
  61. *
  62. * The 45 bit 82576 SYSTIM overflows every
  63. * 2^45 * 10^-9 / 3600 = 9.77 hours.
  64. *
  65. * The 40 bit 82580 SYSTIM overflows every
  66. * 2^40 * 10^-9 / 60 = 18.3 minutes.
  67. */
  68. #define IGB_OVERFLOW_PERIOD (HZ * 60 * 9)
  69. #define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT)
  70. #define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
  71. #define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT)
  72. #define IGB_NBITS_82580 40
  73. /*
  74. * SYSTIM read access for the 82576
  75. */
  76. static cycle_t igb_82576_systim_read(const struct cyclecounter *cc)
  77. {
  78. u64 val;
  79. u32 lo, hi;
  80. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  81. struct e1000_hw *hw = &igb->hw;
  82. lo = rd32(E1000_SYSTIML);
  83. hi = rd32(E1000_SYSTIMH);
  84. val = ((u64) hi) << 32;
  85. val |= lo;
  86. return val;
  87. }
  88. /*
  89. * SYSTIM read access for the 82580
  90. */
  91. static cycle_t igb_82580_systim_read(const struct cyclecounter *cc)
  92. {
  93. u64 val;
  94. u32 lo, hi, jk;
  95. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  96. struct e1000_hw *hw = &igb->hw;
  97. /*
  98. * The timestamp latches on lowest register read. For the 82580
  99. * the lowest register is SYSTIMR instead of SYSTIML. However we only
  100. * need to provide nanosecond resolution, so we just ignore it.
  101. */
  102. jk = rd32(E1000_SYSTIMR);
  103. lo = rd32(E1000_SYSTIML);
  104. hi = rd32(E1000_SYSTIMH);
  105. val = ((u64) hi) << 32;
  106. val |= lo;
  107. return val;
  108. }
  109. /*
  110. * PTP clock operations
  111. */
  112. static int ptp_82576_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  113. {
  114. u64 rate;
  115. u32 incvalue;
  116. int neg_adj = 0;
  117. struct igb_adapter *igb = container_of(ptp, struct igb_adapter, caps);
  118. struct e1000_hw *hw = &igb->hw;
  119. if (ppb < 0) {
  120. neg_adj = 1;
  121. ppb = -ppb;
  122. }
  123. rate = ppb;
  124. rate <<= 14;
  125. rate = div_u64(rate, 1953125);
  126. incvalue = 16 << IGB_82576_TSYNC_SHIFT;
  127. if (neg_adj)
  128. incvalue -= rate;
  129. else
  130. incvalue += rate;
  131. wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
  132. return 0;
  133. }
  134. static int ptp_82580_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  135. {
  136. u64 rate;
  137. u32 inca;
  138. int neg_adj = 0;
  139. struct igb_adapter *igb = container_of(ptp, struct igb_adapter, caps);
  140. struct e1000_hw *hw = &igb->hw;
  141. if (ppb < 0) {
  142. neg_adj = 1;
  143. ppb = -ppb;
  144. }
  145. rate = ppb;
  146. rate <<= 26;
  147. rate = div_u64(rate, 1953125);
  148. inca = rate & INCVALUE_MASK;
  149. if (neg_adj)
  150. inca |= ISGN;
  151. wr32(E1000_TIMINCA, inca);
  152. return 0;
  153. }
  154. static int igb_adjtime(struct ptp_clock_info *ptp, s64 delta)
  155. {
  156. s64 now;
  157. unsigned long flags;
  158. struct igb_adapter *igb = container_of(ptp, struct igb_adapter, caps);
  159. spin_lock_irqsave(&igb->tmreg_lock, flags);
  160. now = timecounter_read(&igb->tc);
  161. now += delta;
  162. timecounter_init(&igb->tc, &igb->cc, now);
  163. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  164. return 0;
  165. }
  166. static int igb_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  167. {
  168. u64 ns;
  169. u32 remainder;
  170. unsigned long flags;
  171. struct igb_adapter *igb = container_of(ptp, struct igb_adapter, caps);
  172. spin_lock_irqsave(&igb->tmreg_lock, flags);
  173. ns = timecounter_read(&igb->tc);
  174. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  175. ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
  176. ts->tv_nsec = remainder;
  177. return 0;
  178. }
  179. static int igb_settime(struct ptp_clock_info *ptp, const struct timespec *ts)
  180. {
  181. u64 ns;
  182. unsigned long flags;
  183. struct igb_adapter *igb = container_of(ptp, struct igb_adapter, caps);
  184. ns = ts->tv_sec * 1000000000ULL;
  185. ns += ts->tv_nsec;
  186. spin_lock_irqsave(&igb->tmreg_lock, flags);
  187. timecounter_init(&igb->tc, &igb->cc, ns);
  188. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  189. return 0;
  190. }
  191. static int ptp_82576_enable(struct ptp_clock_info *ptp,
  192. struct ptp_clock_request *rq, int on)
  193. {
  194. return -EOPNOTSUPP;
  195. }
  196. static int ptp_82580_enable(struct ptp_clock_info *ptp,
  197. struct ptp_clock_request *rq, int on)
  198. {
  199. return -EOPNOTSUPP;
  200. }
  201. static void igb_overflow_check(struct work_struct *work)
  202. {
  203. struct timespec ts;
  204. struct igb_adapter *igb =
  205. container_of(work, struct igb_adapter, overflow_work.work);
  206. igb_gettime(&igb->caps, &ts);
  207. pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  208. schedule_delayed_work(&igb->overflow_work, IGB_OVERFLOW_PERIOD);
  209. }
  210. void igb_ptp_init(struct igb_adapter *adapter)
  211. {
  212. struct e1000_hw *hw = &adapter->hw;
  213. switch (hw->mac.type) {
  214. case e1000_i350:
  215. case e1000_82580:
  216. adapter->caps.owner = THIS_MODULE;
  217. strcpy(adapter->caps.name, "igb-82580");
  218. adapter->caps.max_adj = 62499999;
  219. adapter->caps.n_ext_ts = 0;
  220. adapter->caps.pps = 0;
  221. adapter->caps.adjfreq = ptp_82580_adjfreq;
  222. adapter->caps.adjtime = igb_adjtime;
  223. adapter->caps.gettime = igb_gettime;
  224. adapter->caps.settime = igb_settime;
  225. adapter->caps.enable = ptp_82580_enable;
  226. adapter->cc.read = igb_82580_systim_read;
  227. adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580);
  228. adapter->cc.mult = 1;
  229. adapter->cc.shift = 0;
  230. /* Enable the timer functions by clearing bit 31. */
  231. wr32(E1000_TSAUXC, 0x0);
  232. break;
  233. case e1000_82576:
  234. adapter->caps.owner = THIS_MODULE;
  235. strcpy(adapter->caps.name, "igb-82576");
  236. adapter->caps.max_adj = 1000000000;
  237. adapter->caps.n_ext_ts = 0;
  238. adapter->caps.pps = 0;
  239. adapter->caps.adjfreq = ptp_82576_adjfreq;
  240. adapter->caps.adjtime = igb_adjtime;
  241. adapter->caps.gettime = igb_gettime;
  242. adapter->caps.settime = igb_settime;
  243. adapter->caps.enable = ptp_82576_enable;
  244. adapter->cc.read = igb_82576_systim_read;
  245. adapter->cc.mask = CLOCKSOURCE_MASK(64);
  246. adapter->cc.mult = 1;
  247. adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
  248. /* Dial the nominal frequency. */
  249. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  250. break;
  251. default:
  252. adapter->ptp_clock = NULL;
  253. return;
  254. }
  255. wrfl();
  256. timecounter_init(&adapter->tc, &adapter->cc,
  257. ktime_to_ns(ktime_get_real()));
  258. INIT_DELAYED_WORK(&adapter->overflow_work, igb_overflow_check);
  259. spin_lock_init(&adapter->tmreg_lock);
  260. schedule_delayed_work(&adapter->overflow_work, IGB_OVERFLOW_PERIOD);
  261. adapter->ptp_clock = ptp_clock_register(&adapter->caps);
  262. if (IS_ERR(adapter->ptp_clock)) {
  263. adapter->ptp_clock = NULL;
  264. dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
  265. } else
  266. dev_info(&adapter->pdev->dev, "added PHC on %s\n",
  267. adapter->netdev->name);
  268. }
  269. void igb_ptp_remove(struct igb_adapter *adapter)
  270. {
  271. cancel_delayed_work_sync(&adapter->overflow_work);
  272. if (adapter->ptp_clock) {
  273. ptp_clock_unregister(adapter->ptp_clock);
  274. dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
  275. adapter->netdev->name);
  276. }
  277. }
  278. /**
  279. * igb_systim_to_hwtstamp - convert system time value to hw timestamp
  280. * @adapter: board private structure
  281. * @hwtstamps: timestamp structure to update
  282. * @systim: unsigned 64bit system time value.
  283. *
  284. * We need to convert the system time value stored in the RX/TXSTMP registers
  285. * into a hwtstamp which can be used by the upper level timestamping functions.
  286. *
  287. * The 'tmreg_lock' spinlock is used to protect the consistency of the
  288. * system time value. This is needed because reading the 64 bit time
  289. * value involves reading two (or three) 32 bit registers. The first
  290. * read latches the value. Ditto for writing.
  291. *
  292. * In addition, here have extended the system time with an overflow
  293. * counter in software.
  294. **/
  295. void igb_systim_to_hwtstamp(struct igb_adapter *adapter,
  296. struct skb_shared_hwtstamps *hwtstamps,
  297. u64 systim)
  298. {
  299. u64 ns;
  300. unsigned long flags;
  301. switch (adapter->hw.mac.type) {
  302. case e1000_i350:
  303. case e1000_82580:
  304. case e1000_82576:
  305. break;
  306. default:
  307. return;
  308. }
  309. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  310. ns = timecounter_cyc2time(&adapter->tc, systim);
  311. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  312. memset(hwtstamps, 0, sizeof(*hwtstamps));
  313. hwtstamps->hwtstamp = ns_to_ktime(ns);
  314. }