xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt_ptp.h (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2021 Broadcom Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9 
10 #ifndef BNXT_PTP_H
11 #define BNXT_PTP_H
12 
13 #include <linux/ptp_clock_kernel.h>
14 #include <linux/timecounter.h>
15 
16 #define BNXT_PTP_GRC_WIN	6
17 #define BNXT_PTP_GRC_WIN_BASE	0x6000
18 
19 #define BNXT_MAX_PHC_DRIFT	31000000
20 #define BNXT_CYCLES_SHIFT	23
21 #define BNXT_DEVCLK_FREQ	1000000
22 #define BNXT_LO_TIMER_MASK	0x0000ffffffffUL
23 #define BNXT_HI_TIMER_MASK	0xffff00000000UL
24 #define BNXT_HI_TIMER_SHIFT	24
25 
26 #define BNXT_PTP_DFLT_TX_TMO	1000 /* ms */
27 #define BNXT_PTP_QTS_TIMEOUT	1000
28 #define BNXT_PTP_QTS_MAX_TMO_US	65535U
29 #define BNXT_PTP_QTS_TX_ENABLES	(PORT_TS_QUERY_REQ_ENABLES_PTP_SEQ_ID |	\
30 				 PORT_TS_QUERY_REQ_ENABLES_TS_REQ_TIMEOUT | \
31 				 PORT_TS_QUERY_REQ_ENABLES_PTP_HDR_OFFSET)
32 
33 struct pps_pin {
34 	u8 event;
35 	u8 usage;
36 	u8 state;
37 };
38 
39 #define TSIO_PIN_VALID(pin) ((pin) >= 0 && (pin) < (BNXT_MAX_TSIO_PINS))
40 
41 #define EVENT_DATA2_PPS_EVENT_TYPE(data2)				\
42 	((data2) & ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_EVENT_TYPE)
43 
44 #define EVENT_DATA2_PPS_PIN_NUM(data2)					\
45 	(((data2) &							\
46 	  ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_PIN_NUMBER_MASK) >>\
47 	 ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_PIN_NUMBER_SFT)
48 
49 #define BNXT_DATA2_UPPER_MSK						\
50 	ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_PPS_TIMESTAMP_UPPER_MASK
51 
52 #define BNXT_DATA2_UPPER_SFT						\
53 	(32 -								\
54 	 ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA2_PPS_TIMESTAMP_UPPER_SFT)
55 
56 #define BNXT_DATA1_LOWER_MSK						\
57 	ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA1_PPS_TIMESTAMP_LOWER_MASK
58 
59 #define BNXT_DATA1_LOWER_SFT						\
60 	  ASYNC_EVENT_CMPL_PPS_TIMESTAMP_EVENT_DATA1_PPS_TIMESTAMP_LOWER_SFT
61 
62 #define EVENT_PPS_TS(data2, data1)					\
63 	(((u64)((data2) & BNXT_DATA2_UPPER_MSK) << BNXT_DATA2_UPPER_SFT) |\
64 	 (((data1) & BNXT_DATA1_LOWER_MSK) >> BNXT_DATA1_LOWER_SFT))
65 
66 #define BNXT_PPS_PIN_DISABLE	0
67 #define BNXT_PPS_PIN_ENABLE	1
68 #define BNXT_PPS_PIN_NONE	0
69 #define BNXT_PPS_PIN_PPS_IN	1
70 #define BNXT_PPS_PIN_PPS_OUT	2
71 #define BNXT_PPS_PIN_SYNC_IN	3
72 #define BNXT_PPS_PIN_SYNC_OUT	4
73 
74 #define BNXT_PPS_EVENT_INTERNAL	1
75 #define BNXT_PPS_EVENT_EXTERNAL	2
76 
77 struct bnxt_pps {
78 	u8 num_pins;
79 #define BNXT_MAX_TSIO_PINS	4
80 	struct pps_pin pins[BNXT_MAX_TSIO_PINS];
81 };
82 
83 struct bnxt_ptp_stats {
84 	u64		ts_pkts;
85 	u64		ts_lost;
86 	atomic64_t	ts_err;
87 };
88 
89 #define BNXT_MAX_TX_TS		4
90 #define NEXT_TXTS(idx)		(((idx) + 1) & (BNXT_MAX_TX_TS - 1))
91 
92 struct bnxt_ptp_tx_req {
93 	struct sk_buff		*tx_skb;
94 	u16			tx_seqid;
95 	u16			tx_hdr_off;
96 	unsigned long		abs_txts_tmo;
97 };
98 
99 struct bnxt_ptp_cfg {
100 	struct ptp_clock_info	ptp_info;
101 	struct ptp_clock	*ptp_clock;
102 	struct cyclecounter	cc;
103 	struct timecounter	tc;
104 	struct bnxt_pps		pps_info;
105 	/* serialize timecounter access */
106 	seqlock_t		ptp_lock;
107 	/* serialize ts tx request queuing */
108 	spinlock_t		ptp_tx_lock;
109 	u64			current_time;
110 	unsigned long		next_period;
111 	unsigned long		next_overflow_check;
112 	u32			cmult;
113 	/* cache of upper 24 bits of cyclecoutner. 8 bits are used to check for roll-over */
114 	u32			old_time;
115 	/* a 23b shift cyclecounter will overflow in ~36 mins.  Check overflow every 18 mins. */
116 	#define BNXT_PHC_OVERFLOW_PERIOD	(18 * 60 * HZ)
117 
118 	struct bnxt_ptp_tx_req	txts_req[BNXT_MAX_TX_TS];
119 
120 	struct bnxt		*bp;
121 	u32			tx_avail;
122 	u16			rxctl;
123 #define BNXT_PTP_MSG_SYNC			(1 << 0)
124 #define BNXT_PTP_MSG_DELAY_REQ			(1 << 1)
125 #define BNXT_PTP_MSG_PDELAY_REQ			(1 << 2)
126 #define BNXT_PTP_MSG_PDELAY_RESP		(1 << 3)
127 #define BNXT_PTP_MSG_FOLLOW_UP			(1 << 8)
128 #define BNXT_PTP_MSG_DELAY_RESP			(1 << 9)
129 #define BNXT_PTP_MSG_PDELAY_RESP_FOLLOW_UP	(1 << 10)
130 #define BNXT_PTP_MSG_ANNOUNCE			(1 << 11)
131 #define BNXT_PTP_MSG_SIGNALING			(1 << 12)
132 #define BNXT_PTP_MSG_MANAGEMENT			(1 << 13)
133 #define BNXT_PTP_MSG_EVENTS		(BNXT_PTP_MSG_SYNC |		\
134 					 BNXT_PTP_MSG_DELAY_REQ |	\
135 					 BNXT_PTP_MSG_PDELAY_REQ |	\
136 					 BNXT_PTP_MSG_PDELAY_RESP)
137 	u8			tx_tstamp_en:1;
138 	u8			rtc_configured:1;
139 	int			rx_filter;
140 	u32			tstamp_filters;
141 
142 	u32			refclk_regs[2];
143 	u32			refclk_mapped_regs[2];
144 	u32			txts_tmo;
145 	u16			txts_prod;
146 	u16			txts_cons;
147 
148 	struct bnxt_ptp_stats	stats;
149 };
150 
151 #define BNXT_PTP_INC_TX_AVAIL(ptp)		\
152 do {						\
153 	spin_lock_bh(&(ptp)->ptp_tx_lock);	\
154 	(ptp)->tx_avail++;			\
155 	spin_unlock_bh(&(ptp)->ptp_tx_lock);	\
156 } while (0)
157 
158 int bnxt_ptp_parse(struct sk_buff *skb, u16 *seq_id, u16 *hdr_off);
159 void bnxt_ptp_update_current_time(struct bnxt *bp);
160 void bnxt_ptp_pps_event(struct bnxt *bp, u32 data1, u32 data2);
161 int bnxt_ptp_cfg_tstamp_filters(struct bnxt *bp);
162 void bnxt_ptp_reapply_pps(struct bnxt *bp);
163 int bnxt_hwtstamp_set(struct net_device *dev, struct ifreq *ifr);
164 int bnxt_hwtstamp_get(struct net_device *dev, struct ifreq *ifr);
165 int bnxt_ptp_get_txts_prod(struct bnxt_ptp_cfg *ptp, u16 *prod);
166 void bnxt_get_tx_ts_p5(struct bnxt *bp, struct sk_buff *skb, u16 prod);
167 int bnxt_get_rx_ts_p5(struct bnxt *bp, u64 *ts, u32 pkt_ts);
168 void bnxt_tx_ts_cmp(struct bnxt *bp, struct bnxt_napi *bnapi,
169 		    struct tx_ts_cmp *tscmp);
170 void bnxt_ptp_rtc_timecounter_init(struct bnxt_ptp_cfg *ptp, u64 ns);
171 int bnxt_ptp_init_rtc(struct bnxt *bp, bool phc_cfg);
172 int bnxt_ptp_init(struct bnxt *bp);
173 void bnxt_ptp_clear(struct bnxt *bp);
174 static inline u64 bnxt_timecounter_cyc2time(struct bnxt_ptp_cfg *ptp, u64 ts)
175 {
176 	unsigned int seq;
177 	u64 ns;
178 
179 	do {
180 		seq = read_seqbegin(&ptp->ptp_lock);
181 		ns = timecounter_cyc2time(&ptp->tc, ts);
182 	} while (read_seqretry(&ptp->ptp_lock, seq));
183 
184 	return ns;
185 }
186 
187 static inline u64 bnxt_extend_cycles_32b_to_48b(struct bnxt_ptp_cfg *ptp, u32 ts)
188 {
189 	u64 time, cycles;
190 
191 	time = (u64)READ_ONCE(ptp->old_time) << BNXT_HI_TIMER_SHIFT;
192 	cycles = (time & BNXT_HI_TIMER_MASK) | ts;
193 	if (ts < (time & BNXT_LO_TIMER_MASK))
194 		cycles += BNXT_LO_TIMER_MASK + 1;
195 	return cycles;
196 }
197 #endif
198