macb_ptp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. if (sign)
  150. now = timespec64_sub(now, then);
  151. else
  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. 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. case HWTSTAMP_TX_ON:
  393. tx_bd_control = TSTAMP_ALL_FRAMES;
  394. break;
  395. default:
  396. return -ERANGE;
  397. }
  398. switch (tstamp_config->rx_filter) {
  399. case HWTSTAMP_FILTER_NONE:
  400. break;
  401. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  402. break;
  403. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  404. break;
  405. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  406. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  407. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  408. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  409. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  410. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  411. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  412. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  413. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  414. rx_bd_control = TSTAMP_ALL_PTP_FRAMES;
  415. tstamp_config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  416. regval = macb_readl(bp, NCR);
  417. macb_writel(bp, NCR, (regval | MACB_BIT(SRTSM)));
  418. break;
  419. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  420. case HWTSTAMP_FILTER_ALL:
  421. rx_bd_control = TSTAMP_ALL_FRAMES;
  422. tstamp_config->rx_filter = HWTSTAMP_FILTER_ALL;
  423. break;
  424. default:
  425. tstamp_config->rx_filter = HWTSTAMP_FILTER_NONE;
  426. return -ERANGE;
  427. }
  428. if (gem_ptp_set_ts_mode(bp, tx_bd_control, rx_bd_control) != 0)
  429. return -ERANGE;
  430. if (copy_to_user(ifr->ifr_data, tstamp_config, sizeof(*tstamp_config)))
  431. return -EFAULT;
  432. else
  433. return 0;
  434. }