cpts.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * TI Common Platform Time Sync
  4. *
  5. * Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
  6. *
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/err.h>
  10. #include <linux/if.h>
  11. #include <linux/hrtimer.h>
  12. #include <linux/module.h>
  13. #include <linux/net_tstamp.h>
  14. #include <linux/ptp_classify.h>
  15. #include <linux/time.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/if_vlan.h>
  20. #include "cpts.h"
  21. #define CPTS_SKB_TX_WORK_TIMEOUT 1 /* jiffies */
  22. #define CPTS_SKB_RX_TX_TMO 100 /*ms */
  23. #define CPTS_EVENT_RX_TX_TIMEOUT (100) /* ms */
  24. struct cpts_skb_cb_data {
  25. u32 skb_mtype_seqid;
  26. unsigned long tmo;
  27. };
  28. #define cpts_read32(c, r) readl_relaxed(&c->reg->r)
  29. #define cpts_write32(c, v, r) writel_relaxed(v, &c->reg->r)
  30. static int cpts_event_port(struct cpts_event *event)
  31. {
  32. return (event->high >> PORT_NUMBER_SHIFT) & PORT_NUMBER_MASK;
  33. }
  34. static int event_expired(struct cpts_event *event)
  35. {
  36. return time_after(jiffies, event->tmo);
  37. }
  38. static int event_type(struct cpts_event *event)
  39. {
  40. return (event->high >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
  41. }
  42. static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)
  43. {
  44. u32 r = cpts_read32(cpts, intstat_raw);
  45. if (r & TS_PEND_RAW) {
  46. *high = cpts_read32(cpts, event_high);
  47. *low = cpts_read32(cpts, event_low);
  48. cpts_write32(cpts, EVENT_POP, event_pop);
  49. return 0;
  50. }
  51. return -1;
  52. }
  53. static int cpts_purge_events(struct cpts *cpts)
  54. {
  55. struct list_head *this, *next;
  56. struct cpts_event *event;
  57. int removed = 0;
  58. list_for_each_safe(this, next, &cpts->events) {
  59. event = list_entry(this, struct cpts_event, list);
  60. if (event_expired(event)) {
  61. list_del_init(&event->list);
  62. list_add(&event->list, &cpts->pool);
  63. ++removed;
  64. }
  65. }
  66. if (removed)
  67. dev_dbg(cpts->dev, "cpts: event pool cleaned up %d\n", removed);
  68. return removed ? 0 : -1;
  69. }
  70. static void cpts_purge_txq(struct cpts *cpts)
  71. {
  72. struct cpts_skb_cb_data *skb_cb;
  73. struct sk_buff *skb, *tmp;
  74. int removed = 0;
  75. skb_queue_walk_safe(&cpts->txq, skb, tmp) {
  76. skb_cb = (struct cpts_skb_cb_data *)skb->cb;
  77. if (time_after(jiffies, skb_cb->tmo)) {
  78. __skb_unlink(skb, &cpts->txq);
  79. dev_consume_skb_any(skb);
  80. ++removed;
  81. }
  82. }
  83. if (removed)
  84. dev_dbg(cpts->dev, "txq cleaned up %d\n", removed);
  85. }
  86. /*
  87. * Returns zero if matching event type was found.
  88. */
  89. static int cpts_fifo_read(struct cpts *cpts, int match)
  90. {
  91. struct ptp_clock_event pevent;
  92. bool need_schedule = false;
  93. struct cpts_event *event;
  94. unsigned long flags;
  95. int i, type = -1;
  96. u32 hi, lo;
  97. spin_lock_irqsave(&cpts->lock, flags);
  98. for (i = 0; i < CPTS_FIFO_DEPTH; i++) {
  99. if (cpts_fifo_pop(cpts, &hi, &lo))
  100. break;
  101. if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) {
  102. dev_warn(cpts->dev, "cpts: event pool empty\n");
  103. break;
  104. }
  105. event = list_first_entry(&cpts->pool, struct cpts_event, list);
  106. event->high = hi;
  107. event->low = lo;
  108. event->timestamp = timecounter_cyc2time(&cpts->tc, event->low);
  109. type = event_type(event);
  110. dev_dbg(cpts->dev, "CPTS_EV: %d high:%08X low:%08x\n",
  111. type, event->high, event->low);
  112. switch (type) {
  113. case CPTS_EV_PUSH:
  114. WRITE_ONCE(cpts->cur_timestamp, lo);
  115. timecounter_read(&cpts->tc);
  116. if (cpts->mult_new) {
  117. cpts->cc.mult = cpts->mult_new;
  118. cpts->mult_new = 0;
  119. }
  120. if (!cpts->irq_poll)
  121. complete(&cpts->ts_push_complete);
  122. break;
  123. case CPTS_EV_TX:
  124. case CPTS_EV_RX:
  125. event->tmo = jiffies +
  126. msecs_to_jiffies(CPTS_EVENT_RX_TX_TIMEOUT);
  127. list_del_init(&event->list);
  128. list_add_tail(&event->list, &cpts->events);
  129. need_schedule = true;
  130. break;
  131. case CPTS_EV_ROLL:
  132. case CPTS_EV_HALF:
  133. break;
  134. case CPTS_EV_HW:
  135. pevent.timestamp = event->timestamp;
  136. pevent.type = PTP_CLOCK_EXTTS;
  137. pevent.index = cpts_event_port(event) - 1;
  138. ptp_clock_event(cpts->clock, &pevent);
  139. break;
  140. default:
  141. dev_err(cpts->dev, "cpts: unknown event type\n");
  142. break;
  143. }
  144. if (type == match)
  145. break;
  146. }
  147. spin_unlock_irqrestore(&cpts->lock, flags);
  148. if (!cpts->irq_poll && need_schedule)
  149. ptp_schedule_worker(cpts->clock, 0);
  150. return type == match ? 0 : -1;
  151. }
  152. void cpts_misc_interrupt(struct cpts *cpts)
  153. {
  154. cpts_fifo_read(cpts, -1);
  155. }
  156. EXPORT_SYMBOL_GPL(cpts_misc_interrupt);
  157. static u64 cpts_systim_read(struct cyclecounter *cc)
  158. {
  159. struct cpts *cpts = container_of(cc, struct cpts, cc);
  160. return READ_ONCE(cpts->cur_timestamp);
  161. }
  162. static void cpts_update_cur_time(struct cpts *cpts, int match,
  163. struct ptp_system_timestamp *sts)
  164. {
  165. unsigned long flags;
  166. reinit_completion(&cpts->ts_push_complete);
  167. /* use spin_lock_irqsave() here as it has to run very fast */
  168. spin_lock_irqsave(&cpts->lock, flags);
  169. ptp_read_system_prets(sts);
  170. cpts_write32(cpts, TS_PUSH, ts_push);
  171. cpts_read32(cpts, ts_push);
  172. ptp_read_system_postts(sts);
  173. spin_unlock_irqrestore(&cpts->lock, flags);
  174. if (cpts->irq_poll && cpts_fifo_read(cpts, match) && match != -1)
  175. dev_err(cpts->dev, "cpts: unable to obtain a time stamp\n");
  176. if (!cpts->irq_poll &&
  177. !wait_for_completion_timeout(&cpts->ts_push_complete, HZ))
  178. dev_err(cpts->dev, "cpts: obtain a time stamp timeout\n");
  179. }
  180. /* PTP clock operations */
  181. static int cpts_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  182. {
  183. struct cpts *cpts = container_of(ptp, struct cpts, info);
  184. mutex_lock(&cpts->ptp_clk_mutex);
  185. cpts->mult_new = adjust_by_scaled_ppm(cpts->cc_mult, scaled_ppm);
  186. cpts_update_cur_time(cpts, CPTS_EV_PUSH, NULL);
  187. mutex_unlock(&cpts->ptp_clk_mutex);
  188. return 0;
  189. }
  190. static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  191. {
  192. struct cpts *cpts = container_of(ptp, struct cpts, info);
  193. mutex_lock(&cpts->ptp_clk_mutex);
  194. timecounter_adjtime(&cpts->tc, delta);
  195. mutex_unlock(&cpts->ptp_clk_mutex);
  196. return 0;
  197. }
  198. static int cpts_ptp_gettimeex(struct ptp_clock_info *ptp,
  199. struct timespec64 *ts,
  200. struct ptp_system_timestamp *sts)
  201. {
  202. struct cpts *cpts = container_of(ptp, struct cpts, info);
  203. u64 ns;
  204. mutex_lock(&cpts->ptp_clk_mutex);
  205. cpts_update_cur_time(cpts, CPTS_EV_PUSH, sts);
  206. ns = timecounter_read(&cpts->tc);
  207. mutex_unlock(&cpts->ptp_clk_mutex);
  208. *ts = ns_to_timespec64(ns);
  209. return 0;
  210. }
  211. static int cpts_ptp_settime(struct ptp_clock_info *ptp,
  212. const struct timespec64 *ts)
  213. {
  214. struct cpts *cpts = container_of(ptp, struct cpts, info);
  215. u64 ns;
  216. ns = timespec64_to_ns(ts);
  217. mutex_lock(&cpts->ptp_clk_mutex);
  218. timecounter_init(&cpts->tc, &cpts->cc, ns);
  219. mutex_unlock(&cpts->ptp_clk_mutex);
  220. return 0;
  221. }
  222. static int cpts_extts_enable(struct cpts *cpts, u32 index, int on)
  223. {
  224. u32 v;
  225. if (((cpts->hw_ts_enable & BIT(index)) >> index) == on)
  226. return 0;
  227. mutex_lock(&cpts->ptp_clk_mutex);
  228. v = cpts_read32(cpts, control);
  229. if (on) {
  230. v |= BIT(8 + index);
  231. cpts->hw_ts_enable |= BIT(index);
  232. } else {
  233. v &= ~BIT(8 + index);
  234. cpts->hw_ts_enable &= ~BIT(index);
  235. }
  236. cpts_write32(cpts, v, control);
  237. mutex_unlock(&cpts->ptp_clk_mutex);
  238. return 0;
  239. }
  240. static int cpts_ptp_enable(struct ptp_clock_info *ptp,
  241. struct ptp_clock_request *rq, int on)
  242. {
  243. struct cpts *cpts = container_of(ptp, struct cpts, info);
  244. switch (rq->type) {
  245. case PTP_CLK_REQ_EXTTS:
  246. return cpts_extts_enable(cpts, rq->extts.index, on);
  247. default:
  248. break;
  249. }
  250. return -EOPNOTSUPP;
  251. }
  252. static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event)
  253. {
  254. struct sk_buff_head txq_list;
  255. struct sk_buff *skb, *tmp;
  256. unsigned long flags;
  257. bool found = false;
  258. u32 mtype_seqid;
  259. mtype_seqid = event->high &
  260. ((MESSAGE_TYPE_MASK << MESSAGE_TYPE_SHIFT) |
  261. (SEQUENCE_ID_MASK << SEQUENCE_ID_SHIFT) |
  262. (EVENT_TYPE_MASK << EVENT_TYPE_SHIFT));
  263. __skb_queue_head_init(&txq_list);
  264. spin_lock_irqsave(&cpts->txq.lock, flags);
  265. skb_queue_splice_init(&cpts->txq, &txq_list);
  266. spin_unlock_irqrestore(&cpts->txq.lock, flags);
  267. skb_queue_walk_safe(&txq_list, skb, tmp) {
  268. struct skb_shared_hwtstamps ssh;
  269. struct cpts_skb_cb_data *skb_cb =
  270. (struct cpts_skb_cb_data *)skb->cb;
  271. if (mtype_seqid == skb_cb->skb_mtype_seqid) {
  272. memset(&ssh, 0, sizeof(ssh));
  273. ssh.hwtstamp = ns_to_ktime(event->timestamp);
  274. skb_tstamp_tx(skb, &ssh);
  275. found = true;
  276. __skb_unlink(skb, &txq_list);
  277. dev_consume_skb_any(skb);
  278. dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n",
  279. mtype_seqid);
  280. break;
  281. }
  282. if (time_after(jiffies, skb_cb->tmo)) {
  283. /* timeout any expired skbs over 1s */
  284. dev_dbg(cpts->dev, "expiring tx timestamp from txq\n");
  285. __skb_unlink(skb, &txq_list);
  286. dev_consume_skb_any(skb);
  287. }
  288. }
  289. spin_lock_irqsave(&cpts->txq.lock, flags);
  290. skb_queue_splice(&txq_list, &cpts->txq);
  291. spin_unlock_irqrestore(&cpts->txq.lock, flags);
  292. return found;
  293. }
  294. static void cpts_process_events(struct cpts *cpts)
  295. {
  296. struct list_head *this, *next;
  297. struct cpts_event *event;
  298. LIST_HEAD(events_free);
  299. unsigned long flags;
  300. LIST_HEAD(events);
  301. spin_lock_irqsave(&cpts->lock, flags);
  302. list_splice_init(&cpts->events, &events);
  303. spin_unlock_irqrestore(&cpts->lock, flags);
  304. list_for_each_safe(this, next, &events) {
  305. event = list_entry(this, struct cpts_event, list);
  306. if (cpts_match_tx_ts(cpts, event) ||
  307. time_after(jiffies, event->tmo)) {
  308. list_del_init(&event->list);
  309. list_add(&event->list, &events_free);
  310. }
  311. }
  312. spin_lock_irqsave(&cpts->lock, flags);
  313. list_splice_tail(&events, &cpts->events);
  314. list_splice_tail(&events_free, &cpts->pool);
  315. spin_unlock_irqrestore(&cpts->lock, flags);
  316. }
  317. static long cpts_overflow_check(struct ptp_clock_info *ptp)
  318. {
  319. struct cpts *cpts = container_of(ptp, struct cpts, info);
  320. unsigned long delay = cpts->ov_check_period;
  321. unsigned long flags;
  322. u64 ns;
  323. mutex_lock(&cpts->ptp_clk_mutex);
  324. cpts_update_cur_time(cpts, -1, NULL);
  325. ns = timecounter_read(&cpts->tc);
  326. cpts_process_events(cpts);
  327. spin_lock_irqsave(&cpts->txq.lock, flags);
  328. if (!skb_queue_empty(&cpts->txq)) {
  329. cpts_purge_txq(cpts);
  330. if (!skb_queue_empty(&cpts->txq))
  331. delay = CPTS_SKB_TX_WORK_TIMEOUT;
  332. }
  333. spin_unlock_irqrestore(&cpts->txq.lock, flags);
  334. dev_dbg(cpts->dev, "cpts overflow check at %lld\n", ns);
  335. mutex_unlock(&cpts->ptp_clk_mutex);
  336. return (long)delay;
  337. }
  338. static const struct ptp_clock_info cpts_info = {
  339. .owner = THIS_MODULE,
  340. .name = "CTPS timer",
  341. .max_adj = 1000000,
  342. .n_ext_ts = 0,
  343. .n_pins = 0,
  344. .pps = 0,
  345. .adjfine = cpts_ptp_adjfine,
  346. .adjtime = cpts_ptp_adjtime,
  347. .gettimex64 = cpts_ptp_gettimeex,
  348. .settime64 = cpts_ptp_settime,
  349. .enable = cpts_ptp_enable,
  350. .do_aux_work = cpts_overflow_check,
  351. };
  352. static int cpts_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid)
  353. {
  354. unsigned int ptp_class = ptp_classify_raw(skb);
  355. struct ptp_header *hdr;
  356. u8 msgtype;
  357. u16 seqid;
  358. if (ptp_class == PTP_CLASS_NONE)
  359. return 0;
  360. hdr = ptp_parse_header(skb, ptp_class);
  361. if (!hdr)
  362. return 0;
  363. msgtype = ptp_get_msgtype(hdr, ptp_class);
  364. seqid = ntohs(hdr->sequence_id);
  365. *mtype_seqid = (msgtype & MESSAGE_TYPE_MASK) << MESSAGE_TYPE_SHIFT;
  366. *mtype_seqid |= (seqid & SEQUENCE_ID_MASK) << SEQUENCE_ID_SHIFT;
  367. return 1;
  368. }
  369. static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb,
  370. int ev_type, u32 skb_mtype_seqid)
  371. {
  372. struct list_head *this, *next;
  373. struct cpts_event *event;
  374. unsigned long flags;
  375. u32 mtype_seqid;
  376. u64 ns = 0;
  377. cpts_fifo_read(cpts, -1);
  378. spin_lock_irqsave(&cpts->lock, flags);
  379. list_for_each_safe(this, next, &cpts->events) {
  380. event = list_entry(this, struct cpts_event, list);
  381. if (event_expired(event)) {
  382. list_del_init(&event->list);
  383. list_add(&event->list, &cpts->pool);
  384. continue;
  385. }
  386. mtype_seqid = event->high &
  387. ((MESSAGE_TYPE_MASK << MESSAGE_TYPE_SHIFT) |
  388. (SEQUENCE_ID_MASK << SEQUENCE_ID_SHIFT) |
  389. (EVENT_TYPE_MASK << EVENT_TYPE_SHIFT));
  390. if (mtype_seqid == skb_mtype_seqid) {
  391. ns = event->timestamp;
  392. list_del_init(&event->list);
  393. list_add(&event->list, &cpts->pool);
  394. break;
  395. }
  396. }
  397. spin_unlock_irqrestore(&cpts->lock, flags);
  398. return ns;
  399. }
  400. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  401. {
  402. struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb;
  403. struct skb_shared_hwtstamps *ssh;
  404. int ret;
  405. u64 ns;
  406. /* cpts_rx_timestamp() is called before eth_type_trans(), so
  407. * skb MAC Hdr properties are not configured yet. Hence need to
  408. * reset skb MAC header here
  409. */
  410. skb_reset_mac_header(skb);
  411. ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
  412. if (!ret)
  413. return;
  414. skb_cb->skb_mtype_seqid |= (CPTS_EV_RX << EVENT_TYPE_SHIFT);
  415. dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
  416. __func__, skb_cb->skb_mtype_seqid);
  417. ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid);
  418. if (!ns)
  419. return;
  420. ssh = skb_hwtstamps(skb);
  421. memset(ssh, 0, sizeof(*ssh));
  422. ssh->hwtstamp = ns_to_ktime(ns);
  423. }
  424. EXPORT_SYMBOL_GPL(cpts_rx_timestamp);
  425. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  426. {
  427. struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb;
  428. int ret;
  429. if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  430. return;
  431. ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
  432. if (!ret)
  433. return;
  434. skb_cb->skb_mtype_seqid |= (CPTS_EV_TX << EVENT_TYPE_SHIFT);
  435. dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
  436. __func__, skb_cb->skb_mtype_seqid);
  437. /* Always defer TX TS processing to PTP worker */
  438. skb_get(skb);
  439. /* get the timestamp for timeouts */
  440. skb_cb->tmo = jiffies + msecs_to_jiffies(CPTS_SKB_RX_TX_TMO);
  441. skb_queue_tail(&cpts->txq, skb);
  442. ptp_schedule_worker(cpts->clock, 0);
  443. }
  444. EXPORT_SYMBOL_GPL(cpts_tx_timestamp);
  445. int cpts_register(struct cpts *cpts)
  446. {
  447. int err, i;
  448. skb_queue_head_init(&cpts->txq);
  449. INIT_LIST_HEAD(&cpts->events);
  450. INIT_LIST_HEAD(&cpts->pool);
  451. for (i = 0; i < CPTS_MAX_EVENTS; i++)
  452. list_add(&cpts->pool_data[i].list, &cpts->pool);
  453. err = clk_enable(cpts->refclk);
  454. if (err)
  455. return err;
  456. cpts_write32(cpts, CPTS_EN, control);
  457. cpts_write32(cpts, TS_PEND_EN, int_enable);
  458. timecounter_init(&cpts->tc, &cpts->cc, ktime_get_real_ns());
  459. cpts->clock = ptp_clock_register(&cpts->info, cpts->dev);
  460. if (IS_ERR(cpts->clock)) {
  461. err = PTR_ERR(cpts->clock);
  462. cpts->clock = NULL;
  463. goto err_ptp;
  464. }
  465. cpts->phc_index = ptp_clock_index(cpts->clock);
  466. ptp_schedule_worker(cpts->clock, cpts->ov_check_period);
  467. return 0;
  468. err_ptp:
  469. clk_disable(cpts->refclk);
  470. return err;
  471. }
  472. EXPORT_SYMBOL_GPL(cpts_register);
  473. void cpts_unregister(struct cpts *cpts)
  474. {
  475. if (WARN_ON(!cpts->clock))
  476. return;
  477. ptp_clock_unregister(cpts->clock);
  478. cpts->clock = NULL;
  479. cpts->phc_index = -1;
  480. cpts_write32(cpts, 0, int_enable);
  481. cpts_write32(cpts, 0, control);
  482. /* Drop all packet */
  483. skb_queue_purge(&cpts->txq);
  484. clk_disable(cpts->refclk);
  485. }
  486. EXPORT_SYMBOL_GPL(cpts_unregister);
  487. static void cpts_calc_mult_shift(struct cpts *cpts)
  488. {
  489. u64 frac, maxsec, ns;
  490. u32 freq;
  491. freq = clk_get_rate(cpts->refclk);
  492. /* Calc the maximum number of seconds which we can run before
  493. * wrapping around.
  494. */
  495. maxsec = cpts->cc.mask;
  496. do_div(maxsec, freq);
  497. /* limit conversation rate to 10 sec as higher values will produce
  498. * too small mult factors and so reduce the conversion accuracy
  499. */
  500. if (maxsec > 10)
  501. maxsec = 10;
  502. /* Calc overflow check period (maxsec / 2) */
  503. cpts->ov_check_period = (HZ * maxsec) / 2;
  504. dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n",
  505. cpts->ov_check_period);
  506. if (cpts->cc.mult || cpts->cc.shift)
  507. return;
  508. clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift,
  509. freq, NSEC_PER_SEC, maxsec);
  510. frac = 0;
  511. ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac);
  512. dev_info(cpts->dev,
  513. "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n",
  514. freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC));
  515. }
  516. static void cpts_clk_unregister(void *clk)
  517. {
  518. clk_hw_unregister_mux(clk);
  519. }
  520. static void cpts_clk_del_provider(void *np)
  521. {
  522. of_clk_del_provider(np);
  523. }
  524. static int cpts_of_mux_clk_setup(struct cpts *cpts, struct device_node *node)
  525. {
  526. struct device_node *refclk_np;
  527. const char **parent_names;
  528. unsigned int num_parents;
  529. struct clk_hw *clk_hw;
  530. int ret = -EINVAL;
  531. u32 *mux_table;
  532. refclk_np = of_get_child_by_name(node, "cpts-refclk-mux");
  533. if (!refclk_np)
  534. /* refclk selection supported not for all SoCs */
  535. return 0;
  536. num_parents = of_clk_get_parent_count(refclk_np);
  537. if (num_parents < 1) {
  538. dev_err(cpts->dev, "mux-clock %s must have parents\n",
  539. refclk_np->name);
  540. goto mux_fail;
  541. }
  542. parent_names = devm_kcalloc(cpts->dev, num_parents,
  543. sizeof(*parent_names), GFP_KERNEL);
  544. mux_table = devm_kcalloc(cpts->dev, num_parents, sizeof(*mux_table),
  545. GFP_KERNEL);
  546. if (!mux_table || !parent_names) {
  547. ret = -ENOMEM;
  548. goto mux_fail;
  549. }
  550. of_clk_parent_fill(refclk_np, parent_names, num_parents);
  551. ret = of_property_read_variable_u32_array(refclk_np, "ti,mux-tbl",
  552. mux_table,
  553. num_parents, num_parents);
  554. if (ret < 0)
  555. goto mux_fail;
  556. clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name,
  557. parent_names, num_parents,
  558. 0,
  559. &cpts->reg->rftclk_sel, 0, 0x1F,
  560. 0, mux_table, NULL);
  561. if (IS_ERR(clk_hw)) {
  562. ret = PTR_ERR(clk_hw);
  563. goto mux_fail;
  564. }
  565. ret = devm_add_action_or_reset(cpts->dev, cpts_clk_unregister, clk_hw);
  566. if (ret) {
  567. dev_err(cpts->dev, "add clkmux unreg action %d", ret);
  568. goto mux_fail;
  569. }
  570. ret = of_clk_add_hw_provider(refclk_np, of_clk_hw_simple_get, clk_hw);
  571. if (ret)
  572. goto mux_fail;
  573. ret = devm_add_action_or_reset(cpts->dev, cpts_clk_del_provider,
  574. refclk_np);
  575. if (ret) {
  576. dev_err(cpts->dev, "add clkmux provider unreg action %d", ret);
  577. goto mux_fail;
  578. }
  579. return ret;
  580. mux_fail:
  581. of_node_put(refclk_np);
  582. return ret;
  583. }
  584. static int cpts_of_parse(struct cpts *cpts, struct device_node *node)
  585. {
  586. int ret = -EINVAL;
  587. u32 prop;
  588. if (!of_property_read_u32(node, "cpts_clock_mult", &prop))
  589. cpts->cc.mult = prop;
  590. if (!of_property_read_u32(node, "cpts_clock_shift", &prop))
  591. cpts->cc.shift = prop;
  592. if ((cpts->cc.mult && !cpts->cc.shift) ||
  593. (!cpts->cc.mult && cpts->cc.shift))
  594. goto of_error;
  595. return cpts_of_mux_clk_setup(cpts, node);
  596. of_error:
  597. dev_err(cpts->dev, "CPTS: Missing property in the DT.\n");
  598. return ret;
  599. }
  600. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  601. struct device_node *node, u32 n_ext_ts)
  602. {
  603. struct cpts *cpts;
  604. int ret;
  605. cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
  606. if (!cpts)
  607. return ERR_PTR(-ENOMEM);
  608. cpts->dev = dev;
  609. cpts->reg = (struct cpsw_cpts __iomem *)regs;
  610. cpts->irq_poll = true;
  611. spin_lock_init(&cpts->lock);
  612. mutex_init(&cpts->ptp_clk_mutex);
  613. init_completion(&cpts->ts_push_complete);
  614. ret = cpts_of_parse(cpts, node);
  615. if (ret)
  616. return ERR_PTR(ret);
  617. cpts->refclk = devm_get_clk_from_child(dev, node, "cpts");
  618. if (IS_ERR(cpts->refclk))
  619. /* try get clk from dev node for compatibility */
  620. cpts->refclk = devm_clk_get(dev, "cpts");
  621. if (IS_ERR(cpts->refclk)) {
  622. dev_err(dev, "Failed to get cpts refclk %ld\n",
  623. PTR_ERR(cpts->refclk));
  624. return ERR_CAST(cpts->refclk);
  625. }
  626. ret = clk_prepare(cpts->refclk);
  627. if (ret)
  628. return ERR_PTR(ret);
  629. cpts->cc.read = cpts_systim_read;
  630. cpts->cc.mask = CLOCKSOURCE_MASK(32);
  631. cpts->info = cpts_info;
  632. cpts->phc_index = -1;
  633. if (n_ext_ts)
  634. cpts->info.n_ext_ts = n_ext_ts;
  635. cpts_calc_mult_shift(cpts);
  636. /* save cc.mult original value as it can be modified
  637. * by cpts_ptp_adjfine().
  638. */
  639. cpts->cc_mult = cpts->cc.mult;
  640. return cpts;
  641. }
  642. EXPORT_SYMBOL_GPL(cpts_create);
  643. void cpts_release(struct cpts *cpts)
  644. {
  645. if (!cpts)
  646. return;
  647. if (WARN_ON(!cpts->refclk))
  648. return;
  649. clk_unprepare(cpts->refclk);
  650. }
  651. EXPORT_SYMBOL_GPL(cpts_release);
  652. MODULE_LICENSE("GPL v2");
  653. MODULE_DESCRIPTION("TI CPTS driver");
  654. MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");