Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
0003  */
0004 #ifndef _SJA1105_PTP_H
0005 #define _SJA1105_PTP_H
0006 
0007 #include <linux/timer.h>
0008 
0009 #if IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP)
0010 
0011 /* Timestamps are in units of 8 ns clock ticks (equivalent to
0012  * a fixed 125 MHz clock).
0013  */
0014 #define SJA1105_TICK_NS         8
0015 
0016 static inline s64 ns_to_sja1105_ticks(s64 ns)
0017 {
0018     return ns / SJA1105_TICK_NS;
0019 }
0020 
0021 static inline s64 sja1105_ticks_to_ns(s64 ticks)
0022 {
0023     return ticks * SJA1105_TICK_NS;
0024 }
0025 
0026 /* Calculate the first base_time in the future that satisfies this
0027  * relationship:
0028  *
0029  * future_base_time = base_time + N x cycle_time >= now, or
0030  *
0031  *      now - base_time
0032  * N >= ---------------
0033  *         cycle_time
0034  *
0035  * Because N is an integer, the ceiling value of the above "a / b" ratio
0036  * is in fact precisely the floor value of "(a + b - 1) / b", which is
0037  * easier to calculate only having integer division tools.
0038  */
0039 static inline s64 future_base_time(s64 base_time, s64 cycle_time, s64 now)
0040 {
0041     s64 a, b, n;
0042 
0043     if (base_time >= now)
0044         return base_time;
0045 
0046     a = now - base_time;
0047     b = cycle_time;
0048     n = div_s64(a + b - 1, b);
0049 
0050     return base_time + n * cycle_time;
0051 }
0052 
0053 /* This is not a preprocessor macro because the "ns" argument may or may not be
0054  * s64 at caller side. This ensures it is properly type-cast before div_s64.
0055  */
0056 static inline s64 ns_to_sja1105_delta(s64 ns)
0057 {
0058     return div_s64(ns, 200);
0059 }
0060 
0061 static inline s64 sja1105_delta_to_ns(s64 delta)
0062 {
0063     return delta * 200;
0064 }
0065 
0066 struct sja1105_ptp_cmd {
0067     u64 startptpcp;     /* start toggling PTP_CLK pin */
0068     u64 stopptpcp;      /* stop toggling PTP_CLK pin */
0069     u64 ptpstrtsch;     /* start schedule */
0070     u64 ptpstopsch;     /* stop schedule */
0071     u64 resptp;     /* reset */
0072     u64 corrclk4ts;     /* use the corrected clock for timestamps */
0073     u64 ptpclkadd;      /* enum sja1105_ptp_clk_mode */
0074 };
0075 
0076 struct sja1105_ptp_data {
0077     struct timer_list extts_timer;
0078     /* Used only on SJA1105 to reconstruct partial timestamps */
0079     struct sk_buff_head skb_rxtstamp_queue;
0080     /* Used on SJA1110 where meta frames are generated only for
0081      * 2-step TX timestamps
0082      */
0083     struct sk_buff_head skb_txtstamp_queue;
0084     struct ptp_clock_info caps;
0085     struct ptp_clock *clock;
0086     struct sja1105_ptp_cmd cmd;
0087     /* Serializes all operations on the PTP hardware clock */
0088     struct mutex lock;
0089     bool extts_enabled;
0090     u64 ptpsyncts;
0091 };
0092 
0093 int sja1105_ptp_clock_register(struct dsa_switch *ds);
0094 
0095 void sja1105_ptp_clock_unregister(struct dsa_switch *ds);
0096 
0097 void sja1105et_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
0098                    enum packing_op op);
0099 
0100 void sja1105pqrs_ptp_cmd_packing(u8 *buf, struct sja1105_ptp_cmd *cmd,
0101                  enum packing_op op);
0102 
0103 int sja1105_get_ts_info(struct dsa_switch *ds, int port,
0104             struct ethtool_ts_info *ts);
0105 
0106 void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
0107                   struct sk_buff *clone);
0108 
0109 bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
0110                struct sk_buff *skb, unsigned int type);
0111 
0112 void sja1105_port_txtstamp(struct dsa_switch *ds, int port,
0113                struct sk_buff *skb);
0114 
0115 int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
0116 
0117 int sja1105_hwtstamp_set(struct dsa_switch *ds, int port, struct ifreq *ifr);
0118 
0119 int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
0120                struct ptp_system_timestamp *sts);
0121 
0122 int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
0123               struct ptp_system_timestamp *ptp_sts);
0124 
0125 int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta);
0126 
0127 int sja1105_ptp_commit(struct dsa_switch *ds, struct sja1105_ptp_cmd *cmd,
0128                sja1105_spi_rw_mode_t rw);
0129 
0130 bool sja1105_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
0131 bool sja1110_rxtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
0132 void sja1110_txtstamp(struct dsa_switch *ds, int port, struct sk_buff *skb);
0133 
0134 void sja1110_process_meta_tstamp(struct dsa_switch *ds, int port, u8 ts_id,
0135                  enum sja1110_meta_tstamp dir, u64 tstamp);
0136 
0137 #else
0138 
0139 struct sja1105_ptp_cmd;
0140 
0141 /* Structures cannot be empty in C. Bah!
0142  * Keep the mutex as the only element, which is a bit more difficult to
0143  * refactor out of sja1105_main.c anyway.
0144  */
0145 struct sja1105_ptp_data {
0146     struct mutex lock;
0147 };
0148 
0149 static inline int sja1105_ptp_clock_register(struct dsa_switch *ds)
0150 {
0151     return 0;
0152 }
0153 
0154 static inline void sja1105_ptp_clock_unregister(struct dsa_switch *ds) { }
0155 
0156 static inline void sja1105_ptp_txtstamp_skb(struct dsa_switch *ds, int slot,
0157                         struct sk_buff *clone)
0158 {
0159 }
0160 
0161 static inline int __sja1105_ptp_gettimex(struct dsa_switch *ds, u64 *ns,
0162                      struct ptp_system_timestamp *sts)
0163 {
0164     return 0;
0165 }
0166 
0167 static inline int __sja1105_ptp_settime(struct dsa_switch *ds, u64 ns,
0168                     struct ptp_system_timestamp *ptp_sts)
0169 {
0170     return 0;
0171 }
0172 
0173 static inline int __sja1105_ptp_adjtime(struct dsa_switch *ds, s64 delta)
0174 {
0175     return 0;
0176 }
0177 
0178 static inline int sja1105_ptp_commit(struct dsa_switch *ds,
0179                      struct sja1105_ptp_cmd *cmd,
0180                      sja1105_spi_rw_mode_t rw)
0181 {
0182     return 0;
0183 }
0184 
0185 #define sja1105et_ptp_cmd_packing NULL
0186 
0187 #define sja1105pqrs_ptp_cmd_packing NULL
0188 
0189 #define sja1105_get_ts_info NULL
0190 
0191 #define sja1105_port_rxtstamp NULL
0192 
0193 #define sja1105_port_txtstamp NULL
0194 
0195 #define sja1105_hwtstamp_get NULL
0196 
0197 #define sja1105_hwtstamp_set NULL
0198 
0199 #define sja1105_rxtstamp NULL
0200 #define sja1110_rxtstamp NULL
0201 #define sja1110_txtstamp NULL
0202 
0203 #define sja1110_process_meta_tstamp NULL
0204 
0205 #endif /* IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP) */
0206 
0207 #endif /* _SJA1105_PTP_H */