fec_ptp.c 17 KB

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