macb_ptp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. /* RegBit[15:0] = Subns[23:8]; RegBit[31:24] = Subns[7:0] */
  104. gem_writel(bp, TISUBN, GEM_BF(SUBNSINCRL, incr_spec->sub_ns) |
  105. GEM_BF(SUBNSINCRH, (incr_spec->sub_ns >>
  106. GEM_SUBNSINCRL_SIZE)));
  107. gem_writel(bp, TI, GEM_BF(NSINCR, incr_spec->ns));
  108. spin_unlock_irqrestore(&bp->tsu_clk_lock, flags);
  109. return 0;
  110. }
  111. static int gem_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  112. {
  113. struct macb *bp = container_of(ptp, struct macb, ptp_clock_info);
  114. struct tsu_incr incr_spec;
  115. bool neg_adj = false;
  116. u32 word;
  117. u64 adj;
  118. if (scaled_ppm < 0) {
  119. neg_adj = true;
  120. scaled_ppm = -scaled_ppm;
  121. }
  122. /* Adjustment is relative to base frequency */
  123. incr_spec.sub_ns = bp->tsu_incr.sub_ns;
  124. incr_spec.ns = bp->tsu_incr.ns;
  125. /* scaling: unused(8bit) | ns(8bit) | fractions(16bit) */
  126. word = ((u64)incr_spec.ns << GEM_SUBNSINCR_SIZE) + incr_spec.sub_ns;
  127. adj = (u64)scaled_ppm * word;
  128. /* Divide with rounding, equivalent to floating dividing:
  129. * (temp / USEC_PER_SEC) + 0.5
  130. */
  131. adj += (USEC_PER_SEC >> 1);
  132. adj >>= GEM_SUBNSINCR_SIZE; /* remove fractions */
  133. adj = div_u64(adj, USEC_PER_SEC);
  134. adj = neg_adj ? (word - adj) : (word + adj);
  135. incr_spec.ns = (adj >> GEM_SUBNSINCR_SIZE)
  136. & ((1 << GEM_NSINCR_SIZE) - 1);
  137. incr_spec.sub_ns = adj & ((1 << GEM_SUBNSINCR_SIZE) - 1);
  138. gem_tsu_incr_set(bp, &incr_spec);
  139. return 0;
  140. }
  141. static int gem_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  142. {
  143. struct macb *bp = container_of(ptp, struct macb, ptp_clock_info);
  144. struct timespec64 now, then = ns_to_timespec64(delta);
  145. u32 adj, sign = 0;
  146. if (delta < 0) {
  147. sign = 1;
  148. delta = -delta;
  149. }
  150. if (delta > TSU_NSEC_MAX_VAL) {
  151. gem_tsu_get_time(&bp->ptp_clock_info, &now);
  152. now = timespec64_add(now, then);
  153. gem_tsu_set_time(&bp->ptp_clock_info,
  154. (const struct timespec64 *)&now);
  155. } else {
  156. adj = (sign << GEM_ADDSUB_OFFSET) | delta;
  157. gem_writel(bp, TA, adj);
  158. }
  159. return 0;
  160. }
  161. static int gem_ptp_enable(struct ptp_clock_info *ptp,
  162. struct ptp_clock_request *rq, int on)
  163. {
  164. return -EOPNOTSUPP;
  165. }
  166. static const struct ptp_clock_info gem_ptp_caps_template = {
  167. .owner = THIS_MODULE,
  168. .name = GEM_PTP_TIMER_NAME,
  169. .max_adj = 0,
  170. .n_alarm = 0,
  171. .n_ext_ts = 0,
  172. .n_per_out = 0,
  173. .n_pins = 0,
  174. .pps = 1,
  175. .adjfine = gem_ptp_adjfine,
  176. .adjtime = gem_ptp_adjtime,
  177. .gettime64 = gem_tsu_get_time,
  178. .settime64 = gem_tsu_set_time,
  179. .enable = gem_ptp_enable,
  180. };
  181. static void gem_ptp_init_timer(struct macb *bp)
  182. {
  183. u32 rem = 0;
  184. u64 adj;
  185. bp->tsu_incr.ns = div_u64_rem(NSEC_PER_SEC, bp->tsu_rate, &rem);
  186. if (rem) {
  187. adj = rem;
  188. adj <<= GEM_SUBNSINCR_SIZE;
  189. bp->tsu_incr.sub_ns = div_u64(adj, bp->tsu_rate);
  190. } else {
  191. bp->tsu_incr.sub_ns = 0;
  192. }
  193. }
  194. static void gem_ptp_init_tsu(struct macb *bp)
  195. {
  196. struct timespec64 ts;
  197. /* 1. get current system time */
  198. ts = ns_to_timespec64(ktime_to_ns(ktime_get_real()));
  199. /* 2. set ptp timer */
  200. gem_tsu_set_time(&bp->ptp_clock_info, &ts);
  201. /* 3. set PTP timer increment value to BASE_INCREMENT */
  202. gem_tsu_incr_set(bp, &bp->tsu_incr);
  203. gem_writel(bp, TA, 0);
  204. }
  205. static void gem_ptp_clear_timer(struct macb *bp)
  206. {
  207. bp->tsu_incr.sub_ns = 0;
  208. bp->tsu_incr.ns = 0;
  209. gem_writel(bp, TISUBN, GEM_BF(SUBNSINCR, 0));
  210. gem_writel(bp, TI, GEM_BF(NSINCR, 0));
  211. gem_writel(bp, TA, 0);
  212. }
  213. static int gem_hw_timestamp(struct macb *bp, u32 dma_desc_ts_1,
  214. u32 dma_desc_ts_2, struct timespec64 *ts)
  215. {
  216. struct timespec64 tsu;
  217. ts->tv_sec = (GEM_BFEXT(DMA_SECH, dma_desc_ts_2) << GEM_DMA_SECL_SIZE) |
  218. GEM_BFEXT(DMA_SECL, dma_desc_ts_1);
  219. ts->tv_nsec = GEM_BFEXT(DMA_NSEC, dma_desc_ts_1);
  220. /* TSU overlapping workaround
  221. * The timestamp only contains lower few bits of seconds,
  222. * so add value from 1588 timer
  223. */
  224. gem_tsu_get_time(&bp->ptp_clock_info, &tsu);
  225. /* If the top bit is set in the timestamp,
  226. * but not in 1588 timer, it has rolled over,
  227. * so subtract max size
  228. */
  229. if ((ts->tv_sec & (GEM_DMA_SEC_TOP >> 1)) &&
  230. !(tsu.tv_sec & (GEM_DMA_SEC_TOP >> 1)))
  231. ts->tv_sec -= GEM_DMA_SEC_TOP;
  232. ts->tv_sec += ((~GEM_DMA_SEC_MASK) & tsu.tv_sec);
  233. return 0;
  234. }
  235. void gem_ptp_rxstamp(struct macb *bp, struct sk_buff *skb,
  236. struct macb_dma_desc *desc)
  237. {
  238. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  239. struct macb_dma_desc_ptp *desc_ptp;
  240. struct timespec64 ts;
  241. if (GEM_BFEXT(DMA_RXVALID, desc->addr)) {
  242. desc_ptp = macb_ptp_desc(bp, desc);
  243. gem_hw_timestamp(bp, desc_ptp->ts_1, desc_ptp->ts_2, &ts);
  244. memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
  245. shhwtstamps->hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
  246. }
  247. }
  248. static void gem_tstamp_tx(struct macb *bp, struct sk_buff *skb,
  249. struct macb_dma_desc_ptp *desc_ptp)
  250. {
  251. struct skb_shared_hwtstamps shhwtstamps;
  252. struct timespec64 ts;
  253. gem_hw_timestamp(bp, desc_ptp->ts_1, desc_ptp->ts_2, &ts);
  254. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  255. shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
  256. skb_tstamp_tx(skb, &shhwtstamps);
  257. }
  258. int gem_ptp_txstamp(struct macb_queue *queue, struct sk_buff *skb,
  259. struct macb_dma_desc *desc)
  260. {
  261. unsigned long tail = READ_ONCE(queue->tx_ts_tail);
  262. unsigned long head = queue->tx_ts_head;
  263. struct macb_dma_desc_ptp *desc_ptp;
  264. struct gem_tx_ts *tx_timestamp;
  265. if (!GEM_BFEXT(DMA_TXVALID, desc->ctrl))
  266. return -EINVAL;
  267. if (CIRC_SPACE(head, tail, PTP_TS_BUFFER_SIZE) == 0)
  268. return -ENOMEM;
  269. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  270. desc_ptp = macb_ptp_desc(queue->bp, desc);
  271. tx_timestamp = &queue->tx_timestamps[head];
  272. tx_timestamp->skb = skb;
  273. /* ensure ts_1/ts_2 is loaded after ctrl (TX_USED check) */
  274. dma_rmb();
  275. tx_timestamp->desc_ptp.ts_1 = desc_ptp->ts_1;
  276. tx_timestamp->desc_ptp.ts_2 = desc_ptp->ts_2;
  277. /* move head */
  278. smp_store_release(&queue->tx_ts_head,
  279. (head + 1) & (PTP_TS_BUFFER_SIZE - 1));
  280. schedule_work(&queue->tx_ts_task);
  281. return 0;
  282. }
  283. static void gem_tx_timestamp_flush(struct work_struct *work)
  284. {
  285. struct macb_queue *queue =
  286. container_of(work, struct macb_queue, tx_ts_task);
  287. unsigned long head, tail;
  288. struct gem_tx_ts *tx_ts;
  289. /* take current head */
  290. head = smp_load_acquire(&queue->tx_ts_head);
  291. tail = queue->tx_ts_tail;
  292. while (CIRC_CNT(head, tail, PTP_TS_BUFFER_SIZE)) {
  293. tx_ts = &queue->tx_timestamps[tail];
  294. gem_tstamp_tx(queue->bp, tx_ts->skb, &tx_ts->desc_ptp);
  295. /* cleanup */
  296. dev_kfree_skb_any(tx_ts->skb);
  297. /* remove old tail */
  298. smp_store_release(&queue->tx_ts_tail,
  299. (tail + 1) & (PTP_TS_BUFFER_SIZE - 1));
  300. tail = queue->tx_ts_tail;
  301. }
  302. }
  303. void gem_ptp_init(struct net_device *dev)
  304. {
  305. struct macb *bp = netdev_priv(dev);
  306. struct macb_queue *queue;
  307. unsigned int q;
  308. bp->ptp_clock_info = gem_ptp_caps_template;
  309. /* nominal frequency and maximum adjustment in ppb */
  310. bp->tsu_rate = bp->ptp_info->get_tsu_rate(bp);
  311. bp->ptp_clock_info.max_adj = bp->ptp_info->get_ptp_max_adj();
  312. gem_ptp_init_timer(bp);
  313. bp->ptp_clock = ptp_clock_register(&bp->ptp_clock_info, &dev->dev);
  314. if (IS_ERR(bp->ptp_clock)) {
  315. pr_err("ptp clock register failed: %ld\n",
  316. PTR_ERR(bp->ptp_clock));
  317. bp->ptp_clock = NULL;
  318. return;
  319. } else if (bp->ptp_clock == NULL) {
  320. pr_err("ptp clock register failed\n");
  321. return;
  322. }
  323. spin_lock_init(&bp->tsu_clk_lock);
  324. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  325. queue->tx_ts_head = 0;
  326. queue->tx_ts_tail = 0;
  327. INIT_WORK(&queue->tx_ts_task, gem_tx_timestamp_flush);
  328. }
  329. gem_ptp_init_tsu(bp);
  330. dev_info(&bp->pdev->dev, "%s ptp clock registered.\n",
  331. GEM_PTP_TIMER_NAME);
  332. }
  333. void gem_ptp_remove(struct net_device *ndev)
  334. {
  335. struct macb *bp = netdev_priv(ndev);
  336. if (bp->ptp_clock)
  337. ptp_clock_unregister(bp->ptp_clock);
  338. gem_ptp_clear_timer(bp);
  339. dev_info(&bp->pdev->dev, "%s ptp clock unregistered.\n",
  340. GEM_PTP_TIMER_NAME);
  341. }
  342. static int gem_ptp_set_ts_mode(struct macb *bp,
  343. enum macb_bd_control tx_bd_control,
  344. enum macb_bd_control rx_bd_control)
  345. {
  346. gem_writel(bp, TXBDCTRL, GEM_BF(TXTSMODE, tx_bd_control));
  347. gem_writel(bp, RXBDCTRL, GEM_BF(RXTSMODE, rx_bd_control));
  348. return 0;
  349. }
  350. int gem_get_hwtst(struct net_device *dev, struct ifreq *rq)
  351. {
  352. struct hwtstamp_config *tstamp_config;
  353. struct macb *bp = netdev_priv(dev);
  354. tstamp_config = &bp->tstamp_config;
  355. if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0)
  356. return -EOPNOTSUPP;
  357. if (copy_to_user(rq->ifr_data, tstamp_config, sizeof(*tstamp_config)))
  358. return -EFAULT;
  359. else
  360. return 0;
  361. }
  362. static int gem_ptp_set_one_step_sync(struct macb *bp, u8 enable)
  363. {
  364. u32 reg_val;
  365. reg_val = macb_readl(bp, NCR);
  366. if (enable)
  367. macb_writel(bp, NCR, reg_val | MACB_BIT(OSSMODE));
  368. else
  369. macb_writel(bp, NCR, reg_val & ~MACB_BIT(OSSMODE));
  370. return 0;
  371. }
  372. int gem_set_hwtst(struct net_device *dev, struct ifreq *ifr, int cmd)
  373. {
  374. enum macb_bd_control tx_bd_control = TSTAMP_DISABLED;
  375. enum macb_bd_control rx_bd_control = TSTAMP_DISABLED;
  376. struct hwtstamp_config *tstamp_config;
  377. struct macb *bp = netdev_priv(dev);
  378. u32 regval;
  379. tstamp_config = &bp->tstamp_config;
  380. if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0)
  381. return -EOPNOTSUPP;
  382. if (copy_from_user(tstamp_config, ifr->ifr_data,
  383. sizeof(*tstamp_config)))
  384. return -EFAULT;
  385. /* reserved for future extensions */
  386. if (tstamp_config->flags)
  387. return -EINVAL;
  388. switch (tstamp_config->tx_type) {
  389. case HWTSTAMP_TX_OFF:
  390. break;
  391. case HWTSTAMP_TX_ONESTEP_SYNC:
  392. if (gem_ptp_set_one_step_sync(bp, 1) != 0)
  393. return -ERANGE;
  394. /* fall through */
  395. case HWTSTAMP_TX_ON:
  396. tx_bd_control = TSTAMP_ALL_FRAMES;
  397. break;
  398. default:
  399. return -ERANGE;
  400. }
  401. switch (tstamp_config->rx_filter) {
  402. case HWTSTAMP_FILTER_NONE:
  403. break;
  404. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  405. break;
  406. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  407. break;
  408. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  409. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  410. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  411. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  412. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  413. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  414. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  415. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  416. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  417. rx_bd_control = TSTAMP_ALL_PTP_FRAMES;
  418. tstamp_config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  419. regval = macb_readl(bp, NCR);
  420. macb_writel(bp, NCR, (regval | MACB_BIT(SRTSM)));
  421. break;
  422. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  423. case HWTSTAMP_FILTER_ALL:
  424. rx_bd_control = TSTAMP_ALL_FRAMES;
  425. tstamp_config->rx_filter = HWTSTAMP_FILTER_ALL;
  426. break;
  427. default:
  428. tstamp_config->rx_filter = HWTSTAMP_FILTER_NONE;
  429. return -ERANGE;
  430. }
  431. if (gem_ptp_set_ts_mode(bp, tx_bd_control, rx_bd_control) != 0)
  432. return -ERANGE;
  433. if (copy_to_user(ifr->ifr_data, tstamp_config, sizeof(*tstamp_config)))
  434. return -EFAULT;
  435. else
  436. return 0;
  437. }