cpts.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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_prepare_enable(cpts->refclk);
  212. }
  213. static void cpts_clk_release(struct cpts *cpts)
  214. {
  215. clk_disable(cpts->refclk);
  216. clk_put(cpts->refclk);
  217. }
  218. static int cpts_match(struct sk_buff *skb, unsigned int ptp_class,
  219. u16 ts_seqid, u8 ts_msgtype)
  220. {
  221. u16 *seqid;
  222. unsigned int offset;
  223. u8 *msgtype, *data = skb->data;
  224. switch (ptp_class) {
  225. case PTP_CLASS_V1_IPV4:
  226. case PTP_CLASS_V2_IPV4:
  227. offset = ETH_HLEN + IPV4_HLEN(data) + UDP_HLEN;
  228. break;
  229. case PTP_CLASS_V1_IPV6:
  230. case PTP_CLASS_V2_IPV6:
  231. offset = OFF_PTP6;
  232. break;
  233. case PTP_CLASS_V2_L2:
  234. offset = ETH_HLEN;
  235. break;
  236. case PTP_CLASS_V2_VLAN:
  237. offset = ETH_HLEN + VLAN_HLEN;
  238. break;
  239. default:
  240. return 0;
  241. }
  242. if (skb->len + ETH_HLEN < offset + OFF_PTP_SEQUENCE_ID + sizeof(*seqid))
  243. return 0;
  244. if (unlikely(ptp_class & PTP_CLASS_V1))
  245. msgtype = data + offset + OFF_PTP_CONTROL;
  246. else
  247. msgtype = data + offset;
  248. seqid = (u16 *)(data + offset + OFF_PTP_SEQUENCE_ID);
  249. return (ts_msgtype == (*msgtype & 0xf) && ts_seqid == ntohs(*seqid));
  250. }
  251. static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type)
  252. {
  253. u64 ns = 0;
  254. struct cpts_event *event;
  255. struct list_head *this, *next;
  256. unsigned int class = sk_run_filter(skb, ptp_filter);
  257. unsigned long flags;
  258. u16 seqid;
  259. u8 mtype;
  260. if (class == PTP_CLASS_NONE)
  261. return 0;
  262. spin_lock_irqsave(&cpts->lock, flags);
  263. cpts_fifo_read(cpts, CPTS_EV_PUSH);
  264. list_for_each_safe(this, next, &cpts->events) {
  265. event = list_entry(this, struct cpts_event, list);
  266. if (event_expired(event)) {
  267. list_del_init(&event->list);
  268. list_add(&event->list, &cpts->pool);
  269. continue;
  270. }
  271. mtype = (event->high >> MESSAGE_TYPE_SHIFT) & MESSAGE_TYPE_MASK;
  272. seqid = (event->high >> SEQUENCE_ID_SHIFT) & SEQUENCE_ID_MASK;
  273. if (ev_type == event_type(event) &&
  274. cpts_match(skb, class, seqid, mtype)) {
  275. ns = timecounter_cyc2time(&cpts->tc, event->low);
  276. list_del_init(&event->list);
  277. list_add(&event->list, &cpts->pool);
  278. break;
  279. }
  280. }
  281. spin_unlock_irqrestore(&cpts->lock, flags);
  282. return ns;
  283. }
  284. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  285. {
  286. u64 ns;
  287. struct skb_shared_hwtstamps *ssh;
  288. if (!cpts->rx_enable)
  289. return;
  290. ns = cpts_find_ts(cpts, skb, CPTS_EV_RX);
  291. if (!ns)
  292. return;
  293. ssh = skb_hwtstamps(skb);
  294. memset(ssh, 0, sizeof(*ssh));
  295. ssh->hwtstamp = ns_to_ktime(ns);
  296. }
  297. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  298. {
  299. u64 ns;
  300. struct skb_shared_hwtstamps ssh;
  301. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  302. return;
  303. ns = cpts_find_ts(cpts, skb, CPTS_EV_TX);
  304. if (!ns)
  305. return;
  306. memset(&ssh, 0, sizeof(ssh));
  307. ssh.hwtstamp = ns_to_ktime(ns);
  308. skb_tstamp_tx(skb, &ssh);
  309. }
  310. #endif /*CONFIG_TI_CPTS*/
  311. int cpts_register(struct device *dev, struct cpts *cpts,
  312. u32 mult, u32 shift)
  313. {
  314. #ifdef CONFIG_TI_CPTS
  315. int err, i;
  316. unsigned long flags;
  317. if (ptp_filter_init(ptp_filter, ARRAY_SIZE(ptp_filter))) {
  318. pr_err("cpts: bad ptp filter\n");
  319. return -EINVAL;
  320. }
  321. cpts->info = cpts_info;
  322. cpts->clock = ptp_clock_register(&cpts->info, dev);
  323. if (IS_ERR(cpts->clock)) {
  324. err = PTR_ERR(cpts->clock);
  325. cpts->clock = NULL;
  326. return err;
  327. }
  328. spin_lock_init(&cpts->lock);
  329. cpts->cc.read = cpts_systim_read;
  330. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  331. cpts->cc_mult = mult;
  332. cpts->cc.mult = mult;
  333. cpts->cc.shift = shift;
  334. INIT_LIST_HEAD(&cpts->events);
  335. INIT_LIST_HEAD(&cpts->pool);
  336. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  337. list_add(&cpts->pool_data[i].list, &cpts->pool);
  338. cpts_clk_init(cpts);
  339. cpts_write32(cpts, CPTS_EN, control);
  340. cpts_write32(cpts, TS_PEND_EN, int_enable);
  341. spin_lock_irqsave(&cpts->lock, flags);
  342. timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real()));
  343. spin_unlock_irqrestore(&cpts->lock, flags);
  344. INIT_DELAYED_WORK(&cpts->overflow_work, cpts_overflow_check);
  345. schedule_delayed_work(&cpts->overflow_work, CPTS_OVERFLOW_PERIOD);
  346. cpts->phc_index = ptp_clock_index(cpts->clock);
  347. #endif
  348. return 0;
  349. }
  350. void cpts_unregister(struct cpts *cpts)
  351. {
  352. #ifdef CONFIG_TI_CPTS
  353. if (cpts->clock) {
  354. ptp_clock_unregister(cpts->clock);
  355. cancel_delayed_work_sync(&cpts->overflow_work);
  356. }
  357. if (cpts->refclk)
  358. cpts_clk_release(cpts);
  359. #endif
  360. }