cpts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * TI Common Platform Time Sync
  3. *
  4. * Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/err.h>
  21. #include <linux/if.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/module.h>
  24. #include <linux/net_tstamp.h>
  25. #include <linux/ptp_classify.h>
  26. #include <linux/time.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/if_vlan.h>
  31. #include "cpts.h"
  32. #define CPTS_SKB_TX_WORK_TIMEOUT 1 /* jiffies */
  33. struct cpts_skb_cb_data {
  34. unsigned long tmo;
  35. };
  36. #define cpts_read32(c, r) readl_relaxed(&c->reg->r)
  37. #define cpts_write32(c, v, r) writel_relaxed(v, &c->reg->r)
  38. static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
  39. u16 ts_seqid, u8 ts_msgtype);
  40. static int event_expired(struct cpts_event *event)
  41. {
  42. return time_after(jiffies, event->tmo);
  43. }
  44. static int event_type(struct cpts_event *event)
  45. {
  46. return (event->high >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  47. }
  48. static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)
  49. {
  50. u32 r = cpts_read32(cpts, intstat_raw);
  51. if (r & TS_PEND_RAW) {
  52. *high = cpts_read32(cpts, event_high);
  53. *low = cpts_read32(cpts, event_low);
  54. cpts_write32(cpts, EVENT_POP, event_pop);
  55. return 0;
  56. }
  57. return -1;
  58. }
  59. static int cpts_purge_events(struct cpts *cpts)
  60. {
  61. struct list_head *this, *next;
  62. struct cpts_event *event;
  63. int removed = 0;
  64. list_for_each_safe(this, next, &cpts->events) {
  65. event = list_entry(this, struct cpts_event, list);
  66. if (event_expired(event)) {
  67. list_del_init(&event->list);
  68. list_add(&event->list, &cpts->pool);
  69. ++removed;
  70. }
  71. }
  72. if (removed)
  73. pr_debug("cpts: event pool cleaned up %d\n", removed);
  74. return removed ? 0 : -1;
  75. }
  76. static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event)
  77. {
  78. struct sk_buff *skb, *tmp;
  79. u16 seqid;
  80. u8 mtype;
  81. bool found = false;
  82. mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
  83. seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
  84. /* no need to grab txq.lock as access is always done under cpts->lock */
  85. skb_queue_walk_safe(&cpts->txq, skb, tmp) {
  86. struct skb_shared_hwtstamps ssh;
  87. unsigned int class = ptp_classify_raw(skb);
  88. struct cpts_skb_cb_data *skb_cb =
  89. (struct cpts_skb_cb_data *)skb->cb;
  90. if (cpts_match(skb, class, seqid, mtype)) {
  91. u64 ns = timecounter_cyc2time(&cpts->tc, event->low);
  92. memset(&ssh, 0, sizeof(ssh));
  93. ssh.hwtstamp = ns_to_ktime(ns);
  94. skb_tstamp_tx(skb, &ssh);
  95. found = true;
  96. __skb_unlink(skb, &cpts->txq);
  97. dev_consume_skb_any(skb);
  98. dev_dbg(cpts->dev, "match tx timestamp mtype %u seqid %04x\n",
  99. mtype, seqid);
  100. } else if (time_after(jiffies, skb_cb->tmo)) {
  101. /* timeout any expired skbs over 1s */
  102. dev_dbg(cpts->dev,
  103. "expiring tx timestamp mtype %u seqid %04x\n",
  104. mtype, seqid);
  105. __skb_unlink(skb, &cpts->txq);
  106. dev_consume_skb_any(skb);
  107. }
  108. }
  109. return found;
  110. }
  111. /*
  112. * Returns zero if matching event type was found.
  113. */
  114. static int cpts_fifo_read(struct cpts *cpts, int match)
  115. {
  116. int i, type = -1;
  117. u32 hi, lo;
  118. struct cpts_event *event;
  119. for (i = 0; i < CPTS_FIFO_DEPTH; i++) {
  120. if (cpts_fifo_pop(cpts, &hi, &lo))
  121. break;
  122. if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) {
  123. pr_err("cpts: event pool empty\n");
  124. return -1;
  125. }
  126. event = list_first_entry(&cpts->pool, struct cpts_event, list);
  127. event->tmo = jiffies + 2;
  128. event->high = hi;
  129. event->low = lo;
  130. type = event_type(event);
  131. switch (type) {
  132. case CPTS_EV_TX:
  133. if (cpts_match_tx_ts(cpts, event)) {
  134. /* if the new event matches an existing skb,
  135. * then don't queue it
  136. */
  137. break;
  138. }
  139. case CPTS_EV_PUSH:
  140. case CPTS_EV_RX:
  141. list_del_init(&event->list);
  142. list_add_tail(&event->list, &cpts->events);
  143. break;
  144. case CPTS_EV_ROLL:
  145. case CPTS_EV_HALF:
  146. case CPTS_EV_HW:
  147. break;
  148. default:
  149. pr_err("cpts: unknown event type\n");
  150. break;
  151. }
  152. if (type == match)
  153. break;
  154. }
  155. return type == match ? 0 : -1;
  156. }
  157. static u64 cpts_systim_read(const struct cyclecounter *cc)
  158. {
  159. u64 val = 0;
  160. struct cpts_event *event;
  161. struct list_head *this, *next;
  162. struct cpts *cpts = container_of(cc, struct cpts, cc);
  163. cpts_write32(cpts, TS_PUSH, ts_push);
  164. if (cpts_fifo_read(cpts, CPTS_EV_PUSH))
  165. pr_err("cpts: unable to obtain a time stamp\n");
  166. list_for_each_safe(this, next, &cpts->events) {
  167. event = list_entry(this, struct cpts_event, list);
  168. if (event_type(event) == CPTS_EV_PUSH) {
  169. list_del_init(&event->list);
  170. list_add(&event->list, &cpts->pool);
  171. val = event->low;
  172. break;
  173. }
  174. }
  175. return val;
  176. }
  177. /* PTP clock operations */
  178. static int cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  179. {
  180. u64 adj;
  181. u32 diff, mult;
  182. int neg_adj = 0;
  183. unsigned long flags;
  184. struct cpts *cpts = container_of(ptp, struct cpts, info);
  185. if (ppb < 0) {
  186. neg_adj = 1;
  187. ppb = -ppb;
  188. }
  189. mult = cpts->cc_mult;
  190. adj = mult;
  191. adj *= ppb;
  192. diff = div_u64(adj, 1000000000ULL);
  193. spin_lock_irqsave(&cpts->lock, flags);
  194. timecounter_read(&cpts->tc);
  195. cpts->cc.mult = neg_adj ? mult - diff : mult + diff;
  196. spin_unlock_irqrestore(&cpts->lock, flags);
  197. return 0;
  198. }
  199. static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  200. {
  201. unsigned long flags;
  202. struct cpts *cpts = container_of(ptp, struct cpts, info);
  203. spin_lock_irqsave(&cpts->lock, flags);
  204. timecounter_adjtime(&cpts->tc, delta);
  205. spin_unlock_irqrestore(&cpts->lock, flags);
  206. return 0;
  207. }
  208. static int cpts_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  209. {
  210. u64 ns;
  211. unsigned long flags;
  212. struct cpts *cpts = container_of(ptp, struct cpts, info);
  213. spin_lock_irqsave(&cpts->lock, flags);
  214. ns = timecounter_read(&cpts->tc);
  215. spin_unlock_irqrestore(&cpts->lock, flags);
  216. *ts = ns_to_timespec64(ns);
  217. return 0;
  218. }
  219. static int cpts_ptp_settime(struct ptp_clock_info *ptp,
  220. const struct timespec64 *ts)
  221. {
  222. u64 ns;
  223. unsigned long flags;
  224. struct cpts *cpts = container_of(ptp, struct cpts, info);
  225. ns = timespec64_to_ns(ts);
  226. spin_lock_irqsave(&cpts->lock, flags);
  227. timecounter_init(&cpts->tc, &cpts->cc, ns);
  228. spin_unlock_irqrestore(&cpts->lock, flags);
  229. return 0;
  230. }
  231. static int cpts_ptp_enable(struct ptp_clock_info *ptp,
  232. struct ptp_clock_request *rq, int on)
  233. {
  234. return -EOPNOTSUPP;
  235. }
  236. static long cpts_overflow_check(struct ptp_clock_info *ptp)
  237. {
  238. struct cpts *cpts = container_of(ptp, struct cpts, info);
  239. unsigned long delay = cpts->ov_check_period;
  240. struct timespec64 ts;
  241. unsigned long flags;
  242. spin_lock_irqsave(&cpts->lock, flags);
  243. ts = ns_to_timespec64(timecounter_read(&cpts->tc));
  244. if (!skb_queue_empty(&cpts->txq))
  245. delay = CPTS_SKB_TX_WORK_TIMEOUT;
  246. spin_unlock_irqrestore(&cpts->lock, flags);
  247. pr_debug("cpts overflow check at %lld.%09ld\n",
  248. (long long)ts.tv_sec, ts.tv_nsec);
  249. return (long)delay;
  250. }
  251. static const struct ptp_clock_info cpts_info = {
  252. .owner = THIS_MODULE,
  253. .name = "CTPS timer",
  254. .max_adj = 1000000,
  255. .n_ext_ts = 0,
  256. .n_pins = 0,
  257. .pps = 0,
  258. .adjfreq = cpts_ptp_adjfreq,
  259. .adjtime = cpts_ptp_adjtime,
  260. .gettime64 = cpts_ptp_gettime,
  261. .settime64 = cpts_ptp_settime,
  262. .enable = cpts_ptp_enable,
  263. .do_aux_work = cpts_overflow_check,
  264. };
  265. static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
  266. u16 ts_seqid, u8 ts_msgtype)
  267. {
  268. u16 *seqid;
  269. unsigned int offset = 0;
  270. u8 *msgtype, *data = skb->data;
  271. if (ptp_class & PTP_CLASS_VLAN)
  272. offset += VLAN_HLEN;
  273. switch (ptp_class & PTP_CLASS_PMASK) {
  274. case PTP_CLASS_IPV4:
  275. offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
  276. break;
  277. case PTP_CLASS_IPV6:
  278. offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
  279. break;
  280. case PTP_CLASS_L2:
  281. offset += ETH_HLEN;
  282. break;
  283. default:
  284. return 0;
  285. }
  286. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  287. return 0;
  288. if (unlikely(ptp_class & PTP_CLASS_V1))
  289. msgtype = data + offset + OFF_PTP_CONTROL;
  290. else
  291. msgtype = data + offset;
  292. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  293. return (ts_msgtype == (*msgtype & 0xf) && ts_seqid == ntohs(*seqid));
  294. }
  295. static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
  296. {
  297. u64 ns = 0;
  298. struct cpts_event *event;
  299. struct list_head *this, *next;
  300. unsigned int class = ptp_classify_raw(skb);
  301. unsigned long flags;
  302. u16 seqid;
  303. u8 mtype;
  304. if (class == PTP_CLASS_NONE)
  305. return 0;
  306. spin_lock_irqsave(&cpts->lock, flags);
  307. cpts_fifo_read(cpts, -1);
  308. list_for_each_safe(this, next, &cpts->events) {
  309. event = list_entry(this, struct cpts_event, list);
  310. if (event_expired(event)) {
  311. list_del_init(&event->list);
  312. list_add(&event->list, &cpts->pool);
  313. continue;
  314. }
  315. mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
  316. seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
  317. if (ev_type == event_type(event) &&
  318. cpts_match(skb, class, seqid, mtype)) {
  319. ns = timecounter_cyc2time(&cpts->tc, event->low);
  320. list_del_init(&event->list);
  321. list_add(&event->list, &cpts->pool);
  322. break;
  323. }
  324. }
  325. if (ev_type == CPTS_EV_TX && !ns) {
  326. struct cpts_skb_cb_data *skb_cb =
  327. (struct cpts_skb_cb_data *)skb->cb;
  328. /* Not found, add frame to queue for processing later.
  329. * The periodic FIFO check will handle this.
  330. */
  331. skb_get(skb);
  332. /* get the timestamp for timeouts */
  333. skb_cb->tmo = jiffies + msecs_to_jiffies(100);
  334. __skb_queue_tail(&cpts->txq, skb);
  335. ptp_schedule_worker(cpts->clock, 0);
  336. }
  337. spin_unlock_irqrestore(&cpts->lock, flags);
  338. return ns;
  339. }
  340. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  341. {
  342. u64 ns;
  343. struct skb_shared_hwtstamps *ssh;
  344. if (!cpts->rx_enable)
  345. return;
  346. ns = cpts_find_ts(cpts, skb, CPTS_EV_RX);
  347. if (!ns)
  348. return;
  349. ssh = skb_hwtstamps(skb);
  350. memset(ssh, 0, sizeof(*ssh));
  351. ssh->hwtstamp = ns_to_ktime(ns);
  352. }
  353. EXPORT_SYMBOL_GPL(cpts_rx_timestamp);
  354. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  355. {
  356. u64 ns;
  357. struct skb_shared_hwtstamps ssh;
  358. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  359. return;
  360. ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
  361. if (!ns)
  362. return;
  363. memset(&ssh, 0, sizeof(ssh));
  364. ssh.hwtstamp = ns_to_ktime(ns);
  365. skb_tstamp_tx(skb, &ssh);
  366. }
  367. EXPORT_SYMBOL_GPL(cpts_tx_timestamp);
  368. int cpts_register(struct cpts *cpts)
  369. {
  370. int err, i;
  371. skb_queue_head_init(&cpts->txq);
  372. INIT_LIST_HEAD(&cpts->events);
  373. INIT_LIST_HEAD(&cpts->pool);
  374. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  375. list_add(&cpts->pool_data[i].list, &cpts->pool);
  376. clk_enable(cpts->refclk);
  377. cpts_write32(cpts, CPTS_EN, control);
  378. cpts_write32(cpts, TS_PEND_EN, int_enable);
  379. timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real()));
  380. cpts->clock = ptp_clock_register(&cpts->info, cpts->dev);
  381. if (IS_ERR(cpts->clock)) {
  382. err = PTR_ERR(cpts->clock);
  383. cpts->clock = NULL;
  384. goto err_ptp;
  385. }
  386. cpts->phc_index = ptp_clock_index(cpts->clock);
  387. ptp_schedule_worker(cpts->clock, cpts->ov_check_period);
  388. return 0;
  389. err_ptp:
  390. clk_disable(cpts->refclk);
  391. return err;
  392. }
  393. EXPORT_SYMBOL_GPL(cpts_register);
  394. void cpts_unregister(struct cpts *cpts)
  395. {
  396. if (WARN_ON(!cpts->clock))
  397. return;
  398. ptp_clock_unregister(cpts->clock);
  399. cpts->clock = NULL;
  400. cpts_write32(cpts, 0, int_enable);
  401. cpts_write32(cpts, 0, control);
  402. /* Drop all packet */
  403. skb_queue_purge(&cpts->txq);
  404. clk_disable(cpts->refclk);
  405. }
  406. EXPORT_SYMBOL_GPL(cpts_unregister);
  407. static void cpts_calc_mult_shift(struct cpts *cpts)
  408. {
  409. u64 frac, maxsec, ns;
  410. u32 freq;
  411. freq = clk_get_rate(cpts->refclk);
  412. /* Calc the maximum number of seconds which we can run before
  413. * wrapping around.
  414. */
  415. maxsec = cpts->cc.mask;
  416. do_div(maxsec, freq);
  417. /* limit conversation rate to 10 sec as higher values will produce
  418. * too small mult factors and so reduce the conversion accuracy
  419. */
  420. if (maxsec > 10)
  421. maxsec = 10;
  422. /* Calc overflow check period (maxsec / 2) */
  423. cpts->ov_check_period = (HZ * maxsec) / 2;
  424. dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n",
  425. cpts->ov_check_period);
  426. if (cpts->cc.mult || cpts->cc.shift)
  427. return;
  428. clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift,
  429. freq, NSEC_PER_SEC, maxsec);
  430. frac = 0;
  431. ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac);
  432. dev_info(cpts->dev,
  433. "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n",
  434. freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC));
  435. }
  436. static int cpts_of_parse(struct cpts *cpts, struct device_node *node)
  437. {
  438. int ret = -EINVAL;
  439. u32 prop;
  440. if (!of_property_read_u32(node, "cpts_clock_mult", &prop))
  441. cpts->cc.mult = prop;
  442. if (!of_property_read_u32(node, "cpts_clock_shift", &prop))
  443. cpts->cc.shift = prop;
  444. if ((cpts->cc.mult && !cpts->cc.shift) ||
  445. (!cpts->cc.mult && cpts->cc.shift))
  446. goto of_error;
  447. return 0;
  448. of_error:
  449. dev_err(cpts->dev, "CPTS: Missing property in the DT.\n");
  450. return ret;
  451. }
  452. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  453. struct device_node *node)
  454. {
  455. struct cpts *cpts;
  456. int ret;
  457. cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
  458. if (!cpts)
  459. return ERR_PTR(-ENOMEM);
  460. cpts->dev = dev;
  461. cpts->reg = (struct cpsw_cpts __iomem *)regs;
  462. spin_lock_init(&cpts->lock);
  463. ret = cpts_of_parse(cpts, node);
  464. if (ret)
  465. return ERR_PTR(ret);
  466. cpts->refclk = devm_clk_get(dev, "cpts");
  467. if (IS_ERR(cpts->refclk)) {
  468. dev_err(dev, "Failed to get cpts refclk\n");
  469. return ERR_CAST(cpts->refclk);
  470. }
  471. clk_prepare(cpts->refclk);
  472. cpts->cc.read = cpts_systim_read;
  473. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  474. cpts->info = cpts_info;
  475. cpts_calc_mult_shift(cpts);
  476. /* save cc.mult original value as it can be modified
  477. * by cpts_ptp_adjfreq().
  478. */
  479. cpts->cc_mult = cpts->cc.mult;
  480. return cpts;
  481. }
  482. EXPORT_SYMBOL_GPL(cpts_create);
  483. void cpts_release(struct cpts *cpts)
  484. {
  485. if (!cpts)
  486. return;
  487. if (WARN_ON(!cpts->refclk))
  488. return;
  489. clk_unprepare(cpts->refclk);
  490. }
  491. EXPORT_SYMBOL_GPL(cpts_release);
  492. MODULE_LICENSE("GPL v2");
  493. MODULE_DESCRIPTION("TI CPTS driver");
  494. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");