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