xref: /linux/drivers/ptp/ptp_ocp.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
3 
4 #include <linux/bits.h>
5 #include <linux/err.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/debugfs.h>
9 #include <linux/init.h>
10 #include <linux/pci.h>
11 #include <linux/serial_8250.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk-provider.h>
14 #include <linux/platform_device.h>
15 #include <linux/platform_data/i2c-xiic.h>
16 #include <linux/platform_data/i2c-ocores.h>
17 #include <linux/ptp_clock_kernel.h>
18 #include <linux/spi/spi.h>
19 #include <linux/spi/xilinx_spi.h>
20 #include <linux/spi/altera.h>
21 #include <net/devlink.h>
22 #include <linux/i2c.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/nvmem-consumer.h>
25 #include <linux/crc16.h>
26 #include <linux/dpll.h>
27 
28 #define PCI_VENDOR_ID_FACEBOOK			0x1d9b
29 #define PCI_DEVICE_ID_FACEBOOK_TIMECARD		0x0400
30 
31 #define PCI_VENDOR_ID_CELESTICA			0x18d4
32 #define PCI_DEVICE_ID_CELESTICA_TIMECARD	0x1008
33 
34 #define PCI_VENDOR_ID_OROLIA			0x1ad7
35 #define PCI_DEVICE_ID_OROLIA_ARTCARD		0xa000
36 
37 static struct class timecard_class = {
38 	.name		= "timecard",
39 };
40 
41 struct ocp_reg {
42 	u32	ctrl;
43 	u32	status;
44 	u32	select;
45 	u32	version;
46 	u32	time_ns;
47 	u32	time_sec;
48 	u32	__pad0[2];
49 	u32	adjust_ns;
50 	u32	adjust_sec;
51 	u32	__pad1[2];
52 	u32	offset_ns;
53 	u32	offset_window_ns;
54 	u32	__pad2[2];
55 	u32	drift_ns;
56 	u32	drift_window_ns;
57 	u32	__pad3[6];
58 	u32	servo_offset_p;
59 	u32	servo_offset_i;
60 	u32	servo_drift_p;
61 	u32	servo_drift_i;
62 	u32	status_offset;
63 	u32	status_drift;
64 };
65 
66 #define OCP_CTRL_ENABLE		BIT(0)
67 #define OCP_CTRL_ADJUST_TIME	BIT(1)
68 #define OCP_CTRL_ADJUST_OFFSET	BIT(2)
69 #define OCP_CTRL_ADJUST_DRIFT	BIT(3)
70 #define OCP_CTRL_ADJUST_SERVO	BIT(8)
71 #define OCP_CTRL_READ_TIME_REQ	BIT(30)
72 #define OCP_CTRL_READ_TIME_DONE	BIT(31)
73 
74 #define OCP_STATUS_IN_SYNC	BIT(0)
75 #define OCP_STATUS_IN_HOLDOVER	BIT(1)
76 
77 #define OCP_SELECT_CLK_NONE	0
78 #define OCP_SELECT_CLK_REG	0xfe
79 
80 struct tod_reg {
81 	u32	ctrl;
82 	u32	status;
83 	u32	uart_polarity;
84 	u32	version;
85 	u32	adj_sec;
86 	u32	__pad0[3];
87 	u32	uart_baud;
88 	u32	__pad1[3];
89 	u32	utc_status;
90 	u32	leap;
91 };
92 
93 #define TOD_CTRL_PROTOCOL	BIT(28)
94 #define TOD_CTRL_DISABLE_FMT_A	BIT(17)
95 #define TOD_CTRL_DISABLE_FMT_B	BIT(16)
96 #define TOD_CTRL_ENABLE		BIT(0)
97 #define TOD_CTRL_GNSS_MASK	GENMASK(3, 0)
98 #define TOD_CTRL_GNSS_SHIFT	24
99 
100 #define TOD_STATUS_UTC_MASK		GENMASK(7, 0)
101 #define TOD_STATUS_UTC_VALID		BIT(8)
102 #define TOD_STATUS_LEAP_ANNOUNCE	BIT(12)
103 #define TOD_STATUS_LEAP_VALID		BIT(16)
104 
105 struct ts_reg {
106 	u32	enable;
107 	u32	error;
108 	u32	polarity;
109 	u32	version;
110 	u32	__pad0[4];
111 	u32	cable_delay;
112 	u32	__pad1[3];
113 	u32	intr;
114 	u32	intr_mask;
115 	u32	event_count;
116 	u32	__pad2[1];
117 	u32	ts_count;
118 	u32	time_ns;
119 	u32	time_sec;
120 	u32	data_width;
121 	u32	data;
122 };
123 
124 struct pps_reg {
125 	u32	ctrl;
126 	u32	status;
127 	u32	__pad0[6];
128 	u32	cable_delay;
129 };
130 
131 #define PPS_STATUS_FILTER_ERR	BIT(0)
132 #define PPS_STATUS_SUPERV_ERR	BIT(1)
133 
134 struct img_reg {
135 	u32	version;
136 };
137 
138 struct gpio_reg {
139 	u32	gpio1;
140 	u32	__pad0;
141 	u32	gpio2;
142 	u32	__pad1;
143 };
144 
145 struct irig_master_reg {
146 	u32	ctrl;
147 	u32	status;
148 	u32	__pad0;
149 	u32	version;
150 	u32	adj_sec;
151 	u32	mode_ctrl;
152 };
153 
154 #define IRIG_M_CTRL_ENABLE	BIT(0)
155 
156 struct irig_slave_reg {
157 	u32	ctrl;
158 	u32	status;
159 	u32	__pad0;
160 	u32	version;
161 	u32	adj_sec;
162 	u32	mode_ctrl;
163 };
164 
165 #define IRIG_S_CTRL_ENABLE	BIT(0)
166 
167 struct dcf_master_reg {
168 	u32	ctrl;
169 	u32	status;
170 	u32	__pad0;
171 	u32	version;
172 	u32	adj_sec;
173 };
174 
175 #define DCF_M_CTRL_ENABLE	BIT(0)
176 
177 struct dcf_slave_reg {
178 	u32	ctrl;
179 	u32	status;
180 	u32	__pad0;
181 	u32	version;
182 	u32	adj_sec;
183 };
184 
185 #define DCF_S_CTRL_ENABLE	BIT(0)
186 
187 struct signal_reg {
188 	u32	enable;
189 	u32	status;
190 	u32	polarity;
191 	u32	version;
192 	u32	__pad0[4];
193 	u32	cable_delay;
194 	u32	__pad1[3];
195 	u32	intr;
196 	u32	intr_mask;
197 	u32	__pad2[2];
198 	u32	start_ns;
199 	u32	start_sec;
200 	u32	pulse_ns;
201 	u32	pulse_sec;
202 	u32	period_ns;
203 	u32	period_sec;
204 	u32	repeat_count;
205 };
206 
207 struct frequency_reg {
208 	u32	ctrl;
209 	u32	status;
210 };
211 
212 struct board_config_reg {
213 	u32 mro50_serial_activate;
214 };
215 
216 #define FREQ_STATUS_VALID	BIT(31)
217 #define FREQ_STATUS_ERROR	BIT(30)
218 #define FREQ_STATUS_OVERRUN	BIT(29)
219 #define FREQ_STATUS_MASK	GENMASK(23, 0)
220 
221 struct ptp_ocp_flash_info {
222 	const char *name;
223 	int pci_offset;
224 	int data_size;
225 	void *data;
226 };
227 
228 struct ptp_ocp_firmware_header {
229 	char magic[4];
230 	__be16 pci_vendor_id;
231 	__be16 pci_device_id;
232 	__be32 image_size;
233 	__be16 hw_revision;
234 	__be16 crc;
235 };
236 
237 #define OCP_FIRMWARE_MAGIC_HEADER "OCPC"
238 
239 struct ptp_ocp_i2c_info {
240 	const char *name;
241 	unsigned long fixed_rate;
242 	size_t data_size;
243 	void *data;
244 };
245 
246 struct ptp_ocp_ext_info {
247 	int index;
248 	irqreturn_t (*irq_fcn)(int irq, void *priv);
249 	int (*enable)(void *priv, u32 req, bool enable);
250 };
251 
252 struct ptp_ocp_ext_src {
253 	void __iomem		*mem;
254 	struct ptp_ocp		*bp;
255 	struct ptp_ocp_ext_info	*info;
256 	int			irq_vec;
257 };
258 
259 enum ptp_ocp_sma_mode {
260 	SMA_MODE_IN,
261 	SMA_MODE_OUT,
262 };
263 
264 static struct dpll_pin_frequency ptp_ocp_sma_freq[] = {
265 	DPLL_PIN_FREQUENCY_1PPS,
266 	DPLL_PIN_FREQUENCY_10MHZ,
267 	DPLL_PIN_FREQUENCY_IRIG_B,
268 	DPLL_PIN_FREQUENCY_DCF77,
269 };
270 
271 struct ptp_ocp_sma_connector {
272 	enum	ptp_ocp_sma_mode mode;
273 	bool	fixed_fcn;
274 	bool	fixed_dir;
275 	bool	disabled;
276 	u8	default_fcn;
277 	struct dpll_pin		   *dpll_pin;
278 	struct dpll_pin_properties dpll_prop;
279 };
280 
281 struct ocp_attr_group {
282 	u64 cap;
283 	const struct attribute_group *group;
284 };
285 
286 #define OCP_CAP_BASIC	BIT(0)
287 #define OCP_CAP_SIGNAL	BIT(1)
288 #define OCP_CAP_FREQ	BIT(2)
289 
290 struct ptp_ocp_signal {
291 	ktime_t		period;
292 	ktime_t		pulse;
293 	ktime_t		phase;
294 	ktime_t		start;
295 	int		duty;
296 	bool		polarity;
297 	bool		running;
298 };
299 
300 struct ptp_ocp_serial_port {
301 	int line;
302 	int baud;
303 };
304 
305 #define OCP_BOARD_ID_LEN		13
306 #define OCP_SERIAL_LEN			6
307 #define OCP_SMA_NUM			4
308 
309 struct ptp_ocp {
310 	struct pci_dev		*pdev;
311 	struct device		dev;
312 	spinlock_t		lock;
313 	struct ocp_reg __iomem	*reg;
314 	struct tod_reg __iomem	*tod;
315 	struct pps_reg __iomem	*pps_to_ext;
316 	struct pps_reg __iomem	*pps_to_clk;
317 	struct board_config_reg __iomem	*board_config;
318 	struct gpio_reg __iomem	*pps_select;
319 	struct gpio_reg __iomem	*sma_map1;
320 	struct gpio_reg __iomem	*sma_map2;
321 	struct irig_master_reg	__iomem *irig_out;
322 	struct irig_slave_reg	__iomem *irig_in;
323 	struct dcf_master_reg	__iomem *dcf_out;
324 	struct dcf_slave_reg	__iomem *dcf_in;
325 	struct tod_reg		__iomem *nmea_out;
326 	struct frequency_reg	__iomem *freq_in[4];
327 	struct ptp_ocp_ext_src	*signal_out[4];
328 	struct ptp_ocp_ext_src	*pps;
329 	struct ptp_ocp_ext_src	*ts0;
330 	struct ptp_ocp_ext_src	*ts1;
331 	struct ptp_ocp_ext_src	*ts2;
332 	struct ptp_ocp_ext_src	*ts3;
333 	struct ptp_ocp_ext_src	*ts4;
334 	struct ocp_art_gpio_reg __iomem *art_sma;
335 	struct img_reg __iomem	*image;
336 	struct ptp_clock	*ptp;
337 	struct ptp_clock_info	ptp_info;
338 	struct platform_device	*i2c_ctrl;
339 	struct platform_device	*spi_flash;
340 	struct clk_hw		*i2c_clk;
341 	struct timer_list	watchdog;
342 	const struct attribute_group **attr_group;
343 	const struct ptp_ocp_eeprom_map *eeprom_map;
344 	struct dentry		*debug_root;
345 	bool			sync;
346 	time64_t		gnss_lost;
347 	struct delayed_work	sync_work;
348 	int			id;
349 	int			n_irqs;
350 	struct ptp_ocp_serial_port	gnss_port;
351 	struct ptp_ocp_serial_port	gnss2_port;
352 	struct ptp_ocp_serial_port	mac_port;   /* miniature atomic clock */
353 	struct ptp_ocp_serial_port	nmea_port;
354 	bool			fw_loader;
355 	u8			fw_tag;
356 	u16			fw_version;
357 	u8			board_id[OCP_BOARD_ID_LEN];
358 	u8			serial[OCP_SERIAL_LEN];
359 	bool			has_eeprom_data;
360 	u32			pps_req_map;
361 	int			flash_start;
362 	u32			utc_tai_offset;
363 	u32			ts_window_adjust;
364 	u64			fw_cap;
365 	struct ptp_ocp_signal	signal[4];
366 	struct ptp_ocp_sma_connector sma[OCP_SMA_NUM];
367 	const struct ocp_sma_op *sma_op;
368 	struct dpll_device *dpll;
369 };
370 
371 #define OCP_REQ_TIMESTAMP	BIT(0)
372 #define OCP_REQ_PPS		BIT(1)
373 
374 struct ocp_resource {
375 	unsigned long offset;
376 	int size;
377 	int irq_vec;
378 	int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
379 	void *extra;
380 	unsigned long bp_offset;
381 	const char * const name;
382 };
383 
384 static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
385 static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
386 static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
387 static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
388 static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
389 static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
390 static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
391 static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv);
392 static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
393 static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
394 				      struct ptp_perout_request *req);
395 static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable);
396 static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr);
397 
398 static int ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
399 
400 static const struct ocp_attr_group fb_timecard_groups[];
401 
402 static const struct ocp_attr_group art_timecard_groups[];
403 
404 struct ptp_ocp_eeprom_map {
405 	u16	off;
406 	u16	len;
407 	u32	bp_offset;
408 	const void * const tag;
409 };
410 
411 #define EEPROM_ENTRY(addr, member)				\
412 	.off = addr,						\
413 	.len = sizeof_field(struct ptp_ocp, member),		\
414 	.bp_offset = offsetof(struct ptp_ocp, member)
415 
416 #define BP_MAP_ENTRY_ADDR(bp, map) ({				\
417 	(void *)((uintptr_t)(bp) + (map)->bp_offset);		\
418 })
419 
420 static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
421 	{ EEPROM_ENTRY(0x43, board_id) },
422 	{ EEPROM_ENTRY(0x00, serial), .tag = "mac" },
423 	{ }
424 };
425 
426 static struct ptp_ocp_eeprom_map art_eeprom_map[] = {
427 	{ EEPROM_ENTRY(0x200 + 0x43, board_id) },
428 	{ EEPROM_ENTRY(0x200 + 0x63, serial) },
429 	{ }
430 };
431 
432 #define bp_assign_entry(bp, res, val) ({				\
433 	uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset;		\
434 	*(typeof(val) *)addr = val;					\
435 })
436 
437 #define OCP_RES_LOCATION(member) \
438 	.name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
439 
440 #define OCP_MEM_RESOURCE(member) \
441 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
442 
443 #define OCP_SERIAL_RESOURCE(member) \
444 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
445 
446 #define OCP_I2C_RESOURCE(member) \
447 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
448 
449 #define OCP_SPI_RESOURCE(member) \
450 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
451 
452 #define OCP_EXT_RESOURCE(member) \
453 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
454 
455 /* This is the MSI vector mapping used.
456  * 0: PPS (TS5)
457  * 1: TS0
458  * 2: TS1
459  * 3: GNSS1
460  * 4: GNSS2
461  * 5: MAC
462  * 6: TS2
463  * 7: I2C controller
464  * 8: HWICAP (notused)
465  * 9: SPI Flash
466  * 10: NMEA
467  * 11: Signal Generator 1
468  * 12: Signal Generator 2
469  * 13: Signal Generator 3
470  * 14: Signal Generator 4
471  * 15: TS3
472  * 16: TS4
473  --
474  * 8: Orolia TS1
475  * 10: Orolia TS2
476  * 11: Orolia TS0 (GNSS)
477  * 12: Orolia PPS
478  * 14: Orolia TS3
479  * 15: Orolia TS4
480  */
481 
482 static struct ocp_resource ocp_fb_resource[] = {
483 	{
484 		OCP_MEM_RESOURCE(reg),
485 		.offset = 0x01000000, .size = 0x10000,
486 	},
487 	{
488 		OCP_EXT_RESOURCE(ts0),
489 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
490 		.extra = &(struct ptp_ocp_ext_info) {
491 			.index = 0,
492 			.irq_fcn = ptp_ocp_ts_irq,
493 			.enable = ptp_ocp_ts_enable,
494 		},
495 	},
496 	{
497 		OCP_EXT_RESOURCE(ts1),
498 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
499 		.extra = &(struct ptp_ocp_ext_info) {
500 			.index = 1,
501 			.irq_fcn = ptp_ocp_ts_irq,
502 			.enable = ptp_ocp_ts_enable,
503 		},
504 	},
505 	{
506 		OCP_EXT_RESOURCE(ts2),
507 		.offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
508 		.extra = &(struct ptp_ocp_ext_info) {
509 			.index = 2,
510 			.irq_fcn = ptp_ocp_ts_irq,
511 			.enable = ptp_ocp_ts_enable,
512 		},
513 	},
514 	{
515 		OCP_EXT_RESOURCE(ts3),
516 		.offset = 0x01110000, .size = 0x10000, .irq_vec = 15,
517 		.extra = &(struct ptp_ocp_ext_info) {
518 			.index = 3,
519 			.irq_fcn = ptp_ocp_ts_irq,
520 			.enable = ptp_ocp_ts_enable,
521 		},
522 	},
523 	{
524 		OCP_EXT_RESOURCE(ts4),
525 		.offset = 0x01120000, .size = 0x10000, .irq_vec = 16,
526 		.extra = &(struct ptp_ocp_ext_info) {
527 			.index = 4,
528 			.irq_fcn = ptp_ocp_ts_irq,
529 			.enable = ptp_ocp_ts_enable,
530 		},
531 	},
532 	/* Timestamp for PHC and/or PPS generator */
533 	{
534 		OCP_EXT_RESOURCE(pps),
535 		.offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
536 		.extra = &(struct ptp_ocp_ext_info) {
537 			.index = 5,
538 			.irq_fcn = ptp_ocp_ts_irq,
539 			.enable = ptp_ocp_ts_enable,
540 		},
541 	},
542 	{
543 		OCP_EXT_RESOURCE(signal_out[0]),
544 		.offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
545 		.extra = &(struct ptp_ocp_ext_info) {
546 			.index = 1,
547 			.irq_fcn = ptp_ocp_signal_irq,
548 			.enable = ptp_ocp_signal_enable,
549 		},
550 	},
551 	{
552 		OCP_EXT_RESOURCE(signal_out[1]),
553 		.offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
554 		.extra = &(struct ptp_ocp_ext_info) {
555 			.index = 2,
556 			.irq_fcn = ptp_ocp_signal_irq,
557 			.enable = ptp_ocp_signal_enable,
558 		},
559 	},
560 	{
561 		OCP_EXT_RESOURCE(signal_out[2]),
562 		.offset = 0x010F0000, .size = 0x10000, .irq_vec = 13,
563 		.extra = &(struct ptp_ocp_ext_info) {
564 			.index = 3,
565 			.irq_fcn = ptp_ocp_signal_irq,
566 			.enable = ptp_ocp_signal_enable,
567 		},
568 	},
569 	{
570 		OCP_EXT_RESOURCE(signal_out[3]),
571 		.offset = 0x01100000, .size = 0x10000, .irq_vec = 14,
572 		.extra = &(struct ptp_ocp_ext_info) {
573 			.index = 4,
574 			.irq_fcn = ptp_ocp_signal_irq,
575 			.enable = ptp_ocp_signal_enable,
576 		},
577 	},
578 	{
579 		OCP_MEM_RESOURCE(pps_to_ext),
580 		.offset = 0x01030000, .size = 0x10000,
581 	},
582 	{
583 		OCP_MEM_RESOURCE(pps_to_clk),
584 		.offset = 0x01040000, .size = 0x10000,
585 	},
586 	{
587 		OCP_MEM_RESOURCE(tod),
588 		.offset = 0x01050000, .size = 0x10000,
589 	},
590 	{
591 		OCP_MEM_RESOURCE(irig_in),
592 		.offset = 0x01070000, .size = 0x10000,
593 	},
594 	{
595 		OCP_MEM_RESOURCE(irig_out),
596 		.offset = 0x01080000, .size = 0x10000,
597 	},
598 	{
599 		OCP_MEM_RESOURCE(dcf_in),
600 		.offset = 0x01090000, .size = 0x10000,
601 	},
602 	{
603 		OCP_MEM_RESOURCE(dcf_out),
604 		.offset = 0x010A0000, .size = 0x10000,
605 	},
606 	{
607 		OCP_MEM_RESOURCE(nmea_out),
608 		.offset = 0x010B0000, .size = 0x10000,
609 	},
610 	{
611 		OCP_MEM_RESOURCE(image),
612 		.offset = 0x00020000, .size = 0x1000,
613 	},
614 	{
615 		OCP_MEM_RESOURCE(pps_select),
616 		.offset = 0x00130000, .size = 0x1000,
617 	},
618 	{
619 		OCP_MEM_RESOURCE(sma_map1),
620 		.offset = 0x00140000, .size = 0x1000,
621 	},
622 	{
623 		OCP_MEM_RESOURCE(sma_map2),
624 		.offset = 0x00220000, .size = 0x1000,
625 	},
626 	{
627 		OCP_I2C_RESOURCE(i2c_ctrl),
628 		.offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
629 		.extra = &(struct ptp_ocp_i2c_info) {
630 			.name = "xiic-i2c",
631 			.fixed_rate = 50000000,
632 			.data_size = sizeof(struct xiic_i2c_platform_data),
633 			.data = &(struct xiic_i2c_platform_data) {
634 				.num_devices = 2,
635 				.devices = (struct i2c_board_info[]) {
636 					{ I2C_BOARD_INFO("24c02", 0x50) },
637 					{ I2C_BOARD_INFO("24mac402", 0x58),
638 					  .platform_data = "mac" },
639 				},
640 			},
641 		},
642 	},
643 	{
644 		OCP_SERIAL_RESOURCE(gnss_port),
645 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
646 		.extra = &(struct ptp_ocp_serial_port) {
647 			.baud = 115200,
648 		},
649 	},
650 	{
651 		OCP_SERIAL_RESOURCE(gnss2_port),
652 		.offset = 0x00170000 + 0x1000, .irq_vec = 4,
653 		.extra = &(struct ptp_ocp_serial_port) {
654 			.baud = 115200,
655 		},
656 	},
657 	{
658 		OCP_SERIAL_RESOURCE(mac_port),
659 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
660 		.extra = &(struct ptp_ocp_serial_port) {
661 			.baud = 57600,
662 		},
663 	},
664 	{
665 		OCP_SERIAL_RESOURCE(nmea_port),
666 		.offset = 0x00190000 + 0x1000, .irq_vec = 10,
667 	},
668 	{
669 		OCP_SPI_RESOURCE(spi_flash),
670 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
671 		.extra = &(struct ptp_ocp_flash_info) {
672 			.name = "xilinx_spi", .pci_offset = 0,
673 			.data_size = sizeof(struct xspi_platform_data),
674 			.data = &(struct xspi_platform_data) {
675 				.num_chipselect = 1,
676 				.bits_per_word = 8,
677 				.num_devices = 1,
678 				.force_irq = true,
679 				.devices = &(struct spi_board_info) {
680 					.modalias = "spi-nor",
681 				},
682 			},
683 		},
684 	},
685 	{
686 		OCP_MEM_RESOURCE(freq_in[0]),
687 		.offset = 0x01200000, .size = 0x10000,
688 	},
689 	{
690 		OCP_MEM_RESOURCE(freq_in[1]),
691 		.offset = 0x01210000, .size = 0x10000,
692 	},
693 	{
694 		OCP_MEM_RESOURCE(freq_in[2]),
695 		.offset = 0x01220000, .size = 0x10000,
696 	},
697 	{
698 		OCP_MEM_RESOURCE(freq_in[3]),
699 		.offset = 0x01230000, .size = 0x10000,
700 	},
701 	{
702 		.setup = ptp_ocp_fb_board_init,
703 	},
704 	{ }
705 };
706 
707 #define OCP_ART_CONFIG_SIZE		144
708 #define OCP_ART_TEMP_TABLE_SIZE		368
709 
710 struct ocp_art_gpio_reg {
711 	struct {
712 		u32	gpio;
713 		u32	__pad[3];
714 	} map[4];
715 };
716 
717 static struct ocp_resource ocp_art_resource[] = {
718 	{
719 		OCP_MEM_RESOURCE(reg),
720 		.offset = 0x01000000, .size = 0x10000,
721 	},
722 	{
723 		OCP_SERIAL_RESOURCE(gnss_port),
724 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
725 		.extra = &(struct ptp_ocp_serial_port) {
726 			.baud = 115200,
727 		},
728 	},
729 	{
730 		OCP_MEM_RESOURCE(art_sma),
731 		.offset = 0x003C0000, .size = 0x1000,
732 	},
733 	/* Timestamp associated with GNSS1 receiver PPS */
734 	{
735 		OCP_EXT_RESOURCE(ts0),
736 		.offset = 0x360000, .size = 0x20, .irq_vec = 12,
737 		.extra = &(struct ptp_ocp_ext_info) {
738 			.index = 0,
739 			.irq_fcn = ptp_ocp_ts_irq,
740 			.enable = ptp_ocp_ts_enable,
741 		},
742 	},
743 	{
744 		OCP_EXT_RESOURCE(ts1),
745 		.offset = 0x380000, .size = 0x20, .irq_vec = 8,
746 		.extra = &(struct ptp_ocp_ext_info) {
747 			.index = 1,
748 			.irq_fcn = ptp_ocp_ts_irq,
749 			.enable = ptp_ocp_ts_enable,
750 		},
751 	},
752 	{
753 		OCP_EXT_RESOURCE(ts2),
754 		.offset = 0x390000, .size = 0x20, .irq_vec = 10,
755 		.extra = &(struct ptp_ocp_ext_info) {
756 			.index = 2,
757 			.irq_fcn = ptp_ocp_ts_irq,
758 			.enable = ptp_ocp_ts_enable,
759 		},
760 	},
761 	{
762 		OCP_EXT_RESOURCE(ts3),
763 		.offset = 0x3A0000, .size = 0x20, .irq_vec = 14,
764 		.extra = &(struct ptp_ocp_ext_info) {
765 			.index = 3,
766 			.irq_fcn = ptp_ocp_ts_irq,
767 			.enable = ptp_ocp_ts_enable,
768 		},
769 	},
770 	{
771 		OCP_EXT_RESOURCE(ts4),
772 		.offset = 0x3B0000, .size = 0x20, .irq_vec = 15,
773 		.extra = &(struct ptp_ocp_ext_info) {
774 			.index = 4,
775 			.irq_fcn = ptp_ocp_ts_irq,
776 			.enable = ptp_ocp_ts_enable,
777 		},
778 	},
779 	/* Timestamp associated with Internal PPS of the card */
780 	{
781 		OCP_EXT_RESOURCE(pps),
782 		.offset = 0x00330000, .size = 0x20, .irq_vec = 11,
783 		.extra = &(struct ptp_ocp_ext_info) {
784 			.index = 5,
785 			.irq_fcn = ptp_ocp_ts_irq,
786 			.enable = ptp_ocp_ts_enable,
787 		},
788 	},
789 	{
790 		OCP_SPI_RESOURCE(spi_flash),
791 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
792 		.extra = &(struct ptp_ocp_flash_info) {
793 			.name = "spi_altera", .pci_offset = 0,
794 			.data_size = sizeof(struct altera_spi_platform_data),
795 			.data = &(struct altera_spi_platform_data) {
796 				.num_chipselect = 1,
797 				.num_devices = 1,
798 				.devices = &(struct spi_board_info) {
799 					.modalias = "spi-nor",
800 				},
801 			},
802 		},
803 	},
804 	{
805 		OCP_I2C_RESOURCE(i2c_ctrl),
806 		.offset = 0x350000, .size = 0x100, .irq_vec = 4,
807 		.extra = &(struct ptp_ocp_i2c_info) {
808 			.name = "ocores-i2c",
809 			.fixed_rate = 400000,
810 			.data_size = sizeof(struct ocores_i2c_platform_data),
811 			.data = &(struct ocores_i2c_platform_data) {
812 				.clock_khz = 125000,
813 				.bus_khz = 400,
814 				.num_devices = 1,
815 				.devices = &(struct i2c_board_info) {
816 					I2C_BOARD_INFO("24c08", 0x50),
817 				},
818 			},
819 		},
820 	},
821 	{
822 		OCP_SERIAL_RESOURCE(mac_port),
823 		.offset = 0x00190000, .irq_vec = 7,
824 		.extra = &(struct ptp_ocp_serial_port) {
825 			.baud = 9600,
826 		},
827 	},
828 	{
829 		OCP_MEM_RESOURCE(board_config),
830 		.offset = 0x210000, .size = 0x1000,
831 	},
832 	{
833 		.setup = ptp_ocp_art_board_init,
834 	},
835 	{ }
836 };
837 
838 static const struct pci_device_id ptp_ocp_pcidev_id[] = {
839 	{ PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
840 	{ PCI_DEVICE_DATA(CELESTICA, TIMECARD, &ocp_fb_resource) },
841 	{ PCI_DEVICE_DATA(OROLIA, ARTCARD, &ocp_art_resource) },
842 	{ }
843 };
844 MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
845 
846 static DEFINE_MUTEX(ptp_ocp_lock);
847 static DEFINE_IDR(ptp_ocp_idr);
848 
849 struct ocp_selector {
850 	const char *name;
851 	int value;
852 	u64 frequency;
853 };
854 
855 static const struct ocp_selector ptp_ocp_clock[] = {
856 	{ .name = "NONE",	.value = 0 },
857 	{ .name = "TOD",	.value = 1 },
858 	{ .name = "IRIG",	.value = 2 },
859 	{ .name = "PPS",	.value = 3 },
860 	{ .name = "PTP",	.value = 4 },
861 	{ .name = "RTC",	.value = 5 },
862 	{ .name = "DCF",	.value = 6 },
863 	{ .name = "REGS",	.value = 0xfe },
864 	{ .name = "EXT",	.value = 0xff },
865 	{ }
866 };
867 
868 #define SMA_DISABLE		BIT(16)
869 #define SMA_ENABLE		BIT(15)
870 #define SMA_SELECT_MASK		GENMASK(14, 0)
871 
872 static const struct ocp_selector ptp_ocp_sma_in[] = {
873 	{ .name = "10Mhz",  .value = 0x0000,      .frequency = 10000000 },
874 	{ .name = "PPS1",   .value = 0x0001,      .frequency = 1 },
875 	{ .name = "PPS2",   .value = 0x0002,      .frequency = 1 },
876 	{ .name = "TS1",    .value = 0x0004,      .frequency = 0 },
877 	{ .name = "TS2",    .value = 0x0008,      .frequency = 0 },
878 	{ .name = "IRIG",   .value = 0x0010,      .frequency = 10000 },
879 	{ .name = "DCF",    .value = 0x0020,      .frequency = 77500 },
880 	{ .name = "TS3",    .value = 0x0040,      .frequency = 0 },
881 	{ .name = "TS4",    .value = 0x0080,      .frequency = 0 },
882 	{ .name = "FREQ1",  .value = 0x0100,      .frequency = 0 },
883 	{ .name = "FREQ2",  .value = 0x0200,      .frequency = 0 },
884 	{ .name = "FREQ3",  .value = 0x0400,      .frequency = 0 },
885 	{ .name = "FREQ4",  .value = 0x0800,      .frequency = 0 },
886 	{ .name = "None",   .value = SMA_DISABLE, .frequency = 0 },
887 	{ }
888 };
889 
890 static const struct ocp_selector ptp_ocp_sma_out[] = {
891 	{ .name = "10Mhz",	.value = 0x0000,  .frequency = 10000000 },
892 	{ .name = "PHC",	.value = 0x0001,  .frequency = 1 },
893 	{ .name = "MAC",	.value = 0x0002,  .frequency = 1 },
894 	{ .name = "GNSS1",	.value = 0x0004,  .frequency = 1 },
895 	{ .name = "GNSS2",	.value = 0x0008,  .frequency = 1 },
896 	{ .name = "IRIG",	.value = 0x0010,  .frequency = 10000 },
897 	{ .name = "DCF",	.value = 0x0020,  .frequency = 77000 },
898 	{ .name = "GEN1",	.value = 0x0040 },
899 	{ .name = "GEN2",	.value = 0x0080 },
900 	{ .name = "GEN3",	.value = 0x0100 },
901 	{ .name = "GEN4",	.value = 0x0200 },
902 	{ .name = "GND",	.value = 0x2000 },
903 	{ .name = "VCC",	.value = 0x4000 },
904 	{ }
905 };
906 
907 static const struct ocp_selector ptp_ocp_art_sma_in[] = {
908 	{ .name = "PPS1",	.value = 0x0001,  .frequency = 1 },
909 	{ .name = "10Mhz",	.value = 0x0008,  .frequency = 1000000 },
910 	{ }
911 };
912 
913 static const struct ocp_selector ptp_ocp_art_sma_out[] = {
914 	{ .name = "PHC",	.value = 0x0002,  .frequency = 1 },
915 	{ .name = "GNSS",	.value = 0x0004,  .frequency = 1 },
916 	{ .name = "10Mhz",	.value = 0x0010,  .frequency = 10000000 },
917 	{ }
918 };
919 
920 struct ocp_sma_op {
921 	const struct ocp_selector *tbl[2];
922 	void (*init)(struct ptp_ocp *bp);
923 	u32 (*get)(struct ptp_ocp *bp, int sma_nr);
924 	int (*set_inputs)(struct ptp_ocp *bp, int sma_nr, u32 val);
925 	int (*set_output)(struct ptp_ocp *bp, int sma_nr, u32 val);
926 };
927 
928 static void
929 ptp_ocp_sma_init(struct ptp_ocp *bp)
930 {
931 	return bp->sma_op->init(bp);
932 }
933 
934 static u32
935 ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr)
936 {
937 	return bp->sma_op->get(bp, sma_nr);
938 }
939 
940 static int
941 ptp_ocp_sma_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
942 {
943 	return bp->sma_op->set_inputs(bp, sma_nr, val);
944 }
945 
946 static int
947 ptp_ocp_sma_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
948 {
949 	return bp->sma_op->set_output(bp, sma_nr, val);
950 }
951 
952 static const char *
953 ptp_ocp_select_name_from_val(const struct ocp_selector *tbl, int val)
954 {
955 	int i;
956 
957 	for (i = 0; tbl[i].name; i++)
958 		if (tbl[i].value == val)
959 			return tbl[i].name;
960 	return NULL;
961 }
962 
963 static int
964 ptp_ocp_select_val_from_name(const struct ocp_selector *tbl, const char *name)
965 {
966 	const char *select;
967 	int i;
968 
969 	for (i = 0; tbl[i].name; i++) {
970 		select = tbl[i].name;
971 		if (!strncasecmp(name, select, strlen(select)))
972 			return tbl[i].value;
973 	}
974 	return -EINVAL;
975 }
976 
977 static ssize_t
978 ptp_ocp_select_table_show(const struct ocp_selector *tbl, char *buf)
979 {
980 	ssize_t count;
981 	int i;
982 
983 	count = 0;
984 	for (i = 0; tbl[i].name; i++)
985 		count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
986 	if (count)
987 		count--;
988 	count += sysfs_emit_at(buf, count, "\n");
989 	return count;
990 }
991 
992 static int
993 __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
994 			 struct ptp_system_timestamp *sts)
995 {
996 	u32 ctrl, time_sec, time_ns;
997 	int i;
998 
999 	ptp_read_system_prets(sts);
1000 
1001 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
1002 	iowrite32(ctrl, &bp->reg->ctrl);
1003 
1004 	for (i = 0; i < 100; i++) {
1005 		ctrl = ioread32(&bp->reg->ctrl);
1006 		if (ctrl & OCP_CTRL_READ_TIME_DONE)
1007 			break;
1008 	}
1009 	ptp_read_system_postts(sts);
1010 
1011 	if (sts && bp->ts_window_adjust) {
1012 		s64 ns = timespec64_to_ns(&sts->post_ts);
1013 
1014 		sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
1015 	}
1016 
1017 	time_ns = ioread32(&bp->reg->time_ns);
1018 	time_sec = ioread32(&bp->reg->time_sec);
1019 
1020 	ts->tv_sec = time_sec;
1021 	ts->tv_nsec = time_ns;
1022 
1023 	return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
1024 }
1025 
1026 static int
1027 ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
1028 		 struct ptp_system_timestamp *sts)
1029 {
1030 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1031 	unsigned long flags;
1032 	int err;
1033 
1034 	spin_lock_irqsave(&bp->lock, flags);
1035 	err = __ptp_ocp_gettime_locked(bp, ts, sts);
1036 	spin_unlock_irqrestore(&bp->lock, flags);
1037 
1038 	return err;
1039 }
1040 
1041 static void
1042 __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
1043 {
1044 	u32 ctrl, time_sec, time_ns;
1045 	u32 select;
1046 
1047 	time_ns = ts->tv_nsec;
1048 	time_sec = ts->tv_sec;
1049 
1050 	select = ioread32(&bp->reg->select);
1051 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1052 
1053 	iowrite32(time_ns, &bp->reg->adjust_ns);
1054 	iowrite32(time_sec, &bp->reg->adjust_sec);
1055 
1056 	ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
1057 	iowrite32(ctrl, &bp->reg->ctrl);
1058 
1059 	/* restore clock selection */
1060 	iowrite32(select >> 16, &bp->reg->select);
1061 }
1062 
1063 static int
1064 ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
1065 {
1066 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1067 	unsigned long flags;
1068 
1069 	spin_lock_irqsave(&bp->lock, flags);
1070 	__ptp_ocp_settime_locked(bp, ts);
1071 	spin_unlock_irqrestore(&bp->lock, flags);
1072 
1073 	return 0;
1074 }
1075 
1076 static void
1077 __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
1078 {
1079 	u32 select, ctrl;
1080 
1081 	select = ioread32(&bp->reg->select);
1082 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1083 
1084 	iowrite32(adj_val, &bp->reg->offset_ns);
1085 	iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
1086 
1087 	ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
1088 	iowrite32(ctrl, &bp->reg->ctrl);
1089 
1090 	/* restore clock selection */
1091 	iowrite32(select >> 16, &bp->reg->select);
1092 }
1093 
1094 static void
1095 ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns)
1096 {
1097 	struct timespec64 ts;
1098 	unsigned long flags;
1099 	int err;
1100 
1101 	spin_lock_irqsave(&bp->lock, flags);
1102 	err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
1103 	if (likely(!err)) {
1104 		set_normalized_timespec64(&ts, ts.tv_sec,
1105 					  ts.tv_nsec + delta_ns);
1106 		__ptp_ocp_settime_locked(bp, &ts);
1107 	}
1108 	spin_unlock_irqrestore(&bp->lock, flags);
1109 }
1110 
1111 static int
1112 ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
1113 {
1114 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1115 	unsigned long flags;
1116 	u32 adj_ns, sign;
1117 
1118 	if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
1119 		ptp_ocp_adjtime_coarse(bp, delta_ns);
1120 		return 0;
1121 	}
1122 
1123 	sign = delta_ns < 0 ? BIT(31) : 0;
1124 	adj_ns = sign ? -delta_ns : delta_ns;
1125 
1126 	spin_lock_irqsave(&bp->lock, flags);
1127 	__ptp_ocp_adjtime_locked(bp, sign | adj_ns);
1128 	spin_unlock_irqrestore(&bp->lock, flags);
1129 
1130 	return 0;
1131 }
1132 
1133 static int
1134 ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
1135 {
1136 	if (scaled_ppm == 0)
1137 		return 0;
1138 
1139 	return -EOPNOTSUPP;
1140 }
1141 
1142 static s32
1143 ptp_ocp_null_getmaxphase(struct ptp_clock_info *ptp_info)
1144 {
1145 	return 0;
1146 }
1147 
1148 static int
1149 ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
1150 {
1151 	return -EOPNOTSUPP;
1152 }
1153 
1154 static int
1155 ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
1156 	       int on)
1157 {
1158 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1159 	struct ptp_ocp_ext_src *ext = NULL;
1160 	u32 req;
1161 	int err;
1162 
1163 	switch (rq->type) {
1164 	case PTP_CLK_REQ_EXTTS:
1165 		req = OCP_REQ_TIMESTAMP;
1166 		switch (rq->extts.index) {
1167 		case 0:
1168 			ext = bp->ts0;
1169 			break;
1170 		case 1:
1171 			ext = bp->ts1;
1172 			break;
1173 		case 2:
1174 			ext = bp->ts2;
1175 			break;
1176 		case 3:
1177 			ext = bp->ts3;
1178 			break;
1179 		case 4:
1180 			ext = bp->ts4;
1181 			break;
1182 		case 5:
1183 			ext = bp->pps;
1184 			break;
1185 		}
1186 		break;
1187 	case PTP_CLK_REQ_PPS:
1188 		req = OCP_REQ_PPS;
1189 		ext = bp->pps;
1190 		break;
1191 	case PTP_CLK_REQ_PEROUT:
1192 		switch (rq->perout.index) {
1193 		case 0:
1194 			/* This is a request for 1PPS on an output SMA.
1195 			 * Allow, but assume manual configuration.
1196 			 */
1197 			if (on && (rq->perout.period.sec != 1 ||
1198 				   rq->perout.period.nsec != 0))
1199 				return -EINVAL;
1200 			return 0;
1201 		case 1:
1202 		case 2:
1203 		case 3:
1204 		case 4:
1205 			req = rq->perout.index - 1;
1206 			ext = bp->signal_out[req];
1207 			err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
1208 			if (err)
1209 				return err;
1210 			break;
1211 		}
1212 		break;
1213 	default:
1214 		return -EOPNOTSUPP;
1215 	}
1216 
1217 	err = -ENXIO;
1218 	if (ext)
1219 		err = ext->info->enable(ext, req, on);
1220 
1221 	return err;
1222 }
1223 
1224 static int
1225 ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
1226 	       enum ptp_pin_function func, unsigned chan)
1227 {
1228 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1229 	char buf[16];
1230 
1231 	switch (func) {
1232 	case PTP_PF_NONE:
1233 		snprintf(buf, sizeof(buf), "IN: None");
1234 		break;
1235 	case PTP_PF_EXTTS:
1236 		/* Allow timestamps, but require sysfs configuration. */
1237 		return 0;
1238 	case PTP_PF_PEROUT:
1239 		/* channel 0 is 1PPS from PHC.
1240 		 * channels 1..4 are the frequency generators.
1241 		 */
1242 		if (chan)
1243 			snprintf(buf, sizeof(buf), "OUT: GEN%d", chan);
1244 		else
1245 			snprintf(buf, sizeof(buf), "OUT: PHC");
1246 		break;
1247 	default:
1248 		return -EOPNOTSUPP;
1249 	}
1250 
1251 	return ptp_ocp_sma_store(bp, buf, pin + 1);
1252 }
1253 
1254 static const struct ptp_clock_info ptp_ocp_clock_info = {
1255 	.owner		= THIS_MODULE,
1256 	.name		= KBUILD_MODNAME,
1257 	.max_adj	= 100000000,
1258 	.gettimex64	= ptp_ocp_gettimex,
1259 	.settime64	= ptp_ocp_settime,
1260 	.adjtime	= ptp_ocp_adjtime,
1261 	.adjfine	= ptp_ocp_null_adjfine,
1262 	.adjphase	= ptp_ocp_null_adjphase,
1263 	.getmaxphase	= ptp_ocp_null_getmaxphase,
1264 	.enable		= ptp_ocp_enable,
1265 	.verify		= ptp_ocp_verify,
1266 	.pps		= true,
1267 	.n_ext_ts	= 6,
1268 	.n_per_out	= 5,
1269 };
1270 
1271 static void
1272 __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
1273 {
1274 	u32 ctrl, select;
1275 
1276 	select = ioread32(&bp->reg->select);
1277 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1278 
1279 	iowrite32(0, &bp->reg->drift_ns);
1280 
1281 	ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
1282 	iowrite32(ctrl, &bp->reg->ctrl);
1283 
1284 	/* restore clock selection */
1285 	iowrite32(select >> 16, &bp->reg->select);
1286 }
1287 
1288 static void
1289 ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
1290 {
1291 	unsigned long flags;
1292 
1293 	spin_lock_irqsave(&bp->lock, flags);
1294 
1295 	bp->utc_tai_offset = val;
1296 
1297 	if (bp->irig_out)
1298 		iowrite32(val, &bp->irig_out->adj_sec);
1299 	if (bp->dcf_out)
1300 		iowrite32(val, &bp->dcf_out->adj_sec);
1301 	if (bp->nmea_out)
1302 		iowrite32(val, &bp->nmea_out->adj_sec);
1303 
1304 	spin_unlock_irqrestore(&bp->lock, flags);
1305 }
1306 
1307 static void
1308 ptp_ocp_watchdog(struct timer_list *t)
1309 {
1310 	struct ptp_ocp *bp = from_timer(bp, t, watchdog);
1311 	unsigned long flags;
1312 	u32 status, utc_offset;
1313 
1314 	status = ioread32(&bp->pps_to_clk->status);
1315 
1316 	if (status & PPS_STATUS_SUPERV_ERR) {
1317 		iowrite32(status, &bp->pps_to_clk->status);
1318 		if (!bp->gnss_lost) {
1319 			spin_lock_irqsave(&bp->lock, flags);
1320 			__ptp_ocp_clear_drift_locked(bp);
1321 			spin_unlock_irqrestore(&bp->lock, flags);
1322 			bp->gnss_lost = ktime_get_real_seconds();
1323 		}
1324 
1325 	} else if (bp->gnss_lost) {
1326 		bp->gnss_lost = 0;
1327 	}
1328 
1329 	/* if GNSS provides correct data we can rely on
1330 	 * it to get leap second information
1331 	 */
1332 	if (bp->tod) {
1333 		status = ioread32(&bp->tod->utc_status);
1334 		utc_offset = status & TOD_STATUS_UTC_MASK;
1335 		if (status & TOD_STATUS_UTC_VALID &&
1336 		    utc_offset != bp->utc_tai_offset)
1337 			ptp_ocp_utc_distribute(bp, utc_offset);
1338 	}
1339 
1340 	mod_timer(&bp->watchdog, jiffies + HZ);
1341 }
1342 
1343 static void
1344 ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
1345 {
1346 	ktime_t start, end;
1347 	ktime_t delay;
1348 	u32 ctrl;
1349 
1350 	ctrl = ioread32(&bp->reg->ctrl);
1351 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
1352 
1353 	iowrite32(ctrl, &bp->reg->ctrl);
1354 
1355 	start = ktime_get_ns();
1356 
1357 	ctrl = ioread32(&bp->reg->ctrl);
1358 
1359 	end = ktime_get_ns();
1360 
1361 	delay = end - start;
1362 	bp->ts_window_adjust = (delay >> 5) * 3;
1363 }
1364 
1365 static int
1366 ptp_ocp_init_clock(struct ptp_ocp *bp)
1367 {
1368 	struct timespec64 ts;
1369 	u32 ctrl;
1370 
1371 	ctrl = OCP_CTRL_ENABLE;
1372 	iowrite32(ctrl, &bp->reg->ctrl);
1373 
1374 	/* NO DRIFT Correction */
1375 	/* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
1376 	iowrite32(0x2000, &bp->reg->servo_offset_p);
1377 	iowrite32(0x1000, &bp->reg->servo_offset_i);
1378 	iowrite32(0,	  &bp->reg->servo_drift_p);
1379 	iowrite32(0,	  &bp->reg->servo_drift_i);
1380 
1381 	/* latch servo values */
1382 	ctrl |= OCP_CTRL_ADJUST_SERVO;
1383 	iowrite32(ctrl, &bp->reg->ctrl);
1384 
1385 	if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
1386 		dev_err(&bp->pdev->dev, "clock not enabled\n");
1387 		return -ENODEV;
1388 	}
1389 
1390 	ptp_ocp_estimate_pci_timing(bp);
1391 
1392 	bp->sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
1393 	if (!bp->sync) {
1394 		ktime_get_clocktai_ts64(&ts);
1395 		ptp_ocp_settime(&bp->ptp_info, &ts);
1396 	}
1397 
1398 	/* If there is a clock supervisor, then enable the watchdog */
1399 	if (bp->pps_to_clk) {
1400 		timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
1401 		mod_timer(&bp->watchdog, jiffies + HZ);
1402 	}
1403 
1404 	return 0;
1405 }
1406 
1407 static void
1408 ptp_ocp_tod_init(struct ptp_ocp *bp)
1409 {
1410 	u32 ctrl, reg;
1411 
1412 	ctrl = ioread32(&bp->tod->ctrl);
1413 	ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
1414 	ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
1415 	iowrite32(ctrl, &bp->tod->ctrl);
1416 
1417 	reg = ioread32(&bp->tod->utc_status);
1418 	if (reg & TOD_STATUS_UTC_VALID)
1419 		ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
1420 }
1421 
1422 static const char *
1423 ptp_ocp_tod_proto_name(const int idx)
1424 {
1425 	static const char * const proto_name[] = {
1426 		"NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
1427 		"UBX", "UBX_UTC", "UBX_LS", "UBX_none"
1428 	};
1429 	return proto_name[idx];
1430 }
1431 
1432 static const char *
1433 ptp_ocp_tod_gnss_name(int idx)
1434 {
1435 	static const char * const gnss_name[] = {
1436 		"ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
1437 		"Unknown"
1438 	};
1439 	if (idx >= ARRAY_SIZE(gnss_name))
1440 		idx = ARRAY_SIZE(gnss_name) - 1;
1441 	return gnss_name[idx];
1442 }
1443 
1444 struct ptp_ocp_nvmem_match_info {
1445 	struct ptp_ocp *bp;
1446 	const void * const tag;
1447 };
1448 
1449 static int
1450 ptp_ocp_nvmem_match(struct device *dev, const void *data)
1451 {
1452 	const struct ptp_ocp_nvmem_match_info *info = data;
1453 
1454 	dev = dev->parent;
1455 	if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
1456 		return 0;
1457 
1458 	while ((dev = dev->parent))
1459 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
1460 			return info->bp == dev_get_drvdata(dev);
1461 	return 0;
1462 }
1463 
1464 static inline struct nvmem_device *
1465 ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
1466 {
1467 	struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
1468 
1469 	return nvmem_device_find(&info, ptp_ocp_nvmem_match);
1470 }
1471 
1472 static inline void
1473 ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
1474 {
1475 	if (!IS_ERR_OR_NULL(*nvmemp))
1476 		nvmem_device_put(*nvmemp);
1477 	*nvmemp = NULL;
1478 }
1479 
1480 static void
1481 ptp_ocp_read_eeprom(struct ptp_ocp *bp)
1482 {
1483 	const struct ptp_ocp_eeprom_map *map;
1484 	struct nvmem_device *nvmem;
1485 	const void *tag;
1486 	int ret;
1487 
1488 	if (!bp->i2c_ctrl)
1489 		return;
1490 
1491 	tag = NULL;
1492 	nvmem = NULL;
1493 
1494 	for (map = bp->eeprom_map; map->len; map++) {
1495 		if (map->tag != tag) {
1496 			tag = map->tag;
1497 			ptp_ocp_nvmem_device_put(&nvmem);
1498 		}
1499 		if (!nvmem) {
1500 			nvmem = ptp_ocp_nvmem_device_get(bp, tag);
1501 			if (IS_ERR(nvmem)) {
1502 				ret = PTR_ERR(nvmem);
1503 				goto fail;
1504 			}
1505 		}
1506 		ret = nvmem_device_read(nvmem, map->off, map->len,
1507 					BP_MAP_ENTRY_ADDR(bp, map));
1508 		if (ret != map->len)
1509 			goto fail;
1510 	}
1511 
1512 	bp->has_eeprom_data = true;
1513 
1514 out:
1515 	ptp_ocp_nvmem_device_put(&nvmem);
1516 	return;
1517 
1518 fail:
1519 	dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
1520 	goto out;
1521 }
1522 
1523 static struct device *
1524 ptp_ocp_find_flash(struct ptp_ocp *bp)
1525 {
1526 	struct device *dev, *last;
1527 
1528 	last = NULL;
1529 	dev = &bp->spi_flash->dev;
1530 
1531 	while ((dev = device_find_any_child(dev))) {
1532 		if (!strcmp("mtd", dev_bus_name(dev)))
1533 			break;
1534 		put_device(last);
1535 		last = dev;
1536 	}
1537 	put_device(last);
1538 
1539 	return dev;
1540 }
1541 
1542 static int
1543 ptp_ocp_devlink_fw_image(struct devlink *devlink, const struct firmware *fw,
1544 			 const u8 **data, size_t *size)
1545 {
1546 	struct ptp_ocp *bp = devlink_priv(devlink);
1547 	const struct ptp_ocp_firmware_header *hdr;
1548 	size_t offset, length;
1549 	u16 crc;
1550 
1551 	hdr = (const struct ptp_ocp_firmware_header *)fw->data;
1552 	if (memcmp(hdr->magic, OCP_FIRMWARE_MAGIC_HEADER, 4)) {
1553 		devlink_flash_update_status_notify(devlink,
1554 			"No firmware header found, cancel firmware upgrade",
1555 			NULL, 0, 0);
1556 		return -EINVAL;
1557 	}
1558 
1559 	if (be16_to_cpu(hdr->pci_vendor_id) != bp->pdev->vendor ||
1560 	    be16_to_cpu(hdr->pci_device_id) != bp->pdev->device) {
1561 		devlink_flash_update_status_notify(devlink,
1562 			"Firmware image compatibility check failed",
1563 			NULL, 0, 0);
1564 		return -EINVAL;
1565 	}
1566 
1567 	offset = sizeof(*hdr);
1568 	length = be32_to_cpu(hdr->image_size);
1569 	if (length != (fw->size - offset)) {
1570 		devlink_flash_update_status_notify(devlink,
1571 			"Firmware image size check failed",
1572 			NULL, 0, 0);
1573 		return -EINVAL;
1574 	}
1575 
1576 	crc = crc16(0xffff, &fw->data[offset], length);
1577 	if (be16_to_cpu(hdr->crc) != crc) {
1578 		devlink_flash_update_status_notify(devlink,
1579 			"Firmware image CRC check failed",
1580 			NULL, 0, 0);
1581 		return -EINVAL;
1582 	}
1583 
1584 	*data = &fw->data[offset];
1585 	*size = length;
1586 
1587 	return 0;
1588 }
1589 
1590 static int
1591 ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1592 		      const struct firmware *fw)
1593 {
1594 	struct mtd_info *mtd = dev_get_drvdata(dev);
1595 	struct ptp_ocp *bp = devlink_priv(devlink);
1596 	size_t off, len, size, resid, wrote;
1597 	struct erase_info erase;
1598 	size_t base, blksz;
1599 	const u8 *data;
1600 	int err;
1601 
1602 	err = ptp_ocp_devlink_fw_image(devlink, fw, &data, &size);
1603 	if (err)
1604 		goto out;
1605 
1606 	off = 0;
1607 	base = bp->flash_start;
1608 	blksz = 4096;
1609 	resid = size;
1610 
1611 	while (resid) {
1612 		devlink_flash_update_status_notify(devlink, "Flashing",
1613 						   NULL, off, size);
1614 
1615 		len = min_t(size_t, resid, blksz);
1616 		erase.addr = base + off;
1617 		erase.len = blksz;
1618 
1619 		err = mtd_erase(mtd, &erase);
1620 		if (err)
1621 			goto out;
1622 
1623 		err = mtd_write(mtd, base + off, len, &wrote, data + off);
1624 		if (err)
1625 			goto out;
1626 
1627 		off += blksz;
1628 		resid -= len;
1629 	}
1630 out:
1631 	return err;
1632 }
1633 
1634 static int
1635 ptp_ocp_devlink_flash_update(struct devlink *devlink,
1636 			     struct devlink_flash_update_params *params,
1637 			     struct netlink_ext_ack *extack)
1638 {
1639 	struct ptp_ocp *bp = devlink_priv(devlink);
1640 	struct device *dev;
1641 	const char *msg;
1642 	int err;
1643 
1644 	dev = ptp_ocp_find_flash(bp);
1645 	if (!dev) {
1646 		dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1647 		return -ENODEV;
1648 	}
1649 
1650 	devlink_flash_update_status_notify(devlink, "Preparing to flash",
1651 					   NULL, 0, 0);
1652 
1653 	err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1654 
1655 	msg = err ? "Flash error" : "Flash complete";
1656 	devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1657 
1658 	put_device(dev);
1659 	return err;
1660 }
1661 
1662 static int
1663 ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1664 			 struct netlink_ext_ack *extack)
1665 {
1666 	struct ptp_ocp *bp = devlink_priv(devlink);
1667 	const char *fw_image;
1668 	char buf[32];
1669 	int err;
1670 
1671 	fw_image = bp->fw_loader ? "loader" : "fw";
1672 	sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version);
1673 	err = devlink_info_version_running_put(req, fw_image, buf);
1674 	if (err)
1675 		return err;
1676 
1677 	if (!bp->has_eeprom_data) {
1678 		ptp_ocp_read_eeprom(bp);
1679 		if (!bp->has_eeprom_data)
1680 			return 0;
1681 	}
1682 
1683 	sprintf(buf, "%pM", bp->serial);
1684 	err = devlink_info_serial_number_put(req, buf);
1685 	if (err)
1686 		return err;
1687 
1688 	err = devlink_info_version_fixed_put(req,
1689 			DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
1690 			bp->board_id);
1691 	if (err)
1692 		return err;
1693 
1694 	return 0;
1695 }
1696 
1697 static const struct devlink_ops ptp_ocp_devlink_ops = {
1698 	.flash_update = ptp_ocp_devlink_flash_update,
1699 	.info_get = ptp_ocp_devlink_info_get,
1700 };
1701 
1702 static void __iomem *
1703 __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size)
1704 {
1705 	struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1706 
1707 	return devm_ioremap_resource(&bp->pdev->dev, &res);
1708 }
1709 
1710 static void __iomem *
1711 ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1712 {
1713 	resource_size_t start;
1714 
1715 	start = pci_resource_start(bp->pdev, 0) + r->offset;
1716 	return __ptp_ocp_get_mem(bp, start, r->size);
1717 }
1718 
1719 static int
1720 ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1721 {
1722 	struct ptp_ocp_flash_info *info;
1723 	struct pci_dev *pdev = bp->pdev;
1724 	struct platform_device *p;
1725 	struct resource res[2];
1726 	resource_size_t start;
1727 	int id;
1728 
1729 	start = pci_resource_start(pdev, 0) + r->offset;
1730 	res[0] = DEFINE_RES_MEM(start, r->size);
1731 	res[1] = DEFINE_RES_IRQ(pci_irq_vector(pdev, r->irq_vec));
1732 
1733 	info = r->extra;
1734 	id = pci_dev_id(pdev) << 1;
1735 	id += info->pci_offset;
1736 
1737 	p = platform_device_register_resndata(&pdev->dev, info->name, id,
1738 					      res, ARRAY_SIZE(res), info->data,
1739 					      info->data_size);
1740 	if (IS_ERR(p))
1741 		return PTR_ERR(p);
1742 
1743 	bp_assign_entry(bp, r, p);
1744 
1745 	return 0;
1746 }
1747 
1748 static struct platform_device *
1749 ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1750 {
1751 	struct ptp_ocp_i2c_info *info;
1752 	struct resource res[2];
1753 	resource_size_t start;
1754 
1755 	info = r->extra;
1756 	start = pci_resource_start(pdev, 0) + r->offset;
1757 	res[0] = DEFINE_RES_MEM(start, r->size);
1758 	res[1] = DEFINE_RES_IRQ(pci_irq_vector(pdev, r->irq_vec));
1759 
1760 	return platform_device_register_resndata(&pdev->dev, info->name,
1761 						 id, res, ARRAY_SIZE(res),
1762 						 info->data, info->data_size);
1763 }
1764 
1765 static int
1766 ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1767 {
1768 	struct pci_dev *pdev = bp->pdev;
1769 	struct ptp_ocp_i2c_info *info;
1770 	struct platform_device *p;
1771 	struct clk_hw *clk;
1772 	char buf[32];
1773 	int id;
1774 
1775 	info = r->extra;
1776 	id = pci_dev_id(bp->pdev);
1777 
1778 	sprintf(buf, "AXI.%d", id);
1779 	clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
1780 					 info->fixed_rate);
1781 	if (IS_ERR(clk))
1782 		return PTR_ERR(clk);
1783 	bp->i2c_clk = clk;
1784 
1785 	sprintf(buf, "%s.%d", info->name, id);
1786 	devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1787 	p = ptp_ocp_i2c_bus(bp->pdev, r, id);
1788 	if (IS_ERR(p))
1789 		return PTR_ERR(p);
1790 
1791 	bp_assign_entry(bp, r, p);
1792 
1793 	return 0;
1794 }
1795 
1796 /* The expectation is that this is triggered only on error. */
1797 static irqreturn_t
1798 ptp_ocp_signal_irq(int irq, void *priv)
1799 {
1800 	struct ptp_ocp_ext_src *ext = priv;
1801 	struct signal_reg __iomem *reg = ext->mem;
1802 	struct ptp_ocp *bp = ext->bp;
1803 	u32 enable, status;
1804 	int gen;
1805 
1806 	gen = ext->info->index - 1;
1807 
1808 	enable = ioread32(&reg->enable);
1809 	status = ioread32(&reg->status);
1810 
1811 	/* disable generator on error */
1812 	if (status || !enable) {
1813 		iowrite32(0, &reg->intr_mask);
1814 		iowrite32(0, &reg->enable);
1815 		bp->signal[gen].running = false;
1816 	}
1817 
1818 	iowrite32(0, &reg->intr);	/* ack interrupt */
1819 
1820 	return IRQ_HANDLED;
1821 }
1822 
1823 static int
1824 ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
1825 {
1826 	struct ptp_system_timestamp sts;
1827 	struct timespec64 ts;
1828 	ktime_t start_ns;
1829 	int err;
1830 
1831 	if (!s->period)
1832 		return 0;
1833 
1834 	if (!s->pulse)
1835 		s->pulse = ktime_divns(s->period * s->duty, 100);
1836 
1837 	err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
1838 	if (err)
1839 		return err;
1840 
1841 	start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
1842 	if (!s->start) {
1843 		/* roundup() does not work on 32-bit systems */
1844 		s->start = DIV64_U64_ROUND_UP(start_ns, s->period);
1845 		s->start = ktime_add(s->start, s->phase);
1846 	}
1847 
1848 	if (s->duty < 1 || s->duty > 99)
1849 		return -EINVAL;
1850 
1851 	if (s->pulse < 1 || s->pulse > s->period)
1852 		return -EINVAL;
1853 
1854 	if (s->start < start_ns)
1855 		return -EINVAL;
1856 
1857 	bp->signal[gen] = *s;
1858 
1859 	return 0;
1860 }
1861 
1862 static int
1863 ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
1864 			   struct ptp_perout_request *req)
1865 {
1866 	struct ptp_ocp_signal s = { };
1867 
1868 	s.polarity = bp->signal[gen].polarity;
1869 	s.period = ktime_set(req->period.sec, req->period.nsec);
1870 	if (!s.period)
1871 		return 0;
1872 
1873 	if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
1874 		s.pulse = ktime_set(req->on.sec, req->on.nsec);
1875 		s.duty = ktime_divns(s.pulse * 100, s.period);
1876 	}
1877 
1878 	if (req->flags & PTP_PEROUT_PHASE)
1879 		s.phase = ktime_set(req->phase.sec, req->phase.nsec);
1880 	else
1881 		s.start = ktime_set(req->start.sec, req->start.nsec);
1882 
1883 	return ptp_ocp_signal_set(bp, gen, &s);
1884 }
1885 
1886 static int
1887 ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
1888 {
1889 	struct ptp_ocp_ext_src *ext = priv;
1890 	struct signal_reg __iomem *reg = ext->mem;
1891 	struct ptp_ocp *bp = ext->bp;
1892 	struct timespec64 ts;
1893 	int gen;
1894 
1895 	gen = ext->info->index - 1;
1896 
1897 	iowrite32(0, &reg->intr_mask);
1898 	iowrite32(0, &reg->enable);
1899 	bp->signal[gen].running = false;
1900 	if (!enable)
1901 		return 0;
1902 
1903 	ts = ktime_to_timespec64(bp->signal[gen].start);
1904 	iowrite32(ts.tv_sec, &reg->start_sec);
1905 	iowrite32(ts.tv_nsec, &reg->start_ns);
1906 
1907 	ts = ktime_to_timespec64(bp->signal[gen].period);
1908 	iowrite32(ts.tv_sec, &reg->period_sec);
1909 	iowrite32(ts.tv_nsec, &reg->period_ns);
1910 
1911 	ts = ktime_to_timespec64(bp->signal[gen].pulse);
1912 	iowrite32(ts.tv_sec, &reg->pulse_sec);
1913 	iowrite32(ts.tv_nsec, &reg->pulse_ns);
1914 
1915 	iowrite32(bp->signal[gen].polarity, &reg->polarity);
1916 	iowrite32(0, &reg->repeat_count);
1917 
1918 	iowrite32(0, &reg->intr);		/* clear interrupt state */
1919 	iowrite32(1, &reg->intr_mask);		/* enable interrupt */
1920 	iowrite32(3, &reg->enable);		/* valid & enable */
1921 
1922 	bp->signal[gen].running = true;
1923 
1924 	return 0;
1925 }
1926 
1927 static irqreturn_t
1928 ptp_ocp_ts_irq(int irq, void *priv)
1929 {
1930 	struct ptp_ocp_ext_src *ext = priv;
1931 	struct ts_reg __iomem *reg = ext->mem;
1932 	struct ptp_clock_event ev;
1933 	u32 sec, nsec;
1934 
1935 	if (ext == ext->bp->pps) {
1936 		if (ext->bp->pps_req_map & OCP_REQ_PPS) {
1937 			ev.type = PTP_CLOCK_PPS;
1938 			ptp_clock_event(ext->bp->ptp, &ev);
1939 		}
1940 
1941 		if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
1942 			goto out;
1943 	}
1944 
1945 	/* XXX should fix API - this converts s/ns -> ts -> s/ns */
1946 	sec = ioread32(&reg->time_sec);
1947 	nsec = ioread32(&reg->time_ns);
1948 
1949 	ev.type = PTP_CLOCK_EXTTS;
1950 	ev.index = ext->info->index;
1951 	ev.timestamp = sec * NSEC_PER_SEC + nsec;
1952 
1953 	ptp_clock_event(ext->bp->ptp, &ev);
1954 
1955 out:
1956 	iowrite32(1, &reg->intr);	/* write 1 to ack */
1957 
1958 	return IRQ_HANDLED;
1959 }
1960 
1961 static int
1962 ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
1963 {
1964 	struct ptp_ocp_ext_src *ext = priv;
1965 	struct ts_reg __iomem *reg = ext->mem;
1966 	struct ptp_ocp *bp = ext->bp;
1967 
1968 	if (ext == bp->pps) {
1969 		u32 old_map = bp->pps_req_map;
1970 
1971 		if (enable)
1972 			bp->pps_req_map |= req;
1973 		else
1974 			bp->pps_req_map &= ~req;
1975 
1976 		/* if no state change, just return */
1977 		if ((!!old_map ^ !!bp->pps_req_map) == 0)
1978 			return 0;
1979 	}
1980 
1981 	if (enable) {
1982 		iowrite32(1, &reg->enable);
1983 		iowrite32(1, &reg->intr_mask);
1984 		iowrite32(1, &reg->intr);
1985 	} else {
1986 		iowrite32(0, &reg->intr_mask);
1987 		iowrite32(0, &reg->enable);
1988 	}
1989 
1990 	return 0;
1991 }
1992 
1993 static void
1994 ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1995 {
1996 	ext->info->enable(ext, ~0, false);
1997 	pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1998 	kfree(ext);
1999 }
2000 
2001 static int
2002 ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
2003 {
2004 	struct pci_dev *pdev = bp->pdev;
2005 	struct ptp_ocp_ext_src *ext;
2006 	int err;
2007 
2008 	ext = kzalloc(sizeof(*ext), GFP_KERNEL);
2009 	if (!ext)
2010 		return -ENOMEM;
2011 
2012 	ext->mem = ptp_ocp_get_mem(bp, r);
2013 	if (IS_ERR(ext->mem)) {
2014 		err = PTR_ERR(ext->mem);
2015 		goto out;
2016 	}
2017 
2018 	ext->bp = bp;
2019 	ext->info = r->extra;
2020 	ext->irq_vec = r->irq_vec;
2021 
2022 	err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
2023 			      ext, "ocp%d.%s", bp->id, r->name);
2024 	if (err) {
2025 		dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
2026 		goto out;
2027 	}
2028 
2029 	bp_assign_entry(bp, r, ext);
2030 
2031 	return 0;
2032 
2033 out:
2034 	kfree(ext);
2035 	return err;
2036 }
2037 
2038 static int
2039 ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
2040 {
2041 	struct pci_dev *pdev = bp->pdev;
2042 	struct uart_8250_port uart;
2043 
2044 	/* Setting UPF_IOREMAP and leaving port.membase unspecified lets
2045 	 * the serial port device claim and release the pci resource.
2046 	 */
2047 	memset(&uart, 0, sizeof(uart));
2048 	uart.port.dev = &pdev->dev;
2049 	uart.port.iotype = UPIO_MEM;
2050 	uart.port.regshift = 2;
2051 	uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
2052 	uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
2053 	uart.port.uartclk = 50000000;
2054 	uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
2055 	uart.port.type = PORT_16550A;
2056 
2057 	return serial8250_register_8250_port(&uart);
2058 }
2059 
2060 static int
2061 ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
2062 {
2063 	struct ptp_ocp_serial_port *p = (struct ptp_ocp_serial_port *)r->extra;
2064 	struct ptp_ocp_serial_port port = {};
2065 
2066 	port.line = ptp_ocp_serial_line(bp, r);
2067 	if (port.line < 0)
2068 		return port.line;
2069 
2070 	if (p)
2071 		port.baud = p->baud;
2072 
2073 	bp_assign_entry(bp, r, port);
2074 
2075 	return 0;
2076 }
2077 
2078 static int
2079 ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
2080 {
2081 	void __iomem *mem;
2082 
2083 	mem = ptp_ocp_get_mem(bp, r);
2084 	if (IS_ERR(mem))
2085 		return PTR_ERR(mem);
2086 
2087 	bp_assign_entry(bp, r, mem);
2088 
2089 	return 0;
2090 }
2091 
2092 static void
2093 ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
2094 {
2095 	if (!bp->nmea_out)
2096 		return;
2097 
2098 	iowrite32(0, &bp->nmea_out->ctrl);		/* disable */
2099 	iowrite32(7, &bp->nmea_out->uart_baud);		/* 115200 */
2100 	iowrite32(1, &bp->nmea_out->ctrl);		/* enable */
2101 }
2102 
2103 static void
2104 _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
2105 {
2106 	u32 val;
2107 
2108 	iowrite32(0, &reg->enable);		/* disable */
2109 
2110 	val = ioread32(&reg->polarity);
2111 	s->polarity = val ? true : false;
2112 	s->duty = 50;
2113 }
2114 
2115 static void
2116 ptp_ocp_signal_init(struct ptp_ocp *bp)
2117 {
2118 	int i;
2119 
2120 	for (i = 0; i < 4; i++)
2121 		if (bp->signal_out[i])
2122 			_ptp_ocp_signal_init(&bp->signal[i],
2123 					     bp->signal_out[i]->mem);
2124 }
2125 
2126 static void
2127 ptp_ocp_attr_group_del(struct ptp_ocp *bp)
2128 {
2129 	sysfs_remove_groups(&bp->dev.kobj, bp->attr_group);
2130 	kfree(bp->attr_group);
2131 }
2132 
2133 static int
2134 ptp_ocp_attr_group_add(struct ptp_ocp *bp,
2135 		       const struct ocp_attr_group *attr_tbl)
2136 {
2137 	int count, i;
2138 	int err;
2139 
2140 	count = 0;
2141 	for (i = 0; attr_tbl[i].cap; i++)
2142 		if (attr_tbl[i].cap & bp->fw_cap)
2143 			count++;
2144 
2145 	bp->attr_group = kcalloc(count + 1, sizeof(struct attribute_group *),
2146 				 GFP_KERNEL);
2147 	if (!bp->attr_group)
2148 		return -ENOMEM;
2149 
2150 	count = 0;
2151 	for (i = 0; attr_tbl[i].cap; i++)
2152 		if (attr_tbl[i].cap & bp->fw_cap)
2153 			bp->attr_group[count++] = attr_tbl[i].group;
2154 
2155 	err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group);
2156 	if (err)
2157 		bp->attr_group[0] = NULL;
2158 
2159 	return err;
2160 }
2161 
2162 static void
2163 ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
2164 {
2165 	u32 ctrl;
2166 	bool on;
2167 
2168 	ctrl = ioread32(reg);
2169 	on = ctrl & bit;
2170 	if (on ^ enable) {
2171 		ctrl &= ~bit;
2172 		ctrl |= enable ? bit : 0;
2173 		iowrite32(ctrl, reg);
2174 	}
2175 }
2176 
2177 static void
2178 ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
2179 {
2180 	return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
2181 				   IRIG_M_CTRL_ENABLE, enable);
2182 }
2183 
2184 static void
2185 ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
2186 {
2187 	return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
2188 				   IRIG_S_CTRL_ENABLE, enable);
2189 }
2190 
2191 static void
2192 ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
2193 {
2194 	return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
2195 				   DCF_M_CTRL_ENABLE, enable);
2196 }
2197 
2198 static void
2199 ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
2200 {
2201 	return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
2202 				   DCF_S_CTRL_ENABLE, enable);
2203 }
2204 
2205 static void
2206 __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
2207 {
2208 	ptp_ocp_irig_out(bp, val & 0x00100010);
2209 	ptp_ocp_dcf_out(bp, val & 0x00200020);
2210 }
2211 
2212 static void
2213 __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
2214 {
2215 	ptp_ocp_irig_in(bp, val & 0x00100010);
2216 	ptp_ocp_dcf_in(bp, val & 0x00200020);
2217 }
2218 
2219 static u32
2220 ptp_ocp_sma_fb_get(struct ptp_ocp *bp, int sma_nr)
2221 {
2222 	u32 __iomem *gpio;
2223 	u32 shift;
2224 
2225 	if (bp->sma[sma_nr - 1].fixed_fcn)
2226 		return (sma_nr - 1) & 1;
2227 
2228 	if (bp->sma[sma_nr - 1].mode == SMA_MODE_IN)
2229 		gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2230 	else
2231 		gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2232 	shift = sma_nr & 1 ? 0 : 16;
2233 
2234 	return (ioread32(gpio) >> shift) & 0xffff;
2235 }
2236 
2237 static int
2238 ptp_ocp_sma_fb_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2239 {
2240 	u32 reg, mask, shift;
2241 	unsigned long flags;
2242 	u32 __iomem *gpio;
2243 
2244 	gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2245 	shift = sma_nr & 1 ? 0 : 16;
2246 
2247 	mask = 0xffff << (16 - shift);
2248 
2249 	spin_lock_irqsave(&bp->lock, flags);
2250 
2251 	reg = ioread32(gpio);
2252 	reg = (reg & mask) | (val << shift);
2253 
2254 	__handle_signal_outputs(bp, reg);
2255 
2256 	iowrite32(reg, gpio);
2257 
2258 	spin_unlock_irqrestore(&bp->lock, flags);
2259 
2260 	return 0;
2261 }
2262 
2263 static int
2264 ptp_ocp_sma_fb_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2265 {
2266 	u32 reg, mask, shift;
2267 	unsigned long flags;
2268 	u32 __iomem *gpio;
2269 
2270 	gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2271 	shift = sma_nr & 1 ? 0 : 16;
2272 
2273 	mask = 0xffff << (16 - shift);
2274 
2275 	spin_lock_irqsave(&bp->lock, flags);
2276 
2277 	reg = ioread32(gpio);
2278 	reg = (reg & mask) | (val << shift);
2279 
2280 	__handle_signal_inputs(bp, reg);
2281 
2282 	iowrite32(reg, gpio);
2283 
2284 	spin_unlock_irqrestore(&bp->lock, flags);
2285 
2286 	return 0;
2287 }
2288 
2289 static void
2290 ptp_ocp_sma_fb_init(struct ptp_ocp *bp)
2291 {
2292 	struct dpll_pin_properties prop = {
2293 		.board_label = NULL,
2294 		.type = DPLL_PIN_TYPE_EXT,
2295 		.capabilities = DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE,
2296 		.freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq),
2297 		.freq_supported = ptp_ocp_sma_freq,
2298 
2299 	};
2300 	u32 reg;
2301 	int i;
2302 
2303 	/* defaults */
2304 	for (i = 0; i < OCP_SMA_NUM; i++) {
2305 		bp->sma[i].default_fcn = i & 1;
2306 		bp->sma[i].dpll_prop = prop;
2307 		bp->sma[i].dpll_prop.board_label =
2308 			bp->ptp_info.pin_config[i].name;
2309 	}
2310 	bp->sma[0].mode = SMA_MODE_IN;
2311 	bp->sma[1].mode = SMA_MODE_IN;
2312 	bp->sma[2].mode = SMA_MODE_OUT;
2313 	bp->sma[3].mode = SMA_MODE_OUT;
2314 	/* If no SMA1 map, the pin functions and directions are fixed. */
2315 	if (!bp->sma_map1) {
2316 		for (i = 0; i < OCP_SMA_NUM; i++) {
2317 			bp->sma[i].fixed_fcn = true;
2318 			bp->sma[i].fixed_dir = true;
2319 			bp->sma[1].dpll_prop.capabilities &=
2320 				~DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
2321 		}
2322 		return;
2323 	}
2324 
2325 	/* If SMA2 GPIO output map is all 1, it is not present.
2326 	 * This indicates the firmware has fixed direction SMA pins.
2327 	 */
2328 	reg = ioread32(&bp->sma_map2->gpio2);
2329 	if (reg == 0xffffffff) {
2330 		for (i = 0; i < OCP_SMA_NUM; i++)
2331 			bp->sma[i].fixed_dir = true;
2332 	} else {
2333 		reg = ioread32(&bp->sma_map1->gpio1);
2334 		bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
2335 		bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
2336 
2337 		reg = ioread32(&bp->sma_map1->gpio2);
2338 		bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
2339 		bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
2340 	}
2341 }
2342 
2343 static const struct ocp_sma_op ocp_fb_sma_op = {
2344 	.tbl		= { ptp_ocp_sma_in, ptp_ocp_sma_out },
2345 	.init		= ptp_ocp_sma_fb_init,
2346 	.get		= ptp_ocp_sma_fb_get,
2347 	.set_inputs	= ptp_ocp_sma_fb_set_inputs,
2348 	.set_output	= ptp_ocp_sma_fb_set_output,
2349 };
2350 
2351 static int
2352 ptp_ocp_set_pins(struct ptp_ocp *bp)
2353 {
2354 	struct ptp_pin_desc *config;
2355 	int i;
2356 
2357 	config = kcalloc(4, sizeof(*config), GFP_KERNEL);
2358 	if (!config)
2359 		return -ENOMEM;
2360 
2361 	for (i = 0; i < 4; i++) {
2362 		sprintf(config[i].name, "sma%d", i + 1);
2363 		config[i].index = i;
2364 	}
2365 
2366 	bp->ptp_info.n_pins = 4;
2367 	bp->ptp_info.pin_config = config;
2368 
2369 	return 0;
2370 }
2371 
2372 static void
2373 ptp_ocp_fb_set_version(struct ptp_ocp *bp)
2374 {
2375 	u64 cap = OCP_CAP_BASIC;
2376 	u32 version;
2377 
2378 	version = ioread32(&bp->image->version);
2379 
2380 	/* if lower 16 bits are empty, this is the fw loader. */
2381 	if ((version & 0xffff) == 0) {
2382 		version = version >> 16;
2383 		bp->fw_loader = true;
2384 	}
2385 
2386 	bp->fw_tag = version >> 15;
2387 	bp->fw_version = version & 0x7fff;
2388 
2389 	if (bp->fw_tag) {
2390 		/* FPGA firmware */
2391 		if (version >= 5)
2392 			cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ;
2393 	} else {
2394 		/* SOM firmware */
2395 		if (version >= 19)
2396 			cap |= OCP_CAP_SIGNAL;
2397 		if (version >= 20)
2398 			cap |= OCP_CAP_FREQ;
2399 	}
2400 
2401 	bp->fw_cap = cap;
2402 }
2403 
2404 /* FB specific board initializers; last "resource" registered. */
2405 static int
2406 ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2407 {
2408 	int err;
2409 
2410 	bp->flash_start = 1024 * 4096;
2411 	bp->eeprom_map = fb_eeprom_map;
2412 	bp->fw_version = ioread32(&bp->image->version);
2413 	bp->sma_op = &ocp_fb_sma_op;
2414 
2415 	ptp_ocp_fb_set_version(bp);
2416 
2417 	ptp_ocp_tod_init(bp);
2418 	ptp_ocp_nmea_out_init(bp);
2419 	ptp_ocp_signal_init(bp);
2420 
2421 	err = ptp_ocp_attr_group_add(bp, fb_timecard_groups);
2422 	if (err)
2423 		return err;
2424 
2425 	err = ptp_ocp_set_pins(bp);
2426 	if (err)
2427 		return err;
2428 	ptp_ocp_sma_init(bp);
2429 
2430 	return ptp_ocp_init_clock(bp);
2431 }
2432 
2433 static bool
2434 ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
2435 {
2436 	bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
2437 
2438 	if (!allow)
2439 		dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
2440 			r->irq_vec, r->name);
2441 	return allow;
2442 }
2443 
2444 static int
2445 ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
2446 {
2447 	struct ocp_resource *r, *table;
2448 	int err = 0;
2449 
2450 	table = (struct ocp_resource *)driver_data;
2451 	for (r = table; r->setup; r++) {
2452 		if (!ptp_ocp_allow_irq(bp, r))
2453 			continue;
2454 		err = r->setup(bp, r);
2455 		if (err) {
2456 			dev_err(&bp->pdev->dev,
2457 				"Could not register %s: err %d\n",
2458 				r->name, err);
2459 			break;
2460 		}
2461 	}
2462 	return err;
2463 }
2464 
2465 static void
2466 ptp_ocp_art_sma_init(struct ptp_ocp *bp)
2467 {
2468 	struct dpll_pin_properties prop = {
2469 		.board_label = NULL,
2470 		.type = DPLL_PIN_TYPE_EXT,
2471 		.capabilities = 0,
2472 		.freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq),
2473 		.freq_supported = ptp_ocp_sma_freq,
2474 
2475 	};
2476 	u32 reg;
2477 	int i;
2478 
2479 	/* defaults */
2480 	bp->sma[0].mode = SMA_MODE_IN;
2481 	bp->sma[1].mode = SMA_MODE_IN;
2482 	bp->sma[2].mode = SMA_MODE_OUT;
2483 	bp->sma[3].mode = SMA_MODE_OUT;
2484 
2485 	bp->sma[0].default_fcn = 0x08;	/* IN: 10Mhz */
2486 	bp->sma[1].default_fcn = 0x01;	/* IN: PPS1 */
2487 	bp->sma[2].default_fcn = 0x10;	/* OUT: 10Mhz */
2488 	bp->sma[3].default_fcn = 0x02;	/* OUT: PHC */
2489 
2490 	for (i = 0; i < OCP_SMA_NUM; i++) {
2491 		/* If no SMA map, the pin functions and directions are fixed. */
2492 		bp->sma[i].dpll_prop = prop;
2493 		bp->sma[i].dpll_prop.board_label =
2494 			bp->ptp_info.pin_config[i].name;
2495 		if (!bp->art_sma) {
2496 			bp->sma[i].fixed_fcn = true;
2497 			bp->sma[i].fixed_dir = true;
2498 			continue;
2499 		}
2500 		reg = ioread32(&bp->art_sma->map[i].gpio);
2501 
2502 		switch (reg & 0xff) {
2503 		case 0:
2504 			bp->sma[i].fixed_fcn = true;
2505 			bp->sma[i].fixed_dir = true;
2506 			break;
2507 		case 1:
2508 		case 8:
2509 			bp->sma[i].mode = SMA_MODE_IN;
2510 			bp->sma[i].dpll_prop.capabilities =
2511 				DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
2512 			break;
2513 		default:
2514 			bp->sma[i].mode = SMA_MODE_OUT;
2515 			bp->sma[i].dpll_prop.capabilities =
2516 				DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
2517 			break;
2518 		}
2519 	}
2520 }
2521 
2522 static u32
2523 ptp_ocp_art_sma_get(struct ptp_ocp *bp, int sma_nr)
2524 {
2525 	if (bp->sma[sma_nr - 1].fixed_fcn)
2526 		return bp->sma[sma_nr - 1].default_fcn;
2527 
2528 	return ioread32(&bp->art_sma->map[sma_nr - 1].gpio) & 0xff;
2529 }
2530 
2531 /* note: store 0 is considered invalid. */
2532 static int
2533 ptp_ocp_art_sma_set(struct ptp_ocp *bp, int sma_nr, u32 val)
2534 {
2535 	unsigned long flags;
2536 	u32 __iomem *gpio;
2537 	int err = 0;
2538 	u32 reg;
2539 
2540 	val &= SMA_SELECT_MASK;
2541 	if (hweight32(val) > 1)
2542 		return -EINVAL;
2543 
2544 	gpio = &bp->art_sma->map[sma_nr - 1].gpio;
2545 
2546 	spin_lock_irqsave(&bp->lock, flags);
2547 	reg = ioread32(gpio);
2548 	if (((reg >> 16) & val) == 0) {
2549 		err = -EOPNOTSUPP;
2550 	} else {
2551 		reg = (reg & 0xff00) | (val & 0xff);
2552 		iowrite32(reg, gpio);
2553 	}
2554 	spin_unlock_irqrestore(&bp->lock, flags);
2555 
2556 	return err;
2557 }
2558 
2559 static const struct ocp_sma_op ocp_art_sma_op = {
2560 	.tbl		= { ptp_ocp_art_sma_in, ptp_ocp_art_sma_out },
2561 	.init		= ptp_ocp_art_sma_init,
2562 	.get		= ptp_ocp_art_sma_get,
2563 	.set_inputs	= ptp_ocp_art_sma_set,
2564 	.set_output	= ptp_ocp_art_sma_set,
2565 };
2566 
2567 /* ART specific board initializers; last "resource" registered. */
2568 static int
2569 ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2570 {
2571 	int err;
2572 
2573 	bp->flash_start = 0x1000000;
2574 	bp->eeprom_map = art_eeprom_map;
2575 	bp->fw_cap = OCP_CAP_BASIC;
2576 	bp->fw_version = ioread32(&bp->reg->version);
2577 	bp->fw_tag = 2;
2578 	bp->sma_op = &ocp_art_sma_op;
2579 
2580 	/* Enable MAC serial port during initialisation */
2581 	iowrite32(1, &bp->board_config->mro50_serial_activate);
2582 
2583 	err = ptp_ocp_set_pins(bp);
2584 	if (err)
2585 		return err;
2586 	ptp_ocp_sma_init(bp);
2587 
2588 	err = ptp_ocp_attr_group_add(bp, art_timecard_groups);
2589 	if (err)
2590 		return err;
2591 
2592 	return ptp_ocp_init_clock(bp);
2593 }
2594 
2595 static ssize_t
2596 ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf,
2597 		    int def_val)
2598 {
2599 	const char *name;
2600 	ssize_t count;
2601 
2602 	count = sysfs_emit(buf, "OUT: ");
2603 	name = ptp_ocp_select_name_from_val(tbl, val);
2604 	if (!name)
2605 		name = ptp_ocp_select_name_from_val(tbl, def_val);
2606 	count += sysfs_emit_at(buf, count, "%s\n", name);
2607 	return count;
2608 }
2609 
2610 static ssize_t
2611 ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf,
2612 		    int def_val)
2613 {
2614 	const char *name;
2615 	ssize_t count;
2616 	int i;
2617 
2618 	count = sysfs_emit(buf, "IN: ");
2619 	for (i = 0; tbl[i].name; i++) {
2620 		if (val & tbl[i].value) {
2621 			name = tbl[i].name;
2622 			count += sysfs_emit_at(buf, count, "%s ", name);
2623 		}
2624 	}
2625 	if (!val && def_val >= 0) {
2626 		name = ptp_ocp_select_name_from_val(tbl, def_val);
2627 		count += sysfs_emit_at(buf, count, "%s ", name);
2628 	}
2629 	if (count)
2630 		count--;
2631 	count += sysfs_emit_at(buf, count, "\n");
2632 	return count;
2633 }
2634 
2635 static int
2636 sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf,
2637 		 enum ptp_ocp_sma_mode *mode)
2638 {
2639 	int idx, count, dir;
2640 	char **argv;
2641 	int ret;
2642 
2643 	argv = argv_split(GFP_KERNEL, buf, &count);
2644 	if (!argv)
2645 		return -ENOMEM;
2646 
2647 	ret = -EINVAL;
2648 	if (!count)
2649 		goto out;
2650 
2651 	idx = 0;
2652 	dir = *mode == SMA_MODE_IN ? 0 : 1;
2653 	if (!strcasecmp("IN:", argv[0])) {
2654 		dir = 0;
2655 		idx++;
2656 	}
2657 	if (!strcasecmp("OUT:", argv[0])) {
2658 		dir = 1;
2659 		idx++;
2660 	}
2661 	*mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
2662 
2663 	ret = 0;
2664 	for (; idx < count; idx++)
2665 		ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
2666 	if (ret < 0)
2667 		ret = -EINVAL;
2668 
2669 out:
2670 	argv_free(argv);
2671 	return ret;
2672 }
2673 
2674 static ssize_t
2675 ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
2676 		 int default_in_val, int default_out_val)
2677 {
2678 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2679 	const struct ocp_selector * const *tbl;
2680 	u32 val;
2681 
2682 	tbl = bp->sma_op->tbl;
2683 	val = ptp_ocp_sma_get(bp, sma_nr) & SMA_SELECT_MASK;
2684 
2685 	if (sma->mode == SMA_MODE_IN) {
2686 		if (sma->disabled)
2687 			val = SMA_DISABLE;
2688 		return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val);
2689 	}
2690 
2691 	return ptp_ocp_show_output(tbl[1], val, buf, default_out_val);
2692 }
2693 
2694 static ssize_t
2695 sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
2696 {
2697 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2698 
2699 	return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
2700 }
2701 
2702 static ssize_t
2703 sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
2704 {
2705 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2706 
2707 	return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
2708 }
2709 
2710 static ssize_t
2711 sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
2712 {
2713 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2714 
2715 	return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
2716 }
2717 
2718 static ssize_t
2719 sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
2720 {
2721 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2722 
2723 	return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
2724 }
2725 
2726 static int
2727 ptp_ocp_sma_store_val(struct ptp_ocp *bp, int val, enum ptp_ocp_sma_mode mode, int sma_nr)
2728 {
2729 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2730 
2731 	if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
2732 		return -EOPNOTSUPP;
2733 
2734 	if (sma->fixed_fcn) {
2735 		if (val != sma->default_fcn)
2736 			return -EOPNOTSUPP;
2737 		return 0;
2738 	}
2739 
2740 	sma->disabled = !!(val & SMA_DISABLE);
2741 
2742 	if (mode != sma->mode) {
2743 		if (mode == SMA_MODE_IN)
2744 			ptp_ocp_sma_set_output(bp, sma_nr, 0);
2745 		else
2746 			ptp_ocp_sma_set_inputs(bp, sma_nr, 0);
2747 		sma->mode = mode;
2748 	}
2749 
2750 	if (!sma->fixed_dir)
2751 		val |= SMA_ENABLE;		/* add enable bit */
2752 
2753 	if (sma->disabled)
2754 		val = 0;
2755 
2756 	if (mode == SMA_MODE_IN)
2757 		val = ptp_ocp_sma_set_inputs(bp, sma_nr, val);
2758 	else
2759 		val = ptp_ocp_sma_set_output(bp, sma_nr, val);
2760 
2761 	return val;
2762 }
2763 
2764 static int
2765 ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
2766 {
2767 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2768 	enum ptp_ocp_sma_mode mode;
2769 	int val;
2770 
2771 	mode = sma->mode;
2772 	val = sma_parse_inputs(bp->sma_op->tbl, buf, &mode);
2773 	if (val < 0)
2774 		return val;
2775 	return ptp_ocp_sma_store_val(bp, val, mode, sma_nr);
2776 }
2777 
2778 static ssize_t
2779 sma1_store(struct device *dev, struct device_attribute *attr,
2780 	   const char *buf, size_t count)
2781 {
2782 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2783 	int err;
2784 
2785 	err = ptp_ocp_sma_store(bp, buf, 1);
2786 	return err ? err : count;
2787 }
2788 
2789 static ssize_t
2790 sma2_store(struct device *dev, struct device_attribute *attr,
2791 	   const char *buf, size_t count)
2792 {
2793 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2794 	int err;
2795 
2796 	err = ptp_ocp_sma_store(bp, buf, 2);
2797 	return err ? err : count;
2798 }
2799 
2800 static ssize_t
2801 sma3_store(struct device *dev, struct device_attribute *attr,
2802 	   const char *buf, size_t count)
2803 {
2804 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2805 	int err;
2806 
2807 	err = ptp_ocp_sma_store(bp, buf, 3);
2808 	return err ? err : count;
2809 }
2810 
2811 static ssize_t
2812 sma4_store(struct device *dev, struct device_attribute *attr,
2813 	   const char *buf, size_t count)
2814 {
2815 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2816 	int err;
2817 
2818 	err = ptp_ocp_sma_store(bp, buf, 4);
2819 	return err ? err : count;
2820 }
2821 static DEVICE_ATTR_RW(sma1);
2822 static DEVICE_ATTR_RW(sma2);
2823 static DEVICE_ATTR_RW(sma3);
2824 static DEVICE_ATTR_RW(sma4);
2825 
2826 static ssize_t
2827 available_sma_inputs_show(struct device *dev,
2828 			  struct device_attribute *attr, char *buf)
2829 {
2830 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2831 
2832 	return ptp_ocp_select_table_show(bp->sma_op->tbl[0], buf);
2833 }
2834 static DEVICE_ATTR_RO(available_sma_inputs);
2835 
2836 static ssize_t
2837 available_sma_outputs_show(struct device *dev,
2838 			   struct device_attribute *attr, char *buf)
2839 {
2840 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2841 
2842 	return ptp_ocp_select_table_show(bp->sma_op->tbl[1], buf);
2843 }
2844 static DEVICE_ATTR_RO(available_sma_outputs);
2845 
2846 #define EXT_ATTR_RO(_group, _name, _val)				\
2847 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
2848 		{ __ATTR_RO(_name), (void *)_val }
2849 #define EXT_ATTR_RW(_group, _name, _val)				\
2850 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
2851 		{ __ATTR_RW(_name), (void *)_val }
2852 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2853 
2854 /* period [duty [phase [polarity]]] */
2855 static ssize_t
2856 signal_store(struct device *dev, struct device_attribute *attr,
2857 	     const char *buf, size_t count)
2858 {
2859 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2860 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2861 	struct ptp_ocp_signal s = { };
2862 	int gen = (uintptr_t)ea->var;
2863 	int argc, err;
2864 	char **argv;
2865 
2866 	argv = argv_split(GFP_KERNEL, buf, &argc);
2867 	if (!argv)
2868 		return -ENOMEM;
2869 
2870 	err = -EINVAL;
2871 	s.duty = bp->signal[gen].duty;
2872 	s.phase = bp->signal[gen].phase;
2873 	s.period = bp->signal[gen].period;
2874 	s.polarity = bp->signal[gen].polarity;
2875 
2876 	switch (argc) {
2877 	case 4:
2878 		argc--;
2879 		err = kstrtobool(argv[argc], &s.polarity);
2880 		if (err)
2881 			goto out;
2882 		fallthrough;
2883 	case 3:
2884 		argc--;
2885 		err = kstrtou64(argv[argc], 0, &s.phase);
2886 		if (err)
2887 			goto out;
2888 		fallthrough;
2889 	case 2:
2890 		argc--;
2891 		err = kstrtoint(argv[argc], 0, &s.duty);
2892 		if (err)
2893 			goto out;
2894 		fallthrough;
2895 	case 1:
2896 		argc--;
2897 		err = kstrtou64(argv[argc], 0, &s.period);
2898 		if (err)
2899 			goto out;
2900 		break;
2901 	default:
2902 		goto out;
2903 	}
2904 
2905 	err = ptp_ocp_signal_set(bp, gen, &s);
2906 	if (err)
2907 		goto out;
2908 
2909 	err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
2910 
2911 out:
2912 	argv_free(argv);
2913 	return err ? err : count;
2914 }
2915 
2916 static ssize_t
2917 signal_show(struct device *dev, struct device_attribute *attr, char *buf)
2918 {
2919 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2920 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2921 	struct ptp_ocp_signal *signal;
2922 	struct timespec64 ts;
2923 	ssize_t count;
2924 	int i;
2925 
2926 	i = (uintptr_t)ea->var;
2927 	signal = &bp->signal[i];
2928 
2929 	count = sysfs_emit(buf, "%llu %d %llu %d", signal->period,
2930 			   signal->duty, signal->phase, signal->polarity);
2931 
2932 	ts = ktime_to_timespec64(signal->start);
2933 	count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts);
2934 
2935 	return count;
2936 }
2937 static EXT_ATTR_RW(signal, signal, 0);
2938 static EXT_ATTR_RW(signal, signal, 1);
2939 static EXT_ATTR_RW(signal, signal, 2);
2940 static EXT_ATTR_RW(signal, signal, 3);
2941 
2942 static ssize_t
2943 duty_show(struct device *dev, struct device_attribute *attr, char *buf)
2944 {
2945 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2946 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2947 	int i = (uintptr_t)ea->var;
2948 
2949 	return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
2950 }
2951 static EXT_ATTR_RO(signal, duty, 0);
2952 static EXT_ATTR_RO(signal, duty, 1);
2953 static EXT_ATTR_RO(signal, duty, 2);
2954 static EXT_ATTR_RO(signal, duty, 3);
2955 
2956 static ssize_t
2957 period_show(struct device *dev, struct device_attribute *attr, char *buf)
2958 {
2959 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2960 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2961 	int i = (uintptr_t)ea->var;
2962 
2963 	return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
2964 }
2965 static EXT_ATTR_RO(signal, period, 0);
2966 static EXT_ATTR_RO(signal, period, 1);
2967 static EXT_ATTR_RO(signal, period, 2);
2968 static EXT_ATTR_RO(signal, period, 3);
2969 
2970 static ssize_t
2971 phase_show(struct device *dev, struct device_attribute *attr, char *buf)
2972 {
2973 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2974 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2975 	int i = (uintptr_t)ea->var;
2976 
2977 	return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
2978 }
2979 static EXT_ATTR_RO(signal, phase, 0);
2980 static EXT_ATTR_RO(signal, phase, 1);
2981 static EXT_ATTR_RO(signal, phase, 2);
2982 static EXT_ATTR_RO(signal, phase, 3);
2983 
2984 static ssize_t
2985 polarity_show(struct device *dev, struct device_attribute *attr,
2986 	      char *buf)
2987 {
2988 	struct dev_ext_attribute *ea = to_ext_attr(attr);
2989 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2990 	int i = (uintptr_t)ea->var;
2991 
2992 	return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
2993 }
2994 static EXT_ATTR_RO(signal, polarity, 0);
2995 static EXT_ATTR_RO(signal, polarity, 1);
2996 static EXT_ATTR_RO(signal, polarity, 2);
2997 static EXT_ATTR_RO(signal, polarity, 3);
2998 
2999 static ssize_t
3000 running_show(struct device *dev, struct device_attribute *attr, char *buf)
3001 {
3002 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3003 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3004 	int i = (uintptr_t)ea->var;
3005 
3006 	return sysfs_emit(buf, "%d\n", bp->signal[i].running);
3007 }
3008 static EXT_ATTR_RO(signal, running, 0);
3009 static EXT_ATTR_RO(signal, running, 1);
3010 static EXT_ATTR_RO(signal, running, 2);
3011 static EXT_ATTR_RO(signal, running, 3);
3012 
3013 static ssize_t
3014 start_show(struct device *dev, struct device_attribute *attr, char *buf)
3015 {
3016 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3017 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3018 	int i = (uintptr_t)ea->var;
3019 	struct timespec64 ts;
3020 
3021 	ts = ktime_to_timespec64(bp->signal[i].start);
3022 	return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
3023 }
3024 static EXT_ATTR_RO(signal, start, 0);
3025 static EXT_ATTR_RO(signal, start, 1);
3026 static EXT_ATTR_RO(signal, start, 2);
3027 static EXT_ATTR_RO(signal, start, 3);
3028 
3029 static ssize_t
3030 seconds_store(struct device *dev, struct device_attribute *attr,
3031 	      const char *buf, size_t count)
3032 {
3033 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3034 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3035 	int idx = (uintptr_t)ea->var;
3036 	u32 val;
3037 	int err;
3038 
3039 	err = kstrtou32(buf, 0, &val);
3040 	if (err)
3041 		return err;
3042 	if (val > 0xff)
3043 		return -EINVAL;
3044 
3045 	if (val)
3046 		val = (val << 8) | 0x1;
3047 
3048 	iowrite32(val, &bp->freq_in[idx]->ctrl);
3049 
3050 	return count;
3051 }
3052 
3053 static ssize_t
3054 seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
3055 {
3056 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3057 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3058 	int idx = (uintptr_t)ea->var;
3059 	u32 val;
3060 
3061 	val = ioread32(&bp->freq_in[idx]->ctrl);
3062 	if (val & 1)
3063 		val = (val >> 8) & 0xff;
3064 	else
3065 		val = 0;
3066 
3067 	return sysfs_emit(buf, "%u\n", val);
3068 }
3069 static EXT_ATTR_RW(freq, seconds, 0);
3070 static EXT_ATTR_RW(freq, seconds, 1);
3071 static EXT_ATTR_RW(freq, seconds, 2);
3072 static EXT_ATTR_RW(freq, seconds, 3);
3073 
3074 static ssize_t
3075 frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
3076 {
3077 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3078 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3079 	int idx = (uintptr_t)ea->var;
3080 	u32 val;
3081 
3082 	val = ioread32(&bp->freq_in[idx]->status);
3083 	if (val & FREQ_STATUS_ERROR)
3084 		return sysfs_emit(buf, "error\n");
3085 	if (val & FREQ_STATUS_OVERRUN)
3086 		return sysfs_emit(buf, "overrun\n");
3087 	if (val & FREQ_STATUS_VALID)
3088 		return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
3089 	return 0;
3090 }
3091 static EXT_ATTR_RO(freq, frequency, 0);
3092 static EXT_ATTR_RO(freq, frequency, 1);
3093 static EXT_ATTR_RO(freq, frequency, 2);
3094 static EXT_ATTR_RO(freq, frequency, 3);
3095 
3096 static ssize_t
3097 serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
3098 {
3099 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3100 
3101 	if (!bp->has_eeprom_data)
3102 		ptp_ocp_read_eeprom(bp);
3103 
3104 	return sysfs_emit(buf, "%pM\n", bp->serial);
3105 }
3106 static DEVICE_ATTR_RO(serialnum);
3107 
3108 static ssize_t
3109 gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
3110 {
3111 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3112 	ssize_t ret;
3113 
3114 	if (bp->gnss_lost)
3115 		ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
3116 	else
3117 		ret = sysfs_emit(buf, "SYNC\n");
3118 
3119 	return ret;
3120 }
3121 static DEVICE_ATTR_RO(gnss_sync);
3122 
3123 static ssize_t
3124 utc_tai_offset_show(struct device *dev,
3125 		    struct device_attribute *attr, char *buf)
3126 {
3127 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3128 
3129 	return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
3130 }
3131 
3132 static ssize_t
3133 utc_tai_offset_store(struct device *dev,
3134 		     struct device_attribute *attr,
3135 		     const char *buf, size_t count)
3136 {
3137 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3138 	int err;
3139 	u32 val;
3140 
3141 	err = kstrtou32(buf, 0, &val);
3142 	if (err)
3143 		return err;
3144 
3145 	ptp_ocp_utc_distribute(bp, val);
3146 
3147 	return count;
3148 }
3149 static DEVICE_ATTR_RW(utc_tai_offset);
3150 
3151 static ssize_t
3152 ts_window_adjust_show(struct device *dev,
3153 		      struct device_attribute *attr, char *buf)
3154 {
3155 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3156 
3157 	return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
3158 }
3159 
3160 static ssize_t
3161 ts_window_adjust_store(struct device *dev,
3162 		       struct device_attribute *attr,
3163 		       const char *buf, size_t count)
3164 {
3165 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3166 	int err;
3167 	u32 val;
3168 
3169 	err = kstrtou32(buf, 0, &val);
3170 	if (err)
3171 		return err;
3172 
3173 	bp->ts_window_adjust = val;
3174 
3175 	return count;
3176 }
3177 static DEVICE_ATTR_RW(ts_window_adjust);
3178 
3179 static ssize_t
3180 irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
3181 {
3182 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3183 	u32 val;
3184 
3185 	val = ioread32(&bp->irig_out->ctrl);
3186 	val = (val >> 16) & 0x07;
3187 	return sysfs_emit(buf, "%d\n", val);
3188 }
3189 
3190 static ssize_t
3191 irig_b_mode_store(struct device *dev,
3192 		  struct device_attribute *attr,
3193 		  const char *buf, size_t count)
3194 {
3195 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3196 	unsigned long flags;
3197 	int err;
3198 	u32 reg;
3199 	u8 val;
3200 
3201 	err = kstrtou8(buf, 0, &val);
3202 	if (err)
3203 		return err;
3204 	if (val > 7)
3205 		return -EINVAL;
3206 
3207 	reg = ((val & 0x7) << 16);
3208 
3209 	spin_lock_irqsave(&bp->lock, flags);
3210 	iowrite32(0, &bp->irig_out->ctrl);		/* disable */
3211 	iowrite32(reg, &bp->irig_out->ctrl);		/* change mode */
3212 	iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
3213 	spin_unlock_irqrestore(&bp->lock, flags);
3214 
3215 	return count;
3216 }
3217 static DEVICE_ATTR_RW(irig_b_mode);
3218 
3219 static ssize_t
3220 clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
3221 {
3222 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3223 	const char *p;
3224 	u32 select;
3225 
3226 	select = ioread32(&bp->reg->select);
3227 	p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
3228 
3229 	return sysfs_emit(buf, "%s\n", p);
3230 }
3231 
3232 static ssize_t
3233 clock_source_store(struct device *dev, struct device_attribute *attr,
3234 		   const char *buf, size_t count)
3235 {
3236 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3237 	unsigned long flags;
3238 	int val;
3239 
3240 	val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
3241 	if (val < 0)
3242 		return val;
3243 
3244 	spin_lock_irqsave(&bp->lock, flags);
3245 	iowrite32(val, &bp->reg->select);
3246 	spin_unlock_irqrestore(&bp->lock, flags);
3247 
3248 	return count;
3249 }
3250 static DEVICE_ATTR_RW(clock_source);
3251 
3252 static ssize_t
3253 available_clock_sources_show(struct device *dev,
3254 			     struct device_attribute *attr, char *buf)
3255 {
3256 	return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
3257 }
3258 static DEVICE_ATTR_RO(available_clock_sources);
3259 
3260 static ssize_t
3261 clock_status_drift_show(struct device *dev,
3262 			struct device_attribute *attr, char *buf)
3263 {
3264 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3265 	u32 val;
3266 	int res;
3267 
3268 	val = ioread32(&bp->reg->status_drift);
3269 	res = (val & ~INT_MAX) ? -1 : 1;
3270 	res *= (val & INT_MAX);
3271 	return sysfs_emit(buf, "%d\n", res);
3272 }
3273 static DEVICE_ATTR_RO(clock_status_drift);
3274 
3275 static ssize_t
3276 clock_status_offset_show(struct device *dev,
3277 			 struct device_attribute *attr, char *buf)
3278 {
3279 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3280 	u32 val;
3281 	int res;
3282 
3283 	val = ioread32(&bp->reg->status_offset);
3284 	res = (val & ~INT_MAX) ? -1 : 1;
3285 	res *= (val & INT_MAX);
3286 	return sysfs_emit(buf, "%d\n", res);
3287 }
3288 static DEVICE_ATTR_RO(clock_status_offset);
3289 
3290 static ssize_t
3291 tod_correction_show(struct device *dev,
3292 		    struct device_attribute *attr, char *buf)
3293 {
3294 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3295 	u32 val;
3296 	int res;
3297 
3298 	val = ioread32(&bp->tod->adj_sec);
3299 	res = (val & ~INT_MAX) ? -1 : 1;
3300 	res *= (val & INT_MAX);
3301 	return sysfs_emit(buf, "%d\n", res);
3302 }
3303 
3304 static ssize_t
3305 tod_correction_store(struct device *dev, struct device_attribute *attr,
3306 		     const char *buf, size_t count)
3307 {
3308 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3309 	unsigned long flags;
3310 	int err, res;
3311 	u32 val = 0;
3312 
3313 	err = kstrtos32(buf, 0, &res);
3314 	if (err)
3315 		return err;
3316 	if (res < 0) {
3317 		res *= -1;
3318 		val |= BIT(31);
3319 	}
3320 	val |= res;
3321 
3322 	spin_lock_irqsave(&bp->lock, flags);
3323 	iowrite32(val, &bp->tod->adj_sec);
3324 	spin_unlock_irqrestore(&bp->lock, flags);
3325 
3326 	return count;
3327 }
3328 static DEVICE_ATTR_RW(tod_correction);
3329 
3330 #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr)					\
3331 	static struct attribute *fb_timecard_signal##_nr##_attrs[] = {	\
3332 		&dev_attr_signal##_nr##_signal.attr.attr,		\
3333 		&dev_attr_signal##_nr##_duty.attr.attr,			\
3334 		&dev_attr_signal##_nr##_phase.attr.attr,		\
3335 		&dev_attr_signal##_nr##_period.attr.attr,		\
3336 		&dev_attr_signal##_nr##_polarity.attr.attr,		\
3337 		&dev_attr_signal##_nr##_running.attr.attr,		\
3338 		&dev_attr_signal##_nr##_start.attr.attr,		\
3339 		NULL,							\
3340 	}
3341 
3342 #define DEVICE_SIGNAL_GROUP(_name, _nr)					\
3343 	_DEVICE_SIGNAL_GROUP_ATTRS(_nr);				\
3344 	static const struct attribute_group				\
3345 			fb_timecard_signal##_nr##_group = {		\
3346 		.name = #_name,						\
3347 		.attrs = fb_timecard_signal##_nr##_attrs,		\
3348 }
3349 
3350 DEVICE_SIGNAL_GROUP(gen1, 0);
3351 DEVICE_SIGNAL_GROUP(gen2, 1);
3352 DEVICE_SIGNAL_GROUP(gen3, 2);
3353 DEVICE_SIGNAL_GROUP(gen4, 3);
3354 
3355 #define _DEVICE_FREQ_GROUP_ATTRS(_nr)					\
3356 	static struct attribute *fb_timecard_freq##_nr##_attrs[] = {	\
3357 		&dev_attr_freq##_nr##_seconds.attr.attr,		\
3358 		&dev_attr_freq##_nr##_frequency.attr.attr,		\
3359 		NULL,							\
3360 	}
3361 
3362 #define DEVICE_FREQ_GROUP(_name, _nr)					\
3363 	_DEVICE_FREQ_GROUP_ATTRS(_nr);					\
3364 	static const struct attribute_group				\
3365 			fb_timecard_freq##_nr##_group = {		\
3366 		.name = #_name,						\
3367 		.attrs = fb_timecard_freq##_nr##_attrs,			\
3368 }
3369 
3370 DEVICE_FREQ_GROUP(freq1, 0);
3371 DEVICE_FREQ_GROUP(freq2, 1);
3372 DEVICE_FREQ_GROUP(freq3, 2);
3373 DEVICE_FREQ_GROUP(freq4, 3);
3374 
3375 static ssize_t
3376 disciplining_config_read(struct file *filp, struct kobject *kobj,
3377 			 struct bin_attribute *bin_attr, char *buf,
3378 			 loff_t off, size_t count)
3379 {
3380 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3381 	size_t size = OCP_ART_CONFIG_SIZE;
3382 	struct nvmem_device *nvmem;
3383 	ssize_t err;
3384 
3385 	nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3386 	if (IS_ERR(nvmem))
3387 		return PTR_ERR(nvmem);
3388 
3389 	if (off > size) {
3390 		err = 0;
3391 		goto out;
3392 	}
3393 
3394 	if (off + count > size)
3395 		count = size - off;
3396 
3397 	// the configuration is in the very beginning of the EEPROM
3398 	err = nvmem_device_read(nvmem, off, count, buf);
3399 	if (err != count) {
3400 		err = -EFAULT;
3401 		goto out;
3402 	}
3403 
3404 out:
3405 	ptp_ocp_nvmem_device_put(&nvmem);
3406 
3407 	return err;
3408 }
3409 
3410 static ssize_t
3411 disciplining_config_write(struct file *filp, struct kobject *kobj,
3412 			  struct bin_attribute *bin_attr, char *buf,
3413 			  loff_t off, size_t count)
3414 {
3415 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3416 	struct nvmem_device *nvmem;
3417 	ssize_t err;
3418 
3419 	/* Allow write of the whole area only */
3420 	if (off || count != OCP_ART_CONFIG_SIZE)
3421 		return -EFAULT;
3422 
3423 	nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3424 	if (IS_ERR(nvmem))
3425 		return PTR_ERR(nvmem);
3426 
3427 	err = nvmem_device_write(nvmem, 0x00, count, buf);
3428 	if (err != count)
3429 		err = -EFAULT;
3430 
3431 	ptp_ocp_nvmem_device_put(&nvmem);
3432 
3433 	return err;
3434 }
3435 static BIN_ATTR_RW(disciplining_config, OCP_ART_CONFIG_SIZE);
3436 
3437 static ssize_t
3438 temperature_table_read(struct file *filp, struct kobject *kobj,
3439 		       struct bin_attribute *bin_attr, char *buf,
3440 		       loff_t off, size_t count)
3441 {
3442 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3443 	size_t size = OCP_ART_TEMP_TABLE_SIZE;
3444 	struct nvmem_device *nvmem;
3445 	ssize_t err;
3446 
3447 	nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3448 	if (IS_ERR(nvmem))
3449 		return PTR_ERR(nvmem);
3450 
3451 	if (off > size) {
3452 		err = 0;
3453 		goto out;
3454 	}
3455 
3456 	if (off + count > size)
3457 		count = size - off;
3458 
3459 	// the configuration is in the very beginning of the EEPROM
3460 	err = nvmem_device_read(nvmem, 0x90 + off, count, buf);
3461 	if (err != count) {
3462 		err = -EFAULT;
3463 		goto out;
3464 	}
3465 
3466 out:
3467 	ptp_ocp_nvmem_device_put(&nvmem);
3468 
3469 	return err;
3470 }
3471 
3472 static ssize_t
3473 temperature_table_write(struct file *filp, struct kobject *kobj,
3474 			struct bin_attribute *bin_attr, char *buf,
3475 			loff_t off, size_t count)
3476 {
3477 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3478 	struct nvmem_device *nvmem;
3479 	ssize_t err;
3480 
3481 	/* Allow write of the whole area only */
3482 	if (off || count != OCP_ART_TEMP_TABLE_SIZE)
3483 		return -EFAULT;
3484 
3485 	nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3486 	if (IS_ERR(nvmem))
3487 		return PTR_ERR(nvmem);
3488 
3489 	err = nvmem_device_write(nvmem, 0x90, count, buf);
3490 	if (err != count)
3491 		err = -EFAULT;
3492 
3493 	ptp_ocp_nvmem_device_put(&nvmem);
3494 
3495 	return err;
3496 }
3497 static BIN_ATTR_RW(temperature_table, OCP_ART_TEMP_TABLE_SIZE);
3498 
3499 static struct attribute *fb_timecard_attrs[] = {
3500 	&dev_attr_serialnum.attr,
3501 	&dev_attr_gnss_sync.attr,
3502 	&dev_attr_clock_source.attr,
3503 	&dev_attr_available_clock_sources.attr,
3504 	&dev_attr_sma1.attr,
3505 	&dev_attr_sma2.attr,
3506 	&dev_attr_sma3.attr,
3507 	&dev_attr_sma4.attr,
3508 	&dev_attr_available_sma_inputs.attr,
3509 	&dev_attr_available_sma_outputs.attr,
3510 	&dev_attr_clock_status_drift.attr,
3511 	&dev_attr_clock_status_offset.attr,
3512 	&dev_attr_irig_b_mode.attr,
3513 	&dev_attr_utc_tai_offset.attr,
3514 	&dev_attr_ts_window_adjust.attr,
3515 	&dev_attr_tod_correction.attr,
3516 	NULL,
3517 };
3518 
3519 static const struct attribute_group fb_timecard_group = {
3520 	.attrs = fb_timecard_attrs,
3521 };
3522 
3523 static const struct ocp_attr_group fb_timecard_groups[] = {
3524 	{ .cap = OCP_CAP_BASIC,	    .group = &fb_timecard_group },
3525 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal0_group },
3526 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal1_group },
3527 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal2_group },
3528 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal3_group },
3529 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq0_group },
3530 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq1_group },
3531 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq2_group },
3532 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq3_group },
3533 	{ },
3534 };
3535 
3536 static struct attribute *art_timecard_attrs[] = {
3537 	&dev_attr_serialnum.attr,
3538 	&dev_attr_clock_source.attr,
3539 	&dev_attr_available_clock_sources.attr,
3540 	&dev_attr_utc_tai_offset.attr,
3541 	&dev_attr_ts_window_adjust.attr,
3542 	&dev_attr_sma1.attr,
3543 	&dev_attr_sma2.attr,
3544 	&dev_attr_sma3.attr,
3545 	&dev_attr_sma4.attr,
3546 	&dev_attr_available_sma_inputs.attr,
3547 	&dev_attr_available_sma_outputs.attr,
3548 	NULL,
3549 };
3550 
3551 static struct bin_attribute *bin_art_timecard_attrs[] = {
3552 	&bin_attr_disciplining_config,
3553 	&bin_attr_temperature_table,
3554 	NULL,
3555 };
3556 
3557 static const struct attribute_group art_timecard_group = {
3558 	.attrs = art_timecard_attrs,
3559 	.bin_attrs = bin_art_timecard_attrs,
3560 };
3561 
3562 static const struct ocp_attr_group art_timecard_groups[] = {
3563 	{ .cap = OCP_CAP_BASIC,	    .group = &art_timecard_group },
3564 	{ },
3565 };
3566 
3567 static void
3568 gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
3569 	       const char *def)
3570 {
3571 	int i;
3572 
3573 	for (i = 0; i < 4; i++) {
3574 		if (bp->sma[i].mode != SMA_MODE_IN)
3575 			continue;
3576 		if (map[i][0] & (1 << bit)) {
3577 			sprintf(buf, "sma%d", i + 1);
3578 			return;
3579 		}
3580 	}
3581 	if (!def)
3582 		def = "----";
3583 	strcpy(buf, def);
3584 }
3585 
3586 static void
3587 gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
3588 {
3589 	char *ans = buf;
3590 	int i;
3591 
3592 	strcpy(ans, "----");
3593 	for (i = 0; i < 4; i++) {
3594 		if (bp->sma[i].mode != SMA_MODE_OUT)
3595 			continue;
3596 		if (map[i][1] & (1 << bit))
3597 			ans += sprintf(ans, "sma%d ", i + 1);
3598 	}
3599 }
3600 
3601 static void
3602 _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
3603 {
3604 	struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
3605 	struct ptp_ocp_signal *signal = &bp->signal[nr];
3606 	char label[8];
3607 	bool on;
3608 	u32 val;
3609 
3610 	if (!signal)
3611 		return;
3612 
3613 	on = signal->running;
3614 	sprintf(label, "GEN%d", nr + 1);
3615 	seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
3616 		   label, on ? " ON" : "OFF",
3617 		   signal->period, signal->duty, signal->phase,
3618 		   signal->polarity);
3619 
3620 	val = ioread32(&reg->enable);
3621 	seq_printf(s, " [%x", val);
3622 	val = ioread32(&reg->status);
3623 	seq_printf(s, " %x]", val);
3624 
3625 	seq_printf(s, " start:%llu\n", signal->start);
3626 }
3627 
3628 static void
3629 _frequency_summary_show(struct seq_file *s, int nr,
3630 			struct frequency_reg __iomem *reg)
3631 {
3632 	char label[8];
3633 	bool on;
3634 	u32 val;
3635 
3636 	if (!reg)
3637 		return;
3638 
3639 	sprintf(label, "FREQ%d", nr + 1);
3640 	val = ioread32(&reg->ctrl);
3641 	on = val & 1;
3642 	val = (val >> 8) & 0xff;
3643 	seq_printf(s, "%7s: %s, sec:%u",
3644 		   label,
3645 		   on ? " ON" : "OFF",
3646 		   val);
3647 
3648 	val = ioread32(&reg->status);
3649 	if (val & FREQ_STATUS_ERROR)
3650 		seq_printf(s, ", error");
3651 	if (val & FREQ_STATUS_OVERRUN)
3652 		seq_printf(s, ", overrun");
3653 	if (val & FREQ_STATUS_VALID)
3654 		seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
3655 	seq_printf(s, "  reg:%x\n", val);
3656 }
3657 
3658 static int
3659 ptp_ocp_summary_show(struct seq_file *s, void *data)
3660 {
3661 	struct device *dev = s->private;
3662 	struct ptp_system_timestamp sts;
3663 	struct ts_reg __iomem *ts_reg;
3664 	char *buf, *src, *mac_src;
3665 	struct timespec64 ts;
3666 	struct ptp_ocp *bp;
3667 	u16 sma_val[4][2];
3668 	u32 ctrl, val;
3669 	bool on, map;
3670 	int i;
3671 
3672 	buf = (char *)__get_free_page(GFP_KERNEL);
3673 	if (!buf)
3674 		return -ENOMEM;
3675 
3676 	bp = dev_get_drvdata(dev);
3677 
3678 	seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
3679 	if (bp->gnss_port.line != -1)
3680 		seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1",
3681 			   bp->gnss_port.line);
3682 	if (bp->gnss2_port.line != -1)
3683 		seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2",
3684 			   bp->gnss2_port.line);
3685 	if (bp->mac_port.line != -1)
3686 		seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port.line);
3687 	if (bp->nmea_port.line != -1)
3688 		seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port.line);
3689 
3690 	memset(sma_val, 0xff, sizeof(sma_val));
3691 	if (bp->sma_map1) {
3692 		u32 reg;
3693 
3694 		reg = ioread32(&bp->sma_map1->gpio1);
3695 		sma_val[0][0] = reg & 0xffff;
3696 		sma_val[1][0] = reg >> 16;
3697 
3698 		reg = ioread32(&bp->sma_map1->gpio2);
3699 		sma_val[2][1] = reg & 0xffff;
3700 		sma_val[3][1] = reg >> 16;
3701 
3702 		reg = ioread32(&bp->sma_map2->gpio1);
3703 		sma_val[2][0] = reg & 0xffff;
3704 		sma_val[3][0] = reg >> 16;
3705 
3706 		reg = ioread32(&bp->sma_map2->gpio2);
3707 		sma_val[0][1] = reg & 0xffff;
3708 		sma_val[1][1] = reg >> 16;
3709 	}
3710 
3711 	sma1_show(dev, NULL, buf);
3712 	seq_printf(s, "   sma1: %04x,%04x %s",
3713 		   sma_val[0][0], sma_val[0][1], buf);
3714 
3715 	sma2_show(dev, NULL, buf);
3716 	seq_printf(s, "   sma2: %04x,%04x %s",
3717 		   sma_val[1][0], sma_val[1][1], buf);
3718 
3719 	sma3_show(dev, NULL, buf);
3720 	seq_printf(s, "   sma3: %04x,%04x %s",
3721 		   sma_val[2][0], sma_val[2][1], buf);
3722 
3723 	sma4_show(dev, NULL, buf);
3724 	seq_printf(s, "   sma4: %04x,%04x %s",
3725 		   sma_val[3][0], sma_val[3][1], buf);
3726 
3727 	if (bp->ts0) {
3728 		ts_reg = bp->ts0->mem;
3729 		on = ioread32(&ts_reg->enable);
3730 		src = "GNSS1";
3731 		seq_printf(s, "%7s: %s, src: %s\n", "TS0",
3732 			   on ? " ON" : "OFF", src);
3733 	}
3734 
3735 	if (bp->ts1) {
3736 		ts_reg = bp->ts1->mem;
3737 		on = ioread32(&ts_reg->enable);
3738 		gpio_input_map(buf, bp, sma_val, 2, NULL);
3739 		seq_printf(s, "%7s: %s, src: %s\n", "TS1",
3740 			   on ? " ON" : "OFF", buf);
3741 	}
3742 
3743 	if (bp->ts2) {
3744 		ts_reg = bp->ts2->mem;
3745 		on = ioread32(&ts_reg->enable);
3746 		gpio_input_map(buf, bp, sma_val, 3, NULL);
3747 		seq_printf(s, "%7s: %s, src: %s\n", "TS2",
3748 			   on ? " ON" : "OFF", buf);
3749 	}
3750 
3751 	if (bp->ts3) {
3752 		ts_reg = bp->ts3->mem;
3753 		on = ioread32(&ts_reg->enable);
3754 		gpio_input_map(buf, bp, sma_val, 6, NULL);
3755 		seq_printf(s, "%7s: %s, src: %s\n", "TS3",
3756 			   on ? " ON" : "OFF", buf);
3757 	}
3758 
3759 	if (bp->ts4) {
3760 		ts_reg = bp->ts4->mem;
3761 		on = ioread32(&ts_reg->enable);
3762 		gpio_input_map(buf, bp, sma_val, 7, NULL);
3763 		seq_printf(s, "%7s: %s, src: %s\n", "TS4",
3764 			   on ? " ON" : "OFF", buf);
3765 	}
3766 
3767 	if (bp->pps) {
3768 		ts_reg = bp->pps->mem;
3769 		src = "PHC";
3770 		on = ioread32(&ts_reg->enable);
3771 		map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
3772 		seq_printf(s, "%7s: %s, src: %s\n", "TS5",
3773 			   on && map ? " ON" : "OFF", src);
3774 
3775 		map = !!(bp->pps_req_map & OCP_REQ_PPS);
3776 		seq_printf(s, "%7s: %s, src: %s\n", "PPS",
3777 			   on && map ? " ON" : "OFF", src);
3778 	}
3779 
3780 	if (bp->fw_cap & OCP_CAP_SIGNAL)
3781 		for (i = 0; i < 4; i++)
3782 			_signal_summary_show(s, bp, i);
3783 
3784 	if (bp->fw_cap & OCP_CAP_FREQ)
3785 		for (i = 0; i < 4; i++)
3786 			_frequency_summary_show(s, i, bp->freq_in[i]);
3787 
3788 	if (bp->irig_out) {
3789 		ctrl = ioread32(&bp->irig_out->ctrl);
3790 		on = ctrl & IRIG_M_CTRL_ENABLE;
3791 		val = ioread32(&bp->irig_out->status);
3792 		gpio_output_map(buf, bp, sma_val, 4);
3793 		seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
3794 			   on ? " ON" : "OFF", val, (ctrl >> 16), buf);
3795 	}
3796 
3797 	if (bp->irig_in) {
3798 		on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
3799 		val = ioread32(&bp->irig_in->status);
3800 		gpio_input_map(buf, bp, sma_val, 4, NULL);
3801 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
3802 			   on ? " ON" : "OFF", val, buf);
3803 	}
3804 
3805 	if (bp->dcf_out) {
3806 		on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
3807 		val = ioread32(&bp->dcf_out->status);
3808 		gpio_output_map(buf, bp, sma_val, 5);
3809 		seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
3810 			   on ? " ON" : "OFF", val, buf);
3811 	}
3812 
3813 	if (bp->dcf_in) {
3814 		on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
3815 		val = ioread32(&bp->dcf_in->status);
3816 		gpio_input_map(buf, bp, sma_val, 5, NULL);
3817 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
3818 			   on ? " ON" : "OFF", val, buf);
3819 	}
3820 
3821 	if (bp->nmea_out) {
3822 		on = ioread32(&bp->nmea_out->ctrl) & 1;
3823 		val = ioread32(&bp->nmea_out->status);
3824 		seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
3825 			   on ? " ON" : "OFF", val);
3826 	}
3827 
3828 	/* compute src for PPS1, used below. */
3829 	if (bp->pps_select) {
3830 		val = ioread32(&bp->pps_select->gpio1);
3831 		src = &buf[80];
3832 		mac_src = "GNSS1";
3833 		if (val & 0x01) {
3834 			gpio_input_map(src, bp, sma_val, 0, NULL);
3835 			mac_src = src;
3836 		} else if (val & 0x02) {
3837 			src = "MAC";
3838 		} else if (val & 0x04) {
3839 			src = "GNSS1";
3840 		} else {
3841 			src = "----";
3842 			mac_src = src;
3843 		}
3844 	} else {
3845 		src = "?";
3846 		mac_src = src;
3847 	}
3848 	seq_printf(s, "MAC PPS1 src: %s\n", mac_src);
3849 
3850 	gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
3851 	seq_printf(s, "MAC PPS2 src: %s\n", buf);
3852 
3853 	/* assumes automatic switchover/selection */
3854 	val = ioread32(&bp->reg->select);
3855 	switch (val >> 16) {
3856 	case 0:
3857 		sprintf(buf, "----");
3858 		break;
3859 	case 2:
3860 		sprintf(buf, "IRIG");
3861 		break;
3862 	case 3:
3863 		sprintf(buf, "%s via PPS1", src);
3864 		break;
3865 	case 6:
3866 		sprintf(buf, "DCF");
3867 		break;
3868 	default:
3869 		strcpy(buf, "unknown");
3870 		break;
3871 	}
3872 	seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
3873 		   bp->sync ? "sync" : "unsynced");
3874 
3875 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
3876 		struct timespec64 sys_ts;
3877 		s64 pre_ns, post_ns, ns;
3878 
3879 		pre_ns = timespec64_to_ns(&sts.pre_ts);
3880 		post_ns = timespec64_to_ns(&sts.post_ts);
3881 		ns = (pre_ns + post_ns) / 2;
3882 		ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
3883 		sys_ts = ns_to_timespec64(ns);
3884 
3885 		seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC",
3886 			   ts.tv_sec, ts.tv_nsec, &ts);
3887 		seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS",
3888 			   sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts,
3889 			   bp->utc_tai_offset);
3890 		seq_printf(s, "%7s: PHC:SYS offset: %lld  window: %lld\n", "",
3891 			   timespec64_to_ns(&ts) - ns,
3892 			   post_ns - pre_ns);
3893 	}
3894 
3895 	free_page((unsigned long)buf);
3896 	return 0;
3897 }
3898 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
3899 
3900 static int
3901 ptp_ocp_tod_status_show(struct seq_file *s, void *data)
3902 {
3903 	struct device *dev = s->private;
3904 	struct ptp_ocp *bp;
3905 	u32 val;
3906 	int idx;
3907 
3908 	bp = dev_get_drvdata(dev);
3909 
3910 	val = ioread32(&bp->tod->ctrl);
3911 	if (!(val & TOD_CTRL_ENABLE)) {
3912 		seq_printf(s, "TOD Slave disabled\n");
3913 		return 0;
3914 	}
3915 	seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
3916 
3917 	idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
3918 	idx += (val >> 16) & 3;
3919 	seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
3920 
3921 	idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
3922 	seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
3923 
3924 	val = ioread32(&bp->tod->version);
3925 	seq_printf(s, "TOD Version %d.%d.%d\n",
3926 		val >> 24, (val >> 16) & 0xff, val & 0xffff);
3927 
3928 	val = ioread32(&bp->tod->status);
3929 	seq_printf(s, "Status register: 0x%08X\n", val);
3930 
3931 	val = ioread32(&bp->tod->adj_sec);
3932 	idx = (val & ~INT_MAX) ? -1 : 1;
3933 	idx *= (val & INT_MAX);
3934 	seq_printf(s, "Correction seconds: %d\n", idx);
3935 
3936 	val = ioread32(&bp->tod->utc_status);
3937 	seq_printf(s, "UTC status register: 0x%08X\n", val);
3938 	seq_printf(s, "UTC offset: %ld  valid:%d\n",
3939 		val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
3940 	seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
3941 		val & TOD_STATUS_LEAP_VALID ? 1 : 0,
3942 		val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
3943 
3944 	val = ioread32(&bp->tod->leap);
3945 	seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
3946 
3947 	return 0;
3948 }
3949 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
3950 
3951 static struct dentry *ptp_ocp_debugfs_root;
3952 
3953 static void
3954 ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
3955 {
3956 	struct dentry *d;
3957 
3958 	d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
3959 	bp->debug_root = d;
3960 	debugfs_create_file("summary", 0444, bp->debug_root,
3961 			    &bp->dev, &ptp_ocp_summary_fops);
3962 	if (bp->tod)
3963 		debugfs_create_file("tod_status", 0444, bp->debug_root,
3964 				    &bp->dev, &ptp_ocp_tod_status_fops);
3965 }
3966 
3967 static void
3968 ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
3969 {
3970 	debugfs_remove_recursive(bp->debug_root);
3971 }
3972 
3973 static void
3974 ptp_ocp_debugfs_init(void)
3975 {
3976 	ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
3977 }
3978 
3979 static void
3980 ptp_ocp_debugfs_fini(void)
3981 {
3982 	debugfs_remove_recursive(ptp_ocp_debugfs_root);
3983 }
3984 
3985 static void
3986 ptp_ocp_dev_release(struct device *dev)
3987 {
3988 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3989 
3990 	mutex_lock(&ptp_ocp_lock);
3991 	idr_remove(&ptp_ocp_idr, bp->id);
3992 	mutex_unlock(&ptp_ocp_lock);
3993 }
3994 
3995 static int
3996 ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
3997 {
3998 	int err;
3999 
4000 	mutex_lock(&ptp_ocp_lock);
4001 	err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
4002 	mutex_unlock(&ptp_ocp_lock);
4003 	if (err < 0) {
4004 		dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
4005 		return err;
4006 	}
4007 	bp->id = err;
4008 
4009 	bp->ptp_info = ptp_ocp_clock_info;
4010 	spin_lock_init(&bp->lock);
4011 	bp->gnss_port.line = -1;
4012 	bp->gnss2_port.line = -1;
4013 	bp->mac_port.line = -1;
4014 	bp->nmea_port.line = -1;
4015 	bp->pdev = pdev;
4016 
4017 	device_initialize(&bp->dev);
4018 	dev_set_name(&bp->dev, "ocp%d", bp->id);
4019 	bp->dev.class = &timecard_class;
4020 	bp->dev.parent = &pdev->dev;
4021 	bp->dev.release = ptp_ocp_dev_release;
4022 	dev_set_drvdata(&bp->dev, bp);
4023 
4024 	err = device_add(&bp->dev);
4025 	if (err) {
4026 		dev_err(&bp->dev, "device add failed: %d\n", err);
4027 		goto out;
4028 	}
4029 
4030 	pci_set_drvdata(pdev, bp);
4031 
4032 	return 0;
4033 
4034 out:
4035 	put_device(&bp->dev);
4036 	return err;
4037 }
4038 
4039 static void
4040 ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
4041 {
4042 	struct device *dev = &bp->dev;
4043 
4044 	if (sysfs_create_link(&dev->kobj, &child->kobj, link))
4045 		dev_err(dev, "%s symlink failed\n", link);
4046 }
4047 
4048 static void
4049 ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
4050 {
4051 	struct device *dev, *child;
4052 
4053 	dev = &bp->pdev->dev;
4054 
4055 	child = device_find_child_by_name(dev, name);
4056 	if (!child) {
4057 		dev_err(dev, "Could not find device %s\n", name);
4058 		return;
4059 	}
4060 
4061 	ptp_ocp_symlink(bp, child, link);
4062 	put_device(child);
4063 }
4064 
4065 static int
4066 ptp_ocp_complete(struct ptp_ocp *bp)
4067 {
4068 	struct pps_device *pps;
4069 	char buf[32];
4070 
4071 	if (bp->gnss_port.line != -1) {
4072 		sprintf(buf, "ttyS%d", bp->gnss_port.line);
4073 		ptp_ocp_link_child(bp, buf, "ttyGNSS");
4074 	}
4075 	if (bp->gnss2_port.line != -1) {
4076 		sprintf(buf, "ttyS%d", bp->gnss2_port.line);
4077 		ptp_ocp_link_child(bp, buf, "ttyGNSS2");
4078 	}
4079 	if (bp->mac_port.line != -1) {
4080 		sprintf(buf, "ttyS%d", bp->mac_port.line);
4081 		ptp_ocp_link_child(bp, buf, "ttyMAC");
4082 	}
4083 	if (bp->nmea_port.line != -1) {
4084 		sprintf(buf, "ttyS%d", bp->nmea_port.line);
4085 		ptp_ocp_link_child(bp, buf, "ttyNMEA");
4086 	}
4087 	sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
4088 	ptp_ocp_link_child(bp, buf, "ptp");
4089 
4090 	pps = pps_lookup_dev(bp->ptp);
4091 	if (pps)
4092 		ptp_ocp_symlink(bp, pps->dev, "pps");
4093 
4094 	ptp_ocp_debugfs_add_device(bp);
4095 
4096 	return 0;
4097 }
4098 
4099 static void
4100 ptp_ocp_phc_info(struct ptp_ocp *bp)
4101 {
4102 	struct timespec64 ts;
4103 	u32 version, select;
4104 
4105 	version = ioread32(&bp->reg->version);
4106 	select = ioread32(&bp->reg->select);
4107 	dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
4108 		 version >> 24, (version >> 16) & 0xff, version & 0xffff,
4109 		 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
4110 		 ptp_clock_index(bp->ptp));
4111 
4112 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
4113 		dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
4114 			 ts.tv_sec, ts.tv_nsec,
4115 			 bp->sync ? "in-sync" : "UNSYNCED");
4116 }
4117 
4118 static void
4119 ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
4120 {
4121 	if (port != -1)
4122 		dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
4123 }
4124 
4125 static void
4126 ptp_ocp_info(struct ptp_ocp *bp)
4127 {
4128 	static int nmea_baud[] = {
4129 		1200, 2400, 4800, 9600, 19200, 38400,
4130 		57600, 115200, 230400, 460800, 921600,
4131 		1000000, 2000000
4132 	};
4133 	struct device *dev = &bp->pdev->dev;
4134 	u32 reg;
4135 
4136 	ptp_ocp_phc_info(bp);
4137 
4138 	ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port.line,
4139 			    bp->gnss_port.baud);
4140 	ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port.line,
4141 			    bp->gnss2_port.baud);
4142 	ptp_ocp_serial_info(dev, "MAC", bp->mac_port.line, bp->mac_port.baud);
4143 	if (bp->nmea_out && bp->nmea_port.line != -1) {
4144 		bp->nmea_port.baud = -1;
4145 
4146 		reg = ioread32(&bp->nmea_out->uart_baud);
4147 		if (reg < ARRAY_SIZE(nmea_baud))
4148 			bp->nmea_port.baud = nmea_baud[reg];
4149 
4150 		ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port.line,
4151 				    bp->nmea_port.baud);
4152 	}
4153 }
4154 
4155 static void
4156 ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
4157 {
4158 	struct device *dev = &bp->dev;
4159 
4160 	sysfs_remove_link(&dev->kobj, "ttyGNSS");
4161 	sysfs_remove_link(&dev->kobj, "ttyGNSS2");
4162 	sysfs_remove_link(&dev->kobj, "ttyMAC");
4163 	sysfs_remove_link(&dev->kobj, "ptp");
4164 	sysfs_remove_link(&dev->kobj, "pps");
4165 }
4166 
4167 static void
4168 ptp_ocp_detach(struct ptp_ocp *bp)
4169 {
4170 	int i;
4171 
4172 	ptp_ocp_debugfs_remove_device(bp);
4173 	ptp_ocp_detach_sysfs(bp);
4174 	ptp_ocp_attr_group_del(bp);
4175 	if (timer_pending(&bp->watchdog))
4176 		del_timer_sync(&bp->watchdog);
4177 	if (bp->ts0)
4178 		ptp_ocp_unregister_ext(bp->ts0);
4179 	if (bp->ts1)
4180 		ptp_ocp_unregister_ext(bp->ts1);
4181 	if (bp->ts2)
4182 		ptp_ocp_unregister_ext(bp->ts2);
4183 	if (bp->ts3)
4184 		ptp_ocp_unregister_ext(bp->ts3);
4185 	if (bp->ts4)
4186 		ptp_ocp_unregister_ext(bp->ts4);
4187 	if (bp->pps)
4188 		ptp_ocp_unregister_ext(bp->pps);
4189 	for (i = 0; i < 4; i++)
4190 		if (bp->signal_out[i])
4191 			ptp_ocp_unregister_ext(bp->signal_out[i]);
4192 	if (bp->gnss_port.line != -1)
4193 		serial8250_unregister_port(bp->gnss_port.line);
4194 	if (bp->gnss2_port.line != -1)
4195 		serial8250_unregister_port(bp->gnss2_port.line);
4196 	if (bp->mac_port.line != -1)
4197 		serial8250_unregister_port(bp->mac_port.line);
4198 	if (bp->nmea_port.line != -1)
4199 		serial8250_unregister_port(bp->nmea_port.line);
4200 	platform_device_unregister(bp->spi_flash);
4201 	platform_device_unregister(bp->i2c_ctrl);
4202 	if (bp->i2c_clk)
4203 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
4204 	if (bp->n_irqs)
4205 		pci_free_irq_vectors(bp->pdev);
4206 	if (bp->ptp)
4207 		ptp_clock_unregister(bp->ptp);
4208 	kfree(bp->ptp_info.pin_config);
4209 	device_unregister(&bp->dev);
4210 }
4211 
4212 static int ptp_ocp_dpll_lock_status_get(const struct dpll_device *dpll,
4213 					void *priv,
4214 					enum dpll_lock_status *status,
4215 					struct netlink_ext_ack *extack)
4216 {
4217 	struct ptp_ocp *bp = priv;
4218 
4219 	*status = bp->sync ? DPLL_LOCK_STATUS_LOCKED : DPLL_LOCK_STATUS_UNLOCKED;
4220 
4221 	return 0;
4222 }
4223 
4224 static int ptp_ocp_dpll_state_get(const struct dpll_pin *pin, void *pin_priv,
4225 				  const struct dpll_device *dpll, void *priv,
4226 				  enum dpll_pin_state *state,
4227 				  struct netlink_ext_ack *extack)
4228 {
4229 	struct ptp_ocp *bp = priv;
4230 	int idx;
4231 
4232 	if (bp->pps_select) {
4233 		idx = ioread32(&bp->pps_select->gpio1);
4234 		*state = (&bp->sma[idx] == pin_priv) ? DPLL_PIN_STATE_CONNECTED :
4235 						      DPLL_PIN_STATE_SELECTABLE;
4236 		return 0;
4237 	}
4238 	NL_SET_ERR_MSG(extack, "pin selection is not supported on current HW");
4239 	return -EINVAL;
4240 }
4241 
4242 static int ptp_ocp_dpll_mode_get(const struct dpll_device *dpll, void *priv,
4243 				 enum dpll_mode *mode, struct netlink_ext_ack *extack)
4244 {
4245 	*mode = DPLL_MODE_AUTOMATIC;
4246 	return 0;
4247 }
4248 
4249 static int ptp_ocp_dpll_direction_get(const struct dpll_pin *pin,
4250 				      void *pin_priv,
4251 				      const struct dpll_device *dpll,
4252 				      void *priv,
4253 				      enum dpll_pin_direction *direction,
4254 				      struct netlink_ext_ack *extack)
4255 {
4256 	struct ptp_ocp_sma_connector *sma = pin_priv;
4257 
4258 	*direction = sma->mode == SMA_MODE_IN ?
4259 				  DPLL_PIN_DIRECTION_INPUT :
4260 				  DPLL_PIN_DIRECTION_OUTPUT;
4261 	return 0;
4262 }
4263 
4264 static int ptp_ocp_dpll_direction_set(const struct dpll_pin *pin,
4265 				      void *pin_priv,
4266 				      const struct dpll_device *dpll,
4267 				      void *dpll_priv,
4268 				      enum dpll_pin_direction direction,
4269 				      struct netlink_ext_ack *extack)
4270 {
4271 	struct ptp_ocp_sma_connector *sma = pin_priv;
4272 	struct ptp_ocp *bp = dpll_priv;
4273 	enum ptp_ocp_sma_mode mode;
4274 	int sma_nr = (sma - bp->sma);
4275 
4276 	if (sma->fixed_dir)
4277 		return -EOPNOTSUPP;
4278 	mode = direction == DPLL_PIN_DIRECTION_INPUT ?
4279 			    SMA_MODE_IN : SMA_MODE_OUT;
4280 	return ptp_ocp_sma_store_val(bp, 0, mode, sma_nr);
4281 }
4282 
4283 static int ptp_ocp_dpll_frequency_set(const struct dpll_pin *pin,
4284 				      void *pin_priv,
4285 				      const struct dpll_device *dpll,
4286 				      void *dpll_priv, u64 frequency,
4287 				      struct netlink_ext_ack *extack)
4288 {
4289 	struct ptp_ocp_sma_connector *sma = pin_priv;
4290 	struct ptp_ocp *bp = dpll_priv;
4291 	const struct ocp_selector *tbl;
4292 	int sma_nr = (sma - bp->sma);
4293 	int i;
4294 
4295 	if (sma->fixed_fcn)
4296 		return -EOPNOTSUPP;
4297 
4298 	tbl = bp->sma_op->tbl[sma->mode];
4299 	for (i = 0; tbl[i].name; i++)
4300 		if (tbl[i].frequency == frequency)
4301 			return ptp_ocp_sma_store_val(bp, i, sma->mode, sma_nr);
4302 	return -EINVAL;
4303 }
4304 
4305 static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
4306 				      void *pin_priv,
4307 				      const struct dpll_device *dpll,
4308 				      void *dpll_priv, u64 *frequency,
4309 				      struct netlink_ext_ack *extack)
4310 {
4311 	struct ptp_ocp_sma_connector *sma = pin_priv;
4312 	struct ptp_ocp *bp = dpll_priv;
4313 	const struct ocp_selector *tbl;
4314 	int sma_nr = (sma - bp->sma);
4315 	u32 val;
4316 	int i;
4317 
4318 	val = bp->sma_op->get(bp, sma_nr);
4319 	tbl = bp->sma_op->tbl[sma->mode];
4320 	for (i = 0; tbl[i].name; i++)
4321 		if (val == tbl[i].value) {
4322 			*frequency = tbl[i].frequency;
4323 			return 0;
4324 		}
4325 
4326 	return -EINVAL;
4327 }
4328 
4329 static const struct dpll_device_ops dpll_ops = {
4330 	.lock_status_get = ptp_ocp_dpll_lock_status_get,
4331 	.mode_get = ptp_ocp_dpll_mode_get,
4332 };
4333 
4334 static const struct dpll_pin_ops dpll_pins_ops = {
4335 	.frequency_get = ptp_ocp_dpll_frequency_get,
4336 	.frequency_set = ptp_ocp_dpll_frequency_set,
4337 	.direction_get = ptp_ocp_dpll_direction_get,
4338 	.direction_set = ptp_ocp_dpll_direction_set,
4339 	.state_on_dpll_get = ptp_ocp_dpll_state_get,
4340 };
4341 
4342 static void
4343 ptp_ocp_sync_work(struct work_struct *work)
4344 {
4345 	struct ptp_ocp *bp;
4346 	bool sync;
4347 
4348 	bp = container_of(work, struct ptp_ocp, sync_work.work);
4349 	sync = !!(ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC);
4350 
4351 	if (bp->sync != sync)
4352 		dpll_device_change_ntf(bp->dpll);
4353 
4354 	bp->sync = sync;
4355 
4356 	queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ);
4357 }
4358 
4359 static int
4360 ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4361 {
4362 	struct devlink *devlink;
4363 	struct ptp_ocp *bp;
4364 	int err, i;
4365 	u64 clkid;
4366 
4367 	devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
4368 	if (!devlink) {
4369 		dev_err(&pdev->dev, "devlink_alloc failed\n");
4370 		return -ENOMEM;
4371 	}
4372 
4373 	err = pci_enable_device(pdev);
4374 	if (err) {
4375 		dev_err(&pdev->dev, "pci_enable_device\n");
4376 		goto out_free;
4377 	}
4378 
4379 	bp = devlink_priv(devlink);
4380 	err = ptp_ocp_device_init(bp, pdev);
4381 	if (err)
4382 		goto out_disable;
4383 
4384 	INIT_DELAYED_WORK(&bp->sync_work, ptp_ocp_sync_work);
4385 
4386 	/* compat mode.
4387 	 * Older FPGA firmware only returns 2 irq's.
4388 	 * allow this - if not all of the IRQ's are returned, skip the
4389 	 * extra devices and just register the clock.
4390 	 */
4391 	err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX);
4392 	if (err < 0) {
4393 		dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
4394 		goto out;
4395 	}
4396 	bp->n_irqs = err;
4397 	pci_set_master(pdev);
4398 
4399 	err = ptp_ocp_register_resources(bp, id->driver_data);
4400 	if (err)
4401 		goto out;
4402 
4403 	bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
4404 	if (IS_ERR(bp->ptp)) {
4405 		err = PTR_ERR(bp->ptp);
4406 		dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
4407 		bp->ptp = NULL;
4408 		goto out;
4409 	}
4410 
4411 	err = ptp_ocp_complete(bp);
4412 	if (err)
4413 		goto out;
4414 
4415 	ptp_ocp_info(bp);
4416 	devlink_register(devlink);
4417 
4418 	clkid = pci_get_dsn(pdev);
4419 	bp->dpll = dpll_device_get(clkid, 0, THIS_MODULE);
4420 	if (IS_ERR(bp->dpll)) {
4421 		err = PTR_ERR(bp->dpll);
4422 		dev_err(&pdev->dev, "dpll_device_alloc failed\n");
4423 		goto out;
4424 	}
4425 
4426 	err = dpll_device_register(bp->dpll, DPLL_TYPE_PPS, &dpll_ops, bp);
4427 	if (err)
4428 		goto out;
4429 
4430 	for (i = 0; i < OCP_SMA_NUM; i++) {
4431 		bp->sma[i].dpll_pin = dpll_pin_get(clkid, i, THIS_MODULE, &bp->sma[i].dpll_prop);
4432 		if (IS_ERR(bp->sma[i].dpll_pin)) {
4433 			err = PTR_ERR(bp->sma[i].dpll_pin);
4434 			goto out_dpll;
4435 		}
4436 
4437 		err = dpll_pin_register(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops,
4438 					&bp->sma[i]);
4439 		if (err) {
4440 			dpll_pin_put(bp->sma[i].dpll_pin);
4441 			goto out_dpll;
4442 		}
4443 	}
4444 	queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ);
4445 
4446 	return 0;
4447 out_dpll:
4448 	while (i) {
4449 		--i;
4450 		dpll_pin_unregister(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]);
4451 		dpll_pin_put(bp->sma[i].dpll_pin);
4452 	}
4453 	dpll_device_put(bp->dpll);
4454 out:
4455 	ptp_ocp_detach(bp);
4456 out_disable:
4457 	pci_disable_device(pdev);
4458 out_free:
4459 	devlink_free(devlink);
4460 	return err;
4461 }
4462 
4463 static void
4464 ptp_ocp_remove(struct pci_dev *pdev)
4465 {
4466 	struct ptp_ocp *bp = pci_get_drvdata(pdev);
4467 	struct devlink *devlink = priv_to_devlink(bp);
4468 	int i;
4469 
4470 	cancel_delayed_work_sync(&bp->sync_work);
4471 	for (i = 0; i < OCP_SMA_NUM; i++) {
4472 		if (bp->sma[i].dpll_pin) {
4473 			dpll_pin_unregister(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]);
4474 			dpll_pin_put(bp->sma[i].dpll_pin);
4475 		}
4476 	}
4477 	dpll_device_unregister(bp->dpll, &dpll_ops, bp);
4478 	dpll_device_put(bp->dpll);
4479 	devlink_unregister(devlink);
4480 	ptp_ocp_detach(bp);
4481 	pci_disable_device(pdev);
4482 
4483 	devlink_free(devlink);
4484 }
4485 
4486 static struct pci_driver ptp_ocp_driver = {
4487 	.name		= KBUILD_MODNAME,
4488 	.id_table	= ptp_ocp_pcidev_id,
4489 	.probe		= ptp_ocp_probe,
4490 	.remove		= ptp_ocp_remove,
4491 };
4492 
4493 static int
4494 ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
4495 			  unsigned long action, void *data)
4496 {
4497 	struct device *dev, *child = data;
4498 	struct ptp_ocp *bp;
4499 	bool add;
4500 
4501 	switch (action) {
4502 	case BUS_NOTIFY_ADD_DEVICE:
4503 	case BUS_NOTIFY_DEL_DEVICE:
4504 		add = action == BUS_NOTIFY_ADD_DEVICE;
4505 		break;
4506 	default:
4507 		return 0;
4508 	}
4509 
4510 	if (!i2c_verify_adapter(child))
4511 		return 0;
4512 
4513 	dev = child;
4514 	while ((dev = dev->parent))
4515 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
4516 			goto found;
4517 	return 0;
4518 
4519 found:
4520 	bp = dev_get_drvdata(dev);
4521 	if (add)
4522 		ptp_ocp_symlink(bp, child, "i2c");
4523 	else
4524 		sysfs_remove_link(&bp->dev.kobj, "i2c");
4525 
4526 	return 0;
4527 }
4528 
4529 static struct notifier_block ptp_ocp_i2c_notifier = {
4530 	.notifier_call = ptp_ocp_i2c_notifier_call,
4531 };
4532 
4533 static int __init
4534 ptp_ocp_init(void)
4535 {
4536 	const char *what;
4537 	int err;
4538 
4539 	ptp_ocp_debugfs_init();
4540 
4541 	what = "timecard class";
4542 	err = class_register(&timecard_class);
4543 	if (err)
4544 		goto out;
4545 
4546 	what = "i2c notifier";
4547 	err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4548 	if (err)
4549 		goto out_notifier;
4550 
4551 	what = "ptp_ocp driver";
4552 	err = pci_register_driver(&ptp_ocp_driver);
4553 	if (err)
4554 		goto out_register;
4555 
4556 	return 0;
4557 
4558 out_register:
4559 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4560 out_notifier:
4561 	class_unregister(&timecard_class);
4562 out:
4563 	ptp_ocp_debugfs_fini();
4564 	pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
4565 	return err;
4566 }
4567 
4568 static void __exit
4569 ptp_ocp_fini(void)
4570 {
4571 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4572 	pci_unregister_driver(&ptp_ocp_driver);
4573 	class_unregister(&timecard_class);
4574 	ptp_ocp_debugfs_fini();
4575 }
4576 
4577 module_init(ptp_ocp_init);
4578 module_exit(ptp_ocp_fini);
4579 
4580 MODULE_DESCRIPTION("OpenCompute TimeCard driver");
4581 MODULE_LICENSE("GPL v2");
4582