cpts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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.%09lu\n", ts.tv_sec, ts.tv_nsec);
  248. return (long)delay;
  249. }
  250. static const struct ptp_clock_info cpts_info = {
  251. .owner = THIS_MODULE,
  252. .name = "CTPS timer",
  253. .max_adj = 1000000,
  254. .n_ext_ts = 0,
  255. .n_pins = 0,
  256. .pps = 0,
  257. .adjfreq = cpts_ptp_adjfreq,
  258. .adjtime = cpts_ptp_adjtime,
  259. .gettime64 = cpts_ptp_gettime,
  260. .settime64 = cpts_ptp_settime,
  261. .enable = cpts_ptp_enable,
  262. .do_aux_work = cpts_overflow_check,
  263. };
  264. static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
  265. u16 ts_seqid, u8 ts_msgtype)
  266. {
  267. u16 *seqid;
  268. unsigned int offset = 0;
  269. u8 *msgtype, *data = skb->data;
  270. if (ptp_class & PTP_CLASS_VLAN)
  271. offset += VLAN_HLEN;
  272. switch (ptp_class & PTP_CLASS_PMASK) {
  273. case PTP_CLASS_IPV4:
  274. offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
  275. break;
  276. case PTP_CLASS_IPV6:
  277. offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
  278. break;
  279. case PTP_CLASS_L2:
  280. offset += ETH_HLEN;
  281. break;
  282. default:
  283. return 0;
  284. }
  285. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  286. return 0;
  287. if (unlikely(ptp_class & PTP_CLASS_V1))
  288. msgtype = data + offset + OFF_PTP_CONTROL;
  289. else
  290. msgtype = data + offset;
  291. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  292. return (ts_msgtype == (*msgtype & 0xf) && ts_seqid == ntohs(*seqid));
  293. }
  294. static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
  295. {
  296. u64 ns = 0;
  297. struct cpts_event *event;
  298. struct list_head *this, *next;
  299. unsigned int class = ptp_classify_raw(skb);
  300. unsigned long flags;
  301. u16 seqid;
  302. u8 mtype;
  303. if (class == PTP_CLASS_NONE)
  304. return 0;
  305. spin_lock_irqsave(&cpts->lock, flags);
  306. cpts_fifo_read(cpts, -1);
  307. list_for_each_safe(this, next, &cpts->events) {
  308. event = list_entry(this, struct cpts_event, list);
  309. if (event_expired(event)) {
  310. list_del_init(&event->list);
  311. list_add(&event->list, &cpts->pool);
  312. continue;
  313. }
  314. mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
  315. seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
  316. if (ev_type == event_type(event) &&
  317. cpts_match(skb, class, seqid, mtype)) {
  318. ns = timecounter_cyc2time(&cpts->tc, event->low);
  319. list_del_init(&event->list);
  320. list_add(&event->list, &cpts->pool);
  321. break;
  322. }
  323. }
  324. if (ev_type == CPTS_EV_TX && !ns) {
  325. struct cpts_skb_cb_data *skb_cb =
  326. (struct cpts_skb_cb_data *)skb->cb;
  327. /* Not found, add frame to queue for processing later.
  328. * The periodic FIFO check will handle this.
  329. */
  330. skb_get(skb);
  331. /* get the timestamp for timeouts */
  332. skb_cb->tmo = jiffies + msecs_to_jiffies(100);
  333. __skb_queue_tail(&cpts->txq, skb);
  334. ptp_schedule_worker(cpts->clock, 0);
  335. }
  336. spin_unlock_irqrestore(&cpts->lock, flags);
  337. return ns;
  338. }
  339. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  340. {
  341. u64 ns;
  342. struct skb_shared_hwtstamps *ssh;
  343. if (!cpts->rx_enable)
  344. return;
  345. ns = cpts_find_ts(cpts, skb, CPTS_EV_RX);
  346. if (!ns)
  347. return;
  348. ssh = skb_hwtstamps(skb);
  349. memset(ssh, 0, sizeof(*ssh));
  350. ssh->hwtstamp = ns_to_ktime(ns);
  351. }
  352. EXPORT_SYMBOL_GPL(cpts_rx_timestamp);
  353. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  354. {
  355. u64 ns;
  356. struct skb_shared_hwtstamps ssh;
  357. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  358. return;
  359. ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
  360. if (!ns)
  361. return;
  362. memset(&ssh, 0, sizeof(ssh));
  363. ssh.hwtstamp = ns_to_ktime(ns);
  364. skb_tstamp_tx(skb, &ssh);
  365. }
  366. EXPORT_SYMBOL_GPL(cpts_tx_timestamp);
  367. int cpts_register(struct cpts *cpts)
  368. {
  369. int err, i;
  370. skb_queue_head_init(&cpts->txq);
  371. INIT_LIST_HEAD(&cpts->events);
  372. INIT_LIST_HEAD(&cpts->pool);
  373. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  374. list_add(&cpts->pool_data[i].list, &cpts->pool);
  375. clk_enable(cpts->refclk);
  376. cpts_write32(cpts, CPTS_EN, control);
  377. cpts_write32(cpts, TS_PEND_EN, int_enable);
  378. timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real()));
  379. cpts->clock = ptp_clock_register(&cpts->info, cpts->dev);
  380. if (IS_ERR(cpts->clock)) {
  381. err = PTR_ERR(cpts->clock);
  382. cpts->clock = NULL;
  383. goto err_ptp;
  384. }
  385. cpts->phc_index = ptp_clock_index(cpts->clock);
  386. ptp_schedule_worker(cpts->clock, cpts->ov_check_period);
  387. return 0;
  388. err_ptp:
  389. clk_disable(cpts->refclk);
  390. return err;
  391. }
  392. EXPORT_SYMBOL_GPL(cpts_register);
  393. void cpts_unregister(struct cpts *cpts)
  394. {
  395. if (WARN_ON(!cpts->clock))
  396. return;
  397. ptp_clock_unregister(cpts->clock);
  398. cpts->clock = NULL;
  399. cpts_write32(cpts, 0, int_enable);
  400. cpts_write32(cpts, 0, control);
  401. /* Drop all packet */
  402. skb_queue_purge(&cpts->txq);
  403. clk_disable(cpts->refclk);
  404. }
  405. EXPORT_SYMBOL_GPL(cpts_unregister);
  406. static void cpts_calc_mult_shift(struct cpts *cpts)
  407. {
  408. u64 frac, maxsec, ns;
  409. u32 freq;
  410. freq = clk_get_rate(cpts->refclk);
  411. /* Calc the maximum number of seconds which we can run before
  412. * wrapping around.
  413. */
  414. maxsec = cpts->cc.mask;
  415. do_div(maxsec, freq);
  416. /* limit conversation rate to 10 sec as higher values will produce
  417. * too small mult factors and so reduce the conversion accuracy
  418. */
  419. if (maxsec > 10)
  420. maxsec = 10;
  421. /* Calc overflow check period (maxsec / 2) */
  422. cpts->ov_check_period = (HZ * maxsec) / 2;
  423. dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n",
  424. cpts->ov_check_period);
  425. if (cpts->cc.mult || cpts->cc.shift)
  426. return;
  427. clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift,
  428. freq, NSEC_PER_SEC, maxsec);
  429. frac = 0;
  430. ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac);
  431. dev_info(cpts->dev,
  432. "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n",
  433. freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC));
  434. }
  435. static int cpts_of_parse(struct cpts *cpts, struct device_node *node)
  436. {
  437. int ret = -EINVAL;
  438. u32 prop;
  439. if (!of_property_read_u32(node, "cpts_clock_mult", &prop))
  440. cpts->cc.mult = prop;
  441. if (!of_property_read_u32(node, "cpts_clock_shift", &prop))
  442. cpts->cc.shift = prop;
  443. if ((cpts->cc.mult && !cpts->cc.shift) ||
  444. (!cpts->cc.mult && cpts->cc.shift))
  445. goto of_error;
  446. return 0;
  447. of_error:
  448. dev_err(cpts->dev, "CPTS: Missing property in the DT.\n");
  449. return ret;
  450. }
  451. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  452. struct device_node *node)
  453. {
  454. struct cpts *cpts;
  455. int ret;
  456. cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
  457. if (!cpts)
  458. return ERR_PTR(-ENOMEM);
  459. cpts->dev = dev;
  460. cpts->reg = (struct cpsw_cpts __iomem *)regs;
  461. spin_lock_init(&cpts->lock);
  462. ret = cpts_of_parse(cpts, node);
  463. if (ret)
  464. return ERR_PTR(ret);
  465. cpts->refclk = devm_clk_get(dev, "cpts");
  466. if (IS_ERR(cpts->refclk)) {
  467. dev_err(dev, "Failed to get cpts refclk\n");
  468. return ERR_PTR(PTR_ERR(cpts->refclk));
  469. }
  470. clk_prepare(cpts->refclk);
  471. cpts->cc.read = cpts_systim_read;
  472. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  473. cpts->info = cpts_info;
  474. cpts_calc_mult_shift(cpts);
  475. /* save cc.mult original value as it can be modified
  476. * by cpts_ptp_adjfreq().
  477. */
  478. cpts->cc_mult = cpts->cc.mult;
  479. return cpts;
  480. }
  481. EXPORT_SYMBOL_GPL(cpts_create);
  482. void cpts_release(struct cpts *cpts)
  483. {
  484. if (!cpts)
  485. return;
  486. if (WARN_ON(!cpts->refclk))
  487. return;
  488. clk_unprepare(cpts->refclk);
  489. }
  490. EXPORT_SYMBOL_GPL(cpts_release);
  491. MODULE_LICENSE("GPL v2");
  492. MODULE_DESCRIPTION("TI CPTS driver");
  493. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");