fec_ptp.c 18 KB

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