fec_ptp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/errno.h>
  13. #include <linux/ioport.h>
  14. #include <linux/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/bitops.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/clk.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/phy.h>
  29. #include <linux/fec.h>
  30. #include <linux/of.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/of_net.h>
  34. #include "fec.h"
  35. /* FEC 1588 register bits */
  36. #define FEC_T_CTRL_SLAVE 0x00002000
  37. #define FEC_T_CTRL_CAPTURE 0x00000800
  38. #define FEC_T_CTRL_RESTART 0x00000200
  39. #define FEC_T_CTRL_PERIOD_RST 0x00000030
  40. #define FEC_T_CTRL_PERIOD_EN 0x00000010
  41. #define FEC_T_CTRL_ENABLE 0x00000001
  42. #define FEC_T_INC_MASK 0x0000007f
  43. #define FEC_T_INC_OFFSET 0
  44. #define FEC_T_INC_CORR_MASK 0x00007f00
  45. #define FEC_T_INC_CORR_OFFSET 8
  46. #define FEC_T_CTRL_PINPER 0x00000080
  47. #define FEC_T_TF0_MASK 0x00000001
  48. #define FEC_T_TF0_OFFSET 0
  49. #define FEC_T_TF1_MASK 0x00000002
  50. #define FEC_T_TF1_OFFSET 1
  51. #define FEC_T_TF2_MASK 0x00000004
  52. #define FEC_T_TF2_OFFSET 2
  53. #define FEC_T_TF3_MASK 0x00000008
  54. #define FEC_T_TF3_OFFSET 3
  55. #define FEC_T_TDRE_MASK 0x00000001
  56. #define FEC_T_TDRE_OFFSET 0
  57. #define FEC_T_TMODE_MASK 0x0000003C
  58. #define FEC_T_TMODE_OFFSET 2
  59. #define FEC_T_TIE_MASK 0x00000040
  60. #define FEC_T_TIE_OFFSET 6
  61. #define FEC_T_TF_MASK 0x00000080
  62. #define FEC_T_TF_OFFSET 7
  63. #define FEC_ATIME_CTRL 0x400
  64. #define FEC_ATIME 0x404
  65. #define FEC_ATIME_EVT_OFFSET 0x408
  66. #define FEC_ATIME_EVT_PERIOD 0x40c
  67. #define FEC_ATIME_CORR 0x410
  68. #define FEC_ATIME_INC 0x414
  69. #define FEC_TS_TIMESTAMP 0x418
  70. #define FEC_TGSR 0x604
  71. #define FEC_TCSR(n) (0x608 + n * 0x08)
  72. #define FEC_TCCR(n) (0x60C + n * 0x08)
  73. #define MAX_TIMER_CHANNEL 3
  74. #define FEC_TMODE_TOGGLE 0x05
  75. #define FEC_HIGH_PULSE 0x0F
  76. #define FEC_CC_MULT (1 << 31)
  77. #define FEC_COUNTER_PERIOD (1 << 31)
  78. #define PPS_OUPUT_RELOAD_PERIOD NSEC_PER_SEC
  79. #define FEC_CHANNLE_0 0
  80. #define DEFAULT_PPS_CHANNEL FEC_CHANNLE_0
  81. /**
  82. * fec_ptp_enable_pps
  83. * @fep: the fec_enet_private structure handle
  84. * @enable: enable the channel pps output
  85. *
  86. * This function enble the PPS ouput on the timer channel.
  87. */
  88. static int fec_ptp_enable_pps(struct fec_enet_private *fep, uint enable)
  89. {
  90. unsigned long flags;
  91. u32 val, tempval;
  92. struct timespec64 ts;
  93. u64 ns;
  94. val = 0;
  95. if (!(fep->hwts_tx_en || fep->hwts_rx_en)) {
  96. dev_err(&fep->pdev->dev, "No ptp stack is running\n");
  97. return -EINVAL;
  98. }
  99. if (fep->pps_enable == enable)
  100. return 0;
  101. fep->pps_channel = DEFAULT_PPS_CHANNEL;
  102. fep->reload_period = PPS_OUPUT_RELOAD_PERIOD;
  103. spin_lock_irqsave(&fep->tmreg_lock, flags);
  104. if (enable) {
  105. /* clear capture or output compare interrupt status if have.
  106. */
  107. writel(FEC_T_TF_MASK, fep->hwp + FEC_TCSR(fep->pps_channel));
  108. /* It is recommended to double check the TMODE field in the
  109. * TCSR register to be cleared before the first compare counter
  110. * is written into TCCR register. Just add a double check.
  111. */
  112. val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
  113. do {
  114. val &= ~(FEC_T_TMODE_MASK);
  115. writel(val, fep->hwp + FEC_TCSR(fep->pps_channel));
  116. val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
  117. } while (val & FEC_T_TMODE_MASK);
  118. /* Dummy read counter to update the counter */
  119. timecounter_read(&fep->tc);
  120. /* We want to find the first compare event in the next
  121. * second point. So we need to know what the ptp time
  122. * is now and how many nanoseconds is ahead to get next second.
  123. * The remaining nanosecond ahead before the next second would be
  124. * NSEC_PER_SEC - ts.tv_nsec. Add the remaining nanoseconds
  125. * to current timer would be next second.
  126. */
  127. tempval = readl(fep->hwp + FEC_ATIME_CTRL);
  128. tempval |= FEC_T_CTRL_CAPTURE;
  129. writel(tempval, fep->hwp + FEC_ATIME_CTRL);
  130. tempval = readl(fep->hwp + FEC_ATIME);
  131. /* Convert the ptp local counter to 1588 timestamp */
  132. ns = timecounter_cyc2time(&fep->tc, tempval);
  133. ts = ns_to_timespec64(ns);
  134. /* The tempval is less than 3 seconds, and so val is less than
  135. * 4 seconds. No overflow for 32bit calculation.
  136. */
  137. val = NSEC_PER_SEC - (u32)ts.tv_nsec + tempval;
  138. /* Need to consider the situation that the current time is
  139. * very close to the second point, which means NSEC_PER_SEC
  140. * - ts.tv_nsec is close to be zero(For example 20ns); Since the timer
  141. * is still running when we calculate the first compare event, it is
  142. * possible that the remaining nanoseonds run out before the compare
  143. * counter is calculated and written into TCCR register. To avoid
  144. * this possibility, we will set the compare event to be the next
  145. * of next second. The current setting is 31-bit timer and wrap
  146. * around over 2 seconds. So it is okay to set the next of next
  147. * seond for the timer.
  148. */
  149. val += NSEC_PER_SEC;
  150. /* We add (2 * NSEC_PER_SEC - (u32)ts.tv_nsec) to current
  151. * ptp counter, which maybe cause 32-bit wrap. Since the
  152. * (NSEC_PER_SEC - (u32)ts.tv_nsec) is less than 2 second.
  153. * We can ensure the wrap will not cause issue. If the offset
  154. * is bigger than fep->cc.mask would be a error.
  155. */
  156. val &= fep->cc.mask;
  157. writel(val, fep->hwp + FEC_TCCR(fep->pps_channel));
  158. /* Calculate the second the compare event timestamp */
  159. fep->next_counter = (val + fep->reload_period) & fep->cc.mask;
  160. /* * Enable compare event when overflow */
  161. val = readl(fep->hwp + FEC_ATIME_CTRL);
  162. val |= FEC_T_CTRL_PINPER;
  163. writel(val, fep->hwp + FEC_ATIME_CTRL);
  164. /* Compare channel setting. */
  165. val = readl(fep->hwp + FEC_TCSR(fep->pps_channel));
  166. val |= (1 << FEC_T_TF_OFFSET | 1 << FEC_T_TIE_OFFSET);
  167. val &= ~(1 << FEC_T_TDRE_OFFSET);
  168. val &= ~(FEC_T_TMODE_MASK);
  169. val |= (FEC_HIGH_PULSE << FEC_T_TMODE_OFFSET);
  170. writel(val, fep->hwp + FEC_TCSR(fep->pps_channel));
  171. /* Write the second compare event timestamp and calculate
  172. * the third timestamp. Refer the TCCR register detail in the spec.
  173. */
  174. writel(fep->next_counter, fep->hwp + FEC_TCCR(fep->pps_channel));
  175. fep->next_counter = (fep->next_counter + fep->reload_period) & fep->cc.mask;
  176. } else {
  177. writel(0, fep->hwp + FEC_TCSR(fep->pps_channel));
  178. }
  179. fep->pps_enable = enable;
  180. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  181. return 0;
  182. }
  183. /**
  184. * fec_ptp_read - read raw cycle counter (to be used by time counter)
  185. * @cc: the cyclecounter structure
  186. *
  187. * this function reads the cyclecounter registers and is called by the
  188. * cyclecounter structure used to construct a ns counter from the
  189. * arbitrary fixed point registers
  190. */
  191. static u64 fec_ptp_read(const struct cyclecounter *cc)
  192. {
  193. struct fec_enet_private *fep =
  194. container_of(cc, struct fec_enet_private, cc);
  195. const struct platform_device_id *id_entry =
  196. platform_get_device_id(fep->pdev);
  197. u32 tempval;
  198. tempval = readl(fep->hwp + FEC_ATIME_CTRL);
  199. tempval |= FEC_T_CTRL_CAPTURE;
  200. writel(tempval, fep->hwp + FEC_ATIME_CTRL);
  201. if (id_entry->driver_data & FEC_QUIRK_BUG_CAPTURE)
  202. udelay(1);
  203. return readl(fep->hwp + FEC_ATIME);
  204. }
  205. /**
  206. * fec_ptp_start_cyclecounter - create the cycle counter from hw
  207. * @ndev: network device
  208. *
  209. * this function initializes the timecounter and cyclecounter
  210. * structures for use in generated a ns counter from the arbitrary
  211. * fixed point cycles registers in the hardware.
  212. */
  213. void fec_ptp_start_cyclecounter(struct net_device *ndev)
  214. {
  215. struct fec_enet_private *fep = netdev_priv(ndev);
  216. unsigned long flags;
  217. int inc;
  218. inc = 1000000000 / fep->cycle_speed;
  219. /* grab the ptp lock */
  220. spin_lock_irqsave(&fep->tmreg_lock, flags);
  221. /* 1ns counter */
  222. writel(inc << FEC_T_INC_OFFSET, fep->hwp + FEC_ATIME_INC);
  223. /* use 31-bit timer counter */
  224. writel(FEC_COUNTER_PERIOD, fep->hwp + FEC_ATIME_EVT_PERIOD);
  225. writel(FEC_T_CTRL_ENABLE | FEC_T_CTRL_PERIOD_RST,
  226. fep->hwp + FEC_ATIME_CTRL);
  227. memset(&fep->cc, 0, sizeof(fep->cc));
  228. fep->cc.read = fec_ptp_read;
  229. fep->cc.mask = CLOCKSOURCE_MASK(31);
  230. fep->cc.shift = 31;
  231. fep->cc.mult = FEC_CC_MULT;
  232. /* reset the ns time counter */
  233. timecounter_init(&fep->tc, &fep->cc, ktime_to_ns(ktime_get_real()));
  234. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  235. }
  236. /**
  237. * fec_ptp_adjfreq - adjust ptp cycle frequency
  238. * @ptp: the ptp clock structure
  239. * @ppb: parts per billion adjustment from base
  240. *
  241. * Adjust the frequency of the ptp cycle counter by the
  242. * indicated ppb from the base frequency.
  243. *
  244. * Because ENET hardware frequency adjust is complex,
  245. * using software method to do that.
  246. */
  247. static int fec_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  248. {
  249. unsigned long flags;
  250. int neg_adj = 0;
  251. u32 i, tmp;
  252. u32 corr_inc, corr_period;
  253. u32 corr_ns;
  254. u64 lhs, rhs;
  255. struct fec_enet_private *fep =
  256. container_of(ptp, struct fec_enet_private, ptp_caps);
  257. if (ppb == 0)
  258. return 0;
  259. if (ppb < 0) {
  260. ppb = -ppb;
  261. neg_adj = 1;
  262. }
  263. /* In theory, corr_inc/corr_period = ppb/NSEC_PER_SEC;
  264. * Try to find the corr_inc between 1 to fep->ptp_inc to
  265. * meet adjustment requirement.
  266. */
  267. lhs = NSEC_PER_SEC;
  268. rhs = (u64)ppb * (u64)fep->ptp_inc;
  269. for (i = 1; i <= fep->ptp_inc; i++) {
  270. if (lhs >= rhs) {
  271. corr_inc = i;
  272. corr_period = div_u64(lhs, rhs);
  273. break;
  274. }
  275. lhs += NSEC_PER_SEC;
  276. }
  277. /* Not found? Set it to high value - double speed
  278. * correct in every clock step.
  279. */
  280. if (i > fep->ptp_inc) {
  281. corr_inc = fep->ptp_inc;
  282. corr_period = 1;
  283. }
  284. if (neg_adj)
  285. corr_ns = fep->ptp_inc - corr_inc;
  286. else
  287. corr_ns = fep->ptp_inc + corr_inc;
  288. spin_lock_irqsave(&fep->tmreg_lock, flags);
  289. tmp = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_MASK;
  290. tmp |= corr_ns << FEC_T_INC_CORR_OFFSET;
  291. writel(tmp, fep->hwp + FEC_ATIME_INC);
  292. corr_period = corr_period > 1 ? corr_period - 1 : corr_period;
  293. writel(corr_period, fep->hwp + FEC_ATIME_CORR);
  294. /* dummy read to update the timer. */
  295. timecounter_read(&fep->tc);
  296. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  297. return 0;
  298. }
  299. /**
  300. * fec_ptp_adjtime
  301. * @ptp: the ptp clock structure
  302. * @delta: offset to adjust the cycle counter by
  303. *
  304. * adjust the timer by resetting the timecounter structure.
  305. */
  306. static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  307. {
  308. struct fec_enet_private *fep =
  309. container_of(ptp, struct fec_enet_private, ptp_caps);
  310. unsigned long flags;
  311. spin_lock_irqsave(&fep->tmreg_lock, flags);
  312. timecounter_adjtime(&fep->tc, delta);
  313. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  314. return 0;
  315. }
  316. /**
  317. * fec_ptp_gettime
  318. * @ptp: the ptp clock structure
  319. * @ts: timespec structure to hold the current time value
  320. *
  321. * read the timecounter and return the correct value on ns,
  322. * after converting it into a struct timespec.
  323. */
  324. static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  325. {
  326. struct fec_enet_private *adapter =
  327. container_of(ptp, struct fec_enet_private, ptp_caps);
  328. u64 ns;
  329. unsigned long flags;
  330. mutex_lock(&adapter->ptp_clk_mutex);
  331. /* Check the ptp clock */
  332. if (!adapter->ptp_clk_on) {
  333. mutex_unlock(&adapter->ptp_clk_mutex);
  334. return -EINVAL;
  335. }
  336. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  337. ns = timecounter_read(&adapter->tc);
  338. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  339. mutex_unlock(&adapter->ptp_clk_mutex);
  340. *ts = ns_to_timespec64(ns);
  341. return 0;
  342. }
  343. /**
  344. * fec_ptp_settime
  345. * @ptp: the ptp clock structure
  346. * @ts: the timespec containing the new time for the cycle counter
  347. *
  348. * reset the timecounter to use a new base value instead of the kernel
  349. * wall timer value.
  350. */
  351. static int fec_ptp_settime(struct ptp_clock_info *ptp,
  352. const struct timespec64 *ts)
  353. {
  354. struct fec_enet_private *fep =
  355. container_of(ptp, struct fec_enet_private, ptp_caps);
  356. u64 ns;
  357. unsigned long flags;
  358. u32 counter;
  359. mutex_lock(&fep->ptp_clk_mutex);
  360. /* Check the ptp clock */
  361. if (!fep->ptp_clk_on) {
  362. mutex_unlock(&fep->ptp_clk_mutex);
  363. return -EINVAL;
  364. }
  365. ns = timespec64_to_ns(ts);
  366. /* Get the timer value based on timestamp.
  367. * Update the counter with the masked value.
  368. */
  369. counter = ns & fep->cc.mask;
  370. spin_lock_irqsave(&fep->tmreg_lock, flags);
  371. writel(counter, fep->hwp + FEC_ATIME);
  372. timecounter_init(&fep->tc, &fep->cc, ns);
  373. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  374. mutex_unlock(&fep->ptp_clk_mutex);
  375. return 0;
  376. }
  377. /**
  378. * fec_ptp_enable
  379. * @ptp: the ptp clock structure
  380. * @rq: the requested feature to change
  381. * @on: whether to enable or disable the feature
  382. *
  383. */
  384. static int fec_ptp_enable(struct ptp_clock_info *ptp,
  385. struct ptp_clock_request *rq, int on)
  386. {
  387. struct fec_enet_private *fep =
  388. container_of(ptp, struct fec_enet_private, ptp_caps);
  389. int ret = 0;
  390. if (rq->type == PTP_CLK_REQ_PPS) {
  391. ret = fec_ptp_enable_pps(fep, on);
  392. return ret;
  393. }
  394. return -EOPNOTSUPP;
  395. }
  396. int fec_ptp_set(struct net_device *ndev, struct ifreq *ifr)
  397. {
  398. struct fec_enet_private *fep = netdev_priv(ndev);
  399. struct hwtstamp_config config;
  400. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  401. return -EFAULT;
  402. /* reserved for future extensions */
  403. if (config.flags)
  404. return -EINVAL;
  405. switch (config.tx_type) {
  406. case HWTSTAMP_TX_OFF:
  407. fep->hwts_tx_en = 0;
  408. break;
  409. case HWTSTAMP_TX_ON:
  410. fep->hwts_tx_en = 1;
  411. break;
  412. default:
  413. return -ERANGE;
  414. }
  415. switch (config.rx_filter) {
  416. case HWTSTAMP_FILTER_NONE:
  417. if (fep->hwts_rx_en)
  418. fep->hwts_rx_en = 0;
  419. config.rx_filter = HWTSTAMP_FILTER_NONE;
  420. break;
  421. default:
  422. fep->hwts_rx_en = 1;
  423. config.rx_filter = HWTSTAMP_FILTER_ALL;
  424. break;
  425. }
  426. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  427. -EFAULT : 0;
  428. }
  429. int fec_ptp_get(struct net_device *ndev, struct ifreq *ifr)
  430. {
  431. struct fec_enet_private *fep = netdev_priv(ndev);
  432. struct hwtstamp_config config;
  433. config.flags = 0;
  434. config.tx_type = fep->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  435. config.rx_filter = (fep->hwts_rx_en ?
  436. HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
  437. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  438. -EFAULT : 0;
  439. }
  440. /**
  441. * fec_time_keep - call timecounter_read every second to avoid timer overrun
  442. * because ENET just support 32bit counter, will timeout in 4s
  443. */
  444. static void fec_time_keep(struct work_struct *work)
  445. {
  446. struct delayed_work *dwork = to_delayed_work(work);
  447. struct fec_enet_private *fep = container_of(dwork, struct fec_enet_private, time_keep);
  448. u64 ns;
  449. unsigned long flags;
  450. mutex_lock(&fep->ptp_clk_mutex);
  451. if (fep->ptp_clk_on) {
  452. spin_lock_irqsave(&fep->tmreg_lock, flags);
  453. ns = timecounter_read(&fep->tc);
  454. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  455. }
  456. mutex_unlock(&fep->ptp_clk_mutex);
  457. schedule_delayed_work(&fep->time_keep, HZ);
  458. }
  459. /* This function checks the pps event and reloads the timer compare counter. */
  460. static irqreturn_t fec_pps_interrupt(int irq, void *dev_id)
  461. {
  462. struct net_device *ndev = dev_id;
  463. struct fec_enet_private *fep = netdev_priv(ndev);
  464. u32 val;
  465. u8 channel = fep->pps_channel;
  466. struct ptp_clock_event event;
  467. val = readl(fep->hwp + FEC_TCSR(channel));
  468. if (val & FEC_T_TF_MASK) {
  469. /* Write the next next compare(not the next according the spec)
  470. * value to the register
  471. */
  472. writel(fep->next_counter, fep->hwp + FEC_TCCR(channel));
  473. do {
  474. writel(val, fep->hwp + FEC_TCSR(channel));
  475. } while (readl(fep->hwp + FEC_TCSR(channel)) & FEC_T_TF_MASK);
  476. /* Update the counter; */
  477. fep->next_counter = (fep->next_counter + fep->reload_period) &
  478. fep->cc.mask;
  479. event.type = PTP_CLOCK_PPS;
  480. ptp_clock_event(fep->ptp_clock, &event);
  481. return IRQ_HANDLED;
  482. }
  483. return IRQ_NONE;
  484. }
  485. /**
  486. * fec_ptp_init
  487. * @ndev: The FEC network adapter
  488. *
  489. * This function performs the required steps for enabling ptp
  490. * support. If ptp support has already been loaded it simply calls the
  491. * cyclecounter init routine and exits.
  492. */
  493. void fec_ptp_init(struct platform_device *pdev, int irq_idx)
  494. {
  495. struct net_device *ndev = platform_get_drvdata(pdev);
  496. struct fec_enet_private *fep = netdev_priv(ndev);
  497. int irq;
  498. int ret;
  499. fep->ptp_caps.owner = THIS_MODULE;
  500. snprintf(fep->ptp_caps.name, 16, "fec ptp");
  501. fep->ptp_caps.max_adj = 250000000;
  502. fep->ptp_caps.n_alarm = 0;
  503. fep->ptp_caps.n_ext_ts = 0;
  504. fep->ptp_caps.n_per_out = 0;
  505. fep->ptp_caps.n_pins = 0;
  506. fep->ptp_caps.pps = 1;
  507. fep->ptp_caps.adjfreq = fec_ptp_adjfreq;
  508. fep->ptp_caps.adjtime = fec_ptp_adjtime;
  509. fep->ptp_caps.gettime64 = fec_ptp_gettime;
  510. fep->ptp_caps.settime64 = fec_ptp_settime;
  511. fep->ptp_caps.enable = fec_ptp_enable;
  512. fep->cycle_speed = clk_get_rate(fep->clk_ptp);
  513. fep->ptp_inc = NSEC_PER_SEC / fep->cycle_speed;
  514. spin_lock_init(&fep->tmreg_lock);
  515. fec_ptp_start_cyclecounter(ndev);
  516. INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep);
  517. irq = platform_get_irq_byname(pdev, "pps");
  518. if (irq < 0)
  519. irq = platform_get_irq(pdev, irq_idx);
  520. /* Failure to get an irq is not fatal,
  521. * only the PTP_CLOCK_PPS clock events should stop
  522. */
  523. if (irq >= 0) {
  524. ret = devm_request_irq(&pdev->dev, irq, fec_pps_interrupt,
  525. 0, pdev->name, ndev);
  526. if (ret < 0)
  527. dev_warn(&pdev->dev, "request for pps irq failed(%d)\n",
  528. ret);
  529. }
  530. fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
  531. if (IS_ERR(fep->ptp_clock)) {
  532. fep->ptp_clock = NULL;
  533. pr_err("ptp_clock_register failed\n");
  534. }
  535. schedule_delayed_work(&fep->time_keep, HZ);
  536. }
  537. void fec_ptp_stop(struct platform_device *pdev)
  538. {
  539. struct net_device *ndev = platform_get_drvdata(pdev);
  540. struct fec_enet_private *fep = netdev_priv(ndev);
  541. cancel_delayed_work_sync(&fep->time_keep);
  542. if (fep->ptp_clock)
  543. ptp_clock_unregister(fep->ptp_clock);
  544. }