fec_ptp.c 18 KB

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