xref: /openbmc/linux/drivers/ptp/ptp_ocp.c (revision dd21bfa4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
3 
4 #include <linux/err.h>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/debugfs.h>
8 #include <linux/init.h>
9 #include <linux/pci.h>
10 #include <linux/serial_8250.h>
11 #include <linux/clkdev.h>
12 #include <linux/clk-provider.h>
13 #include <linux/platform_device.h>
14 #include <linux/ptp_clock_kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/spi/xilinx_spi.h>
17 #include <net/devlink.h>
18 #include <linux/i2c.h>
19 #include <linux/mtd/mtd.h>
20 
21 #ifndef PCI_VENDOR_ID_FACEBOOK
22 #define PCI_VENDOR_ID_FACEBOOK 0x1d9b
23 #endif
24 
25 #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD
26 #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
27 #endif
28 
29 static struct class timecard_class = {
30 	.owner		= THIS_MODULE,
31 	.name		= "timecard",
32 };
33 
34 struct ocp_reg {
35 	u32	ctrl;
36 	u32	status;
37 	u32	select;
38 	u32	version;
39 	u32	time_ns;
40 	u32	time_sec;
41 	u32	__pad0[2];
42 	u32	adjust_ns;
43 	u32	adjust_sec;
44 	u32	__pad1[2];
45 	u32	offset_ns;
46 	u32	offset_window_ns;
47 	u32	__pad2[2];
48 	u32	drift_ns;
49 	u32	drift_window_ns;
50 	u32	__pad3[6];
51 	u32	servo_offset_p;
52 	u32	servo_offset_i;
53 	u32	servo_drift_p;
54 	u32	servo_drift_i;
55 };
56 
57 #define OCP_CTRL_ENABLE		BIT(0)
58 #define OCP_CTRL_ADJUST_TIME	BIT(1)
59 #define OCP_CTRL_ADJUST_OFFSET	BIT(2)
60 #define OCP_CTRL_ADJUST_DRIFT	BIT(3)
61 #define OCP_CTRL_ADJUST_SERVO	BIT(8)
62 #define OCP_CTRL_READ_TIME_REQ	BIT(30)
63 #define OCP_CTRL_READ_TIME_DONE	BIT(31)
64 
65 #define OCP_STATUS_IN_SYNC	BIT(0)
66 #define OCP_STATUS_IN_HOLDOVER	BIT(1)
67 
68 #define OCP_SELECT_CLK_NONE	0
69 #define OCP_SELECT_CLK_REG	0xfe
70 
71 struct tod_reg {
72 	u32	ctrl;
73 	u32	status;
74 	u32	uart_polarity;
75 	u32	version;
76 	u32	adj_sec;
77 	u32	__pad0[3];
78 	u32	uart_baud;
79 	u32	__pad1[3];
80 	u32	utc_status;
81 	u32	leap;
82 };
83 
84 #define TOD_CTRL_PROTOCOL	BIT(28)
85 #define TOD_CTRL_DISABLE_FMT_A	BIT(17)
86 #define TOD_CTRL_DISABLE_FMT_B	BIT(16)
87 #define TOD_CTRL_ENABLE		BIT(0)
88 #define TOD_CTRL_GNSS_MASK	((1U << 4) - 1)
89 #define TOD_CTRL_GNSS_SHIFT	24
90 
91 #define TOD_STATUS_UTC_MASK	0xff
92 #define TOD_STATUS_UTC_VALID	BIT(8)
93 #define TOD_STATUS_LEAP_VALID	BIT(16)
94 
95 struct ts_reg {
96 	u32	enable;
97 	u32	error;
98 	u32	polarity;
99 	u32	version;
100 	u32	__pad0[4];
101 	u32	cable_delay;
102 	u32	__pad1[3];
103 	u32	intr;
104 	u32	intr_mask;
105 	u32	event_count;
106 	u32	__pad2[1];
107 	u32	ts_count;
108 	u32	time_ns;
109 	u32	time_sec;
110 	u32	data_width;
111 	u32	data;
112 };
113 
114 struct pps_reg {
115 	u32	ctrl;
116 	u32	status;
117 	u32	__pad0[6];
118 	u32	cable_delay;
119 };
120 
121 #define PPS_STATUS_FILTER_ERR	BIT(0)
122 #define PPS_STATUS_SUPERV_ERR	BIT(1)
123 
124 struct img_reg {
125 	u32	version;
126 };
127 
128 struct gpio_reg {
129 	u32	gpio1;
130 	u32	__pad0;
131 	u32	gpio2;
132 	u32	__pad1;
133 };
134 
135 struct irig_master_reg {
136 	u32	ctrl;
137 	u32	status;
138 	u32	__pad0;
139 	u32	version;
140 	u32	adj_sec;
141 	u32	mode_ctrl;
142 };
143 
144 #define IRIG_M_CTRL_ENABLE	BIT(0)
145 
146 struct irig_slave_reg {
147 	u32	ctrl;
148 	u32	status;
149 	u32	__pad0;
150 	u32	version;
151 	u32	adj_sec;
152 	u32	mode_ctrl;
153 };
154 
155 #define IRIG_S_CTRL_ENABLE	BIT(0)
156 
157 struct dcf_master_reg {
158 	u32	ctrl;
159 	u32	status;
160 	u32	__pad0;
161 	u32	version;
162 	u32	adj_sec;
163 };
164 
165 #define DCF_M_CTRL_ENABLE	BIT(0)
166 
167 struct dcf_slave_reg {
168 	u32	ctrl;
169 	u32	status;
170 	u32	__pad0;
171 	u32	version;
172 	u32	adj_sec;
173 };
174 
175 #define DCF_S_CTRL_ENABLE	BIT(0)
176 
177 struct ptp_ocp_flash_info {
178 	const char *name;
179 	int pci_offset;
180 	int data_size;
181 	void *data;
182 };
183 
184 struct ptp_ocp_i2c_info {
185 	const char *name;
186 	unsigned long fixed_rate;
187 	size_t data_size;
188 	void *data;
189 };
190 
191 struct ptp_ocp_ext_info {
192 	int index;
193 	irqreturn_t (*irq_fcn)(int irq, void *priv);
194 	int (*enable)(void *priv, u32 req, bool enable);
195 };
196 
197 struct ptp_ocp_ext_src {
198 	void __iomem		*mem;
199 	struct ptp_ocp		*bp;
200 	struct ptp_ocp_ext_info	*info;
201 	int			irq_vec;
202 };
203 
204 struct ptp_ocp {
205 	struct pci_dev		*pdev;
206 	struct device		dev;
207 	spinlock_t		lock;
208 	struct ocp_reg __iomem	*reg;
209 	struct tod_reg __iomem	*tod;
210 	struct pps_reg __iomem	*pps_to_ext;
211 	struct pps_reg __iomem	*pps_to_clk;
212 	struct gpio_reg __iomem	*pps_select;
213 	struct gpio_reg __iomem	*sma;
214 	struct irig_master_reg	__iomem *irig_out;
215 	struct irig_slave_reg	__iomem *irig_in;
216 	struct dcf_master_reg	__iomem *dcf_out;
217 	struct dcf_slave_reg	__iomem *dcf_in;
218 	struct tod_reg		__iomem *nmea_out;
219 	struct ptp_ocp_ext_src	*pps;
220 	struct ptp_ocp_ext_src	*ts0;
221 	struct ptp_ocp_ext_src	*ts1;
222 	struct ptp_ocp_ext_src	*ts2;
223 	struct img_reg __iomem	*image;
224 	struct ptp_clock	*ptp;
225 	struct ptp_clock_info	ptp_info;
226 	struct platform_device	*i2c_ctrl;
227 	struct platform_device	*spi_flash;
228 	struct clk_hw		*i2c_clk;
229 	struct timer_list	watchdog;
230 	struct dentry		*debug_root;
231 	time64_t		gnss_lost;
232 	int			id;
233 	int			n_irqs;
234 	int			gnss_port;
235 	int			gnss2_port;
236 	int			mac_port;	/* miniature atomic clock */
237 	int			nmea_port;
238 	u8			serial[6];
239 	bool			has_serial;
240 	u32			pps_req_map;
241 	int			flash_start;
242 	u32			utc_tai_offset;
243 	u32			ts_window_adjust;
244 };
245 
246 #define OCP_REQ_TIMESTAMP	BIT(0)
247 #define OCP_REQ_PPS		BIT(1)
248 
249 struct ocp_resource {
250 	unsigned long offset;
251 	int size;
252 	int irq_vec;
253 	int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
254 	void *extra;
255 	unsigned long bp_offset;
256 	const char * const name;
257 };
258 
259 static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
260 static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
261 static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
262 static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
263 static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
264 static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
265 static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
266 static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
267 
268 #define bp_assign_entry(bp, res, val) ({				\
269 	uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset;		\
270 	*(typeof(val) *)addr = val;					\
271 })
272 
273 #define OCP_RES_LOCATION(member) \
274 	.name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
275 
276 #define OCP_MEM_RESOURCE(member) \
277 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
278 
279 #define OCP_SERIAL_RESOURCE(member) \
280 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
281 
282 #define OCP_I2C_RESOURCE(member) \
283 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
284 
285 #define OCP_SPI_RESOURCE(member) \
286 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
287 
288 #define OCP_EXT_RESOURCE(member) \
289 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
290 
291 /* This is the MSI vector mapping used.
292  * 0: TS3 (and PPS)
293  * 1: TS0
294  * 2: TS1
295  * 3: GNSS
296  * 4: GNSS2
297  * 5: MAC
298  * 6: TS2
299  * 7: I2C controller
300  * 8: HWICAP (notused)
301  * 9: SPI Flash
302  * 10: NMEA
303  */
304 
305 static struct ocp_resource ocp_fb_resource[] = {
306 	{
307 		OCP_MEM_RESOURCE(reg),
308 		.offset = 0x01000000, .size = 0x10000,
309 	},
310 	{
311 		OCP_EXT_RESOURCE(ts0),
312 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
313 		.extra = &(struct ptp_ocp_ext_info) {
314 			.index = 0,
315 			.irq_fcn = ptp_ocp_ts_irq,
316 			.enable = ptp_ocp_ts_enable,
317 		},
318 	},
319 	{
320 		OCP_EXT_RESOURCE(ts1),
321 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
322 		.extra = &(struct ptp_ocp_ext_info) {
323 			.index = 1,
324 			.irq_fcn = ptp_ocp_ts_irq,
325 			.enable = ptp_ocp_ts_enable,
326 		},
327 	},
328 	{
329 		OCP_EXT_RESOURCE(ts2),
330 		.offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
331 		.extra = &(struct ptp_ocp_ext_info) {
332 			.index = 2,
333 			.irq_fcn = ptp_ocp_ts_irq,
334 			.enable = ptp_ocp_ts_enable,
335 		},
336 	},
337 	{
338 		OCP_EXT_RESOURCE(pps),
339 		.offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
340 		.extra = &(struct ptp_ocp_ext_info) {
341 			.index = 3,
342 			.irq_fcn = ptp_ocp_ts_irq,
343 			.enable = ptp_ocp_ts_enable,
344 		},
345 	},
346 	{
347 		OCP_MEM_RESOURCE(pps_to_ext),
348 		.offset = 0x01030000, .size = 0x10000,
349 	},
350 	{
351 		OCP_MEM_RESOURCE(pps_to_clk),
352 		.offset = 0x01040000, .size = 0x10000,
353 	},
354 	{
355 		OCP_MEM_RESOURCE(tod),
356 		.offset = 0x01050000, .size = 0x10000,
357 	},
358 	{
359 		OCP_MEM_RESOURCE(irig_in),
360 		.offset = 0x01070000, .size = 0x10000,
361 	},
362 	{
363 		OCP_MEM_RESOURCE(irig_out),
364 		.offset = 0x01080000, .size = 0x10000,
365 	},
366 	{
367 		OCP_MEM_RESOURCE(dcf_in),
368 		.offset = 0x01090000, .size = 0x10000,
369 	},
370 	{
371 		OCP_MEM_RESOURCE(dcf_out),
372 		.offset = 0x010A0000, .size = 0x10000,
373 	},
374 	{
375 		OCP_MEM_RESOURCE(nmea_out),
376 		.offset = 0x010B0000, .size = 0x10000,
377 	},
378 	{
379 		OCP_MEM_RESOURCE(image),
380 		.offset = 0x00020000, .size = 0x1000,
381 	},
382 	{
383 		OCP_MEM_RESOURCE(pps_select),
384 		.offset = 0x00130000, .size = 0x1000,
385 	},
386 	{
387 		OCP_MEM_RESOURCE(sma),
388 		.offset = 0x00140000, .size = 0x1000,
389 	},
390 	{
391 		OCP_I2C_RESOURCE(i2c_ctrl),
392 		.offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
393 		.extra = &(struct ptp_ocp_i2c_info) {
394 			.name = "xiic-i2c",
395 			.fixed_rate = 50000000,
396 		},
397 	},
398 	{
399 		OCP_SERIAL_RESOURCE(gnss_port),
400 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
401 	},
402 	{
403 		OCP_SERIAL_RESOURCE(gnss2_port),
404 		.offset = 0x00170000 + 0x1000, .irq_vec = 4,
405 	},
406 	{
407 		OCP_SERIAL_RESOURCE(mac_port),
408 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
409 	},
410 	{
411 		OCP_SERIAL_RESOURCE(nmea_port),
412 		.offset = 0x00190000 + 0x1000, .irq_vec = 10,
413 	},
414 	{
415 		OCP_SPI_RESOURCE(spi_flash),
416 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
417 		.extra = &(struct ptp_ocp_flash_info) {
418 			.name = "xilinx_spi", .pci_offset = 0,
419 			.data_size = sizeof(struct xspi_platform_data),
420 			.data = &(struct xspi_platform_data) {
421 				.num_chipselect = 1,
422 				.bits_per_word = 8,
423 				.num_devices = 1,
424 				.devices = &(struct spi_board_info) {
425 					.modalias = "spi-nor",
426 				},
427 			},
428 		},
429 	},
430 	{
431 		.setup = ptp_ocp_fb_board_init,
432 	},
433 	{ }
434 };
435 
436 static const struct pci_device_id ptp_ocp_pcidev_id[] = {
437 	{ PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
438 	{ 0 }
439 };
440 MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
441 
442 static DEFINE_MUTEX(ptp_ocp_lock);
443 static DEFINE_IDR(ptp_ocp_idr);
444 
445 struct ocp_selector {
446 	const char *name;
447 	int value;
448 };
449 
450 static struct ocp_selector ptp_ocp_clock[] = {
451 	{ .name = "NONE",	.value = 0 },
452 	{ .name = "TOD",	.value = 1 },
453 	{ .name = "IRIG",	.value = 2 },
454 	{ .name = "PPS",	.value = 3 },
455 	{ .name = "PTP",	.value = 4 },
456 	{ .name = "RTC",	.value = 5 },
457 	{ .name = "DCF",	.value = 6 },
458 	{ .name = "REGS",	.value = 0xfe },
459 	{ .name = "EXT",	.value = 0xff },
460 	{ }
461 };
462 
463 static struct ocp_selector ptp_ocp_sma_in[] = {
464 	{ .name = "10Mhz",	.value = 0x00 },
465 	{ .name = "PPS1",	.value = 0x01 },
466 	{ .name = "PPS2",	.value = 0x02 },
467 	{ .name = "TS1",	.value = 0x04 },
468 	{ .name = "TS2",	.value = 0x08 },
469 	{ .name = "IRIG",	.value = 0x10 },
470 	{ .name = "DCF",	.value = 0x20 },
471 	{ }
472 };
473 
474 static struct ocp_selector ptp_ocp_sma_out[] = {
475 	{ .name = "10Mhz",	.value = 0x00 },
476 	{ .name = "PHC",	.value = 0x01 },
477 	{ .name = "MAC",	.value = 0x02 },
478 	{ .name = "GNSS",	.value = 0x04 },
479 	{ .name = "GNSS2",	.value = 0x08 },
480 	{ .name = "IRIG",	.value = 0x10 },
481 	{ .name = "DCF",	.value = 0x20 },
482 	{ }
483 };
484 
485 static const char *
486 ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val)
487 {
488 	int i;
489 
490 	for (i = 0; tbl[i].name; i++)
491 		if (tbl[i].value == val)
492 			return tbl[i].name;
493 	return NULL;
494 }
495 
496 static int
497 ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name)
498 {
499 	const char *select;
500 	int i;
501 
502 	for (i = 0; tbl[i].name; i++) {
503 		select = tbl[i].name;
504 		if (!strncasecmp(name, select, strlen(select)))
505 			return tbl[i].value;
506 	}
507 	return -EINVAL;
508 }
509 
510 static ssize_t
511 ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf)
512 {
513 	ssize_t count;
514 	int i;
515 
516 	count = 0;
517 	for (i = 0; tbl[i].name; i++)
518 		count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
519 	if (count)
520 		count--;
521 	count += sysfs_emit_at(buf, count, "\n");
522 	return count;
523 }
524 
525 static int
526 __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
527 			 struct ptp_system_timestamp *sts)
528 {
529 	u32 ctrl, time_sec, time_ns;
530 	int i;
531 
532 	ptp_read_system_prets(sts);
533 
534 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
535 	iowrite32(ctrl, &bp->reg->ctrl);
536 
537 	for (i = 0; i < 100; i++) {
538 		ctrl = ioread32(&bp->reg->ctrl);
539 		if (ctrl & OCP_CTRL_READ_TIME_DONE)
540 			break;
541 	}
542 	ptp_read_system_postts(sts);
543 
544 	if (sts && bp->ts_window_adjust) {
545 		s64 ns = timespec64_to_ns(&sts->post_ts);
546 
547 		sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
548 	}
549 
550 	time_ns = ioread32(&bp->reg->time_ns);
551 	time_sec = ioread32(&bp->reg->time_sec);
552 
553 	ts->tv_sec = time_sec;
554 	ts->tv_nsec = time_ns;
555 
556 	return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
557 }
558 
559 static int
560 ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
561 		 struct ptp_system_timestamp *sts)
562 {
563 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
564 	unsigned long flags;
565 	int err;
566 
567 	spin_lock_irqsave(&bp->lock, flags);
568 	err = __ptp_ocp_gettime_locked(bp, ts, sts);
569 	spin_unlock_irqrestore(&bp->lock, flags);
570 
571 	return err;
572 }
573 
574 static void
575 __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
576 {
577 	u32 ctrl, time_sec, time_ns;
578 	u32 select;
579 
580 	time_ns = ts->tv_nsec;
581 	time_sec = ts->tv_sec;
582 
583 	select = ioread32(&bp->reg->select);
584 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
585 
586 	iowrite32(time_ns, &bp->reg->adjust_ns);
587 	iowrite32(time_sec, &bp->reg->adjust_sec);
588 
589 	ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
590 	iowrite32(ctrl, &bp->reg->ctrl);
591 
592 	/* restore clock selection */
593 	iowrite32(select >> 16, &bp->reg->select);
594 }
595 
596 static int
597 ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
598 {
599 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
600 	unsigned long flags;
601 
602 	spin_lock_irqsave(&bp->lock, flags);
603 	__ptp_ocp_settime_locked(bp, ts);
604 	spin_unlock_irqrestore(&bp->lock, flags);
605 
606 	return 0;
607 }
608 
609 static void
610 __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
611 {
612 	u32 select, ctrl;
613 
614 	select = ioread32(&bp->reg->select);
615 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
616 
617 	iowrite32(adj_val, &bp->reg->offset_ns);
618 	iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
619 
620 	ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
621 	iowrite32(ctrl, &bp->reg->ctrl);
622 
623 	/* restore clock selection */
624 	iowrite32(select >> 16, &bp->reg->select);
625 }
626 
627 static void
628 ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns)
629 {
630 	struct timespec64 ts;
631 	unsigned long flags;
632 	int err;
633 
634 	spin_lock_irqsave(&bp->lock, flags);
635 	err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
636 	if (likely(!err)) {
637 		timespec64_add_ns(&ts, delta_ns);
638 		__ptp_ocp_settime_locked(bp, &ts);
639 	}
640 	spin_unlock_irqrestore(&bp->lock, flags);
641 }
642 
643 static int
644 ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
645 {
646 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
647 	unsigned long flags;
648 	u32 adj_ns, sign;
649 
650 	if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
651 		ptp_ocp_adjtime_coarse(bp, delta_ns);
652 		return 0;
653 	}
654 
655 	sign = delta_ns < 0 ? BIT(31) : 0;
656 	adj_ns = sign ? -delta_ns : delta_ns;
657 
658 	spin_lock_irqsave(&bp->lock, flags);
659 	__ptp_ocp_adjtime_locked(bp, sign | adj_ns);
660 	spin_unlock_irqrestore(&bp->lock, flags);
661 
662 	return 0;
663 }
664 
665 static int
666 ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
667 {
668 	if (scaled_ppm == 0)
669 		return 0;
670 
671 	return -EOPNOTSUPP;
672 }
673 
674 static int
675 ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
676 {
677 	return -EOPNOTSUPP;
678 }
679 
680 static int
681 ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
682 	       int on)
683 {
684 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
685 	struct ptp_ocp_ext_src *ext = NULL;
686 	u32 req;
687 	int err;
688 
689 	switch (rq->type) {
690 	case PTP_CLK_REQ_EXTTS:
691 		req = OCP_REQ_TIMESTAMP;
692 		switch (rq->extts.index) {
693 		case 0:
694 			ext = bp->ts0;
695 			break;
696 		case 1:
697 			ext = bp->ts1;
698 			break;
699 		case 2:
700 			ext = bp->ts2;
701 			break;
702 		case 3:
703 			ext = bp->pps;
704 			break;
705 		}
706 		break;
707 	case PTP_CLK_REQ_PPS:
708 		req = OCP_REQ_PPS;
709 		ext = bp->pps;
710 		break;
711 	case PTP_CLK_REQ_PEROUT:
712 		if (on &&
713 		    (rq->perout.period.sec != 1 || rq->perout.period.nsec != 0))
714 			return -EINVAL;
715 		/* This is a request for 1PPS on an output SMA.
716 		 * Allow, but assume manual configuration.
717 		 */
718 		return 0;
719 	default:
720 		return -EOPNOTSUPP;
721 	}
722 
723 	err = -ENXIO;
724 	if (ext)
725 		err = ext->info->enable(ext, req, on);
726 
727 	return err;
728 }
729 
730 static const struct ptp_clock_info ptp_ocp_clock_info = {
731 	.owner		= THIS_MODULE,
732 	.name		= KBUILD_MODNAME,
733 	.max_adj	= 100000000,
734 	.gettimex64	= ptp_ocp_gettimex,
735 	.settime64	= ptp_ocp_settime,
736 	.adjtime	= ptp_ocp_adjtime,
737 	.adjfine	= ptp_ocp_null_adjfine,
738 	.adjphase	= ptp_ocp_null_adjphase,
739 	.enable		= ptp_ocp_enable,
740 	.pps		= true,
741 	.n_ext_ts	= 4,
742 	.n_per_out	= 1,
743 };
744 
745 static void
746 __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
747 {
748 	u32 ctrl, select;
749 
750 	select = ioread32(&bp->reg->select);
751 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
752 
753 	iowrite32(0, &bp->reg->drift_ns);
754 
755 	ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
756 	iowrite32(ctrl, &bp->reg->ctrl);
757 
758 	/* restore clock selection */
759 	iowrite32(select >> 16, &bp->reg->select);
760 }
761 
762 static void
763 ptp_ocp_watchdog(struct timer_list *t)
764 {
765 	struct ptp_ocp *bp = from_timer(bp, t, watchdog);
766 	unsigned long flags;
767 	u32 status;
768 
769 	status = ioread32(&bp->pps_to_clk->status);
770 
771 	if (status & PPS_STATUS_SUPERV_ERR) {
772 		iowrite32(status, &bp->pps_to_clk->status);
773 		if (!bp->gnss_lost) {
774 			spin_lock_irqsave(&bp->lock, flags);
775 			__ptp_ocp_clear_drift_locked(bp);
776 			spin_unlock_irqrestore(&bp->lock, flags);
777 			bp->gnss_lost = ktime_get_real_seconds();
778 		}
779 
780 	} else if (bp->gnss_lost) {
781 		bp->gnss_lost = 0;
782 	}
783 
784 	mod_timer(&bp->watchdog, jiffies + HZ);
785 }
786 
787 static void
788 ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
789 {
790 	ktime_t start, end;
791 	ktime_t delay;
792 	u32 ctrl;
793 
794 	ctrl = ioread32(&bp->reg->ctrl);
795 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
796 
797 	iowrite32(ctrl, &bp->reg->ctrl);
798 
799 	start = ktime_get_ns();
800 
801 	ctrl = ioread32(&bp->reg->ctrl);
802 
803 	end = ktime_get_ns();
804 
805 	delay = end - start;
806 	bp->ts_window_adjust = (delay >> 5) * 3;
807 }
808 
809 static int
810 ptp_ocp_init_clock(struct ptp_ocp *bp)
811 {
812 	struct timespec64 ts;
813 	bool sync;
814 	u32 ctrl;
815 
816 	ctrl = OCP_CTRL_ENABLE;
817 	iowrite32(ctrl, &bp->reg->ctrl);
818 
819 	/* NO DRIFT Correction */
820 	/* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
821 	iowrite32(0x2000, &bp->reg->servo_offset_p);
822 	iowrite32(0x1000, &bp->reg->servo_offset_i);
823 	iowrite32(0,	  &bp->reg->servo_drift_p);
824 	iowrite32(0,	  &bp->reg->servo_drift_i);
825 
826 	/* latch servo values */
827 	ctrl |= OCP_CTRL_ADJUST_SERVO;
828 	iowrite32(ctrl, &bp->reg->ctrl);
829 
830 	if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
831 		dev_err(&bp->pdev->dev, "clock not enabled\n");
832 		return -ENODEV;
833 	}
834 
835 	ptp_ocp_estimate_pci_timing(bp);
836 
837 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
838 	if (!sync) {
839 		ktime_get_clocktai_ts64(&ts);
840 		ptp_ocp_settime(&bp->ptp_info, &ts);
841 	}
842 
843 	/* If there is a clock supervisor, then enable the watchdog */
844 	if (bp->pps_to_clk) {
845 		timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
846 		mod_timer(&bp->watchdog, jiffies + HZ);
847 	}
848 
849 	return 0;
850 }
851 
852 static void
853 ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
854 {
855 	unsigned long flags;
856 
857 	spin_lock_irqsave(&bp->lock, flags);
858 
859 	bp->utc_tai_offset = val;
860 
861 	if (bp->irig_out)
862 		iowrite32(val, &bp->irig_out->adj_sec);
863 	if (bp->dcf_out)
864 		iowrite32(val, &bp->dcf_out->adj_sec);
865 	if (bp->nmea_out)
866 		iowrite32(val, &bp->nmea_out->adj_sec);
867 
868 	spin_unlock_irqrestore(&bp->lock, flags);
869 }
870 
871 static void
872 ptp_ocp_tod_init(struct ptp_ocp *bp)
873 {
874 	u32 ctrl, reg;
875 
876 	ctrl = ioread32(&bp->tod->ctrl);
877 	ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
878 	ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
879 	iowrite32(ctrl, &bp->tod->ctrl);
880 
881 	reg = ioread32(&bp->tod->utc_status);
882 	if (reg & TOD_STATUS_UTC_VALID)
883 		ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
884 }
885 
886 static void
887 ptp_ocp_tod_info(struct ptp_ocp *bp)
888 {
889 	static const char * const proto_name[] = {
890 		"NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
891 		"UBX", "UBX_UTC", "UBX_LS", "UBX_none"
892 	};
893 	static const char * const gnss_name[] = {
894 		"ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
895 	};
896 	u32 version, ctrl, reg;
897 	int idx;
898 
899 	version = ioread32(&bp->tod->version);
900 	dev_info(&bp->pdev->dev, "TOD Version %d.%d.%d\n",
901 		 version >> 24, (version >> 16) & 0xff, version & 0xffff);
902 
903 	ctrl = ioread32(&bp->tod->ctrl);
904 	idx = ctrl & TOD_CTRL_PROTOCOL ? 4 : 0;
905 	idx += (ctrl >> 16) & 3;
906 	dev_info(&bp->pdev->dev, "control: %x\n", ctrl);
907 	dev_info(&bp->pdev->dev, "TOD Protocol %s %s\n", proto_name[idx],
908 		 ctrl & TOD_CTRL_ENABLE ? "enabled" : "");
909 
910 	idx = (ctrl >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
911 	if (idx < ARRAY_SIZE(gnss_name))
912 		dev_info(&bp->pdev->dev, "GNSS %s\n", gnss_name[idx]);
913 
914 	reg = ioread32(&bp->tod->status);
915 	dev_info(&bp->pdev->dev, "status: %x\n", reg);
916 
917 	reg = ioread32(&bp->tod->adj_sec);
918 	dev_info(&bp->pdev->dev, "correction: %d\n", reg);
919 
920 	reg = ioread32(&bp->tod->utc_status);
921 	dev_info(&bp->pdev->dev, "utc_status: %x\n", reg);
922 	dev_info(&bp->pdev->dev, "utc_offset: %d  valid:%d  leap_valid:%d\n",
923 		 reg & TOD_STATUS_UTC_MASK, reg & TOD_STATUS_UTC_VALID ? 1 : 0,
924 		 reg & TOD_STATUS_LEAP_VALID ? 1 : 0);
925 }
926 
927 static int
928 ptp_ocp_firstchild(struct device *dev, void *data)
929 {
930 	return 1;
931 }
932 
933 static int
934 ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data)
935 {
936 	struct i2c_msg msgs[2] = {
937 		{
938 			.addr = addr,
939 			.len = 1,
940 			.buf = &reg,
941 		},
942 		{
943 			.addr = addr,
944 			.flags = I2C_M_RD,
945 			.len = 2,
946 			.buf = data,
947 		},
948 	};
949 	int err;
950 	u8 len;
951 
952 	/* xiic-i2c for some stupid reason only does 2 byte reads. */
953 	while (sz) {
954 		len = min_t(u8, sz, 2);
955 		msgs[1].len = len;
956 		err = i2c_transfer(adap, msgs, 2);
957 		if (err != msgs[1].len)
958 			return err;
959 		msgs[1].buf += len;
960 		reg += len;
961 		sz -= len;
962 	}
963 	return 0;
964 }
965 
966 static void
967 ptp_ocp_get_serial_number(struct ptp_ocp *bp)
968 {
969 	struct i2c_adapter *adap;
970 	struct device *dev;
971 	int err;
972 
973 	if (!bp->i2c_ctrl)
974 		return;
975 
976 	dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild);
977 	if (!dev) {
978 		dev_err(&bp->pdev->dev, "Can't find I2C adapter\n");
979 		return;
980 	}
981 
982 	adap = i2c_verify_adapter(dev);
983 	if (!adap) {
984 		dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n",
985 			dev_name(dev));
986 		goto out;
987 	}
988 
989 	err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial);
990 	if (err) {
991 		dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err);
992 		goto out;
993 	}
994 
995 	bp->has_serial = true;
996 
997 out:
998 	put_device(dev);
999 }
1000 
1001 static struct device *
1002 ptp_ocp_find_flash(struct ptp_ocp *bp)
1003 {
1004 	struct device *dev, *last;
1005 
1006 	last = NULL;
1007 	dev = &bp->spi_flash->dev;
1008 
1009 	while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) {
1010 		if (!strcmp("mtd", dev_bus_name(dev)))
1011 			break;
1012 		put_device(last);
1013 		last = dev;
1014 	}
1015 	put_device(last);
1016 
1017 	return dev;
1018 }
1019 
1020 static int
1021 ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1022 		      const struct firmware *fw)
1023 {
1024 	struct mtd_info *mtd = dev_get_drvdata(dev);
1025 	struct ptp_ocp *bp = devlink_priv(devlink);
1026 	size_t off, len, resid, wrote;
1027 	struct erase_info erase;
1028 	size_t base, blksz;
1029 	int err = 0;
1030 
1031 	off = 0;
1032 	base = bp->flash_start;
1033 	blksz = 4096;
1034 	resid = fw->size;
1035 
1036 	while (resid) {
1037 		devlink_flash_update_status_notify(devlink, "Flashing",
1038 						   NULL, off, fw->size);
1039 
1040 		len = min_t(size_t, resid, blksz);
1041 		erase.addr = base + off;
1042 		erase.len = blksz;
1043 
1044 		err = mtd_erase(mtd, &erase);
1045 		if (err)
1046 			goto out;
1047 
1048 		err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]);
1049 		if (err)
1050 			goto out;
1051 
1052 		off += blksz;
1053 		resid -= len;
1054 	}
1055 out:
1056 	return err;
1057 }
1058 
1059 static int
1060 ptp_ocp_devlink_flash_update(struct devlink *devlink,
1061 			     struct devlink_flash_update_params *params,
1062 			     struct netlink_ext_ack *extack)
1063 {
1064 	struct ptp_ocp *bp = devlink_priv(devlink);
1065 	struct device *dev;
1066 	const char *msg;
1067 	int err;
1068 
1069 	dev = ptp_ocp_find_flash(bp);
1070 	if (!dev) {
1071 		dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1072 		return -ENODEV;
1073 	}
1074 
1075 	devlink_flash_update_status_notify(devlink, "Preparing to flash",
1076 					   NULL, 0, 0);
1077 
1078 	err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1079 
1080 	msg = err ? "Flash error" : "Flash complete";
1081 	devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1082 
1083 	put_device(dev);
1084 	return err;
1085 }
1086 
1087 static int
1088 ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1089 			 struct netlink_ext_ack *extack)
1090 {
1091 	struct ptp_ocp *bp = devlink_priv(devlink);
1092 	char buf[32];
1093 	int err;
1094 
1095 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
1096 	if (err)
1097 		return err;
1098 
1099 	if (bp->image) {
1100 		u32 ver = ioread32(&bp->image->version);
1101 
1102 		if (ver & 0xffff) {
1103 			sprintf(buf, "%d", ver);
1104 			err = devlink_info_version_running_put(req,
1105 							       "fw",
1106 							       buf);
1107 		} else {
1108 			sprintf(buf, "%d", ver >> 16);
1109 			err = devlink_info_version_running_put(req,
1110 							       "loader",
1111 							       buf);
1112 		}
1113 		if (err)
1114 			return err;
1115 	}
1116 
1117 	if (!bp->has_serial)
1118 		ptp_ocp_get_serial_number(bp);
1119 
1120 	if (bp->has_serial) {
1121 		sprintf(buf, "%pM", bp->serial);
1122 		err = devlink_info_serial_number_put(req, buf);
1123 		if (err)
1124 			return err;
1125 	}
1126 
1127 	return 0;
1128 }
1129 
1130 static const struct devlink_ops ptp_ocp_devlink_ops = {
1131 	.flash_update = ptp_ocp_devlink_flash_update,
1132 	.info_get = ptp_ocp_devlink_info_get,
1133 };
1134 
1135 static void __iomem *
1136 __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size)
1137 {
1138 	struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1139 
1140 	return devm_ioremap_resource(&bp->pdev->dev, &res);
1141 }
1142 
1143 static void __iomem *
1144 ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1145 {
1146 	unsigned long start;
1147 
1148 	start = pci_resource_start(bp->pdev, 0) + r->offset;
1149 	return __ptp_ocp_get_mem(bp, start, r->size);
1150 }
1151 
1152 static void
1153 ptp_ocp_set_irq_resource(struct resource *res, int irq)
1154 {
1155 	struct resource r = DEFINE_RES_IRQ(irq);
1156 	*res = r;
1157 }
1158 
1159 static void
1160 ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size)
1161 {
1162 	struct resource r = DEFINE_RES_MEM(start, size);
1163 	*res = r;
1164 }
1165 
1166 static int
1167 ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1168 {
1169 	struct ptp_ocp_flash_info *info;
1170 	struct pci_dev *pdev = bp->pdev;
1171 	struct platform_device *p;
1172 	struct resource res[2];
1173 	unsigned long start;
1174 	int id;
1175 
1176 	start = pci_resource_start(pdev, 0) + r->offset;
1177 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
1178 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1179 
1180 	info = r->extra;
1181 	id = pci_dev_id(pdev) << 1;
1182 	id += info->pci_offset;
1183 
1184 	p = platform_device_register_resndata(&pdev->dev, info->name, id,
1185 					      res, 2, info->data,
1186 					      info->data_size);
1187 	if (IS_ERR(p))
1188 		return PTR_ERR(p);
1189 
1190 	bp_assign_entry(bp, r, p);
1191 
1192 	return 0;
1193 }
1194 
1195 static struct platform_device *
1196 ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1197 {
1198 	struct ptp_ocp_i2c_info *info;
1199 	struct resource res[2];
1200 	unsigned long start;
1201 
1202 	info = r->extra;
1203 	start = pci_resource_start(pdev, 0) + r->offset;
1204 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
1205 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1206 
1207 	return platform_device_register_resndata(&pdev->dev, info->name,
1208 						 id, res, 2,
1209 						 info->data, info->data_size);
1210 }
1211 
1212 static int
1213 ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1214 {
1215 	struct pci_dev *pdev = bp->pdev;
1216 	struct ptp_ocp_i2c_info *info;
1217 	struct platform_device *p;
1218 	struct clk_hw *clk;
1219 	char buf[32];
1220 	int id;
1221 
1222 	info = r->extra;
1223 	id = pci_dev_id(bp->pdev);
1224 
1225 	sprintf(buf, "AXI.%d", id);
1226 	clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
1227 					 info->fixed_rate);
1228 	if (IS_ERR(clk))
1229 		return PTR_ERR(clk);
1230 	bp->i2c_clk = clk;
1231 
1232 	sprintf(buf, "%s.%d", info->name, id);
1233 	devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1234 	p = ptp_ocp_i2c_bus(bp->pdev, r, id);
1235 	if (IS_ERR(p))
1236 		return PTR_ERR(p);
1237 
1238 	bp_assign_entry(bp, r, p);
1239 
1240 	return 0;
1241 }
1242 
1243 static irqreturn_t
1244 ptp_ocp_ts_irq(int irq, void *priv)
1245 {
1246 	struct ptp_ocp_ext_src *ext = priv;
1247 	struct ts_reg __iomem *reg = ext->mem;
1248 	struct ptp_clock_event ev;
1249 	u32 sec, nsec;
1250 
1251 	if (ext == ext->bp->pps) {
1252 		if (ext->bp->pps_req_map & OCP_REQ_PPS) {
1253 			ev.type = PTP_CLOCK_PPS;
1254 			ptp_clock_event(ext->bp->ptp, &ev);
1255 		}
1256 
1257 		if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
1258 			goto out;
1259 	}
1260 
1261 	/* XXX should fix API - this converts s/ns -> ts -> s/ns */
1262 	sec = ioread32(&reg->time_sec);
1263 	nsec = ioread32(&reg->time_ns);
1264 
1265 	ev.type = PTP_CLOCK_EXTTS;
1266 	ev.index = ext->info->index;
1267 	ev.timestamp = sec * NSEC_PER_SEC + nsec;
1268 
1269 	ptp_clock_event(ext->bp->ptp, &ev);
1270 
1271 out:
1272 	iowrite32(1, &reg->intr);	/* write 1 to ack */
1273 
1274 	return IRQ_HANDLED;
1275 }
1276 
1277 static int
1278 ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
1279 {
1280 	struct ptp_ocp_ext_src *ext = priv;
1281 	struct ts_reg __iomem *reg = ext->mem;
1282 	struct ptp_ocp *bp = ext->bp;
1283 
1284 	if (ext == bp->pps) {
1285 		u32 old_map = bp->pps_req_map;
1286 
1287 		if (enable)
1288 			bp->pps_req_map |= req;
1289 		else
1290 			bp->pps_req_map &= ~req;
1291 
1292 		/* if no state change, just return */
1293 		if ((!!old_map ^ !!bp->pps_req_map) == 0)
1294 			return 0;
1295 	}
1296 
1297 	if (enable) {
1298 		iowrite32(1, &reg->enable);
1299 		iowrite32(1, &reg->intr_mask);
1300 		iowrite32(1, &reg->intr);
1301 	} else {
1302 		iowrite32(0, &reg->intr_mask);
1303 		iowrite32(0, &reg->enable);
1304 	}
1305 
1306 	return 0;
1307 }
1308 
1309 static void
1310 ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1311 {
1312 	ext->info->enable(ext, ~0, false);
1313 	pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1314 	kfree(ext);
1315 }
1316 
1317 static int
1318 ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
1319 {
1320 	struct pci_dev *pdev = bp->pdev;
1321 	struct ptp_ocp_ext_src *ext;
1322 	int err;
1323 
1324 	ext = kzalloc(sizeof(*ext), GFP_KERNEL);
1325 	if (!ext)
1326 		return -ENOMEM;
1327 
1328 	ext->mem = ptp_ocp_get_mem(bp, r);
1329 	if (IS_ERR(ext->mem)) {
1330 		err = PTR_ERR(ext->mem);
1331 		goto out;
1332 	}
1333 
1334 	ext->bp = bp;
1335 	ext->info = r->extra;
1336 	ext->irq_vec = r->irq_vec;
1337 
1338 	err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
1339 			      ext, "ocp%d.%s", bp->id, r->name);
1340 	if (err) {
1341 		dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
1342 		goto out;
1343 	}
1344 
1345 	bp_assign_entry(bp, r, ext);
1346 
1347 	return 0;
1348 
1349 out:
1350 	kfree(ext);
1351 	return err;
1352 }
1353 
1354 static int
1355 ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
1356 {
1357 	struct pci_dev *pdev = bp->pdev;
1358 	struct uart_8250_port uart;
1359 
1360 	/* Setting UPF_IOREMAP and leaving port.membase unspecified lets
1361 	 * the serial port device claim and release the pci resource.
1362 	 */
1363 	memset(&uart, 0, sizeof(uart));
1364 	uart.port.dev = &pdev->dev;
1365 	uart.port.iotype = UPIO_MEM;
1366 	uart.port.regshift = 2;
1367 	uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
1368 	uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
1369 	uart.port.uartclk = 50000000;
1370 	uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP;
1371 	uart.port.type = PORT_16550A;
1372 
1373 	return serial8250_register_8250_port(&uart);
1374 }
1375 
1376 static int
1377 ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
1378 {
1379 	int port;
1380 
1381 	port = ptp_ocp_serial_line(bp, r);
1382 	if (port < 0)
1383 		return port;
1384 
1385 	bp_assign_entry(bp, r, port);
1386 
1387 	return 0;
1388 }
1389 
1390 static int
1391 ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1392 {
1393 	void __iomem *mem;
1394 
1395 	mem = ptp_ocp_get_mem(bp, r);
1396 	if (IS_ERR(mem))
1397 		return PTR_ERR(mem);
1398 
1399 	bp_assign_entry(bp, r, mem);
1400 
1401 	return 0;
1402 }
1403 
1404 static void
1405 ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
1406 {
1407 	if (!bp->nmea_out)
1408 		return;
1409 
1410 	iowrite32(0, &bp->nmea_out->ctrl);		/* disable */
1411 	iowrite32(7, &bp->nmea_out->uart_baud);		/* 115200 */
1412 	iowrite32(1, &bp->nmea_out->ctrl);		/* enable */
1413 }
1414 
1415 /* FB specific board initializers; last "resource" registered. */
1416 static int
1417 ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
1418 {
1419 	bp->flash_start = 1024 * 4096;
1420 
1421 	ptp_ocp_tod_init(bp);
1422 	ptp_ocp_nmea_out_init(bp);
1423 
1424 	return ptp_ocp_init_clock(bp);
1425 }
1426 
1427 static bool
1428 ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
1429 {
1430 	bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
1431 
1432 	if (!allow)
1433 		dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
1434 			r->irq_vec, r->name);
1435 	return allow;
1436 }
1437 
1438 static int
1439 ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
1440 {
1441 	struct ocp_resource *r, *table;
1442 	int err = 0;
1443 
1444 	table = (struct ocp_resource *)driver_data;
1445 	for (r = table; r->setup; r++) {
1446 		if (!ptp_ocp_allow_irq(bp, r))
1447 			continue;
1448 		err = r->setup(bp, r);
1449 		if (err) {
1450 			dev_err(&bp->pdev->dev,
1451 				"Could not register %s: err %d\n",
1452 				r->name, err);
1453 			break;
1454 		}
1455 	}
1456 	return err;
1457 }
1458 
1459 static void
1460 ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
1461 {
1462 	u32 ctrl;
1463 	bool on;
1464 
1465 	ctrl = ioread32(reg);
1466 	on = ctrl & bit;
1467 	if (on ^ enable) {
1468 		ctrl &= ~bit;
1469 		ctrl |= enable ? bit : 0;
1470 		iowrite32(ctrl, reg);
1471 	}
1472 }
1473 
1474 static void
1475 ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
1476 {
1477 	return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
1478 				   IRIG_M_CTRL_ENABLE, enable);
1479 }
1480 
1481 static void
1482 ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
1483 {
1484 	return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
1485 				   IRIG_S_CTRL_ENABLE, enable);
1486 }
1487 
1488 static void
1489 ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
1490 {
1491 	return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
1492 				   DCF_M_CTRL_ENABLE, enable);
1493 }
1494 
1495 static void
1496 ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
1497 {
1498 	return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
1499 				   DCF_S_CTRL_ENABLE, enable);
1500 }
1501 
1502 static void
1503 __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
1504 {
1505 	ptp_ocp_irig_out(bp, val & 0x00100010);
1506 	ptp_ocp_dcf_out(bp, val & 0x00200020);
1507 }
1508 
1509 static void
1510 __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
1511 {
1512 	ptp_ocp_irig_in(bp, val & 0x00100010);
1513 	ptp_ocp_dcf_in(bp, val & 0x00200020);
1514 }
1515 
1516 /*
1517  * ANT0 == gps	(in)
1518  * ANT1 == sma1 (in)
1519  * ANT2 == sma2 (in)
1520  * ANT3 == sma3 (out)
1521  * ANT4 == sma4 (out)
1522  */
1523 
1524 enum ptp_ocp_sma_mode {
1525 	SMA_MODE_IN,
1526 	SMA_MODE_OUT,
1527 };
1528 
1529 static struct ptp_ocp_sma_connector {
1530 	enum	ptp_ocp_sma_mode mode;
1531 	bool	fixed_mode;
1532 	u16	default_out_idx;
1533 } ptp_ocp_sma_map[4] = {
1534 	{
1535 		.mode = SMA_MODE_IN,
1536 		.fixed_mode = true,
1537 	},
1538 	{
1539 		.mode = SMA_MODE_IN,
1540 		.fixed_mode = true,
1541 	},
1542 	{
1543 		.mode = SMA_MODE_OUT,
1544 		.fixed_mode = true,
1545 		.default_out_idx = 0,		/* 10Mhz */
1546 	},
1547 	{
1548 		.mode = SMA_MODE_OUT,
1549 		.fixed_mode = true,
1550 		.default_out_idx = 1,		/* PHC */
1551 	},
1552 };
1553 
1554 static ssize_t
1555 ptp_ocp_show_output(u32 val, char *buf, int default_idx)
1556 {
1557 	const char *name;
1558 	ssize_t count;
1559 
1560 	count = sysfs_emit(buf, "OUT: ");
1561 	name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val);
1562 	if (!name)
1563 		name = ptp_ocp_sma_out[default_idx].name;
1564 	count += sysfs_emit_at(buf, count, "%s\n", name);
1565 	return count;
1566 }
1567 
1568 static ssize_t
1569 ptp_ocp_show_inputs(u32 val, char *buf, const char *zero_in)
1570 {
1571 	const char *name;
1572 	ssize_t count;
1573 	int i;
1574 
1575 	count = sysfs_emit(buf, "IN: ");
1576 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) {
1577 		if (val & ptp_ocp_sma_in[i].value) {
1578 			name = ptp_ocp_sma_in[i].name;
1579 			count += sysfs_emit_at(buf, count, "%s ", name);
1580 		}
1581 	}
1582 	if (!val && zero_in)
1583 		count += sysfs_emit_at(buf, count, "%s ", zero_in);
1584 	if (count)
1585 		count--;
1586 	count += sysfs_emit_at(buf, count, "\n");
1587 	return count;
1588 }
1589 
1590 static int
1591 sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode)
1592 {
1593 	struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out };
1594 	int idx, count, dir;
1595 	char **argv;
1596 	int ret;
1597 
1598 	argv = argv_split(GFP_KERNEL, buf, &count);
1599 	if (!argv)
1600 		return -ENOMEM;
1601 
1602 	ret = -EINVAL;
1603 	if (!count)
1604 		goto out;
1605 
1606 	idx = 0;
1607 	dir = *mode == SMA_MODE_IN ? 0 : 1;
1608 	if (!strcasecmp("IN:", argv[idx])) {
1609 		dir = 0;
1610 		idx++;
1611 	}
1612 	if (!strcasecmp("OUT:", argv[0])) {
1613 		dir = 1;
1614 		idx++;
1615 	}
1616 	*mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
1617 
1618 	ret = 0;
1619 	for (; idx < count; idx++)
1620 		ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
1621 	if (ret < 0)
1622 		ret = -EINVAL;
1623 
1624 out:
1625 	argv_free(argv);
1626 	return ret;
1627 }
1628 
1629 static ssize_t
1630 ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, u32 val, char *buf,
1631 		 const char *zero_in)
1632 {
1633 	struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1];
1634 
1635 	if (sma->mode == SMA_MODE_IN)
1636 		return ptp_ocp_show_inputs(val, buf, zero_in);
1637 
1638 	return ptp_ocp_show_output(val, buf, sma->default_out_idx);
1639 }
1640 
1641 static ssize_t
1642 sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
1643 {
1644 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1645 	u32 val;
1646 
1647 	val = ioread32(&bp->sma->gpio1) & 0x3f;
1648 	return ptp_ocp_sma_show(bp, 1, val, buf, ptp_ocp_sma_in[0].name);
1649 }
1650 
1651 static ssize_t
1652 sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
1653 {
1654 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1655 	u32 val;
1656 
1657 	val = (ioread32(&bp->sma->gpio1) >> 16) & 0x3f;
1658 	return ptp_ocp_sma_show(bp, 2, val, buf, NULL);
1659 }
1660 
1661 static ssize_t
1662 sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
1663 {
1664 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1665 	u32 val;
1666 
1667 	val = ioread32(&bp->sma->gpio2) & 0x3f;
1668 	return ptp_ocp_sma_show(bp, 3, val, buf, NULL);
1669 }
1670 
1671 static ssize_t
1672 sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
1673 {
1674 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1675 	u32 val;
1676 
1677 	val = (ioread32(&bp->sma->gpio2) >> 16) & 0x3f;
1678 	return ptp_ocp_sma_show(bp, 4, val, buf, NULL);
1679 }
1680 
1681 static void
1682 ptp_ocp_sma_store_output(struct ptp_ocp *bp, u32 val, u32 shift)
1683 {
1684 	unsigned long flags;
1685 	u32 gpio, mask;
1686 
1687 	mask = 0xffff << (16 - shift);
1688 
1689 	spin_lock_irqsave(&bp->lock, flags);
1690 
1691 	gpio = ioread32(&bp->sma->gpio2);
1692 	gpio = (gpio & mask) | (val << shift);
1693 
1694 	__handle_signal_outputs(bp, gpio);
1695 
1696 	iowrite32(gpio, &bp->sma->gpio2);
1697 
1698 	spin_unlock_irqrestore(&bp->lock, flags);
1699 }
1700 
1701 static void
1702 ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, u32 val, u32 shift)
1703 {
1704 	unsigned long flags;
1705 	u32 gpio, mask;
1706 
1707 	mask = 0xffff << (16 - shift);
1708 
1709 	spin_lock_irqsave(&bp->lock, flags);
1710 
1711 	gpio = ioread32(&bp->sma->gpio1);
1712 	gpio = (gpio & mask) | (val << shift);
1713 
1714 	__handle_signal_inputs(bp, gpio);
1715 
1716 	iowrite32(gpio, &bp->sma->gpio1);
1717 
1718 	spin_unlock_irqrestore(&bp->lock, flags);
1719 }
1720 
1721 static ssize_t
1722 ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr, u32 shift)
1723 {
1724 	struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1];
1725 	enum ptp_ocp_sma_mode mode;
1726 	int val;
1727 
1728 	mode = sma->mode;
1729 	val = sma_parse_inputs(buf, &mode);
1730 	if (val < 0)
1731 		return val;
1732 
1733 	if (mode != sma->mode && sma->fixed_mode)
1734 		return -EOPNOTSUPP;
1735 
1736 	if (mode != sma->mode) {
1737 		pr_err("Mode changes not supported yet.\n");
1738 		return -EOPNOTSUPP;
1739 	}
1740 
1741 	if (sma->mode == SMA_MODE_IN)
1742 		ptp_ocp_sma_store_inputs(bp, val, shift);
1743 	else
1744 		ptp_ocp_sma_store_output(bp, val, shift);
1745 
1746 	return 0;
1747 }
1748 
1749 static ssize_t
1750 sma1_store(struct device *dev, struct device_attribute *attr,
1751 	   const char *buf, size_t count)
1752 {
1753 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1754 	int err;
1755 
1756 	err = ptp_ocp_sma_store(bp, buf, 1, 0);
1757 	return err ? err : count;
1758 }
1759 
1760 static ssize_t
1761 sma2_store(struct device *dev, struct device_attribute *attr,
1762 	   const char *buf, size_t count)
1763 {
1764 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1765 	int err;
1766 
1767 	err = ptp_ocp_sma_store(bp, buf, 2, 16);
1768 	return err ? err : count;
1769 }
1770 
1771 static ssize_t
1772 sma3_store(struct device *dev, struct device_attribute *attr,
1773 	   const char *buf, size_t count)
1774 {
1775 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1776 	int err;
1777 
1778 	err = ptp_ocp_sma_store(bp, buf, 3, 0);
1779 	return err ? err : count;
1780 }
1781 
1782 static ssize_t
1783 sma4_store(struct device *dev, struct device_attribute *attr,
1784 	   const char *buf, size_t count)
1785 {
1786 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1787 	int err;
1788 
1789 	err = ptp_ocp_sma_store(bp, buf, 4, 16);
1790 	return err ? err : count;
1791 }
1792 static DEVICE_ATTR_RW(sma1);
1793 static DEVICE_ATTR_RW(sma2);
1794 static DEVICE_ATTR_RW(sma3);
1795 static DEVICE_ATTR_RW(sma4);
1796 
1797 static ssize_t
1798 available_sma_inputs_show(struct device *dev,
1799 			  struct device_attribute *attr, char *buf)
1800 {
1801 	return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf);
1802 }
1803 static DEVICE_ATTR_RO(available_sma_inputs);
1804 
1805 static ssize_t
1806 available_sma_outputs_show(struct device *dev,
1807 			   struct device_attribute *attr, char *buf)
1808 {
1809 	return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf);
1810 }
1811 static DEVICE_ATTR_RO(available_sma_outputs);
1812 
1813 static ssize_t
1814 serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
1815 {
1816 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1817 
1818 	if (!bp->has_serial)
1819 		ptp_ocp_get_serial_number(bp);
1820 
1821 	return sysfs_emit(buf, "%pM\n", bp->serial);
1822 }
1823 static DEVICE_ATTR_RO(serialnum);
1824 
1825 static ssize_t
1826 gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
1827 {
1828 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1829 	ssize_t ret;
1830 
1831 	if (bp->gnss_lost)
1832 		ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
1833 	else
1834 		ret = sysfs_emit(buf, "SYNC\n");
1835 
1836 	return ret;
1837 }
1838 static DEVICE_ATTR_RO(gnss_sync);
1839 
1840 static ssize_t
1841 utc_tai_offset_show(struct device *dev,
1842 		    struct device_attribute *attr, char *buf)
1843 {
1844 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1845 
1846 	return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
1847 }
1848 
1849 static ssize_t
1850 utc_tai_offset_store(struct device *dev,
1851 		     struct device_attribute *attr,
1852 		     const char *buf, size_t count)
1853 {
1854 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1855 	int err;
1856 	u32 val;
1857 
1858 	err = kstrtou32(buf, 0, &val);
1859 	if (err)
1860 		return err;
1861 
1862 	ptp_ocp_utc_distribute(bp, val);
1863 
1864 	return count;
1865 }
1866 static DEVICE_ATTR_RW(utc_tai_offset);
1867 
1868 static ssize_t
1869 ts_window_adjust_show(struct device *dev,
1870 		      struct device_attribute *attr, char *buf)
1871 {
1872 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1873 
1874 	return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
1875 }
1876 
1877 static ssize_t
1878 ts_window_adjust_store(struct device *dev,
1879 		       struct device_attribute *attr,
1880 		       const char *buf, size_t count)
1881 {
1882 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1883 	int err;
1884 	u32 val;
1885 
1886 	err = kstrtou32(buf, 0, &val);
1887 	if (err)
1888 		return err;
1889 
1890 	bp->ts_window_adjust = val;
1891 
1892 	return count;
1893 }
1894 static DEVICE_ATTR_RW(ts_window_adjust);
1895 
1896 static ssize_t
1897 irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1898 {
1899 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1900 	u32 val;
1901 
1902 	val = ioread32(&bp->irig_out->ctrl);
1903 	val = (val >> 16) & 0x07;
1904 	return sysfs_emit(buf, "%d\n", val);
1905 }
1906 
1907 static ssize_t
1908 irig_b_mode_store(struct device *dev,
1909 		  struct device_attribute *attr,
1910 		  const char *buf, size_t count)
1911 {
1912 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1913 	unsigned long flags;
1914 	int err;
1915 	u32 reg;
1916 	u8 val;
1917 
1918 	err = kstrtou8(buf, 0, &val);
1919 	if (err)
1920 		return err;
1921 	if (val > 7)
1922 		return -EINVAL;
1923 
1924 	reg = ((val & 0x7) << 16);
1925 
1926 	spin_lock_irqsave(&bp->lock, flags);
1927 	iowrite32(0, &bp->irig_out->ctrl);		/* disable */
1928 	iowrite32(reg, &bp->irig_out->ctrl);		/* change mode */
1929 	iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
1930 	spin_unlock_irqrestore(&bp->lock, flags);
1931 
1932 	return count;
1933 }
1934 static DEVICE_ATTR_RW(irig_b_mode);
1935 
1936 static ssize_t
1937 clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
1938 {
1939 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1940 	const char *p;
1941 	u32 select;
1942 
1943 	select = ioread32(&bp->reg->select);
1944 	p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
1945 
1946 	return sysfs_emit(buf, "%s\n", p);
1947 }
1948 
1949 static ssize_t
1950 clock_source_store(struct device *dev, struct device_attribute *attr,
1951 		   const char *buf, size_t count)
1952 {
1953 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1954 	unsigned long flags;
1955 	int val;
1956 
1957 	val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
1958 	if (val < 0)
1959 		return val;
1960 
1961 	spin_lock_irqsave(&bp->lock, flags);
1962 	iowrite32(val, &bp->reg->select);
1963 	spin_unlock_irqrestore(&bp->lock, flags);
1964 
1965 	return count;
1966 }
1967 static DEVICE_ATTR_RW(clock_source);
1968 
1969 static ssize_t
1970 available_clock_sources_show(struct device *dev,
1971 			     struct device_attribute *attr, char *buf)
1972 {
1973 	return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
1974 }
1975 static DEVICE_ATTR_RO(available_clock_sources);
1976 
1977 static struct attribute *timecard_attrs[] = {
1978 	&dev_attr_serialnum.attr,
1979 	&dev_attr_gnss_sync.attr,
1980 	&dev_attr_clock_source.attr,
1981 	&dev_attr_available_clock_sources.attr,
1982 	&dev_attr_sma1.attr,
1983 	&dev_attr_sma2.attr,
1984 	&dev_attr_sma3.attr,
1985 	&dev_attr_sma4.attr,
1986 	&dev_attr_available_sma_inputs.attr,
1987 	&dev_attr_available_sma_outputs.attr,
1988 	&dev_attr_irig_b_mode.attr,
1989 	&dev_attr_utc_tai_offset.attr,
1990 	&dev_attr_ts_window_adjust.attr,
1991 	NULL,
1992 };
1993 ATTRIBUTE_GROUPS(timecard);
1994 
1995 static const char *
1996 gpio_map(u32 gpio, u32 bit, const char *pri, const char *sec, const char *def)
1997 {
1998 	const char *ans;
1999 
2000 	if (gpio & (1 << bit))
2001 		ans = pri;
2002 	else if (gpio & (1 << (bit + 16)))
2003 		ans = sec;
2004 	else
2005 		ans = def;
2006 	return ans;
2007 }
2008 
2009 static void
2010 gpio_multi_map(char *buf, u32 gpio, u32 bit,
2011 	       const char *pri, const char *sec, const char *def)
2012 {
2013 	char *ans = buf;
2014 
2015 	strcpy(ans, def);
2016 	if (gpio & (1 << bit))
2017 		ans += sprintf(ans, "%s ", pri);
2018 	if (gpio & (1 << (bit + 16)))
2019 		ans += sprintf(ans, "%s ", sec);
2020 }
2021 
2022 static int
2023 ptp_ocp_summary_show(struct seq_file *s, void *data)
2024 {
2025 	struct device *dev = s->private;
2026 	struct ptp_system_timestamp sts;
2027 	u32 sma_in, sma_out, ctrl, val;
2028 	struct ts_reg __iomem *ts_reg;
2029 	struct timespec64 ts;
2030 	struct ptp_ocp *bp;
2031 	const char *src;
2032 	bool on, map;
2033 	char *buf;
2034 
2035 	buf = (char *)__get_free_page(GFP_KERNEL);
2036 	if (!buf)
2037 		return -ENOMEM;
2038 
2039 	bp = dev_get_drvdata(dev);
2040 	sma_in = ioread32(&bp->sma->gpio1);
2041 	sma_out = ioread32(&bp->sma->gpio2);
2042 
2043 	seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
2044 
2045 	sma1_show(dev, NULL, buf);
2046 	seq_printf(s, "   sma1: %s", buf);
2047 
2048 	sma2_show(dev, NULL, buf);
2049 	seq_printf(s, "   sma2: %s", buf);
2050 
2051 	sma3_show(dev, NULL, buf);
2052 	seq_printf(s, "   sma3: %s", buf);
2053 
2054 	sma4_show(dev, NULL, buf);
2055 	seq_printf(s, "   sma4: %s", buf);
2056 
2057 	if (bp->ts0) {
2058 		ts_reg = bp->ts0->mem;
2059 		on = ioread32(&ts_reg->enable);
2060 		src = "GNSS";
2061 		seq_printf(s, "%7s: %s, src: %s\n", "TS0",
2062 			   on ? " ON" : "OFF", src);
2063 	}
2064 
2065 	if (bp->ts1) {
2066 		ts_reg = bp->ts1->mem;
2067 		on = ioread32(&ts_reg->enable);
2068 		src = gpio_map(sma_in, 2, "sma1", "sma2", "----");
2069 		seq_printf(s, "%7s: %s, src: %s\n", "TS1",
2070 			   on ? " ON" : "OFF", src);
2071 	}
2072 
2073 	if (bp->ts2) {
2074 		ts_reg = bp->ts2->mem;
2075 		on = ioread32(&ts_reg->enable);
2076 		src = gpio_map(sma_in, 3, "sma1", "sma2", "----");
2077 		seq_printf(s, "%7s: %s, src: %s\n", "TS2",
2078 			   on ? " ON" : "OFF", src);
2079 	}
2080 
2081 	if (bp->pps) {
2082 		ts_reg = bp->pps->mem;
2083 		src = "PHC";
2084 		on = ioread32(&ts_reg->enable);
2085 		map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
2086 		seq_printf(s, "%7s: %s, src: %s\n", "TS3",
2087 			   on && map ? " ON" : "OFF", src);
2088 
2089 		map = !!(bp->pps_req_map & OCP_REQ_PPS);
2090 		seq_printf(s, "%7s: %s, src: %s\n", "PPS",
2091 			   on && map ? " ON" : "OFF", src);
2092 	}
2093 
2094 	if (bp->irig_out) {
2095 		ctrl = ioread32(&bp->irig_out->ctrl);
2096 		on = ctrl & IRIG_M_CTRL_ENABLE;
2097 		val = ioread32(&bp->irig_out->status);
2098 		gpio_multi_map(buf, sma_out, 4, "sma3", "sma4", "----");
2099 		seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
2100 			   on ? " ON" : "OFF", val, (ctrl >> 16), buf);
2101 	}
2102 
2103 	if (bp->irig_in) {
2104 		on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
2105 		val = ioread32(&bp->irig_in->status);
2106 		src = gpio_map(sma_in, 4, "sma1", "sma2", "----");
2107 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
2108 			   on ? " ON" : "OFF", val, src);
2109 	}
2110 
2111 	if (bp->dcf_out) {
2112 		on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
2113 		val = ioread32(&bp->dcf_out->status);
2114 		gpio_multi_map(buf, sma_out, 5, "sma3", "sma4", "----");
2115 		seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
2116 			   on ? " ON" : "OFF", val, buf);
2117 	}
2118 
2119 	if (bp->dcf_in) {
2120 		on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
2121 		val = ioread32(&bp->dcf_in->status);
2122 		src = gpio_map(sma_in, 5, "sma1", "sma2", "----");
2123 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
2124 			   on ? " ON" : "OFF", val, src);
2125 	}
2126 
2127 	if (bp->nmea_out) {
2128 		on = ioread32(&bp->nmea_out->ctrl) & 1;
2129 		val = ioread32(&bp->nmea_out->status);
2130 		seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
2131 			   on ? " ON" : "OFF", val);
2132 	}
2133 
2134 	/* compute src for PPS1, used below. */
2135 	if (bp->pps_select) {
2136 		val = ioread32(&bp->pps_select->gpio1);
2137 		if (val & 0x01)
2138 			src = gpio_map(sma_in, 0, "sma1", "sma2", "----");
2139 		else if (val & 0x02)
2140 			src = "MAC";
2141 		else if (val & 0x04)
2142 			src = "GNSS";
2143 		else
2144 			src = "----";
2145 	} else {
2146 		src = "?";
2147 	}
2148 
2149 	/* assumes automatic switchover/selection */
2150 	val = ioread32(&bp->reg->select);
2151 	switch (val >> 16) {
2152 	case 0:
2153 		sprintf(buf, "----");
2154 		break;
2155 	case 2:
2156 		sprintf(buf, "IRIG");
2157 		break;
2158 	case 3:
2159 		sprintf(buf, "%s via PPS1", src);
2160 		break;
2161 	case 6:
2162 		sprintf(buf, "DCF");
2163 		break;
2164 	default:
2165 		strcpy(buf, "unknown");
2166 		break;
2167 	}
2168 	val = ioread32(&bp->reg->status);
2169 	seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
2170 		   val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced");
2171 
2172 	/* reuses PPS1 src from earlier */
2173 	seq_printf(s, "MAC PPS1 src: %s\n", src);
2174 
2175 	src = gpio_map(sma_in, 1, "sma1", "sma2", "GNSS2");
2176 	seq_printf(s, "MAC PPS2 src: %s\n", src);
2177 
2178 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
2179 		struct timespec64 sys_ts;
2180 		s64 pre_ns, post_ns, ns;
2181 
2182 		pre_ns = timespec64_to_ns(&sts.pre_ts);
2183 		post_ns = timespec64_to_ns(&sts.post_ts);
2184 		ns = (pre_ns + post_ns) / 2;
2185 		ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
2186 		sys_ts = ns_to_timespec64(ns);
2187 
2188 		seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC",
2189 			   ts.tv_sec, ts.tv_nsec, &ts);
2190 		seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS",
2191 			   sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts,
2192 			   bp->utc_tai_offset);
2193 		seq_printf(s, "%7s: PHC:SYS offset: %lld  window: %lld\n", "",
2194 			   timespec64_to_ns(&ts) - ns,
2195 			   post_ns - pre_ns);
2196 	}
2197 
2198 	free_page((unsigned long)buf);
2199 	return 0;
2200 }
2201 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
2202 
2203 static struct dentry *ptp_ocp_debugfs_root;
2204 
2205 static void
2206 ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
2207 {
2208 	struct dentry *d;
2209 
2210 	d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
2211 	bp->debug_root = d;
2212 	debugfs_create_file("summary", 0444, bp->debug_root,
2213 			    &bp->dev, &ptp_ocp_summary_fops);
2214 }
2215 
2216 static void
2217 ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
2218 {
2219 	debugfs_remove_recursive(bp->debug_root);
2220 }
2221 
2222 static void
2223 ptp_ocp_debugfs_init(void)
2224 {
2225 	ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
2226 }
2227 
2228 static void
2229 ptp_ocp_debugfs_fini(void)
2230 {
2231 	debugfs_remove_recursive(ptp_ocp_debugfs_root);
2232 }
2233 
2234 static void
2235 ptp_ocp_dev_release(struct device *dev)
2236 {
2237 	struct ptp_ocp *bp = dev_get_drvdata(dev);
2238 
2239 	mutex_lock(&ptp_ocp_lock);
2240 	idr_remove(&ptp_ocp_idr, bp->id);
2241 	mutex_unlock(&ptp_ocp_lock);
2242 }
2243 
2244 static int
2245 ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
2246 {
2247 	int err;
2248 
2249 	mutex_lock(&ptp_ocp_lock);
2250 	err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
2251 	mutex_unlock(&ptp_ocp_lock);
2252 	if (err < 0) {
2253 		dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
2254 		return err;
2255 	}
2256 	bp->id = err;
2257 
2258 	bp->ptp_info = ptp_ocp_clock_info;
2259 	spin_lock_init(&bp->lock);
2260 	bp->gnss_port = -1;
2261 	bp->gnss2_port = -1;
2262 	bp->mac_port = -1;
2263 	bp->nmea_port = -1;
2264 	bp->pdev = pdev;
2265 
2266 	device_initialize(&bp->dev);
2267 	dev_set_name(&bp->dev, "ocp%d", bp->id);
2268 	bp->dev.class = &timecard_class;
2269 	bp->dev.parent = &pdev->dev;
2270 	bp->dev.release = ptp_ocp_dev_release;
2271 	dev_set_drvdata(&bp->dev, bp);
2272 
2273 	err = device_add(&bp->dev);
2274 	if (err) {
2275 		dev_err(&bp->dev, "device add failed: %d\n", err);
2276 		goto out;
2277 	}
2278 
2279 	pci_set_drvdata(pdev, bp);
2280 
2281 	return 0;
2282 
2283 out:
2284 	ptp_ocp_dev_release(&bp->dev);
2285 	put_device(&bp->dev);
2286 	return err;
2287 }
2288 
2289 static void
2290 ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
2291 {
2292 	struct device *dev = &bp->dev;
2293 
2294 	if (sysfs_create_link(&dev->kobj, &child->kobj, link))
2295 		dev_err(dev, "%s symlink failed\n", link);
2296 }
2297 
2298 static void
2299 ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
2300 {
2301 	struct device *dev, *child;
2302 
2303 	dev = &bp->pdev->dev;
2304 
2305 	child = device_find_child_by_name(dev, name);
2306 	if (!child) {
2307 		dev_err(dev, "Could not find device %s\n", name);
2308 		return;
2309 	}
2310 
2311 	ptp_ocp_symlink(bp, child, link);
2312 	put_device(child);
2313 }
2314 
2315 static int
2316 ptp_ocp_complete(struct ptp_ocp *bp)
2317 {
2318 	struct pps_device *pps;
2319 	char buf[32];
2320 
2321 	if (bp->gnss_port != -1) {
2322 		sprintf(buf, "ttyS%d", bp->gnss_port);
2323 		ptp_ocp_link_child(bp, buf, "ttyGNSS");
2324 	}
2325 	if (bp->gnss2_port != -1) {
2326 		sprintf(buf, "ttyS%d", bp->gnss2_port);
2327 		ptp_ocp_link_child(bp, buf, "ttyGNSS2");
2328 	}
2329 	if (bp->mac_port != -1) {
2330 		sprintf(buf, "ttyS%d", bp->mac_port);
2331 		ptp_ocp_link_child(bp, buf, "ttyMAC");
2332 	}
2333 	if (bp->nmea_port != -1) {
2334 		sprintf(buf, "ttyS%d", bp->nmea_port);
2335 		ptp_ocp_link_child(bp, buf, "ttyNMEA");
2336 	}
2337 	sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
2338 	ptp_ocp_link_child(bp, buf, "ptp");
2339 
2340 	pps = pps_lookup_dev(bp->ptp);
2341 	if (pps)
2342 		ptp_ocp_symlink(bp, pps->dev, "pps");
2343 
2344 	if (device_add_groups(&bp->dev, timecard_groups))
2345 		pr_err("device add groups failed\n");
2346 
2347 	ptp_ocp_debugfs_add_device(bp);
2348 
2349 	return 0;
2350 }
2351 
2352 static void
2353 ptp_ocp_phc_info(struct ptp_ocp *bp)
2354 {
2355 	struct timespec64 ts;
2356 	u32 version, select;
2357 	bool sync;
2358 
2359 	version = ioread32(&bp->reg->version);
2360 	select = ioread32(&bp->reg->select);
2361 	dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
2362 		 version >> 24, (version >> 16) & 0xff, version & 0xffff,
2363 		 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
2364 		 ptp_clock_index(bp->ptp));
2365 
2366 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
2367 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
2368 		dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
2369 			 ts.tv_sec, ts.tv_nsec,
2370 			 sync ? "in-sync" : "UNSYNCED");
2371 }
2372 
2373 static void
2374 ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
2375 {
2376 	if (port != -1)
2377 		dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
2378 }
2379 
2380 static void
2381 ptp_ocp_info(struct ptp_ocp *bp)
2382 {
2383 	static int nmea_baud[] = {
2384 		1200, 2400, 4800, 9600, 19200, 38400,
2385 		57600, 115200, 230400, 460800, 921600,
2386 		1000000, 2000000
2387 	};
2388 	struct device *dev = &bp->pdev->dev;
2389 	u32 reg;
2390 
2391 	ptp_ocp_phc_info(bp);
2392 	if (bp->tod)
2393 		ptp_ocp_tod_info(bp);
2394 
2395 	if (bp->image) {
2396 		u32 ver = ioread32(&bp->image->version);
2397 
2398 		dev_info(dev, "version %x\n", ver);
2399 		if (ver & 0xffff)
2400 			dev_info(dev, "regular image, version %d\n",
2401 				 ver & 0xffff);
2402 		else
2403 			dev_info(dev, "golden image, version %d\n",
2404 				 ver >> 16);
2405 	}
2406 	ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200);
2407 	ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200);
2408 	ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600);
2409 	if (bp->nmea_out && bp->nmea_port != -1) {
2410 		int baud = -1;
2411 
2412 		reg = ioread32(&bp->nmea_out->uart_baud);
2413 		if (reg < ARRAY_SIZE(nmea_baud))
2414 			baud = nmea_baud[reg];
2415 		ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud);
2416 	}
2417 }
2418 
2419 static void
2420 ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
2421 {
2422 	struct device *dev = &bp->dev;
2423 
2424 	sysfs_remove_link(&dev->kobj, "ttyGNSS");
2425 	sysfs_remove_link(&dev->kobj, "ttyMAC");
2426 	sysfs_remove_link(&dev->kobj, "ptp");
2427 	sysfs_remove_link(&dev->kobj, "pps");
2428 	device_remove_groups(dev, timecard_groups);
2429 }
2430 
2431 static void
2432 ptp_ocp_detach(struct ptp_ocp *bp)
2433 {
2434 	ptp_ocp_debugfs_remove_device(bp);
2435 	ptp_ocp_detach_sysfs(bp);
2436 	if (timer_pending(&bp->watchdog))
2437 		del_timer_sync(&bp->watchdog);
2438 	if (bp->ts0)
2439 		ptp_ocp_unregister_ext(bp->ts0);
2440 	if (bp->ts1)
2441 		ptp_ocp_unregister_ext(bp->ts1);
2442 	if (bp->ts2)
2443 		ptp_ocp_unregister_ext(bp->ts2);
2444 	if (bp->pps)
2445 		ptp_ocp_unregister_ext(bp->pps);
2446 	if (bp->gnss_port != -1)
2447 		serial8250_unregister_port(bp->gnss_port);
2448 	if (bp->gnss2_port != -1)
2449 		serial8250_unregister_port(bp->gnss2_port);
2450 	if (bp->mac_port != -1)
2451 		serial8250_unregister_port(bp->mac_port);
2452 	if (bp->nmea_port != -1)
2453 		serial8250_unregister_port(bp->nmea_port);
2454 	if (bp->spi_flash)
2455 		platform_device_unregister(bp->spi_flash);
2456 	if (bp->i2c_ctrl)
2457 		platform_device_unregister(bp->i2c_ctrl);
2458 	if (bp->i2c_clk)
2459 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
2460 	if (bp->n_irqs)
2461 		pci_free_irq_vectors(bp->pdev);
2462 	if (bp->ptp)
2463 		ptp_clock_unregister(bp->ptp);
2464 	device_unregister(&bp->dev);
2465 }
2466 
2467 static int
2468 ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2469 {
2470 	struct devlink *devlink;
2471 	struct ptp_ocp *bp;
2472 	int err;
2473 
2474 	devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
2475 	if (!devlink) {
2476 		dev_err(&pdev->dev, "devlink_alloc failed\n");
2477 		return -ENOMEM;
2478 	}
2479 
2480 	err = pci_enable_device(pdev);
2481 	if (err) {
2482 		dev_err(&pdev->dev, "pci_enable_device\n");
2483 		goto out_unregister;
2484 	}
2485 
2486 	bp = devlink_priv(devlink);
2487 	err = ptp_ocp_device_init(bp, pdev);
2488 	if (err)
2489 		goto out_disable;
2490 
2491 	/* compat mode.
2492 	 * Older FPGA firmware only returns 2 irq's.
2493 	 * allow this - if not all of the IRQ's are returned, skip the
2494 	 * extra devices and just register the clock.
2495 	 */
2496 	err = pci_alloc_irq_vectors(pdev, 1, 11, PCI_IRQ_MSI | PCI_IRQ_MSIX);
2497 	if (err < 0) {
2498 		dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
2499 		goto out;
2500 	}
2501 	bp->n_irqs = err;
2502 	pci_set_master(pdev);
2503 
2504 	err = ptp_ocp_register_resources(bp, id->driver_data);
2505 	if (err)
2506 		goto out;
2507 
2508 	bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
2509 	if (IS_ERR(bp->ptp)) {
2510 		err = PTR_ERR(bp->ptp);
2511 		dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
2512 		bp->ptp = NULL;
2513 		goto out;
2514 	}
2515 
2516 	err = ptp_ocp_complete(bp);
2517 	if (err)
2518 		goto out;
2519 
2520 	ptp_ocp_info(bp);
2521 	devlink_register(devlink);
2522 	return 0;
2523 
2524 out:
2525 	ptp_ocp_detach(bp);
2526 	pci_set_drvdata(pdev, NULL);
2527 out_disable:
2528 	pci_disable_device(pdev);
2529 out_unregister:
2530 	devlink_free(devlink);
2531 	return err;
2532 }
2533 
2534 static void
2535 ptp_ocp_remove(struct pci_dev *pdev)
2536 {
2537 	struct ptp_ocp *bp = pci_get_drvdata(pdev);
2538 	struct devlink *devlink = priv_to_devlink(bp);
2539 
2540 	devlink_unregister(devlink);
2541 	ptp_ocp_detach(bp);
2542 	pci_set_drvdata(pdev, NULL);
2543 	pci_disable_device(pdev);
2544 
2545 	devlink_free(devlink);
2546 }
2547 
2548 static struct pci_driver ptp_ocp_driver = {
2549 	.name		= KBUILD_MODNAME,
2550 	.id_table	= ptp_ocp_pcidev_id,
2551 	.probe		= ptp_ocp_probe,
2552 	.remove		= ptp_ocp_remove,
2553 };
2554 
2555 static int
2556 ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
2557 			  unsigned long action, void *data)
2558 {
2559 	struct device *dev, *child = data;
2560 	struct ptp_ocp *bp;
2561 	bool add;
2562 
2563 	switch (action) {
2564 	case BUS_NOTIFY_ADD_DEVICE:
2565 	case BUS_NOTIFY_DEL_DEVICE:
2566 		add = action == BUS_NOTIFY_ADD_DEVICE;
2567 		break;
2568 	default:
2569 		return 0;
2570 	}
2571 
2572 	if (!i2c_verify_adapter(child))
2573 		return 0;
2574 
2575 	dev = child;
2576 	while ((dev = dev->parent))
2577 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
2578 			goto found;
2579 	return 0;
2580 
2581 found:
2582 	bp = dev_get_drvdata(dev);
2583 	if (add)
2584 		ptp_ocp_symlink(bp, child, "i2c");
2585 	else
2586 		sysfs_remove_link(&bp->dev.kobj, "i2c");
2587 
2588 	return 0;
2589 }
2590 
2591 static struct notifier_block ptp_ocp_i2c_notifier = {
2592 	.notifier_call = ptp_ocp_i2c_notifier_call,
2593 };
2594 
2595 static int __init
2596 ptp_ocp_init(void)
2597 {
2598 	const char *what;
2599 	int err;
2600 
2601 	ptp_ocp_debugfs_init();
2602 
2603 	what = "timecard class";
2604 	err = class_register(&timecard_class);
2605 	if (err)
2606 		goto out;
2607 
2608 	what = "i2c notifier";
2609 	err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
2610 	if (err)
2611 		goto out_notifier;
2612 
2613 	what = "ptp_ocp driver";
2614 	err = pci_register_driver(&ptp_ocp_driver);
2615 	if (err)
2616 		goto out_register;
2617 
2618 	return 0;
2619 
2620 out_register:
2621 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
2622 out_notifier:
2623 	class_unregister(&timecard_class);
2624 out:
2625 	ptp_ocp_debugfs_fini();
2626 	pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
2627 	return err;
2628 }
2629 
2630 static void __exit
2631 ptp_ocp_fini(void)
2632 {
2633 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
2634 	pci_unregister_driver(&ptp_ocp_driver);
2635 	class_unregister(&timecard_class);
2636 	ptp_ocp_debugfs_fini();
2637 }
2638 
2639 module_init(ptp_ocp_init);
2640 module_exit(ptp_ocp_fini);
2641 
2642 MODULE_DESCRIPTION("OpenCompute TimeCard driver");
2643 MODULE_LICENSE("GPL v2");
2644