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