cpts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. break;
  101. }
  102. if (time_after(jiffies, skb_cb->tmo)) {
  103. /* timeout any expired skbs over 1s */
  104. dev_dbg(cpts->dev, "expiring tx timestamp from txq\n");
  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. /* fall through */
  140. case CPTS_EV_PUSH:
  141. case CPTS_EV_RX:
  142. list_del_init(&event->list);
  143. list_add_tail(&event->list, &cpts->events);
  144. break;
  145. case CPTS_EV_ROLL:
  146. case CPTS_EV_HALF:
  147. case CPTS_EV_HW:
  148. break;
  149. default:
  150. pr_err("cpts: unknown event type\n");
  151. break;
  152. }
  153. if (type == match)
  154. break;
  155. }
  156. return type == match ? 0 : -1;
  157. }
  158. static u64 cpts_systim_read(const struct cyclecounter *cc)
  159. {
  160. u64 val = 0;
  161. struct cpts_event *event;
  162. struct list_head *this, *next;
  163. struct cpts *cpts = container_of(cc, struct cpts, cc);
  164. cpts_write32(cpts, TS_PUSH, ts_push);
  165. if (cpts_fifo_read(cpts, CPTS_EV_PUSH))
  166. pr_err("cpts: unable to obtain a time stamp\n");
  167. list_for_each_safe(this, next, &cpts->events) {
  168. event = list_entry(this, struct cpts_event, list);
  169. if (event_type(event) == CPTS_EV_PUSH) {
  170. list_del_init(&event->list);
  171. list_add(&event->list, &cpts->pool);
  172. val = event->low;
  173. break;
  174. }
  175. }
  176. return val;
  177. }
  178. /* PTP clock operations */
  179. static int cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  180. {
  181. u64 adj;
  182. u32 diff, mult;
  183. int neg_adj = 0;
  184. unsigned long flags;
  185. struct cpts *cpts = container_of(ptp, struct cpts, info);
  186. if (ppb < 0) {
  187. neg_adj = 1;
  188. ppb = -ppb;
  189. }
  190. mult = cpts->cc_mult;
  191. adj = mult;
  192. adj *= ppb;
  193. diff = div_u64(adj, 1000000000ULL);
  194. spin_lock_irqsave(&cpts->lock, flags);
  195. timecounter_read(&cpts->tc);
  196. cpts->cc.mult = neg_adj ? mult - diff : mult + diff;
  197. spin_unlock_irqrestore(&cpts->lock, flags);
  198. return 0;
  199. }
  200. static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  201. {
  202. unsigned long flags;
  203. struct cpts *cpts = container_of(ptp, struct cpts, info);
  204. spin_lock_irqsave(&cpts->lock, flags);
  205. timecounter_adjtime(&cpts->tc, delta);
  206. spin_unlock_irqrestore(&cpts->lock, flags);
  207. return 0;
  208. }
  209. static int cpts_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  210. {
  211. u64 ns;
  212. unsigned long flags;
  213. struct cpts *cpts = container_of(ptp, struct cpts, info);
  214. spin_lock_irqsave(&cpts->lock, flags);
  215. ns = timecounter_read(&cpts->tc);
  216. spin_unlock_irqrestore(&cpts->lock, flags);
  217. *ts = ns_to_timespec64(ns);
  218. return 0;
  219. }
  220. static int cpts_ptp_settime(struct ptp_clock_info *ptp,
  221. const struct timespec64 *ts)
  222. {
  223. u64 ns;
  224. unsigned long flags;
  225. struct cpts *cpts = container_of(ptp, struct cpts, info);
  226. ns = timespec64_to_ns(ts);
  227. spin_lock_irqsave(&cpts->lock, flags);
  228. timecounter_init(&cpts->tc, &cpts->cc, ns);
  229. spin_unlock_irqrestore(&cpts->lock, flags);
  230. return 0;
  231. }
  232. static int cpts_ptp_enable(struct ptp_clock_info *ptp,
  233. struct ptp_clock_request *rq, int on)
  234. {
  235. return -EOPNOTSUPP;
  236. }
  237. static long cpts_overflow_check(struct ptp_clock_info *ptp)
  238. {
  239. struct cpts *cpts = container_of(ptp, struct cpts, info);
  240. unsigned long delay = cpts->ov_check_period;
  241. struct timespec64 ts;
  242. unsigned long flags;
  243. spin_lock_irqsave(&cpts->lock, flags);
  244. ts = ns_to_timespec64(timecounter_read(&cpts->tc));
  245. if (!skb_queue_empty(&cpts->txq))
  246. delay = CPTS_SKB_TX_WORK_TIMEOUT;
  247. spin_unlock_irqrestore(&cpts->lock, flags);
  248. pr_debug("cpts overflow check at %lld.%09ld\n",
  249. (long long)ts.tv_sec, ts.tv_nsec);
  250. return (long)delay;
  251. }
  252. static const struct ptp_clock_info cpts_info = {
  253. .owner = THIS_MODULE,
  254. .name = "CTPS timer",
  255. .max_adj = 1000000,
  256. .n_ext_ts = 0,
  257. .n_pins = 0,
  258. .pps = 0,
  259. .adjfreq = cpts_ptp_adjfreq,
  260. .adjtime = cpts_ptp_adjtime,
  261. .gettime64 = cpts_ptp_gettime,
  262. .settime64 = cpts_ptp_settime,
  263. .enable = cpts_ptp_enable,
  264. .do_aux_work = cpts_overflow_check,
  265. };
  266. static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
  267. u16 ts_seqid, u8 ts_msgtype)
  268. {
  269. u16 *seqid;
  270. unsigned int offset = 0;
  271. u8 *msgtype, *data = skb->data;
  272. if (ptp_class & PTP_CLASS_VLAN)
  273. offset += VLAN_HLEN;
  274. switch (ptp_class & PTP_CLASS_PMASK) {
  275. case PTP_CLASS_IPV4:
  276. offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
  277. break;
  278. case PTP_CLASS_IPV6:
  279. offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
  280. break;
  281. case PTP_CLASS_L2:
  282. offset += ETH_HLEN;
  283. break;
  284. default:
  285. return 0;
  286. }
  287. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  288. return 0;
  289. if (unlikely(ptp_class & PTP_CLASS_V1))
  290. msgtype = data + offset + OFF_PTP_CONTROL;
  291. else
  292. msgtype = data + offset;
  293. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  294. return (ts_msgtype == (*msgtype & 0xf) && ts_seqid == ntohs(*seqid));
  295. }
  296. static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
  297. {
  298. u64 ns = 0;
  299. struct cpts_event *event;
  300. struct list_head *this, *next;
  301. unsigned int class = ptp_classify_raw(skb);
  302. unsigned long flags;
  303. u16 seqid;
  304. u8 mtype;
  305. if (class == PTP_CLASS_NONE)
  306. return 0;
  307. spin_lock_irqsave(&cpts->lock, flags);
  308. cpts_fifo_read(cpts, -1);
  309. list_for_each_safe(this, next, &cpts->events) {
  310. event = list_entry(this, struct cpts_event, list);
  311. if (event_expired(event)) {
  312. list_del_init(&event->list);
  313. list_add(&event->list, &cpts->pool);
  314. continue;
  315. }
  316. mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
  317. seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
  318. if (ev_type == event_type(event) &&
  319. cpts_match(skb, class, seqid, mtype)) {
  320. ns = timecounter_cyc2time(&cpts->tc, event->low);
  321. list_del_init(&event->list);
  322. list_add(&event->list, &cpts->pool);
  323. break;
  324. }
  325. }
  326. if (ev_type == CPTS_EV_TX && !ns) {
  327. struct cpts_skb_cb_data *skb_cb =
  328. (struct cpts_skb_cb_data *)skb->cb;
  329. /* Not found, add frame to queue for processing later.
  330. * The periodic FIFO check will handle this.
  331. */
  332. skb_get(skb);
  333. /* get the timestamp for timeouts */
  334. skb_cb->tmo = jiffies + msecs_to_jiffies(100);
  335. __skb_queue_tail(&cpts->txq, skb);
  336. ptp_schedule_worker(cpts->clock, 0);
  337. }
  338. spin_unlock_irqrestore(&cpts->lock, flags);
  339. return ns;
  340. }
  341. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  342. {
  343. u64 ns;
  344. struct skb_shared_hwtstamps *ssh;
  345. if (!cpts->rx_enable)
  346. return;
  347. ns = cpts_find_ts(cpts, skb, CPTS_EV_RX);
  348. if (!ns)
  349. return;
  350. ssh = skb_hwtstamps(skb);
  351. memset(ssh, 0, sizeof(*ssh));
  352. ssh->hwtstamp = ns_to_ktime(ns);
  353. }
  354. EXPORT_SYMBOL_GPL(cpts_rx_timestamp);
  355. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  356. {
  357. u64 ns;
  358. struct skb_shared_hwtstamps ssh;
  359. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  360. return;
  361. ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
  362. if (!ns)
  363. return;
  364. memset(&ssh, 0, sizeof(ssh));
  365. ssh.hwtstamp = ns_to_ktime(ns);
  366. skb_tstamp_tx(skb, &ssh);
  367. }
  368. EXPORT_SYMBOL_GPL(cpts_tx_timestamp);
  369. int cpts_register(struct cpts *cpts)
  370. {
  371. int err, i;
  372. skb_queue_head_init(&cpts->txq);
  373. INIT_LIST_HEAD(&cpts->events);
  374. INIT_LIST_HEAD(&cpts->pool);
  375. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  376. list_add(&cpts->pool_data[i].list, &cpts->pool);
  377. clk_enable(cpts->refclk);
  378. cpts_write32(cpts, CPTS_EN, control);
  379. cpts_write32(cpts, TS_PEND_EN, int_enable);
  380. timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real()));
  381. cpts->clock = ptp_clock_register(&cpts->info, cpts->dev);
  382. if (IS_ERR(cpts->clock)) {
  383. err = PTR_ERR(cpts->clock);
  384. cpts->clock = NULL;
  385. goto err_ptp;
  386. }
  387. cpts->phc_index = ptp_clock_index(cpts->clock);
  388. ptp_schedule_worker(cpts->clock, cpts->ov_check_period);
  389. return 0;
  390. err_ptp:
  391. clk_disable(cpts->refclk);
  392. return err;
  393. }
  394. EXPORT_SYMBOL_GPL(cpts_register);
  395. void cpts_unregister(struct cpts *cpts)
  396. {
  397. if (WARN_ON(!cpts->clock))
  398. return;
  399. ptp_clock_unregister(cpts->clock);
  400. cpts->clock = NULL;
  401. cpts_write32(cpts, 0, int_enable);
  402. cpts_write32(cpts, 0, control);
  403. /* Drop all packet */
  404. skb_queue_purge(&cpts->txq);
  405. clk_disable(cpts->refclk);
  406. }
  407. EXPORT_SYMBOL_GPL(cpts_unregister);
  408. static void cpts_calc_mult_shift(struct cpts *cpts)
  409. {
  410. u64 frac, maxsec, ns;
  411. u32 freq;
  412. freq = clk_get_rate(cpts->refclk);
  413. /* Calc the maximum number of seconds which we can run before
  414. * wrapping around.
  415. */
  416. maxsec = cpts->cc.mask;
  417. do_div(maxsec, freq);
  418. /* limit conversation rate to 10 sec as higher values will produce
  419. * too small mult factors and so reduce the conversion accuracy
  420. */
  421. if (maxsec > 10)
  422. maxsec = 10;
  423. /* Calc overflow check period (maxsec / 2) */
  424. cpts->ov_check_period = (HZ * maxsec) / 2;
  425. dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n",
  426. cpts->ov_check_period);
  427. if (cpts->cc.mult || cpts->cc.shift)
  428. return;
  429. clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift,
  430. freq, NSEC_PER_SEC, maxsec);
  431. frac = 0;
  432. ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac);
  433. dev_info(cpts->dev,
  434. "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n",
  435. freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC));
  436. }
  437. static int cpts_of_parse(struct cpts *cpts, struct device_node *node)
  438. {
  439. int ret = -EINVAL;
  440. u32 prop;
  441. if (!of_property_read_u32(node, "cpts_clock_mult", &prop))
  442. cpts->cc.mult = prop;
  443. if (!of_property_read_u32(node, "cpts_clock_shift", &prop))
  444. cpts->cc.shift = prop;
  445. if ((cpts->cc.mult && !cpts->cc.shift) ||
  446. (!cpts->cc.mult && cpts->cc.shift))
  447. goto of_error;
  448. return 0;
  449. of_error:
  450. dev_err(cpts->dev, "CPTS: Missing property in the DT.\n");
  451. return ret;
  452. }
  453. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  454. struct device_node *node)
  455. {
  456. struct cpts *cpts;
  457. int ret;
  458. cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
  459. if (!cpts)
  460. return ERR_PTR(-ENOMEM);
  461. cpts->dev = dev;
  462. cpts->reg = (struct cpsw_cpts __iomem *)regs;
  463. spin_lock_init(&cpts->lock);
  464. ret = cpts_of_parse(cpts, node);
  465. if (ret)
  466. return ERR_PTR(ret);
  467. cpts->refclk = devm_clk_get(dev, "cpts");
  468. if (IS_ERR(cpts->refclk)) {
  469. dev_err(dev, "Failed to get cpts refclk\n");
  470. return ERR_CAST(cpts->refclk);
  471. }
  472. ret = clk_prepare(cpts->refclk);
  473. if (ret)
  474. return ERR_PTR(ret);
  475. cpts->cc.read = cpts_systim_read;
  476. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  477. cpts->info = cpts_info;
  478. cpts_calc_mult_shift(cpts);
  479. /* save cc.mult original value as it can be modified
  480. * by cpts_ptp_adjfreq().
  481. */
  482. cpts->cc_mult = cpts->cc.mult;
  483. return cpts;
  484. }
  485. EXPORT_SYMBOL_GPL(cpts_create);
  486. void cpts_release(struct cpts *cpts)
  487. {
  488. if (!cpts)
  489. return;
  490. if (WARN_ON(!cpts->refclk))
  491. return;
  492. clk_unprepare(cpts->refclk);
  493. }
  494. EXPORT_SYMBOL_GPL(cpts_release);
  495. MODULE_LICENSE("GPL v2");
  496. MODULE_DESCRIPTION("TI CPTS driver");
  497. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");