Lines Matching +full:- +full:refclk
1 // SPDX-License-Identifier: GPL-2.0+
8 #include <linux/clk-provider.h>
32 #define cpts_read32(c, r) readl_relaxed(&c->reg->r)
33 #define cpts_write32(c, v, r) writel_relaxed(v, &c->reg->r)
37 return (event->high >> PORT_NUMBER_SHIFT) & PORT_NUMBER_MASK; in cpts_event_port()
42 return time_after(jiffies, event->tmo); in event_expired()
47 return (event->high >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; in event_type()
60 return -1; in cpts_fifo_pop()
69 list_for_each_safe(this, next, &cpts->events) { in cpts_purge_events()
72 list_del_init(&event->list); 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()
80 return removed ? 0 : -1; in cpts_purge_events()
89 skb_queue_walk_safe(&cpts->txq, skb, tmp) { in cpts_purge_txq()
90 skb_cb = (struct cpts_skb_cb_data *)skb->cb; in cpts_purge_txq()
91 if (time_after(jiffies, skb_cb->tmo)) { 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()
111 int i, type = -1; in cpts_fifo_read()
114 spin_lock_irqsave(&cpts->lock, flags); 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()
126 event->high = hi; in cpts_fifo_read()
127 event->low = lo; 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()
132 type, event->high, event->low); 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()
146 event->tmo = jiffies + in cpts_fifo_read()
149 list_del_init(&event->list); in cpts_fifo_read()
150 list_add_tail(&event->list, &cpts->events); in cpts_fifo_read()
157 pevent.timestamp = event->timestamp; in cpts_fifo_read()
159 pevent.index = cpts_event_port(event) - 1; 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()
175 return type == match ? 0 : -1; in cpts_fifo_read()
180 cpts_fifo_read(cpts, -1); in cpts_misc_interrupt()
188 return READ_ONCE(cpts->cur_timestamp); in cpts_systim_read()
196 reinit_completion(&cpts->ts_push_complete); in cpts_update_cur_time()
199 spin_lock_irqsave(&cpts->lock, flags); 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()
225 ppb = -ppb; in cpts_ptp_adjfreq()
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()
238 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_adjfreq()
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()
260 mutex_lock(&cpts->ptp_clk_mutex); 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()
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()
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()
299 cpts->hw_ts_enable |= BIT(index); in cpts_extts_enable()
302 cpts->hw_ts_enable &= ~BIT(index); in cpts_extts_enable()
306 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_extts_enable()
316 switch (rq->type) { in cpts_ptp_enable()
318 return cpts_extts_enable(cpts, rq->extts.index, on); in cpts_ptp_enable()
323 return -EOPNOTSUPP; in cpts_ptp_enable()
334 mtype_seqid = event->high & in cpts_match_tx_ts()
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()
348 (struct cpts_skb_cb_data *)skb->cb; in cpts_match_tx_ts()
350 if (mtype_seqid == skb_cb->skb_mtype_seqid) { in cpts_match_tx_ts()
352 ssh.hwtstamp = ns_to_ktime(event->timestamp); in cpts_match_tx_ts()
357 dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n", in cpts_match_tx_ts()
362 if (time_after(jiffies, skb_cb->tmo)) { 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()
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()
392 time_after(jiffies, event->tmo)) { in cpts_process_events()
393 list_del_init(&event->list); in cpts_process_events()
394 list_add(&event->list, &events_free); 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()
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()
418 spin_lock_irqsave(&cpts->txq.lock, flags); in cpts_overflow_check()
419 if (!skb_queue_empty(&cpts->txq)) { 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()
461 seqid = ntohs(hdr->sequence_id); in cpts_skb_get_mtype_seqid()
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()
483 list_del_init(&event->list); in cpts_find_ts()
484 list_add(&event->list, &cpts->pool); in cpts_find_ts()
488 mtype_seqid = event->high & in cpts_find_ts()
494 ns = event->timestamp; in cpts_find_ts()
495 list_del_init(&event->list); 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()
507 struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb; in cpts_rx_timestamp()
517 ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid); in cpts_rx_timestamp()
521 skb_cb->skb_mtype_seqid |= (CPTS_EV_RX << EVENT_TYPE_SHIFT); in cpts_rx_timestamp()
523 dev_dbg(cpts->dev, "%s mtype seqid %08x\n", in cpts_rx_timestamp()
524 __func__, skb_cb->skb_mtype_seqid); in cpts_rx_timestamp()
526 ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid); in cpts_rx_timestamp()
531 ssh->hwtstamp = ns_to_ktime(ns); in cpts_rx_timestamp()
537 struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb; in cpts_tx_timestamp()
540 if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) in cpts_tx_timestamp()
543 ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid); in cpts_tx_timestamp()
547 skb_cb->skb_mtype_seqid |= (CPTS_EV_TX << EVENT_TYPE_SHIFT); in cpts_tx_timestamp()
549 dev_dbg(cpts->dev, "%s mtype seqid %08x\n", in cpts_tx_timestamp()
550 __func__, skb_cb->skb_mtype_seqid); in cpts_tx_timestamp()
555 skb_cb->tmo = jiffies + msecs_to_jiffies(CPTS_SKB_RX_TX_TMO); 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()
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 clk_enable(cpts->refclk); in cpts_register()
576 timecounter_init(&cpts->tc, &cpts->cc, ktime_get_real_ns()); in cpts_register()
578 cpts->clock = ptp_clock_register(&cpts->info, cpts->dev); in cpts_register()
579 if (IS_ERR(cpts->clock)) { in cpts_register()
580 err = PTR_ERR(cpts->clock); in cpts_register()
581 cpts->clock = NULL; in cpts_register()
584 cpts->phc_index = ptp_clock_index(cpts->clock); in cpts_register()
586 ptp_schedule_worker(cpts->clock, cpts->ov_check_period); in cpts_register()
590 clk_disable(cpts->refclk); in cpts_register()
597 if (WARN_ON(!cpts->clock)) in cpts_unregister()
600 ptp_clock_unregister(cpts->clock); in cpts_unregister()
601 cpts->clock = NULL; in cpts_unregister()
607 skb_queue_purge(&cpts->txq); in cpts_unregister()
609 clk_disable(cpts->refclk); in cpts_unregister()
618 freq = clk_get_rate(cpts->refclk); in cpts_calc_mult_shift()
623 maxsec = cpts->cc.mask; in cpts_calc_mult_shift()
632 cpts->ov_check_period = (HZ * maxsec) / 2; in cpts_calc_mult_shift()
633 dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n", in cpts_calc_mult_shift()
634 cpts->ov_check_period); in cpts_calc_mult_shift()
636 if (cpts->cc.mult || cpts->cc.shift) in cpts_calc_mult_shift()
639 clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift, in cpts_calc_mult_shift()
643 ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac); in cpts_calc_mult_shift()
645 dev_info(cpts->dev, in cpts_calc_mult_shift()
647 freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC)); in cpts_calc_mult_shift()
656 int ret = -EINVAL; in cpts_of_mux_clk_setup()
659 refclk_np = of_get_child_by_name(node, "cpts-refclk-mux"); in cpts_of_mux_clk_setup()
661 /* refclk selection supported not for all SoCs */ in cpts_of_mux_clk_setup()
666 dev_err(cpts->dev, "mux-clock %s must have parents\n", in cpts_of_mux_clk_setup()
667 refclk_np->name); in cpts_of_mux_clk_setup()
671 parent_names = devm_kzalloc(cpts->dev, (sizeof(char *) * num_parents), in cpts_of_mux_clk_setup()
674 mux_table = devm_kzalloc(cpts->dev, sizeof(*mux_table) * num_parents, in cpts_of_mux_clk_setup()
677 ret = -ENOMEM; in cpts_of_mux_clk_setup()
683 ret = of_property_read_variable_u32_array(refclk_np, "ti,mux-tbl", in cpts_of_mux_clk_setup()
689 clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name, in cpts_of_mux_clk_setup()
692 &cpts->reg->rftclk_sel, 0, 0x1F, in cpts_of_mux_clk_setup()
699 ret = devm_add_action_or_reset(cpts->dev, in cpts_of_mux_clk_setup()
703 dev_err(cpts->dev, "add clkmux unreg action %d", ret); in cpts_of_mux_clk_setup()
711 ret = devm_add_action_or_reset(cpts->dev, in cpts_of_mux_clk_setup()
715 dev_err(cpts->dev, "add clkmux provider unreg action %d", ret); in cpts_of_mux_clk_setup()
728 int ret = -EINVAL; in cpts_of_parse()
732 cpts->cc.mult = prop; in cpts_of_parse()
735 cpts->cc.shift = prop; in cpts_of_parse()
737 if ((cpts->cc.mult && !cpts->cc.shift) || in cpts_of_parse()
738 (!cpts->cc.mult && cpts->cc.shift)) in cpts_of_parse()
744 dev_err(cpts->dev, "CPTS: Missing property in the DT.\n"); in cpts_of_parse()
756 return ERR_PTR(-ENOMEM); in cpts_create()
758 cpts->dev = dev; in cpts_create()
759 cpts->reg = (struct cpsw_cpts __iomem *)regs; in cpts_create()
760 cpts->irq_poll = true; in cpts_create()
761 spin_lock_init(&cpts->lock); in cpts_create()
762 mutex_init(&cpts->ptp_clk_mutex); in cpts_create()
763 init_completion(&cpts->ts_push_complete); in cpts_create()
769 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); in cpts_create()
770 if (IS_ERR(cpts->refclk)) in cpts_create()
772 cpts->refclk = devm_clk_get(dev, "cpts"); in cpts_create()
774 if (IS_ERR(cpts->refclk)) { in cpts_create()
775 dev_err(dev, "Failed to get cpts refclk %ld\n", in cpts_create()
776 PTR_ERR(cpts->refclk)); in cpts_create()
777 return ERR_CAST(cpts->refclk); in cpts_create()
780 ret = clk_prepare(cpts->refclk); in cpts_create()
784 cpts->cc.read = cpts_systim_read; in cpts_create()
785 cpts->cc.mask = CLOCKSOURCE_MASK(32); in cpts_create()
786 cpts->info = cpts_info; in cpts_create()
789 cpts->info.n_ext_ts = n_ext_ts; in cpts_create()
795 cpts->cc_mult = cpts->cc.mult; in cpts_create()
806 if (WARN_ON(!cpts->refclk)) in cpts_release()
809 clk_unprepare(cpts->refclk); in cpts_release()