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