cpts.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. #ifdef CONFIG_TI_CPTS
  33. #define cpts_read32(c, r) __raw_readl(&c->reg->r)
  34. #define cpts_write32(c, v, r) __raw_writel(v, &c->reg->r)
  35. static int event_expired(struct cpts_event *event)
  36. {
  37. return time_after(jiffies, event->tmo);
  38. }
  39. static int event_type(struct cpts_event *event)
  40. {
  41. return (event->high >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  42. }
  43. static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)
  44. {
  45. u32 r = cpts_read32(cpts, intstat_raw);
  46. if (r & TS_PEND_RAW) {
  47. *high = cpts_read32(cpts, event_high);
  48. *low = cpts_read32(cpts, event_low);
  49. cpts_write32(cpts, EVENT_POP, event_pop);
  50. return 0;
  51. }
  52. return -1;
  53. }
  54. /*
  55. * Returns zero if matching event type was found.
  56. */
  57. static int cpts_fifo_read(struct cpts *cpts, int match)
  58. {
  59. int i, type = -1;
  60. u32 hi, lo;
  61. struct cpts_event *event;
  62. for (i = 0; i < CPTS_FIFO_DEPTH; i++) {
  63. if (cpts_fifo_pop(cpts, &hi, &lo))
  64. break;
  65. if (list_empty(&cpts->pool)) {
  66. pr_err("cpts: event pool is empty\n");
  67. return -1;
  68. }
  69. event = list_first_entry(&cpts->pool, struct cpts_event, list);
  70. event->tmo = jiffies + 2;
  71. event->high = hi;
  72. event->low = lo;
  73. type = event_type(event);
  74. switch (type) {
  75. case CPTS_EV_PUSH:
  76. case CPTS_EV_RX:
  77. case CPTS_EV_TX:
  78. list_del_init(&event->list);
  79. list_add_tail(&event->list, &cpts->events);
  80. break;
  81. case CPTS_EV_ROLL:
  82. case CPTS_EV_HALF:
  83. case CPTS_EV_HW:
  84. break;
  85. default:
  86. pr_err("cpts: unknown event type\n");
  87. break;
  88. }
  89. if (type == match)
  90. break;
  91. }
  92. return type == match ? 0 : -1;
  93. }
  94. static cycle_t cpts_systim_read(const struct cyclecounter *cc)
  95. {
  96. u64 val = 0;
  97. struct cpts_event *event;
  98. struct list_head *this, *next;
  99. struct cpts *cpts = container_of(cc, struct cpts, cc);
  100. cpts_write32(cpts, TS_PUSH, ts_push);
  101. if (cpts_fifo_read(cpts, CPTS_EV_PUSH))
  102. pr_err("cpts: unable to obtain a time stamp\n");
  103. list_for_each_safe(this, next, &cpts->events) {
  104. event = list_entry(this, struct cpts_event, list);
  105. if (event_type(event) == CPTS_EV_PUSH) {
  106. list_del_init(&event->list);
  107. list_add(&event->list, &cpts->pool);
  108. val = event->low;
  109. break;
  110. }
  111. }
  112. return val;
  113. }
  114. /* PTP clock operations */
  115. static int cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  116. {
  117. u64 adj;
  118. u32 diff, mult;
  119. int neg_adj = 0;
  120. unsigned long flags;
  121. struct cpts *cpts = container_of(ptp, struct cpts, info);
  122. if (ppb < 0) {
  123. neg_adj = 1;
  124. ppb = -ppb;
  125. }
  126. mult = cpts->cc_mult;
  127. adj = mult;
  128. adj *= ppb;
  129. diff = div_u64(adj, 1000000000ULL);
  130. spin_lock_irqsave(&cpts->lock, flags);
  131. timecounter_read(&cpts->tc);
  132. cpts->cc.mult = neg_adj ? mult - diff : mult + diff;
  133. spin_unlock_irqrestore(&cpts->lock, flags);
  134. return 0;
  135. }
  136. static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  137. {
  138. unsigned long flags;
  139. struct cpts *cpts = container_of(ptp, struct cpts, info);
  140. spin_lock_irqsave(&cpts->lock, flags);
  141. timecounter_adjtime(&cpts->tc, delta);
  142. spin_unlock_irqrestore(&cpts->lock, flags);
  143. return 0;
  144. }
  145. static int cpts_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  146. {
  147. u64 ns;
  148. unsigned long flags;
  149. struct cpts *cpts = container_of(ptp, struct cpts, info);
  150. spin_lock_irqsave(&cpts->lock, flags);
  151. ns = timecounter_read(&cpts->tc);
  152. spin_unlock_irqrestore(&cpts->lock, flags);
  153. *ts = ns_to_timespec64(ns);
  154. return 0;
  155. }
  156. static int cpts_ptp_settime(struct ptp_clock_info *ptp,
  157. const struct timespec64 *ts)
  158. {
  159. u64 ns;
  160. unsigned long flags;
  161. struct cpts *cpts = container_of(ptp, struct cpts, info);
  162. ns = timespec64_to_ns(ts);
  163. spin_lock_irqsave(&cpts->lock, flags);
  164. timecounter_init(&cpts->tc, &cpts->cc, ns);
  165. spin_unlock_irqrestore(&cpts->lock, flags);
  166. return 0;
  167. }
  168. static int cpts_ptp_enable(struct ptp_clock_info *ptp,
  169. struct ptp_clock_request *rq, int on)
  170. {
  171. return -EOPNOTSUPP;
  172. }
  173. static struct ptp_clock_info cpts_info = {
  174. .owner = THIS_MODULE,
  175. .name = "CTPS timer",
  176. .max_adj = 1000000,
  177. .n_ext_ts = 0,
  178. .n_pins = 0,
  179. .pps = 0,
  180. .adjfreq = cpts_ptp_adjfreq,
  181. .adjtime = cpts_ptp_adjtime,
  182. .gettime64 = cpts_ptp_gettime,
  183. .settime64 = cpts_ptp_settime,
  184. .enable = cpts_ptp_enable,
  185. };
  186. static void cpts_overflow_check(struct work_struct *work)
  187. {
  188. struct timespec64 ts;
  189. struct cpts *cpts = container_of(work, struct cpts, overflow_work.work);
  190. cpts_write32(cpts, CPTS_EN, control);
  191. cpts_write32(cpts, TS_PEND_EN, int_enable);
  192. cpts_ptp_gettime(&cpts->info, &ts);
  193. pr_debug("cpts overflow check at %lld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  194. schedule_delayed_work(&cpts->overflow_work, CPTS_OVERFLOW_PERIOD);
  195. }
  196. static void cpts_clk_init(struct device *dev, struct cpts *cpts)
  197. {
  198. cpts->refclk = devm_clk_get(dev, "cpts");
  199. if (IS_ERR(cpts->refclk)) {
  200. dev_err(dev, "Failed to get cpts refclk\n");
  201. cpts->refclk = NULL;
  202. return;
  203. }
  204. clk_prepare_enable(cpts->refclk);
  205. }
  206. static void cpts_clk_release(struct cpts *cpts)
  207. {
  208. clk_disable(cpts->refclk);
  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. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  287. {
  288. u64 ns;
  289. struct skb_shared_hwtstamps ssh;
  290. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  291. return;
  292. ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
  293. if (!ns)
  294. return;
  295. memset(&ssh, 0, sizeof(ssh));
  296. ssh.hwtstamp = ns_to_ktime(ns);
  297. skb_tstamp_tx(skb, &ssh);
  298. }
  299. #endif /*CONFIG_TI_CPTS*/
  300. int cpts_register(struct device *dev, struct cpts *cpts,
  301. u32 mult, u32 shift)
  302. {
  303. #ifdef CONFIG_TI_CPTS
  304. int err, i;
  305. unsigned long flags;
  306. cpts->info = cpts_info;
  307. cpts->clock = ptp_clock_register(&cpts->info, dev);
  308. if (IS_ERR(cpts->clock)) {
  309. err = PTR_ERR(cpts->clock);
  310. cpts->clock = NULL;
  311. return err;
  312. }
  313. spin_lock_init(&cpts->lock);
  314. cpts->cc.read = cpts_systim_read;
  315. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  316. cpts->cc_mult = mult;
  317. cpts->cc.mult = mult;
  318. cpts->cc.shift = shift;
  319. INIT_LIST_HEAD(&cpts->events);
  320. INIT_LIST_HEAD(&cpts->pool);
  321. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  322. list_add(&cpts->pool_data[i].list, &cpts->pool);
  323. cpts_clk_init(dev, cpts);
  324. cpts_write32(cpts, CPTS_EN, control);
  325. cpts_write32(cpts, TS_PEND_EN, int_enable);
  326. spin_lock_irqsave(&cpts->lock, flags);
  327. timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real()));
  328. spin_unlock_irqrestore(&cpts->lock, flags);
  329. INIT_DELAYED_WORK(&cpts->overflow_work, cpts_overflow_check);
  330. schedule_delayed_work(&cpts->overflow_work, CPTS_OVERFLOW_PERIOD);
  331. cpts->phc_index = ptp_clock_index(cpts->clock);
  332. #endif
  333. return 0;
  334. }
  335. void cpts_unregister(struct cpts *cpts)
  336. {
  337. #ifdef CONFIG_TI_CPTS
  338. if (cpts->clock) {
  339. ptp_clock_unregister(cpts->clock);
  340. cancel_delayed_work_sync(&cpts->overflow_work);
  341. }
  342. if (cpts->refclk)
  343. cpts_clk_release(cpts);
  344. #endif
  345. }