xref: /openbmc/linux/drivers/ptp/ptp_ocp.c (revision 8119c9ee)
1a7e1abadSJonathan Lemon // SPDX-License-Identifier: GPL-2.0-only
2a7e1abadSJonathan Lemon /* Copyright (c) 2020 Facebook */
3a7e1abadSJonathan Lemon 
4a7e1abadSJonathan Lemon #include <linux/err.h>
5a7e1abadSJonathan Lemon #include <linux/kernel.h>
6a7e1abadSJonathan Lemon #include <linux/module.h>
7f67bf662SJonathan Lemon #include <linux/debugfs.h>
8a7e1abadSJonathan Lemon #include <linux/init.h>
9a7e1abadSJonathan Lemon #include <linux/pci.h>
10773bda96SJonathan Lemon #include <linux/serial_8250.h>
11773bda96SJonathan Lemon #include <linux/clkdev.h>
12773bda96SJonathan Lemon #include <linux/clk-provider.h>
13773bda96SJonathan Lemon #include <linux/platform_device.h>
140cfcdd1eSJonathan Lemon #include <linux/platform_data/i2c-xiic.h>
15a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h>
16773bda96SJonathan Lemon #include <linux/spi/spi.h>
17773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h>
18773bda96SJonathan Lemon #include <net/devlink.h>
19773bda96SJonathan Lemon #include <linux/i2c.h>
20773bda96SJonathan Lemon #include <linux/mtd/mtd.h>
210cfcdd1eSJonathan Lemon #include <linux/nvmem-consumer.h>
22a7e1abadSJonathan Lemon 
23773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK
24773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b
25773bda96SJonathan Lemon #endif
26773bda96SJonathan Lemon 
27773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD
28773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
29773bda96SJonathan Lemon #endif
30773bda96SJonathan Lemon 
31773bda96SJonathan Lemon static struct class timecard_class = {
32773bda96SJonathan Lemon 	.owner		= THIS_MODULE,
33773bda96SJonathan Lemon 	.name		= "timecard",
34a7e1abadSJonathan Lemon };
35a7e1abadSJonathan Lemon 
36a7e1abadSJonathan Lemon struct ocp_reg {
37a7e1abadSJonathan Lemon 	u32	ctrl;
38a7e1abadSJonathan Lemon 	u32	status;
39a7e1abadSJonathan Lemon 	u32	select;
40a7e1abadSJonathan Lemon 	u32	version;
41a7e1abadSJonathan Lemon 	u32	time_ns;
42a7e1abadSJonathan Lemon 	u32	time_sec;
43a7e1abadSJonathan Lemon 	u32	__pad0[2];
44a7e1abadSJonathan Lemon 	u32	adjust_ns;
45a7e1abadSJonathan Lemon 	u32	adjust_sec;
46a7e1abadSJonathan Lemon 	u32	__pad1[2];
47a7e1abadSJonathan Lemon 	u32	offset_ns;
48a7e1abadSJonathan Lemon 	u32	offset_window_ns;
49773bda96SJonathan Lemon 	u32	__pad2[2];
50773bda96SJonathan Lemon 	u32	drift_ns;
51773bda96SJonathan Lemon 	u32	drift_window_ns;
52773bda96SJonathan Lemon 	u32	__pad3[6];
53773bda96SJonathan Lemon 	u32	servo_offset_p;
54773bda96SJonathan Lemon 	u32	servo_offset_i;
55773bda96SJonathan Lemon 	u32	servo_drift_p;
56773bda96SJonathan Lemon 	u32	servo_drift_i;
572f23f486SVadim Fedorenko 	u32	status_offset;
582f23f486SVadim Fedorenko 	u32	status_drift;
59a7e1abadSJonathan Lemon };
60a7e1abadSJonathan Lemon 
61a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE		BIT(0)
62a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME	BIT(1)
63a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET	BIT(2)
64773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT	BIT(3)
65773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO	BIT(8)
66a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ	BIT(30)
67a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE	BIT(31)
68a7e1abadSJonathan Lemon 
69a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC	BIT(0)
70773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER	BIT(1)
71a7e1abadSJonathan Lemon 
72a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE	0
73773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG	0xfe
74a7e1abadSJonathan Lemon 
75a7e1abadSJonathan Lemon struct tod_reg {
76a7e1abadSJonathan Lemon 	u32	ctrl;
77a7e1abadSJonathan Lemon 	u32	status;
78a7e1abadSJonathan Lemon 	u32	uart_polarity;
79a7e1abadSJonathan Lemon 	u32	version;
80065efcc5SJonathan Lemon 	u32	adj_sec;
81a7e1abadSJonathan Lemon 	u32	__pad0[3];
82a7e1abadSJonathan Lemon 	u32	uart_baud;
83a7e1abadSJonathan Lemon 	u32	__pad1[3];
84a7e1abadSJonathan Lemon 	u32	utc_status;
85a7e1abadSJonathan Lemon 	u32	leap;
86a7e1abadSJonathan Lemon };
87a7e1abadSJonathan Lemon 
88a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL	BIT(28)
89a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A	BIT(17)
90a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B	BIT(16)
91a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE		BIT(0)
92a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK	((1U << 4) - 1)
93a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT	24
94a7e1abadSJonathan Lemon 
95a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK		0xff
96a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID		BIT(8)
979f492c4cSVadim Fedorenko #define TOD_STATUS_LEAP_ANNOUNCE	BIT(12)
98a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID		BIT(16)
99a7e1abadSJonathan Lemon 
100773bda96SJonathan Lemon struct ts_reg {
101773bda96SJonathan Lemon 	u32	enable;
102773bda96SJonathan Lemon 	u32	error;
103773bda96SJonathan Lemon 	u32	polarity;
104773bda96SJonathan Lemon 	u32	version;
105773bda96SJonathan Lemon 	u32	__pad0[4];
106773bda96SJonathan Lemon 	u32	cable_delay;
107773bda96SJonathan Lemon 	u32	__pad1[3];
108773bda96SJonathan Lemon 	u32	intr;
109773bda96SJonathan Lemon 	u32	intr_mask;
110773bda96SJonathan Lemon 	u32	event_count;
111773bda96SJonathan Lemon 	u32	__pad2[1];
112773bda96SJonathan Lemon 	u32	ts_count;
113773bda96SJonathan Lemon 	u32	time_ns;
114773bda96SJonathan Lemon 	u32	time_sec;
115773bda96SJonathan Lemon 	u32	data_width;
116773bda96SJonathan Lemon 	u32	data;
117773bda96SJonathan Lemon };
118773bda96SJonathan Lemon 
119773bda96SJonathan Lemon struct pps_reg {
120773bda96SJonathan Lemon 	u32	ctrl;
121773bda96SJonathan Lemon 	u32	status;
1220d43d4f2SJonathan Lemon 	u32	__pad0[6];
1230d43d4f2SJonathan Lemon 	u32	cable_delay;
124773bda96SJonathan Lemon };
125773bda96SJonathan Lemon 
126773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR	BIT(0)
127773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR	BIT(1)
128773bda96SJonathan Lemon 
129773bda96SJonathan Lemon struct img_reg {
130773bda96SJonathan Lemon 	u32	version;
131773bda96SJonathan Lemon };
132773bda96SJonathan Lemon 
133e1daf0ecSJonathan Lemon struct gpio_reg {
134e1daf0ecSJonathan Lemon 	u32	gpio1;
135e1daf0ecSJonathan Lemon 	u32	__pad0;
136e1daf0ecSJonathan Lemon 	u32	gpio2;
137e1daf0ecSJonathan Lemon 	u32	__pad1;
138e1daf0ecSJonathan Lemon };
139e1daf0ecSJonathan Lemon 
1406baf2925SJonathan Lemon struct irig_master_reg {
1416baf2925SJonathan Lemon 	u32	ctrl;
1426baf2925SJonathan Lemon 	u32	status;
1436baf2925SJonathan Lemon 	u32	__pad0;
1446baf2925SJonathan Lemon 	u32	version;
1456baf2925SJonathan Lemon 	u32	adj_sec;
1466baf2925SJonathan Lemon 	u32	mode_ctrl;
1476baf2925SJonathan Lemon };
1486baf2925SJonathan Lemon 
1496baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE	BIT(0)
1506baf2925SJonathan Lemon 
1516baf2925SJonathan Lemon struct irig_slave_reg {
1526baf2925SJonathan Lemon 	u32	ctrl;
1536baf2925SJonathan Lemon 	u32	status;
1546baf2925SJonathan Lemon 	u32	__pad0;
1556baf2925SJonathan Lemon 	u32	version;
1566baf2925SJonathan Lemon 	u32	adj_sec;
1576baf2925SJonathan Lemon 	u32	mode_ctrl;
1586baf2925SJonathan Lemon };
1596baf2925SJonathan Lemon 
1606baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE	BIT(0)
1616baf2925SJonathan Lemon 
1626baf2925SJonathan Lemon struct dcf_master_reg {
1636baf2925SJonathan Lemon 	u32	ctrl;
1646baf2925SJonathan Lemon 	u32	status;
1656baf2925SJonathan Lemon 	u32	__pad0;
1666baf2925SJonathan Lemon 	u32	version;
1676baf2925SJonathan Lemon 	u32	adj_sec;
1686baf2925SJonathan Lemon };
1696baf2925SJonathan Lemon 
1706baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE	BIT(0)
1716baf2925SJonathan Lemon 
1726baf2925SJonathan Lemon struct dcf_slave_reg {
1736baf2925SJonathan Lemon 	u32	ctrl;
1746baf2925SJonathan Lemon 	u32	status;
1756baf2925SJonathan Lemon 	u32	__pad0;
1766baf2925SJonathan Lemon 	u32	version;
1776baf2925SJonathan Lemon 	u32	adj_sec;
1786baf2925SJonathan Lemon };
1796baf2925SJonathan Lemon 
1806baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE	BIT(0)
1816baf2925SJonathan Lemon 
182b325af3cSJonathan Lemon struct signal_reg {
183b325af3cSJonathan Lemon 	u32	enable;
184b325af3cSJonathan Lemon 	u32	status;
185b325af3cSJonathan Lemon 	u32	polarity;
186b325af3cSJonathan Lemon 	u32	version;
187b325af3cSJonathan Lemon 	u32	__pad0[4];
188b325af3cSJonathan Lemon 	u32	cable_delay;
189b325af3cSJonathan Lemon 	u32	__pad1[3];
190b325af3cSJonathan Lemon 	u32	intr;
191b325af3cSJonathan Lemon 	u32	intr_mask;
192b325af3cSJonathan Lemon 	u32	__pad2[2];
193b325af3cSJonathan Lemon 	u32	start_ns;
194b325af3cSJonathan Lemon 	u32	start_sec;
195b325af3cSJonathan Lemon 	u32	pulse_ns;
196b325af3cSJonathan Lemon 	u32	pulse_sec;
197b325af3cSJonathan Lemon 	u32	period_ns;
198b325af3cSJonathan Lemon 	u32	period_sec;
199b325af3cSJonathan Lemon 	u32	repeat_count;
200b325af3cSJonathan Lemon };
201b325af3cSJonathan Lemon 
2022407f5d6SJonathan Lemon struct frequency_reg {
2032407f5d6SJonathan Lemon 	u32	ctrl;
2042407f5d6SJonathan Lemon 	u32	status;
2052407f5d6SJonathan Lemon };
2062407f5d6SJonathan Lemon #define FREQ_STATUS_VALID	BIT(31)
2072407f5d6SJonathan Lemon #define FREQ_STATUS_ERROR	BIT(30)
2082407f5d6SJonathan Lemon #define FREQ_STATUS_OVERRUN	BIT(29)
2092407f5d6SJonathan Lemon #define FREQ_STATUS_MASK	(BIT(24) - 1)
2102407f5d6SJonathan Lemon 
211773bda96SJonathan Lemon struct ptp_ocp_flash_info {
212773bda96SJonathan Lemon 	const char *name;
213773bda96SJonathan Lemon 	int pci_offset;
214773bda96SJonathan Lemon 	int data_size;
215773bda96SJonathan Lemon 	void *data;
216773bda96SJonathan Lemon };
217773bda96SJonathan Lemon 
2181618df6aSJonathan Lemon struct ptp_ocp_i2c_info {
2191618df6aSJonathan Lemon 	const char *name;
2201618df6aSJonathan Lemon 	unsigned long fixed_rate;
2211618df6aSJonathan Lemon 	size_t data_size;
2221618df6aSJonathan Lemon 	void *data;
2231618df6aSJonathan Lemon };
2241618df6aSJonathan Lemon 
225773bda96SJonathan Lemon struct ptp_ocp_ext_info {
226773bda96SJonathan Lemon 	int index;
227773bda96SJonathan Lemon 	irqreturn_t (*irq_fcn)(int irq, void *priv);
228a62a56d0SJonathan Lemon 	int (*enable)(void *priv, u32 req, bool enable);
229773bda96SJonathan Lemon };
230773bda96SJonathan Lemon 
231773bda96SJonathan Lemon struct ptp_ocp_ext_src {
232773bda96SJonathan Lemon 	void __iomem		*mem;
233773bda96SJonathan Lemon 	struct ptp_ocp		*bp;
234773bda96SJonathan Lemon 	struct ptp_ocp_ext_info	*info;
235773bda96SJonathan Lemon 	int			irq_vec;
236773bda96SJonathan Lemon };
237773bda96SJonathan Lemon 
238a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode {
239a509a7c6SJonathan Lemon 	SMA_MODE_IN,
240a509a7c6SJonathan Lemon 	SMA_MODE_OUT,
241a509a7c6SJonathan Lemon };
242a509a7c6SJonathan Lemon 
243a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector {
244a509a7c6SJonathan Lemon 	enum	ptp_ocp_sma_mode mode;
245a509a7c6SJonathan Lemon 	bool	fixed_fcn;
246a509a7c6SJonathan Lemon 	bool	fixed_dir;
247b2c4f0acSJonathan Lemon 	bool	disabled;
248a509a7c6SJonathan Lemon };
249a509a7c6SJonathan Lemon 
250c205d53cSJonathan Lemon struct ocp_attr_group {
251c205d53cSJonathan Lemon 	u64 cap;
252c205d53cSJonathan Lemon 	const struct attribute_group *group;
253c205d53cSJonathan Lemon };
254c205d53cSJonathan Lemon 
255c205d53cSJonathan Lemon #define OCP_CAP_BASIC	BIT(0)
256b325af3cSJonathan Lemon #define OCP_CAP_SIGNAL	BIT(1)
2572407f5d6SJonathan Lemon #define OCP_CAP_FREQ	BIT(2)
258b325af3cSJonathan Lemon 
259b325af3cSJonathan Lemon struct ptp_ocp_signal {
260b325af3cSJonathan Lemon 	ktime_t		period;
261b325af3cSJonathan Lemon 	ktime_t		pulse;
262b325af3cSJonathan Lemon 	ktime_t		phase;
263b325af3cSJonathan Lemon 	ktime_t		start;
264b325af3cSJonathan Lemon 	int		duty;
265b325af3cSJonathan Lemon 	bool		polarity;
266b325af3cSJonathan Lemon 	bool		running;
267b325af3cSJonathan Lemon };
268c205d53cSJonathan Lemon 
2690cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN		13
2700cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN			6
2710cfcdd1eSJonathan Lemon 
272a7e1abadSJonathan Lemon struct ptp_ocp {
273a7e1abadSJonathan Lemon 	struct pci_dev		*pdev;
274773bda96SJonathan Lemon 	struct device		dev;
275a7e1abadSJonathan Lemon 	spinlock_t		lock;
276a7e1abadSJonathan Lemon 	struct ocp_reg __iomem	*reg;
277a7e1abadSJonathan Lemon 	struct tod_reg __iomem	*tod;
2780d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_ext;
2790d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_clk;
280f67bf662SJonathan Lemon 	struct gpio_reg __iomem	*pps_select;
281a509a7c6SJonathan Lemon 	struct gpio_reg __iomem	*sma_map1;
282a509a7c6SJonathan Lemon 	struct gpio_reg __iomem	*sma_map2;
2836baf2925SJonathan Lemon 	struct irig_master_reg	__iomem *irig_out;
2846baf2925SJonathan Lemon 	struct irig_slave_reg	__iomem *irig_in;
2856baf2925SJonathan Lemon 	struct dcf_master_reg	__iomem *dcf_out;
2866baf2925SJonathan Lemon 	struct dcf_slave_reg	__iomem *dcf_in;
287e3516bb4SJonathan Lemon 	struct tod_reg		__iomem *nmea_out;
2882407f5d6SJonathan Lemon 	struct frequency_reg	__iomem *freq_in[4];
289b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src	*signal_out[4];
290773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*pps;
291773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts0;
292773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts1;
293dcf61469SJonathan Lemon 	struct ptp_ocp_ext_src	*ts2;
2940fa3ff7eSJonathan Lemon 	struct ptp_ocp_ext_src	*ts3;
2950fa3ff7eSJonathan Lemon 	struct ptp_ocp_ext_src	*ts4;
296773bda96SJonathan Lemon 	struct img_reg __iomem	*image;
297a7e1abadSJonathan Lemon 	struct ptp_clock	*ptp;
298a7e1abadSJonathan Lemon 	struct ptp_clock_info	ptp_info;
299773bda96SJonathan Lemon 	struct platform_device	*i2c_ctrl;
300773bda96SJonathan Lemon 	struct platform_device	*spi_flash;
301773bda96SJonathan Lemon 	struct clk_hw		*i2c_clk;
302773bda96SJonathan Lemon 	struct timer_list	watchdog;
303c2239294SJonathan Lemon 	const struct attribute_group **attr_group;
3040cfcdd1eSJonathan Lemon 	const struct ptp_ocp_eeprom_map *eeprom_map;
305f67bf662SJonathan Lemon 	struct dentry		*debug_root;
306ef0cfb34SJonathan Lemon 	time64_t		gnss_lost;
307773bda96SJonathan Lemon 	int			id;
308773bda96SJonathan Lemon 	int			n_irqs;
309ef0cfb34SJonathan Lemon 	int			gnss_port;
31071d7e085SJonathan Lemon 	int			gnss2_port;
311773bda96SJonathan Lemon 	int			mac_port;	/* miniature atomic clock */
312e3516bb4SJonathan Lemon 	int			nmea_port;
313b0ca789aSJonathan Lemon 	u32			fw_version;
3140cfcdd1eSJonathan Lemon 	u8			board_id[OCP_BOARD_ID_LEN];
3150cfcdd1eSJonathan Lemon 	u8			serial[OCP_SERIAL_LEN];
3160cfcdd1eSJonathan Lemon 	bool			has_eeprom_data;
317a62a56d0SJonathan Lemon 	u32			pps_req_map;
31889260d87SJonathan Lemon 	int			flash_start;
31989260d87SJonathan Lemon 	u32			utc_tai_offset;
3201acffc6eSJonathan Lemon 	u32			ts_window_adjust;
321c205d53cSJonathan Lemon 	u64			fw_cap;
322b325af3cSJonathan Lemon 	struct ptp_ocp_signal	signal[4];
323a509a7c6SJonathan Lemon 	struct ptp_ocp_sma_connector sma[4];
324a7e1abadSJonathan Lemon };
325a7e1abadSJonathan Lemon 
326a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP	BIT(0)
327a62a56d0SJonathan Lemon #define OCP_REQ_PPS		BIT(1)
328a62a56d0SJonathan Lemon 
329773bda96SJonathan Lemon struct ocp_resource {
330773bda96SJonathan Lemon 	unsigned long offset;
331773bda96SJonathan Lemon 	int size;
332773bda96SJonathan Lemon 	int irq_vec;
333773bda96SJonathan Lemon 	int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
334773bda96SJonathan Lemon 	void *extra;
335773bda96SJonathan Lemon 	unsigned long bp_offset;
33656ec4403SJonathan Lemon 	const char * const name;
337773bda96SJonathan Lemon };
338773bda96SJonathan Lemon 
339773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
340773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
341773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
342773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
343773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
344773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
345773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
346b325af3cSJonathan Lemon static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv);
347a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
3481aa66a3aSJonathan Lemon static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
3491aa66a3aSJonathan Lemon 				      struct ptp_perout_request *req);
350b325af3cSJonathan Lemon static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable);
351b325af3cSJonathan Lemon static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr);
352773bda96SJonathan Lemon 
353c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[];
354c205d53cSJonathan Lemon 
3550cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map {
3560cfcdd1eSJonathan Lemon 	u16	off;
3570cfcdd1eSJonathan Lemon 	u16	len;
3580cfcdd1eSJonathan Lemon 	u32	bp_offset;
3590cfcdd1eSJonathan Lemon 	const void * const tag;
3600cfcdd1eSJonathan Lemon };
3610cfcdd1eSJonathan Lemon 
3620cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member)				\
3630cfcdd1eSJonathan Lemon 	.off = addr,						\
3640cfcdd1eSJonathan Lemon 	.len = sizeof_field(struct ptp_ocp, member),		\
3650cfcdd1eSJonathan Lemon 	.bp_offset = offsetof(struct ptp_ocp, member)
3660cfcdd1eSJonathan Lemon 
3670cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({				\
3680cfcdd1eSJonathan Lemon 	(void *)((uintptr_t)(bp) + (map)->bp_offset);		\
3690cfcdd1eSJonathan Lemon })
3700cfcdd1eSJonathan Lemon 
3710cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
3720cfcdd1eSJonathan Lemon 	{ EEPROM_ENTRY(0x43, board_id) },
3730cfcdd1eSJonathan Lemon 	{ EEPROM_ENTRY(0x00, serial), .tag = "mac" },
3740cfcdd1eSJonathan Lemon 	{ }
3750cfcdd1eSJonathan Lemon };
3760cfcdd1eSJonathan Lemon 
377773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({				\
378773bda96SJonathan Lemon 	uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset;		\
379773bda96SJonathan Lemon 	*(typeof(val) *)addr = val;					\
380773bda96SJonathan Lemon })
381773bda96SJonathan Lemon 
382773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \
38356ec4403SJonathan Lemon 	.name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
384773bda96SJonathan Lemon 
385773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \
386773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
387773bda96SJonathan Lemon 
388773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \
389773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
390773bda96SJonathan Lemon 
391773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \
392773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
393773bda96SJonathan Lemon 
394773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \
395773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
396773bda96SJonathan Lemon 
397773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \
398773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
399773bda96SJonathan Lemon 
400773bda96SJonathan Lemon /* This is the MSI vector mapping used.
4010fa3ff7eSJonathan Lemon  * 0: PPS (TS5)
402773bda96SJonathan Lemon  * 1: TS0
403773bda96SJonathan Lemon  * 2: TS1
404be69087cSJonathan Lemon  * 3: GNSS1
40571d7e085SJonathan Lemon  * 4: GNSS2
406773bda96SJonathan Lemon  * 5: MAC
407dcf61469SJonathan Lemon  * 6: TS2
4081447149dSJonathan Lemon  * 7: I2C controller
409e3516bb4SJonathan Lemon  * 8: HWICAP (notused)
410773bda96SJonathan Lemon  * 9: SPI Flash
411e3516bb4SJonathan Lemon  * 10: NMEA
412b325af3cSJonathan Lemon  * 11: Signal Generator 1
413b325af3cSJonathan Lemon  * 12: Signal Generator 2
414b325af3cSJonathan Lemon  * 13: Signal Generator 3
415b325af3cSJonathan Lemon  * 14: Signal Generator 4
4160fa3ff7eSJonathan Lemon  * 15: TS3
4170fa3ff7eSJonathan Lemon  * 16: TS4
418773bda96SJonathan Lemon  */
419773bda96SJonathan Lemon 
420773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = {
421773bda96SJonathan Lemon 	{
422773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(reg),
423773bda96SJonathan Lemon 		.offset = 0x01000000, .size = 0x10000,
424773bda96SJonathan Lemon 	},
425773bda96SJonathan Lemon 	{
426773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts0),
427773bda96SJonathan Lemon 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
428773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
42956ec4403SJonathan Lemon 			.index = 0,
430773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
431773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
432773bda96SJonathan Lemon 		},
433773bda96SJonathan Lemon 	},
434773bda96SJonathan Lemon 	{
435773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts1),
436773bda96SJonathan Lemon 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
437773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
43856ec4403SJonathan Lemon 			.index = 1,
439773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
440773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
441773bda96SJonathan Lemon 		},
442773bda96SJonathan Lemon 	},
443773bda96SJonathan Lemon 	{
444dcf61469SJonathan Lemon 		OCP_EXT_RESOURCE(ts2),
445dcf61469SJonathan Lemon 		.offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
446dcf61469SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
447dcf61469SJonathan Lemon 			.index = 2,
448dcf61469SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
449dcf61469SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
450dcf61469SJonathan Lemon 		},
451dcf61469SJonathan Lemon 	},
452dcf61469SJonathan Lemon 	{
4530fa3ff7eSJonathan Lemon 		OCP_EXT_RESOURCE(ts3),
4540fa3ff7eSJonathan Lemon 		.offset = 0x01110000, .size = 0x10000, .irq_vec = 15,
4550fa3ff7eSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
4560fa3ff7eSJonathan Lemon 			.index = 3,
4570fa3ff7eSJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
4580fa3ff7eSJonathan Lemon 			.enable = ptp_ocp_ts_enable,
4590fa3ff7eSJonathan Lemon 		},
4600fa3ff7eSJonathan Lemon 	},
4610fa3ff7eSJonathan Lemon 	{
4620fa3ff7eSJonathan Lemon 		OCP_EXT_RESOURCE(ts4),
4630fa3ff7eSJonathan Lemon 		.offset = 0x01120000, .size = 0x10000, .irq_vec = 16,
4640fa3ff7eSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
4650fa3ff7eSJonathan Lemon 			.index = 4,
4660fa3ff7eSJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
4670fa3ff7eSJonathan Lemon 			.enable = ptp_ocp_ts_enable,
4680fa3ff7eSJonathan Lemon 		},
4690fa3ff7eSJonathan Lemon 	},
4700fa3ff7eSJonathan Lemon 	/* Timestamp for PHC and/or PPS generator */
4710fa3ff7eSJonathan Lemon 	{
472a62a56d0SJonathan Lemon 		OCP_EXT_RESOURCE(pps),
473a62a56d0SJonathan Lemon 		.offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
474a62a56d0SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
4750fa3ff7eSJonathan Lemon 			.index = 5,
476a62a56d0SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
477a62a56d0SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
478a62a56d0SJonathan Lemon 		},
479a62a56d0SJonathan Lemon 	},
480a62a56d0SJonathan Lemon 	{
481b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[0]),
482b325af3cSJonathan Lemon 		.offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
483b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
484b325af3cSJonathan Lemon 			.index = 1,
485b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
486b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
487b325af3cSJonathan Lemon 		},
488b325af3cSJonathan Lemon 	},
489b325af3cSJonathan Lemon 	{
490b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[1]),
491b325af3cSJonathan Lemon 		.offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
492b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
493b325af3cSJonathan Lemon 			.index = 2,
494b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
495b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
496b325af3cSJonathan Lemon 		},
497b325af3cSJonathan Lemon 	},
498b325af3cSJonathan Lemon 	{
499b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[2]),
500b325af3cSJonathan Lemon 		.offset = 0x010F0000, .size = 0x10000, .irq_vec = 13,
501b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
502b325af3cSJonathan Lemon 			.index = 3,
503b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
504b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
505b325af3cSJonathan Lemon 		},
506b325af3cSJonathan Lemon 	},
507b325af3cSJonathan Lemon 	{
508b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[3]),
509b325af3cSJonathan Lemon 		.offset = 0x01100000, .size = 0x10000, .irq_vec = 14,
510b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
511b325af3cSJonathan Lemon 			.index = 4,
512b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
513b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
514b325af3cSJonathan Lemon 		},
515b325af3cSJonathan Lemon 	},
516b325af3cSJonathan Lemon 	{
5170d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_ext),
5180d43d4f2SJonathan Lemon 		.offset = 0x01030000, .size = 0x10000,
5190d43d4f2SJonathan Lemon 	},
5200d43d4f2SJonathan Lemon 	{
5210d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_clk),
522773bda96SJonathan Lemon 		.offset = 0x01040000, .size = 0x10000,
523773bda96SJonathan Lemon 	},
524773bda96SJonathan Lemon 	{
525773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(tod),
526773bda96SJonathan Lemon 		.offset = 0x01050000, .size = 0x10000,
527773bda96SJonathan Lemon 	},
528773bda96SJonathan Lemon 	{
5296baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(irig_in),
5306baf2925SJonathan Lemon 		.offset = 0x01070000, .size = 0x10000,
5316baf2925SJonathan Lemon 	},
5326baf2925SJonathan Lemon 	{
5336baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(irig_out),
5346baf2925SJonathan Lemon 		.offset = 0x01080000, .size = 0x10000,
5356baf2925SJonathan Lemon 	},
5366baf2925SJonathan Lemon 	{
5376baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(dcf_in),
5386baf2925SJonathan Lemon 		.offset = 0x01090000, .size = 0x10000,
5396baf2925SJonathan Lemon 	},
5406baf2925SJonathan Lemon 	{
5416baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(dcf_out),
5426baf2925SJonathan Lemon 		.offset = 0x010A0000, .size = 0x10000,
5436baf2925SJonathan Lemon 	},
5446baf2925SJonathan Lemon 	{
545e3516bb4SJonathan Lemon 		OCP_MEM_RESOURCE(nmea_out),
546e3516bb4SJonathan Lemon 		.offset = 0x010B0000, .size = 0x10000,
547e3516bb4SJonathan Lemon 	},
548e3516bb4SJonathan Lemon 	{
549773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(image),
550773bda96SJonathan Lemon 		.offset = 0x00020000, .size = 0x1000,
551773bda96SJonathan Lemon 	},
552773bda96SJonathan Lemon 	{
553f67bf662SJonathan Lemon 		OCP_MEM_RESOURCE(pps_select),
554f67bf662SJonathan Lemon 		.offset = 0x00130000, .size = 0x1000,
555f67bf662SJonathan Lemon 	},
556f67bf662SJonathan Lemon 	{
557a509a7c6SJonathan Lemon 		OCP_MEM_RESOURCE(sma_map1),
558e1daf0ecSJonathan Lemon 		.offset = 0x00140000, .size = 0x1000,
559e1daf0ecSJonathan Lemon 	},
560e1daf0ecSJonathan Lemon 	{
561a509a7c6SJonathan Lemon 		OCP_MEM_RESOURCE(sma_map2),
562a509a7c6SJonathan Lemon 		.offset = 0x00220000, .size = 0x1000,
563a509a7c6SJonathan Lemon 	},
564a509a7c6SJonathan Lemon 	{
565773bda96SJonathan Lemon 		OCP_I2C_RESOURCE(i2c_ctrl),
566773bda96SJonathan Lemon 		.offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
5671618df6aSJonathan Lemon 		.extra = &(struct ptp_ocp_i2c_info) {
5681618df6aSJonathan Lemon 			.name = "xiic-i2c",
5691618df6aSJonathan Lemon 			.fixed_rate = 50000000,
5700cfcdd1eSJonathan Lemon 			.data_size = sizeof(struct xiic_i2c_platform_data),
5710cfcdd1eSJonathan Lemon 			.data = &(struct xiic_i2c_platform_data) {
5720cfcdd1eSJonathan Lemon 				.num_devices = 2,
5730cfcdd1eSJonathan Lemon 				.devices = (struct i2c_board_info[]) {
5740cfcdd1eSJonathan Lemon 					{ I2C_BOARD_INFO("24c02", 0x50) },
5750cfcdd1eSJonathan Lemon 					{ I2C_BOARD_INFO("24mac402", 0x58),
5760cfcdd1eSJonathan Lemon 					  .platform_data = "mac" },
5770cfcdd1eSJonathan Lemon 				},
5780cfcdd1eSJonathan Lemon 			},
5791618df6aSJonathan Lemon 		},
580773bda96SJonathan Lemon 	},
581773bda96SJonathan Lemon 	{
582ef0cfb34SJonathan Lemon 		OCP_SERIAL_RESOURCE(gnss_port),
583773bda96SJonathan Lemon 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
584773bda96SJonathan Lemon 	},
585773bda96SJonathan Lemon 	{
58671d7e085SJonathan Lemon 		OCP_SERIAL_RESOURCE(gnss2_port),
58771d7e085SJonathan Lemon 		.offset = 0x00170000 + 0x1000, .irq_vec = 4,
58871d7e085SJonathan Lemon 	},
58971d7e085SJonathan Lemon 	{
590773bda96SJonathan Lemon 		OCP_SERIAL_RESOURCE(mac_port),
591773bda96SJonathan Lemon 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
592773bda96SJonathan Lemon 	},
593773bda96SJonathan Lemon 	{
594e3516bb4SJonathan Lemon 		OCP_SERIAL_RESOURCE(nmea_port),
595e3516bb4SJonathan Lemon 		.offset = 0x00190000 + 0x1000, .irq_vec = 10,
596e3516bb4SJonathan Lemon 	},
597e3516bb4SJonathan Lemon 	{
598773bda96SJonathan Lemon 		OCP_SPI_RESOURCE(spi_flash),
599773bda96SJonathan Lemon 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
600773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_flash_info) {
601773bda96SJonathan Lemon 			.name = "xilinx_spi", .pci_offset = 0,
602773bda96SJonathan Lemon 			.data_size = sizeof(struct xspi_platform_data),
603773bda96SJonathan Lemon 			.data = &(struct xspi_platform_data) {
604773bda96SJonathan Lemon 				.num_chipselect = 1,
605773bda96SJonathan Lemon 				.bits_per_word = 8,
606773bda96SJonathan Lemon 				.num_devices = 1,
607773bda96SJonathan Lemon 				.devices = &(struct spi_board_info) {
608773bda96SJonathan Lemon 					.modalias = "spi-nor",
609773bda96SJonathan Lemon 				},
610773bda96SJonathan Lemon 			},
611773bda96SJonathan Lemon 		},
612773bda96SJonathan Lemon 	},
613773bda96SJonathan Lemon 	{
6142407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[0]),
6152407f5d6SJonathan Lemon 		.offset = 0x01200000, .size = 0x10000,
6162407f5d6SJonathan Lemon 	},
6172407f5d6SJonathan Lemon 	{
6182407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[1]),
6192407f5d6SJonathan Lemon 		.offset = 0x01210000, .size = 0x10000,
6202407f5d6SJonathan Lemon 	},
6212407f5d6SJonathan Lemon 	{
6222407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[2]),
6232407f5d6SJonathan Lemon 		.offset = 0x01220000, .size = 0x10000,
6242407f5d6SJonathan Lemon 	},
6252407f5d6SJonathan Lemon 	{
6262407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[3]),
6272407f5d6SJonathan Lemon 		.offset = 0x01230000, .size = 0x10000,
6282407f5d6SJonathan Lemon 	},
6292407f5d6SJonathan Lemon 	{
630773bda96SJonathan Lemon 		.setup = ptp_ocp_fb_board_init,
631773bda96SJonathan Lemon 	},
632773bda96SJonathan Lemon 	{ }
633773bda96SJonathan Lemon };
634773bda96SJonathan Lemon 
635773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = {
636773bda96SJonathan Lemon 	{ PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
637773bda96SJonathan Lemon 	{ 0 }
638773bda96SJonathan Lemon };
639773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
640773bda96SJonathan Lemon 
641773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock);
642773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr);
643773bda96SJonathan Lemon 
644e1daf0ecSJonathan Lemon struct ocp_selector {
645773bda96SJonathan Lemon 	const char *name;
646773bda96SJonathan Lemon 	int value;
647e1daf0ecSJonathan Lemon };
648e1daf0ecSJonathan Lemon 
649e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = {
650773bda96SJonathan Lemon 	{ .name = "NONE",	.value = 0 },
651773bda96SJonathan Lemon 	{ .name = "TOD",	.value = 1 },
652773bda96SJonathan Lemon 	{ .name = "IRIG",	.value = 2 },
653773bda96SJonathan Lemon 	{ .name = "PPS",	.value = 3 },
654773bda96SJonathan Lemon 	{ .name = "PTP",	.value = 4 },
655773bda96SJonathan Lemon 	{ .name = "RTC",	.value = 5 },
656773bda96SJonathan Lemon 	{ .name = "DCF",	.value = 6 },
657773bda96SJonathan Lemon 	{ .name = "REGS",	.value = 0xfe },
658773bda96SJonathan Lemon 	{ .name = "EXT",	.value = 0xff },
659e1daf0ecSJonathan Lemon 	{ }
660e1daf0ecSJonathan Lemon };
661e1daf0ecSJonathan Lemon 
662a509a7c6SJonathan Lemon #define SMA_ENABLE		BIT(15)
663a509a7c6SJonathan Lemon #define SMA_SELECT_MASK		((1U << 15) - 1)
664b2c4f0acSJonathan Lemon #define SMA_DISABLE		0x10000
665a509a7c6SJonathan Lemon 
666e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = {
6672407f5d6SJonathan Lemon 	{ .name = "10Mhz",	.value = 0x0000 },
6682407f5d6SJonathan Lemon 	{ .name = "PPS1",	.value = 0x0001 },
6692407f5d6SJonathan Lemon 	{ .name = "PPS2",	.value = 0x0002 },
6702407f5d6SJonathan Lemon 	{ .name = "TS1",	.value = 0x0004 },
6712407f5d6SJonathan Lemon 	{ .name = "TS2",	.value = 0x0008 },
6722407f5d6SJonathan Lemon 	{ .name = "IRIG",	.value = 0x0010 },
6732407f5d6SJonathan Lemon 	{ .name = "DCF",	.value = 0x0020 },
6740fa3ff7eSJonathan Lemon 	{ .name = "TS3",	.value = 0x0040 },
6750fa3ff7eSJonathan Lemon 	{ .name = "TS4",	.value = 0x0080 },
6762407f5d6SJonathan Lemon 	{ .name = "FREQ1",	.value = 0x0100 },
6772407f5d6SJonathan Lemon 	{ .name = "FREQ2",	.value = 0x0200 },
6782407f5d6SJonathan Lemon 	{ .name = "FREQ3",	.value = 0x0400 },
6792407f5d6SJonathan Lemon 	{ .name = "FREQ4",	.value = 0x0800 },
680b2c4f0acSJonathan Lemon 	{ .name = "None",	.value = SMA_DISABLE },
681e1daf0ecSJonathan Lemon 	{ }
682e1daf0ecSJonathan Lemon };
683e1daf0ecSJonathan Lemon 
684e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = {
685b325af3cSJonathan Lemon 	{ .name = "10Mhz",	.value = 0x0000 },
686b325af3cSJonathan Lemon 	{ .name = "PHC",	.value = 0x0001 },
687b325af3cSJonathan Lemon 	{ .name = "MAC",	.value = 0x0002 },
688b325af3cSJonathan Lemon 	{ .name = "GNSS1",	.value = 0x0004 },
689b325af3cSJonathan Lemon 	{ .name = "GNSS2",	.value = 0x0008 },
690b325af3cSJonathan Lemon 	{ .name = "IRIG",	.value = 0x0010 },
691b325af3cSJonathan Lemon 	{ .name = "DCF",	.value = 0x0020 },
692b325af3cSJonathan Lemon 	{ .name = "GEN1",	.value = 0x0040 },
693b325af3cSJonathan Lemon 	{ .name = "GEN2",	.value = 0x0080 },
694b325af3cSJonathan Lemon 	{ .name = "GEN3",	.value = 0x0100 },
695b325af3cSJonathan Lemon 	{ .name = "GEN4",	.value = 0x0200 },
696cd09193fSJonathan Lemon 	{ .name = "GND",	.value = 0x2000 },
697cd09193fSJonathan Lemon 	{ .name = "VCC",	.value = 0x4000 },
698e1daf0ecSJonathan Lemon 	{ }
699773bda96SJonathan Lemon };
700773bda96SJonathan Lemon 
701773bda96SJonathan Lemon static const char *
702e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val)
703773bda96SJonathan Lemon {
704773bda96SJonathan Lemon 	int i;
705773bda96SJonathan Lemon 
706e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++)
707e1daf0ecSJonathan Lemon 		if (tbl[i].value == val)
708e1daf0ecSJonathan Lemon 			return tbl[i].name;
709773bda96SJonathan Lemon 	return NULL;
710773bda96SJonathan Lemon }
711773bda96SJonathan Lemon 
712773bda96SJonathan Lemon static int
713e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name)
714773bda96SJonathan Lemon {
715e1daf0ecSJonathan Lemon 	const char *select;
716773bda96SJonathan Lemon 	int i;
717773bda96SJonathan Lemon 
718e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++) {
719e1daf0ecSJonathan Lemon 		select = tbl[i].name;
720e1daf0ecSJonathan Lemon 		if (!strncasecmp(name, select, strlen(select)))
721e1daf0ecSJonathan Lemon 			return tbl[i].value;
722773bda96SJonathan Lemon 	}
723773bda96SJonathan Lemon 	return -EINVAL;
724773bda96SJonathan Lemon }
725773bda96SJonathan Lemon 
726e1daf0ecSJonathan Lemon static ssize_t
727e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf)
728e1daf0ecSJonathan Lemon {
729e1daf0ecSJonathan Lemon 	ssize_t count;
730e1daf0ecSJonathan Lemon 	int i;
731e1daf0ecSJonathan Lemon 
732e1daf0ecSJonathan Lemon 	count = 0;
733e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++)
734e1daf0ecSJonathan Lemon 		count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
735e1daf0ecSJonathan Lemon 	if (count)
736e1daf0ecSJonathan Lemon 		count--;
737e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "\n");
738e1daf0ecSJonathan Lemon 	return count;
739e1daf0ecSJonathan Lemon }
740e1daf0ecSJonathan Lemon 
741a7e1abadSJonathan Lemon static int
742a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
743a7e1abadSJonathan Lemon 			 struct ptp_system_timestamp *sts)
744a7e1abadSJonathan Lemon {
745a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
746a7e1abadSJonathan Lemon 	int i;
747a7e1abadSJonathan Lemon 
748a7e1abadSJonathan Lemon 	ptp_read_system_prets(sts);
7491acffc6eSJonathan Lemon 
7501acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
751a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
752a7e1abadSJonathan Lemon 
753a7e1abadSJonathan Lemon 	for (i = 0; i < 100; i++) {
754a7e1abadSJonathan Lemon 		ctrl = ioread32(&bp->reg->ctrl);
755a7e1abadSJonathan Lemon 		if (ctrl & OCP_CTRL_READ_TIME_DONE)
756a7e1abadSJonathan Lemon 			break;
757a7e1abadSJonathan Lemon 	}
758a7e1abadSJonathan Lemon 	ptp_read_system_postts(sts);
759a7e1abadSJonathan Lemon 
7601acffc6eSJonathan Lemon 	if (sts && bp->ts_window_adjust) {
7611acffc6eSJonathan Lemon 		s64 ns = timespec64_to_ns(&sts->post_ts);
7621acffc6eSJonathan Lemon 
7631acffc6eSJonathan Lemon 		sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
7641acffc6eSJonathan Lemon 	}
7651acffc6eSJonathan Lemon 
766a7e1abadSJonathan Lemon 	time_ns = ioread32(&bp->reg->time_ns);
767a7e1abadSJonathan Lemon 	time_sec = ioread32(&bp->reg->time_sec);
768a7e1abadSJonathan Lemon 
769a7e1abadSJonathan Lemon 	ts->tv_sec = time_sec;
770a7e1abadSJonathan Lemon 	ts->tv_nsec = time_ns;
771a7e1abadSJonathan Lemon 
772a7e1abadSJonathan Lemon 	return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
773a7e1abadSJonathan Lemon }
774a7e1abadSJonathan Lemon 
775a7e1abadSJonathan Lemon static int
776a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
777a7e1abadSJonathan Lemon 		 struct ptp_system_timestamp *sts)
778a7e1abadSJonathan Lemon {
779a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
780a7e1abadSJonathan Lemon 	unsigned long flags;
781a7e1abadSJonathan Lemon 	int err;
782a7e1abadSJonathan Lemon 
783a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
784a7e1abadSJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, ts, sts);
785a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
786a7e1abadSJonathan Lemon 
787a7e1abadSJonathan Lemon 	return err;
788a7e1abadSJonathan Lemon }
789a7e1abadSJonathan Lemon 
790a7e1abadSJonathan Lemon static void
791a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
792a7e1abadSJonathan Lemon {
793a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
794a7e1abadSJonathan Lemon 	u32 select;
795a7e1abadSJonathan Lemon 
796a7e1abadSJonathan Lemon 	time_ns = ts->tv_nsec;
797a7e1abadSJonathan Lemon 	time_sec = ts->tv_sec;
798a7e1abadSJonathan Lemon 
799a7e1abadSJonathan Lemon 	select = ioread32(&bp->reg->select);
800a7e1abadSJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
801a7e1abadSJonathan Lemon 
802a7e1abadSJonathan Lemon 	iowrite32(time_ns, &bp->reg->adjust_ns);
803a7e1abadSJonathan Lemon 	iowrite32(time_sec, &bp->reg->adjust_sec);
804a7e1abadSJonathan Lemon 
8051acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
806a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
807a7e1abadSJonathan Lemon 
808a7e1abadSJonathan Lemon 	/* restore clock selection */
809a7e1abadSJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
810a7e1abadSJonathan Lemon }
811a7e1abadSJonathan Lemon 
812a7e1abadSJonathan Lemon static int
813a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
814a7e1abadSJonathan Lemon {
815a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
816a7e1abadSJonathan Lemon 	unsigned long flags;
817a7e1abadSJonathan Lemon 
818a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
819a7e1abadSJonathan Lemon 	__ptp_ocp_settime_locked(bp, ts);
820a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
821a7e1abadSJonathan Lemon 
822a7e1abadSJonathan Lemon 	return 0;
823a7e1abadSJonathan Lemon }
824a7e1abadSJonathan Lemon 
8256d59d4faSJonathan Lemon static void
82690f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
8276d59d4faSJonathan Lemon {
8286d59d4faSJonathan Lemon 	u32 select, ctrl;
8296d59d4faSJonathan Lemon 
8306d59d4faSJonathan Lemon 	select = ioread32(&bp->reg->select);
8316d59d4faSJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
8326d59d4faSJonathan Lemon 
8336d59d4faSJonathan Lemon 	iowrite32(adj_val, &bp->reg->offset_ns);
83490f8f4c0SJonathan Lemon 	iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
8356d59d4faSJonathan Lemon 
8366d59d4faSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
8376d59d4faSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
8386d59d4faSJonathan Lemon 
8396d59d4faSJonathan Lemon 	/* restore clock selection */
8406d59d4faSJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
8416d59d4faSJonathan Lemon }
8426d59d4faSJonathan Lemon 
84390f8f4c0SJonathan Lemon static void
844da2172a9SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns)
84590f8f4c0SJonathan Lemon {
84690f8f4c0SJonathan Lemon 	struct timespec64 ts;
84790f8f4c0SJonathan Lemon 	unsigned long flags;
84890f8f4c0SJonathan Lemon 	int err;
84990f8f4c0SJonathan Lemon 
85090f8f4c0SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
85190f8f4c0SJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
85290f8f4c0SJonathan Lemon 	if (likely(!err)) {
853da2172a9SJonathan Lemon 		set_normalized_timespec64(&ts, ts.tv_sec,
854da2172a9SJonathan Lemon 					  ts.tv_nsec + delta_ns);
85590f8f4c0SJonathan Lemon 		__ptp_ocp_settime_locked(bp, &ts);
85690f8f4c0SJonathan Lemon 	}
85790f8f4c0SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
85890f8f4c0SJonathan Lemon }
85990f8f4c0SJonathan Lemon 
860a7e1abadSJonathan Lemon static int
861a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
862a7e1abadSJonathan Lemon {
863a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
864a7e1abadSJonathan Lemon 	unsigned long flags;
8656d59d4faSJonathan Lemon 	u32 adj_ns, sign;
866a7e1abadSJonathan Lemon 
86790f8f4c0SJonathan Lemon 	if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
86890f8f4c0SJonathan Lemon 		ptp_ocp_adjtime_coarse(bp, delta_ns);
86990f8f4c0SJonathan Lemon 		return 0;
87090f8f4c0SJonathan Lemon 	}
87190f8f4c0SJonathan Lemon 
8726d59d4faSJonathan Lemon 	sign = delta_ns < 0 ? BIT(31) : 0;
8736d59d4faSJonathan Lemon 	adj_ns = sign ? -delta_ns : delta_ns;
874a7e1abadSJonathan Lemon 
875a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
8766d59d4faSJonathan Lemon 	__ptp_ocp_adjtime_locked(bp, sign | adj_ns);
877a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
878a7e1abadSJonathan Lemon 
8796d59d4faSJonathan Lemon 	return 0;
880a7e1abadSJonathan Lemon }
881a7e1abadSJonathan Lemon 
882a7e1abadSJonathan Lemon static int
883a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
884a7e1abadSJonathan Lemon {
885a7e1abadSJonathan Lemon 	if (scaled_ppm == 0)
886a7e1abadSJonathan Lemon 		return 0;
887a7e1abadSJonathan Lemon 
888a7e1abadSJonathan Lemon 	return -EOPNOTSUPP;
889a7e1abadSJonathan Lemon }
890a7e1abadSJonathan Lemon 
891773bda96SJonathan Lemon static int
8926d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
893773bda96SJonathan Lemon {
894773bda96SJonathan Lemon 	return -EOPNOTSUPP;
895773bda96SJonathan Lemon }
896773bda96SJonathan Lemon 
897773bda96SJonathan Lemon static int
898773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
899773bda96SJonathan Lemon 	       int on)
900773bda96SJonathan Lemon {
901773bda96SJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
902773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = NULL;
903a62a56d0SJonathan Lemon 	u32 req;
904773bda96SJonathan Lemon 	int err;
905773bda96SJonathan Lemon 
906773bda96SJonathan Lemon 	switch (rq->type) {
907773bda96SJonathan Lemon 	case PTP_CLK_REQ_EXTTS:
908a62a56d0SJonathan Lemon 		req = OCP_REQ_TIMESTAMP;
909773bda96SJonathan Lemon 		switch (rq->extts.index) {
910773bda96SJonathan Lemon 		case 0:
911773bda96SJonathan Lemon 			ext = bp->ts0;
912773bda96SJonathan Lemon 			break;
913773bda96SJonathan Lemon 		case 1:
914773bda96SJonathan Lemon 			ext = bp->ts1;
915773bda96SJonathan Lemon 			break;
916dcf61469SJonathan Lemon 		case 2:
917dcf61469SJonathan Lemon 			ext = bp->ts2;
918dcf61469SJonathan Lemon 			break;
919a62a56d0SJonathan Lemon 		case 3:
9200fa3ff7eSJonathan Lemon 			ext = bp->ts3;
9210fa3ff7eSJonathan Lemon 			break;
9220fa3ff7eSJonathan Lemon 		case 4:
9230fa3ff7eSJonathan Lemon 			ext = bp->ts4;
9240fa3ff7eSJonathan Lemon 			break;
9250fa3ff7eSJonathan Lemon 		case 5:
926a62a56d0SJonathan Lemon 			ext = bp->pps;
927a62a56d0SJonathan Lemon 			break;
928773bda96SJonathan Lemon 		}
929773bda96SJonathan Lemon 		break;
930773bda96SJonathan Lemon 	case PTP_CLK_REQ_PPS:
931a62a56d0SJonathan Lemon 		req = OCP_REQ_PPS;
932773bda96SJonathan Lemon 		ext = bp->pps;
933773bda96SJonathan Lemon 		break;
934a62a56d0SJonathan Lemon 	case PTP_CLK_REQ_PEROUT:
9351aa66a3aSJonathan Lemon 		switch (rq->perout.index) {
9361aa66a3aSJonathan Lemon 		case 0:
937a62a56d0SJonathan Lemon 			/* This is a request for 1PPS on an output SMA.
938a62a56d0SJonathan Lemon 			 * Allow, but assume manual configuration.
939a62a56d0SJonathan Lemon 			 */
9401aa66a3aSJonathan Lemon 			if (on && (rq->perout.period.sec != 1 ||
9411aa66a3aSJonathan Lemon 				   rq->perout.period.nsec != 0))
9421aa66a3aSJonathan Lemon 				return -EINVAL;
943a62a56d0SJonathan Lemon 			return 0;
9441aa66a3aSJonathan Lemon 		case 1:
9451aa66a3aSJonathan Lemon 		case 2:
9461aa66a3aSJonathan Lemon 		case 3:
9471aa66a3aSJonathan Lemon 		case 4:
9481aa66a3aSJonathan Lemon 			req = rq->perout.index - 1;
9491aa66a3aSJonathan Lemon 			ext = bp->signal_out[req];
9501aa66a3aSJonathan Lemon 			err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
9511aa66a3aSJonathan Lemon 			if (err)
9521aa66a3aSJonathan Lemon 				return err;
9531aa66a3aSJonathan Lemon 			break;
9541aa66a3aSJonathan Lemon 		}
9551aa66a3aSJonathan Lemon 		break;
956773bda96SJonathan Lemon 	default:
957773bda96SJonathan Lemon 		return -EOPNOTSUPP;
958773bda96SJonathan Lemon 	}
959773bda96SJonathan Lemon 
960773bda96SJonathan Lemon 	err = -ENXIO;
961773bda96SJonathan Lemon 	if (ext)
962a62a56d0SJonathan Lemon 		err = ext->info->enable(ext, req, on);
963773bda96SJonathan Lemon 
964773bda96SJonathan Lemon 	return err;
965773bda96SJonathan Lemon }
966773bda96SJonathan Lemon 
9671aa66a3aSJonathan Lemon static int
9681aa66a3aSJonathan Lemon ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
9691aa66a3aSJonathan Lemon 	       enum ptp_pin_function func, unsigned chan)
9701aa66a3aSJonathan Lemon {
9711aa66a3aSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
9721aa66a3aSJonathan Lemon 	char buf[16];
9731aa66a3aSJonathan Lemon 
97405fc65f3SJonathan Lemon 	switch (func) {
97505fc65f3SJonathan Lemon 	case PTP_PF_NONE:
976d5f497b8SDan Carpenter 		snprintf(buf, sizeof(buf), "IN: None");
97705fc65f3SJonathan Lemon 		break;
97805fc65f3SJonathan Lemon 	case PTP_PF_EXTTS:
97905fc65f3SJonathan Lemon 		/* Allow timestamps, but require sysfs configuration. */
98005fc65f3SJonathan Lemon 		return 0;
98105fc65f3SJonathan Lemon 	case PTP_PF_PEROUT:
98205fc65f3SJonathan Lemon 		/* channel 0 is 1PPS from PHC.
98305fc65f3SJonathan Lemon 		 * channels 1..4 are the frequency generators.
98405fc65f3SJonathan Lemon 		 */
9851aa66a3aSJonathan Lemon 		if (chan)
986d5f497b8SDan Carpenter 			snprintf(buf, sizeof(buf), "OUT: GEN%d", chan);
9871aa66a3aSJonathan Lemon 		else
988d5f497b8SDan Carpenter 			snprintf(buf, sizeof(buf), "OUT: PHC");
98905fc65f3SJonathan Lemon 		break;
99005fc65f3SJonathan Lemon 	default:
99105fc65f3SJonathan Lemon 		return -EOPNOTSUPP;
99205fc65f3SJonathan Lemon 	}
9931aa66a3aSJonathan Lemon 
9941aa66a3aSJonathan Lemon 	return ptp_ocp_sma_store(bp, buf, pin + 1);
9951aa66a3aSJonathan Lemon }
9961aa66a3aSJonathan Lemon 
997a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = {
998a7e1abadSJonathan Lemon 	.owner		= THIS_MODULE,
999a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
1000a7e1abadSJonathan Lemon 	.max_adj	= 100000000,
1001a7e1abadSJonathan Lemon 	.gettimex64	= ptp_ocp_gettimex,
1002a7e1abadSJonathan Lemon 	.settime64	= ptp_ocp_settime,
1003a7e1abadSJonathan Lemon 	.adjtime	= ptp_ocp_adjtime,
1004a7e1abadSJonathan Lemon 	.adjfine	= ptp_ocp_null_adjfine,
10056d59d4faSJonathan Lemon 	.adjphase	= ptp_ocp_null_adjphase,
1006773bda96SJonathan Lemon 	.enable		= ptp_ocp_enable,
10071aa66a3aSJonathan Lemon 	.verify		= ptp_ocp_verify,
1008773bda96SJonathan Lemon 	.pps		= true,
10090fa3ff7eSJonathan Lemon 	.n_ext_ts	= 6,
10101aa66a3aSJonathan Lemon 	.n_per_out	= 5,
1011a7e1abadSJonathan Lemon };
1012a7e1abadSJonathan Lemon 
1013773bda96SJonathan Lemon static void
1014773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
1015773bda96SJonathan Lemon {
1016773bda96SJonathan Lemon 	u32 ctrl, select;
1017773bda96SJonathan Lemon 
1018773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
1019773bda96SJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1020773bda96SJonathan Lemon 
1021773bda96SJonathan Lemon 	iowrite32(0, &bp->reg->drift_ns);
1022773bda96SJonathan Lemon 
10231acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
1024773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
1025773bda96SJonathan Lemon 
1026773bda96SJonathan Lemon 	/* restore clock selection */
1027773bda96SJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
1028773bda96SJonathan Lemon }
1029773bda96SJonathan Lemon 
1030773bda96SJonathan Lemon static void
1031e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
1032e68462a0SVadim Fedorenko {
1033e68462a0SVadim Fedorenko 	unsigned long flags;
1034e68462a0SVadim Fedorenko 
1035e68462a0SVadim Fedorenko 	spin_lock_irqsave(&bp->lock, flags);
1036e68462a0SVadim Fedorenko 
1037e68462a0SVadim Fedorenko 	bp->utc_tai_offset = val;
1038e68462a0SVadim Fedorenko 
1039e68462a0SVadim Fedorenko 	if (bp->irig_out)
1040e68462a0SVadim Fedorenko 		iowrite32(val, &bp->irig_out->adj_sec);
1041e68462a0SVadim Fedorenko 	if (bp->dcf_out)
1042e68462a0SVadim Fedorenko 		iowrite32(val, &bp->dcf_out->adj_sec);
1043e68462a0SVadim Fedorenko 	if (bp->nmea_out)
1044e68462a0SVadim Fedorenko 		iowrite32(val, &bp->nmea_out->adj_sec);
1045e68462a0SVadim Fedorenko 
1046e68462a0SVadim Fedorenko 	spin_unlock_irqrestore(&bp->lock, flags);
1047e68462a0SVadim Fedorenko }
1048e68462a0SVadim Fedorenko 
1049e68462a0SVadim Fedorenko static void
1050773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t)
1051773bda96SJonathan Lemon {
1052773bda96SJonathan Lemon 	struct ptp_ocp *bp = from_timer(bp, t, watchdog);
1053773bda96SJonathan Lemon 	unsigned long flags;
1054e68462a0SVadim Fedorenko 	u32 status, utc_offset;
1055773bda96SJonathan Lemon 
10560d43d4f2SJonathan Lemon 	status = ioread32(&bp->pps_to_clk->status);
1057773bda96SJonathan Lemon 
1058773bda96SJonathan Lemon 	if (status & PPS_STATUS_SUPERV_ERR) {
10590d43d4f2SJonathan Lemon 		iowrite32(status, &bp->pps_to_clk->status);
1060ef0cfb34SJonathan Lemon 		if (!bp->gnss_lost) {
1061773bda96SJonathan Lemon 			spin_lock_irqsave(&bp->lock, flags);
1062773bda96SJonathan Lemon 			__ptp_ocp_clear_drift_locked(bp);
1063773bda96SJonathan Lemon 			spin_unlock_irqrestore(&bp->lock, flags);
1064ef0cfb34SJonathan Lemon 			bp->gnss_lost = ktime_get_real_seconds();
1065773bda96SJonathan Lemon 		}
1066773bda96SJonathan Lemon 
1067ef0cfb34SJonathan Lemon 	} else if (bp->gnss_lost) {
1068ef0cfb34SJonathan Lemon 		bp->gnss_lost = 0;
1069773bda96SJonathan Lemon 	}
1070773bda96SJonathan Lemon 
1071e68462a0SVadim Fedorenko 	/* if GNSS provides correct data we can rely on
1072e68462a0SVadim Fedorenko 	 * it to get leap second information
1073e68462a0SVadim Fedorenko 	 */
1074e68462a0SVadim Fedorenko 	if (bp->tod) {
1075e68462a0SVadim Fedorenko 		status = ioread32(&bp->tod->utc_status);
1076e68462a0SVadim Fedorenko 		utc_offset = status & TOD_STATUS_UTC_MASK;
1077e68462a0SVadim Fedorenko 		if (status & TOD_STATUS_UTC_VALID &&
1078e68462a0SVadim Fedorenko 		    utc_offset != bp->utc_tai_offset)
1079e68462a0SVadim Fedorenko 			ptp_ocp_utc_distribute(bp, utc_offset);
1080e68462a0SVadim Fedorenko 	}
1081e68462a0SVadim Fedorenko 
1082773bda96SJonathan Lemon 	mod_timer(&bp->watchdog, jiffies + HZ);
1083773bda96SJonathan Lemon }
1084773bda96SJonathan Lemon 
10851acffc6eSJonathan Lemon static void
10861acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
10871acffc6eSJonathan Lemon {
10881acffc6eSJonathan Lemon 	ktime_t start, end;
10891acffc6eSJonathan Lemon 	ktime_t delay;
10901acffc6eSJonathan Lemon 	u32 ctrl;
10911acffc6eSJonathan Lemon 
10921acffc6eSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
10931acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
10941acffc6eSJonathan Lemon 
10951acffc6eSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
10961acffc6eSJonathan Lemon 
10971acffc6eSJonathan Lemon 	start = ktime_get_ns();
10981acffc6eSJonathan Lemon 
10991acffc6eSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
11001acffc6eSJonathan Lemon 
11011acffc6eSJonathan Lemon 	end = ktime_get_ns();
11021acffc6eSJonathan Lemon 
11031acffc6eSJonathan Lemon 	delay = end - start;
11041acffc6eSJonathan Lemon 	bp->ts_window_adjust = (delay >> 5) * 3;
11051acffc6eSJonathan Lemon }
11061acffc6eSJonathan Lemon 
1107a7e1abadSJonathan Lemon static int
1108773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp)
1109a7e1abadSJonathan Lemon {
1110a7e1abadSJonathan Lemon 	struct timespec64 ts;
1111a7e1abadSJonathan Lemon 	bool sync;
1112a7e1abadSJonathan Lemon 	u32 ctrl;
1113a7e1abadSJonathan Lemon 
11141acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ENABLE;
1115a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
1116a7e1abadSJonathan Lemon 
1117773bda96SJonathan Lemon 	/* NO DRIFT Correction */
1118773bda96SJonathan Lemon 	/* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
1119773bda96SJonathan Lemon 	iowrite32(0x2000, &bp->reg->servo_offset_p);
1120773bda96SJonathan Lemon 	iowrite32(0x1000, &bp->reg->servo_offset_i);
1121773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_p);
1122773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_i);
1123773bda96SJonathan Lemon 
1124773bda96SJonathan Lemon 	/* latch servo values */
1125773bda96SJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_SERVO;
1126773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
1127773bda96SJonathan Lemon 
1128a7e1abadSJonathan Lemon 	if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
1129a7e1abadSJonathan Lemon 		dev_err(&bp->pdev->dev, "clock not enabled\n");
1130a7e1abadSJonathan Lemon 		return -ENODEV;
1131a7e1abadSJonathan Lemon 	}
1132a7e1abadSJonathan Lemon 
11331acffc6eSJonathan Lemon 	ptp_ocp_estimate_pci_timing(bp);
11341acffc6eSJonathan Lemon 
1135a7e1abadSJonathan Lemon 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
1136a7e1abadSJonathan Lemon 	if (!sync) {
1137065efcc5SJonathan Lemon 		ktime_get_clocktai_ts64(&ts);
1138a7e1abadSJonathan Lemon 		ptp_ocp_settime(&bp->ptp_info, &ts);
1139a7e1abadSJonathan Lemon 	}
1140a7e1abadSJonathan Lemon 
1141065efcc5SJonathan Lemon 	/* If there is a clock supervisor, then enable the watchdog */
1142065efcc5SJonathan Lemon 	if (bp->pps_to_clk) {
1143773bda96SJonathan Lemon 		timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
1144773bda96SJonathan Lemon 		mod_timer(&bp->watchdog, jiffies + HZ);
1145065efcc5SJonathan Lemon 	}
1146773bda96SJonathan Lemon 
1147a7e1abadSJonathan Lemon 	return 0;
1148a7e1abadSJonathan Lemon }
1149a7e1abadSJonathan Lemon 
1150a7e1abadSJonathan Lemon static void
1151065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp)
1152065efcc5SJonathan Lemon {
1153065efcc5SJonathan Lemon 	u32 ctrl, reg;
1154065efcc5SJonathan Lemon 
1155065efcc5SJonathan Lemon 	ctrl = ioread32(&bp->tod->ctrl);
1156065efcc5SJonathan Lemon 	ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
1157065efcc5SJonathan Lemon 	ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
1158065efcc5SJonathan Lemon 	iowrite32(ctrl, &bp->tod->ctrl);
1159065efcc5SJonathan Lemon 
1160065efcc5SJonathan Lemon 	reg = ioread32(&bp->tod->utc_status);
1161065efcc5SJonathan Lemon 	if (reg & TOD_STATUS_UTC_VALID)
1162065efcc5SJonathan Lemon 		ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
1163065efcc5SJonathan Lemon }
1164065efcc5SJonathan Lemon 
11659f492c4cSVadim Fedorenko static const char *
11669f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx)
1167a7e1abadSJonathan Lemon {
1168a7e1abadSJonathan Lemon 	static const char * const proto_name[] = {
1169a7e1abadSJonathan Lemon 		"NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
1170a7e1abadSJonathan Lemon 		"UBX", "UBX_UTC", "UBX_LS", "UBX_none"
1171a7e1abadSJonathan Lemon 	};
11729f492c4cSVadim Fedorenko 	return proto_name[idx];
11739f492c4cSVadim Fedorenko }
11749f492c4cSVadim Fedorenko 
11759f492c4cSVadim Fedorenko static const char *
11769f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx)
11779f492c4cSVadim Fedorenko {
1178a7e1abadSJonathan Lemon 	static const char * const gnss_name[] = {
1179a7e1abadSJonathan Lemon 		"ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
11809f492c4cSVadim Fedorenko 		"Unknown"
1181a7e1abadSJonathan Lemon 	};
118272f00505SDan Carpenter 	if (idx >= ARRAY_SIZE(gnss_name))
11839f492c4cSVadim Fedorenko 		idx = ARRAY_SIZE(gnss_name) - 1;
11849f492c4cSVadim Fedorenko 	return gnss_name[idx];
1185a7e1abadSJonathan Lemon }
1186a7e1abadSJonathan Lemon 
11870cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info {
11880cfcdd1eSJonathan Lemon 	struct ptp_ocp *bp;
11890cfcdd1eSJonathan Lemon 	const void * const tag;
1190773bda96SJonathan Lemon };
1191773bda96SJonathan Lemon 
11920cfcdd1eSJonathan Lemon static int
11930cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data)
11940cfcdd1eSJonathan Lemon {
11950cfcdd1eSJonathan Lemon 	const struct ptp_ocp_nvmem_match_info *info = data;
11960cfcdd1eSJonathan Lemon 
11970cfcdd1eSJonathan Lemon 	dev = dev->parent;
11980cfcdd1eSJonathan Lemon 	if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
11990cfcdd1eSJonathan Lemon 		return 0;
12000cfcdd1eSJonathan Lemon 
12010cfcdd1eSJonathan Lemon 	while ((dev = dev->parent))
12020cfcdd1eSJonathan Lemon 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
12030cfcdd1eSJonathan Lemon 			return info->bp == dev_get_drvdata(dev);
1204773bda96SJonathan Lemon 	return 0;
1205773bda96SJonathan Lemon }
1206773bda96SJonathan Lemon 
12070cfcdd1eSJonathan Lemon static inline struct nvmem_device *
12080cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
1209773bda96SJonathan Lemon {
12100cfcdd1eSJonathan Lemon 	struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
12110cfcdd1eSJonathan Lemon 
12120cfcdd1eSJonathan Lemon 	return nvmem_device_find(&info, ptp_ocp_nvmem_match);
12130cfcdd1eSJonathan Lemon }
12140cfcdd1eSJonathan Lemon 
12150cfcdd1eSJonathan Lemon static inline void
12160cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
12170cfcdd1eSJonathan Lemon {
12188f0588e8SJonathan Lemon 	if (!IS_ERR_OR_NULL(*nvmemp))
12190cfcdd1eSJonathan Lemon 		nvmem_device_put(*nvmemp);
12200cfcdd1eSJonathan Lemon 	*nvmemp = NULL;
12210cfcdd1eSJonathan Lemon }
12220cfcdd1eSJonathan Lemon 
12230cfcdd1eSJonathan Lemon static void
12240cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp)
12250cfcdd1eSJonathan Lemon {
12260cfcdd1eSJonathan Lemon 	const struct ptp_ocp_eeprom_map *map;
12270cfcdd1eSJonathan Lemon 	struct nvmem_device *nvmem;
12280cfcdd1eSJonathan Lemon 	const void *tag;
12290cfcdd1eSJonathan Lemon 	int ret;
1230773bda96SJonathan Lemon 
12311447149dSJonathan Lemon 	if (!bp->i2c_ctrl)
12321447149dSJonathan Lemon 		return;
12331447149dSJonathan Lemon 
12340cfcdd1eSJonathan Lemon 	tag = NULL;
12350cfcdd1eSJonathan Lemon 	nvmem = NULL;
1236773bda96SJonathan Lemon 
12370cfcdd1eSJonathan Lemon 	for (map = bp->eeprom_map; map->len; map++) {
12380cfcdd1eSJonathan Lemon 		if (map->tag != tag) {
12390cfcdd1eSJonathan Lemon 			tag = map->tag;
12400cfcdd1eSJonathan Lemon 			ptp_ocp_nvmem_device_put(&nvmem);
12410cfcdd1eSJonathan Lemon 		}
12420cfcdd1eSJonathan Lemon 		if (!nvmem) {
12430cfcdd1eSJonathan Lemon 			nvmem = ptp_ocp_nvmem_device_get(bp, tag);
12448f0588e8SJonathan Lemon 			if (IS_ERR(nvmem)) {
12458f0588e8SJonathan Lemon 				ret = PTR_ERR(nvmem);
12468f0588e8SJonathan Lemon 				goto fail;
12478f0588e8SJonathan Lemon 			}
1248773bda96SJonathan Lemon 		}
12490cfcdd1eSJonathan Lemon 		ret = nvmem_device_read(nvmem, map->off, map->len,
12500cfcdd1eSJonathan Lemon 					BP_MAP_ENTRY_ADDR(bp, map));
12510cfcdd1eSJonathan Lemon 		if (ret != map->len)
12528f0588e8SJonathan Lemon 			goto fail;
1253773bda96SJonathan Lemon 	}
1254773bda96SJonathan Lemon 
12550cfcdd1eSJonathan Lemon 	bp->has_eeprom_data = true;
1256773bda96SJonathan Lemon 
1257773bda96SJonathan Lemon out:
12580cfcdd1eSJonathan Lemon 	ptp_ocp_nvmem_device_put(&nvmem);
12590cfcdd1eSJonathan Lemon 	return;
12600cfcdd1eSJonathan Lemon 
12618f0588e8SJonathan Lemon fail:
12620cfcdd1eSJonathan Lemon 	dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
12630cfcdd1eSJonathan Lemon 	goto out;
12640cfcdd1eSJonathan Lemon }
12650cfcdd1eSJonathan Lemon 
12660cfcdd1eSJonathan Lemon static int
12670cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data)
12680cfcdd1eSJonathan Lemon {
12690cfcdd1eSJonathan Lemon 	return 1;
1270773bda96SJonathan Lemon }
1271773bda96SJonathan Lemon 
1272773bda96SJonathan Lemon static struct device *
1273773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp)
1274773bda96SJonathan Lemon {
1275773bda96SJonathan Lemon 	struct device *dev, *last;
1276773bda96SJonathan Lemon 
1277773bda96SJonathan Lemon 	last = NULL;
1278773bda96SJonathan Lemon 	dev = &bp->spi_flash->dev;
1279773bda96SJonathan Lemon 
1280773bda96SJonathan Lemon 	while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) {
1281773bda96SJonathan Lemon 		if (!strcmp("mtd", dev_bus_name(dev)))
1282773bda96SJonathan Lemon 			break;
1283773bda96SJonathan Lemon 		put_device(last);
1284773bda96SJonathan Lemon 		last = dev;
1285773bda96SJonathan Lemon 	}
1286773bda96SJonathan Lemon 	put_device(last);
1287773bda96SJonathan Lemon 
1288773bda96SJonathan Lemon 	return dev;
1289773bda96SJonathan Lemon }
1290773bda96SJonathan Lemon 
1291773bda96SJonathan Lemon static int
1292773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1293773bda96SJonathan Lemon 		      const struct firmware *fw)
1294773bda96SJonathan Lemon {
1295773bda96SJonathan Lemon 	struct mtd_info *mtd = dev_get_drvdata(dev);
1296773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
1297773bda96SJonathan Lemon 	size_t off, len, resid, wrote;
1298773bda96SJonathan Lemon 	struct erase_info erase;
1299773bda96SJonathan Lemon 	size_t base, blksz;
13007c807572SJonathan Lemon 	int err = 0;
1301773bda96SJonathan Lemon 
1302773bda96SJonathan Lemon 	off = 0;
1303773bda96SJonathan Lemon 	base = bp->flash_start;
1304773bda96SJonathan Lemon 	blksz = 4096;
1305773bda96SJonathan Lemon 	resid = fw->size;
1306773bda96SJonathan Lemon 
1307773bda96SJonathan Lemon 	while (resid) {
1308773bda96SJonathan Lemon 		devlink_flash_update_status_notify(devlink, "Flashing",
1309773bda96SJonathan Lemon 						   NULL, off, fw->size);
1310773bda96SJonathan Lemon 
1311773bda96SJonathan Lemon 		len = min_t(size_t, resid, blksz);
1312773bda96SJonathan Lemon 		erase.addr = base + off;
1313773bda96SJonathan Lemon 		erase.len = blksz;
1314773bda96SJonathan Lemon 
1315773bda96SJonathan Lemon 		err = mtd_erase(mtd, &erase);
1316773bda96SJonathan Lemon 		if (err)
1317773bda96SJonathan Lemon 			goto out;
1318773bda96SJonathan Lemon 
1319773bda96SJonathan Lemon 		err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]);
1320773bda96SJonathan Lemon 		if (err)
1321773bda96SJonathan Lemon 			goto out;
1322773bda96SJonathan Lemon 
1323773bda96SJonathan Lemon 		off += blksz;
1324773bda96SJonathan Lemon 		resid -= len;
1325773bda96SJonathan Lemon 	}
1326773bda96SJonathan Lemon out:
1327773bda96SJonathan Lemon 	return err;
1328773bda96SJonathan Lemon }
1329773bda96SJonathan Lemon 
1330773bda96SJonathan Lemon static int
1331773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink,
1332773bda96SJonathan Lemon 			     struct devlink_flash_update_params *params,
1333773bda96SJonathan Lemon 			     struct netlink_ext_ack *extack)
1334773bda96SJonathan Lemon {
1335773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
1336773bda96SJonathan Lemon 	struct device *dev;
1337773bda96SJonathan Lemon 	const char *msg;
1338773bda96SJonathan Lemon 	int err;
1339773bda96SJonathan Lemon 
1340773bda96SJonathan Lemon 	dev = ptp_ocp_find_flash(bp);
1341773bda96SJonathan Lemon 	if (!dev) {
1342773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1343773bda96SJonathan Lemon 		return -ENODEV;
1344773bda96SJonathan Lemon 	}
1345773bda96SJonathan Lemon 
1346773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, "Preparing to flash",
1347773bda96SJonathan Lemon 					   NULL, 0, 0);
1348773bda96SJonathan Lemon 
1349773bda96SJonathan Lemon 	err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1350773bda96SJonathan Lemon 
1351773bda96SJonathan Lemon 	msg = err ? "Flash error" : "Flash complete";
1352773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1353773bda96SJonathan Lemon 
1354773bda96SJonathan Lemon 	put_device(dev);
1355773bda96SJonathan Lemon 	return err;
1356773bda96SJonathan Lemon }
1357773bda96SJonathan Lemon 
1358773bda96SJonathan Lemon static int
1359773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1360773bda96SJonathan Lemon 			 struct netlink_ext_ack *extack)
1361773bda96SJonathan Lemon {
1362773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
1363773bda96SJonathan Lemon 	char buf[32];
1364773bda96SJonathan Lemon 	int err;
1365773bda96SJonathan Lemon 
1366773bda96SJonathan Lemon 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
1367773bda96SJonathan Lemon 	if (err)
1368773bda96SJonathan Lemon 		return err;
1369773bda96SJonathan Lemon 
1370b0ca789aSJonathan Lemon 	if (bp->fw_version & 0xffff) {
1371b0ca789aSJonathan Lemon 		sprintf(buf, "%d", bp->fw_version);
1372b0ca789aSJonathan Lemon 		err = devlink_info_version_running_put(req, "fw", buf);
1373773bda96SJonathan Lemon 	} else {
1374b0ca789aSJonathan Lemon 		sprintf(buf, "%d", bp->fw_version >> 16);
1375b0ca789aSJonathan Lemon 		err = devlink_info_version_running_put(req, "loader", buf);
1376773bda96SJonathan Lemon 	}
1377773bda96SJonathan Lemon 	if (err)
1378773bda96SJonathan Lemon 		return err;
1379773bda96SJonathan Lemon 
13800cfcdd1eSJonathan Lemon 	if (!bp->has_eeprom_data) {
13810cfcdd1eSJonathan Lemon 		ptp_ocp_read_eeprom(bp);
13820cfcdd1eSJonathan Lemon 		if (!bp->has_eeprom_data)
13830cfcdd1eSJonathan Lemon 			return 0;
13840cfcdd1eSJonathan Lemon 	}
1385773bda96SJonathan Lemon 
1386773bda96SJonathan Lemon 	sprintf(buf, "%pM", bp->serial);
1387773bda96SJonathan Lemon 	err = devlink_info_serial_number_put(req, buf);
1388773bda96SJonathan Lemon 	if (err)
1389773bda96SJonathan Lemon 		return err;
13900cfcdd1eSJonathan Lemon 
13910cfcdd1eSJonathan Lemon 	err = devlink_info_version_fixed_put(req,
13920cfcdd1eSJonathan Lemon 			DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
13930cfcdd1eSJonathan Lemon 			bp->board_id);
13940cfcdd1eSJonathan Lemon 	if (err)
13950cfcdd1eSJonathan Lemon 		return err;
1396773bda96SJonathan Lemon 
1397773bda96SJonathan Lemon 	return 0;
1398773bda96SJonathan Lemon }
1399773bda96SJonathan Lemon 
1400773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = {
1401773bda96SJonathan Lemon 	.flash_update = ptp_ocp_devlink_flash_update,
1402773bda96SJonathan Lemon 	.info_get = ptp_ocp_devlink_info_get,
1403773bda96SJonathan Lemon };
1404773bda96SJonathan Lemon 
1405773bda96SJonathan Lemon static void __iomem *
1406*8119c9eeSJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size)
1407773bda96SJonathan Lemon {
1408773bda96SJonathan Lemon 	struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1409773bda96SJonathan Lemon 
1410773bda96SJonathan Lemon 	return devm_ioremap_resource(&bp->pdev->dev, &res);
1411773bda96SJonathan Lemon }
1412773bda96SJonathan Lemon 
1413773bda96SJonathan Lemon static void __iomem *
1414773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1415773bda96SJonathan Lemon {
1416*8119c9eeSJonathan Lemon 	resource_size_t start;
1417773bda96SJonathan Lemon 
1418773bda96SJonathan Lemon 	start = pci_resource_start(bp->pdev, 0) + r->offset;
1419773bda96SJonathan Lemon 	return __ptp_ocp_get_mem(bp, start, r->size);
1420773bda96SJonathan Lemon }
1421773bda96SJonathan Lemon 
1422773bda96SJonathan Lemon static void
1423773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq)
1424773bda96SJonathan Lemon {
1425773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_IRQ(irq);
1426773bda96SJonathan Lemon 	*res = r;
1427773bda96SJonathan Lemon }
1428773bda96SJonathan Lemon 
1429773bda96SJonathan Lemon static void
1430*8119c9eeSJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, resource_size_t start, int size)
1431773bda96SJonathan Lemon {
1432773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_MEM(start, size);
1433773bda96SJonathan Lemon 	*res = r;
1434773bda96SJonathan Lemon }
1435773bda96SJonathan Lemon 
1436773bda96SJonathan Lemon static int
1437773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1438773bda96SJonathan Lemon {
1439773bda96SJonathan Lemon 	struct ptp_ocp_flash_info *info;
1440773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1441773bda96SJonathan Lemon 	struct platform_device *p;
1442773bda96SJonathan Lemon 	struct resource res[2];
1443*8119c9eeSJonathan Lemon 	resource_size_t start;
1444773bda96SJonathan Lemon 	int id;
1445773bda96SJonathan Lemon 
1446773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
1447773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
1448773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1449773bda96SJonathan Lemon 
1450773bda96SJonathan Lemon 	info = r->extra;
1451773bda96SJonathan Lemon 	id = pci_dev_id(pdev) << 1;
1452773bda96SJonathan Lemon 	id += info->pci_offset;
1453773bda96SJonathan Lemon 
1454773bda96SJonathan Lemon 	p = platform_device_register_resndata(&pdev->dev, info->name, id,
1455773bda96SJonathan Lemon 					      res, 2, info->data,
1456773bda96SJonathan Lemon 					      info->data_size);
1457773bda96SJonathan Lemon 	if (IS_ERR(p))
1458773bda96SJonathan Lemon 		return PTR_ERR(p);
1459773bda96SJonathan Lemon 
1460773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
1461773bda96SJonathan Lemon 
1462773bda96SJonathan Lemon 	return 0;
1463773bda96SJonathan Lemon }
1464773bda96SJonathan Lemon 
1465773bda96SJonathan Lemon static struct platform_device *
1466773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1467773bda96SJonathan Lemon {
14681618df6aSJonathan Lemon 	struct ptp_ocp_i2c_info *info;
1469773bda96SJonathan Lemon 	struct resource res[2];
1470*8119c9eeSJonathan Lemon 	resource_size_t start;
1471773bda96SJonathan Lemon 
14721618df6aSJonathan Lemon 	info = r->extra;
1473773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
1474773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
1475773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1476773bda96SJonathan Lemon 
14771618df6aSJonathan Lemon 	return platform_device_register_resndata(&pdev->dev, info->name,
14781618df6aSJonathan Lemon 						 id, res, 2,
14791618df6aSJonathan Lemon 						 info->data, info->data_size);
1480773bda96SJonathan Lemon }
1481773bda96SJonathan Lemon 
1482773bda96SJonathan Lemon static int
1483773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1484773bda96SJonathan Lemon {
1485773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
14861618df6aSJonathan Lemon 	struct ptp_ocp_i2c_info *info;
1487773bda96SJonathan Lemon 	struct platform_device *p;
1488773bda96SJonathan Lemon 	struct clk_hw *clk;
1489773bda96SJonathan Lemon 	char buf[32];
1490773bda96SJonathan Lemon 	int id;
1491773bda96SJonathan Lemon 
14921618df6aSJonathan Lemon 	info = r->extra;
1493773bda96SJonathan Lemon 	id = pci_dev_id(bp->pdev);
1494773bda96SJonathan Lemon 
1495773bda96SJonathan Lemon 	sprintf(buf, "AXI.%d", id);
14961618df6aSJonathan Lemon 	clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
14971618df6aSJonathan Lemon 					 info->fixed_rate);
1498773bda96SJonathan Lemon 	if (IS_ERR(clk))
1499773bda96SJonathan Lemon 		return PTR_ERR(clk);
1500773bda96SJonathan Lemon 	bp->i2c_clk = clk;
1501773bda96SJonathan Lemon 
15021618df6aSJonathan Lemon 	sprintf(buf, "%s.%d", info->name, id);
1503773bda96SJonathan Lemon 	devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1504773bda96SJonathan Lemon 	p = ptp_ocp_i2c_bus(bp->pdev, r, id);
1505773bda96SJonathan Lemon 	if (IS_ERR(p))
1506773bda96SJonathan Lemon 		return PTR_ERR(p);
1507773bda96SJonathan Lemon 
1508773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
1509773bda96SJonathan Lemon 
1510773bda96SJonathan Lemon 	return 0;
1511773bda96SJonathan Lemon }
1512773bda96SJonathan Lemon 
1513b325af3cSJonathan Lemon /* The expectation is that this is triggered only on error. */
1514b325af3cSJonathan Lemon static irqreturn_t
1515b325af3cSJonathan Lemon ptp_ocp_signal_irq(int irq, void *priv)
1516b325af3cSJonathan Lemon {
1517b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1518b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = ext->mem;
1519b325af3cSJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1520b325af3cSJonathan Lemon 	u32 enable, status;
1521b325af3cSJonathan Lemon 	int gen;
1522b325af3cSJonathan Lemon 
1523b325af3cSJonathan Lemon 	gen = ext->info->index - 1;
1524b325af3cSJonathan Lemon 
1525b325af3cSJonathan Lemon 	enable = ioread32(&reg->enable);
1526b325af3cSJonathan Lemon 	status = ioread32(&reg->status);
1527b325af3cSJonathan Lemon 
1528b325af3cSJonathan Lemon 	/* disable generator on error */
1529b325af3cSJonathan Lemon 	if (status || !enable) {
1530b325af3cSJonathan Lemon 		iowrite32(0, &reg->intr_mask);
1531b325af3cSJonathan Lemon 		iowrite32(0, &reg->enable);
1532b325af3cSJonathan Lemon 		bp->signal[gen].running = false;
1533b325af3cSJonathan Lemon 	}
1534b325af3cSJonathan Lemon 
1535b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr);	/* ack interrupt */
1536b325af3cSJonathan Lemon 
1537b325af3cSJonathan Lemon 	return IRQ_HANDLED;
1538b325af3cSJonathan Lemon }
1539b325af3cSJonathan Lemon 
1540b325af3cSJonathan Lemon static int
1541b325af3cSJonathan Lemon ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
1542b325af3cSJonathan Lemon {
1543b325af3cSJonathan Lemon 	struct ptp_system_timestamp sts;
1544b325af3cSJonathan Lemon 	struct timespec64 ts;
1545b325af3cSJonathan Lemon 	ktime_t start_ns;
1546b325af3cSJonathan Lemon 	int err;
1547b325af3cSJonathan Lemon 
1548b325af3cSJonathan Lemon 	if (!s->period)
1549b325af3cSJonathan Lemon 		return 0;
1550b325af3cSJonathan Lemon 
1551b325af3cSJonathan Lemon 	if (!s->pulse)
1552b325af3cSJonathan Lemon 		s->pulse = ktime_divns(s->period * s->duty, 100);
1553b325af3cSJonathan Lemon 
1554b325af3cSJonathan Lemon 	err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
1555b325af3cSJonathan Lemon 	if (err)
1556b325af3cSJonathan Lemon 		return err;
1557b325af3cSJonathan Lemon 
1558b325af3cSJonathan Lemon 	start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
1559b325af3cSJonathan Lemon 	if (!s->start) {
1560b325af3cSJonathan Lemon 		/* roundup() does not work on 32-bit systems */
15614bd46bb0SJonathan Lemon 		s->start = DIV64_U64_ROUND_UP(start_ns, s->period);
1562b325af3cSJonathan Lemon 		s->start = ktime_add(s->start, s->phase);
1563b325af3cSJonathan Lemon 	}
1564b325af3cSJonathan Lemon 
1565b325af3cSJonathan Lemon 	if (s->duty < 1 || s->duty > 99)
1566b325af3cSJonathan Lemon 		return -EINVAL;
1567b325af3cSJonathan Lemon 
1568b325af3cSJonathan Lemon 	if (s->pulse < 1 || s->pulse > s->period)
1569b325af3cSJonathan Lemon 		return -EINVAL;
1570b325af3cSJonathan Lemon 
1571b325af3cSJonathan Lemon 	if (s->start < start_ns)
1572b325af3cSJonathan Lemon 		return -EINVAL;
1573b325af3cSJonathan Lemon 
1574b325af3cSJonathan Lemon 	bp->signal[gen] = *s;
1575b325af3cSJonathan Lemon 
1576b325af3cSJonathan Lemon 	return 0;
1577b325af3cSJonathan Lemon }
1578b325af3cSJonathan Lemon 
1579b325af3cSJonathan Lemon static int
15801aa66a3aSJonathan Lemon ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
15811aa66a3aSJonathan Lemon 			   struct ptp_perout_request *req)
15821aa66a3aSJonathan Lemon {
15831aa66a3aSJonathan Lemon 	struct ptp_ocp_signal s = { };
15841aa66a3aSJonathan Lemon 
15851aa66a3aSJonathan Lemon 	s.polarity = bp->signal[gen].polarity;
15861aa66a3aSJonathan Lemon 	s.period = ktime_set(req->period.sec, req->period.nsec);
15871aa66a3aSJonathan Lemon 	if (!s.period)
15881aa66a3aSJonathan Lemon 		return 0;
15891aa66a3aSJonathan Lemon 
15901aa66a3aSJonathan Lemon 	if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
15911aa66a3aSJonathan Lemon 		s.pulse = ktime_set(req->on.sec, req->on.nsec);
15921aa66a3aSJonathan Lemon 		s.duty = ktime_divns(s.pulse * 100, s.period);
15931aa66a3aSJonathan Lemon 	}
15941aa66a3aSJonathan Lemon 
15951aa66a3aSJonathan Lemon 	if (req->flags & PTP_PEROUT_PHASE)
15961aa66a3aSJonathan Lemon 		s.phase = ktime_set(req->phase.sec, req->phase.nsec);
15971aa66a3aSJonathan Lemon 	else
15981aa66a3aSJonathan Lemon 		s.start = ktime_set(req->start.sec, req->start.nsec);
15991aa66a3aSJonathan Lemon 
16001aa66a3aSJonathan Lemon 	return ptp_ocp_signal_set(bp, gen, &s);
16011aa66a3aSJonathan Lemon }
16021aa66a3aSJonathan Lemon 
16031aa66a3aSJonathan Lemon static int
1604b325af3cSJonathan Lemon ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
1605b325af3cSJonathan Lemon {
1606b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1607b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = ext->mem;
1608b325af3cSJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1609b325af3cSJonathan Lemon 	struct timespec64 ts;
1610b325af3cSJonathan Lemon 	int gen;
1611b325af3cSJonathan Lemon 
1612b325af3cSJonathan Lemon 	gen = ext->info->index - 1;
1613b325af3cSJonathan Lemon 
1614b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr_mask);
1615b325af3cSJonathan Lemon 	iowrite32(0, &reg->enable);
1616b325af3cSJonathan Lemon 	bp->signal[gen].running = false;
1617b325af3cSJonathan Lemon 	if (!enable)
1618b325af3cSJonathan Lemon 		return 0;
1619b325af3cSJonathan Lemon 
1620b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].start);
1621b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->start_sec);
1622b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->start_ns);
1623b325af3cSJonathan Lemon 
1624b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].period);
1625b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->period_sec);
1626b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->period_ns);
1627b325af3cSJonathan Lemon 
1628b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].pulse);
1629b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->pulse_sec);
1630b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->pulse_ns);
1631b325af3cSJonathan Lemon 
1632b325af3cSJonathan Lemon 	iowrite32(bp->signal[gen].polarity, &reg->polarity);
1633b325af3cSJonathan Lemon 	iowrite32(0, &reg->repeat_count);
1634b325af3cSJonathan Lemon 
1635b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr);		/* clear interrupt state */
1636b325af3cSJonathan Lemon 	iowrite32(1, &reg->intr_mask);		/* enable interrupt */
1637b325af3cSJonathan Lemon 	iowrite32(3, &reg->enable);		/* valid & enable */
1638b325af3cSJonathan Lemon 
1639b325af3cSJonathan Lemon 	bp->signal[gen].running = true;
1640b325af3cSJonathan Lemon 
1641b325af3cSJonathan Lemon 	return 0;
1642b325af3cSJonathan Lemon }
1643b325af3cSJonathan Lemon 
1644773bda96SJonathan Lemon static irqreturn_t
1645773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv)
1646773bda96SJonathan Lemon {
1647773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1648773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1649773bda96SJonathan Lemon 	struct ptp_clock_event ev;
1650773bda96SJonathan Lemon 	u32 sec, nsec;
1651773bda96SJonathan Lemon 
1652a62a56d0SJonathan Lemon 	if (ext == ext->bp->pps) {
1653a62a56d0SJonathan Lemon 		if (ext->bp->pps_req_map & OCP_REQ_PPS) {
1654a62a56d0SJonathan Lemon 			ev.type = PTP_CLOCK_PPS;
1655a62a56d0SJonathan Lemon 			ptp_clock_event(ext->bp->ptp, &ev);
1656a62a56d0SJonathan Lemon 		}
1657a62a56d0SJonathan Lemon 
1658a62a56d0SJonathan Lemon 		if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
1659a62a56d0SJonathan Lemon 			goto out;
1660a62a56d0SJonathan Lemon 	}
1661a62a56d0SJonathan Lemon 
1662773bda96SJonathan Lemon 	/* XXX should fix API - this converts s/ns -> ts -> s/ns */
1663773bda96SJonathan Lemon 	sec = ioread32(&reg->time_sec);
1664773bda96SJonathan Lemon 	nsec = ioread32(&reg->time_ns);
1665773bda96SJonathan Lemon 
1666773bda96SJonathan Lemon 	ev.type = PTP_CLOCK_EXTTS;
1667773bda96SJonathan Lemon 	ev.index = ext->info->index;
16681acffc6eSJonathan Lemon 	ev.timestamp = sec * NSEC_PER_SEC + nsec;
1669773bda96SJonathan Lemon 
1670773bda96SJonathan Lemon 	ptp_clock_event(ext->bp->ptp, &ev);
1671773bda96SJonathan Lemon 
1672a62a56d0SJonathan Lemon out:
1673773bda96SJonathan Lemon 	iowrite32(1, &reg->intr);	/* write 1 to ack */
1674773bda96SJonathan Lemon 
1675773bda96SJonathan Lemon 	return IRQ_HANDLED;
1676773bda96SJonathan Lemon }
1677773bda96SJonathan Lemon 
1678773bda96SJonathan Lemon static int
1679a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
1680773bda96SJonathan Lemon {
1681773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1682773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1683a62a56d0SJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1684a62a56d0SJonathan Lemon 
1685a62a56d0SJonathan Lemon 	if (ext == bp->pps) {
1686a62a56d0SJonathan Lemon 		u32 old_map = bp->pps_req_map;
1687a62a56d0SJonathan Lemon 
1688a62a56d0SJonathan Lemon 		if (enable)
1689a62a56d0SJonathan Lemon 			bp->pps_req_map |= req;
1690a62a56d0SJonathan Lemon 		else
1691a62a56d0SJonathan Lemon 			bp->pps_req_map &= ~req;
1692a62a56d0SJonathan Lemon 
1693a62a56d0SJonathan Lemon 		/* if no state change, just return */
1694a62a56d0SJonathan Lemon 		if ((!!old_map ^ !!bp->pps_req_map) == 0)
1695a62a56d0SJonathan Lemon 			return 0;
1696a62a56d0SJonathan Lemon 	}
1697773bda96SJonathan Lemon 
1698773bda96SJonathan Lemon 	if (enable) {
1699773bda96SJonathan Lemon 		iowrite32(1, &reg->enable);
1700773bda96SJonathan Lemon 		iowrite32(1, &reg->intr_mask);
1701773bda96SJonathan Lemon 		iowrite32(1, &reg->intr);
1702773bda96SJonathan Lemon 	} else {
1703773bda96SJonathan Lemon 		iowrite32(0, &reg->intr_mask);
1704773bda96SJonathan Lemon 		iowrite32(0, &reg->enable);
1705773bda96SJonathan Lemon 	}
1706773bda96SJonathan Lemon 
1707773bda96SJonathan Lemon 	return 0;
1708773bda96SJonathan Lemon }
1709773bda96SJonathan Lemon 
1710773bda96SJonathan Lemon static void
1711773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1712773bda96SJonathan Lemon {
1713a62a56d0SJonathan Lemon 	ext->info->enable(ext, ~0, false);
1714773bda96SJonathan Lemon 	pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1715773bda96SJonathan Lemon 	kfree(ext);
1716773bda96SJonathan Lemon }
1717773bda96SJonathan Lemon 
1718773bda96SJonathan Lemon static int
1719773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
1720773bda96SJonathan Lemon {
1721773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1722773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext;
1723773bda96SJonathan Lemon 	int err;
1724773bda96SJonathan Lemon 
1725773bda96SJonathan Lemon 	ext = kzalloc(sizeof(*ext), GFP_KERNEL);
1726773bda96SJonathan Lemon 	if (!ext)
1727773bda96SJonathan Lemon 		return -ENOMEM;
1728773bda96SJonathan Lemon 
1729773bda96SJonathan Lemon 	ext->mem = ptp_ocp_get_mem(bp, r);
1730c7521d3aSDan Carpenter 	if (IS_ERR(ext->mem)) {
1731c7521d3aSDan Carpenter 		err = PTR_ERR(ext->mem);
1732773bda96SJonathan Lemon 		goto out;
1733c7521d3aSDan Carpenter 	}
1734773bda96SJonathan Lemon 
1735773bda96SJonathan Lemon 	ext->bp = bp;
1736773bda96SJonathan Lemon 	ext->info = r->extra;
1737773bda96SJonathan Lemon 	ext->irq_vec = r->irq_vec;
1738773bda96SJonathan Lemon 
1739773bda96SJonathan Lemon 	err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
174056ec4403SJonathan Lemon 			      ext, "ocp%d.%s", bp->id, r->name);
1741773bda96SJonathan Lemon 	if (err) {
1742773bda96SJonathan Lemon 		dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
1743773bda96SJonathan Lemon 		goto out;
1744773bda96SJonathan Lemon 	}
1745773bda96SJonathan Lemon 
1746773bda96SJonathan Lemon 	bp_assign_entry(bp, r, ext);
1747773bda96SJonathan Lemon 
1748773bda96SJonathan Lemon 	return 0;
1749773bda96SJonathan Lemon 
1750773bda96SJonathan Lemon out:
1751773bda96SJonathan Lemon 	kfree(ext);
1752773bda96SJonathan Lemon 	return err;
1753773bda96SJonathan Lemon }
1754773bda96SJonathan Lemon 
1755773bda96SJonathan Lemon static int
1756773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
1757773bda96SJonathan Lemon {
1758773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1759773bda96SJonathan Lemon 	struct uart_8250_port uart;
1760773bda96SJonathan Lemon 
1761773bda96SJonathan Lemon 	/* Setting UPF_IOREMAP and leaving port.membase unspecified lets
1762773bda96SJonathan Lemon 	 * the serial port device claim and release the pci resource.
1763773bda96SJonathan Lemon 	 */
1764773bda96SJonathan Lemon 	memset(&uart, 0, sizeof(uart));
1765773bda96SJonathan Lemon 	uart.port.dev = &pdev->dev;
1766773bda96SJonathan Lemon 	uart.port.iotype = UPIO_MEM;
1767773bda96SJonathan Lemon 	uart.port.regshift = 2;
1768773bda96SJonathan Lemon 	uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
1769773bda96SJonathan Lemon 	uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
1770773bda96SJonathan Lemon 	uart.port.uartclk = 50000000;
1771c17c4059SJonathan Lemon 	uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
1772773bda96SJonathan Lemon 	uart.port.type = PORT_16550A;
1773773bda96SJonathan Lemon 
1774773bda96SJonathan Lemon 	return serial8250_register_8250_port(&uart);
1775773bda96SJonathan Lemon }
1776773bda96SJonathan Lemon 
1777773bda96SJonathan Lemon static int
1778773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
1779773bda96SJonathan Lemon {
1780773bda96SJonathan Lemon 	int port;
1781773bda96SJonathan Lemon 
1782773bda96SJonathan Lemon 	port = ptp_ocp_serial_line(bp, r);
1783773bda96SJonathan Lemon 	if (port < 0)
1784773bda96SJonathan Lemon 		return port;
1785773bda96SJonathan Lemon 
1786773bda96SJonathan Lemon 	bp_assign_entry(bp, r, port);
1787773bda96SJonathan Lemon 
1788773bda96SJonathan Lemon 	return 0;
1789773bda96SJonathan Lemon }
1790773bda96SJonathan Lemon 
1791773bda96SJonathan Lemon static int
1792773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1793773bda96SJonathan Lemon {
1794773bda96SJonathan Lemon 	void __iomem *mem;
1795773bda96SJonathan Lemon 
1796773bda96SJonathan Lemon 	mem = ptp_ocp_get_mem(bp, r);
1797c7521d3aSDan Carpenter 	if (IS_ERR(mem))
1798c7521d3aSDan Carpenter 		return PTR_ERR(mem);
1799773bda96SJonathan Lemon 
1800773bda96SJonathan Lemon 	bp_assign_entry(bp, r, mem);
1801773bda96SJonathan Lemon 
1802773bda96SJonathan Lemon 	return 0;
1803773bda96SJonathan Lemon }
1804773bda96SJonathan Lemon 
1805e3516bb4SJonathan Lemon static void
1806e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
1807e3516bb4SJonathan Lemon {
1808e3516bb4SJonathan Lemon 	if (!bp->nmea_out)
1809e3516bb4SJonathan Lemon 		return;
1810e3516bb4SJonathan Lemon 
1811e3516bb4SJonathan Lemon 	iowrite32(0, &bp->nmea_out->ctrl);		/* disable */
1812e3516bb4SJonathan Lemon 	iowrite32(7, &bp->nmea_out->uart_baud);		/* 115200 */
1813e3516bb4SJonathan Lemon 	iowrite32(1, &bp->nmea_out->ctrl);		/* enable */
1814e3516bb4SJonathan Lemon }
1815e3516bb4SJonathan Lemon 
1816a509a7c6SJonathan Lemon static void
1817b325af3cSJonathan Lemon _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
1818b325af3cSJonathan Lemon {
1819b325af3cSJonathan Lemon 	u32 val;
1820b325af3cSJonathan Lemon 
1821b325af3cSJonathan Lemon 	iowrite32(0, &reg->enable);		/* disable */
1822b325af3cSJonathan Lemon 
1823b325af3cSJonathan Lemon 	val = ioread32(&reg->polarity);
1824b325af3cSJonathan Lemon 	s->polarity = val ? true : false;
1825b325af3cSJonathan Lemon 	s->duty = 50;
1826b325af3cSJonathan Lemon }
1827b325af3cSJonathan Lemon 
1828b325af3cSJonathan Lemon static void
1829b325af3cSJonathan Lemon ptp_ocp_signal_init(struct ptp_ocp *bp)
1830b325af3cSJonathan Lemon {
1831b325af3cSJonathan Lemon 	int i;
1832b325af3cSJonathan Lemon 
1833b325af3cSJonathan Lemon 	for (i = 0; i < 4; i++)
1834b325af3cSJonathan Lemon 		if (bp->signal_out[i])
1835b325af3cSJonathan Lemon 			_ptp_ocp_signal_init(&bp->signal[i],
1836b325af3cSJonathan Lemon 					     bp->signal_out[i]->mem);
1837b325af3cSJonathan Lemon }
1838b325af3cSJonathan Lemon 
1839b325af3cSJonathan Lemon static void
1840c2239294SJonathan Lemon ptp_ocp_attr_group_del(struct ptp_ocp *bp)
1841c2239294SJonathan Lemon {
1842c2239294SJonathan Lemon 	sysfs_remove_groups(&bp->dev.kobj, bp->attr_group);
1843c2239294SJonathan Lemon 	kfree(bp->attr_group);
1844c2239294SJonathan Lemon }
1845c2239294SJonathan Lemon 
1846c2239294SJonathan Lemon static int
1847c2239294SJonathan Lemon ptp_ocp_attr_group_add(struct ptp_ocp *bp,
1848c2239294SJonathan Lemon 		       const struct ocp_attr_group *attr_tbl)
1849c2239294SJonathan Lemon {
1850c2239294SJonathan Lemon 	int count, i;
1851c2239294SJonathan Lemon 	int err;
1852c2239294SJonathan Lemon 
1853c2239294SJonathan Lemon 	count = 0;
1854c2239294SJonathan Lemon 	for (i = 0; attr_tbl[i].cap; i++)
1855c2239294SJonathan Lemon 		if (attr_tbl[i].cap & bp->fw_cap)
1856c2239294SJonathan Lemon 			count++;
1857c2239294SJonathan Lemon 
1858c2239294SJonathan Lemon 	bp->attr_group = kcalloc(count + 1, sizeof(struct attribute_group *),
1859c2239294SJonathan Lemon 				 GFP_KERNEL);
1860c2239294SJonathan Lemon 	if (!bp->attr_group)
1861c2239294SJonathan Lemon 		return -ENOMEM;
1862c2239294SJonathan Lemon 
1863c2239294SJonathan Lemon 	count = 0;
1864c2239294SJonathan Lemon 	for (i = 0; attr_tbl[i].cap; i++)
1865c2239294SJonathan Lemon 		if (attr_tbl[i].cap & bp->fw_cap)
1866c2239294SJonathan Lemon 			bp->attr_group[count++] = attr_tbl[i].group;
1867c2239294SJonathan Lemon 
1868c2239294SJonathan Lemon 	err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group);
1869c2239294SJonathan Lemon 	if (err)
1870c2239294SJonathan Lemon 		bp->attr_group[0] = NULL;
1871c2239294SJonathan Lemon 
1872c2239294SJonathan Lemon 	return err;
1873c2239294SJonathan Lemon }
1874c2239294SJonathan Lemon 
1875c2239294SJonathan Lemon static void
1876a509a7c6SJonathan Lemon ptp_ocp_sma_init(struct ptp_ocp *bp)
1877a509a7c6SJonathan Lemon {
1878a509a7c6SJonathan Lemon 	u32 reg;
1879a509a7c6SJonathan Lemon 	int i;
1880a509a7c6SJonathan Lemon 
1881a509a7c6SJonathan Lemon 	/* defaults */
1882a509a7c6SJonathan Lemon 	bp->sma[0].mode = SMA_MODE_IN;
1883a509a7c6SJonathan Lemon 	bp->sma[1].mode = SMA_MODE_IN;
1884a509a7c6SJonathan Lemon 	bp->sma[2].mode = SMA_MODE_OUT;
1885a509a7c6SJonathan Lemon 	bp->sma[3].mode = SMA_MODE_OUT;
1886a509a7c6SJonathan Lemon 
1887a509a7c6SJonathan Lemon 	/* If no SMA1 map, the pin functions and directions are fixed. */
1888a509a7c6SJonathan Lemon 	if (!bp->sma_map1) {
1889a509a7c6SJonathan Lemon 		for (i = 0; i < 4; i++) {
1890a509a7c6SJonathan Lemon 			bp->sma[i].fixed_fcn = true;
1891a509a7c6SJonathan Lemon 			bp->sma[i].fixed_dir = true;
1892a509a7c6SJonathan Lemon 		}
1893a509a7c6SJonathan Lemon 		return;
1894a509a7c6SJonathan Lemon 	}
1895a509a7c6SJonathan Lemon 
1896a509a7c6SJonathan Lemon 	/* If SMA2 GPIO output map is all 1, it is not present.
1897a509a7c6SJonathan Lemon 	 * This indicates the firmware has fixed direction SMA pins.
1898a509a7c6SJonathan Lemon 	 */
1899a509a7c6SJonathan Lemon 	reg = ioread32(&bp->sma_map2->gpio2);
1900a509a7c6SJonathan Lemon 	if (reg == 0xffffffff) {
1901a509a7c6SJonathan Lemon 		for (i = 0; i < 4; i++)
1902a509a7c6SJonathan Lemon 			bp->sma[i].fixed_dir = true;
1903a509a7c6SJonathan Lemon 	} else {
1904a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio1);
1905a509a7c6SJonathan Lemon 		bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
1906a509a7c6SJonathan Lemon 		bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
1907a509a7c6SJonathan Lemon 
1908a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio2);
1909a509a7c6SJonathan Lemon 		bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
1910a509a7c6SJonathan Lemon 		bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
1911a509a7c6SJonathan Lemon 	}
1912a509a7c6SJonathan Lemon }
1913a509a7c6SJonathan Lemon 
19141aa66a3aSJonathan Lemon static int
19151aa66a3aSJonathan Lemon ptp_ocp_fb_set_pins(struct ptp_ocp *bp)
19161aa66a3aSJonathan Lemon {
19171aa66a3aSJonathan Lemon 	struct ptp_pin_desc *config;
19181aa66a3aSJonathan Lemon 	int i;
19191aa66a3aSJonathan Lemon 
19201aa66a3aSJonathan Lemon 	config = kzalloc(sizeof(*config) * 4, GFP_KERNEL);
19211aa66a3aSJonathan Lemon 	if (!config)
19221aa66a3aSJonathan Lemon 		return -ENOMEM;
19231aa66a3aSJonathan Lemon 
19241aa66a3aSJonathan Lemon 	for (i = 0; i < 4; i++) {
19251aa66a3aSJonathan Lemon 		sprintf(config[i].name, "sma%d", i + 1);
19261aa66a3aSJonathan Lemon 		config[i].index = i;
19271aa66a3aSJonathan Lemon 	}
19281aa66a3aSJonathan Lemon 
19291aa66a3aSJonathan Lemon 	bp->ptp_info.n_pins = 4;
19301aa66a3aSJonathan Lemon 	bp->ptp_info.pin_config = config;
19311aa66a3aSJonathan Lemon 
19321aa66a3aSJonathan Lemon 	return 0;
19331aa66a3aSJonathan Lemon }
19341aa66a3aSJonathan Lemon 
1935773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */
1936773bda96SJonathan Lemon static int
1937773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
1938773bda96SJonathan Lemon {
19391aa66a3aSJonathan Lemon 	int ver, err;
1940b325af3cSJonathan Lemon 
1941773bda96SJonathan Lemon 	bp->flash_start = 1024 * 4096;
19420cfcdd1eSJonathan Lemon 	bp->eeprom_map = fb_eeprom_map;
1943b0ca789aSJonathan Lemon 	bp->fw_version = ioread32(&bp->image->version);
1944c205d53cSJonathan Lemon 	bp->fw_cap = OCP_CAP_BASIC;
1945773bda96SJonathan Lemon 
1946b325af3cSJonathan Lemon 	ver = bp->fw_version & 0xffff;
1947b325af3cSJonathan Lemon 	if (ver >= 19)
1948b325af3cSJonathan Lemon 		bp->fw_cap |= OCP_CAP_SIGNAL;
19492407f5d6SJonathan Lemon 	if (ver >= 20)
19502407f5d6SJonathan Lemon 		bp->fw_cap |= OCP_CAP_FREQ;
1951b325af3cSJonathan Lemon 
1952065efcc5SJonathan Lemon 	ptp_ocp_tod_init(bp);
1953e3516bb4SJonathan Lemon 	ptp_ocp_nmea_out_init(bp);
1954a509a7c6SJonathan Lemon 	ptp_ocp_sma_init(bp);
1955b325af3cSJonathan Lemon 	ptp_ocp_signal_init(bp);
1956065efcc5SJonathan Lemon 
1957c2239294SJonathan Lemon 	err = ptp_ocp_attr_group_add(bp, fb_timecard_groups);
1958c2239294SJonathan Lemon 	if (err)
1959c2239294SJonathan Lemon 		return err;
1960c2239294SJonathan Lemon 
19611aa66a3aSJonathan Lemon 	err = ptp_ocp_fb_set_pins(bp);
19621aa66a3aSJonathan Lemon 	if (err)
19631aa66a3aSJonathan Lemon 		return err;
19641aa66a3aSJonathan Lemon 
1965773bda96SJonathan Lemon 	return ptp_ocp_init_clock(bp);
1966773bda96SJonathan Lemon }
1967773bda96SJonathan Lemon 
196856ec4403SJonathan Lemon static bool
196956ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
197056ec4403SJonathan Lemon {
197156ec4403SJonathan Lemon 	bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
197256ec4403SJonathan Lemon 
197356ec4403SJonathan Lemon 	if (!allow)
197456ec4403SJonathan Lemon 		dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
197556ec4403SJonathan Lemon 			r->irq_vec, r->name);
197656ec4403SJonathan Lemon 	return allow;
197756ec4403SJonathan Lemon }
197856ec4403SJonathan Lemon 
1979773bda96SJonathan Lemon static int
1980773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
1981773bda96SJonathan Lemon {
1982773bda96SJonathan Lemon 	struct ocp_resource *r, *table;
1983773bda96SJonathan Lemon 	int err = 0;
1984773bda96SJonathan Lemon 
1985773bda96SJonathan Lemon 	table = (struct ocp_resource *)driver_data;
1986773bda96SJonathan Lemon 	for (r = table; r->setup; r++) {
198756ec4403SJonathan Lemon 		if (!ptp_ocp_allow_irq(bp, r))
198856ec4403SJonathan Lemon 			continue;
1989773bda96SJonathan Lemon 		err = r->setup(bp, r);
1990bceff290SJonathan Lemon 		if (err) {
1991bceff290SJonathan Lemon 			dev_err(&bp->pdev->dev,
1992bceff290SJonathan Lemon 				"Could not register %s: err %d\n",
1993bceff290SJonathan Lemon 				r->name, err);
1994773bda96SJonathan Lemon 			break;
1995773bda96SJonathan Lemon 		}
1996bceff290SJonathan Lemon 	}
1997773bda96SJonathan Lemon 	return err;
1998773bda96SJonathan Lemon }
1999773bda96SJonathan Lemon 
20006baf2925SJonathan Lemon static void
20016baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
20026baf2925SJonathan Lemon {
20036baf2925SJonathan Lemon 	u32 ctrl;
20046baf2925SJonathan Lemon 	bool on;
20056baf2925SJonathan Lemon 
20066baf2925SJonathan Lemon 	ctrl = ioread32(reg);
20076baf2925SJonathan Lemon 	on = ctrl & bit;
20086baf2925SJonathan Lemon 	if (on ^ enable) {
20096baf2925SJonathan Lemon 		ctrl &= ~bit;
20106baf2925SJonathan Lemon 		ctrl |= enable ? bit : 0;
20116baf2925SJonathan Lemon 		iowrite32(ctrl, reg);
20126baf2925SJonathan Lemon 	}
20136baf2925SJonathan Lemon }
20146baf2925SJonathan Lemon 
20156baf2925SJonathan Lemon static void
20166baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
20176baf2925SJonathan Lemon {
20186baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
20196baf2925SJonathan Lemon 				   IRIG_M_CTRL_ENABLE, enable);
20206baf2925SJonathan Lemon }
20216baf2925SJonathan Lemon 
20226baf2925SJonathan Lemon static void
20236baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
20246baf2925SJonathan Lemon {
20256baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
20266baf2925SJonathan Lemon 				   IRIG_S_CTRL_ENABLE, enable);
20276baf2925SJonathan Lemon }
20286baf2925SJonathan Lemon 
20296baf2925SJonathan Lemon static void
20306baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
20316baf2925SJonathan Lemon {
20326baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
20336baf2925SJonathan Lemon 				   DCF_M_CTRL_ENABLE, enable);
20346baf2925SJonathan Lemon }
20356baf2925SJonathan Lemon 
20366baf2925SJonathan Lemon static void
20376baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
20386baf2925SJonathan Lemon {
20396baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
20406baf2925SJonathan Lemon 				   DCF_S_CTRL_ENABLE, enable);
20416baf2925SJonathan Lemon }
20426baf2925SJonathan Lemon 
20436baf2925SJonathan Lemon static void
20446baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
20456baf2925SJonathan Lemon {
20466baf2925SJonathan Lemon 	ptp_ocp_irig_out(bp, val & 0x00100010);
20476baf2925SJonathan Lemon 	ptp_ocp_dcf_out(bp, val & 0x00200020);
20486baf2925SJonathan Lemon }
20496baf2925SJonathan Lemon 
20506baf2925SJonathan Lemon static void
20516baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
20526baf2925SJonathan Lemon {
20536baf2925SJonathan Lemon 	ptp_ocp_irig_in(bp, val & 0x00100010);
20546baf2925SJonathan Lemon 	ptp_ocp_dcf_in(bp, val & 0x00200020);
20556baf2925SJonathan Lemon }
20566baf2925SJonathan Lemon 
2057e1daf0ecSJonathan Lemon /*
2058e1daf0ecSJonathan Lemon  * ANT0 == gps	(in)
2059e1daf0ecSJonathan Lemon  * ANT1 == sma1 (in)
2060e1daf0ecSJonathan Lemon  * ANT2 == sma2 (in)
2061e1daf0ecSJonathan Lemon  * ANT3 == sma3 (out)
2062e1daf0ecSJonathan Lemon  * ANT4 == sma4 (out)
2063e1daf0ecSJonathan Lemon  */
2064e1daf0ecSJonathan Lemon 
2065e1daf0ecSJonathan Lemon static ssize_t
2066b2c4f0acSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int def_val)
2067e1daf0ecSJonathan Lemon {
2068e1daf0ecSJonathan Lemon 	const char *name;
2069e1daf0ecSJonathan Lemon 	ssize_t count;
2070e1daf0ecSJonathan Lemon 
2071e1daf0ecSJonathan Lemon 	count = sysfs_emit(buf, "OUT: ");
2072e1daf0ecSJonathan Lemon 	name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val);
2073e1daf0ecSJonathan Lemon 	if (!name)
2074b2c4f0acSJonathan Lemon 		name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, def_val);
2075e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "%s\n", name);
2076e1daf0ecSJonathan Lemon 	return count;
2077e1daf0ecSJonathan Lemon }
2078e1daf0ecSJonathan Lemon 
2079e1daf0ecSJonathan Lemon static ssize_t
2080b2c4f0acSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, int def_val)
2081e1daf0ecSJonathan Lemon {
2082e1daf0ecSJonathan Lemon 	const char *name;
2083e1daf0ecSJonathan Lemon 	ssize_t count;
2084e1daf0ecSJonathan Lemon 	int i;
2085e1daf0ecSJonathan Lemon 
2086e1daf0ecSJonathan Lemon 	count = sysfs_emit(buf, "IN: ");
2087e1daf0ecSJonathan Lemon 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) {
2088e1daf0ecSJonathan Lemon 		if (val & ptp_ocp_sma_in[i].value) {
2089e1daf0ecSJonathan Lemon 			name = ptp_ocp_sma_in[i].name;
2090e1daf0ecSJonathan Lemon 			count += sysfs_emit_at(buf, count, "%s ", name);
2091e1daf0ecSJonathan Lemon 		}
2092e1daf0ecSJonathan Lemon 	}
2093b2c4f0acSJonathan Lemon 	if (!val && def_val >= 0) {
2094b2c4f0acSJonathan Lemon 		name = ptp_ocp_select_name_from_val(ptp_ocp_sma_in, def_val);
2095b2c4f0acSJonathan Lemon 		count += sysfs_emit_at(buf, count, "%s ", name);
2096b2c4f0acSJonathan Lemon 	}
2097e1daf0ecSJonathan Lemon 	if (count)
2098e1daf0ecSJonathan Lemon 		count--;
2099e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "\n");
2100e1daf0ecSJonathan Lemon 	return count;
2101e1daf0ecSJonathan Lemon }
2102e1daf0ecSJonathan Lemon 
2103e1daf0ecSJonathan Lemon static int
2104e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode)
2105e1daf0ecSJonathan Lemon {
2106e1daf0ecSJonathan Lemon 	struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out };
2107e1daf0ecSJonathan Lemon 	int idx, count, dir;
2108e1daf0ecSJonathan Lemon 	char **argv;
2109e1daf0ecSJonathan Lemon 	int ret;
2110e1daf0ecSJonathan Lemon 
2111e1daf0ecSJonathan Lemon 	argv = argv_split(GFP_KERNEL, buf, &count);
2112e1daf0ecSJonathan Lemon 	if (!argv)
2113e1daf0ecSJonathan Lemon 		return -ENOMEM;
2114e1daf0ecSJonathan Lemon 
2115e1daf0ecSJonathan Lemon 	ret = -EINVAL;
2116e1daf0ecSJonathan Lemon 	if (!count)
2117e1daf0ecSJonathan Lemon 		goto out;
2118e1daf0ecSJonathan Lemon 
2119e1daf0ecSJonathan Lemon 	idx = 0;
2120e1daf0ecSJonathan Lemon 	dir = *mode == SMA_MODE_IN ? 0 : 1;
2121a509a7c6SJonathan Lemon 	if (!strcasecmp("IN:", argv[0])) {
2122e1daf0ecSJonathan Lemon 		dir = 0;
2123e1daf0ecSJonathan Lemon 		idx++;
2124e1daf0ecSJonathan Lemon 	}
2125e1daf0ecSJonathan Lemon 	if (!strcasecmp("OUT:", argv[0])) {
2126e1daf0ecSJonathan Lemon 		dir = 1;
2127e1daf0ecSJonathan Lemon 		idx++;
2128e1daf0ecSJonathan Lemon 	}
2129e1daf0ecSJonathan Lemon 	*mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
2130e1daf0ecSJonathan Lemon 
2131e1daf0ecSJonathan Lemon 	ret = 0;
2132e1daf0ecSJonathan Lemon 	for (; idx < count; idx++)
2133e1daf0ecSJonathan Lemon 		ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
2134e1daf0ecSJonathan Lemon 	if (ret < 0)
2135e1daf0ecSJonathan Lemon 		ret = -EINVAL;
2136e1daf0ecSJonathan Lemon 
2137e1daf0ecSJonathan Lemon out:
2138e1daf0ecSJonathan Lemon 	argv_free(argv);
2139e1daf0ecSJonathan Lemon 	return ret;
2140e1daf0ecSJonathan Lemon }
2141e1daf0ecSJonathan Lemon 
2142a509a7c6SJonathan Lemon static u32
2143a509a7c6SJonathan Lemon ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr, enum ptp_ocp_sma_mode mode)
2144e1daf0ecSJonathan Lemon {
2145a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2146a509a7c6SJonathan Lemon 	u32 shift;
2147a509a7c6SJonathan Lemon 
2148a509a7c6SJonathan Lemon 	if (bp->sma[sma_nr - 1].fixed_fcn)
2149a509a7c6SJonathan Lemon 		return (sma_nr - 1) & 1;
2150a509a7c6SJonathan Lemon 
2151a509a7c6SJonathan Lemon 	if (mode == SMA_MODE_IN)
2152a509a7c6SJonathan Lemon 		gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2153a509a7c6SJonathan Lemon 	else
2154a509a7c6SJonathan Lemon 		gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2155a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2156a509a7c6SJonathan Lemon 
2157a509a7c6SJonathan Lemon 	return (ioread32(gpio) >> shift) & 0xffff;
2158a509a7c6SJonathan Lemon }
2159a509a7c6SJonathan Lemon 
2160a509a7c6SJonathan Lemon static ssize_t
2161a509a7c6SJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
2162b2c4f0acSJonathan Lemon 		 int default_in_val, int default_out_val)
2163a509a7c6SJonathan Lemon {
2164a509a7c6SJonathan Lemon 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2165a509a7c6SJonathan Lemon 	u32 val;
2166a509a7c6SJonathan Lemon 
2167a509a7c6SJonathan Lemon 	val = ptp_ocp_sma_get(bp, sma_nr, sma->mode) & SMA_SELECT_MASK;
2168e1daf0ecSJonathan Lemon 
2169b2c4f0acSJonathan Lemon 	if (sma->mode == SMA_MODE_IN) {
2170b2c4f0acSJonathan Lemon 		if (sma->disabled)
2171b2c4f0acSJonathan Lemon 			val = SMA_DISABLE;
2172b2c4f0acSJonathan Lemon 		return ptp_ocp_show_inputs(val, buf, default_in_val);
2173b2c4f0acSJonathan Lemon 	}
2174e1daf0ecSJonathan Lemon 
2175b2c4f0acSJonathan Lemon 	return ptp_ocp_show_output(val, buf, default_out_val);
2176e1daf0ecSJonathan Lemon }
2177e1daf0ecSJonathan Lemon 
2178e1daf0ecSJonathan Lemon static ssize_t
2179e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
2180e1daf0ecSJonathan Lemon {
2181e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2182e1daf0ecSJonathan Lemon 
2183a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
2184e1daf0ecSJonathan Lemon }
2185e1daf0ecSJonathan Lemon 
2186e1daf0ecSJonathan Lemon static ssize_t
2187e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
2188e1daf0ecSJonathan Lemon {
2189e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2190e1daf0ecSJonathan Lemon 
2191a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
2192e1daf0ecSJonathan Lemon }
2193e1daf0ecSJonathan Lemon 
2194e1daf0ecSJonathan Lemon static ssize_t
2195e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
2196e1daf0ecSJonathan Lemon {
2197e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2198e1daf0ecSJonathan Lemon 
2199a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
2200e1daf0ecSJonathan Lemon }
2201e1daf0ecSJonathan Lemon 
2202e1daf0ecSJonathan Lemon static ssize_t
2203e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
2204e1daf0ecSJonathan Lemon {
2205e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2206e1daf0ecSJonathan Lemon 
2207a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
2208e1daf0ecSJonathan Lemon }
2209e1daf0ecSJonathan Lemon 
2210e1daf0ecSJonathan Lemon static void
2211a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2212e1daf0ecSJonathan Lemon {
2213a509a7c6SJonathan Lemon 	u32 reg, mask, shift;
2214e1daf0ecSJonathan Lemon 	unsigned long flags;
2215a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2216a509a7c6SJonathan Lemon 
2217a509a7c6SJonathan Lemon 	gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2218a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2219e1daf0ecSJonathan Lemon 
2220e1daf0ecSJonathan Lemon 	mask = 0xffff << (16 - shift);
2221e1daf0ecSJonathan Lemon 
2222e1daf0ecSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2223e1daf0ecSJonathan Lemon 
2224a509a7c6SJonathan Lemon 	reg = ioread32(gpio);
2225a509a7c6SJonathan Lemon 	reg = (reg & mask) | (val << shift);
22266baf2925SJonathan Lemon 
2227a509a7c6SJonathan Lemon 	__handle_signal_outputs(bp, reg);
22286baf2925SJonathan Lemon 
2229a509a7c6SJonathan Lemon 	iowrite32(reg, gpio);
2230e1daf0ecSJonathan Lemon 
2231e1daf0ecSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2232e1daf0ecSJonathan Lemon }
2233e1daf0ecSJonathan Lemon 
2234e1daf0ecSJonathan Lemon static void
2235a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2236e1daf0ecSJonathan Lemon {
2237a509a7c6SJonathan Lemon 	u32 reg, mask, shift;
2238e1daf0ecSJonathan Lemon 	unsigned long flags;
2239a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2240a509a7c6SJonathan Lemon 
2241a509a7c6SJonathan Lemon 	gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2242a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2243e1daf0ecSJonathan Lemon 
2244e1daf0ecSJonathan Lemon 	mask = 0xffff << (16 - shift);
2245e1daf0ecSJonathan Lemon 
2246e1daf0ecSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2247e1daf0ecSJonathan Lemon 
2248a509a7c6SJonathan Lemon 	reg = ioread32(gpio);
2249a509a7c6SJonathan Lemon 	reg = (reg & mask) | (val << shift);
22506baf2925SJonathan Lemon 
2251a509a7c6SJonathan Lemon 	__handle_signal_inputs(bp, reg);
22526baf2925SJonathan Lemon 
2253a509a7c6SJonathan Lemon 	iowrite32(reg, gpio);
2254e1daf0ecSJonathan Lemon 
2255e1daf0ecSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2256e1daf0ecSJonathan Lemon }
2257e1daf0ecSJonathan Lemon 
2258a509a7c6SJonathan Lemon static int
2259a509a7c6SJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
2260e1daf0ecSJonathan Lemon {
2261a509a7c6SJonathan Lemon 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2262e1daf0ecSJonathan Lemon 	enum ptp_ocp_sma_mode mode;
2263e1daf0ecSJonathan Lemon 	int val;
2264e1daf0ecSJonathan Lemon 
2265e1daf0ecSJonathan Lemon 	mode = sma->mode;
2266e1daf0ecSJonathan Lemon 	val = sma_parse_inputs(buf, &mode);
2267e1daf0ecSJonathan Lemon 	if (val < 0)
2268e1daf0ecSJonathan Lemon 		return val;
2269e1daf0ecSJonathan Lemon 
2270b2c4f0acSJonathan Lemon 	if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
2271e1daf0ecSJonathan Lemon 		return -EOPNOTSUPP;
2272e1daf0ecSJonathan Lemon 
2273a509a7c6SJonathan Lemon 	if (sma->fixed_fcn) {
2274a509a7c6SJonathan Lemon 		if (val != ((sma_nr - 1) & 1))
2275e1daf0ecSJonathan Lemon 			return -EOPNOTSUPP;
2276a509a7c6SJonathan Lemon 		return 0;
2277e1daf0ecSJonathan Lemon 	}
2278e1daf0ecSJonathan Lemon 
2279b2c4f0acSJonathan Lemon 	sma->disabled = !!(val & SMA_DISABLE);
2280b2c4f0acSJonathan Lemon 
2281a509a7c6SJonathan Lemon 	if (mode != sma->mode) {
2282a509a7c6SJonathan Lemon 		if (mode == SMA_MODE_IN)
2283a509a7c6SJonathan Lemon 			ptp_ocp_sma_store_output(bp, sma_nr, 0);
2284e1daf0ecSJonathan Lemon 		else
2285a509a7c6SJonathan Lemon 			ptp_ocp_sma_store_inputs(bp, sma_nr, 0);
2286a509a7c6SJonathan Lemon 		sma->mode = mode;
2287a509a7c6SJonathan Lemon 	}
2288a509a7c6SJonathan Lemon 
2289a509a7c6SJonathan Lemon 	if (!sma->fixed_dir)
2290a509a7c6SJonathan Lemon 		val |= SMA_ENABLE;		/* add enable bit */
2291a509a7c6SJonathan Lemon 
2292b2c4f0acSJonathan Lemon 	if (sma->disabled)
2293b2c4f0acSJonathan Lemon 		val = 0;
2294b2c4f0acSJonathan Lemon 
2295a509a7c6SJonathan Lemon 	if (mode == SMA_MODE_IN)
2296a509a7c6SJonathan Lemon 		ptp_ocp_sma_store_inputs(bp, sma_nr, val);
2297a509a7c6SJonathan Lemon 	else
2298a509a7c6SJonathan Lemon 		ptp_ocp_sma_store_output(bp, sma_nr, val);
2299e1daf0ecSJonathan Lemon 
2300e1daf0ecSJonathan Lemon 	return 0;
2301e1daf0ecSJonathan Lemon }
2302e1daf0ecSJonathan Lemon 
2303e1daf0ecSJonathan Lemon static ssize_t
2304e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr,
2305e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2306e1daf0ecSJonathan Lemon {
2307e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2308e1daf0ecSJonathan Lemon 	int err;
2309e1daf0ecSJonathan Lemon 
2310a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 1);
2311e1daf0ecSJonathan Lemon 	return err ? err : count;
2312e1daf0ecSJonathan Lemon }
2313e1daf0ecSJonathan Lemon 
2314e1daf0ecSJonathan Lemon static ssize_t
2315e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr,
2316e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2317e1daf0ecSJonathan Lemon {
2318e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2319e1daf0ecSJonathan Lemon 	int err;
2320e1daf0ecSJonathan Lemon 
2321a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 2);
2322e1daf0ecSJonathan Lemon 	return err ? err : count;
2323e1daf0ecSJonathan Lemon }
2324e1daf0ecSJonathan Lemon 
2325e1daf0ecSJonathan Lemon static ssize_t
2326e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr,
2327e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2328e1daf0ecSJonathan Lemon {
2329e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2330e1daf0ecSJonathan Lemon 	int err;
2331e1daf0ecSJonathan Lemon 
2332a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 3);
2333e1daf0ecSJonathan Lemon 	return err ? err : count;
2334e1daf0ecSJonathan Lemon }
2335e1daf0ecSJonathan Lemon 
2336e1daf0ecSJonathan Lemon static ssize_t
2337e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr,
2338e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2339e1daf0ecSJonathan Lemon {
2340e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2341e1daf0ecSJonathan Lemon 	int err;
2342e1daf0ecSJonathan Lemon 
2343a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 4);
2344e1daf0ecSJonathan Lemon 	return err ? err : count;
2345e1daf0ecSJonathan Lemon }
2346e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1);
2347e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2);
2348e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3);
2349e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4);
2350e1daf0ecSJonathan Lemon 
2351e1daf0ecSJonathan Lemon static ssize_t
2352e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev,
2353e1daf0ecSJonathan Lemon 			  struct device_attribute *attr, char *buf)
2354e1daf0ecSJonathan Lemon {
2355e1daf0ecSJonathan Lemon 	return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf);
2356e1daf0ecSJonathan Lemon }
2357e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs);
2358e1daf0ecSJonathan Lemon 
2359e1daf0ecSJonathan Lemon static ssize_t
2360e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev,
2361e1daf0ecSJonathan Lemon 			   struct device_attribute *attr, char *buf)
2362e1daf0ecSJonathan Lemon {
2363e1daf0ecSJonathan Lemon 	return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf);
2364e1daf0ecSJonathan Lemon }
2365e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs);
2366e1daf0ecSJonathan Lemon 
2367b325af3cSJonathan Lemon #define EXT_ATTR_RO(_group, _name, _val)				\
2368b325af3cSJonathan Lemon 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
2369b325af3cSJonathan Lemon 		{ __ATTR_RO(_name), (void *)_val }
2370b325af3cSJonathan Lemon #define EXT_ATTR_RW(_group, _name, _val)				\
2371b325af3cSJonathan Lemon 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
2372b325af3cSJonathan Lemon 		{ __ATTR_RW(_name), (void *)_val }
2373b325af3cSJonathan Lemon #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2374b325af3cSJonathan Lemon 
2375b325af3cSJonathan Lemon /* period [duty [phase [polarity]]] */
2376b325af3cSJonathan Lemon static ssize_t
2377b325af3cSJonathan Lemon signal_store(struct device *dev, struct device_attribute *attr,
2378b325af3cSJonathan Lemon 	     const char *buf, size_t count)
2379b325af3cSJonathan Lemon {
2380b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2381b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2382b325af3cSJonathan Lemon 	struct ptp_ocp_signal s = { };
2383b325af3cSJonathan Lemon 	int gen = (uintptr_t)ea->var;
2384b325af3cSJonathan Lemon 	int argc, err;
2385b325af3cSJonathan Lemon 	char **argv;
2386b325af3cSJonathan Lemon 
2387b325af3cSJonathan Lemon 	argv = argv_split(GFP_KERNEL, buf, &argc);
2388b325af3cSJonathan Lemon 	if (!argv)
2389b325af3cSJonathan Lemon 		return -ENOMEM;
2390b325af3cSJonathan Lemon 
2391b325af3cSJonathan Lemon 	err = -EINVAL;
2392b325af3cSJonathan Lemon 	s.duty = bp->signal[gen].duty;
2393b325af3cSJonathan Lemon 	s.phase = bp->signal[gen].phase;
2394b325af3cSJonathan Lemon 	s.period = bp->signal[gen].period;
2395b325af3cSJonathan Lemon 	s.polarity = bp->signal[gen].polarity;
2396b325af3cSJonathan Lemon 
2397b325af3cSJonathan Lemon 	switch (argc) {
2398b325af3cSJonathan Lemon 	case 4:
2399b325af3cSJonathan Lemon 		argc--;
2400b325af3cSJonathan Lemon 		err = kstrtobool(argv[argc], &s.polarity);
2401b325af3cSJonathan Lemon 		if (err)
2402b325af3cSJonathan Lemon 			goto out;
2403b325af3cSJonathan Lemon 		fallthrough;
2404b325af3cSJonathan Lemon 	case 3:
2405b325af3cSJonathan Lemon 		argc--;
2406b325af3cSJonathan Lemon 		err = kstrtou64(argv[argc], 0, &s.phase);
2407b325af3cSJonathan Lemon 		if (err)
2408b325af3cSJonathan Lemon 			goto out;
2409b325af3cSJonathan Lemon 		fallthrough;
2410b325af3cSJonathan Lemon 	case 2:
2411b325af3cSJonathan Lemon 		argc--;
2412b325af3cSJonathan Lemon 		err = kstrtoint(argv[argc], 0, &s.duty);
2413b325af3cSJonathan Lemon 		if (err)
2414b325af3cSJonathan Lemon 			goto out;
2415b325af3cSJonathan Lemon 		fallthrough;
2416b325af3cSJonathan Lemon 	case 1:
2417b325af3cSJonathan Lemon 		argc--;
2418b325af3cSJonathan Lemon 		err = kstrtou64(argv[argc], 0, &s.period);
2419b325af3cSJonathan Lemon 		if (err)
2420b325af3cSJonathan Lemon 			goto out;
2421b325af3cSJonathan Lemon 		break;
2422b325af3cSJonathan Lemon 	default:
2423b325af3cSJonathan Lemon 		goto out;
2424b325af3cSJonathan Lemon 	}
2425b325af3cSJonathan Lemon 
2426b325af3cSJonathan Lemon 	err = ptp_ocp_signal_set(bp, gen, &s);
2427b325af3cSJonathan Lemon 	if (err)
2428b325af3cSJonathan Lemon 		goto out;
2429b325af3cSJonathan Lemon 
2430b325af3cSJonathan Lemon 	err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
2431b325af3cSJonathan Lemon 
2432b325af3cSJonathan Lemon out:
2433b325af3cSJonathan Lemon 	argv_free(argv);
2434b325af3cSJonathan Lemon 	return err ? err : count;
2435b325af3cSJonathan Lemon }
2436b325af3cSJonathan Lemon 
2437b325af3cSJonathan Lemon static ssize_t
2438b325af3cSJonathan Lemon signal_show(struct device *dev, struct device_attribute *attr, char *buf)
2439b325af3cSJonathan Lemon {
2440b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2441b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2442b325af3cSJonathan Lemon 	struct ptp_ocp_signal *signal;
2443b325af3cSJonathan Lemon 	struct timespec64 ts;
2444b325af3cSJonathan Lemon 	ssize_t count;
2445b325af3cSJonathan Lemon 	int i;
2446b325af3cSJonathan Lemon 
2447b325af3cSJonathan Lemon 	i = (uintptr_t)ea->var;
2448b325af3cSJonathan Lemon 	signal = &bp->signal[i];
2449b325af3cSJonathan Lemon 
2450b325af3cSJonathan Lemon 	count = sysfs_emit(buf, "%llu %d %llu %d", signal->period,
2451b325af3cSJonathan Lemon 			   signal->duty, signal->phase, signal->polarity);
2452b325af3cSJonathan Lemon 
2453b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(signal->start);
2454b325af3cSJonathan Lemon 	count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts);
2455b325af3cSJonathan Lemon 
2456b325af3cSJonathan Lemon 	return count;
2457b325af3cSJonathan Lemon }
2458b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 0);
2459b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 1);
2460b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 2);
2461b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 3);
2462b325af3cSJonathan Lemon 
2463b325af3cSJonathan Lemon static ssize_t
2464b325af3cSJonathan Lemon duty_show(struct device *dev, struct device_attribute *attr, char *buf)
2465b325af3cSJonathan Lemon {
2466b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2467b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2468b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2469b325af3cSJonathan Lemon 
2470b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
2471b325af3cSJonathan Lemon }
2472b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 0);
2473b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 1);
2474b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 2);
2475b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 3);
2476b325af3cSJonathan Lemon 
2477b325af3cSJonathan Lemon static ssize_t
2478b325af3cSJonathan Lemon period_show(struct device *dev, struct device_attribute *attr, char *buf)
2479b325af3cSJonathan Lemon {
2480b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2481b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2482b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2483b325af3cSJonathan Lemon 
2484b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
2485b325af3cSJonathan Lemon }
2486b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 0);
2487b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 1);
2488b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 2);
2489b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 3);
2490b325af3cSJonathan Lemon 
2491b325af3cSJonathan Lemon static ssize_t
2492b325af3cSJonathan Lemon phase_show(struct device *dev, struct device_attribute *attr, char *buf)
2493b325af3cSJonathan Lemon {
2494b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2495b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2496b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2497b325af3cSJonathan Lemon 
2498b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
2499b325af3cSJonathan Lemon }
2500b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 0);
2501b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 1);
2502b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 2);
2503b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 3);
2504b325af3cSJonathan Lemon 
2505b325af3cSJonathan Lemon static ssize_t
2506b325af3cSJonathan Lemon polarity_show(struct device *dev, struct device_attribute *attr,
2507b325af3cSJonathan Lemon 	      char *buf)
2508b325af3cSJonathan Lemon {
2509b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2510b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2511b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2512b325af3cSJonathan Lemon 
2513b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
2514b325af3cSJonathan Lemon }
2515b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 0);
2516b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 1);
2517b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 2);
2518b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 3);
2519b325af3cSJonathan Lemon 
2520b325af3cSJonathan Lemon static ssize_t
2521b325af3cSJonathan Lemon running_show(struct device *dev, struct device_attribute *attr, char *buf)
2522b325af3cSJonathan Lemon {
2523b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2524b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2525b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2526b325af3cSJonathan Lemon 
2527b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].running);
2528b325af3cSJonathan Lemon }
2529b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 0);
2530b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 1);
2531b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 2);
2532b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 3);
2533b325af3cSJonathan Lemon 
2534b325af3cSJonathan Lemon static ssize_t
2535b325af3cSJonathan Lemon start_show(struct device *dev, struct device_attribute *attr, char *buf)
2536b325af3cSJonathan Lemon {
2537b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2538b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2539b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2540b325af3cSJonathan Lemon 	struct timespec64 ts;
2541b325af3cSJonathan Lemon 
2542b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[i].start);
2543b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
2544b325af3cSJonathan Lemon }
2545b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 0);
2546b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 1);
2547b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 2);
2548b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 3);
2549b325af3cSJonathan Lemon 
2550773bda96SJonathan Lemon static ssize_t
25512407f5d6SJonathan Lemon seconds_store(struct device *dev, struct device_attribute *attr,
25522407f5d6SJonathan Lemon 	      const char *buf, size_t count)
25532407f5d6SJonathan Lemon {
25542407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
25552407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
25562407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
25572407f5d6SJonathan Lemon 	u32 val;
25582407f5d6SJonathan Lemon 	int err;
25592407f5d6SJonathan Lemon 
25602407f5d6SJonathan Lemon 	err = kstrtou32(buf, 0, &val);
25612407f5d6SJonathan Lemon 	if (err)
25622407f5d6SJonathan Lemon 		return err;
25632407f5d6SJonathan Lemon 	if (val > 0xff)
25642407f5d6SJonathan Lemon 		return -EINVAL;
25652407f5d6SJonathan Lemon 
25662407f5d6SJonathan Lemon 	if (val)
25672407f5d6SJonathan Lemon 		val = (val << 8) | 0x1;
25682407f5d6SJonathan Lemon 
25692407f5d6SJonathan Lemon 	iowrite32(val, &bp->freq_in[idx]->ctrl);
25702407f5d6SJonathan Lemon 
25712407f5d6SJonathan Lemon 	return count;
25722407f5d6SJonathan Lemon }
25732407f5d6SJonathan Lemon 
25742407f5d6SJonathan Lemon static ssize_t
25752407f5d6SJonathan Lemon seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
25762407f5d6SJonathan Lemon {
25772407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
25782407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
25792407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
25802407f5d6SJonathan Lemon 	u32 val;
25812407f5d6SJonathan Lemon 
25822407f5d6SJonathan Lemon 	val = ioread32(&bp->freq_in[idx]->ctrl);
25832407f5d6SJonathan Lemon 	if (val & 1)
25842407f5d6SJonathan Lemon 		val = (val >> 8) & 0xff;
25852407f5d6SJonathan Lemon 	else
25862407f5d6SJonathan Lemon 		val = 0;
25872407f5d6SJonathan Lemon 
25882407f5d6SJonathan Lemon 	return sysfs_emit(buf, "%u\n", val);
25892407f5d6SJonathan Lemon }
25902407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 0);
25912407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 1);
25922407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 2);
25932407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 3);
25942407f5d6SJonathan Lemon 
25952407f5d6SJonathan Lemon static ssize_t
25962407f5d6SJonathan Lemon frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
25972407f5d6SJonathan Lemon {
25982407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
25992407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
26002407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
26012407f5d6SJonathan Lemon 	u32 val;
26022407f5d6SJonathan Lemon 
26032407f5d6SJonathan Lemon 	val = ioread32(&bp->freq_in[idx]->status);
26042407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_ERROR)
26052407f5d6SJonathan Lemon 		return sysfs_emit(buf, "error\n");
26062407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_OVERRUN)
26072407f5d6SJonathan Lemon 		return sysfs_emit(buf, "overrun\n");
26082407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_VALID)
26092407f5d6SJonathan Lemon 		return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
26102407f5d6SJonathan Lemon 	return 0;
26112407f5d6SJonathan Lemon }
26122407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 0);
26132407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 1);
26142407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 2);
26152407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 3);
26162407f5d6SJonathan Lemon 
26172407f5d6SJonathan Lemon static ssize_t
2618773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
2619773bda96SJonathan Lemon {
2620773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2621773bda96SJonathan Lemon 
26220cfcdd1eSJonathan Lemon 	if (!bp->has_eeprom_data)
26230cfcdd1eSJonathan Lemon 		ptp_ocp_read_eeprom(bp);
2624773bda96SJonathan Lemon 
2625773bda96SJonathan Lemon 	return sysfs_emit(buf, "%pM\n", bp->serial);
2626773bda96SJonathan Lemon }
2627773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum);
2628773bda96SJonathan Lemon 
2629773bda96SJonathan Lemon static ssize_t
2630ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
2631773bda96SJonathan Lemon {
2632773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2633773bda96SJonathan Lemon 	ssize_t ret;
2634773bda96SJonathan Lemon 
2635ef0cfb34SJonathan Lemon 	if (bp->gnss_lost)
2636ef0cfb34SJonathan Lemon 		ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
2637773bda96SJonathan Lemon 	else
2638773bda96SJonathan Lemon 		ret = sysfs_emit(buf, "SYNC\n");
2639773bda96SJonathan Lemon 
2640773bda96SJonathan Lemon 	return ret;
2641773bda96SJonathan Lemon }
2642ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync);
2643773bda96SJonathan Lemon 
2644773bda96SJonathan Lemon static ssize_t
264589260d87SJonathan Lemon utc_tai_offset_show(struct device *dev,
264689260d87SJonathan Lemon 		    struct device_attribute *attr, char *buf)
264789260d87SJonathan Lemon {
264889260d87SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
264989260d87SJonathan Lemon 
265089260d87SJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
265189260d87SJonathan Lemon }
265289260d87SJonathan Lemon 
265389260d87SJonathan Lemon static ssize_t
265489260d87SJonathan Lemon utc_tai_offset_store(struct device *dev,
265589260d87SJonathan Lemon 		     struct device_attribute *attr,
265689260d87SJonathan Lemon 		     const char *buf, size_t count)
265789260d87SJonathan Lemon {
265889260d87SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
265989260d87SJonathan Lemon 	int err;
266089260d87SJonathan Lemon 	u32 val;
266189260d87SJonathan Lemon 
266289260d87SJonathan Lemon 	err = kstrtou32(buf, 0, &val);
266389260d87SJonathan Lemon 	if (err)
266489260d87SJonathan Lemon 		return err;
266589260d87SJonathan Lemon 
266689260d87SJonathan Lemon 	ptp_ocp_utc_distribute(bp, val);
266789260d87SJonathan Lemon 
266889260d87SJonathan Lemon 	return count;
266989260d87SJonathan Lemon }
267089260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset);
267189260d87SJonathan Lemon 
267289260d87SJonathan Lemon static ssize_t
26731acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev,
26741acffc6eSJonathan Lemon 		      struct device_attribute *attr, char *buf)
26751acffc6eSJonathan Lemon {
26761acffc6eSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
26771acffc6eSJonathan Lemon 
26781acffc6eSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
26791acffc6eSJonathan Lemon }
26801acffc6eSJonathan Lemon 
26811acffc6eSJonathan Lemon static ssize_t
26821acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev,
26831acffc6eSJonathan Lemon 		       struct device_attribute *attr,
26841acffc6eSJonathan Lemon 		       const char *buf, size_t count)
26851acffc6eSJonathan Lemon {
26861acffc6eSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
26871acffc6eSJonathan Lemon 	int err;
26881acffc6eSJonathan Lemon 	u32 val;
26891acffc6eSJonathan Lemon 
26901acffc6eSJonathan Lemon 	err = kstrtou32(buf, 0, &val);
26911acffc6eSJonathan Lemon 	if (err)
26921acffc6eSJonathan Lemon 		return err;
26931acffc6eSJonathan Lemon 
26941acffc6eSJonathan Lemon 	bp->ts_window_adjust = val;
26951acffc6eSJonathan Lemon 
26961acffc6eSJonathan Lemon 	return count;
26971acffc6eSJonathan Lemon }
26981acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust);
26991acffc6eSJonathan Lemon 
27001acffc6eSJonathan Lemon static ssize_t
2701d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
2702d14ee252SJonathan Lemon {
2703d14ee252SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2704d14ee252SJonathan Lemon 	u32 val;
2705d14ee252SJonathan Lemon 
2706d14ee252SJonathan Lemon 	val = ioread32(&bp->irig_out->ctrl);
2707d14ee252SJonathan Lemon 	val = (val >> 16) & 0x07;
2708d14ee252SJonathan Lemon 	return sysfs_emit(buf, "%d\n", val);
2709d14ee252SJonathan Lemon }
2710d14ee252SJonathan Lemon 
2711d14ee252SJonathan Lemon static ssize_t
2712d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev,
2713d14ee252SJonathan Lemon 		  struct device_attribute *attr,
2714d14ee252SJonathan Lemon 		  const char *buf, size_t count)
2715d14ee252SJonathan Lemon {
2716d14ee252SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2717d14ee252SJonathan Lemon 	unsigned long flags;
2718d14ee252SJonathan Lemon 	int err;
2719d14ee252SJonathan Lemon 	u32 reg;
2720d14ee252SJonathan Lemon 	u8 val;
2721d14ee252SJonathan Lemon 
2722d14ee252SJonathan Lemon 	err = kstrtou8(buf, 0, &val);
2723d14ee252SJonathan Lemon 	if (err)
2724d14ee252SJonathan Lemon 		return err;
2725d14ee252SJonathan Lemon 	if (val > 7)
2726d14ee252SJonathan Lemon 		return -EINVAL;
2727d14ee252SJonathan Lemon 
2728d14ee252SJonathan Lemon 	reg = ((val & 0x7) << 16);
2729d14ee252SJonathan Lemon 
2730d14ee252SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2731d14ee252SJonathan Lemon 	iowrite32(0, &bp->irig_out->ctrl);		/* disable */
2732d14ee252SJonathan Lemon 	iowrite32(reg, &bp->irig_out->ctrl);		/* change mode */
2733d14ee252SJonathan Lemon 	iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
2734d14ee252SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2735d14ee252SJonathan Lemon 
2736d14ee252SJonathan Lemon 	return count;
2737d14ee252SJonathan Lemon }
2738d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode);
2739d14ee252SJonathan Lemon 
2740d14ee252SJonathan Lemon static ssize_t
2741773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
2742773bda96SJonathan Lemon {
2743773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2744773bda96SJonathan Lemon 	const char *p;
2745773bda96SJonathan Lemon 	u32 select;
2746773bda96SJonathan Lemon 
2747773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
2748e1daf0ecSJonathan Lemon 	p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
2749773bda96SJonathan Lemon 
2750773bda96SJonathan Lemon 	return sysfs_emit(buf, "%s\n", p);
2751773bda96SJonathan Lemon }
2752773bda96SJonathan Lemon 
2753773bda96SJonathan Lemon static ssize_t
2754773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr,
2755773bda96SJonathan Lemon 		   const char *buf, size_t count)
2756773bda96SJonathan Lemon {
2757773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2758773bda96SJonathan Lemon 	unsigned long flags;
2759773bda96SJonathan Lemon 	int val;
2760773bda96SJonathan Lemon 
2761e1daf0ecSJonathan Lemon 	val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
2762773bda96SJonathan Lemon 	if (val < 0)
2763773bda96SJonathan Lemon 		return val;
2764773bda96SJonathan Lemon 
2765773bda96SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2766773bda96SJonathan Lemon 	iowrite32(val, &bp->reg->select);
2767773bda96SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2768773bda96SJonathan Lemon 
2769773bda96SJonathan Lemon 	return count;
2770773bda96SJonathan Lemon }
2771773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source);
2772773bda96SJonathan Lemon 
2773773bda96SJonathan Lemon static ssize_t
2774773bda96SJonathan Lemon available_clock_sources_show(struct device *dev,
2775773bda96SJonathan Lemon 			     struct device_attribute *attr, char *buf)
2776773bda96SJonathan Lemon {
2777e1daf0ecSJonathan Lemon 	return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
2778773bda96SJonathan Lemon }
2779773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources);
2780773bda96SJonathan Lemon 
27812f23f486SVadim Fedorenko static ssize_t
27822f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev,
27832f23f486SVadim Fedorenko 			struct device_attribute *attr, char *buf)
27842f23f486SVadim Fedorenko {
27852f23f486SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
27862f23f486SVadim Fedorenko 	u32 val;
27872f23f486SVadim Fedorenko 	int res;
27882f23f486SVadim Fedorenko 
27892f23f486SVadim Fedorenko 	val = ioread32(&bp->reg->status_drift);
27902f23f486SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
27912f23f486SVadim Fedorenko 	res *= (val & INT_MAX);
27922f23f486SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
27932f23f486SVadim Fedorenko }
27942f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift);
27952f23f486SVadim Fedorenko 
27962f23f486SVadim Fedorenko static ssize_t
27972f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev,
27982f23f486SVadim Fedorenko 			 struct device_attribute *attr, char *buf)
27992f23f486SVadim Fedorenko {
28002f23f486SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
28012f23f486SVadim Fedorenko 	u32 val;
28022f23f486SVadim Fedorenko 	int res;
28032f23f486SVadim Fedorenko 
28042f23f486SVadim Fedorenko 	val = ioread32(&bp->reg->status_offset);
28052f23f486SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
28062f23f486SVadim Fedorenko 	res *= (val & INT_MAX);
28072f23f486SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
28082f23f486SVadim Fedorenko }
28092f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset);
28102f23f486SVadim Fedorenko 
281144a412d1SVadim Fedorenko static ssize_t
281244a412d1SVadim Fedorenko tod_correction_show(struct device *dev,
281344a412d1SVadim Fedorenko 		    struct device_attribute *attr, char *buf)
281444a412d1SVadim Fedorenko {
281544a412d1SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
281644a412d1SVadim Fedorenko 	u32 val;
281744a412d1SVadim Fedorenko 	int res;
281844a412d1SVadim Fedorenko 
281944a412d1SVadim Fedorenko 	val = ioread32(&bp->tod->adj_sec);
282044a412d1SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
282144a412d1SVadim Fedorenko 	res *= (val & INT_MAX);
282244a412d1SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
282344a412d1SVadim Fedorenko }
282444a412d1SVadim Fedorenko 
282544a412d1SVadim Fedorenko static ssize_t
282644a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr,
282744a412d1SVadim Fedorenko 		     const char *buf, size_t count)
282844a412d1SVadim Fedorenko {
282944a412d1SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
283044a412d1SVadim Fedorenko 	unsigned long flags;
283144a412d1SVadim Fedorenko 	int err, res;
283244a412d1SVadim Fedorenko 	u32 val = 0;
283344a412d1SVadim Fedorenko 
283444a412d1SVadim Fedorenko 	err = kstrtos32(buf, 0, &res);
283544a412d1SVadim Fedorenko 	if (err)
283644a412d1SVadim Fedorenko 		return err;
283744a412d1SVadim Fedorenko 	if (res < 0) {
283844a412d1SVadim Fedorenko 		res *= -1;
283944a412d1SVadim Fedorenko 		val |= BIT(31);
284044a412d1SVadim Fedorenko 	}
284144a412d1SVadim Fedorenko 	val |= res;
284244a412d1SVadim Fedorenko 
284344a412d1SVadim Fedorenko 	spin_lock_irqsave(&bp->lock, flags);
284444a412d1SVadim Fedorenko 	iowrite32(val, &bp->tod->adj_sec);
284544a412d1SVadim Fedorenko 	spin_unlock_irqrestore(&bp->lock, flags);
284644a412d1SVadim Fedorenko 
284744a412d1SVadim Fedorenko 	return count;
284844a412d1SVadim Fedorenko }
284944a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction);
285044a412d1SVadim Fedorenko 
2851b325af3cSJonathan Lemon #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr)					\
2852b325af3cSJonathan Lemon 	static struct attribute *fb_timecard_signal##_nr##_attrs[] = {	\
2853b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_signal.attr.attr,		\
2854b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_duty.attr.attr,			\
2855b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_phase.attr.attr,		\
2856b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_period.attr.attr,		\
2857b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_polarity.attr.attr,		\
2858b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_running.attr.attr,		\
2859b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_start.attr.attr,		\
2860b325af3cSJonathan Lemon 		NULL,							\
2861b325af3cSJonathan Lemon 	}
2862b325af3cSJonathan Lemon 
2863b325af3cSJonathan Lemon #define DEVICE_SIGNAL_GROUP(_name, _nr)					\
2864b325af3cSJonathan Lemon 	_DEVICE_SIGNAL_GROUP_ATTRS(_nr);				\
2865b325af3cSJonathan Lemon 	static const struct attribute_group				\
2866b325af3cSJonathan Lemon 			fb_timecard_signal##_nr##_group = {		\
2867b325af3cSJonathan Lemon 		.name = #_name,						\
2868b325af3cSJonathan Lemon 		.attrs = fb_timecard_signal##_nr##_attrs,		\
2869b325af3cSJonathan Lemon }
2870b325af3cSJonathan Lemon 
2871b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen1, 0);
2872b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen2, 1);
2873b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen3, 2);
2874b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen4, 3);
2875b325af3cSJonathan Lemon 
28762407f5d6SJonathan Lemon #define _DEVICE_FREQ_GROUP_ATTRS(_nr)					\
28772407f5d6SJonathan Lemon 	static struct attribute *fb_timecard_freq##_nr##_attrs[] = {	\
28782407f5d6SJonathan Lemon 		&dev_attr_freq##_nr##_seconds.attr.attr,		\
28792407f5d6SJonathan Lemon 		&dev_attr_freq##_nr##_frequency.attr.attr,		\
28802407f5d6SJonathan Lemon 		NULL,							\
28812407f5d6SJonathan Lemon 	}
28822407f5d6SJonathan Lemon 
28832407f5d6SJonathan Lemon #define DEVICE_FREQ_GROUP(_name, _nr)					\
28842407f5d6SJonathan Lemon 	_DEVICE_FREQ_GROUP_ATTRS(_nr);					\
28852407f5d6SJonathan Lemon 	static const struct attribute_group				\
28862407f5d6SJonathan Lemon 			fb_timecard_freq##_nr##_group = {		\
28872407f5d6SJonathan Lemon 		.name = #_name,						\
28882407f5d6SJonathan Lemon 		.attrs = fb_timecard_freq##_nr##_attrs,			\
28892407f5d6SJonathan Lemon }
28902407f5d6SJonathan Lemon 
28912407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq1, 0);
28922407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq2, 1);
28932407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq3, 2);
28942407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq4, 3);
28952407f5d6SJonathan Lemon 
2896c205d53cSJonathan Lemon static struct attribute *fb_timecard_attrs[] = {
2897773bda96SJonathan Lemon 	&dev_attr_serialnum.attr,
2898ef0cfb34SJonathan Lemon 	&dev_attr_gnss_sync.attr,
2899773bda96SJonathan Lemon 	&dev_attr_clock_source.attr,
2900773bda96SJonathan Lemon 	&dev_attr_available_clock_sources.attr,
2901e1daf0ecSJonathan Lemon 	&dev_attr_sma1.attr,
2902e1daf0ecSJonathan Lemon 	&dev_attr_sma2.attr,
2903e1daf0ecSJonathan Lemon 	&dev_attr_sma3.attr,
2904e1daf0ecSJonathan Lemon 	&dev_attr_sma4.attr,
2905e1daf0ecSJonathan Lemon 	&dev_attr_available_sma_inputs.attr,
2906e1daf0ecSJonathan Lemon 	&dev_attr_available_sma_outputs.attr,
29072f23f486SVadim Fedorenko 	&dev_attr_clock_status_drift.attr,
29082f23f486SVadim Fedorenko 	&dev_attr_clock_status_offset.attr,
2909d14ee252SJonathan Lemon 	&dev_attr_irig_b_mode.attr,
291089260d87SJonathan Lemon 	&dev_attr_utc_tai_offset.attr,
29111acffc6eSJonathan Lemon 	&dev_attr_ts_window_adjust.attr,
291244a412d1SVadim Fedorenko 	&dev_attr_tod_correction.attr,
2913773bda96SJonathan Lemon 	NULL,
2914773bda96SJonathan Lemon };
2915c205d53cSJonathan Lemon static const struct attribute_group fb_timecard_group = {
2916c205d53cSJonathan Lemon 	.attrs = fb_timecard_attrs,
2917c205d53cSJonathan Lemon };
2918c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[] = {
2919c205d53cSJonathan Lemon 	{ .cap = OCP_CAP_BASIC,	    .group = &fb_timecard_group },
2920b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal0_group },
2921b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal1_group },
2922b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal2_group },
2923b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal3_group },
29242407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq0_group },
29252407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq1_group },
29262407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq2_group },
29272407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq3_group },
2928c205d53cSJonathan Lemon 	{ },
2929c205d53cSJonathan Lemon };
2930773bda96SJonathan Lemon 
2931a509a7c6SJonathan Lemon static void
2932a509a7c6SJonathan Lemon gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
2933a509a7c6SJonathan Lemon 	       const char *def)
2934f67bf662SJonathan Lemon {
2935a509a7c6SJonathan Lemon 	int i;
2936f67bf662SJonathan Lemon 
2937a509a7c6SJonathan Lemon 	for (i = 0; i < 4; i++) {
2938a509a7c6SJonathan Lemon 		if (bp->sma[i].mode != SMA_MODE_IN)
2939a509a7c6SJonathan Lemon 			continue;
2940a509a7c6SJonathan Lemon 		if (map[i][0] & (1 << bit)) {
2941a509a7c6SJonathan Lemon 			sprintf(buf, "sma%d", i + 1);
2942a509a7c6SJonathan Lemon 			return;
2943a509a7c6SJonathan Lemon 		}
2944a509a7c6SJonathan Lemon 	}
2945a509a7c6SJonathan Lemon 	if (!def)
2946a509a7c6SJonathan Lemon 		def = "----";
2947a509a7c6SJonathan Lemon 	strcpy(buf, def);
2948f67bf662SJonathan Lemon }
2949f67bf662SJonathan Lemon 
2950f67bf662SJonathan Lemon static void
2951a509a7c6SJonathan Lemon gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
2952f67bf662SJonathan Lemon {
2953f67bf662SJonathan Lemon 	char *ans = buf;
2954a509a7c6SJonathan Lemon 	int i;
2955f67bf662SJonathan Lemon 
2956a509a7c6SJonathan Lemon 	strcpy(ans, "----");
2957a509a7c6SJonathan Lemon 	for (i = 0; i < 4; i++) {
2958a509a7c6SJonathan Lemon 		if (bp->sma[i].mode != SMA_MODE_OUT)
2959a509a7c6SJonathan Lemon 			continue;
2960a509a7c6SJonathan Lemon 		if (map[i][1] & (1 << bit))
2961a509a7c6SJonathan Lemon 			ans += sprintf(ans, "sma%d ", i + 1);
2962a509a7c6SJonathan Lemon 	}
2963f67bf662SJonathan Lemon }
2964f67bf662SJonathan Lemon 
2965b325af3cSJonathan Lemon static void
2966b325af3cSJonathan Lemon _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
2967b325af3cSJonathan Lemon {
2968b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
2969b325af3cSJonathan Lemon 	struct ptp_ocp_signal *signal = &bp->signal[nr];
2970b325af3cSJonathan Lemon 	char label[8];
2971b325af3cSJonathan Lemon 	bool on;
2972b325af3cSJonathan Lemon 	u32 val;
2973b325af3cSJonathan Lemon 
2974b325af3cSJonathan Lemon 	if (!signal)
2975b325af3cSJonathan Lemon 		return;
2976b325af3cSJonathan Lemon 
2977b325af3cSJonathan Lemon 	on = signal->running;
297805fc65f3SJonathan Lemon 	sprintf(label, "GEN%d", nr + 1);
2979b325af3cSJonathan Lemon 	seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
2980b325af3cSJonathan Lemon 		   label, on ? " ON" : "OFF",
2981b325af3cSJonathan Lemon 		   signal->period, signal->duty, signal->phase,
2982b325af3cSJonathan Lemon 		   signal->polarity);
2983b325af3cSJonathan Lemon 
2984b325af3cSJonathan Lemon 	val = ioread32(&reg->enable);
2985b325af3cSJonathan Lemon 	seq_printf(s, " [%x", val);
2986b325af3cSJonathan Lemon 	val = ioread32(&reg->status);
2987b325af3cSJonathan Lemon 	seq_printf(s, " %x]", val);
2988b325af3cSJonathan Lemon 
2989b325af3cSJonathan Lemon 	seq_printf(s, " start:%llu\n", signal->start);
2990b325af3cSJonathan Lemon }
2991b325af3cSJonathan Lemon 
29922407f5d6SJonathan Lemon static void
29932407f5d6SJonathan Lemon _frequency_summary_show(struct seq_file *s, int nr,
29942407f5d6SJonathan Lemon 			struct frequency_reg __iomem *reg)
29952407f5d6SJonathan Lemon {
29962407f5d6SJonathan Lemon 	char label[8];
29972407f5d6SJonathan Lemon 	bool on;
29982407f5d6SJonathan Lemon 	u32 val;
29992407f5d6SJonathan Lemon 
30002407f5d6SJonathan Lemon 	if (!reg)
30012407f5d6SJonathan Lemon 		return;
30022407f5d6SJonathan Lemon 
300305fc65f3SJonathan Lemon 	sprintf(label, "FREQ%d", nr + 1);
30042407f5d6SJonathan Lemon 	val = ioread32(&reg->ctrl);
30052407f5d6SJonathan Lemon 	on = val & 1;
30062407f5d6SJonathan Lemon 	val = (val >> 8) & 0xff;
30072407f5d6SJonathan Lemon 	seq_printf(s, "%7s: %s, sec:%u",
30082407f5d6SJonathan Lemon 		   label,
30092407f5d6SJonathan Lemon 		   on ? " ON" : "OFF",
30102407f5d6SJonathan Lemon 		   val);
30112407f5d6SJonathan Lemon 
30122407f5d6SJonathan Lemon 	val = ioread32(&reg->status);
30132407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_ERROR)
30142407f5d6SJonathan Lemon 		seq_printf(s, ", error");
30152407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_OVERRUN)
30162407f5d6SJonathan Lemon 		seq_printf(s, ", overrun");
30172407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_VALID)
30182407f5d6SJonathan Lemon 		seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
30192407f5d6SJonathan Lemon 	seq_printf(s, "  reg:%x\n", val);
30202407f5d6SJonathan Lemon }
30212407f5d6SJonathan Lemon 
3022f67bf662SJonathan Lemon static int
3023f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data)
3024f67bf662SJonathan Lemon {
3025f67bf662SJonathan Lemon 	struct device *dev = s->private;
3026f67bf662SJonathan Lemon 	struct ptp_system_timestamp sts;
3027f67bf662SJonathan Lemon 	struct ts_reg __iomem *ts_reg;
3028f67bf662SJonathan Lemon 	struct timespec64 ts;
3029f67bf662SJonathan Lemon 	struct ptp_ocp *bp;
30302b341f75SJonathan Lemon 	u16 sma_val[4][2];
3031a509a7c6SJonathan Lemon 	char *src, *buf;
30322b341f75SJonathan Lemon 	u32 ctrl, val;
3033a62a56d0SJonathan Lemon 	bool on, map;
3034b325af3cSJonathan Lemon 	int i;
3035f67bf662SJonathan Lemon 
3036f67bf662SJonathan Lemon 	buf = (char *)__get_free_page(GFP_KERNEL);
3037f67bf662SJonathan Lemon 	if (!buf)
3038f67bf662SJonathan Lemon 		return -ENOMEM;
3039f67bf662SJonathan Lemon 
3040f67bf662SJonathan Lemon 	bp = dev_get_drvdata(dev);
3041f67bf662SJonathan Lemon 
3042f67bf662SJonathan Lemon 	seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
304361fd7ac2SJonathan Lemon 	if (bp->gnss_port != -1)
304461fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port);
304561fd7ac2SJonathan Lemon 	if (bp->gnss2_port != -1)
304661fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port);
304761fd7ac2SJonathan Lemon 	if (bp->mac_port != -1)
304861fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port);
304961fd7ac2SJonathan Lemon 	if (bp->nmea_port != -1)
305061fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port);
3051f67bf662SJonathan Lemon 
3052a509a7c6SJonathan Lemon 	memset(sma_val, 0xff, sizeof(sma_val));
3053a509a7c6SJonathan Lemon 	if (bp->sma_map1) {
3054a509a7c6SJonathan Lemon 		u32 reg;
3055a509a7c6SJonathan Lemon 
3056a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio1);
3057a509a7c6SJonathan Lemon 		sma_val[0][0] = reg & 0xffff;
3058a509a7c6SJonathan Lemon 		sma_val[1][0] = reg >> 16;
3059a509a7c6SJonathan Lemon 
3060a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio2);
3061a509a7c6SJonathan Lemon 		sma_val[2][1] = reg & 0xffff;
3062a509a7c6SJonathan Lemon 		sma_val[3][1] = reg >> 16;
3063a509a7c6SJonathan Lemon 
3064a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map2->gpio1);
3065a509a7c6SJonathan Lemon 		sma_val[2][0] = reg & 0xffff;
3066a509a7c6SJonathan Lemon 		sma_val[3][0] = reg >> 16;
3067a509a7c6SJonathan Lemon 
3068a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map2->gpio2);
3069a509a7c6SJonathan Lemon 		sma_val[0][1] = reg & 0xffff;
3070a509a7c6SJonathan Lemon 		sma_val[1][1] = reg >> 16;
3071a509a7c6SJonathan Lemon 	}
3072a509a7c6SJonathan Lemon 
3073f67bf662SJonathan Lemon 	sma1_show(dev, NULL, buf);
3074a509a7c6SJonathan Lemon 	seq_printf(s, "   sma1: %04x,%04x %s",
3075a509a7c6SJonathan Lemon 		   sma_val[0][0], sma_val[0][1], buf);
3076f67bf662SJonathan Lemon 
3077f67bf662SJonathan Lemon 	sma2_show(dev, NULL, buf);
3078a509a7c6SJonathan Lemon 	seq_printf(s, "   sma2: %04x,%04x %s",
3079a509a7c6SJonathan Lemon 		   sma_val[1][0], sma_val[1][1], buf);
3080f67bf662SJonathan Lemon 
3081f67bf662SJonathan Lemon 	sma3_show(dev, NULL, buf);
3082a509a7c6SJonathan Lemon 	seq_printf(s, "   sma3: %04x,%04x %s",
3083a509a7c6SJonathan Lemon 		   sma_val[2][0], sma_val[2][1], buf);
3084f67bf662SJonathan Lemon 
3085f67bf662SJonathan Lemon 	sma4_show(dev, NULL, buf);
3086a509a7c6SJonathan Lemon 	seq_printf(s, "   sma4: %04x,%04x %s",
3087a509a7c6SJonathan Lemon 		   sma_val[3][0], sma_val[3][1], buf);
3088f67bf662SJonathan Lemon 
3089f67bf662SJonathan Lemon 	if (bp->ts0) {
3090f67bf662SJonathan Lemon 		ts_reg = bp->ts0->mem;
3091f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3092be69087cSJonathan Lemon 		src = "GNSS1";
3093f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS0",
3094f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", src);
3095f67bf662SJonathan Lemon 	}
3096f67bf662SJonathan Lemon 
3097f67bf662SJonathan Lemon 	if (bp->ts1) {
3098f67bf662SJonathan Lemon 		ts_reg = bp->ts1->mem;
3099f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3100a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 2, NULL);
3101f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS1",
3102a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", buf);
3103f67bf662SJonathan Lemon 	}
3104f67bf662SJonathan Lemon 
3105f67bf662SJonathan Lemon 	if (bp->ts2) {
3106f67bf662SJonathan Lemon 		ts_reg = bp->ts2->mem;
3107f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3108a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 3, NULL);
3109f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS2",
3110a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", buf);
3111f67bf662SJonathan Lemon 	}
3112f67bf662SJonathan Lemon 
31130fa3ff7eSJonathan Lemon 	if (bp->ts3) {
31140fa3ff7eSJonathan Lemon 		ts_reg = bp->ts3->mem;
31150fa3ff7eSJonathan Lemon 		on = ioread32(&ts_reg->enable);
31160fa3ff7eSJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 6, NULL);
31170fa3ff7eSJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS3",
31180fa3ff7eSJonathan Lemon 			   on ? " ON" : "OFF", buf);
31190fa3ff7eSJonathan Lemon 	}
31200fa3ff7eSJonathan Lemon 
31210fa3ff7eSJonathan Lemon 	if (bp->ts4) {
31220fa3ff7eSJonathan Lemon 		ts_reg = bp->ts4->mem;
31230fa3ff7eSJonathan Lemon 		on = ioread32(&ts_reg->enable);
31240fa3ff7eSJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 7, NULL);
31250fa3ff7eSJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS4",
31260fa3ff7eSJonathan Lemon 			   on ? " ON" : "OFF", buf);
31270fa3ff7eSJonathan Lemon 	}
31280fa3ff7eSJonathan Lemon 
3129a62a56d0SJonathan Lemon 	if (bp->pps) {
3130a62a56d0SJonathan Lemon 		ts_reg = bp->pps->mem;
3131a62a56d0SJonathan Lemon 		src = "PHC";
3132a62a56d0SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3133a62a56d0SJonathan Lemon 		map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
31340fa3ff7eSJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS5",
31351a575cdeSNathan Chancellor 			   on && map ? " ON" : "OFF", src);
3136a62a56d0SJonathan Lemon 
3137a62a56d0SJonathan Lemon 		map = !!(bp->pps_req_map & OCP_REQ_PPS);
3138a62a56d0SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "PPS",
31391a575cdeSNathan Chancellor 			   on && map ? " ON" : "OFF", src);
3140a62a56d0SJonathan Lemon 	}
3141a62a56d0SJonathan Lemon 
3142b325af3cSJonathan Lemon 	if (bp->fw_cap & OCP_CAP_SIGNAL)
3143b325af3cSJonathan Lemon 		for (i = 0; i < 4; i++)
3144b325af3cSJonathan Lemon 			_signal_summary_show(s, bp, i);
3145b325af3cSJonathan Lemon 
31462407f5d6SJonathan Lemon 	if (bp->fw_cap & OCP_CAP_FREQ)
31472407f5d6SJonathan Lemon 		for (i = 0; i < 4; i++)
31482407f5d6SJonathan Lemon 			_frequency_summary_show(s, i, bp->freq_in[i]);
31492407f5d6SJonathan Lemon 
3150f67bf662SJonathan Lemon 	if (bp->irig_out) {
3151f67bf662SJonathan Lemon 		ctrl = ioread32(&bp->irig_out->ctrl);
3152f67bf662SJonathan Lemon 		on = ctrl & IRIG_M_CTRL_ENABLE;
3153f67bf662SJonathan Lemon 		val = ioread32(&bp->irig_out->status);
3154a509a7c6SJonathan Lemon 		gpio_output_map(buf, bp, sma_val, 4);
3155f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
3156f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", val, (ctrl >> 16), buf);
3157f67bf662SJonathan Lemon 	}
3158f67bf662SJonathan Lemon 
3159f67bf662SJonathan Lemon 	if (bp->irig_in) {
3160f67bf662SJonathan Lemon 		on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
3161f67bf662SJonathan Lemon 		val = ioread32(&bp->irig_in->status);
3162a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 4, NULL);
3163f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
3164a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3165f67bf662SJonathan Lemon 	}
3166f67bf662SJonathan Lemon 
3167f67bf662SJonathan Lemon 	if (bp->dcf_out) {
3168f67bf662SJonathan Lemon 		on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
3169f67bf662SJonathan Lemon 		val = ioread32(&bp->dcf_out->status);
3170a509a7c6SJonathan Lemon 		gpio_output_map(buf, bp, sma_val, 5);
3171f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
3172f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3173f67bf662SJonathan Lemon 	}
3174f67bf662SJonathan Lemon 
3175f67bf662SJonathan Lemon 	if (bp->dcf_in) {
3176f67bf662SJonathan Lemon 		on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
3177f67bf662SJonathan Lemon 		val = ioread32(&bp->dcf_in->status);
3178a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 5, NULL);
3179f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
3180a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3181f67bf662SJonathan Lemon 	}
3182f67bf662SJonathan Lemon 
3183e3516bb4SJonathan Lemon 	if (bp->nmea_out) {
3184e3516bb4SJonathan Lemon 		on = ioread32(&bp->nmea_out->ctrl) & 1;
3185e3516bb4SJonathan Lemon 		val = ioread32(&bp->nmea_out->status);
3186e3516bb4SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
3187e3516bb4SJonathan Lemon 			   on ? " ON" : "OFF", val);
3188e3516bb4SJonathan Lemon 	}
3189e3516bb4SJonathan Lemon 
3190f67bf662SJonathan Lemon 	/* compute src for PPS1, used below. */
3191f67bf662SJonathan Lemon 	if (bp->pps_select) {
3192f67bf662SJonathan Lemon 		val = ioread32(&bp->pps_select->gpio1);
3193a509a7c6SJonathan Lemon 		src = &buf[80];
3194f67bf662SJonathan Lemon 		if (val & 0x01)
3195a509a7c6SJonathan Lemon 			gpio_input_map(src, bp, sma_val, 0, NULL);
3196f67bf662SJonathan Lemon 		else if (val & 0x02)
3197f67bf662SJonathan Lemon 			src = "MAC";
3198f67bf662SJonathan Lemon 		else if (val & 0x04)
3199be69087cSJonathan Lemon 			src = "GNSS1";
3200f67bf662SJonathan Lemon 		else
3201f67bf662SJonathan Lemon 			src = "----";
3202f67bf662SJonathan Lemon 	} else {
3203f67bf662SJonathan Lemon 		src = "?";
3204f67bf662SJonathan Lemon 	}
3205f67bf662SJonathan Lemon 
3206f67bf662SJonathan Lemon 	/* assumes automatic switchover/selection */
3207f67bf662SJonathan Lemon 	val = ioread32(&bp->reg->select);
3208f67bf662SJonathan Lemon 	switch (val >> 16) {
3209f67bf662SJonathan Lemon 	case 0:
3210f67bf662SJonathan Lemon 		sprintf(buf, "----");
3211f67bf662SJonathan Lemon 		break;
3212f67bf662SJonathan Lemon 	case 2:
3213f67bf662SJonathan Lemon 		sprintf(buf, "IRIG");
3214f67bf662SJonathan Lemon 		break;
3215f67bf662SJonathan Lemon 	case 3:
3216f67bf662SJonathan Lemon 		sprintf(buf, "%s via PPS1", src);
3217f67bf662SJonathan Lemon 		break;
3218f67bf662SJonathan Lemon 	case 6:
3219f67bf662SJonathan Lemon 		sprintf(buf, "DCF");
3220f67bf662SJonathan Lemon 		break;
3221f67bf662SJonathan Lemon 	default:
3222f67bf662SJonathan Lemon 		strcpy(buf, "unknown");
3223f67bf662SJonathan Lemon 		break;
3224f67bf662SJonathan Lemon 	}
3225f67bf662SJonathan Lemon 	val = ioread32(&bp->reg->status);
3226f67bf662SJonathan Lemon 	seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
3227f67bf662SJonathan Lemon 		   val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced");
3228f67bf662SJonathan Lemon 
3229f67bf662SJonathan Lemon 	/* reuses PPS1 src from earlier */
3230f67bf662SJonathan Lemon 	seq_printf(s, "MAC PPS1 src: %s\n", src);
3231f67bf662SJonathan Lemon 
3232a509a7c6SJonathan Lemon 	gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
3233a509a7c6SJonathan Lemon 	seq_printf(s, "MAC PPS2 src: %s\n", buf);
3234f67bf662SJonathan Lemon 
3235f67bf662SJonathan Lemon 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
3236f67bf662SJonathan Lemon 		struct timespec64 sys_ts;
3237f67bf662SJonathan Lemon 		s64 pre_ns, post_ns, ns;
3238f67bf662SJonathan Lemon 
3239f67bf662SJonathan Lemon 		pre_ns = timespec64_to_ns(&sts.pre_ts);
3240f67bf662SJonathan Lemon 		post_ns = timespec64_to_ns(&sts.post_ts);
3241f67bf662SJonathan Lemon 		ns = (pre_ns + post_ns) / 2;
3242f67bf662SJonathan Lemon 		ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
3243f67bf662SJonathan Lemon 		sys_ts = ns_to_timespec64(ns);
3244f67bf662SJonathan Lemon 
3245f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC",
3246f67bf662SJonathan Lemon 			   ts.tv_sec, ts.tv_nsec, &ts);
3247f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS",
3248f67bf662SJonathan Lemon 			   sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts,
3249f67bf662SJonathan Lemon 			   bp->utc_tai_offset);
3250f67bf662SJonathan Lemon 		seq_printf(s, "%7s: PHC:SYS offset: %lld  window: %lld\n", "",
3251f67bf662SJonathan Lemon 			   timespec64_to_ns(&ts) - ns,
3252f67bf662SJonathan Lemon 			   post_ns - pre_ns);
3253f67bf662SJonathan Lemon 	}
3254f67bf662SJonathan Lemon 
3255f67bf662SJonathan Lemon 	free_page((unsigned long)buf);
3256f67bf662SJonathan Lemon 	return 0;
3257f67bf662SJonathan Lemon }
3258f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
3259f67bf662SJonathan Lemon 
32609f492c4cSVadim Fedorenko static int
32619f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data)
32629f492c4cSVadim Fedorenko {
32639f492c4cSVadim Fedorenko 	struct device *dev = s->private;
32649f492c4cSVadim Fedorenko 	struct ptp_ocp *bp;
32659f492c4cSVadim Fedorenko 	u32 val;
32669f492c4cSVadim Fedorenko 	int idx;
32679f492c4cSVadim Fedorenko 
32689f492c4cSVadim Fedorenko 	bp = dev_get_drvdata(dev);
32699f492c4cSVadim Fedorenko 
32709f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->ctrl);
32719f492c4cSVadim Fedorenko 	if (!(val & TOD_CTRL_ENABLE)) {
32729f492c4cSVadim Fedorenko 		seq_printf(s, "TOD Slave disabled\n");
32739f492c4cSVadim Fedorenko 		return 0;
32749f492c4cSVadim Fedorenko 	}
32759f492c4cSVadim Fedorenko 	seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
32769f492c4cSVadim Fedorenko 
32779f492c4cSVadim Fedorenko 	idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
32789f492c4cSVadim Fedorenko 	idx += (val >> 16) & 3;
32799f492c4cSVadim Fedorenko 	seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
32809f492c4cSVadim Fedorenko 
32819f492c4cSVadim Fedorenko 	idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
32829f492c4cSVadim Fedorenko 	seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
32839f492c4cSVadim Fedorenko 
32849f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->version);
32859f492c4cSVadim Fedorenko 	seq_printf(s, "TOD Version %d.%d.%d\n",
32869f492c4cSVadim Fedorenko 		val >> 24, (val >> 16) & 0xff, val & 0xffff);
32879f492c4cSVadim Fedorenko 
32889f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->status);
32899f492c4cSVadim Fedorenko 	seq_printf(s, "Status register: 0x%08X\n", val);
32909f492c4cSVadim Fedorenko 
32919f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->adj_sec);
32929f492c4cSVadim Fedorenko 	idx = (val & ~INT_MAX) ? -1 : 1;
32939f492c4cSVadim Fedorenko 	idx *= (val & INT_MAX);
32949f492c4cSVadim Fedorenko 	seq_printf(s, "Correction seconds: %d\n", idx);
32959f492c4cSVadim Fedorenko 
32969f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->utc_status);
32979f492c4cSVadim Fedorenko 	seq_printf(s, "UTC status register: 0x%08X\n", val);
32989f492c4cSVadim Fedorenko 	seq_printf(s, "UTC offset: %d  valid:%d\n",
32999f492c4cSVadim Fedorenko 		val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
33009f492c4cSVadim Fedorenko 	seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
33019f492c4cSVadim Fedorenko 		val & TOD_STATUS_LEAP_VALID ? 1 : 0,
33029f492c4cSVadim Fedorenko 		val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
33039f492c4cSVadim Fedorenko 
33049f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->leap);
33059f492c4cSVadim Fedorenko 	seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
33069f492c4cSVadim Fedorenko 
33079f492c4cSVadim Fedorenko 	return 0;
33089f492c4cSVadim Fedorenko }
33099f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
33109f492c4cSVadim Fedorenko 
3311f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root;
3312f67bf662SJonathan Lemon 
3313f67bf662SJonathan Lemon static void
3314f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
3315f67bf662SJonathan Lemon {
3316f67bf662SJonathan Lemon 	struct dentry *d;
3317f67bf662SJonathan Lemon 
3318f67bf662SJonathan Lemon 	d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
3319f67bf662SJonathan Lemon 	bp->debug_root = d;
3320f67bf662SJonathan Lemon 	debugfs_create_file("summary", 0444, bp->debug_root,
3321f67bf662SJonathan Lemon 			    &bp->dev, &ptp_ocp_summary_fops);
33229f492c4cSVadim Fedorenko 	if (bp->tod)
33239f492c4cSVadim Fedorenko 		debugfs_create_file("tod_status", 0444, bp->debug_root,
33249f492c4cSVadim Fedorenko 				    &bp->dev, &ptp_ocp_tod_status_fops);
3325f67bf662SJonathan Lemon }
3326f67bf662SJonathan Lemon 
3327f67bf662SJonathan Lemon static void
3328f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
3329f67bf662SJonathan Lemon {
3330f67bf662SJonathan Lemon 	debugfs_remove_recursive(bp->debug_root);
3331f67bf662SJonathan Lemon }
3332f67bf662SJonathan Lemon 
3333f67bf662SJonathan Lemon static void
3334f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void)
3335f67bf662SJonathan Lemon {
3336f67bf662SJonathan Lemon 	ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
3337f67bf662SJonathan Lemon }
3338f67bf662SJonathan Lemon 
3339f67bf662SJonathan Lemon static void
3340f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void)
3341f67bf662SJonathan Lemon {
3342f67bf662SJonathan Lemon 	debugfs_remove_recursive(ptp_ocp_debugfs_root);
3343f67bf662SJonathan Lemon }
3344f67bf662SJonathan Lemon 
3345773bda96SJonathan Lemon static void
3346773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev)
3347773bda96SJonathan Lemon {
3348773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3349773bda96SJonathan Lemon 
3350773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
3351773bda96SJonathan Lemon 	idr_remove(&ptp_ocp_idr, bp->id);
3352773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
3353773bda96SJonathan Lemon }
3354773bda96SJonathan Lemon 
3355773bda96SJonathan Lemon static int
3356773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
3357773bda96SJonathan Lemon {
3358773bda96SJonathan Lemon 	int err;
3359773bda96SJonathan Lemon 
3360773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
3361773bda96SJonathan Lemon 	err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
3362773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
3363773bda96SJonathan Lemon 	if (err < 0) {
3364773bda96SJonathan Lemon 		dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
3365773bda96SJonathan Lemon 		return err;
3366773bda96SJonathan Lemon 	}
3367773bda96SJonathan Lemon 	bp->id = err;
3368773bda96SJonathan Lemon 
3369773bda96SJonathan Lemon 	bp->ptp_info = ptp_ocp_clock_info;
3370773bda96SJonathan Lemon 	spin_lock_init(&bp->lock);
3371ef0cfb34SJonathan Lemon 	bp->gnss_port = -1;
337271d7e085SJonathan Lemon 	bp->gnss2_port = -1;
3373773bda96SJonathan Lemon 	bp->mac_port = -1;
3374e3516bb4SJonathan Lemon 	bp->nmea_port = -1;
3375773bda96SJonathan Lemon 	bp->pdev = pdev;
3376773bda96SJonathan Lemon 
3377773bda96SJonathan Lemon 	device_initialize(&bp->dev);
3378773bda96SJonathan Lemon 	dev_set_name(&bp->dev, "ocp%d", bp->id);
3379773bda96SJonathan Lemon 	bp->dev.class = &timecard_class;
3380773bda96SJonathan Lemon 	bp->dev.parent = &pdev->dev;
3381773bda96SJonathan Lemon 	bp->dev.release = ptp_ocp_dev_release;
3382773bda96SJonathan Lemon 	dev_set_drvdata(&bp->dev, bp);
3383773bda96SJonathan Lemon 
3384773bda96SJonathan Lemon 	err = device_add(&bp->dev);
3385773bda96SJonathan Lemon 	if (err) {
3386773bda96SJonathan Lemon 		dev_err(&bp->dev, "device add failed: %d\n", err);
3387773bda96SJonathan Lemon 		goto out;
3388773bda96SJonathan Lemon 	}
3389773bda96SJonathan Lemon 
3390773bda96SJonathan Lemon 	pci_set_drvdata(pdev, bp);
3391773bda96SJonathan Lemon 
3392773bda96SJonathan Lemon 	return 0;
3393773bda96SJonathan Lemon 
3394773bda96SJonathan Lemon out:
3395773bda96SJonathan Lemon 	ptp_ocp_dev_release(&bp->dev);
3396d12f23faSJonathan Lemon 	put_device(&bp->dev);
3397773bda96SJonathan Lemon 	return err;
3398773bda96SJonathan Lemon }
3399773bda96SJonathan Lemon 
3400773bda96SJonathan Lemon static void
3401773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
3402773bda96SJonathan Lemon {
3403773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
3404773bda96SJonathan Lemon 
3405773bda96SJonathan Lemon 	if (sysfs_create_link(&dev->kobj, &child->kobj, link))
3406773bda96SJonathan Lemon 		dev_err(dev, "%s symlink failed\n", link);
3407773bda96SJonathan Lemon }
3408773bda96SJonathan Lemon 
3409773bda96SJonathan Lemon static void
3410773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
3411773bda96SJonathan Lemon {
3412773bda96SJonathan Lemon 	struct device *dev, *child;
3413773bda96SJonathan Lemon 
3414773bda96SJonathan Lemon 	dev = &bp->pdev->dev;
3415773bda96SJonathan Lemon 
3416773bda96SJonathan Lemon 	child = device_find_child_by_name(dev, name);
3417773bda96SJonathan Lemon 	if (!child) {
3418773bda96SJonathan Lemon 		dev_err(dev, "Could not find device %s\n", name);
3419773bda96SJonathan Lemon 		return;
3420773bda96SJonathan Lemon 	}
3421773bda96SJonathan Lemon 
3422773bda96SJonathan Lemon 	ptp_ocp_symlink(bp, child, link);
3423773bda96SJonathan Lemon 	put_device(child);
3424773bda96SJonathan Lemon }
3425773bda96SJonathan Lemon 
3426773bda96SJonathan Lemon static int
3427773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp)
3428773bda96SJonathan Lemon {
3429773bda96SJonathan Lemon 	struct pps_device *pps;
3430773bda96SJonathan Lemon 	char buf[32];
3431773bda96SJonathan Lemon 
3432ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1) {
3433ef0cfb34SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->gnss_port);
3434ef0cfb34SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyGNSS");
3435773bda96SJonathan Lemon 	}
343671d7e085SJonathan Lemon 	if (bp->gnss2_port != -1) {
343771d7e085SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->gnss2_port);
343871d7e085SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyGNSS2");
343971d7e085SJonathan Lemon 	}
3440773bda96SJonathan Lemon 	if (bp->mac_port != -1) {
3441773bda96SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->mac_port);
3442773bda96SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyMAC");
3443773bda96SJonathan Lemon 	}
3444e3516bb4SJonathan Lemon 	if (bp->nmea_port != -1) {
3445e3516bb4SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->nmea_port);
3446e3516bb4SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyNMEA");
3447e3516bb4SJonathan Lemon 	}
3448773bda96SJonathan Lemon 	sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
3449773bda96SJonathan Lemon 	ptp_ocp_link_child(bp, buf, "ptp");
3450773bda96SJonathan Lemon 
3451773bda96SJonathan Lemon 	pps = pps_lookup_dev(bp->ptp);
3452773bda96SJonathan Lemon 	if (pps)
3453773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, pps->dev, "pps");
3454773bda96SJonathan Lemon 
3455f67bf662SJonathan Lemon 	ptp_ocp_debugfs_add_device(bp);
3456f67bf662SJonathan Lemon 
3457773bda96SJonathan Lemon 	return 0;
3458773bda96SJonathan Lemon }
3459773bda96SJonathan Lemon 
3460773bda96SJonathan Lemon static void
3461065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp)
3462065efcc5SJonathan Lemon {
3463065efcc5SJonathan Lemon 	struct timespec64 ts;
3464065efcc5SJonathan Lemon 	u32 version, select;
3465065efcc5SJonathan Lemon 	bool sync;
3466065efcc5SJonathan Lemon 
3467065efcc5SJonathan Lemon 	version = ioread32(&bp->reg->version);
3468065efcc5SJonathan Lemon 	select = ioread32(&bp->reg->select);
3469065efcc5SJonathan Lemon 	dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
3470065efcc5SJonathan Lemon 		 version >> 24, (version >> 16) & 0xff, version & 0xffff,
3471065efcc5SJonathan Lemon 		 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
3472065efcc5SJonathan Lemon 		 ptp_clock_index(bp->ptp));
3473065efcc5SJonathan Lemon 
3474065efcc5SJonathan Lemon 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
3475065efcc5SJonathan Lemon 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
3476065efcc5SJonathan Lemon 		dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
3477065efcc5SJonathan Lemon 			 ts.tv_sec, ts.tv_nsec,
3478065efcc5SJonathan Lemon 			 sync ? "in-sync" : "UNSYNCED");
3479065efcc5SJonathan Lemon }
3480065efcc5SJonathan Lemon 
3481065efcc5SJonathan Lemon static void
3482065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
3483065efcc5SJonathan Lemon {
3484065efcc5SJonathan Lemon 	if (port != -1)
3485065efcc5SJonathan Lemon 		dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
3486065efcc5SJonathan Lemon }
3487065efcc5SJonathan Lemon 
3488065efcc5SJonathan Lemon static void
3489065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp)
3490773bda96SJonathan Lemon {
3491e3516bb4SJonathan Lemon 	static int nmea_baud[] = {
3492e3516bb4SJonathan Lemon 		1200, 2400, 4800, 9600, 19200, 38400,
3493e3516bb4SJonathan Lemon 		57600, 115200, 230400, 460800, 921600,
3494e3516bb4SJonathan Lemon 		1000000, 2000000
3495e3516bb4SJonathan Lemon 	};
3496773bda96SJonathan Lemon 	struct device *dev = &bp->pdev->dev;
3497e3516bb4SJonathan Lemon 	u32 reg;
3498773bda96SJonathan Lemon 
3499065efcc5SJonathan Lemon 	ptp_ocp_phc_info(bp);
3500065efcc5SJonathan Lemon 
3501b0ca789aSJonathan Lemon 	dev_info(dev, "version %x\n", bp->fw_version);
3502b0ca789aSJonathan Lemon 	if (bp->fw_version & 0xffff)
3503773bda96SJonathan Lemon 		dev_info(dev, "regular image, version %d\n",
3504b0ca789aSJonathan Lemon 			 bp->fw_version & 0xffff);
3505773bda96SJonathan Lemon 	else
3506773bda96SJonathan Lemon 		dev_info(dev, "golden image, version %d\n",
3507b0ca789aSJonathan Lemon 			 bp->fw_version >> 16);
3508b0ca789aSJonathan Lemon 
3509065efcc5SJonathan Lemon 	ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200);
351071d7e085SJonathan Lemon 	ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200);
3511065efcc5SJonathan Lemon 	ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600);
3512e3516bb4SJonathan Lemon 	if (bp->nmea_out && bp->nmea_port != -1) {
3513e3516bb4SJonathan Lemon 		int baud = -1;
3514e3516bb4SJonathan Lemon 
3515e3516bb4SJonathan Lemon 		reg = ioread32(&bp->nmea_out->uart_baud);
3516e3516bb4SJonathan Lemon 		if (reg < ARRAY_SIZE(nmea_baud))
3517e3516bb4SJonathan Lemon 			baud = nmea_baud[reg];
3518e3516bb4SJonathan Lemon 		ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud);
3519e3516bb4SJonathan Lemon 	}
3520773bda96SJonathan Lemon }
3521773bda96SJonathan Lemon 
3522773bda96SJonathan Lemon static void
3523773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
3524773bda96SJonathan Lemon {
3525773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
3526773bda96SJonathan Lemon 
3527ef0cfb34SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyGNSS");
3528773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyMAC");
3529773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ptp");
3530773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "pps");
3531773bda96SJonathan Lemon }
3532773bda96SJonathan Lemon 
3533773bda96SJonathan Lemon static void
3534773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp)
3535773bda96SJonathan Lemon {
3536b325af3cSJonathan Lemon 	int i;
3537b325af3cSJonathan Lemon 
3538f67bf662SJonathan Lemon 	ptp_ocp_debugfs_remove_device(bp);
3539773bda96SJonathan Lemon 	ptp_ocp_detach_sysfs(bp);
3540c2239294SJonathan Lemon 	ptp_ocp_attr_group_del(bp);
3541773bda96SJonathan Lemon 	if (timer_pending(&bp->watchdog))
3542773bda96SJonathan Lemon 		del_timer_sync(&bp->watchdog);
3543773bda96SJonathan Lemon 	if (bp->ts0)
3544773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts0);
3545773bda96SJonathan Lemon 	if (bp->ts1)
3546773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts1);
3547dcf61469SJonathan Lemon 	if (bp->ts2)
3548dcf61469SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts2);
35490fa3ff7eSJonathan Lemon 	if (bp->ts3)
35500fa3ff7eSJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts3);
35510fa3ff7eSJonathan Lemon 	if (bp->ts4)
35520fa3ff7eSJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts4);
3553773bda96SJonathan Lemon 	if (bp->pps)
3554773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->pps);
3555b325af3cSJonathan Lemon 	for (i = 0; i < 4; i++)
3556b325af3cSJonathan Lemon 		if (bp->signal_out[i])
3557b325af3cSJonathan Lemon 			ptp_ocp_unregister_ext(bp->signal_out[i]);
3558ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1)
3559ef0cfb34SJonathan Lemon 		serial8250_unregister_port(bp->gnss_port);
356071d7e085SJonathan Lemon 	if (bp->gnss2_port != -1)
356171d7e085SJonathan Lemon 		serial8250_unregister_port(bp->gnss2_port);
3562773bda96SJonathan Lemon 	if (bp->mac_port != -1)
3563773bda96SJonathan Lemon 		serial8250_unregister_port(bp->mac_port);
3564e3516bb4SJonathan Lemon 	if (bp->nmea_port != -1)
3565e3516bb4SJonathan Lemon 		serial8250_unregister_port(bp->nmea_port);
3566773bda96SJonathan Lemon 	if (bp->spi_flash)
3567773bda96SJonathan Lemon 		platform_device_unregister(bp->spi_flash);
3568773bda96SJonathan Lemon 	if (bp->i2c_ctrl)
3569773bda96SJonathan Lemon 		platform_device_unregister(bp->i2c_ctrl);
3570773bda96SJonathan Lemon 	if (bp->i2c_clk)
3571773bda96SJonathan Lemon 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
3572773bda96SJonathan Lemon 	if (bp->n_irqs)
3573773bda96SJonathan Lemon 		pci_free_irq_vectors(bp->pdev);
3574773bda96SJonathan Lemon 	if (bp->ptp)
3575773bda96SJonathan Lemon 		ptp_clock_unregister(bp->ptp);
35761aa66a3aSJonathan Lemon 	kfree(bp->ptp_info.pin_config);
3577773bda96SJonathan Lemon 	device_unregister(&bp->dev);
3578773bda96SJonathan Lemon }
3579773bda96SJonathan Lemon 
3580a7e1abadSJonathan Lemon static int
3581a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3582a7e1abadSJonathan Lemon {
3583773bda96SJonathan Lemon 	struct devlink *devlink;
3584a7e1abadSJonathan Lemon 	struct ptp_ocp *bp;
3585a7e1abadSJonathan Lemon 	int err;
3586a7e1abadSJonathan Lemon 
3587919d13a7SLeon Romanovsky 	devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
3588773bda96SJonathan Lemon 	if (!devlink) {
3589773bda96SJonathan Lemon 		dev_err(&pdev->dev, "devlink_alloc failed\n");
3590a7e1abadSJonathan Lemon 		return -ENOMEM;
3591773bda96SJonathan Lemon 	}
3592773bda96SJonathan Lemon 
3593a7e1abadSJonathan Lemon 	err = pci_enable_device(pdev);
3594a7e1abadSJonathan Lemon 	if (err) {
3595a7e1abadSJonathan Lemon 		dev_err(&pdev->dev, "pci_enable_device\n");
35964587369bSJonathan Lemon 		goto out_free;
3597a7e1abadSJonathan Lemon 	}
3598a7e1abadSJonathan Lemon 
3599773bda96SJonathan Lemon 	bp = devlink_priv(devlink);
3600773bda96SJonathan Lemon 	err = ptp_ocp_device_init(bp, pdev);
3601773bda96SJonathan Lemon 	if (err)
3602d9fdbf13SJonathan Lemon 		goto out_disable;
3603a7e1abadSJonathan Lemon 
3604773bda96SJonathan Lemon 	/* compat mode.
3605773bda96SJonathan Lemon 	 * Older FPGA firmware only returns 2 irq's.
3606773bda96SJonathan Lemon 	 * allow this - if not all of the IRQ's are returned, skip the
3607773bda96SJonathan Lemon 	 * extra devices and just register the clock.
3608773bda96SJonathan Lemon 	 */
36090fa3ff7eSJonathan Lemon 	err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX);
3610773bda96SJonathan Lemon 	if (err < 0) {
3611773bda96SJonathan Lemon 		dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
3612773bda96SJonathan Lemon 		goto out;
3613a7e1abadSJonathan Lemon 	}
3614773bda96SJonathan Lemon 	bp->n_irqs = err;
3615773bda96SJonathan Lemon 	pci_set_master(pdev);
3616a7e1abadSJonathan Lemon 
3617773bda96SJonathan Lemon 	err = ptp_ocp_register_resources(bp, id->driver_data);
3618a7e1abadSJonathan Lemon 	if (err)
3619a7e1abadSJonathan Lemon 		goto out;
3620a7e1abadSJonathan Lemon 
3621a7e1abadSJonathan Lemon 	bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
3622a7e1abadSJonathan Lemon 	if (IS_ERR(bp->ptp)) {
3623a7e1abadSJonathan Lemon 		err = PTR_ERR(bp->ptp);
3624773bda96SJonathan Lemon 		dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
3625773bda96SJonathan Lemon 		bp->ptp = NULL;
3626a7e1abadSJonathan Lemon 		goto out;
3627a7e1abadSJonathan Lemon 	}
3628a7e1abadSJonathan Lemon 
3629773bda96SJonathan Lemon 	err = ptp_ocp_complete(bp);
3630773bda96SJonathan Lemon 	if (err)
3631773bda96SJonathan Lemon 		goto out;
3632773bda96SJonathan Lemon 
3633a7e1abadSJonathan Lemon 	ptp_ocp_info(bp);
3634c89f78e9SLeon Romanovsky 	devlink_register(devlink);
3635a7e1abadSJonathan Lemon 	return 0;
3636a7e1abadSJonathan Lemon 
3637a7e1abadSJonathan Lemon out:
3638773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
3639773bda96SJonathan Lemon 	pci_set_drvdata(pdev, NULL);
3640d9fdbf13SJonathan Lemon out_disable:
3641d9fdbf13SJonathan Lemon 	pci_disable_device(pdev);
36424587369bSJonathan Lemon out_free:
3643773bda96SJonathan Lemon 	devlink_free(devlink);
3644a7e1abadSJonathan Lemon 	return err;
3645a7e1abadSJonathan Lemon }
3646a7e1abadSJonathan Lemon 
3647a7e1abadSJonathan Lemon static void
3648a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev)
3649a7e1abadSJonathan Lemon {
3650a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = pci_get_drvdata(pdev);
3651773bda96SJonathan Lemon 	struct devlink *devlink = priv_to_devlink(bp);
3652a7e1abadSJonathan Lemon 
3653c89f78e9SLeon Romanovsky 	devlink_unregister(devlink);
3654773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
3655a7e1abadSJonathan Lemon 	pci_set_drvdata(pdev, NULL);
3656d9fdbf13SJonathan Lemon 	pci_disable_device(pdev);
3657773bda96SJonathan Lemon 
3658773bda96SJonathan Lemon 	devlink_free(devlink);
3659a7e1abadSJonathan Lemon }
3660a7e1abadSJonathan Lemon 
3661a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = {
3662a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
3663a7e1abadSJonathan Lemon 	.id_table	= ptp_ocp_pcidev_id,
3664a7e1abadSJonathan Lemon 	.probe		= ptp_ocp_probe,
3665a7e1abadSJonathan Lemon 	.remove		= ptp_ocp_remove,
3666a7e1abadSJonathan Lemon };
3667a7e1abadSJonathan Lemon 
3668773bda96SJonathan Lemon static int
3669773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
3670773bda96SJonathan Lemon 			  unsigned long action, void *data)
3671773bda96SJonathan Lemon {
3672773bda96SJonathan Lemon 	struct device *dev, *child = data;
3673773bda96SJonathan Lemon 	struct ptp_ocp *bp;
3674773bda96SJonathan Lemon 	bool add;
3675773bda96SJonathan Lemon 
3676773bda96SJonathan Lemon 	switch (action) {
3677773bda96SJonathan Lemon 	case BUS_NOTIFY_ADD_DEVICE:
3678773bda96SJonathan Lemon 	case BUS_NOTIFY_DEL_DEVICE:
3679773bda96SJonathan Lemon 		add = action == BUS_NOTIFY_ADD_DEVICE;
3680773bda96SJonathan Lemon 		break;
3681773bda96SJonathan Lemon 	default:
3682773bda96SJonathan Lemon 		return 0;
3683773bda96SJonathan Lemon 	}
3684773bda96SJonathan Lemon 
3685773bda96SJonathan Lemon 	if (!i2c_verify_adapter(child))
3686773bda96SJonathan Lemon 		return 0;
3687773bda96SJonathan Lemon 
3688773bda96SJonathan Lemon 	dev = child;
3689773bda96SJonathan Lemon 	while ((dev = dev->parent))
3690773bda96SJonathan Lemon 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
3691773bda96SJonathan Lemon 			goto found;
3692773bda96SJonathan Lemon 	return 0;
3693773bda96SJonathan Lemon 
3694773bda96SJonathan Lemon found:
3695773bda96SJonathan Lemon 	bp = dev_get_drvdata(dev);
3696773bda96SJonathan Lemon 	if (add)
3697773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, child, "i2c");
3698773bda96SJonathan Lemon 	else
3699773bda96SJonathan Lemon 		sysfs_remove_link(&bp->dev.kobj, "i2c");
3700773bda96SJonathan Lemon 
3701773bda96SJonathan Lemon 	return 0;
3702773bda96SJonathan Lemon }
3703773bda96SJonathan Lemon 
3704773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = {
3705773bda96SJonathan Lemon 	.notifier_call = ptp_ocp_i2c_notifier_call,
3706773bda96SJonathan Lemon };
3707773bda96SJonathan Lemon 
3708a7e1abadSJonathan Lemon static int __init
3709a7e1abadSJonathan Lemon ptp_ocp_init(void)
3710a7e1abadSJonathan Lemon {
3711773bda96SJonathan Lemon 	const char *what;
3712a7e1abadSJonathan Lemon 	int err;
3713a7e1abadSJonathan Lemon 
3714f67bf662SJonathan Lemon 	ptp_ocp_debugfs_init();
3715f67bf662SJonathan Lemon 
3716773bda96SJonathan Lemon 	what = "timecard class";
3717773bda96SJonathan Lemon 	err = class_register(&timecard_class);
3718773bda96SJonathan Lemon 	if (err)
3719773bda96SJonathan Lemon 		goto out;
3720773bda96SJonathan Lemon 
3721773bda96SJonathan Lemon 	what = "i2c notifier";
3722773bda96SJonathan Lemon 	err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3723773bda96SJonathan Lemon 	if (err)
3724773bda96SJonathan Lemon 		goto out_notifier;
3725773bda96SJonathan Lemon 
3726773bda96SJonathan Lemon 	what = "ptp_ocp driver";
3727a7e1abadSJonathan Lemon 	err = pci_register_driver(&ptp_ocp_driver);
3728773bda96SJonathan Lemon 	if (err)
3729773bda96SJonathan Lemon 		goto out_register;
3730773bda96SJonathan Lemon 
3731773bda96SJonathan Lemon 	return 0;
3732773bda96SJonathan Lemon 
3733773bda96SJonathan Lemon out_register:
3734773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3735773bda96SJonathan Lemon out_notifier:
3736773bda96SJonathan Lemon 	class_unregister(&timecard_class);
3737773bda96SJonathan Lemon out:
3738f67bf662SJonathan Lemon 	ptp_ocp_debugfs_fini();
3739773bda96SJonathan Lemon 	pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
3740a7e1abadSJonathan Lemon 	return err;
3741a7e1abadSJonathan Lemon }
3742a7e1abadSJonathan Lemon 
3743a7e1abadSJonathan Lemon static void __exit
3744a7e1abadSJonathan Lemon ptp_ocp_fini(void)
3745a7e1abadSJonathan Lemon {
3746773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3747a7e1abadSJonathan Lemon 	pci_unregister_driver(&ptp_ocp_driver);
3748773bda96SJonathan Lemon 	class_unregister(&timecard_class);
3749f67bf662SJonathan Lemon 	ptp_ocp_debugfs_fini();
3750a7e1abadSJonathan Lemon }
3751a7e1abadSJonathan Lemon 
3752a7e1abadSJonathan Lemon module_init(ptp_ocp_init);
3753a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini);
3754a7e1abadSJonathan Lemon 
3755a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver");
3756a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2");
3757