cpts.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. s64 now;
  139. unsigned long flags;
  140. struct cpts *cpts = container_of(ptp, struct cpts, info);
  141. spin_lock_irqsave(&cpts->lock, flags);
  142. now = timecounter_read(&cpts->tc);
  143. now += delta;
  144. timecounter_init(&cpts->tc, &cpts->cc, now);
  145. spin_unlock_irqrestore(&cpts->lock, flags);
  146. return 0;
  147. }
  148. static int cpts_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  149. {
  150. u64 ns;
  151. u32 remainder;
  152. unsigned long flags;
  153. struct cpts *cpts = container_of(ptp, struct cpts, info);
  154. spin_lock_irqsave(&cpts->lock, flags);
  155. ns = timecounter_read(&cpts->tc);
  156. spin_unlock_irqrestore(&cpts->lock, flags);
  157. ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
  158. ts->tv_nsec = remainder;
  159. return 0;
  160. }
  161. static int cpts_ptp_settime(struct ptp_clock_info *ptp,
  162. const struct timespec *ts)
  163. {
  164. u64 ns;
  165. unsigned long flags;
  166. struct cpts *cpts = container_of(ptp, struct cpts, info);
  167. ns = ts->tv_sec * 1000000000ULL;
  168. ns += ts->tv_nsec;
  169. spin_lock_irqsave(&cpts->lock, flags);
  170. timecounter_init(&cpts->tc, &cpts->cc, ns);
  171. spin_unlock_irqrestore(&cpts->lock, flags);
  172. return 0;
  173. }
  174. static int cpts_ptp_enable(struct ptp_clock_info *ptp,
  175. struct ptp_clock_request *rq, int on)
  176. {
  177. return -EOPNOTSUPP;
  178. }
  179. static struct ptp_clock_info cpts_info = {
  180. .owner = THIS_MODULE,
  181. .name = "CTPS timer",
  182. .max_adj = 1000000,
  183. .n_ext_ts = 0,
  184. .n_pins = 0,
  185. .pps = 0,
  186. .adjfreq = cpts_ptp_adjfreq,
  187. .adjtime = cpts_ptp_adjtime,
  188. .gettime = cpts_ptp_gettime,
  189. .settime = cpts_ptp_settime,
  190. .enable = cpts_ptp_enable,
  191. };
  192. static void cpts_overflow_check(struct work_struct *work)
  193. {
  194. struct timespec ts;
  195. struct cpts *cpts = container_of(work, struct cpts, overflow_work.work);
  196. cpts_write32(cpts, CPTS_EN, control);
  197. cpts_write32(cpts, TS_PEND_EN, int_enable);
  198. cpts_ptp_gettime(&cpts->info, &ts);
  199. pr_debug("cpts overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
  200. schedule_delayed_work(&cpts->overflow_work, CPTS_OVERFLOW_PERIOD);
  201. }
  202. static void cpts_clk_init(struct device *dev, struct cpts *cpts)
  203. {
  204. cpts->refclk = devm_clk_get(dev, "cpts");
  205. if (IS_ERR(cpts->refclk)) {
  206. dev_err(dev, "Failed to get cpts refclk\n");
  207. cpts->refclk = NULL;
  208. return;
  209. }
  210. clk_prepare_enable(cpts->refclk);
  211. }
  212. static void cpts_clk_release(struct cpts *cpts)
  213. {
  214. clk_disable(cpts->refclk);
  215. }
  216. static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
  217. u16 ts_seqid, u8 ts_msgtype)
  218. {
  219. u16 *seqid;
  220. unsigned int offset;
  221. u8 *msgtype, *data = skb->data;
  222. switch (ptp_class) {
  223. case PTP_CLASS_V1_IPV4:
  224. case PTP_CLASS_V2_IPV4:
  225. offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
  226. break;
  227. case PTP_CLASS_V1_IPV6:
  228. case PTP_CLASS_V2_IPV6:
  229. offset = OFF_PTP6;
  230. break;
  231. case PTP_CLASS_V2_L2:
  232. offset = ETH_HLEN;
  233. break;
  234. case PTP_CLASS_V2_VLAN:
  235. offset = ETH_HLEN + VLAN_HLEN;
  236. break;
  237. default:
  238. return 0;
  239. }
  240. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  241. return 0;
  242. if (unlikely(ptp_class & PTP_CLASS_V1))
  243. msgtype = data + offset + OFF_PTP_CONTROL;
  244. else
  245. msgtype = data + offset;
  246. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  247. return (ts_msgtype == (*msgtype & 0xf) && ts_seqid == ntohs(*seqid));
  248. }
  249. static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
  250. {
  251. u64 ns = 0;
  252. struct cpts_event *event;
  253. struct list_head *this, *next;
  254. unsigned int class = ptp_classify_raw(skb);
  255. unsigned long flags;
  256. u16 seqid;
  257. u8 mtype;
  258. if (class == PTP_CLASS_NONE)
  259. return 0;
  260. spin_lock_irqsave(&cpts->lock, flags);
  261. cpts_fifo_read(cpts, CPTS_EV_PUSH);
  262. list_for_each_safe(this, next, &cpts->events) {
  263. event = list_entry(this, struct cpts_event, list);
  264. if (event_expired(event)) {
  265. list_del_init(&event->list);
  266. list_add(&event->list, &cpts->pool);
  267. continue;
  268. }
  269. mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
  270. seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
  271. if (ev_type == event_type(event) &&
  272. cpts_match(skb, class, seqid, mtype)) {
  273. ns = timecounter_cyc2time(&cpts->tc, event->low);
  274. list_del_init(&event->list);
  275. list_add(&event->list, &cpts->pool);
  276. break;
  277. }
  278. }
  279. spin_unlock_irqrestore(&cpts->lock, flags);
  280. return ns;
  281. }
  282. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  283. {
  284. u64 ns;
  285. struct skb_shared_hwtstamps *ssh;
  286. if (!cpts->rx_enable)
  287. return;
  288. ns = cpts_find_ts(cpts, skb, CPTS_EV_RX);
  289. if (!ns)
  290. return;
  291. ssh = skb_hwtstamps(skb);
  292. memset(ssh, 0, sizeof(*ssh));
  293. ssh->hwtstamp = ns_to_ktime(ns);
  294. }
  295. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  296. {
  297. u64 ns;
  298. struct skb_shared_hwtstamps ssh;
  299. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  300. return;
  301. ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
  302. if (!ns)
  303. return;
  304. memset(&ssh, 0, sizeof(ssh));
  305. ssh.hwtstamp = ns_to_ktime(ns);
  306. skb_tstamp_tx(skb, &ssh);
  307. }
  308. #endif /*CONFIG_TI_CPTS*/
  309. int cpts_register(struct device *dev, struct cpts *cpts,
  310. u32 mult, u32 shift)
  311. {
  312. #ifdef CONFIG_TI_CPTS
  313. int err, i;
  314. unsigned long flags;
  315. cpts->info = cpts_info;
  316. cpts->clock = ptp_clock_register(&cpts->info, dev);
  317. if (IS_ERR(cpts->clock)) {
  318. err = PTR_ERR(cpts->clock);
  319. cpts->clock = NULL;
  320. return err;
  321. }
  322. spin_lock_init(&cpts->lock);
  323. cpts->cc.read = cpts_systim_read;
  324. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  325. cpts->cc_mult = mult;
  326. cpts->cc.mult = mult;
  327. cpts->cc.shift = shift;
  328. INIT_LIST_HEAD(&cpts->events);
  329. INIT_LIST_HEAD(&cpts->pool);
  330. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  331. list_add(&cpts->pool_data[i].list, &cpts->pool);
  332. cpts_clk_init(dev, cpts);
  333. cpts_write32(cpts, CPTS_EN, control);
  334. cpts_write32(cpts, TS_PEND_EN, int_enable);
  335. spin_lock_irqsave(&cpts->lock, flags);
  336. timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real()));
  337. spin_unlock_irqrestore(&cpts->lock, flags);
  338. INIT_DELAYED_WORK(&cpts->overflow_work, cpts_overflow_check);
  339. schedule_delayed_work(&cpts->overflow_work, CPTS_OVERFLOW_PERIOD);
  340. cpts->phc_index = ptp_clock_index(cpts->clock);
  341. #endif
  342. return 0;
  343. }
  344. void cpts_unregister(struct cpts *cpts)
  345. {
  346. #ifdef CONFIG_TI_CPTS
  347. if (cpts->clock) {
  348. ptp_clock_unregister(cpts->clock);
  349. cancel_delayed_work_sync(&cpts->overflow_work);
  350. }
  351. if (cpts->refclk)
  352. cpts_clk_release(cpts);
  353. #endif
  354. }