macb_ptp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /**
  2. * 1588 PTP support for Cadence GEM device.
  3. *
  4. * Copyright (C) 2017 Cadence Design Systems - http://www.cadence.com
  5. *
  6. * Authors: Rafal Ozieblo <rafalo@cadence.com>
  7. * Bartosz Folta <bfolta@cadence.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/types.h>
  23. #include <linux/clk.h>
  24. #include <linux/device.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/time64.h>
  28. #include <linux/ptp_classify.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/if_vlan.h>
  31. #include <linux/net_tstamp.h>
  32. #include <linux/circ_buf.h>
  33. #include <linux/spinlock.h>
  34. #include "macb.h"
  35. #define GEM_PTP_TIMER_NAME "gem-ptp-timer"
  36. static struct macb_dma_desc_ptp *macb_ptp_desc(struct macb *bp,
  37. struct macb_dma_desc *desc)
  38. {
  39. if (bp->hw_dma_cap == HW_DMA_CAP_PTP)
  40. return (struct macb_dma_desc_ptp *)
  41. ((u8 *)desc + sizeof(struct macb_dma_desc));
  42. if (bp->hw_dma_cap == HW_DMA_CAP_64B_PTP)
  43. return (struct macb_dma_desc_ptp *)
  44. ((u8 *)desc + sizeof(struct macb_dma_desc)
  45. + sizeof(struct macb_dma_desc_64));
  46. return NULL;
  47. }
  48. static int gem_tsu_get_time(struct ptp_clock_info *ptp, struct timespec64 *ts)
  49. {
  50. struct macb *bp = container_of(ptp, struct macb, ptp_clock_info);
  51. unsigned long flags;
  52. long first, second;
  53. u32 secl, sech;
  54. spin_lock_irqsave(&bp->tsu_clk_lock, flags);
  55. first = gem_readl(bp, TN);
  56. secl = gem_readl(bp, TSL);
  57. sech = gem_readl(bp, TSH);
  58. second = gem_readl(bp, TN);
  59. /* test for nsec rollover */
  60. if (first > second) {
  61. /* if so, use later read & re-read seconds
  62. * (assume all done within 1s)
  63. */
  64. ts->tv_nsec = gem_readl(bp, TN);
  65. secl = gem_readl(bp, TSL);
  66. sech = gem_readl(bp, TSH);
  67. } else {
  68. ts->tv_nsec = first;
  69. }
  70. spin_unlock_irqrestore(&bp->tsu_clk_lock, flags);
  71. ts->tv_sec = (((u64)sech << GEM_TSL_SIZE) | secl)
  72. & TSU_SEC_MAX_VAL;
  73. return 0;
  74. }
  75. static int gem_tsu_set_time(struct ptp_clock_info *ptp,
  76. const struct timespec64 *ts)
  77. {
  78. struct macb *bp = container_of(ptp, struct macb, ptp_clock_info);
  79. unsigned long flags;
  80. u32 ns, sech, secl;
  81. secl = (u32)ts->tv_sec;
  82. sech = (ts->tv_sec >> GEM_TSL_SIZE) & ((1 << GEM_TSH_SIZE) - 1);
  83. ns = ts->tv_nsec;
  84. spin_lock_irqsave(&bp->tsu_clk_lock, flags);
  85. /* TSH doesn't latch the time and no atomicity! */
  86. gem_writel(bp, TN, 0); /* clear to avoid overflow */
  87. gem_writel(bp, TSH, sech);
  88. /* write lower bits 2nd, for synchronized secs update */
  89. gem_writel(bp, TSL, secl);
  90. gem_writel(bp, TN, ns);
  91. spin_unlock_irqrestore(&bp->tsu_clk_lock, flags);
  92. return 0;
  93. }
  94. static int gem_tsu_incr_set(struct macb *bp, struct tsu_incr *incr_spec)
  95. {
  96. unsigned long flags;
  97. /* tsu_timer_incr register must be written after
  98. * the tsu_timer_incr_sub_ns register and the write operation
  99. * will cause the value written to the tsu_timer_incr_sub_ns register
  100. * to take effect.
  101. */
  102. spin_lock_irqsave(&bp->tsu_clk_lock, flags);
  103. gem_writel(bp, TISUBN, GEM_BF(SUBNSINCR, incr_spec->sub_ns));
  104. gem_writel(bp, TI, GEM_BF(NSINCR, incr_spec->ns));
  105. spin_unlock_irqrestore(&bp->tsu_clk_lock, flags);
  106. return 0;
  107. }
  108. static int gem_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  109. {
  110. struct macb *bp = container_of(ptp, struct macb, ptp_clock_info);
  111. struct tsu_incr incr_spec;
  112. bool neg_adj = false;
  113. u32 word;
  114. u64 adj;
  115. if (scaled_ppm < 0) {
  116. neg_adj = true;
  117. scaled_ppm = -scaled_ppm;
  118. }
  119. /* Adjustment is relative to base frequency */
  120. incr_spec.sub_ns = bp->tsu_incr.sub_ns;
  121. incr_spec.ns = bp->tsu_incr.ns;
  122. /* scaling: unused(8bit) | ns(8bit) | fractions(16bit) */
  123. word = ((u64)incr_spec.ns << GEM_SUBNSINCR_SIZE) + incr_spec.sub_ns;
  124. adj = (u64)scaled_ppm * word;
  125. /* Divide with rounding, equivalent to floating dividing:
  126. * (temp / USEC_PER_SEC) + 0.5
  127. */
  128. adj += (USEC_PER_SEC >> 1);
  129. adj >>= GEM_SUBNSINCR_SIZE; /* remove fractions */
  130. adj = div_u64(adj, USEC_PER_SEC);
  131. adj = neg_adj ? (word - adj) : (word + adj);
  132. incr_spec.ns = (adj >> GEM_SUBNSINCR_SIZE)
  133. & ((1 << GEM_NSINCR_SIZE) - 1);
  134. incr_spec.sub_ns = adj & ((1 << GEM_SUBNSINCR_SIZE) - 1);
  135. gem_tsu_incr_set(bp, &incr_spec);
  136. return 0;
  137. }
  138. static int gem_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  139. {
  140. struct macb *bp = container_of(ptp, struct macb, ptp_clock_info);
  141. struct timespec64 now, then = ns_to_timespec64(delta);
  142. u32 adj, sign = 0;
  143. if (delta < 0) {
  144. sign = 1;
  145. delta = -delta;
  146. }
  147. if (delta > TSU_NSEC_MAX_VAL) {
  148. gem_tsu_get_time(&bp->ptp_clock_info, &now);
  149. now = timespec64_add(now, then);
  150. gem_tsu_set_time(&bp->ptp_clock_info,
  151. (const struct timespec64 *)&now);
  152. } else {
  153. adj = (sign << GEM_ADDSUB_OFFSET) | delta;
  154. gem_writel(bp, TA, adj);
  155. }
  156. return 0;
  157. }
  158. static int gem_ptp_enable(struct ptp_clock_info *ptp,
  159. struct ptp_clock_request *rq, int on)
  160. {
  161. return -EOPNOTSUPP;
  162. }
  163. static const struct ptp_clock_info gem_ptp_caps_template = {
  164. .owner = THIS_MODULE,
  165. .name = GEM_PTP_TIMER_NAME,
  166. .max_adj = 0,
  167. .n_alarm = 0,
  168. .n_ext_ts = 0,
  169. .n_per_out = 0,
  170. .n_pins = 0,
  171. .pps = 1,
  172. .adjfine = gem_ptp_adjfine,
  173. .adjtime = gem_ptp_adjtime,
  174. .gettime64 = gem_tsu_get_time,
  175. .settime64 = gem_tsu_set_time,
  176. .enable = gem_ptp_enable,
  177. };
  178. static void gem_ptp_init_timer(struct macb *bp)
  179. {
  180. u32 rem = 0;
  181. u64 adj;
  182. bp->tsu_incr.ns = div_u64_rem(NSEC_PER_SEC, bp->tsu_rate, &rem);
  183. if (rem) {
  184. adj = rem;
  185. adj <<= GEM_SUBNSINCR_SIZE;
  186. bp->tsu_incr.sub_ns = div_u64(adj, bp->tsu_rate);
  187. } else {
  188. bp->tsu_incr.sub_ns = 0;
  189. }
  190. }
  191. static void gem_ptp_init_tsu(struct macb *bp)
  192. {
  193. struct timespec64 ts;
  194. /* 1. get current system time */
  195. ts = ns_to_timespec64(ktime_to_ns(ktime_get_real()));
  196. /* 2. set ptp timer */
  197. gem_tsu_set_time(&bp->ptp_clock_info, &ts);
  198. /* 3. set PTP timer increment value to BASE_INCREMENT */
  199. gem_tsu_incr_set(bp, &bp->tsu_incr);
  200. gem_writel(bp, TA, 0);
  201. }
  202. static void gem_ptp_clear_timer(struct macb *bp)
  203. {
  204. bp->tsu_incr.sub_ns = 0;
  205. bp->tsu_incr.ns = 0;
  206. gem_writel(bp, TISUBN, GEM_BF(SUBNSINCR, 0));
  207. gem_writel(bp, TI, GEM_BF(NSINCR, 0));
  208. gem_writel(bp, TA, 0);
  209. }
  210. static int gem_hw_timestamp(struct macb *bp, u32 dma_desc_ts_1,
  211. u32 dma_desc_ts_2, struct timespec64 *ts)
  212. {
  213. struct timespec64 tsu;
  214. ts->tv_sec = (GEM_BFEXT(DMA_SECH, dma_desc_ts_2) << GEM_DMA_SECL_SIZE) |
  215. GEM_BFEXT(DMA_SECL, dma_desc_ts_1);
  216. ts->tv_nsec = GEM_BFEXT(DMA_NSEC, dma_desc_ts_1);
  217. /* TSU overlapping workaround
  218. * The timestamp only contains lower few bits of seconds,
  219. * so add value from 1588 timer
  220. */
  221. gem_tsu_get_time(&bp->ptp_clock_info, &tsu);
  222. /* If the top bit is set in the timestamp,
  223. * but not in 1588 timer, it has rolled over,
  224. * so subtract max size
  225. */
  226. if ((ts->tv_sec & (GEM_DMA_SEC_TOP >> 1)) &&
  227. !(tsu.tv_sec & (GEM_DMA_SEC_TOP >> 1)))
  228. ts->tv_sec -= GEM_DMA_SEC_TOP;
  229. ts->tv_sec += ((~GEM_DMA_SEC_MASK) & tsu.tv_sec);
  230. return 0;
  231. }
  232. void gem_ptp_rxstamp(struct macb *bp, struct sk_buff *skb,
  233. struct macb_dma_desc *desc)
  234. {
  235. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  236. struct macb_dma_desc_ptp *desc_ptp;
  237. struct timespec64 ts;
  238. if (GEM_BFEXT(DMA_RXVALID, desc->addr)) {
  239. desc_ptp = macb_ptp_desc(bp, desc);
  240. gem_hw_timestamp(bp, desc_ptp->ts_1, desc_ptp->ts_2, &ts);
  241. memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
  242. shhwtstamps->hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
  243. }
  244. }
  245. static void gem_tstamp_tx(struct macb *bp, struct sk_buff *skb,
  246. struct macb_dma_desc_ptp *desc_ptp)
  247. {
  248. struct skb_shared_hwtstamps shhwtstamps;
  249. struct timespec64 ts;
  250. gem_hw_timestamp(bp, desc_ptp->ts_1, desc_ptp->ts_2, &ts);
  251. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  252. shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
  253. skb_tstamp_tx(skb, &shhwtstamps);
  254. }
  255. int gem_ptp_txstamp(struct macb_queue *queue, struct sk_buff *skb,
  256. struct macb_dma_desc *desc)
  257. {
  258. unsigned long tail = READ_ONCE(queue->tx_ts_tail);
  259. unsigned long head = queue->tx_ts_head;
  260. struct macb_dma_desc_ptp *desc_ptp;
  261. struct gem_tx_ts *tx_timestamp;
  262. if (!GEM_BFEXT(DMA_TXVALID, desc->ctrl))
  263. return -EINVAL;
  264. if (CIRC_SPACE(head, tail, PTP_TS_BUFFER_SIZE) == 0)
  265. return -ENOMEM;
  266. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  267. desc_ptp = macb_ptp_desc(queue->bp, desc);
  268. tx_timestamp = &queue->tx_timestamps[head];
  269. tx_timestamp->skb = skb;
  270. tx_timestamp->desc_ptp.ts_1 = desc_ptp->ts_1;
  271. tx_timestamp->desc_ptp.ts_2 = desc_ptp->ts_2;
  272. /* move head */
  273. smp_store_release(&queue->tx_ts_head,
  274. (head + 1) & (PTP_TS_BUFFER_SIZE - 1));
  275. schedule_work(&queue->tx_ts_task);
  276. return 0;
  277. }
  278. static void gem_tx_timestamp_flush(struct work_struct *work)
  279. {
  280. struct macb_queue *queue =
  281. container_of(work, struct macb_queue, tx_ts_task);
  282. unsigned long head, tail;
  283. struct gem_tx_ts *tx_ts;
  284. /* take current head */
  285. head = smp_load_acquire(&queue->tx_ts_head);
  286. tail = queue->tx_ts_tail;
  287. while (CIRC_CNT(head, tail, PTP_TS_BUFFER_SIZE)) {
  288. tx_ts = &queue->tx_timestamps[tail];
  289. gem_tstamp_tx(queue->bp, tx_ts->skb, &tx_ts->desc_ptp);
  290. /* cleanup */
  291. dev_kfree_skb_any(tx_ts->skb);
  292. /* remove old tail */
  293. smp_store_release(&queue->tx_ts_tail,
  294. (tail + 1) & (PTP_TS_BUFFER_SIZE - 1));
  295. tail = queue->tx_ts_tail;
  296. }
  297. }
  298. void gem_ptp_init(struct net_device *dev)
  299. {
  300. struct macb *bp = netdev_priv(dev);
  301. struct macb_queue *queue;
  302. unsigned int q;
  303. bp->ptp_clock_info = gem_ptp_caps_template;
  304. /* nominal frequency and maximum adjustment in ppb */
  305. bp->tsu_rate = bp->ptp_info->get_tsu_rate(bp);
  306. bp->ptp_clock_info.max_adj = bp->ptp_info->get_ptp_max_adj();
  307. gem_ptp_init_timer(bp);
  308. bp->ptp_clock = ptp_clock_register(&bp->ptp_clock_info, &dev->dev);
  309. if (IS_ERR(bp->ptp_clock)) {
  310. pr_err("ptp clock register failed: %ld\n",
  311. PTR_ERR(bp->ptp_clock));
  312. bp->ptp_clock = NULL;
  313. return;
  314. } else if (bp->ptp_clock == NULL) {
  315. pr_err("ptp clock register failed\n");
  316. return;
  317. }
  318. spin_lock_init(&bp->tsu_clk_lock);
  319. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  320. queue->tx_ts_head = 0;
  321. queue->tx_ts_tail = 0;
  322. INIT_WORK(&queue->tx_ts_task, gem_tx_timestamp_flush);
  323. }
  324. gem_ptp_init_tsu(bp);
  325. dev_info(&bp->pdev->dev, "%s ptp clock registered.\n",
  326. GEM_PTP_TIMER_NAME);
  327. }
  328. void gem_ptp_remove(struct net_device *ndev)
  329. {
  330. struct macb *bp = netdev_priv(ndev);
  331. if (bp->ptp_clock)
  332. ptp_clock_unregister(bp->ptp_clock);
  333. gem_ptp_clear_timer(bp);
  334. dev_info(&bp->pdev->dev, "%s ptp clock unregistered.\n",
  335. GEM_PTP_TIMER_NAME);
  336. }
  337. static int gem_ptp_set_ts_mode(struct macb *bp,
  338. enum macb_bd_control tx_bd_control,
  339. enum macb_bd_control rx_bd_control)
  340. {
  341. gem_writel(bp, TXBDCTRL, GEM_BF(TXTSMODE, tx_bd_control));
  342. gem_writel(bp, RXBDCTRL, GEM_BF(RXTSMODE, rx_bd_control));
  343. return 0;
  344. }
  345. int gem_get_hwtst(struct net_device *dev, struct ifreq *rq)
  346. {
  347. struct hwtstamp_config *tstamp_config;
  348. struct macb *bp = netdev_priv(dev);
  349. tstamp_config = &bp->tstamp_config;
  350. if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0)
  351. return -EOPNOTSUPP;
  352. if (copy_to_user(rq->ifr_data, tstamp_config, sizeof(*tstamp_config)))
  353. return -EFAULT;
  354. else
  355. return 0;
  356. }
  357. static int gem_ptp_set_one_step_sync(struct macb *bp, u8 enable)
  358. {
  359. u32 reg_val;
  360. reg_val = macb_readl(bp, NCR);
  361. if (enable)
  362. macb_writel(bp, NCR, reg_val | MACB_BIT(OSSMODE));
  363. else
  364. macb_writel(bp, NCR, reg_val & ~MACB_BIT(OSSMODE));
  365. return 0;
  366. }
  367. int gem_set_hwtst(struct net_device *dev, struct ifreq *ifr, int cmd)
  368. {
  369. enum macb_bd_control tx_bd_control = TSTAMP_DISABLED;
  370. enum macb_bd_control rx_bd_control = TSTAMP_DISABLED;
  371. struct hwtstamp_config *tstamp_config;
  372. struct macb *bp = netdev_priv(dev);
  373. u32 regval;
  374. tstamp_config = &bp->tstamp_config;
  375. if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0)
  376. return -EOPNOTSUPP;
  377. if (copy_from_user(tstamp_config, ifr->ifr_data,
  378. sizeof(*tstamp_config)))
  379. return -EFAULT;
  380. /* reserved for future extensions */
  381. if (tstamp_config->flags)
  382. return -EINVAL;
  383. switch (tstamp_config->tx_type) {
  384. case HWTSTAMP_TX_OFF:
  385. break;
  386. case HWTSTAMP_TX_ONESTEP_SYNC:
  387. if (gem_ptp_set_one_step_sync(bp, 1) != 0)
  388. return -ERANGE;
  389. case HWTSTAMP_TX_ON:
  390. tx_bd_control = TSTAMP_ALL_FRAMES;
  391. break;
  392. default:
  393. return -ERANGE;
  394. }
  395. switch (tstamp_config->rx_filter) {
  396. case HWTSTAMP_FILTER_NONE:
  397. break;
  398. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  399. break;
  400. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  401. break;
  402. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  403. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  404. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  405. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  406. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  407. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  408. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  409. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  410. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  411. rx_bd_control = TSTAMP_ALL_PTP_FRAMES;
  412. tstamp_config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  413. regval = macb_readl(bp, NCR);
  414. macb_writel(bp, NCR, (regval | MACB_BIT(SRTSM)));
  415. break;
  416. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  417. case HWTSTAMP_FILTER_ALL:
  418. rx_bd_control = TSTAMP_ALL_FRAMES;
  419. tstamp_config->rx_filter = HWTSTAMP_FILTER_ALL;
  420. break;
  421. default:
  422. tstamp_config->rx_filter = HWTSTAMP_FILTER_NONE;
  423. return -ERANGE;
  424. }
  425. if (gem_ptp_set_ts_mode(bp, tx_bd_control, rx_bd_control) != 0)
  426. return -ERANGE;
  427. if (copy_to_user(ifr->ifr_data, tstamp_config, sizeof(*tstamp_config)))
  428. return -EFAULT;
  429. else
  430. return 0;
  431. }