fec_ptp.c 17 KB

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