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 #if IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP)
8 
9 /* Timestamps are in units of 8 ns clock ticks (equivalent to
10  * a fixed 125 MHz clock).
11  */
12 #define SJA1105_TICK_NS			8
13 
14 static inline s64 ns_to_sja1105_ticks(s64 ns)
15 {
16 	return ns / SJA1105_TICK_NS;
17 }
18 
19 static inline s64 sja1105_ticks_to_ns(s64 ticks)
20 {
21 	return ticks * SJA1105_TICK_NS;
22 }
23 
24 struct sja1105_ptp_cmd {
25 	u64 ptpstrtsch;		/* start schedule */
26 	u64 ptpstopsch;		/* stop schedule */
27 	u64 resptp;		/* reset */
28 	u64 corrclk4ts;		/* use the corrected clock for timestamps */
29 	u64 ptpclkadd;		/* enum sja1105_ptp_clk_mode */
30 };
31 
32 struct sja1105_ptp_data {
33 	struct ptp_clock_info caps;
34 	struct ptp_clock *clock;
35 	struct sja1105_ptp_cmd cmd;
36 	/* Serializes all operations on the PTP hardware clock */
37 	struct mutex lock;
38 };
39 
40 int sja1105_ptp_clock_register(struct dsa_switch *ds);
41 
42 void sja1105_ptp_clock_unregister(struct dsa_switch *ds);
43 
44 void sja1105et_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
45 			       enum packing_op op);
46 
47 void sja1105pqrs_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
48 				 enum packing_op op);
49 
50 int sja1105_get_ts_info(struct dsa_switch *ds, int port,
51 			struct ethtool_ts_info *ts);
52 
53 void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
54 			      struct sk_buff *clone);
55 
56 bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
57 			   struct sk_buff *skb, unsigned int type);
58 
59 bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
60 			   struct sk_buff *skb, unsigned int type);
61 
62 int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
63 
64 int sja1105_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
65 
66 int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
67 			   struct ptp_system_timestamp *sts);
68 
69 int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
70 			  struct ptp_system_timestamp *ptp_sts);
71 
72 int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta);
73 
74 int sja1105_ptp_commit(struct dsa_switch *ds, struct sja1105_ptp_cmd *cmd,
75 		       sja1105_spi_rw_mode_t rw);
76 
77 #else
78 
79 struct sja1105_ptp_cmd;
80 
81 /* Structures cannot be empty in C. Bah!
82  * Keep the mutex as the only element, which is a bit more difficult to
83  * refactor out of sja1105_main.c anyway.
84  */
85 struct sja1105_ptp_data {
86 	struct mutex lock;
87 };
88 
89 static inline int sja1105_ptp_clock_register(struct dsa_switch *ds)
90 {
91 	return 0;
92 }
93 
94 static inline void sja1105_ptp_clock_unregister(struct dsa_switch *ds) { }
95 
96 static inline void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
97 					    struct sk_buff *clone)
98 {
99 }
100 
101 static inline int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
102 					 struct ptp_system_timestamp *sts)
103 {
104 	return 0;
105 }
106 
107 static inline int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
108 					struct ptp_system_timestamp *ptp_sts)
109 {
110 	return 0;
111 }
112 
113 static inline int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta)
114 {
115 	return 0;
116 }
117 
118 static inline int sja1105_ptp_commit(struct dsa_switch *ds,
119 				     struct sja1105_ptp_cmd *cmd,
120 				     sja1105_spi_rw_mode_t rw)
121 {
122 	return 0;
123 }
124 
125 #define sja1105et_ptp_cmd_packing NULL
126 
127 #define sja1105pqrs_ptp_cmd_packing NULL
128 
129 #define sja1105_get_ts_info NULL
130 
131 #define sja1105_port_rxtstamp NULL
132 
133 #define sja1105_port_txtstamp NULL
134 
135 #define sja1105_hwtstamp_get NULL
136 
137 #define sja1105_hwtstamp_set NULL
138 
139 #endif /* IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP) */
140 
141 #endif /* _SJA1105_PTP_H */
142