xref: /openbmc/linux/drivers/ptp/ptp_ocp.c (revision 2407f5d620172fb0f0321e5ebbbf4de99401a0bc)
1a7e1abadSJonathan Lemon // SPDX-License-Identifier: GPL-2.0-only
2a7e1abadSJonathan Lemon /* Copyright (c) 2020 Facebook */
3a7e1abadSJonathan Lemon 
4a7e1abadSJonathan Lemon #include <linux/err.h>
5a7e1abadSJonathan Lemon #include <linux/kernel.h>
6a7e1abadSJonathan Lemon #include <linux/module.h>
7f67bf662SJonathan Lemon #include <linux/debugfs.h>
8a7e1abadSJonathan Lemon #include <linux/init.h>
9a7e1abadSJonathan Lemon #include <linux/pci.h>
10773bda96SJonathan Lemon #include <linux/serial_8250.h>
11773bda96SJonathan Lemon #include <linux/clkdev.h>
12773bda96SJonathan Lemon #include <linux/clk-provider.h>
13773bda96SJonathan Lemon #include <linux/platform_device.h>
140cfcdd1eSJonathan Lemon #include <linux/platform_data/i2c-xiic.h>
15a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h>
16773bda96SJonathan Lemon #include <linux/spi/spi.h>
17773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h>
18773bda96SJonathan Lemon #include <net/devlink.h>
19773bda96SJonathan Lemon #include <linux/i2c.h>
20773bda96SJonathan Lemon #include <linux/mtd/mtd.h>
210cfcdd1eSJonathan Lemon #include <linux/nvmem-consumer.h>
22a7e1abadSJonathan Lemon 
23773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK
24773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b
25773bda96SJonathan Lemon #endif
26773bda96SJonathan Lemon 
27773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD
28773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
29773bda96SJonathan Lemon #endif
30773bda96SJonathan Lemon 
31773bda96SJonathan Lemon static struct class timecard_class = {
32773bda96SJonathan Lemon 	.owner		= THIS_MODULE,
33773bda96SJonathan Lemon 	.name		= "timecard",
34a7e1abadSJonathan Lemon };
35a7e1abadSJonathan Lemon 
36a7e1abadSJonathan Lemon struct ocp_reg {
37a7e1abadSJonathan Lemon 	u32	ctrl;
38a7e1abadSJonathan Lemon 	u32	status;
39a7e1abadSJonathan Lemon 	u32	select;
40a7e1abadSJonathan Lemon 	u32	version;
41a7e1abadSJonathan Lemon 	u32	time_ns;
42a7e1abadSJonathan Lemon 	u32	time_sec;
43a7e1abadSJonathan Lemon 	u32	__pad0[2];
44a7e1abadSJonathan Lemon 	u32	adjust_ns;
45a7e1abadSJonathan Lemon 	u32	adjust_sec;
46a7e1abadSJonathan Lemon 	u32	__pad1[2];
47a7e1abadSJonathan Lemon 	u32	offset_ns;
48a7e1abadSJonathan Lemon 	u32	offset_window_ns;
49773bda96SJonathan Lemon 	u32	__pad2[2];
50773bda96SJonathan Lemon 	u32	drift_ns;
51773bda96SJonathan Lemon 	u32	drift_window_ns;
52773bda96SJonathan Lemon 	u32	__pad3[6];
53773bda96SJonathan Lemon 	u32	servo_offset_p;
54773bda96SJonathan Lemon 	u32	servo_offset_i;
55773bda96SJonathan Lemon 	u32	servo_drift_p;
56773bda96SJonathan Lemon 	u32	servo_drift_i;
572f23f486SVadim Fedorenko 	u32	status_offset;
582f23f486SVadim Fedorenko 	u32	status_drift;
59a7e1abadSJonathan Lemon };
60a7e1abadSJonathan Lemon 
61a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE		BIT(0)
62a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME	BIT(1)
63a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET	BIT(2)
64773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT	BIT(3)
65773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO	BIT(8)
66a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ	BIT(30)
67a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE	BIT(31)
68a7e1abadSJonathan Lemon 
69a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC	BIT(0)
70773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER	BIT(1)
71a7e1abadSJonathan Lemon 
72a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE	0
73773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG	0xfe
74a7e1abadSJonathan Lemon 
75a7e1abadSJonathan Lemon struct tod_reg {
76a7e1abadSJonathan Lemon 	u32	ctrl;
77a7e1abadSJonathan Lemon 	u32	status;
78a7e1abadSJonathan Lemon 	u32	uart_polarity;
79a7e1abadSJonathan Lemon 	u32	version;
80065efcc5SJonathan Lemon 	u32	adj_sec;
81a7e1abadSJonathan Lemon 	u32	__pad0[3];
82a7e1abadSJonathan Lemon 	u32	uart_baud;
83a7e1abadSJonathan Lemon 	u32	__pad1[3];
84a7e1abadSJonathan Lemon 	u32	utc_status;
85a7e1abadSJonathan Lemon 	u32	leap;
86a7e1abadSJonathan Lemon };
87a7e1abadSJonathan Lemon 
88a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL	BIT(28)
89a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A	BIT(17)
90a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B	BIT(16)
91a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE		BIT(0)
92a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK	((1U << 4) - 1)
93a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT	24
94a7e1abadSJonathan Lemon 
95a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK		0xff
96a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID		BIT(8)
979f492c4cSVadim Fedorenko #define TOD_STATUS_LEAP_ANNOUNCE	BIT(12)
98a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID		BIT(16)
99a7e1abadSJonathan Lemon 
100773bda96SJonathan Lemon struct ts_reg {
101773bda96SJonathan Lemon 	u32	enable;
102773bda96SJonathan Lemon 	u32	error;
103773bda96SJonathan Lemon 	u32	polarity;
104773bda96SJonathan Lemon 	u32	version;
105773bda96SJonathan Lemon 	u32	__pad0[4];
106773bda96SJonathan Lemon 	u32	cable_delay;
107773bda96SJonathan Lemon 	u32	__pad1[3];
108773bda96SJonathan Lemon 	u32	intr;
109773bda96SJonathan Lemon 	u32	intr_mask;
110773bda96SJonathan Lemon 	u32	event_count;
111773bda96SJonathan Lemon 	u32	__pad2[1];
112773bda96SJonathan Lemon 	u32	ts_count;
113773bda96SJonathan Lemon 	u32	time_ns;
114773bda96SJonathan Lemon 	u32	time_sec;
115773bda96SJonathan Lemon 	u32	data_width;
116773bda96SJonathan Lemon 	u32	data;
117773bda96SJonathan Lemon };
118773bda96SJonathan Lemon 
119773bda96SJonathan Lemon struct pps_reg {
120773bda96SJonathan Lemon 	u32	ctrl;
121773bda96SJonathan Lemon 	u32	status;
1220d43d4f2SJonathan Lemon 	u32	__pad0[6];
1230d43d4f2SJonathan Lemon 	u32	cable_delay;
124773bda96SJonathan Lemon };
125773bda96SJonathan Lemon 
126773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR	BIT(0)
127773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR	BIT(1)
128773bda96SJonathan Lemon 
129773bda96SJonathan Lemon struct img_reg {
130773bda96SJonathan Lemon 	u32	version;
131773bda96SJonathan Lemon };
132773bda96SJonathan Lemon 
133e1daf0ecSJonathan Lemon struct gpio_reg {
134e1daf0ecSJonathan Lemon 	u32	gpio1;
135e1daf0ecSJonathan Lemon 	u32	__pad0;
136e1daf0ecSJonathan Lemon 	u32	gpio2;
137e1daf0ecSJonathan Lemon 	u32	__pad1;
138e1daf0ecSJonathan Lemon };
139e1daf0ecSJonathan Lemon 
1406baf2925SJonathan Lemon struct irig_master_reg {
1416baf2925SJonathan Lemon 	u32	ctrl;
1426baf2925SJonathan Lemon 	u32	status;
1436baf2925SJonathan Lemon 	u32	__pad0;
1446baf2925SJonathan Lemon 	u32	version;
1456baf2925SJonathan Lemon 	u32	adj_sec;
1466baf2925SJonathan Lemon 	u32	mode_ctrl;
1476baf2925SJonathan Lemon };
1486baf2925SJonathan Lemon 
1496baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE	BIT(0)
1506baf2925SJonathan Lemon 
1516baf2925SJonathan Lemon struct irig_slave_reg {
1526baf2925SJonathan Lemon 	u32	ctrl;
1536baf2925SJonathan Lemon 	u32	status;
1546baf2925SJonathan Lemon 	u32	__pad0;
1556baf2925SJonathan Lemon 	u32	version;
1566baf2925SJonathan Lemon 	u32	adj_sec;
1576baf2925SJonathan Lemon 	u32	mode_ctrl;
1586baf2925SJonathan Lemon };
1596baf2925SJonathan Lemon 
1606baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE	BIT(0)
1616baf2925SJonathan Lemon 
1626baf2925SJonathan Lemon struct dcf_master_reg {
1636baf2925SJonathan Lemon 	u32	ctrl;
1646baf2925SJonathan Lemon 	u32	status;
1656baf2925SJonathan Lemon 	u32	__pad0;
1666baf2925SJonathan Lemon 	u32	version;
1676baf2925SJonathan Lemon 	u32	adj_sec;
1686baf2925SJonathan Lemon };
1696baf2925SJonathan Lemon 
1706baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE	BIT(0)
1716baf2925SJonathan Lemon 
1726baf2925SJonathan Lemon struct dcf_slave_reg {
1736baf2925SJonathan Lemon 	u32	ctrl;
1746baf2925SJonathan Lemon 	u32	status;
1756baf2925SJonathan Lemon 	u32	__pad0;
1766baf2925SJonathan Lemon 	u32	version;
1776baf2925SJonathan Lemon 	u32	adj_sec;
1786baf2925SJonathan Lemon };
1796baf2925SJonathan Lemon 
1806baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE	BIT(0)
1816baf2925SJonathan Lemon 
182b325af3cSJonathan Lemon struct signal_reg {
183b325af3cSJonathan Lemon 	u32	enable;
184b325af3cSJonathan Lemon 	u32	status;
185b325af3cSJonathan Lemon 	u32	polarity;
186b325af3cSJonathan Lemon 	u32	version;
187b325af3cSJonathan Lemon 	u32	__pad0[4];
188b325af3cSJonathan Lemon 	u32	cable_delay;
189b325af3cSJonathan Lemon 	u32	__pad1[3];
190b325af3cSJonathan Lemon 	u32	intr;
191b325af3cSJonathan Lemon 	u32	intr_mask;
192b325af3cSJonathan Lemon 	u32	__pad2[2];
193b325af3cSJonathan Lemon 	u32	start_ns;
194b325af3cSJonathan Lemon 	u32	start_sec;
195b325af3cSJonathan Lemon 	u32	pulse_ns;
196b325af3cSJonathan Lemon 	u32	pulse_sec;
197b325af3cSJonathan Lemon 	u32	period_ns;
198b325af3cSJonathan Lemon 	u32	period_sec;
199b325af3cSJonathan Lemon 	u32	repeat_count;
200b325af3cSJonathan Lemon };
201b325af3cSJonathan Lemon 
202*2407f5d6SJonathan Lemon struct frequency_reg {
203*2407f5d6SJonathan Lemon 	u32	ctrl;
204*2407f5d6SJonathan Lemon 	u32	status;
205*2407f5d6SJonathan Lemon };
206*2407f5d6SJonathan Lemon #define FREQ_STATUS_VALID	BIT(31)
207*2407f5d6SJonathan Lemon #define FREQ_STATUS_ERROR	BIT(30)
208*2407f5d6SJonathan Lemon #define FREQ_STATUS_OVERRUN	BIT(29)
209*2407f5d6SJonathan Lemon #define FREQ_STATUS_MASK	(BIT(24) - 1)
210*2407f5d6SJonathan Lemon 
211773bda96SJonathan Lemon struct ptp_ocp_flash_info {
212773bda96SJonathan Lemon 	const char *name;
213773bda96SJonathan Lemon 	int pci_offset;
214773bda96SJonathan Lemon 	int data_size;
215773bda96SJonathan Lemon 	void *data;
216773bda96SJonathan Lemon };
217773bda96SJonathan Lemon 
2181618df6aSJonathan Lemon struct ptp_ocp_i2c_info {
2191618df6aSJonathan Lemon 	const char *name;
2201618df6aSJonathan Lemon 	unsigned long fixed_rate;
2211618df6aSJonathan Lemon 	size_t data_size;
2221618df6aSJonathan Lemon 	void *data;
2231618df6aSJonathan Lemon };
2241618df6aSJonathan Lemon 
225773bda96SJonathan Lemon struct ptp_ocp_ext_info {
226773bda96SJonathan Lemon 	int index;
227773bda96SJonathan Lemon 	irqreturn_t (*irq_fcn)(int irq, void *priv);
228a62a56d0SJonathan Lemon 	int (*enable)(void *priv, u32 req, bool enable);
229773bda96SJonathan Lemon };
230773bda96SJonathan Lemon 
231773bda96SJonathan Lemon struct ptp_ocp_ext_src {
232773bda96SJonathan Lemon 	void __iomem		*mem;
233773bda96SJonathan Lemon 	struct ptp_ocp		*bp;
234773bda96SJonathan Lemon 	struct ptp_ocp_ext_info	*info;
235773bda96SJonathan Lemon 	int			irq_vec;
236773bda96SJonathan Lemon };
237773bda96SJonathan Lemon 
238a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode {
239a509a7c6SJonathan Lemon 	SMA_MODE_IN,
240a509a7c6SJonathan Lemon 	SMA_MODE_OUT,
241a509a7c6SJonathan Lemon };
242a509a7c6SJonathan Lemon 
243a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector {
244a509a7c6SJonathan Lemon 	enum	ptp_ocp_sma_mode mode;
245a509a7c6SJonathan Lemon 	bool	fixed_fcn;
246a509a7c6SJonathan Lemon 	bool	fixed_dir;
247b2c4f0acSJonathan Lemon 	bool	disabled;
248a509a7c6SJonathan Lemon };
249a509a7c6SJonathan Lemon 
250c205d53cSJonathan Lemon struct ocp_attr_group {
251c205d53cSJonathan Lemon 	u64 cap;
252c205d53cSJonathan Lemon 	const struct attribute_group *group;
253c205d53cSJonathan Lemon };
254c205d53cSJonathan Lemon 
255c205d53cSJonathan Lemon #define OCP_CAP_BASIC	BIT(0)
256b325af3cSJonathan Lemon #define OCP_CAP_SIGNAL	BIT(1)
257*2407f5d6SJonathan Lemon #define OCP_CAP_FREQ	BIT(2)
258b325af3cSJonathan Lemon 
259b325af3cSJonathan Lemon struct ptp_ocp_signal {
260b325af3cSJonathan Lemon 	ktime_t		period;
261b325af3cSJonathan Lemon 	ktime_t		pulse;
262b325af3cSJonathan Lemon 	ktime_t		phase;
263b325af3cSJonathan Lemon 	ktime_t		start;
264b325af3cSJonathan Lemon 	int		duty;
265b325af3cSJonathan Lemon 	bool		polarity;
266b325af3cSJonathan Lemon 	bool		running;
267b325af3cSJonathan Lemon };
268c205d53cSJonathan Lemon 
2690cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN		13
2700cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN			6
2710cfcdd1eSJonathan Lemon 
272a7e1abadSJonathan Lemon struct ptp_ocp {
273a7e1abadSJonathan Lemon 	struct pci_dev		*pdev;
274773bda96SJonathan Lemon 	struct device		dev;
275a7e1abadSJonathan Lemon 	spinlock_t		lock;
276a7e1abadSJonathan Lemon 	struct ocp_reg __iomem	*reg;
277a7e1abadSJonathan Lemon 	struct tod_reg __iomem	*tod;
2780d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_ext;
2790d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_clk;
280f67bf662SJonathan Lemon 	struct gpio_reg __iomem	*pps_select;
281a509a7c6SJonathan Lemon 	struct gpio_reg __iomem	*sma_map1;
282a509a7c6SJonathan Lemon 	struct gpio_reg __iomem	*sma_map2;
2836baf2925SJonathan Lemon 	struct irig_master_reg	__iomem *irig_out;
2846baf2925SJonathan Lemon 	struct irig_slave_reg	__iomem *irig_in;
2856baf2925SJonathan Lemon 	struct dcf_master_reg	__iomem *dcf_out;
2866baf2925SJonathan Lemon 	struct dcf_slave_reg	__iomem *dcf_in;
287e3516bb4SJonathan Lemon 	struct tod_reg		__iomem *nmea_out;
288*2407f5d6SJonathan Lemon 	struct frequency_reg	__iomem *freq_in[4];
289b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src	*signal_out[4];
290773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*pps;
291773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts0;
292773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts1;
293dcf61469SJonathan Lemon 	struct ptp_ocp_ext_src	*ts2;
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;
301c205d53cSJonathan Lemon 	const struct ocp_attr_group *attr_tbl;
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;
311b0ca789aSJonathan Lemon 	u32			fw_version;
3120cfcdd1eSJonathan Lemon 	u8			board_id[OCP_BOARD_ID_LEN];
3130cfcdd1eSJonathan Lemon 	u8			serial[OCP_SERIAL_LEN];
3140cfcdd1eSJonathan Lemon 	bool			has_eeprom_data;
315a62a56d0SJonathan Lemon 	u32			pps_req_map;
31689260d87SJonathan Lemon 	int			flash_start;
31789260d87SJonathan Lemon 	u32			utc_tai_offset;
3181acffc6eSJonathan Lemon 	u32			ts_window_adjust;
319c205d53cSJonathan Lemon 	u64			fw_cap;
320b325af3cSJonathan Lemon 	struct ptp_ocp_signal	signal[4];
321a509a7c6SJonathan Lemon 	struct ptp_ocp_sma_connector sma[4];
322a7e1abadSJonathan Lemon };
323a7e1abadSJonathan Lemon 
324a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP	BIT(0)
325a62a56d0SJonathan Lemon #define OCP_REQ_PPS		BIT(1)
326a62a56d0SJonathan Lemon 
327773bda96SJonathan Lemon struct ocp_resource {
328773bda96SJonathan Lemon 	unsigned long offset;
329773bda96SJonathan Lemon 	int size;
330773bda96SJonathan Lemon 	int irq_vec;
331773bda96SJonathan Lemon 	int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
332773bda96SJonathan Lemon 	void *extra;
333773bda96SJonathan Lemon 	unsigned long bp_offset;
33456ec4403SJonathan Lemon 	const char * const name;
335773bda96SJonathan Lemon };
336773bda96SJonathan Lemon 
337773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
338773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
339773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
340773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
341773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
342773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
343773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
344b325af3cSJonathan Lemon static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv);
345a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
3461aa66a3aSJonathan Lemon static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
3471aa66a3aSJonathan Lemon 				      struct ptp_perout_request *req);
348b325af3cSJonathan Lemon static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable);
349b325af3cSJonathan Lemon static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr);
350773bda96SJonathan Lemon 
351c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[];
352c205d53cSJonathan Lemon 
3530cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map {
3540cfcdd1eSJonathan Lemon 	u16	off;
3550cfcdd1eSJonathan Lemon 	u16	len;
3560cfcdd1eSJonathan Lemon 	u32	bp_offset;
3570cfcdd1eSJonathan Lemon 	const void * const tag;
3580cfcdd1eSJonathan Lemon };
3590cfcdd1eSJonathan Lemon 
3600cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member)				\
3610cfcdd1eSJonathan Lemon 	.off = addr,						\
3620cfcdd1eSJonathan Lemon 	.len = sizeof_field(struct ptp_ocp, member),		\
3630cfcdd1eSJonathan Lemon 	.bp_offset = offsetof(struct ptp_ocp, member)
3640cfcdd1eSJonathan Lemon 
3650cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({				\
3660cfcdd1eSJonathan Lemon 	(void *)((uintptr_t)(bp) + (map)->bp_offset);		\
3670cfcdd1eSJonathan Lemon })
3680cfcdd1eSJonathan Lemon 
3690cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
3700cfcdd1eSJonathan Lemon 	{ EEPROM_ENTRY(0x43, board_id) },
3710cfcdd1eSJonathan Lemon 	{ EEPROM_ENTRY(0x00, serial), .tag = "mac" },
3720cfcdd1eSJonathan Lemon 	{ }
3730cfcdd1eSJonathan Lemon };
3740cfcdd1eSJonathan Lemon 
375773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({				\
376773bda96SJonathan Lemon 	uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset;		\
377773bda96SJonathan Lemon 	*(typeof(val) *)addr = val;					\
378773bda96SJonathan Lemon })
379773bda96SJonathan Lemon 
380773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \
38156ec4403SJonathan Lemon 	.name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
382773bda96SJonathan Lemon 
383773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \
384773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
385773bda96SJonathan Lemon 
386773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \
387773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
388773bda96SJonathan Lemon 
389773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \
390773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
391773bda96SJonathan Lemon 
392773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \
393773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
394773bda96SJonathan Lemon 
395773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \
396773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
397773bda96SJonathan Lemon 
398773bda96SJonathan Lemon /* This is the MSI vector mapping used.
399a62a56d0SJonathan Lemon  * 0: TS3 (and PPS)
400773bda96SJonathan Lemon  * 1: TS0
401773bda96SJonathan Lemon  * 2: TS1
402be69087cSJonathan Lemon  * 3: GNSS1
40371d7e085SJonathan Lemon  * 4: GNSS2
404773bda96SJonathan Lemon  * 5: MAC
405dcf61469SJonathan Lemon  * 6: TS2
4061447149dSJonathan Lemon  * 7: I2C controller
407e3516bb4SJonathan Lemon  * 8: HWICAP (notused)
408773bda96SJonathan Lemon  * 9: SPI Flash
409e3516bb4SJonathan Lemon  * 10: NMEA
410b325af3cSJonathan Lemon  * 11: Signal Generator 1
411b325af3cSJonathan Lemon  * 12: Signal Generator 2
412b325af3cSJonathan Lemon  * 13: Signal Generator 3
413b325af3cSJonathan Lemon  * 14: Signal Generator 4
414773bda96SJonathan Lemon  */
415773bda96SJonathan Lemon 
416773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = {
417773bda96SJonathan Lemon 	{
418773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(reg),
419773bda96SJonathan Lemon 		.offset = 0x01000000, .size = 0x10000,
420773bda96SJonathan Lemon 	},
421773bda96SJonathan Lemon 	{
422773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts0),
423773bda96SJonathan Lemon 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
424773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
42556ec4403SJonathan Lemon 			.index = 0,
426773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
427773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
428773bda96SJonathan Lemon 		},
429773bda96SJonathan Lemon 	},
430773bda96SJonathan Lemon 	{
431773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts1),
432773bda96SJonathan Lemon 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
433773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
43456ec4403SJonathan Lemon 			.index = 1,
435773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
436773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
437773bda96SJonathan Lemon 		},
438773bda96SJonathan Lemon 	},
439773bda96SJonathan Lemon 	{
440dcf61469SJonathan Lemon 		OCP_EXT_RESOURCE(ts2),
441dcf61469SJonathan Lemon 		.offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
442dcf61469SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
443dcf61469SJonathan Lemon 			.index = 2,
444dcf61469SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
445dcf61469SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
446dcf61469SJonathan Lemon 		},
447dcf61469SJonathan Lemon 	},
448dcf61469SJonathan Lemon 	{
449a62a56d0SJonathan Lemon 		OCP_EXT_RESOURCE(pps),
450a62a56d0SJonathan Lemon 		.offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
451a62a56d0SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
452a62a56d0SJonathan Lemon 			.index = 3,
453a62a56d0SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
454a62a56d0SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
455a62a56d0SJonathan Lemon 		},
456a62a56d0SJonathan Lemon 	},
457a62a56d0SJonathan Lemon 	{
458b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[0]),
459b325af3cSJonathan Lemon 		.offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
460b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
461b325af3cSJonathan Lemon 			.index = 1,
462b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
463b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
464b325af3cSJonathan Lemon 		},
465b325af3cSJonathan Lemon 	},
466b325af3cSJonathan Lemon 	{
467b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[1]),
468b325af3cSJonathan Lemon 		.offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
469b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
470b325af3cSJonathan Lemon 			.index = 2,
471b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
472b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
473b325af3cSJonathan Lemon 		},
474b325af3cSJonathan Lemon 	},
475b325af3cSJonathan Lemon 	{
476b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[2]),
477b325af3cSJonathan Lemon 		.offset = 0x010F0000, .size = 0x10000, .irq_vec = 13,
478b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
479b325af3cSJonathan Lemon 			.index = 3,
480b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
481b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
482b325af3cSJonathan Lemon 		},
483b325af3cSJonathan Lemon 	},
484b325af3cSJonathan Lemon 	{
485b325af3cSJonathan Lemon 		OCP_EXT_RESOURCE(signal_out[3]),
486b325af3cSJonathan Lemon 		.offset = 0x01100000, .size = 0x10000, .irq_vec = 14,
487b325af3cSJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
488b325af3cSJonathan Lemon 			.index = 4,
489b325af3cSJonathan Lemon 			.irq_fcn = ptp_ocp_signal_irq,
490b325af3cSJonathan Lemon 			.enable = ptp_ocp_signal_enable,
491b325af3cSJonathan Lemon 		},
492b325af3cSJonathan Lemon 	},
493b325af3cSJonathan Lemon 	{
4940d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_ext),
4950d43d4f2SJonathan Lemon 		.offset = 0x01030000, .size = 0x10000,
4960d43d4f2SJonathan Lemon 	},
4970d43d4f2SJonathan Lemon 	{
4980d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_clk),
499773bda96SJonathan Lemon 		.offset = 0x01040000, .size = 0x10000,
500773bda96SJonathan Lemon 	},
501773bda96SJonathan Lemon 	{
502773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(tod),
503773bda96SJonathan Lemon 		.offset = 0x01050000, .size = 0x10000,
504773bda96SJonathan Lemon 	},
505773bda96SJonathan Lemon 	{
5066baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(irig_in),
5076baf2925SJonathan Lemon 		.offset = 0x01070000, .size = 0x10000,
5086baf2925SJonathan Lemon 	},
5096baf2925SJonathan Lemon 	{
5106baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(irig_out),
5116baf2925SJonathan Lemon 		.offset = 0x01080000, .size = 0x10000,
5126baf2925SJonathan Lemon 	},
5136baf2925SJonathan Lemon 	{
5146baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(dcf_in),
5156baf2925SJonathan Lemon 		.offset = 0x01090000, .size = 0x10000,
5166baf2925SJonathan Lemon 	},
5176baf2925SJonathan Lemon 	{
5186baf2925SJonathan Lemon 		OCP_MEM_RESOURCE(dcf_out),
5196baf2925SJonathan Lemon 		.offset = 0x010A0000, .size = 0x10000,
5206baf2925SJonathan Lemon 	},
5216baf2925SJonathan Lemon 	{
522e3516bb4SJonathan Lemon 		OCP_MEM_RESOURCE(nmea_out),
523e3516bb4SJonathan Lemon 		.offset = 0x010B0000, .size = 0x10000,
524e3516bb4SJonathan Lemon 	},
525e3516bb4SJonathan Lemon 	{
526773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(image),
527773bda96SJonathan Lemon 		.offset = 0x00020000, .size = 0x1000,
528773bda96SJonathan Lemon 	},
529773bda96SJonathan Lemon 	{
530f67bf662SJonathan Lemon 		OCP_MEM_RESOURCE(pps_select),
531f67bf662SJonathan Lemon 		.offset = 0x00130000, .size = 0x1000,
532f67bf662SJonathan Lemon 	},
533f67bf662SJonathan Lemon 	{
534a509a7c6SJonathan Lemon 		OCP_MEM_RESOURCE(sma_map1),
535e1daf0ecSJonathan Lemon 		.offset = 0x00140000, .size = 0x1000,
536e1daf0ecSJonathan Lemon 	},
537e1daf0ecSJonathan Lemon 	{
538a509a7c6SJonathan Lemon 		OCP_MEM_RESOURCE(sma_map2),
539a509a7c6SJonathan Lemon 		.offset = 0x00220000, .size = 0x1000,
540a509a7c6SJonathan Lemon 	},
541a509a7c6SJonathan Lemon 	{
542773bda96SJonathan Lemon 		OCP_I2C_RESOURCE(i2c_ctrl),
543773bda96SJonathan Lemon 		.offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
5441618df6aSJonathan Lemon 		.extra = &(struct ptp_ocp_i2c_info) {
5451618df6aSJonathan Lemon 			.name = "xiic-i2c",
5461618df6aSJonathan Lemon 			.fixed_rate = 50000000,
5470cfcdd1eSJonathan Lemon 			.data_size = sizeof(struct xiic_i2c_platform_data),
5480cfcdd1eSJonathan Lemon 			.data = &(struct xiic_i2c_platform_data) {
5490cfcdd1eSJonathan Lemon 				.num_devices = 2,
5500cfcdd1eSJonathan Lemon 				.devices = (struct i2c_board_info[]) {
5510cfcdd1eSJonathan Lemon 					{ I2C_BOARD_INFO("24c02", 0x50) },
5520cfcdd1eSJonathan Lemon 					{ I2C_BOARD_INFO("24mac402", 0x58),
5530cfcdd1eSJonathan Lemon 					  .platform_data = "mac" },
5540cfcdd1eSJonathan Lemon 				},
5550cfcdd1eSJonathan Lemon 			},
5561618df6aSJonathan Lemon 		},
557773bda96SJonathan Lemon 	},
558773bda96SJonathan Lemon 	{
559ef0cfb34SJonathan Lemon 		OCP_SERIAL_RESOURCE(gnss_port),
560773bda96SJonathan Lemon 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
561773bda96SJonathan Lemon 	},
562773bda96SJonathan Lemon 	{
56371d7e085SJonathan Lemon 		OCP_SERIAL_RESOURCE(gnss2_port),
56471d7e085SJonathan Lemon 		.offset = 0x00170000 + 0x1000, .irq_vec = 4,
56571d7e085SJonathan Lemon 	},
56671d7e085SJonathan Lemon 	{
567773bda96SJonathan Lemon 		OCP_SERIAL_RESOURCE(mac_port),
568773bda96SJonathan Lemon 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
569773bda96SJonathan Lemon 	},
570773bda96SJonathan Lemon 	{
571e3516bb4SJonathan Lemon 		OCP_SERIAL_RESOURCE(nmea_port),
572e3516bb4SJonathan Lemon 		.offset = 0x00190000 + 0x1000, .irq_vec = 10,
573e3516bb4SJonathan Lemon 	},
574e3516bb4SJonathan Lemon 	{
575773bda96SJonathan Lemon 		OCP_SPI_RESOURCE(spi_flash),
576773bda96SJonathan Lemon 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
577773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_flash_info) {
578773bda96SJonathan Lemon 			.name = "xilinx_spi", .pci_offset = 0,
579773bda96SJonathan Lemon 			.data_size = sizeof(struct xspi_platform_data),
580773bda96SJonathan Lemon 			.data = &(struct xspi_platform_data) {
581773bda96SJonathan Lemon 				.num_chipselect = 1,
582773bda96SJonathan Lemon 				.bits_per_word = 8,
583773bda96SJonathan Lemon 				.num_devices = 1,
584773bda96SJonathan Lemon 				.devices = &(struct spi_board_info) {
585773bda96SJonathan Lemon 					.modalias = "spi-nor",
586773bda96SJonathan Lemon 				},
587773bda96SJonathan Lemon 			},
588773bda96SJonathan Lemon 		},
589773bda96SJonathan Lemon 	},
590773bda96SJonathan Lemon 	{
591*2407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[0]),
592*2407f5d6SJonathan Lemon 		.offset = 0x01200000, .size = 0x10000,
593*2407f5d6SJonathan Lemon 	},
594*2407f5d6SJonathan Lemon 	{
595*2407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[1]),
596*2407f5d6SJonathan Lemon 		.offset = 0x01210000, .size = 0x10000,
597*2407f5d6SJonathan Lemon 	},
598*2407f5d6SJonathan Lemon 	{
599*2407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[2]),
600*2407f5d6SJonathan Lemon 		.offset = 0x01220000, .size = 0x10000,
601*2407f5d6SJonathan Lemon 	},
602*2407f5d6SJonathan Lemon 	{
603*2407f5d6SJonathan Lemon 		OCP_MEM_RESOURCE(freq_in[3]),
604*2407f5d6SJonathan Lemon 		.offset = 0x01230000, .size = 0x10000,
605*2407f5d6SJonathan Lemon 	},
606*2407f5d6SJonathan Lemon 	{
607773bda96SJonathan Lemon 		.setup = ptp_ocp_fb_board_init,
608773bda96SJonathan Lemon 	},
609773bda96SJonathan Lemon 	{ }
610773bda96SJonathan Lemon };
611773bda96SJonathan Lemon 
612773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = {
613773bda96SJonathan Lemon 	{ PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
614773bda96SJonathan Lemon 	{ 0 }
615773bda96SJonathan Lemon };
616773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
617773bda96SJonathan Lemon 
618773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock);
619773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr);
620773bda96SJonathan Lemon 
621e1daf0ecSJonathan Lemon struct ocp_selector {
622773bda96SJonathan Lemon 	const char *name;
623773bda96SJonathan Lemon 	int value;
624e1daf0ecSJonathan Lemon };
625e1daf0ecSJonathan Lemon 
626e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = {
627773bda96SJonathan Lemon 	{ .name = "NONE",	.value = 0 },
628773bda96SJonathan Lemon 	{ .name = "TOD",	.value = 1 },
629773bda96SJonathan Lemon 	{ .name = "IRIG",	.value = 2 },
630773bda96SJonathan Lemon 	{ .name = "PPS",	.value = 3 },
631773bda96SJonathan Lemon 	{ .name = "PTP",	.value = 4 },
632773bda96SJonathan Lemon 	{ .name = "RTC",	.value = 5 },
633773bda96SJonathan Lemon 	{ .name = "DCF",	.value = 6 },
634773bda96SJonathan Lemon 	{ .name = "REGS",	.value = 0xfe },
635773bda96SJonathan Lemon 	{ .name = "EXT",	.value = 0xff },
636e1daf0ecSJonathan Lemon 	{ }
637e1daf0ecSJonathan Lemon };
638e1daf0ecSJonathan Lemon 
639a509a7c6SJonathan Lemon #define SMA_ENABLE		BIT(15)
640a509a7c6SJonathan Lemon #define SMA_SELECT_MASK		((1U << 15) - 1)
641b2c4f0acSJonathan Lemon #define SMA_DISABLE		0x10000
642a509a7c6SJonathan Lemon 
643e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = {
644*2407f5d6SJonathan Lemon 	{ .name = "10Mhz",	.value = 0x0000 },
645*2407f5d6SJonathan Lemon 	{ .name = "PPS1",	.value = 0x0001 },
646*2407f5d6SJonathan Lemon 	{ .name = "PPS2",	.value = 0x0002 },
647*2407f5d6SJonathan Lemon 	{ .name = "TS1",	.value = 0x0004 },
648*2407f5d6SJonathan Lemon 	{ .name = "TS2",	.value = 0x0008 },
649*2407f5d6SJonathan Lemon 	{ .name = "IRIG",	.value = 0x0010 },
650*2407f5d6SJonathan Lemon 	{ .name = "DCF",	.value = 0x0020 },
651*2407f5d6SJonathan Lemon 	{ .name = "FREQ1",	.value = 0x0100 },
652*2407f5d6SJonathan Lemon 	{ .name = "FREQ2",	.value = 0x0200 },
653*2407f5d6SJonathan Lemon 	{ .name = "FREQ3",	.value = 0x0400 },
654*2407f5d6SJonathan Lemon 	{ .name = "FREQ4",	.value = 0x0800 },
655b2c4f0acSJonathan Lemon 	{ .name = "None",	.value = SMA_DISABLE },
656e1daf0ecSJonathan Lemon 	{ }
657e1daf0ecSJonathan Lemon };
658e1daf0ecSJonathan Lemon 
659e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = {
660b325af3cSJonathan Lemon 	{ .name = "10Mhz",	.value = 0x0000 },
661b325af3cSJonathan Lemon 	{ .name = "PHC",	.value = 0x0001 },
662b325af3cSJonathan Lemon 	{ .name = "MAC",	.value = 0x0002 },
663b325af3cSJonathan Lemon 	{ .name = "GNSS1",	.value = 0x0004 },
664b325af3cSJonathan Lemon 	{ .name = "GNSS2",	.value = 0x0008 },
665b325af3cSJonathan Lemon 	{ .name = "IRIG",	.value = 0x0010 },
666b325af3cSJonathan Lemon 	{ .name = "DCF",	.value = 0x0020 },
667b325af3cSJonathan Lemon 	{ .name = "GEN1",	.value = 0x0040 },
668b325af3cSJonathan Lemon 	{ .name = "GEN2",	.value = 0x0080 },
669b325af3cSJonathan Lemon 	{ .name = "GEN3",	.value = 0x0100 },
670b325af3cSJonathan Lemon 	{ .name = "GEN4",	.value = 0x0200 },
671cd09193fSJonathan Lemon 	{ .name = "GND",	.value = 0x2000 },
672cd09193fSJonathan Lemon 	{ .name = "VCC",	.value = 0x4000 },
673e1daf0ecSJonathan Lemon 	{ }
674773bda96SJonathan Lemon };
675773bda96SJonathan Lemon 
676773bda96SJonathan Lemon static const char *
677e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val)
678773bda96SJonathan Lemon {
679773bda96SJonathan Lemon 	int i;
680773bda96SJonathan Lemon 
681e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++)
682e1daf0ecSJonathan Lemon 		if (tbl[i].value == val)
683e1daf0ecSJonathan Lemon 			return tbl[i].name;
684773bda96SJonathan Lemon 	return NULL;
685773bda96SJonathan Lemon }
686773bda96SJonathan Lemon 
687773bda96SJonathan Lemon static int
688e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name)
689773bda96SJonathan Lemon {
690e1daf0ecSJonathan Lemon 	const char *select;
691773bda96SJonathan Lemon 	int i;
692773bda96SJonathan Lemon 
693e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++) {
694e1daf0ecSJonathan Lemon 		select = tbl[i].name;
695e1daf0ecSJonathan Lemon 		if (!strncasecmp(name, select, strlen(select)))
696e1daf0ecSJonathan Lemon 			return tbl[i].value;
697773bda96SJonathan Lemon 	}
698773bda96SJonathan Lemon 	return -EINVAL;
699773bda96SJonathan Lemon }
700773bda96SJonathan Lemon 
701e1daf0ecSJonathan Lemon static ssize_t
702e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf)
703e1daf0ecSJonathan Lemon {
704e1daf0ecSJonathan Lemon 	ssize_t count;
705e1daf0ecSJonathan Lemon 	int i;
706e1daf0ecSJonathan Lemon 
707e1daf0ecSJonathan Lemon 	count = 0;
708e1daf0ecSJonathan Lemon 	for (i = 0; tbl[i].name; i++)
709e1daf0ecSJonathan Lemon 		count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
710e1daf0ecSJonathan Lemon 	if (count)
711e1daf0ecSJonathan Lemon 		count--;
712e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "\n");
713e1daf0ecSJonathan Lemon 	return count;
714e1daf0ecSJonathan Lemon }
715e1daf0ecSJonathan Lemon 
716a7e1abadSJonathan Lemon static int
717a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
718a7e1abadSJonathan Lemon 			 struct ptp_system_timestamp *sts)
719a7e1abadSJonathan Lemon {
720a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
721a7e1abadSJonathan Lemon 	int i;
722a7e1abadSJonathan Lemon 
723a7e1abadSJonathan Lemon 	ptp_read_system_prets(sts);
7241acffc6eSJonathan Lemon 
7251acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
726a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
727a7e1abadSJonathan Lemon 
728a7e1abadSJonathan Lemon 	for (i = 0; i < 100; i++) {
729a7e1abadSJonathan Lemon 		ctrl = ioread32(&bp->reg->ctrl);
730a7e1abadSJonathan Lemon 		if (ctrl & OCP_CTRL_READ_TIME_DONE)
731a7e1abadSJonathan Lemon 			break;
732a7e1abadSJonathan Lemon 	}
733a7e1abadSJonathan Lemon 	ptp_read_system_postts(sts);
734a7e1abadSJonathan Lemon 
7351acffc6eSJonathan Lemon 	if (sts && bp->ts_window_adjust) {
7361acffc6eSJonathan Lemon 		s64 ns = timespec64_to_ns(&sts->post_ts);
7371acffc6eSJonathan Lemon 
7381acffc6eSJonathan Lemon 		sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
7391acffc6eSJonathan Lemon 	}
7401acffc6eSJonathan Lemon 
741a7e1abadSJonathan Lemon 	time_ns = ioread32(&bp->reg->time_ns);
742a7e1abadSJonathan Lemon 	time_sec = ioread32(&bp->reg->time_sec);
743a7e1abadSJonathan Lemon 
744a7e1abadSJonathan Lemon 	ts->tv_sec = time_sec;
745a7e1abadSJonathan Lemon 	ts->tv_nsec = time_ns;
746a7e1abadSJonathan Lemon 
747a7e1abadSJonathan Lemon 	return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
748a7e1abadSJonathan Lemon }
749a7e1abadSJonathan Lemon 
750a7e1abadSJonathan Lemon static int
751a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
752a7e1abadSJonathan Lemon 		 struct ptp_system_timestamp *sts)
753a7e1abadSJonathan Lemon {
754a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
755a7e1abadSJonathan Lemon 	unsigned long flags;
756a7e1abadSJonathan Lemon 	int err;
757a7e1abadSJonathan Lemon 
758a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
759a7e1abadSJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, ts, sts);
760a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
761a7e1abadSJonathan Lemon 
762a7e1abadSJonathan Lemon 	return err;
763a7e1abadSJonathan Lemon }
764a7e1abadSJonathan Lemon 
765a7e1abadSJonathan Lemon static void
766a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
767a7e1abadSJonathan Lemon {
768a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
769a7e1abadSJonathan Lemon 	u32 select;
770a7e1abadSJonathan Lemon 
771a7e1abadSJonathan Lemon 	time_ns = ts->tv_nsec;
772a7e1abadSJonathan Lemon 	time_sec = ts->tv_sec;
773a7e1abadSJonathan Lemon 
774a7e1abadSJonathan Lemon 	select = ioread32(&bp->reg->select);
775a7e1abadSJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
776a7e1abadSJonathan Lemon 
777a7e1abadSJonathan Lemon 	iowrite32(time_ns, &bp->reg->adjust_ns);
778a7e1abadSJonathan Lemon 	iowrite32(time_sec, &bp->reg->adjust_sec);
779a7e1abadSJonathan Lemon 
7801acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
781a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
782a7e1abadSJonathan Lemon 
783a7e1abadSJonathan Lemon 	/* restore clock selection */
784a7e1abadSJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
785a7e1abadSJonathan Lemon }
786a7e1abadSJonathan Lemon 
787a7e1abadSJonathan Lemon static int
788a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
789a7e1abadSJonathan Lemon {
790a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
791a7e1abadSJonathan Lemon 	unsigned long flags;
792a7e1abadSJonathan Lemon 
793a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
794a7e1abadSJonathan Lemon 	__ptp_ocp_settime_locked(bp, ts);
795a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
796a7e1abadSJonathan Lemon 
797a7e1abadSJonathan Lemon 	return 0;
798a7e1abadSJonathan Lemon }
799a7e1abadSJonathan Lemon 
8006d59d4faSJonathan Lemon static void
80190f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
8026d59d4faSJonathan Lemon {
8036d59d4faSJonathan Lemon 	u32 select, ctrl;
8046d59d4faSJonathan Lemon 
8056d59d4faSJonathan Lemon 	select = ioread32(&bp->reg->select);
8066d59d4faSJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
8076d59d4faSJonathan Lemon 
8086d59d4faSJonathan Lemon 	iowrite32(adj_val, &bp->reg->offset_ns);
80990f8f4c0SJonathan Lemon 	iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
8106d59d4faSJonathan Lemon 
8116d59d4faSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
8126d59d4faSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
8136d59d4faSJonathan Lemon 
8146d59d4faSJonathan Lemon 	/* restore clock selection */
8156d59d4faSJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
8166d59d4faSJonathan Lemon }
8176d59d4faSJonathan Lemon 
81890f8f4c0SJonathan Lemon static void
81990f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns)
82090f8f4c0SJonathan Lemon {
82190f8f4c0SJonathan Lemon 	struct timespec64 ts;
82290f8f4c0SJonathan Lemon 	unsigned long flags;
82390f8f4c0SJonathan Lemon 	int err;
82490f8f4c0SJonathan Lemon 
82590f8f4c0SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
82690f8f4c0SJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
82790f8f4c0SJonathan Lemon 	if (likely(!err)) {
82890f8f4c0SJonathan Lemon 		timespec64_add_ns(&ts, delta_ns);
82990f8f4c0SJonathan Lemon 		__ptp_ocp_settime_locked(bp, &ts);
83090f8f4c0SJonathan Lemon 	}
83190f8f4c0SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
83290f8f4c0SJonathan Lemon }
83390f8f4c0SJonathan Lemon 
834a7e1abadSJonathan Lemon static int
835a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
836a7e1abadSJonathan Lemon {
837a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
838a7e1abadSJonathan Lemon 	unsigned long flags;
8396d59d4faSJonathan Lemon 	u32 adj_ns, sign;
840a7e1abadSJonathan Lemon 
84190f8f4c0SJonathan Lemon 	if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
84290f8f4c0SJonathan Lemon 		ptp_ocp_adjtime_coarse(bp, delta_ns);
84390f8f4c0SJonathan Lemon 		return 0;
84490f8f4c0SJonathan Lemon 	}
84590f8f4c0SJonathan Lemon 
8466d59d4faSJonathan Lemon 	sign = delta_ns < 0 ? BIT(31) : 0;
8476d59d4faSJonathan Lemon 	adj_ns = sign ? -delta_ns : delta_ns;
848a7e1abadSJonathan Lemon 
849a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
8506d59d4faSJonathan Lemon 	__ptp_ocp_adjtime_locked(bp, sign | adj_ns);
851a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
852a7e1abadSJonathan Lemon 
8536d59d4faSJonathan Lemon 	return 0;
854a7e1abadSJonathan Lemon }
855a7e1abadSJonathan Lemon 
856a7e1abadSJonathan Lemon static int
857a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
858a7e1abadSJonathan Lemon {
859a7e1abadSJonathan Lemon 	if (scaled_ppm == 0)
860a7e1abadSJonathan Lemon 		return 0;
861a7e1abadSJonathan Lemon 
862a7e1abadSJonathan Lemon 	return -EOPNOTSUPP;
863a7e1abadSJonathan Lemon }
864a7e1abadSJonathan Lemon 
865773bda96SJonathan Lemon static int
8666d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
867773bda96SJonathan Lemon {
868773bda96SJonathan Lemon 	return -EOPNOTSUPP;
869773bda96SJonathan Lemon }
870773bda96SJonathan Lemon 
871773bda96SJonathan Lemon static int
872773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
873773bda96SJonathan Lemon 	       int on)
874773bda96SJonathan Lemon {
875773bda96SJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
876773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = NULL;
877a62a56d0SJonathan Lemon 	u32 req;
878773bda96SJonathan Lemon 	int err;
879773bda96SJonathan Lemon 
880773bda96SJonathan Lemon 	switch (rq->type) {
881773bda96SJonathan Lemon 	case PTP_CLK_REQ_EXTTS:
882a62a56d0SJonathan Lemon 		req = OCP_REQ_TIMESTAMP;
883773bda96SJonathan Lemon 		switch (rq->extts.index) {
884773bda96SJonathan Lemon 		case 0:
885773bda96SJonathan Lemon 			ext = bp->ts0;
886773bda96SJonathan Lemon 			break;
887773bda96SJonathan Lemon 		case 1:
888773bda96SJonathan Lemon 			ext = bp->ts1;
889773bda96SJonathan Lemon 			break;
890dcf61469SJonathan Lemon 		case 2:
891dcf61469SJonathan Lemon 			ext = bp->ts2;
892dcf61469SJonathan Lemon 			break;
893a62a56d0SJonathan Lemon 		case 3:
894a62a56d0SJonathan Lemon 			ext = bp->pps;
895a62a56d0SJonathan Lemon 			break;
896773bda96SJonathan Lemon 		}
897773bda96SJonathan Lemon 		break;
898773bda96SJonathan Lemon 	case PTP_CLK_REQ_PPS:
899a62a56d0SJonathan Lemon 		req = OCP_REQ_PPS;
900773bda96SJonathan Lemon 		ext = bp->pps;
901773bda96SJonathan Lemon 		break;
902a62a56d0SJonathan Lemon 	case PTP_CLK_REQ_PEROUT:
9031aa66a3aSJonathan Lemon 		switch (rq->perout.index) {
9041aa66a3aSJonathan Lemon 		case 0:
905a62a56d0SJonathan Lemon 			/* This is a request for 1PPS on an output SMA.
906a62a56d0SJonathan Lemon 			 * Allow, but assume manual configuration.
907a62a56d0SJonathan Lemon 			 */
9081aa66a3aSJonathan Lemon 			if (on && (rq->perout.period.sec != 1 ||
9091aa66a3aSJonathan Lemon 				   rq->perout.period.nsec != 0))
9101aa66a3aSJonathan Lemon 				return -EINVAL;
911a62a56d0SJonathan Lemon 			return 0;
9121aa66a3aSJonathan Lemon 		case 1:
9131aa66a3aSJonathan Lemon 		case 2:
9141aa66a3aSJonathan Lemon 		case 3:
9151aa66a3aSJonathan Lemon 		case 4:
9161aa66a3aSJonathan Lemon 			req = rq->perout.index - 1;
9171aa66a3aSJonathan Lemon 			ext = bp->signal_out[req];
9181aa66a3aSJonathan Lemon 			err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
9191aa66a3aSJonathan Lemon 			if (err)
9201aa66a3aSJonathan Lemon 				return err;
9211aa66a3aSJonathan Lemon 			break;
9221aa66a3aSJonathan Lemon 		}
9231aa66a3aSJonathan Lemon 		break;
924773bda96SJonathan Lemon 	default:
925773bda96SJonathan Lemon 		return -EOPNOTSUPP;
926773bda96SJonathan Lemon 	}
927773bda96SJonathan Lemon 
928773bda96SJonathan Lemon 	err = -ENXIO;
929773bda96SJonathan Lemon 	if (ext)
930a62a56d0SJonathan Lemon 		err = ext->info->enable(ext, req, on);
931773bda96SJonathan Lemon 
932773bda96SJonathan Lemon 	return err;
933773bda96SJonathan Lemon }
934773bda96SJonathan Lemon 
9351aa66a3aSJonathan Lemon static int
9361aa66a3aSJonathan Lemon ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
9371aa66a3aSJonathan Lemon 	       enum ptp_pin_function func, unsigned chan)
9381aa66a3aSJonathan Lemon {
9391aa66a3aSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
9401aa66a3aSJonathan Lemon 	char buf[16];
9411aa66a3aSJonathan Lemon 
9421aa66a3aSJonathan Lemon 	if (func != PTP_PF_PEROUT)
9431aa66a3aSJonathan Lemon 		return -EOPNOTSUPP;
9441aa66a3aSJonathan Lemon 
9451aa66a3aSJonathan Lemon 	if (chan)
9461aa66a3aSJonathan Lemon 		sprintf(buf, "OUT: GEN%d", chan);
9471aa66a3aSJonathan Lemon 	else
9481aa66a3aSJonathan Lemon 		sprintf(buf, "OUT: PHC");
9491aa66a3aSJonathan Lemon 
9501aa66a3aSJonathan Lemon 	return ptp_ocp_sma_store(bp, buf, pin + 1);
9511aa66a3aSJonathan Lemon }
9521aa66a3aSJonathan Lemon 
953a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = {
954a7e1abadSJonathan Lemon 	.owner		= THIS_MODULE,
955a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
956a7e1abadSJonathan Lemon 	.max_adj	= 100000000,
957a7e1abadSJonathan Lemon 	.gettimex64	= ptp_ocp_gettimex,
958a7e1abadSJonathan Lemon 	.settime64	= ptp_ocp_settime,
959a7e1abadSJonathan Lemon 	.adjtime	= ptp_ocp_adjtime,
960a7e1abadSJonathan Lemon 	.adjfine	= ptp_ocp_null_adjfine,
9616d59d4faSJonathan Lemon 	.adjphase	= ptp_ocp_null_adjphase,
962773bda96SJonathan Lemon 	.enable		= ptp_ocp_enable,
9631aa66a3aSJonathan Lemon 	.verify		= ptp_ocp_verify,
964773bda96SJonathan Lemon 	.pps		= true,
965a62a56d0SJonathan Lemon 	.n_ext_ts	= 4,
9661aa66a3aSJonathan Lemon 	.n_per_out	= 5,
967a7e1abadSJonathan Lemon };
968a7e1abadSJonathan Lemon 
969773bda96SJonathan Lemon static void
970773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
971773bda96SJonathan Lemon {
972773bda96SJonathan Lemon 	u32 ctrl, select;
973773bda96SJonathan Lemon 
974773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
975773bda96SJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
976773bda96SJonathan Lemon 
977773bda96SJonathan Lemon 	iowrite32(0, &bp->reg->drift_ns);
978773bda96SJonathan Lemon 
9791acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
980773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
981773bda96SJonathan Lemon 
982773bda96SJonathan Lemon 	/* restore clock selection */
983773bda96SJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
984773bda96SJonathan Lemon }
985773bda96SJonathan Lemon 
986773bda96SJonathan Lemon static void
987e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
988e68462a0SVadim Fedorenko {
989e68462a0SVadim Fedorenko 	unsigned long flags;
990e68462a0SVadim Fedorenko 
991e68462a0SVadim Fedorenko 	spin_lock_irqsave(&bp->lock, flags);
992e68462a0SVadim Fedorenko 
993e68462a0SVadim Fedorenko 	bp->utc_tai_offset = val;
994e68462a0SVadim Fedorenko 
995e68462a0SVadim Fedorenko 	if (bp->irig_out)
996e68462a0SVadim Fedorenko 		iowrite32(val, &bp->irig_out->adj_sec);
997e68462a0SVadim Fedorenko 	if (bp->dcf_out)
998e68462a0SVadim Fedorenko 		iowrite32(val, &bp->dcf_out->adj_sec);
999e68462a0SVadim Fedorenko 	if (bp->nmea_out)
1000e68462a0SVadim Fedorenko 		iowrite32(val, &bp->nmea_out->adj_sec);
1001e68462a0SVadim Fedorenko 
1002e68462a0SVadim Fedorenko 	spin_unlock_irqrestore(&bp->lock, flags);
1003e68462a0SVadim Fedorenko }
1004e68462a0SVadim Fedorenko 
1005e68462a0SVadim Fedorenko static void
1006773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t)
1007773bda96SJonathan Lemon {
1008773bda96SJonathan Lemon 	struct ptp_ocp *bp = from_timer(bp, t, watchdog);
1009773bda96SJonathan Lemon 	unsigned long flags;
1010e68462a0SVadim Fedorenko 	u32 status, utc_offset;
1011773bda96SJonathan Lemon 
10120d43d4f2SJonathan Lemon 	status = ioread32(&bp->pps_to_clk->status);
1013773bda96SJonathan Lemon 
1014773bda96SJonathan Lemon 	if (status & PPS_STATUS_SUPERV_ERR) {
10150d43d4f2SJonathan Lemon 		iowrite32(status, &bp->pps_to_clk->status);
1016ef0cfb34SJonathan Lemon 		if (!bp->gnss_lost) {
1017773bda96SJonathan Lemon 			spin_lock_irqsave(&bp->lock, flags);
1018773bda96SJonathan Lemon 			__ptp_ocp_clear_drift_locked(bp);
1019773bda96SJonathan Lemon 			spin_unlock_irqrestore(&bp->lock, flags);
1020ef0cfb34SJonathan Lemon 			bp->gnss_lost = ktime_get_real_seconds();
1021773bda96SJonathan Lemon 		}
1022773bda96SJonathan Lemon 
1023ef0cfb34SJonathan Lemon 	} else if (bp->gnss_lost) {
1024ef0cfb34SJonathan Lemon 		bp->gnss_lost = 0;
1025773bda96SJonathan Lemon 	}
1026773bda96SJonathan Lemon 
1027e68462a0SVadim Fedorenko 	/* if GNSS provides correct data we can rely on
1028e68462a0SVadim Fedorenko 	 * it to get leap second information
1029e68462a0SVadim Fedorenko 	 */
1030e68462a0SVadim Fedorenko 	if (bp->tod) {
1031e68462a0SVadim Fedorenko 		status = ioread32(&bp->tod->utc_status);
1032e68462a0SVadim Fedorenko 		utc_offset = status & TOD_STATUS_UTC_MASK;
1033e68462a0SVadim Fedorenko 		if (status & TOD_STATUS_UTC_VALID &&
1034e68462a0SVadim Fedorenko 		    utc_offset != bp->utc_tai_offset)
1035e68462a0SVadim Fedorenko 			ptp_ocp_utc_distribute(bp, utc_offset);
1036e68462a0SVadim Fedorenko 	}
1037e68462a0SVadim Fedorenko 
1038773bda96SJonathan Lemon 	mod_timer(&bp->watchdog, jiffies + HZ);
1039773bda96SJonathan Lemon }
1040773bda96SJonathan Lemon 
10411acffc6eSJonathan Lemon static void
10421acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
10431acffc6eSJonathan Lemon {
10441acffc6eSJonathan Lemon 	ktime_t start, end;
10451acffc6eSJonathan Lemon 	ktime_t delay;
10461acffc6eSJonathan Lemon 	u32 ctrl;
10471acffc6eSJonathan Lemon 
10481acffc6eSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
10491acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
10501acffc6eSJonathan Lemon 
10511acffc6eSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
10521acffc6eSJonathan Lemon 
10531acffc6eSJonathan Lemon 	start = ktime_get_ns();
10541acffc6eSJonathan Lemon 
10551acffc6eSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
10561acffc6eSJonathan Lemon 
10571acffc6eSJonathan Lemon 	end = ktime_get_ns();
10581acffc6eSJonathan Lemon 
10591acffc6eSJonathan Lemon 	delay = end - start;
10601acffc6eSJonathan Lemon 	bp->ts_window_adjust = (delay >> 5) * 3;
10611acffc6eSJonathan Lemon }
10621acffc6eSJonathan Lemon 
1063a7e1abadSJonathan Lemon static int
1064773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp)
1065a7e1abadSJonathan Lemon {
1066a7e1abadSJonathan Lemon 	struct timespec64 ts;
1067a7e1abadSJonathan Lemon 	bool sync;
1068a7e1abadSJonathan Lemon 	u32 ctrl;
1069a7e1abadSJonathan Lemon 
10701acffc6eSJonathan Lemon 	ctrl = OCP_CTRL_ENABLE;
1071a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
1072a7e1abadSJonathan Lemon 
1073773bda96SJonathan Lemon 	/* NO DRIFT Correction */
1074773bda96SJonathan Lemon 	/* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
1075773bda96SJonathan Lemon 	iowrite32(0x2000, &bp->reg->servo_offset_p);
1076773bda96SJonathan Lemon 	iowrite32(0x1000, &bp->reg->servo_offset_i);
1077773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_p);
1078773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_i);
1079773bda96SJonathan Lemon 
1080773bda96SJonathan Lemon 	/* latch servo values */
1081773bda96SJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_SERVO;
1082773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
1083773bda96SJonathan Lemon 
1084a7e1abadSJonathan Lemon 	if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
1085a7e1abadSJonathan Lemon 		dev_err(&bp->pdev->dev, "clock not enabled\n");
1086a7e1abadSJonathan Lemon 		return -ENODEV;
1087a7e1abadSJonathan Lemon 	}
1088a7e1abadSJonathan Lemon 
10891acffc6eSJonathan Lemon 	ptp_ocp_estimate_pci_timing(bp);
10901acffc6eSJonathan Lemon 
1091a7e1abadSJonathan Lemon 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
1092a7e1abadSJonathan Lemon 	if (!sync) {
1093065efcc5SJonathan Lemon 		ktime_get_clocktai_ts64(&ts);
1094a7e1abadSJonathan Lemon 		ptp_ocp_settime(&bp->ptp_info, &ts);
1095a7e1abadSJonathan Lemon 	}
1096a7e1abadSJonathan Lemon 
1097065efcc5SJonathan Lemon 	/* If there is a clock supervisor, then enable the watchdog */
1098065efcc5SJonathan Lemon 	if (bp->pps_to_clk) {
1099773bda96SJonathan Lemon 		timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
1100773bda96SJonathan Lemon 		mod_timer(&bp->watchdog, jiffies + HZ);
1101065efcc5SJonathan Lemon 	}
1102773bda96SJonathan Lemon 
1103a7e1abadSJonathan Lemon 	return 0;
1104a7e1abadSJonathan Lemon }
1105a7e1abadSJonathan Lemon 
1106a7e1abadSJonathan Lemon static void
1107065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp)
1108065efcc5SJonathan Lemon {
1109065efcc5SJonathan Lemon 	u32 ctrl, reg;
1110065efcc5SJonathan Lemon 
1111065efcc5SJonathan Lemon 	ctrl = ioread32(&bp->tod->ctrl);
1112065efcc5SJonathan Lemon 	ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
1113065efcc5SJonathan Lemon 	ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
1114065efcc5SJonathan Lemon 	iowrite32(ctrl, &bp->tod->ctrl);
1115065efcc5SJonathan Lemon 
1116065efcc5SJonathan Lemon 	reg = ioread32(&bp->tod->utc_status);
1117065efcc5SJonathan Lemon 	if (reg & TOD_STATUS_UTC_VALID)
1118065efcc5SJonathan Lemon 		ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
1119065efcc5SJonathan Lemon }
1120065efcc5SJonathan Lemon 
11219f492c4cSVadim Fedorenko static const char *
11229f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx)
1123a7e1abadSJonathan Lemon {
1124a7e1abadSJonathan Lemon 	static const char * const proto_name[] = {
1125a7e1abadSJonathan Lemon 		"NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
1126a7e1abadSJonathan Lemon 		"UBX", "UBX_UTC", "UBX_LS", "UBX_none"
1127a7e1abadSJonathan Lemon 	};
11289f492c4cSVadim Fedorenko 	return proto_name[idx];
11299f492c4cSVadim Fedorenko }
11309f492c4cSVadim Fedorenko 
11319f492c4cSVadim Fedorenko static const char *
11329f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx)
11339f492c4cSVadim Fedorenko {
1134a7e1abadSJonathan Lemon 	static const char * const gnss_name[] = {
1135a7e1abadSJonathan Lemon 		"ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
11369f492c4cSVadim Fedorenko 		"Unknown"
1137a7e1abadSJonathan Lemon 	};
113872f00505SDan Carpenter 	if (idx >= ARRAY_SIZE(gnss_name))
11399f492c4cSVadim Fedorenko 		idx = ARRAY_SIZE(gnss_name) - 1;
11409f492c4cSVadim Fedorenko 	return gnss_name[idx];
1141a7e1abadSJonathan Lemon }
1142a7e1abadSJonathan Lemon 
11430cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info {
11440cfcdd1eSJonathan Lemon 	struct ptp_ocp *bp;
11450cfcdd1eSJonathan Lemon 	const void * const tag;
1146773bda96SJonathan Lemon };
1147773bda96SJonathan Lemon 
11480cfcdd1eSJonathan Lemon static int
11490cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data)
11500cfcdd1eSJonathan Lemon {
11510cfcdd1eSJonathan Lemon 	const struct ptp_ocp_nvmem_match_info *info = data;
11520cfcdd1eSJonathan Lemon 
11530cfcdd1eSJonathan Lemon 	dev = dev->parent;
11540cfcdd1eSJonathan Lemon 	if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
11550cfcdd1eSJonathan Lemon 		return 0;
11560cfcdd1eSJonathan Lemon 
11570cfcdd1eSJonathan Lemon 	while ((dev = dev->parent))
11580cfcdd1eSJonathan Lemon 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
11590cfcdd1eSJonathan Lemon 			return info->bp == dev_get_drvdata(dev);
1160773bda96SJonathan Lemon 	return 0;
1161773bda96SJonathan Lemon }
1162773bda96SJonathan Lemon 
11630cfcdd1eSJonathan Lemon static inline struct nvmem_device *
11640cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
1165773bda96SJonathan Lemon {
11660cfcdd1eSJonathan Lemon 	struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
11670cfcdd1eSJonathan Lemon 
11680cfcdd1eSJonathan Lemon 	return nvmem_device_find(&info, ptp_ocp_nvmem_match);
11690cfcdd1eSJonathan Lemon }
11700cfcdd1eSJonathan Lemon 
11710cfcdd1eSJonathan Lemon static inline void
11720cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
11730cfcdd1eSJonathan Lemon {
11740cfcdd1eSJonathan Lemon 	if (*nvmemp != NULL) {
11750cfcdd1eSJonathan Lemon 		nvmem_device_put(*nvmemp);
11760cfcdd1eSJonathan Lemon 		*nvmemp = NULL;
11770cfcdd1eSJonathan Lemon 	}
11780cfcdd1eSJonathan Lemon }
11790cfcdd1eSJonathan Lemon 
11800cfcdd1eSJonathan Lemon static void
11810cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp)
11820cfcdd1eSJonathan Lemon {
11830cfcdd1eSJonathan Lemon 	const struct ptp_ocp_eeprom_map *map;
11840cfcdd1eSJonathan Lemon 	struct nvmem_device *nvmem;
11850cfcdd1eSJonathan Lemon 	const void *tag;
11860cfcdd1eSJonathan Lemon 	int ret;
1187773bda96SJonathan Lemon 
11881447149dSJonathan Lemon 	if (!bp->i2c_ctrl)
11891447149dSJonathan Lemon 		return;
11901447149dSJonathan Lemon 
11910cfcdd1eSJonathan Lemon 	tag = NULL;
11920cfcdd1eSJonathan Lemon 	nvmem = NULL;
1193773bda96SJonathan Lemon 
11940cfcdd1eSJonathan Lemon 	for (map = bp->eeprom_map; map->len; map++) {
11950cfcdd1eSJonathan Lemon 		if (map->tag != tag) {
11960cfcdd1eSJonathan Lemon 			tag = map->tag;
11970cfcdd1eSJonathan Lemon 			ptp_ocp_nvmem_device_put(&nvmem);
11980cfcdd1eSJonathan Lemon 		}
11990cfcdd1eSJonathan Lemon 		if (!nvmem) {
12000cfcdd1eSJonathan Lemon 			nvmem = ptp_ocp_nvmem_device_get(bp, tag);
12010cfcdd1eSJonathan Lemon 			if (!nvmem)
1202773bda96SJonathan Lemon 				goto out;
1203773bda96SJonathan Lemon 		}
12040cfcdd1eSJonathan Lemon 		ret = nvmem_device_read(nvmem, map->off, map->len,
12050cfcdd1eSJonathan Lemon 					BP_MAP_ENTRY_ADDR(bp, map));
12060cfcdd1eSJonathan Lemon 		if (ret != map->len)
12070cfcdd1eSJonathan Lemon 			goto read_fail;
1208773bda96SJonathan Lemon 	}
1209773bda96SJonathan Lemon 
12100cfcdd1eSJonathan Lemon 	bp->has_eeprom_data = true;
1211773bda96SJonathan Lemon 
1212773bda96SJonathan Lemon out:
12130cfcdd1eSJonathan Lemon 	ptp_ocp_nvmem_device_put(&nvmem);
12140cfcdd1eSJonathan Lemon 	return;
12150cfcdd1eSJonathan Lemon 
12160cfcdd1eSJonathan Lemon read_fail:
12170cfcdd1eSJonathan Lemon 	dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
12180cfcdd1eSJonathan Lemon 	goto out;
12190cfcdd1eSJonathan Lemon }
12200cfcdd1eSJonathan Lemon 
12210cfcdd1eSJonathan Lemon static int
12220cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data)
12230cfcdd1eSJonathan Lemon {
12240cfcdd1eSJonathan Lemon 	return 1;
1225773bda96SJonathan Lemon }
1226773bda96SJonathan Lemon 
1227773bda96SJonathan Lemon static struct device *
1228773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp)
1229773bda96SJonathan Lemon {
1230773bda96SJonathan Lemon 	struct device *dev, *last;
1231773bda96SJonathan Lemon 
1232773bda96SJonathan Lemon 	last = NULL;
1233773bda96SJonathan Lemon 	dev = &bp->spi_flash->dev;
1234773bda96SJonathan Lemon 
1235773bda96SJonathan Lemon 	while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) {
1236773bda96SJonathan Lemon 		if (!strcmp("mtd", dev_bus_name(dev)))
1237773bda96SJonathan Lemon 			break;
1238773bda96SJonathan Lemon 		put_device(last);
1239773bda96SJonathan Lemon 		last = dev;
1240773bda96SJonathan Lemon 	}
1241773bda96SJonathan Lemon 	put_device(last);
1242773bda96SJonathan Lemon 
1243773bda96SJonathan Lemon 	return dev;
1244773bda96SJonathan Lemon }
1245773bda96SJonathan Lemon 
1246773bda96SJonathan Lemon static int
1247773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1248773bda96SJonathan Lemon 		      const struct firmware *fw)
1249773bda96SJonathan Lemon {
1250773bda96SJonathan Lemon 	struct mtd_info *mtd = dev_get_drvdata(dev);
1251773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
1252773bda96SJonathan Lemon 	size_t off, len, resid, wrote;
1253773bda96SJonathan Lemon 	struct erase_info erase;
1254773bda96SJonathan Lemon 	size_t base, blksz;
12557c807572SJonathan Lemon 	int err = 0;
1256773bda96SJonathan Lemon 
1257773bda96SJonathan Lemon 	off = 0;
1258773bda96SJonathan Lemon 	base = bp->flash_start;
1259773bda96SJonathan Lemon 	blksz = 4096;
1260773bda96SJonathan Lemon 	resid = fw->size;
1261773bda96SJonathan Lemon 
1262773bda96SJonathan Lemon 	while (resid) {
1263773bda96SJonathan Lemon 		devlink_flash_update_status_notify(devlink, "Flashing",
1264773bda96SJonathan Lemon 						   NULL, off, fw->size);
1265773bda96SJonathan Lemon 
1266773bda96SJonathan Lemon 		len = min_t(size_t, resid, blksz);
1267773bda96SJonathan Lemon 		erase.addr = base + off;
1268773bda96SJonathan Lemon 		erase.len = blksz;
1269773bda96SJonathan Lemon 
1270773bda96SJonathan Lemon 		err = mtd_erase(mtd, &erase);
1271773bda96SJonathan Lemon 		if (err)
1272773bda96SJonathan Lemon 			goto out;
1273773bda96SJonathan Lemon 
1274773bda96SJonathan Lemon 		err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]);
1275773bda96SJonathan Lemon 		if (err)
1276773bda96SJonathan Lemon 			goto out;
1277773bda96SJonathan Lemon 
1278773bda96SJonathan Lemon 		off += blksz;
1279773bda96SJonathan Lemon 		resid -= len;
1280773bda96SJonathan Lemon 	}
1281773bda96SJonathan Lemon out:
1282773bda96SJonathan Lemon 	return err;
1283773bda96SJonathan Lemon }
1284773bda96SJonathan Lemon 
1285773bda96SJonathan Lemon static int
1286773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink,
1287773bda96SJonathan Lemon 			     struct devlink_flash_update_params *params,
1288773bda96SJonathan Lemon 			     struct netlink_ext_ack *extack)
1289773bda96SJonathan Lemon {
1290773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
1291773bda96SJonathan Lemon 	struct device *dev;
1292773bda96SJonathan Lemon 	const char *msg;
1293773bda96SJonathan Lemon 	int err;
1294773bda96SJonathan Lemon 
1295773bda96SJonathan Lemon 	dev = ptp_ocp_find_flash(bp);
1296773bda96SJonathan Lemon 	if (!dev) {
1297773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1298773bda96SJonathan Lemon 		return -ENODEV;
1299773bda96SJonathan Lemon 	}
1300773bda96SJonathan Lemon 
1301773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, "Preparing to flash",
1302773bda96SJonathan Lemon 					   NULL, 0, 0);
1303773bda96SJonathan Lemon 
1304773bda96SJonathan Lemon 	err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1305773bda96SJonathan Lemon 
1306773bda96SJonathan Lemon 	msg = err ? "Flash error" : "Flash complete";
1307773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1308773bda96SJonathan Lemon 
1309773bda96SJonathan Lemon 	put_device(dev);
1310773bda96SJonathan Lemon 	return err;
1311773bda96SJonathan Lemon }
1312773bda96SJonathan Lemon 
1313773bda96SJonathan Lemon static int
1314773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1315773bda96SJonathan Lemon 			 struct netlink_ext_ack *extack)
1316773bda96SJonathan Lemon {
1317773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
1318773bda96SJonathan Lemon 	char buf[32];
1319773bda96SJonathan Lemon 	int err;
1320773bda96SJonathan Lemon 
1321773bda96SJonathan Lemon 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
1322773bda96SJonathan Lemon 	if (err)
1323773bda96SJonathan Lemon 		return err;
1324773bda96SJonathan Lemon 
1325b0ca789aSJonathan Lemon 	if (bp->fw_version & 0xffff) {
1326b0ca789aSJonathan Lemon 		sprintf(buf, "%d", bp->fw_version);
1327b0ca789aSJonathan Lemon 		err = devlink_info_version_running_put(req, "fw", buf);
1328773bda96SJonathan Lemon 	} else {
1329b0ca789aSJonathan Lemon 		sprintf(buf, "%d", bp->fw_version >> 16);
1330b0ca789aSJonathan Lemon 		err = devlink_info_version_running_put(req, "loader", buf);
1331773bda96SJonathan Lemon 	}
1332773bda96SJonathan Lemon 	if (err)
1333773bda96SJonathan Lemon 		return err;
1334773bda96SJonathan Lemon 
13350cfcdd1eSJonathan Lemon 	if (!bp->has_eeprom_data) {
13360cfcdd1eSJonathan Lemon 		ptp_ocp_read_eeprom(bp);
13370cfcdd1eSJonathan Lemon 		if (!bp->has_eeprom_data)
13380cfcdd1eSJonathan Lemon 			return 0;
13390cfcdd1eSJonathan Lemon 	}
1340773bda96SJonathan Lemon 
1341773bda96SJonathan Lemon 	sprintf(buf, "%pM", bp->serial);
1342773bda96SJonathan Lemon 	err = devlink_info_serial_number_put(req, buf);
1343773bda96SJonathan Lemon 	if (err)
1344773bda96SJonathan Lemon 		return err;
13450cfcdd1eSJonathan Lemon 
13460cfcdd1eSJonathan Lemon 	err = devlink_info_version_fixed_put(req,
13470cfcdd1eSJonathan Lemon 			DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
13480cfcdd1eSJonathan Lemon 			bp->board_id);
13490cfcdd1eSJonathan Lemon 	if (err)
13500cfcdd1eSJonathan Lemon 		return err;
1351773bda96SJonathan Lemon 
1352773bda96SJonathan Lemon 	return 0;
1353773bda96SJonathan Lemon }
1354773bda96SJonathan Lemon 
1355773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = {
1356773bda96SJonathan Lemon 	.flash_update = ptp_ocp_devlink_flash_update,
1357773bda96SJonathan Lemon 	.info_get = ptp_ocp_devlink_info_get,
1358773bda96SJonathan Lemon };
1359773bda96SJonathan Lemon 
1360773bda96SJonathan Lemon static void __iomem *
1361773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size)
1362773bda96SJonathan Lemon {
1363773bda96SJonathan Lemon 	struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1364773bda96SJonathan Lemon 
1365773bda96SJonathan Lemon 	return devm_ioremap_resource(&bp->pdev->dev, &res);
1366773bda96SJonathan Lemon }
1367773bda96SJonathan Lemon 
1368773bda96SJonathan Lemon static void __iomem *
1369773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1370773bda96SJonathan Lemon {
1371773bda96SJonathan Lemon 	unsigned long start;
1372773bda96SJonathan Lemon 
1373773bda96SJonathan Lemon 	start = pci_resource_start(bp->pdev, 0) + r->offset;
1374773bda96SJonathan Lemon 	return __ptp_ocp_get_mem(bp, start, r->size);
1375773bda96SJonathan Lemon }
1376773bda96SJonathan Lemon 
1377773bda96SJonathan Lemon static void
1378773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq)
1379773bda96SJonathan Lemon {
1380773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_IRQ(irq);
1381773bda96SJonathan Lemon 	*res = r;
1382773bda96SJonathan Lemon }
1383773bda96SJonathan Lemon 
1384773bda96SJonathan Lemon static void
1385773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size)
1386773bda96SJonathan Lemon {
1387773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_MEM(start, size);
1388773bda96SJonathan Lemon 	*res = r;
1389773bda96SJonathan Lemon }
1390773bda96SJonathan Lemon 
1391773bda96SJonathan Lemon static int
1392773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1393773bda96SJonathan Lemon {
1394773bda96SJonathan Lemon 	struct ptp_ocp_flash_info *info;
1395773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1396773bda96SJonathan Lemon 	struct platform_device *p;
1397773bda96SJonathan Lemon 	struct resource res[2];
1398773bda96SJonathan Lemon 	unsigned long start;
1399773bda96SJonathan Lemon 	int id;
1400773bda96SJonathan Lemon 
1401773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
1402773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
1403773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1404773bda96SJonathan Lemon 
1405773bda96SJonathan Lemon 	info = r->extra;
1406773bda96SJonathan Lemon 	id = pci_dev_id(pdev) << 1;
1407773bda96SJonathan Lemon 	id += info->pci_offset;
1408773bda96SJonathan Lemon 
1409773bda96SJonathan Lemon 	p = platform_device_register_resndata(&pdev->dev, info->name, id,
1410773bda96SJonathan Lemon 					      res, 2, info->data,
1411773bda96SJonathan Lemon 					      info->data_size);
1412773bda96SJonathan Lemon 	if (IS_ERR(p))
1413773bda96SJonathan Lemon 		return PTR_ERR(p);
1414773bda96SJonathan Lemon 
1415773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
1416773bda96SJonathan Lemon 
1417773bda96SJonathan Lemon 	return 0;
1418773bda96SJonathan Lemon }
1419773bda96SJonathan Lemon 
1420773bda96SJonathan Lemon static struct platform_device *
1421773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1422773bda96SJonathan Lemon {
14231618df6aSJonathan Lemon 	struct ptp_ocp_i2c_info *info;
1424773bda96SJonathan Lemon 	struct resource res[2];
1425773bda96SJonathan Lemon 	unsigned long start;
1426773bda96SJonathan Lemon 
14271618df6aSJonathan Lemon 	info = r->extra;
1428773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
1429773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
1430773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1431773bda96SJonathan Lemon 
14321618df6aSJonathan Lemon 	return platform_device_register_resndata(&pdev->dev, info->name,
14331618df6aSJonathan Lemon 						 id, res, 2,
14341618df6aSJonathan Lemon 						 info->data, info->data_size);
1435773bda96SJonathan Lemon }
1436773bda96SJonathan Lemon 
1437773bda96SJonathan Lemon static int
1438773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1439773bda96SJonathan Lemon {
1440773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
14411618df6aSJonathan Lemon 	struct ptp_ocp_i2c_info *info;
1442773bda96SJonathan Lemon 	struct platform_device *p;
1443773bda96SJonathan Lemon 	struct clk_hw *clk;
1444773bda96SJonathan Lemon 	char buf[32];
1445773bda96SJonathan Lemon 	int id;
1446773bda96SJonathan Lemon 
14471618df6aSJonathan Lemon 	info = r->extra;
1448773bda96SJonathan Lemon 	id = pci_dev_id(bp->pdev);
1449773bda96SJonathan Lemon 
1450773bda96SJonathan Lemon 	sprintf(buf, "AXI.%d", id);
14511618df6aSJonathan Lemon 	clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
14521618df6aSJonathan Lemon 					 info->fixed_rate);
1453773bda96SJonathan Lemon 	if (IS_ERR(clk))
1454773bda96SJonathan Lemon 		return PTR_ERR(clk);
1455773bda96SJonathan Lemon 	bp->i2c_clk = clk;
1456773bda96SJonathan Lemon 
14571618df6aSJonathan Lemon 	sprintf(buf, "%s.%d", info->name, id);
1458773bda96SJonathan Lemon 	devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1459773bda96SJonathan Lemon 	p = ptp_ocp_i2c_bus(bp->pdev, r, id);
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 
1468b325af3cSJonathan Lemon /* The expectation is that this is triggered only on error. */
1469b325af3cSJonathan Lemon static irqreturn_t
1470b325af3cSJonathan Lemon ptp_ocp_signal_irq(int irq, void *priv)
1471b325af3cSJonathan Lemon {
1472b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1473b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = ext->mem;
1474b325af3cSJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1475b325af3cSJonathan Lemon 	u32 enable, status;
1476b325af3cSJonathan Lemon 	int gen;
1477b325af3cSJonathan Lemon 
1478b325af3cSJonathan Lemon 	gen = ext->info->index - 1;
1479b325af3cSJonathan Lemon 
1480b325af3cSJonathan Lemon 	enable = ioread32(&reg->enable);
1481b325af3cSJonathan Lemon 	status = ioread32(&reg->status);
1482b325af3cSJonathan Lemon 
1483b325af3cSJonathan Lemon 	/* disable generator on error */
1484b325af3cSJonathan Lemon 	if (status || !enable) {
1485b325af3cSJonathan Lemon 		iowrite32(0, &reg->intr_mask);
1486b325af3cSJonathan Lemon 		iowrite32(0, &reg->enable);
1487b325af3cSJonathan Lemon 		bp->signal[gen].running = false;
1488b325af3cSJonathan Lemon 	}
1489b325af3cSJonathan Lemon 
1490b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr);	/* ack interrupt */
1491b325af3cSJonathan Lemon 
1492b325af3cSJonathan Lemon 	return IRQ_HANDLED;
1493b325af3cSJonathan Lemon }
1494b325af3cSJonathan Lemon 
1495b325af3cSJonathan Lemon static int
1496b325af3cSJonathan Lemon ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
1497b325af3cSJonathan Lemon {
1498b325af3cSJonathan Lemon 	struct ptp_system_timestamp sts;
1499b325af3cSJonathan Lemon 	struct timespec64 ts;
1500b325af3cSJonathan Lemon 	ktime_t start_ns;
1501b325af3cSJonathan Lemon 	int err;
1502b325af3cSJonathan Lemon 
1503b325af3cSJonathan Lemon 	if (!s->period)
1504b325af3cSJonathan Lemon 		return 0;
1505b325af3cSJonathan Lemon 
1506b325af3cSJonathan Lemon 	if (!s->pulse)
1507b325af3cSJonathan Lemon 		s->pulse = ktime_divns(s->period * s->duty, 100);
1508b325af3cSJonathan Lemon 
1509b325af3cSJonathan Lemon 	err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
1510b325af3cSJonathan Lemon 	if (err)
1511b325af3cSJonathan Lemon 		return err;
1512b325af3cSJonathan Lemon 
1513b325af3cSJonathan Lemon 	start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
1514b325af3cSJonathan Lemon 	if (!s->start) {
1515b325af3cSJonathan Lemon 		/* roundup() does not work on 32-bit systems */
1516b325af3cSJonathan Lemon 		s->start = DIV_ROUND_UP_ULL(start_ns, s->period);
1517b325af3cSJonathan Lemon 		s->start = ktime_add(s->start, s->phase);
1518b325af3cSJonathan Lemon 	}
1519b325af3cSJonathan Lemon 
1520b325af3cSJonathan Lemon 	if (s->duty < 1 || s->duty > 99)
1521b325af3cSJonathan Lemon 		return -EINVAL;
1522b325af3cSJonathan Lemon 
1523b325af3cSJonathan Lemon 	if (s->pulse < 1 || s->pulse > s->period)
1524b325af3cSJonathan Lemon 		return -EINVAL;
1525b325af3cSJonathan Lemon 
1526b325af3cSJonathan Lemon 	if (s->start < start_ns)
1527b325af3cSJonathan Lemon 		return -EINVAL;
1528b325af3cSJonathan Lemon 
1529b325af3cSJonathan Lemon 	bp->signal[gen] = *s;
1530b325af3cSJonathan Lemon 
1531b325af3cSJonathan Lemon 	return 0;
1532b325af3cSJonathan Lemon }
1533b325af3cSJonathan Lemon 
1534b325af3cSJonathan Lemon static int
15351aa66a3aSJonathan Lemon ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
15361aa66a3aSJonathan Lemon 			   struct ptp_perout_request *req)
15371aa66a3aSJonathan Lemon {
15381aa66a3aSJonathan Lemon 	struct ptp_ocp_signal s = { };
15391aa66a3aSJonathan Lemon 
15401aa66a3aSJonathan Lemon 	s.polarity = bp->signal[gen].polarity;
15411aa66a3aSJonathan Lemon 	s.period = ktime_set(req->period.sec, req->period.nsec);
15421aa66a3aSJonathan Lemon 	if (!s.period)
15431aa66a3aSJonathan Lemon 		return 0;
15441aa66a3aSJonathan Lemon 
15451aa66a3aSJonathan Lemon 	if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
15461aa66a3aSJonathan Lemon 		s.pulse = ktime_set(req->on.sec, req->on.nsec);
15471aa66a3aSJonathan Lemon 		s.duty = ktime_divns(s.pulse * 100, s.period);
15481aa66a3aSJonathan Lemon 	}
15491aa66a3aSJonathan Lemon 
15501aa66a3aSJonathan Lemon 	if (req->flags & PTP_PEROUT_PHASE)
15511aa66a3aSJonathan Lemon 		s.phase = ktime_set(req->phase.sec, req->phase.nsec);
15521aa66a3aSJonathan Lemon 	else
15531aa66a3aSJonathan Lemon 		s.start = ktime_set(req->start.sec, req->start.nsec);
15541aa66a3aSJonathan Lemon 
15551aa66a3aSJonathan Lemon 	return ptp_ocp_signal_set(bp, gen, &s);
15561aa66a3aSJonathan Lemon }
15571aa66a3aSJonathan Lemon 
15581aa66a3aSJonathan Lemon static int
1559b325af3cSJonathan Lemon ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
1560b325af3cSJonathan Lemon {
1561b325af3cSJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1562b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = ext->mem;
1563b325af3cSJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1564b325af3cSJonathan Lemon 	struct timespec64 ts;
1565b325af3cSJonathan Lemon 	int gen;
1566b325af3cSJonathan Lemon 
1567b325af3cSJonathan Lemon 	gen = ext->info->index - 1;
1568b325af3cSJonathan Lemon 
1569b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr_mask);
1570b325af3cSJonathan Lemon 	iowrite32(0, &reg->enable);
1571b325af3cSJonathan Lemon 	bp->signal[gen].running = false;
1572b325af3cSJonathan Lemon 	if (!enable)
1573b325af3cSJonathan Lemon 		return 0;
1574b325af3cSJonathan Lemon 
1575b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].start);
1576b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->start_sec);
1577b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->start_ns);
1578b325af3cSJonathan Lemon 
1579b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].period);
1580b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->period_sec);
1581b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->period_ns);
1582b325af3cSJonathan Lemon 
1583b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[gen].pulse);
1584b325af3cSJonathan Lemon 	iowrite32(ts.tv_sec, &reg->pulse_sec);
1585b325af3cSJonathan Lemon 	iowrite32(ts.tv_nsec, &reg->pulse_ns);
1586b325af3cSJonathan Lemon 
1587b325af3cSJonathan Lemon 	iowrite32(bp->signal[gen].polarity, &reg->polarity);
1588b325af3cSJonathan Lemon 	iowrite32(0, &reg->repeat_count);
1589b325af3cSJonathan Lemon 
1590b325af3cSJonathan Lemon 	iowrite32(0, &reg->intr);		/* clear interrupt state */
1591b325af3cSJonathan Lemon 	iowrite32(1, &reg->intr_mask);		/* enable interrupt */
1592b325af3cSJonathan Lemon 	iowrite32(3, &reg->enable);		/* valid & enable */
1593b325af3cSJonathan Lemon 
1594b325af3cSJonathan Lemon 	bp->signal[gen].running = true;
1595b325af3cSJonathan Lemon 
1596b325af3cSJonathan Lemon 	return 0;
1597b325af3cSJonathan Lemon }
1598b325af3cSJonathan Lemon 
1599773bda96SJonathan Lemon static irqreturn_t
1600773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv)
1601773bda96SJonathan Lemon {
1602773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1603773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1604773bda96SJonathan Lemon 	struct ptp_clock_event ev;
1605773bda96SJonathan Lemon 	u32 sec, nsec;
1606773bda96SJonathan Lemon 
1607a62a56d0SJonathan Lemon 	if (ext == ext->bp->pps) {
1608a62a56d0SJonathan Lemon 		if (ext->bp->pps_req_map & OCP_REQ_PPS) {
1609a62a56d0SJonathan Lemon 			ev.type = PTP_CLOCK_PPS;
1610a62a56d0SJonathan Lemon 			ptp_clock_event(ext->bp->ptp, &ev);
1611a62a56d0SJonathan Lemon 		}
1612a62a56d0SJonathan Lemon 
1613a62a56d0SJonathan Lemon 		if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
1614a62a56d0SJonathan Lemon 			goto out;
1615a62a56d0SJonathan Lemon 	}
1616a62a56d0SJonathan Lemon 
1617773bda96SJonathan Lemon 	/* XXX should fix API - this converts s/ns -> ts -> s/ns */
1618773bda96SJonathan Lemon 	sec = ioread32(&reg->time_sec);
1619773bda96SJonathan Lemon 	nsec = ioread32(&reg->time_ns);
1620773bda96SJonathan Lemon 
1621773bda96SJonathan Lemon 	ev.type = PTP_CLOCK_EXTTS;
1622773bda96SJonathan Lemon 	ev.index = ext->info->index;
16231acffc6eSJonathan Lemon 	ev.timestamp = sec * NSEC_PER_SEC + nsec;
1624773bda96SJonathan Lemon 
1625773bda96SJonathan Lemon 	ptp_clock_event(ext->bp->ptp, &ev);
1626773bda96SJonathan Lemon 
1627a62a56d0SJonathan Lemon out:
1628773bda96SJonathan Lemon 	iowrite32(1, &reg->intr);	/* write 1 to ack */
1629773bda96SJonathan Lemon 
1630773bda96SJonathan Lemon 	return IRQ_HANDLED;
1631773bda96SJonathan Lemon }
1632773bda96SJonathan Lemon 
1633773bda96SJonathan Lemon static int
1634a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
1635773bda96SJonathan Lemon {
1636773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1637773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1638a62a56d0SJonathan Lemon 	struct ptp_ocp *bp = ext->bp;
1639a62a56d0SJonathan Lemon 
1640a62a56d0SJonathan Lemon 	if (ext == bp->pps) {
1641a62a56d0SJonathan Lemon 		u32 old_map = bp->pps_req_map;
1642a62a56d0SJonathan Lemon 
1643a62a56d0SJonathan Lemon 		if (enable)
1644a62a56d0SJonathan Lemon 			bp->pps_req_map |= req;
1645a62a56d0SJonathan Lemon 		else
1646a62a56d0SJonathan Lemon 			bp->pps_req_map &= ~req;
1647a62a56d0SJonathan Lemon 
1648a62a56d0SJonathan Lemon 		/* if no state change, just return */
1649a62a56d0SJonathan Lemon 		if ((!!old_map ^ !!bp->pps_req_map) == 0)
1650a62a56d0SJonathan Lemon 			return 0;
1651a62a56d0SJonathan Lemon 	}
1652773bda96SJonathan Lemon 
1653773bda96SJonathan Lemon 	if (enable) {
1654773bda96SJonathan Lemon 		iowrite32(1, &reg->enable);
1655773bda96SJonathan Lemon 		iowrite32(1, &reg->intr_mask);
1656773bda96SJonathan Lemon 		iowrite32(1, &reg->intr);
1657773bda96SJonathan Lemon 	} else {
1658773bda96SJonathan Lemon 		iowrite32(0, &reg->intr_mask);
1659773bda96SJonathan Lemon 		iowrite32(0, &reg->enable);
1660773bda96SJonathan Lemon 	}
1661773bda96SJonathan Lemon 
1662773bda96SJonathan Lemon 	return 0;
1663773bda96SJonathan Lemon }
1664773bda96SJonathan Lemon 
1665773bda96SJonathan Lemon static void
1666773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1667773bda96SJonathan Lemon {
1668a62a56d0SJonathan Lemon 	ext->info->enable(ext, ~0, false);
1669773bda96SJonathan Lemon 	pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1670773bda96SJonathan Lemon 	kfree(ext);
1671773bda96SJonathan Lemon }
1672773bda96SJonathan Lemon 
1673773bda96SJonathan Lemon static int
1674773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
1675773bda96SJonathan Lemon {
1676773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1677773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext;
1678773bda96SJonathan Lemon 	int err;
1679773bda96SJonathan Lemon 
1680773bda96SJonathan Lemon 	ext = kzalloc(sizeof(*ext), GFP_KERNEL);
1681773bda96SJonathan Lemon 	if (!ext)
1682773bda96SJonathan Lemon 		return -ENOMEM;
1683773bda96SJonathan Lemon 
1684773bda96SJonathan Lemon 	ext->mem = ptp_ocp_get_mem(bp, r);
1685c7521d3aSDan Carpenter 	if (IS_ERR(ext->mem)) {
1686c7521d3aSDan Carpenter 		err = PTR_ERR(ext->mem);
1687773bda96SJonathan Lemon 		goto out;
1688c7521d3aSDan Carpenter 	}
1689773bda96SJonathan Lemon 
1690773bda96SJonathan Lemon 	ext->bp = bp;
1691773bda96SJonathan Lemon 	ext->info = r->extra;
1692773bda96SJonathan Lemon 	ext->irq_vec = r->irq_vec;
1693773bda96SJonathan Lemon 
1694773bda96SJonathan Lemon 	err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
169556ec4403SJonathan Lemon 			      ext, "ocp%d.%s", bp->id, r->name);
1696773bda96SJonathan Lemon 	if (err) {
1697773bda96SJonathan Lemon 		dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
1698773bda96SJonathan Lemon 		goto out;
1699773bda96SJonathan Lemon 	}
1700773bda96SJonathan Lemon 
1701773bda96SJonathan Lemon 	bp_assign_entry(bp, r, ext);
1702773bda96SJonathan Lemon 
1703773bda96SJonathan Lemon 	return 0;
1704773bda96SJonathan Lemon 
1705773bda96SJonathan Lemon out:
1706773bda96SJonathan Lemon 	kfree(ext);
1707773bda96SJonathan Lemon 	return err;
1708773bda96SJonathan Lemon }
1709773bda96SJonathan Lemon 
1710773bda96SJonathan Lemon static int
1711773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
1712773bda96SJonathan Lemon {
1713773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1714773bda96SJonathan Lemon 	struct uart_8250_port uart;
1715773bda96SJonathan Lemon 
1716773bda96SJonathan Lemon 	/* Setting UPF_IOREMAP and leaving port.membase unspecified lets
1717773bda96SJonathan Lemon 	 * the serial port device claim and release the pci resource.
1718773bda96SJonathan Lemon 	 */
1719773bda96SJonathan Lemon 	memset(&uart, 0, sizeof(uart));
1720773bda96SJonathan Lemon 	uart.port.dev = &pdev->dev;
1721773bda96SJonathan Lemon 	uart.port.iotype = UPIO_MEM;
1722773bda96SJonathan Lemon 	uart.port.regshift = 2;
1723773bda96SJonathan Lemon 	uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
1724773bda96SJonathan Lemon 	uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
1725773bda96SJonathan Lemon 	uart.port.uartclk = 50000000;
1726c17c4059SJonathan Lemon 	uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
1727773bda96SJonathan Lemon 	uart.port.type = PORT_16550A;
1728773bda96SJonathan Lemon 
1729773bda96SJonathan Lemon 	return serial8250_register_8250_port(&uart);
1730773bda96SJonathan Lemon }
1731773bda96SJonathan Lemon 
1732773bda96SJonathan Lemon static int
1733773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
1734773bda96SJonathan Lemon {
1735773bda96SJonathan Lemon 	int port;
1736773bda96SJonathan Lemon 
1737773bda96SJonathan Lemon 	port = ptp_ocp_serial_line(bp, r);
1738773bda96SJonathan Lemon 	if (port < 0)
1739773bda96SJonathan Lemon 		return port;
1740773bda96SJonathan Lemon 
1741773bda96SJonathan Lemon 	bp_assign_entry(bp, r, port);
1742773bda96SJonathan Lemon 
1743773bda96SJonathan Lemon 	return 0;
1744773bda96SJonathan Lemon }
1745773bda96SJonathan Lemon 
1746773bda96SJonathan Lemon static int
1747773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1748773bda96SJonathan Lemon {
1749773bda96SJonathan Lemon 	void __iomem *mem;
1750773bda96SJonathan Lemon 
1751773bda96SJonathan Lemon 	mem = ptp_ocp_get_mem(bp, r);
1752c7521d3aSDan Carpenter 	if (IS_ERR(mem))
1753c7521d3aSDan Carpenter 		return PTR_ERR(mem);
1754773bda96SJonathan Lemon 
1755773bda96SJonathan Lemon 	bp_assign_entry(bp, r, mem);
1756773bda96SJonathan Lemon 
1757773bda96SJonathan Lemon 	return 0;
1758773bda96SJonathan Lemon }
1759773bda96SJonathan Lemon 
1760e3516bb4SJonathan Lemon static void
1761e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
1762e3516bb4SJonathan Lemon {
1763e3516bb4SJonathan Lemon 	if (!bp->nmea_out)
1764e3516bb4SJonathan Lemon 		return;
1765e3516bb4SJonathan Lemon 
1766e3516bb4SJonathan Lemon 	iowrite32(0, &bp->nmea_out->ctrl);		/* disable */
1767e3516bb4SJonathan Lemon 	iowrite32(7, &bp->nmea_out->uart_baud);		/* 115200 */
1768e3516bb4SJonathan Lemon 	iowrite32(1, &bp->nmea_out->ctrl);		/* enable */
1769e3516bb4SJonathan Lemon }
1770e3516bb4SJonathan Lemon 
1771a509a7c6SJonathan Lemon static void
1772b325af3cSJonathan Lemon _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
1773b325af3cSJonathan Lemon {
1774b325af3cSJonathan Lemon 	u32 val;
1775b325af3cSJonathan Lemon 
1776b325af3cSJonathan Lemon 	iowrite32(0, &reg->enable);		/* disable */
1777b325af3cSJonathan Lemon 
1778b325af3cSJonathan Lemon 	val = ioread32(&reg->polarity);
1779b325af3cSJonathan Lemon 	s->polarity = val ? true : false;
1780b325af3cSJonathan Lemon 	s->duty = 50;
1781b325af3cSJonathan Lemon }
1782b325af3cSJonathan Lemon 
1783b325af3cSJonathan Lemon static void
1784b325af3cSJonathan Lemon ptp_ocp_signal_init(struct ptp_ocp *bp)
1785b325af3cSJonathan Lemon {
1786b325af3cSJonathan Lemon 	int i;
1787b325af3cSJonathan Lemon 
1788b325af3cSJonathan Lemon 	for (i = 0; i < 4; i++)
1789b325af3cSJonathan Lemon 		if (bp->signal_out[i])
1790b325af3cSJonathan Lemon 			_ptp_ocp_signal_init(&bp->signal[i],
1791b325af3cSJonathan Lemon 					     bp->signal_out[i]->mem);
1792b325af3cSJonathan Lemon }
1793b325af3cSJonathan Lemon 
1794b325af3cSJonathan Lemon static void
1795a509a7c6SJonathan Lemon ptp_ocp_sma_init(struct ptp_ocp *bp)
1796a509a7c6SJonathan Lemon {
1797a509a7c6SJonathan Lemon 	u32 reg;
1798a509a7c6SJonathan Lemon 	int i;
1799a509a7c6SJonathan Lemon 
1800a509a7c6SJonathan Lemon 	/* defaults */
1801a509a7c6SJonathan Lemon 	bp->sma[0].mode = SMA_MODE_IN;
1802a509a7c6SJonathan Lemon 	bp->sma[1].mode = SMA_MODE_IN;
1803a509a7c6SJonathan Lemon 	bp->sma[2].mode = SMA_MODE_OUT;
1804a509a7c6SJonathan Lemon 	bp->sma[3].mode = SMA_MODE_OUT;
1805a509a7c6SJonathan Lemon 
1806a509a7c6SJonathan Lemon 	/* If no SMA1 map, the pin functions and directions are fixed. */
1807a509a7c6SJonathan Lemon 	if (!bp->sma_map1) {
1808a509a7c6SJonathan Lemon 		for (i = 0; i < 4; i++) {
1809a509a7c6SJonathan Lemon 			bp->sma[i].fixed_fcn = true;
1810a509a7c6SJonathan Lemon 			bp->sma[i].fixed_dir = true;
1811a509a7c6SJonathan Lemon 		}
1812a509a7c6SJonathan Lemon 		return;
1813a509a7c6SJonathan Lemon 	}
1814a509a7c6SJonathan Lemon 
1815a509a7c6SJonathan Lemon 	/* If SMA2 GPIO output map is all 1, it is not present.
1816a509a7c6SJonathan Lemon 	 * This indicates the firmware has fixed direction SMA pins.
1817a509a7c6SJonathan Lemon 	 */
1818a509a7c6SJonathan Lemon 	reg = ioread32(&bp->sma_map2->gpio2);
1819a509a7c6SJonathan Lemon 	if (reg == 0xffffffff) {
1820a509a7c6SJonathan Lemon 		for (i = 0; i < 4; i++)
1821a509a7c6SJonathan Lemon 			bp->sma[i].fixed_dir = true;
1822a509a7c6SJonathan Lemon 	} else {
1823a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio1);
1824a509a7c6SJonathan Lemon 		bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
1825a509a7c6SJonathan Lemon 		bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
1826a509a7c6SJonathan Lemon 
1827a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio2);
1828a509a7c6SJonathan Lemon 		bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
1829a509a7c6SJonathan Lemon 		bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
1830a509a7c6SJonathan Lemon 	}
1831a509a7c6SJonathan Lemon }
1832a509a7c6SJonathan Lemon 
18331aa66a3aSJonathan Lemon static int
18341aa66a3aSJonathan Lemon ptp_ocp_fb_set_pins(struct ptp_ocp *bp)
18351aa66a3aSJonathan Lemon {
18361aa66a3aSJonathan Lemon 	struct ptp_pin_desc *config;
18371aa66a3aSJonathan Lemon 	int i;
18381aa66a3aSJonathan Lemon 
18391aa66a3aSJonathan Lemon 	config = kzalloc(sizeof(*config) * 4, GFP_KERNEL);
18401aa66a3aSJonathan Lemon 	if (!config)
18411aa66a3aSJonathan Lemon 		return -ENOMEM;
18421aa66a3aSJonathan Lemon 
18431aa66a3aSJonathan Lemon 	for (i = 0; i < 4; i++) {
18441aa66a3aSJonathan Lemon 		sprintf(config[i].name, "sma%d", i + 1);
18451aa66a3aSJonathan Lemon 		config[i].index = i;
18461aa66a3aSJonathan Lemon 	}
18471aa66a3aSJonathan Lemon 
18481aa66a3aSJonathan Lemon 	bp->ptp_info.n_pins = 4;
18491aa66a3aSJonathan Lemon 	bp->ptp_info.pin_config = config;
18501aa66a3aSJonathan Lemon 
18511aa66a3aSJonathan Lemon 	return 0;
18521aa66a3aSJonathan Lemon }
18531aa66a3aSJonathan Lemon 
1854773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */
1855773bda96SJonathan Lemon static int
1856773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
1857773bda96SJonathan Lemon {
18581aa66a3aSJonathan Lemon 	int ver, err;
1859b325af3cSJonathan Lemon 
1860773bda96SJonathan Lemon 	bp->flash_start = 1024 * 4096;
18610cfcdd1eSJonathan Lemon 	bp->eeprom_map = fb_eeprom_map;
1862b0ca789aSJonathan Lemon 	bp->fw_version = ioread32(&bp->image->version);
1863c205d53cSJonathan Lemon 	bp->attr_tbl = fb_timecard_groups;
1864c205d53cSJonathan Lemon 	bp->fw_cap = OCP_CAP_BASIC;
1865773bda96SJonathan Lemon 
1866b325af3cSJonathan Lemon 	ver = bp->fw_version & 0xffff;
1867b325af3cSJonathan Lemon 	if (ver >= 19)
1868b325af3cSJonathan Lemon 		bp->fw_cap |= OCP_CAP_SIGNAL;
1869*2407f5d6SJonathan Lemon 	if (ver >= 20)
1870*2407f5d6SJonathan Lemon 		bp->fw_cap |= OCP_CAP_FREQ;
1871b325af3cSJonathan Lemon 
1872065efcc5SJonathan Lemon 	ptp_ocp_tod_init(bp);
1873e3516bb4SJonathan Lemon 	ptp_ocp_nmea_out_init(bp);
1874a509a7c6SJonathan Lemon 	ptp_ocp_sma_init(bp);
1875b325af3cSJonathan Lemon 	ptp_ocp_signal_init(bp);
1876065efcc5SJonathan Lemon 
18771aa66a3aSJonathan Lemon 	err = ptp_ocp_fb_set_pins(bp);
18781aa66a3aSJonathan Lemon 	if (err)
18791aa66a3aSJonathan Lemon 		return err;
18801aa66a3aSJonathan Lemon 
1881773bda96SJonathan Lemon 	return ptp_ocp_init_clock(bp);
1882773bda96SJonathan Lemon }
1883773bda96SJonathan Lemon 
188456ec4403SJonathan Lemon static bool
188556ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
188656ec4403SJonathan Lemon {
188756ec4403SJonathan Lemon 	bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
188856ec4403SJonathan Lemon 
188956ec4403SJonathan Lemon 	if (!allow)
189056ec4403SJonathan Lemon 		dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
189156ec4403SJonathan Lemon 			r->irq_vec, r->name);
189256ec4403SJonathan Lemon 	return allow;
189356ec4403SJonathan Lemon }
189456ec4403SJonathan Lemon 
1895773bda96SJonathan Lemon static int
1896773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
1897773bda96SJonathan Lemon {
1898773bda96SJonathan Lemon 	struct ocp_resource *r, *table;
1899773bda96SJonathan Lemon 	int err = 0;
1900773bda96SJonathan Lemon 
1901773bda96SJonathan Lemon 	table = (struct ocp_resource *)driver_data;
1902773bda96SJonathan Lemon 	for (r = table; r->setup; r++) {
190356ec4403SJonathan Lemon 		if (!ptp_ocp_allow_irq(bp, r))
190456ec4403SJonathan Lemon 			continue;
1905773bda96SJonathan Lemon 		err = r->setup(bp, r);
1906bceff290SJonathan Lemon 		if (err) {
1907bceff290SJonathan Lemon 			dev_err(&bp->pdev->dev,
1908bceff290SJonathan Lemon 				"Could not register %s: err %d\n",
1909bceff290SJonathan Lemon 				r->name, err);
1910773bda96SJonathan Lemon 			break;
1911773bda96SJonathan Lemon 		}
1912bceff290SJonathan Lemon 	}
1913773bda96SJonathan Lemon 	return err;
1914773bda96SJonathan Lemon }
1915773bda96SJonathan Lemon 
19166baf2925SJonathan Lemon static void
19176baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
19186baf2925SJonathan Lemon {
19196baf2925SJonathan Lemon 	u32 ctrl;
19206baf2925SJonathan Lemon 	bool on;
19216baf2925SJonathan Lemon 
19226baf2925SJonathan Lemon 	ctrl = ioread32(reg);
19236baf2925SJonathan Lemon 	on = ctrl & bit;
19246baf2925SJonathan Lemon 	if (on ^ enable) {
19256baf2925SJonathan Lemon 		ctrl &= ~bit;
19266baf2925SJonathan Lemon 		ctrl |= enable ? bit : 0;
19276baf2925SJonathan Lemon 		iowrite32(ctrl, reg);
19286baf2925SJonathan Lemon 	}
19296baf2925SJonathan Lemon }
19306baf2925SJonathan Lemon 
19316baf2925SJonathan Lemon static void
19326baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
19336baf2925SJonathan Lemon {
19346baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
19356baf2925SJonathan Lemon 				   IRIG_M_CTRL_ENABLE, enable);
19366baf2925SJonathan Lemon }
19376baf2925SJonathan Lemon 
19386baf2925SJonathan Lemon static void
19396baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
19406baf2925SJonathan Lemon {
19416baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
19426baf2925SJonathan Lemon 				   IRIG_S_CTRL_ENABLE, enable);
19436baf2925SJonathan Lemon }
19446baf2925SJonathan Lemon 
19456baf2925SJonathan Lemon static void
19466baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
19476baf2925SJonathan Lemon {
19486baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
19496baf2925SJonathan Lemon 				   DCF_M_CTRL_ENABLE, enable);
19506baf2925SJonathan Lemon }
19516baf2925SJonathan Lemon 
19526baf2925SJonathan Lemon static void
19536baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
19546baf2925SJonathan Lemon {
19556baf2925SJonathan Lemon 	return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
19566baf2925SJonathan Lemon 				   DCF_S_CTRL_ENABLE, enable);
19576baf2925SJonathan Lemon }
19586baf2925SJonathan Lemon 
19596baf2925SJonathan Lemon static void
19606baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
19616baf2925SJonathan Lemon {
19626baf2925SJonathan Lemon 	ptp_ocp_irig_out(bp, val & 0x00100010);
19636baf2925SJonathan Lemon 	ptp_ocp_dcf_out(bp, val & 0x00200020);
19646baf2925SJonathan Lemon }
19656baf2925SJonathan Lemon 
19666baf2925SJonathan Lemon static void
19676baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
19686baf2925SJonathan Lemon {
19696baf2925SJonathan Lemon 	ptp_ocp_irig_in(bp, val & 0x00100010);
19706baf2925SJonathan Lemon 	ptp_ocp_dcf_in(bp, val & 0x00200020);
19716baf2925SJonathan Lemon }
19726baf2925SJonathan Lemon 
1973e1daf0ecSJonathan Lemon /*
1974e1daf0ecSJonathan Lemon  * ANT0 == gps	(in)
1975e1daf0ecSJonathan Lemon  * ANT1 == sma1 (in)
1976e1daf0ecSJonathan Lemon  * ANT2 == sma2 (in)
1977e1daf0ecSJonathan Lemon  * ANT3 == sma3 (out)
1978e1daf0ecSJonathan Lemon  * ANT4 == sma4 (out)
1979e1daf0ecSJonathan Lemon  */
1980e1daf0ecSJonathan Lemon 
1981e1daf0ecSJonathan Lemon static ssize_t
1982b2c4f0acSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int def_val)
1983e1daf0ecSJonathan Lemon {
1984e1daf0ecSJonathan Lemon 	const char *name;
1985e1daf0ecSJonathan Lemon 	ssize_t count;
1986e1daf0ecSJonathan Lemon 
1987e1daf0ecSJonathan Lemon 	count = sysfs_emit(buf, "OUT: ");
1988e1daf0ecSJonathan Lemon 	name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val);
1989e1daf0ecSJonathan Lemon 	if (!name)
1990b2c4f0acSJonathan Lemon 		name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, def_val);
1991e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "%s\n", name);
1992e1daf0ecSJonathan Lemon 	return count;
1993e1daf0ecSJonathan Lemon }
1994e1daf0ecSJonathan Lemon 
1995e1daf0ecSJonathan Lemon static ssize_t
1996b2c4f0acSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, int def_val)
1997e1daf0ecSJonathan Lemon {
1998e1daf0ecSJonathan Lemon 	const char *name;
1999e1daf0ecSJonathan Lemon 	ssize_t count;
2000e1daf0ecSJonathan Lemon 	int i;
2001e1daf0ecSJonathan Lemon 
2002e1daf0ecSJonathan Lemon 	count = sysfs_emit(buf, "IN: ");
2003e1daf0ecSJonathan Lemon 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) {
2004e1daf0ecSJonathan Lemon 		if (val & ptp_ocp_sma_in[i].value) {
2005e1daf0ecSJonathan Lemon 			name = ptp_ocp_sma_in[i].name;
2006e1daf0ecSJonathan Lemon 			count += sysfs_emit_at(buf, count, "%s ", name);
2007e1daf0ecSJonathan Lemon 		}
2008e1daf0ecSJonathan Lemon 	}
2009b2c4f0acSJonathan Lemon 	if (!val && def_val >= 0) {
2010b2c4f0acSJonathan Lemon 		name = ptp_ocp_select_name_from_val(ptp_ocp_sma_in, def_val);
2011b2c4f0acSJonathan Lemon 		count += sysfs_emit_at(buf, count, "%s ", name);
2012b2c4f0acSJonathan Lemon 	}
2013e1daf0ecSJonathan Lemon 	if (count)
2014e1daf0ecSJonathan Lemon 		count--;
2015e1daf0ecSJonathan Lemon 	count += sysfs_emit_at(buf, count, "\n");
2016e1daf0ecSJonathan Lemon 	return count;
2017e1daf0ecSJonathan Lemon }
2018e1daf0ecSJonathan Lemon 
2019e1daf0ecSJonathan Lemon static int
2020e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode)
2021e1daf0ecSJonathan Lemon {
2022e1daf0ecSJonathan Lemon 	struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out };
2023e1daf0ecSJonathan Lemon 	int idx, count, dir;
2024e1daf0ecSJonathan Lemon 	char **argv;
2025e1daf0ecSJonathan Lemon 	int ret;
2026e1daf0ecSJonathan Lemon 
2027e1daf0ecSJonathan Lemon 	argv = argv_split(GFP_KERNEL, buf, &count);
2028e1daf0ecSJonathan Lemon 	if (!argv)
2029e1daf0ecSJonathan Lemon 		return -ENOMEM;
2030e1daf0ecSJonathan Lemon 
2031e1daf0ecSJonathan Lemon 	ret = -EINVAL;
2032e1daf0ecSJonathan Lemon 	if (!count)
2033e1daf0ecSJonathan Lemon 		goto out;
2034e1daf0ecSJonathan Lemon 
2035e1daf0ecSJonathan Lemon 	idx = 0;
2036e1daf0ecSJonathan Lemon 	dir = *mode == SMA_MODE_IN ? 0 : 1;
2037a509a7c6SJonathan Lemon 	if (!strcasecmp("IN:", argv[0])) {
2038e1daf0ecSJonathan Lemon 		dir = 0;
2039e1daf0ecSJonathan Lemon 		idx++;
2040e1daf0ecSJonathan Lemon 	}
2041e1daf0ecSJonathan Lemon 	if (!strcasecmp("OUT:", argv[0])) {
2042e1daf0ecSJonathan Lemon 		dir = 1;
2043e1daf0ecSJonathan Lemon 		idx++;
2044e1daf0ecSJonathan Lemon 	}
2045e1daf0ecSJonathan Lemon 	*mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
2046e1daf0ecSJonathan Lemon 
2047e1daf0ecSJonathan Lemon 	ret = 0;
2048e1daf0ecSJonathan Lemon 	for (; idx < count; idx++)
2049e1daf0ecSJonathan Lemon 		ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
2050e1daf0ecSJonathan Lemon 	if (ret < 0)
2051e1daf0ecSJonathan Lemon 		ret = -EINVAL;
2052e1daf0ecSJonathan Lemon 
2053e1daf0ecSJonathan Lemon out:
2054e1daf0ecSJonathan Lemon 	argv_free(argv);
2055e1daf0ecSJonathan Lemon 	return ret;
2056e1daf0ecSJonathan Lemon }
2057e1daf0ecSJonathan Lemon 
2058a509a7c6SJonathan Lemon static u32
2059a509a7c6SJonathan Lemon ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr, enum ptp_ocp_sma_mode mode)
2060e1daf0ecSJonathan Lemon {
2061a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2062a509a7c6SJonathan Lemon 	u32 shift;
2063a509a7c6SJonathan Lemon 
2064a509a7c6SJonathan Lemon 	if (bp->sma[sma_nr - 1].fixed_fcn)
2065a509a7c6SJonathan Lemon 		return (sma_nr - 1) & 1;
2066a509a7c6SJonathan Lemon 
2067a509a7c6SJonathan Lemon 	if (mode == SMA_MODE_IN)
2068a509a7c6SJonathan Lemon 		gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2069a509a7c6SJonathan Lemon 	else
2070a509a7c6SJonathan Lemon 		gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2071a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2072a509a7c6SJonathan Lemon 
2073a509a7c6SJonathan Lemon 	return (ioread32(gpio) >> shift) & 0xffff;
2074a509a7c6SJonathan Lemon }
2075a509a7c6SJonathan Lemon 
2076a509a7c6SJonathan Lemon static ssize_t
2077a509a7c6SJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
2078b2c4f0acSJonathan Lemon 		 int default_in_val, int default_out_val)
2079a509a7c6SJonathan Lemon {
2080a509a7c6SJonathan Lemon 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2081a509a7c6SJonathan Lemon 	u32 val;
2082a509a7c6SJonathan Lemon 
2083a509a7c6SJonathan Lemon 	val = ptp_ocp_sma_get(bp, sma_nr, sma->mode) & SMA_SELECT_MASK;
2084e1daf0ecSJonathan Lemon 
2085b2c4f0acSJonathan Lemon 	if (sma->mode == SMA_MODE_IN) {
2086b2c4f0acSJonathan Lemon 		if (sma->disabled)
2087b2c4f0acSJonathan Lemon 			val = SMA_DISABLE;
2088b2c4f0acSJonathan Lemon 		return ptp_ocp_show_inputs(val, buf, default_in_val);
2089b2c4f0acSJonathan Lemon 	}
2090e1daf0ecSJonathan Lemon 
2091b2c4f0acSJonathan Lemon 	return ptp_ocp_show_output(val, buf, default_out_val);
2092e1daf0ecSJonathan Lemon }
2093e1daf0ecSJonathan Lemon 
2094e1daf0ecSJonathan Lemon static ssize_t
2095e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
2096e1daf0ecSJonathan Lemon {
2097e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2098e1daf0ecSJonathan Lemon 
2099a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
2100e1daf0ecSJonathan Lemon }
2101e1daf0ecSJonathan Lemon 
2102e1daf0ecSJonathan Lemon static ssize_t
2103e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
2104e1daf0ecSJonathan Lemon {
2105e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2106e1daf0ecSJonathan Lemon 
2107a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
2108e1daf0ecSJonathan Lemon }
2109e1daf0ecSJonathan Lemon 
2110e1daf0ecSJonathan Lemon static ssize_t
2111e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
2112e1daf0ecSJonathan Lemon {
2113e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2114e1daf0ecSJonathan Lemon 
2115a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
2116e1daf0ecSJonathan Lemon }
2117e1daf0ecSJonathan Lemon 
2118e1daf0ecSJonathan Lemon static ssize_t
2119e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
2120e1daf0ecSJonathan Lemon {
2121e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2122e1daf0ecSJonathan Lemon 
2123a509a7c6SJonathan Lemon 	return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
2124e1daf0ecSJonathan Lemon }
2125e1daf0ecSJonathan Lemon 
2126e1daf0ecSJonathan Lemon static void
2127a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2128e1daf0ecSJonathan Lemon {
2129a509a7c6SJonathan Lemon 	u32 reg, mask, shift;
2130e1daf0ecSJonathan Lemon 	unsigned long flags;
2131a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2132a509a7c6SJonathan Lemon 
2133a509a7c6SJonathan Lemon 	gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2134a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2135e1daf0ecSJonathan Lemon 
2136e1daf0ecSJonathan Lemon 	mask = 0xffff << (16 - shift);
2137e1daf0ecSJonathan Lemon 
2138e1daf0ecSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2139e1daf0ecSJonathan Lemon 
2140a509a7c6SJonathan Lemon 	reg = ioread32(gpio);
2141a509a7c6SJonathan Lemon 	reg = (reg & mask) | (val << shift);
21426baf2925SJonathan Lemon 
2143a509a7c6SJonathan Lemon 	__handle_signal_outputs(bp, reg);
21446baf2925SJonathan Lemon 
2145a509a7c6SJonathan Lemon 	iowrite32(reg, gpio);
2146e1daf0ecSJonathan Lemon 
2147e1daf0ecSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2148e1daf0ecSJonathan Lemon }
2149e1daf0ecSJonathan Lemon 
2150e1daf0ecSJonathan Lemon static void
2151a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2152e1daf0ecSJonathan Lemon {
2153a509a7c6SJonathan Lemon 	u32 reg, mask, shift;
2154e1daf0ecSJonathan Lemon 	unsigned long flags;
2155a509a7c6SJonathan Lemon 	u32 __iomem *gpio;
2156a509a7c6SJonathan Lemon 
2157a509a7c6SJonathan Lemon 	gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2158a509a7c6SJonathan Lemon 	shift = sma_nr & 1 ? 0 : 16;
2159e1daf0ecSJonathan Lemon 
2160e1daf0ecSJonathan Lemon 	mask = 0xffff << (16 - shift);
2161e1daf0ecSJonathan Lemon 
2162e1daf0ecSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2163e1daf0ecSJonathan Lemon 
2164a509a7c6SJonathan Lemon 	reg = ioread32(gpio);
2165a509a7c6SJonathan Lemon 	reg = (reg & mask) | (val << shift);
21666baf2925SJonathan Lemon 
2167a509a7c6SJonathan Lemon 	__handle_signal_inputs(bp, reg);
21686baf2925SJonathan Lemon 
2169a509a7c6SJonathan Lemon 	iowrite32(reg, gpio);
2170e1daf0ecSJonathan Lemon 
2171e1daf0ecSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2172e1daf0ecSJonathan Lemon }
2173e1daf0ecSJonathan Lemon 
2174a509a7c6SJonathan Lemon static int
2175a509a7c6SJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
2176e1daf0ecSJonathan Lemon {
2177a509a7c6SJonathan Lemon 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2178e1daf0ecSJonathan Lemon 	enum ptp_ocp_sma_mode mode;
2179e1daf0ecSJonathan Lemon 	int val;
2180e1daf0ecSJonathan Lemon 
2181e1daf0ecSJonathan Lemon 	mode = sma->mode;
2182e1daf0ecSJonathan Lemon 	val = sma_parse_inputs(buf, &mode);
2183e1daf0ecSJonathan Lemon 	if (val < 0)
2184e1daf0ecSJonathan Lemon 		return val;
2185e1daf0ecSJonathan Lemon 
2186b2c4f0acSJonathan Lemon 	if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
2187e1daf0ecSJonathan Lemon 		return -EOPNOTSUPP;
2188e1daf0ecSJonathan Lemon 
2189a509a7c6SJonathan Lemon 	if (sma->fixed_fcn) {
2190a509a7c6SJonathan Lemon 		if (val != ((sma_nr - 1) & 1))
2191e1daf0ecSJonathan Lemon 			return -EOPNOTSUPP;
2192a509a7c6SJonathan Lemon 		return 0;
2193e1daf0ecSJonathan Lemon 	}
2194e1daf0ecSJonathan Lemon 
2195b2c4f0acSJonathan Lemon 	sma->disabled = !!(val & SMA_DISABLE);
2196b2c4f0acSJonathan Lemon 
2197a509a7c6SJonathan Lemon 	if (mode != sma->mode) {
2198a509a7c6SJonathan Lemon 		if (mode == SMA_MODE_IN)
2199a509a7c6SJonathan Lemon 			ptp_ocp_sma_store_output(bp, sma_nr, 0);
2200e1daf0ecSJonathan Lemon 		else
2201a509a7c6SJonathan Lemon 			ptp_ocp_sma_store_inputs(bp, sma_nr, 0);
2202a509a7c6SJonathan Lemon 		sma->mode = mode;
2203a509a7c6SJonathan Lemon 	}
2204a509a7c6SJonathan Lemon 
2205a509a7c6SJonathan Lemon 	if (!sma->fixed_dir)
2206a509a7c6SJonathan Lemon 		val |= SMA_ENABLE;		/* add enable bit */
2207a509a7c6SJonathan Lemon 
2208b2c4f0acSJonathan Lemon 	if (sma->disabled)
2209b2c4f0acSJonathan Lemon 		val = 0;
2210b2c4f0acSJonathan Lemon 
2211a509a7c6SJonathan Lemon 	if (mode == SMA_MODE_IN)
2212a509a7c6SJonathan Lemon 		ptp_ocp_sma_store_inputs(bp, sma_nr, val);
2213a509a7c6SJonathan Lemon 	else
2214a509a7c6SJonathan Lemon 		ptp_ocp_sma_store_output(bp, sma_nr, val);
2215e1daf0ecSJonathan Lemon 
2216e1daf0ecSJonathan Lemon 	return 0;
2217e1daf0ecSJonathan Lemon }
2218e1daf0ecSJonathan Lemon 
2219e1daf0ecSJonathan Lemon static ssize_t
2220e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr,
2221e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2222e1daf0ecSJonathan Lemon {
2223e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2224e1daf0ecSJonathan Lemon 	int err;
2225e1daf0ecSJonathan Lemon 
2226a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 1);
2227e1daf0ecSJonathan Lemon 	return err ? err : count;
2228e1daf0ecSJonathan Lemon }
2229e1daf0ecSJonathan Lemon 
2230e1daf0ecSJonathan Lemon static ssize_t
2231e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr,
2232e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2233e1daf0ecSJonathan Lemon {
2234e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2235e1daf0ecSJonathan Lemon 	int err;
2236e1daf0ecSJonathan Lemon 
2237a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 2);
2238e1daf0ecSJonathan Lemon 	return err ? err : count;
2239e1daf0ecSJonathan Lemon }
2240e1daf0ecSJonathan Lemon 
2241e1daf0ecSJonathan Lemon static ssize_t
2242e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr,
2243e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2244e1daf0ecSJonathan Lemon {
2245e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2246e1daf0ecSJonathan Lemon 	int err;
2247e1daf0ecSJonathan Lemon 
2248a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 3);
2249e1daf0ecSJonathan Lemon 	return err ? err : count;
2250e1daf0ecSJonathan Lemon }
2251e1daf0ecSJonathan Lemon 
2252e1daf0ecSJonathan Lemon static ssize_t
2253e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr,
2254e1daf0ecSJonathan Lemon 	   const char *buf, size_t count)
2255e1daf0ecSJonathan Lemon {
2256e1daf0ecSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2257e1daf0ecSJonathan Lemon 	int err;
2258e1daf0ecSJonathan Lemon 
2259a509a7c6SJonathan Lemon 	err = ptp_ocp_sma_store(bp, buf, 4);
2260e1daf0ecSJonathan Lemon 	return err ? err : count;
2261e1daf0ecSJonathan Lemon }
2262e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1);
2263e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2);
2264e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3);
2265e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4);
2266e1daf0ecSJonathan Lemon 
2267e1daf0ecSJonathan Lemon static ssize_t
2268e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev,
2269e1daf0ecSJonathan Lemon 			  struct device_attribute *attr, char *buf)
2270e1daf0ecSJonathan Lemon {
2271e1daf0ecSJonathan Lemon 	return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf);
2272e1daf0ecSJonathan Lemon }
2273e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs);
2274e1daf0ecSJonathan Lemon 
2275e1daf0ecSJonathan Lemon static ssize_t
2276e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev,
2277e1daf0ecSJonathan Lemon 			   struct device_attribute *attr, char *buf)
2278e1daf0ecSJonathan Lemon {
2279e1daf0ecSJonathan Lemon 	return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf);
2280e1daf0ecSJonathan Lemon }
2281e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs);
2282e1daf0ecSJonathan Lemon 
2283b325af3cSJonathan Lemon #define EXT_ATTR_RO(_group, _name, _val)				\
2284b325af3cSJonathan Lemon 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
2285b325af3cSJonathan Lemon 		{ __ATTR_RO(_name), (void *)_val }
2286b325af3cSJonathan Lemon #define EXT_ATTR_RW(_group, _name, _val)				\
2287b325af3cSJonathan Lemon 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
2288b325af3cSJonathan Lemon 		{ __ATTR_RW(_name), (void *)_val }
2289b325af3cSJonathan Lemon #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2290b325af3cSJonathan Lemon 
2291b325af3cSJonathan Lemon /* period [duty [phase [polarity]]] */
2292b325af3cSJonathan Lemon static ssize_t
2293b325af3cSJonathan Lemon signal_store(struct device *dev, struct device_attribute *attr,
2294b325af3cSJonathan Lemon 	     const char *buf, size_t count)
2295b325af3cSJonathan Lemon {
2296b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2297b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2298b325af3cSJonathan Lemon 	struct ptp_ocp_signal s = { };
2299b325af3cSJonathan Lemon 	int gen = (uintptr_t)ea->var;
2300b325af3cSJonathan Lemon 	int argc, err;
2301b325af3cSJonathan Lemon 	char **argv;
2302b325af3cSJonathan Lemon 
2303b325af3cSJonathan Lemon 	argv = argv_split(GFP_KERNEL, buf, &argc);
2304b325af3cSJonathan Lemon 	if (!argv)
2305b325af3cSJonathan Lemon 		return -ENOMEM;
2306b325af3cSJonathan Lemon 
2307b325af3cSJonathan Lemon 	err = -EINVAL;
2308b325af3cSJonathan Lemon 	s.duty = bp->signal[gen].duty;
2309b325af3cSJonathan Lemon 	s.phase = bp->signal[gen].phase;
2310b325af3cSJonathan Lemon 	s.period = bp->signal[gen].period;
2311b325af3cSJonathan Lemon 	s.polarity = bp->signal[gen].polarity;
2312b325af3cSJonathan Lemon 
2313b325af3cSJonathan Lemon 	switch (argc) {
2314b325af3cSJonathan Lemon 	case 4:
2315b325af3cSJonathan Lemon 		argc--;
2316b325af3cSJonathan Lemon 		err = kstrtobool(argv[argc], &s.polarity);
2317b325af3cSJonathan Lemon 		if (err)
2318b325af3cSJonathan Lemon 			goto out;
2319b325af3cSJonathan Lemon 		fallthrough;
2320b325af3cSJonathan Lemon 	case 3:
2321b325af3cSJonathan Lemon 		argc--;
2322b325af3cSJonathan Lemon 		err = kstrtou64(argv[argc], 0, &s.phase);
2323b325af3cSJonathan Lemon 		if (err)
2324b325af3cSJonathan Lemon 			goto out;
2325b325af3cSJonathan Lemon 		fallthrough;
2326b325af3cSJonathan Lemon 	case 2:
2327b325af3cSJonathan Lemon 		argc--;
2328b325af3cSJonathan Lemon 		err = kstrtoint(argv[argc], 0, &s.duty);
2329b325af3cSJonathan Lemon 		if (err)
2330b325af3cSJonathan Lemon 			goto out;
2331b325af3cSJonathan Lemon 		fallthrough;
2332b325af3cSJonathan Lemon 	case 1:
2333b325af3cSJonathan Lemon 		argc--;
2334b325af3cSJonathan Lemon 		err = kstrtou64(argv[argc], 0, &s.period);
2335b325af3cSJonathan Lemon 		if (err)
2336b325af3cSJonathan Lemon 			goto out;
2337b325af3cSJonathan Lemon 		break;
2338b325af3cSJonathan Lemon 	default:
2339b325af3cSJonathan Lemon 		goto out;
2340b325af3cSJonathan Lemon 	}
2341b325af3cSJonathan Lemon 
2342b325af3cSJonathan Lemon 	err = ptp_ocp_signal_set(bp, gen, &s);
2343b325af3cSJonathan Lemon 	if (err)
2344b325af3cSJonathan Lemon 		goto out;
2345b325af3cSJonathan Lemon 
2346b325af3cSJonathan Lemon 	err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
2347b325af3cSJonathan Lemon 
2348b325af3cSJonathan Lemon out:
2349b325af3cSJonathan Lemon 	argv_free(argv);
2350b325af3cSJonathan Lemon 	return err ? err : count;
2351b325af3cSJonathan Lemon }
2352b325af3cSJonathan Lemon 
2353b325af3cSJonathan Lemon static ssize_t
2354b325af3cSJonathan Lemon signal_show(struct device *dev, struct device_attribute *attr, char *buf)
2355b325af3cSJonathan Lemon {
2356b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2357b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2358b325af3cSJonathan Lemon 	struct ptp_ocp_signal *signal;
2359b325af3cSJonathan Lemon 	struct timespec64 ts;
2360b325af3cSJonathan Lemon 	ssize_t count;
2361b325af3cSJonathan Lemon 	int i;
2362b325af3cSJonathan Lemon 
2363b325af3cSJonathan Lemon 	i = (uintptr_t)ea->var;
2364b325af3cSJonathan Lemon 	signal = &bp->signal[i];
2365b325af3cSJonathan Lemon 
2366b325af3cSJonathan Lemon 	count = sysfs_emit(buf, "%llu %d %llu %d", signal->period,
2367b325af3cSJonathan Lemon 			   signal->duty, signal->phase, signal->polarity);
2368b325af3cSJonathan Lemon 
2369b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(signal->start);
2370b325af3cSJonathan Lemon 	count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts);
2371b325af3cSJonathan Lemon 
2372b325af3cSJonathan Lemon 	return count;
2373b325af3cSJonathan Lemon }
2374b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 0);
2375b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 1);
2376b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 2);
2377b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 3);
2378b325af3cSJonathan Lemon 
2379b325af3cSJonathan Lemon static ssize_t
2380b325af3cSJonathan Lemon duty_show(struct device *dev, struct device_attribute *attr, char *buf)
2381b325af3cSJonathan Lemon {
2382b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2383b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2384b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2385b325af3cSJonathan Lemon 
2386b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
2387b325af3cSJonathan Lemon }
2388b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 0);
2389b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 1);
2390b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 2);
2391b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 3);
2392b325af3cSJonathan Lemon 
2393b325af3cSJonathan Lemon static ssize_t
2394b325af3cSJonathan Lemon period_show(struct device *dev, struct device_attribute *attr, char *buf)
2395b325af3cSJonathan Lemon {
2396b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2397b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2398b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2399b325af3cSJonathan Lemon 
2400b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
2401b325af3cSJonathan Lemon }
2402b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 0);
2403b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 1);
2404b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 2);
2405b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 3);
2406b325af3cSJonathan Lemon 
2407b325af3cSJonathan Lemon static ssize_t
2408b325af3cSJonathan Lemon phase_show(struct device *dev, struct device_attribute *attr, char *buf)
2409b325af3cSJonathan Lemon {
2410b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2411b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2412b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2413b325af3cSJonathan Lemon 
2414b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
2415b325af3cSJonathan Lemon }
2416b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 0);
2417b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 1);
2418b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 2);
2419b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 3);
2420b325af3cSJonathan Lemon 
2421b325af3cSJonathan Lemon static ssize_t
2422b325af3cSJonathan Lemon polarity_show(struct device *dev, struct device_attribute *attr,
2423b325af3cSJonathan Lemon 	      char *buf)
2424b325af3cSJonathan Lemon {
2425b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2426b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2427b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2428b325af3cSJonathan Lemon 
2429b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
2430b325af3cSJonathan Lemon }
2431b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 0);
2432b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 1);
2433b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 2);
2434b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 3);
2435b325af3cSJonathan Lemon 
2436b325af3cSJonathan Lemon static ssize_t
2437b325af3cSJonathan Lemon running_show(struct device *dev, struct device_attribute *attr, char *buf)
2438b325af3cSJonathan Lemon {
2439b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2440b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2441b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2442b325af3cSJonathan Lemon 
2443b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->signal[i].running);
2444b325af3cSJonathan Lemon }
2445b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 0);
2446b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 1);
2447b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 2);
2448b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 3);
2449b325af3cSJonathan Lemon 
2450b325af3cSJonathan Lemon static ssize_t
2451b325af3cSJonathan Lemon start_show(struct device *dev, struct device_attribute *attr, char *buf)
2452b325af3cSJonathan Lemon {
2453b325af3cSJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2454b325af3cSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2455b325af3cSJonathan Lemon 	int i = (uintptr_t)ea->var;
2456b325af3cSJonathan Lemon 	struct timespec64 ts;
2457b325af3cSJonathan Lemon 
2458b325af3cSJonathan Lemon 	ts = ktime_to_timespec64(bp->signal[i].start);
2459b325af3cSJonathan Lemon 	return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
2460b325af3cSJonathan Lemon }
2461b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 0);
2462b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 1);
2463b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 2);
2464b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 3);
2465b325af3cSJonathan Lemon 
2466773bda96SJonathan Lemon static ssize_t
2467*2407f5d6SJonathan Lemon seconds_store(struct device *dev, struct device_attribute *attr,
2468*2407f5d6SJonathan Lemon 	      const char *buf, size_t count)
2469*2407f5d6SJonathan Lemon {
2470*2407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2471*2407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2472*2407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
2473*2407f5d6SJonathan Lemon 	u32 val;
2474*2407f5d6SJonathan Lemon 	int err;
2475*2407f5d6SJonathan Lemon 
2476*2407f5d6SJonathan Lemon 	err = kstrtou32(buf, 0, &val);
2477*2407f5d6SJonathan Lemon 	if (err)
2478*2407f5d6SJonathan Lemon 		return err;
2479*2407f5d6SJonathan Lemon 	if (val > 0xff)
2480*2407f5d6SJonathan Lemon 		return -EINVAL;
2481*2407f5d6SJonathan Lemon 
2482*2407f5d6SJonathan Lemon 	if (val)
2483*2407f5d6SJonathan Lemon 		val = (val << 8) | 0x1;
2484*2407f5d6SJonathan Lemon 
2485*2407f5d6SJonathan Lemon 	iowrite32(val, &bp->freq_in[idx]->ctrl);
2486*2407f5d6SJonathan Lemon 
2487*2407f5d6SJonathan Lemon 	return count;
2488*2407f5d6SJonathan Lemon }
2489*2407f5d6SJonathan Lemon 
2490*2407f5d6SJonathan Lemon static ssize_t
2491*2407f5d6SJonathan Lemon seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
2492*2407f5d6SJonathan Lemon {
2493*2407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2494*2407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2495*2407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
2496*2407f5d6SJonathan Lemon 	u32 val;
2497*2407f5d6SJonathan Lemon 
2498*2407f5d6SJonathan Lemon 	val = ioread32(&bp->freq_in[idx]->ctrl);
2499*2407f5d6SJonathan Lemon 	if (val & 1)
2500*2407f5d6SJonathan Lemon 		val = (val >> 8) & 0xff;
2501*2407f5d6SJonathan Lemon 	else
2502*2407f5d6SJonathan Lemon 		val = 0;
2503*2407f5d6SJonathan Lemon 
2504*2407f5d6SJonathan Lemon 	return sysfs_emit(buf, "%u\n", val);
2505*2407f5d6SJonathan Lemon }
2506*2407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 0);
2507*2407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 1);
2508*2407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 2);
2509*2407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 3);
2510*2407f5d6SJonathan Lemon 
2511*2407f5d6SJonathan Lemon static ssize_t
2512*2407f5d6SJonathan Lemon frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
2513*2407f5d6SJonathan Lemon {
2514*2407f5d6SJonathan Lemon 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2515*2407f5d6SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2516*2407f5d6SJonathan Lemon 	int idx = (uintptr_t)ea->var;
2517*2407f5d6SJonathan Lemon 	u32 val;
2518*2407f5d6SJonathan Lemon 
2519*2407f5d6SJonathan Lemon 	val = ioread32(&bp->freq_in[idx]->status);
2520*2407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_ERROR)
2521*2407f5d6SJonathan Lemon 		return sysfs_emit(buf, "error\n");
2522*2407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_OVERRUN)
2523*2407f5d6SJonathan Lemon 		return sysfs_emit(buf, "overrun\n");
2524*2407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_VALID)
2525*2407f5d6SJonathan Lemon 		return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
2526*2407f5d6SJonathan Lemon 	return 0;
2527*2407f5d6SJonathan Lemon }
2528*2407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 0);
2529*2407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 1);
2530*2407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 2);
2531*2407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 3);
2532*2407f5d6SJonathan Lemon 
2533*2407f5d6SJonathan Lemon static ssize_t
2534773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
2535773bda96SJonathan Lemon {
2536773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2537773bda96SJonathan Lemon 
25380cfcdd1eSJonathan Lemon 	if (!bp->has_eeprom_data)
25390cfcdd1eSJonathan Lemon 		ptp_ocp_read_eeprom(bp);
2540773bda96SJonathan Lemon 
2541773bda96SJonathan Lemon 	return sysfs_emit(buf, "%pM\n", bp->serial);
2542773bda96SJonathan Lemon }
2543773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum);
2544773bda96SJonathan Lemon 
2545773bda96SJonathan Lemon static ssize_t
2546ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
2547773bda96SJonathan Lemon {
2548773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2549773bda96SJonathan Lemon 	ssize_t ret;
2550773bda96SJonathan Lemon 
2551ef0cfb34SJonathan Lemon 	if (bp->gnss_lost)
2552ef0cfb34SJonathan Lemon 		ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
2553773bda96SJonathan Lemon 	else
2554773bda96SJonathan Lemon 		ret = sysfs_emit(buf, "SYNC\n");
2555773bda96SJonathan Lemon 
2556773bda96SJonathan Lemon 	return ret;
2557773bda96SJonathan Lemon }
2558ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync);
2559773bda96SJonathan Lemon 
2560773bda96SJonathan Lemon static ssize_t
256189260d87SJonathan Lemon utc_tai_offset_show(struct device *dev,
256289260d87SJonathan Lemon 		    struct device_attribute *attr, char *buf)
256389260d87SJonathan Lemon {
256489260d87SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
256589260d87SJonathan Lemon 
256689260d87SJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
256789260d87SJonathan Lemon }
256889260d87SJonathan Lemon 
256989260d87SJonathan Lemon static ssize_t
257089260d87SJonathan Lemon utc_tai_offset_store(struct device *dev,
257189260d87SJonathan Lemon 		     struct device_attribute *attr,
257289260d87SJonathan Lemon 		     const char *buf, size_t count)
257389260d87SJonathan Lemon {
257489260d87SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
257589260d87SJonathan Lemon 	int err;
257689260d87SJonathan Lemon 	u32 val;
257789260d87SJonathan Lemon 
257889260d87SJonathan Lemon 	err = kstrtou32(buf, 0, &val);
257989260d87SJonathan Lemon 	if (err)
258089260d87SJonathan Lemon 		return err;
258189260d87SJonathan Lemon 
258289260d87SJonathan Lemon 	ptp_ocp_utc_distribute(bp, val);
258389260d87SJonathan Lemon 
258489260d87SJonathan Lemon 	return count;
258589260d87SJonathan Lemon }
258689260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset);
258789260d87SJonathan Lemon 
258889260d87SJonathan Lemon static ssize_t
25891acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev,
25901acffc6eSJonathan Lemon 		      struct device_attribute *attr, char *buf)
25911acffc6eSJonathan Lemon {
25921acffc6eSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
25931acffc6eSJonathan Lemon 
25941acffc6eSJonathan Lemon 	return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
25951acffc6eSJonathan Lemon }
25961acffc6eSJonathan Lemon 
25971acffc6eSJonathan Lemon static ssize_t
25981acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev,
25991acffc6eSJonathan Lemon 		       struct device_attribute *attr,
26001acffc6eSJonathan Lemon 		       const char *buf, size_t count)
26011acffc6eSJonathan Lemon {
26021acffc6eSJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
26031acffc6eSJonathan Lemon 	int err;
26041acffc6eSJonathan Lemon 	u32 val;
26051acffc6eSJonathan Lemon 
26061acffc6eSJonathan Lemon 	err = kstrtou32(buf, 0, &val);
26071acffc6eSJonathan Lemon 	if (err)
26081acffc6eSJonathan Lemon 		return err;
26091acffc6eSJonathan Lemon 
26101acffc6eSJonathan Lemon 	bp->ts_window_adjust = val;
26111acffc6eSJonathan Lemon 
26121acffc6eSJonathan Lemon 	return count;
26131acffc6eSJonathan Lemon }
26141acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust);
26151acffc6eSJonathan Lemon 
26161acffc6eSJonathan Lemon static ssize_t
2617d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
2618d14ee252SJonathan Lemon {
2619d14ee252SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2620d14ee252SJonathan Lemon 	u32 val;
2621d14ee252SJonathan Lemon 
2622d14ee252SJonathan Lemon 	val = ioread32(&bp->irig_out->ctrl);
2623d14ee252SJonathan Lemon 	val = (val >> 16) & 0x07;
2624d14ee252SJonathan Lemon 	return sysfs_emit(buf, "%d\n", val);
2625d14ee252SJonathan Lemon }
2626d14ee252SJonathan Lemon 
2627d14ee252SJonathan Lemon static ssize_t
2628d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev,
2629d14ee252SJonathan Lemon 		  struct device_attribute *attr,
2630d14ee252SJonathan Lemon 		  const char *buf, size_t count)
2631d14ee252SJonathan Lemon {
2632d14ee252SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2633d14ee252SJonathan Lemon 	unsigned long flags;
2634d14ee252SJonathan Lemon 	int err;
2635d14ee252SJonathan Lemon 	u32 reg;
2636d14ee252SJonathan Lemon 	u8 val;
2637d14ee252SJonathan Lemon 
2638d14ee252SJonathan Lemon 	err = kstrtou8(buf, 0, &val);
2639d14ee252SJonathan Lemon 	if (err)
2640d14ee252SJonathan Lemon 		return err;
2641d14ee252SJonathan Lemon 	if (val > 7)
2642d14ee252SJonathan Lemon 		return -EINVAL;
2643d14ee252SJonathan Lemon 
2644d14ee252SJonathan Lemon 	reg = ((val & 0x7) << 16);
2645d14ee252SJonathan Lemon 
2646d14ee252SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2647d14ee252SJonathan Lemon 	iowrite32(0, &bp->irig_out->ctrl);		/* disable */
2648d14ee252SJonathan Lemon 	iowrite32(reg, &bp->irig_out->ctrl);		/* change mode */
2649d14ee252SJonathan Lemon 	iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
2650d14ee252SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2651d14ee252SJonathan Lemon 
2652d14ee252SJonathan Lemon 	return count;
2653d14ee252SJonathan Lemon }
2654d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode);
2655d14ee252SJonathan Lemon 
2656d14ee252SJonathan Lemon static ssize_t
2657773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
2658773bda96SJonathan Lemon {
2659773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2660773bda96SJonathan Lemon 	const char *p;
2661773bda96SJonathan Lemon 	u32 select;
2662773bda96SJonathan Lemon 
2663773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
2664e1daf0ecSJonathan Lemon 	p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
2665773bda96SJonathan Lemon 
2666773bda96SJonathan Lemon 	return sysfs_emit(buf, "%s\n", p);
2667773bda96SJonathan Lemon }
2668773bda96SJonathan Lemon 
2669773bda96SJonathan Lemon static ssize_t
2670773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr,
2671773bda96SJonathan Lemon 		   const char *buf, size_t count)
2672773bda96SJonathan Lemon {
2673773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2674773bda96SJonathan Lemon 	unsigned long flags;
2675773bda96SJonathan Lemon 	int val;
2676773bda96SJonathan Lemon 
2677e1daf0ecSJonathan Lemon 	val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
2678773bda96SJonathan Lemon 	if (val < 0)
2679773bda96SJonathan Lemon 		return val;
2680773bda96SJonathan Lemon 
2681773bda96SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
2682773bda96SJonathan Lemon 	iowrite32(val, &bp->reg->select);
2683773bda96SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
2684773bda96SJonathan Lemon 
2685773bda96SJonathan Lemon 	return count;
2686773bda96SJonathan Lemon }
2687773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source);
2688773bda96SJonathan Lemon 
2689773bda96SJonathan Lemon static ssize_t
2690773bda96SJonathan Lemon available_clock_sources_show(struct device *dev,
2691773bda96SJonathan Lemon 			     struct device_attribute *attr, char *buf)
2692773bda96SJonathan Lemon {
2693e1daf0ecSJonathan Lemon 	return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
2694773bda96SJonathan Lemon }
2695773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources);
2696773bda96SJonathan Lemon 
26972f23f486SVadim Fedorenko static ssize_t
26982f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev,
26992f23f486SVadim Fedorenko 			struct device_attribute *attr, char *buf)
27002f23f486SVadim Fedorenko {
27012f23f486SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
27022f23f486SVadim Fedorenko 	u32 val;
27032f23f486SVadim Fedorenko 	int res;
27042f23f486SVadim Fedorenko 
27052f23f486SVadim Fedorenko 	val = ioread32(&bp->reg->status_drift);
27062f23f486SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
27072f23f486SVadim Fedorenko 	res *= (val & INT_MAX);
27082f23f486SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
27092f23f486SVadim Fedorenko }
27102f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift);
27112f23f486SVadim Fedorenko 
27122f23f486SVadim Fedorenko static ssize_t
27132f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev,
27142f23f486SVadim Fedorenko 			 struct device_attribute *attr, char *buf)
27152f23f486SVadim Fedorenko {
27162f23f486SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
27172f23f486SVadim Fedorenko 	u32 val;
27182f23f486SVadim Fedorenko 	int res;
27192f23f486SVadim Fedorenko 
27202f23f486SVadim Fedorenko 	val = ioread32(&bp->reg->status_offset);
27212f23f486SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
27222f23f486SVadim Fedorenko 	res *= (val & INT_MAX);
27232f23f486SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
27242f23f486SVadim Fedorenko }
27252f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset);
27262f23f486SVadim Fedorenko 
272744a412d1SVadim Fedorenko static ssize_t
272844a412d1SVadim Fedorenko tod_correction_show(struct device *dev,
272944a412d1SVadim Fedorenko 		    struct device_attribute *attr, char *buf)
273044a412d1SVadim Fedorenko {
273144a412d1SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
273244a412d1SVadim Fedorenko 	u32 val;
273344a412d1SVadim Fedorenko 	int res;
273444a412d1SVadim Fedorenko 
273544a412d1SVadim Fedorenko 	val = ioread32(&bp->tod->adj_sec);
273644a412d1SVadim Fedorenko 	res = (val & ~INT_MAX) ? -1 : 1;
273744a412d1SVadim Fedorenko 	res *= (val & INT_MAX);
273844a412d1SVadim Fedorenko 	return sysfs_emit(buf, "%d\n", res);
273944a412d1SVadim Fedorenko }
274044a412d1SVadim Fedorenko 
274144a412d1SVadim Fedorenko static ssize_t
274244a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr,
274344a412d1SVadim Fedorenko 		     const char *buf, size_t count)
274444a412d1SVadim Fedorenko {
274544a412d1SVadim Fedorenko 	struct ptp_ocp *bp = dev_get_drvdata(dev);
274644a412d1SVadim Fedorenko 	unsigned long flags;
274744a412d1SVadim Fedorenko 	int err, res;
274844a412d1SVadim Fedorenko 	u32 val = 0;
274944a412d1SVadim Fedorenko 
275044a412d1SVadim Fedorenko 	err = kstrtos32(buf, 0, &res);
275144a412d1SVadim Fedorenko 	if (err)
275244a412d1SVadim Fedorenko 		return err;
275344a412d1SVadim Fedorenko 	if (res < 0) {
275444a412d1SVadim Fedorenko 		res *= -1;
275544a412d1SVadim Fedorenko 		val |= BIT(31);
275644a412d1SVadim Fedorenko 	}
275744a412d1SVadim Fedorenko 	val |= res;
275844a412d1SVadim Fedorenko 
275944a412d1SVadim Fedorenko 	spin_lock_irqsave(&bp->lock, flags);
276044a412d1SVadim Fedorenko 	iowrite32(val, &bp->tod->adj_sec);
276144a412d1SVadim Fedorenko 	spin_unlock_irqrestore(&bp->lock, flags);
276244a412d1SVadim Fedorenko 
276344a412d1SVadim Fedorenko 	return count;
276444a412d1SVadim Fedorenko }
276544a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction);
276644a412d1SVadim Fedorenko 
2767b325af3cSJonathan Lemon #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr)					\
2768b325af3cSJonathan Lemon 	static struct attribute *fb_timecard_signal##_nr##_attrs[] = {	\
2769b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_signal.attr.attr,		\
2770b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_duty.attr.attr,			\
2771b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_phase.attr.attr,		\
2772b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_period.attr.attr,		\
2773b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_polarity.attr.attr,		\
2774b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_running.attr.attr,		\
2775b325af3cSJonathan Lemon 		&dev_attr_signal##_nr##_start.attr.attr,		\
2776b325af3cSJonathan Lemon 		NULL,							\
2777b325af3cSJonathan Lemon 	}
2778b325af3cSJonathan Lemon 
2779b325af3cSJonathan Lemon #define DEVICE_SIGNAL_GROUP(_name, _nr)					\
2780b325af3cSJonathan Lemon 	_DEVICE_SIGNAL_GROUP_ATTRS(_nr);				\
2781b325af3cSJonathan Lemon 	static const struct attribute_group				\
2782b325af3cSJonathan Lemon 			fb_timecard_signal##_nr##_group = {		\
2783b325af3cSJonathan Lemon 		.name = #_name,						\
2784b325af3cSJonathan Lemon 		.attrs = fb_timecard_signal##_nr##_attrs,		\
2785b325af3cSJonathan Lemon }
2786b325af3cSJonathan Lemon 
2787b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen1, 0);
2788b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen2, 1);
2789b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen3, 2);
2790b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen4, 3);
2791b325af3cSJonathan Lemon 
2792*2407f5d6SJonathan Lemon #define _DEVICE_FREQ_GROUP_ATTRS(_nr)					\
2793*2407f5d6SJonathan Lemon 	static struct attribute *fb_timecard_freq##_nr##_attrs[] = {	\
2794*2407f5d6SJonathan Lemon 		&dev_attr_freq##_nr##_seconds.attr.attr,		\
2795*2407f5d6SJonathan Lemon 		&dev_attr_freq##_nr##_frequency.attr.attr,		\
2796*2407f5d6SJonathan Lemon 		NULL,							\
2797*2407f5d6SJonathan Lemon 	}
2798*2407f5d6SJonathan Lemon 
2799*2407f5d6SJonathan Lemon #define DEVICE_FREQ_GROUP(_name, _nr)					\
2800*2407f5d6SJonathan Lemon 	_DEVICE_FREQ_GROUP_ATTRS(_nr);					\
2801*2407f5d6SJonathan Lemon 	static const struct attribute_group				\
2802*2407f5d6SJonathan Lemon 			fb_timecard_freq##_nr##_group = {		\
2803*2407f5d6SJonathan Lemon 		.name = #_name,						\
2804*2407f5d6SJonathan Lemon 		.attrs = fb_timecard_freq##_nr##_attrs,			\
2805*2407f5d6SJonathan Lemon }
2806*2407f5d6SJonathan Lemon 
2807*2407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq1, 0);
2808*2407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq2, 1);
2809*2407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq3, 2);
2810*2407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq4, 3);
2811*2407f5d6SJonathan Lemon 
2812c205d53cSJonathan Lemon static struct attribute *fb_timecard_attrs[] = {
2813773bda96SJonathan Lemon 	&dev_attr_serialnum.attr,
2814ef0cfb34SJonathan Lemon 	&dev_attr_gnss_sync.attr,
2815773bda96SJonathan Lemon 	&dev_attr_clock_source.attr,
2816773bda96SJonathan Lemon 	&dev_attr_available_clock_sources.attr,
2817e1daf0ecSJonathan Lemon 	&dev_attr_sma1.attr,
2818e1daf0ecSJonathan Lemon 	&dev_attr_sma2.attr,
2819e1daf0ecSJonathan Lemon 	&dev_attr_sma3.attr,
2820e1daf0ecSJonathan Lemon 	&dev_attr_sma4.attr,
2821e1daf0ecSJonathan Lemon 	&dev_attr_available_sma_inputs.attr,
2822e1daf0ecSJonathan Lemon 	&dev_attr_available_sma_outputs.attr,
28232f23f486SVadim Fedorenko 	&dev_attr_clock_status_drift.attr,
28242f23f486SVadim Fedorenko 	&dev_attr_clock_status_offset.attr,
2825d14ee252SJonathan Lemon 	&dev_attr_irig_b_mode.attr,
282689260d87SJonathan Lemon 	&dev_attr_utc_tai_offset.attr,
28271acffc6eSJonathan Lemon 	&dev_attr_ts_window_adjust.attr,
282844a412d1SVadim Fedorenko 	&dev_attr_tod_correction.attr,
2829773bda96SJonathan Lemon 	NULL,
2830773bda96SJonathan Lemon };
2831c205d53cSJonathan Lemon static const struct attribute_group fb_timecard_group = {
2832c205d53cSJonathan Lemon 	.attrs = fb_timecard_attrs,
2833c205d53cSJonathan Lemon };
2834c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[] = {
2835c205d53cSJonathan Lemon 	{ .cap = OCP_CAP_BASIC,	    .group = &fb_timecard_group },
2836b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal0_group },
2837b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal1_group },
2838b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal2_group },
2839b325af3cSJonathan Lemon 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal3_group },
2840*2407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq0_group },
2841*2407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq1_group },
2842*2407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq2_group },
2843*2407f5d6SJonathan Lemon 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq3_group },
2844c205d53cSJonathan Lemon 	{ },
2845c205d53cSJonathan Lemon };
2846773bda96SJonathan Lemon 
2847a509a7c6SJonathan Lemon static void
2848a509a7c6SJonathan Lemon gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
2849a509a7c6SJonathan Lemon 	       const char *def)
2850f67bf662SJonathan Lemon {
2851a509a7c6SJonathan Lemon 	int i;
2852f67bf662SJonathan Lemon 
2853a509a7c6SJonathan Lemon 	for (i = 0; i < 4; i++) {
2854a509a7c6SJonathan Lemon 		if (bp->sma[i].mode != SMA_MODE_IN)
2855a509a7c6SJonathan Lemon 			continue;
2856a509a7c6SJonathan Lemon 		if (map[i][0] & (1 << bit)) {
2857a509a7c6SJonathan Lemon 			sprintf(buf, "sma%d", i + 1);
2858a509a7c6SJonathan Lemon 			return;
2859a509a7c6SJonathan Lemon 		}
2860a509a7c6SJonathan Lemon 	}
2861a509a7c6SJonathan Lemon 	if (!def)
2862a509a7c6SJonathan Lemon 		def = "----";
2863a509a7c6SJonathan Lemon 	strcpy(buf, def);
2864f67bf662SJonathan Lemon }
2865f67bf662SJonathan Lemon 
2866f67bf662SJonathan Lemon static void
2867a509a7c6SJonathan Lemon gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
2868f67bf662SJonathan Lemon {
2869f67bf662SJonathan Lemon 	char *ans = buf;
2870a509a7c6SJonathan Lemon 	int i;
2871f67bf662SJonathan Lemon 
2872a509a7c6SJonathan Lemon 	strcpy(ans, "----");
2873a509a7c6SJonathan Lemon 	for (i = 0; i < 4; i++) {
2874a509a7c6SJonathan Lemon 		if (bp->sma[i].mode != SMA_MODE_OUT)
2875a509a7c6SJonathan Lemon 			continue;
2876a509a7c6SJonathan Lemon 		if (map[i][1] & (1 << bit))
2877a509a7c6SJonathan Lemon 			ans += sprintf(ans, "sma%d ", i + 1);
2878a509a7c6SJonathan Lemon 	}
2879f67bf662SJonathan Lemon }
2880f67bf662SJonathan Lemon 
2881b325af3cSJonathan Lemon static void
2882b325af3cSJonathan Lemon _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
2883b325af3cSJonathan Lemon {
2884b325af3cSJonathan Lemon 	struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
2885b325af3cSJonathan Lemon 	struct ptp_ocp_signal *signal = &bp->signal[nr];
2886b325af3cSJonathan Lemon 	char label[8];
2887b325af3cSJonathan Lemon 	bool on;
2888b325af3cSJonathan Lemon 	u32 val;
2889b325af3cSJonathan Lemon 
2890b325af3cSJonathan Lemon 	if (!signal)
2891b325af3cSJonathan Lemon 		return;
2892b325af3cSJonathan Lemon 
2893b325af3cSJonathan Lemon 	on = signal->running;
2894b325af3cSJonathan Lemon 	sprintf(label, "GEN%d", nr);
2895b325af3cSJonathan Lemon 	seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
2896b325af3cSJonathan Lemon 		   label, on ? " ON" : "OFF",
2897b325af3cSJonathan Lemon 		   signal->period, signal->duty, signal->phase,
2898b325af3cSJonathan Lemon 		   signal->polarity);
2899b325af3cSJonathan Lemon 
2900b325af3cSJonathan Lemon 	val = ioread32(&reg->enable);
2901b325af3cSJonathan Lemon 	seq_printf(s, " [%x", val);
2902b325af3cSJonathan Lemon 	val = ioread32(&reg->status);
2903b325af3cSJonathan Lemon 	seq_printf(s, " %x]", val);
2904b325af3cSJonathan Lemon 
2905b325af3cSJonathan Lemon 	seq_printf(s, " start:%llu\n", signal->start);
2906b325af3cSJonathan Lemon }
2907b325af3cSJonathan Lemon 
2908*2407f5d6SJonathan Lemon static void
2909*2407f5d6SJonathan Lemon _frequency_summary_show(struct seq_file *s, int nr,
2910*2407f5d6SJonathan Lemon 			struct frequency_reg __iomem *reg)
2911*2407f5d6SJonathan Lemon {
2912*2407f5d6SJonathan Lemon 	char label[8];
2913*2407f5d6SJonathan Lemon 	bool on;
2914*2407f5d6SJonathan Lemon 	u32 val;
2915*2407f5d6SJonathan Lemon 
2916*2407f5d6SJonathan Lemon 	if (!reg)
2917*2407f5d6SJonathan Lemon 		return;
2918*2407f5d6SJonathan Lemon 
2919*2407f5d6SJonathan Lemon 	sprintf(label, "FREQ%d", nr);
2920*2407f5d6SJonathan Lemon 	val = ioread32(&reg->ctrl);
2921*2407f5d6SJonathan Lemon 	on = val & 1;
2922*2407f5d6SJonathan Lemon 	val = (val >> 8) & 0xff;
2923*2407f5d6SJonathan Lemon 	seq_printf(s, "%7s: %s, sec:%u",
2924*2407f5d6SJonathan Lemon 		   label,
2925*2407f5d6SJonathan Lemon 		   on ? " ON" : "OFF",
2926*2407f5d6SJonathan Lemon 		   val);
2927*2407f5d6SJonathan Lemon 
2928*2407f5d6SJonathan Lemon 	val = ioread32(&reg->status);
2929*2407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_ERROR)
2930*2407f5d6SJonathan Lemon 		seq_printf(s, ", error");
2931*2407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_OVERRUN)
2932*2407f5d6SJonathan Lemon 		seq_printf(s, ", overrun");
2933*2407f5d6SJonathan Lemon 	if (val & FREQ_STATUS_VALID)
2934*2407f5d6SJonathan Lemon 		seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
2935*2407f5d6SJonathan Lemon 	seq_printf(s, "  reg:%x\n", val);
2936*2407f5d6SJonathan Lemon }
2937*2407f5d6SJonathan Lemon 
2938f67bf662SJonathan Lemon static int
2939f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data)
2940f67bf662SJonathan Lemon {
2941f67bf662SJonathan Lemon 	struct device *dev = s->private;
2942f67bf662SJonathan Lemon 	struct ptp_system_timestamp sts;
2943a509a7c6SJonathan Lemon 	u16 sma_val[4][2], ctrl, val;
2944f67bf662SJonathan Lemon 	struct ts_reg __iomem *ts_reg;
2945f67bf662SJonathan Lemon 	struct timespec64 ts;
2946f67bf662SJonathan Lemon 	struct ptp_ocp *bp;
2947a509a7c6SJonathan Lemon 	char *src, *buf;
2948a62a56d0SJonathan Lemon 	bool on, map;
2949b325af3cSJonathan Lemon 	int i;
2950f67bf662SJonathan Lemon 
2951f67bf662SJonathan Lemon 	buf = (char *)__get_free_page(GFP_KERNEL);
2952f67bf662SJonathan Lemon 	if (!buf)
2953f67bf662SJonathan Lemon 		return -ENOMEM;
2954f67bf662SJonathan Lemon 
2955f67bf662SJonathan Lemon 	bp = dev_get_drvdata(dev);
2956f67bf662SJonathan Lemon 
2957f67bf662SJonathan Lemon 	seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
295861fd7ac2SJonathan Lemon 	if (bp->gnss_port != -1)
295961fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port);
296061fd7ac2SJonathan Lemon 	if (bp->gnss2_port != -1)
296161fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port);
296261fd7ac2SJonathan Lemon 	if (bp->mac_port != -1)
296361fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port);
296461fd7ac2SJonathan Lemon 	if (bp->nmea_port != -1)
296561fd7ac2SJonathan Lemon 		seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port);
2966f67bf662SJonathan Lemon 
2967a509a7c6SJonathan Lemon 	memset(sma_val, 0xff, sizeof(sma_val));
2968a509a7c6SJonathan Lemon 	if (bp->sma_map1) {
2969a509a7c6SJonathan Lemon 		u32 reg;
2970a509a7c6SJonathan Lemon 
2971a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio1);
2972a509a7c6SJonathan Lemon 		sma_val[0][0] = reg & 0xffff;
2973a509a7c6SJonathan Lemon 		sma_val[1][0] = reg >> 16;
2974a509a7c6SJonathan Lemon 
2975a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map1->gpio2);
2976a509a7c6SJonathan Lemon 		sma_val[2][1] = reg & 0xffff;
2977a509a7c6SJonathan Lemon 		sma_val[3][1] = reg >> 16;
2978a509a7c6SJonathan Lemon 
2979a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map2->gpio1);
2980a509a7c6SJonathan Lemon 		sma_val[2][0] = reg & 0xffff;
2981a509a7c6SJonathan Lemon 		sma_val[3][0] = reg >> 16;
2982a509a7c6SJonathan Lemon 
2983a509a7c6SJonathan Lemon 		reg = ioread32(&bp->sma_map2->gpio2);
2984a509a7c6SJonathan Lemon 		sma_val[0][1] = reg & 0xffff;
2985a509a7c6SJonathan Lemon 		sma_val[1][1] = reg >> 16;
2986a509a7c6SJonathan Lemon 	}
2987a509a7c6SJonathan Lemon 
2988f67bf662SJonathan Lemon 	sma1_show(dev, NULL, buf);
2989a509a7c6SJonathan Lemon 	seq_printf(s, "   sma1: %04x,%04x %s",
2990a509a7c6SJonathan Lemon 		   sma_val[0][0], sma_val[0][1], buf);
2991f67bf662SJonathan Lemon 
2992f67bf662SJonathan Lemon 	sma2_show(dev, NULL, buf);
2993a509a7c6SJonathan Lemon 	seq_printf(s, "   sma2: %04x,%04x %s",
2994a509a7c6SJonathan Lemon 		   sma_val[1][0], sma_val[1][1], buf);
2995f67bf662SJonathan Lemon 
2996f67bf662SJonathan Lemon 	sma3_show(dev, NULL, buf);
2997a509a7c6SJonathan Lemon 	seq_printf(s, "   sma3: %04x,%04x %s",
2998a509a7c6SJonathan Lemon 		   sma_val[2][0], sma_val[2][1], buf);
2999f67bf662SJonathan Lemon 
3000f67bf662SJonathan Lemon 	sma4_show(dev, NULL, buf);
3001a509a7c6SJonathan Lemon 	seq_printf(s, "   sma4: %04x,%04x %s",
3002a509a7c6SJonathan Lemon 		   sma_val[3][0], sma_val[3][1], buf);
3003f67bf662SJonathan Lemon 
3004f67bf662SJonathan Lemon 	if (bp->ts0) {
3005f67bf662SJonathan Lemon 		ts_reg = bp->ts0->mem;
3006f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3007be69087cSJonathan Lemon 		src = "GNSS1";
3008f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS0",
3009f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", src);
3010f67bf662SJonathan Lemon 	}
3011f67bf662SJonathan Lemon 
3012f67bf662SJonathan Lemon 	if (bp->ts1) {
3013f67bf662SJonathan Lemon 		ts_reg = bp->ts1->mem;
3014f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3015a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 2, NULL);
3016f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS1",
3017a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", buf);
3018f67bf662SJonathan Lemon 	}
3019f67bf662SJonathan Lemon 
3020f67bf662SJonathan Lemon 	if (bp->ts2) {
3021f67bf662SJonathan Lemon 		ts_reg = bp->ts2->mem;
3022f67bf662SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3023a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 3, NULL);
3024f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS2",
3025a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", buf);
3026f67bf662SJonathan Lemon 	}
3027f67bf662SJonathan Lemon 
3028a62a56d0SJonathan Lemon 	if (bp->pps) {
3029a62a56d0SJonathan Lemon 		ts_reg = bp->pps->mem;
3030a62a56d0SJonathan Lemon 		src = "PHC";
3031a62a56d0SJonathan Lemon 		on = ioread32(&ts_reg->enable);
3032a62a56d0SJonathan Lemon 		map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
3033a62a56d0SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "TS3",
30341a575cdeSNathan Chancellor 			   on && map ? " ON" : "OFF", src);
3035a62a56d0SJonathan Lemon 
3036a62a56d0SJonathan Lemon 		map = !!(bp->pps_req_map & OCP_REQ_PPS);
3037a62a56d0SJonathan Lemon 		seq_printf(s, "%7s: %s, src: %s\n", "PPS",
30381a575cdeSNathan Chancellor 			   on && map ? " ON" : "OFF", src);
3039a62a56d0SJonathan Lemon 	}
3040a62a56d0SJonathan Lemon 
3041b325af3cSJonathan Lemon 	if (bp->fw_cap & OCP_CAP_SIGNAL)
3042b325af3cSJonathan Lemon 		for (i = 0; i < 4; i++)
3043b325af3cSJonathan Lemon 			_signal_summary_show(s, bp, i);
3044b325af3cSJonathan Lemon 
3045*2407f5d6SJonathan Lemon 	if (bp->fw_cap & OCP_CAP_FREQ)
3046*2407f5d6SJonathan Lemon 		for (i = 0; i < 4; i++)
3047*2407f5d6SJonathan Lemon 			_frequency_summary_show(s, i, bp->freq_in[i]);
3048*2407f5d6SJonathan Lemon 
3049f67bf662SJonathan Lemon 	if (bp->irig_out) {
3050f67bf662SJonathan Lemon 		ctrl = ioread32(&bp->irig_out->ctrl);
3051f67bf662SJonathan Lemon 		on = ctrl & IRIG_M_CTRL_ENABLE;
3052f67bf662SJonathan Lemon 		val = ioread32(&bp->irig_out->status);
3053a509a7c6SJonathan Lemon 		gpio_output_map(buf, bp, sma_val, 4);
3054f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
3055f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", val, (ctrl >> 16), buf);
3056f67bf662SJonathan Lemon 	}
3057f67bf662SJonathan Lemon 
3058f67bf662SJonathan Lemon 	if (bp->irig_in) {
3059f67bf662SJonathan Lemon 		on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
3060f67bf662SJonathan Lemon 		val = ioread32(&bp->irig_in->status);
3061a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 4, NULL);
3062f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
3063a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3064f67bf662SJonathan Lemon 	}
3065f67bf662SJonathan Lemon 
3066f67bf662SJonathan Lemon 	if (bp->dcf_out) {
3067f67bf662SJonathan Lemon 		on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
3068f67bf662SJonathan Lemon 		val = ioread32(&bp->dcf_out->status);
3069a509a7c6SJonathan Lemon 		gpio_output_map(buf, bp, sma_val, 5);
3070f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
3071f67bf662SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3072f67bf662SJonathan Lemon 	}
3073f67bf662SJonathan Lemon 
3074f67bf662SJonathan Lemon 	if (bp->dcf_in) {
3075f67bf662SJonathan Lemon 		on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
3076f67bf662SJonathan Lemon 		val = ioread32(&bp->dcf_in->status);
3077a509a7c6SJonathan Lemon 		gpio_input_map(buf, bp, sma_val, 5, NULL);
3078f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
3079a509a7c6SJonathan Lemon 			   on ? " ON" : "OFF", val, buf);
3080f67bf662SJonathan Lemon 	}
3081f67bf662SJonathan Lemon 
3082e3516bb4SJonathan Lemon 	if (bp->nmea_out) {
3083e3516bb4SJonathan Lemon 		on = ioread32(&bp->nmea_out->ctrl) & 1;
3084e3516bb4SJonathan Lemon 		val = ioread32(&bp->nmea_out->status);
3085e3516bb4SJonathan Lemon 		seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
3086e3516bb4SJonathan Lemon 			   on ? " ON" : "OFF", val);
3087e3516bb4SJonathan Lemon 	}
3088e3516bb4SJonathan Lemon 
3089f67bf662SJonathan Lemon 	/* compute src for PPS1, used below. */
3090f67bf662SJonathan Lemon 	if (bp->pps_select) {
3091f67bf662SJonathan Lemon 		val = ioread32(&bp->pps_select->gpio1);
3092a509a7c6SJonathan Lemon 		src = &buf[80];
3093f67bf662SJonathan Lemon 		if (val & 0x01)
3094a509a7c6SJonathan Lemon 			gpio_input_map(src, bp, sma_val, 0, NULL);
3095f67bf662SJonathan Lemon 		else if (val & 0x02)
3096f67bf662SJonathan Lemon 			src = "MAC";
3097f67bf662SJonathan Lemon 		else if (val & 0x04)
3098be69087cSJonathan Lemon 			src = "GNSS1";
3099f67bf662SJonathan Lemon 		else
3100f67bf662SJonathan Lemon 			src = "----";
3101f67bf662SJonathan Lemon 	} else {
3102f67bf662SJonathan Lemon 		src = "?";
3103f67bf662SJonathan Lemon 	}
3104f67bf662SJonathan Lemon 
3105f67bf662SJonathan Lemon 	/* assumes automatic switchover/selection */
3106f67bf662SJonathan Lemon 	val = ioread32(&bp->reg->select);
3107f67bf662SJonathan Lemon 	switch (val >> 16) {
3108f67bf662SJonathan Lemon 	case 0:
3109f67bf662SJonathan Lemon 		sprintf(buf, "----");
3110f67bf662SJonathan Lemon 		break;
3111f67bf662SJonathan Lemon 	case 2:
3112f67bf662SJonathan Lemon 		sprintf(buf, "IRIG");
3113f67bf662SJonathan Lemon 		break;
3114f67bf662SJonathan Lemon 	case 3:
3115f67bf662SJonathan Lemon 		sprintf(buf, "%s via PPS1", src);
3116f67bf662SJonathan Lemon 		break;
3117f67bf662SJonathan Lemon 	case 6:
3118f67bf662SJonathan Lemon 		sprintf(buf, "DCF");
3119f67bf662SJonathan Lemon 		break;
3120f67bf662SJonathan Lemon 	default:
3121f67bf662SJonathan Lemon 		strcpy(buf, "unknown");
3122f67bf662SJonathan Lemon 		break;
3123f67bf662SJonathan Lemon 	}
3124f67bf662SJonathan Lemon 	val = ioread32(&bp->reg->status);
3125f67bf662SJonathan Lemon 	seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
3126f67bf662SJonathan Lemon 		   val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced");
3127f67bf662SJonathan Lemon 
3128f67bf662SJonathan Lemon 	/* reuses PPS1 src from earlier */
3129f67bf662SJonathan Lemon 	seq_printf(s, "MAC PPS1 src: %s\n", src);
3130f67bf662SJonathan Lemon 
3131a509a7c6SJonathan Lemon 	gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
3132a509a7c6SJonathan Lemon 	seq_printf(s, "MAC PPS2 src: %s\n", buf);
3133f67bf662SJonathan Lemon 
3134f67bf662SJonathan Lemon 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
3135f67bf662SJonathan Lemon 		struct timespec64 sys_ts;
3136f67bf662SJonathan Lemon 		s64 pre_ns, post_ns, ns;
3137f67bf662SJonathan Lemon 
3138f67bf662SJonathan Lemon 		pre_ns = timespec64_to_ns(&sts.pre_ts);
3139f67bf662SJonathan Lemon 		post_ns = timespec64_to_ns(&sts.post_ts);
3140f67bf662SJonathan Lemon 		ns = (pre_ns + post_ns) / 2;
3141f67bf662SJonathan Lemon 		ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
3142f67bf662SJonathan Lemon 		sys_ts = ns_to_timespec64(ns);
3143f67bf662SJonathan Lemon 
3144f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC",
3145f67bf662SJonathan Lemon 			   ts.tv_sec, ts.tv_nsec, &ts);
3146f67bf662SJonathan Lemon 		seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS",
3147f67bf662SJonathan Lemon 			   sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts,
3148f67bf662SJonathan Lemon 			   bp->utc_tai_offset);
3149f67bf662SJonathan Lemon 		seq_printf(s, "%7s: PHC:SYS offset: %lld  window: %lld\n", "",
3150f67bf662SJonathan Lemon 			   timespec64_to_ns(&ts) - ns,
3151f67bf662SJonathan Lemon 			   post_ns - pre_ns);
3152f67bf662SJonathan Lemon 	}
3153f67bf662SJonathan Lemon 
3154f67bf662SJonathan Lemon 	free_page((unsigned long)buf);
3155f67bf662SJonathan Lemon 	return 0;
3156f67bf662SJonathan Lemon }
3157f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
3158f67bf662SJonathan Lemon 
31599f492c4cSVadim Fedorenko static int
31609f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data)
31619f492c4cSVadim Fedorenko {
31629f492c4cSVadim Fedorenko 	struct device *dev = s->private;
31639f492c4cSVadim Fedorenko 	struct ptp_ocp *bp;
31649f492c4cSVadim Fedorenko 	u32 val;
31659f492c4cSVadim Fedorenko 	int idx;
31669f492c4cSVadim Fedorenko 
31679f492c4cSVadim Fedorenko 	bp = dev_get_drvdata(dev);
31689f492c4cSVadim Fedorenko 
31699f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->ctrl);
31709f492c4cSVadim Fedorenko 	if (!(val & TOD_CTRL_ENABLE)) {
31719f492c4cSVadim Fedorenko 		seq_printf(s, "TOD Slave disabled\n");
31729f492c4cSVadim Fedorenko 		return 0;
31739f492c4cSVadim Fedorenko 	}
31749f492c4cSVadim Fedorenko 	seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
31759f492c4cSVadim Fedorenko 
31769f492c4cSVadim Fedorenko 	idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
31779f492c4cSVadim Fedorenko 	idx += (val >> 16) & 3;
31789f492c4cSVadim Fedorenko 	seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
31799f492c4cSVadim Fedorenko 
31809f492c4cSVadim Fedorenko 	idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
31819f492c4cSVadim Fedorenko 	seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
31829f492c4cSVadim Fedorenko 
31839f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->version);
31849f492c4cSVadim Fedorenko 	seq_printf(s, "TOD Version %d.%d.%d\n",
31859f492c4cSVadim Fedorenko 		val >> 24, (val >> 16) & 0xff, val & 0xffff);
31869f492c4cSVadim Fedorenko 
31879f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->status);
31889f492c4cSVadim Fedorenko 	seq_printf(s, "Status register: 0x%08X\n", val);
31899f492c4cSVadim Fedorenko 
31909f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->adj_sec);
31919f492c4cSVadim Fedorenko 	idx = (val & ~INT_MAX) ? -1 : 1;
31929f492c4cSVadim Fedorenko 	idx *= (val & INT_MAX);
31939f492c4cSVadim Fedorenko 	seq_printf(s, "Correction seconds: %d\n", idx);
31949f492c4cSVadim Fedorenko 
31959f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->utc_status);
31969f492c4cSVadim Fedorenko 	seq_printf(s, "UTC status register: 0x%08X\n", val);
31979f492c4cSVadim Fedorenko 	seq_printf(s, "UTC offset: %d  valid:%d\n",
31989f492c4cSVadim Fedorenko 		val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
31999f492c4cSVadim Fedorenko 	seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
32009f492c4cSVadim Fedorenko 		val & TOD_STATUS_LEAP_VALID ? 1 : 0,
32019f492c4cSVadim Fedorenko 		val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
32029f492c4cSVadim Fedorenko 
32039f492c4cSVadim Fedorenko 	val = ioread32(&bp->tod->leap);
32049f492c4cSVadim Fedorenko 	seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
32059f492c4cSVadim Fedorenko 
32069f492c4cSVadim Fedorenko 	return 0;
32079f492c4cSVadim Fedorenko }
32089f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
32099f492c4cSVadim Fedorenko 
3210f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root;
3211f67bf662SJonathan Lemon 
3212f67bf662SJonathan Lemon static void
3213f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
3214f67bf662SJonathan Lemon {
3215f67bf662SJonathan Lemon 	struct dentry *d;
3216f67bf662SJonathan Lemon 
3217f67bf662SJonathan Lemon 	d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
3218f67bf662SJonathan Lemon 	bp->debug_root = d;
3219f67bf662SJonathan Lemon 	debugfs_create_file("summary", 0444, bp->debug_root,
3220f67bf662SJonathan Lemon 			    &bp->dev, &ptp_ocp_summary_fops);
32219f492c4cSVadim Fedorenko 	if (bp->tod)
32229f492c4cSVadim Fedorenko 		debugfs_create_file("tod_status", 0444, bp->debug_root,
32239f492c4cSVadim Fedorenko 				    &bp->dev, &ptp_ocp_tod_status_fops);
3224f67bf662SJonathan Lemon }
3225f67bf662SJonathan Lemon 
3226f67bf662SJonathan Lemon static void
3227f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
3228f67bf662SJonathan Lemon {
3229f67bf662SJonathan Lemon 	debugfs_remove_recursive(bp->debug_root);
3230f67bf662SJonathan Lemon }
3231f67bf662SJonathan Lemon 
3232f67bf662SJonathan Lemon static void
3233f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void)
3234f67bf662SJonathan Lemon {
3235f67bf662SJonathan Lemon 	ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
3236f67bf662SJonathan Lemon }
3237f67bf662SJonathan Lemon 
3238f67bf662SJonathan Lemon static void
3239f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void)
3240f67bf662SJonathan Lemon {
3241f67bf662SJonathan Lemon 	debugfs_remove_recursive(ptp_ocp_debugfs_root);
3242f67bf662SJonathan Lemon }
3243f67bf662SJonathan Lemon 
3244773bda96SJonathan Lemon static void
3245773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev)
3246773bda96SJonathan Lemon {
3247773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3248773bda96SJonathan Lemon 
3249773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
3250773bda96SJonathan Lemon 	idr_remove(&ptp_ocp_idr, bp->id);
3251773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
3252773bda96SJonathan Lemon }
3253773bda96SJonathan Lemon 
3254773bda96SJonathan Lemon static int
3255773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
3256773bda96SJonathan Lemon {
3257773bda96SJonathan Lemon 	int err;
3258773bda96SJonathan Lemon 
3259773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
3260773bda96SJonathan Lemon 	err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
3261773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
3262773bda96SJonathan Lemon 	if (err < 0) {
3263773bda96SJonathan Lemon 		dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
3264773bda96SJonathan Lemon 		return err;
3265773bda96SJonathan Lemon 	}
3266773bda96SJonathan Lemon 	bp->id = err;
3267773bda96SJonathan Lemon 
3268773bda96SJonathan Lemon 	bp->ptp_info = ptp_ocp_clock_info;
3269773bda96SJonathan Lemon 	spin_lock_init(&bp->lock);
3270ef0cfb34SJonathan Lemon 	bp->gnss_port = -1;
327171d7e085SJonathan Lemon 	bp->gnss2_port = -1;
3272773bda96SJonathan Lemon 	bp->mac_port = -1;
3273e3516bb4SJonathan Lemon 	bp->nmea_port = -1;
3274773bda96SJonathan Lemon 	bp->pdev = pdev;
3275773bda96SJonathan Lemon 
3276773bda96SJonathan Lemon 	device_initialize(&bp->dev);
3277773bda96SJonathan Lemon 	dev_set_name(&bp->dev, "ocp%d", bp->id);
3278773bda96SJonathan Lemon 	bp->dev.class = &timecard_class;
3279773bda96SJonathan Lemon 	bp->dev.parent = &pdev->dev;
3280773bda96SJonathan Lemon 	bp->dev.release = ptp_ocp_dev_release;
3281773bda96SJonathan Lemon 	dev_set_drvdata(&bp->dev, bp);
3282773bda96SJonathan Lemon 
3283773bda96SJonathan Lemon 	err = device_add(&bp->dev);
3284773bda96SJonathan Lemon 	if (err) {
3285773bda96SJonathan Lemon 		dev_err(&bp->dev, "device add failed: %d\n", err);
3286773bda96SJonathan Lemon 		goto out;
3287773bda96SJonathan Lemon 	}
3288773bda96SJonathan Lemon 
3289773bda96SJonathan Lemon 	pci_set_drvdata(pdev, bp);
3290773bda96SJonathan Lemon 
3291773bda96SJonathan Lemon 	return 0;
3292773bda96SJonathan Lemon 
3293773bda96SJonathan Lemon out:
3294773bda96SJonathan Lemon 	ptp_ocp_dev_release(&bp->dev);
3295d12f23faSJonathan Lemon 	put_device(&bp->dev);
3296773bda96SJonathan Lemon 	return err;
3297773bda96SJonathan Lemon }
3298773bda96SJonathan Lemon 
3299773bda96SJonathan Lemon static void
3300773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
3301773bda96SJonathan Lemon {
3302773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
3303773bda96SJonathan Lemon 
3304773bda96SJonathan Lemon 	if (sysfs_create_link(&dev->kobj, &child->kobj, link))
3305773bda96SJonathan Lemon 		dev_err(dev, "%s symlink failed\n", link);
3306773bda96SJonathan Lemon }
3307773bda96SJonathan Lemon 
3308773bda96SJonathan Lemon static void
3309773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
3310773bda96SJonathan Lemon {
3311773bda96SJonathan Lemon 	struct device *dev, *child;
3312773bda96SJonathan Lemon 
3313773bda96SJonathan Lemon 	dev = &bp->pdev->dev;
3314773bda96SJonathan Lemon 
3315773bda96SJonathan Lemon 	child = device_find_child_by_name(dev, name);
3316773bda96SJonathan Lemon 	if (!child) {
3317773bda96SJonathan Lemon 		dev_err(dev, "Could not find device %s\n", name);
3318773bda96SJonathan Lemon 		return;
3319773bda96SJonathan Lemon 	}
3320773bda96SJonathan Lemon 
3321773bda96SJonathan Lemon 	ptp_ocp_symlink(bp, child, link);
3322773bda96SJonathan Lemon 	put_device(child);
3323773bda96SJonathan Lemon }
3324773bda96SJonathan Lemon 
3325773bda96SJonathan Lemon static int
3326773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp)
3327773bda96SJonathan Lemon {
3328773bda96SJonathan Lemon 	struct pps_device *pps;
3329773bda96SJonathan Lemon 	char buf[32];
3330c205d53cSJonathan Lemon 	int i, err;
3331773bda96SJonathan Lemon 
3332ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1) {
3333ef0cfb34SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->gnss_port);
3334ef0cfb34SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyGNSS");
3335773bda96SJonathan Lemon 	}
333671d7e085SJonathan Lemon 	if (bp->gnss2_port != -1) {
333771d7e085SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->gnss2_port);
333871d7e085SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyGNSS2");
333971d7e085SJonathan Lemon 	}
3340773bda96SJonathan Lemon 	if (bp->mac_port != -1) {
3341773bda96SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->mac_port);
3342773bda96SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyMAC");
3343773bda96SJonathan Lemon 	}
3344e3516bb4SJonathan Lemon 	if (bp->nmea_port != -1) {
3345e3516bb4SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->nmea_port);
3346e3516bb4SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyNMEA");
3347e3516bb4SJonathan Lemon 	}
3348773bda96SJonathan Lemon 	sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
3349773bda96SJonathan Lemon 	ptp_ocp_link_child(bp, buf, "ptp");
3350773bda96SJonathan Lemon 
3351773bda96SJonathan Lemon 	pps = pps_lookup_dev(bp->ptp);
3352773bda96SJonathan Lemon 	if (pps)
3353773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, pps->dev, "pps");
3354773bda96SJonathan Lemon 
3355c205d53cSJonathan Lemon 	for (i = 0; bp->attr_tbl[i].cap; i++) {
3356c205d53cSJonathan Lemon 		if (!(bp->attr_tbl[i].cap & bp->fw_cap))
3357c205d53cSJonathan Lemon 			continue;
3358c205d53cSJonathan Lemon 		err = sysfs_create_group(&bp->dev.kobj, bp->attr_tbl[i].group);
3359c205d53cSJonathan Lemon 		if (err)
3360c205d53cSJonathan Lemon 			return err;
3361c205d53cSJonathan Lemon 	}
3362773bda96SJonathan Lemon 
3363f67bf662SJonathan Lemon 	ptp_ocp_debugfs_add_device(bp);
3364f67bf662SJonathan Lemon 
3365773bda96SJonathan Lemon 	return 0;
3366773bda96SJonathan Lemon }
3367773bda96SJonathan Lemon 
3368773bda96SJonathan Lemon static void
3369065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp)
3370065efcc5SJonathan Lemon {
3371065efcc5SJonathan Lemon 	struct timespec64 ts;
3372065efcc5SJonathan Lemon 	u32 version, select;
3373065efcc5SJonathan Lemon 	bool sync;
3374065efcc5SJonathan Lemon 
3375065efcc5SJonathan Lemon 	version = ioread32(&bp->reg->version);
3376065efcc5SJonathan Lemon 	select = ioread32(&bp->reg->select);
3377065efcc5SJonathan Lemon 	dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
3378065efcc5SJonathan Lemon 		 version >> 24, (version >> 16) & 0xff, version & 0xffff,
3379065efcc5SJonathan Lemon 		 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
3380065efcc5SJonathan Lemon 		 ptp_clock_index(bp->ptp));
3381065efcc5SJonathan Lemon 
3382065efcc5SJonathan Lemon 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
3383065efcc5SJonathan Lemon 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
3384065efcc5SJonathan Lemon 		dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
3385065efcc5SJonathan Lemon 			 ts.tv_sec, ts.tv_nsec,
3386065efcc5SJonathan Lemon 			 sync ? "in-sync" : "UNSYNCED");
3387065efcc5SJonathan Lemon }
3388065efcc5SJonathan Lemon 
3389065efcc5SJonathan Lemon static void
3390065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
3391065efcc5SJonathan Lemon {
3392065efcc5SJonathan Lemon 	if (port != -1)
3393065efcc5SJonathan Lemon 		dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
3394065efcc5SJonathan Lemon }
3395065efcc5SJonathan Lemon 
3396065efcc5SJonathan Lemon static void
3397065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp)
3398773bda96SJonathan Lemon {
3399e3516bb4SJonathan Lemon 	static int nmea_baud[] = {
3400e3516bb4SJonathan Lemon 		1200, 2400, 4800, 9600, 19200, 38400,
3401e3516bb4SJonathan Lemon 		57600, 115200, 230400, 460800, 921600,
3402e3516bb4SJonathan Lemon 		1000000, 2000000
3403e3516bb4SJonathan Lemon 	};
3404773bda96SJonathan Lemon 	struct device *dev = &bp->pdev->dev;
3405e3516bb4SJonathan Lemon 	u32 reg;
3406773bda96SJonathan Lemon 
3407065efcc5SJonathan Lemon 	ptp_ocp_phc_info(bp);
3408065efcc5SJonathan Lemon 
3409b0ca789aSJonathan Lemon 	dev_info(dev, "version %x\n", bp->fw_version);
3410b0ca789aSJonathan Lemon 	if (bp->fw_version & 0xffff)
3411773bda96SJonathan Lemon 		dev_info(dev, "regular image, version %d\n",
3412b0ca789aSJonathan Lemon 			 bp->fw_version & 0xffff);
3413773bda96SJonathan Lemon 	else
3414773bda96SJonathan Lemon 		dev_info(dev, "golden image, version %d\n",
3415b0ca789aSJonathan Lemon 			 bp->fw_version >> 16);
3416b0ca789aSJonathan Lemon 
3417065efcc5SJonathan Lemon 	ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200);
341871d7e085SJonathan Lemon 	ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200);
3419065efcc5SJonathan Lemon 	ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600);
3420e3516bb4SJonathan Lemon 	if (bp->nmea_out && bp->nmea_port != -1) {
3421e3516bb4SJonathan Lemon 		int baud = -1;
3422e3516bb4SJonathan Lemon 
3423e3516bb4SJonathan Lemon 		reg = ioread32(&bp->nmea_out->uart_baud);
3424e3516bb4SJonathan Lemon 		if (reg < ARRAY_SIZE(nmea_baud))
3425e3516bb4SJonathan Lemon 			baud = nmea_baud[reg];
3426e3516bb4SJonathan Lemon 		ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud);
3427e3516bb4SJonathan Lemon 	}
3428773bda96SJonathan Lemon }
3429773bda96SJonathan Lemon 
3430773bda96SJonathan Lemon static void
3431773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
3432773bda96SJonathan Lemon {
3433773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
3434c205d53cSJonathan Lemon 	int i;
3435773bda96SJonathan Lemon 
3436ef0cfb34SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyGNSS");
3437773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyMAC");
3438773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ptp");
3439773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "pps");
3440c205d53cSJonathan Lemon 	if (bp->attr_tbl)
3441c205d53cSJonathan Lemon 		for (i = 0; bp->attr_tbl[i].cap; i++)
3442c205d53cSJonathan Lemon 			sysfs_remove_group(&dev->kobj, bp->attr_tbl[i].group);
3443773bda96SJonathan Lemon }
3444773bda96SJonathan Lemon 
3445773bda96SJonathan Lemon static void
3446773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp)
3447773bda96SJonathan Lemon {
3448b325af3cSJonathan Lemon 	int i;
3449b325af3cSJonathan Lemon 
3450f67bf662SJonathan Lemon 	ptp_ocp_debugfs_remove_device(bp);
3451773bda96SJonathan Lemon 	ptp_ocp_detach_sysfs(bp);
3452773bda96SJonathan Lemon 	if (timer_pending(&bp->watchdog))
3453773bda96SJonathan Lemon 		del_timer_sync(&bp->watchdog);
3454773bda96SJonathan Lemon 	if (bp->ts0)
3455773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts0);
3456773bda96SJonathan Lemon 	if (bp->ts1)
3457773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts1);
3458dcf61469SJonathan Lemon 	if (bp->ts2)
3459dcf61469SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts2);
3460773bda96SJonathan Lemon 	if (bp->pps)
3461773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->pps);
3462b325af3cSJonathan Lemon 	for (i = 0; i < 4; i++)
3463b325af3cSJonathan Lemon 		if (bp->signal_out[i])
3464b325af3cSJonathan Lemon 			ptp_ocp_unregister_ext(bp->signal_out[i]);
3465ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1)
3466ef0cfb34SJonathan Lemon 		serial8250_unregister_port(bp->gnss_port);
346771d7e085SJonathan Lemon 	if (bp->gnss2_port != -1)
346871d7e085SJonathan Lemon 		serial8250_unregister_port(bp->gnss2_port);
3469773bda96SJonathan Lemon 	if (bp->mac_port != -1)
3470773bda96SJonathan Lemon 		serial8250_unregister_port(bp->mac_port);
3471e3516bb4SJonathan Lemon 	if (bp->nmea_port != -1)
3472e3516bb4SJonathan Lemon 		serial8250_unregister_port(bp->nmea_port);
3473773bda96SJonathan Lemon 	if (bp->spi_flash)
3474773bda96SJonathan Lemon 		platform_device_unregister(bp->spi_flash);
3475773bda96SJonathan Lemon 	if (bp->i2c_ctrl)
3476773bda96SJonathan Lemon 		platform_device_unregister(bp->i2c_ctrl);
3477773bda96SJonathan Lemon 	if (bp->i2c_clk)
3478773bda96SJonathan Lemon 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
3479773bda96SJonathan Lemon 	if (bp->n_irqs)
3480773bda96SJonathan Lemon 		pci_free_irq_vectors(bp->pdev);
3481773bda96SJonathan Lemon 	if (bp->ptp)
3482773bda96SJonathan Lemon 		ptp_clock_unregister(bp->ptp);
34831aa66a3aSJonathan Lemon 	kfree(bp->ptp_info.pin_config);
3484773bda96SJonathan Lemon 	device_unregister(&bp->dev);
3485773bda96SJonathan Lemon }
3486773bda96SJonathan Lemon 
3487a7e1abadSJonathan Lemon static int
3488a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3489a7e1abadSJonathan Lemon {
3490773bda96SJonathan Lemon 	struct devlink *devlink;
3491a7e1abadSJonathan Lemon 	struct ptp_ocp *bp;
3492a7e1abadSJonathan Lemon 	int err;
3493a7e1abadSJonathan Lemon 
3494919d13a7SLeon Romanovsky 	devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
3495773bda96SJonathan Lemon 	if (!devlink) {
3496773bda96SJonathan Lemon 		dev_err(&pdev->dev, "devlink_alloc failed\n");
3497a7e1abadSJonathan Lemon 		return -ENOMEM;
3498773bda96SJonathan Lemon 	}
3499773bda96SJonathan Lemon 
3500a7e1abadSJonathan Lemon 	err = pci_enable_device(pdev);
3501a7e1abadSJonathan Lemon 	if (err) {
3502a7e1abadSJonathan Lemon 		dev_err(&pdev->dev, "pci_enable_device\n");
35034587369bSJonathan Lemon 		goto out_free;
3504a7e1abadSJonathan Lemon 	}
3505a7e1abadSJonathan Lemon 
3506773bda96SJonathan Lemon 	bp = devlink_priv(devlink);
3507773bda96SJonathan Lemon 	err = ptp_ocp_device_init(bp, pdev);
3508773bda96SJonathan Lemon 	if (err)
3509d9fdbf13SJonathan Lemon 		goto out_disable;
3510a7e1abadSJonathan Lemon 
3511773bda96SJonathan Lemon 	/* compat mode.
3512773bda96SJonathan Lemon 	 * Older FPGA firmware only returns 2 irq's.
3513773bda96SJonathan Lemon 	 * allow this - if not all of the IRQ's are returned, skip the
3514773bda96SJonathan Lemon 	 * extra devices and just register the clock.
3515773bda96SJonathan Lemon 	 */
3516b325af3cSJonathan Lemon 	err = pci_alloc_irq_vectors(pdev, 1, 15, PCI_IRQ_MSI | PCI_IRQ_MSIX);
3517773bda96SJonathan Lemon 	if (err < 0) {
3518773bda96SJonathan Lemon 		dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
3519773bda96SJonathan Lemon 		goto out;
3520a7e1abadSJonathan Lemon 	}
3521773bda96SJonathan Lemon 	bp->n_irqs = err;
3522773bda96SJonathan Lemon 	pci_set_master(pdev);
3523a7e1abadSJonathan Lemon 
3524773bda96SJonathan Lemon 	err = ptp_ocp_register_resources(bp, id->driver_data);
3525a7e1abadSJonathan Lemon 	if (err)
3526a7e1abadSJonathan Lemon 		goto out;
3527a7e1abadSJonathan Lemon 
3528a7e1abadSJonathan Lemon 	bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
3529a7e1abadSJonathan Lemon 	if (IS_ERR(bp->ptp)) {
3530a7e1abadSJonathan Lemon 		err = PTR_ERR(bp->ptp);
3531773bda96SJonathan Lemon 		dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
3532773bda96SJonathan Lemon 		bp->ptp = NULL;
3533a7e1abadSJonathan Lemon 		goto out;
3534a7e1abadSJonathan Lemon 	}
3535a7e1abadSJonathan Lemon 
3536773bda96SJonathan Lemon 	err = ptp_ocp_complete(bp);
3537773bda96SJonathan Lemon 	if (err)
3538773bda96SJonathan Lemon 		goto out;
3539773bda96SJonathan Lemon 
3540a7e1abadSJonathan Lemon 	ptp_ocp_info(bp);
3541c89f78e9SLeon Romanovsky 	devlink_register(devlink);
3542a7e1abadSJonathan Lemon 	return 0;
3543a7e1abadSJonathan Lemon 
3544a7e1abadSJonathan Lemon out:
3545773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
3546773bda96SJonathan Lemon 	pci_set_drvdata(pdev, NULL);
3547d9fdbf13SJonathan Lemon out_disable:
3548d9fdbf13SJonathan Lemon 	pci_disable_device(pdev);
35494587369bSJonathan Lemon out_free:
3550773bda96SJonathan Lemon 	devlink_free(devlink);
3551a7e1abadSJonathan Lemon 	return err;
3552a7e1abadSJonathan Lemon }
3553a7e1abadSJonathan Lemon 
3554a7e1abadSJonathan Lemon static void
3555a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev)
3556a7e1abadSJonathan Lemon {
3557a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = pci_get_drvdata(pdev);
3558773bda96SJonathan Lemon 	struct devlink *devlink = priv_to_devlink(bp);
3559a7e1abadSJonathan Lemon 
3560c89f78e9SLeon Romanovsky 	devlink_unregister(devlink);
3561773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
3562a7e1abadSJonathan Lemon 	pci_set_drvdata(pdev, NULL);
3563d9fdbf13SJonathan Lemon 	pci_disable_device(pdev);
3564773bda96SJonathan Lemon 
3565773bda96SJonathan Lemon 	devlink_free(devlink);
3566a7e1abadSJonathan Lemon }
3567a7e1abadSJonathan Lemon 
3568a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = {
3569a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
3570a7e1abadSJonathan Lemon 	.id_table	= ptp_ocp_pcidev_id,
3571a7e1abadSJonathan Lemon 	.probe		= ptp_ocp_probe,
3572a7e1abadSJonathan Lemon 	.remove		= ptp_ocp_remove,
3573a7e1abadSJonathan Lemon };
3574a7e1abadSJonathan Lemon 
3575773bda96SJonathan Lemon static int
3576773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
3577773bda96SJonathan Lemon 			  unsigned long action, void *data)
3578773bda96SJonathan Lemon {
3579773bda96SJonathan Lemon 	struct device *dev, *child = data;
3580773bda96SJonathan Lemon 	struct ptp_ocp *bp;
3581773bda96SJonathan Lemon 	bool add;
3582773bda96SJonathan Lemon 
3583773bda96SJonathan Lemon 	switch (action) {
3584773bda96SJonathan Lemon 	case BUS_NOTIFY_ADD_DEVICE:
3585773bda96SJonathan Lemon 	case BUS_NOTIFY_DEL_DEVICE:
3586773bda96SJonathan Lemon 		add = action == BUS_NOTIFY_ADD_DEVICE;
3587773bda96SJonathan Lemon 		break;
3588773bda96SJonathan Lemon 	default:
3589773bda96SJonathan Lemon 		return 0;
3590773bda96SJonathan Lemon 	}
3591773bda96SJonathan Lemon 
3592773bda96SJonathan Lemon 	if (!i2c_verify_adapter(child))
3593773bda96SJonathan Lemon 		return 0;
3594773bda96SJonathan Lemon 
3595773bda96SJonathan Lemon 	dev = child;
3596773bda96SJonathan Lemon 	while ((dev = dev->parent))
3597773bda96SJonathan Lemon 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
3598773bda96SJonathan Lemon 			goto found;
3599773bda96SJonathan Lemon 	return 0;
3600773bda96SJonathan Lemon 
3601773bda96SJonathan Lemon found:
3602773bda96SJonathan Lemon 	bp = dev_get_drvdata(dev);
3603773bda96SJonathan Lemon 	if (add)
3604773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, child, "i2c");
3605773bda96SJonathan Lemon 	else
3606773bda96SJonathan Lemon 		sysfs_remove_link(&bp->dev.kobj, "i2c");
3607773bda96SJonathan Lemon 
3608773bda96SJonathan Lemon 	return 0;
3609773bda96SJonathan Lemon }
3610773bda96SJonathan Lemon 
3611773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = {
3612773bda96SJonathan Lemon 	.notifier_call = ptp_ocp_i2c_notifier_call,
3613773bda96SJonathan Lemon };
3614773bda96SJonathan Lemon 
3615a7e1abadSJonathan Lemon static int __init
3616a7e1abadSJonathan Lemon ptp_ocp_init(void)
3617a7e1abadSJonathan Lemon {
3618773bda96SJonathan Lemon 	const char *what;
3619a7e1abadSJonathan Lemon 	int err;
3620a7e1abadSJonathan Lemon 
3621f67bf662SJonathan Lemon 	ptp_ocp_debugfs_init();
3622f67bf662SJonathan Lemon 
3623773bda96SJonathan Lemon 	what = "timecard class";
3624773bda96SJonathan Lemon 	err = class_register(&timecard_class);
3625773bda96SJonathan Lemon 	if (err)
3626773bda96SJonathan Lemon 		goto out;
3627773bda96SJonathan Lemon 
3628773bda96SJonathan Lemon 	what = "i2c notifier";
3629773bda96SJonathan Lemon 	err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3630773bda96SJonathan Lemon 	if (err)
3631773bda96SJonathan Lemon 		goto out_notifier;
3632773bda96SJonathan Lemon 
3633773bda96SJonathan Lemon 	what = "ptp_ocp driver";
3634a7e1abadSJonathan Lemon 	err = pci_register_driver(&ptp_ocp_driver);
3635773bda96SJonathan Lemon 	if (err)
3636773bda96SJonathan Lemon 		goto out_register;
3637773bda96SJonathan Lemon 
3638773bda96SJonathan Lemon 	return 0;
3639773bda96SJonathan Lemon 
3640773bda96SJonathan Lemon out_register:
3641773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3642773bda96SJonathan Lemon out_notifier:
3643773bda96SJonathan Lemon 	class_unregister(&timecard_class);
3644773bda96SJonathan Lemon out:
3645f67bf662SJonathan Lemon 	ptp_ocp_debugfs_fini();
3646773bda96SJonathan Lemon 	pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
3647a7e1abadSJonathan Lemon 	return err;
3648a7e1abadSJonathan Lemon }
3649a7e1abadSJonathan Lemon 
3650a7e1abadSJonathan Lemon static void __exit
3651a7e1abadSJonathan Lemon ptp_ocp_fini(void)
3652a7e1abadSJonathan Lemon {
3653773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3654a7e1abadSJonathan Lemon 	pci_unregister_driver(&ptp_ocp_driver);
3655773bda96SJonathan Lemon 	class_unregister(&timecard_class);
3656f67bf662SJonathan Lemon 	ptp_ocp_debugfs_fini();
3657a7e1abadSJonathan Lemon }
3658a7e1abadSJonathan Lemon 
3659a7e1abadSJonathan Lemon module_init(ptp_ocp_init);
3660a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini);
3661a7e1abadSJonathan Lemon 
3662a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver");
3663a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2");
3664