igb_ptp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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_SYSTIM_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_ptp_read_82576(const struct cyclecounter *cc)
  77. {
  78. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  79. struct e1000_hw *hw = &igb->hw;
  80. u64 val;
  81. u32 lo, hi;
  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_ptp_read_82580(const struct cyclecounter *cc)
  92. {
  93. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  94. struct e1000_hw *hw = &igb->hw;
  95. u64 val;
  96. u32 lo, hi, jk;
  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. * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
  111. * @adapter: board private structure
  112. * @hwtstamps: timestamp structure to update
  113. * @systim: unsigned 64bit system time value.
  114. *
  115. * We need to convert the system time value stored in the RX/TXSTMP registers
  116. * into a hwtstamp which can be used by the upper level timestamping functions.
  117. *
  118. * The 'tmreg_lock' spinlock is used to protect the consistency of the
  119. * system time value. This is needed because reading the 64 bit time
  120. * value involves reading two (or three) 32 bit registers. The first
  121. * read latches the value. Ditto for writing.
  122. *
  123. * In addition, here have extended the system time with an overflow
  124. * counter in software.
  125. **/
  126. static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
  127. struct skb_shared_hwtstamps *hwtstamps,
  128. u64 systim)
  129. {
  130. unsigned long flags;
  131. u64 ns;
  132. switch (adapter->hw.mac.type) {
  133. case e1000_i210:
  134. case e1000_i211:
  135. case e1000_i350:
  136. case e1000_82580:
  137. case e1000_82576:
  138. break;
  139. default:
  140. return;
  141. }
  142. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  143. ns = timecounter_cyc2time(&adapter->tc, systim);
  144. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  145. memset(hwtstamps, 0, sizeof(*hwtstamps));
  146. hwtstamps->hwtstamp = ns_to_ktime(ns);
  147. }
  148. /*
  149. * PTP clock operations
  150. */
  151. static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
  152. {
  153. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  154. ptp_caps);
  155. struct e1000_hw *hw = &igb->hw;
  156. int neg_adj = 0;
  157. u64 rate;
  158. u32 incvalue;
  159. if (ppb < 0) {
  160. neg_adj = 1;
  161. ppb = -ppb;
  162. }
  163. rate = ppb;
  164. rate <<= 14;
  165. rate = div_u64(rate, 1953125);
  166. incvalue = 16 << IGB_82576_TSYNC_SHIFT;
  167. if (neg_adj)
  168. incvalue -= rate;
  169. else
  170. incvalue += rate;
  171. wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
  172. return 0;
  173. }
  174. static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb)
  175. {
  176. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  177. ptp_caps);
  178. struct e1000_hw *hw = &igb->hw;
  179. int neg_adj = 0;
  180. u64 rate;
  181. u32 inca;
  182. if (ppb < 0) {
  183. neg_adj = 1;
  184. ppb = -ppb;
  185. }
  186. rate = ppb;
  187. rate <<= 26;
  188. rate = div_u64(rate, 1953125);
  189. inca = rate & INCVALUE_MASK;
  190. if (neg_adj)
  191. inca |= ISGN;
  192. wr32(E1000_TIMINCA, inca);
  193. return 0;
  194. }
  195. static int igb_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  196. {
  197. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  198. ptp_caps);
  199. unsigned long flags;
  200. s64 now;
  201. spin_lock_irqsave(&igb->tmreg_lock, flags);
  202. now = timecounter_read(&igb->tc);
  203. now += delta;
  204. timecounter_init(&igb->tc, &igb->cc, now);
  205. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  206. return 0;
  207. }
  208. static int igb_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  209. {
  210. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  211. ptp_caps);
  212. unsigned long flags;
  213. u64 ns;
  214. u32 remainder;
  215. spin_lock_irqsave(&igb->tmreg_lock, flags);
  216. ns = timecounter_read(&igb->tc);
  217. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  218. ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
  219. ts->tv_nsec = remainder;
  220. return 0;
  221. }
  222. static int igb_ptp_settime(struct ptp_clock_info *ptp,
  223. const struct timespec *ts)
  224. {
  225. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  226. ptp_caps);
  227. unsigned long flags;
  228. u64 ns;
  229. ns = ts->tv_sec * 1000000000ULL;
  230. ns += ts->tv_nsec;
  231. spin_lock_irqsave(&igb->tmreg_lock, flags);
  232. timecounter_init(&igb->tc, &igb->cc, ns);
  233. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  234. return 0;
  235. }
  236. static int igb_ptp_enable(struct ptp_clock_info *ptp,
  237. struct ptp_clock_request *rq, int on)
  238. {
  239. return -EOPNOTSUPP;
  240. }
  241. static void igb_ptp_overflow_check(struct work_struct *work)
  242. {
  243. struct igb_adapter *igb =
  244. container_of(work, struct igb_adapter, ptp_overflow_work.work);
  245. struct timespec ts;
  246. igb_ptp_gettime(&igb->ptp_caps, &ts);
  247. pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  248. schedule_delayed_work(&igb->ptp_overflow_work,
  249. IGB_SYSTIM_OVERFLOW_PERIOD);
  250. }
  251. /**
  252. * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  253. * @q_vector: pointer to q_vector containing needed info
  254. * @buffer: pointer to igb_tx_buffer structure
  255. *
  256. * If we were asked to do hardware stamping and such a time stamp is
  257. * available, then it must have been for this skb here because we only
  258. * allow only one such packet into the queue.
  259. */
  260. void igb_ptp_tx_hwtstamp(struct igb_q_vector *q_vector,
  261. struct igb_tx_buffer *buffer_info)
  262. {
  263. struct igb_adapter *adapter = q_vector->adapter;
  264. struct e1000_hw *hw = &adapter->hw;
  265. struct skb_shared_hwtstamps shhwtstamps;
  266. u64 regval;
  267. /* if skb does not support hw timestamp or TX stamp not valid exit */
  268. if (likely(!(buffer_info->tx_flags & IGB_TX_FLAGS_TSTAMP)) ||
  269. !(rd32(E1000_TSYNCTXCTL) & E1000_TSYNCTXCTL_VALID))
  270. return;
  271. regval = rd32(E1000_TXSTMPL);
  272. regval |= (u64)rd32(E1000_TXSTMPH) << 32;
  273. igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
  274. skb_tstamp_tx(buffer_info->skb, &shhwtstamps);
  275. }
  276. void igb_ptp_rx_hwtstamp(struct igb_q_vector *q_vector,
  277. union e1000_adv_rx_desc *rx_desc,
  278. struct sk_buff *skb)
  279. {
  280. struct igb_adapter *adapter = q_vector->adapter;
  281. struct e1000_hw *hw = &adapter->hw;
  282. u64 regval;
  283. if (!igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP |
  284. E1000_RXDADV_STAT_TS))
  285. return;
  286. /*
  287. * If this bit is set, then the RX registers contain the time stamp. No
  288. * other packet will be time stamped until we read these registers, so
  289. * read the registers to make them available again. Because only one
  290. * packet can be time stamped at a time, we know that the register
  291. * values must belong to this one here and therefore we don't need to
  292. * compare any of the additional attributes stored for it.
  293. *
  294. * If nothing went wrong, then it should have a shared tx_flags that we
  295. * can turn into a skb_shared_hwtstamps.
  296. */
  297. if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
  298. u32 *stamp = (u32 *)skb->data;
  299. regval = le32_to_cpu(*(stamp + 2));
  300. regval |= (u64)le32_to_cpu(*(stamp + 3)) << 32;
  301. skb_pull(skb, IGB_TS_HDR_LEN);
  302. } else {
  303. if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
  304. return;
  305. regval = rd32(E1000_RXSTMPL);
  306. regval |= (u64)rd32(E1000_RXSTMPH) << 32;
  307. }
  308. igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  309. }
  310. /**
  311. * igb_ptp_hwtstamp_ioctl - control hardware time stamping
  312. * @netdev:
  313. * @ifreq:
  314. * @cmd:
  315. *
  316. * Outgoing time stamping can be enabled and disabled. Play nice and
  317. * disable it when requested, although it shouldn't case any overhead
  318. * when no packet needs it. At most one packet in the queue may be
  319. * marked for time stamping, otherwise it would be impossible to tell
  320. * for sure to which packet the hardware time stamp belongs.
  321. *
  322. * Incoming time stamping has to be configured via the hardware
  323. * filters. Not all combinations are supported, in particular event
  324. * type has to be specified. Matching the kind of event packet is
  325. * not supported, with the exception of "all V2 events regardless of
  326. * level 2 or 4".
  327. *
  328. **/
  329. int igb_ptp_hwtstamp_ioctl(struct net_device *netdev,
  330. struct ifreq *ifr, int cmd)
  331. {
  332. struct igb_adapter *adapter = netdev_priv(netdev);
  333. struct e1000_hw *hw = &adapter->hw;
  334. struct hwtstamp_config config;
  335. u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
  336. u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  337. u32 tsync_rx_cfg = 0;
  338. bool is_l4 = false;
  339. bool is_l2 = false;
  340. u32 regval;
  341. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  342. return -EFAULT;
  343. /* reserved for future extensions */
  344. if (config.flags)
  345. return -EINVAL;
  346. switch (config.tx_type) {
  347. case HWTSTAMP_TX_OFF:
  348. tsync_tx_ctl = 0;
  349. case HWTSTAMP_TX_ON:
  350. break;
  351. default:
  352. return -ERANGE;
  353. }
  354. switch (config.rx_filter) {
  355. case HWTSTAMP_FILTER_NONE:
  356. tsync_rx_ctl = 0;
  357. break;
  358. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  359. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  360. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  361. case HWTSTAMP_FILTER_ALL:
  362. /*
  363. * register TSYNCRXCFG must be set, therefore it is not
  364. * possible to time stamp both Sync and Delay_Req messages
  365. * => fall back to time stamping all packets
  366. */
  367. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  368. config.rx_filter = HWTSTAMP_FILTER_ALL;
  369. break;
  370. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  371. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  372. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
  373. is_l4 = true;
  374. break;
  375. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  376. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  377. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
  378. is_l4 = true;
  379. break;
  380. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  381. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  382. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
  383. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_SYNC_MESSAGE;
  384. is_l2 = true;
  385. is_l4 = true;
  386. config.rx_filter = HWTSTAMP_FILTER_SOME;
  387. break;
  388. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  389. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  390. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
  391. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_DELAY_REQ_MESSAGE;
  392. is_l2 = true;
  393. is_l4 = true;
  394. config.rx_filter = HWTSTAMP_FILTER_SOME;
  395. break;
  396. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  397. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  398. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  399. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
  400. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  401. is_l2 = true;
  402. is_l4 = true;
  403. break;
  404. default:
  405. return -ERANGE;
  406. }
  407. if (hw->mac.type == e1000_82575) {
  408. if (tsync_rx_ctl | tsync_tx_ctl)
  409. return -EINVAL;
  410. return 0;
  411. }
  412. /*
  413. * Per-packet timestamping only works if all packets are
  414. * timestamped, so enable timestamping in all packets as
  415. * long as one rx filter was configured.
  416. */
  417. if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
  418. tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  419. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  420. }
  421. /* enable/disable TX */
  422. regval = rd32(E1000_TSYNCTXCTL);
  423. regval &= ~E1000_TSYNCTXCTL_ENABLED;
  424. regval |= tsync_tx_ctl;
  425. wr32(E1000_TSYNCTXCTL, regval);
  426. /* enable/disable RX */
  427. regval = rd32(E1000_TSYNCRXCTL);
  428. regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
  429. regval |= tsync_rx_ctl;
  430. wr32(E1000_TSYNCRXCTL, regval);
  431. /* define which PTP packets are time stamped */
  432. wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
  433. /* define ethertype filter for timestamped packets */
  434. if (is_l2)
  435. wr32(E1000_ETQF(3),
  436. (E1000_ETQF_FILTER_ENABLE | /* enable filter */
  437. E1000_ETQF_1588 | /* enable timestamping */
  438. ETH_P_1588)); /* 1588 eth protocol type */
  439. else
  440. wr32(E1000_ETQF(3), 0);
  441. #define PTP_PORT 319
  442. /* L4 Queue Filter[3]: filter by destination port and protocol */
  443. if (is_l4) {
  444. u32 ftqf = (IPPROTO_UDP /* UDP */
  445. | E1000_FTQF_VF_BP /* VF not compared */
  446. | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
  447. | E1000_FTQF_MASK); /* mask all inputs */
  448. ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
  449. wr32(E1000_IMIR(3), htons(PTP_PORT));
  450. wr32(E1000_IMIREXT(3),
  451. (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
  452. if (hw->mac.type == e1000_82576) {
  453. /* enable source port check */
  454. wr32(E1000_SPQF(3), htons(PTP_PORT));
  455. ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
  456. }
  457. wr32(E1000_FTQF(3), ftqf);
  458. } else {
  459. wr32(E1000_FTQF(3), E1000_FTQF_MASK);
  460. }
  461. wrfl();
  462. /* clear TX/RX time stamp registers, just to be sure */
  463. regval = rd32(E1000_TXSTMPH);
  464. regval = rd32(E1000_RXSTMPH);
  465. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  466. -EFAULT : 0;
  467. }
  468. void igb_ptp_init(struct igb_adapter *adapter)
  469. {
  470. struct e1000_hw *hw = &adapter->hw;
  471. switch (hw->mac.type) {
  472. case e1000_i210:
  473. case e1000_i211:
  474. case e1000_i350:
  475. case e1000_82580:
  476. adapter->ptp_caps.owner = THIS_MODULE;
  477. strcpy(adapter->ptp_caps.name, "igb-82580");
  478. adapter->ptp_caps.max_adj = 62499999;
  479. adapter->ptp_caps.n_ext_ts = 0;
  480. adapter->ptp_caps.pps = 0;
  481. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  482. adapter->ptp_caps.adjtime = igb_ptp_adjtime;
  483. adapter->ptp_caps.gettime = igb_ptp_gettime;
  484. adapter->ptp_caps.settime = igb_ptp_settime;
  485. adapter->ptp_caps.enable = igb_ptp_enable;
  486. adapter->cc.read = igb_ptp_read_82580;
  487. adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580);
  488. adapter->cc.mult = 1;
  489. adapter->cc.shift = 0;
  490. /* Enable the timer functions by clearing bit 31. */
  491. wr32(E1000_TSAUXC, 0x0);
  492. break;
  493. case e1000_82576:
  494. adapter->ptp_caps.owner = THIS_MODULE;
  495. strcpy(adapter->ptp_caps.name, "igb-82576");
  496. adapter->ptp_caps.max_adj = 1000000000;
  497. adapter->ptp_caps.n_ext_ts = 0;
  498. adapter->ptp_caps.pps = 0;
  499. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
  500. adapter->ptp_caps.adjtime = igb_ptp_adjtime;
  501. adapter->ptp_caps.gettime = igb_ptp_gettime;
  502. adapter->ptp_caps.settime = igb_ptp_settime;
  503. adapter->ptp_caps.enable = igb_ptp_enable;
  504. adapter->cc.read = igb_ptp_read_82576;
  505. adapter->cc.mask = CLOCKSOURCE_MASK(64);
  506. adapter->cc.mult = 1;
  507. adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
  508. /* Dial the nominal frequency. */
  509. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  510. break;
  511. default:
  512. adapter->ptp_clock = NULL;
  513. return;
  514. }
  515. wrfl();
  516. timecounter_init(&adapter->tc, &adapter->cc,
  517. ktime_to_ns(ktime_get_real()));
  518. INIT_DELAYED_WORK(&adapter->ptp_overflow_work, igb_ptp_overflow_check);
  519. spin_lock_init(&adapter->tmreg_lock);
  520. schedule_delayed_work(&adapter->ptp_overflow_work,
  521. IGB_SYSTIM_OVERFLOW_PERIOD);
  522. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps);
  523. if (IS_ERR(adapter->ptp_clock)) {
  524. adapter->ptp_clock = NULL;
  525. dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
  526. } else
  527. dev_info(&adapter->pdev->dev, "added PHC on %s\n",
  528. adapter->netdev->name);
  529. }
  530. /**
  531. * igb_ptp_stop - Disable PTP device and stop the overflow check.
  532. * @adapter: Board private structure.
  533. *
  534. * This function stops the PTP support and cancels the delayed work.
  535. **/
  536. void igb_ptp_stop(struct igb_adapter *adapter)
  537. {
  538. switch (adapter->hw.mac.type) {
  539. case e1000_i211:
  540. case e1000_i210:
  541. case e1000_i350:
  542. case e1000_82580:
  543. case e1000_82576:
  544. cancel_delayed_work_sync(&adapter->ptp_overflow_work);
  545. break;
  546. default:
  547. return;
  548. }
  549. if (adapter->ptp_clock) {
  550. ptp_clock_unregister(adapter->ptp_clock);
  551. dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
  552. adapter->netdev->name);
  553. }
  554. }