cpts.c 10 KB

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