Lines Matching full:cpts

21 #include "cpts.h"
50 static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low) in cpts_fifo_pop() argument
52 u32 r = cpts_read32(cpts, intstat_raw); in cpts_fifo_pop()
55 *high = cpts_read32(cpts, event_high); in cpts_fifo_pop()
56 *low = cpts_read32(cpts, event_low); in cpts_fifo_pop()
57 cpts_write32(cpts, EVENT_POP, event_pop); in cpts_fifo_pop()
63 static int cpts_purge_events(struct cpts *cpts) in cpts_purge_events() argument
69 list_for_each_safe(this, next, &cpts->events) { in cpts_purge_events()
73 list_add(&event->list, &cpts->pool); in cpts_purge_events()
79 dev_dbg(cpts->dev, "cpts: event pool cleaned up %d\n", removed); in cpts_purge_events()
83 static void cpts_purge_txq(struct cpts *cpts) in cpts_purge_txq() argument
89 skb_queue_walk_safe(&cpts->txq, skb, tmp) { in cpts_purge_txq()
92 __skb_unlink(skb, &cpts->txq); in cpts_purge_txq()
99 dev_dbg(cpts->dev, "txq cleaned up %d\n", removed); in cpts_purge_txq()
105 static int cpts_fifo_read(struct cpts *cpts, int match) in cpts_fifo_read() argument
114 spin_lock_irqsave(&cpts->lock, flags); in cpts_fifo_read()
117 if (cpts_fifo_pop(cpts, &hi, &lo)) in cpts_fifo_read()
120 if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) { in cpts_fifo_read()
121 dev_warn(cpts->dev, "cpts: event pool empty\n"); in cpts_fifo_read()
125 event = list_first_entry(&cpts->pool, struct cpts_event, list); in cpts_fifo_read()
128 event->timestamp = timecounter_cyc2time(&cpts->tc, event->low); in cpts_fifo_read()
131 dev_dbg(cpts->dev, "CPTS_EV: %d high:%08X low:%08x\n", in cpts_fifo_read()
135 WRITE_ONCE(cpts->cur_timestamp, lo); in cpts_fifo_read()
136 timecounter_read(&cpts->tc); in cpts_fifo_read()
137 if (cpts->mult_new) { in cpts_fifo_read()
138 cpts->cc.mult = cpts->mult_new; in cpts_fifo_read()
139 cpts->mult_new = 0; in cpts_fifo_read()
141 if (!cpts->irq_poll) in cpts_fifo_read()
142 complete(&cpts->ts_push_complete); in cpts_fifo_read()
150 list_add_tail(&event->list, &cpts->events); in cpts_fifo_read()
160 ptp_clock_event(cpts->clock, &pevent); in cpts_fifo_read()
163 dev_err(cpts->dev, "cpts: unknown event type\n"); in cpts_fifo_read()
170 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_fifo_read()
172 if (!cpts->irq_poll && need_schedule) in cpts_fifo_read()
173 ptp_schedule_worker(cpts->clock, 0); in cpts_fifo_read()
178 void cpts_misc_interrupt(struct cpts *cpts) in cpts_misc_interrupt() argument
180 cpts_fifo_read(cpts, -1); in cpts_misc_interrupt()
186 struct cpts *cpts = container_of(cc, struct cpts, cc); in cpts_systim_read() local
188 return READ_ONCE(cpts->cur_timestamp); in cpts_systim_read()
191 static void cpts_update_cur_time(struct cpts *cpts, int match, in cpts_update_cur_time() argument
196 reinit_completion(&cpts->ts_push_complete); in cpts_update_cur_time()
199 spin_lock_irqsave(&cpts->lock, flags); in cpts_update_cur_time()
201 cpts_write32(cpts, TS_PUSH, ts_push); in cpts_update_cur_time()
202 cpts_read32(cpts, ts_push); in cpts_update_cur_time()
204 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_update_cur_time()
206 if (cpts->irq_poll && cpts_fifo_read(cpts, match) && match != -1) in cpts_update_cur_time()
207 dev_err(cpts->dev, "cpts: unable to obtain a time stamp\n"); in cpts_update_cur_time()
209 if (!cpts->irq_poll && in cpts_update_cur_time()
210 !wait_for_completion_timeout(&cpts->ts_push_complete, HZ)) in cpts_update_cur_time()
211 dev_err(cpts->dev, "cpts: obtain a time stamp timeout\n"); in cpts_update_cur_time()
218 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_adjfreq() local
227 mult = cpts->cc_mult; in cpts_ptp_adjfreq()
232 mutex_lock(&cpts->ptp_clk_mutex); in cpts_ptp_adjfreq()
234 cpts->mult_new = neg_adj ? mult - diff : mult + diff; in cpts_ptp_adjfreq()
236 cpts_update_cur_time(cpts, CPTS_EV_PUSH, NULL); in cpts_ptp_adjfreq()
238 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_adjfreq()
244 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_adjtime() local
246 mutex_lock(&cpts->ptp_clk_mutex); in cpts_ptp_adjtime()
247 timecounter_adjtime(&cpts->tc, delta); in cpts_ptp_adjtime()
248 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_adjtime()
257 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_gettimeex() local
260 mutex_lock(&cpts->ptp_clk_mutex); in cpts_ptp_gettimeex()
262 cpts_update_cur_time(cpts, CPTS_EV_PUSH, sts); in cpts_ptp_gettimeex()
264 ns = timecounter_read(&cpts->tc); in cpts_ptp_gettimeex()
265 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_gettimeex()
275 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_settime() local
280 mutex_lock(&cpts->ptp_clk_mutex); in cpts_ptp_settime()
281 timecounter_init(&cpts->tc, &cpts->cc, ns); in cpts_ptp_settime()
282 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_settime()
287 static int cpts_extts_enable(struct cpts *cpts, u32 index, int on) in cpts_extts_enable() argument
291 if (((cpts->hw_ts_enable & BIT(index)) >> index) == on) in cpts_extts_enable()
294 mutex_lock(&cpts->ptp_clk_mutex); in cpts_extts_enable()
296 v = cpts_read32(cpts, control); in cpts_extts_enable()
299 cpts->hw_ts_enable |= BIT(index); in cpts_extts_enable()
302 cpts->hw_ts_enable &= ~BIT(index); in cpts_extts_enable()
304 cpts_write32(cpts, v, control); in cpts_extts_enable()
306 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_extts_enable()
314 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_enable() local
318 return cpts_extts_enable(cpts, rq->extts.index, on); in cpts_ptp_enable()
326 static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event) in cpts_match_tx_ts() argument
341 spin_lock_irqsave(&cpts->txq.lock, flags); in cpts_match_tx_ts()
342 skb_queue_splice_init(&cpts->txq, &txq_list); in cpts_match_tx_ts()
343 spin_unlock_irqrestore(&cpts->txq.lock, flags); in cpts_match_tx_ts()
357 dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n", in cpts_match_tx_ts()
364 dev_dbg(cpts->dev, "expiring tx timestamp from txq\n"); in cpts_match_tx_ts()
370 spin_lock_irqsave(&cpts->txq.lock, flags); in cpts_match_tx_ts()
371 skb_queue_splice(&txq_list, &cpts->txq); in cpts_match_tx_ts()
372 spin_unlock_irqrestore(&cpts->txq.lock, flags); in cpts_match_tx_ts()
377 static void cpts_process_events(struct cpts *cpts) in cpts_process_events() argument
385 spin_lock_irqsave(&cpts->lock, flags); in cpts_process_events()
386 list_splice_init(&cpts->events, &events); in cpts_process_events()
387 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_process_events()
391 if (cpts_match_tx_ts(cpts, event) || in cpts_process_events()
398 spin_lock_irqsave(&cpts->lock, flags); in cpts_process_events()
399 list_splice_tail(&events, &cpts->events); in cpts_process_events()
400 list_splice_tail(&events_free, &cpts->pool); in cpts_process_events()
401 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_process_events()
406 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_overflow_check() local
407 unsigned long delay = cpts->ov_check_period; in cpts_overflow_check()
411 mutex_lock(&cpts->ptp_clk_mutex); in cpts_overflow_check()
413 cpts_update_cur_time(cpts, -1, NULL); in cpts_overflow_check()
414 ns = timecounter_read(&cpts->tc); in cpts_overflow_check()
416 cpts_process_events(cpts); in cpts_overflow_check()
418 spin_lock_irqsave(&cpts->txq.lock, flags); in cpts_overflow_check()
419 if (!skb_queue_empty(&cpts->txq)) { in cpts_overflow_check()
420 cpts_purge_txq(cpts); in cpts_overflow_check()
421 if (!skb_queue_empty(&cpts->txq)) in cpts_overflow_check()
424 spin_unlock_irqrestore(&cpts->txq.lock, flags); in cpts_overflow_check()
426 dev_dbg(cpts->dev, "cpts overflow check at %lld\n", ns); in cpts_overflow_check()
427 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_overflow_check()
469 static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, in cpts_find_ts() argument
478 cpts_fifo_read(cpts, -1); in cpts_find_ts()
479 spin_lock_irqsave(&cpts->lock, flags); in cpts_find_ts()
480 list_for_each_safe(this, next, &cpts->events) { in cpts_find_ts()
484 list_add(&event->list, &cpts->pool); in cpts_find_ts()
496 list_add(&event->list, &cpts->pool); in cpts_find_ts()
500 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_find_ts()
505 void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb) in cpts_rx_timestamp() argument
523 dev_dbg(cpts->dev, "%s mtype seqid %08x\n", in cpts_rx_timestamp()
526 ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid); in cpts_rx_timestamp()
535 void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb) in cpts_tx_timestamp() argument
549 dev_dbg(cpts->dev, "%s mtype seqid %08x\n", in cpts_tx_timestamp()
556 skb_queue_tail(&cpts->txq, skb); in cpts_tx_timestamp()
557 ptp_schedule_worker(cpts->clock, 0); in cpts_tx_timestamp()
561 int cpts_register(struct cpts *cpts) in cpts_register() argument
565 skb_queue_head_init(&cpts->txq); in cpts_register()
566 INIT_LIST_HEAD(&cpts->events); in cpts_register()
567 INIT_LIST_HEAD(&cpts->pool); in cpts_register()
569 list_add(&cpts->pool_data[i].list, &cpts->pool); in cpts_register()
571 err = clk_enable(cpts->refclk); in cpts_register()
575 cpts_write32(cpts, CPTS_EN, control); in cpts_register()
576 cpts_write32(cpts, TS_PEND_EN, int_enable); in cpts_register()
578 timecounter_init(&cpts->tc, &cpts->cc, ktime_get_real_ns()); in cpts_register()
580 cpts->clock = ptp_clock_register(&cpts->info, cpts->dev); in cpts_register()
581 if (IS_ERR(cpts->clock)) { in cpts_register()
582 err = PTR_ERR(cpts->clock); in cpts_register()
583 cpts->clock = NULL; in cpts_register()
586 cpts->phc_index = ptp_clock_index(cpts->clock); in cpts_register()
588 ptp_schedule_worker(cpts->clock, cpts->ov_check_period); in cpts_register()
592 clk_disable(cpts->refclk); in cpts_register()
597 void cpts_unregister(struct cpts *cpts) in cpts_unregister() argument
599 if (WARN_ON(!cpts->clock)) in cpts_unregister()
602 ptp_clock_unregister(cpts->clock); in cpts_unregister()
603 cpts->clock = NULL; in cpts_unregister()
604 cpts->phc_index = -1; in cpts_unregister()
606 cpts_write32(cpts, 0, int_enable); in cpts_unregister()
607 cpts_write32(cpts, 0, control); in cpts_unregister()
610 skb_queue_purge(&cpts->txq); in cpts_unregister()
612 clk_disable(cpts->refclk); in cpts_unregister()
616 static void cpts_calc_mult_shift(struct cpts *cpts) in cpts_calc_mult_shift() argument
621 freq = clk_get_rate(cpts->refclk); in cpts_calc_mult_shift()
626 maxsec = cpts->cc.mask; in cpts_calc_mult_shift()
635 cpts->ov_check_period = (HZ * maxsec) / 2; in cpts_calc_mult_shift()
636 dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n", in cpts_calc_mult_shift()
637 cpts->ov_check_period); in cpts_calc_mult_shift()
639 if (cpts->cc.mult || cpts->cc.shift) in cpts_calc_mult_shift()
642 clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift, in cpts_calc_mult_shift()
646 ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac); in cpts_calc_mult_shift()
648 dev_info(cpts->dev, in cpts_calc_mult_shift()
649 "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n", in cpts_calc_mult_shift()
650 freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC)); in cpts_calc_mult_shift()
653 static int cpts_of_mux_clk_setup(struct cpts *cpts, struct device_node *node) in cpts_of_mux_clk_setup() argument
662 refclk_np = of_get_child_by_name(node, "cpts-refclk-mux"); in cpts_of_mux_clk_setup()
669 dev_err(cpts->dev, "mux-clock %s must have parents\n", in cpts_of_mux_clk_setup()
674 parent_names = devm_kcalloc(cpts->dev, num_parents, in cpts_of_mux_clk_setup()
677 mux_table = devm_kcalloc(cpts->dev, num_parents, sizeof(*mux_table), in cpts_of_mux_clk_setup()
692 clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name, in cpts_of_mux_clk_setup()
695 &cpts->reg->rftclk_sel, 0, 0x1F, in cpts_of_mux_clk_setup()
702 ret = devm_add_action_or_reset(cpts->dev, in cpts_of_mux_clk_setup()
706 dev_err(cpts->dev, "add clkmux unreg action %d", ret); in cpts_of_mux_clk_setup()
714 ret = devm_add_action_or_reset(cpts->dev, in cpts_of_mux_clk_setup()
718 dev_err(cpts->dev, "add clkmux provider unreg action %d", ret); in cpts_of_mux_clk_setup()
729 static int cpts_of_parse(struct cpts *cpts, struct device_node *node) in cpts_of_parse() argument
735 cpts->cc.mult = prop; in cpts_of_parse()
738 cpts->cc.shift = prop; in cpts_of_parse()
740 if ((cpts->cc.mult && !cpts->cc.shift) || in cpts_of_parse()
741 (!cpts->cc.mult && cpts->cc.shift)) in cpts_of_parse()
744 return cpts_of_mux_clk_setup(cpts, node); in cpts_of_parse()
747 dev_err(cpts->dev, "CPTS: Missing property in the DT.\n"); in cpts_of_parse()
751 struct cpts *cpts_create(struct device *dev, void __iomem *regs, in cpts_create()
754 struct cpts *cpts; in cpts_create() local
757 cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL); in cpts_create()
758 if (!cpts) in cpts_create()
761 cpts->dev = dev; in cpts_create()
762 cpts->reg = (struct cpsw_cpts __iomem *)regs; in cpts_create()
763 cpts->irq_poll = true; in cpts_create()
764 spin_lock_init(&cpts->lock); in cpts_create()
765 mutex_init(&cpts->ptp_clk_mutex); in cpts_create()
766 init_completion(&cpts->ts_push_complete); in cpts_create()
768 ret = cpts_of_parse(cpts, node); in cpts_create()
772 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); in cpts_create()
773 if (IS_ERR(cpts->refclk)) in cpts_create()
775 cpts->refclk = devm_clk_get(dev, "cpts"); in cpts_create()
777 if (IS_ERR(cpts->refclk)) { in cpts_create()
778 dev_err(dev, "Failed to get cpts refclk %ld\n", in cpts_create()
779 PTR_ERR(cpts->refclk)); in cpts_create()
780 return ERR_CAST(cpts->refclk); in cpts_create()
783 ret = clk_prepare(cpts->refclk); in cpts_create()
787 cpts->cc.read = cpts_systim_read; in cpts_create()
788 cpts->cc.mask = CLOCKSOURCE_MASK(32); in cpts_create()
789 cpts->info = cpts_info; in cpts_create()
790 cpts->phc_index = -1; in cpts_create()
793 cpts->info.n_ext_ts = n_ext_ts; in cpts_create()
795 cpts_calc_mult_shift(cpts); in cpts_create()
799 cpts->cc_mult = cpts->cc.mult; in cpts_create()
801 return cpts; in cpts_create()
805 void cpts_release(struct cpts *cpts) in cpts_release() argument
807 if (!cpts) in cpts_release()
810 if (WARN_ON(!cpts->refclk)) in cpts_release()
813 clk_unprepare(cpts->refclk); in cpts_release()
818 MODULE_DESCRIPTION("TI CPTS driver");