WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / dsa / sja1105 / sja1105_ptp.h
blob3daa33e98e77bcf613203dc7d42c187e02ca237f
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
7 #include <linux/timer.h>
9 #if IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP)
11 /* Timestamps are in units of 8 ns clock ticks (equivalent to
12 * a fixed 125 MHz clock).
14 #define SJA1105_TICK_NS 8
16 static inline s64 ns_to_sja1105_ticks(s64 ns)
18 return ns / SJA1105_TICK_NS;
21 static inline s64 sja1105_ticks_to_ns(s64 ticks)
23 return ticks * SJA1105_TICK_NS;
26 /* Calculate the first base_time in the future that satisfies this
27 * relationship:
29 * future_base_time = base_time + N x cycle_time >= now, or
31 * now - base_time
32 * N >= ---------------
33 * cycle_time
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.
39 static inline s64 future_base_time(s64 base_time, s64 cycle_time, s64 now)
41 s64 a, b, n;
43 if (base_time >= now)
44 return base_time;
46 a = now - base_time;
47 b = cycle_time;
48 n = div_s64(a + b - 1, b);
50 return base_time + n * cycle_time;
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.
56 static inline s64 ns_to_sja1105_delta(s64 ns)
58 return div_s64(ns, 200);
61 static inline s64 sja1105_delta_to_ns(s64 delta)
63 return delta * 200;
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 */
76 struct sja1105_ptp_data {
77 struct timer_list extts_timer;
78 struct sk_buff_head skb_rxtstamp_queue;
79 struct ptp_clock_info caps;
80 struct ptp_clock *clock;
81 struct sja1105_ptp_cmd cmd;
82 /* Serializes all operations on the PTP hardware clock */
83 struct mutex lock;
84 bool extts_enabled;
85 u64 ptpsyncts;
88 int sja1105_ptp_clock_register(struct dsa_switch *ds);
90 void sja1105_ptp_clock_unregister(struct dsa_switch *ds);
92 void sja1105et_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
93 enum packing_op op);
95 void sja1105pqrs_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
96 enum packing_op op);
98 int sja1105_get_ts_info(struct dsa_switch *ds, int port,
99 struct ethtool_ts_info *ts);
101 void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
102 struct sk_buff *clone);
104 bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
105 struct sk_buff *skb, unsigned int type);
107 bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
108 struct sk_buff *skb, unsigned int type);
110 int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
112 int sja1105_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
114 int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
115 struct ptp_system_timestamp *sts);
117 int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
118 struct ptp_system_timestamp *ptp_sts);
120 int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta);
122 int sja1105_ptp_commit(struct dsa_switch *ds, struct sja1105_ptp_cmd *cmd,
123 sja1105_spi_rw_mode_t rw);
125 #else
127 struct sja1105_ptp_cmd;
129 /* Structures cannot be empty in C. Bah!
130 * Keep the mutex as the only element, which is a bit more difficult to
131 * refactor out of sja1105_main.c anyway.
133 struct sja1105_ptp_data {
134 struct mutex lock;
137 static inline int sja1105_ptp_clock_register(struct dsa_switch *ds)
139 return 0;
142 static inline void sja1105_ptp_clock_unregister(struct dsa_switch *ds) { }
144 static inline void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
145 struct sk_buff *clone)
149 static inline int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
150 struct ptp_system_timestamp *sts)
152 return 0;
155 static inline int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
156 struct ptp_system_timestamp *ptp_sts)
158 return 0;
161 static inline int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta)
163 return 0;
166 static inline int sja1105_ptp_commit(struct dsa_switch *ds,
167 struct sja1105_ptp_cmd *cmd,
168 sja1105_spi_rw_mode_t rw)
170 return 0;
173 #define sja1105et_ptp_cmd_packing NULL
175 #define sja1105pqrs_ptp_cmd_packing NULL
177 #define sja1105_get_ts_info NULL
179 #define sja1105_port_rxtstamp NULL
181 #define sja1105_port_txtstamp NULL
183 #define sja1105_hwtstamp_get NULL
185 #define sja1105_hwtstamp_set NULL
187 #endif /* IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP) */
189 #endif /* _SJA1105_PTP_H */