1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
3 */
4 #ifndef _SJA1105_PTP_H
5 #define _SJA1105_PTP_H
6
7 #include <linux/timer.h>
8
9 #if IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP)
10
11 /* Timestamps are in units of 8 ns clock ticks (equivalent to
12 * a fixed 125 MHz clock).
13 */
14 #define SJA1105_TICK_NS 8
15
ns_to_sja1105_ticks(s64 ns)16 static inline s64 ns_to_sja1105_ticks(s64 ns)
17 {
18 return ns / SJA1105_TICK_NS;
19 }
20
sja1105_ticks_to_ns(s64 ticks)21 static inline s64 sja1105_ticks_to_ns(s64 ticks)
22 {
23 return ticks * SJA1105_TICK_NS;
24 }
25
26 /* Calculate the first base_time in the future that satisfies this
27 * relationship:
28 *
29 * future_base_time = base_time + N x cycle_time >= now, or
30 *
31 * now - base_time
32 * N >= ---------------
33 * cycle_time
34 *
35 * Because N is an integer, the ceiling value of the above "a / b" ratio
36 * is in fact precisely the floor value of "(a + b - 1) / b", which is
37 * easier to calculate only having integer division tools.
38 */
future_base_time(s64 base_time,s64 cycle_time,s64 now)39 static inline s64 future_base_time(s64 base_time, s64 cycle_time, s64 now)
40 {
41 s64 a, b, n;
42
43 if (base_time >= now)
44 return base_time;
45
46 a = now - base_time;
47 b = cycle_time;
48 n = div_s64(a + b - 1, b);
49
50 return base_time + n * cycle_time;
51 }
52
53 /* This is not a preprocessor macro because the "ns" argument may or may not be
54 * s64 at caller side. This ensures it is properly type-cast before div_s64.
55 */
ns_to_sja1105_delta(s64 ns)56 static inline s64 ns_to_sja1105_delta(s64 ns)
57 {
58 return div_s64(ns, 200);
59 }
60
sja1105_delta_to_ns(s64 delta)61 static inline s64 sja1105_delta_to_ns(s64 delta)
62 {
63 return delta * 200;
64 }
65
66 struct sja1105_ptp_cmd {
67 u64 startptpcp; /* start toggling PTP_CLK pin */
68 u64 stopptpcp; /* stop toggling PTP_CLK pin */
69 u64 ptpstrtsch; /* start schedule */
70 u64 ptpstopsch; /* stop schedule */
71 u64 resptp; /* reset */
72 u64 corrclk4ts; /* use the corrected clock for timestamps */
73 u64 ptpclkadd; /* enum sja1105_ptp_clk_mode */
74 };
75
76 struct sja1105_ptp_data {
77 struct timer_list extts_timer;
78 /* Used only on SJA1105 to reconstruct partial timestamps */
79 struct sk_buff_head skb_rxtstamp_queue;
80 /* Used on SJA1110 where meta frames are generated only for
81 * 2-step TX timestamps
82 */
83 struct sk_buff_head skb_txtstamp_queue;
84 struct ptp_clock_info caps;
85 struct ptp_clock *clock;
86 struct sja1105_ptp_cmd cmd;
87 /* Serializes all operations on the PTP hardware clock */
88 struct mutex lock;
89 bool extts_enabled;
90 u64 ptpsyncts;
91 };
92
93 int sja1105_ptp_clock_register(struct dsa_switch *ds);
94
95 void sja1105_ptp_clock_unregister(struct dsa_switch *ds);
96
97 void sja1105et_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
98 enum packing_op op);
99
100 void sja1105pqrs_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
101 enum packing_op op);
102
103 int sja1105_get_ts_info(struct dsa_switch *ds, int port,
104 struct kernel_ethtool_ts_info *ts);
105
106 void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
107 struct sk_buff *clone);
108
109 bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
110 struct sk_buff *skb, unsigned int type);
111
112 void sja1105_port_txtstamp(struct dsa_switch *ds, int port,
113 struct sk_buff *skb);
114
115 int sja1105_hwtstamp_get(struct dsa_switch *ds, int port,
116 struct kernel_hwtstamp_config *config);
117
118 int sja1105_hwtstamp_set(struct dsa_switch *ds, int port,
119 struct kernel_hwtstamp_config *config,
120 struct netlink_ext_ack *extack);
121
122 int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
123 struct ptp_system_timestamp *sts);
124
125 int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
126 struct ptp_system_timestamp *ptp_sts);
127
128 int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta);
129
130 int sja1105_ptp_commit(struct dsa_switch *ds, struct sja1105_ptp_cmd *cmd,
131 sja1105_spi_rw_mode_t rw);
132
133 bool sja1105_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
134 bool sja1110_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
135 void sja1110_txtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
136
137 void sja1110_process_meta_tstamp(struct dsa_switch *ds, int port, u8 ts_id,
138 enum sja1110_meta_tstamp dir, u64 tstamp);
139
140 #else
141
142 struct sja1105_ptp_cmd;
143
144 /* Structures cannot be empty in C. Bah!
145 * Keep the mutex as the only element, which is a bit more difficult to
146 * refactor out of sja1105_main.c anyway.
147 */
148 struct sja1105_ptp_data {
149 struct mutex lock;
150 };
151
sja1105_ptp_clock_register(struct dsa_switch * ds)152 static inline int sja1105_ptp_clock_register(struct dsa_switch *ds)
153 {
154 return 0;
155 }
156
sja1105_ptp_clock_unregister(struct dsa_switch * ds)157 static inline void sja1105_ptp_clock_unregister(struct dsa_switch *ds) { }
158
sja1105_ptp_txtstamp_skb(struct dsa_switch * ds,int slot,struct sk_buff * clone)159 static inline void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
160 struct sk_buff *clone)
161 {
162 }
163
__sja1105_ptp_gettimex(struct dsa_switch * ds,u64 * ns,struct ptp_system_timestamp * sts)164 static inline int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
165 struct ptp_system_timestamp *sts)
166 {
167 return 0;
168 }
169
__sja1105_ptp_settime(struct dsa_switch * ds,u64 ns,struct ptp_system_timestamp * ptp_sts)170 static inline int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
171 struct ptp_system_timestamp *ptp_sts)
172 {
173 return 0;
174 }
175
__sja1105_ptp_adjtime(struct dsa_switch * ds,s64 delta)176 static inline int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta)
177 {
178 return 0;
179 }
180
sja1105_ptp_commit(struct dsa_switch * ds,struct sja1105_ptp_cmd * cmd,sja1105_spi_rw_mode_t rw)181 static inline int sja1105_ptp_commit(struct dsa_switch *ds,
182 struct sja1105_ptp_cmd *cmd,
183 sja1105_spi_rw_mode_t rw)
184 {
185 return 0;
186 }
187
188 #define sja1105et_ptp_cmd_packing NULL
189
190 #define sja1105pqrs_ptp_cmd_packing NULL
191
192 #define sja1105_get_ts_info NULL
193
194 #define sja1105_port_rxtstamp NULL
195
196 #define sja1105_port_txtstamp NULL
197
198 #define sja1105_hwtstamp_get NULL
199
200 #define sja1105_hwtstamp_set NULL
201
202 #define sja1105_rxtstamp NULL
203 #define sja1110_rxtstamp NULL
204 #define sja1110_txtstamp NULL
205
206 #define sja1110_process_meta_tstamp NULL
207
208 #endif /* IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP) */
209
210 #endif /* _SJA1105_PTP_H */
211