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 #define BNXT_PTP_GRC_WIN	5
14 #define BNXT_PTP_GRC_WIN_BASE	0x5000
15 
16 #define BNXT_MAX_PHC_DRIFT	31000000
17 #define BNXT_LO_TIMER_MASK	0x0000ffffffffUL
18 #define BNXT_HI_TIMER_MASK	0xffff00000000UL
19 
20 #define BNXT_PTP_QTS_TIMEOUT	1000
21 #define BNXT_PTP_QTS_TX_ENABLES	(PORT_TS_QUERY_REQ_ENABLES_PTP_SEQ_ID |	\
22 				 PORT_TS_QUERY_REQ_ENABLES_TS_REQ_TIMEOUT)
23 
24 struct bnxt_ptp_cfg {
25 	struct ptp_clock_info	ptp_info;
26 	struct ptp_clock	*ptp_clock;
27 	struct cyclecounter	cc;
28 	struct timecounter	tc;
29 	/* serialize timecounter access */
30 	spinlock_t		ptp_lock;
31 	struct sk_buff		*tx_skb;
32 	u64			current_time;
33 	u64			old_time;
34 	unsigned long		next_period;
35 	unsigned long		next_overflow_check;
36 	/* 48-bit PHC overflows in 78 hours.  Check overflow every 19 hours. */
37 	#define BNXT_PHC_OVERFLOW_PERIOD	(19 * 3600 * HZ)
38 
39 	u16			tx_seqid;
40 	struct bnxt		*bp;
41 	atomic_t		tx_avail;
42 #define BNXT_MAX_TX_TS	1
43 	u16			rxctl;
44 #define BNXT_PTP_MSG_SYNC			(1 << 0)
45 #define BNXT_PTP_MSG_DELAY_REQ			(1 << 1)
46 #define BNXT_PTP_MSG_PDELAY_REQ			(1 << 2)
47 #define BNXT_PTP_MSG_PDELAY_RESP		(1 << 3)
48 #define BNXT_PTP_MSG_FOLLOW_UP			(1 << 8)
49 #define BNXT_PTP_MSG_DELAY_RESP			(1 << 9)
50 #define BNXT_PTP_MSG_PDELAY_RESP_FOLLOW_UP	(1 << 10)
51 #define BNXT_PTP_MSG_ANNOUNCE			(1 << 11)
52 #define BNXT_PTP_MSG_SIGNALING			(1 << 12)
53 #define BNXT_PTP_MSG_MANAGEMENT			(1 << 13)
54 #define BNXT_PTP_MSG_EVENTS		(BNXT_PTP_MSG_SYNC |		\
55 					 BNXT_PTP_MSG_DELAY_REQ |	\
56 					 BNXT_PTP_MSG_PDELAY_REQ |	\
57 					 BNXT_PTP_MSG_PDELAY_RESP)
58 	u8			tx_tstamp_en:1;
59 	int			rx_filter;
60 
61 	u32			refclk_regs[2];
62 	u32			refclk_mapped_regs[2];
63 };
64 
65 #if BITS_PER_LONG == 32
66 #define BNXT_READ_TIME64(ptp, dst, src)		\
67 do {						\
68 	spin_lock_bh(&(ptp)->ptp_lock);		\
69 	(dst) = (src);				\
70 	spin_unlock_bh(&(ptp)->ptp_lock);	\
71 } while (0)
72 #else
73 #define BNXT_READ_TIME64(ptp, dst, src)		\
74 	((dst) = READ_ONCE(src))
75 #endif
76 
77 int bnxt_ptp_parse(struct sk_buff *skb, u16 *seq_id);
78 int bnxt_hwtstamp_set(struct net_device *dev, struct ifreq *ifr);
79 int bnxt_hwtstamp_get(struct net_device *dev, struct ifreq *ifr);
80 int bnxt_get_tx_ts_p5(struct bnxt *bp, struct sk_buff *skb);
81 int bnxt_get_rx_ts_p5(struct bnxt *bp, u64 *ts, u32 pkt_ts);
82 int bnxt_ptp_init(struct bnxt *bp);
83 void bnxt_ptp_clear(struct bnxt *bp);
84 #endif
85