igb_ptp.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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, 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 timespec 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 = timespec_to_ns(&ts);
  445. ns = ns >> 1;
  446. if (on && ns <= 70000000LL) {
  447. if (ns < 8LL)
  448. return -EINVAL;
  449. use_freq = 1;
  450. }
  451. ts = ns_to_timespec(ns);
  452. if (rq->perout.index == 1) {
  453. if (use_freq) {
  454. tsauxc_mask = TSAUXC_EN_CLK1 | TSAUXC_ST1;
  455. tsim_mask = 0;
  456. } else {
  457. tsauxc_mask = TSAUXC_EN_TT1;
  458. tsim_mask = TSINTR_TT1;
  459. }
  460. trgttiml = E1000_TRGTTIML1;
  461. trgttimh = E1000_TRGTTIMH1;
  462. freqout = E1000_FREQOUT1;
  463. } else {
  464. if (use_freq) {
  465. tsauxc_mask = TSAUXC_EN_CLK0 | TSAUXC_ST0;
  466. tsim_mask = 0;
  467. } else {
  468. tsauxc_mask = TSAUXC_EN_TT0;
  469. tsim_mask = TSINTR_TT0;
  470. }
  471. trgttiml = E1000_TRGTTIML0;
  472. trgttimh = E1000_TRGTTIMH0;
  473. freqout = E1000_FREQOUT0;
  474. }
  475. spin_lock_irqsave(&igb->tmreg_lock, flags);
  476. tsauxc = rd32(E1000_TSAUXC);
  477. tsim = rd32(E1000_TSIM);
  478. if (rq->perout.index == 1) {
  479. tsauxc &= ~(TSAUXC_EN_TT1 | TSAUXC_EN_CLK1 | TSAUXC_ST1);
  480. tsim &= ~TSINTR_TT1;
  481. } else {
  482. tsauxc &= ~(TSAUXC_EN_TT0 | TSAUXC_EN_CLK0 | TSAUXC_ST0);
  483. tsim &= ~TSINTR_TT0;
  484. }
  485. if (on) {
  486. int i = rq->perout.index;
  487. igb_pin_perout(igb, i, pin, use_freq);
  488. igb->perout[i].start.tv_sec = rq->perout.start.sec;
  489. igb->perout[i].start.tv_nsec = rq->perout.start.nsec;
  490. igb->perout[i].period.tv_sec = ts.tv_sec;
  491. igb->perout[i].period.tv_nsec = ts.tv_nsec;
  492. wr32(trgttimh, rq->perout.start.sec);
  493. wr32(trgttiml, rq->perout.start.nsec);
  494. if (use_freq)
  495. wr32(freqout, ns);
  496. tsauxc |= tsauxc_mask;
  497. tsim |= tsim_mask;
  498. }
  499. wr32(E1000_TSAUXC, tsauxc);
  500. wr32(E1000_TSIM, tsim);
  501. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  502. return 0;
  503. case PTP_CLK_REQ_PPS:
  504. spin_lock_irqsave(&igb->tmreg_lock, flags);
  505. tsim = rd32(E1000_TSIM);
  506. if (on)
  507. tsim |= TSINTR_SYS_WRAP;
  508. else
  509. tsim &= ~TSINTR_SYS_WRAP;
  510. wr32(E1000_TSIM, tsim);
  511. spin_unlock_irqrestore(&igb->tmreg_lock, flags);
  512. return 0;
  513. }
  514. return -EOPNOTSUPP;
  515. }
  516. static int igb_ptp_feature_enable(struct ptp_clock_info *ptp,
  517. struct ptp_clock_request *rq, int on)
  518. {
  519. return -EOPNOTSUPP;
  520. }
  521. static int igb_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
  522. enum ptp_pin_function func, unsigned int chan)
  523. {
  524. switch (func) {
  525. case PTP_PF_NONE:
  526. case PTP_PF_EXTTS:
  527. case PTP_PF_PEROUT:
  528. break;
  529. case PTP_PF_PHYSYNC:
  530. return -1;
  531. }
  532. return 0;
  533. }
  534. /**
  535. * igb_ptp_tx_work
  536. * @work: pointer to work struct
  537. *
  538. * This work function polls the TSYNCTXCTL valid bit to determine when a
  539. * timestamp has been taken for the current stored skb.
  540. **/
  541. static void igb_ptp_tx_work(struct work_struct *work)
  542. {
  543. struct igb_adapter *adapter = container_of(work, struct igb_adapter,
  544. ptp_tx_work);
  545. struct e1000_hw *hw = &adapter->hw;
  546. u32 tsynctxctl;
  547. if (!adapter->ptp_tx_skb)
  548. return;
  549. if (time_is_before_jiffies(adapter->ptp_tx_start +
  550. IGB_PTP_TX_TIMEOUT)) {
  551. dev_kfree_skb_any(adapter->ptp_tx_skb);
  552. adapter->ptp_tx_skb = NULL;
  553. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  554. adapter->tx_hwtstamp_timeouts++;
  555. dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang\n");
  556. return;
  557. }
  558. tsynctxctl = rd32(E1000_TSYNCTXCTL);
  559. if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
  560. igb_ptp_tx_hwtstamp(adapter);
  561. else
  562. /* reschedule to check later */
  563. schedule_work(&adapter->ptp_tx_work);
  564. }
  565. static void igb_ptp_overflow_check(struct work_struct *work)
  566. {
  567. struct igb_adapter *igb =
  568. container_of(work, struct igb_adapter, ptp_overflow_work.work);
  569. struct timespec64 ts;
  570. igb->ptp_caps.gettime64(&igb->ptp_caps, &ts);
  571. pr_debug("igb overflow check at %lld.%09lu\n",
  572. (long long) ts.tv_sec, ts.tv_nsec);
  573. schedule_delayed_work(&igb->ptp_overflow_work,
  574. IGB_SYSTIM_OVERFLOW_PERIOD);
  575. }
  576. /**
  577. * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched
  578. * @adapter: private network adapter structure
  579. *
  580. * This watchdog task is scheduled to detect error case where hardware has
  581. * dropped an Rx packet that was timestamped when the ring is full. The
  582. * particular error is rare but leaves the device in a state unable to timestamp
  583. * any future packets.
  584. **/
  585. void igb_ptp_rx_hang(struct igb_adapter *adapter)
  586. {
  587. struct e1000_hw *hw = &adapter->hw;
  588. u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL);
  589. unsigned long rx_event;
  590. if (hw->mac.type != e1000_82576)
  591. return;
  592. /* If we don't have a valid timestamp in the registers, just update the
  593. * timeout counter and exit
  594. */
  595. if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) {
  596. adapter->last_rx_ptp_check = jiffies;
  597. return;
  598. }
  599. /* Determine the most recent watchdog or rx_timestamp event */
  600. rx_event = adapter->last_rx_ptp_check;
  601. if (time_after(adapter->last_rx_timestamp, rx_event))
  602. rx_event = adapter->last_rx_timestamp;
  603. /* Only need to read the high RXSTMP register to clear the lock */
  604. if (time_is_before_jiffies(rx_event + 5 * HZ)) {
  605. rd32(E1000_RXSTMPH);
  606. adapter->last_rx_ptp_check = jiffies;
  607. adapter->rx_hwtstamp_cleared++;
  608. dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang\n");
  609. }
  610. }
  611. /**
  612. * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
  613. * @adapter: Board private structure.
  614. *
  615. * If we were asked to do hardware stamping and such a time stamp is
  616. * available, then it must have been for this skb here because we only
  617. * allow only one such packet into the queue.
  618. **/
  619. static void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
  620. {
  621. struct e1000_hw *hw = &adapter->hw;
  622. struct skb_shared_hwtstamps shhwtstamps;
  623. u64 regval;
  624. regval = rd32(E1000_TXSTMPL);
  625. regval |= (u64)rd32(E1000_TXSTMPH) << 32;
  626. igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
  627. skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
  628. dev_kfree_skb_any(adapter->ptp_tx_skb);
  629. adapter->ptp_tx_skb = NULL;
  630. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  631. }
  632. /**
  633. * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
  634. * @q_vector: Pointer to interrupt specific structure
  635. * @va: Pointer to address containing Rx buffer
  636. * @skb: Buffer containing timestamp and packet
  637. *
  638. * This function is meant to retrieve a timestamp from the first buffer of an
  639. * incoming frame. The value is stored in little endian format starting on
  640. * byte 8.
  641. **/
  642. void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
  643. unsigned char *va,
  644. struct sk_buff *skb)
  645. {
  646. __le64 *regval = (__le64 *)va;
  647. /* The timestamp is recorded in little endian format.
  648. * DWORD: 0 1 2 3
  649. * Field: Reserved Reserved SYSTIML SYSTIMH
  650. */
  651. igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
  652. le64_to_cpu(regval[1]));
  653. }
  654. /**
  655. * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
  656. * @q_vector: Pointer to interrupt specific structure
  657. * @skb: Buffer containing timestamp and packet
  658. *
  659. * This function is meant to retrieve a timestamp from the internal registers
  660. * of the adapter and store it in the skb.
  661. **/
  662. void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
  663. struct sk_buff *skb)
  664. {
  665. struct igb_adapter *adapter = q_vector->adapter;
  666. struct e1000_hw *hw = &adapter->hw;
  667. u64 regval;
  668. /* If this bit is set, then the RX registers contain the time stamp. No
  669. * other packet will be time stamped until we read these registers, so
  670. * read the registers to make them available again. Because only one
  671. * packet can be time stamped at a time, we know that the register
  672. * values must belong to this one here and therefore we don't need to
  673. * compare any of the additional attributes stored for it.
  674. *
  675. * If nothing went wrong, then it should have a shared tx_flags that we
  676. * can turn into a skb_shared_hwtstamps.
  677. */
  678. if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
  679. return;
  680. regval = rd32(E1000_RXSTMPL);
  681. regval |= (u64)rd32(E1000_RXSTMPH) << 32;
  682. igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  683. /* Update the last_rx_timestamp timer in order to enable watchdog check
  684. * for error case of latched timestamp on a dropped packet.
  685. */
  686. adapter->last_rx_timestamp = jiffies;
  687. }
  688. /**
  689. * igb_ptp_get_ts_config - get hardware time stamping config
  690. * @netdev:
  691. * @ifreq:
  692. *
  693. * Get the hwtstamp_config settings to return to the user. Rather than attempt
  694. * to deconstruct the settings from the registers, just return a shadow copy
  695. * of the last known settings.
  696. **/
  697. int igb_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
  698. {
  699. struct igb_adapter *adapter = netdev_priv(netdev);
  700. struct hwtstamp_config *config = &adapter->tstamp_config;
  701. return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
  702. -EFAULT : 0;
  703. }
  704. /**
  705. * igb_ptp_set_timestamp_mode - setup hardware for timestamping
  706. * @adapter: networking device structure
  707. * @config: hwtstamp configuration
  708. *
  709. * Outgoing time stamping can be enabled and disabled. Play nice and
  710. * disable it when requested, although it shouldn't case any overhead
  711. * when no packet needs it. At most one packet in the queue may be
  712. * marked for time stamping, otherwise it would be impossible to tell
  713. * for sure to which packet the hardware time stamp belongs.
  714. *
  715. * Incoming time stamping has to be configured via the hardware
  716. * filters. Not all combinations are supported, in particular event
  717. * type has to be specified. Matching the kind of event packet is
  718. * not supported, with the exception of "all V2 events regardless of
  719. * level 2 or 4".
  720. */
  721. static int igb_ptp_set_timestamp_mode(struct igb_adapter *adapter,
  722. struct hwtstamp_config *config)
  723. {
  724. struct e1000_hw *hw = &adapter->hw;
  725. u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
  726. u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  727. u32 tsync_rx_cfg = 0;
  728. bool is_l4 = false;
  729. bool is_l2 = false;
  730. u32 regval;
  731. /* reserved for future extensions */
  732. if (config->flags)
  733. return -EINVAL;
  734. switch (config->tx_type) {
  735. case HWTSTAMP_TX_OFF:
  736. tsync_tx_ctl = 0;
  737. case HWTSTAMP_TX_ON:
  738. break;
  739. default:
  740. return -ERANGE;
  741. }
  742. switch (config->rx_filter) {
  743. case HWTSTAMP_FILTER_NONE:
  744. tsync_rx_ctl = 0;
  745. break;
  746. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  747. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  748. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
  749. is_l4 = true;
  750. break;
  751. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  752. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  753. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
  754. is_l4 = true;
  755. break;
  756. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  757. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  758. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  759. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  760. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  761. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  762. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  763. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  764. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  765. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
  766. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  767. is_l2 = true;
  768. is_l4 = true;
  769. break;
  770. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  771. case HWTSTAMP_FILTER_ALL:
  772. /* 82576 cannot timestamp all packets, which it needs to do to
  773. * support both V1 Sync and Delay_Req messages
  774. */
  775. if (hw->mac.type != e1000_82576) {
  776. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  777. config->rx_filter = HWTSTAMP_FILTER_ALL;
  778. break;
  779. }
  780. /* fall through */
  781. default:
  782. config->rx_filter = HWTSTAMP_FILTER_NONE;
  783. return -ERANGE;
  784. }
  785. if (hw->mac.type == e1000_82575) {
  786. if (tsync_rx_ctl | tsync_tx_ctl)
  787. return -EINVAL;
  788. return 0;
  789. }
  790. /* Per-packet timestamping only works if all packets are
  791. * timestamped, so enable timestamping in all packets as
  792. * long as one Rx filter was configured.
  793. */
  794. if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
  795. tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  796. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  797. config->rx_filter = HWTSTAMP_FILTER_ALL;
  798. is_l2 = true;
  799. is_l4 = true;
  800. if ((hw->mac.type == e1000_i210) ||
  801. (hw->mac.type == e1000_i211)) {
  802. regval = rd32(E1000_RXPBS);
  803. regval |= E1000_RXPBS_CFG_TS_EN;
  804. wr32(E1000_RXPBS, regval);
  805. }
  806. }
  807. /* enable/disable TX */
  808. regval = rd32(E1000_TSYNCTXCTL);
  809. regval &= ~E1000_TSYNCTXCTL_ENABLED;
  810. regval |= tsync_tx_ctl;
  811. wr32(E1000_TSYNCTXCTL, regval);
  812. /* enable/disable RX */
  813. regval = rd32(E1000_TSYNCRXCTL);
  814. regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
  815. regval |= tsync_rx_ctl;
  816. wr32(E1000_TSYNCRXCTL, regval);
  817. /* define which PTP packets are time stamped */
  818. wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
  819. /* define ethertype filter for timestamped packets */
  820. if (is_l2)
  821. wr32(E1000_ETQF(3),
  822. (E1000_ETQF_FILTER_ENABLE | /* enable filter */
  823. E1000_ETQF_1588 | /* enable timestamping */
  824. ETH_P_1588)); /* 1588 eth protocol type */
  825. else
  826. wr32(E1000_ETQF(3), 0);
  827. /* L4 Queue Filter[3]: filter by destination port and protocol */
  828. if (is_l4) {
  829. u32 ftqf = (IPPROTO_UDP /* UDP */
  830. | E1000_FTQF_VF_BP /* VF not compared */
  831. | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
  832. | E1000_FTQF_MASK); /* mask all inputs */
  833. ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
  834. wr32(E1000_IMIR(3), htons(PTP_EV_PORT));
  835. wr32(E1000_IMIREXT(3),
  836. (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
  837. if (hw->mac.type == e1000_82576) {
  838. /* enable source port check */
  839. wr32(E1000_SPQF(3), htons(PTP_EV_PORT));
  840. ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
  841. }
  842. wr32(E1000_FTQF(3), ftqf);
  843. } else {
  844. wr32(E1000_FTQF(3), E1000_FTQF_MASK);
  845. }
  846. wrfl();
  847. /* clear TX/RX time stamp registers, just to be sure */
  848. regval = rd32(E1000_TXSTMPL);
  849. regval = rd32(E1000_TXSTMPH);
  850. regval = rd32(E1000_RXSTMPL);
  851. regval = rd32(E1000_RXSTMPH);
  852. return 0;
  853. }
  854. /**
  855. * igb_ptp_set_ts_config - set hardware time stamping config
  856. * @netdev:
  857. * @ifreq:
  858. *
  859. **/
  860. int igb_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
  861. {
  862. struct igb_adapter *adapter = netdev_priv(netdev);
  863. struct hwtstamp_config config;
  864. int err;
  865. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  866. return -EFAULT;
  867. err = igb_ptp_set_timestamp_mode(adapter, &config);
  868. if (err)
  869. return err;
  870. /* save these settings for future reference */
  871. memcpy(&adapter->tstamp_config, &config,
  872. sizeof(adapter->tstamp_config));
  873. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  874. -EFAULT : 0;
  875. }
  876. void igb_ptp_init(struct igb_adapter *adapter)
  877. {
  878. struct e1000_hw *hw = &adapter->hw;
  879. struct net_device *netdev = adapter->netdev;
  880. int i;
  881. switch (hw->mac.type) {
  882. case e1000_82576:
  883. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  884. adapter->ptp_caps.owner = THIS_MODULE;
  885. adapter->ptp_caps.max_adj = 999999881;
  886. adapter->ptp_caps.n_ext_ts = 0;
  887. adapter->ptp_caps.pps = 0;
  888. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
  889. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  890. adapter->ptp_caps.gettime64 = igb_ptp_gettime_82576;
  891. adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
  892. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  893. adapter->cc.read = igb_ptp_read_82576;
  894. adapter->cc.mask = CYCLECOUNTER_MASK(64);
  895. adapter->cc.mult = 1;
  896. adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
  897. /* Dial the nominal frequency. */
  898. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  899. break;
  900. case e1000_82580:
  901. case e1000_i354:
  902. case e1000_i350:
  903. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  904. adapter->ptp_caps.owner = THIS_MODULE;
  905. adapter->ptp_caps.max_adj = 62499999;
  906. adapter->ptp_caps.n_ext_ts = 0;
  907. adapter->ptp_caps.pps = 0;
  908. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  909. adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
  910. adapter->ptp_caps.gettime64 = igb_ptp_gettime_82576;
  911. adapter->ptp_caps.settime64 = igb_ptp_settime_82576;
  912. adapter->ptp_caps.enable = igb_ptp_feature_enable;
  913. adapter->cc.read = igb_ptp_read_82580;
  914. adapter->cc.mask = CYCLECOUNTER_MASK(IGB_NBITS_82580);
  915. adapter->cc.mult = 1;
  916. adapter->cc.shift = 0;
  917. /* Enable the timer functions by clearing bit 31. */
  918. wr32(E1000_TSAUXC, 0x0);
  919. break;
  920. case e1000_i210:
  921. case e1000_i211:
  922. for (i = 0; i < IGB_N_SDP; i++) {
  923. struct ptp_pin_desc *ppd = &adapter->sdp_config[i];
  924. snprintf(ppd->name, sizeof(ppd->name), "SDP%d", i);
  925. ppd->index = i;
  926. ppd->func = PTP_PF_NONE;
  927. }
  928. snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
  929. adapter->ptp_caps.owner = THIS_MODULE;
  930. adapter->ptp_caps.max_adj = 62499999;
  931. adapter->ptp_caps.n_ext_ts = IGB_N_EXTTS;
  932. adapter->ptp_caps.n_per_out = IGB_N_PEROUT;
  933. adapter->ptp_caps.n_pins = IGB_N_SDP;
  934. adapter->ptp_caps.pps = 1;
  935. adapter->ptp_caps.pin_config = adapter->sdp_config;
  936. adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
  937. adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
  938. adapter->ptp_caps.gettime64 = igb_ptp_gettime_i210;
  939. adapter->ptp_caps.settime64 = igb_ptp_settime_i210;
  940. adapter->ptp_caps.enable = igb_ptp_feature_enable_i210;
  941. adapter->ptp_caps.verify = igb_ptp_verify_pin;
  942. /* Enable the timer functions by clearing bit 31. */
  943. wr32(E1000_TSAUXC, 0x0);
  944. break;
  945. default:
  946. adapter->ptp_clock = NULL;
  947. return;
  948. }
  949. wrfl();
  950. spin_lock_init(&adapter->tmreg_lock);
  951. INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
  952. /* Initialize the clock and overflow work for devices that need it. */
  953. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  954. struct timespec64 ts = ktime_to_timespec64(ktime_get_real());
  955. igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
  956. } else {
  957. timecounter_init(&adapter->tc, &adapter->cc,
  958. ktime_to_ns(ktime_get_real()));
  959. INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
  960. igb_ptp_overflow_check);
  961. schedule_delayed_work(&adapter->ptp_overflow_work,
  962. IGB_SYSTIM_OVERFLOW_PERIOD);
  963. }
  964. /* Initialize the time sync interrupts for devices that support it. */
  965. if (hw->mac.type >= e1000_82580) {
  966. wr32(E1000_TSIM, TSYNC_INTERRUPTS);
  967. wr32(E1000_IMS, E1000_IMS_TS);
  968. }
  969. adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  970. adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  971. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
  972. &adapter->pdev->dev);
  973. if (IS_ERR(adapter->ptp_clock)) {
  974. adapter->ptp_clock = NULL;
  975. dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
  976. } else {
  977. dev_info(&adapter->pdev->dev, "added PHC on %s\n",
  978. adapter->netdev->name);
  979. adapter->flags |= IGB_FLAG_PTP;
  980. }
  981. }
  982. /**
  983. * igb_ptp_stop - Disable PTP device and stop the overflow check.
  984. * @adapter: Board private structure.
  985. *
  986. * This function stops the PTP support and cancels the delayed work.
  987. **/
  988. void igb_ptp_stop(struct igb_adapter *adapter)
  989. {
  990. switch (adapter->hw.mac.type) {
  991. case e1000_82576:
  992. case e1000_82580:
  993. case e1000_i354:
  994. case e1000_i350:
  995. cancel_delayed_work_sync(&adapter->ptp_overflow_work);
  996. break;
  997. case e1000_i210:
  998. case e1000_i211:
  999. /* No delayed work to cancel. */
  1000. break;
  1001. default:
  1002. return;
  1003. }
  1004. cancel_work_sync(&adapter->ptp_tx_work);
  1005. if (adapter->ptp_tx_skb) {
  1006. dev_kfree_skb_any(adapter->ptp_tx_skb);
  1007. adapter->ptp_tx_skb = NULL;
  1008. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  1009. }
  1010. if (adapter->ptp_clock) {
  1011. ptp_clock_unregister(adapter->ptp_clock);
  1012. dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
  1013. adapter->netdev->name);
  1014. adapter->flags &= ~IGB_FLAG_PTP;
  1015. }
  1016. }
  1017. /**
  1018. * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
  1019. * @adapter: Board private structure.
  1020. *
  1021. * This function handles the reset work required to re-enable the PTP device.
  1022. **/
  1023. void igb_ptp_reset(struct igb_adapter *adapter)
  1024. {
  1025. struct e1000_hw *hw = &adapter->hw;
  1026. unsigned long flags;
  1027. if (!(adapter->flags & IGB_FLAG_PTP))
  1028. return;
  1029. /* reset the tstamp_config */
  1030. igb_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
  1031. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  1032. switch (adapter->hw.mac.type) {
  1033. case e1000_82576:
  1034. /* Dial the nominal frequency. */
  1035. wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
  1036. break;
  1037. case e1000_82580:
  1038. case e1000_i354:
  1039. case e1000_i350:
  1040. case e1000_i210:
  1041. case e1000_i211:
  1042. wr32(E1000_TSAUXC, 0x0);
  1043. wr32(E1000_TSSDP, 0x0);
  1044. wr32(E1000_TSIM, TSYNC_INTERRUPTS);
  1045. wr32(E1000_IMS, E1000_IMS_TS);
  1046. break;
  1047. default:
  1048. /* No work to do. */
  1049. goto out;
  1050. }
  1051. /* Re-initialize the timer. */
  1052. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
  1053. struct timespec64 ts = ktime_to_timespec64(ktime_get_real());
  1054. igb_ptp_write_i210(adapter, &ts);
  1055. } else {
  1056. timecounter_init(&adapter->tc, &adapter->cc,
  1057. ktime_to_ns(ktime_get_real()));
  1058. }
  1059. out:
  1060. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  1061. }