cpts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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_read32(c, r) readl_relaxed(&c->reg->r)
  33. #define cpts_write32(c, v, r) writel_relaxed(v, &c->reg->r)
  34. static int event_expired(struct cpts_event *event)
  35. {
  36. return time_after(jiffies, event->tmo);
  37. }
  38. static int event_type(struct cpts_event *event)
  39. {
  40. return (event->high >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  41. }
  42. static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)
  43. {
  44. u32 r = cpts_read32(cpts, intstat_raw);
  45. if (r & TS_PEND_RAW) {
  46. *high = cpts_read32(cpts, event_high);
  47. *low = cpts_read32(cpts, event_low);
  48. cpts_write32(cpts, EVENT_POP, event_pop);
  49. return 0;
  50. }
  51. return -1;
  52. }
  53. static int cpts_purge_events(struct cpts *cpts)
  54. {
  55. struct list_head *this, *next;
  56. struct cpts_event *event;
  57. int removed = 0;
  58. list_for_each_safe(this, next, &cpts->events) {
  59. event = list_entry(this, struct cpts_event, list);
  60. if (event_expired(event)) {
  61. list_del_init(&event->list);
  62. list_add(&event->list, &cpts->pool);
  63. ++removed;
  64. }
  65. }
  66. if (removed)
  67. pr_debug("cpts: event pool cleaned up %d\n", removed);
  68. return removed ? 0 : -1;
  69. }
  70. /*
  71. * Returns zero if matching event type was found.
  72. */
  73. static int cpts_fifo_read(struct cpts *cpts, int match)
  74. {
  75. int i, type = -1;
  76. u32 hi, lo;
  77. struct cpts_event *event;
  78. for (i = 0; i < CPTS_FIFO_DEPTH; i++) {
  79. if (cpts_fifo_pop(cpts, &hi, &lo))
  80. break;
  81. if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) {
  82. pr_err("cpts: event pool empty\n");
  83. return -1;
  84. }
  85. event = list_first_entry(&cpts->pool, struct cpts_event, list);
  86. event->tmo = jiffies + 2;
  87. event->high = hi;
  88. event->low = lo;
  89. type = event_type(event);
  90. switch (type) {
  91. case CPTS_EV_PUSH:
  92. case CPTS_EV_RX:
  93. case CPTS_EV_TX:
  94. list_del_init(&event->list);
  95. list_add_tail(&event->list, &cpts->events);
  96. break;
  97. case CPTS_EV_ROLL:
  98. case CPTS_EV_HALF:
  99. case CPTS_EV_HW:
  100. break;
  101. default:
  102. pr_err("cpts: unknown event type\n");
  103. break;
  104. }
  105. if (type == match)
  106. break;
  107. }
  108. return type == match ? 0 : -1;
  109. }
  110. static u64 cpts_systim_read(const struct cyclecounter *cc)
  111. {
  112. u64 val = 0;
  113. struct cpts_event *event;
  114. struct list_head *this, *next;
  115. struct cpts *cpts = container_of(cc, struct cpts, cc);
  116. cpts_write32(cpts, TS_PUSH, ts_push);
  117. if (cpts_fifo_read(cpts, CPTS_EV_PUSH))
  118. pr_err("cpts: unable to obtain a time stamp\n");
  119. list_for_each_safe(this, next, &cpts->events) {
  120. event = list_entry(this, struct cpts_event, list);
  121. if (event_type(event) == CPTS_EV_PUSH) {
  122. list_del_init(&event->list);
  123. list_add(&event->list, &cpts->pool);
  124. val = event->low;
  125. break;
  126. }
  127. }
  128. return val;
  129. }
  130. /* PTP clock operations */
  131. static int cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  132. {
  133. u64 adj;
  134. u32 diff, mult;
  135. int neg_adj = 0;
  136. unsigned long flags;
  137. struct cpts *cpts = container_of(ptp, struct cpts, info);
  138. if (ppb < 0) {
  139. neg_adj = 1;
  140. ppb = -ppb;
  141. }
  142. mult = cpts->cc_mult;
  143. adj = mult;
  144. adj *= ppb;
  145. diff = div_u64(adj, 1000000000ULL);
  146. spin_lock_irqsave(&cpts->lock, flags);
  147. timecounter_read(&cpts->tc);
  148. cpts->cc.mult = neg_adj ? mult - diff : mult + diff;
  149. spin_unlock_irqrestore(&cpts->lock, flags);
  150. return 0;
  151. }
  152. static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  153. {
  154. unsigned long flags;
  155. struct cpts *cpts = container_of(ptp, struct cpts, info);
  156. spin_lock_irqsave(&cpts->lock, flags);
  157. timecounter_adjtime(&cpts->tc, delta);
  158. spin_unlock_irqrestore(&cpts->lock, flags);
  159. return 0;
  160. }
  161. static int cpts_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  162. {
  163. u64 ns;
  164. unsigned long flags;
  165. struct cpts *cpts = container_of(ptp, struct cpts, info);
  166. spin_lock_irqsave(&cpts->lock, flags);
  167. ns = timecounter_read(&cpts->tc);
  168. spin_unlock_irqrestore(&cpts->lock, flags);
  169. *ts = ns_to_timespec64(ns);
  170. return 0;
  171. }
  172. static int cpts_ptp_settime(struct ptp_clock_info *ptp,
  173. const struct timespec64 *ts)
  174. {
  175. u64 ns;
  176. unsigned long flags;
  177. struct cpts *cpts = container_of(ptp, struct cpts, info);
  178. ns = timespec64_to_ns(ts);
  179. spin_lock_irqsave(&cpts->lock, flags);
  180. timecounter_init(&cpts->tc, &cpts->cc, ns);
  181. spin_unlock_irqrestore(&cpts->lock, flags);
  182. return 0;
  183. }
  184. static int cpts_ptp_enable(struct ptp_clock_info *ptp,
  185. struct ptp_clock_request *rq, int on)
  186. {
  187. return -EOPNOTSUPP;
  188. }
  189. static struct ptp_clock_info cpts_info = {
  190. .owner = THIS_MODULE,
  191. .name = "CTPS timer",
  192. .max_adj = 1000000,
  193. .n_ext_ts = 0,
  194. .n_pins = 0,
  195. .pps = 0,
  196. .adjfreq = cpts_ptp_adjfreq,
  197. .adjtime = cpts_ptp_adjtime,
  198. .gettime64 = cpts_ptp_gettime,
  199. .settime64 = cpts_ptp_settime,
  200. .enable = cpts_ptp_enable,
  201. };
  202. static void cpts_overflow_check(struct work_struct *work)
  203. {
  204. struct timespec64 ts;
  205. struct cpts *cpts = container_of(work, struct cpts, overflow_work.work);
  206. cpts_ptp_gettime(&cpts->info, &ts);
  207. pr_debug("cpts overflow check at %lld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  208. schedule_delayed_work(&cpts->overflow_work, cpts->ov_check_period);
  209. }
  210. static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
  211. u16 ts_seqid, u8 ts_msgtype)
  212. {
  213. u16 *seqid;
  214. unsigned int offset = 0;
  215. u8 *msgtype, *data = skb->data;
  216. if (ptp_class & PTP_CLASS_VLAN)
  217. offset += VLAN_HLEN;
  218. switch (ptp_class & PTP_CLASS_PMASK) {
  219. case PTP_CLASS_IPV4:
  220. offset += ETH_HLEN + IPV4_HLEN(data + offset) + UDP_HLEN;
  221. break;
  222. case PTP_CLASS_IPV6:
  223. offset += ETH_HLEN + IP6_HLEN + UDP_HLEN;
  224. break;
  225. case PTP_CLASS_L2:
  226. offset += ETH_HLEN;
  227. break;
  228. default:
  229. return 0;
  230. }
  231. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  232. return 0;
  233. if (unlikely(ptp_class & PTP_CLASS_V1))
  234. msgtype = data + offset + OFF_PTP_CONTROL;
  235. else
  236. msgtype = data + offset;
  237. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  238. return (ts_msgtype == (*msgtype & 0xf) && ts_seqid == ntohs(*seqid));
  239. }
  240. static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
  241. {
  242. u64 ns = 0;
  243. struct cpts_event *event;
  244. struct list_head *this, *next;
  245. unsigned int class = ptp_classify_raw(skb);
  246. unsigned long flags;
  247. u16 seqid;
  248. u8 mtype;
  249. if (class == PTP_CLASS_NONE)
  250. return 0;
  251. spin_lock_irqsave(&cpts->lock, flags);
  252. cpts_fifo_read(cpts, CPTS_EV_PUSH);
  253. list_for_each_safe(this, next, &cpts->events) {
  254. event = list_entry(this, struct cpts_event, list);
  255. if (event_expired(event)) {
  256. list_del_init(&event->list);
  257. list_add(&event->list, &cpts->pool);
  258. continue;
  259. }
  260. mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
  261. seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
  262. if (ev_type == event_type(event) &&
  263. cpts_match(skb, class, seqid, mtype)) {
  264. ns = timecounter_cyc2time(&cpts->tc, event->low);
  265. list_del_init(&event->list);
  266. list_add(&event->list, &cpts->pool);
  267. break;
  268. }
  269. }
  270. spin_unlock_irqrestore(&cpts->lock, flags);
  271. return ns;
  272. }
  273. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  274. {
  275. u64 ns;
  276. struct skb_shared_hwtstamps *ssh;
  277. if (!cpts->rx_enable)
  278. return;
  279. ns = cpts_find_ts(cpts, skb, CPTS_EV_RX);
  280. if (!ns)
  281. return;
  282. ssh = skb_hwtstamps(skb);
  283. memset(ssh, 0, sizeof(*ssh));
  284. ssh->hwtstamp = ns_to_ktime(ns);
  285. }
  286. EXPORT_SYMBOL_GPL(cpts_rx_timestamp);
  287. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  288. {
  289. u64 ns;
  290. struct skb_shared_hwtstamps ssh;
  291. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  292. return;
  293. ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
  294. if (!ns)
  295. return;
  296. memset(&ssh, 0, sizeof(ssh));
  297. ssh.hwtstamp = ns_to_ktime(ns);
  298. skb_tstamp_tx(skb, &ssh);
  299. }
  300. EXPORT_SYMBOL_GPL(cpts_tx_timestamp);
  301. int cpts_register(struct cpts *cpts)
  302. {
  303. int err, i;
  304. INIT_LIST_HEAD(&cpts->events);
  305. INIT_LIST_HEAD(&cpts->pool);
  306. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  307. list_add(&cpts->pool_data[i].list, &cpts->pool);
  308. clk_enable(cpts->refclk);
  309. cpts_write32(cpts, CPTS_EN, control);
  310. cpts_write32(cpts, TS_PEND_EN, int_enable);
  311. timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real()));
  312. cpts->clock = ptp_clock_register(&cpts->info, cpts->dev);
  313. if (IS_ERR(cpts->clock)) {
  314. err = PTR_ERR(cpts->clock);
  315. cpts->clock = NULL;
  316. goto err_ptp;
  317. }
  318. cpts->phc_index = ptp_clock_index(cpts->clock);
  319. schedule_delayed_work(&cpts->overflow_work, cpts->ov_check_period);
  320. return 0;
  321. err_ptp:
  322. clk_disable(cpts->refclk);
  323. return err;
  324. }
  325. EXPORT_SYMBOL_GPL(cpts_register);
  326. void cpts_unregister(struct cpts *cpts)
  327. {
  328. if (WARN_ON(!cpts->clock))
  329. return;
  330. cancel_delayed_work_sync(&cpts->overflow_work);
  331. ptp_clock_unregister(cpts->clock);
  332. cpts->clock = NULL;
  333. cpts_write32(cpts, 0, int_enable);
  334. cpts_write32(cpts, 0, control);
  335. clk_disable(cpts->refclk);
  336. }
  337. EXPORT_SYMBOL_GPL(cpts_unregister);
  338. static void cpts_calc_mult_shift(struct cpts *cpts)
  339. {
  340. u64 frac, maxsec, ns;
  341. u32 freq;
  342. freq = clk_get_rate(cpts->refclk);
  343. /* Calc the maximum number of seconds which we can run before
  344. * wrapping around.
  345. */
  346. maxsec = cpts->cc.mask;
  347. do_div(maxsec, freq);
  348. /* limit conversation rate to 10 sec as higher values will produce
  349. * too small mult factors and so reduce the conversion accuracy
  350. */
  351. if (maxsec > 10)
  352. maxsec = 10;
  353. /* Calc overflow check period (maxsec / 2) */
  354. cpts->ov_check_period = (HZ * maxsec) / 2;
  355. dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n",
  356. cpts->ov_check_period);
  357. if (cpts->cc.mult || cpts->cc.shift)
  358. return;
  359. clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift,
  360. freq, NSEC_PER_SEC, maxsec);
  361. frac = 0;
  362. ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac);
  363. dev_info(cpts->dev,
  364. "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n",
  365. freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC));
  366. }
  367. static int cpts_of_parse(struct cpts *cpts, struct device_node *node)
  368. {
  369. int ret = -EINVAL;
  370. u32 prop;
  371. if (!of_property_read_u32(node, "cpts_clock_mult", &prop))
  372. cpts->cc.mult = prop;
  373. if (!of_property_read_u32(node, "cpts_clock_shift", &prop))
  374. cpts->cc.shift = prop;
  375. if ((cpts->cc.mult && !cpts->cc.shift) ||
  376. (!cpts->cc.mult && cpts->cc.shift))
  377. goto of_error;
  378. return 0;
  379. of_error:
  380. dev_err(cpts->dev, "CPTS: Missing property in the DT.\n");
  381. return ret;
  382. }
  383. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  384. struct device_node *node)
  385. {
  386. struct cpts *cpts;
  387. int ret;
  388. cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
  389. if (!cpts)
  390. return ERR_PTR(-ENOMEM);
  391. cpts->dev = dev;
  392. cpts->reg = (struct cpsw_cpts __iomem *)regs;
  393. spin_lock_init(&cpts->lock);
  394. INIT_DELAYED_WORK(&cpts->overflow_work, cpts_overflow_check);
  395. ret = cpts_of_parse(cpts, node);
  396. if (ret)
  397. return ERR_PTR(ret);
  398. cpts->refclk = devm_clk_get(dev, "cpts");
  399. if (IS_ERR(cpts->refclk)) {
  400. dev_err(dev, "Failed to get cpts refclk\n");
  401. return ERR_PTR(PTR_ERR(cpts->refclk));
  402. }
  403. clk_prepare(cpts->refclk);
  404. cpts->cc.read = cpts_systim_read;
  405. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  406. cpts->info = cpts_info;
  407. cpts_calc_mult_shift(cpts);
  408. /* save cc.mult original value as it can be modified
  409. * by cpts_ptp_adjfreq().
  410. */
  411. cpts->cc_mult = cpts->cc.mult;
  412. return cpts;
  413. }
  414. EXPORT_SYMBOL_GPL(cpts_create);
  415. void cpts_release(struct cpts *cpts)
  416. {
  417. if (!cpts)
  418. return;
  419. if (WARN_ON(!cpts->refclk))
  420. return;
  421. clk_unprepare(cpts->refclk);
  422. }
  423. EXPORT_SYMBOL_GPL(cpts_release);
  424. MODULE_LICENSE("GPL v2");
  425. MODULE_DESCRIPTION("TI CPTS driver");
  426. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");