cpts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "cpts.h"
  30. #ifdef CONFIG_TI_CPTS
  31. static struct sock_filter ptp_filter[] = {
  32. PTP_FILTER
  33. };
  34. #define cpts_read32(c, r) __raw_readl(&c->reg->r)
  35. #define cpts_write32(c, v, r) __raw_writel(v, &c->reg->r)
  36. static int event_expired(struct cpts_event *event)
  37. {
  38. return time_after(jiffies, event->tmo);
  39. }
  40. static int event_type(struct cpts_event *event)
  41. {
  42. return (event->high >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  43. }
  44. static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)
  45. {
  46. u32 r = cpts_read32(cpts, intstat_raw);
  47. if (r & TS_PEND_RAW) {
  48. *high = cpts_read32(cpts, event_high);
  49. *low = cpts_read32(cpts, event_low);
  50. cpts_write32(cpts, EVENT_POP, event_pop);
  51. return 0;
  52. }
  53. return -1;
  54. }
  55. /*
  56. * Returns zero if matching event type was found.
  57. */
  58. static int cpts_fifo_read(struct cpts *cpts, int match)
  59. {
  60. int i, type = -1;
  61. u32 hi, lo;
  62. struct cpts_event *event;
  63. for (i = 0; i < CPTS_FIFO_DEPTH; i++) {
  64. if (cpts_fifo_pop(cpts, &hi, &lo))
  65. break;
  66. if (list_empty(&cpts->pool)) {
  67. pr_err("cpts: event pool is empty\n");
  68. return -1;
  69. }
  70. event = list_first_entry(&cpts->pool, struct cpts_event, list);
  71. event->tmo = jiffies + 2;
  72. event->high = hi;
  73. event->low = lo;
  74. type = event_type(event);
  75. switch (type) {
  76. case CPTS_EV_PUSH:
  77. case CPTS_EV_RX:
  78. case CPTS_EV_TX:
  79. list_del_init(&event->list);
  80. list_add_tail(&event->list, &cpts->events);
  81. break;
  82. case CPTS_EV_ROLL:
  83. case CPTS_EV_HALF:
  84. case CPTS_EV_HW:
  85. break;
  86. default:
  87. pr_err("cpts: unkown event type\n");
  88. break;
  89. }
  90. if (type == match)
  91. break;
  92. }
  93. return type == match ? 0 : -1;
  94. }
  95. static cycle_t cpts_systim_read(const struct cyclecounter *cc)
  96. {
  97. u64 val = 0;
  98. struct cpts_event *event;
  99. struct list_head *this, *next;
  100. struct cpts *cpts = container_of(cc, struct cpts, cc);
  101. cpts_write32(cpts, TS_PUSH, ts_push);
  102. if (cpts_fifo_read(cpts, CPTS_EV_PUSH))
  103. pr_err("cpts: unable to obtain a time stamp\n");
  104. list_for_each_safe(this, next, &cpts->events) {
  105. event = list_entry(this, struct cpts_event, list);
  106. if (event_type(event) == CPTS_EV_PUSH) {
  107. list_del_init(&event->list);
  108. list_add(&event->list, &cpts->pool);
  109. val = event->low;
  110. break;
  111. }
  112. }
  113. return val;
  114. }
  115. /* PTP clock operations */
  116. static int cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  117. {
  118. u64 adj;
  119. u32 diff, mult;
  120. int neg_adj = 0;
  121. unsigned long flags;
  122. struct cpts *cpts = container_of(ptp, struct cpts, info);
  123. if (ppb < 0) {
  124. neg_adj = 1;
  125. ppb = -ppb;
  126. }
  127. mult = cpts->cc_mult;
  128. adj = mult;
  129. adj *= ppb;
  130. diff = div_u64(adj, 1000000000ULL);
  131. spin_lock_irqsave(&cpts->lock, flags);
  132. timecounter_read(&cpts->tc);
  133. cpts->cc.mult = neg_adj ? mult - diff : mult + diff;
  134. spin_unlock_irqrestore(&cpts->lock, flags);
  135. return 0;
  136. }
  137. static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  138. {
  139. s64 now;
  140. unsigned long flags;
  141. struct cpts *cpts = container_of(ptp, struct cpts, info);
  142. spin_lock_irqsave(&cpts->lock, flags);
  143. now = timecounter_read(&cpts->tc);
  144. now += delta;
  145. timecounter_init(&cpts->tc, &cpts->cc, now);
  146. spin_unlock_irqrestore(&cpts->lock, flags);
  147. return 0;
  148. }
  149. static int cpts_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  150. {
  151. u64 ns;
  152. u32 remainder;
  153. unsigned long flags;
  154. struct cpts *cpts = container_of(ptp, struct cpts, info);
  155. spin_lock_irqsave(&cpts->lock, flags);
  156. ns = timecounter_read(&cpts->tc);
  157. spin_unlock_irqrestore(&cpts->lock, flags);
  158. ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
  159. ts->tv_nsec = remainder;
  160. return 0;
  161. }
  162. static int cpts_ptp_settime(struct ptp_clock_info *ptp,
  163. const struct timespec *ts)
  164. {
  165. u64 ns;
  166. unsigned long flags;
  167. struct cpts *cpts = container_of(ptp, struct cpts, info);
  168. ns = ts->tv_sec * 1000000000ULL;
  169. ns += ts->tv_nsec;
  170. spin_lock_irqsave(&cpts->lock, flags);
  171. timecounter_init(&cpts->tc, &cpts->cc, ns);
  172. spin_unlock_irqrestore(&cpts->lock, flags);
  173. return 0;
  174. }
  175. static int cpts_ptp_enable(struct ptp_clock_info *ptp,
  176. struct ptp_clock_request *rq, int on)
  177. {
  178. return -EOPNOTSUPP;
  179. }
  180. static struct ptp_clock_info cpts_info = {
  181. .owner = THIS_MODULE,
  182. .name = "CTPS timer",
  183. .max_adj = 1000000,
  184. .n_ext_ts = 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. #define CPTS_REF_CLOCK_NAME "cpsw_cpts_rft_clk"
  203. static void cpts_clk_init(struct cpts *cpts)
  204. {
  205. cpts->refclk = clk_get(NULL, CPTS_REF_CLOCK_NAME);
  206. if (IS_ERR(cpts->refclk)) {
  207. pr_err("Failed to clk_get %s\n", CPTS_REF_CLOCK_NAME);
  208. cpts->refclk = NULL;
  209. return;
  210. }
  211. clk_enable(cpts->refclk);
  212. cpts->freq = cpts->refclk->recalc(cpts->refclk);
  213. }
  214. static void cpts_clk_release(struct cpts *cpts)
  215. {
  216. clk_disable(cpts->refclk);
  217. clk_put(cpts->refclk);
  218. }
  219. static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
  220. u16 ts_seqid, u8 ts_msgtype)
  221. {
  222. u16 *seqid;
  223. unsigned int offset;
  224. u8 *msgtype, *data = skb->data;
  225. switch (ptp_class) {
  226. case PTP_CLASS_V1_IPV4:
  227. case PTP_CLASS_V2_IPV4:
  228. offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
  229. break;
  230. case PTP_CLASS_V1_IPV6:
  231. case PTP_CLASS_V2_IPV6:
  232. offset = OFF_PTP6;
  233. break;
  234. case PTP_CLASS_V2_L2:
  235. offset = ETH_HLEN;
  236. break;
  237. case PTP_CLASS_V2_VLAN:
  238. offset = ETH_HLEN + VLAN_HLEN;
  239. break;
  240. default:
  241. return 0;
  242. }
  243. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  244. return 0;
  245. if (unlikely(ptp_class & PTP_CLASS_V1))
  246. msgtype = data + offset + OFF_PTP_CONTROL;
  247. else
  248. msgtype = data + offset;
  249. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  250. return (ts_msgtype == (*msgtype & 0xf) && ts_seqid == ntohs(*seqid));
  251. }
  252. static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
  253. {
  254. u64 ns = 0;
  255. struct cpts_event *event;
  256. struct list_head *this, *next;
  257. unsigned int class = sk_run_filter(skb, ptp_filter);
  258. unsigned long flags;
  259. u16 seqid;
  260. u8 mtype;
  261. if (class == PTP_CLASS_NONE)
  262. return 0;
  263. spin_lock_irqsave(&cpts->lock, flags);
  264. cpts_fifo_read(cpts, CPTS_EV_PUSH);
  265. list_for_each_safe(this, next, &cpts->events) {
  266. event = list_entry(this, struct cpts_event, list);
  267. if (event_expired(event)) {
  268. list_del_init(&event->list);
  269. list_add(&event->list, &cpts->pool);
  270. continue;
  271. }
  272. mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
  273. seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
  274. if (ev_type == event_type(event) &&
  275. cpts_match(skb, class, seqid, mtype)) {
  276. ns = timecounter_cyc2time(&cpts->tc, event->low);
  277. list_del_init(&event->list);
  278. list_add(&event->list, &cpts->pool);
  279. break;
  280. }
  281. }
  282. spin_unlock_irqrestore(&cpts->lock, flags);
  283. return ns;
  284. }
  285. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  286. {
  287. u64 ns;
  288. struct skb_shared_hwtstamps *ssh;
  289. if (!cpts->rx_enable)
  290. return;
  291. ns = cpts_find_ts(cpts, skb, CPTS_EV_RX);
  292. if (!ns)
  293. return;
  294. ssh = skb_hwtstamps(skb);
  295. memset(ssh, 0, sizeof(*ssh));
  296. ssh->hwtstamp = ns_to_ktime(ns);
  297. }
  298. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  299. {
  300. u64 ns;
  301. struct skb_shared_hwtstamps ssh;
  302. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  303. return;
  304. ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
  305. if (!ns)
  306. return;
  307. memset(&ssh, 0, sizeof(ssh));
  308. ssh.hwtstamp = ns_to_ktime(ns);
  309. skb_tstamp_tx(skb, &ssh);
  310. }
  311. #endif /*CONFIG_TI_CPTS*/
  312. int cpts_register(struct device *dev, struct cpts *cpts,
  313. u32 mult, u32 shift)
  314. {
  315. #ifdef CONFIG_TI_CPTS
  316. int err, i;
  317. unsigned long flags;
  318. if (ptp_filter_init(ptp_filter, ARRAY_SIZE(ptp_filter))) {
  319. pr_err("cpts: bad ptp filter\n");
  320. return -EINVAL;
  321. }
  322. cpts->info = cpts_info;
  323. cpts->clock = ptp_clock_register(&cpts->info, dev);
  324. if (IS_ERR(cpts->clock)) {
  325. err = PTR_ERR(cpts->clock);
  326. cpts->clock = NULL;
  327. return err;
  328. }
  329. spin_lock_init(&cpts->lock);
  330. cpts->cc.read = cpts_systim_read;
  331. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  332. cpts->cc_mult = mult;
  333. cpts->cc.mult = mult;
  334. cpts->cc.shift = shift;
  335. INIT_LIST_HEAD(&cpts->events);
  336. INIT_LIST_HEAD(&cpts->pool);
  337. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  338. list_add(&cpts->pool_data[i].list, &cpts->pool);
  339. cpts_clk_init(cpts);
  340. cpts_write32(cpts, CPTS_EN, control);
  341. cpts_write32(cpts, TS_PEND_EN, int_enable);
  342. spin_lock_irqsave(&cpts->lock, flags);
  343. timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real()));
  344. spin_unlock_irqrestore(&cpts->lock, flags);
  345. INIT_DELAYED_WORK(&cpts->overflow_work, cpts_overflow_check);
  346. schedule_delayed_work(&cpts->overflow_work, CPTS_OVERFLOW_PERIOD);
  347. cpts->phc_index = ptp_clock_index(cpts->clock);
  348. #endif
  349. return 0;
  350. }
  351. void cpts_unregister(struct cpts *cpts)
  352. {
  353. #ifdef CONFIG_TI_CPTS
  354. if (cpts->clock) {
  355. ptp_clock_unregister(cpts->clock);
  356. cancel_delayed_work_sync(&cpts->overflow_work);
  357. }
  358. if (cpts->refclk)
  359. cpts_clk_release(cpts);
  360. #endif
  361. }