macb_ptp.c 13 KB

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