xref: /openbmc/linux/drivers/net/ethernet/ti/cpts.c (revision c514fbb6231483b05c97eb22587188d4c453b28e)
168cf027fSGrygorii Strashko // SPDX-License-Identifier: GPL-2.0+
287c0e764SRichard Cochran /*
387c0e764SRichard Cochran  * TI Common Platform Time Sync
487c0e764SRichard Cochran  *
587c0e764SRichard Cochran  * Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
687c0e764SRichard Cochran  *
787c0e764SRichard Cochran  */
8a3047a81SGrygorii Strashko #include <linux/clk-provider.h>
987c0e764SRichard Cochran #include <linux/err.h>
1087c0e764SRichard Cochran #include <linux/if.h>
1187c0e764SRichard Cochran #include <linux/hrtimer.h>
1287c0e764SRichard Cochran #include <linux/module.h>
1387c0e764SRichard Cochran #include <linux/net_tstamp.h>
1487c0e764SRichard Cochran #include <linux/ptp_classify.h>
1587c0e764SRichard Cochran #include <linux/time.h>
1687c0e764SRichard Cochran #include <linux/uaccess.h>
1787c0e764SRichard Cochran #include <linux/workqueue.h>
1879eb9d28SAlexei Starovoitov #include <linux/if_ether.h>
1979eb9d28SAlexei Starovoitov #include <linux/if_vlan.h>
2087c0e764SRichard Cochran 
2187c0e764SRichard Cochran #include "cpts.h"
2287c0e764SRichard Cochran 
230d5f54feSGrygorii Strashko #define CPTS_SKB_TX_WORK_TIMEOUT 1 /* jiffies */
24c8f8e47eSGrygorii Strashko #define CPTS_SKB_RX_TX_TMO 100 /*ms */
25c8f8e47eSGrygorii Strashko #define CPTS_EVENT_RX_TX_TIMEOUT (100) /* ms */
260d5f54feSGrygorii Strashko 
270d5f54feSGrygorii Strashko struct cpts_skb_cb_data {
283bfd41b5SGrygorii Strashko 	u32 skb_mtype_seqid;
290d5f54feSGrygorii Strashko 	unsigned long tmo;
300d5f54feSGrygorii Strashko };
310d5f54feSGrygorii Strashko 
32391fd6caSGrygorii Strashko #define cpts_read32(c, r)	readl_relaxed(&c->reg->r)
33391fd6caSGrygorii Strashko #define cpts_write32(c, v, r)	writel_relaxed(v, &c->reg->r)
3487c0e764SRichard Cochran 
35b78aba49SGrygorii Strashko static int cpts_event_port(struct cpts_event *event)
36b78aba49SGrygorii Strashko {
37b78aba49SGrygorii Strashko 	return (event->high >> PORT_NUMBER_SHIFT) & PORT_NUMBER_MASK;
38b78aba49SGrygorii Strashko }
39b78aba49SGrygorii Strashko 
4087c0e764SRichard Cochran static int event_expired(struct cpts_event *event)
4187c0e764SRichard Cochran {
4287c0e764SRichard Cochran 	return time_after(jiffies, event->tmo);
4387c0e764SRichard Cochran }
4487c0e764SRichard Cochran 
4587c0e764SRichard Cochran static int event_type(struct cpts_event *event)
4687c0e764SRichard Cochran {
4787c0e764SRichard Cochran 	return (event->high >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
4887c0e764SRichard Cochran }
4987c0e764SRichard Cochran 
5087c0e764SRichard Cochran static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)
5187c0e764SRichard Cochran {
5287c0e764SRichard Cochran 	u32 r = cpts_read32(cpts, intstat_raw);
5387c0e764SRichard Cochran 
5487c0e764SRichard Cochran 	if (r & TS_PEND_RAW) {
5587c0e764SRichard Cochran 		*high = cpts_read32(cpts, event_high);
5687c0e764SRichard Cochran 		*low  = cpts_read32(cpts, event_low);
5787c0e764SRichard Cochran 		cpts_write32(cpts, EVENT_POP, event_pop);
5887c0e764SRichard Cochran 		return 0;
5987c0e764SRichard Cochran 	}
6087c0e764SRichard Cochran 	return -1;
6187c0e764SRichard Cochran }
6287c0e764SRichard Cochran 
63e4439fa8SWingMan Kwok static int cpts_purge_events(struct cpts *cpts)
64e4439fa8SWingMan Kwok {
65e4439fa8SWingMan Kwok 	struct list_head *this, *next;
66e4439fa8SWingMan Kwok 	struct cpts_event *event;
67e4439fa8SWingMan Kwok 	int removed = 0;
68e4439fa8SWingMan Kwok 
69e4439fa8SWingMan Kwok 	list_for_each_safe(this, next, &cpts->events) {
70e4439fa8SWingMan Kwok 		event = list_entry(this, struct cpts_event, list);
71e4439fa8SWingMan Kwok 		if (event_expired(event)) {
72e4439fa8SWingMan Kwok 			list_del_init(&event->list);
73e4439fa8SWingMan Kwok 			list_add(&event->list, &cpts->pool);
74e4439fa8SWingMan Kwok 			++removed;
75e4439fa8SWingMan Kwok 		}
76e4439fa8SWingMan Kwok 	}
77e4439fa8SWingMan Kwok 
78e4439fa8SWingMan Kwok 	if (removed)
7979d6e755SGrygorii Strashko 		dev_dbg(cpts->dev, "cpts: event pool cleaned up %d\n", removed);
80e4439fa8SWingMan Kwok 	return removed ? 0 : -1;
81e4439fa8SWingMan Kwok }
82e4439fa8SWingMan Kwok 
83f19dcd5fSIvan Khoronzhuk static void cpts_purge_txq(struct cpts *cpts)
84f19dcd5fSIvan Khoronzhuk {
85f19dcd5fSIvan Khoronzhuk 	struct cpts_skb_cb_data *skb_cb;
86f19dcd5fSIvan Khoronzhuk 	struct sk_buff *skb, *tmp;
87f19dcd5fSIvan Khoronzhuk 	int removed = 0;
88f19dcd5fSIvan Khoronzhuk 
89f19dcd5fSIvan Khoronzhuk 	skb_queue_walk_safe(&cpts->txq, skb, tmp) {
90f19dcd5fSIvan Khoronzhuk 		skb_cb = (struct cpts_skb_cb_data *)skb->cb;
91f19dcd5fSIvan Khoronzhuk 		if (time_after(jiffies, skb_cb->tmo)) {
92f19dcd5fSIvan Khoronzhuk 			__skb_unlink(skb, &cpts->txq);
93f19dcd5fSIvan Khoronzhuk 			dev_consume_skb_any(skb);
94f19dcd5fSIvan Khoronzhuk 			++removed;
95f19dcd5fSIvan Khoronzhuk 		}
96f19dcd5fSIvan Khoronzhuk 	}
97f19dcd5fSIvan Khoronzhuk 
98f19dcd5fSIvan Khoronzhuk 	if (removed)
99f19dcd5fSIvan Khoronzhuk 		dev_dbg(cpts->dev, "txq cleaned up %d\n", removed);
100f19dcd5fSIvan Khoronzhuk }
101f19dcd5fSIvan Khoronzhuk 
10287c0e764SRichard Cochran /*
10387c0e764SRichard Cochran  * Returns zero if matching event type was found.
10487c0e764SRichard Cochran  */
10587c0e764SRichard Cochran static int cpts_fifo_read(struct cpts *cpts, int match)
10687c0e764SRichard Cochran {
107b78aba49SGrygorii Strashko 	struct ptp_clock_event pevent;
10885624412SGrygorii Strashko 	bool need_schedule = false;
109ba107428SGrygorii Strashko 	struct cpts_event *event;
110ba107428SGrygorii Strashko 	unsigned long flags;
11187c0e764SRichard Cochran 	int i, type = -1;
11287c0e764SRichard Cochran 	u32 hi, lo;
113ba107428SGrygorii Strashko 
114ba107428SGrygorii Strashko 	spin_lock_irqsave(&cpts->lock, flags);
11587c0e764SRichard Cochran 
11687c0e764SRichard Cochran 	for (i = 0; i < CPTS_FIFO_DEPTH; i++) {
11787c0e764SRichard Cochran 		if (cpts_fifo_pop(cpts, &hi, &lo))
11887c0e764SRichard Cochran 			break;
119e4439fa8SWingMan Kwok 
120e4439fa8SWingMan Kwok 		if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) {
12179d6e755SGrygorii Strashko 			dev_warn(cpts->dev, "cpts: event pool empty\n");
122ba107428SGrygorii Strashko 			break;
12387c0e764SRichard Cochran 		}
124e4439fa8SWingMan Kwok 
12587c0e764SRichard Cochran 		event = list_first_entry(&cpts->pool, struct cpts_event, list);
12687c0e764SRichard Cochran 		event->high = hi;
12787c0e764SRichard Cochran 		event->low = lo;
128e66dccceSGrygorii Strashko 		event->timestamp = timecounter_cyc2time(&cpts->tc, event->low);
12987c0e764SRichard Cochran 		type = event_type(event);
130e66dccceSGrygorii Strashko 
131e66dccceSGrygorii Strashko 		dev_dbg(cpts->dev, "CPTS_EV: %d high:%08X low:%08x\n",
132e66dccceSGrygorii Strashko 			type, event->high, event->low);
13387c0e764SRichard Cochran 		switch (type) {
134e66dccceSGrygorii Strashko 		case CPTS_EV_PUSH:
135e66dccceSGrygorii Strashko 			WRITE_ONCE(cpts->cur_timestamp, lo);
136e66dccceSGrygorii Strashko 			timecounter_read(&cpts->tc);
1370d6df3e6SGrygorii Strashko 			if (cpts->mult_new) {
1380d6df3e6SGrygorii Strashko 				cpts->cc.mult = cpts->mult_new;
1390d6df3e6SGrygorii Strashko 				cpts->mult_new = 0;
1400d6df3e6SGrygorii Strashko 			}
14185624412SGrygorii Strashko 			if (!cpts->irq_poll)
14285624412SGrygorii Strashko 				complete(&cpts->ts_push_complete);
143e66dccceSGrygorii Strashko 			break;
1440d5f54feSGrygorii Strashko 		case CPTS_EV_TX:
14587c0e764SRichard Cochran 		case CPTS_EV_RX:
146c8f8e47eSGrygorii Strashko 			event->tmo = jiffies +
147c8f8e47eSGrygorii Strashko 				msecs_to_jiffies(CPTS_EVENT_RX_TX_TIMEOUT);
148c8f8e47eSGrygorii Strashko 
14987c0e764SRichard Cochran 			list_del_init(&event->list);
15087c0e764SRichard Cochran 			list_add_tail(&event->list, &cpts->events);
15185624412SGrygorii Strashko 			need_schedule = true;
15287c0e764SRichard Cochran 			break;
15387c0e764SRichard Cochran 		case CPTS_EV_ROLL:
15487c0e764SRichard Cochran 		case CPTS_EV_HALF:
155b78aba49SGrygorii Strashko 			break;
15687c0e764SRichard Cochran 		case CPTS_EV_HW:
157b78aba49SGrygorii Strashko 			pevent.timestamp = event->timestamp;
158b78aba49SGrygorii Strashko 			pevent.type = PTP_CLOCK_EXTTS;
159b78aba49SGrygorii Strashko 			pevent.index = cpts_event_port(event) - 1;
160b78aba49SGrygorii Strashko 			ptp_clock_event(cpts->clock, &pevent);
16187c0e764SRichard Cochran 			break;
16287c0e764SRichard Cochran 		default:
16379d6e755SGrygorii Strashko 			dev_err(cpts->dev, "cpts: unknown event type\n");
16487c0e764SRichard Cochran 			break;
16587c0e764SRichard Cochran 		}
16687c0e764SRichard Cochran 		if (type == match)
16787c0e764SRichard Cochran 			break;
16887c0e764SRichard Cochran 	}
169ba107428SGrygorii Strashko 
170ba107428SGrygorii Strashko 	spin_unlock_irqrestore(&cpts->lock, flags);
171ba107428SGrygorii Strashko 
17285624412SGrygorii Strashko 	if (!cpts->irq_poll && need_schedule)
17385624412SGrygorii Strashko 		ptp_schedule_worker(cpts->clock, 0);
17485624412SGrygorii Strashko 
17587c0e764SRichard Cochran 	return type == match ? 0 : -1;
17687c0e764SRichard Cochran }
17787c0e764SRichard Cochran 
17885624412SGrygorii Strashko void cpts_misc_interrupt(struct cpts *cpts)
17985624412SGrygorii Strashko {
18085624412SGrygorii Strashko 	cpts_fifo_read(cpts, -1);
18185624412SGrygorii Strashko }
18285624412SGrygorii Strashko EXPORT_SYMBOL_GPL(cpts_misc_interrupt);
18385624412SGrygorii Strashko 
184a5a1d1c2SThomas Gleixner static u64 cpts_systim_read(const struct cyclecounter *cc)
18587c0e764SRichard Cochran {
18687c0e764SRichard Cochran 	struct cpts *cpts = container_of(cc, struct cpts, cc);
18787c0e764SRichard Cochran 
188e66dccceSGrygorii Strashko 	return READ_ONCE(cpts->cur_timestamp);
189e66dccceSGrygorii Strashko }
190e66dccceSGrygorii Strashko 
191856e59abSGrygorii Strashko static void cpts_update_cur_time(struct cpts *cpts, int match,
192856e59abSGrygorii Strashko 				 struct ptp_system_timestamp *sts)
193e66dccceSGrygorii Strashko {
194ba107428SGrygorii Strashko 	unsigned long flags;
195ba107428SGrygorii Strashko 
19685624412SGrygorii Strashko 	reinit_completion(&cpts->ts_push_complete);
19785624412SGrygorii Strashko 
198ba107428SGrygorii Strashko 	/* use spin_lock_irqsave() here as it has to run very fast */
199ba107428SGrygorii Strashko 	spin_lock_irqsave(&cpts->lock, flags);
200856e59abSGrygorii Strashko 	ptp_read_system_prets(sts);
20187c0e764SRichard Cochran 	cpts_write32(cpts, TS_PUSH, ts_push);
202856e59abSGrygorii Strashko 	cpts_read32(cpts, ts_push);
203856e59abSGrygorii Strashko 	ptp_read_system_postts(sts);
204ba107428SGrygorii Strashko 	spin_unlock_irqrestore(&cpts->lock, flags);
205e66dccceSGrygorii Strashko 
20685624412SGrygorii Strashko 	if (cpts->irq_poll && cpts_fifo_read(cpts, match) && match != -1)
20779d6e755SGrygorii Strashko 		dev_err(cpts->dev, "cpts: unable to obtain a time stamp\n");
20885624412SGrygorii Strashko 
20985624412SGrygorii Strashko 	if (!cpts->irq_poll &&
21085624412SGrygorii Strashko 	    !wait_for_completion_timeout(&cpts->ts_push_complete, HZ))
21185624412SGrygorii Strashko 		dev_err(cpts->dev, "cpts: obtain a time stamp timeout\n");
21287c0e764SRichard Cochran }
21387c0e764SRichard Cochran 
21487c0e764SRichard Cochran /* PTP clock operations */
21587c0e764SRichard Cochran 
21687c0e764SRichard Cochran static int cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
21787c0e764SRichard Cochran {
21887c0e764SRichard Cochran 	struct cpts *cpts = container_of(ptp, struct cpts, info);
219ba107428SGrygorii Strashko 	int neg_adj = 0;
220ba107428SGrygorii Strashko 	u32 diff, mult;
221ba107428SGrygorii Strashko 	u64 adj;
22287c0e764SRichard Cochran 
22387c0e764SRichard Cochran 	if (ppb < 0) {
22487c0e764SRichard Cochran 		neg_adj = 1;
22587c0e764SRichard Cochran 		ppb = -ppb;
22687c0e764SRichard Cochran 	}
22787c0e764SRichard Cochran 	mult = cpts->cc_mult;
22887c0e764SRichard Cochran 	adj = mult;
22987c0e764SRichard Cochran 	adj *= ppb;
23087c0e764SRichard Cochran 	diff = div_u64(adj, 1000000000ULL);
23187c0e764SRichard Cochran 
232ba107428SGrygorii Strashko 	mutex_lock(&cpts->ptp_clk_mutex);
23387c0e764SRichard Cochran 
2340d6df3e6SGrygorii Strashko 	cpts->mult_new = neg_adj ? mult - diff : mult + diff;
23587c0e764SRichard Cochran 
236856e59abSGrygorii Strashko 	cpts_update_cur_time(cpts, CPTS_EV_PUSH, NULL);
23787c0e764SRichard Cochran 
238ba107428SGrygorii Strashko 	mutex_unlock(&cpts->ptp_clk_mutex);
23987c0e764SRichard Cochran 	return 0;
24087c0e764SRichard Cochran }
24187c0e764SRichard Cochran 
24287c0e764SRichard Cochran static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
24387c0e764SRichard Cochran {
24487c0e764SRichard Cochran 	struct cpts *cpts = container_of(ptp, struct cpts, info);
24587c0e764SRichard Cochran 
246ba107428SGrygorii Strashko 	mutex_lock(&cpts->ptp_clk_mutex);
247f25a30beSRichard Cochran 	timecounter_adjtime(&cpts->tc, delta);
248ba107428SGrygorii Strashko 	mutex_unlock(&cpts->ptp_clk_mutex);
24987c0e764SRichard Cochran 
25087c0e764SRichard Cochran 	return 0;
25187c0e764SRichard Cochran }
25287c0e764SRichard Cochran 
253856e59abSGrygorii Strashko static int cpts_ptp_gettimeex(struct ptp_clock_info *ptp,
254856e59abSGrygorii Strashko 			      struct timespec64 *ts,
255856e59abSGrygorii Strashko 			      struct ptp_system_timestamp *sts)
25687c0e764SRichard Cochran {
25787c0e764SRichard Cochran 	struct cpts *cpts = container_of(ptp, struct cpts, info);
258856e59abSGrygorii Strashko 	u64 ns;
25987c0e764SRichard Cochran 
260ba107428SGrygorii Strashko 	mutex_lock(&cpts->ptp_clk_mutex);
261e66dccceSGrygorii Strashko 
262856e59abSGrygorii Strashko 	cpts_update_cur_time(cpts, CPTS_EV_PUSH, sts);
263e66dccceSGrygorii Strashko 
26487c0e764SRichard Cochran 	ns = timecounter_read(&cpts->tc);
265ba107428SGrygorii Strashko 	mutex_unlock(&cpts->ptp_clk_mutex);
26687c0e764SRichard Cochran 
26784d923ceSRichard Cochran 	*ts = ns_to_timespec64(ns);
26887c0e764SRichard Cochran 
26987c0e764SRichard Cochran 	return 0;
27087c0e764SRichard Cochran }
27187c0e764SRichard Cochran 
27287c0e764SRichard Cochran static int cpts_ptp_settime(struct ptp_clock_info *ptp,
273a5c79c26SRichard Cochran 			    const struct timespec64 *ts)
27487c0e764SRichard Cochran {
27587c0e764SRichard Cochran 	struct cpts *cpts = container_of(ptp, struct cpts, info);
276ba107428SGrygorii Strashko 	u64 ns;
27787c0e764SRichard Cochran 
27884d923ceSRichard Cochran 	ns = timespec64_to_ns(ts);
27987c0e764SRichard Cochran 
280ba107428SGrygorii Strashko 	mutex_lock(&cpts->ptp_clk_mutex);
28187c0e764SRichard Cochran 	timecounter_init(&cpts->tc, &cpts->cc, ns);
282ba107428SGrygorii Strashko 	mutex_unlock(&cpts->ptp_clk_mutex);
28387c0e764SRichard Cochran 
28487c0e764SRichard Cochran 	return 0;
28587c0e764SRichard Cochran }
28687c0e764SRichard Cochran 
287b78aba49SGrygorii Strashko static int cpts_extts_enable(struct cpts *cpts, u32 index, int on)
288b78aba49SGrygorii Strashko {
289b78aba49SGrygorii Strashko 	u32 v;
290b78aba49SGrygorii Strashko 
291b78aba49SGrygorii Strashko 	if (((cpts->hw_ts_enable & BIT(index)) >> index) == on)
292b78aba49SGrygorii Strashko 		return 0;
293b78aba49SGrygorii Strashko 
294b78aba49SGrygorii Strashko 	mutex_lock(&cpts->ptp_clk_mutex);
295b78aba49SGrygorii Strashko 
296b78aba49SGrygorii Strashko 	v = cpts_read32(cpts, control);
297b78aba49SGrygorii Strashko 	if (on) {
298b78aba49SGrygorii Strashko 		v |= BIT(8 + index);
299b78aba49SGrygorii Strashko 		cpts->hw_ts_enable |= BIT(index);
300b78aba49SGrygorii Strashko 	} else {
301b78aba49SGrygorii Strashko 		v &= ~BIT(8 + index);
302b78aba49SGrygorii Strashko 		cpts->hw_ts_enable &= ~BIT(index);
303b78aba49SGrygorii Strashko 	}
304b78aba49SGrygorii Strashko 	cpts_write32(cpts, v, control);
305b78aba49SGrygorii Strashko 
306b78aba49SGrygorii Strashko 	mutex_unlock(&cpts->ptp_clk_mutex);
307b78aba49SGrygorii Strashko 
308b78aba49SGrygorii Strashko 	return 0;
309b78aba49SGrygorii Strashko }
310b78aba49SGrygorii Strashko 
31187c0e764SRichard Cochran static int cpts_ptp_enable(struct ptp_clock_info *ptp,
31287c0e764SRichard Cochran 			   struct ptp_clock_request *rq, int on)
31387c0e764SRichard Cochran {
314b78aba49SGrygorii Strashko 	struct cpts *cpts = container_of(ptp, struct cpts, info);
315b78aba49SGrygorii Strashko 
316b78aba49SGrygorii Strashko 	switch (rq->type) {
317b78aba49SGrygorii Strashko 	case PTP_CLK_REQ_EXTTS:
318b78aba49SGrygorii Strashko 		return cpts_extts_enable(cpts, rq->extts.index, on);
319b78aba49SGrygorii Strashko 	default:
320b78aba49SGrygorii Strashko 		break;
321b78aba49SGrygorii Strashko 	}
322b78aba49SGrygorii Strashko 
32387c0e764SRichard Cochran 	return -EOPNOTSUPP;
32487c0e764SRichard Cochran }
32587c0e764SRichard Cochran 
326c8f8e47eSGrygorii Strashko static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event)
327c8f8e47eSGrygorii Strashko {
328c8f8e47eSGrygorii Strashko 	struct sk_buff_head txq_list;
329c8f8e47eSGrygorii Strashko 	struct sk_buff *skb, *tmp;
330c8f8e47eSGrygorii Strashko 	unsigned long flags;
331c8f8e47eSGrygorii Strashko 	bool found = false;
332c8f8e47eSGrygorii Strashko 	u32 mtype_seqid;
333c8f8e47eSGrygorii Strashko 
334c8f8e47eSGrygorii Strashko 	mtype_seqid = event->high &
335c8f8e47eSGrygorii Strashko 		      ((MESSAGE_TYPE_MASK << MESSAGE_TYPE_SHIFT) |
336c8f8e47eSGrygorii Strashko 		       (SEQUENCE_ID_MASK << SEQUENCE_ID_SHIFT) |
337c8f8e47eSGrygorii Strashko 		       (EVENT_TYPE_MASK << EVENT_TYPE_SHIFT));
338c8f8e47eSGrygorii Strashko 
339c8f8e47eSGrygorii Strashko 	__skb_queue_head_init(&txq_list);
340c8f8e47eSGrygorii Strashko 
341c8f8e47eSGrygorii Strashko 	spin_lock_irqsave(&cpts->txq.lock, flags);
342c8f8e47eSGrygorii Strashko 	skb_queue_splice_init(&cpts->txq, &txq_list);
343c8f8e47eSGrygorii Strashko 	spin_unlock_irqrestore(&cpts->txq.lock, flags);
344c8f8e47eSGrygorii Strashko 
345c8f8e47eSGrygorii Strashko 	skb_queue_walk_safe(&txq_list, skb, tmp) {
346c8f8e47eSGrygorii Strashko 		struct skb_shared_hwtstamps ssh;
347c8f8e47eSGrygorii Strashko 		struct cpts_skb_cb_data *skb_cb =
348c8f8e47eSGrygorii Strashko 					(struct cpts_skb_cb_data *)skb->cb;
349c8f8e47eSGrygorii Strashko 
350c8f8e47eSGrygorii Strashko 		if (mtype_seqid == skb_cb->skb_mtype_seqid) {
351c8f8e47eSGrygorii Strashko 			memset(&ssh, 0, sizeof(ssh));
352c8f8e47eSGrygorii Strashko 			ssh.hwtstamp = ns_to_ktime(event->timestamp);
353c8f8e47eSGrygorii Strashko 			skb_tstamp_tx(skb, &ssh);
354c8f8e47eSGrygorii Strashko 			found = true;
355c8f8e47eSGrygorii Strashko 			__skb_unlink(skb, &txq_list);
356c8f8e47eSGrygorii Strashko 			dev_consume_skb_any(skb);
357c8f8e47eSGrygorii Strashko 			dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n",
358c8f8e47eSGrygorii Strashko 				mtype_seqid);
359c8f8e47eSGrygorii Strashko 			break;
360c8f8e47eSGrygorii Strashko 		}
361c8f8e47eSGrygorii Strashko 
362c8f8e47eSGrygorii Strashko 		if (time_after(jiffies, skb_cb->tmo)) {
363c8f8e47eSGrygorii Strashko 			/* timeout any expired skbs over 1s */
364c8f8e47eSGrygorii Strashko 			dev_dbg(cpts->dev, "expiring tx timestamp from txq\n");
365c8f8e47eSGrygorii Strashko 			__skb_unlink(skb, &txq_list);
366c8f8e47eSGrygorii Strashko 			dev_consume_skb_any(skb);
367c8f8e47eSGrygorii Strashko 		}
368c8f8e47eSGrygorii Strashko 	}
369c8f8e47eSGrygorii Strashko 
370c8f8e47eSGrygorii Strashko 	spin_lock_irqsave(&cpts->txq.lock, flags);
371c8f8e47eSGrygorii Strashko 	skb_queue_splice(&txq_list, &cpts->txq);
372c8f8e47eSGrygorii Strashko 	spin_unlock_irqrestore(&cpts->txq.lock, flags);
373c8f8e47eSGrygorii Strashko 
374c8f8e47eSGrygorii Strashko 	return found;
375c8f8e47eSGrygorii Strashko }
376c8f8e47eSGrygorii Strashko 
377c8f8e47eSGrygorii Strashko static void cpts_process_events(struct cpts *cpts)
378c8f8e47eSGrygorii Strashko {
379c8f8e47eSGrygorii Strashko 	struct list_head *this, *next;
380c8f8e47eSGrygorii Strashko 	struct cpts_event *event;
381c8f8e47eSGrygorii Strashko 	LIST_HEAD(events_free);
382c8f8e47eSGrygorii Strashko 	unsigned long flags;
383c8f8e47eSGrygorii Strashko 	LIST_HEAD(events);
384c8f8e47eSGrygorii Strashko 
385c8f8e47eSGrygorii Strashko 	spin_lock_irqsave(&cpts->lock, flags);
386c8f8e47eSGrygorii Strashko 	list_splice_init(&cpts->events, &events);
387c8f8e47eSGrygorii Strashko 	spin_unlock_irqrestore(&cpts->lock, flags);
388c8f8e47eSGrygorii Strashko 
389c8f8e47eSGrygorii Strashko 	list_for_each_safe(this, next, &events) {
390c8f8e47eSGrygorii Strashko 		event = list_entry(this, struct cpts_event, list);
391c8f8e47eSGrygorii Strashko 		if (cpts_match_tx_ts(cpts, event) ||
392c8f8e47eSGrygorii Strashko 		    time_after(jiffies, event->tmo)) {
393c8f8e47eSGrygorii Strashko 			list_del_init(&event->list);
394c8f8e47eSGrygorii Strashko 			list_add(&event->list, &events_free);
395c8f8e47eSGrygorii Strashko 		}
396c8f8e47eSGrygorii Strashko 	}
397c8f8e47eSGrygorii Strashko 
398c8f8e47eSGrygorii Strashko 	spin_lock_irqsave(&cpts->lock, flags);
399c8f8e47eSGrygorii Strashko 	list_splice_tail(&events, &cpts->events);
400c8f8e47eSGrygorii Strashko 	list_splice_tail(&events_free, &cpts->pool);
401c8f8e47eSGrygorii Strashko 	spin_unlock_irqrestore(&cpts->lock, flags);
402c8f8e47eSGrygorii Strashko }
403c8f8e47eSGrygorii Strashko 
404999f1292SGrygorii Strashko static long cpts_overflow_check(struct ptp_clock_info *ptp)
405999f1292SGrygorii Strashko {
406999f1292SGrygorii Strashko 	struct cpts *cpts = container_of(ptp, struct cpts, info);
407999f1292SGrygorii Strashko 	unsigned long delay = cpts->ov_check_period;
4080d5f54feSGrygorii Strashko 	unsigned long flags;
409e66dccceSGrygorii Strashko 	u64 ns;
410999f1292SGrygorii Strashko 
411ba107428SGrygorii Strashko 	mutex_lock(&cpts->ptp_clk_mutex);
412e66dccceSGrygorii Strashko 
413ba107428SGrygorii Strashko 	cpts_update_cur_time(cpts, -1, NULL);
414e66dccceSGrygorii Strashko 	ns = timecounter_read(&cpts->tc);
4150d5f54feSGrygorii Strashko 
416c8f8e47eSGrygorii Strashko 	cpts_process_events(cpts);
417c8f8e47eSGrygorii Strashko 
418c8f8e47eSGrygorii Strashko 	spin_lock_irqsave(&cpts->txq.lock, flags);
419f19dcd5fSIvan Khoronzhuk 	if (!skb_queue_empty(&cpts->txq)) {
420f19dcd5fSIvan Khoronzhuk 		cpts_purge_txq(cpts);
4210d5f54feSGrygorii Strashko 		if (!skb_queue_empty(&cpts->txq))
4220d5f54feSGrygorii Strashko 			delay = CPTS_SKB_TX_WORK_TIMEOUT;
423f19dcd5fSIvan Khoronzhuk 	}
424c8f8e47eSGrygorii Strashko 	spin_unlock_irqrestore(&cpts->txq.lock, flags);
4250d5f54feSGrygorii Strashko 
426e66dccceSGrygorii Strashko 	dev_dbg(cpts->dev, "cpts overflow check at %lld\n", ns);
427ba107428SGrygorii Strashko 	mutex_unlock(&cpts->ptp_clk_mutex);
428999f1292SGrygorii Strashko 	return (long)delay;
429999f1292SGrygorii Strashko }
430999f1292SGrygorii Strashko 
431b6d08bd8SBhumika Goyal static const struct ptp_clock_info cpts_info = {
43287c0e764SRichard Cochran 	.owner		= THIS_MODULE,
43387c0e764SRichard Cochran 	.name		= "CTPS timer",
43487c0e764SRichard Cochran 	.max_adj	= 1000000,
43587c0e764SRichard Cochran 	.n_ext_ts	= 0,
4364986b4f0SRichard Cochran 	.n_pins		= 0,
43787c0e764SRichard Cochran 	.pps		= 0,
43887c0e764SRichard Cochran 	.adjfreq	= cpts_ptp_adjfreq,
43987c0e764SRichard Cochran 	.adjtime	= cpts_ptp_adjtime,
440856e59abSGrygorii Strashko 	.gettimex64	= cpts_ptp_gettimeex,
441a5c79c26SRichard Cochran 	.settime64	= cpts_ptp_settime,
44287c0e764SRichard Cochran 	.enable		= cpts_ptp_enable,
443999f1292SGrygorii Strashko 	.do_aux_work	= cpts_overflow_check,
44487c0e764SRichard Cochran };
44587c0e764SRichard Cochran 
4463bfd41b5SGrygorii Strashko static int cpts_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid)
44787c0e764SRichard Cochran {
4483bfd41b5SGrygorii Strashko 	unsigned int ptp_class = ptp_classify_raw(skb);
44917de44c2SKurt Kanzenbach 	struct ptp_header *hdr;
45017de44c2SKurt Kanzenbach 	u8 msgtype;
45117de44c2SKurt Kanzenbach 	u16 seqid;
4523bfd41b5SGrygorii Strashko 
4533bfd41b5SGrygorii Strashko 	if (ptp_class == PTP_CLASS_NONE)
4543bfd41b5SGrygorii Strashko 		return 0;
45587c0e764SRichard Cochran 
45617de44c2SKurt Kanzenbach 	hdr = ptp_parse_header(skb, ptp_class);
45717de44c2SKurt Kanzenbach 	if (!hdr)
45887c0e764SRichard Cochran 		return 0;
45987c0e764SRichard Cochran 
46017de44c2SKurt Kanzenbach 	msgtype = ptp_get_msgtype(hdr, ptp_class);
46117de44c2SKurt Kanzenbach 	seqid	= ntohs(hdr->sequence_id);
46287c0e764SRichard Cochran 
46317de44c2SKurt Kanzenbach 	*mtype_seqid  = (msgtype & MESSAGE_TYPE_MASK) << MESSAGE_TYPE_SHIFT;
46417de44c2SKurt Kanzenbach 	*mtype_seqid |= (seqid & SEQUENCE_ID_MASK) << SEQUENCE_ID_SHIFT;
46587c0e764SRichard Cochran 
4663bfd41b5SGrygorii Strashko 	return 1;
46787c0e764SRichard Cochran }
46887c0e764SRichard Cochran 
4693bfd41b5SGrygorii Strashko static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb,
4703bfd41b5SGrygorii Strashko 			int ev_type, u32 skb_mtype_seqid)
47187c0e764SRichard Cochran {
47287c0e764SRichard Cochran 	struct list_head *this, *next;
4733bfd41b5SGrygorii Strashko 	struct cpts_event *event;
47487c0e764SRichard Cochran 	unsigned long flags;
4753bfd41b5SGrygorii Strashko 	u32 mtype_seqid;
4763bfd41b5SGrygorii Strashko 	u64 ns = 0;
47787c0e764SRichard Cochran 
478a93439ccSGrygorii Strashko 	cpts_fifo_read(cpts, -1);
479ba107428SGrygorii Strashko 	spin_lock_irqsave(&cpts->lock, flags);
48087c0e764SRichard Cochran 	list_for_each_safe(this, next, &cpts->events) {
48187c0e764SRichard Cochran 		event = list_entry(this, struct cpts_event, list);
48287c0e764SRichard Cochran 		if (event_expired(event)) {
48387c0e764SRichard Cochran 			list_del_init(&event->list);
48487c0e764SRichard Cochran 			list_add(&event->list, &cpts->pool);
48587c0e764SRichard Cochran 			continue;
48687c0e764SRichard Cochran 		}
4873bfd41b5SGrygorii Strashko 
4883bfd41b5SGrygorii Strashko 		mtype_seqid = event->high &
4893bfd41b5SGrygorii Strashko 			      ((MESSAGE_TYPE_MASK << MESSAGE_TYPE_SHIFT) |
4903bfd41b5SGrygorii Strashko 			       (SEQUENCE_ID_MASK << SEQUENCE_ID_SHIFT) |
4913bfd41b5SGrygorii Strashko 			       (EVENT_TYPE_MASK << EVENT_TYPE_SHIFT));
4923bfd41b5SGrygorii Strashko 
4933bfd41b5SGrygorii Strashko 		if (mtype_seqid == skb_mtype_seqid) {
494e66dccceSGrygorii Strashko 			ns = event->timestamp;
49587c0e764SRichard Cochran 			list_del_init(&event->list);
49687c0e764SRichard Cochran 			list_add(&event->list, &cpts->pool);
49787c0e764SRichard Cochran 			break;
49887c0e764SRichard Cochran 		}
49987c0e764SRichard Cochran 	}
50087c0e764SRichard Cochran 	spin_unlock_irqrestore(&cpts->lock, flags);
50187c0e764SRichard Cochran 
50287c0e764SRichard Cochran 	return ns;
50387c0e764SRichard Cochran }
50487c0e764SRichard Cochran 
50587c0e764SRichard Cochran void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
50687c0e764SRichard Cochran {
5073bfd41b5SGrygorii Strashko 	struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb;
50887c0e764SRichard Cochran 	struct skb_shared_hwtstamps *ssh;
5093bfd41b5SGrygorii Strashko 	int ret;
5103bfd41b5SGrygorii Strashko 	u64 ns;
51187c0e764SRichard Cochran 
51217de44c2SKurt Kanzenbach 	/* cpts_rx_timestamp() is called before eth_type_trans(), so
51317de44c2SKurt Kanzenbach 	 * skb MAC Hdr properties are not configured yet. Hence need to
51417de44c2SKurt Kanzenbach 	 * reset skb MAC header here
51517de44c2SKurt Kanzenbach 	 */
51617de44c2SKurt Kanzenbach 	skb_reset_mac_header(skb);
5173bfd41b5SGrygorii Strashko 	ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
5183bfd41b5SGrygorii Strashko 	if (!ret)
5193bfd41b5SGrygorii Strashko 		return;
5203bfd41b5SGrygorii Strashko 
5213bfd41b5SGrygorii Strashko 	skb_cb->skb_mtype_seqid |= (CPTS_EV_RX << EVENT_TYPE_SHIFT);
5223bfd41b5SGrygorii Strashko 
5233bfd41b5SGrygorii Strashko 	dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
5243bfd41b5SGrygorii Strashko 		__func__, skb_cb->skb_mtype_seqid);
5253bfd41b5SGrygorii Strashko 
5263bfd41b5SGrygorii Strashko 	ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid);
52787c0e764SRichard Cochran 	if (!ns)
52887c0e764SRichard Cochran 		return;
52987c0e764SRichard Cochran 	ssh = skb_hwtstamps(skb);
53087c0e764SRichard Cochran 	memset(ssh, 0, sizeof(*ssh));
53187c0e764SRichard Cochran 	ssh->hwtstamp = ns_to_ktime(ns);
53287c0e764SRichard Cochran }
533c8395d4eSGrygorii Strashko EXPORT_SYMBOL_GPL(cpts_rx_timestamp);
53487c0e764SRichard Cochran 
53587c0e764SRichard Cochran void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
53687c0e764SRichard Cochran {
5373bfd41b5SGrygorii Strashko 	struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb;
5383bfd41b5SGrygorii Strashko 	int ret;
53987c0e764SRichard Cochran 
54087c0e764SRichard Cochran 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
54187c0e764SRichard Cochran 		return;
5423bfd41b5SGrygorii Strashko 
5433bfd41b5SGrygorii Strashko 	ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
5443bfd41b5SGrygorii Strashko 	if (!ret)
5453bfd41b5SGrygorii Strashko 		return;
5463bfd41b5SGrygorii Strashko 
5473bfd41b5SGrygorii Strashko 	skb_cb->skb_mtype_seqid |= (CPTS_EV_TX << EVENT_TYPE_SHIFT);
5483bfd41b5SGrygorii Strashko 
5493bfd41b5SGrygorii Strashko 	dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
5503bfd41b5SGrygorii Strashko 		__func__, skb_cb->skb_mtype_seqid);
5513bfd41b5SGrygorii Strashko 
552c8f8e47eSGrygorii Strashko 	/* Always defer TX TS processing to PTP worker */
553c8f8e47eSGrygorii Strashko 	skb_get(skb);
554c8f8e47eSGrygorii Strashko 	/* get the timestamp for timeouts */
555c8f8e47eSGrygorii Strashko 	skb_cb->tmo = jiffies + msecs_to_jiffies(CPTS_SKB_RX_TX_TMO);
556c8f8e47eSGrygorii Strashko 	skb_queue_tail(&cpts->txq, skb);
557c8f8e47eSGrygorii Strashko 	ptp_schedule_worker(cpts->clock, 0);
55887c0e764SRichard Cochran }
559c8395d4eSGrygorii Strashko EXPORT_SYMBOL_GPL(cpts_tx_timestamp);
56087c0e764SRichard Cochran 
5618a2c9a5aSGrygorii Strashko int cpts_register(struct cpts *cpts)
56287c0e764SRichard Cochran {
56387c0e764SRichard Cochran 	int err, i;
56487c0e764SRichard Cochran 
5650d5f54feSGrygorii Strashko 	skb_queue_head_init(&cpts->txq);
56687c0e764SRichard Cochran 	INIT_LIST_HEAD(&cpts->events);
56787c0e764SRichard Cochran 	INIT_LIST_HEAD(&cpts->pool);
56887c0e764SRichard Cochran 	for (i = 0; i < CPTS_MAX_EVENTS; i++)
56987c0e764SRichard Cochran 		list_add(&cpts->pool_data[i].list, &cpts->pool);
57087c0e764SRichard Cochran 
5718a2c9a5aSGrygorii Strashko 	clk_enable(cpts->refclk);
5728a2c9a5aSGrygorii Strashko 
57387c0e764SRichard Cochran 	cpts_write32(cpts, CPTS_EN, control);
57487c0e764SRichard Cochran 	cpts_write32(cpts, TS_PEND_EN, int_enable);
57587c0e764SRichard Cochran 
576693bd8b7SIvan Khoronzhuk 	timecounter_init(&cpts->tc, &cpts->cc, ktime_get_real_ns());
57787c0e764SRichard Cochran 
5788a2c9a5aSGrygorii Strashko 	cpts->clock = ptp_clock_register(&cpts->info, cpts->dev);
5796c691405SGrygorii Strashko 	if (IS_ERR(cpts->clock)) {
5806c691405SGrygorii Strashko 		err = PTR_ERR(cpts->clock);
5816c691405SGrygorii Strashko 		cpts->clock = NULL;
5826c691405SGrygorii Strashko 		goto err_ptp;
5836c691405SGrygorii Strashko 	}
5846c691405SGrygorii Strashko 	cpts->phc_index = ptp_clock_index(cpts->clock);
5856c691405SGrygorii Strashko 
586999f1292SGrygorii Strashko 	ptp_schedule_worker(cpts->clock, cpts->ov_check_period);
58787c0e764SRichard Cochran 	return 0;
5886c691405SGrygorii Strashko 
5896c691405SGrygorii Strashko err_ptp:
5908a2c9a5aSGrygorii Strashko 	clk_disable(cpts->refclk);
5916c691405SGrygorii Strashko 	return err;
59287c0e764SRichard Cochran }
593c8395d4eSGrygorii Strashko EXPORT_SYMBOL_GPL(cpts_register);
59487c0e764SRichard Cochran 
59587c0e764SRichard Cochran void cpts_unregister(struct cpts *cpts)
59687c0e764SRichard Cochran {
5978a2c9a5aSGrygorii Strashko 	if (WARN_ON(!cpts->clock))
5988a2c9a5aSGrygorii Strashko 		return;
5998a2c9a5aSGrygorii Strashko 
6008a2c9a5aSGrygorii Strashko 	ptp_clock_unregister(cpts->clock);
6018a2c9a5aSGrygorii Strashko 	cpts->clock = NULL;
6024614792eSGrygorii Strashko 	cpts->phc_index = -1;
6038fcd6891SGrygorii Strashko 
6048fcd6891SGrygorii Strashko 	cpts_write32(cpts, 0, int_enable);
6058fcd6891SGrygorii Strashko 	cpts_write32(cpts, 0, control);
6068fcd6891SGrygorii Strashko 
6070d5f54feSGrygorii Strashko 	/* Drop all packet */
6080d5f54feSGrygorii Strashko 	skb_queue_purge(&cpts->txq);
6090d5f54feSGrygorii Strashko 
6108a2c9a5aSGrygorii Strashko 	clk_disable(cpts->refclk);
61187c0e764SRichard Cochran }
612c8395d4eSGrygorii Strashko EXPORT_SYMBOL_GPL(cpts_unregister);
613c8395d4eSGrygorii Strashko 
61488f0f0b0SGrygorii Strashko static void cpts_calc_mult_shift(struct cpts *cpts)
61588f0f0b0SGrygorii Strashko {
61688f0f0b0SGrygorii Strashko 	u64 frac, maxsec, ns;
61788f0f0b0SGrygorii Strashko 	u32 freq;
61888f0f0b0SGrygorii Strashko 
61988f0f0b0SGrygorii Strashko 	freq = clk_get_rate(cpts->refclk);
62088f0f0b0SGrygorii Strashko 
62188f0f0b0SGrygorii Strashko 	/* Calc the maximum number of seconds which we can run before
62288f0f0b0SGrygorii Strashko 	 * wrapping around.
62388f0f0b0SGrygorii Strashko 	 */
62488f0f0b0SGrygorii Strashko 	maxsec = cpts->cc.mask;
62588f0f0b0SGrygorii Strashko 	do_div(maxsec, freq);
62688f0f0b0SGrygorii Strashko 	/* limit conversation rate to 10 sec as higher values will produce
62788f0f0b0SGrygorii Strashko 	 * too small mult factors and so reduce the conversion accuracy
62888f0f0b0SGrygorii Strashko 	 */
62988f0f0b0SGrygorii Strashko 	if (maxsec > 10)
63088f0f0b0SGrygorii Strashko 		maxsec = 10;
63188f0f0b0SGrygorii Strashko 
63220138cf9SGrygorii Strashko 	/* Calc overflow check period (maxsec / 2) */
63320138cf9SGrygorii Strashko 	cpts->ov_check_period = (HZ * maxsec) / 2;
63420138cf9SGrygorii Strashko 	dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n",
63520138cf9SGrygorii Strashko 		 cpts->ov_check_period);
63620138cf9SGrygorii Strashko 
63788f0f0b0SGrygorii Strashko 	if (cpts->cc.mult || cpts->cc.shift)
63888f0f0b0SGrygorii Strashko 		return;
63988f0f0b0SGrygorii Strashko 
64088f0f0b0SGrygorii Strashko 	clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift,
64188f0f0b0SGrygorii Strashko 			       freq, NSEC_PER_SEC, maxsec);
64288f0f0b0SGrygorii Strashko 
64388f0f0b0SGrygorii Strashko 	frac = 0;
64488f0f0b0SGrygorii Strashko 	ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac);
64588f0f0b0SGrygorii Strashko 
64688f0f0b0SGrygorii Strashko 	dev_info(cpts->dev,
64788f0f0b0SGrygorii Strashko 		 "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n",
64888f0f0b0SGrygorii Strashko 		 freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC));
64988f0f0b0SGrygorii Strashko }
65088f0f0b0SGrygorii Strashko 
651a3047a81SGrygorii Strashko static int cpts_of_mux_clk_setup(struct cpts *cpts, struct device_node *node)
652a3047a81SGrygorii Strashko {
653a3047a81SGrygorii Strashko 	struct device_node *refclk_np;
654a3047a81SGrygorii Strashko 	const char **parent_names;
655a3047a81SGrygorii Strashko 	unsigned int num_parents;
656a3047a81SGrygorii Strashko 	struct clk_hw *clk_hw;
657a3047a81SGrygorii Strashko 	int ret = -EINVAL;
658a3047a81SGrygorii Strashko 	u32 *mux_table;
659a3047a81SGrygorii Strashko 
660a3047a81SGrygorii Strashko 	refclk_np = of_get_child_by_name(node, "cpts-refclk-mux");
661a3047a81SGrygorii Strashko 	if (!refclk_np)
662a3047a81SGrygorii Strashko 		/* refclk selection supported not for all SoCs */
663a3047a81SGrygorii Strashko 		return 0;
664a3047a81SGrygorii Strashko 
665a3047a81SGrygorii Strashko 	num_parents = of_clk_get_parent_count(refclk_np);
666a3047a81SGrygorii Strashko 	if (num_parents < 1) {
667a3047a81SGrygorii Strashko 		dev_err(cpts->dev, "mux-clock %s must have parents\n",
668a3047a81SGrygorii Strashko 			refclk_np->name);
669a3047a81SGrygorii Strashko 		goto mux_fail;
670a3047a81SGrygorii Strashko 	}
671a3047a81SGrygorii Strashko 
672*c514fbb6SGustavo A. R. Silva 	parent_names = devm_kcalloc(cpts->dev, num_parents,
673*c514fbb6SGustavo A. R. Silva 				    sizeof(*parent_names), GFP_KERNEL);
674a3047a81SGrygorii Strashko 
675*c514fbb6SGustavo A. R. Silva 	mux_table = devm_kcalloc(cpts->dev, num_parents, sizeof(*mux_table),
676a3047a81SGrygorii Strashko 				 GFP_KERNEL);
677a3047a81SGrygorii Strashko 	if (!mux_table || !parent_names) {
678a3047a81SGrygorii Strashko 		ret = -ENOMEM;
679a3047a81SGrygorii Strashko 		goto mux_fail;
680a3047a81SGrygorii Strashko 	}
681a3047a81SGrygorii Strashko 
682a3047a81SGrygorii Strashko 	of_clk_parent_fill(refclk_np, parent_names, num_parents);
683a3047a81SGrygorii Strashko 
684a3047a81SGrygorii Strashko 	ret = of_property_read_variable_u32_array(refclk_np, "ti,mux-tbl",
685a3047a81SGrygorii Strashko 						  mux_table,
686a3047a81SGrygorii Strashko 						  num_parents, num_parents);
687a3047a81SGrygorii Strashko 	if (ret < 0)
688a3047a81SGrygorii Strashko 		goto mux_fail;
689a3047a81SGrygorii Strashko 
690a3047a81SGrygorii Strashko 	clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name,
691a3047a81SGrygorii Strashko 					   parent_names, num_parents,
692a3047a81SGrygorii Strashko 					   0,
693a3047a81SGrygorii Strashko 					   &cpts->reg->rftclk_sel, 0, 0x1F,
694a3047a81SGrygorii Strashko 					   0, mux_table, NULL);
695a3047a81SGrygorii Strashko 	if (IS_ERR(clk_hw)) {
696a3047a81SGrygorii Strashko 		ret = PTR_ERR(clk_hw);
697a3047a81SGrygorii Strashko 		goto mux_fail;
698a3047a81SGrygorii Strashko 	}
699a3047a81SGrygorii Strashko 
700a3047a81SGrygorii Strashko 	ret = devm_add_action_or_reset(cpts->dev,
701a3047a81SGrygorii Strashko 				       (void(*)(void *))clk_hw_unregister_mux,
702a3047a81SGrygorii Strashko 				       clk_hw);
703a3047a81SGrygorii Strashko 	if (ret) {
704a3047a81SGrygorii Strashko 		dev_err(cpts->dev, "add clkmux unreg action %d", ret);
705a3047a81SGrygorii Strashko 		goto mux_fail;
706a3047a81SGrygorii Strashko 	}
707a3047a81SGrygorii Strashko 
708a3047a81SGrygorii Strashko 	ret = of_clk_add_hw_provider(refclk_np, of_clk_hw_simple_get, clk_hw);
709a3047a81SGrygorii Strashko 	if (ret)
710a3047a81SGrygorii Strashko 		goto mux_fail;
711a3047a81SGrygorii Strashko 
712a3047a81SGrygorii Strashko 	ret = devm_add_action_or_reset(cpts->dev,
713a3047a81SGrygorii Strashko 				       (void(*)(void *))of_clk_del_provider,
714a3047a81SGrygorii Strashko 				       refclk_np);
715a3047a81SGrygorii Strashko 	if (ret) {
716a3047a81SGrygorii Strashko 		dev_err(cpts->dev, "add clkmux provider unreg action %d", ret);
717a3047a81SGrygorii Strashko 		goto mux_fail;
718a3047a81SGrygorii Strashko 	}
719a3047a81SGrygorii Strashko 
720a3047a81SGrygorii Strashko 	return ret;
721a3047a81SGrygorii Strashko 
722a3047a81SGrygorii Strashko mux_fail:
723a3047a81SGrygorii Strashko 	of_node_put(refclk_np);
724a3047a81SGrygorii Strashko 	return ret;
725a3047a81SGrygorii Strashko }
726a3047a81SGrygorii Strashko 
7274a88fb95SGrygorii Strashko static int cpts_of_parse(struct cpts *cpts, struct device_node *node)
7284a88fb95SGrygorii Strashko {
7294a88fb95SGrygorii Strashko 	int ret = -EINVAL;
7304a88fb95SGrygorii Strashko 	u32 prop;
7314a88fb95SGrygorii Strashko 
73288f0f0b0SGrygorii Strashko 	if (!of_property_read_u32(node, "cpts_clock_mult", &prop))
7334a88fb95SGrygorii Strashko 		cpts->cc.mult = prop;
7344a88fb95SGrygorii Strashko 
73588f0f0b0SGrygorii Strashko 	if (!of_property_read_u32(node, "cpts_clock_shift", &prop))
7364a88fb95SGrygorii Strashko 		cpts->cc.shift = prop;
7374a88fb95SGrygorii Strashko 
73888f0f0b0SGrygorii Strashko 	if ((cpts->cc.mult && !cpts->cc.shift) ||
73988f0f0b0SGrygorii Strashko 	    (!cpts->cc.mult && cpts->cc.shift))
74088f0f0b0SGrygorii Strashko 		goto of_error;
74188f0f0b0SGrygorii Strashko 
742a3047a81SGrygorii Strashko 	return cpts_of_mux_clk_setup(cpts, node);
7434a88fb95SGrygorii Strashko 
7444a88fb95SGrygorii Strashko of_error:
7454a88fb95SGrygorii Strashko 	dev_err(cpts->dev, "CPTS: Missing property in the DT.\n");
7464a88fb95SGrygorii Strashko 	return ret;
7474a88fb95SGrygorii Strashko }
7484a88fb95SGrygorii Strashko 
7498a2c9a5aSGrygorii Strashko struct cpts *cpts_create(struct device *dev, void __iomem *regs,
750b78aba49SGrygorii Strashko 			 struct device_node *node, u32 n_ext_ts)
7518a2c9a5aSGrygorii Strashko {
7528a2c9a5aSGrygorii Strashko 	struct cpts *cpts;
7534a88fb95SGrygorii Strashko 	int ret;
7548a2c9a5aSGrygorii Strashko 
7558a2c9a5aSGrygorii Strashko 	cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
7568a2c9a5aSGrygorii Strashko 	if (!cpts)
7578a2c9a5aSGrygorii Strashko 		return ERR_PTR(-ENOMEM);
7588a2c9a5aSGrygorii Strashko 
7598a2c9a5aSGrygorii Strashko 	cpts->dev = dev;
7608a2c9a5aSGrygorii Strashko 	cpts->reg = (struct cpsw_cpts __iomem *)regs;
76185624412SGrygorii Strashko 	cpts->irq_poll = true;
7628a2c9a5aSGrygorii Strashko 	spin_lock_init(&cpts->lock);
763ba107428SGrygorii Strashko 	mutex_init(&cpts->ptp_clk_mutex);
76485624412SGrygorii Strashko 	init_completion(&cpts->ts_push_complete);
7658a2c9a5aSGrygorii Strashko 
7664a88fb95SGrygorii Strashko 	ret = cpts_of_parse(cpts, node);
7674a88fb95SGrygorii Strashko 	if (ret)
7684a88fb95SGrygorii Strashko 		return ERR_PTR(ret);
7694a88fb95SGrygorii Strashko 
7708a6389a5SGrygorii Strashko 	cpts->refclk = devm_get_clk_from_child(dev, node, "cpts");
7718a6389a5SGrygorii Strashko 	if (IS_ERR(cpts->refclk))
7728a6389a5SGrygorii Strashko 		/* try get clk from dev node for compatibility */
7738a2c9a5aSGrygorii Strashko 		cpts->refclk = devm_clk_get(dev, "cpts");
7748a6389a5SGrygorii Strashko 
7758a2c9a5aSGrygorii Strashko 	if (IS_ERR(cpts->refclk)) {
7768a6389a5SGrygorii Strashko 		dev_err(dev, "Failed to get cpts refclk %ld\n",
7778a6389a5SGrygorii Strashko 			PTR_ERR(cpts->refclk));
778bde4c563SHernán Gonzalez 		return ERR_CAST(cpts->refclk);
7798a2c9a5aSGrygorii Strashko 	}
7808a2c9a5aSGrygorii Strashko 
7812d822f2dSKangjie Lu 	ret = clk_prepare(cpts->refclk);
7822d822f2dSKangjie Lu 	if (ret)
7832d822f2dSKangjie Lu 		return ERR_PTR(ret);
7848a2c9a5aSGrygorii Strashko 
7858a2c9a5aSGrygorii Strashko 	cpts->cc.read = cpts_systim_read;
7868a2c9a5aSGrygorii Strashko 	cpts->cc.mask = CLOCKSOURCE_MASK(32);
78788f0f0b0SGrygorii Strashko 	cpts->info = cpts_info;
7884614792eSGrygorii Strashko 	cpts->phc_index = -1;
78988f0f0b0SGrygorii Strashko 
790b78aba49SGrygorii Strashko 	if (n_ext_ts)
791b78aba49SGrygorii Strashko 		cpts->info.n_ext_ts = n_ext_ts;
792b78aba49SGrygorii Strashko 
79388f0f0b0SGrygorii Strashko 	cpts_calc_mult_shift(cpts);
7944a88fb95SGrygorii Strashko 	/* save cc.mult original value as it can be modified
7954a88fb95SGrygorii Strashko 	 * by cpts_ptp_adjfreq().
7964a88fb95SGrygorii Strashko 	 */
7974a88fb95SGrygorii Strashko 	cpts->cc_mult = cpts->cc.mult;
7988a2c9a5aSGrygorii Strashko 
7998a2c9a5aSGrygorii Strashko 	return cpts;
8008a2c9a5aSGrygorii Strashko }
8018a2c9a5aSGrygorii Strashko EXPORT_SYMBOL_GPL(cpts_create);
8028a2c9a5aSGrygorii Strashko 
8038a2c9a5aSGrygorii Strashko void cpts_release(struct cpts *cpts)
8048a2c9a5aSGrygorii Strashko {
8058a2c9a5aSGrygorii Strashko 	if (!cpts)
8068a2c9a5aSGrygorii Strashko 		return;
8078a2c9a5aSGrygorii Strashko 
8088a2c9a5aSGrygorii Strashko 	if (WARN_ON(!cpts->refclk))
8098a2c9a5aSGrygorii Strashko 		return;
8108a2c9a5aSGrygorii Strashko 
8118a2c9a5aSGrygorii Strashko 	clk_unprepare(cpts->refclk);
8128a2c9a5aSGrygorii Strashko }
8138a2c9a5aSGrygorii Strashko EXPORT_SYMBOL_GPL(cpts_release);
8148a2c9a5aSGrygorii Strashko 
815c8395d4eSGrygorii Strashko MODULE_LICENSE("GPL v2");
816c8395d4eSGrygorii Strashko MODULE_DESCRIPTION("TI CPTS driver");
817c8395d4eSGrygorii Strashko MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
818