Lines Matching +full:refclk +full:- +full:mux
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()
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()
226 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_ptp_adjfine()
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()
248 mutex_lock(&cpts->ptp_clk_mutex); 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()
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()
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()
287 cpts->hw_ts_enable |= BIT(index); in cpts_extts_enable()
290 cpts->hw_ts_enable &= ~BIT(index); in cpts_extts_enable()
294 mutex_unlock(&cpts->ptp_clk_mutex); in cpts_extts_enable()
304 switch (rq->type) { in cpts_ptp_enable()
306 return cpts_extts_enable(cpts, rq->extts.index, on); in cpts_ptp_enable()
311 return -EOPNOTSUPP; in cpts_ptp_enable()
322 mtype_seqid = event->high & in cpts_match_tx_ts()
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()
336 (struct cpts_skb_cb_data *)skb->cb; in cpts_match_tx_ts()
338 if (mtype_seqid == skb_cb->skb_mtype_seqid) { in cpts_match_tx_ts()
340 ssh.hwtstamp = ns_to_ktime(event->timestamp); in cpts_match_tx_ts()
345 dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n", in cpts_match_tx_ts()
350 if (time_after(jiffies, skb_cb->tmo)) { 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()
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()
380 time_after(jiffies, event->tmo)) { in cpts_process_events()
381 list_del_init(&event->list); in cpts_process_events()
382 list_add(&event->list, &events_free); 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()
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()
406 spin_lock_irqsave(&cpts->txq.lock, flags); in cpts_overflow_check()
407 if (!skb_queue_empty(&cpts->txq)) { 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()
449 seqid = ntohs(hdr->sequence_id); in cpts_skb_get_mtype_seqid()
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()
471 list_del_init(&event->list); in cpts_find_ts()
472 list_add(&event->list, &cpts->pool); in cpts_find_ts()
476 mtype_seqid = event->high & in cpts_find_ts()
482 ns = event->timestamp; 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 spin_unlock_irqrestore(&cpts->lock, flags); in cpts_find_ts()
495 struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb; in cpts_rx_timestamp()
505 ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid); in cpts_rx_timestamp()
509 skb_cb->skb_mtype_seqid |= (CPTS_EV_RX << EVENT_TYPE_SHIFT); in cpts_rx_timestamp()
511 dev_dbg(cpts->dev, "%s mtype seqid %08x\n", in cpts_rx_timestamp()
512 __func__, skb_cb->skb_mtype_seqid); in cpts_rx_timestamp()
514 ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid); in cpts_rx_timestamp()
519 ssh->hwtstamp = ns_to_ktime(ns); in cpts_rx_timestamp()
525 struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb; in cpts_tx_timestamp()
528 if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) in cpts_tx_timestamp()
531 ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid); in cpts_tx_timestamp()
535 skb_cb->skb_mtype_seqid |= (CPTS_EV_TX << EVENT_TYPE_SHIFT); in cpts_tx_timestamp()
537 dev_dbg(cpts->dev, "%s mtype seqid %08x\n", in cpts_tx_timestamp()
538 __func__, skb_cb->skb_mtype_seqid); in cpts_tx_timestamp()
543 skb_cb->tmo = jiffies + msecs_to_jiffies(CPTS_SKB_RX_TX_TMO); 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()
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()
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()
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()
598 skb_queue_purge(&cpts->txq); in cpts_unregister()
600 clk_disable(cpts->refclk); in cpts_unregister()
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()
638 freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC)); in cpts_calc_mult_shift()
647 int ret = -EINVAL; in cpts_of_mux_clk_setup()
650 refclk_np = of_get_child_by_name(node, "cpts-refclk-mux"); in cpts_of_mux_clk_setup()
652 /* refclk selection supported not for all SoCs */ in cpts_of_mux_clk_setup()
657 dev_err(cpts->dev, "mux-clock %s must have parents\n", in cpts_of_mux_clk_setup()
658 refclk_np->name); in cpts_of_mux_clk_setup()
662 parent_names = devm_kcalloc(cpts->dev, num_parents, in cpts_of_mux_clk_setup()
665 mux_table = devm_kcalloc(cpts->dev, num_parents, sizeof(*mux_table), in cpts_of_mux_clk_setup()
668 ret = -ENOMEM; in cpts_of_mux_clk_setup()
674 ret = of_property_read_variable_u32_array(refclk_np, "ti,mux-tbl", in cpts_of_mux_clk_setup()
680 clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name, in cpts_of_mux_clk_setup()
683 &cpts->reg->rftclk_sel, 0, 0x1F, in cpts_of_mux_clk_setup()
690 ret = devm_add_action_or_reset(cpts->dev, in cpts_of_mux_clk_setup()
694 dev_err(cpts->dev, "add clkmux unreg action %d", ret); 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 provider unreg action %d", ret); in cpts_of_mux_clk_setup()
719 int ret = -EINVAL; in cpts_of_parse()
723 cpts->cc.mult = prop; in cpts_of_parse()
726 cpts->cc.shift = prop; in cpts_of_parse()
728 if ((cpts->cc.mult && !cpts->cc.shift) || in cpts_of_parse()
729 (!cpts->cc.mult && cpts->cc.shift)) in cpts_of_parse()
735 dev_err(cpts->dev, "CPTS: Missing property in the DT.\n"); in cpts_of_parse()
747 return ERR_PTR(-ENOMEM); in cpts_create()
749 cpts->dev = dev; in cpts_create()
750 cpts->reg = (struct cpsw_cpts __iomem *)regs; in cpts_create()
751 cpts->irq_poll = true; in cpts_create()
752 spin_lock_init(&cpts->lock); in cpts_create()
753 mutex_init(&cpts->ptp_clk_mutex); in cpts_create()
754 init_completion(&cpts->ts_push_complete); in cpts_create()
760 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); in cpts_create()
761 if (IS_ERR(cpts->refclk)) in cpts_create()
763 cpts->refclk = devm_clk_get(dev, "cpts"); in cpts_create()
765 if (IS_ERR(cpts->refclk)) { in cpts_create()
766 dev_err(dev, "Failed to get cpts refclk %ld\n", in cpts_create()
767 PTR_ERR(cpts->refclk)); in cpts_create()
768 return ERR_CAST(cpts->refclk); in cpts_create()
771 ret = clk_prepare(cpts->refclk); in cpts_create()
775 cpts->cc.read = cpts_systim_read; in cpts_create()
776 cpts->cc.mask = CLOCKSOURCE_MASK(32); in cpts_create()
777 cpts->info = cpts_info; in cpts_create()
778 cpts->phc_index = -1; in cpts_create()
781 cpts->info.n_ext_ts = n_ext_ts; in cpts_create()
787 cpts->cc_mult = cpts->cc.mult; in cpts_create()
798 if (WARN_ON(!cpts->refclk)) in cpts_release()
801 clk_unprepare(cpts->refclk); in cpts_release()