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 #ifndef _TI_CPTS_H_
9*4882a593Smuzhiyun #define _TI_CPTS_H_
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_TI_CPTS)
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/clk.h>
14*4882a593Smuzhiyun #include <linux/clkdev.h>
15*4882a593Smuzhiyun #include <linux/clocksource.h>
16*4882a593Smuzhiyun #include <linux/device.h>
17*4882a593Smuzhiyun #include <linux/list.h>
18*4882a593Smuzhiyun #include <linux/of.h>
19*4882a593Smuzhiyun #include <linux/ptp_clock_kernel.h>
20*4882a593Smuzhiyun #include <linux/skbuff.h>
21*4882a593Smuzhiyun #include <linux/ptp_classify.h>
22*4882a593Smuzhiyun #include <linux/timecounter.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun struct cpsw_cpts {
25*4882a593Smuzhiyun u32 idver; /* Identification and version */
26*4882a593Smuzhiyun u32 control; /* Time sync control */
27*4882a593Smuzhiyun u32 rftclk_sel; /* Reference Clock Select Register */
28*4882a593Smuzhiyun u32 ts_push; /* Time stamp event push */
29*4882a593Smuzhiyun u32 ts_load_val; /* Time stamp load value */
30*4882a593Smuzhiyun u32 ts_load_en; /* Time stamp load enable */
31*4882a593Smuzhiyun u32 res2[2];
32*4882a593Smuzhiyun u32 intstat_raw; /* Time sync interrupt status raw */
33*4882a593Smuzhiyun u32 intstat_masked; /* Time sync interrupt status masked */
34*4882a593Smuzhiyun u32 int_enable; /* Time sync interrupt enable */
35*4882a593Smuzhiyun u32 res3;
36*4882a593Smuzhiyun u32 event_pop; /* Event interrupt pop */
37*4882a593Smuzhiyun u32 event_low; /* 32 Bit Event Time Stamp */
38*4882a593Smuzhiyun u32 event_high; /* Event Type Fields */
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /* Bit definitions for the IDVER register */
42*4882a593Smuzhiyun #define TX_IDENT_SHIFT (16) /* TX Identification Value */
43*4882a593Smuzhiyun #define TX_IDENT_MASK (0xffff)
44*4882a593Smuzhiyun #define RTL_VER_SHIFT (11) /* RTL Version Value */
45*4882a593Smuzhiyun #define RTL_VER_MASK (0x1f)
46*4882a593Smuzhiyun #define MAJOR_VER_SHIFT (8) /* Major Version Value */
47*4882a593Smuzhiyun #define MAJOR_VER_MASK (0x7)
48*4882a593Smuzhiyun #define MINOR_VER_SHIFT (0) /* Minor Version Value */
49*4882a593Smuzhiyun #define MINOR_VER_MASK (0xff)
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /* Bit definitions for the CONTROL register */
52*4882a593Smuzhiyun #define HW4_TS_PUSH_EN (1<<11) /* Hardware push 4 enable */
53*4882a593Smuzhiyun #define HW3_TS_PUSH_EN (1<<10) /* Hardware push 3 enable */
54*4882a593Smuzhiyun #define HW2_TS_PUSH_EN (1<<9) /* Hardware push 2 enable */
55*4882a593Smuzhiyun #define HW1_TS_PUSH_EN (1<<8) /* Hardware push 1 enable */
56*4882a593Smuzhiyun #define INT_TEST (1<<1) /* Interrupt Test */
57*4882a593Smuzhiyun #define CPTS_EN (1<<0) /* Time Sync Enable */
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * Definitions for the single bit resisters:
61*4882a593Smuzhiyun * TS_PUSH TS_LOAD_EN INTSTAT_RAW INTSTAT_MASKED INT_ENABLE EVENT_POP
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun #define TS_PUSH (1<<0) /* Time stamp event push */
64*4882a593Smuzhiyun #define TS_LOAD_EN (1<<0) /* Time Stamp Load */
65*4882a593Smuzhiyun #define TS_PEND_RAW (1<<0) /* int read (before enable) */
66*4882a593Smuzhiyun #define TS_PEND (1<<0) /* masked interrupt read (after enable) */
67*4882a593Smuzhiyun #define TS_PEND_EN (1<<0) /* masked interrupt enable */
68*4882a593Smuzhiyun #define EVENT_POP (1<<0) /* writing discards one event */
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Bit definitions for the EVENT_HIGH register */
71*4882a593Smuzhiyun #define PORT_NUMBER_SHIFT (24) /* Indicates Ethernet port or HW pin */
72*4882a593Smuzhiyun #define PORT_NUMBER_MASK (0x1f)
73*4882a593Smuzhiyun #define EVENT_TYPE_SHIFT (20) /* Time sync event type */
74*4882a593Smuzhiyun #define EVENT_TYPE_MASK (0xf)
75*4882a593Smuzhiyun #define MESSAGE_TYPE_SHIFT (16) /* PTP message type */
76*4882a593Smuzhiyun #define MESSAGE_TYPE_MASK (0xf)
77*4882a593Smuzhiyun #define SEQUENCE_ID_SHIFT (0) /* PTP message sequence ID */
78*4882a593Smuzhiyun #define SEQUENCE_ID_MASK (0xffff)
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun enum {
81*4882a593Smuzhiyun CPTS_EV_PUSH, /* Time Stamp Push Event */
82*4882a593Smuzhiyun CPTS_EV_ROLL, /* Time Stamp Rollover Event */
83*4882a593Smuzhiyun CPTS_EV_HALF, /* Time Stamp Half Rollover Event */
84*4882a593Smuzhiyun CPTS_EV_HW, /* Hardware Time Stamp Push Event */
85*4882a593Smuzhiyun CPTS_EV_RX, /* Ethernet Receive Event */
86*4882a593Smuzhiyun CPTS_EV_TX, /* Ethernet Transmit Event */
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun #define CPTS_FIFO_DEPTH 16
90*4882a593Smuzhiyun #define CPTS_MAX_EVENTS 32
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun struct cpts_event {
93*4882a593Smuzhiyun struct list_head list;
94*4882a593Smuzhiyun unsigned long tmo;
95*4882a593Smuzhiyun u32 high;
96*4882a593Smuzhiyun u32 low;
97*4882a593Smuzhiyun u64 timestamp;
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun struct cpts {
101*4882a593Smuzhiyun struct device *dev;
102*4882a593Smuzhiyun struct cpsw_cpts __iomem *reg;
103*4882a593Smuzhiyun int tx_enable;
104*4882a593Smuzhiyun int rx_enable;
105*4882a593Smuzhiyun struct ptp_clock_info info;
106*4882a593Smuzhiyun struct ptp_clock *clock;
107*4882a593Smuzhiyun spinlock_t lock; /* protects fifo/events */
108*4882a593Smuzhiyun u32 cc_mult; /* for the nominal frequency */
109*4882a593Smuzhiyun struct cyclecounter cc;
110*4882a593Smuzhiyun struct timecounter tc;
111*4882a593Smuzhiyun int phc_index;
112*4882a593Smuzhiyun struct clk *refclk;
113*4882a593Smuzhiyun struct list_head events;
114*4882a593Smuzhiyun struct list_head pool;
115*4882a593Smuzhiyun struct cpts_event pool_data[CPTS_MAX_EVENTS];
116*4882a593Smuzhiyun unsigned long ov_check_period;
117*4882a593Smuzhiyun struct sk_buff_head txq;
118*4882a593Smuzhiyun u64 cur_timestamp;
119*4882a593Smuzhiyun u32 mult_new;
120*4882a593Smuzhiyun struct mutex ptp_clk_mutex; /* sync PTP interface and worker */
121*4882a593Smuzhiyun bool irq_poll;
122*4882a593Smuzhiyun struct completion ts_push_complete;
123*4882a593Smuzhiyun u32 hw_ts_enable;
124*4882a593Smuzhiyun };
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb);
127*4882a593Smuzhiyun void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb);
128*4882a593Smuzhiyun int cpts_register(struct cpts *cpts);
129*4882a593Smuzhiyun void cpts_unregister(struct cpts *cpts);
130*4882a593Smuzhiyun struct cpts *cpts_create(struct device *dev, void __iomem *regs,
131*4882a593Smuzhiyun struct device_node *node, u32 n_ext_ts);
132*4882a593Smuzhiyun void cpts_release(struct cpts *cpts);
133*4882a593Smuzhiyun void cpts_misc_interrupt(struct cpts *cpts);
134*4882a593Smuzhiyun
cpts_can_timestamp(struct cpts * cpts,struct sk_buff * skb)135*4882a593Smuzhiyun static inline bool cpts_can_timestamp(struct cpts *cpts, struct sk_buff *skb)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun unsigned int class = ptp_classify_raw(skb);
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun if (class == PTP_CLASS_NONE)
140*4882a593Smuzhiyun return false;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun return true;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun
cpts_set_irqpoll(struct cpts * cpts,bool en)145*4882a593Smuzhiyun static inline void cpts_set_irqpoll(struct cpts *cpts, bool en)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun cpts->irq_poll = en;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun #else
151*4882a593Smuzhiyun struct cpts;
152*4882a593Smuzhiyun
cpts_rx_timestamp(struct cpts * cpts,struct sk_buff * skb)153*4882a593Smuzhiyun static inline void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun }
cpts_tx_timestamp(struct cpts * cpts,struct sk_buff * skb)156*4882a593Smuzhiyun static inline void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun static inline
cpts_create(struct device * dev,void __iomem * regs,struct device_node * node,u32 n_ext_ts)161*4882a593Smuzhiyun struct cpts *cpts_create(struct device *dev, void __iomem *regs,
162*4882a593Smuzhiyun struct device_node *node, u32 n_ext_ts)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun return NULL;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
cpts_release(struct cpts * cpts)167*4882a593Smuzhiyun static inline void cpts_release(struct cpts *cpts)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun static inline int
cpts_register(struct cpts * cpts)172*4882a593Smuzhiyun cpts_register(struct cpts *cpts)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun return 0;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
cpts_unregister(struct cpts * cpts)177*4882a593Smuzhiyun static inline void cpts_unregister(struct cpts *cpts)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
cpts_can_timestamp(struct cpts * cpts,struct sk_buff * skb)181*4882a593Smuzhiyun static inline bool cpts_can_timestamp(struct cpts *cpts, struct sk_buff *skb)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun return false;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
cpts_misc_interrupt(struct cpts * cpts)186*4882a593Smuzhiyun static inline void cpts_misc_interrupt(struct cpts *cpts)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
cpts_set_irqpoll(struct cpts * cpts,bool en)190*4882a593Smuzhiyun static inline void cpts_set_irqpoll(struct cpts *cpts, bool en)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun #endif
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun #endif
197