Lines Matching refs:cpts

45 static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)  in cpts_fifo_pop()  argument
47 u32 r = cpts_read32(cpts, intstat_raw); in cpts_fifo_pop()
50 *high = cpts_read32(cpts, event_high); in cpts_fifo_pop()
51 *low = cpts_read32(cpts, event_low); in cpts_fifo_pop()
52 cpts_write32(cpts, EVENT_POP, event_pop); in cpts_fifo_pop()
58 static int cpts_purge_events(struct cpts *cpts) in cpts_purge_events() argument
64 list_for_each_safe(this, next, &cpts->events) { in cpts_purge_events()
68 list_add(&event->list, &cpts->pool); in cpts_purge_events()
78 static void cpts_purge_txq(struct cpts *cpts) in cpts_purge_txq() argument
84 skb_queue_walk_safe(&cpts->txq, skb, tmp) { in cpts_purge_txq()
87 __skb_unlink(skb, &cpts->txq); in cpts_purge_txq()
94 dev_dbg(cpts->dev, "txq cleaned up %d\n", removed); in cpts_purge_txq()
97 static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event) in cpts_match_tx_ts() argument
108 skb_queue_walk_safe(&cpts->txq, skb, tmp) { in cpts_match_tx_ts()
115 u64 ns = timecounter_cyc2time(&cpts->tc, event->low); in cpts_match_tx_ts()
121 __skb_unlink(skb, &cpts->txq); in cpts_match_tx_ts()
123 dev_dbg(cpts->dev, "match tx timestamp mtype %u seqid %04x\n", in cpts_match_tx_ts()
130 dev_dbg(cpts->dev, "expiring tx timestamp from txq\n"); in cpts_match_tx_ts()
131 __skb_unlink(skb, &cpts->txq); in cpts_match_tx_ts()
142 static int cpts_fifo_read(struct cpts *cpts, int match) in cpts_fifo_read() argument
149 if (cpts_fifo_pop(cpts, &hi, &lo)) in cpts_fifo_read()
152 if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) { in cpts_fifo_read()
157 event = list_first_entry(&cpts->pool, struct cpts_event, list); in cpts_fifo_read()
164 if (cpts_match_tx_ts(cpts, event)) { in cpts_fifo_read()
174 list_add_tail(&event->list, &cpts->events); in cpts_fifo_read()
195 struct cpts *cpts = container_of(cc, struct cpts, cc); in cpts_systim_read() local
197 cpts_write32(cpts, TS_PUSH, ts_push); in cpts_systim_read()
198 if (cpts_fifo_read(cpts, CPTS_EV_PUSH)) in cpts_systim_read()
201 list_for_each_safe(this, next, &cpts->events) { in cpts_systim_read()
205 list_add(&event->list, &cpts->pool); in cpts_systim_read()
222 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_adjfreq() local
228 mult = cpts->cc_mult; in cpts_ptp_adjfreq()
233 spin_lock_irqsave(&cpts->lock, flags); in cpts_ptp_adjfreq()
235 timecounter_read(&cpts->tc); in cpts_ptp_adjfreq()
237 cpts->cc.mult = neg_adj ? mult - diff : mult + diff; in cpts_ptp_adjfreq()
239 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_ptp_adjfreq()
247 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_adjtime() local
249 spin_lock_irqsave(&cpts->lock, flags); in cpts_ptp_adjtime()
250 timecounter_adjtime(&cpts->tc, delta); in cpts_ptp_adjtime()
251 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_ptp_adjtime()
260 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_gettime() local
262 spin_lock_irqsave(&cpts->lock, flags); in cpts_ptp_gettime()
263 ns = timecounter_read(&cpts->tc); in cpts_ptp_gettime()
264 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_ptp_gettime()
276 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_ptp_settime() local
280 spin_lock_irqsave(&cpts->lock, flags); in cpts_ptp_settime()
281 timecounter_init(&cpts->tc, &cpts->cc, ns); in cpts_ptp_settime()
282 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_ptp_settime()
295 struct cpts *cpts = container_of(ptp, struct cpts, info); in cpts_overflow_check() local
296 unsigned long delay = cpts->ov_check_period; in cpts_overflow_check()
300 spin_lock_irqsave(&cpts->lock, flags); in cpts_overflow_check()
301 ts = ns_to_timespec64(timecounter_read(&cpts->tc)); in cpts_overflow_check()
303 if (!skb_queue_empty(&cpts->txq)) { in cpts_overflow_check()
304 cpts_purge_txq(cpts); in cpts_overflow_check()
305 if (!skb_queue_empty(&cpts->txq)) in cpts_overflow_check()
308 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_overflow_check()
367 static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb, int ev_type) in cpts_find_ts() argument
380 spin_lock_irqsave(&cpts->lock, flags); in cpts_find_ts()
381 cpts_fifo_read(cpts, -1); in cpts_find_ts()
382 list_for_each_safe(this, next, &cpts->events) { in cpts_find_ts()
386 list_add(&event->list, &cpts->pool); in cpts_find_ts()
393 ns = timecounter_cyc2time(&cpts->tc, event->low); in cpts_find_ts()
395 list_add(&event->list, &cpts->pool); in cpts_find_ts()
409 __skb_queue_tail(&cpts->txq, skb); in cpts_find_ts()
410 ptp_schedule_worker(cpts->clock, 0); in cpts_find_ts()
412 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_find_ts()
417 void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb) in cpts_rx_timestamp() argument
422 ns = cpts_find_ts(cpts, skb, CPTS_EV_RX); in cpts_rx_timestamp()
431 void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb) in cpts_tx_timestamp() argument
438 ns = cpts_find_ts(cpts, skb, CPTS_EV_TX); in cpts_tx_timestamp()
447 int cpts_register(struct cpts *cpts) in cpts_register() argument
451 skb_queue_head_init(&cpts->txq); in cpts_register()
452 INIT_LIST_HEAD(&cpts->events); in cpts_register()
453 INIT_LIST_HEAD(&cpts->pool); in cpts_register()
455 list_add(&cpts->pool_data[i].list, &cpts->pool); in cpts_register()
457 clk_enable(cpts->refclk); in cpts_register()
459 cpts_write32(cpts, CPTS_EN, control); in cpts_register()
460 cpts_write32(cpts, TS_PEND_EN, int_enable); in cpts_register()
462 timecounter_init(&cpts->tc, &cpts->cc, ktime_to_ns(ktime_get_real())); in cpts_register()
464 cpts->clock = ptp_clock_register(&cpts->info, cpts->dev); in cpts_register()
465 if (IS_ERR(cpts->clock)) { in cpts_register()
466 err = PTR_ERR(cpts->clock); in cpts_register()
467 cpts->clock = NULL; in cpts_register()
470 cpts->phc_index = ptp_clock_index(cpts->clock); in cpts_register()
472 ptp_schedule_worker(cpts->clock, cpts->ov_check_period); in cpts_register()
476 clk_disable(cpts->refclk); in cpts_register()
481 void cpts_unregister(struct cpts *cpts) in cpts_unregister() argument
483 if (WARN_ON(!cpts->clock)) in cpts_unregister()
486 ptp_clock_unregister(cpts->clock); in cpts_unregister()
487 cpts->clock = NULL; in cpts_unregister()
489 cpts_write32(cpts, 0, int_enable); in cpts_unregister()
490 cpts_write32(cpts, 0, control); in cpts_unregister()
493 skb_queue_purge(&cpts->txq); in cpts_unregister()
495 clk_disable(cpts->refclk); in cpts_unregister()
499 static void cpts_calc_mult_shift(struct cpts *cpts) in cpts_calc_mult_shift() argument
504 freq = clk_get_rate(cpts->refclk); in cpts_calc_mult_shift()
509 maxsec = cpts->cc.mask; in cpts_calc_mult_shift()
518 cpts->ov_check_period = (HZ * maxsec) / 2; in cpts_calc_mult_shift()
519 dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n", in cpts_calc_mult_shift()
520 cpts->ov_check_period); in cpts_calc_mult_shift()
522 if (cpts->cc.mult || cpts->cc.shift) in cpts_calc_mult_shift()
525 clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift, in cpts_calc_mult_shift()
529 ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac); in cpts_calc_mult_shift()
531 dev_info(cpts->dev, in cpts_calc_mult_shift()
533 freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC)); in cpts_calc_mult_shift()
536 static int cpts_of_mux_clk_setup(struct cpts *cpts, struct device_node *node) in cpts_of_mux_clk_setup() argument
552 dev_err(cpts->dev, "mux-clock %s must have parents\n", in cpts_of_mux_clk_setup()
557 parent_names = devm_kzalloc(cpts->dev, (sizeof(char *) * num_parents), in cpts_of_mux_clk_setup()
560 mux_table = devm_kzalloc(cpts->dev, sizeof(*mux_table) * num_parents, in cpts_of_mux_clk_setup()
575 clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name, in cpts_of_mux_clk_setup()
578 &cpts->reg->rftclk_sel, 0, 0x1F, in cpts_of_mux_clk_setup()
585 ret = devm_add_action_or_reset(cpts->dev, in cpts_of_mux_clk_setup()
589 dev_err(cpts->dev, "add clkmux unreg action %d", ret); in cpts_of_mux_clk_setup()
597 ret = devm_add_action_or_reset(cpts->dev, in cpts_of_mux_clk_setup()
601 dev_err(cpts->dev, "add clkmux provider unreg action %d", ret); in cpts_of_mux_clk_setup()
612 static int cpts_of_parse(struct cpts *cpts, struct device_node *node) in cpts_of_parse() argument
618 cpts->cc.mult = prop; in cpts_of_parse()
621 cpts->cc.shift = prop; in cpts_of_parse()
623 if ((cpts->cc.mult && !cpts->cc.shift) || in cpts_of_parse()
624 (!cpts->cc.mult && cpts->cc.shift)) in cpts_of_parse()
627 return cpts_of_mux_clk_setup(cpts, node); in cpts_of_parse()
630 dev_err(cpts->dev, "CPTS: Missing property in the DT.\n"); in cpts_of_parse()
634 struct cpts *cpts_create(struct device *dev, void __iomem *regs, in cpts_create()
637 struct cpts *cpts; in cpts_create() local
640 cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL); in cpts_create()
641 if (!cpts) in cpts_create()
644 cpts->dev = dev; in cpts_create()
645 cpts->reg = (struct cpsw_cpts __iomem *)regs; in cpts_create()
646 spin_lock_init(&cpts->lock); in cpts_create()
648 ret = cpts_of_parse(cpts, node); in cpts_create()
652 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); in cpts_create()
653 if (IS_ERR(cpts->refclk)) in cpts_create()
655 cpts->refclk = devm_clk_get(dev, "cpts"); in cpts_create()
657 if (IS_ERR(cpts->refclk)) { in cpts_create()
659 PTR_ERR(cpts->refclk)); in cpts_create()
660 return ERR_CAST(cpts->refclk); in cpts_create()
663 ret = clk_prepare(cpts->refclk); in cpts_create()
667 cpts->cc.read = cpts_systim_read; in cpts_create()
668 cpts->cc.mask = CLOCKSOURCE_MASK(32); in cpts_create()
669 cpts->info = cpts_info; in cpts_create()
671 cpts_calc_mult_shift(cpts); in cpts_create()
675 cpts->cc_mult = cpts->cc.mult; in cpts_create()
677 return cpts; in cpts_create()
681 void cpts_release(struct cpts *cpts) in cpts_release() argument
683 if (!cpts) in cpts_release()
686 if (WARN_ON(!cpts->refclk)) in cpts_release()
689 clk_unprepare(cpts->refclk); in cpts_release()