igb_ptp.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /* PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580
  2. *
  3. * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/pci.h>
  21. #include <linux/ptp_classify.h>
  22. #include "igb.h"
  23. #define INCVALUE_MASK 0x7fffffff
  24. #define ISGN 0x80000000
  25. /* The 82580 timesync updates the system timer every 8ns by 8ns,
  26. * and this update value cannot be reprogrammed.
  27. *
  28. * Neither the 82576 nor the 82580 offer registers wide enough to hold
  29. * nanoseconds time values for very long. For the 82580, SYSTIM always
  30. * counts nanoseconds, but the upper 24 bits are not available. The
  31. * frequency is adjusted by changing the 32 bit fractional nanoseconds
  32. * register, TIMINCA.
  33. *
  34. * For the 82576, the SYSTIM register time unit is affect by the
  35. * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
  36. * field are needed to provide the nominal 16 nanosecond period,
  37. * leaving 19 bits for fractional nanoseconds.
  38. *
  39. * We scale the NIC clock cycle by a large factor so that relatively
  40. * small clock corrections can be added or subtracted at each clock
  41. * tick. The drawbacks of a large factor are a) that the clock
  42. * register overflows more quickly (not such a big deal) and b) that
  43. * the increment per tick has to fit into 24 bits. As a result we
  44. * need to use a shift of 19 so we can fit a value of 16 into the
  45. * TIMINCA register.
  46. *
  47. *
  48. * SYSTIMH SYSTIML
  49. * +--------------+ +---+---+------+
  50. * 82576 | 32 | | 8 | 5 | 19 |
  51. * +--------------+ +---+---+------+
  52. * \________ 45 bits _______/ fract
  53. *
  54. * +----------+---+ +--------------+
  55. * 82580 | 24 | 8 | | 32 |
  56. * +----------+---+ +--------------+
  57. * reserved \______ 40 bits _____/
  58. *
  59. *
  60. * The 45 bit 82576 SYSTIM overflows every
  61. * 2^45 * 10^-9 / 3600 = 9.77 hours.
  62. *
  63. * The 40 bit 82580 SYSTIM overflows every
  64. * 2^40 * 10^-9 / 60 = 18.3 minutes.
  65. */
  66. #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9)
  67. #define IGB_PTP_TX_TIMEOUT (HZ * 15)
  68. #define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT)
  69. #define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
  70. #define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT)
  71. #define IGB_NBITS_82580 40
  72. static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter);
  73. /* SYSTIM read access for the 82576 */
  74. static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc)
  75. {
  76. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  77. struct e1000_hw *hw = &igb->hw;
  78. u64 val;
  79. u32 lo, hi;
  80. lo = rd32(E1000_SYSTIML);
  81. hi = rd32(E1000_SYSTIMH);
  82. val = ((u64) hi) << 32;
  83. val |= lo;
  84. return val;
  85. }
  86. /* SYSTIM read access for the 82580 */
  87. static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc)
  88. {
  89. struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
  90. struct e1000_hw *hw = &igb->hw;
  91. u32 lo, hi;
  92. u64 val;
  93. /* The timestamp latches on lowest register read. For the 82580
  94. * the lowest register is SYSTIMR instead of SYSTIML. However we only
  95. * need to provide nanosecond resolution, so we just ignore it.
  96. */
  97. rd32(E1000_SYSTIMR);
  98. lo = rd32(E1000_SYSTIML);
  99. hi = rd32(E1000_SYSTIMH);
  100. val = ((u64) hi) << 32;
  101. val |= lo;
  102. return val;
  103. }
  104. /* SYSTIM read access for I210/I211 */
  105. static void igb_ptp_read_i210(struct igb_adapter *adapter,
  106. struct timespec64 *ts)
  107. {
  108. struct e1000_hw *hw = &adapter->hw;
  109. u32 sec, nsec;
  110. /* The timestamp latches on lowest register read. For I210/I211, the
  111. * lowest register is SYSTIMR. Since we only need to provide nanosecond
  112. * resolution, we can ignore it.
  113. */
  114. rd32(E1000_SYSTIMR);
  115. nsec = rd32(E1000_SYSTIML);
  116. sec = rd32(E1000_SYSTIMH);
  117. ts->tv_sec = sec;
  118. ts->tv_nsec = nsec;
  119. }
  120. static void igb_ptp_write_i210(struct igb_adapter *adapter,
  121. const struct timespec64 *ts)
  122. {
  123. struct e1000_hw *hw = &adapter->hw;
  124. /* Writing the SYSTIMR register is not necessary as it only provides
  125. * sub-nanosecond resolution.
  126. */
  127. wr32(E1000_SYSTIML, ts->tv_nsec);
  128. wr32(E1000_SYSTIMH, (u32)ts->tv_sec);
  129. }
  130. /**
  131. * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
  132. * @adapter: board private structure
  133. * @hwtstamps: timestamp structure to update
  134. * @systim: unsigned 64bit system time value.
  135. *
  136. * We need to convert the system time value stored in the RX/TXSTMP registers
  137. * into a hwtstamp which can be used by the upper level timestamping functions.
  138. *
  139. * The 'tmreg_lock' spinlock is used to protect the consistency of the
  140. * system time value. This is needed because reading the 64 bit time
  141. * value involves reading two (or three) 32 bit registers. The first
  142. * read latches the value. Ditto for writing.
  143. *
  144. * In addition, here have extended the system time with an overflow
  145. * counter in software.
  146. **/
  147. static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
  148. struct skb_shared_hwtstamps *hwtstamps,
  149. u64 systim)
  150. {
  151. unsigned long flags;
  152. u64 ns;
  153. switch (adapter->hw.mac.type) {
  154. case e1000_82576:
  155. case e1000_82580:
  156. case e1000_i354:
  157. case e1000_i350:
  158. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  159. ns = timecounter_cyc2time(&adapter->tc, systim);
  160. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  161. memset(hwtstamps, 0, sizeof(*hwtstamps));
  162. hwtstamps->hwtstamp = ns_to_ktime(ns);
  163. break;
  164. case e1000_i210:
  165. case e1000_i211:
  166. memset(hwtstamps, 0, sizeof(*hwtstamps));
  167. /* Upper 32 bits contain s, lower 32 bits contain ns. */
  168. hwtstamps->hwtstamp = ktime_set(systim >> 32,
  169. systim & 0xFFFFFFFF);
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. /* PTP clock operations */
  176. static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
  177. {
  178. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  179. ptp_caps);
  180. struct e1000_hw *hw = &igb->hw;
  181. int neg_adj = 0;
  182. u64 rate;
  183. u32 incvalue;
  184. if (ppb < 0) {
  185. neg_adj = 1;
  186. ppb = -ppb;
  187. }
  188. rate = ppb;
  189. rate <<= 14;
  190. rate = div_u64(rate, 1953125);
  191. incvalue = 16 << IGB_82576_TSYNC_SHIFT;
  192. if (neg_adj)
  193. incvalue -= rate;
  194. else
  195. incvalue += rate;
  196. wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
  197. return 0;
  198. }
  199. static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb)
  200. {
  201. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  202. ptp_caps);
  203. struct e1000_hw *hw = &igb->hw;
  204. int neg_adj = 0;
  205. u64 rate;
  206. u32 inca;
  207. if (ppb < 0) {
  208. neg_adj = 1;
  209. ppb = -ppb;
  210. }
  211. rate = ppb;
  212. rate <<= 26;
  213. rate = div_u64(rate, 1953125);
  214. inca = rate & INCVALUE_MASK;
  215. if (neg_adj)
  216. inca |= ISGN;
  217. wr32(E1000_TIMINCA, inca);
  218. return 0;
  219. }
  220. static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
  221. {
  222. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  223. ptp_caps);
  224. unsigned long flags;
  225. spin_lock_irqsave(&igb->tmreg_lock, flags);
  226. timecounter_adjtime(&igb->tc, delta);
  227. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  228. return 0;
  229. }
  230. static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
  231. {
  232. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  233. ptp_caps);
  234. unsigned long flags;
  235. struct timespec64 now, then = ns_to_timespec64(delta);
  236. spin_lock_irqsave(&igb->tmreg_lock, flags);
  237. igb_ptp_read_i210(igb, &now);
  238. now = timespec64_add(now, then);
  239. igb_ptp_write_i210(igb, (const struct timespec64 *)&now);
  240. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  241. return 0;
  242. }
  243. static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
  244. struct timespec64 *ts)
  245. {
  246. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  247. ptp_caps);
  248. unsigned long flags;
  249. u64 ns;
  250. spin_lock_irqsave(&igb->tmreg_lock, flags);
  251. ns = timecounter_read(&igb->tc);
  252. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  253. *ts = ns_to_timespec64(ns);
  254. return 0;
  255. }
  256. static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
  257. struct timespec64 *ts)
  258. {
  259. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  260. ptp_caps);
  261. unsigned long flags;
  262. spin_lock_irqsave(&igb->tmreg_lock, flags);
  263. igb_ptp_read_i210(igb, ts);
  264. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  265. return 0;
  266. }
  267. static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
  268. const struct timespec64 *ts)
  269. {
  270. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  271. ptp_caps);
  272. unsigned long flags;
  273. u64 ns;
  274. ns = timespec64_to_ns(ts);
  275. spin_lock_irqsave(&igb->tmreg_lock, flags);
  276. timecounter_init(&igb->tc, &igb->cc, ns);
  277. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  278. return 0;
  279. }
  280. static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
  281. const struct timespec64 *ts)
  282. {
  283. struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
  284. ptp_caps);
  285. unsigned long flags;
  286. spin_lock_irqsave(&igb->tmreg_lock, flags);
  287. igb_ptp_write_i210(igb, ts);
  288. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  289. return 0;
  290. }
  291. static void igb_pin_direction(int pin, int input, u32 *ctrl, u32 *ctrl_ext)
  292. {
  293. u32 *ptr = pin < 2 ? ctrl : ctrl_ext;
  294. static const u32 mask[IGB_N_SDP] = {
  295. E1000_CTRL_SDP0_DIR,
  296. E1000_CTRL_SDP1_DIR,
  297. E1000_CTRL_EXT_SDP2_DIR,
  298. E1000_CTRL_EXT_SDP3_DIR,
  299. };
  300. if (input)
  301. *ptr &= ~mask[pin];
  302. else
  303. *ptr |= mask[pin];
  304. }
  305. static void igb_pin_extts(struct igb_adapter *igb, int chan, int pin)
  306. {
  307. static const u32 aux0_sel_sdp[IGB_N_SDP] = {
  308. AUX0_SEL_SDP0, AUX0_SEL_SDP1, AUX0_SEL_SDP2, AUX0_SEL_SDP3,
  309. };
  310. static const u32 aux1_sel_sdp[IGB_N_SDP] = {
  311. AUX1_SEL_SDP0, AUX1_SEL_SDP1, AUX1_SEL_SDP2, AUX1_SEL_SDP3,
  312. };
  313. static const u32 ts_sdp_en[IGB_N_SDP] = {
  314. TS_SDP0_EN, TS_SDP1_EN, TS_SDP2_EN, TS_SDP3_EN,
  315. };
  316. struct e1000_hw *hw = &igb->hw;
  317. u32 ctrl, ctrl_ext, tssdp = 0;
  318. ctrl = rd32(E1000_CTRL);
  319. ctrl_ext = rd32(E1000_CTRL_EXT);
  320. tssdp = rd32(E1000_TSSDP);
  321. igb_pin_direction(pin, 1, &ctrl, &ctrl_ext);
  322. /* Make sure this pin is not enabled as an output. */
  323. tssdp &= ~ts_sdp_en[pin];
  324. if (chan == 1) {
  325. tssdp &= ~AUX1_SEL_SDP3;
  326. tssdp |= aux1_sel_sdp[pin] | AUX1_TS_SDP_EN;
  327. } else {
  328. tssdp &= ~AUX0_SEL_SDP3;
  329. tssdp |= aux0_sel_sdp[pin] | AUX0_TS_SDP_EN;
  330. }
  331. wr32(E1000_TSSDP, tssdp);
  332. wr32(E1000_CTRL, ctrl);
  333. wr32(E1000_CTRL_EXT, ctrl_ext);
  334. }
  335. static void igb_pin_perout(struct igb_adapter *igb, int chan, int pin, int freq)
  336. {
  337. static const u32 aux0_sel_sdp[IGB_N_SDP] = {
  338. AUX0_SEL_SDP0, AUX0_SEL_SDP1, AUX0_SEL_SDP2, AUX0_SEL_SDP3,
  339. };
  340. static const u32 aux1_sel_sdp[IGB_N_SDP] = {
  341. AUX1_SEL_SDP0, AUX1_SEL_SDP1, AUX1_SEL_SDP2, AUX1_SEL_SDP3,
  342. };
  343. static const u32 ts_sdp_en[IGB_N_SDP] = {
  344. TS_SDP0_EN, TS_SDP1_EN, TS_SDP2_EN, TS_SDP3_EN,
  345. };
  346. static const u32 ts_sdp_sel_tt0[IGB_N_SDP] = {
  347. TS_SDP0_SEL_TT0, TS_SDP1_SEL_TT0,
  348. TS_SDP2_SEL_TT0, TS_SDP3_SEL_TT0,
  349. };
  350. static const u32 ts_sdp_sel_tt1[IGB_N_SDP] = {
  351. TS_SDP0_SEL_TT1, TS_SDP1_SEL_TT1,
  352. TS_SDP2_SEL_TT1, TS_SDP3_SEL_TT1,
  353. };
  354. static const u32 ts_sdp_sel_fc0[IGB_N_SDP] = {
  355. TS_SDP0_SEL_FC0, TS_SDP1_SEL_FC0,
  356. TS_SDP2_SEL_FC0, TS_SDP3_SEL_FC0,
  357. };
  358. static const u32 ts_sdp_sel_fc1[IGB_N_SDP] = {
  359. TS_SDP0_SEL_FC1, TS_SDP1_SEL_FC1,
  360. TS_SDP2_SEL_FC1, TS_SDP3_SEL_FC1,
  361. };
  362. static const u32 ts_sdp_sel_clr[IGB_N_SDP] = {
  363. TS_SDP0_SEL_FC1, TS_SDP1_SEL_FC1,
  364. TS_SDP2_SEL_FC1, TS_SDP3_SEL_FC1,
  365. };
  366. struct e1000_hw *hw = &igb->hw;
  367. u32 ctrl, ctrl_ext, tssdp = 0;
  368. ctrl = rd32(E1000_CTRL);
  369. ctrl_ext = rd32(E1000_CTRL_EXT);
  370. tssdp = rd32(E1000_TSSDP);
  371. igb_pin_direction(pin, 0, &ctrl, &ctrl_ext);
  372. /* Make sure this pin is not enabled as an input. */
  373. if ((tssdp & AUX0_SEL_SDP3) == aux0_sel_sdp[pin])
  374. tssdp &= ~AUX0_TS_SDP_EN;
  375. if ((tssdp & AUX1_SEL_SDP3) == aux1_sel_sdp[pin])
  376. tssdp &= ~AUX1_TS_SDP_EN;
  377. tssdp &= ~ts_sdp_sel_clr[pin];
  378. if (freq) {
  379. if (chan == 1)
  380. tssdp |= ts_sdp_sel_fc1[pin];
  381. else
  382. tssdp |= ts_sdp_sel_fc0[pin];
  383. } else {
  384. if (chan == 1)
  385. tssdp |= ts_sdp_sel_tt1[pin];
  386. else
  387. tssdp |= ts_sdp_sel_tt0[pin];
  388. }
  389. tssdp |= ts_sdp_en[pin];
  390. wr32(E1000_TSSDP, tssdp);
  391. wr32(E1000_CTRL, ctrl);
  392. wr32(E1000_CTRL_EXT, ctrl_ext);
  393. }
  394. static int igb_ptp_feature_enable_i210(struct ptp_clock_info *ptp,
  395. struct ptp_clock_request *rq, int on)
  396. {
  397. struct igb_adapter *igb =
  398. container_of(ptp, struct igb_adapter, ptp_caps);
  399. struct e1000_hw *hw = &igb->hw;
  400. u32 tsauxc, tsim, tsauxc_mask, tsim_mask, trgttiml, trgttimh, freqout;
  401. unsigned long flags;
  402. struct timespec64 ts;
  403. int use_freq = 0, pin = -1;
  404. s64 ns;
  405. switch (rq->type) {
  406. case PTP_CLK_REQ_EXTTS:
  407. if (on) {
  408. pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS,
  409. rq->extts.index);
  410. if (pin < 0)
  411. return -EBUSY;
  412. }
  413. if (rq->extts.index == 1) {
  414. tsauxc_mask = TSAUXC_EN_TS1;
  415. tsim_mask = TSINTR_AUTT1;
  416. } else {
  417. tsauxc_mask = TSAUXC_EN_TS0;
  418. tsim_mask = TSINTR_AUTT0;
  419. }
  420. spin_lock_irqsave(&igb->tmreg_lock, flags);
  421. tsauxc = rd32(E1000_TSAUXC);
  422. tsim = rd32(E1000_TSIM);
  423. if (on) {
  424. igb_pin_extts(igb, rq->extts.index, pin);
  425. tsauxc |= tsauxc_mask;
  426. tsim |= tsim_mask;
  427. } else {
  428. tsauxc &= ~tsauxc_mask;
  429. tsim &= ~tsim_mask;
  430. }
  431. wr32(E1000_TSAUXC, tsauxc);
  432. wr32(E1000_TSIM, tsim);
  433. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  434. return 0;
  435. case PTP_CLK_REQ_PEROUT:
  436. if (on) {
  437. pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT,
  438. rq->perout.index);
  439. if (pin < 0)
  440. return -EBUSY;
  441. }
  442. ts.tv_sec = rq->perout.period.sec;
  443. ts.tv_nsec = rq->perout.period.nsec;
  444. ns = timespec64_to_ns(&ts);
  445. ns = ns >> 1;
  446. if (on && ((ns <= 70000000LL) || (ns == 125000000LL) ||
  447. (ns == 250000000LL) || (ns == 500000000LL))) {
  448. if (ns < 8LL)
  449. return -EINVAL;
  450. use_freq = 1;
  451. }
  452. ts = ns_to_timespec64(ns);
  453. if (rq->perout.index == 1) {
  454. if (use_freq) {
  455. tsauxc_mask = TSAUXC_EN_CLK1 | TSAUXC_ST1;
  456. tsim_mask = 0;
  457. } else {
  458. tsauxc_mask = TSAUXC_EN_TT1;
  459. tsim_mask = TSINTR_TT1;
  460. }
  461. trgttiml = E1000_TRGTTIML1;
  462. trgttimh = E1000_TRGTTIMH1;
  463. freqout = E1000_FREQOUT1;
  464. } else {
  465. if (use_freq) {
  466. tsauxc_mask = TSAUXC_EN_CLK0 | TSAUXC_ST0;
  467. tsim_mask = 0;
  468. } else {
  469. tsauxc_mask = TSAUXC_EN_TT0;
  470. tsim_mask = TSINTR_TT0;
  471. }
  472. trgttiml = E1000_TRGTTIML0;
  473. trgttimh = E1000_TRGTTIMH0;
  474. freqout = E1000_FREQOUT0;
  475. }
  476. spin_lock_irqsave(&igb->tmreg_lock, flags);
  477. tsauxc = rd32(E1000_TSAUXC);
  478. tsim = rd32(E1000_TSIM);
  479. if (rq->perout.index == 1) {
  480. tsauxc &= ~(TSAUXC_EN_TT1 | TSAUXC_EN_CLK1 | TSAUXC_ST1);
  481. tsim &= ~TSINTR_TT1;
  482. } else {
  483. tsauxc &= ~(TSAUXC_EN_TT0 | TSAUXC_EN_CLK0 | TSAUXC_ST0);
  484. tsim &= ~TSINTR_TT0;
  485. }
  486. if (on) {
  487. int i = rq->perout.index;
  488. igb_pin_perout(igb, i, pin, use_freq);
  489. igb->perout[i].start.tv_sec = rq->perout.start.sec;
  490. igb->perout[i].start.tv_nsec = rq->perout.start.nsec;
  491. igb->perout[i].period.tv_sec = ts.tv_sec;
  492. igb->perout[i].period.tv_nsec = ts.tv_nsec;
  493. wr32(trgttimh, rq->perout.start.sec);
  494. wr32(trgttiml, rq->perout.start.nsec);
  495. if (use_freq)
  496. wr32(freqout, ns);
  497. tsauxc |= tsauxc_mask;
  498. tsim |= tsim_mask;
  499. }
  500. wr32(E1000_TSAUXC, tsauxc);
  501. wr32(E1000_TSIM, tsim);
  502. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  503. return 0;
  504. case PTP_CLK_REQ_PPS:
  505. spin_lock_irqsave(&igb->tmreg_lock, flags);
  506. tsim = rd32(E1000_TSIM);
  507. if (on)
  508. tsim |= TSINTR_SYS_WRAP;
  509. else
  510. tsim &= ~TSINTR_SYS_WRAP;
  511. wr32(E1000_TSIM, tsim);
  512. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  513. return 0;
  514. }
  515. return -EOPNOTSUPP;
  516. }
  517. static int igb_ptp_feature_enable(struct ptp_clock_info *ptp,
  518. struct ptp_clock_request *rq, int on)
  519. {
  520. return -EOPNOTSUPP;
  521. }
  522. static int igb_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
  523. enum ptp_pin_function func, unsigned int chan)
  524. {
  525. switch (func) {
  526. case PTP_PF_NONE:
  527. case PTP_PF_EXTTS:
  528. case PTP_PF_PEROUT:
  529. break;
  530. case PTP_PF_PHYSYNC:
  531. return -1;
  532. }
  533. return 0;
  534. }
  535. /**
  536. * igb_ptp_tx_work
  537. * @work: pointer to work struct
  538. *
  539. * This work function polls the TSYNCTXCTL valid bit to determine when a
  540. * timestamp has been taken for the current stored skb.
  541. **/
  542. static void igb_ptp_tx_work(struct work_struct *work)
  543. {
  544. struct igb_adapter *adapter = container_of(work, struct igb_adapter,
  545. ptp_tx_work);
  546. struct e1000_hw *hw = &adapter->hw;
  547. u32 tsynctxctl;
  548. if (!adapter->ptp_tx_skb)
  549. return;
  550. if (time_is_before_jiffies(adapter->ptp_tx_start +
  551. IGB_PTP_TX_TIMEOUT)) {
  552. dev_kfree_skb_any(adapter->ptp_tx_skb);
  553. adapter->ptp_tx_skb = NULL;
  554. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  555. adapter->tx_hwtstamp_timeouts++;
  556. dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
  557. return;
  558. }
  559. tsynctxctl = rd32(E1000_TSYNCTXCTL);
  560. if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
  561. igb_ptp_tx_hwtstamp(adapter);
  562. else
  563. /* reschedule to check later */
  564. schedule_work(&adapter->ptp_tx_work);
  565. }
  566. static void igb_ptp_overflow_check(struct work_struct *work)
  567. {
  568. struct igb_adapter *igb =
  569. container_of(work, struct igb_adapter, ptp_overflow_work.work);
  570. struct timespec64 ts;
  571. igb->ptp_caps.gettime64(&igb->ptp_caps, &ts);
  572. pr_debug("igb overflow check at %lld.%09lu\n",
  573. (long long) ts.tv_sec, ts.tv_nsec);
  574. schedule_delayed_work(&igb->ptp_overflow_work,
  575. IGB_SYSTIM_OVERFLOW_PERIOD);
  576. }
  577. /**
  578. * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
  579. * @adapter: private network adapter structure
  580. *
  581. * This watchdog task is scheduled to detect error case where hardware has
  582. * dropped an Rx packet that was timestamped when the ring is full. The
  583. * particular error is rare but leaves the device in a state unable to timestamp
  584. * any future packets.
  585. **/
  586. void igb_ptp_rx_hang(struct igb_adapter *adapter)
  587. {
  588. struct e1000_hw *hw = &adapter->hw;
  589. u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
  590. unsigned long rx_event;
  591. if (hw->mac.type != e1000_82576)
  592. return;
  593. /* If we don't have a valid timestamp in the registers, just update the
  594. * timeout counter and exit
  595. */
  596. if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
  597. adapter->last_rx_ptp_check = jiffies;
  598. return;
  599. }
  600. /* Determine the most recent watchdog or rx_timestamp event */
  601. rx_event = adapter->last_rx_ptp_check;
  602. if (time_after(adapter->last_rx_timestamp, rx_event))
  603. rx_event = adapter->last_rx_timestamp;
  604. /* Only need to read the high RXSTMP register to clear the lock */
  605. if (time_is_before_jiffies(rx_event + 5 * HZ)) {
  606. rd32(E1000_RXSTMPH);
  607. adapter->last_rx_ptp_check = jiffies;
  608. adapter->rx_hwtstamp_cleared++;
  609. dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang\n");
  610. }
  611. }
  612. /**
  613. * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  614. * @adapter: Board private structure.
  615. *
  616. * If we were asked to do hardware stamping and such a time stamp is
  617. * available, then it must have been for this skb here because we only
  618. * allow only one such packet into the queue.
  619. **/
  620. static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
  621. {
  622. struct e1000_hw *hw = &adapter->hw;
  623. struct skb_shared_hwtstamps shhwtstamps;
  624. u64 regval;
  625. regval = rd32(E1000_TXSTMPL);
  626. regval |= (u64)rd32(E1000_TXSTMPH) << 32;
  627. igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
  628. skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
  629. dev_kfree_skb_any(adapter->ptp_tx_skb);
  630. adapter->ptp_tx_skb = NULL;
  631. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  632. }
  633. /**
  634. * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
  635. * @q_vector: Pointer to interrupt specific structure
  636. * @va: Pointer to address containing Rx buffer
  637. * @skb: Buffer containing timestamp and packet
  638. *
  639. * This function is meant to retrieve a timestamp from the first buffer of an
  640. * incoming frame. The value is stored in little endian format starting on
  641. * byte 8.
  642. **/
  643. void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
  644. unsigned char *va,
  645. struct sk_buff *skb)
  646. {
  647. __le64 *regval = (__le64 *)va;
  648. /* The timestamp is recorded in little endian format.
  649. * DWORD: 0 1 2 3
  650. * Field: Reserved Reserved SYSTIML SYSTIMH
  651. */
  652. igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
  653. le64_to_cpu(regval[1]));
  654. }
  655. /**
  656. * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
  657. * @q_vector: Pointer to interrupt specific structure
  658. * @skb: Buffer containing timestamp and packet
  659. *
  660. * This function is meant to retrieve a timestamp from the internal registers
  661. * of the adapter and store it in the skb.
  662. **/
  663. void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
  664. struct sk_buff *skb)
  665. {
  666. struct igb_adapter *adapter = q_vector->adapter;
  667. struct e1000_hw *hw = &adapter->hw;
  668. u64 regval;
  669. /* If this bit is set, then the RX registers contain the time stamp. No
  670. * other packet will be time stamped until we read these registers, so
  671. * read the registers to make them available again. Because only one
  672. * packet can be time stamped at a time, we know that the register
  673. * values must belong to this one here and therefore we don't need to
  674. * compare any of the additional attributes stored for it.
  675. *
  676. * If nothing went wrong, then it should have a shared tx_flags that we
  677. * can turn into a skb_shared_hwtstamps.
  678. */
  679. if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
  680. return;
  681. regval = rd32(E1000_RXSTMPL);
  682. regval |= (u64)rd32(E1000_RXSTMPH) << 32;
  683. igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  684. /* Update the last_rx_timestamp timer in order to enable watchdog check
  685. * for error case of latched timestamp on a dropped packet.
  686. */
  687. adapter->last_rx_timestamp = jiffies;
  688. }
  689. /**
  690. * igb_ptp_get_ts_config - get hardware time stamping config
  691. * @netdev:
  692. * @ifreq:
  693. *
  694. * Get the hwtstamp_config settings to return to the user. Rather than attempt
  695. * to deconstruct the settings from the registers, just return a shadow copy
  696. * of the last known settings.
  697. **/
  698. int igb_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
  699. {
  700. struct igb_adapter *adapter = netdev_priv(netdev);
  701. struct hwtstamp_config *config = &adapter->tstamp_config;
  702. return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
  703. -EFAULT : 0;
  704. }
  705. /**
  706. * igb_ptp_set_timestamp_mode - setup hardware for timestamping
  707. * @adapter: networking device structure
  708. * @config: hwtstamp configuration
  709. *
  710. * Outgoing time stamping can be enabled and disabled. Play nice and
  711. * disable it when requested, although it shouldn't case any overhead
  712. * when no packet needs it. At most one packet in the queue may be
  713. * marked for time stamping, otherwise it would be impossible to tell
  714. * for sure to which packet the hardware time stamp belongs.
  715. *
  716. * Incoming time stamping has to be configured via the hardware
  717. * filters. Not all combinations are supported, in particular event
  718. * type has to be specified. Matching the kind of event packet is
  719. * not supported, with the exception of "all V2 events regardless of
  720. * level 2 or 4".
  721. */
  722. static int igb_ptp_set_timestamp_mode(struct igb_adapter *adapter,
  723. struct hwtstamp_config *config)
  724. {
  725. struct e1000_hw *hw = &adapter->hw;
  726. u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
  727. u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  728. u32 tsync_rx_cfg = 0;
  729. bool is_l4 = false;
  730. bool is_l2 = false;
  731. u32 regval;
  732. /* reserved for future extensions */
  733. if (config->flags)
  734. return -EINVAL;
  735. switch (config->tx_type) {
  736. case HWTSTAMP_TX_OFF:
  737. tsync_tx_ctl = 0;
  738. case HWTSTAMP_TX_ON:
  739. break;
  740. default:
  741. return -ERANGE;
  742. }
  743. switch (config->rx_filter) {
  744. case HWTSTAMP_FILTER_NONE:
  745. tsync_rx_ctl = 0;
  746. break;
  747. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  748. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  749. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
  750. is_l4 = true;
  751. break;
  752. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  753. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  754. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
  755. is_l4 = true;
  756. break;
  757. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  758. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  759. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  760. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  761. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  762. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  763. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  764. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  765. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  766. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
  767. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  768. is_l2 = true;
  769. is_l4 = true;
  770. break;
  771. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  772. case HWTSTAMP_FILTER_ALL:
  773. /* 82576 cannot timestamp all packets, which it needs to do to
  774. * support both V1 Sync and Delay_Req messages
  775. */
  776. if (hw->mac.type != e1000_82576) {
  777. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  778. config->rx_filter = HWTSTAMP_FILTER_ALL;
  779. break;
  780. }
  781. /* fall through */
  782. default:
  783. config->rx_filter = HWTSTAMP_FILTER_NONE;
  784. return -ERANGE;
  785. }
  786. if (hw->mac.type == e1000_82575) {
  787. if (tsync_rx_ctl | tsync_tx_ctl)
  788. return -EINVAL;
  789. return 0;
  790. }
  791. /* Per-packet timestamping only works if all packets are
  792. * timestamped, so enable timestamping in all packets as
  793. * long as one Rx filter was configured.
  794. */
  795. if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
  796. tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  797. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  798. config->rx_filter = HWTSTAMP_FILTER_ALL;
  799. is_l2 = true;
  800. is_l4 = true;
  801. if ((hw->mac.type == e1000_i210) ||
  802. (hw->mac.type == e1000_i211)) {
  803. regval = rd32(E1000_RXPBS);
  804. regval |= E1000_RXPBS_CFG_TS_EN;
  805. wr32(E1000_RXPBS, regval);
  806. }
  807. }
  808. /* enable/disable TX */
  809. regval = rd32(E1000_TSYNCTXCTL);
  810. regval &= ~E1000_TSYNCTXCTL_ENABLED;
  811. regval |= tsync_tx_ctl;
  812. wr32(E1000_TSYNCTXCTL, regval);
  813. /* enable/disable RX */
  814. regval = rd32(E1000_TSYNCRXCTL);
  815. regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
  816. regval |= tsync_rx_ctl;
  817. wr32(E1000_TSYNCRXCTL, regval);
  818. /* define which PTP packets are time stamped */
  819. wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
  820. /* define ethertype filter for timestamped packets */
  821. if (is_l2)
  822. wr32(E1000_ETQF(3),
  823. (E1000_ETQF_FILTER_ENABLE | /* enable filter */
  824. E1000_ETQF_1588 | /* enable timestamping */
  825. ETH_P_1588)); /* 1588 eth protocol type */
  826. else
  827. wr32(E1000_ETQF(3), 0);
  828. /* L4 Queue Filter[3]: filter by destination port and protocol */
  829. if (is_l4) {
  830. u32 ftqf = (IPPROTO_UDP /* UDP */
  831. | E1000_FTQF_VF_BP /* VF not compared */
  832. | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
  833. | E1000_FTQF_MASK); /* mask all inputs */
  834. ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
  835. wr32(E1000_IMIR(3), htons(PTP_EV_PORT));
  836. wr32(E1000_IMIREXT(3),
  837. (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
  838. if (hw->mac.type == e1000_82576) {
  839. /* enable source port check */
  840. wr32(E1000_SPQF(3), htons(PTP_EV_PORT));
  841. ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
  842. }
  843. wr32(E1000_FTQF(3), ftqf);
  844. } else {
  845. wr32(E1000_FTQF(3), E1000_FTQF_MASK);
  846. }
  847. wrfl();
  848. /* clear TX/RX time stamp registers, just to be sure */
  849. regval = rd32(E1000_TXSTMPL);
  850. regval = rd32(E1000_TXSTMPH);
  851. regval = rd32(E1000_RXSTMPL);
  852. regval = rd32(E1000_RXSTMPH);
  853. return 0;
  854. }
  855. /**
  856. * igb_ptp_set_ts_config - set hardware time stamping config
  857. * @netdev:
  858. * @ifreq:
  859. *
  860. **/
  861. int igb_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
  862. {
  863. struct igb_adapter *adapter = netdev_priv(netdev);
  864. struct hwtstamp_config config;
  865. int err;
  866. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  867. return -EFAULT;
  868. err = igb_ptp_set_timestamp_mode(adapter, &config);
  869. if (err)
  870. return err;
  871. /* save these settings for future reference */
  872. memcpy(&adapter->tstamp_config, &config,
  873. sizeof(adapter->tstamp_config));
  874. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  875. -EFAULT : 0;
  876. }
  877. void igb_ptp_init(struct igb_adapter *adapter)
  878. {
  879. struct e1000_hw *hw = &adapter->hw;
  880. struct net_device *netdev = adapter->netdev;
  881. int i;
  882. switch (hw->mac.type) {
  883. case e1000_82576:
  884. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  885. adapter->ptp_caps.owner = THIS_MODULE;
  886. adapter->ptp_caps.max_adj = 999999881;
  887. adapter->ptp_caps.n_ext_ts = 0;
  888. adapter->ptp_caps.pps = 0;
  889. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
  890. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  891. adapter->ptp_caps.gettime64 = igb_ptp_gettime_82576;
  892. adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
  893. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  894. adapter->cc.read = igb_ptp_read_82576;
  895. adapter->cc.mask = CYCLECOUNTER_MASK(64);
  896. adapter->cc.mult = 1;
  897. adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
  898. /* Dial the nominal frequency. */
  899. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  900. break;
  901. case e1000_82580:
  902. case e1000_i354:
  903. case e1000_i350:
  904. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  905. adapter->ptp_caps.owner = THIS_MODULE;
  906. adapter->ptp_caps.max_adj = 62499999;
  907. adapter->ptp_caps.n_ext_ts = 0;
  908. adapter->ptp_caps.pps = 0;
  909. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  910. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  911. adapter->ptp_caps.gettime64 = igb_ptp_gettime_82576;
  912. adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
  913. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  914. adapter->cc.read = igb_ptp_read_82580;
  915. adapter->cc.mask = CYCLECOUNTER_MASK(IGB_NBITS_82580);
  916. adapter->cc.mult = 1;
  917. adapter->cc.shift = 0;
  918. /* Enable the timer functions by clearing bit 31. */
  919. wr32(E1000_TSAUXC, 0x0);
  920. break;
  921. case e1000_i210:
  922. case e1000_i211:
  923. for (i = 0; i < IGB_N_SDP; i++) {
  924. struct ptp_pin_desc *ppd = &adapter->sdp_config[i];
  925. snprintf(ppd->name, sizeof(ppd->name), "SDP%d", i);
  926. ppd->index = i;
  927. ppd->func = PTP_PF_NONE;
  928. }
  929. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  930. adapter->ptp_caps.owner = THIS_MODULE;
  931. adapter->ptp_caps.max_adj = 62499999;
  932. adapter->ptp_caps.n_ext_ts = IGB_N_EXTTS;
  933. adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
  934. adapter->ptp_caps.n_pins = IGB_N_SDP;
  935. adapter->ptp_caps.pps = 1;
  936. adapter->ptp_caps.pin_config = adapter->sdp_config;
  937. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  938. adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
  939. adapter->ptp_caps.gettime64 = igb_ptp_gettime_i210;
  940. adapter->ptp_caps.settime64 = igb_ptp_settime_i210;
  941. adapter->ptp_caps.enable = igb_ptp_feature_enable_i210;
  942. adapter->ptp_caps.verify = igb_ptp_verify_pin;
  943. /* Enable the timer functions by clearing bit 31. */
  944. wr32(E1000_TSAUXC, 0x0);
  945. break;
  946. default:
  947. adapter->ptp_clock = NULL;
  948. return;
  949. }
  950. wrfl();
  951. spin_lock_init(&adapter->tmreg_lock);
  952. INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
  953. /* Initialize the clock and overflow work for devices that need it. */
  954. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  955. struct timespec64 ts = ktime_to_timespec64(ktime_get_real());
  956. igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
  957. } else {
  958. timecounter_init(&adapter->tc, &adapter->cc,
  959. ktime_to_ns(ktime_get_real()));
  960. INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
  961. igb_ptp_overflow_check);
  962. schedule_delayed_work(&adapter->ptp_overflow_work,
  963. IGB_SYSTIM_OVERFLOW_PERIOD);
  964. }
  965. /* Initialize the time sync interrupts for devices that support it. */
  966. if (hw->mac.type >= e1000_82580) {
  967. wr32(E1000_TSIM, TSYNC_INTERRUPTS);
  968. wr32(E1000_IMS, E1000_IMS_TS);
  969. }
  970. adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  971. adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  972. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  973. &adapter->pdev->dev);
  974. if (IS_ERR(adapter->ptp_clock)) {
  975. adapter->ptp_clock = NULL;
  976. dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
  977. } else {
  978. dev_info(&adapter->pdev->dev, "added PHC on %s\n",
  979. adapter->netdev->name);
  980. adapter->flags |= IGB_FLAG_PTP;
  981. }
  982. }
  983. /**
  984. * igb_ptp_stop - Disable PTP device and stop the overflow check.
  985. * @adapter: Board private structure.
  986. *
  987. * This function stops the PTP support and cancels the delayed work.
  988. **/
  989. void igb_ptp_stop(struct igb_adapter *adapter)
  990. {
  991. switch (adapter->hw.mac.type) {
  992. case e1000_82576:
  993. case e1000_82580:
  994. case e1000_i354:
  995. case e1000_i350:
  996. cancel_delayed_work_sync(&adapter->ptp_overflow_work);
  997. break;
  998. case e1000_i210:
  999. case e1000_i211:
  1000. /* No delayed work to cancel. */
  1001. break;
  1002. default:
  1003. return;
  1004. }
  1005. cancel_work_sync(&adapter->ptp_tx_work);
  1006. if (adapter->ptp_tx_skb) {
  1007. dev_kfree_skb_any(adapter->ptp_tx_skb);
  1008. adapter->ptp_tx_skb = NULL;
  1009. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  1010. }
  1011. if (adapter->ptp_clock) {
  1012. ptp_clock_unregister(adapter->ptp_clock);
  1013. dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
  1014. adapter->netdev->name);
  1015. adapter->flags &= ~IGB_FLAG_PTP;
  1016. }
  1017. }
  1018. /**
  1019. * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
  1020. * @adapter: Board private structure.
  1021. *
  1022. * This function handles the reset work required to re-enable the PTP device.
  1023. **/
  1024. void igb_ptp_reset(struct igb_adapter *adapter)
  1025. {
  1026. struct e1000_hw *hw = &adapter->hw;
  1027. unsigned long flags;
  1028. if (!(adapter->flags & IGB_FLAG_PTP))
  1029. return;
  1030. /* reset the tstamp_config */
  1031. igb_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
  1032. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  1033. switch (adapter->hw.mac.type) {
  1034. case e1000_82576:
  1035. /* Dial the nominal frequency. */
  1036. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  1037. break;
  1038. case e1000_82580:
  1039. case e1000_i354:
  1040. case e1000_i350:
  1041. case e1000_i210:
  1042. case e1000_i211:
  1043. wr32(E1000_TSAUXC, 0x0);
  1044. wr32(E1000_TSSDP, 0x0);
  1045. wr32(E1000_TSIM, TSYNC_INTERRUPTS);
  1046. wr32(E1000_IMS, E1000_IMS_TS);
  1047. break;
  1048. default:
  1049. /* No work to do. */
  1050. goto out;
  1051. }
  1052. /* Re-initialize the timer. */
  1053. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  1054. struct timespec64 ts = ktime_to_timespec64(ktime_get_real());
  1055. igb_ptp_write_i210(adapter, &ts);
  1056. } else {
  1057. timecounter_init(&adapter->tc, &adapter->cc,
  1058. ktime_to_ns(ktime_get_real()));
  1059. }
  1060. out:
  1061. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  1062. }