fec_ptp.c 18 KB

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