Lines Matching +full:refclk +full:- +full:mux

1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com
9 #include <linux/clk-provider.h>
23 #include "am65-cpts.h"
164 struct clk *refclk; member
186 #define am65_cpts_write32(c, v, r) writel(v, &(c)->reg->r)
187 #define am65_cpts_read32(c, r) readl(&(c)->reg->r)
204 cpts->ts_add_val = (NSEC_PER_SEC / cpts->refclk_freq - 1) & 0x7; in am65_cpts_set_add_val()
206 am65_cpts_write32(cpts, cpts->ts_add_val, ts_add_val); in am65_cpts_set_add_val()
217 return (event->event1 & AM65_CPTS_EVENT_1_PORT_NUMBER_MASK) >> in am65_cpts_event_get_port()
223 return (event->event1 & AM65_CPTS_EVENT_1_EVENT_TYPE_MASK) >> in am65_cpts_event_get_type()
233 list_for_each_safe(this, next, &cpts->events) { in am65_cpts_cpts_purge_events()
235 if (time_after(jiffies, event->tmo)) { in am65_cpts_cpts_purge_events()
236 list_del_init(&event->list); in am65_cpts_cpts_purge_events()
237 list_add(&event->list, &cpts->pool); in am65_cpts_cpts_purge_events()
243 dev_dbg(cpts->dev, "event pool cleaned up %d\n", removed); in am65_cpts_cpts_purge_events()
244 return removed ? 0 : -1; in am65_cpts_cpts_purge_events()
253 event->timestamp = am65_cpts_read32(cpts, event_0); in am65_cpts_fifo_pop_event()
254 event->event1 = am65_cpts_read32(cpts, event_1); in am65_cpts_fifo_pop_event()
255 event->event2 = am65_cpts_read32(cpts, event_2); in am65_cpts_fifo_pop_event()
256 event->timestamp |= (u64)am65_cpts_read32(cpts, event_3) << 32; in am65_cpts_fifo_pop_event()
271 spin_lock_irqsave(&cpts->lock, flags); in am65_cpts_fifo_read()
273 event = list_first_entry_or_null(&cpts->pool, in am65_cpts_fifo_read()
278 dev_err(cpts->dev, "cpts: event pool empty\n"); in am65_cpts_fifo_read()
279 ret = -1; in am65_cpts_fifo_read()
291 cpts->timestamp = event->timestamp; in am65_cpts_fifo_read()
292 dev_dbg(cpts->dev, "AM65_CPTS_EV_PUSH t:%llu\n", in am65_cpts_fifo_read()
293 cpts->timestamp); in am65_cpts_fifo_read()
297 event->tmo = jiffies + in am65_cpts_fifo_read()
300 list_del_init(&event->list); in am65_cpts_fifo_read()
301 list_add_tail(&event->list, &cpts->events); in am65_cpts_fifo_read()
303 dev_dbg(cpts->dev, in am65_cpts_fifo_read()
305 event->event1, event->event2, in am65_cpts_fifo_read()
306 event->timestamp); in am65_cpts_fifo_read()
310 pevent.index = am65_cpts_event_get_port(event) - 1; in am65_cpts_fifo_read()
311 pevent.timestamp = event->timestamp; in am65_cpts_fifo_read()
313 dev_dbg(cpts->dev, "AM65_CPTS_EV_HW p:%d t:%llu\n", in am65_cpts_fifo_read()
314 pevent.index, event->timestamp); in am65_cpts_fifo_read()
316 ptp_clock_event(cpts->ptp_clock, &pevent); in am65_cpts_fifo_read()
323 dev_dbg(cpts->dev, in am65_cpts_fifo_read()
326 event->event1, event->event2, in am65_cpts_fifo_read()
327 event->timestamp); in am65_cpts_fifo_read()
330 dev_err(cpts->dev, "cpts: unknown event type\n"); in am65_cpts_fifo_read()
331 ret = -1; in am65_cpts_fifo_read()
337 spin_unlock_irqrestore(&cpts->lock, flags); in am65_cpts_fifo_read()
340 ptp_schedule_worker(cpts->ptp_clock, 0); in am65_cpts_fifo_read()
352 * doubled read. Interrupt can be in-flight - it's Ok. in am65_cpts_gettime()
357 spin_lock_irqsave(&cpts->lock, flags); in am65_cpts_gettime()
362 spin_unlock_irqrestore(&cpts->lock, flags); in am65_cpts_gettime()
368 val = cpts->timestamp; in am65_cpts_gettime()
378 dev_dbg(cpts->dev, "cpts: unable to obtain a time stamp\n"); in am65_cpts_interrupt()
393 ppb = -ppb; in am65_cpts_ptp_adjfreq()
405 adj_period = div_u64(cpts->refclk_freq, ppb); in am65_cpts_ptp_adjfreq()
407 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ptp_adjfreq()
421 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ptp_adjfreq()
431 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ptp_adjtime()
435 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ptp_adjtime()
447 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ptp_gettimex()
449 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ptp_gettimex()
460 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ns_gettime()
462 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ns_gettime()
475 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_ptp_settime()
477 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_ptp_settime()
489 cpts->hw_ts_enable |= BIT(index); in am65_cpts_extts_enable_hw()
492 cpts->hw_ts_enable &= ~BIT(index); in am65_cpts_extts_enable_hw()
499 if (!!(cpts->hw_ts_enable & BIT(index)) == !!on) in am65_cpts_extts_enable()
502 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_extts_enable()
504 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_extts_enable()
506 dev_dbg(cpts->dev, "%s: ExtTS:%u %s\n", in am65_cpts_extts_enable()
518 cycles = cfg->ns_period * cpts->refclk_freq; in am65_cpts_estf_enable()
521 return -EINVAL; in am65_cpts_estf_enable()
526 val = upper_32_bits(cfg->ns_start); in am65_cpts_estf_enable()
528 val = lower_32_bits(cfg->ns_start); in am65_cpts_estf_enable()
533 dev_dbg(cpts->dev, "%s: ESTF:%u enabled\n", __func__, idx); in am65_cpts_estf_enable()
543 dev_dbg(cpts->dev, "%s: ESTF:%u disabled\n", __func__, idx); in am65_cpts_estf_disable()
555 ts.tv_sec = req->period.sec; in am65_cpts_perout_enable_hw()
556 ts.tv_nsec = req->period.nsec; in am65_cpts_perout_enable_hw()
559 cycles = (ns_period * cpts->refclk_freq) / NSEC_PER_SEC; in am65_cpts_perout_enable_hw()
561 ts.tv_sec = req->start.sec; in am65_cpts_perout_enable_hw()
562 ts.tv_nsec = req->start.nsec; in am65_cpts_perout_enable_hw()
566 am65_cpts_write32(cpts, val, genf[req->index].comp_hi); in am65_cpts_perout_enable_hw()
568 am65_cpts_write32(cpts, val, genf[req->index].comp_lo); in am65_cpts_perout_enable_hw()
570 am65_cpts_write32(cpts, val, genf[req->index].length); in am65_cpts_perout_enable_hw()
572 cpts->genf_enable |= BIT(req->index); in am65_cpts_perout_enable_hw()
574 am65_cpts_write32(cpts, 0, genf[req->index].length); in am65_cpts_perout_enable_hw()
576 cpts->genf_enable &= ~BIT(req->index); in am65_cpts_perout_enable_hw()
583 if (!!(cpts->genf_enable & BIT(req->index)) == !!on) in am65_cpts_perout_enable()
586 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_perout_enable()
588 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_perout_enable()
590 dev_dbg(cpts->dev, "%s: GenF:%u %s\n", in am65_cpts_perout_enable()
591 __func__, req->index, on ? "enabled" : "disabled"); in am65_cpts_perout_enable()
601 switch (rq->type) { in am65_cpts_ptp_enable()
603 return am65_cpts_extts_enable(cpts, rq->extts.index, on); in am65_cpts_ptp_enable()
605 return am65_cpts_perout_enable(cpts, &rq->perout, on); in am65_cpts_ptp_enable()
610 return -EOPNOTSUPP; in am65_cpts_ptp_enable()
635 mtype_seqid = event->event1 & in am65_cpts_match_tx_ts()
642 spin_lock_irqsave(&cpts->txq.lock, flags); in am65_cpts_match_tx_ts()
643 skb_queue_splice_init(&cpts->txq, &txq_list); in am65_cpts_match_tx_ts()
644 spin_unlock_irqrestore(&cpts->txq.lock, flags); in am65_cpts_match_tx_ts()
646 /* no need to grab txq.lock as access is always done under cpts->lock */ in am65_cpts_match_tx_ts()
650 (struct am65_cpts_skb_cb_data *)skb->cb; in am65_cpts_match_tx_ts()
652 if (mtype_seqid == skb_cb->skb_mtype_seqid) { in am65_cpts_match_tx_ts()
653 u64 ns = event->timestamp; in am65_cpts_match_tx_ts()
661 dev_dbg(cpts->dev, in am65_cpts_match_tx_ts()
667 if (time_after(jiffies, skb_cb->tmo)) { in am65_cpts_match_tx_ts()
669 dev_dbg(cpts->dev, in am65_cpts_match_tx_ts()
677 spin_lock_irqsave(&cpts->txq.lock, flags); in am65_cpts_match_tx_ts()
678 skb_queue_splice(&txq_list, &cpts->txq); in am65_cpts_match_tx_ts()
679 spin_unlock_irqrestore(&cpts->txq.lock, flags); in am65_cpts_match_tx_ts()
692 spin_lock_irqsave(&cpts->lock, flags); in am65_cpts_find_ts()
693 list_splice_init(&cpts->events, &events); in am65_cpts_find_ts()
694 spin_unlock_irqrestore(&cpts->lock, flags); in am65_cpts_find_ts()
699 time_after(jiffies, event->tmo)) { in am65_cpts_find_ts()
700 list_del_init(&event->list); in am65_cpts_find_ts()
701 list_add(&event->list, &events_free); in am65_cpts_find_ts()
705 spin_lock_irqsave(&cpts->lock, flags); in am65_cpts_find_ts()
706 list_splice_tail(&events, &cpts->events); in am65_cpts_find_ts()
707 list_splice_tail(&events_free, &cpts->pool); in am65_cpts_find_ts()
708 spin_unlock_irqrestore(&cpts->lock, flags); in am65_cpts_find_ts()
715 long delay = -1; in am65_cpts_ts_work()
719 spin_lock_irqsave(&cpts->txq.lock, flags); in am65_cpts_ts_work()
720 if (!skb_queue_empty(&cpts->txq)) in am65_cpts_ts_work()
722 spin_unlock_irqrestore(&cpts->txq.lock, flags); in am65_cpts_ts_work()
728 * am65_cpts_rx_enable - enable rx timestamping
739 mutex_lock(&cpts->ptp_clk_lock); in am65_cpts_rx_enable()
746 mutex_unlock(&cpts->ptp_clk_lock); in am65_cpts_rx_enable()
765 seqid = ntohs(hdr->sequence_id); in am65_skb_get_mtype_seqid()
775 * am65_cpts_tx_timestamp - save tx packet for timestamping
784 struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb; in am65_cpts_tx_timestamp()
786 if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) in am65_cpts_tx_timestamp()
794 skb_cb->tmo = jiffies + msecs_to_jiffies(100); in am65_cpts_tx_timestamp()
795 skb_queue_tail(&cpts->txq, skb); in am65_cpts_tx_timestamp()
796 ptp_schedule_worker(cpts->ptp_clock, 0); in am65_cpts_tx_timestamp()
801 * am65_cpts_prep_tx_timestamp - check and prepare tx packet for timestamping
807 * in skb-cb and marks packet as SKBTX_IN_PROGRESS.
811 struct am65_cpts_skb_cb_data *skb_cb = (void *)skb->cb; in am65_cpts_prep_tx_timestamp()
814 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) in am65_cpts_prep_tx_timestamp()
817 ret = am65_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid); in am65_cpts_prep_tx_timestamp()
820 skb_cb->skb_mtype_seqid |= (AM65_CPTS_EV_TX << in am65_cpts_prep_tx_timestamp()
823 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; in am65_cpts_prep_tx_timestamp()
829 return cpts->phc_index; in am65_cpts_phc_index()
837 of_clk_del_provider(cpts->clk_mux_np); in cpts_free_clk_mux()
838 clk_hw_unregister_mux(cpts->clk_mux_hw); in cpts_free_clk_mux()
839 of_node_put(cpts->clk_mux_np); in cpts_free_clk_mux()
849 int ret = -EINVAL; in cpts_of_mux_clk_setup()
851 cpts->clk_mux_np = of_get_child_by_name(node, "refclk-mux"); in cpts_of_mux_clk_setup()
852 if (!cpts->clk_mux_np) in cpts_of_mux_clk_setup()
855 num_parents = of_clk_get_parent_count(cpts->clk_mux_np); in cpts_of_mux_clk_setup()
857 dev_err(cpts->dev, "mux-clock %pOF must have parents\n", in cpts_of_mux_clk_setup()
858 cpts->clk_mux_np); in cpts_of_mux_clk_setup()
862 parent_names = devm_kcalloc(cpts->dev, sizeof(char *), num_parents, in cpts_of_mux_clk_setup()
865 ret = -ENOMEM; in cpts_of_mux_clk_setup()
869 of_clk_parent_fill(cpts->clk_mux_np, parent_names, num_parents); in cpts_of_mux_clk_setup()
871 clk_mux_name = devm_kasprintf(cpts->dev, GFP_KERNEL, "%s.%pOFn", in cpts_of_mux_clk_setup()
872 dev_name(cpts->dev), cpts->clk_mux_np); in cpts_of_mux_clk_setup()
874 ret = -ENOMEM; in cpts_of_mux_clk_setup()
878 reg = &cpts->reg->rftclk_sel; in cpts_of_mux_clk_setup()
882 cpts->clk_mux_hw = clk_hw_register_mux(NULL, clk_mux_name, in cpts_of_mux_clk_setup()
885 if (IS_ERR(cpts->clk_mux_hw)) { in cpts_of_mux_clk_setup()
886 ret = PTR_ERR(cpts->clk_mux_hw); in cpts_of_mux_clk_setup()
890 ret = of_clk_add_hw_provider(cpts->clk_mux_np, of_clk_hw_simple_get, in cpts_of_mux_clk_setup()
891 cpts->clk_mux_hw); in cpts_of_mux_clk_setup()
895 ret = devm_add_action_or_reset(cpts->dev, cpts_free_clk_mux, cpts); in cpts_of_mux_clk_setup()
897 dev_err(cpts->dev, "failed to add clkmux reset action %d", ret); in cpts_of_mux_clk_setup()
902 clk_hw_unregister_mux(cpts->clk_mux_hw); in cpts_of_mux_clk_setup()
904 of_node_put(cpts->clk_mux_np); in cpts_of_mux_clk_setup()
912 if (!of_property_read_u32(node, "ti,cpts-ext-ts-inputs", &prop[0])) in am65_cpts_of_parse()
913 cpts->ext_ts_inputs = prop[0]; in am65_cpts_of_parse()
915 if (!of_property_read_u32(node, "ti,cpts-periodic-outputs", &prop[0])) in am65_cpts_of_parse()
916 cpts->genf_num = prop[0]; in am65_cpts_of_parse()
925 ptp_clock_unregister(cpts->ptp_clock); in am65_cpts_release()
927 clk_disable_unprepare(cpts->refclk); in am65_cpts_release()
938 return ERR_PTR(-ENOMEM); in am65_cpts_create()
940 cpts->dev = dev; in am65_cpts_create()
941 cpts->reg = (struct am65_cpts_regs __iomem *)regs; in am65_cpts_create()
943 cpts->irq = of_irq_get_byname(node, "cpts"); in am65_cpts_create()
944 if (cpts->irq <= 0) { in am65_cpts_create()
945 ret = cpts->irq ?: -ENXIO; in am65_cpts_create()
946 if (ret != -EPROBE_DEFER) in am65_cpts_create()
956 mutex_init(&cpts->ptp_clk_lock); in am65_cpts_create()
957 INIT_LIST_HEAD(&cpts->events); in am65_cpts_create()
958 INIT_LIST_HEAD(&cpts->pool); in am65_cpts_create()
959 spin_lock_init(&cpts->lock); in am65_cpts_create()
960 skb_queue_head_init(&cpts->txq); in am65_cpts_create()
963 list_add(&cpts->pool_data[i].list, &cpts->pool); in am65_cpts_create()
965 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts"); in am65_cpts_create()
966 if (IS_ERR(cpts->refclk)) { in am65_cpts_create()
967 ret = PTR_ERR(cpts->refclk); in am65_cpts_create()
968 if (ret != -EPROBE_DEFER) in am65_cpts_create()
969 dev_err(dev, "Failed to get refclk %d\n", ret); in am65_cpts_create()
973 ret = clk_prepare_enable(cpts->refclk); in am65_cpts_create()
975 dev_err(dev, "Failed to enable refclk %d\n", ret); in am65_cpts_create()
979 cpts->refclk_freq = clk_get_rate(cpts->refclk); in am65_cpts_create()
981 am65_ptp_info.max_adj = cpts->refclk_freq / AM65_CPTS_MIN_PPM; in am65_cpts_create()
982 cpts->ptp_info = am65_ptp_info; in am65_cpts_create()
984 if (cpts->ext_ts_inputs) in am65_cpts_create()
985 cpts->ptp_info.n_ext_ts = cpts->ext_ts_inputs; in am65_cpts_create()
986 if (cpts->genf_num) in am65_cpts_create()
987 cpts->ptp_info.n_per_out = cpts->genf_num; in am65_cpts_create()
1000 cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev); in am65_cpts_create()
1001 if (IS_ERR_OR_NULL(cpts->ptp_clock)) { in am65_cpts_create()
1003 PTR_ERR(cpts->ptp_clock)); in am65_cpts_create()
1004 ret = cpts->ptp_clock ? PTR_ERR(cpts->ptp_clock) : -ENODEV; in am65_cpts_create()
1007 cpts->phc_index = ptp_clock_index(cpts->ptp_clock); in am65_cpts_create()
1015 ret = devm_request_threaded_irq(dev, cpts->irq, NULL, in am65_cpts_create()
1019 dev_err(cpts->dev, "error attaching irq %d\n", ret); in am65_cpts_create()
1025 cpts->refclk_freq, cpts->ts_add_val); in am65_cpts_create()
1030 clk_disable_unprepare(cpts->refclk); in am65_cpts_create()
1037 struct device_node *node = pdev->dev.of_node; in am65_cpts_probe()
1038 struct device *dev = &pdev->dev; in am65_cpts_probe()
1053 { .compatible = "ti,am65-cpts", },
1054 { .compatible = "ti,j721e-cpts", },
1062 .name = "am65-cpts",