fec_ptp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. spin_lock_irqsave(&adapter->tmreg_lock, flags);
  331. ns = timecounter_read(&adapter->tc);
  332. spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
  333. *ts = ns_to_timespec64(ns);
  334. return 0;
  335. }
  336. /**
  337. * fec_ptp_settime
  338. * @ptp: the ptp clock structure
  339. * @ts: the timespec containing the new time for the cycle counter
  340. *
  341. * reset the timecounter to use a new base value instead of the kernel
  342. * wall timer value.
  343. */
  344. static int fec_ptp_settime(struct ptp_clock_info *ptp,
  345. const struct timespec64 *ts)
  346. {
  347. struct fec_enet_private *fep =
  348. container_of(ptp, struct fec_enet_private, ptp_caps);
  349. u64 ns;
  350. unsigned long flags;
  351. u32 counter;
  352. mutex_lock(&fep->ptp_clk_mutex);
  353. /* Check the ptp clock */
  354. if (!fep->ptp_clk_on) {
  355. mutex_unlock(&fep->ptp_clk_mutex);
  356. return -EINVAL;
  357. }
  358. ns = timespec64_to_ns(ts);
  359. /* Get the timer value based on timestamp.
  360. * Update the counter with the masked value.
  361. */
  362. counter = ns & fep->cc.mask;
  363. spin_lock_irqsave(&fep->tmreg_lock, flags);
  364. writel(counter, fep->hwp + FEC_ATIME);
  365. timecounter_init(&fep->tc, &fep->cc, ns);
  366. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  367. mutex_unlock(&fep->ptp_clk_mutex);
  368. return 0;
  369. }
  370. /**
  371. * fec_ptp_enable
  372. * @ptp: the ptp clock structure
  373. * @rq: the requested feature to change
  374. * @on: whether to enable or disable the feature
  375. *
  376. */
  377. static int fec_ptp_enable(struct ptp_clock_info *ptp,
  378. struct ptp_clock_request *rq, int on)
  379. {
  380. struct fec_enet_private *fep =
  381. container_of(ptp, struct fec_enet_private, ptp_caps);
  382. int ret = 0;
  383. if (rq->type == PTP_CLK_REQ_PPS) {
  384. ret = fec_ptp_enable_pps(fep, on);
  385. return ret;
  386. }
  387. return -EOPNOTSUPP;
  388. }
  389. int fec_ptp_set(struct net_device *ndev, struct ifreq *ifr)
  390. {
  391. struct fec_enet_private *fep = netdev_priv(ndev);
  392. struct hwtstamp_config config;
  393. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  394. return -EFAULT;
  395. /* reserved for future extensions */
  396. if (config.flags)
  397. return -EINVAL;
  398. switch (config.tx_type) {
  399. case HWTSTAMP_TX_OFF:
  400. fep->hwts_tx_en = 0;
  401. break;
  402. case HWTSTAMP_TX_ON:
  403. fep->hwts_tx_en = 1;
  404. break;
  405. default:
  406. return -ERANGE;
  407. }
  408. switch (config.rx_filter) {
  409. case HWTSTAMP_FILTER_NONE:
  410. if (fep->hwts_rx_en)
  411. fep->hwts_rx_en = 0;
  412. config.rx_filter = HWTSTAMP_FILTER_NONE;
  413. break;
  414. default:
  415. fep->hwts_rx_en = 1;
  416. config.rx_filter = HWTSTAMP_FILTER_ALL;
  417. break;
  418. }
  419. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  420. -EFAULT : 0;
  421. }
  422. int fec_ptp_get(struct net_device *ndev, struct ifreq *ifr)
  423. {
  424. struct fec_enet_private *fep = netdev_priv(ndev);
  425. struct hwtstamp_config config;
  426. config.flags = 0;
  427. config.tx_type = fep->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  428. config.rx_filter = (fep->hwts_rx_en ?
  429. HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
  430. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  431. -EFAULT : 0;
  432. }
  433. /**
  434. * fec_time_keep - call timecounter_read every second to avoid timer overrun
  435. * because ENET just support 32bit counter, will timeout in 4s
  436. */
  437. static void fec_time_keep(struct work_struct *work)
  438. {
  439. struct delayed_work *dwork = to_delayed_work(work);
  440. struct fec_enet_private *fep = container_of(dwork, struct fec_enet_private, time_keep);
  441. u64 ns;
  442. unsigned long flags;
  443. mutex_lock(&fep->ptp_clk_mutex);
  444. if (fep->ptp_clk_on) {
  445. spin_lock_irqsave(&fep->tmreg_lock, flags);
  446. ns = timecounter_read(&fep->tc);
  447. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  448. }
  449. mutex_unlock(&fep->ptp_clk_mutex);
  450. schedule_delayed_work(&fep->time_keep, HZ);
  451. }
  452. /* This function checks the pps event and reloads the timer compare counter. */
  453. static irqreturn_t fec_pps_interrupt(int irq, void *dev_id)
  454. {
  455. struct net_device *ndev = dev_id;
  456. struct fec_enet_private *fep = netdev_priv(ndev);
  457. u32 val;
  458. u8 channel = fep->pps_channel;
  459. struct ptp_clock_event event;
  460. val = readl(fep->hwp + FEC_TCSR(channel));
  461. if (val & FEC_T_TF_MASK) {
  462. /* Write the next next compare(not the next according the spec)
  463. * value to the register
  464. */
  465. writel(fep->next_counter, fep->hwp + FEC_TCCR(channel));
  466. do {
  467. writel(val, fep->hwp + FEC_TCSR(channel));
  468. } while (readl(fep->hwp + FEC_TCSR(channel)) & FEC_T_TF_MASK);
  469. /* Update the counter; */
  470. fep->next_counter = (fep->next_counter + fep->reload_period) &
  471. fep->cc.mask;
  472. event.type = PTP_CLOCK_PPS;
  473. ptp_clock_event(fep->ptp_clock, &event);
  474. return IRQ_HANDLED;
  475. }
  476. return IRQ_NONE;
  477. }
  478. /**
  479. * fec_ptp_init
  480. * @ndev: The FEC network adapter
  481. *
  482. * This function performs the required steps for enabling ptp
  483. * support. If ptp support has already been loaded it simply calls the
  484. * cyclecounter init routine and exits.
  485. */
  486. void fec_ptp_init(struct platform_device *pdev, int irq_idx)
  487. {
  488. struct net_device *ndev = platform_get_drvdata(pdev);
  489. struct fec_enet_private *fep = netdev_priv(ndev);
  490. int irq;
  491. int ret;
  492. fep->ptp_caps.owner = THIS_MODULE;
  493. snprintf(fep->ptp_caps.name, 16, "fec ptp");
  494. fep->ptp_caps.max_adj = 250000000;
  495. fep->ptp_caps.n_alarm = 0;
  496. fep->ptp_caps.n_ext_ts = 0;
  497. fep->ptp_caps.n_per_out = 0;
  498. fep->ptp_caps.n_pins = 0;
  499. fep->ptp_caps.pps = 1;
  500. fep->ptp_caps.adjfreq = fec_ptp_adjfreq;
  501. fep->ptp_caps.adjtime = fec_ptp_adjtime;
  502. fep->ptp_caps.gettime64 = fec_ptp_gettime;
  503. fep->ptp_caps.settime64 = fec_ptp_settime;
  504. fep->ptp_caps.enable = fec_ptp_enable;
  505. fep->cycle_speed = clk_get_rate(fep->clk_ptp);
  506. fep->ptp_inc = NSEC_PER_SEC / fep->cycle_speed;
  507. spin_lock_init(&fep->tmreg_lock);
  508. fec_ptp_start_cyclecounter(ndev);
  509. INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep);
  510. irq = platform_get_irq_byname(pdev, "pps");
  511. if (irq < 0)
  512. irq = platform_get_irq(pdev, irq_idx);
  513. /* Failure to get an irq is not fatal,
  514. * only the PTP_CLOCK_PPS clock events should stop
  515. */
  516. if (irq >= 0) {
  517. ret = devm_request_irq(&pdev->dev, irq, fec_pps_interrupt,
  518. 0, pdev->name, ndev);
  519. if (ret < 0)
  520. dev_warn(&pdev->dev, "request for pps irq failed(%d)\n",
  521. ret);
  522. }
  523. fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
  524. if (IS_ERR(fep->ptp_clock)) {
  525. fep->ptp_clock = NULL;
  526. pr_err("ptp_clock_register failed\n");
  527. }
  528. schedule_delayed_work(&fep->time_keep, HZ);
  529. }
  530. void fec_ptp_stop(struct platform_device *pdev)
  531. {
  532. struct net_device *ndev = platform_get_drvdata(pdev);
  533. struct fec_enet_private *fep = netdev_priv(ndev);
  534. cancel_delayed_work_sync(&fep->time_keep);
  535. if (fep->ptp_clock)
  536. ptp_clock_unregister(fep->ptp_clock);
  537. }