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_adjfine() local
220 mutex_lock(&cpts->ptp_clk_mutex); in cpts_ptp_adjfine()
222 cpts->mult_new = adjust_by_scaled_ppm(cpts->cc_mult, scaled_ppm); in cpts_ptp_adjfine()
224 cpts_update_cur_time(cpts, CPTS_EV_PUSH, NULL); in cpts_ptp_adjfine()
226 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_adjfine()
232 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_adjtime() local
234 mutex_lock(&cpts->ptp_clk_mutex); in cpts_ptp_adjtime()
235 timecounter_adjtime(&cpts->tc, delta); in cpts_ptp_adjtime()
236 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_adjtime()
245 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_gettimeex() local
248 mutex_lock(&cpts->ptp_clk_mutex); in cpts_ptp_gettimeex()
250 cpts_update_cur_time(cpts, CPTS_EV_PUSH, sts); in cpts_ptp_gettimeex()
252 ns = timecounter_read(&cpts->tc); in cpts_ptp_gettimeex()
253 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_gettimeex()
263 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_settime() local
268 mutex_lock(&cpts->ptp_clk_mutex); in cpts_ptp_settime()
269 timecounter_init(&cpts->tc, &cpts->cc, ns); in cpts_ptp_settime()
270 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_settime()
275 static int cpts_extts_enable(struct cpts *cpts, u32 index, int on) in cpts_extts_enable() argument
279 if (((cpts->hw_ts_enable & BIT(index)) >> index) == on) in cpts_extts_enable()
282 mutex_lock(&cpts->ptp_clk_mutex); in cpts_extts_enable()
284 v = cpts_read32(cpts, control); in cpts_extts_enable()
287 cpts->hw_ts_enable |= BIT(index); in cpts_extts_enable()
290 cpts->hw_ts_enable &= ~BIT(index); in cpts_extts_enable()
292 cpts_write32(cpts, v, control); in cpts_extts_enable()
294 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_extts_enable()
302 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_enable() local
306 return cpts_extts_enable(cpts, rq->extts.index, on); in cpts_ptp_enable()
314 static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event) in cpts_match_tx_ts() argument
329 spin_lock_irqsave(&cpts->txq.lock, flags); in cpts_match_tx_ts()
330 skb_queue_splice_init(&cpts->txq, &txq_list); in cpts_match_tx_ts()
331 spin_unlock_irqrestore(&cpts->txq.lock, flags); in cpts_match_tx_ts()
345 dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n", in cpts_match_tx_ts()
352 dev_dbg(cpts->dev, "expiring tx timestamp from txq\n"); in cpts_match_tx_ts()
358 spin_lock_irqsave(&cpts->txq.lock, flags); in cpts_match_tx_ts()
359 skb_queue_splice(&txq_list, &cpts->txq); in cpts_match_tx_ts()
360 spin_unlock_irqrestore(&cpts->txq.lock, flags); in cpts_match_tx_ts()
365 static void cpts_process_events(struct cpts *cpts) in cpts_process_events() argument
373 spin_lock_irqsave(&cpts->lock, flags); in cpts_process_events()
374 list_splice_init(&cpts->events, &events); in cpts_process_events()
375 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_process_events()
379 if (cpts_match_tx_ts(cpts, event) || in cpts_process_events()
386 spin_lock_irqsave(&cpts->lock, flags); in cpts_process_events()
387 list_splice_tail(&events, &cpts->events); in cpts_process_events()
388 list_splice_tail(&events_free, &cpts->pool); in cpts_process_events()
389 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_process_events()
394 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_overflow_check() local
395 unsigned long delay = cpts->ov_check_period; in cpts_overflow_check()
399 mutex_lock(&cpts->ptp_clk_mutex); in cpts_overflow_check()
401 cpts_update_cur_time(cpts, -1, NULL); in cpts_overflow_check()
402 ns = timecounter_read(&cpts->tc); in cpts_overflow_check()
404 cpts_process_events(cpts); in cpts_overflow_check()
406 spin_lock_irqsave(&cpts->txq.lock, flags); in cpts_overflow_check()
407 if (!skb_queue_empty(&cpts->txq)) { in cpts_overflow_check()
408 cpts_purge_txq(cpts); in cpts_overflow_check()
409 if (!skb_queue_empty(&cpts->txq)) in cpts_overflow_check()
412 spin_unlock_irqrestore(&cpts->txq.lock, flags); in cpts_overflow_check()
414 dev_dbg(cpts->dev, "cpts overflow check at %lld\n", ns); in cpts_overflow_check()
415 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_overflow_check()
457 static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, in cpts_find_ts() argument
466 cpts_fifo_read(cpts, -1); in cpts_find_ts()
467 spin_lock_irqsave(&cpts->lock, flags); in cpts_find_ts()
468 list_for_each_safe(this, next, &cpts->events) { in cpts_find_ts()
472 list_add(&event->list, &cpts->pool); in cpts_find_ts()
484 list_add(&event->list, &cpts->pool); in cpts_find_ts()
488 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_find_ts()
493 void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb) in cpts_rx_timestamp() argument
511 dev_dbg(cpts->dev, "%s mtype seqid %08x\n", in cpts_rx_timestamp()
514 ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid); in cpts_rx_timestamp()
523 void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb) in cpts_tx_timestamp() argument
537 dev_dbg(cpts->dev, "%s mtype seqid %08x\n", in cpts_tx_timestamp()
544 skb_queue_tail(&cpts->txq, skb); in cpts_tx_timestamp()
545 ptp_schedule_worker(cpts->clock, 0); in cpts_tx_timestamp()
549 int cpts_register(struct cpts *cpts) in cpts_register() argument
553 skb_queue_head_init(&cpts->txq); in cpts_register()
554 INIT_LIST_HEAD(&cpts->events); in cpts_register()
555 INIT_LIST_HEAD(&cpts->pool); in cpts_register()
557 list_add(&cpts->pool_data[i].list, &cpts->pool); in cpts_register()
559 err = clk_enable(cpts->refclk); in cpts_register()
563 cpts_write32(cpts, CPTS_EN, control); in cpts_register()
564 cpts_write32(cpts, TS_PEND_EN, int_enable); in cpts_register()
566 timecounter_init(&cpts->tc, &cpts->cc, ktime_get_real_ns()); in cpts_register()
568 cpts->clock = ptp_clock_register(&cpts->info, cpts->dev); in cpts_register()
569 if (IS_ERR(cpts->clock)) { in cpts_register()
570 err = PTR_ERR(cpts->clock); in cpts_register()
571 cpts->clock = NULL; in cpts_register()
574 cpts->phc_index = ptp_clock_index(cpts->clock); in cpts_register()
576 ptp_schedule_worker(cpts->clock, cpts->ov_check_period); in cpts_register()
580 clk_disable(cpts->refclk); in cpts_register()
585 void cpts_unregister(struct cpts *cpts) in cpts_unregister() argument
587 if (WARN_ON(!cpts->clock)) in cpts_unregister()
590 ptp_clock_unregister(cpts->clock); in cpts_unregister()
591 cpts->clock = NULL; in cpts_unregister()
592 cpts->phc_index = -1; in cpts_unregister()
594 cpts_write32(cpts, 0, int_enable); in cpts_unregister()
595 cpts_write32(cpts, 0, control); in cpts_unregister()
598 skb_queue_purge(&cpts->txq); in cpts_unregister()
600 clk_disable(cpts->refclk); in cpts_unregister()
604 static void cpts_calc_mult_shift(struct cpts *cpts) in cpts_calc_mult_shift() argument
609 freq = clk_get_rate(cpts->refclk); in cpts_calc_mult_shift()
614 maxsec = cpts->cc.mask; in cpts_calc_mult_shift()
623 cpts->ov_check_period = (HZ * maxsec) / 2; in cpts_calc_mult_shift()
624 dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n", in cpts_calc_mult_shift()
625 cpts->ov_check_period); in cpts_calc_mult_shift()
627 if (cpts->cc.mult || cpts->cc.shift) in cpts_calc_mult_shift()
630 clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift, in cpts_calc_mult_shift()
634 ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac); in cpts_calc_mult_shift()
636 dev_info(cpts->dev, in cpts_calc_mult_shift()
637 "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n", in cpts_calc_mult_shift()
638 freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC)); in cpts_calc_mult_shift()
651 static int cpts_of_mux_clk_setup(struct cpts *cpts, struct device_node *node) in cpts_of_mux_clk_setup() argument
660 refclk_np = of_get_child_by_name(node, "cpts-refclk-mux"); in cpts_of_mux_clk_setup()
667 dev_err(cpts->dev, "mux-clock %s must have parents\n", in cpts_of_mux_clk_setup()
672 parent_names = devm_kcalloc(cpts->dev, num_parents, in cpts_of_mux_clk_setup()
675 mux_table = devm_kcalloc(cpts->dev, num_parents, sizeof(*mux_table), in cpts_of_mux_clk_setup()
690 clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name, in cpts_of_mux_clk_setup()
693 &cpts->reg->rftclk_sel, 0, 0x1F, in cpts_of_mux_clk_setup()
700 ret = devm_add_action_or_reset(cpts->dev, cpts_clk_unregister, clk_hw); in cpts_of_mux_clk_setup()
702 dev_err(cpts->dev, "add clkmux unreg action %d", ret); in cpts_of_mux_clk_setup()
710 ret = devm_add_action_or_reset(cpts->dev, cpts_clk_del_provider, in cpts_of_mux_clk_setup()
713 dev_err(cpts->dev, "add clkmux provider unreg action %d", ret); in cpts_of_mux_clk_setup()
724 static int cpts_of_parse(struct cpts *cpts, struct device_node *node) in cpts_of_parse() argument
730 cpts->cc.mult = prop; in cpts_of_parse()
733 cpts->cc.shift = prop; in cpts_of_parse()
735 if ((cpts->cc.mult && !cpts->cc.shift) || in cpts_of_parse()
736 (!cpts->cc.mult && cpts->cc.shift)) in cpts_of_parse()
739 return cpts_of_mux_clk_setup(cpts, node); in cpts_of_parse()
742 dev_err(cpts->dev, "CPTS: Missing property in the DT.\n"); in cpts_of_parse()
746 struct cpts *cpts_create(struct device *dev, void __iomem *regs, in cpts_create()
749 struct cpts *cpts; in cpts_create() local
752 cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL); in cpts_create()
753 if (!cpts) in cpts_create()
756 cpts->dev = dev; in cpts_create()
757 cpts->reg = (struct cpsw_cpts __iomem *)regs; in cpts_create()
758 cpts->irq_poll = true; in cpts_create()
759 spin_lock_init(&cpts->lock); in cpts_create()
760 mutex_init(&cpts->ptp_clk_mutex); in cpts_create()
761 init_completion(&cpts->ts_push_complete); in cpts_create()
763 ret = cpts_of_parse(cpts, node); in cpts_create()
767 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); in cpts_create()
768 if (IS_ERR(cpts->refclk)) in cpts_create()
770 cpts->refclk = devm_clk_get(dev, "cpts"); in cpts_create()
772 if (IS_ERR(cpts->refclk)) { in cpts_create()
773 dev_err(dev, "Failed to get cpts refclk %ld\n", in cpts_create()
774 PTR_ERR(cpts->refclk)); in cpts_create()
775 return ERR_CAST(cpts->refclk); in cpts_create()
778 ret = clk_prepare(cpts->refclk); in cpts_create()
782 cpts->cc.read = cpts_systim_read; in cpts_create()
783 cpts->cc.mask = CLOCKSOURCE_MASK(32); in cpts_create()
784 cpts->info = cpts_info; in cpts_create()
785 cpts->phc_index = -1; in cpts_create()
788 cpts->info.n_ext_ts = n_ext_ts; in cpts_create()
790 cpts_calc_mult_shift(cpts); in cpts_create()
794 cpts->cc_mult = cpts->cc.mult; in cpts_create()
796 return cpts; in cpts_create()
800 void cpts_release(struct cpts *cpts) in cpts_release() argument
802 if (!cpts) in cpts_release()
805 if (WARN_ON(!cpts->refclk)) in cpts_release()
808 clk_unprepare(cpts->refclk); in cpts_release()
813 MODULE_DESCRIPTION("TI CPTS driver");