fec_ptp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Fast Ethernet Controller (ENET) PTP driver for MX6x.
  4. *
  5. * Copyright (C) 2012 Freescale Semiconductor, Inc.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/errno.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/fec.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/of.h>
  22. #include <linux/of_net.h>
  23. #include <linux/pci.h>
  24. #include <linux/phy.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/string.h>
  31. #include <linux/workqueue.h>
  32. #include "fec.h"
  33. /* FEC 1588 register bits */
  34. #define FEC_T_CTRL_SLAVE 0x00002000
  35. #define FEC_T_CTRL_CAPTURE 0x00000800
  36. #define FEC_T_CTRL_RESTART 0x00000200
  37. #define FEC_T_CTRL_PERIOD_RST 0x00000030
  38. #define FEC_T_CTRL_PERIOD_EN 0x00000010
  39. #define FEC_T_CTRL_ENABLE 0x00000001
  40. #define FEC_T_INC_MASK 0x0000007f
  41. #define FEC_T_INC_OFFSET 0
  42. #define FEC_T_INC_CORR_MASK 0x00007f00
  43. #define FEC_T_INC_CORR_OFFSET 8
  44. #define FEC_T_CTRL_PINPER 0x00000080
  45. #define FEC_T_TF0_MASK 0x00000001
  46. #define FEC_T_TF0_OFFSET 0
  47. #define FEC_T_TF1_MASK 0x00000002
  48. #define FEC_T_TF1_OFFSET 1
  49. #define FEC_T_TF2_MASK 0x00000004
  50. #define FEC_T_TF2_OFFSET 2
  51. #define FEC_T_TF3_MASK 0x00000008
  52. #define FEC_T_TF3_OFFSET 3
  53. #define FEC_T_TDRE_MASK 0x00000001
  54. #define FEC_T_TDRE_OFFSET 0
  55. #define FEC_T_TMODE_MASK 0x0000003C
  56. #define FEC_T_TMODE_OFFSET 2
  57. #define FEC_T_TIE_MASK 0x00000040
  58. #define FEC_T_TIE_OFFSET 6
  59. #define FEC_T_TF_MASK 0x00000080
  60. #define FEC_T_TF_OFFSET 7
  61. #define FEC_ATIME_CTRL 0x400
  62. #define FEC_ATIME 0x404
  63. #define FEC_ATIME_EVT_OFFSET 0x408
  64. #define FEC_ATIME_EVT_PERIOD 0x40c
  65. #define FEC_ATIME_CORR 0x410
  66. #define FEC_ATIME_INC 0x414
  67. #define FEC_TS_TIMESTAMP 0x418
  68. #define FEC_TGSR 0x604
  69. #define FEC_TCSR(n) (0x608 + n * 0x08)
  70. #define FEC_TCCR(n) (0x60C + n * 0x08)
  71. #define MAX_TIMER_CHANNEL 3
  72. #define FEC_TMODE_TOGGLE 0x05
  73. #define FEC_HIGH_PULSE 0x0F
  74. #define FEC_CC_MULT (1 << 31)
  75. #define FEC_COUNTER_PERIOD (1 << 31)
  76. #define PPS_OUPUT_RELOAD_PERIOD NSEC_PER_SEC
  77. #define DEFAULT_PPS_CHANNEL 0
  78. #define FEC_PTP_MAX_NSEC_PERIOD 4000000000ULL
  79. #define FEC_PTP_MAX_NSEC_COUNTER 0x80000000ULL
  80. /**
  81. * fec_ptp_read - read raw cycle counter (to be used by time counter)
  82. * @cc: the cyclecounter structure
  83. *
  84. * this function reads the cyclecounter registers and is called by the
  85. * cyclecounter structure used to construct a ns counter from the
  86. * arbitrary fixed point registers
  87. */
  88. static u64 fec_ptp_read(struct cyclecounter *cc)
  89. {
  90. struct fec_enet_private *fep =
  91. container_of(cc, struct fec_enet_private, cc);
  92. u32 tempval;
  93. tempval = readl(fep->hwp + FEC_ATIME_CTRL);
  94. tempval |= FEC_T_CTRL_CAPTURE;
  95. writel(tempval, fep->hwp + FEC_ATIME_CTRL);
  96. if (fep->quirks & FEC_QUIRK_BUG_CAPTURE)
  97. udelay(1);
  98. return readl(fep->hwp + FEC_ATIME);
  99. }
  100. /**
  101. * fec_ptp_enable_pps
  102. * @fep: the fec_enet_private structure handle
  103. * @enable: enable the channel pps output
  104. *
  105. * This function enables the PPS output on the timer channel.
  106. */
  107. static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable)
  108. {
  109. unsigned long flags;
  110. u32 val, tempval;
  111. struct timespec64 ts;
  112. u64 ns;
  113. spin_lock_irqsave(&fep->tmreg_lock, flags);
  114. if (fep->perout_enable) {
  115. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  116. dev_err(&fep->pdev->dev, "PEROUT is running");
  117. return -EBUSY;
  118. }
  119. if (fep->pps_enable == enable) {
  120. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  121. return 0;
  122. }
  123. if (enable) {
  124. /* clear capture or output compare interrupt status if have.
  125. */
  126. writel(FEC_T_TF_MASK, fep->hwp + FEC_TCSR(fep->pps_channel));
  127. /* It is recommended to double check the TMODE field in the
  128. * TCSR register to be cleared before the first compare counter
  129. * is written into TCCR register. Just add a double check.
  130. */
  131. val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
  132. do {
  133. val &= ~(FEC_T_TMODE_MASK);
  134. writel(val, fep->hwp + FEC_TCSR(fep->pps_channel));
  135. val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
  136. } while (val & FEC_T_TMODE_MASK);
  137. /* Dummy read counter to update the counter */
  138. timecounter_read(&fep->tc);
  139. /* We want to find the first compare event in the next
  140. * second point. So we need to know what the ptp time
  141. * is now and how many nanoseconds is ahead to get next second.
  142. * The remaining nanosecond ahead before the next second would be
  143. * NSEC_PER_SEC - ts.tv_nsec. Add the remaining nanoseconds
  144. * to current timer would be next second.
  145. */
  146. tempval = fec_ptp_read(&fep->cc);
  147. /* Convert the ptp local counter to 1588 timestamp */
  148. ns = timecounter_cyc2time(&fep->tc, tempval);
  149. ts = ns_to_timespec64(ns);
  150. /* The tempval is less than 3 seconds, and so val is less than
  151. * 4 seconds. No overflow for 32bit calculation.
  152. */
  153. val = NSEC_PER_SEC - (u32)ts.tv_nsec + tempval;
  154. /* Need to consider the situation that the current time is
  155. * very close to the second point, which means NSEC_PER_SEC
  156. * - ts.tv_nsec is close to be zero(For example 20ns); Since the timer
  157. * is still running when we calculate the first compare event, it is
  158. * possible that the remaining nanoseconds run out before the compare
  159. * counter is calculated and written into TCCR register. To avoid
  160. * this possibility, we will set the compare event to be the next
  161. * of next second. The current setting is 31-bit timer and wrap
  162. * around over 2 seconds. So it is okay to set the next of next
  163. * seond for the timer.
  164. */
  165. val += NSEC_PER_SEC;
  166. /* We add (2 * NSEC_PER_SEC - (u32)ts.tv_nsec) to current
  167. * ptp counter, which maybe cause 32-bit wrap. Since the
  168. * (NSEC_PER_SEC - (u32)ts.tv_nsec) is less than 2 second.
  169. * We can ensure the wrap will not cause issue. If the offset
  170. * is bigger than fep->cc.mask would be a error.
  171. */
  172. val &= fep->cc.mask;
  173. writel(val, fep->hwp + FEC_TCCR(fep->pps_channel));
  174. /* Calculate the second the compare event timestamp */
  175. fep->next_counter = (val + fep->reload_period) & fep->cc.mask;
  176. /* * Enable compare event when overflow */
  177. val = readl(fep->hwp + FEC_ATIME_CTRL);
  178. val |= FEC_T_CTRL_PINPER;
  179. writel(val, fep->hwp + FEC_ATIME_CTRL);
  180. /* Compare channel setting. */
  181. val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
  182. val |= (1 << FEC_T_TF_OFFSET | 1 << FEC_T_TIE_OFFSET);
  183. val &= ~(1 << FEC_T_TDRE_OFFSET);
  184. val &= ~(FEC_T_TMODE_MASK);
  185. val |= (FEC_HIGH_PULSE << FEC_T_TMODE_OFFSET);
  186. writel(val, fep->hwp + FEC_TCSR(fep->pps_channel));
  187. /* Write the second compare event timestamp and calculate
  188. * the third timestamp. Refer the TCCR register detail in the spec.
  189. */
  190. writel(fep->next_counter, fep->hwp + FEC_TCCR(fep->pps_channel));
  191. fep->next_counter = (fep->next_counter + fep->reload_period) & fep->cc.mask;
  192. } else {
  193. writel(0, fep->hwp + FEC_TCSR(fep->pps_channel));
  194. }
  195. fep->pps_enable = enable;
  196. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  197. return 0;
  198. }
  199. static int fec_ptp_pps_perout(struct fec_enet_private *fep)
  200. {
  201. u32 compare_val, ptp_hc, temp_val;
  202. u64 curr_time;
  203. unsigned long flags;
  204. spin_lock_irqsave(&fep->tmreg_lock, flags);
  205. /* Update time counter */
  206. timecounter_read(&fep->tc);
  207. /* Get the current ptp hardware time counter */
  208. ptp_hc = fec_ptp_read(&fep->cc);
  209. /* Convert the ptp local counter to 1588 timestamp */
  210. curr_time = timecounter_cyc2time(&fep->tc, ptp_hc);
  211. /* If the pps start time less than current time add 100ms, just return.
  212. * Because the software might not able to set the comparison time into
  213. * the FEC_TCCR register in time and missed the start time.
  214. */
  215. if (fep->perout_stime < curr_time + 100 * NSEC_PER_MSEC) {
  216. fep->perout_enable = false;
  217. dev_err(&fep->pdev->dev, "Current time is too close to the start time!\n");
  218. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  219. return -1;
  220. }
  221. compare_val = fep->perout_stime - curr_time + ptp_hc;
  222. compare_val &= fep->cc.mask;
  223. writel(compare_val, fep->hwp + FEC_TCCR(fep->pps_channel));
  224. fep->next_counter = (compare_val + fep->reload_period) & fep->cc.mask;
  225. /* Enable compare event when overflow */
  226. temp_val = readl(fep->hwp + FEC_ATIME_CTRL);
  227. temp_val |= FEC_T_CTRL_PINPER;
  228. writel(temp_val, fep->hwp + FEC_ATIME_CTRL);
  229. /* Compare channel setting. */
  230. temp_val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
  231. temp_val |= (1 << FEC_T_TF_OFFSET | 1 << FEC_T_TIE_OFFSET);
  232. temp_val &= ~(1 << FEC_T_TDRE_OFFSET);
  233. temp_val &= ~(FEC_T_TMODE_MASK);
  234. temp_val |= (FEC_TMODE_TOGGLE << FEC_T_TMODE_OFFSET);
  235. writel(temp_val, fep->hwp + FEC_TCSR(fep->pps_channel));
  236. /* Write the second compare event timestamp and calculate
  237. * the third timestamp. Refer the TCCR register detail in the spec.
  238. */
  239. writel(fep->next_counter, fep->hwp + FEC_TCCR(fep->pps_channel));
  240. fep->next_counter = (fep->next_counter + fep->reload_period) & fep->cc.mask;
  241. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  242. return 0;
  243. }
  244. static enum hrtimer_restart fec_ptp_pps_perout_handler(struct hrtimer *timer)
  245. {
  246. struct fec_enet_private *fep = container_of(timer,
  247. struct fec_enet_private, perout_timer);
  248. fec_ptp_pps_perout(fep);
  249. return HRTIMER_NORESTART;
  250. }
  251. /**
  252. * fec_ptp_start_cyclecounter - create the cycle counter from hw
  253. * @ndev: network device
  254. *
  255. * this function initializes the timecounter and cyclecounter
  256. * structures for use in generated a ns counter from the arbitrary
  257. * fixed point cycles registers in the hardware.
  258. */
  259. void fec_ptp_start_cyclecounter(struct net_device *ndev)
  260. {
  261. struct fec_enet_private *fep = netdev_priv(ndev);
  262. unsigned long flags;
  263. int inc;
  264. inc = 1000000000 / fep->cycle_speed;
  265. /* grab the ptp lock */
  266. spin_lock_irqsave(&fep->tmreg_lock, flags);
  267. /* 1ns counter */
  268. writel(inc << FEC_T_INC_OFFSET, fep->hwp + FEC_ATIME_INC);
  269. /* use 31-bit timer counter */
  270. writel(FEC_COUNTER_PERIOD, fep->hwp + FEC_ATIME_EVT_PERIOD);
  271. writel(FEC_T_CTRL_ENABLE | FEC_T_CTRL_PERIOD_RST,
  272. fep->hwp + FEC_ATIME_CTRL);
  273. memset(&fep->cc, 0, sizeof(fep->cc));
  274. fep->cc.read = fec_ptp_read;
  275. fep->cc.mask = CLOCKSOURCE_MASK(31);
  276. fep->cc.shift = 31;
  277. fep->cc.mult = FEC_CC_MULT;
  278. /* reset the ns time counter */
  279. timecounter_init(&fep->tc, &fep->cc, 0);
  280. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  281. }
  282. /**
  283. * fec_ptp_adjfine - adjust ptp cycle frequency
  284. * @ptp: the ptp clock structure
  285. * @scaled_ppm: scaled parts per million adjustment from base
  286. *
  287. * Adjust the frequency of the ptp cycle counter by the
  288. * indicated amount from the base frequency.
  289. *
  290. * Scaled parts per million is ppm with a 16-bit binary fractional field.
  291. *
  292. * Because ENET hardware frequency adjust is complex,
  293. * using software method to do that.
  294. */
  295. static int fec_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  296. {
  297. s32 ppb = scaled_ppm_to_ppb(scaled_ppm);
  298. unsigned long flags;
  299. int neg_adj = 0;
  300. u32 i, tmp;
  301. u32 corr_inc, corr_period;
  302. u32 corr_ns;
  303. u64 lhs, rhs;
  304. struct fec_enet_private *fep =
  305. container_of(ptp, struct fec_enet_private, ptp_caps);
  306. if (ppb == 0)
  307. return 0;
  308. if (ppb < 0) {
  309. ppb = -ppb;
  310. neg_adj = 1;
  311. }
  312. /* In theory, corr_inc/corr_period = ppb/NSEC_PER_SEC;
  313. * Try to find the corr_inc between 1 to fep->ptp_inc to
  314. * meet adjustment requirement.
  315. */
  316. lhs = NSEC_PER_SEC;
  317. rhs = (u64)ppb * (u64)fep->ptp_inc;
  318. for (i = 1; i <= fep->ptp_inc; i++) {
  319. if (lhs >= rhs) {
  320. corr_inc = i;
  321. corr_period = div_u64(lhs, rhs);
  322. break;
  323. }
  324. lhs += NSEC_PER_SEC;
  325. }
  326. /* Not found? Set it to high value - double speed
  327. * correct in every clock step.
  328. */
  329. if (i > fep->ptp_inc) {
  330. corr_inc = fep->ptp_inc;
  331. corr_period = 1;
  332. }
  333. if (neg_adj)
  334. corr_ns = fep->ptp_inc - corr_inc;
  335. else
  336. corr_ns = fep->ptp_inc + corr_inc;
  337. spin_lock_irqsave(&fep->tmreg_lock, flags);
  338. tmp = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_MASK;
  339. tmp |= corr_ns << FEC_T_INC_CORR_OFFSET;
  340. writel(tmp, fep->hwp + FEC_ATIME_INC);
  341. corr_period = corr_period > 1 ? corr_period - 1 : corr_period;
  342. writel(corr_period, fep->hwp + FEC_ATIME_CORR);
  343. /* dummy read to update the timer. */
  344. timecounter_read(&fep->tc);
  345. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  346. return 0;
  347. }
  348. /**
  349. * fec_ptp_adjtime
  350. * @ptp: the ptp clock structure
  351. * @delta: offset to adjust the cycle counter by
  352. *
  353. * adjust the timer by resetting the timecounter structure.
  354. */
  355. static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  356. {
  357. struct fec_enet_private *fep =
  358. container_of(ptp, struct fec_enet_private, ptp_caps);
  359. unsigned long flags;
  360. spin_lock_irqsave(&fep->tmreg_lock, flags);
  361. timecounter_adjtime(&fep->tc, delta);
  362. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  363. return 0;
  364. }
  365. /**
  366. * fec_ptp_gettime
  367. * @ptp: the ptp clock structure
  368. * @ts: timespec structure to hold the current time value
  369. *
  370. * read the timecounter and return the correct value on ns,
  371. * after converting it into a struct timespec.
  372. */
  373. static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  374. {
  375. struct fec_enet_private *fep =
  376. container_of(ptp, struct fec_enet_private, ptp_caps);
  377. u64 ns;
  378. unsigned long flags;
  379. mutex_lock(&fep->ptp_clk_mutex);
  380. /* Check the ptp clock */
  381. if (!fep->ptp_clk_on) {
  382. mutex_unlock(&fep->ptp_clk_mutex);
  383. return -EINVAL;
  384. }
  385. spin_lock_irqsave(&fep->tmreg_lock, flags);
  386. ns = timecounter_read(&fep->tc);
  387. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  388. mutex_unlock(&fep->ptp_clk_mutex);
  389. *ts = ns_to_timespec64(ns);
  390. return 0;
  391. }
  392. /**
  393. * fec_ptp_settime
  394. * @ptp: the ptp clock structure
  395. * @ts: the timespec containing the new time for the cycle counter
  396. *
  397. * reset the timecounter to use a new base value instead of the kernel
  398. * wall timer value.
  399. */
  400. static int fec_ptp_settime(struct ptp_clock_info *ptp,
  401. const struct timespec64 *ts)
  402. {
  403. struct fec_enet_private *fep =
  404. container_of(ptp, struct fec_enet_private, ptp_caps);
  405. u64 ns;
  406. unsigned long flags;
  407. u32 counter;
  408. mutex_lock(&fep->ptp_clk_mutex);
  409. /* Check the ptp clock */
  410. if (!fep->ptp_clk_on) {
  411. mutex_unlock(&fep->ptp_clk_mutex);
  412. return -EINVAL;
  413. }
  414. ns = timespec64_to_ns(ts);
  415. /* Get the timer value based on timestamp.
  416. * Update the counter with the masked value.
  417. */
  418. counter = ns & fep->cc.mask;
  419. spin_lock_irqsave(&fep->tmreg_lock, flags);
  420. writel(counter, fep->hwp + FEC_ATIME);
  421. timecounter_init(&fep->tc, &fep->cc, ns);
  422. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  423. mutex_unlock(&fep->ptp_clk_mutex);
  424. return 0;
  425. }
  426. static int fec_ptp_pps_disable(struct fec_enet_private *fep, uint channel)
  427. {
  428. unsigned long flags;
  429. hrtimer_cancel(&fep->perout_timer);
  430. spin_lock_irqsave(&fep->tmreg_lock, flags);
  431. fep->perout_enable = false;
  432. writel(0, fep->hwp + FEC_TCSR(channel));
  433. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  434. return 0;
  435. }
  436. /**
  437. * fec_ptp_enable
  438. * @ptp: the ptp clock structure
  439. * @rq: the requested feature to change
  440. * @on: whether to enable or disable the feature
  441. *
  442. */
  443. static int fec_ptp_enable(struct ptp_clock_info *ptp,
  444. struct ptp_clock_request *rq, int on)
  445. {
  446. struct fec_enet_private *fep =
  447. container_of(ptp, struct fec_enet_private, ptp_caps);
  448. ktime_t timeout;
  449. struct timespec64 start_time, period;
  450. u64 curr_time, delta, period_ns;
  451. unsigned long flags;
  452. int ret = 0;
  453. if (rq->type == PTP_CLK_REQ_PPS) {
  454. fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
  455. ret = fec_ptp_enable_pps(fep, on);
  456. return ret;
  457. } else if (rq->type == PTP_CLK_REQ_PEROUT) {
  458. u32 reload_period;
  459. /* Reject requests with unsupported flags */
  460. if (rq->perout.flags)
  461. return -EOPNOTSUPP;
  462. period.tv_sec = rq->perout.period.sec;
  463. period.tv_nsec = rq->perout.period.nsec;
  464. period_ns = timespec64_to_ns(&period);
  465. /* FEC PTP timer only has 31 bits, so if the period exceed
  466. * 4s is not supported.
  467. */
  468. if (period_ns > FEC_PTP_MAX_NSEC_PERIOD) {
  469. dev_err(&fep->pdev->dev, "The period must equal to or less than 4s!\n");
  470. return -EOPNOTSUPP;
  471. }
  472. reload_period = div_u64(period_ns, 2);
  473. if (on && reload_period) {
  474. u64 perout_stime;
  475. /* Convert 1588 timestamp to ns*/
  476. start_time.tv_sec = rq->perout.start.sec;
  477. start_time.tv_nsec = rq->perout.start.nsec;
  478. perout_stime = timespec64_to_ns(&start_time);
  479. mutex_lock(&fep->ptp_clk_mutex);
  480. if (!fep->ptp_clk_on) {
  481. dev_err(&fep->pdev->dev, "Error: PTP clock is closed!\n");
  482. mutex_unlock(&fep->ptp_clk_mutex);
  483. return -EOPNOTSUPP;
  484. }
  485. spin_lock_irqsave(&fep->tmreg_lock, flags);
  486. if (fep->pps_enable) {
  487. dev_err(&fep->pdev->dev, "PPS is running");
  488. ret = -EBUSY;
  489. goto unlock;
  490. }
  491. if (fep->perout_enable) {
  492. dev_err(&fep->pdev->dev,
  493. "PEROUT has been enabled\n");
  494. ret = -EBUSY;
  495. goto unlock;
  496. }
  497. /* Read current timestamp */
  498. curr_time = timecounter_read(&fep->tc);
  499. if (perout_stime <= curr_time) {
  500. dev_err(&fep->pdev->dev,
  501. "Start time must be greater than current time\n");
  502. ret = -EINVAL;
  503. goto unlock;
  504. }
  505. /* Calculate time difference */
  506. delta = perout_stime - curr_time;
  507. fep->reload_period = reload_period;
  508. fep->perout_stime = perout_stime;
  509. fep->perout_enable = true;
  510. unlock:
  511. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  512. mutex_unlock(&fep->ptp_clk_mutex);
  513. if (ret)
  514. return ret;
  515. /* Because the timer counter of FEC only has 31-bits, correspondingly,
  516. * the time comparison register FEC_TCCR also only low 31 bits can be
  517. * set. If the start time of pps signal exceeds current time more than
  518. * 0x80000000 ns, a software timer is used and the timer expires about
  519. * 1 second before the start time to be able to set FEC_TCCR.
  520. */
  521. if (delta > FEC_PTP_MAX_NSEC_COUNTER) {
  522. timeout = ns_to_ktime(delta - NSEC_PER_SEC);
  523. hrtimer_start(&fep->perout_timer, timeout, HRTIMER_MODE_REL);
  524. } else {
  525. return fec_ptp_pps_perout(fep);
  526. }
  527. } else {
  528. fec_ptp_pps_disable(fep, fep->pps_channel);
  529. }
  530. return 0;
  531. } else {
  532. return -EOPNOTSUPP;
  533. }
  534. }
  535. int fec_ptp_set(struct net_device *ndev, struct kernel_hwtstamp_config *config,
  536. struct netlink_ext_ack *extack)
  537. {
  538. struct fec_enet_private *fep = netdev_priv(ndev);
  539. switch (config->tx_type) {
  540. case HWTSTAMP_TX_OFF:
  541. fep->hwts_tx_en = 0;
  542. break;
  543. case HWTSTAMP_TX_ON:
  544. fep->hwts_tx_en = 1;
  545. break;
  546. default:
  547. return -ERANGE;
  548. }
  549. switch (config->rx_filter) {
  550. case HWTSTAMP_FILTER_NONE:
  551. fep->hwts_rx_en = 0;
  552. break;
  553. default:
  554. fep->hwts_rx_en = 1;
  555. config->rx_filter = HWTSTAMP_FILTER_ALL;
  556. break;
  557. }
  558. return 0;
  559. }
  560. void fec_ptp_get(struct net_device *ndev, struct kernel_hwtstamp_config *config)
  561. {
  562. struct fec_enet_private *fep = netdev_priv(ndev);
  563. config->flags = 0;
  564. config->tx_type = fep->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  565. config->rx_filter = (fep->hwts_rx_en ?
  566. HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
  567. }
  568. /*
  569. * fec_time_keep - call timecounter_read every second to avoid timer overrun
  570. * because ENET just support 32bit counter, will timeout in 4s
  571. */
  572. static void fec_time_keep(struct work_struct *work)
  573. {
  574. struct delayed_work *dwork = to_delayed_work(work);
  575. struct fec_enet_private *fep = container_of(dwork, struct fec_enet_private, time_keep);
  576. unsigned long flags;
  577. mutex_lock(&fep->ptp_clk_mutex);
  578. if (fep->ptp_clk_on) {
  579. spin_lock_irqsave(&fep->tmreg_lock, flags);
  580. timecounter_read(&fep->tc);
  581. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  582. }
  583. mutex_unlock(&fep->ptp_clk_mutex);
  584. schedule_delayed_work(&fep->time_keep, HZ);
  585. }
  586. /* This function checks the pps event and reloads the timer compare counter. */
  587. static irqreturn_t fec_pps_interrupt(int irq, void *dev_id)
  588. {
  589. struct net_device *ndev = dev_id;
  590. struct fec_enet_private *fep = netdev_priv(ndev);
  591. u32 val;
  592. u8 channel = fep->pps_channel;
  593. struct ptp_clock_event event;
  594. val = readl(fep->hwp + FEC_TCSR(channel));
  595. if (val & FEC_T_TF_MASK) {
  596. /* Write the next next compare(not the next according the spec)
  597. * value to the register
  598. */
  599. writel(fep->next_counter, fep->hwp + FEC_TCCR(channel));
  600. do {
  601. writel(val, fep->hwp + FEC_TCSR(channel));
  602. } while (readl(fep->hwp + FEC_TCSR(channel)) & FEC_T_TF_MASK);
  603. /* Update the counter; */
  604. fep->next_counter = (fep->next_counter + fep->reload_period) &
  605. fep->cc.mask;
  606. if (fep->pps_enable) {
  607. event.type = PTP_CLOCK_PPS;
  608. ptp_clock_event(fep->ptp_clock, &event);
  609. }
  610. return IRQ_HANDLED;
  611. }
  612. return IRQ_NONE;
  613. }
  614. /**
  615. * fec_ptp_init
  616. * @pdev: The FEC network adapter
  617. * @irq_idx: the interrupt index
  618. *
  619. * This function performs the required steps for enabling ptp
  620. * support. If ptp support has already been loaded it simply calls the
  621. * cyclecounter init routine and exits.
  622. */
  623. void fec_ptp_init(struct platform_device *pdev, int irq_idx)
  624. {
  625. struct net_device *ndev = platform_get_drvdata(pdev);
  626. struct fec_enet_private *fep = netdev_priv(ndev);
  627. struct device_node *np = fep->pdev->dev.of_node;
  628. int irq;
  629. int ret;
  630. fep->ptp_caps.owner = THIS_MODULE;
  631. strscpy(fep->ptp_caps.name, "fec ptp", sizeof(fep->ptp_caps.name));
  632. fep->pps_channel = DEFAULT_PPS_CHANNEL;
  633. of_property_read_u32(np, "fsl,pps-channel", &fep->pps_channel);
  634. fep->ptp_caps.max_adj = 250000000;
  635. fep->ptp_caps.n_alarm = 0;
  636. fep->ptp_caps.n_ext_ts = 0;
  637. fep->ptp_caps.n_per_out = 1;
  638. fep->ptp_caps.n_pins = 0;
  639. fep->ptp_caps.pps = 1;
  640. fep->ptp_caps.adjfine = fec_ptp_adjfine;
  641. fep->ptp_caps.adjtime = fec_ptp_adjtime;
  642. fep->ptp_caps.gettime64 = fec_ptp_gettime;
  643. fep->ptp_caps.settime64 = fec_ptp_settime;
  644. fep->ptp_caps.enable = fec_ptp_enable;
  645. fep->cycle_speed = clk_get_rate(fep->clk_ptp);
  646. if (!fep->cycle_speed) {
  647. fep->cycle_speed = NSEC_PER_SEC;
  648. dev_err(&fep->pdev->dev, "clk_ptp clock rate is zero\n");
  649. }
  650. fep->ptp_inc = NSEC_PER_SEC / fep->cycle_speed;
  651. spin_lock_init(&fep->tmreg_lock);
  652. fec_ptp_start_cyclecounter(ndev);
  653. INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep);
  654. hrtimer_setup(&fep->perout_timer, fec_ptp_pps_perout_handler, CLOCK_REALTIME,
  655. HRTIMER_MODE_REL);
  656. irq = platform_get_irq_byname_optional(pdev, "pps");
  657. if (irq < 0)
  658. irq = platform_get_irq_optional(pdev, irq_idx);
  659. /* Failure to get an irq is not fatal,
  660. * only the PTP_CLOCK_PPS clock events should stop
  661. */
  662. if (irq >= 0) {
  663. ret = devm_request_irq(&pdev->dev, irq, fec_pps_interrupt,
  664. 0, pdev->name, ndev);
  665. if (ret < 0)
  666. dev_warn(&pdev->dev, "request for pps irq failed(%d)\n",
  667. ret);
  668. }
  669. fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
  670. if (IS_ERR(fep->ptp_clock)) {
  671. fep->ptp_clock = NULL;
  672. dev_err(&pdev->dev, "ptp_clock_register failed\n");
  673. }
  674. schedule_delayed_work(&fep->time_keep, HZ);
  675. }
  676. void fec_ptp_save_state(struct fec_enet_private *fep)
  677. {
  678. unsigned long flags;
  679. u32 atime_inc_corr;
  680. spin_lock_irqsave(&fep->tmreg_lock, flags);
  681. fep->ptp_saved_state.pps_enable = fep->pps_enable;
  682. fep->ptp_saved_state.ns_phc = timecounter_read(&fep->tc);
  683. fep->ptp_saved_state.ns_sys = ktime_get_ns();
  684. fep->ptp_saved_state.at_corr = readl(fep->hwp + FEC_ATIME_CORR);
  685. atime_inc_corr = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_CORR_MASK;
  686. fep->ptp_saved_state.at_inc_corr = (u8)(atime_inc_corr >> FEC_T_INC_CORR_OFFSET);
  687. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  688. }
  689. /* Restore PTP functionality after a reset */
  690. void fec_ptp_restore_state(struct fec_enet_private *fep)
  691. {
  692. u32 atime_inc = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_MASK;
  693. unsigned long flags;
  694. u32 counter;
  695. u64 ns;
  696. spin_lock_irqsave(&fep->tmreg_lock, flags);
  697. /* Reset turned it off, so adjust our status flag */
  698. fep->pps_enable = 0;
  699. writel(fep->ptp_saved_state.at_corr, fep->hwp + FEC_ATIME_CORR);
  700. atime_inc |= ((u32)fep->ptp_saved_state.at_inc_corr) << FEC_T_INC_CORR_OFFSET;
  701. writel(atime_inc, fep->hwp + FEC_ATIME_INC);
  702. ns = ktime_get_ns() - fep->ptp_saved_state.ns_sys + fep->ptp_saved_state.ns_phc;
  703. counter = ns & fep->cc.mask;
  704. writel(counter, fep->hwp + FEC_ATIME);
  705. timecounter_init(&fep->tc, &fep->cc, ns);
  706. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  707. /* Restart PPS if needed */
  708. if (fep->ptp_saved_state.pps_enable) {
  709. /* Re-enable PPS */
  710. fec_ptp_enable_pps(fep, 1);
  711. }
  712. }
  713. void fec_ptp_stop(struct platform_device *pdev)
  714. {
  715. struct net_device *ndev = platform_get_drvdata(pdev);
  716. struct fec_enet_private *fep = netdev_priv(ndev);
  717. if (fep->pps_enable)
  718. fec_ptp_enable_pps(fep, 0);
  719. cancel_delayed_work_sync(&fep->time_keep);
  720. hrtimer_cancel(&fep->perout_timer);
  721. if (fep->ptp_clock)
  722. ptp_clock_unregister(fep->ptp_clock);
  723. }