xref: /linux/drivers/net/dsa/sja1105/sja1105_ptp.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1b790b554SNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
2b790b554SNishad Kamdar /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
3bb77f36aSVladimir Oltean  */
4bb77f36aSVladimir Oltean #ifndef _SJA1105_PTP_H
5bb77f36aSVladimir Oltean #define _SJA1105_PTP_H
6bb77f36aSVladimir Oltean 
7af9fdd2bSVladimir Oltean #include <linux/timer.h>
8af9fdd2bSVladimir Oltean 
9bb77f36aSVladimir Oltean #if IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP)
10bb77f36aSVladimir Oltean 
11fcbf979aSVladimir Oltean /* Timestamps are in units of 8 ns clock ticks (equivalent to
12fcbf979aSVladimir Oltean  * a fixed 125 MHz clock).
13fcbf979aSVladimir Oltean  */
14fcbf979aSVladimir Oltean #define SJA1105_TICK_NS			8
15fcbf979aSVladimir Oltean 
ns_to_sja1105_ticks(s64 ns)16fcbf979aSVladimir Oltean static inline s64 ns_to_sja1105_ticks(s64 ns)
17fcbf979aSVladimir Oltean {
18fcbf979aSVladimir Oltean 	return ns / SJA1105_TICK_NS;
19fcbf979aSVladimir Oltean }
20fcbf979aSVladimir Oltean 
sja1105_ticks_to_ns(s64 ticks)21fcbf979aSVladimir Oltean static inline s64 sja1105_ticks_to_ns(s64 ticks)
22fcbf979aSVladimir Oltean {
23fcbf979aSVladimir Oltean 	return ticks * SJA1105_TICK_NS;
24fcbf979aSVladimir Oltean }
25fcbf979aSVladimir Oltean 
264bfa1616SVladimir Oltean /* Calculate the first base_time in the future that satisfies this
274bfa1616SVladimir Oltean  * relationship:
284bfa1616SVladimir Oltean  *
294bfa1616SVladimir Oltean  * future_base_time = base_time + N x cycle_time >= now, or
304bfa1616SVladimir Oltean  *
314bfa1616SVladimir Oltean  *      now - base_time
324bfa1616SVladimir Oltean  * N >= ---------------
334bfa1616SVladimir Oltean  *         cycle_time
344bfa1616SVladimir Oltean  *
354bfa1616SVladimir Oltean  * Because N is an integer, the ceiling value of the above "a / b" ratio
364bfa1616SVladimir Oltean  * is in fact precisely the floor value of "(a + b - 1) / b", which is
374bfa1616SVladimir Oltean  * easier to calculate only having integer division tools.
384bfa1616SVladimir Oltean  */
future_base_time(s64 base_time,s64 cycle_time,s64 now)394bfa1616SVladimir Oltean static inline s64 future_base_time(s64 base_time, s64 cycle_time, s64 now)
404bfa1616SVladimir Oltean {
414bfa1616SVladimir Oltean 	s64 a, b, n;
424bfa1616SVladimir Oltean 
434bfa1616SVladimir Oltean 	if (base_time >= now)
444bfa1616SVladimir Oltean 		return base_time;
454bfa1616SVladimir Oltean 
464bfa1616SVladimir Oltean 	a = now - base_time;
474bfa1616SVladimir Oltean 	b = cycle_time;
484bfa1616SVladimir Oltean 	n = div_s64(a + b - 1, b);
494bfa1616SVladimir Oltean 
504bfa1616SVladimir Oltean 	return base_time + n * cycle_time;
514bfa1616SVladimir Oltean }
524bfa1616SVladimir Oltean 
53834f8933SVladimir Oltean /* This is not a preprocessor macro because the "ns" argument may or may not be
54834f8933SVladimir Oltean  * s64 at caller side. This ensures it is properly type-cast before div_s64.
55834f8933SVladimir Oltean  */
ns_to_sja1105_delta(s64 ns)56834f8933SVladimir Oltean static inline s64 ns_to_sja1105_delta(s64 ns)
57834f8933SVladimir Oltean {
58834f8933SVladimir Oltean 	return div_s64(ns, 200);
59834f8933SVladimir Oltean }
60834f8933SVladimir Oltean 
sja1105_delta_to_ns(s64 delta)61834f8933SVladimir Oltean static inline s64 sja1105_delta_to_ns(s64 delta)
62834f8933SVladimir Oltean {
63834f8933SVladimir Oltean 	return delta * 200;
64834f8933SVladimir Oltean }
65834f8933SVladimir Oltean 
6666427778SVladimir Oltean struct sja1105_ptp_cmd {
67747e5eb3SVladimir Oltean 	u64 startptpcp;		/* start toggling PTP_CLK pin */
68747e5eb3SVladimir Oltean 	u64 stopptpcp;		/* stop toggling PTP_CLK pin */
6986db36a3SVladimir Oltean 	u64 ptpstrtsch;		/* start schedule */
7086db36a3SVladimir Oltean 	u64 ptpstopsch;		/* stop schedule */
7166427778SVladimir Oltean 	u64 resptp;		/* reset */
722fb079a2SVladimir Oltean 	u64 corrclk4ts;		/* use the corrected clock for timestamps */
732fb079a2SVladimir Oltean 	u64 ptpclkadd;		/* enum sja1105_ptp_clk_mode */
7466427778SVladimir Oltean };
7566427778SVladimir Oltean 
76a9d6ed7aSVladimir Oltean struct sja1105_ptp_data {
77af9fdd2bSVladimir Oltean 	struct timer_list extts_timer;
78566b18c8SVladimir Oltean 	/* Used only on SJA1105 to reconstruct partial timestamps */
791e762bd2SVladimir Oltean 	struct sk_buff_head skb_rxtstamp_queue;
80fcbf979aSVladimir Oltean 	/* Used on SJA1110 where meta frames are generated only for
81fcbf979aSVladimir Oltean 	 * 2-step TX timestamps
82fcbf979aSVladimir Oltean 	 */
83fcbf979aSVladimir Oltean 	struct sk_buff_head skb_txtstamp_queue;
84a9d6ed7aSVladimir Oltean 	struct ptp_clock_info caps;
85a9d6ed7aSVladimir Oltean 	struct ptp_clock *clock;
8666427778SVladimir Oltean 	struct sja1105_ptp_cmd cmd;
872fb079a2SVladimir Oltean 	/* Serializes all operations on the PTP hardware clock */
88a9d6ed7aSVladimir Oltean 	struct mutex lock;
89af9fdd2bSVladimir Oltean 	bool extts_enabled;
90747e5eb3SVladimir Oltean 	u64 ptpsyncts;
91a9d6ed7aSVladimir Oltean };
92a9d6ed7aSVladimir Oltean 
9361c77126SVladimir Oltean int sja1105_ptp_clock_register(struct dsa_switch *ds);
94bb77f36aSVladimir Oltean 
9561c77126SVladimir Oltean void sja1105_ptp_clock_unregister(struct dsa_switch *ds);
96bb77f36aSVladimir Oltean 
9741603d78SVladimir Oltean void sja1105et_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
9841603d78SVladimir Oltean 			       enum packing_op op);
99bb77f36aSVladimir Oltean 
10041603d78SVladimir Oltean void sja1105pqrs_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
10141603d78SVladimir Oltean 				 enum packing_op op);
102bb77f36aSVladimir Oltean 
103bb77f36aSVladimir Oltean int sja1105_get_ts_info(struct dsa_switch *ds, int port,
104*2111375bSKory Maincent 			struct kernel_ethtool_ts_info *ts);
105bb77f36aSVladimir Oltean 
106a9d6ed7aSVladimir Oltean void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
107a9d6ed7aSVladimir Oltean 			      struct sk_buff *clone);
10847ed985eSVladimir Oltean 
109a9d6ed7aSVladimir Oltean bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
110a9d6ed7aSVladimir Oltean 			   struct sk_buff *skb, unsigned int type);
111a9d6ed7aSVladimir Oltean 
1125c5416f5SYangbo Lu void sja1105_port_txtstamp(struct dsa_switch *ds, int port,
113cf536ea3SYangbo Lu 			   struct sk_buff *skb);
114a9d6ed7aSVladimir Oltean 
115a9d6ed7aSVladimir Oltean int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
116a9d6ed7aSVladimir Oltean 
117a9d6ed7aSVladimir Oltean int sja1105_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
118a9d6ed7aSVladimir Oltean 
1196cf99c13SVladimir Oltean int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
1206cf99c13SVladimir Oltean 			   struct ptp_system_timestamp *sts);
1216cf99c13SVladimir Oltean 
1226cf99c13SVladimir Oltean int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
1236cf99c13SVladimir Oltean 			  struct ptp_system_timestamp *ptp_sts);
1246cf99c13SVladimir Oltean 
1256cf99c13SVladimir Oltean int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta);
1266cf99c13SVladimir Oltean 
12786db36a3SVladimir Oltean int sja1105_ptp_commit(struct dsa_switch *ds, struct sja1105_ptp_cmd *cmd,
12886db36a3SVladimir Oltean 		       sja1105_spi_rw_mode_t rw);
12986db36a3SVladimir Oltean 
13030b73242SVladimir Oltean bool sja1105_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
13130b73242SVladimir Oltean bool sja1110_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
132566b18c8SVladimir Oltean void sja1110_txtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
13330b73242SVladimir Oltean 
134fcbf979aSVladimir Oltean void sja1110_process_meta_tstamp(struct dsa_switch *ds, int port, u8 ts_id,
135fcbf979aSVladimir Oltean 				 enum sja1110_meta_tstamp dir, u64 tstamp);
136fcbf979aSVladimir Oltean 
137bb77f36aSVladimir Oltean #else
138bb77f36aSVladimir Oltean 
13966427778SVladimir Oltean struct sja1105_ptp_cmd;
14066427778SVladimir Oltean 
141a9d6ed7aSVladimir Oltean /* Structures cannot be empty in C. Bah!
142a9d6ed7aSVladimir Oltean  * Keep the mutex as the only element, which is a bit more difficult to
143a9d6ed7aSVladimir Oltean  * refactor out of sja1105_main.c anyway.
144a9d6ed7aSVladimir Oltean  */
145a9d6ed7aSVladimir Oltean struct sja1105_ptp_data {
146a9d6ed7aSVladimir Oltean 	struct mutex lock;
147a9d6ed7aSVladimir Oltean };
148a9d6ed7aSVladimir Oltean 
sja1105_ptp_clock_register(struct dsa_switch * ds)14961c77126SVladimir Oltean static inline int sja1105_ptp_clock_register(struct dsa_switch *ds)
150bb77f36aSVladimir Oltean {
151bb77f36aSVladimir Oltean 	return 0;
152bb77f36aSVladimir Oltean }
153bb77f36aSVladimir Oltean 
sja1105_ptp_clock_unregister(struct dsa_switch * ds)15461c77126SVladimir Oltean static inline void sja1105_ptp_clock_unregister(struct dsa_switch *ds) { }
155bb77f36aSVladimir Oltean 
sja1105_ptp_txtstamp_skb(struct dsa_switch * ds,int slot,struct sk_buff * clone)156a9d6ed7aSVladimir Oltean static inline void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
157a9d6ed7aSVladimir Oltean 					    struct sk_buff *clone)
15847ed985eSVladimir Oltean {
15947ed985eSVladimir Oltean }
16047ed985eSVladimir Oltean 
__sja1105_ptp_gettimex(struct dsa_switch * ds,u64 * ns,struct ptp_system_timestamp * sts)1616cf99c13SVladimir Oltean static inline int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
1626cf99c13SVladimir Oltean 					 struct ptp_system_timestamp *sts)
1636cf99c13SVladimir Oltean {
1646cf99c13SVladimir Oltean 	return 0;
1656cf99c13SVladimir Oltean }
1666cf99c13SVladimir Oltean 
__sja1105_ptp_settime(struct dsa_switch * ds,u64 ns,struct ptp_system_timestamp * ptp_sts)1676cf99c13SVladimir Oltean static inline int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
1686cf99c13SVladimir Oltean 					struct ptp_system_timestamp *ptp_sts)
1696cf99c13SVladimir Oltean {
1706cf99c13SVladimir Oltean 	return 0;
1716cf99c13SVladimir Oltean }
1726cf99c13SVladimir Oltean 
__sja1105_ptp_adjtime(struct dsa_switch * ds,s64 delta)1736cf99c13SVladimir Oltean static inline int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta)
174bb77f36aSVladimir Oltean {
175bb77f36aSVladimir Oltean 	return 0;
176bb77f36aSVladimir Oltean }
177bb77f36aSVladimir Oltean 
sja1105_ptp_commit(struct dsa_switch * ds,struct sja1105_ptp_cmd * cmd,sja1105_spi_rw_mode_t rw)17886db36a3SVladimir Oltean static inline int sja1105_ptp_commit(struct dsa_switch *ds,
17986db36a3SVladimir Oltean 				     struct sja1105_ptp_cmd *cmd,
18086db36a3SVladimir Oltean 				     sja1105_spi_rw_mode_t rw)
18186db36a3SVladimir Oltean {
18286db36a3SVladimir Oltean 	return 0;
18386db36a3SVladimir Oltean }
18486db36a3SVladimir Oltean 
18541603d78SVladimir Oltean #define sja1105et_ptp_cmd_packing NULL
186bb77f36aSVladimir Oltean 
18741603d78SVladimir Oltean #define sja1105pqrs_ptp_cmd_packing NULL
188bb77f36aSVladimir Oltean 
189bb77f36aSVladimir Oltean #define sja1105_get_ts_info NULL
190bb77f36aSVladimir Oltean 
191a9d6ed7aSVladimir Oltean #define sja1105_port_rxtstamp NULL
192a9d6ed7aSVladimir Oltean 
193a9d6ed7aSVladimir Oltean #define sja1105_port_txtstamp NULL
194a9d6ed7aSVladimir Oltean 
195a9d6ed7aSVladimir Oltean #define sja1105_hwtstamp_get NULL
196a9d6ed7aSVladimir Oltean 
197a9d6ed7aSVladimir Oltean #define sja1105_hwtstamp_set NULL
198a9d6ed7aSVladimir Oltean 
19930b73242SVladimir Oltean #define sja1105_rxtstamp NULL
20030b73242SVladimir Oltean #define sja1110_rxtstamp NULL
201566b18c8SVladimir Oltean #define sja1110_txtstamp NULL
20230b73242SVladimir Oltean 
203fcbf979aSVladimir Oltean #define sja1110_process_meta_tstamp NULL
204fcbf979aSVladimir Oltean 
205bb77f36aSVladimir Oltean #endif /* IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP) */
206bb77f36aSVladimir Oltean 
207bb77f36aSVladimir Oltean #endif /* _SJA1105_PTP_H */
208