Lines Matching full:cpts
32 #include "cpts.h"
56 static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low) in cpts_fifo_pop() argument
58 u32 r = cpts_read32(cpts, intstat_raw); in cpts_fifo_pop()
61 *high = cpts_read32(cpts, event_high); in cpts_fifo_pop()
62 *low = cpts_read32(cpts, event_low); in cpts_fifo_pop()
63 cpts_write32(cpts, EVENT_POP, event_pop); in cpts_fifo_pop()
69 static int cpts_purge_events(struct cpts *cpts) in cpts_purge_events() argument
75 list_for_each_safe(this, next, &cpts->events) { in cpts_purge_events()
79 list_add(&event->list, &cpts->pool); in cpts_purge_events()
85 pr_debug("cpts: event pool cleaned up %d\n", removed); in cpts_purge_events()
89 static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event) in cpts_match_tx_ts() argument
99 /* no need to grab txq.lock as access is always done under cpts->lock */ in cpts_match_tx_ts()
100 skb_queue_walk_safe(&cpts->txq, skb, tmp) { in cpts_match_tx_ts()
107 u64 ns = timecounter_cyc2time(&cpts->tc, event->low); in cpts_match_tx_ts()
113 __skb_unlink(skb, &cpts->txq); in cpts_match_tx_ts()
115 dev_dbg(cpts->dev, "match tx timestamp mtype %u seqid %04x\n", in cpts_match_tx_ts()
122 dev_dbg(cpts->dev, "expiring tx timestamp from txq\n"); in cpts_match_tx_ts()
123 __skb_unlink(skb, &cpts->txq); in cpts_match_tx_ts()
134 static int cpts_fifo_read(struct cpts *cpts, int match) in cpts_fifo_read() argument
141 if (cpts_fifo_pop(cpts, &hi, &lo)) in cpts_fifo_read()
144 if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) { in cpts_fifo_read()
145 pr_err("cpts: event pool empty\n"); in cpts_fifo_read()
149 event = list_first_entry(&cpts->pool, struct cpts_event, list); in cpts_fifo_read()
156 if (cpts_match_tx_ts(cpts, event)) { in cpts_fifo_read()
166 list_add_tail(&event->list, &cpts->events); in cpts_fifo_read()
173 pr_err("cpts: unknown event type\n"); in cpts_fifo_read()
187 struct cpts *cpts = container_of(cc, struct cpts, cc); in cpts_systim_read() local
189 cpts_write32(cpts, TS_PUSH, ts_push); in cpts_systim_read()
190 if (cpts_fifo_read(cpts, CPTS_EV_PUSH)) in cpts_systim_read()
191 pr_err("cpts: unable to obtain a time stamp\n"); in cpts_systim_read()
193 list_for_each_safe(this, next, &cpts->events) { in cpts_systim_read()
197 list_add(&event->list, &cpts->pool); in cpts_systim_read()
214 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_adjfreq() local
220 mult = cpts->cc_mult; in cpts_ptp_adjfreq()
225 spin_lock_irqsave(&cpts->lock, flags); in cpts_ptp_adjfreq()
227 timecounter_read(&cpts->tc); in cpts_ptp_adjfreq()
229 cpts->cc.mult = neg_adj ? mult - diff : mult + diff; in cpts_ptp_adjfreq()
231 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_ptp_adjfreq()
239 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_adjtime() local
241 spin_lock_irqsave(&cpts->lock, flags); in cpts_ptp_adjtime()
242 timecounter_adjtime(&cpts->tc, delta); in cpts_ptp_adjtime()
243 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_ptp_adjtime()
252 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_gettime() local
254 spin_lock_irqsave(&cpts->lock, flags); in cpts_ptp_gettime()
255 ns = timecounter_read(&cpts->tc); in cpts_ptp_gettime()
256 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_ptp_gettime()
268 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_settime() local
272 spin_lock_irqsave(&cpts->lock, flags); in cpts_ptp_settime()
273 timecounter_init(&cpts->tc, &cpts->cc, ns); in cpts_ptp_settime()
274 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_ptp_settime()
287 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_overflow_check() local
288 unsigned long delay = cpts->ov_check_period; in cpts_overflow_check()
292 spin_lock_irqsave(&cpts->lock, flags); in cpts_overflow_check()
293 ts = ns_to_timespec64(timecounter_read(&cpts->tc)); in cpts_overflow_check()
295 if (!skb_queue_empty(&cpts->txq)) in cpts_overflow_check()
297 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_overflow_check()
299 pr_debug("cpts overflow check at %lld.%09ld\n", in cpts_overflow_check()
356 static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type) in cpts_find_ts() argument
369 spin_lock_irqsave(&cpts->lock, flags); in cpts_find_ts()
370 cpts_fifo_read(cpts, -1); in cpts_find_ts()
371 list_for_each_safe(this, next, &cpts->events) { in cpts_find_ts()
375 list_add(&event->list, &cpts->pool); in cpts_find_ts()
382 ns = timecounter_cyc2time(&cpts->tc, event->low); in cpts_find_ts()
384 list_add(&event->list, &cpts->pool); in cpts_find_ts()
398 __skb_queue_tail(&cpts->txq, skb); in cpts_find_ts()
399 ptp_schedule_worker(cpts->clock, 0); in cpts_find_ts()
401 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_find_ts()
406 void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb) in cpts_rx_timestamp() argument
411 if (!cpts->rx_enable) in cpts_rx_timestamp()
413 ns = cpts_find_ts(cpts, skb, CPTS_EV_RX); in cpts_rx_timestamp()
422 void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb) in cpts_tx_timestamp() argument
429 ns = cpts_find_ts(cpts, skb, CPTS_EV_TX); in cpts_tx_timestamp()
438 int cpts_register(struct cpts *cpts) in cpts_register() argument
442 skb_queue_head_init(&cpts->txq); in cpts_register()
443 INIT_LIST_HEAD(&cpts->events); in cpts_register()
444 INIT_LIST_HEAD(&cpts->pool); in cpts_register()
446 list_add(&cpts->pool_data[i].list, &cpts->pool); in cpts_register()
448 clk_enable(cpts->refclk); in cpts_register()
450 cpts_write32(cpts, CPTS_EN, control); in cpts_register()
451 cpts_write32(cpts, TS_PEND_EN, int_enable); in cpts_register()
453 timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real())); in cpts_register()
455 cpts->clock = ptp_clock_register(&cpts->info, cpts->dev); in cpts_register()
456 if (IS_ERR(cpts->clock)) { in cpts_register()
457 err = PTR_ERR(cpts->clock); in cpts_register()
458 cpts->clock = NULL; in cpts_register()
461 cpts->phc_index = ptp_clock_index(cpts->clock); in cpts_register()
463 ptp_schedule_worker(cpts->clock, cpts->ov_check_period); in cpts_register()
467 clk_disable(cpts->refclk); in cpts_register()
472 void cpts_unregister(struct cpts *cpts) in cpts_unregister() argument
474 if (WARN_ON(!cpts->clock)) in cpts_unregister()
477 ptp_clock_unregister(cpts->clock); in cpts_unregister()
478 cpts->clock = NULL; in cpts_unregister()
480 cpts_write32(cpts, 0, int_enable); in cpts_unregister()
481 cpts_write32(cpts, 0, control); in cpts_unregister()
484 skb_queue_purge(&cpts->txq); in cpts_unregister()
486 clk_disable(cpts->refclk); in cpts_unregister()
490 static void cpts_calc_mult_shift(struct cpts *cpts) in cpts_calc_mult_shift() argument
495 freq = clk_get_rate(cpts->refclk); in cpts_calc_mult_shift()
500 maxsec = cpts->cc.mask; in cpts_calc_mult_shift()
509 cpts->ov_check_period = (HZ * maxsec) / 2; in cpts_calc_mult_shift()
510 dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n", in cpts_calc_mult_shift()
511 cpts->ov_check_period); in cpts_calc_mult_shift()
513 if (cpts->cc.mult || cpts->cc.shift) in cpts_calc_mult_shift()
516 clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift, in cpts_calc_mult_shift()
520 ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac); in cpts_calc_mult_shift()
522 dev_info(cpts->dev, in cpts_calc_mult_shift()
523 "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n", in cpts_calc_mult_shift()
524 freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC)); in cpts_calc_mult_shift()
527 static int cpts_of_parse(struct cpts *cpts, struct device_node *node) in cpts_of_parse() argument
533 cpts->cc.mult = prop; in cpts_of_parse()
536 cpts->cc.shift = prop; in cpts_of_parse()
538 if ((cpts->cc.mult && !cpts->cc.shift) || in cpts_of_parse()
539 (!cpts->cc.mult && cpts->cc.shift)) in cpts_of_parse()
545 dev_err(cpts->dev, "CPTS: Missing property in the DT.\n"); in cpts_of_parse()
549 struct cpts *cpts_create(struct device *dev, void __iomem *regs, in cpts_create()
552 struct cpts *cpts; in cpts_create() local
555 cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL); in cpts_create()
556 if (!cpts) in cpts_create()
559 cpts->dev = dev; in cpts_create()
560 cpts->reg = (struct cpsw_cpts __iomem *)regs; in cpts_create()
561 spin_lock_init(&cpts->lock); in cpts_create()
563 ret = cpts_of_parse(cpts, node); in cpts_create()
567 cpts->refclk = devm_clk_get(dev, "cpts"); in cpts_create()
568 if (IS_ERR(cpts->refclk)) { in cpts_create()
569 dev_err(dev, "Failed to get cpts refclk\n"); in cpts_create()
570 return ERR_CAST(cpts->refclk); in cpts_create()
573 ret = clk_prepare(cpts->refclk); in cpts_create()
577 cpts->cc.read = cpts_systim_read; in cpts_create()
578 cpts->cc.mask = CLOCKSOURCE_MASK(32); in cpts_create()
579 cpts->info = cpts_info; in cpts_create()
581 cpts_calc_mult_shift(cpts); in cpts_create()
585 cpts->cc_mult = cpts->cc.mult; in cpts_create()
587 return cpts; in cpts_create()
591 void cpts_release(struct cpts *cpts) in cpts_release() argument
593 if (!cpts) in cpts_release()
596 if (WARN_ON(!cpts->refclk)) in cpts_release()
599 clk_unprepare(cpts->refclk); in cpts_release()
604 MODULE_DESCRIPTION("TI CPTS driver");