1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* cavium_ptp.h - PTP 1588 clock on Cavium hardware
3  * Copyright (c) 2003-2015, 2017 Cavium, Inc.
4  */
5 
6 #ifndef CAVIUM_PTP_H
7 #define CAVIUM_PTP_H
8 
9 #include <linux/ptp_clock_kernel.h>
10 #include <linux/timecounter.h>
11 
12 struct cavium_ptp {
13 	struct pci_dev *pdev;
14 
15 	/* Serialize access to cycle_counter, time_counter and hw_registers */
16 	spinlock_t spin_lock;
17 	struct cyclecounter cycle_counter;
18 	struct timecounter time_counter;
19 	void __iomem *reg_base;
20 
21 	u32 clock_rate;
22 
23 	struct ptp_clock_info ptp_info;
24 	struct ptp_clock *ptp_clock;
25 };
26 
27 #if IS_REACHABLE(CONFIG_CAVIUM_PTP)
28 
29 struct cavium_ptp *cavium_ptp_get(void);
30 void cavium_ptp_put(struct cavium_ptp *ptp);
31 
32 static inline u64 cavium_ptp_tstamp2time(struct cavium_ptp *ptp, u64 tstamp)
33 {
34 	unsigned long flags;
35 	u64 ret;
36 
37 	spin_lock_irqsave(&ptp->spin_lock, flags);
38 	ret = timecounter_cyc2time(&ptp->time_counter, tstamp);
39 	spin_unlock_irqrestore(&ptp->spin_lock, flags);
40 
41 	return ret;
42 }
43 
44 static inline int cavium_ptp_clock_index(struct cavium_ptp *clock)
45 {
46 	return ptp_clock_index(clock->ptp_clock);
47 }
48 
49 #else
50 
51 static inline struct cavium_ptp *cavium_ptp_get(void)
52 {
53 	return ERR_PTR(-ENODEV);
54 }
55 
56 static inline void cavium_ptp_put(struct cavium_ptp *ptp) {}
57 
58 static inline u64 cavium_ptp_tstamp2time(struct cavium_ptp *ptp, u64 tstamp)
59 {
60 	return 0;
61 }
62 
63 static inline int cavium_ptp_clock_index(struct cavium_ptp *clock)
64 {
65 	return -1;
66 }
67 
68 #endif
69 
70 #endif
71