1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * TI Common Platform Time Sync
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun #include <linux/clk-provider.h>
9*4882a593Smuzhiyun #include <linux/err.h>
10*4882a593Smuzhiyun #include <linux/if.h>
11*4882a593Smuzhiyun #include <linux/hrtimer.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/net_tstamp.h>
14*4882a593Smuzhiyun #include <linux/ptp_classify.h>
15*4882a593Smuzhiyun #include <linux/time.h>
16*4882a593Smuzhiyun #include <linux/uaccess.h>
17*4882a593Smuzhiyun #include <linux/workqueue.h>
18*4882a593Smuzhiyun #include <linux/if_ether.h>
19*4882a593Smuzhiyun #include <linux/if_vlan.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include "cpts.h"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #define CPTS_SKB_TX_WORK_TIMEOUT 1 /* jiffies */
24*4882a593Smuzhiyun #define CPTS_SKB_RX_TX_TMO 100 /*ms */
25*4882a593Smuzhiyun #define CPTS_EVENT_RX_TX_TIMEOUT (100) /* ms */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun struct cpts_skb_cb_data {
28*4882a593Smuzhiyun u32 skb_mtype_seqid;
29*4882a593Smuzhiyun unsigned long tmo;
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define cpts_read32(c, r) readl_relaxed(&c->reg->r)
33*4882a593Smuzhiyun #define cpts_write32(c, v, r) writel_relaxed(v, &c->reg->r)
34*4882a593Smuzhiyun
cpts_event_port(struct cpts_event * event)35*4882a593Smuzhiyun static int cpts_event_port(struct cpts_event *event)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun return (event->high >> PORT_NUMBER_SHIFT) & PORT_NUMBER_MASK;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun
event_expired(struct cpts_event * event)40*4882a593Smuzhiyun static int event_expired(struct cpts_event *event)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun return time_after(jiffies, event->tmo);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
event_type(struct cpts_event * event)45*4882a593Smuzhiyun static int event_type(struct cpts_event *event)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun return (event->high >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
cpts_fifo_pop(struct cpts * cpts,u32 * high,u32 * low)50*4882a593Smuzhiyun static int cpts_fifo_pop(struct cpts *cpts, u32 *high, u32 *low)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun u32 r = cpts_read32(cpts, intstat_raw);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (r & TS_PEND_RAW) {
55*4882a593Smuzhiyun *high = cpts_read32(cpts, event_high);
56*4882a593Smuzhiyun *low = cpts_read32(cpts, event_low);
57*4882a593Smuzhiyun cpts_write32(cpts, EVENT_POP, event_pop);
58*4882a593Smuzhiyun return 0;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun return -1;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
cpts_purge_events(struct cpts * cpts)63*4882a593Smuzhiyun static int cpts_purge_events(struct cpts *cpts)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun struct list_head *this, *next;
66*4882a593Smuzhiyun struct cpts_event *event;
67*4882a593Smuzhiyun int removed = 0;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun list_for_each_safe(this, next, &cpts->events) {
70*4882a593Smuzhiyun event = list_entry(this, struct cpts_event, list);
71*4882a593Smuzhiyun if (event_expired(event)) {
72*4882a593Smuzhiyun list_del_init(&event->list);
73*4882a593Smuzhiyun list_add(&event->list, &cpts->pool);
74*4882a593Smuzhiyun ++removed;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun if (removed)
79*4882a593Smuzhiyun dev_dbg(cpts->dev, "cpts: event pool cleaned up %d\n", removed);
80*4882a593Smuzhiyun return removed ? 0 : -1;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
cpts_purge_txq(struct cpts * cpts)83*4882a593Smuzhiyun static void cpts_purge_txq(struct cpts *cpts)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun struct cpts_skb_cb_data *skb_cb;
86*4882a593Smuzhiyun struct sk_buff *skb, *tmp;
87*4882a593Smuzhiyun int removed = 0;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun skb_queue_walk_safe(&cpts->txq, skb, tmp) {
90*4882a593Smuzhiyun skb_cb = (struct cpts_skb_cb_data *)skb->cb;
91*4882a593Smuzhiyun if (time_after(jiffies, skb_cb->tmo)) {
92*4882a593Smuzhiyun __skb_unlink(skb, &cpts->txq);
93*4882a593Smuzhiyun dev_consume_skb_any(skb);
94*4882a593Smuzhiyun ++removed;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun if (removed)
99*4882a593Smuzhiyun dev_dbg(cpts->dev, "txq cleaned up %d\n", removed);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * Returns zero if matching event type was found.
104*4882a593Smuzhiyun */
cpts_fifo_read(struct cpts * cpts,int match)105*4882a593Smuzhiyun static int cpts_fifo_read(struct cpts *cpts, int match)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun struct ptp_clock_event pevent;
108*4882a593Smuzhiyun bool need_schedule = false;
109*4882a593Smuzhiyun struct cpts_event *event;
110*4882a593Smuzhiyun unsigned long flags;
111*4882a593Smuzhiyun int i, type = -1;
112*4882a593Smuzhiyun u32 hi, lo;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun spin_lock_irqsave(&cpts->lock, flags);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun for (i = 0; i < CPTS_FIFO_DEPTH; i++) {
117*4882a593Smuzhiyun if (cpts_fifo_pop(cpts, &hi, &lo))
118*4882a593Smuzhiyun break;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun if (list_empty(&cpts->pool) && cpts_purge_events(cpts)) {
121*4882a593Smuzhiyun dev_warn(cpts->dev, "cpts: event pool empty\n");
122*4882a593Smuzhiyun break;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun event = list_first_entry(&cpts->pool, struct cpts_event, list);
126*4882a593Smuzhiyun event->high = hi;
127*4882a593Smuzhiyun event->low = lo;
128*4882a593Smuzhiyun event->timestamp = timecounter_cyc2time(&cpts->tc, event->low);
129*4882a593Smuzhiyun type = event_type(event);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun dev_dbg(cpts->dev, "CPTS_EV: %d high:%08X low:%08x\n",
132*4882a593Smuzhiyun type, event->high, event->low);
133*4882a593Smuzhiyun switch (type) {
134*4882a593Smuzhiyun case CPTS_EV_PUSH:
135*4882a593Smuzhiyun WRITE_ONCE(cpts->cur_timestamp, lo);
136*4882a593Smuzhiyun timecounter_read(&cpts->tc);
137*4882a593Smuzhiyun if (cpts->mult_new) {
138*4882a593Smuzhiyun cpts->cc.mult = cpts->mult_new;
139*4882a593Smuzhiyun cpts->mult_new = 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun if (!cpts->irq_poll)
142*4882a593Smuzhiyun complete(&cpts->ts_push_complete);
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun case CPTS_EV_TX:
145*4882a593Smuzhiyun case CPTS_EV_RX:
146*4882a593Smuzhiyun event->tmo = jiffies +
147*4882a593Smuzhiyun msecs_to_jiffies(CPTS_EVENT_RX_TX_TIMEOUT);
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun list_del_init(&event->list);
150*4882a593Smuzhiyun list_add_tail(&event->list, &cpts->events);
151*4882a593Smuzhiyun need_schedule = true;
152*4882a593Smuzhiyun break;
153*4882a593Smuzhiyun case CPTS_EV_ROLL:
154*4882a593Smuzhiyun case CPTS_EV_HALF:
155*4882a593Smuzhiyun break;
156*4882a593Smuzhiyun case CPTS_EV_HW:
157*4882a593Smuzhiyun pevent.timestamp = event->timestamp;
158*4882a593Smuzhiyun pevent.type = PTP_CLOCK_EXTTS;
159*4882a593Smuzhiyun pevent.index = cpts_event_port(event) - 1;
160*4882a593Smuzhiyun ptp_clock_event(cpts->clock, &pevent);
161*4882a593Smuzhiyun break;
162*4882a593Smuzhiyun default:
163*4882a593Smuzhiyun dev_err(cpts->dev, "cpts: unknown event type\n");
164*4882a593Smuzhiyun break;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun if (type == match)
167*4882a593Smuzhiyun break;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun spin_unlock_irqrestore(&cpts->lock, flags);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun if (!cpts->irq_poll && need_schedule)
173*4882a593Smuzhiyun ptp_schedule_worker(cpts->clock, 0);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun return type == match ? 0 : -1;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
cpts_misc_interrupt(struct cpts * cpts)178*4882a593Smuzhiyun void cpts_misc_interrupt(struct cpts *cpts)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun cpts_fifo_read(cpts, -1);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cpts_misc_interrupt);
183*4882a593Smuzhiyun
cpts_systim_read(const struct cyclecounter * cc)184*4882a593Smuzhiyun static u64 cpts_systim_read(const struct cyclecounter *cc)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun struct cpts *cpts = container_of(cc, struct cpts, cc);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun return READ_ONCE(cpts->cur_timestamp);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
cpts_update_cur_time(struct cpts * cpts,int match,struct ptp_system_timestamp * sts)191*4882a593Smuzhiyun static void cpts_update_cur_time(struct cpts *cpts, int match,
192*4882a593Smuzhiyun struct ptp_system_timestamp *sts)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun unsigned long flags;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun reinit_completion(&cpts->ts_push_complete);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /* use spin_lock_irqsave() here as it has to run very fast */
199*4882a593Smuzhiyun spin_lock_irqsave(&cpts->lock, flags);
200*4882a593Smuzhiyun ptp_read_system_prets(sts);
201*4882a593Smuzhiyun cpts_write32(cpts, TS_PUSH, ts_push);
202*4882a593Smuzhiyun cpts_read32(cpts, ts_push);
203*4882a593Smuzhiyun ptp_read_system_postts(sts);
204*4882a593Smuzhiyun spin_unlock_irqrestore(&cpts->lock, flags);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun if (cpts->irq_poll && cpts_fifo_read(cpts, match) && match != -1)
207*4882a593Smuzhiyun dev_err(cpts->dev, "cpts: unable to obtain a time stamp\n");
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (!cpts->irq_poll &&
210*4882a593Smuzhiyun !wait_for_completion_timeout(&cpts->ts_push_complete, HZ))
211*4882a593Smuzhiyun dev_err(cpts->dev, "cpts: obtain a time stamp timeout\n");
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /* PTP clock operations */
215*4882a593Smuzhiyun
cpts_ptp_adjfreq(struct ptp_clock_info * ptp,s32 ppb)216*4882a593Smuzhiyun static int cpts_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun struct cpts *cpts = container_of(ptp, struct cpts, info);
219*4882a593Smuzhiyun int neg_adj = 0;
220*4882a593Smuzhiyun u32 diff, mult;
221*4882a593Smuzhiyun u64 adj;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun if (ppb < 0) {
224*4882a593Smuzhiyun neg_adj = 1;
225*4882a593Smuzhiyun ppb = -ppb;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun mult = cpts->cc_mult;
228*4882a593Smuzhiyun adj = mult;
229*4882a593Smuzhiyun adj *= ppb;
230*4882a593Smuzhiyun diff = div_u64(adj, 1000000000ULL);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun mutex_lock(&cpts->ptp_clk_mutex);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun cpts->mult_new = neg_adj ? mult - diff : mult + diff;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun cpts_update_cur_time(cpts, CPTS_EV_PUSH, NULL);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun mutex_unlock(&cpts->ptp_clk_mutex);
239*4882a593Smuzhiyun return 0;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
cpts_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)242*4882a593Smuzhiyun static int cpts_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun struct cpts *cpts = container_of(ptp, struct cpts, info);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun mutex_lock(&cpts->ptp_clk_mutex);
247*4882a593Smuzhiyun timecounter_adjtime(&cpts->tc, delta);
248*4882a593Smuzhiyun mutex_unlock(&cpts->ptp_clk_mutex);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun return 0;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
cpts_ptp_gettimeex(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)253*4882a593Smuzhiyun static int cpts_ptp_gettimeex(struct ptp_clock_info *ptp,
254*4882a593Smuzhiyun struct timespec64 *ts,
255*4882a593Smuzhiyun struct ptp_system_timestamp *sts)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun struct cpts *cpts = container_of(ptp, struct cpts, info);
258*4882a593Smuzhiyun u64 ns;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun mutex_lock(&cpts->ptp_clk_mutex);
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun cpts_update_cur_time(cpts, CPTS_EV_PUSH, sts);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun ns = timecounter_read(&cpts->tc);
265*4882a593Smuzhiyun mutex_unlock(&cpts->ptp_clk_mutex);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun *ts = ns_to_timespec64(ns);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun return 0;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
cpts_ptp_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)272*4882a593Smuzhiyun static int cpts_ptp_settime(struct ptp_clock_info *ptp,
273*4882a593Smuzhiyun const struct timespec64 *ts)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun struct cpts *cpts = container_of(ptp, struct cpts, info);
276*4882a593Smuzhiyun u64 ns;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun ns = timespec64_to_ns(ts);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun mutex_lock(&cpts->ptp_clk_mutex);
281*4882a593Smuzhiyun timecounter_init(&cpts->tc, &cpts->cc, ns);
282*4882a593Smuzhiyun mutex_unlock(&cpts->ptp_clk_mutex);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun return 0;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
cpts_extts_enable(struct cpts * cpts,u32 index,int on)287*4882a593Smuzhiyun static int cpts_extts_enable(struct cpts *cpts, u32 index, int on)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun u32 v;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun if (((cpts->hw_ts_enable & BIT(index)) >> index) == on)
292*4882a593Smuzhiyun return 0;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun mutex_lock(&cpts->ptp_clk_mutex);
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun v = cpts_read32(cpts, control);
297*4882a593Smuzhiyun if (on) {
298*4882a593Smuzhiyun v |= BIT(8 + index);
299*4882a593Smuzhiyun cpts->hw_ts_enable |= BIT(index);
300*4882a593Smuzhiyun } else {
301*4882a593Smuzhiyun v &= ~BIT(8 + index);
302*4882a593Smuzhiyun cpts->hw_ts_enable &= ~BIT(index);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun cpts_write32(cpts, v, control);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun mutex_unlock(&cpts->ptp_clk_mutex);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun return 0;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
cpts_ptp_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)311*4882a593Smuzhiyun static int cpts_ptp_enable(struct ptp_clock_info *ptp,
312*4882a593Smuzhiyun struct ptp_clock_request *rq, int on)
313*4882a593Smuzhiyun {
314*4882a593Smuzhiyun struct cpts *cpts = container_of(ptp, struct cpts, info);
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun switch (rq->type) {
317*4882a593Smuzhiyun case PTP_CLK_REQ_EXTTS:
318*4882a593Smuzhiyun return cpts_extts_enable(cpts, rq->extts.index, on);
319*4882a593Smuzhiyun default:
320*4882a593Smuzhiyun break;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun return -EOPNOTSUPP;
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
cpts_match_tx_ts(struct cpts * cpts,struct cpts_event * event)326*4882a593Smuzhiyun static bool cpts_match_tx_ts(struct cpts *cpts, struct cpts_event *event)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun struct sk_buff_head txq_list;
329*4882a593Smuzhiyun struct sk_buff *skb, *tmp;
330*4882a593Smuzhiyun unsigned long flags;
331*4882a593Smuzhiyun bool found = false;
332*4882a593Smuzhiyun u32 mtype_seqid;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun mtype_seqid = event->high &
335*4882a593Smuzhiyun ((MESSAGE_TYPE_MASK << MESSAGE_TYPE_SHIFT) |
336*4882a593Smuzhiyun (SEQUENCE_ID_MASK << SEQUENCE_ID_SHIFT) |
337*4882a593Smuzhiyun (EVENT_TYPE_MASK << EVENT_TYPE_SHIFT));
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun __skb_queue_head_init(&txq_list);
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun spin_lock_irqsave(&cpts->txq.lock, flags);
342*4882a593Smuzhiyun skb_queue_splice_init(&cpts->txq, &txq_list);
343*4882a593Smuzhiyun spin_unlock_irqrestore(&cpts->txq.lock, flags);
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun skb_queue_walk_safe(&txq_list, skb, tmp) {
346*4882a593Smuzhiyun struct skb_shared_hwtstamps ssh;
347*4882a593Smuzhiyun struct cpts_skb_cb_data *skb_cb =
348*4882a593Smuzhiyun (struct cpts_skb_cb_data *)skb->cb;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun if (mtype_seqid == skb_cb->skb_mtype_seqid) {
351*4882a593Smuzhiyun memset(&ssh, 0, sizeof(ssh));
352*4882a593Smuzhiyun ssh.hwtstamp = ns_to_ktime(event->timestamp);
353*4882a593Smuzhiyun skb_tstamp_tx(skb, &ssh);
354*4882a593Smuzhiyun found = true;
355*4882a593Smuzhiyun __skb_unlink(skb, &txq_list);
356*4882a593Smuzhiyun dev_consume_skb_any(skb);
357*4882a593Smuzhiyun dev_dbg(cpts->dev, "match tx timestamp mtype_seqid %08x\n",
358*4882a593Smuzhiyun mtype_seqid);
359*4882a593Smuzhiyun break;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun if (time_after(jiffies, skb_cb->tmo)) {
363*4882a593Smuzhiyun /* timeout any expired skbs over 1s */
364*4882a593Smuzhiyun dev_dbg(cpts->dev, "expiring tx timestamp from txq\n");
365*4882a593Smuzhiyun __skb_unlink(skb, &txq_list);
366*4882a593Smuzhiyun dev_consume_skb_any(skb);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun spin_lock_irqsave(&cpts->txq.lock, flags);
371*4882a593Smuzhiyun skb_queue_splice(&txq_list, &cpts->txq);
372*4882a593Smuzhiyun spin_unlock_irqrestore(&cpts->txq.lock, flags);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun return found;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
cpts_process_events(struct cpts * cpts)377*4882a593Smuzhiyun static void cpts_process_events(struct cpts *cpts)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun struct list_head *this, *next;
380*4882a593Smuzhiyun struct cpts_event *event;
381*4882a593Smuzhiyun LIST_HEAD(events_free);
382*4882a593Smuzhiyun unsigned long flags;
383*4882a593Smuzhiyun LIST_HEAD(events);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun spin_lock_irqsave(&cpts->lock, flags);
386*4882a593Smuzhiyun list_splice_init(&cpts->events, &events);
387*4882a593Smuzhiyun spin_unlock_irqrestore(&cpts->lock, flags);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun list_for_each_safe(this, next, &events) {
390*4882a593Smuzhiyun event = list_entry(this, struct cpts_event, list);
391*4882a593Smuzhiyun if (cpts_match_tx_ts(cpts, event) ||
392*4882a593Smuzhiyun time_after(jiffies, event->tmo)) {
393*4882a593Smuzhiyun list_del_init(&event->list);
394*4882a593Smuzhiyun list_add(&event->list, &events_free);
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun spin_lock_irqsave(&cpts->lock, flags);
399*4882a593Smuzhiyun list_splice_tail(&events, &cpts->events);
400*4882a593Smuzhiyun list_splice_tail(&events_free, &cpts->pool);
401*4882a593Smuzhiyun spin_unlock_irqrestore(&cpts->lock, flags);
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
cpts_overflow_check(struct ptp_clock_info * ptp)404*4882a593Smuzhiyun static long cpts_overflow_check(struct ptp_clock_info *ptp)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun struct cpts *cpts = container_of(ptp, struct cpts, info);
407*4882a593Smuzhiyun unsigned long delay = cpts->ov_check_period;
408*4882a593Smuzhiyun unsigned long flags;
409*4882a593Smuzhiyun u64 ns;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun mutex_lock(&cpts->ptp_clk_mutex);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun cpts_update_cur_time(cpts, -1, NULL);
414*4882a593Smuzhiyun ns = timecounter_read(&cpts->tc);
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun cpts_process_events(cpts);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun spin_lock_irqsave(&cpts->txq.lock, flags);
419*4882a593Smuzhiyun if (!skb_queue_empty(&cpts->txq)) {
420*4882a593Smuzhiyun cpts_purge_txq(cpts);
421*4882a593Smuzhiyun if (!skb_queue_empty(&cpts->txq))
422*4882a593Smuzhiyun delay = CPTS_SKB_TX_WORK_TIMEOUT;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun spin_unlock_irqrestore(&cpts->txq.lock, flags);
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun dev_dbg(cpts->dev, "cpts overflow check at %lld\n", ns);
427*4882a593Smuzhiyun mutex_unlock(&cpts->ptp_clk_mutex);
428*4882a593Smuzhiyun return (long)delay;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun static const struct ptp_clock_info cpts_info = {
432*4882a593Smuzhiyun .owner = THIS_MODULE,
433*4882a593Smuzhiyun .name = "CTPS timer",
434*4882a593Smuzhiyun .max_adj = 1000000,
435*4882a593Smuzhiyun .n_ext_ts = 0,
436*4882a593Smuzhiyun .n_pins = 0,
437*4882a593Smuzhiyun .pps = 0,
438*4882a593Smuzhiyun .adjfreq = cpts_ptp_adjfreq,
439*4882a593Smuzhiyun .adjtime = cpts_ptp_adjtime,
440*4882a593Smuzhiyun .gettimex64 = cpts_ptp_gettimeex,
441*4882a593Smuzhiyun .settime64 = cpts_ptp_settime,
442*4882a593Smuzhiyun .enable = cpts_ptp_enable,
443*4882a593Smuzhiyun .do_aux_work = cpts_overflow_check,
444*4882a593Smuzhiyun };
445*4882a593Smuzhiyun
cpts_skb_get_mtype_seqid(struct sk_buff * skb,u32 * mtype_seqid)446*4882a593Smuzhiyun static int cpts_skb_get_mtype_seqid(struct sk_buff *skb, u32 *mtype_seqid)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun unsigned int ptp_class = ptp_classify_raw(skb);
449*4882a593Smuzhiyun struct ptp_header *hdr;
450*4882a593Smuzhiyun u8 msgtype;
451*4882a593Smuzhiyun u16 seqid;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun if (ptp_class == PTP_CLASS_NONE)
454*4882a593Smuzhiyun return 0;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun hdr = ptp_parse_header(skb, ptp_class);
457*4882a593Smuzhiyun if (!hdr)
458*4882a593Smuzhiyun return 0;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun msgtype = ptp_get_msgtype(hdr, ptp_class);
461*4882a593Smuzhiyun seqid = ntohs(hdr->sequence_id);
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun *mtype_seqid = (msgtype & MESSAGE_TYPE_MASK) << MESSAGE_TYPE_SHIFT;
464*4882a593Smuzhiyun *mtype_seqid |= (seqid & SEQUENCE_ID_MASK) << SEQUENCE_ID_SHIFT;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun return 1;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
cpts_find_ts(struct cpts * cpts,struct sk_buff * skb,int ev_type,u32 skb_mtype_seqid)469*4882a593Smuzhiyun static u64 cpts_find_ts(struct cpts *cpts, struct sk_buff *skb,
470*4882a593Smuzhiyun int ev_type, u32 skb_mtype_seqid)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun struct list_head *this, *next;
473*4882a593Smuzhiyun struct cpts_event *event;
474*4882a593Smuzhiyun unsigned long flags;
475*4882a593Smuzhiyun u32 mtype_seqid;
476*4882a593Smuzhiyun u64 ns = 0;
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun cpts_fifo_read(cpts, -1);
479*4882a593Smuzhiyun spin_lock_irqsave(&cpts->lock, flags);
480*4882a593Smuzhiyun list_for_each_safe(this, next, &cpts->events) {
481*4882a593Smuzhiyun event = list_entry(this, struct cpts_event, list);
482*4882a593Smuzhiyun if (event_expired(event)) {
483*4882a593Smuzhiyun list_del_init(&event->list);
484*4882a593Smuzhiyun list_add(&event->list, &cpts->pool);
485*4882a593Smuzhiyun continue;
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun mtype_seqid = event->high &
489*4882a593Smuzhiyun ((MESSAGE_TYPE_MASK << MESSAGE_TYPE_SHIFT) |
490*4882a593Smuzhiyun (SEQUENCE_ID_MASK << SEQUENCE_ID_SHIFT) |
491*4882a593Smuzhiyun (EVENT_TYPE_MASK << EVENT_TYPE_SHIFT));
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun if (mtype_seqid == skb_mtype_seqid) {
494*4882a593Smuzhiyun ns = event->timestamp;
495*4882a593Smuzhiyun list_del_init(&event->list);
496*4882a593Smuzhiyun list_add(&event->list, &cpts->pool);
497*4882a593Smuzhiyun break;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun spin_unlock_irqrestore(&cpts->lock, flags);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun return ns;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
cpts_rx_timestamp(struct cpts * cpts,struct sk_buff * skb)505*4882a593Smuzhiyun void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb;
508*4882a593Smuzhiyun struct skb_shared_hwtstamps *ssh;
509*4882a593Smuzhiyun int ret;
510*4882a593Smuzhiyun u64 ns;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun /* cpts_rx_timestamp() is called before eth_type_trans(), so
513*4882a593Smuzhiyun * skb MAC Hdr properties are not configured yet. Hence need to
514*4882a593Smuzhiyun * reset skb MAC header here
515*4882a593Smuzhiyun */
516*4882a593Smuzhiyun skb_reset_mac_header(skb);
517*4882a593Smuzhiyun ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
518*4882a593Smuzhiyun if (!ret)
519*4882a593Smuzhiyun return;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun skb_cb->skb_mtype_seqid |= (CPTS_EV_RX << EVENT_TYPE_SHIFT);
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
524*4882a593Smuzhiyun __func__, skb_cb->skb_mtype_seqid);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun ns = cpts_find_ts(cpts, skb, CPTS_EV_RX, skb_cb->skb_mtype_seqid);
527*4882a593Smuzhiyun if (!ns)
528*4882a593Smuzhiyun return;
529*4882a593Smuzhiyun ssh = skb_hwtstamps(skb);
530*4882a593Smuzhiyun memset(ssh, 0, sizeof(*ssh));
531*4882a593Smuzhiyun ssh->hwtstamp = ns_to_ktime(ns);
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cpts_rx_timestamp);
534*4882a593Smuzhiyun
cpts_tx_timestamp(struct cpts * cpts,struct sk_buff * skb)535*4882a593Smuzhiyun void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun struct cpts_skb_cb_data *skb_cb = (struct cpts_skb_cb_data *)skb->cb;
538*4882a593Smuzhiyun int ret;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun if (!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
541*4882a593Smuzhiyun return;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun ret = cpts_skb_get_mtype_seqid(skb, &skb_cb->skb_mtype_seqid);
544*4882a593Smuzhiyun if (!ret)
545*4882a593Smuzhiyun return;
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun skb_cb->skb_mtype_seqid |= (CPTS_EV_TX << EVENT_TYPE_SHIFT);
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun dev_dbg(cpts->dev, "%s mtype seqid %08x\n",
550*4882a593Smuzhiyun __func__, skb_cb->skb_mtype_seqid);
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun /* Always defer TX TS processing to PTP worker */
553*4882a593Smuzhiyun skb_get(skb);
554*4882a593Smuzhiyun /* get the timestamp for timeouts */
555*4882a593Smuzhiyun skb_cb->tmo = jiffies + msecs_to_jiffies(CPTS_SKB_RX_TX_TMO);
556*4882a593Smuzhiyun skb_queue_tail(&cpts->txq, skb);
557*4882a593Smuzhiyun ptp_schedule_worker(cpts->clock, 0);
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cpts_tx_timestamp);
560*4882a593Smuzhiyun
cpts_register(struct cpts * cpts)561*4882a593Smuzhiyun int cpts_register(struct cpts *cpts)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun int err, i;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun skb_queue_head_init(&cpts->txq);
566*4882a593Smuzhiyun INIT_LIST_HEAD(&cpts->events);
567*4882a593Smuzhiyun INIT_LIST_HEAD(&cpts->pool);
568*4882a593Smuzhiyun for (i = 0; i < CPTS_MAX_EVENTS; i++)
569*4882a593Smuzhiyun list_add(&cpts->pool_data[i].list, &cpts->pool);
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun err = clk_enable(cpts->refclk);
572*4882a593Smuzhiyun if (err)
573*4882a593Smuzhiyun return err;
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun cpts_write32(cpts, CPTS_EN, control);
576*4882a593Smuzhiyun cpts_write32(cpts, TS_PEND_EN, int_enable);
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun timecounter_init(&cpts->tc, &cpts->cc, ktime_get_real_ns());
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun cpts->clock = ptp_clock_register(&cpts->info, cpts->dev);
581*4882a593Smuzhiyun if (IS_ERR(cpts->clock)) {
582*4882a593Smuzhiyun err = PTR_ERR(cpts->clock);
583*4882a593Smuzhiyun cpts->clock = NULL;
584*4882a593Smuzhiyun goto err_ptp;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun cpts->phc_index = ptp_clock_index(cpts->clock);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun ptp_schedule_worker(cpts->clock, cpts->ov_check_period);
589*4882a593Smuzhiyun return 0;
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun err_ptp:
592*4882a593Smuzhiyun clk_disable(cpts->refclk);
593*4882a593Smuzhiyun return err;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cpts_register);
596*4882a593Smuzhiyun
cpts_unregister(struct cpts * cpts)597*4882a593Smuzhiyun void cpts_unregister(struct cpts *cpts)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun if (WARN_ON(!cpts->clock))
600*4882a593Smuzhiyun return;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun ptp_clock_unregister(cpts->clock);
603*4882a593Smuzhiyun cpts->clock = NULL;
604*4882a593Smuzhiyun cpts->phc_index = -1;
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun cpts_write32(cpts, 0, int_enable);
607*4882a593Smuzhiyun cpts_write32(cpts, 0, control);
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun /* Drop all packet */
610*4882a593Smuzhiyun skb_queue_purge(&cpts->txq);
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun clk_disable(cpts->refclk);
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cpts_unregister);
615*4882a593Smuzhiyun
cpts_calc_mult_shift(struct cpts * cpts)616*4882a593Smuzhiyun static void cpts_calc_mult_shift(struct cpts *cpts)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun u64 frac, maxsec, ns;
619*4882a593Smuzhiyun u32 freq;
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun freq = clk_get_rate(cpts->refclk);
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun /* Calc the maximum number of seconds which we can run before
624*4882a593Smuzhiyun * wrapping around.
625*4882a593Smuzhiyun */
626*4882a593Smuzhiyun maxsec = cpts->cc.mask;
627*4882a593Smuzhiyun do_div(maxsec, freq);
628*4882a593Smuzhiyun /* limit conversation rate to 10 sec as higher values will produce
629*4882a593Smuzhiyun * too small mult factors and so reduce the conversion accuracy
630*4882a593Smuzhiyun */
631*4882a593Smuzhiyun if (maxsec > 10)
632*4882a593Smuzhiyun maxsec = 10;
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun /* Calc overflow check period (maxsec / 2) */
635*4882a593Smuzhiyun cpts->ov_check_period = (HZ * maxsec) / 2;
636*4882a593Smuzhiyun dev_info(cpts->dev, "cpts: overflow check period %lu (jiffies)\n",
637*4882a593Smuzhiyun cpts->ov_check_period);
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun if (cpts->cc.mult || cpts->cc.shift)
640*4882a593Smuzhiyun return;
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun clocks_calc_mult_shift(&cpts->cc.mult, &cpts->cc.shift,
643*4882a593Smuzhiyun freq, NSEC_PER_SEC, maxsec);
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun frac = 0;
646*4882a593Smuzhiyun ns = cyclecounter_cyc2ns(&cpts->cc, freq, cpts->cc.mask, &frac);
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun dev_info(cpts->dev,
649*4882a593Smuzhiyun "CPTS: ref_clk_freq:%u calc_mult:%u calc_shift:%u error:%lld nsec/sec\n",
650*4882a593Smuzhiyun freq, cpts->cc.mult, cpts->cc.shift, (ns - NSEC_PER_SEC));
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun
cpts_of_mux_clk_setup(struct cpts * cpts,struct device_node * node)653*4882a593Smuzhiyun static int cpts_of_mux_clk_setup(struct cpts *cpts, struct device_node *node)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun struct device_node *refclk_np;
656*4882a593Smuzhiyun const char **parent_names;
657*4882a593Smuzhiyun unsigned int num_parents;
658*4882a593Smuzhiyun struct clk_hw *clk_hw;
659*4882a593Smuzhiyun int ret = -EINVAL;
660*4882a593Smuzhiyun u32 *mux_table;
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun refclk_np = of_get_child_by_name(node, "cpts-refclk-mux");
663*4882a593Smuzhiyun if (!refclk_np)
664*4882a593Smuzhiyun /* refclk selection supported not for all SoCs */
665*4882a593Smuzhiyun return 0;
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun num_parents = of_clk_get_parent_count(refclk_np);
668*4882a593Smuzhiyun if (num_parents < 1) {
669*4882a593Smuzhiyun dev_err(cpts->dev, "mux-clock %s must have parents\n",
670*4882a593Smuzhiyun refclk_np->name);
671*4882a593Smuzhiyun goto mux_fail;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun parent_names = devm_kzalloc(cpts->dev, (sizeof(char *) * num_parents),
675*4882a593Smuzhiyun GFP_KERNEL);
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun mux_table = devm_kzalloc(cpts->dev, sizeof(*mux_table) * num_parents,
678*4882a593Smuzhiyun GFP_KERNEL);
679*4882a593Smuzhiyun if (!mux_table || !parent_names) {
680*4882a593Smuzhiyun ret = -ENOMEM;
681*4882a593Smuzhiyun goto mux_fail;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun of_clk_parent_fill(refclk_np, parent_names, num_parents);
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun ret = of_property_read_variable_u32_array(refclk_np, "ti,mux-tbl",
687*4882a593Smuzhiyun mux_table,
688*4882a593Smuzhiyun num_parents, num_parents);
689*4882a593Smuzhiyun if (ret < 0)
690*4882a593Smuzhiyun goto mux_fail;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun clk_hw = clk_hw_register_mux_table(cpts->dev, refclk_np->name,
693*4882a593Smuzhiyun parent_names, num_parents,
694*4882a593Smuzhiyun 0,
695*4882a593Smuzhiyun &cpts->reg->rftclk_sel, 0, 0x1F,
696*4882a593Smuzhiyun 0, mux_table, NULL);
697*4882a593Smuzhiyun if (IS_ERR(clk_hw)) {
698*4882a593Smuzhiyun ret = PTR_ERR(clk_hw);
699*4882a593Smuzhiyun goto mux_fail;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun ret = devm_add_action_or_reset(cpts->dev,
703*4882a593Smuzhiyun (void(*)(void *))clk_hw_unregister_mux,
704*4882a593Smuzhiyun clk_hw);
705*4882a593Smuzhiyun if (ret) {
706*4882a593Smuzhiyun dev_err(cpts->dev, "add clkmux unreg action %d", ret);
707*4882a593Smuzhiyun goto mux_fail;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun ret = of_clk_add_hw_provider(refclk_np, of_clk_hw_simple_get, clk_hw);
711*4882a593Smuzhiyun if (ret)
712*4882a593Smuzhiyun goto mux_fail;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun ret = devm_add_action_or_reset(cpts->dev,
715*4882a593Smuzhiyun (void(*)(void *))of_clk_del_provider,
716*4882a593Smuzhiyun refclk_np);
717*4882a593Smuzhiyun if (ret) {
718*4882a593Smuzhiyun dev_err(cpts->dev, "add clkmux provider unreg action %d", ret);
719*4882a593Smuzhiyun goto mux_fail;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun return ret;
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun mux_fail:
725*4882a593Smuzhiyun of_node_put(refclk_np);
726*4882a593Smuzhiyun return ret;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun
cpts_of_parse(struct cpts * cpts,struct device_node * node)729*4882a593Smuzhiyun static int cpts_of_parse(struct cpts *cpts, struct device_node *node)
730*4882a593Smuzhiyun {
731*4882a593Smuzhiyun int ret = -EINVAL;
732*4882a593Smuzhiyun u32 prop;
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun if (!of_property_read_u32(node, "cpts_clock_mult", &prop))
735*4882a593Smuzhiyun cpts->cc.mult = prop;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun if (!of_property_read_u32(node, "cpts_clock_shift", &prop))
738*4882a593Smuzhiyun cpts->cc.shift = prop;
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun if ((cpts->cc.mult && !cpts->cc.shift) ||
741*4882a593Smuzhiyun (!cpts->cc.mult && cpts->cc.shift))
742*4882a593Smuzhiyun goto of_error;
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun return cpts_of_mux_clk_setup(cpts, node);
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun of_error:
747*4882a593Smuzhiyun dev_err(cpts->dev, "CPTS: Missing property in the DT.\n");
748*4882a593Smuzhiyun return ret;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun
cpts_create(struct device * dev,void __iomem * regs,struct device_node * node,u32 n_ext_ts)751*4882a593Smuzhiyun struct cpts *cpts_create(struct device *dev, void __iomem *regs,
752*4882a593Smuzhiyun struct device_node *node, u32 n_ext_ts)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun struct cpts *cpts;
755*4882a593Smuzhiyun int ret;
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun cpts = devm_kzalloc(dev, sizeof(*cpts), GFP_KERNEL);
758*4882a593Smuzhiyun if (!cpts)
759*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun cpts->dev = dev;
762*4882a593Smuzhiyun cpts->reg = (struct cpsw_cpts __iomem *)regs;
763*4882a593Smuzhiyun cpts->irq_poll = true;
764*4882a593Smuzhiyun spin_lock_init(&cpts->lock);
765*4882a593Smuzhiyun mutex_init(&cpts->ptp_clk_mutex);
766*4882a593Smuzhiyun init_completion(&cpts->ts_push_complete);
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun ret = cpts_of_parse(cpts, node);
769*4882a593Smuzhiyun if (ret)
770*4882a593Smuzhiyun return ERR_PTR(ret);
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun cpts->refclk = devm_get_clk_from_child(dev, node, "cpts");
773*4882a593Smuzhiyun if (IS_ERR(cpts->refclk))
774*4882a593Smuzhiyun /* try get clk from dev node for compatibility */
775*4882a593Smuzhiyun cpts->refclk = devm_clk_get(dev, "cpts");
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun if (IS_ERR(cpts->refclk)) {
778*4882a593Smuzhiyun dev_err(dev, "Failed to get cpts refclk %ld\n",
779*4882a593Smuzhiyun PTR_ERR(cpts->refclk));
780*4882a593Smuzhiyun return ERR_CAST(cpts->refclk);
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun ret = clk_prepare(cpts->refclk);
784*4882a593Smuzhiyun if (ret)
785*4882a593Smuzhiyun return ERR_PTR(ret);
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun cpts->cc.read = cpts_systim_read;
788*4882a593Smuzhiyun cpts->cc.mask = CLOCKSOURCE_MASK(32);
789*4882a593Smuzhiyun cpts->info = cpts_info;
790*4882a593Smuzhiyun cpts->phc_index = -1;
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun if (n_ext_ts)
793*4882a593Smuzhiyun cpts->info.n_ext_ts = n_ext_ts;
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun cpts_calc_mult_shift(cpts);
796*4882a593Smuzhiyun /* save cc.mult original value as it can be modified
797*4882a593Smuzhiyun * by cpts_ptp_adjfreq().
798*4882a593Smuzhiyun */
799*4882a593Smuzhiyun cpts->cc_mult = cpts->cc.mult;
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun return cpts;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cpts_create);
804*4882a593Smuzhiyun
cpts_release(struct cpts * cpts)805*4882a593Smuzhiyun void cpts_release(struct cpts *cpts)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun if (!cpts)
808*4882a593Smuzhiyun return;
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun if (WARN_ON(!cpts->refclk))
811*4882a593Smuzhiyun return;
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun clk_unprepare(cpts->refclk);
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cpts_release);
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
818*4882a593Smuzhiyun MODULE_DESCRIPTION("TI CPTS driver");
819*4882a593Smuzhiyun MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
820