Lines Matching refs:cpts
21 #include "cpts.h"
50 static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)
52 u32 r = cpts_read32(cpts, intstat_raw);
55 *high = cpts_read32(cpts, event_high);
56 *low = cpts_read32(cpts, event_low);
57 cpts_write32(cpts, EVENT_POP, event_pop);
63 static int cpts_purge_events(struct cpts *cpts)
69 list_for_each_safe(this, next, &cpts->events) {
73 list_add(&event->list, &cpts->pool);
79 dev_dbg(cpts->dev, "cpts: event pool cleaned up %d\n", removed);
83 static void cpts_purge_txq(struct cpts *cpts)
89 skb_queue_walk_safe(&cpts->txq, skb, tmp) {
92 __skb_unlink(skb, &cpts->txq);
99 dev_dbg(cpts->dev, "txq cleaned up %d\n", removed);
105 static int cpts_fifo_read(struct cpts *cpts, int match)
114 spin_lock_irqsave(&cpts->lock, flags);
117 if (cpts_fifo_pop(cpts, &hi, &lo))
120 if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) {
121 dev_warn(cpts->dev, "cpts: event pool empty\n");
125 event = list_first_entry(&cpts->pool, struct cpts_event, list);
128 event->timestamp = timecounter_cyc2time(&cpts->tc, event->low);
131 dev_dbg(cpts->dev, "CPTS_EV: %d high:%08X low:%08x\n",
135 WRITE_ONCE(cpts->cur_timestamp, lo);
136 timecounter_read(&cpts->tc);
137 if (cpts->mult_new) {
138 cpts->cc.mult = cpts->mult_new;
139 cpts->mult_new = 0;
141 if (!cpts->irq_poll)
142 complete(&cpts->ts_push_complete);
150 list_add_tail(&event->list, &cpts->events);
160 ptp_clock_event(cpts->clock, &pevent);
163 dev_err(cpts->dev, "cpts: unknown event type\n");
170 spin_unlock_irqrestore(&cpts->lock, flags);
172 if (!cpts->irq_poll && need_schedule)
173 ptp_schedule_worker(cpts->clock, 0);
178 void cpts_misc_interrupt(struct cpts *cpts)
180 cpts_fifo_read(cpts, -1);
186 struct cpts *cpts = container_of(cc, struct cpts, cc);
188 return READ_ONCE(cpts->cur_timestamp);
191 static void cpts_update_cur_time(struct cpts *cpts, int match,
196 reinit_completion(&cpts->ts_push_complete);
199 spin_lock_irqsave(&cpts->lock, flags);
201 cpts_write32(cpts, TS_PUSH, ts_push);
202 cpts_read32(cpts, ts_push);
204 spin_unlock_irqrestore(&cpts->lock, flags);
206 if (cpts->irq_poll && cpts_fifo_read(cpts, match) && match != -1)
207 dev_err(cpts->dev, "cpts: unable to obtain a time stamp\n");
209 if (!cpts->irq_poll &&
210 !wait_for_completion_timeout(&cpts->ts_push_complete, HZ))
211 dev_err(cpts->dev, "cpts: obtain a time stamp timeout\n");
218 struct cpts *cpts = container_of(ptp, struct cpts, info);
227 mult = cpts->cc_mult;
232 mutex_lock(&cpts->ptp_clk_mutex);
234 cpts->mult_new = neg_adj ? mult - diff : mult + diff;
236 cpts_update_cur_time(cpts, CPTS_EV_PUSH, NULL);
238 mutex_unlock(&cpts->ptp_clk_mutex);
244 struct cpts *cpts = container_of(ptp, struct cpts, info);
246 mutex_lock(&cpts->ptp_clk_mutex);
247 timecounter_adjtime(&cpts->tc, delta);
248 mutex_unlock(&cpts->ptp_clk_mutex);
257 struct cpts *cpts = container_of(ptp, struct cpts, info);
260 mutex_lock(&cpts->ptp_clk_mutex);
262 cpts_update_cur_time(cpts, CPTS_EV_PUSH, sts);
264 ns = timecounter_read(&cpts->tc);
265 mutex_unlock(&cpts->ptp_clk_mutex);
275 struct cpts *cpts = container_of(ptp, struct cpts, info);
280 mutex_lock(&cpts->ptp_clk_mutex);
281 timecounter_init(&cpts->tc, &cpts->cc, ns);
282 mutex_unlock(&cpts->ptp_clk_mutex);
287 static int cpts_extts_enable(struct cpts *cpts, u32 index, int on)
291 if (((cpts->hw_ts_enable & BIT(index)) >> index) == on)
294 mutex_lock(&cpts->ptp_clk_mutex);
296 v = cpts_read32(cpts, control);
299 cpts->hw_ts_enable |= BIT(index);
302 cpts->hw_ts_enable &= ~BIT(index);
304 cpts_write32(cpts, v, control);
306 mutex_unlock(&cpts->ptp_clk_mutex);
314 struct cpts *cpts = container_of(ptp, struct cpts, info);
318 return cpts_extts_enable(cpts, rq->extts.index, on);
326 static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event)
341 spin_lock_irqsave(&cpts->txq.lock, flags);
342 skb_queue_splice_init(&cpts->txq, &txq_list);
343 spin_unlock_irqrestore(&cpts->txq.lock, flags);
357 dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n",
364 dev_dbg(cpts->dev, "expiring tx timestamp from txq\n");
370 spin_lock_irqsave(&cpts->txq.lock, flags);
371 skb_queue_splice(&txq_list, &cpts->txq);
372 spin_unlock_irqrestore(&cpts->txq.lock, flags);
377 static void cpts_process_events(struct cpts *cpts)
385 spin_lock_irqsave(&cpts->lock, flags);
386 list_splice_init(&cpts->events, &events);
387 spin_unlock_irqrestore(&cpts->lock, flags);
391 if (cpts_match_tx_ts(cpts, event) ||
398 spin_lock_irqsave(&cpts->lock, flags);
399 list_splice_tail(&events, &cpts->events);
400 list_splice_tail(&events_free, &cpts->pool);
401 spin_unlock_irqrestore(&cpts->lock, flags);
406 struct cpts *cpts = container_of(ptp, struct cpts, info);
407 unsigned long delay = cpts->ov_check_period;
411 mutex_lock(&cpts->ptp_clk_mutex);
413 cpts_update_cur_time(cpts, -1, NULL);
414 ns = timecounter_read(&cpts->tc);
416 cpts_process_events(cpts);
418 spin_lock_irqsave(&cpts->txq.lock, flags);
419 if (!skb_queue_empty(&cpts->txq)) {
420 cpts_purge_txq(cpts);
421 if (!skb_queue_empty(&cpts->txq))
424 spin_unlock_irqrestore(&cpts->txq.lock, flags);
426 dev_dbg(cpts->dev, "cpts overflow check at %lld\n", ns);
427 mutex_unlock(&cpts->ptp_clk_mutex);
469 static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb,
478 cpts_fifo_read(cpts, -1);
479 spin_lock_irqsave(&cpts->lock, flags);
480 list_for_each_safe(this, next, &cpts->events) {
484 list_add(&event->list, &cpts->pool);
496 list_add(&event->list, &cpts->pool);
500 spin_unlock_irqrestore(&cpts->lock, flags);
505 void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
523 dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
526 ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid);
535 void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
549 dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
556 skb_queue_tail(&cpts->txq, skb);
557 ptp_schedule_worker(cpts->clock, 0);
561 int cpts_register(struct cpts *cpts)
565 skb_queue_head_init(&cpts->txq);
566 INIT_LIST_HEAD(&cpts->events);
567 INIT_LIST_HEAD(&cpts->pool);
569 list_add(&cpts->pool_data[i].list, &cpts->pool);
571 err = clk_enable(cpts->refclk);
575 cpts_write32(cpts, CPTS_EN, control);
576 cpts_write32(cpts, TS_PEND_EN, int_enable);
578 timecounter_init(&cpts->tc, &cpts->cc, ktime_get_real_ns());
580 cpts->clock = ptp_clock_register(&cpts->info, cpts->dev);
581 if (IS_ERR(cpts->clock)) {
582 err = PTR_ERR(cpts->clock);
583 cpts->clock = NULL;
586 cpts->phc_index = ptp_clock_index(cpts->clock);
588 ptp_schedule_worker(cpts->clock, cpts->ov_check_period);
592 clk_disable(cpts->refclk);
597 void cpts_unregister(struct cpts *cpts)
599 if (WARN_ON(!cpts->clock))
602 ptp_clock_unregister(cpts->clock);
603 cpts->clock = NULL;
604 cpts->phc_index = -1;
606 cpts_write32(cpts, 0, int_enable);
607 cpts_write32(cpts, 0, control);
610 skb_queue_purge(&cpts->txq);
612 clk_disable(cpts->refclk);
616 static void cpts_calc_mult_shift(struct cpts *cpts)
621 freq = clk_get_rate(cpts->refclk);
626 maxsec = cpts->cc.mask;
635 cpts->ov_check_period = (HZ * maxsec) / 2;
636 dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n",
637 cpts->ov_check_period);
639 if (cpts->cc.mult || cpts->cc.shift)
642 clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift,
646 ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac);
648 dev_info(cpts->dev,
650 freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC));
653 static int cpts_of_mux_clk_setup(struct cpts *cpts, struct device_node *node)
662 refclk_np = of_get_child_by_name(node, "cpts-refclk-mux");
669 dev_err(cpts->dev, "mux-clock %s must have parents\n",
674 parent_names = devm_kzalloc(cpts->dev, (sizeof(char *) * num_parents),
677 mux_table = devm_kzalloc(cpts->dev, sizeof(*mux_table) * num_parents,
692 clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name,
695 &cpts->reg->rftclk_sel, 0, 0x1F,
702 ret = devm_add_action_or_reset(cpts->dev,
706 dev_err(cpts->dev, "add clkmux unreg action %d", ret);
714 ret = devm_add_action_or_reset(cpts->dev,
718 dev_err(cpts->dev, "add clkmux provider unreg action %d", ret);
729 static int cpts_of_parse(struct cpts *cpts, struct device_node *node)
735 cpts->cc.mult = prop;
738 cpts->cc.shift = prop;
740 if ((cpts->cc.mult && !cpts->cc.shift) ||
741 (!cpts->cc.mult && cpts->cc.shift))
744 return cpts_of_mux_clk_setup(cpts, node);
747 dev_err(cpts->dev, "CPTS: Missing property in the DT.\n");
751 struct cpts *cpts_create(struct device *dev, void __iomem *regs,
754 struct cpts *cpts;
757 cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
758 if (!cpts)
761 cpts->dev = dev;
762 cpts->reg = (struct cpsw_cpts __iomem *)regs;
763 cpts->irq_poll = true;
764 spin_lock_init(&cpts->lock);
765 mutex_init(&cpts->ptp_clk_mutex);
766 init_completion(&cpts->ts_push_complete);
768 ret = cpts_of_parse(cpts, node);
772 cpts->refclk = devm_get_clk_from_child(dev, node, "cpts");
773 if (IS_ERR(cpts->refclk))
775 cpts->refclk = devm_clk_get(dev, "cpts");
777 if (IS_ERR(cpts->refclk)) {
778 dev_err(dev, "Failed to get cpts refclk %ld\n",
779 PTR_ERR(cpts->refclk));
780 return ERR_CAST(cpts->refclk);
783 ret = clk_prepare(cpts->refclk);
787 cpts->cc.read = cpts_systim_read;
788 cpts->cc.mask = CLOCKSOURCE_MASK(32);
789 cpts->info = cpts_info;
790 cpts->phc_index = -1;
793 cpts->info.n_ext_ts = n_ext_ts;
795 cpts_calc_mult_shift(cpts);
799 cpts->cc_mult = cpts->cc.mult;
801 return cpts;
805 void cpts_release(struct cpts *cpts)
807 if (!cpts)
810 if (WARN_ON(!cpts->refclk))
813 clk_unprepare(cpts->refclk);