xref: /openbmc/linux/drivers/ptp/ptp_ocp.c (revision 3f3fe41c0bdfe541aa7aa9a8625efe3bd3ef60cc)
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 #define PCI_VENDOR_ID_FACEBOOK			0x1d9b
24773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD		0x0400
25773bda96SJonathan Lemon 
2681fa652eSVadim Fedorenko #define PCI_VENDOR_ID_CELESTICA			0x18d4
2781fa652eSVadim Fedorenko #define PCI_DEVICE_ID_CELESTICA_TIMECARD	0x1008
2881fa652eSVadim Fedorenko 
29773bda96SJonathan Lemon static struct class timecard_class = {
30773bda96SJonathan Lemon 	.owner		= THIS_MODULE,
31773bda96SJonathan Lemon 	.name		= "timecard",
32a7e1abadSJonathan Lemon };
33a7e1abadSJonathan Lemon 
34a7e1abadSJonathan Lemon struct ocp_reg {
35a7e1abadSJonathan Lemon 	u32	ctrl;
36a7e1abadSJonathan Lemon 	u32	status;
37a7e1abadSJonathan Lemon 	u32	select;
38a7e1abadSJonathan Lemon 	u32	version;
39a7e1abadSJonathan Lemon 	u32	time_ns;
40a7e1abadSJonathan Lemon 	u32	time_sec;
41a7e1abadSJonathan Lemon 	u32	__pad0[2];
42a7e1abadSJonathan Lemon 	u32	adjust_ns;
43a7e1abadSJonathan Lemon 	u32	adjust_sec;
44a7e1abadSJonathan Lemon 	u32	__pad1[2];
45a7e1abadSJonathan Lemon 	u32	offset_ns;
46a7e1abadSJonathan Lemon 	u32	offset_window_ns;
47773bda96SJonathan Lemon 	u32	__pad2[2];
48773bda96SJonathan Lemon 	u32	drift_ns;
49773bda96SJonathan Lemon 	u32	drift_window_ns;
50773bda96SJonathan Lemon 	u32	__pad3[6];
51773bda96SJonathan Lemon 	u32	servo_offset_p;
52773bda96SJonathan Lemon 	u32	servo_offset_i;
53773bda96SJonathan Lemon 	u32	servo_drift_p;
54773bda96SJonathan Lemon 	u32	servo_drift_i;
552f23f486SVadim Fedorenko 	u32	status_offset;
562f23f486SVadim Fedorenko 	u32	status_drift;
57a7e1abadSJonathan Lemon };
58a7e1abadSJonathan Lemon 
59a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE		BIT(0)
60a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME	BIT(1)
61a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET	BIT(2)
62773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT	BIT(3)
63773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO	BIT(8)
64a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ	BIT(30)
65a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE	BIT(31)
66a7e1abadSJonathan Lemon 
67a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC	BIT(0)
68773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER	BIT(1)
69a7e1abadSJonathan Lemon 
70a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE	0
71773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG	0xfe
72a7e1abadSJonathan Lemon 
73a7e1abadSJonathan Lemon struct tod_reg {
74a7e1abadSJonathan Lemon 	u32	ctrl;
75a7e1abadSJonathan Lemon 	u32	status;
76a7e1abadSJonathan Lemon 	u32	uart_polarity;
77a7e1abadSJonathan Lemon 	u32	version;
78065efcc5SJonathan Lemon 	u32	adj_sec;
79a7e1abadSJonathan Lemon 	u32	__pad0[3];
80a7e1abadSJonathan Lemon 	u32	uart_baud;
81a7e1abadSJonathan Lemon 	u32	__pad1[3];
82a7e1abadSJonathan Lemon 	u32	utc_status;
83a7e1abadSJonathan Lemon 	u32	leap;
84a7e1abadSJonathan Lemon };
85a7e1abadSJonathan Lemon 
86a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL	BIT(28)
87a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A	BIT(17)
88a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B	BIT(16)
89a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE		BIT(0)
90a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK	((1U << 4) - 1)
91a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT	24
92a7e1abadSJonathan Lemon 
93a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK		0xff
94a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID		BIT(8)
959f492c4cSVadim Fedorenko #define TOD_STATUS_LEAP_ANNOUNCE	BIT(12)
96a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID		BIT(16)
97a7e1abadSJonathan Lemon 
98773bda96SJonathan Lemon struct ts_reg {
99773bda96SJonathan Lemon 	u32	enable;
100773bda96SJonathan Lemon 	u32	error;
101773bda96SJonathan Lemon 	u32	polarity;
102773bda96SJonathan Lemon 	u32	version;
103773bda96SJonathan Lemon 	u32	__pad0[4];
104773bda96SJonathan Lemon 	u32	cable_delay;
105773bda96SJonathan Lemon 	u32	__pad1[3];
106773bda96SJonathan Lemon 	u32	intr;
107773bda96SJonathan Lemon 	u32	intr_mask;
108773bda96SJonathan Lemon 	u32	event_count;
109773bda96SJonathan Lemon 	u32	__pad2[1];
110773bda96SJonathan Lemon 	u32	ts_count;
111773bda96SJonathan Lemon 	u32	time_ns;
112773bda96SJonathan Lemon 	u32	time_sec;
113773bda96SJonathan Lemon 	u32	data_width;
114773bda96SJonathan Lemon 	u32	data;
115773bda96SJonathan Lemon };
116773bda96SJonathan Lemon 
117773bda96SJonathan Lemon struct pps_reg {
118773bda96SJonathan Lemon 	u32	ctrl;
119773bda96SJonathan Lemon 	u32	status;
1200d43d4f2SJonathan Lemon 	u32	__pad0[6];
1210d43d4f2SJonathan Lemon 	u32	cable_delay;
122773bda96SJonathan Lemon };
123773bda96SJonathan Lemon 
124773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR	BIT(0)
125773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR	BIT(1)
126773bda96SJonathan Lemon 
127773bda96SJonathan Lemon struct img_reg {
128773bda96SJonathan Lemon 	u32	version;
129773bda96SJonathan Lemon };
130773bda96SJonathan Lemon 
131e1daf0ecSJonathan Lemon struct gpio_reg {
132e1daf0ecSJonathan Lemon 	u32	gpio1;
133e1daf0ecSJonathan Lemon 	u32	__pad0;
134e1daf0ecSJonathan Lemon 	u32	gpio2;
135e1daf0ecSJonathan Lemon 	u32	__pad1;
136e1daf0ecSJonathan Lemon };
137e1daf0ecSJonathan Lemon 
1386baf2925SJonathan Lemon struct irig_master_reg {
1396baf2925SJonathan Lemon 	u32	ctrl;
1406baf2925SJonathan Lemon 	u32	status;
1416baf2925SJonathan Lemon 	u32	__pad0;
1426baf2925SJonathan Lemon 	u32	version;
1436baf2925SJonathan Lemon 	u32	adj_sec;
1446baf2925SJonathan Lemon 	u32	mode_ctrl;
1456baf2925SJonathan Lemon };
1466baf2925SJonathan Lemon 
1476baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE	BIT(0)
1486baf2925SJonathan Lemon 
1496baf2925SJonathan Lemon struct irig_slave_reg {
1506baf2925SJonathan Lemon 	u32	ctrl;
1516baf2925SJonathan Lemon 	u32	status;
1526baf2925SJonathan Lemon 	u32	__pad0;
1536baf2925SJonathan Lemon 	u32	version;
1546baf2925SJonathan Lemon 	u32	adj_sec;
1556baf2925SJonathan Lemon 	u32	mode_ctrl;
1566baf2925SJonathan Lemon };
1576baf2925SJonathan Lemon 
1586baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE	BIT(0)
1596baf2925SJonathan Lemon 
1606baf2925SJonathan Lemon struct dcf_master_reg {
1616baf2925SJonathan Lemon 	u32	ctrl;
1626baf2925SJonathan Lemon 	u32	status;
1636baf2925SJonathan Lemon 	u32	__pad0;
1646baf2925SJonathan Lemon 	u32	version;
1656baf2925SJonathan Lemon 	u32	adj_sec;
1666baf2925SJonathan Lemon };
1676baf2925SJonathan Lemon 
1686baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE	BIT(0)
1696baf2925SJonathan Lemon 
1706baf2925SJonathan Lemon struct dcf_slave_reg {
1716baf2925SJonathan Lemon 	u32	ctrl;
1726baf2925SJonathan Lemon 	u32	status;
1736baf2925SJonathan Lemon 	u32	__pad0;
1746baf2925SJonathan Lemon 	u32	version;
1756baf2925SJonathan Lemon 	u32	adj_sec;
1766baf2925SJonathan Lemon };
1776baf2925SJonathan Lemon 
1786baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE	BIT(0)
1796baf2925SJonathan Lemon 
180b325af3cSJonathan Lemon struct signal_reg {
181b325af3cSJonathan Lemon 	u32	enable;
182b325af3cSJonathan Lemon 	u32	status;
183b325af3cSJonathan Lemon 	u32	polarity;
184b325af3cSJonathan Lemon 	u32	version;
185b325af3cSJonathan Lemon 	u32	__pad0[4];
186b325af3cSJonathan Lemon 	u32	cable_delay;
187b325af3cSJonathan Lemon 	u32	__pad1[3];
188b325af3cSJonathan Lemon 	u32	intr;
189b325af3cSJonathan Lemon 	u32	intr_mask;
190b325af3cSJonathan Lemon 	u32	__pad2[2];
191b325af3cSJonathan Lemon 	u32	start_ns;
192b325af3cSJonathan Lemon 	u32	start_sec;
193b325af3cSJonathan Lemon 	u32	pulse_ns;
194b325af3cSJonathan Lemon 	u32	pulse_sec;
195b325af3cSJonathan Lemon 	u32	period_ns;
196b325af3cSJonathan Lemon 	u32	period_sec;
197b325af3cSJonathan Lemon 	u32	repeat_count;
198b325af3cSJonathan Lemon };
199b325af3cSJonathan Lemon 
2002407f5d6SJonathan Lemon struct frequency_reg {
2012407f5d6SJonathan Lemon 	u32	ctrl;
2022407f5d6SJonathan Lemon 	u32	status;
2032407f5d6SJonathan Lemon };
2042407f5d6SJonathan Lemon #define FREQ_STATUS_VALID	BIT(31)
2052407f5d6SJonathan Lemon #define FREQ_STATUS_ERROR	BIT(30)
2062407f5d6SJonathan Lemon #define FREQ_STATUS_OVERRUN	BIT(29)
2072407f5d6SJonathan Lemon #define FREQ_STATUS_MASK	(BIT(24) - 1)
2082407f5d6SJonathan Lemon 
209773bda96SJonathan Lemon struct ptp_ocp_flash_info {
210773bda96SJonathan Lemon 	const char *name;
211773bda96SJonathan Lemon 	int pci_offset;
212773bda96SJonathan Lemon 	int data_size;
213773bda96SJonathan Lemon 	void *data;
214773bda96SJonathan Lemon };
215773bda96SJonathan Lemon 
2161618df6aSJonathan Lemon struct ptp_ocp_i2c_info {
2171618df6aSJonathan Lemon 	const char *name;
2181618df6aSJonathan Lemon 	unsigned long fixed_rate;
2191618df6aSJonathan Lemon 	size_t data_size;
2201618df6aSJonathan Lemon 	void *data;
2211618df6aSJonathan Lemon };
2221618df6aSJonathan Lemon 
223773bda96SJonathan Lemon struct ptp_ocp_ext_info {
224773bda96SJonathan Lemon 	int index;
225773bda96SJonathan Lemon 	irqreturn_t (*irq_fcn)(int irq, void *priv);
226a62a56d0SJonathan Lemon 	int (*enable)(void *priv, u32 req, bool enable);
227773bda96SJonathan Lemon };
228773bda96SJonathan Lemon 
229773bda96SJonathan Lemon struct ptp_ocp_ext_src {
230773bda96SJonathan Lemon 	void __iomem		*mem;
231773bda96SJonathan Lemon 	struct ptp_ocp		*bp;
232773bda96SJonathan Lemon 	struct ptp_ocp_ext_info	*info;
233773bda96SJonathan Lemon 	int			irq_vec;
234773bda96SJonathan Lemon };
235773bda96SJonathan Lemon 
236a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode {
237a509a7c6SJonathan Lemon 	SMA_MODE_IN,
238a509a7c6SJonathan Lemon 	SMA_MODE_OUT,
239a509a7c6SJonathan Lemon };
240a509a7c6SJonathan Lemon 
241a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector {
242a509a7c6SJonathan Lemon 	enum	ptp_ocp_sma_mode mode;
243a509a7c6SJonathan Lemon 	bool	fixed_fcn;
244a509a7c6SJonathan Lemon 	bool	fixed_dir;
245b2c4f0acSJonathan Lemon 	bool	disabled;
246a509a7c6SJonathan Lemon };
247a509a7c6SJonathan Lemon 
248c205d53cSJonathan Lemon struct ocp_attr_group {
249c205d53cSJonathan Lemon 	u64 cap;
250c205d53cSJonathan Lemon 	const struct attribute_group *group;
251c205d53cSJonathan Lemon };
252c205d53cSJonathan Lemon 
253c205d53cSJonathan Lemon #define OCP_CAP_BASIC	BIT(0)
254b325af3cSJonathan Lemon #define OCP_CAP_SIGNAL	BIT(1)
2552407f5d6SJonathan Lemon #define OCP_CAP_FREQ	BIT(2)
256b325af3cSJonathan Lemon 
257b325af3cSJonathan Lemon struct ptp_ocp_signal {
258b325af3cSJonathan Lemon 	ktime_t		period;
259b325af3cSJonathan Lemon 	ktime_t		pulse;
260b325af3cSJonathan Lemon 	ktime_t		phase;
261b325af3cSJonathan Lemon 	ktime_t		start;
262b325af3cSJonathan Lemon 	int		duty;
263b325af3cSJonathan Lemon 	bool		polarity;
264b325af3cSJonathan Lemon 	bool		running;
265b325af3cSJonathan Lemon };
266c205d53cSJonathan Lemon 
2670cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN		13
2680cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN			6
2690cfcdd1eSJonathan Lemon 
270a7e1abadSJonathan Lemon struct ptp_ocp {
271a7e1abadSJonathan Lemon 	struct pci_dev		*pdev;
272773bda96SJonathan Lemon 	struct device		dev;
273a7e1abadSJonathan Lemon 	spinlock_t		lock;
274a7e1abadSJonathan Lemon 	struct ocp_reg __iomem	*reg;
275a7e1abadSJonathan Lemon 	struct tod_reg __iomem	*tod;
2760d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_ext;
2770d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_clk;
278f67bf662SJonathan Lemon 	struct gpio_reg __iomem	*pps_select;
279a509a7c6SJonathan Lemon 	struct gpio_reg __iomem	*sma_map1;
280a509a7c6SJonathan Lemon 	struct gpio_reg __iomem	*sma_map2;
2816baf2925SJonathan Lemon 	struct irig_master_reg	__iomem *irig_out;
2826baf2925SJonathan Lemon 	struct irig_slave_reg	__iomem *irig_in;
2836baf2925SJonathan Lemon 	struct dcf_master_reg	__iomem *dcf_out;
2846baf2925SJonathan Lemon 	struct dcf_slave_reg	__iomem *dcf_in;
285e3516bb4SJonathan Lemon 	struct tod_reg		__iomem *nmea_out;
2862407f5d6SJonathan Lemon 	struct frequency_reg	__iomem *freq_in[4];
287b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src	*signal_out[4];
288773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*pps;
289773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts0;
290773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts1;
291dcf61469SJonathan Lemon 	struct ptp_ocp_ext_src	*ts2;
2920fa3ff7eSJonathan Lemon 	struct ptp_ocp_ext_src	*ts3;
2930fa3ff7eSJonathan Lemon 	struct ptp_ocp_ext_src	*ts4;
294773bda96SJonathan Lemon 	struct img_reg __iomem	*image;
295a7e1abadSJonathan Lemon 	struct ptp_clock	*ptp;
296a7e1abadSJonathan Lemon 	struct ptp_clock_info	ptp_info;
297773bda96SJonathan Lemon 	struct platform_device	*i2c_ctrl;
298773bda96SJonathan Lemon 	struct platform_device	*spi_flash;
299773bda96SJonathan Lemon 	struct clk_hw		*i2c_clk;
300773bda96SJonathan Lemon 	struct timer_list	watchdog;
301c2239294SJonathan Lemon 	const struct attribute_group **attr_group;
3020cfcdd1eSJonathan Lemon 	const struct ptp_ocp_eeprom_map *eeprom_map;
303f67bf662SJonathan Lemon 	struct dentry		*debug_root;
304ef0cfb34SJonathan Lemon 	time64_t		gnss_lost;
305773bda96SJonathan Lemon 	int			id;
306773bda96SJonathan Lemon 	int			n_irqs;
307ef0cfb34SJonathan Lemon 	int			gnss_port;
30871d7e085SJonathan Lemon 	int			gnss2_port;
309773bda96SJonathan Lemon 	int			mac_port;	/* miniature atomic clock */
310e3516bb4SJonathan Lemon 	int			nmea_port;
3115a728ac5SJonathan Lemon 	bool			fw_loader;
3125a728ac5SJonathan Lemon 	u8			fw_tag;
3135a728ac5SJonathan Lemon 	u16			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];
324aa56a7ffSJonathan Lemon 	u8			sma_tbl;
325a7e1abadSJonathan Lemon };
326a7e1abadSJonathan Lemon 
327a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP	BIT(0)
328a62a56d0SJonathan Lemon #define OCP_REQ_PPS		BIT(1)
329a62a56d0SJonathan Lemon 
330773bda96SJonathan Lemon struct ocp_resource {
331773bda96SJonathan Lemon 	unsigned long offset;
332773bda96SJonathan Lemon 	int size;
333773bda96SJonathan Lemon 	int irq_vec;
334773bda96SJonathan Lemon 	int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
335773bda96SJonathan Lemon 	void *extra;
336773bda96SJonathan Lemon 	unsigned long bp_offset;
33756ec4403SJonathan Lemon 	const char * const name;
338773bda96SJonathan Lemon };
339773bda96SJonathan Lemon 
340773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
341773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
342773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
343773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
344773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
345773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
346773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
347b325af3cSJonathan Lemon static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv);
348a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
3491aa66a3aSJonathan Lemon static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
3501aa66a3aSJonathan Lemon 				      struct ptp_perout_request *req);
351b325af3cSJonathan Lemon static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable);
352b325af3cSJonathan Lemon static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr);
353773bda96SJonathan Lemon 
354c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[];
355c205d53cSJonathan Lemon 
3560cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map {
3570cfcdd1eSJonathan Lemon 	u16	off;
3580cfcdd1eSJonathan Lemon 	u16	len;
3590cfcdd1eSJonathan Lemon 	u32	bp_offset;
3600cfcdd1eSJonathan Lemon 	const void * const tag;
3610cfcdd1eSJonathan Lemon };
3620cfcdd1eSJonathan Lemon 
3630cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member)				\
3640cfcdd1eSJonathan Lemon 	.off = addr,						\
3650cfcdd1eSJonathan Lemon 	.len = sizeof_field(struct ptp_ocp, member),		\
3660cfcdd1eSJonathan Lemon 	.bp_offset = offsetof(struct ptp_ocp, member)
3670cfcdd1eSJonathan Lemon 
3680cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({				\
3690cfcdd1eSJonathan Lemon 	(void *)((uintptr_t)(bp) + (map)->bp_offset);		\
3700cfcdd1eSJonathan Lemon })
3710cfcdd1eSJonathan Lemon 
3720cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
3730cfcdd1eSJonathan Lemon 	{ EEPROM_ENTRY(0x43, board_id) },
3740cfcdd1eSJonathan Lemon 	{ EEPROM_ENTRY(0x00, serial), .tag = "mac" },
3750cfcdd1eSJonathan Lemon 	{ }
3760cfcdd1eSJonathan Lemon };
3770cfcdd1eSJonathan Lemon 
378773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({				\
379773bda96SJonathan Lemon 	uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset;		\
380773bda96SJonathan Lemon 	*(typeof(val) *)addr = val;					\
381773bda96SJonathan Lemon })
382773bda96SJonathan Lemon 
383773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \
38456ec4403SJonathan Lemon 	.name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
385773bda96SJonathan Lemon 
386773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \
387773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
388773bda96SJonathan Lemon 
389773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \
390773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
391773bda96SJonathan Lemon 
392773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \
393773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
394773bda96SJonathan Lemon 
395773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \
396773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
397773bda96SJonathan Lemon 
398773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \
399773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
400773bda96SJonathan Lemon 
401773bda96SJonathan Lemon /* This is the MSI vector mapping used.
4020fa3ff7eSJonathan Lemon  * 0: PPS (TS5)
403773bda96SJonathan Lemon  * 1: TS0
404773bda96SJonathan Lemon  * 2: TS1
405be69087cSJonathan Lemon  * 3: GNSS1
40671d7e085SJonathan Lemon  * 4: GNSS2
407773bda96SJonathan Lemon  * 5: MAC
408dcf61469SJonathan Lemon  * 6: TS2
4091447149dSJonathan Lemon  * 7: I2C controller
410e3516bb4SJonathan Lemon  * 8: HWICAP (notused)
411773bda96SJonathan Lemon  * 9: SPI Flash
412e3516bb4SJonathan Lemon  * 10: NMEA
413b325af3cSJonathan Lemon  * 11: Signal Generator 1
414b325af3cSJonathan Lemon  * 12: Signal Generator 2
415b325af3cSJonathan Lemon  * 13: Signal Generator 3
416b325af3cSJonathan Lemon  * 14: Signal Generator 4
4170fa3ff7eSJonathan Lemon  * 15: TS3
4180fa3ff7eSJonathan Lemon  * 16: TS4
419773bda96SJonathan Lemon  */
420773bda96SJonathan Lemon 
421773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = {
422773bda96SJonathan Lemon 	{
423773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(reg),
424773bda96SJonathan Lemon 		.offset = 0x01000000, .size = 0x10000,
425773bda96SJonathan Lemon 	},
426773bda96SJonathan Lemon 	{
427773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts0),
428773bda96SJonathan Lemon 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
429773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
43056ec4403SJonathan Lemon 			.index = 0,
431773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
432773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
433773bda96SJonathan Lemon 		},
434773bda96SJonathan Lemon 	},
435773bda96SJonathan Lemon 	{
436773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts1),
437773bda96SJonathan Lemon 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
438773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
43956ec4403SJonathan Lemon 			.index = 1,
440773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
441773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
442773bda96SJonathan Lemon 		},
443773bda96SJonathan Lemon 	},
444773bda96SJonathan Lemon 	{
445dcf61469SJonathan Lemon 		OCP_EXT_RESOURCE(ts2),
446dcf61469SJonathan Lemon 		.offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
447dcf61469SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
448dcf61469SJonathan Lemon 			.index = 2,
449dcf61469SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
450dcf61469SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
451dcf61469SJonathan Lemon 		},
452dcf61469SJonathan Lemon 	},
453dcf61469SJonathan Lemon 	{
4540fa3ff7eSJonathan Lemon 		OCP_EXT_RESOURCE(ts3),
4550fa3ff7eSJonathan Lemon 		.offset = 0x01110000, .size = 0x10000, .irq_vec = 15,
4560fa3ff7eSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
4570fa3ff7eSJonathan Lemon 			.index = 3,
4580fa3ff7eSJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
4590fa3ff7eSJonathan Lemon 			.enable = ptp_ocp_ts_enable,
4600fa3ff7eSJonathan Lemon 		},
4610fa3ff7eSJonathan Lemon 	},
4620fa3ff7eSJonathan Lemon 	{
4630fa3ff7eSJonathan Lemon 		OCP_EXT_RESOURCE(ts4),
4640fa3ff7eSJonathan Lemon 		.offset = 0x01120000, .size = 0x10000, .irq_vec = 16,
4650fa3ff7eSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
4660fa3ff7eSJonathan Lemon 			.index = 4,
4670fa3ff7eSJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
4680fa3ff7eSJonathan Lemon 			.enable = ptp_ocp_ts_enable,
4690fa3ff7eSJonathan Lemon 		},
4700fa3ff7eSJonathan Lemon 	},
4710fa3ff7eSJonathan Lemon 	/* Timestamp for PHC and/or PPS generator */
4720fa3ff7eSJonathan Lemon 	{
473a62a56d0SJonathan Lemon 		OCP_EXT_RESOURCE(pps),
474a62a56d0SJonathan Lemon 		.offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
475a62a56d0SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
4760fa3ff7eSJonathan Lemon 			.index = 5,
477a62a56d0SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
478a62a56d0SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
479a62a56d0SJonathan Lemon 		},
480a62a56d0SJonathan Lemon 	},
481a62a56d0SJonathan Lemon 	{
482b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[0]),
483b325af3cSJonathan Lemon 		.offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
484b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
485b325af3cSJonathan Lemon 			.index = 1,
486b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
487b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
488b325af3cSJonathan Lemon 		},
489b325af3cSJonathan Lemon 	},
490b325af3cSJonathan Lemon 	{
491b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[1]),
492b325af3cSJonathan Lemon 		.offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
493b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
494b325af3cSJonathan Lemon 			.index = 2,
495b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
496b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
497b325af3cSJonathan Lemon 		},
498b325af3cSJonathan Lemon 	},
499b325af3cSJonathan Lemon 	{
500b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[2]),
501b325af3cSJonathan Lemon 		.offset = 0x010F0000, .size = 0x10000, .irq_vec = 13,
502b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
503b325af3cSJonathan Lemon 			.index = 3,
504b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
505b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
506b325af3cSJonathan Lemon 		},
507b325af3cSJonathan Lemon 	},
508b325af3cSJonathan Lemon 	{
509b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[3]),
510b325af3cSJonathan Lemon 		.offset = 0x01100000, .size = 0x10000, .irq_vec = 14,
511b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
512b325af3cSJonathan Lemon 			.index = 4,
513b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
514b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
515b325af3cSJonathan Lemon 		},
516b325af3cSJonathan Lemon 	},
517b325af3cSJonathan Lemon 	{
5180d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_ext),
5190d43d4f2SJonathan Lemon 		.offset = 0x01030000, .size = 0x10000,
5200d43d4f2SJonathan Lemon 	},
5210d43d4f2SJonathan Lemon 	{
5220d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_clk),
523773bda96SJonathan Lemon 		.offset = 0x01040000, .size = 0x10000,
524773bda96SJonathan Lemon 	},
525773bda96SJonathan Lemon 	{
526773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(tod),
527773bda96SJonathan Lemon 		.offset = 0x01050000, .size = 0x10000,
528773bda96SJonathan Lemon 	},
529773bda96SJonathan Lemon 	{
5306baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(irig_in),
5316baf2925SJonathan Lemon 		.offset = 0x01070000, .size = 0x10000,
5326baf2925SJonathan Lemon 	},
5336baf2925SJonathan Lemon 	{
5346baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(irig_out),
5356baf2925SJonathan Lemon 		.offset = 0x01080000, .size = 0x10000,
5366baf2925SJonathan Lemon 	},
5376baf2925SJonathan Lemon 	{
5386baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(dcf_in),
5396baf2925SJonathan Lemon 		.offset = 0x01090000, .size = 0x10000,
5406baf2925SJonathan Lemon 	},
5416baf2925SJonathan Lemon 	{
5426baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(dcf_out),
5436baf2925SJonathan Lemon 		.offset = 0x010A0000, .size = 0x10000,
5446baf2925SJonathan Lemon 	},
5456baf2925SJonathan Lemon 	{
546e3516bb4SJonathan Lemon 		OCP_MEM_RESOURCE(nmea_out),
547e3516bb4SJonathan Lemon 		.offset = 0x010B0000, .size = 0x10000,
548e3516bb4SJonathan Lemon 	},
549e3516bb4SJonathan Lemon 	{
550773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(image),
551773bda96SJonathan Lemon 		.offset = 0x00020000, .size = 0x1000,
552773bda96SJonathan Lemon 	},
553773bda96SJonathan Lemon 	{
554f67bf662SJonathan Lemon 		OCP_MEM_RESOURCE(pps_select),
555f67bf662SJonathan Lemon 		.offset = 0x00130000, .size = 0x1000,
556f67bf662SJonathan Lemon 	},
557f67bf662SJonathan Lemon 	{
558a509a7c6SJonathan Lemon 		OCP_MEM_RESOURCE(sma_map1),
559e1daf0ecSJonathan Lemon 		.offset = 0x00140000, .size = 0x1000,
560e1daf0ecSJonathan Lemon 	},
561e1daf0ecSJonathan Lemon 	{
562a509a7c6SJonathan Lemon 		OCP_MEM_RESOURCE(sma_map2),
563a509a7c6SJonathan Lemon 		.offset = 0x00220000, .size = 0x1000,
564a509a7c6SJonathan Lemon 	},
565a509a7c6SJonathan Lemon 	{
566773bda96SJonathan Lemon 		OCP_I2C_RESOURCE(i2c_ctrl),
567773bda96SJonathan Lemon 		.offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
5681618df6aSJonathan Lemon 		.extra = &(struct ptp_ocp_i2c_info) {
5691618df6aSJonathan Lemon 			.name = "xiic-i2c",
5701618df6aSJonathan Lemon 			.fixed_rate = 50000000,
5710cfcdd1eSJonathan Lemon 			.data_size = sizeof(struct xiic_i2c_platform_data),
5720cfcdd1eSJonathan Lemon 			.data = &(struct xiic_i2c_platform_data) {
5730cfcdd1eSJonathan Lemon 				.num_devices = 2,
5740cfcdd1eSJonathan Lemon 				.devices = (struct i2c_board_info[]) {
5750cfcdd1eSJonathan Lemon 					{ I2C_BOARD_INFO("24c02", 0x50) },
5760cfcdd1eSJonathan Lemon 					{ I2C_BOARD_INFO("24mac402", 0x58),
5770cfcdd1eSJonathan Lemon 					  .platform_data = "mac" },
5780cfcdd1eSJonathan Lemon 				},
5790cfcdd1eSJonathan Lemon 			},
5801618df6aSJonathan Lemon 		},
581773bda96SJonathan Lemon 	},
582773bda96SJonathan Lemon 	{
583ef0cfb34SJonathan Lemon 		OCP_SERIAL_RESOURCE(gnss_port),
584773bda96SJonathan Lemon 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
585773bda96SJonathan Lemon 	},
586773bda96SJonathan Lemon 	{
58771d7e085SJonathan Lemon 		OCP_SERIAL_RESOURCE(gnss2_port),
58871d7e085SJonathan Lemon 		.offset = 0x00170000 + 0x1000, .irq_vec = 4,
58971d7e085SJonathan Lemon 	},
59071d7e085SJonathan Lemon 	{
591773bda96SJonathan Lemon 		OCP_SERIAL_RESOURCE(mac_port),
592773bda96SJonathan Lemon 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
593773bda96SJonathan Lemon 	},
594773bda96SJonathan Lemon 	{
595e3516bb4SJonathan Lemon 		OCP_SERIAL_RESOURCE(nmea_port),
596e3516bb4SJonathan Lemon 		.offset = 0x00190000 + 0x1000, .irq_vec = 10,
597e3516bb4SJonathan Lemon 	},
598e3516bb4SJonathan Lemon 	{
599773bda96SJonathan Lemon 		OCP_SPI_RESOURCE(spi_flash),
600773bda96SJonathan Lemon 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
601773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_flash_info) {
602773bda96SJonathan Lemon 			.name = "xilinx_spi", .pci_offset = 0,
603773bda96SJonathan Lemon 			.data_size = sizeof(struct xspi_platform_data),
604773bda96SJonathan Lemon 			.data = &(struct xspi_platform_data) {
605773bda96SJonathan Lemon 				.num_chipselect = 1,
606773bda96SJonathan Lemon 				.bits_per_word = 8,
607773bda96SJonathan Lemon 				.num_devices = 1,
608773bda96SJonathan Lemon 				.devices = &(struct spi_board_info) {
609773bda96SJonathan Lemon 					.modalias = "spi-nor",
610773bda96SJonathan Lemon 				},
611773bda96SJonathan Lemon 			},
612773bda96SJonathan Lemon 		},
613773bda96SJonathan Lemon 	},
614773bda96SJonathan Lemon 	{
6152407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[0]),
6162407f5d6SJonathan Lemon 		.offset = 0x01200000, .size = 0x10000,
6172407f5d6SJonathan Lemon 	},
6182407f5d6SJonathan Lemon 	{
6192407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[1]),
6202407f5d6SJonathan Lemon 		.offset = 0x01210000, .size = 0x10000,
6212407f5d6SJonathan Lemon 	},
6222407f5d6SJonathan Lemon 	{
6232407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[2]),
6242407f5d6SJonathan Lemon 		.offset = 0x01220000, .size = 0x10000,
6252407f5d6SJonathan Lemon 	},
6262407f5d6SJonathan Lemon 	{
6272407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[3]),
6282407f5d6SJonathan Lemon 		.offset = 0x01230000, .size = 0x10000,
6292407f5d6SJonathan Lemon 	},
6302407f5d6SJonathan Lemon 	{
631773bda96SJonathan Lemon 		.setup = ptp_ocp_fb_board_init,
632773bda96SJonathan Lemon 	},
633773bda96SJonathan Lemon 	{ }
634773bda96SJonathan Lemon };
635773bda96SJonathan Lemon 
636773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = {
637773bda96SJonathan Lemon 	{ PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
63881fa652eSVadim Fedorenko 	{ PCI_DEVICE_DATA(CELESTICA, TIMECARD, &ocp_fb_resource) },
63981fa652eSVadim Fedorenko 	{ }
640773bda96SJonathan Lemon };
641773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
642773bda96SJonathan Lemon 
643773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock);
644773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr);
645773bda96SJonathan Lemon 
646e1daf0ecSJonathan Lemon struct ocp_selector {
647773bda96SJonathan Lemon 	const char *name;
648773bda96SJonathan Lemon 	int value;
649e1daf0ecSJonathan Lemon };
650e1daf0ecSJonathan Lemon 
651*3f3fe41cSJonathan Lemon static const struct ocp_selector ptp_ocp_clock[] = {
652773bda96SJonathan Lemon 	{ .name = "NONE",	.value = 0 },
653773bda96SJonathan Lemon 	{ .name = "TOD",	.value = 1 },
654773bda96SJonathan Lemon 	{ .name = "IRIG",	.value = 2 },
655773bda96SJonathan Lemon 	{ .name = "PPS",	.value = 3 },
656773bda96SJonathan Lemon 	{ .name = "PTP",	.value = 4 },
657773bda96SJonathan Lemon 	{ .name = "RTC",	.value = 5 },
658773bda96SJonathan Lemon 	{ .name = "DCF",	.value = 6 },
659773bda96SJonathan Lemon 	{ .name = "REGS",	.value = 0xfe },
660773bda96SJonathan Lemon 	{ .name = "EXT",	.value = 0xff },
661e1daf0ecSJonathan Lemon 	{ }
662e1daf0ecSJonathan Lemon };
663e1daf0ecSJonathan Lemon 
664a509a7c6SJonathan Lemon #define SMA_ENABLE		BIT(15)
665a509a7c6SJonathan Lemon #define SMA_SELECT_MASK		((1U << 15) - 1)
666b2c4f0acSJonathan Lemon #define SMA_DISABLE		0x10000
667a509a7c6SJonathan Lemon 
668*3f3fe41cSJonathan Lemon static const struct ocp_selector ptp_ocp_sma_in[] = {
6692407f5d6SJonathan Lemon 	{ .name = "10Mhz",	.value = 0x0000 },
6702407f5d6SJonathan Lemon 	{ .name = "PPS1",	.value = 0x0001 },
6712407f5d6SJonathan Lemon 	{ .name = "PPS2",	.value = 0x0002 },
6722407f5d6SJonathan Lemon 	{ .name = "TS1",	.value = 0x0004 },
6732407f5d6SJonathan Lemon 	{ .name = "TS2",	.value = 0x0008 },
6742407f5d6SJonathan Lemon 	{ .name = "IRIG",	.value = 0x0010 },
6752407f5d6SJonathan Lemon 	{ .name = "DCF",	.value = 0x0020 },
6760fa3ff7eSJonathan Lemon 	{ .name = "TS3",	.value = 0x0040 },
6770fa3ff7eSJonathan Lemon 	{ .name = "TS4",	.value = 0x0080 },
6782407f5d6SJonathan Lemon 	{ .name = "FREQ1",	.value = 0x0100 },
6792407f5d6SJonathan Lemon 	{ .name = "FREQ2",	.value = 0x0200 },
6802407f5d6SJonathan Lemon 	{ .name = "FREQ3",	.value = 0x0400 },
6812407f5d6SJonathan Lemon 	{ .name = "FREQ4",	.value = 0x0800 },
682b2c4f0acSJonathan Lemon 	{ .name = "None",	.value = SMA_DISABLE },
683e1daf0ecSJonathan Lemon 	{ }
684e1daf0ecSJonathan Lemon };
685e1daf0ecSJonathan Lemon 
686*3f3fe41cSJonathan Lemon static const struct ocp_selector ptp_ocp_sma_out[] = {
687b325af3cSJonathan Lemon 	{ .name = "10Mhz",	.value = 0x0000 },
688b325af3cSJonathan Lemon 	{ .name = "PHC",	.value = 0x0001 },
689b325af3cSJonathan Lemon 	{ .name = "MAC",	.value = 0x0002 },
690b325af3cSJonathan Lemon 	{ .name = "GNSS1",	.value = 0x0004 },
691b325af3cSJonathan Lemon 	{ .name = "GNSS2",	.value = 0x0008 },
692b325af3cSJonathan Lemon 	{ .name = "IRIG",	.value = 0x0010 },
693b325af3cSJonathan Lemon 	{ .name = "DCF",	.value = 0x0020 },
694b325af3cSJonathan Lemon 	{ .name = "GEN1",	.value = 0x0040 },
695b325af3cSJonathan Lemon 	{ .name = "GEN2",	.value = 0x0080 },
696b325af3cSJonathan Lemon 	{ .name = "GEN3",	.value = 0x0100 },
697b325af3cSJonathan Lemon 	{ .name = "GEN4",	.value = 0x0200 },
698cd09193fSJonathan Lemon 	{ .name = "GND",	.value = 0x2000 },
699cd09193fSJonathan Lemon 	{ .name = "VCC",	.value = 0x4000 },
700e1daf0ecSJonathan Lemon 	{ }
701773bda96SJonathan Lemon };
702773bda96SJonathan Lemon 
703*3f3fe41cSJonathan Lemon static const struct ocp_selector *ocp_sma_tbl[][2] = {
704aa56a7ffSJonathan Lemon 	{ ptp_ocp_sma_in, ptp_ocp_sma_out },
705aa56a7ffSJonathan Lemon };
706aa56a7ffSJonathan Lemon 
707773bda96SJonathan Lemon static const char *
708*3f3fe41cSJonathan Lemon ptp_ocp_select_name_from_val(const struct ocp_selector *tbl, int val)
709773bda96SJonathan Lemon {
710773bda96SJonathan Lemon 	int i;
711773bda96SJonathan Lemon 
712e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++)
713e1daf0ecSJonathan Lemon 		if (tbl[i].value == val)
714e1daf0ecSJonathan Lemon 			return tbl[i].name;
715773bda96SJonathan Lemon 	return NULL;
716773bda96SJonathan Lemon }
717773bda96SJonathan Lemon 
718773bda96SJonathan Lemon static int
719*3f3fe41cSJonathan Lemon ptp_ocp_select_val_from_name(const struct ocp_selector *tbl, const char *name)
720773bda96SJonathan Lemon {
721e1daf0ecSJonathan Lemon 	const char *select;
722773bda96SJonathan Lemon 	int i;
723773bda96SJonathan Lemon 
724e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++) {
725e1daf0ecSJonathan Lemon 		select = tbl[i].name;
726e1daf0ecSJonathan Lemon 		if (!strncasecmp(name, select, strlen(select)))
727e1daf0ecSJonathan Lemon 			return tbl[i].value;
728773bda96SJonathan Lemon 	}
729773bda96SJonathan Lemon 	return -EINVAL;
730773bda96SJonathan Lemon }
731773bda96SJonathan Lemon 
732e1daf0ecSJonathan Lemon static ssize_t
733*3f3fe41cSJonathan Lemon ptp_ocp_select_table_show(const struct ocp_selector *tbl, char *buf)
734e1daf0ecSJonathan Lemon {
735e1daf0ecSJonathan Lemon 	ssize_t count;
736e1daf0ecSJonathan Lemon 	int i;
737e1daf0ecSJonathan Lemon 
738e1daf0ecSJonathan Lemon 	count = 0;
739e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++)
740e1daf0ecSJonathan Lemon 		count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
741e1daf0ecSJonathan Lemon 	if (count)
742e1daf0ecSJonathan Lemon 		count--;
743e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "\n");
744e1daf0ecSJonathan Lemon 	return count;
745e1daf0ecSJonathan Lemon }
746e1daf0ecSJonathan Lemon 
747a7e1abadSJonathan Lemon static int
748a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
749a7e1abadSJonathan Lemon 			 struct ptp_system_timestamp *sts)
750a7e1abadSJonathan Lemon {
751a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
752a7e1abadSJonathan Lemon 	int i;
753a7e1abadSJonathan Lemon 
754a7e1abadSJonathan Lemon 	ptp_read_system_prets(sts);
7551acffc6eSJonathan Lemon 
7561acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
757a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
758a7e1abadSJonathan Lemon 
759a7e1abadSJonathan Lemon 	for (i = 0; i < 100; i++) {
760a7e1abadSJonathan Lemon 		ctrl = ioread32(&bp->reg->ctrl);
761a7e1abadSJonathan Lemon 		if (ctrl & OCP_CTRL_READ_TIME_DONE)
762a7e1abadSJonathan Lemon 			break;
763a7e1abadSJonathan Lemon 	}
764a7e1abadSJonathan Lemon 	ptp_read_system_postts(sts);
765a7e1abadSJonathan Lemon 
7661acffc6eSJonathan Lemon 	if (sts && bp->ts_window_adjust) {
7671acffc6eSJonathan Lemon 		s64 ns = timespec64_to_ns(&sts->post_ts);
7681acffc6eSJonathan Lemon 
7691acffc6eSJonathan Lemon 		sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
7701acffc6eSJonathan Lemon 	}
7711acffc6eSJonathan Lemon 
772a7e1abadSJonathan Lemon 	time_ns = ioread32(&bp->reg->time_ns);
773a7e1abadSJonathan Lemon 	time_sec = ioread32(&bp->reg->time_sec);
774a7e1abadSJonathan Lemon 
775a7e1abadSJonathan Lemon 	ts->tv_sec = time_sec;
776a7e1abadSJonathan Lemon 	ts->tv_nsec = time_ns;
777a7e1abadSJonathan Lemon 
778a7e1abadSJonathan Lemon 	return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
779a7e1abadSJonathan Lemon }
780a7e1abadSJonathan Lemon 
781a7e1abadSJonathan Lemon static int
782a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
783a7e1abadSJonathan Lemon 		 struct ptp_system_timestamp *sts)
784a7e1abadSJonathan Lemon {
785a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
786a7e1abadSJonathan Lemon 	unsigned long flags;
787a7e1abadSJonathan Lemon 	int err;
788a7e1abadSJonathan Lemon 
789a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
790a7e1abadSJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, ts, sts);
791a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
792a7e1abadSJonathan Lemon 
793a7e1abadSJonathan Lemon 	return err;
794a7e1abadSJonathan Lemon }
795a7e1abadSJonathan Lemon 
796a7e1abadSJonathan Lemon static void
797a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
798a7e1abadSJonathan Lemon {
799a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
800a7e1abadSJonathan Lemon 	u32 select;
801a7e1abadSJonathan Lemon 
802a7e1abadSJonathan Lemon 	time_ns = ts->tv_nsec;
803a7e1abadSJonathan Lemon 	time_sec = ts->tv_sec;
804a7e1abadSJonathan Lemon 
805a7e1abadSJonathan Lemon 	select = ioread32(&bp->reg->select);
806a7e1abadSJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
807a7e1abadSJonathan Lemon 
808a7e1abadSJonathan Lemon 	iowrite32(time_ns, &bp->reg->adjust_ns);
809a7e1abadSJonathan Lemon 	iowrite32(time_sec, &bp->reg->adjust_sec);
810a7e1abadSJonathan Lemon 
8111acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
812a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
813a7e1abadSJonathan Lemon 
814a7e1abadSJonathan Lemon 	/* restore clock selection */
815a7e1abadSJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
816a7e1abadSJonathan Lemon }
817a7e1abadSJonathan Lemon 
818a7e1abadSJonathan Lemon static int
819a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
820a7e1abadSJonathan Lemon {
821a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
822a7e1abadSJonathan Lemon 	unsigned long flags;
823a7e1abadSJonathan Lemon 
824a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
825a7e1abadSJonathan Lemon 	__ptp_ocp_settime_locked(bp, ts);
826a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
827a7e1abadSJonathan Lemon 
828a7e1abadSJonathan Lemon 	return 0;
829a7e1abadSJonathan Lemon }
830a7e1abadSJonathan Lemon 
8316d59d4faSJonathan Lemon static void
83290f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
8336d59d4faSJonathan Lemon {
8346d59d4faSJonathan Lemon 	u32 select, ctrl;
8356d59d4faSJonathan Lemon 
8366d59d4faSJonathan Lemon 	select = ioread32(&bp->reg->select);
8376d59d4faSJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
8386d59d4faSJonathan Lemon 
8396d59d4faSJonathan Lemon 	iowrite32(adj_val, &bp->reg->offset_ns);
84090f8f4c0SJonathan Lemon 	iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
8416d59d4faSJonathan Lemon 
8426d59d4faSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
8436d59d4faSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
8446d59d4faSJonathan Lemon 
8456d59d4faSJonathan Lemon 	/* restore clock selection */
8466d59d4faSJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
8476d59d4faSJonathan Lemon }
8486d59d4faSJonathan Lemon 
84990f8f4c0SJonathan Lemon static void
850da2172a9SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns)
85190f8f4c0SJonathan Lemon {
85290f8f4c0SJonathan Lemon 	struct timespec64 ts;
85390f8f4c0SJonathan Lemon 	unsigned long flags;
85490f8f4c0SJonathan Lemon 	int err;
85590f8f4c0SJonathan Lemon 
85690f8f4c0SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
85790f8f4c0SJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
85890f8f4c0SJonathan Lemon 	if (likely(!err)) {
859da2172a9SJonathan Lemon 		set_normalized_timespec64(&ts, ts.tv_sec,
860da2172a9SJonathan Lemon 					  ts.tv_nsec + delta_ns);
86190f8f4c0SJonathan Lemon 		__ptp_ocp_settime_locked(bp, &ts);
86290f8f4c0SJonathan Lemon 	}
86390f8f4c0SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
86490f8f4c0SJonathan Lemon }
86590f8f4c0SJonathan Lemon 
866a7e1abadSJonathan Lemon static int
867a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
868a7e1abadSJonathan Lemon {
869a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
870a7e1abadSJonathan Lemon 	unsigned long flags;
8716d59d4faSJonathan Lemon 	u32 adj_ns, sign;
872a7e1abadSJonathan Lemon 
87390f8f4c0SJonathan Lemon 	if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
87490f8f4c0SJonathan Lemon 		ptp_ocp_adjtime_coarse(bp, delta_ns);
87590f8f4c0SJonathan Lemon 		return 0;
87690f8f4c0SJonathan Lemon 	}
87790f8f4c0SJonathan Lemon 
8786d59d4faSJonathan Lemon 	sign = delta_ns < 0 ? BIT(31) : 0;
8796d59d4faSJonathan Lemon 	adj_ns = sign ? -delta_ns : delta_ns;
880a7e1abadSJonathan Lemon 
881a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
8826d59d4faSJonathan Lemon 	__ptp_ocp_adjtime_locked(bp, sign | adj_ns);
883a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
884a7e1abadSJonathan Lemon 
8856d59d4faSJonathan Lemon 	return 0;
886a7e1abadSJonathan Lemon }
887a7e1abadSJonathan Lemon 
888a7e1abadSJonathan Lemon static int
889a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
890a7e1abadSJonathan Lemon {
891a7e1abadSJonathan Lemon 	if (scaled_ppm == 0)
892a7e1abadSJonathan Lemon 		return 0;
893a7e1abadSJonathan Lemon 
894a7e1abadSJonathan Lemon 	return -EOPNOTSUPP;
895a7e1abadSJonathan Lemon }
896a7e1abadSJonathan Lemon 
897773bda96SJonathan Lemon static int
8986d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
899773bda96SJonathan Lemon {
900773bda96SJonathan Lemon 	return -EOPNOTSUPP;
901773bda96SJonathan Lemon }
902773bda96SJonathan Lemon 
903773bda96SJonathan Lemon static int
904773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
905773bda96SJonathan Lemon 	       int on)
906773bda96SJonathan Lemon {
907773bda96SJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
908773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = NULL;
909a62a56d0SJonathan Lemon 	u32 req;
910773bda96SJonathan Lemon 	int err;
911773bda96SJonathan Lemon 
912773bda96SJonathan Lemon 	switch (rq->type) {
913773bda96SJonathan Lemon 	case PTP_CLK_REQ_EXTTS:
914a62a56d0SJonathan Lemon 		req = OCP_REQ_TIMESTAMP;
915773bda96SJonathan Lemon 		switch (rq->extts.index) {
916773bda96SJonathan Lemon 		case 0:
917773bda96SJonathan Lemon 			ext = bp->ts0;
918773bda96SJonathan Lemon 			break;
919773bda96SJonathan Lemon 		case 1:
920773bda96SJonathan Lemon 			ext = bp->ts1;
921773bda96SJonathan Lemon 			break;
922dcf61469SJonathan Lemon 		case 2:
923dcf61469SJonathan Lemon 			ext = bp->ts2;
924dcf61469SJonathan Lemon 			break;
925a62a56d0SJonathan Lemon 		case 3:
9260fa3ff7eSJonathan Lemon 			ext = bp->ts3;
9270fa3ff7eSJonathan Lemon 			break;
9280fa3ff7eSJonathan Lemon 		case 4:
9290fa3ff7eSJonathan Lemon 			ext = bp->ts4;
9300fa3ff7eSJonathan Lemon 			break;
9310fa3ff7eSJonathan Lemon 		case 5:
932a62a56d0SJonathan Lemon 			ext = bp->pps;
933a62a56d0SJonathan Lemon 			break;
934773bda96SJonathan Lemon 		}
935773bda96SJonathan Lemon 		break;
936773bda96SJonathan Lemon 	case PTP_CLK_REQ_PPS:
937a62a56d0SJonathan Lemon 		req = OCP_REQ_PPS;
938773bda96SJonathan Lemon 		ext = bp->pps;
939773bda96SJonathan Lemon 		break;
940a62a56d0SJonathan Lemon 	case PTP_CLK_REQ_PEROUT:
9411aa66a3aSJonathan Lemon 		switch (rq->perout.index) {
9421aa66a3aSJonathan Lemon 		case 0:
943a62a56d0SJonathan Lemon 			/* This is a request for 1PPS on an output SMA.
944a62a56d0SJonathan Lemon 			 * Allow, but assume manual configuration.
945a62a56d0SJonathan Lemon 			 */
9461aa66a3aSJonathan Lemon 			if (on && (rq->perout.period.sec != 1 ||
9471aa66a3aSJonathan Lemon 				   rq->perout.period.nsec != 0))
9481aa66a3aSJonathan Lemon 				return -EINVAL;
949a62a56d0SJonathan Lemon 			return 0;
9501aa66a3aSJonathan Lemon 		case 1:
9511aa66a3aSJonathan Lemon 		case 2:
9521aa66a3aSJonathan Lemon 		case 3:
9531aa66a3aSJonathan Lemon 		case 4:
9541aa66a3aSJonathan Lemon 			req = rq->perout.index - 1;
9551aa66a3aSJonathan Lemon 			ext = bp->signal_out[req];
9561aa66a3aSJonathan Lemon 			err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
9571aa66a3aSJonathan Lemon 			if (err)
9581aa66a3aSJonathan Lemon 				return err;
9591aa66a3aSJonathan Lemon 			break;
9601aa66a3aSJonathan Lemon 		}
9611aa66a3aSJonathan Lemon 		break;
962773bda96SJonathan Lemon 	default:
963773bda96SJonathan Lemon 		return -EOPNOTSUPP;
964773bda96SJonathan Lemon 	}
965773bda96SJonathan Lemon 
966773bda96SJonathan Lemon 	err = -ENXIO;
967773bda96SJonathan Lemon 	if (ext)
968a62a56d0SJonathan Lemon 		err = ext->info->enable(ext, req, on);
969773bda96SJonathan Lemon 
970773bda96SJonathan Lemon 	return err;
971773bda96SJonathan Lemon }
972773bda96SJonathan Lemon 
9731aa66a3aSJonathan Lemon static int
9741aa66a3aSJonathan Lemon ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
9751aa66a3aSJonathan Lemon 	       enum ptp_pin_function func, unsigned chan)
9761aa66a3aSJonathan Lemon {
9771aa66a3aSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
9781aa66a3aSJonathan Lemon 	char buf[16];
9791aa66a3aSJonathan Lemon 
98005fc65f3SJonathan Lemon 	switch (func) {
98105fc65f3SJonathan Lemon 	case PTP_PF_NONE:
982d5f497b8SDan Carpenter 		snprintf(buf, sizeof(buf), "IN: None");
98305fc65f3SJonathan Lemon 		break;
98405fc65f3SJonathan Lemon 	case PTP_PF_EXTTS:
98505fc65f3SJonathan Lemon 		/* Allow timestamps, but require sysfs configuration. */
98605fc65f3SJonathan Lemon 		return 0;
98705fc65f3SJonathan Lemon 	case PTP_PF_PEROUT:
98805fc65f3SJonathan Lemon 		/* channel 0 is 1PPS from PHC.
98905fc65f3SJonathan Lemon 		 * channels 1..4 are the frequency generators.
99005fc65f3SJonathan Lemon 		 */
9911aa66a3aSJonathan Lemon 		if (chan)
992d5f497b8SDan Carpenter 			snprintf(buf, sizeof(buf), "OUT: GEN%d", chan);
9931aa66a3aSJonathan Lemon 		else
994d5f497b8SDan Carpenter 			snprintf(buf, sizeof(buf), "OUT: PHC");
99505fc65f3SJonathan Lemon 		break;
99605fc65f3SJonathan Lemon 	default:
99705fc65f3SJonathan Lemon 		return -EOPNOTSUPP;
99805fc65f3SJonathan Lemon 	}
9991aa66a3aSJonathan Lemon 
10001aa66a3aSJonathan Lemon 	return ptp_ocp_sma_store(bp, buf, pin + 1);
10011aa66a3aSJonathan Lemon }
10021aa66a3aSJonathan Lemon 
1003a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = {
1004a7e1abadSJonathan Lemon 	.owner		= THIS_MODULE,
1005a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
1006a7e1abadSJonathan Lemon 	.max_adj	= 100000000,
1007a7e1abadSJonathan Lemon 	.gettimex64	= ptp_ocp_gettimex,
1008a7e1abadSJonathan Lemon 	.settime64	= ptp_ocp_settime,
1009a7e1abadSJonathan Lemon 	.adjtime	= ptp_ocp_adjtime,
1010a7e1abadSJonathan Lemon 	.adjfine	= ptp_ocp_null_adjfine,
10116d59d4faSJonathan Lemon 	.adjphase	= ptp_ocp_null_adjphase,
1012773bda96SJonathan Lemon 	.enable		= ptp_ocp_enable,
10131aa66a3aSJonathan Lemon 	.verify		= ptp_ocp_verify,
1014773bda96SJonathan Lemon 	.pps		= true,
10150fa3ff7eSJonathan Lemon 	.n_ext_ts	= 6,
10161aa66a3aSJonathan Lemon 	.n_per_out	= 5,
1017a7e1abadSJonathan Lemon };
1018a7e1abadSJonathan Lemon 
1019773bda96SJonathan Lemon static void
1020773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
1021773bda96SJonathan Lemon {
1022773bda96SJonathan Lemon 	u32 ctrl, select;
1023773bda96SJonathan Lemon 
1024773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
1025773bda96SJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1026773bda96SJonathan Lemon 
1027773bda96SJonathan Lemon 	iowrite32(0, &bp->reg->drift_ns);
1028773bda96SJonathan Lemon 
10291acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
1030773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
1031773bda96SJonathan Lemon 
1032773bda96SJonathan Lemon 	/* restore clock selection */
1033773bda96SJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
1034773bda96SJonathan Lemon }
1035773bda96SJonathan Lemon 
1036773bda96SJonathan Lemon static void
1037e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
1038e68462a0SVadim Fedorenko {
1039e68462a0SVadim Fedorenko 	unsigned long flags;
1040e68462a0SVadim Fedorenko 
1041e68462a0SVadim Fedorenko 	spin_lock_irqsave(&bp->lock, flags);
1042e68462a0SVadim Fedorenko 
1043e68462a0SVadim Fedorenko 	bp->utc_tai_offset = val;
1044e68462a0SVadim Fedorenko 
1045e68462a0SVadim Fedorenko 	if (bp->irig_out)
1046e68462a0SVadim Fedorenko 		iowrite32(val, &bp->irig_out->adj_sec);
1047e68462a0SVadim Fedorenko 	if (bp->dcf_out)
1048e68462a0SVadim Fedorenko 		iowrite32(val, &bp->dcf_out->adj_sec);
1049e68462a0SVadim Fedorenko 	if (bp->nmea_out)
1050e68462a0SVadim Fedorenko 		iowrite32(val, &bp->nmea_out->adj_sec);
1051e68462a0SVadim Fedorenko 
1052e68462a0SVadim Fedorenko 	spin_unlock_irqrestore(&bp->lock, flags);
1053e68462a0SVadim Fedorenko }
1054e68462a0SVadim Fedorenko 
1055e68462a0SVadim Fedorenko static void
1056773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t)
1057773bda96SJonathan Lemon {
1058773bda96SJonathan Lemon 	struct ptp_ocp *bp = from_timer(bp, t, watchdog);
1059773bda96SJonathan Lemon 	unsigned long flags;
1060e68462a0SVadim Fedorenko 	u32 status, utc_offset;
1061773bda96SJonathan Lemon 
10620d43d4f2SJonathan Lemon 	status = ioread32(&bp->pps_to_clk->status);
1063773bda96SJonathan Lemon 
1064773bda96SJonathan Lemon 	if (status & PPS_STATUS_SUPERV_ERR) {
10650d43d4f2SJonathan Lemon 		iowrite32(status, &bp->pps_to_clk->status);
1066ef0cfb34SJonathan Lemon 		if (!bp->gnss_lost) {
1067773bda96SJonathan Lemon 			spin_lock_irqsave(&bp->lock, flags);
1068773bda96SJonathan Lemon 			__ptp_ocp_clear_drift_locked(bp);
1069773bda96SJonathan Lemon 			spin_unlock_irqrestore(&bp->lock, flags);
1070ef0cfb34SJonathan Lemon 			bp->gnss_lost = ktime_get_real_seconds();
1071773bda96SJonathan Lemon 		}
1072773bda96SJonathan Lemon 
1073ef0cfb34SJonathan Lemon 	} else if (bp->gnss_lost) {
1074ef0cfb34SJonathan Lemon 		bp->gnss_lost = 0;
1075773bda96SJonathan Lemon 	}
1076773bda96SJonathan Lemon 
1077e68462a0SVadim Fedorenko 	/* if GNSS provides correct data we can rely on
1078e68462a0SVadim Fedorenko 	 * it to get leap second information
1079e68462a0SVadim Fedorenko 	 */
1080e68462a0SVadim Fedorenko 	if (bp->tod) {
1081e68462a0SVadim Fedorenko 		status = ioread32(&bp->tod->utc_status);
1082e68462a0SVadim Fedorenko 		utc_offset = status & TOD_STATUS_UTC_MASK;
1083e68462a0SVadim Fedorenko 		if (status & TOD_STATUS_UTC_VALID &&
1084e68462a0SVadim Fedorenko 		    utc_offset != bp->utc_tai_offset)
1085e68462a0SVadim Fedorenko 			ptp_ocp_utc_distribute(bp, utc_offset);
1086e68462a0SVadim Fedorenko 	}
1087e68462a0SVadim Fedorenko 
1088773bda96SJonathan Lemon 	mod_timer(&bp->watchdog, jiffies + HZ);
1089773bda96SJonathan Lemon }
1090773bda96SJonathan Lemon 
10911acffc6eSJonathan Lemon static void
10921acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
10931acffc6eSJonathan Lemon {
10941acffc6eSJonathan Lemon 	ktime_t start, end;
10951acffc6eSJonathan Lemon 	ktime_t delay;
10961acffc6eSJonathan Lemon 	u32 ctrl;
10971acffc6eSJonathan Lemon 
10981acffc6eSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
10991acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
11001acffc6eSJonathan Lemon 
11011acffc6eSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
11021acffc6eSJonathan Lemon 
11031acffc6eSJonathan Lemon 	start = ktime_get_ns();
11041acffc6eSJonathan Lemon 
11051acffc6eSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
11061acffc6eSJonathan Lemon 
11071acffc6eSJonathan Lemon 	end = ktime_get_ns();
11081acffc6eSJonathan Lemon 
11091acffc6eSJonathan Lemon 	delay = end - start;
11101acffc6eSJonathan Lemon 	bp->ts_window_adjust = (delay >> 5) * 3;
11111acffc6eSJonathan Lemon }
11121acffc6eSJonathan Lemon 
1113a7e1abadSJonathan Lemon static int
1114773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp)
1115a7e1abadSJonathan Lemon {
1116a7e1abadSJonathan Lemon 	struct timespec64 ts;
1117a7e1abadSJonathan Lemon 	bool sync;
1118a7e1abadSJonathan Lemon 	u32 ctrl;
1119a7e1abadSJonathan Lemon 
11201acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ENABLE;
1121a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
1122a7e1abadSJonathan Lemon 
1123773bda96SJonathan Lemon 	/* NO DRIFT Correction */
1124773bda96SJonathan Lemon 	/* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
1125773bda96SJonathan Lemon 	iowrite32(0x2000, &bp->reg->servo_offset_p);
1126773bda96SJonathan Lemon 	iowrite32(0x1000, &bp->reg->servo_offset_i);
1127773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_p);
1128773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_i);
1129773bda96SJonathan Lemon 
1130773bda96SJonathan Lemon 	/* latch servo values */
1131773bda96SJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_SERVO;
1132773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
1133773bda96SJonathan Lemon 
1134a7e1abadSJonathan Lemon 	if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
1135a7e1abadSJonathan Lemon 		dev_err(&bp->pdev->dev, "clock not enabled\n");
1136a7e1abadSJonathan Lemon 		return -ENODEV;
1137a7e1abadSJonathan Lemon 	}
1138a7e1abadSJonathan Lemon 
11391acffc6eSJonathan Lemon 	ptp_ocp_estimate_pci_timing(bp);
11401acffc6eSJonathan Lemon 
1141a7e1abadSJonathan Lemon 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
1142a7e1abadSJonathan Lemon 	if (!sync) {
1143065efcc5SJonathan Lemon 		ktime_get_clocktai_ts64(&ts);
1144a7e1abadSJonathan Lemon 		ptp_ocp_settime(&bp->ptp_info, &ts);
1145a7e1abadSJonathan Lemon 	}
1146a7e1abadSJonathan Lemon 
1147065efcc5SJonathan Lemon 	/* If there is a clock supervisor, then enable the watchdog */
1148065efcc5SJonathan Lemon 	if (bp->pps_to_clk) {
1149773bda96SJonathan Lemon 		timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
1150773bda96SJonathan Lemon 		mod_timer(&bp->watchdog, jiffies + HZ);
1151065efcc5SJonathan Lemon 	}
1152773bda96SJonathan Lemon 
1153a7e1abadSJonathan Lemon 	return 0;
1154a7e1abadSJonathan Lemon }
1155a7e1abadSJonathan Lemon 
1156a7e1abadSJonathan Lemon static void
1157065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp)
1158065efcc5SJonathan Lemon {
1159065efcc5SJonathan Lemon 	u32 ctrl, reg;
1160065efcc5SJonathan Lemon 
1161065efcc5SJonathan Lemon 	ctrl = ioread32(&bp->tod->ctrl);
1162065efcc5SJonathan Lemon 	ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
1163065efcc5SJonathan Lemon 	ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
1164065efcc5SJonathan Lemon 	iowrite32(ctrl, &bp->tod->ctrl);
1165065efcc5SJonathan Lemon 
1166065efcc5SJonathan Lemon 	reg = ioread32(&bp->tod->utc_status);
1167065efcc5SJonathan Lemon 	if (reg & TOD_STATUS_UTC_VALID)
1168065efcc5SJonathan Lemon 		ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
1169065efcc5SJonathan Lemon }
1170065efcc5SJonathan Lemon 
11719f492c4cSVadim Fedorenko static const char *
11729f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx)
1173a7e1abadSJonathan Lemon {
1174a7e1abadSJonathan Lemon 	static const char * const proto_name[] = {
1175a7e1abadSJonathan Lemon 		"NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
1176a7e1abadSJonathan Lemon 		"UBX", "UBX_UTC", "UBX_LS", "UBX_none"
1177a7e1abadSJonathan Lemon 	};
11789f492c4cSVadim Fedorenko 	return proto_name[idx];
11799f492c4cSVadim Fedorenko }
11809f492c4cSVadim Fedorenko 
11819f492c4cSVadim Fedorenko static const char *
11829f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx)
11839f492c4cSVadim Fedorenko {
1184a7e1abadSJonathan Lemon 	static const char * const gnss_name[] = {
1185a7e1abadSJonathan Lemon 		"ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
11869f492c4cSVadim Fedorenko 		"Unknown"
1187a7e1abadSJonathan Lemon 	};
118872f00505SDan Carpenter 	if (idx >= ARRAY_SIZE(gnss_name))
11899f492c4cSVadim Fedorenko 		idx = ARRAY_SIZE(gnss_name) - 1;
11909f492c4cSVadim Fedorenko 	return gnss_name[idx];
1191a7e1abadSJonathan Lemon }
1192a7e1abadSJonathan Lemon 
11930cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info {
11940cfcdd1eSJonathan Lemon 	struct ptp_ocp *bp;
11950cfcdd1eSJonathan Lemon 	const void * const tag;
1196773bda96SJonathan Lemon };
1197773bda96SJonathan Lemon 
11980cfcdd1eSJonathan Lemon static int
11990cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data)
12000cfcdd1eSJonathan Lemon {
12010cfcdd1eSJonathan Lemon 	const struct ptp_ocp_nvmem_match_info *info = data;
12020cfcdd1eSJonathan Lemon 
12030cfcdd1eSJonathan Lemon 	dev = dev->parent;
12040cfcdd1eSJonathan Lemon 	if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
12050cfcdd1eSJonathan Lemon 		return 0;
12060cfcdd1eSJonathan Lemon 
12070cfcdd1eSJonathan Lemon 	while ((dev = dev->parent))
12080cfcdd1eSJonathan Lemon 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
12090cfcdd1eSJonathan Lemon 			return info->bp == dev_get_drvdata(dev);
1210773bda96SJonathan Lemon 	return 0;
1211773bda96SJonathan Lemon }
1212773bda96SJonathan Lemon 
12130cfcdd1eSJonathan Lemon static inline struct nvmem_device *
12140cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
1215773bda96SJonathan Lemon {
12160cfcdd1eSJonathan Lemon 	struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
12170cfcdd1eSJonathan Lemon 
12180cfcdd1eSJonathan Lemon 	return nvmem_device_find(&info, ptp_ocp_nvmem_match);
12190cfcdd1eSJonathan Lemon }
12200cfcdd1eSJonathan Lemon 
12210cfcdd1eSJonathan Lemon static inline void
12220cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
12230cfcdd1eSJonathan Lemon {
12248f0588e8SJonathan Lemon 	if (!IS_ERR_OR_NULL(*nvmemp))
12250cfcdd1eSJonathan Lemon 		nvmem_device_put(*nvmemp);
12260cfcdd1eSJonathan Lemon 	*nvmemp = NULL;
12270cfcdd1eSJonathan Lemon }
12280cfcdd1eSJonathan Lemon 
12290cfcdd1eSJonathan Lemon static void
12300cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp)
12310cfcdd1eSJonathan Lemon {
12320cfcdd1eSJonathan Lemon 	const struct ptp_ocp_eeprom_map *map;
12330cfcdd1eSJonathan Lemon 	struct nvmem_device *nvmem;
12340cfcdd1eSJonathan Lemon 	const void *tag;
12350cfcdd1eSJonathan Lemon 	int ret;
1236773bda96SJonathan Lemon 
12371447149dSJonathan Lemon 	if (!bp->i2c_ctrl)
12381447149dSJonathan Lemon 		return;
12391447149dSJonathan Lemon 
12400cfcdd1eSJonathan Lemon 	tag = NULL;
12410cfcdd1eSJonathan Lemon 	nvmem = NULL;
1242773bda96SJonathan Lemon 
12430cfcdd1eSJonathan Lemon 	for (map = bp->eeprom_map; map->len; map++) {
12440cfcdd1eSJonathan Lemon 		if (map->tag != tag) {
12450cfcdd1eSJonathan Lemon 			tag = map->tag;
12460cfcdd1eSJonathan Lemon 			ptp_ocp_nvmem_device_put(&nvmem);
12470cfcdd1eSJonathan Lemon 		}
12480cfcdd1eSJonathan Lemon 		if (!nvmem) {
12490cfcdd1eSJonathan Lemon 			nvmem = ptp_ocp_nvmem_device_get(bp, tag);
12508f0588e8SJonathan Lemon 			if (IS_ERR(nvmem)) {
12518f0588e8SJonathan Lemon 				ret = PTR_ERR(nvmem);
12528f0588e8SJonathan Lemon 				goto fail;
12538f0588e8SJonathan Lemon 			}
1254773bda96SJonathan Lemon 		}
12550cfcdd1eSJonathan Lemon 		ret = nvmem_device_read(nvmem, map->off, map->len,
12560cfcdd1eSJonathan Lemon 					BP_MAP_ENTRY_ADDR(bp, map));
12570cfcdd1eSJonathan Lemon 		if (ret != map->len)
12588f0588e8SJonathan Lemon 			goto fail;
1259773bda96SJonathan Lemon 	}
1260773bda96SJonathan Lemon 
12610cfcdd1eSJonathan Lemon 	bp->has_eeprom_data = true;
1262773bda96SJonathan Lemon 
1263773bda96SJonathan Lemon out:
12640cfcdd1eSJonathan Lemon 	ptp_ocp_nvmem_device_put(&nvmem);
12650cfcdd1eSJonathan Lemon 	return;
12660cfcdd1eSJonathan Lemon 
12678f0588e8SJonathan Lemon fail:
12680cfcdd1eSJonathan Lemon 	dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
12690cfcdd1eSJonathan Lemon 	goto out;
12700cfcdd1eSJonathan Lemon }
12710cfcdd1eSJonathan Lemon 
12720cfcdd1eSJonathan Lemon static int
12730cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data)
12740cfcdd1eSJonathan Lemon {
12750cfcdd1eSJonathan Lemon 	return 1;
1276773bda96SJonathan Lemon }
1277773bda96SJonathan Lemon 
1278773bda96SJonathan Lemon static struct device *
1279773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp)
1280773bda96SJonathan Lemon {
1281773bda96SJonathan Lemon 	struct device *dev, *last;
1282773bda96SJonathan Lemon 
1283773bda96SJonathan Lemon 	last = NULL;
1284773bda96SJonathan Lemon 	dev = &bp->spi_flash->dev;
1285773bda96SJonathan Lemon 
1286773bda96SJonathan Lemon 	while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) {
1287773bda96SJonathan Lemon 		if (!strcmp("mtd", dev_bus_name(dev)))
1288773bda96SJonathan Lemon 			break;
1289773bda96SJonathan Lemon 		put_device(last);
1290773bda96SJonathan Lemon 		last = dev;
1291773bda96SJonathan Lemon 	}
1292773bda96SJonathan Lemon 	put_device(last);
1293773bda96SJonathan Lemon 
1294773bda96SJonathan Lemon 	return dev;
1295773bda96SJonathan Lemon }
1296773bda96SJonathan Lemon 
1297773bda96SJonathan Lemon static int
1298773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1299773bda96SJonathan Lemon 		      const struct firmware *fw)
1300773bda96SJonathan Lemon {
1301773bda96SJonathan Lemon 	struct mtd_info *mtd = dev_get_drvdata(dev);
1302773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
1303773bda96SJonathan Lemon 	size_t off, len, resid, wrote;
1304773bda96SJonathan Lemon 	struct erase_info erase;
1305773bda96SJonathan Lemon 	size_t base, blksz;
13067c807572SJonathan Lemon 	int err = 0;
1307773bda96SJonathan Lemon 
1308773bda96SJonathan Lemon 	off = 0;
1309773bda96SJonathan Lemon 	base = bp->flash_start;
1310773bda96SJonathan Lemon 	blksz = 4096;
1311773bda96SJonathan Lemon 	resid = fw->size;
1312773bda96SJonathan Lemon 
1313773bda96SJonathan Lemon 	while (resid) {
1314773bda96SJonathan Lemon 		devlink_flash_update_status_notify(devlink, "Flashing",
1315773bda96SJonathan Lemon 						   NULL, off, fw->size);
1316773bda96SJonathan Lemon 
1317773bda96SJonathan Lemon 		len = min_t(size_t, resid, blksz);
1318773bda96SJonathan Lemon 		erase.addr = base + off;
1319773bda96SJonathan Lemon 		erase.len = blksz;
1320773bda96SJonathan Lemon 
1321773bda96SJonathan Lemon 		err = mtd_erase(mtd, &erase);
1322773bda96SJonathan Lemon 		if (err)
1323773bda96SJonathan Lemon 			goto out;
1324773bda96SJonathan Lemon 
1325773bda96SJonathan Lemon 		err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]);
1326773bda96SJonathan Lemon 		if (err)
1327773bda96SJonathan Lemon 			goto out;
1328773bda96SJonathan Lemon 
1329773bda96SJonathan Lemon 		off += blksz;
1330773bda96SJonathan Lemon 		resid -= len;
1331773bda96SJonathan Lemon 	}
1332773bda96SJonathan Lemon out:
1333773bda96SJonathan Lemon 	return err;
1334773bda96SJonathan Lemon }
1335773bda96SJonathan Lemon 
1336773bda96SJonathan Lemon static int
1337773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink,
1338773bda96SJonathan Lemon 			     struct devlink_flash_update_params *params,
1339773bda96SJonathan Lemon 			     struct netlink_ext_ack *extack)
1340773bda96SJonathan Lemon {
1341773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
1342773bda96SJonathan Lemon 	struct device *dev;
1343773bda96SJonathan Lemon 	const char *msg;
1344773bda96SJonathan Lemon 	int err;
1345773bda96SJonathan Lemon 
1346773bda96SJonathan Lemon 	dev = ptp_ocp_find_flash(bp);
1347773bda96SJonathan Lemon 	if (!dev) {
1348773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1349773bda96SJonathan Lemon 		return -ENODEV;
1350773bda96SJonathan Lemon 	}
1351773bda96SJonathan Lemon 
1352773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, "Preparing to flash",
1353773bda96SJonathan Lemon 					   NULL, 0, 0);
1354773bda96SJonathan Lemon 
1355773bda96SJonathan Lemon 	err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1356773bda96SJonathan Lemon 
1357773bda96SJonathan Lemon 	msg = err ? "Flash error" : "Flash complete";
1358773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1359773bda96SJonathan Lemon 
1360773bda96SJonathan Lemon 	put_device(dev);
1361773bda96SJonathan Lemon 	return err;
1362773bda96SJonathan Lemon }
1363773bda96SJonathan Lemon 
1364773bda96SJonathan Lemon static int
1365773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1366773bda96SJonathan Lemon 			 struct netlink_ext_ack *extack)
1367773bda96SJonathan Lemon {
1368773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
13695a728ac5SJonathan Lemon 	const char *fw_image;
1370773bda96SJonathan Lemon 	char buf[32];
1371773bda96SJonathan Lemon 	int err;
1372773bda96SJonathan Lemon 
1373773bda96SJonathan Lemon 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
1374773bda96SJonathan Lemon 	if (err)
1375773bda96SJonathan Lemon 		return err;
1376773bda96SJonathan Lemon 
13775a728ac5SJonathan Lemon 	fw_image = bp->fw_loader ? "loader" : "fw";
13785a728ac5SJonathan Lemon 	sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version);
13795a728ac5SJonathan Lemon 	err = devlink_info_version_running_put(req, fw_image, buf);
1380773bda96SJonathan Lemon 	if (err)
1381773bda96SJonathan Lemon 		return err;
1382773bda96SJonathan Lemon 
13830cfcdd1eSJonathan Lemon 	if (!bp->has_eeprom_data) {
13840cfcdd1eSJonathan Lemon 		ptp_ocp_read_eeprom(bp);
13850cfcdd1eSJonathan Lemon 		if (!bp->has_eeprom_data)
13860cfcdd1eSJonathan Lemon 			return 0;
13870cfcdd1eSJonathan Lemon 	}
1388773bda96SJonathan Lemon 
1389773bda96SJonathan Lemon 	sprintf(buf, "%pM", bp->serial);
1390773bda96SJonathan Lemon 	err = devlink_info_serial_number_put(req, buf);
1391773bda96SJonathan Lemon 	if (err)
1392773bda96SJonathan Lemon 		return err;
13930cfcdd1eSJonathan Lemon 
13940cfcdd1eSJonathan Lemon 	err = devlink_info_version_fixed_put(req,
13950cfcdd1eSJonathan Lemon 			DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
13960cfcdd1eSJonathan Lemon 			bp->board_id);
13970cfcdd1eSJonathan Lemon 	if (err)
13980cfcdd1eSJonathan Lemon 		return err;
1399773bda96SJonathan Lemon 
1400773bda96SJonathan Lemon 	return 0;
1401773bda96SJonathan Lemon }
1402773bda96SJonathan Lemon 
1403773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = {
1404773bda96SJonathan Lemon 	.flash_update = ptp_ocp_devlink_flash_update,
1405773bda96SJonathan Lemon 	.info_get = ptp_ocp_devlink_info_get,
1406773bda96SJonathan Lemon };
1407773bda96SJonathan Lemon 
1408773bda96SJonathan Lemon static void __iomem *
14098119c9eeSJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size)
1410773bda96SJonathan Lemon {
1411773bda96SJonathan Lemon 	struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1412773bda96SJonathan Lemon 
1413773bda96SJonathan Lemon 	return devm_ioremap_resource(&bp->pdev->dev, &res);
1414773bda96SJonathan Lemon }
1415773bda96SJonathan Lemon 
1416773bda96SJonathan Lemon static void __iomem *
1417773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1418773bda96SJonathan Lemon {
14198119c9eeSJonathan Lemon 	resource_size_t start;
1420773bda96SJonathan Lemon 
1421773bda96SJonathan Lemon 	start = pci_resource_start(bp->pdev, 0) + r->offset;
1422773bda96SJonathan Lemon 	return __ptp_ocp_get_mem(bp, start, r->size);
1423773bda96SJonathan Lemon }
1424773bda96SJonathan Lemon 
1425773bda96SJonathan Lemon static void
1426773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq)
1427773bda96SJonathan Lemon {
1428773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_IRQ(irq);
1429773bda96SJonathan Lemon 	*res = r;
1430773bda96SJonathan Lemon }
1431773bda96SJonathan Lemon 
1432773bda96SJonathan Lemon static void
14338119c9eeSJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, resource_size_t start, int size)
1434773bda96SJonathan Lemon {
1435773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_MEM(start, size);
1436773bda96SJonathan Lemon 	*res = r;
1437773bda96SJonathan Lemon }
1438773bda96SJonathan Lemon 
1439773bda96SJonathan Lemon static int
1440773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1441773bda96SJonathan Lemon {
1442773bda96SJonathan Lemon 	struct ptp_ocp_flash_info *info;
1443773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1444773bda96SJonathan Lemon 	struct platform_device *p;
1445773bda96SJonathan Lemon 	struct resource res[2];
14468119c9eeSJonathan Lemon 	resource_size_t start;
1447773bda96SJonathan Lemon 	int id;
1448773bda96SJonathan Lemon 
1449773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
1450773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
1451773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1452773bda96SJonathan Lemon 
1453773bda96SJonathan Lemon 	info = r->extra;
1454773bda96SJonathan Lemon 	id = pci_dev_id(pdev) << 1;
1455773bda96SJonathan Lemon 	id += info->pci_offset;
1456773bda96SJonathan Lemon 
1457773bda96SJonathan Lemon 	p = platform_device_register_resndata(&pdev->dev, info->name, id,
1458773bda96SJonathan Lemon 					      res, 2, info->data,
1459773bda96SJonathan Lemon 					      info->data_size);
1460773bda96SJonathan Lemon 	if (IS_ERR(p))
1461773bda96SJonathan Lemon 		return PTR_ERR(p);
1462773bda96SJonathan Lemon 
1463773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
1464773bda96SJonathan Lemon 
1465773bda96SJonathan Lemon 	return 0;
1466773bda96SJonathan Lemon }
1467773bda96SJonathan Lemon 
1468773bda96SJonathan Lemon static struct platform_device *
1469773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1470773bda96SJonathan Lemon {
14711618df6aSJonathan Lemon 	struct ptp_ocp_i2c_info *info;
1472773bda96SJonathan Lemon 	struct resource res[2];
14738119c9eeSJonathan Lemon 	resource_size_t start;
1474773bda96SJonathan Lemon 
14751618df6aSJonathan Lemon 	info = r->extra;
1476773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
1477773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
1478773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1479773bda96SJonathan Lemon 
14801618df6aSJonathan Lemon 	return platform_device_register_resndata(&pdev->dev, info->name,
14811618df6aSJonathan Lemon 						 id, res, 2,
14821618df6aSJonathan Lemon 						 info->data, info->data_size);
1483773bda96SJonathan Lemon }
1484773bda96SJonathan Lemon 
1485773bda96SJonathan Lemon static int
1486773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1487773bda96SJonathan Lemon {
1488773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
14891618df6aSJonathan Lemon 	struct ptp_ocp_i2c_info *info;
1490773bda96SJonathan Lemon 	struct platform_device *p;
1491773bda96SJonathan Lemon 	struct clk_hw *clk;
1492773bda96SJonathan Lemon 	char buf[32];
1493773bda96SJonathan Lemon 	int id;
1494773bda96SJonathan Lemon 
14951618df6aSJonathan Lemon 	info = r->extra;
1496773bda96SJonathan Lemon 	id = pci_dev_id(bp->pdev);
1497773bda96SJonathan Lemon 
1498773bda96SJonathan Lemon 	sprintf(buf, "AXI.%d", id);
14991618df6aSJonathan Lemon 	clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
15001618df6aSJonathan Lemon 					 info->fixed_rate);
1501773bda96SJonathan Lemon 	if (IS_ERR(clk))
1502773bda96SJonathan Lemon 		return PTR_ERR(clk);
1503773bda96SJonathan Lemon 	bp->i2c_clk = clk;
1504773bda96SJonathan Lemon 
15051618df6aSJonathan Lemon 	sprintf(buf, "%s.%d", info->name, id);
1506773bda96SJonathan Lemon 	devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1507773bda96SJonathan Lemon 	p = ptp_ocp_i2c_bus(bp->pdev, r, id);
1508773bda96SJonathan Lemon 	if (IS_ERR(p))
1509773bda96SJonathan Lemon 		return PTR_ERR(p);
1510773bda96SJonathan Lemon 
1511773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
1512773bda96SJonathan Lemon 
1513773bda96SJonathan Lemon 	return 0;
1514773bda96SJonathan Lemon }
1515773bda96SJonathan Lemon 
1516b325af3cSJonathan Lemon /* The expectation is that this is triggered only on error. */
1517b325af3cSJonathan Lemon static irqreturn_t
1518b325af3cSJonathan Lemon ptp_ocp_signal_irq(int irq, void *priv)
1519b325af3cSJonathan Lemon {
1520b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1521b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = ext->mem;
1522b325af3cSJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1523b325af3cSJonathan Lemon 	u32 enable, status;
1524b325af3cSJonathan Lemon 	int gen;
1525b325af3cSJonathan Lemon 
1526b325af3cSJonathan Lemon 	gen = ext->info->index - 1;
1527b325af3cSJonathan Lemon 
1528b325af3cSJonathan Lemon 	enable = ioread32(&reg->enable);
1529b325af3cSJonathan Lemon 	status = ioread32(&reg->status);
1530b325af3cSJonathan Lemon 
1531b325af3cSJonathan Lemon 	/* disable generator on error */
1532b325af3cSJonathan Lemon 	if (status || !enable) {
1533b325af3cSJonathan Lemon 		iowrite32(0, &reg->intr_mask);
1534b325af3cSJonathan Lemon 		iowrite32(0, &reg->enable);
1535b325af3cSJonathan Lemon 		bp->signal[gen].running = false;
1536b325af3cSJonathan Lemon 	}
1537b325af3cSJonathan Lemon 
1538b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr);	/* ack interrupt */
1539b325af3cSJonathan Lemon 
1540b325af3cSJonathan Lemon 	return IRQ_HANDLED;
1541b325af3cSJonathan Lemon }
1542b325af3cSJonathan Lemon 
1543b325af3cSJonathan Lemon static int
1544b325af3cSJonathan Lemon ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
1545b325af3cSJonathan Lemon {
1546b325af3cSJonathan Lemon 	struct ptp_system_timestamp sts;
1547b325af3cSJonathan Lemon 	struct timespec64 ts;
1548b325af3cSJonathan Lemon 	ktime_t start_ns;
1549b325af3cSJonathan Lemon 	int err;
1550b325af3cSJonathan Lemon 
1551b325af3cSJonathan Lemon 	if (!s->period)
1552b325af3cSJonathan Lemon 		return 0;
1553b325af3cSJonathan Lemon 
1554b325af3cSJonathan Lemon 	if (!s->pulse)
1555b325af3cSJonathan Lemon 		s->pulse = ktime_divns(s->period * s->duty, 100);
1556b325af3cSJonathan Lemon 
1557b325af3cSJonathan Lemon 	err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
1558b325af3cSJonathan Lemon 	if (err)
1559b325af3cSJonathan Lemon 		return err;
1560b325af3cSJonathan Lemon 
1561b325af3cSJonathan Lemon 	start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
1562b325af3cSJonathan Lemon 	if (!s->start) {
1563b325af3cSJonathan Lemon 		/* roundup() does not work on 32-bit systems */
15644bd46bb0SJonathan Lemon 		s->start = DIV64_U64_ROUND_UP(start_ns, s->period);
1565b325af3cSJonathan Lemon 		s->start = ktime_add(s->start, s->phase);
1566b325af3cSJonathan Lemon 	}
1567b325af3cSJonathan Lemon 
1568b325af3cSJonathan Lemon 	if (s->duty < 1 || s->duty > 99)
1569b325af3cSJonathan Lemon 		return -EINVAL;
1570b325af3cSJonathan Lemon 
1571b325af3cSJonathan Lemon 	if (s->pulse < 1 || s->pulse > s->period)
1572b325af3cSJonathan Lemon 		return -EINVAL;
1573b325af3cSJonathan Lemon 
1574b325af3cSJonathan Lemon 	if (s->start < start_ns)
1575b325af3cSJonathan Lemon 		return -EINVAL;
1576b325af3cSJonathan Lemon 
1577b325af3cSJonathan Lemon 	bp->signal[gen] = *s;
1578b325af3cSJonathan Lemon 
1579b325af3cSJonathan Lemon 	return 0;
1580b325af3cSJonathan Lemon }
1581b325af3cSJonathan Lemon 
1582b325af3cSJonathan Lemon static int
15831aa66a3aSJonathan Lemon ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
15841aa66a3aSJonathan Lemon 			   struct ptp_perout_request *req)
15851aa66a3aSJonathan Lemon {
15861aa66a3aSJonathan Lemon 	struct ptp_ocp_signal s = { };
15871aa66a3aSJonathan Lemon 
15881aa66a3aSJonathan Lemon 	s.polarity = bp->signal[gen].polarity;
15891aa66a3aSJonathan Lemon 	s.period = ktime_set(req->period.sec, req->period.nsec);
15901aa66a3aSJonathan Lemon 	if (!s.period)
15911aa66a3aSJonathan Lemon 		return 0;
15921aa66a3aSJonathan Lemon 
15931aa66a3aSJonathan Lemon 	if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
15941aa66a3aSJonathan Lemon 		s.pulse = ktime_set(req->on.sec, req->on.nsec);
15951aa66a3aSJonathan Lemon 		s.duty = ktime_divns(s.pulse * 100, s.period);
15961aa66a3aSJonathan Lemon 	}
15971aa66a3aSJonathan Lemon 
15981aa66a3aSJonathan Lemon 	if (req->flags & PTP_PEROUT_PHASE)
15991aa66a3aSJonathan Lemon 		s.phase = ktime_set(req->phase.sec, req->phase.nsec);
16001aa66a3aSJonathan Lemon 	else
16011aa66a3aSJonathan Lemon 		s.start = ktime_set(req->start.sec, req->start.nsec);
16021aa66a3aSJonathan Lemon 
16031aa66a3aSJonathan Lemon 	return ptp_ocp_signal_set(bp, gen, &s);
16041aa66a3aSJonathan Lemon }
16051aa66a3aSJonathan Lemon 
16061aa66a3aSJonathan Lemon static int
1607b325af3cSJonathan Lemon ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
1608b325af3cSJonathan Lemon {
1609b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1610b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = ext->mem;
1611b325af3cSJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1612b325af3cSJonathan Lemon 	struct timespec64 ts;
1613b325af3cSJonathan Lemon 	int gen;
1614b325af3cSJonathan Lemon 
1615b325af3cSJonathan Lemon 	gen = ext->info->index - 1;
1616b325af3cSJonathan Lemon 
1617b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr_mask);
1618b325af3cSJonathan Lemon 	iowrite32(0, &reg->enable);
1619b325af3cSJonathan Lemon 	bp->signal[gen].running = false;
1620b325af3cSJonathan Lemon 	if (!enable)
1621b325af3cSJonathan Lemon 		return 0;
1622b325af3cSJonathan Lemon 
1623b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].start);
1624b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->start_sec);
1625b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->start_ns);
1626b325af3cSJonathan Lemon 
1627b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].period);
1628b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->period_sec);
1629b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->period_ns);
1630b325af3cSJonathan Lemon 
1631b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].pulse);
1632b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->pulse_sec);
1633b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->pulse_ns);
1634b325af3cSJonathan Lemon 
1635b325af3cSJonathan Lemon 	iowrite32(bp->signal[gen].polarity, &reg->polarity);
1636b325af3cSJonathan Lemon 	iowrite32(0, &reg->repeat_count);
1637b325af3cSJonathan Lemon 
1638b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr);		/* clear interrupt state */
1639b325af3cSJonathan Lemon 	iowrite32(1, &reg->intr_mask);		/* enable interrupt */
1640b325af3cSJonathan Lemon 	iowrite32(3, &reg->enable);		/* valid & enable */
1641b325af3cSJonathan Lemon 
1642b325af3cSJonathan Lemon 	bp->signal[gen].running = true;
1643b325af3cSJonathan Lemon 
1644b325af3cSJonathan Lemon 	return 0;
1645b325af3cSJonathan Lemon }
1646b325af3cSJonathan Lemon 
1647773bda96SJonathan Lemon static irqreturn_t
1648773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv)
1649773bda96SJonathan Lemon {
1650773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1651773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1652773bda96SJonathan Lemon 	struct ptp_clock_event ev;
1653773bda96SJonathan Lemon 	u32 sec, nsec;
1654773bda96SJonathan Lemon 
1655a62a56d0SJonathan Lemon 	if (ext == ext->bp->pps) {
1656a62a56d0SJonathan Lemon 		if (ext->bp->pps_req_map & OCP_REQ_PPS) {
1657a62a56d0SJonathan Lemon 			ev.type = PTP_CLOCK_PPS;
1658a62a56d0SJonathan Lemon 			ptp_clock_event(ext->bp->ptp, &ev);
1659a62a56d0SJonathan Lemon 		}
1660a62a56d0SJonathan Lemon 
1661a62a56d0SJonathan Lemon 		if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
1662a62a56d0SJonathan Lemon 			goto out;
1663a62a56d0SJonathan Lemon 	}
1664a62a56d0SJonathan Lemon 
1665773bda96SJonathan Lemon 	/* XXX should fix API - this converts s/ns -> ts -> s/ns */
1666773bda96SJonathan Lemon 	sec = ioread32(&reg->time_sec);
1667773bda96SJonathan Lemon 	nsec = ioread32(&reg->time_ns);
1668773bda96SJonathan Lemon 
1669773bda96SJonathan Lemon 	ev.type = PTP_CLOCK_EXTTS;
1670773bda96SJonathan Lemon 	ev.index = ext->info->index;
16711acffc6eSJonathan Lemon 	ev.timestamp = sec * NSEC_PER_SEC + nsec;
1672773bda96SJonathan Lemon 
1673773bda96SJonathan Lemon 	ptp_clock_event(ext->bp->ptp, &ev);
1674773bda96SJonathan Lemon 
1675a62a56d0SJonathan Lemon out:
1676773bda96SJonathan Lemon 	iowrite32(1, &reg->intr);	/* write 1 to ack */
1677773bda96SJonathan Lemon 
1678773bda96SJonathan Lemon 	return IRQ_HANDLED;
1679773bda96SJonathan Lemon }
1680773bda96SJonathan Lemon 
1681773bda96SJonathan Lemon static int
1682a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
1683773bda96SJonathan Lemon {
1684773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1685773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1686a62a56d0SJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1687a62a56d0SJonathan Lemon 
1688a62a56d0SJonathan Lemon 	if (ext == bp->pps) {
1689a62a56d0SJonathan Lemon 		u32 old_map = bp->pps_req_map;
1690a62a56d0SJonathan Lemon 
1691a62a56d0SJonathan Lemon 		if (enable)
1692a62a56d0SJonathan Lemon 			bp->pps_req_map |= req;
1693a62a56d0SJonathan Lemon 		else
1694a62a56d0SJonathan Lemon 			bp->pps_req_map &= ~req;
1695a62a56d0SJonathan Lemon 
1696a62a56d0SJonathan Lemon 		/* if no state change, just return */
1697a62a56d0SJonathan Lemon 		if ((!!old_map ^ !!bp->pps_req_map) == 0)
1698a62a56d0SJonathan Lemon 			return 0;
1699a62a56d0SJonathan Lemon 	}
1700773bda96SJonathan Lemon 
1701773bda96SJonathan Lemon 	if (enable) {
1702773bda96SJonathan Lemon 		iowrite32(1, &reg->enable);
1703773bda96SJonathan Lemon 		iowrite32(1, &reg->intr_mask);
1704773bda96SJonathan Lemon 		iowrite32(1, &reg->intr);
1705773bda96SJonathan Lemon 	} else {
1706773bda96SJonathan Lemon 		iowrite32(0, &reg->intr_mask);
1707773bda96SJonathan Lemon 		iowrite32(0, &reg->enable);
1708773bda96SJonathan Lemon 	}
1709773bda96SJonathan Lemon 
1710773bda96SJonathan Lemon 	return 0;
1711773bda96SJonathan Lemon }
1712773bda96SJonathan Lemon 
1713773bda96SJonathan Lemon static void
1714773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1715773bda96SJonathan Lemon {
1716a62a56d0SJonathan Lemon 	ext->info->enable(ext, ~0, false);
1717773bda96SJonathan Lemon 	pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1718773bda96SJonathan Lemon 	kfree(ext);
1719773bda96SJonathan Lemon }
1720773bda96SJonathan Lemon 
1721773bda96SJonathan Lemon static int
1722773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
1723773bda96SJonathan Lemon {
1724773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1725773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext;
1726773bda96SJonathan Lemon 	int err;
1727773bda96SJonathan Lemon 
1728773bda96SJonathan Lemon 	ext = kzalloc(sizeof(*ext), GFP_KERNEL);
1729773bda96SJonathan Lemon 	if (!ext)
1730773bda96SJonathan Lemon 		return -ENOMEM;
1731773bda96SJonathan Lemon 
1732773bda96SJonathan Lemon 	ext->mem = ptp_ocp_get_mem(bp, r);
1733c7521d3aSDan Carpenter 	if (IS_ERR(ext->mem)) {
1734c7521d3aSDan Carpenter 		err = PTR_ERR(ext->mem);
1735773bda96SJonathan Lemon 		goto out;
1736c7521d3aSDan Carpenter 	}
1737773bda96SJonathan Lemon 
1738773bda96SJonathan Lemon 	ext->bp = bp;
1739773bda96SJonathan Lemon 	ext->info = r->extra;
1740773bda96SJonathan Lemon 	ext->irq_vec = r->irq_vec;
1741773bda96SJonathan Lemon 
1742773bda96SJonathan Lemon 	err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
174356ec4403SJonathan Lemon 			      ext, "ocp%d.%s", bp->id, r->name);
1744773bda96SJonathan Lemon 	if (err) {
1745773bda96SJonathan Lemon 		dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
1746773bda96SJonathan Lemon 		goto out;
1747773bda96SJonathan Lemon 	}
1748773bda96SJonathan Lemon 
1749773bda96SJonathan Lemon 	bp_assign_entry(bp, r, ext);
1750773bda96SJonathan Lemon 
1751773bda96SJonathan Lemon 	return 0;
1752773bda96SJonathan Lemon 
1753773bda96SJonathan Lemon out:
1754773bda96SJonathan Lemon 	kfree(ext);
1755773bda96SJonathan Lemon 	return err;
1756773bda96SJonathan Lemon }
1757773bda96SJonathan Lemon 
1758773bda96SJonathan Lemon static int
1759773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
1760773bda96SJonathan Lemon {
1761773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1762773bda96SJonathan Lemon 	struct uart_8250_port uart;
1763773bda96SJonathan Lemon 
1764773bda96SJonathan Lemon 	/* Setting UPF_IOREMAP and leaving port.membase unspecified lets
1765773bda96SJonathan Lemon 	 * the serial port device claim and release the pci resource.
1766773bda96SJonathan Lemon 	 */
1767773bda96SJonathan Lemon 	memset(&uart, 0, sizeof(uart));
1768773bda96SJonathan Lemon 	uart.port.dev = &pdev->dev;
1769773bda96SJonathan Lemon 	uart.port.iotype = UPIO_MEM;
1770773bda96SJonathan Lemon 	uart.port.regshift = 2;
1771773bda96SJonathan Lemon 	uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
1772773bda96SJonathan Lemon 	uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
1773773bda96SJonathan Lemon 	uart.port.uartclk = 50000000;
1774c17c4059SJonathan Lemon 	uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
1775773bda96SJonathan Lemon 	uart.port.type = PORT_16550A;
1776773bda96SJonathan Lemon 
1777773bda96SJonathan Lemon 	return serial8250_register_8250_port(&uart);
1778773bda96SJonathan Lemon }
1779773bda96SJonathan Lemon 
1780773bda96SJonathan Lemon static int
1781773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
1782773bda96SJonathan Lemon {
1783773bda96SJonathan Lemon 	int port;
1784773bda96SJonathan Lemon 
1785773bda96SJonathan Lemon 	port = ptp_ocp_serial_line(bp, r);
1786773bda96SJonathan Lemon 	if (port < 0)
1787773bda96SJonathan Lemon 		return port;
1788773bda96SJonathan Lemon 
1789773bda96SJonathan Lemon 	bp_assign_entry(bp, r, port);
1790773bda96SJonathan Lemon 
1791773bda96SJonathan Lemon 	return 0;
1792773bda96SJonathan Lemon }
1793773bda96SJonathan Lemon 
1794773bda96SJonathan Lemon static int
1795773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1796773bda96SJonathan Lemon {
1797773bda96SJonathan Lemon 	void __iomem *mem;
1798773bda96SJonathan Lemon 
1799773bda96SJonathan Lemon 	mem = ptp_ocp_get_mem(bp, r);
1800c7521d3aSDan Carpenter 	if (IS_ERR(mem))
1801c7521d3aSDan Carpenter 		return PTR_ERR(mem);
1802773bda96SJonathan Lemon 
1803773bda96SJonathan Lemon 	bp_assign_entry(bp, r, mem);
1804773bda96SJonathan Lemon 
1805773bda96SJonathan Lemon 	return 0;
1806773bda96SJonathan Lemon }
1807773bda96SJonathan Lemon 
1808e3516bb4SJonathan Lemon static void
1809e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
1810e3516bb4SJonathan Lemon {
1811e3516bb4SJonathan Lemon 	if (!bp->nmea_out)
1812e3516bb4SJonathan Lemon 		return;
1813e3516bb4SJonathan Lemon 
1814e3516bb4SJonathan Lemon 	iowrite32(0, &bp->nmea_out->ctrl);		/* disable */
1815e3516bb4SJonathan Lemon 	iowrite32(7, &bp->nmea_out->uart_baud);		/* 115200 */
1816e3516bb4SJonathan Lemon 	iowrite32(1, &bp->nmea_out->ctrl);		/* enable */
1817e3516bb4SJonathan Lemon }
1818e3516bb4SJonathan Lemon 
1819a509a7c6SJonathan Lemon static void
1820b325af3cSJonathan Lemon _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
1821b325af3cSJonathan Lemon {
1822b325af3cSJonathan Lemon 	u32 val;
1823b325af3cSJonathan Lemon 
1824b325af3cSJonathan Lemon 	iowrite32(0, &reg->enable);		/* disable */
1825b325af3cSJonathan Lemon 
1826b325af3cSJonathan Lemon 	val = ioread32(&reg->polarity);
1827b325af3cSJonathan Lemon 	s->polarity = val ? true : false;
1828b325af3cSJonathan Lemon 	s->duty = 50;
1829b325af3cSJonathan Lemon }
1830b325af3cSJonathan Lemon 
1831b325af3cSJonathan Lemon static void
1832b325af3cSJonathan Lemon ptp_ocp_signal_init(struct ptp_ocp *bp)
1833b325af3cSJonathan Lemon {
1834b325af3cSJonathan Lemon 	int i;
1835b325af3cSJonathan Lemon 
1836b325af3cSJonathan Lemon 	for (i = 0; i < 4; i++)
1837b325af3cSJonathan Lemon 		if (bp->signal_out[i])
1838b325af3cSJonathan Lemon 			_ptp_ocp_signal_init(&bp->signal[i],
1839b325af3cSJonathan Lemon 					     bp->signal_out[i]->mem);
1840b325af3cSJonathan Lemon }
1841b325af3cSJonathan Lemon 
1842b325af3cSJonathan Lemon static void
1843c2239294SJonathan Lemon ptp_ocp_attr_group_del(struct ptp_ocp *bp)
1844c2239294SJonathan Lemon {
1845c2239294SJonathan Lemon 	sysfs_remove_groups(&bp->dev.kobj, bp->attr_group);
1846c2239294SJonathan Lemon 	kfree(bp->attr_group);
1847c2239294SJonathan Lemon }
1848c2239294SJonathan Lemon 
1849c2239294SJonathan Lemon static int
1850c2239294SJonathan Lemon ptp_ocp_attr_group_add(struct ptp_ocp *bp,
1851c2239294SJonathan Lemon 		       const struct ocp_attr_group *attr_tbl)
1852c2239294SJonathan Lemon {
1853c2239294SJonathan Lemon 	int count, i;
1854c2239294SJonathan Lemon 	int err;
1855c2239294SJonathan Lemon 
1856c2239294SJonathan Lemon 	count = 0;
1857c2239294SJonathan Lemon 	for (i = 0; attr_tbl[i].cap; i++)
1858c2239294SJonathan Lemon 		if (attr_tbl[i].cap & bp->fw_cap)
1859c2239294SJonathan Lemon 			count++;
1860c2239294SJonathan Lemon 
1861c2239294SJonathan Lemon 	bp->attr_group = kcalloc(count + 1, sizeof(struct attribute_group *),
1862c2239294SJonathan Lemon 				 GFP_KERNEL);
1863c2239294SJonathan Lemon 	if (!bp->attr_group)
1864c2239294SJonathan Lemon 		return -ENOMEM;
1865c2239294SJonathan Lemon 
1866c2239294SJonathan Lemon 	count = 0;
1867c2239294SJonathan Lemon 	for (i = 0; attr_tbl[i].cap; i++)
1868c2239294SJonathan Lemon 		if (attr_tbl[i].cap & bp->fw_cap)
1869c2239294SJonathan Lemon 			bp->attr_group[count++] = attr_tbl[i].group;
1870c2239294SJonathan Lemon 
1871c2239294SJonathan Lemon 	err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group);
1872c2239294SJonathan Lemon 	if (err)
1873c2239294SJonathan Lemon 		bp->attr_group[0] = NULL;
1874c2239294SJonathan Lemon 
1875c2239294SJonathan Lemon 	return err;
1876c2239294SJonathan Lemon }
1877c2239294SJonathan Lemon 
1878c2239294SJonathan Lemon static void
1879a509a7c6SJonathan Lemon ptp_ocp_sma_init(struct ptp_ocp *bp)
1880a509a7c6SJonathan Lemon {
1881a509a7c6SJonathan Lemon 	u32 reg;
1882a509a7c6SJonathan Lemon 	int i;
1883a509a7c6SJonathan Lemon 
1884a509a7c6SJonathan Lemon 	/* defaults */
1885a509a7c6SJonathan Lemon 	bp->sma[0].mode = SMA_MODE_IN;
1886a509a7c6SJonathan Lemon 	bp->sma[1].mode = SMA_MODE_IN;
1887a509a7c6SJonathan Lemon 	bp->sma[2].mode = SMA_MODE_OUT;
1888a509a7c6SJonathan Lemon 	bp->sma[3].mode = SMA_MODE_OUT;
1889a509a7c6SJonathan Lemon 
1890a509a7c6SJonathan Lemon 	/* If no SMA1 map, the pin functions and directions are fixed. */
1891a509a7c6SJonathan Lemon 	if (!bp->sma_map1) {
1892a509a7c6SJonathan Lemon 		for (i = 0; i < 4; i++) {
1893a509a7c6SJonathan Lemon 			bp->sma[i].fixed_fcn = true;
1894a509a7c6SJonathan Lemon 			bp->sma[i].fixed_dir = true;
1895a509a7c6SJonathan Lemon 		}
1896a509a7c6SJonathan Lemon 		return;
1897a509a7c6SJonathan Lemon 	}
1898a509a7c6SJonathan Lemon 
1899a509a7c6SJonathan Lemon 	/* If SMA2 GPIO output map is all 1, it is not present.
1900a509a7c6SJonathan Lemon 	 * This indicates the firmware has fixed direction SMA pins.
1901a509a7c6SJonathan Lemon 	 */
1902a509a7c6SJonathan Lemon 	reg = ioread32(&bp->sma_map2->gpio2);
1903a509a7c6SJonathan Lemon 	if (reg == 0xffffffff) {
1904a509a7c6SJonathan Lemon 		for (i = 0; i < 4; i++)
1905a509a7c6SJonathan Lemon 			bp->sma[i].fixed_dir = true;
1906a509a7c6SJonathan Lemon 	} else {
1907a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio1);
1908a509a7c6SJonathan Lemon 		bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
1909a509a7c6SJonathan Lemon 		bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
1910a509a7c6SJonathan Lemon 
1911a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio2);
1912a509a7c6SJonathan Lemon 		bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
1913a509a7c6SJonathan Lemon 		bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
1914a509a7c6SJonathan Lemon 	}
1915a509a7c6SJonathan Lemon }
1916a509a7c6SJonathan Lemon 
19171aa66a3aSJonathan Lemon static int
19181aa66a3aSJonathan Lemon ptp_ocp_fb_set_pins(struct ptp_ocp *bp)
19191aa66a3aSJonathan Lemon {
19201aa66a3aSJonathan Lemon 	struct ptp_pin_desc *config;
19211aa66a3aSJonathan Lemon 	int i;
19221aa66a3aSJonathan Lemon 
19231aa66a3aSJonathan Lemon 	config = kzalloc(sizeof(*config) * 4, GFP_KERNEL);
19241aa66a3aSJonathan Lemon 	if (!config)
19251aa66a3aSJonathan Lemon 		return -ENOMEM;
19261aa66a3aSJonathan Lemon 
19271aa66a3aSJonathan Lemon 	for (i = 0; i < 4; i++) {
19281aa66a3aSJonathan Lemon 		sprintf(config[i].name, "sma%d", i + 1);
19291aa66a3aSJonathan Lemon 		config[i].index = i;
19301aa66a3aSJonathan Lemon 	}
19311aa66a3aSJonathan Lemon 
19321aa66a3aSJonathan Lemon 	bp->ptp_info.n_pins = 4;
19331aa66a3aSJonathan Lemon 	bp->ptp_info.pin_config = config;
19341aa66a3aSJonathan Lemon 
19351aa66a3aSJonathan Lemon 	return 0;
19361aa66a3aSJonathan Lemon }
19371aa66a3aSJonathan Lemon 
19385a728ac5SJonathan Lemon static void
19395a728ac5SJonathan Lemon ptp_ocp_fb_set_version(struct ptp_ocp *bp)
19405a728ac5SJonathan Lemon {
19415a728ac5SJonathan Lemon 	u64 cap = OCP_CAP_BASIC;
19425a728ac5SJonathan Lemon 	u32 version;
19435a728ac5SJonathan Lemon 
19445a728ac5SJonathan Lemon 	version = ioread32(&bp->image->version);
19455a728ac5SJonathan Lemon 
19465a728ac5SJonathan Lemon 	/* if lower 16 bits are empty, this is the fw loader. */
19475a728ac5SJonathan Lemon 	if ((version & 0xffff) == 0) {
19485a728ac5SJonathan Lemon 		version = version >> 16;
19495a728ac5SJonathan Lemon 		bp->fw_loader = true;
19505a728ac5SJonathan Lemon 	}
19515a728ac5SJonathan Lemon 
19525a728ac5SJonathan Lemon 	bp->fw_tag = version >> 15;
19535a728ac5SJonathan Lemon 	bp->fw_version = version & 0x7fff;
19545a728ac5SJonathan Lemon 
19555a728ac5SJonathan Lemon 	if (bp->fw_tag) {
19565a728ac5SJonathan Lemon 		/* FPGA firmware */
19575a728ac5SJonathan Lemon 		if (version >= 5)
19585a728ac5SJonathan Lemon 			cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ;
19595a728ac5SJonathan Lemon 	} else {
19605a728ac5SJonathan Lemon 		/* SOM firmware */
19615a728ac5SJonathan Lemon 		if (version >= 19)
19625a728ac5SJonathan Lemon 			cap |= OCP_CAP_SIGNAL;
19635a728ac5SJonathan Lemon 		if (version >= 20)
19645a728ac5SJonathan Lemon 			cap |= OCP_CAP_FREQ;
19655a728ac5SJonathan Lemon 	}
19665a728ac5SJonathan Lemon 
19675a728ac5SJonathan Lemon 	bp->fw_cap = cap;
19685a728ac5SJonathan Lemon }
19695a728ac5SJonathan Lemon 
1970773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */
1971773bda96SJonathan Lemon static int
1972773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
1973773bda96SJonathan Lemon {
19745a728ac5SJonathan Lemon 	int err;
1975b325af3cSJonathan Lemon 
1976773bda96SJonathan Lemon 	bp->flash_start = 1024 * 4096;
19770cfcdd1eSJonathan Lemon 	bp->eeprom_map = fb_eeprom_map;
1978b0ca789aSJonathan Lemon 	bp->fw_version = ioread32(&bp->image->version);
1979773bda96SJonathan Lemon 
19805a728ac5SJonathan Lemon 	ptp_ocp_fb_set_version(bp);
1981b325af3cSJonathan Lemon 
1982065efcc5SJonathan Lemon 	ptp_ocp_tod_init(bp);
1983e3516bb4SJonathan Lemon 	ptp_ocp_nmea_out_init(bp);
1984a509a7c6SJonathan Lemon 	ptp_ocp_sma_init(bp);
1985b325af3cSJonathan Lemon 	ptp_ocp_signal_init(bp);
1986065efcc5SJonathan Lemon 
1987c2239294SJonathan Lemon 	err = ptp_ocp_attr_group_add(bp, fb_timecard_groups);
1988c2239294SJonathan Lemon 	if (err)
1989c2239294SJonathan Lemon 		return err;
1990c2239294SJonathan Lemon 
19911aa66a3aSJonathan Lemon 	err = ptp_ocp_fb_set_pins(bp);
19921aa66a3aSJonathan Lemon 	if (err)
19931aa66a3aSJonathan Lemon 		return err;
19941aa66a3aSJonathan Lemon 
1995773bda96SJonathan Lemon 	return ptp_ocp_init_clock(bp);
1996773bda96SJonathan Lemon }
1997773bda96SJonathan Lemon 
199856ec4403SJonathan Lemon static bool
199956ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
200056ec4403SJonathan Lemon {
200156ec4403SJonathan Lemon 	bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
200256ec4403SJonathan Lemon 
200356ec4403SJonathan Lemon 	if (!allow)
200456ec4403SJonathan Lemon 		dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
200556ec4403SJonathan Lemon 			r->irq_vec, r->name);
200656ec4403SJonathan Lemon 	return allow;
200756ec4403SJonathan Lemon }
200856ec4403SJonathan Lemon 
2009773bda96SJonathan Lemon static int
2010773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
2011773bda96SJonathan Lemon {
2012773bda96SJonathan Lemon 	struct ocp_resource *r, *table;
2013773bda96SJonathan Lemon 	int err = 0;
2014773bda96SJonathan Lemon 
2015773bda96SJonathan Lemon 	table = (struct ocp_resource *)driver_data;
2016773bda96SJonathan Lemon 	for (r = table; r->setup; r++) {
201756ec4403SJonathan Lemon 		if (!ptp_ocp_allow_irq(bp, r))
201856ec4403SJonathan Lemon 			continue;
2019773bda96SJonathan Lemon 		err = r->setup(bp, r);
2020bceff290SJonathan Lemon 		if (err) {
2021bceff290SJonathan Lemon 			dev_err(&bp->pdev->dev,
2022bceff290SJonathan Lemon 				"Could not register %s: err %d\n",
2023bceff290SJonathan Lemon 				r->name, err);
2024773bda96SJonathan Lemon 			break;
2025773bda96SJonathan Lemon 		}
2026bceff290SJonathan Lemon 	}
2027773bda96SJonathan Lemon 	return err;
2028773bda96SJonathan Lemon }
2029773bda96SJonathan Lemon 
20306baf2925SJonathan Lemon static void
20316baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
20326baf2925SJonathan Lemon {
20336baf2925SJonathan Lemon 	u32 ctrl;
20346baf2925SJonathan Lemon 	bool on;
20356baf2925SJonathan Lemon 
20366baf2925SJonathan Lemon 	ctrl = ioread32(reg);
20376baf2925SJonathan Lemon 	on = ctrl & bit;
20386baf2925SJonathan Lemon 	if (on ^ enable) {
20396baf2925SJonathan Lemon 		ctrl &= ~bit;
20406baf2925SJonathan Lemon 		ctrl |= enable ? bit : 0;
20416baf2925SJonathan Lemon 		iowrite32(ctrl, reg);
20426baf2925SJonathan Lemon 	}
20436baf2925SJonathan Lemon }
20446baf2925SJonathan Lemon 
20456baf2925SJonathan Lemon static void
20466baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
20476baf2925SJonathan Lemon {
20486baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
20496baf2925SJonathan Lemon 				   IRIG_M_CTRL_ENABLE, enable);
20506baf2925SJonathan Lemon }
20516baf2925SJonathan Lemon 
20526baf2925SJonathan Lemon static void
20536baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
20546baf2925SJonathan Lemon {
20556baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
20566baf2925SJonathan Lemon 				   IRIG_S_CTRL_ENABLE, enable);
20576baf2925SJonathan Lemon }
20586baf2925SJonathan Lemon 
20596baf2925SJonathan Lemon static void
20606baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
20616baf2925SJonathan Lemon {
20626baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
20636baf2925SJonathan Lemon 				   DCF_M_CTRL_ENABLE, enable);
20646baf2925SJonathan Lemon }
20656baf2925SJonathan Lemon 
20666baf2925SJonathan Lemon static void
20676baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
20686baf2925SJonathan Lemon {
20696baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
20706baf2925SJonathan Lemon 				   DCF_S_CTRL_ENABLE, enable);
20716baf2925SJonathan Lemon }
20726baf2925SJonathan Lemon 
20736baf2925SJonathan Lemon static void
20746baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
20756baf2925SJonathan Lemon {
20766baf2925SJonathan Lemon 	ptp_ocp_irig_out(bp, val & 0x00100010);
20776baf2925SJonathan Lemon 	ptp_ocp_dcf_out(bp, val & 0x00200020);
20786baf2925SJonathan Lemon }
20796baf2925SJonathan Lemon 
20806baf2925SJonathan Lemon static void
20816baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
20826baf2925SJonathan Lemon {
20836baf2925SJonathan Lemon 	ptp_ocp_irig_in(bp, val & 0x00100010);
20846baf2925SJonathan Lemon 	ptp_ocp_dcf_in(bp, val & 0x00200020);
20856baf2925SJonathan Lemon }
20866baf2925SJonathan Lemon 
2087e1daf0ecSJonathan Lemon /*
2088e1daf0ecSJonathan Lemon  * ANT0 == gps	(in)
2089e1daf0ecSJonathan Lemon  * ANT1 == sma1 (in)
2090e1daf0ecSJonathan Lemon  * ANT2 == sma2 (in)
2091e1daf0ecSJonathan Lemon  * ANT3 == sma3 (out)
2092e1daf0ecSJonathan Lemon  * ANT4 == sma4 (out)
2093e1daf0ecSJonathan Lemon  */
2094e1daf0ecSJonathan Lemon 
2095e1daf0ecSJonathan Lemon static ssize_t
2096*3f3fe41cSJonathan Lemon ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf,
2097*3f3fe41cSJonathan Lemon 		    int def_val)
2098e1daf0ecSJonathan Lemon {
2099e1daf0ecSJonathan Lemon 	const char *name;
2100e1daf0ecSJonathan Lemon 	ssize_t count;
2101e1daf0ecSJonathan Lemon 
2102e1daf0ecSJonathan Lemon 	count = sysfs_emit(buf, "OUT: ");
2103aa56a7ffSJonathan Lemon 	name = ptp_ocp_select_name_from_val(tbl, val);
2104e1daf0ecSJonathan Lemon 	if (!name)
2105aa56a7ffSJonathan Lemon 		name = ptp_ocp_select_name_from_val(tbl, def_val);
2106e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "%s\n", name);
2107e1daf0ecSJonathan Lemon 	return count;
2108e1daf0ecSJonathan Lemon }
2109e1daf0ecSJonathan Lemon 
2110e1daf0ecSJonathan Lemon static ssize_t
2111*3f3fe41cSJonathan Lemon ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf,
2112*3f3fe41cSJonathan Lemon 		    int def_val)
2113e1daf0ecSJonathan Lemon {
2114e1daf0ecSJonathan Lemon 	const char *name;
2115e1daf0ecSJonathan Lemon 	ssize_t count;
2116e1daf0ecSJonathan Lemon 	int i;
2117e1daf0ecSJonathan Lemon 
2118e1daf0ecSJonathan Lemon 	count = sysfs_emit(buf, "IN: ");
2119aa56a7ffSJonathan Lemon 	for (i = 0; tbl[i].name; i++) {
2120aa56a7ffSJonathan Lemon 		if (val & tbl[i].value) {
2121aa56a7ffSJonathan Lemon 			name = tbl[i].name;
2122e1daf0ecSJonathan Lemon 			count += sysfs_emit_at(buf, count, "%s ", name);
2123e1daf0ecSJonathan Lemon 		}
2124e1daf0ecSJonathan Lemon 	}
2125b2c4f0acSJonathan Lemon 	if (!val && def_val >= 0) {
2126aa56a7ffSJonathan Lemon 		name = ptp_ocp_select_name_from_val(tbl, def_val);
2127b2c4f0acSJonathan Lemon 		count += sysfs_emit_at(buf, count, "%s ", name);
2128b2c4f0acSJonathan Lemon 	}
2129e1daf0ecSJonathan Lemon 	if (count)
2130e1daf0ecSJonathan Lemon 		count--;
2131e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "\n");
2132e1daf0ecSJonathan Lemon 	return count;
2133e1daf0ecSJonathan Lemon }
2134e1daf0ecSJonathan Lemon 
2135e1daf0ecSJonathan Lemon static int
2136*3f3fe41cSJonathan Lemon sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf,
2137aa56a7ffSJonathan Lemon 		 enum ptp_ocp_sma_mode *mode)
2138e1daf0ecSJonathan Lemon {
2139e1daf0ecSJonathan Lemon 	int idx, count, dir;
2140e1daf0ecSJonathan Lemon 	char **argv;
2141e1daf0ecSJonathan Lemon 	int ret;
2142e1daf0ecSJonathan Lemon 
2143e1daf0ecSJonathan Lemon 	argv = argv_split(GFP_KERNEL, buf, &count);
2144e1daf0ecSJonathan Lemon 	if (!argv)
2145e1daf0ecSJonathan Lemon 		return -ENOMEM;
2146e1daf0ecSJonathan Lemon 
2147e1daf0ecSJonathan Lemon 	ret = -EINVAL;
2148e1daf0ecSJonathan Lemon 	if (!count)
2149e1daf0ecSJonathan Lemon 		goto out;
2150e1daf0ecSJonathan Lemon 
2151e1daf0ecSJonathan Lemon 	idx = 0;
2152e1daf0ecSJonathan Lemon 	dir = *mode == SMA_MODE_IN ? 0 : 1;
2153a509a7c6SJonathan Lemon 	if (!strcasecmp("IN:", argv[0])) {
2154e1daf0ecSJonathan Lemon 		dir = 0;
2155e1daf0ecSJonathan Lemon 		idx++;
2156e1daf0ecSJonathan Lemon 	}
2157e1daf0ecSJonathan Lemon 	if (!strcasecmp("OUT:", argv[0])) {
2158e1daf0ecSJonathan Lemon 		dir = 1;
2159e1daf0ecSJonathan Lemon 		idx++;
2160e1daf0ecSJonathan Lemon 	}
2161e1daf0ecSJonathan Lemon 	*mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
2162e1daf0ecSJonathan Lemon 
2163e1daf0ecSJonathan Lemon 	ret = 0;
2164e1daf0ecSJonathan Lemon 	for (; idx < count; idx++)
2165e1daf0ecSJonathan Lemon 		ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
2166e1daf0ecSJonathan Lemon 	if (ret < 0)
2167e1daf0ecSJonathan Lemon 		ret = -EINVAL;
2168e1daf0ecSJonathan Lemon 
2169e1daf0ecSJonathan Lemon out:
2170e1daf0ecSJonathan Lemon 	argv_free(argv);
2171e1daf0ecSJonathan Lemon 	return ret;
2172e1daf0ecSJonathan Lemon }
2173e1daf0ecSJonathan Lemon 
2174a509a7c6SJonathan Lemon static u32
2175a509a7c6SJonathan Lemon ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr, enum ptp_ocp_sma_mode mode)
2176e1daf0ecSJonathan Lemon {
2177a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2178a509a7c6SJonathan Lemon 	u32 shift;
2179a509a7c6SJonathan Lemon 
2180a509a7c6SJonathan Lemon 	if (bp->sma[sma_nr - 1].fixed_fcn)
2181a509a7c6SJonathan Lemon 		return (sma_nr - 1) & 1;
2182a509a7c6SJonathan Lemon 
2183a509a7c6SJonathan Lemon 	if (mode == SMA_MODE_IN)
2184a509a7c6SJonathan Lemon 		gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2185a509a7c6SJonathan Lemon 	else
2186a509a7c6SJonathan Lemon 		gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2187a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2188a509a7c6SJonathan Lemon 
2189a509a7c6SJonathan Lemon 	return (ioread32(gpio) >> shift) & 0xffff;
2190a509a7c6SJonathan Lemon }
2191a509a7c6SJonathan Lemon 
2192a509a7c6SJonathan Lemon static ssize_t
2193a509a7c6SJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
2194b2c4f0acSJonathan Lemon 		 int default_in_val, int default_out_val)
2195a509a7c6SJonathan Lemon {
2196a509a7c6SJonathan Lemon 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2197*3f3fe41cSJonathan Lemon 	const struct ocp_selector * const *tbl;
2198a509a7c6SJonathan Lemon 	u32 val;
2199a509a7c6SJonathan Lemon 
2200aa56a7ffSJonathan Lemon 	tbl = ocp_sma_tbl[bp->sma_tbl];
2201aa56a7ffSJonathan Lemon 
2202a509a7c6SJonathan Lemon 	val = ptp_ocp_sma_get(bp, sma_nr, sma->mode) & SMA_SELECT_MASK;
2203e1daf0ecSJonathan Lemon 
2204b2c4f0acSJonathan Lemon 	if (sma->mode == SMA_MODE_IN) {
2205b2c4f0acSJonathan Lemon 		if (sma->disabled)
2206b2c4f0acSJonathan Lemon 			val = SMA_DISABLE;
2207aa56a7ffSJonathan Lemon 		return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val);
2208b2c4f0acSJonathan Lemon 	}
2209e1daf0ecSJonathan Lemon 
2210aa56a7ffSJonathan Lemon 	return ptp_ocp_show_output(tbl[1], val, buf, default_out_val);
2211e1daf0ecSJonathan Lemon }
2212e1daf0ecSJonathan Lemon 
2213e1daf0ecSJonathan Lemon static ssize_t
2214e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
2215e1daf0ecSJonathan Lemon {
2216e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2217e1daf0ecSJonathan Lemon 
2218a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
2219e1daf0ecSJonathan Lemon }
2220e1daf0ecSJonathan Lemon 
2221e1daf0ecSJonathan Lemon static ssize_t
2222e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
2223e1daf0ecSJonathan Lemon {
2224e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2225e1daf0ecSJonathan Lemon 
2226a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
2227e1daf0ecSJonathan Lemon }
2228e1daf0ecSJonathan Lemon 
2229e1daf0ecSJonathan Lemon static ssize_t
2230e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
2231e1daf0ecSJonathan Lemon {
2232e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2233e1daf0ecSJonathan Lemon 
2234a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
2235e1daf0ecSJonathan Lemon }
2236e1daf0ecSJonathan Lemon 
2237e1daf0ecSJonathan Lemon static ssize_t
2238e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
2239e1daf0ecSJonathan Lemon {
2240e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2241e1daf0ecSJonathan Lemon 
2242a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
2243e1daf0ecSJonathan Lemon }
2244e1daf0ecSJonathan Lemon 
2245e1daf0ecSJonathan Lemon static void
2246a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2247e1daf0ecSJonathan Lemon {
2248a509a7c6SJonathan Lemon 	u32 reg, mask, shift;
2249e1daf0ecSJonathan Lemon 	unsigned long flags;
2250a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2251a509a7c6SJonathan Lemon 
2252a509a7c6SJonathan Lemon 	gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2253a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2254e1daf0ecSJonathan Lemon 
2255e1daf0ecSJonathan Lemon 	mask = 0xffff << (16 - shift);
2256e1daf0ecSJonathan Lemon 
2257e1daf0ecSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2258e1daf0ecSJonathan Lemon 
2259a509a7c6SJonathan Lemon 	reg = ioread32(gpio);
2260a509a7c6SJonathan Lemon 	reg = (reg & mask) | (val << shift);
22616baf2925SJonathan Lemon 
2262a509a7c6SJonathan Lemon 	__handle_signal_outputs(bp, reg);
22636baf2925SJonathan Lemon 
2264a509a7c6SJonathan Lemon 	iowrite32(reg, gpio);
2265e1daf0ecSJonathan Lemon 
2266e1daf0ecSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2267e1daf0ecSJonathan Lemon }
2268e1daf0ecSJonathan Lemon 
2269e1daf0ecSJonathan Lemon static void
2270a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2271e1daf0ecSJonathan Lemon {
2272a509a7c6SJonathan Lemon 	u32 reg, mask, shift;
2273e1daf0ecSJonathan Lemon 	unsigned long flags;
2274a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2275a509a7c6SJonathan Lemon 
2276a509a7c6SJonathan Lemon 	gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2277a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2278e1daf0ecSJonathan Lemon 
2279e1daf0ecSJonathan Lemon 	mask = 0xffff << (16 - shift);
2280e1daf0ecSJonathan Lemon 
2281e1daf0ecSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2282e1daf0ecSJonathan Lemon 
2283a509a7c6SJonathan Lemon 	reg = ioread32(gpio);
2284a509a7c6SJonathan Lemon 	reg = (reg & mask) | (val << shift);
22856baf2925SJonathan Lemon 
2286a509a7c6SJonathan Lemon 	__handle_signal_inputs(bp, reg);
22876baf2925SJonathan Lemon 
2288a509a7c6SJonathan Lemon 	iowrite32(reg, gpio);
2289e1daf0ecSJonathan Lemon 
2290e1daf0ecSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2291e1daf0ecSJonathan Lemon }
2292e1daf0ecSJonathan Lemon 
2293a509a7c6SJonathan Lemon static int
2294a509a7c6SJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
2295e1daf0ecSJonathan Lemon {
2296a509a7c6SJonathan Lemon 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2297e1daf0ecSJonathan Lemon 	enum ptp_ocp_sma_mode mode;
2298e1daf0ecSJonathan Lemon 	int val;
2299e1daf0ecSJonathan Lemon 
2300e1daf0ecSJonathan Lemon 	mode = sma->mode;
2301aa56a7ffSJonathan Lemon 	val = sma_parse_inputs(ocp_sma_tbl[bp->sma_tbl], buf, &mode);
2302e1daf0ecSJonathan Lemon 	if (val < 0)
2303e1daf0ecSJonathan Lemon 		return val;
2304e1daf0ecSJonathan Lemon 
2305b2c4f0acSJonathan Lemon 	if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
2306e1daf0ecSJonathan Lemon 		return -EOPNOTSUPP;
2307e1daf0ecSJonathan Lemon 
2308a509a7c6SJonathan Lemon 	if (sma->fixed_fcn) {
2309a509a7c6SJonathan Lemon 		if (val != ((sma_nr - 1) & 1))
2310e1daf0ecSJonathan Lemon 			return -EOPNOTSUPP;
2311a509a7c6SJonathan Lemon 		return 0;
2312e1daf0ecSJonathan Lemon 	}
2313e1daf0ecSJonathan Lemon 
2314b2c4f0acSJonathan Lemon 	sma->disabled = !!(val & SMA_DISABLE);
2315b2c4f0acSJonathan Lemon 
2316a509a7c6SJonathan Lemon 	if (mode != sma->mode) {
2317a509a7c6SJonathan Lemon 		if (mode == SMA_MODE_IN)
2318a509a7c6SJonathan Lemon 			ptp_ocp_sma_store_output(bp, sma_nr, 0);
2319e1daf0ecSJonathan Lemon 		else
2320a509a7c6SJonathan Lemon 			ptp_ocp_sma_store_inputs(bp, sma_nr, 0);
2321a509a7c6SJonathan Lemon 		sma->mode = mode;
2322a509a7c6SJonathan Lemon 	}
2323a509a7c6SJonathan Lemon 
2324a509a7c6SJonathan Lemon 	if (!sma->fixed_dir)
2325a509a7c6SJonathan Lemon 		val |= SMA_ENABLE;		/* add enable bit */
2326a509a7c6SJonathan Lemon 
2327b2c4f0acSJonathan Lemon 	if (sma->disabled)
2328b2c4f0acSJonathan Lemon 		val = 0;
2329b2c4f0acSJonathan Lemon 
2330a509a7c6SJonathan Lemon 	if (mode == SMA_MODE_IN)
2331a509a7c6SJonathan Lemon 		ptp_ocp_sma_store_inputs(bp, sma_nr, val);
2332a509a7c6SJonathan Lemon 	else
2333a509a7c6SJonathan Lemon 		ptp_ocp_sma_store_output(bp, sma_nr, val);
2334e1daf0ecSJonathan Lemon 
2335e1daf0ecSJonathan Lemon 	return 0;
2336e1daf0ecSJonathan Lemon }
2337e1daf0ecSJonathan Lemon 
2338e1daf0ecSJonathan Lemon static ssize_t
2339e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr,
2340e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2341e1daf0ecSJonathan Lemon {
2342e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2343e1daf0ecSJonathan Lemon 	int err;
2344e1daf0ecSJonathan Lemon 
2345a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 1);
2346e1daf0ecSJonathan Lemon 	return err ? err : count;
2347e1daf0ecSJonathan Lemon }
2348e1daf0ecSJonathan Lemon 
2349e1daf0ecSJonathan Lemon static ssize_t
2350e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr,
2351e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2352e1daf0ecSJonathan Lemon {
2353e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2354e1daf0ecSJonathan Lemon 	int err;
2355e1daf0ecSJonathan Lemon 
2356a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 2);
2357e1daf0ecSJonathan Lemon 	return err ? err : count;
2358e1daf0ecSJonathan Lemon }
2359e1daf0ecSJonathan Lemon 
2360e1daf0ecSJonathan Lemon static ssize_t
2361e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr,
2362e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2363e1daf0ecSJonathan Lemon {
2364e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2365e1daf0ecSJonathan Lemon 	int err;
2366e1daf0ecSJonathan Lemon 
2367a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 3);
2368e1daf0ecSJonathan Lemon 	return err ? err : count;
2369e1daf0ecSJonathan Lemon }
2370e1daf0ecSJonathan Lemon 
2371e1daf0ecSJonathan Lemon static ssize_t
2372e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr,
2373e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2374e1daf0ecSJonathan Lemon {
2375e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2376e1daf0ecSJonathan Lemon 	int err;
2377e1daf0ecSJonathan Lemon 
2378a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 4);
2379e1daf0ecSJonathan Lemon 	return err ? err : count;
2380e1daf0ecSJonathan Lemon }
2381e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1);
2382e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2);
2383e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3);
2384e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4);
2385e1daf0ecSJonathan Lemon 
2386e1daf0ecSJonathan Lemon static ssize_t
2387e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev,
2388e1daf0ecSJonathan Lemon 			  struct device_attribute *attr, char *buf)
2389e1daf0ecSJonathan Lemon {
2390aa56a7ffSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2391aa56a7ffSJonathan Lemon 
2392aa56a7ffSJonathan Lemon 	return ptp_ocp_select_table_show(ocp_sma_tbl[bp->sma_tbl][0], buf);
2393e1daf0ecSJonathan Lemon }
2394e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs);
2395e1daf0ecSJonathan Lemon 
2396e1daf0ecSJonathan Lemon static ssize_t
2397e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev,
2398e1daf0ecSJonathan Lemon 			   struct device_attribute *attr, char *buf)
2399e1daf0ecSJonathan Lemon {
2400aa56a7ffSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2401aa56a7ffSJonathan Lemon 
2402aa56a7ffSJonathan Lemon 	return ptp_ocp_select_table_show(ocp_sma_tbl[bp->sma_tbl][1], buf);
2403e1daf0ecSJonathan Lemon }
2404e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs);
2405e1daf0ecSJonathan Lemon 
2406b325af3cSJonathan Lemon #define EXT_ATTR_RO(_group, _name, _val)				\
2407b325af3cSJonathan Lemon 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
2408b325af3cSJonathan Lemon 		{ __ATTR_RO(_name), (void *)_val }
2409b325af3cSJonathan Lemon #define EXT_ATTR_RW(_group, _name, _val)				\
2410b325af3cSJonathan Lemon 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
2411b325af3cSJonathan Lemon 		{ __ATTR_RW(_name), (void *)_val }
2412b325af3cSJonathan Lemon #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2413b325af3cSJonathan Lemon 
2414b325af3cSJonathan Lemon /* period [duty [phase [polarity]]] */
2415b325af3cSJonathan Lemon static ssize_t
2416b325af3cSJonathan Lemon signal_store(struct device *dev, struct device_attribute *attr,
2417b325af3cSJonathan Lemon 	     const char *buf, size_t count)
2418b325af3cSJonathan Lemon {
2419b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2420b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2421b325af3cSJonathan Lemon 	struct ptp_ocp_signal s = { };
2422b325af3cSJonathan Lemon 	int gen = (uintptr_t)ea->var;
2423b325af3cSJonathan Lemon 	int argc, err;
2424b325af3cSJonathan Lemon 	char **argv;
2425b325af3cSJonathan Lemon 
2426b325af3cSJonathan Lemon 	argv = argv_split(GFP_KERNEL, buf, &argc);
2427b325af3cSJonathan Lemon 	if (!argv)
2428b325af3cSJonathan Lemon 		return -ENOMEM;
2429b325af3cSJonathan Lemon 
2430b325af3cSJonathan Lemon 	err = -EINVAL;
2431b325af3cSJonathan Lemon 	s.duty = bp->signal[gen].duty;
2432b325af3cSJonathan Lemon 	s.phase = bp->signal[gen].phase;
2433b325af3cSJonathan Lemon 	s.period = bp->signal[gen].period;
2434b325af3cSJonathan Lemon 	s.polarity = bp->signal[gen].polarity;
2435b325af3cSJonathan Lemon 
2436b325af3cSJonathan Lemon 	switch (argc) {
2437b325af3cSJonathan Lemon 	case 4:
2438b325af3cSJonathan Lemon 		argc--;
2439b325af3cSJonathan Lemon 		err = kstrtobool(argv[argc], &s.polarity);
2440b325af3cSJonathan Lemon 		if (err)
2441b325af3cSJonathan Lemon 			goto out;
2442b325af3cSJonathan Lemon 		fallthrough;
2443b325af3cSJonathan Lemon 	case 3:
2444b325af3cSJonathan Lemon 		argc--;
2445b325af3cSJonathan Lemon 		err = kstrtou64(argv[argc], 0, &s.phase);
2446b325af3cSJonathan Lemon 		if (err)
2447b325af3cSJonathan Lemon 			goto out;
2448b325af3cSJonathan Lemon 		fallthrough;
2449b325af3cSJonathan Lemon 	case 2:
2450b325af3cSJonathan Lemon 		argc--;
2451b325af3cSJonathan Lemon 		err = kstrtoint(argv[argc], 0, &s.duty);
2452b325af3cSJonathan Lemon 		if (err)
2453b325af3cSJonathan Lemon 			goto out;
2454b325af3cSJonathan Lemon 		fallthrough;
2455b325af3cSJonathan Lemon 	case 1:
2456b325af3cSJonathan Lemon 		argc--;
2457b325af3cSJonathan Lemon 		err = kstrtou64(argv[argc], 0, &s.period);
2458b325af3cSJonathan Lemon 		if (err)
2459b325af3cSJonathan Lemon 			goto out;
2460b325af3cSJonathan Lemon 		break;
2461b325af3cSJonathan Lemon 	default:
2462b325af3cSJonathan Lemon 		goto out;
2463b325af3cSJonathan Lemon 	}
2464b325af3cSJonathan Lemon 
2465b325af3cSJonathan Lemon 	err = ptp_ocp_signal_set(bp, gen, &s);
2466b325af3cSJonathan Lemon 	if (err)
2467b325af3cSJonathan Lemon 		goto out;
2468b325af3cSJonathan Lemon 
2469b325af3cSJonathan Lemon 	err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
2470b325af3cSJonathan Lemon 
2471b325af3cSJonathan Lemon out:
2472b325af3cSJonathan Lemon 	argv_free(argv);
2473b325af3cSJonathan Lemon 	return err ? err : count;
2474b325af3cSJonathan Lemon }
2475b325af3cSJonathan Lemon 
2476b325af3cSJonathan Lemon static ssize_t
2477b325af3cSJonathan Lemon signal_show(struct device *dev, struct device_attribute *attr, char *buf)
2478b325af3cSJonathan Lemon {
2479b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2480b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2481b325af3cSJonathan Lemon 	struct ptp_ocp_signal *signal;
2482b325af3cSJonathan Lemon 	struct timespec64 ts;
2483b325af3cSJonathan Lemon 	ssize_t count;
2484b325af3cSJonathan Lemon 	int i;
2485b325af3cSJonathan Lemon 
2486b325af3cSJonathan Lemon 	i = (uintptr_t)ea->var;
2487b325af3cSJonathan Lemon 	signal = &bp->signal[i];
2488b325af3cSJonathan Lemon 
2489b325af3cSJonathan Lemon 	count = sysfs_emit(buf, "%llu %d %llu %d", signal->period,
2490b325af3cSJonathan Lemon 			   signal->duty, signal->phase, signal->polarity);
2491b325af3cSJonathan Lemon 
2492b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(signal->start);
2493b325af3cSJonathan Lemon 	count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts);
2494b325af3cSJonathan Lemon 
2495b325af3cSJonathan Lemon 	return count;
2496b325af3cSJonathan Lemon }
2497b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 0);
2498b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 1);
2499b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 2);
2500b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 3);
2501b325af3cSJonathan Lemon 
2502b325af3cSJonathan Lemon static ssize_t
2503b325af3cSJonathan Lemon duty_show(struct device *dev, struct device_attribute *attr, char *buf)
2504b325af3cSJonathan Lemon {
2505b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2506b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2507b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2508b325af3cSJonathan Lemon 
2509b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
2510b325af3cSJonathan Lemon }
2511b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 0);
2512b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 1);
2513b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 2);
2514b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 3);
2515b325af3cSJonathan Lemon 
2516b325af3cSJonathan Lemon static ssize_t
2517b325af3cSJonathan Lemon period_show(struct device *dev, struct device_attribute *attr, char *buf)
2518b325af3cSJonathan Lemon {
2519b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2520b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2521b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2522b325af3cSJonathan Lemon 
2523b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
2524b325af3cSJonathan Lemon }
2525b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 0);
2526b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 1);
2527b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 2);
2528b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 3);
2529b325af3cSJonathan Lemon 
2530b325af3cSJonathan Lemon static ssize_t
2531b325af3cSJonathan Lemon phase_show(struct device *dev, struct device_attribute *attr, char *buf)
2532b325af3cSJonathan Lemon {
2533b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2534b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2535b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2536b325af3cSJonathan Lemon 
2537b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
2538b325af3cSJonathan Lemon }
2539b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 0);
2540b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 1);
2541b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 2);
2542b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 3);
2543b325af3cSJonathan Lemon 
2544b325af3cSJonathan Lemon static ssize_t
2545b325af3cSJonathan Lemon polarity_show(struct device *dev, struct device_attribute *attr,
2546b325af3cSJonathan Lemon 	      char *buf)
2547b325af3cSJonathan Lemon {
2548b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2549b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2550b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2551b325af3cSJonathan Lemon 
2552b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
2553b325af3cSJonathan Lemon }
2554b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 0);
2555b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 1);
2556b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 2);
2557b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 3);
2558b325af3cSJonathan Lemon 
2559b325af3cSJonathan Lemon static ssize_t
2560b325af3cSJonathan Lemon running_show(struct device *dev, struct device_attribute *attr, char *buf)
2561b325af3cSJonathan Lemon {
2562b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2563b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2564b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2565b325af3cSJonathan Lemon 
2566b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].running);
2567b325af3cSJonathan Lemon }
2568b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 0);
2569b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 1);
2570b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 2);
2571b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 3);
2572b325af3cSJonathan Lemon 
2573b325af3cSJonathan Lemon static ssize_t
2574b325af3cSJonathan Lemon start_show(struct device *dev, struct device_attribute *attr, char *buf)
2575b325af3cSJonathan Lemon {
2576b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2577b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2578b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2579b325af3cSJonathan Lemon 	struct timespec64 ts;
2580b325af3cSJonathan Lemon 
2581b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[i].start);
2582b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
2583b325af3cSJonathan Lemon }
2584b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 0);
2585b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 1);
2586b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 2);
2587b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 3);
2588b325af3cSJonathan Lemon 
2589773bda96SJonathan Lemon static ssize_t
25902407f5d6SJonathan Lemon seconds_store(struct device *dev, struct device_attribute *attr,
25912407f5d6SJonathan Lemon 	      const char *buf, size_t count)
25922407f5d6SJonathan Lemon {
25932407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
25942407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
25952407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
25962407f5d6SJonathan Lemon 	u32 val;
25972407f5d6SJonathan Lemon 	int err;
25982407f5d6SJonathan Lemon 
25992407f5d6SJonathan Lemon 	err = kstrtou32(buf, 0, &val);
26002407f5d6SJonathan Lemon 	if (err)
26012407f5d6SJonathan Lemon 		return err;
26022407f5d6SJonathan Lemon 	if (val > 0xff)
26032407f5d6SJonathan Lemon 		return -EINVAL;
26042407f5d6SJonathan Lemon 
26052407f5d6SJonathan Lemon 	if (val)
26062407f5d6SJonathan Lemon 		val = (val << 8) | 0x1;
26072407f5d6SJonathan Lemon 
26082407f5d6SJonathan Lemon 	iowrite32(val, &bp->freq_in[idx]->ctrl);
26092407f5d6SJonathan Lemon 
26102407f5d6SJonathan Lemon 	return count;
26112407f5d6SJonathan Lemon }
26122407f5d6SJonathan Lemon 
26132407f5d6SJonathan Lemon static ssize_t
26142407f5d6SJonathan Lemon seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
26152407f5d6SJonathan Lemon {
26162407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
26172407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
26182407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
26192407f5d6SJonathan Lemon 	u32 val;
26202407f5d6SJonathan Lemon 
26212407f5d6SJonathan Lemon 	val = ioread32(&bp->freq_in[idx]->ctrl);
26222407f5d6SJonathan Lemon 	if (val & 1)
26232407f5d6SJonathan Lemon 		val = (val >> 8) & 0xff;
26242407f5d6SJonathan Lemon 	else
26252407f5d6SJonathan Lemon 		val = 0;
26262407f5d6SJonathan Lemon 
26272407f5d6SJonathan Lemon 	return sysfs_emit(buf, "%u\n", val);
26282407f5d6SJonathan Lemon }
26292407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 0);
26302407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 1);
26312407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 2);
26322407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 3);
26332407f5d6SJonathan Lemon 
26342407f5d6SJonathan Lemon static ssize_t
26352407f5d6SJonathan Lemon frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
26362407f5d6SJonathan Lemon {
26372407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
26382407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
26392407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
26402407f5d6SJonathan Lemon 	u32 val;
26412407f5d6SJonathan Lemon 
26422407f5d6SJonathan Lemon 	val = ioread32(&bp->freq_in[idx]->status);
26432407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_ERROR)
26442407f5d6SJonathan Lemon 		return sysfs_emit(buf, "error\n");
26452407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_OVERRUN)
26462407f5d6SJonathan Lemon 		return sysfs_emit(buf, "overrun\n");
26472407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_VALID)
26482407f5d6SJonathan Lemon 		return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
26492407f5d6SJonathan Lemon 	return 0;
26502407f5d6SJonathan Lemon }
26512407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 0);
26522407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 1);
26532407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 2);
26542407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 3);
26552407f5d6SJonathan Lemon 
26562407f5d6SJonathan Lemon static ssize_t
2657773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
2658773bda96SJonathan Lemon {
2659773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2660773bda96SJonathan Lemon 
26610cfcdd1eSJonathan Lemon 	if (!bp->has_eeprom_data)
26620cfcdd1eSJonathan Lemon 		ptp_ocp_read_eeprom(bp);
2663773bda96SJonathan Lemon 
2664773bda96SJonathan Lemon 	return sysfs_emit(buf, "%pM\n", bp->serial);
2665773bda96SJonathan Lemon }
2666773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum);
2667773bda96SJonathan Lemon 
2668773bda96SJonathan Lemon static ssize_t
2669ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
2670773bda96SJonathan Lemon {
2671773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2672773bda96SJonathan Lemon 	ssize_t ret;
2673773bda96SJonathan Lemon 
2674ef0cfb34SJonathan Lemon 	if (bp->gnss_lost)
2675ef0cfb34SJonathan Lemon 		ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
2676773bda96SJonathan Lemon 	else
2677773bda96SJonathan Lemon 		ret = sysfs_emit(buf, "SYNC\n");
2678773bda96SJonathan Lemon 
2679773bda96SJonathan Lemon 	return ret;
2680773bda96SJonathan Lemon }
2681ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync);
2682773bda96SJonathan Lemon 
2683773bda96SJonathan Lemon static ssize_t
268489260d87SJonathan Lemon utc_tai_offset_show(struct device *dev,
268589260d87SJonathan Lemon 		    struct device_attribute *attr, char *buf)
268689260d87SJonathan Lemon {
268789260d87SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
268889260d87SJonathan Lemon 
268989260d87SJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
269089260d87SJonathan Lemon }
269189260d87SJonathan Lemon 
269289260d87SJonathan Lemon static ssize_t
269389260d87SJonathan Lemon utc_tai_offset_store(struct device *dev,
269489260d87SJonathan Lemon 		     struct device_attribute *attr,
269589260d87SJonathan Lemon 		     const char *buf, size_t count)
269689260d87SJonathan Lemon {
269789260d87SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
269889260d87SJonathan Lemon 	int err;
269989260d87SJonathan Lemon 	u32 val;
270089260d87SJonathan Lemon 
270189260d87SJonathan Lemon 	err = kstrtou32(buf, 0, &val);
270289260d87SJonathan Lemon 	if (err)
270389260d87SJonathan Lemon 		return err;
270489260d87SJonathan Lemon 
270589260d87SJonathan Lemon 	ptp_ocp_utc_distribute(bp, val);
270689260d87SJonathan Lemon 
270789260d87SJonathan Lemon 	return count;
270889260d87SJonathan Lemon }
270989260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset);
271089260d87SJonathan Lemon 
271189260d87SJonathan Lemon static ssize_t
27121acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev,
27131acffc6eSJonathan Lemon 		      struct device_attribute *attr, char *buf)
27141acffc6eSJonathan Lemon {
27151acffc6eSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
27161acffc6eSJonathan Lemon 
27171acffc6eSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
27181acffc6eSJonathan Lemon }
27191acffc6eSJonathan Lemon 
27201acffc6eSJonathan Lemon static ssize_t
27211acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev,
27221acffc6eSJonathan Lemon 		       struct device_attribute *attr,
27231acffc6eSJonathan Lemon 		       const char *buf, size_t count)
27241acffc6eSJonathan Lemon {
27251acffc6eSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
27261acffc6eSJonathan Lemon 	int err;
27271acffc6eSJonathan Lemon 	u32 val;
27281acffc6eSJonathan Lemon 
27291acffc6eSJonathan Lemon 	err = kstrtou32(buf, 0, &val);
27301acffc6eSJonathan Lemon 	if (err)
27311acffc6eSJonathan Lemon 		return err;
27321acffc6eSJonathan Lemon 
27331acffc6eSJonathan Lemon 	bp->ts_window_adjust = val;
27341acffc6eSJonathan Lemon 
27351acffc6eSJonathan Lemon 	return count;
27361acffc6eSJonathan Lemon }
27371acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust);
27381acffc6eSJonathan Lemon 
27391acffc6eSJonathan Lemon static ssize_t
2740d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
2741d14ee252SJonathan Lemon {
2742d14ee252SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2743d14ee252SJonathan Lemon 	u32 val;
2744d14ee252SJonathan Lemon 
2745d14ee252SJonathan Lemon 	val = ioread32(&bp->irig_out->ctrl);
2746d14ee252SJonathan Lemon 	val = (val >> 16) & 0x07;
2747d14ee252SJonathan Lemon 	return sysfs_emit(buf, "%d\n", val);
2748d14ee252SJonathan Lemon }
2749d14ee252SJonathan Lemon 
2750d14ee252SJonathan Lemon static ssize_t
2751d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev,
2752d14ee252SJonathan Lemon 		  struct device_attribute *attr,
2753d14ee252SJonathan Lemon 		  const char *buf, size_t count)
2754d14ee252SJonathan Lemon {
2755d14ee252SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2756d14ee252SJonathan Lemon 	unsigned long flags;
2757d14ee252SJonathan Lemon 	int err;
2758d14ee252SJonathan Lemon 	u32 reg;
2759d14ee252SJonathan Lemon 	u8 val;
2760d14ee252SJonathan Lemon 
2761d14ee252SJonathan Lemon 	err = kstrtou8(buf, 0, &val);
2762d14ee252SJonathan Lemon 	if (err)
2763d14ee252SJonathan Lemon 		return err;
2764d14ee252SJonathan Lemon 	if (val > 7)
2765d14ee252SJonathan Lemon 		return -EINVAL;
2766d14ee252SJonathan Lemon 
2767d14ee252SJonathan Lemon 	reg = ((val & 0x7) << 16);
2768d14ee252SJonathan Lemon 
2769d14ee252SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2770d14ee252SJonathan Lemon 	iowrite32(0, &bp->irig_out->ctrl);		/* disable */
2771d14ee252SJonathan Lemon 	iowrite32(reg, &bp->irig_out->ctrl);		/* change mode */
2772d14ee252SJonathan Lemon 	iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
2773d14ee252SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2774d14ee252SJonathan Lemon 
2775d14ee252SJonathan Lemon 	return count;
2776d14ee252SJonathan Lemon }
2777d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode);
2778d14ee252SJonathan Lemon 
2779d14ee252SJonathan Lemon static ssize_t
2780773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
2781773bda96SJonathan Lemon {
2782773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2783773bda96SJonathan Lemon 	const char *p;
2784773bda96SJonathan Lemon 	u32 select;
2785773bda96SJonathan Lemon 
2786773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
2787e1daf0ecSJonathan Lemon 	p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
2788773bda96SJonathan Lemon 
2789773bda96SJonathan Lemon 	return sysfs_emit(buf, "%s\n", p);
2790773bda96SJonathan Lemon }
2791773bda96SJonathan Lemon 
2792773bda96SJonathan Lemon static ssize_t
2793773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr,
2794773bda96SJonathan Lemon 		   const char *buf, size_t count)
2795773bda96SJonathan Lemon {
2796773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2797773bda96SJonathan Lemon 	unsigned long flags;
2798773bda96SJonathan Lemon 	int val;
2799773bda96SJonathan Lemon 
2800e1daf0ecSJonathan Lemon 	val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
2801773bda96SJonathan Lemon 	if (val < 0)
2802773bda96SJonathan Lemon 		return val;
2803773bda96SJonathan Lemon 
2804773bda96SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2805773bda96SJonathan Lemon 	iowrite32(val, &bp->reg->select);
2806773bda96SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2807773bda96SJonathan Lemon 
2808773bda96SJonathan Lemon 	return count;
2809773bda96SJonathan Lemon }
2810773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source);
2811773bda96SJonathan Lemon 
2812773bda96SJonathan Lemon static ssize_t
2813773bda96SJonathan Lemon available_clock_sources_show(struct device *dev,
2814773bda96SJonathan Lemon 			     struct device_attribute *attr, char *buf)
2815773bda96SJonathan Lemon {
2816e1daf0ecSJonathan Lemon 	return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
2817773bda96SJonathan Lemon }
2818773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources);
2819773bda96SJonathan Lemon 
28202f23f486SVadim Fedorenko static ssize_t
28212f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev,
28222f23f486SVadim Fedorenko 			struct device_attribute *attr, char *buf)
28232f23f486SVadim Fedorenko {
28242f23f486SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
28252f23f486SVadim Fedorenko 	u32 val;
28262f23f486SVadim Fedorenko 	int res;
28272f23f486SVadim Fedorenko 
28282f23f486SVadim Fedorenko 	val = ioread32(&bp->reg->status_drift);
28292f23f486SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
28302f23f486SVadim Fedorenko 	res *= (val & INT_MAX);
28312f23f486SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
28322f23f486SVadim Fedorenko }
28332f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift);
28342f23f486SVadim Fedorenko 
28352f23f486SVadim Fedorenko static ssize_t
28362f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev,
28372f23f486SVadim Fedorenko 			 struct device_attribute *attr, char *buf)
28382f23f486SVadim Fedorenko {
28392f23f486SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
28402f23f486SVadim Fedorenko 	u32 val;
28412f23f486SVadim Fedorenko 	int res;
28422f23f486SVadim Fedorenko 
28432f23f486SVadim Fedorenko 	val = ioread32(&bp->reg->status_offset);
28442f23f486SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
28452f23f486SVadim Fedorenko 	res *= (val & INT_MAX);
28462f23f486SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
28472f23f486SVadim Fedorenko }
28482f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset);
28492f23f486SVadim Fedorenko 
285044a412d1SVadim Fedorenko static ssize_t
285144a412d1SVadim Fedorenko tod_correction_show(struct device *dev,
285244a412d1SVadim Fedorenko 		    struct device_attribute *attr, char *buf)
285344a412d1SVadim Fedorenko {
285444a412d1SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
285544a412d1SVadim Fedorenko 	u32 val;
285644a412d1SVadim Fedorenko 	int res;
285744a412d1SVadim Fedorenko 
285844a412d1SVadim Fedorenko 	val = ioread32(&bp->tod->adj_sec);
285944a412d1SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
286044a412d1SVadim Fedorenko 	res *= (val & INT_MAX);
286144a412d1SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
286244a412d1SVadim Fedorenko }
286344a412d1SVadim Fedorenko 
286444a412d1SVadim Fedorenko static ssize_t
286544a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr,
286644a412d1SVadim Fedorenko 		     const char *buf, size_t count)
286744a412d1SVadim Fedorenko {
286844a412d1SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
286944a412d1SVadim Fedorenko 	unsigned long flags;
287044a412d1SVadim Fedorenko 	int err, res;
287144a412d1SVadim Fedorenko 	u32 val = 0;
287244a412d1SVadim Fedorenko 
287344a412d1SVadim Fedorenko 	err = kstrtos32(buf, 0, &res);
287444a412d1SVadim Fedorenko 	if (err)
287544a412d1SVadim Fedorenko 		return err;
287644a412d1SVadim Fedorenko 	if (res < 0) {
287744a412d1SVadim Fedorenko 		res *= -1;
287844a412d1SVadim Fedorenko 		val |= BIT(31);
287944a412d1SVadim Fedorenko 	}
288044a412d1SVadim Fedorenko 	val |= res;
288144a412d1SVadim Fedorenko 
288244a412d1SVadim Fedorenko 	spin_lock_irqsave(&bp->lock, flags);
288344a412d1SVadim Fedorenko 	iowrite32(val, &bp->tod->adj_sec);
288444a412d1SVadim Fedorenko 	spin_unlock_irqrestore(&bp->lock, flags);
288544a412d1SVadim Fedorenko 
288644a412d1SVadim Fedorenko 	return count;
288744a412d1SVadim Fedorenko }
288844a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction);
288944a412d1SVadim Fedorenko 
2890b325af3cSJonathan Lemon #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr)					\
2891b325af3cSJonathan Lemon 	static struct attribute *fb_timecard_signal##_nr##_attrs[] = {	\
2892b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_signal.attr.attr,		\
2893b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_duty.attr.attr,			\
2894b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_phase.attr.attr,		\
2895b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_period.attr.attr,		\
2896b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_polarity.attr.attr,		\
2897b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_running.attr.attr,		\
2898b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_start.attr.attr,		\
2899b325af3cSJonathan Lemon 		NULL,							\
2900b325af3cSJonathan Lemon 	}
2901b325af3cSJonathan Lemon 
2902b325af3cSJonathan Lemon #define DEVICE_SIGNAL_GROUP(_name, _nr)					\
2903b325af3cSJonathan Lemon 	_DEVICE_SIGNAL_GROUP_ATTRS(_nr);				\
2904b325af3cSJonathan Lemon 	static const struct attribute_group				\
2905b325af3cSJonathan Lemon 			fb_timecard_signal##_nr##_group = {		\
2906b325af3cSJonathan Lemon 		.name = #_name,						\
2907b325af3cSJonathan Lemon 		.attrs = fb_timecard_signal##_nr##_attrs,		\
2908b325af3cSJonathan Lemon }
2909b325af3cSJonathan Lemon 
2910b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen1, 0);
2911b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen2, 1);
2912b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen3, 2);
2913b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen4, 3);
2914b325af3cSJonathan Lemon 
29152407f5d6SJonathan Lemon #define _DEVICE_FREQ_GROUP_ATTRS(_nr)					\
29162407f5d6SJonathan Lemon 	static struct attribute *fb_timecard_freq##_nr##_attrs[] = {	\
29172407f5d6SJonathan Lemon 		&dev_attr_freq##_nr##_seconds.attr.attr,		\
29182407f5d6SJonathan Lemon 		&dev_attr_freq##_nr##_frequency.attr.attr,		\
29192407f5d6SJonathan Lemon 		NULL,							\
29202407f5d6SJonathan Lemon 	}
29212407f5d6SJonathan Lemon 
29222407f5d6SJonathan Lemon #define DEVICE_FREQ_GROUP(_name, _nr)					\
29232407f5d6SJonathan Lemon 	_DEVICE_FREQ_GROUP_ATTRS(_nr);					\
29242407f5d6SJonathan Lemon 	static const struct attribute_group				\
29252407f5d6SJonathan Lemon 			fb_timecard_freq##_nr##_group = {		\
29262407f5d6SJonathan Lemon 		.name = #_name,						\
29272407f5d6SJonathan Lemon 		.attrs = fb_timecard_freq##_nr##_attrs,			\
29282407f5d6SJonathan Lemon }
29292407f5d6SJonathan Lemon 
29302407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq1, 0);
29312407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq2, 1);
29322407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq3, 2);
29332407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq4, 3);
29342407f5d6SJonathan Lemon 
2935c205d53cSJonathan Lemon static struct attribute *fb_timecard_attrs[] = {
2936773bda96SJonathan Lemon 	&dev_attr_serialnum.attr,
2937ef0cfb34SJonathan Lemon 	&dev_attr_gnss_sync.attr,
2938773bda96SJonathan Lemon 	&dev_attr_clock_source.attr,
2939773bda96SJonathan Lemon 	&dev_attr_available_clock_sources.attr,
2940e1daf0ecSJonathan Lemon 	&dev_attr_sma1.attr,
2941e1daf0ecSJonathan Lemon 	&dev_attr_sma2.attr,
2942e1daf0ecSJonathan Lemon 	&dev_attr_sma3.attr,
2943e1daf0ecSJonathan Lemon 	&dev_attr_sma4.attr,
2944e1daf0ecSJonathan Lemon 	&dev_attr_available_sma_inputs.attr,
2945e1daf0ecSJonathan Lemon 	&dev_attr_available_sma_outputs.attr,
29462f23f486SVadim Fedorenko 	&dev_attr_clock_status_drift.attr,
29472f23f486SVadim Fedorenko 	&dev_attr_clock_status_offset.attr,
2948d14ee252SJonathan Lemon 	&dev_attr_irig_b_mode.attr,
294989260d87SJonathan Lemon 	&dev_attr_utc_tai_offset.attr,
29501acffc6eSJonathan Lemon 	&dev_attr_ts_window_adjust.attr,
295144a412d1SVadim Fedorenko 	&dev_attr_tod_correction.attr,
2952773bda96SJonathan Lemon 	NULL,
2953773bda96SJonathan Lemon };
2954c205d53cSJonathan Lemon static const struct attribute_group fb_timecard_group = {
2955c205d53cSJonathan Lemon 	.attrs = fb_timecard_attrs,
2956c205d53cSJonathan Lemon };
2957c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[] = {
2958c205d53cSJonathan Lemon 	{ .cap = OCP_CAP_BASIC,	    .group = &fb_timecard_group },
2959b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal0_group },
2960b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal1_group },
2961b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal2_group },
2962b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal3_group },
29632407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq0_group },
29642407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq1_group },
29652407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq2_group },
29662407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq3_group },
2967c205d53cSJonathan Lemon 	{ },
2968c205d53cSJonathan Lemon };
2969773bda96SJonathan Lemon 
2970a509a7c6SJonathan Lemon static void
2971a509a7c6SJonathan Lemon gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
2972a509a7c6SJonathan Lemon 	       const char *def)
2973f67bf662SJonathan Lemon {
2974a509a7c6SJonathan Lemon 	int i;
2975f67bf662SJonathan Lemon 
2976a509a7c6SJonathan Lemon 	for (i = 0; i < 4; i++) {
2977a509a7c6SJonathan Lemon 		if (bp->sma[i].mode != SMA_MODE_IN)
2978a509a7c6SJonathan Lemon 			continue;
2979a509a7c6SJonathan Lemon 		if (map[i][0] & (1 << bit)) {
2980a509a7c6SJonathan Lemon 			sprintf(buf, "sma%d", i + 1);
2981a509a7c6SJonathan Lemon 			return;
2982a509a7c6SJonathan Lemon 		}
2983a509a7c6SJonathan Lemon 	}
2984a509a7c6SJonathan Lemon 	if (!def)
2985a509a7c6SJonathan Lemon 		def = "----";
2986a509a7c6SJonathan Lemon 	strcpy(buf, def);
2987f67bf662SJonathan Lemon }
2988f67bf662SJonathan Lemon 
2989f67bf662SJonathan Lemon static void
2990a509a7c6SJonathan Lemon gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
2991f67bf662SJonathan Lemon {
2992f67bf662SJonathan Lemon 	char *ans = buf;
2993a509a7c6SJonathan Lemon 	int i;
2994f67bf662SJonathan Lemon 
2995a509a7c6SJonathan Lemon 	strcpy(ans, "----");
2996a509a7c6SJonathan Lemon 	for (i = 0; i < 4; i++) {
2997a509a7c6SJonathan Lemon 		if (bp->sma[i].mode != SMA_MODE_OUT)
2998a509a7c6SJonathan Lemon 			continue;
2999a509a7c6SJonathan Lemon 		if (map[i][1] & (1 << bit))
3000a509a7c6SJonathan Lemon 			ans += sprintf(ans, "sma%d ", i + 1);
3001a509a7c6SJonathan Lemon 	}
3002f67bf662SJonathan Lemon }
3003f67bf662SJonathan Lemon 
3004b325af3cSJonathan Lemon static void
3005b325af3cSJonathan Lemon _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
3006b325af3cSJonathan Lemon {
3007b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
3008b325af3cSJonathan Lemon 	struct ptp_ocp_signal *signal = &bp->signal[nr];
3009b325af3cSJonathan Lemon 	char label[8];
3010b325af3cSJonathan Lemon 	bool on;
3011b325af3cSJonathan Lemon 	u32 val;
3012b325af3cSJonathan Lemon 
3013b325af3cSJonathan Lemon 	if (!signal)
3014b325af3cSJonathan Lemon 		return;
3015b325af3cSJonathan Lemon 
3016b325af3cSJonathan Lemon 	on = signal->running;
301705fc65f3SJonathan Lemon 	sprintf(label, "GEN%d", nr + 1);
3018b325af3cSJonathan Lemon 	seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
3019b325af3cSJonathan Lemon 		   label, on ? " ON" : "OFF",
3020b325af3cSJonathan Lemon 		   signal->period, signal->duty, signal->phase,
3021b325af3cSJonathan Lemon 		   signal->polarity);
3022b325af3cSJonathan Lemon 
3023b325af3cSJonathan Lemon 	val = ioread32(&reg->enable);
3024b325af3cSJonathan Lemon 	seq_printf(s, " [%x", val);
3025b325af3cSJonathan Lemon 	val = ioread32(&reg->status);
3026b325af3cSJonathan Lemon 	seq_printf(s, " %x]", val);
3027b325af3cSJonathan Lemon 
3028b325af3cSJonathan Lemon 	seq_printf(s, " start:%llu\n", signal->start);
3029b325af3cSJonathan Lemon }
3030b325af3cSJonathan Lemon 
30312407f5d6SJonathan Lemon static void
30322407f5d6SJonathan Lemon _frequency_summary_show(struct seq_file *s, int nr,
30332407f5d6SJonathan Lemon 			struct frequency_reg __iomem *reg)
30342407f5d6SJonathan Lemon {
30352407f5d6SJonathan Lemon 	char label[8];
30362407f5d6SJonathan Lemon 	bool on;
30372407f5d6SJonathan Lemon 	u32 val;
30382407f5d6SJonathan Lemon 
30392407f5d6SJonathan Lemon 	if (!reg)
30402407f5d6SJonathan Lemon 		return;
30412407f5d6SJonathan Lemon 
304205fc65f3SJonathan Lemon 	sprintf(label, "FREQ%d", nr + 1);
30432407f5d6SJonathan Lemon 	val = ioread32(&reg->ctrl);
30442407f5d6SJonathan Lemon 	on = val & 1;
30452407f5d6SJonathan Lemon 	val = (val >> 8) & 0xff;
30462407f5d6SJonathan Lemon 	seq_printf(s, "%7s: %s, sec:%u",
30472407f5d6SJonathan Lemon 		   label,
30482407f5d6SJonathan Lemon 		   on ? " ON" : "OFF",
30492407f5d6SJonathan Lemon 		   val);
30502407f5d6SJonathan Lemon 
30512407f5d6SJonathan Lemon 	val = ioread32(&reg->status);
30522407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_ERROR)
30532407f5d6SJonathan Lemon 		seq_printf(s, ", error");
30542407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_OVERRUN)
30552407f5d6SJonathan Lemon 		seq_printf(s, ", overrun");
30562407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_VALID)
30572407f5d6SJonathan Lemon 		seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
30582407f5d6SJonathan Lemon 	seq_printf(s, "  reg:%x\n", val);
30592407f5d6SJonathan Lemon }
30602407f5d6SJonathan Lemon 
3061f67bf662SJonathan Lemon static int
3062f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data)
3063f67bf662SJonathan Lemon {
3064f67bf662SJonathan Lemon 	struct device *dev = s->private;
3065f67bf662SJonathan Lemon 	struct ptp_system_timestamp sts;
3066f67bf662SJonathan Lemon 	struct ts_reg __iomem *ts_reg;
3067f67bf662SJonathan Lemon 	struct timespec64 ts;
3068f67bf662SJonathan Lemon 	struct ptp_ocp *bp;
30692b341f75SJonathan Lemon 	u16 sma_val[4][2];
3070a509a7c6SJonathan Lemon 	char *src, *buf;
30712b341f75SJonathan Lemon 	u32 ctrl, val;
3072a62a56d0SJonathan Lemon 	bool on, map;
3073b325af3cSJonathan Lemon 	int i;
3074f67bf662SJonathan Lemon 
3075f67bf662SJonathan Lemon 	buf = (char *)__get_free_page(GFP_KERNEL);
3076f67bf662SJonathan Lemon 	if (!buf)
3077f67bf662SJonathan Lemon 		return -ENOMEM;
3078f67bf662SJonathan Lemon 
3079f67bf662SJonathan Lemon 	bp = dev_get_drvdata(dev);
3080f67bf662SJonathan Lemon 
3081f67bf662SJonathan Lemon 	seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
308261fd7ac2SJonathan Lemon 	if (bp->gnss_port != -1)
308361fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port);
308461fd7ac2SJonathan Lemon 	if (bp->gnss2_port != -1)
308561fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port);
308661fd7ac2SJonathan Lemon 	if (bp->mac_port != -1)
308761fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port);
308861fd7ac2SJonathan Lemon 	if (bp->nmea_port != -1)
308961fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port);
3090f67bf662SJonathan Lemon 
3091a509a7c6SJonathan Lemon 	memset(sma_val, 0xff, sizeof(sma_val));
3092a509a7c6SJonathan Lemon 	if (bp->sma_map1) {
3093a509a7c6SJonathan Lemon 		u32 reg;
3094a509a7c6SJonathan Lemon 
3095a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio1);
3096a509a7c6SJonathan Lemon 		sma_val[0][0] = reg & 0xffff;
3097a509a7c6SJonathan Lemon 		sma_val[1][0] = reg >> 16;
3098a509a7c6SJonathan Lemon 
3099a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio2);
3100a509a7c6SJonathan Lemon 		sma_val[2][1] = reg & 0xffff;
3101a509a7c6SJonathan Lemon 		sma_val[3][1] = reg >> 16;
3102a509a7c6SJonathan Lemon 
3103a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map2->gpio1);
3104a509a7c6SJonathan Lemon 		sma_val[2][0] = reg & 0xffff;
3105a509a7c6SJonathan Lemon 		sma_val[3][0] = reg >> 16;
3106a509a7c6SJonathan Lemon 
3107a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map2->gpio2);
3108a509a7c6SJonathan Lemon 		sma_val[0][1] = reg & 0xffff;
3109a509a7c6SJonathan Lemon 		sma_val[1][1] = reg >> 16;
3110a509a7c6SJonathan Lemon 	}
3111a509a7c6SJonathan Lemon 
3112f67bf662SJonathan Lemon 	sma1_show(dev, NULL, buf);
3113a509a7c6SJonathan Lemon 	seq_printf(s, "   sma1: %04x,%04x %s",
3114a509a7c6SJonathan Lemon 		   sma_val[0][0], sma_val[0][1], buf);
3115f67bf662SJonathan Lemon 
3116f67bf662SJonathan Lemon 	sma2_show(dev, NULL, buf);
3117a509a7c6SJonathan Lemon 	seq_printf(s, "   sma2: %04x,%04x %s",
3118a509a7c6SJonathan Lemon 		   sma_val[1][0], sma_val[1][1], buf);
3119f67bf662SJonathan Lemon 
3120f67bf662SJonathan Lemon 	sma3_show(dev, NULL, buf);
3121a509a7c6SJonathan Lemon 	seq_printf(s, "   sma3: %04x,%04x %s",
3122a509a7c6SJonathan Lemon 		   sma_val[2][0], sma_val[2][1], buf);
3123f67bf662SJonathan Lemon 
3124f67bf662SJonathan Lemon 	sma4_show(dev, NULL, buf);
3125a509a7c6SJonathan Lemon 	seq_printf(s, "   sma4: %04x,%04x %s",
3126a509a7c6SJonathan Lemon 		   sma_val[3][0], sma_val[3][1], buf);
3127f67bf662SJonathan Lemon 
3128f67bf662SJonathan Lemon 	if (bp->ts0) {
3129f67bf662SJonathan Lemon 		ts_reg = bp->ts0->mem;
3130f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3131be69087cSJonathan Lemon 		src = "GNSS1";
3132f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS0",
3133f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", src);
3134f67bf662SJonathan Lemon 	}
3135f67bf662SJonathan Lemon 
3136f67bf662SJonathan Lemon 	if (bp->ts1) {
3137f67bf662SJonathan Lemon 		ts_reg = bp->ts1->mem;
3138f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3139a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 2, NULL);
3140f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS1",
3141a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", buf);
3142f67bf662SJonathan Lemon 	}
3143f67bf662SJonathan Lemon 
3144f67bf662SJonathan Lemon 	if (bp->ts2) {
3145f67bf662SJonathan Lemon 		ts_reg = bp->ts2->mem;
3146f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3147a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 3, NULL);
3148f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS2",
3149a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", buf);
3150f67bf662SJonathan Lemon 	}
3151f67bf662SJonathan Lemon 
31520fa3ff7eSJonathan Lemon 	if (bp->ts3) {
31530fa3ff7eSJonathan Lemon 		ts_reg = bp->ts3->mem;
31540fa3ff7eSJonathan Lemon 		on = ioread32(&ts_reg->enable);
31550fa3ff7eSJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 6, NULL);
31560fa3ff7eSJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS3",
31570fa3ff7eSJonathan Lemon 			   on ? " ON" : "OFF", buf);
31580fa3ff7eSJonathan Lemon 	}
31590fa3ff7eSJonathan Lemon 
31600fa3ff7eSJonathan Lemon 	if (bp->ts4) {
31610fa3ff7eSJonathan Lemon 		ts_reg = bp->ts4->mem;
31620fa3ff7eSJonathan Lemon 		on = ioread32(&ts_reg->enable);
31630fa3ff7eSJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 7, NULL);
31640fa3ff7eSJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS4",
31650fa3ff7eSJonathan Lemon 			   on ? " ON" : "OFF", buf);
31660fa3ff7eSJonathan Lemon 	}
31670fa3ff7eSJonathan Lemon 
3168a62a56d0SJonathan Lemon 	if (bp->pps) {
3169a62a56d0SJonathan Lemon 		ts_reg = bp->pps->mem;
3170a62a56d0SJonathan Lemon 		src = "PHC";
3171a62a56d0SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3172a62a56d0SJonathan Lemon 		map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
31730fa3ff7eSJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS5",
31741a575cdeSNathan Chancellor 			   on && map ? " ON" : "OFF", src);
3175a62a56d0SJonathan Lemon 
3176a62a56d0SJonathan Lemon 		map = !!(bp->pps_req_map & OCP_REQ_PPS);
3177a62a56d0SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "PPS",
31781a575cdeSNathan Chancellor 			   on && map ? " ON" : "OFF", src);
3179a62a56d0SJonathan Lemon 	}
3180a62a56d0SJonathan Lemon 
3181b325af3cSJonathan Lemon 	if (bp->fw_cap & OCP_CAP_SIGNAL)
3182b325af3cSJonathan Lemon 		for (i = 0; i < 4; i++)
3183b325af3cSJonathan Lemon 			_signal_summary_show(s, bp, i);
3184b325af3cSJonathan Lemon 
31852407f5d6SJonathan Lemon 	if (bp->fw_cap & OCP_CAP_FREQ)
31862407f5d6SJonathan Lemon 		for (i = 0; i < 4; i++)
31872407f5d6SJonathan Lemon 			_frequency_summary_show(s, i, bp->freq_in[i]);
31882407f5d6SJonathan Lemon 
3189f67bf662SJonathan Lemon 	if (bp->irig_out) {
3190f67bf662SJonathan Lemon 		ctrl = ioread32(&bp->irig_out->ctrl);
3191f67bf662SJonathan Lemon 		on = ctrl & IRIG_M_CTRL_ENABLE;
3192f67bf662SJonathan Lemon 		val = ioread32(&bp->irig_out->status);
3193a509a7c6SJonathan Lemon 		gpio_output_map(buf, bp, sma_val, 4);
3194f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
3195f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", val, (ctrl >> 16), buf);
3196f67bf662SJonathan Lemon 	}
3197f67bf662SJonathan Lemon 
3198f67bf662SJonathan Lemon 	if (bp->irig_in) {
3199f67bf662SJonathan Lemon 		on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
3200f67bf662SJonathan Lemon 		val = ioread32(&bp->irig_in->status);
3201a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 4, NULL);
3202f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
3203a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3204f67bf662SJonathan Lemon 	}
3205f67bf662SJonathan Lemon 
3206f67bf662SJonathan Lemon 	if (bp->dcf_out) {
3207f67bf662SJonathan Lemon 		on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
3208f67bf662SJonathan Lemon 		val = ioread32(&bp->dcf_out->status);
3209a509a7c6SJonathan Lemon 		gpio_output_map(buf, bp, sma_val, 5);
3210f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
3211f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3212f67bf662SJonathan Lemon 	}
3213f67bf662SJonathan Lemon 
3214f67bf662SJonathan Lemon 	if (bp->dcf_in) {
3215f67bf662SJonathan Lemon 		on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
3216f67bf662SJonathan Lemon 		val = ioread32(&bp->dcf_in->status);
3217a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 5, NULL);
3218f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
3219a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3220f67bf662SJonathan Lemon 	}
3221f67bf662SJonathan Lemon 
3222e3516bb4SJonathan Lemon 	if (bp->nmea_out) {
3223e3516bb4SJonathan Lemon 		on = ioread32(&bp->nmea_out->ctrl) & 1;
3224e3516bb4SJonathan Lemon 		val = ioread32(&bp->nmea_out->status);
3225e3516bb4SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
3226e3516bb4SJonathan Lemon 			   on ? " ON" : "OFF", val);
3227e3516bb4SJonathan Lemon 	}
3228e3516bb4SJonathan Lemon 
3229f67bf662SJonathan Lemon 	/* compute src for PPS1, used below. */
3230f67bf662SJonathan Lemon 	if (bp->pps_select) {
3231f67bf662SJonathan Lemon 		val = ioread32(&bp->pps_select->gpio1);
3232a509a7c6SJonathan Lemon 		src = &buf[80];
3233f67bf662SJonathan Lemon 		if (val & 0x01)
3234a509a7c6SJonathan Lemon 			gpio_input_map(src, bp, sma_val, 0, NULL);
3235f67bf662SJonathan Lemon 		else if (val & 0x02)
3236f67bf662SJonathan Lemon 			src = "MAC";
3237f67bf662SJonathan Lemon 		else if (val & 0x04)
3238be69087cSJonathan Lemon 			src = "GNSS1";
3239f67bf662SJonathan Lemon 		else
3240f67bf662SJonathan Lemon 			src = "----";
3241f67bf662SJonathan Lemon 	} else {
3242f67bf662SJonathan Lemon 		src = "?";
3243f67bf662SJonathan Lemon 	}
3244f67bf662SJonathan Lemon 
3245f67bf662SJonathan Lemon 	/* assumes automatic switchover/selection */
3246f67bf662SJonathan Lemon 	val = ioread32(&bp->reg->select);
3247f67bf662SJonathan Lemon 	switch (val >> 16) {
3248f67bf662SJonathan Lemon 	case 0:
3249f67bf662SJonathan Lemon 		sprintf(buf, "----");
3250f67bf662SJonathan Lemon 		break;
3251f67bf662SJonathan Lemon 	case 2:
3252f67bf662SJonathan Lemon 		sprintf(buf, "IRIG");
3253f67bf662SJonathan Lemon 		break;
3254f67bf662SJonathan Lemon 	case 3:
3255f67bf662SJonathan Lemon 		sprintf(buf, "%s via PPS1", src);
3256f67bf662SJonathan Lemon 		break;
3257f67bf662SJonathan Lemon 	case 6:
3258f67bf662SJonathan Lemon 		sprintf(buf, "DCF");
3259f67bf662SJonathan Lemon 		break;
3260f67bf662SJonathan Lemon 	default:
3261f67bf662SJonathan Lemon 		strcpy(buf, "unknown");
3262f67bf662SJonathan Lemon 		break;
3263f67bf662SJonathan Lemon 	}
3264f67bf662SJonathan Lemon 	val = ioread32(&bp->reg->status);
3265f67bf662SJonathan Lemon 	seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
3266f67bf662SJonathan Lemon 		   val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced");
3267f67bf662SJonathan Lemon 
3268f67bf662SJonathan Lemon 	/* reuses PPS1 src from earlier */
3269f67bf662SJonathan Lemon 	seq_printf(s, "MAC PPS1 src: %s\n", src);
3270f67bf662SJonathan Lemon 
3271a509a7c6SJonathan Lemon 	gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
3272a509a7c6SJonathan Lemon 	seq_printf(s, "MAC PPS2 src: %s\n", buf);
3273f67bf662SJonathan Lemon 
3274f67bf662SJonathan Lemon 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
3275f67bf662SJonathan Lemon 		struct timespec64 sys_ts;
3276f67bf662SJonathan Lemon 		s64 pre_ns, post_ns, ns;
3277f67bf662SJonathan Lemon 
3278f67bf662SJonathan Lemon 		pre_ns = timespec64_to_ns(&sts.pre_ts);
3279f67bf662SJonathan Lemon 		post_ns = timespec64_to_ns(&sts.post_ts);
3280f67bf662SJonathan Lemon 		ns = (pre_ns + post_ns) / 2;
3281f67bf662SJonathan Lemon 		ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
3282f67bf662SJonathan Lemon 		sys_ts = ns_to_timespec64(ns);
3283f67bf662SJonathan Lemon 
3284f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC",
3285f67bf662SJonathan Lemon 			   ts.tv_sec, ts.tv_nsec, &ts);
3286f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS",
3287f67bf662SJonathan Lemon 			   sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts,
3288f67bf662SJonathan Lemon 			   bp->utc_tai_offset);
3289f67bf662SJonathan Lemon 		seq_printf(s, "%7s: PHC:SYS offset: %lld  window: %lld\n", "",
3290f67bf662SJonathan Lemon 			   timespec64_to_ns(&ts) - ns,
3291f67bf662SJonathan Lemon 			   post_ns - pre_ns);
3292f67bf662SJonathan Lemon 	}
3293f67bf662SJonathan Lemon 
3294f67bf662SJonathan Lemon 	free_page((unsigned long)buf);
3295f67bf662SJonathan Lemon 	return 0;
3296f67bf662SJonathan Lemon }
3297f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
3298f67bf662SJonathan Lemon 
32999f492c4cSVadim Fedorenko static int
33009f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data)
33019f492c4cSVadim Fedorenko {
33029f492c4cSVadim Fedorenko 	struct device *dev = s->private;
33039f492c4cSVadim Fedorenko 	struct ptp_ocp *bp;
33049f492c4cSVadim Fedorenko 	u32 val;
33059f492c4cSVadim Fedorenko 	int idx;
33069f492c4cSVadim Fedorenko 
33079f492c4cSVadim Fedorenko 	bp = dev_get_drvdata(dev);
33089f492c4cSVadim Fedorenko 
33099f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->ctrl);
33109f492c4cSVadim Fedorenko 	if (!(val & TOD_CTRL_ENABLE)) {
33119f492c4cSVadim Fedorenko 		seq_printf(s, "TOD Slave disabled\n");
33129f492c4cSVadim Fedorenko 		return 0;
33139f492c4cSVadim Fedorenko 	}
33149f492c4cSVadim Fedorenko 	seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
33159f492c4cSVadim Fedorenko 
33169f492c4cSVadim Fedorenko 	idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
33179f492c4cSVadim Fedorenko 	idx += (val >> 16) & 3;
33189f492c4cSVadim Fedorenko 	seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
33199f492c4cSVadim Fedorenko 
33209f492c4cSVadim Fedorenko 	idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
33219f492c4cSVadim Fedorenko 	seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
33229f492c4cSVadim Fedorenko 
33239f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->version);
33249f492c4cSVadim Fedorenko 	seq_printf(s, "TOD Version %d.%d.%d\n",
33259f492c4cSVadim Fedorenko 		val >> 24, (val >> 16) & 0xff, val & 0xffff);
33269f492c4cSVadim Fedorenko 
33279f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->status);
33289f492c4cSVadim Fedorenko 	seq_printf(s, "Status register: 0x%08X\n", val);
33299f492c4cSVadim Fedorenko 
33309f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->adj_sec);
33319f492c4cSVadim Fedorenko 	idx = (val & ~INT_MAX) ? -1 : 1;
33329f492c4cSVadim Fedorenko 	idx *= (val & INT_MAX);
33339f492c4cSVadim Fedorenko 	seq_printf(s, "Correction seconds: %d\n", idx);
33349f492c4cSVadim Fedorenko 
33359f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->utc_status);
33369f492c4cSVadim Fedorenko 	seq_printf(s, "UTC status register: 0x%08X\n", val);
33379f492c4cSVadim Fedorenko 	seq_printf(s, "UTC offset: %d  valid:%d\n",
33389f492c4cSVadim Fedorenko 		val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
33399f492c4cSVadim Fedorenko 	seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
33409f492c4cSVadim Fedorenko 		val & TOD_STATUS_LEAP_VALID ? 1 : 0,
33419f492c4cSVadim Fedorenko 		val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
33429f492c4cSVadim Fedorenko 
33439f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->leap);
33449f492c4cSVadim Fedorenko 	seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
33459f492c4cSVadim Fedorenko 
33469f492c4cSVadim Fedorenko 	return 0;
33479f492c4cSVadim Fedorenko }
33489f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
33499f492c4cSVadim Fedorenko 
3350f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root;
3351f67bf662SJonathan Lemon 
3352f67bf662SJonathan Lemon static void
3353f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
3354f67bf662SJonathan Lemon {
3355f67bf662SJonathan Lemon 	struct dentry *d;
3356f67bf662SJonathan Lemon 
3357f67bf662SJonathan Lemon 	d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
3358f67bf662SJonathan Lemon 	bp->debug_root = d;
3359f67bf662SJonathan Lemon 	debugfs_create_file("summary", 0444, bp->debug_root,
3360f67bf662SJonathan Lemon 			    &bp->dev, &ptp_ocp_summary_fops);
33619f492c4cSVadim Fedorenko 	if (bp->tod)
33629f492c4cSVadim Fedorenko 		debugfs_create_file("tod_status", 0444, bp->debug_root,
33639f492c4cSVadim Fedorenko 				    &bp->dev, &ptp_ocp_tod_status_fops);
3364f67bf662SJonathan Lemon }
3365f67bf662SJonathan Lemon 
3366f67bf662SJonathan Lemon static void
3367f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
3368f67bf662SJonathan Lemon {
3369f67bf662SJonathan Lemon 	debugfs_remove_recursive(bp->debug_root);
3370f67bf662SJonathan Lemon }
3371f67bf662SJonathan Lemon 
3372f67bf662SJonathan Lemon static void
3373f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void)
3374f67bf662SJonathan Lemon {
3375f67bf662SJonathan Lemon 	ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
3376f67bf662SJonathan Lemon }
3377f67bf662SJonathan Lemon 
3378f67bf662SJonathan Lemon static void
3379f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void)
3380f67bf662SJonathan Lemon {
3381f67bf662SJonathan Lemon 	debugfs_remove_recursive(ptp_ocp_debugfs_root);
3382f67bf662SJonathan Lemon }
3383f67bf662SJonathan Lemon 
3384773bda96SJonathan Lemon static void
3385773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev)
3386773bda96SJonathan Lemon {
3387773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3388773bda96SJonathan Lemon 
3389773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
3390773bda96SJonathan Lemon 	idr_remove(&ptp_ocp_idr, bp->id);
3391773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
3392773bda96SJonathan Lemon }
3393773bda96SJonathan Lemon 
3394773bda96SJonathan Lemon static int
3395773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
3396773bda96SJonathan Lemon {
3397773bda96SJonathan Lemon 	int err;
3398773bda96SJonathan Lemon 
3399773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
3400773bda96SJonathan Lemon 	err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
3401773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
3402773bda96SJonathan Lemon 	if (err < 0) {
3403773bda96SJonathan Lemon 		dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
3404773bda96SJonathan Lemon 		return err;
3405773bda96SJonathan Lemon 	}
3406773bda96SJonathan Lemon 	bp->id = err;
3407773bda96SJonathan Lemon 
3408773bda96SJonathan Lemon 	bp->ptp_info = ptp_ocp_clock_info;
3409773bda96SJonathan Lemon 	spin_lock_init(&bp->lock);
3410ef0cfb34SJonathan Lemon 	bp->gnss_port = -1;
341171d7e085SJonathan Lemon 	bp->gnss2_port = -1;
3412773bda96SJonathan Lemon 	bp->mac_port = -1;
3413e3516bb4SJonathan Lemon 	bp->nmea_port = -1;
3414773bda96SJonathan Lemon 	bp->pdev = pdev;
3415773bda96SJonathan Lemon 
3416773bda96SJonathan Lemon 	device_initialize(&bp->dev);
3417773bda96SJonathan Lemon 	dev_set_name(&bp->dev, "ocp%d", bp->id);
3418773bda96SJonathan Lemon 	bp->dev.class = &timecard_class;
3419773bda96SJonathan Lemon 	bp->dev.parent = &pdev->dev;
3420773bda96SJonathan Lemon 	bp->dev.release = ptp_ocp_dev_release;
3421773bda96SJonathan Lemon 	dev_set_drvdata(&bp->dev, bp);
3422773bda96SJonathan Lemon 
3423773bda96SJonathan Lemon 	err = device_add(&bp->dev);
3424773bda96SJonathan Lemon 	if (err) {
3425773bda96SJonathan Lemon 		dev_err(&bp->dev, "device add failed: %d\n", err);
3426773bda96SJonathan Lemon 		goto out;
3427773bda96SJonathan Lemon 	}
3428773bda96SJonathan Lemon 
3429773bda96SJonathan Lemon 	pci_set_drvdata(pdev, bp);
3430773bda96SJonathan Lemon 
3431773bda96SJonathan Lemon 	return 0;
3432773bda96SJonathan Lemon 
3433773bda96SJonathan Lemon out:
3434773bda96SJonathan Lemon 	ptp_ocp_dev_release(&bp->dev);
3435d12f23faSJonathan Lemon 	put_device(&bp->dev);
3436773bda96SJonathan Lemon 	return err;
3437773bda96SJonathan Lemon }
3438773bda96SJonathan Lemon 
3439773bda96SJonathan Lemon static void
3440773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
3441773bda96SJonathan Lemon {
3442773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
3443773bda96SJonathan Lemon 
3444773bda96SJonathan Lemon 	if (sysfs_create_link(&dev->kobj, &child->kobj, link))
3445773bda96SJonathan Lemon 		dev_err(dev, "%s symlink failed\n", link);
3446773bda96SJonathan Lemon }
3447773bda96SJonathan Lemon 
3448773bda96SJonathan Lemon static void
3449773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
3450773bda96SJonathan Lemon {
3451773bda96SJonathan Lemon 	struct device *dev, *child;
3452773bda96SJonathan Lemon 
3453773bda96SJonathan Lemon 	dev = &bp->pdev->dev;
3454773bda96SJonathan Lemon 
3455773bda96SJonathan Lemon 	child = device_find_child_by_name(dev, name);
3456773bda96SJonathan Lemon 	if (!child) {
3457773bda96SJonathan Lemon 		dev_err(dev, "Could not find device %s\n", name);
3458773bda96SJonathan Lemon 		return;
3459773bda96SJonathan Lemon 	}
3460773bda96SJonathan Lemon 
3461773bda96SJonathan Lemon 	ptp_ocp_symlink(bp, child, link);
3462773bda96SJonathan Lemon 	put_device(child);
3463773bda96SJonathan Lemon }
3464773bda96SJonathan Lemon 
3465773bda96SJonathan Lemon static int
3466773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp)
3467773bda96SJonathan Lemon {
3468773bda96SJonathan Lemon 	struct pps_device *pps;
3469773bda96SJonathan Lemon 	char buf[32];
3470773bda96SJonathan Lemon 
3471ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1) {
3472ef0cfb34SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->gnss_port);
3473ef0cfb34SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyGNSS");
3474773bda96SJonathan Lemon 	}
347571d7e085SJonathan Lemon 	if (bp->gnss2_port != -1) {
347671d7e085SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->gnss2_port);
347771d7e085SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyGNSS2");
347871d7e085SJonathan Lemon 	}
3479773bda96SJonathan Lemon 	if (bp->mac_port != -1) {
3480773bda96SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->mac_port);
3481773bda96SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyMAC");
3482773bda96SJonathan Lemon 	}
3483e3516bb4SJonathan Lemon 	if (bp->nmea_port != -1) {
3484e3516bb4SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->nmea_port);
3485e3516bb4SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyNMEA");
3486e3516bb4SJonathan Lemon 	}
3487773bda96SJonathan Lemon 	sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
3488773bda96SJonathan Lemon 	ptp_ocp_link_child(bp, buf, "ptp");
3489773bda96SJonathan Lemon 
3490773bda96SJonathan Lemon 	pps = pps_lookup_dev(bp->ptp);
3491773bda96SJonathan Lemon 	if (pps)
3492773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, pps->dev, "pps");
3493773bda96SJonathan Lemon 
3494f67bf662SJonathan Lemon 	ptp_ocp_debugfs_add_device(bp);
3495f67bf662SJonathan Lemon 
3496773bda96SJonathan Lemon 	return 0;
3497773bda96SJonathan Lemon }
3498773bda96SJonathan Lemon 
3499773bda96SJonathan Lemon static void
3500065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp)
3501065efcc5SJonathan Lemon {
3502065efcc5SJonathan Lemon 	struct timespec64 ts;
3503065efcc5SJonathan Lemon 	u32 version, select;
3504065efcc5SJonathan Lemon 	bool sync;
3505065efcc5SJonathan Lemon 
3506065efcc5SJonathan Lemon 	version = ioread32(&bp->reg->version);
3507065efcc5SJonathan Lemon 	select = ioread32(&bp->reg->select);
3508065efcc5SJonathan Lemon 	dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
3509065efcc5SJonathan Lemon 		 version >> 24, (version >> 16) & 0xff, version & 0xffff,
3510065efcc5SJonathan Lemon 		 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
3511065efcc5SJonathan Lemon 		 ptp_clock_index(bp->ptp));
3512065efcc5SJonathan Lemon 
3513065efcc5SJonathan Lemon 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
3514065efcc5SJonathan Lemon 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
3515065efcc5SJonathan Lemon 		dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
3516065efcc5SJonathan Lemon 			 ts.tv_sec, ts.tv_nsec,
3517065efcc5SJonathan Lemon 			 sync ? "in-sync" : "UNSYNCED");
3518065efcc5SJonathan Lemon }
3519065efcc5SJonathan Lemon 
3520065efcc5SJonathan Lemon static void
3521065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
3522065efcc5SJonathan Lemon {
3523065efcc5SJonathan Lemon 	if (port != -1)
3524065efcc5SJonathan Lemon 		dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
3525065efcc5SJonathan Lemon }
3526065efcc5SJonathan Lemon 
3527065efcc5SJonathan Lemon static void
3528065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp)
3529773bda96SJonathan Lemon {
3530e3516bb4SJonathan Lemon 	static int nmea_baud[] = {
3531e3516bb4SJonathan Lemon 		1200, 2400, 4800, 9600, 19200, 38400,
3532e3516bb4SJonathan Lemon 		57600, 115200, 230400, 460800, 921600,
3533e3516bb4SJonathan Lemon 		1000000, 2000000
3534e3516bb4SJonathan Lemon 	};
3535773bda96SJonathan Lemon 	struct device *dev = &bp->pdev->dev;
3536e3516bb4SJonathan Lemon 	u32 reg;
3537773bda96SJonathan Lemon 
3538065efcc5SJonathan Lemon 	ptp_ocp_phc_info(bp);
3539065efcc5SJonathan Lemon 
3540065efcc5SJonathan Lemon 	ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200);
354171d7e085SJonathan Lemon 	ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200);
3542065efcc5SJonathan Lemon 	ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600);
3543e3516bb4SJonathan Lemon 	if (bp->nmea_out && bp->nmea_port != -1) {
3544e3516bb4SJonathan Lemon 		int baud = -1;
3545e3516bb4SJonathan Lemon 
3546e3516bb4SJonathan Lemon 		reg = ioread32(&bp->nmea_out->uart_baud);
3547e3516bb4SJonathan Lemon 		if (reg < ARRAY_SIZE(nmea_baud))
3548e3516bb4SJonathan Lemon 			baud = nmea_baud[reg];
3549e3516bb4SJonathan Lemon 		ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud);
3550e3516bb4SJonathan Lemon 	}
3551773bda96SJonathan Lemon }
3552773bda96SJonathan Lemon 
3553773bda96SJonathan Lemon static void
3554773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
3555773bda96SJonathan Lemon {
3556773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
3557773bda96SJonathan Lemon 
3558ef0cfb34SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyGNSS");
3559773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyMAC");
3560773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ptp");
3561773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "pps");
3562773bda96SJonathan Lemon }
3563773bda96SJonathan Lemon 
3564773bda96SJonathan Lemon static void
3565773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp)
3566773bda96SJonathan Lemon {
3567b325af3cSJonathan Lemon 	int i;
3568b325af3cSJonathan Lemon 
3569f67bf662SJonathan Lemon 	ptp_ocp_debugfs_remove_device(bp);
3570773bda96SJonathan Lemon 	ptp_ocp_detach_sysfs(bp);
3571c2239294SJonathan Lemon 	ptp_ocp_attr_group_del(bp);
3572773bda96SJonathan Lemon 	if (timer_pending(&bp->watchdog))
3573773bda96SJonathan Lemon 		del_timer_sync(&bp->watchdog);
3574773bda96SJonathan Lemon 	if (bp->ts0)
3575773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts0);
3576773bda96SJonathan Lemon 	if (bp->ts1)
3577773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts1);
3578dcf61469SJonathan Lemon 	if (bp->ts2)
3579dcf61469SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts2);
35800fa3ff7eSJonathan Lemon 	if (bp->ts3)
35810fa3ff7eSJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts3);
35820fa3ff7eSJonathan Lemon 	if (bp->ts4)
35830fa3ff7eSJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts4);
3584773bda96SJonathan Lemon 	if (bp->pps)
3585773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->pps);
3586b325af3cSJonathan Lemon 	for (i = 0; i < 4; i++)
3587b325af3cSJonathan Lemon 		if (bp->signal_out[i])
3588b325af3cSJonathan Lemon 			ptp_ocp_unregister_ext(bp->signal_out[i]);
3589ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1)
3590ef0cfb34SJonathan Lemon 		serial8250_unregister_port(bp->gnss_port);
359171d7e085SJonathan Lemon 	if (bp->gnss2_port != -1)
359271d7e085SJonathan Lemon 		serial8250_unregister_port(bp->gnss2_port);
3593773bda96SJonathan Lemon 	if (bp->mac_port != -1)
3594773bda96SJonathan Lemon 		serial8250_unregister_port(bp->mac_port);
3595e3516bb4SJonathan Lemon 	if (bp->nmea_port != -1)
3596e3516bb4SJonathan Lemon 		serial8250_unregister_port(bp->nmea_port);
3597773bda96SJonathan Lemon 	if (bp->spi_flash)
3598773bda96SJonathan Lemon 		platform_device_unregister(bp->spi_flash);
3599773bda96SJonathan Lemon 	if (bp->i2c_ctrl)
3600773bda96SJonathan Lemon 		platform_device_unregister(bp->i2c_ctrl);
3601773bda96SJonathan Lemon 	if (bp->i2c_clk)
3602773bda96SJonathan Lemon 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
3603773bda96SJonathan Lemon 	if (bp->n_irqs)
3604773bda96SJonathan Lemon 		pci_free_irq_vectors(bp->pdev);
3605773bda96SJonathan Lemon 	if (bp->ptp)
3606773bda96SJonathan Lemon 		ptp_clock_unregister(bp->ptp);
36071aa66a3aSJonathan Lemon 	kfree(bp->ptp_info.pin_config);
3608773bda96SJonathan Lemon 	device_unregister(&bp->dev);
3609773bda96SJonathan Lemon }
3610773bda96SJonathan Lemon 
3611a7e1abadSJonathan Lemon static int
3612a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3613a7e1abadSJonathan Lemon {
3614773bda96SJonathan Lemon 	struct devlink *devlink;
3615a7e1abadSJonathan Lemon 	struct ptp_ocp *bp;
3616a7e1abadSJonathan Lemon 	int err;
3617a7e1abadSJonathan Lemon 
3618919d13a7SLeon Romanovsky 	devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
3619773bda96SJonathan Lemon 	if (!devlink) {
3620773bda96SJonathan Lemon 		dev_err(&pdev->dev, "devlink_alloc failed\n");
3621a7e1abadSJonathan Lemon 		return -ENOMEM;
3622773bda96SJonathan Lemon 	}
3623773bda96SJonathan Lemon 
3624a7e1abadSJonathan Lemon 	err = pci_enable_device(pdev);
3625a7e1abadSJonathan Lemon 	if (err) {
3626a7e1abadSJonathan Lemon 		dev_err(&pdev->dev, "pci_enable_device\n");
36274587369bSJonathan Lemon 		goto out_free;
3628a7e1abadSJonathan Lemon 	}
3629a7e1abadSJonathan Lemon 
3630773bda96SJonathan Lemon 	bp = devlink_priv(devlink);
3631773bda96SJonathan Lemon 	err = ptp_ocp_device_init(bp, pdev);
3632773bda96SJonathan Lemon 	if (err)
3633d9fdbf13SJonathan Lemon 		goto out_disable;
3634a7e1abadSJonathan Lemon 
3635773bda96SJonathan Lemon 	/* compat mode.
3636773bda96SJonathan Lemon 	 * Older FPGA firmware only returns 2 irq's.
3637773bda96SJonathan Lemon 	 * allow this - if not all of the IRQ's are returned, skip the
3638773bda96SJonathan Lemon 	 * extra devices and just register the clock.
3639773bda96SJonathan Lemon 	 */
36400fa3ff7eSJonathan Lemon 	err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX);
3641773bda96SJonathan Lemon 	if (err < 0) {
3642773bda96SJonathan Lemon 		dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
3643773bda96SJonathan Lemon 		goto out;
3644a7e1abadSJonathan Lemon 	}
3645773bda96SJonathan Lemon 	bp->n_irqs = err;
3646773bda96SJonathan Lemon 	pci_set_master(pdev);
3647a7e1abadSJonathan Lemon 
3648773bda96SJonathan Lemon 	err = ptp_ocp_register_resources(bp, id->driver_data);
3649a7e1abadSJonathan Lemon 	if (err)
3650a7e1abadSJonathan Lemon 		goto out;
3651a7e1abadSJonathan Lemon 
3652a7e1abadSJonathan Lemon 	bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
3653a7e1abadSJonathan Lemon 	if (IS_ERR(bp->ptp)) {
3654a7e1abadSJonathan Lemon 		err = PTR_ERR(bp->ptp);
3655773bda96SJonathan Lemon 		dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
3656773bda96SJonathan Lemon 		bp->ptp = NULL;
3657a7e1abadSJonathan Lemon 		goto out;
3658a7e1abadSJonathan Lemon 	}
3659a7e1abadSJonathan Lemon 
3660773bda96SJonathan Lemon 	err = ptp_ocp_complete(bp);
3661773bda96SJonathan Lemon 	if (err)
3662773bda96SJonathan Lemon 		goto out;
3663773bda96SJonathan Lemon 
3664a7e1abadSJonathan Lemon 	ptp_ocp_info(bp);
3665c89f78e9SLeon Romanovsky 	devlink_register(devlink);
3666a7e1abadSJonathan Lemon 	return 0;
3667a7e1abadSJonathan Lemon 
3668a7e1abadSJonathan Lemon out:
3669773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
3670773bda96SJonathan Lemon 	pci_set_drvdata(pdev, NULL);
3671d9fdbf13SJonathan Lemon out_disable:
3672d9fdbf13SJonathan Lemon 	pci_disable_device(pdev);
36734587369bSJonathan Lemon out_free:
3674773bda96SJonathan Lemon 	devlink_free(devlink);
3675a7e1abadSJonathan Lemon 	return err;
3676a7e1abadSJonathan Lemon }
3677a7e1abadSJonathan Lemon 
3678a7e1abadSJonathan Lemon static void
3679a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev)
3680a7e1abadSJonathan Lemon {
3681a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = pci_get_drvdata(pdev);
3682773bda96SJonathan Lemon 	struct devlink *devlink = priv_to_devlink(bp);
3683a7e1abadSJonathan Lemon 
3684c89f78e9SLeon Romanovsky 	devlink_unregister(devlink);
3685773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
3686a7e1abadSJonathan Lemon 	pci_set_drvdata(pdev, NULL);
3687d9fdbf13SJonathan Lemon 	pci_disable_device(pdev);
3688773bda96SJonathan Lemon 
3689773bda96SJonathan Lemon 	devlink_free(devlink);
3690a7e1abadSJonathan Lemon }
3691a7e1abadSJonathan Lemon 
3692a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = {
3693a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
3694a7e1abadSJonathan Lemon 	.id_table	= ptp_ocp_pcidev_id,
3695a7e1abadSJonathan Lemon 	.probe		= ptp_ocp_probe,
3696a7e1abadSJonathan Lemon 	.remove		= ptp_ocp_remove,
3697a7e1abadSJonathan Lemon };
3698a7e1abadSJonathan Lemon 
3699773bda96SJonathan Lemon static int
3700773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
3701773bda96SJonathan Lemon 			  unsigned long action, void *data)
3702773bda96SJonathan Lemon {
3703773bda96SJonathan Lemon 	struct device *dev, *child = data;
3704773bda96SJonathan Lemon 	struct ptp_ocp *bp;
3705773bda96SJonathan Lemon 	bool add;
3706773bda96SJonathan Lemon 
3707773bda96SJonathan Lemon 	switch (action) {
3708773bda96SJonathan Lemon 	case BUS_NOTIFY_ADD_DEVICE:
3709773bda96SJonathan Lemon 	case BUS_NOTIFY_DEL_DEVICE:
3710773bda96SJonathan Lemon 		add = action == BUS_NOTIFY_ADD_DEVICE;
3711773bda96SJonathan Lemon 		break;
3712773bda96SJonathan Lemon 	default:
3713773bda96SJonathan Lemon 		return 0;
3714773bda96SJonathan Lemon 	}
3715773bda96SJonathan Lemon 
3716773bda96SJonathan Lemon 	if (!i2c_verify_adapter(child))
3717773bda96SJonathan Lemon 		return 0;
3718773bda96SJonathan Lemon 
3719773bda96SJonathan Lemon 	dev = child;
3720773bda96SJonathan Lemon 	while ((dev = dev->parent))
3721773bda96SJonathan Lemon 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
3722773bda96SJonathan Lemon 			goto found;
3723773bda96SJonathan Lemon 	return 0;
3724773bda96SJonathan Lemon 
3725773bda96SJonathan Lemon found:
3726773bda96SJonathan Lemon 	bp = dev_get_drvdata(dev);
3727773bda96SJonathan Lemon 	if (add)
3728773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, child, "i2c");
3729773bda96SJonathan Lemon 	else
3730773bda96SJonathan Lemon 		sysfs_remove_link(&bp->dev.kobj, "i2c");
3731773bda96SJonathan Lemon 
3732773bda96SJonathan Lemon 	return 0;
3733773bda96SJonathan Lemon }
3734773bda96SJonathan Lemon 
3735773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = {
3736773bda96SJonathan Lemon 	.notifier_call = ptp_ocp_i2c_notifier_call,
3737773bda96SJonathan Lemon };
3738773bda96SJonathan Lemon 
3739a7e1abadSJonathan Lemon static int __init
3740a7e1abadSJonathan Lemon ptp_ocp_init(void)
3741a7e1abadSJonathan Lemon {
3742773bda96SJonathan Lemon 	const char *what;
3743a7e1abadSJonathan Lemon 	int err;
3744a7e1abadSJonathan Lemon 
3745f67bf662SJonathan Lemon 	ptp_ocp_debugfs_init();
3746f67bf662SJonathan Lemon 
3747773bda96SJonathan Lemon 	what = "timecard class";
3748773bda96SJonathan Lemon 	err = class_register(&timecard_class);
3749773bda96SJonathan Lemon 	if (err)
3750773bda96SJonathan Lemon 		goto out;
3751773bda96SJonathan Lemon 
3752773bda96SJonathan Lemon 	what = "i2c notifier";
3753773bda96SJonathan Lemon 	err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3754773bda96SJonathan Lemon 	if (err)
3755773bda96SJonathan Lemon 		goto out_notifier;
3756773bda96SJonathan Lemon 
3757773bda96SJonathan Lemon 	what = "ptp_ocp driver";
3758a7e1abadSJonathan Lemon 	err = pci_register_driver(&ptp_ocp_driver);
3759773bda96SJonathan Lemon 	if (err)
3760773bda96SJonathan Lemon 		goto out_register;
3761773bda96SJonathan Lemon 
3762773bda96SJonathan Lemon 	return 0;
3763773bda96SJonathan Lemon 
3764773bda96SJonathan Lemon out_register:
3765773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3766773bda96SJonathan Lemon out_notifier:
3767773bda96SJonathan Lemon 	class_unregister(&timecard_class);
3768773bda96SJonathan Lemon out:
3769f67bf662SJonathan Lemon 	ptp_ocp_debugfs_fini();
3770773bda96SJonathan Lemon 	pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
3771a7e1abadSJonathan Lemon 	return err;
3772a7e1abadSJonathan Lemon }
3773a7e1abadSJonathan Lemon 
3774a7e1abadSJonathan Lemon static void __exit
3775a7e1abadSJonathan Lemon ptp_ocp_fini(void)
3776a7e1abadSJonathan Lemon {
3777773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3778a7e1abadSJonathan Lemon 	pci_unregister_driver(&ptp_ocp_driver);
3779773bda96SJonathan Lemon 	class_unregister(&timecard_class);
3780f67bf662SJonathan Lemon 	ptp_ocp_debugfs_fini();
3781a7e1abadSJonathan Lemon }
3782a7e1abadSJonathan Lemon 
3783a7e1abadSJonathan Lemon module_init(ptp_ocp_init);
3784a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini);
3785a7e1abadSJonathan Lemon 
3786a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver");
3787a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2");
3788